From 9fcafc290c5b5ee9cb83c4c3b2ff600f75210c8e Mon Sep 17 00:00:00 2001 From: Andraxion Date: Sat, 25 Jul 2026 20:00:21 -0400 Subject: [PATCH] Avoid rewriting warm extraction cache --- src/docforge/adapter_contract.py | 36 ++++++++++++++++---------------- tests/test_adapter_contract.py | 3 +++ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/docforge/adapter_contract.py b/src/docforge/adapter_contract.py index 0c24f35..a4b6bab 100644 --- a/src/docforge/adapter_contract.py +++ b/src/docforge/adapter_contract.py @@ -483,14 +483,7 @@ class AdapterProject: if source.source_id in invalidated: contribution = loader.extract_source(source) reparsed.append(source.source_id) - else: - record = cached[source.source_id] - contribution = source_projection(record.payload) - hits.append(source.source_id) - validate_source_projection(source, contribution) - contributions.append(contribution) - cache_records.append( - CachedSource( + cache_record = CachedSource( source_id=source.source_id, source_path=source.source_path, fingerprint=source.fingerprint, @@ -498,7 +491,13 @@ class AdapterProject: dependencies=source.dependencies, payload=source_payload(contribution), ) - ) + else: + cache_record = cached[source.source_id] + contribution = source_projection(cache_record.payload) + hits.append(source.source_id) + validate_source_projection(source, contribution) + contributions.append(contribution) + cache_records.append(cache_record) projection = AdapterProjection( project_id=manifest.project_id, title=manifest.title, @@ -542,15 +541,16 @@ class AdapterProject: raise DocForgeError( "source_changed", "Adapter sources changed during incremental extraction" ) - write_extraction_cache( - self._cache_path, - ExtractionCache( - project_id=manifest.project_id, - adapter_id=manifest.adapter_id, - adapter_version=manifest.adapter_version, - sources=tuple(cache_records), - ), - ) + if cache is None or invalidated or deleted: + write_extraction_cache( + self._cache_path, + ExtractionCache( + project_id=manifest.project_id, + adapter_id=manifest.adapter_id, + adapter_version=manifest.adapter_version, + sources=tuple(cache_records), + ), + ) self._last_logic = logic_projections self._last_build_report = { "mode": "incremental", diff --git a/tests/test_adapter_contract.py b/tests/test_adapter_contract.py index 99355a8..69815ca 100644 --- a/tests/test_adapter_contract.py +++ b/tests/test_adapter_contract.py @@ -303,11 +303,14 @@ class AdapterContractTests(unittest.TestCase): self.assertEqual(2, first["build"]["reparsed_sources"]) self.assertEqual(0, first["build"]["cache_hits"]) loader.extract_calls.clear() + cache_path = root / ".cache" / "incremental" / "extractions.json" + cache_modified = cache_path.stat().st_mtime_ns second = index.build() self.assertEqual([], loader.extract_calls) self.assertEqual(0, second["build"]["reparsed_sources"]) self.assertEqual(2, second["build"]["cache_hits"]) + self.assertEqual(cache_modified, cache_path.stat().st_mtime_ns) loader.sources["guide.foundation"] = "Changed foundation." loader.extract_calls.clear()