From ff63200a0ff9bc4a195523af51c3aa82cb753bd8 Mon Sep 17 00:00:00 2001 From: princepride Date: Mon, 2 Nov 2026 14:31:01 +0000 Subject: [PATCH] [perf] storage: cache a long project's whole commit history ## summary - Every project read validates the full commit chain, and each record holds a full project snapshot. The record cache kept at most 128 records or 8 MiB, so a longer history was reparsed from disk on every read. A 317-commit project (55 MB of records) took 3-13 s per snapshot on network storage. - Those reads held the catalog lock for as long, so session heartbeats and job checkpoints timed out: edit sessions expired and running generations were interrupted at submit, poll, download or save. - The cache now holds up to 4096 records or 512 MiB. The same project reads in about 0.05 s. Changed, replaced or missing records are still detected by their file signature before a cached record is reused. ## validation - `.venv/bin/python -m pytest tests -q`: 614 passed. The new `test_long_history_is_validated_from_disk_only_once` fails without this change. - `npm test`: 423 passed. Co-Authored-By: Claude Opus 5.5 --- .../infrastructure/creation_projects.py | 10 +++++++++- tests/test_creation_storage.py | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/core/src/opendirector/infrastructure/creation_projects.py b/packages/core/src/opendirector/infrastructure/creation_projects.py index c64a382e7..89e33af80 100644 --- a/packages/core/src/opendirector/infrastructure/creation_projects.py +++ b/packages/core/src/opendirector/infrastructure/creation_projects.py @@ -151,6 +151,14 @@ def object_changes(before, after, command): return changed +# Every read validates the whole commit chain, and each record holds a full +# project snapshot. The cache must fit a long project's history: below that, +# each read reparses every record from disk and holds the catalog lock long +# enough to expire sessions and interrupt running jobs. +RECORD_CACHE_ENTRIES = 4096 +RECORD_CACHE_BYTES = 512 * 1024 * 1024 + + class CreationProjects: """Authoritative repository for lifecycle and shared creation commands. @@ -247,7 +255,7 @@ def signature(): if os.name != 'nt' and before[2] <= 8 * 1024 * 1024: self._records_cache[file] = (before, record) self._records_cache_bytes += before[2] - while len(self._records_cache) > 128 or self._records_cache_bytes > 8 * 1024 * 1024: + while len(self._records_cache) > RECORD_CACHE_ENTRIES or self._records_cache_bytes > RECORD_CACHE_BYTES: _, removed = self._records_cache.popitem(last=False) self._records_cache_bytes -= removed[0][2] return record diff --git a/tests/test_creation_storage.py b/tests/test_creation_storage.py index 35a6e8353..d0fa23945 100644 --- a/tests/test_creation_storage.py +++ b/tests/test_creation_storage.py @@ -332,6 +332,26 @@ def reading(path): projects.snapshot(created.projectId) +@pytest.mark.skipif(os.name == 'nt', reason='Commit signature caching requires POSIX change time') +def test_long_history_is_validated_from_disk_only_once(projects, monkeypatch): + # Each read validates the whole chain; a cache smaller than the history + # would reparse every record on every read of a long project. + from opendirector.infrastructure import creation_projects as module + created = create(projects) + for revision in range(1, 200): + assert projects.execute(rename(created, f'Renamed {revision}', revision)).status == 'applied' + projects.snapshot(created.projectId) + reads = [] + original = module.read_json + def reading(path): + if path.parent.name == 'commits': + reads.append(path) + return original(path) + monkeypatch.setattr(module, 'read_json', reading) + assert projects.snapshot(created.projectId).project.name == 'Renamed 199' + assert reads == [] + + def test_returned_snapshots_and_history_cannot_mutate_record_cache(projects): created = create(projects) session = str(uuid4())