Files
ai-agent/backend/test_tailcopy.py
gabrielvidalandClaude Opus 5.5 be24545e21 perf(indexer): copy only a grown transcript's new bytes into the archive
A live transcript is re-imported every couple of seconds and shutil.copy2
rewrote all of it each time (100 MB for one new line on the biggest). Every
writer of the source dirs appends, so tailcopy.sync appends the bytes past the
archive copy's end when the 4 KB before it still match, and falls back to a
full copy when the source shrank, was rewritten, or only its mtime moved. An
flock on the destination keeps the watcher, the indexer's sweep and a
blue-green standby (two backends share the archive) from duplicating a tail.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-26 23:03:24 +01:00

94 lines
3.0 KiB
Python

"""tailcopy.sync: a grown transcript gets only its new bytes, anything else a
full copy, and two syncs racing on one file never duplicate the tail."""
import os
import pathlib
import tempfile
import threading
import unittest
import tailcopy
class TailCopy(unittest.TestCase):
def setUp(self):
d = pathlib.Path(tempfile.mkdtemp())
self.src, self.dst = d / "s.jsonl", d / "d.jsonl"
self.src.write_bytes(b"".join(b'{"n": %d}\n' % i for i in range(2000)))
def same(self):
self.assertEqual(self.dst.read_bytes(), self.src.read_bytes())
self.assertEqual(self.dst.stat().st_mtime_ns, self.src.stat().st_mtime_ns)
def test_a_new_file_is_copied_whole(self):
tailcopy.sync(self.src, self.dst)
self.same()
def test_growth_appends_only_the_tail(self):
tailcopy.sync(self.src, self.dst)
ino = self.dst.stat().st_ino
with open(self.dst, "r+b") as fh: # mark a byte the append must not touch
fh.write(b"#")
with self.src.open("ab") as fh:
fh.write(b'{"n": "more"}\n')
tailcopy.sync(self.src, self.dst)
out = self.dst.read_bytes()
self.assertEqual(out[:1], b"#")
self.assertEqual(out[1:], self.src.read_bytes()[1:])
self.assertEqual(self.dst.stat().st_ino, ino)
def test_a_rewrite_is_copied_whole(self):
tailcopy.sync(self.src, self.dst)
self.src.write_bytes(b"rewritten\n" * 3000) # longer, different bytes
tailcopy.sync(self.src, self.dst)
self.same()
def test_a_shrunk_source_is_copied_whole(self):
tailcopy.sync(self.src, self.dst)
self.src.write_bytes(b'{"n": 0}\n')
tailcopy.sync(self.src, self.dst)
self.same()
def test_same_size_but_newer_is_copied_whole(self):
tailcopy.sync(self.src, self.dst)
data = bytearray(self.src.read_bytes())
data[0:1] = b"["
self.src.write_bytes(bytes(data))
tailcopy.sync(self.src, self.dst)
self.same()
def test_racing_syncs_never_duplicate_the_tail(self):
tailcopy.sync(self.src, self.dst)
stop = threading.Event()
def grow():
for i in range(300):
with self.src.open("ab") as fh:
fh.write(b'{"g": %d}\n' % i)
stop.set()
def sync():
while not stop.is_set():
tailcopy.sync(self.src, self.dst)
ts = [threading.Thread(target=grow)] + [threading.Thread(target=sync) for _ in range(3)]
for t in ts:
t.start()
for t in ts:
t.join()
tailcopy.sync(self.src, self.dst)
self.same()
def test_the_lock_is_released(self):
tailcopy.sync(self.src, self.dst)
import fcntl
fd = os.open(self.dst, os.O_RDWR)
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) # raises if still held
finally:
os.close(fd)
if __name__ == "__main__":
unittest.main()