test: harden TCS root freshness and ZCode entry

This commit is contained in:
冰朔 2026-09-08 13:50:03 +08:00
commit c78e90e1e4
4 changed files with 47 additions and 1 deletions

View file

@ -165,6 +165,11 @@ def refresh(repo: Path, output: Path, pointer: Path, trigger: str) -> dict[str,
snapshot = build(repo)
current_path = output / "CURRENT.json"
previous = json.loads(current_path.read_text()) if current_path.is_file() else None
if previous:
previous_for_hash = dict(previous)
previous_token = previous_for_hash.pop("freshness_token", None)
if previous_token != digest(stable(previous_for_hash)):
raise RootError("PREVIOUS_CURRENT_FRESHNESS_TOKEN_MISMATCH")
if previous and previous.get("source_commit") == snapshot["source_commit"] and previous.get("freshness_token") == snapshot["freshness_token"]:
return {"outcome": "PASS", "state": "CURRENT_IDEMPOTENT", "source_commit": snapshot["source_commit"], "current_sha256": digest(current_path.read_bytes())}
if previous:

View file

@ -36,6 +36,23 @@ class RootAgentTest(unittest.TestCase):
again = MODULE.refresh(ROOT, output, pointer, 'test')
self.assertEqual(again['state'], 'CURRENT_IDEMPOTENT')
def test_unadmitted_numbered_source_drift_preserves_previous_current(self):
with tempfile.TemporaryDirectory() as temp:
base = Path(temp)
output, pointer = base / 'state', base / 'TCS-ROOT.json'
snapshot = MODULE.build(ROOT)
snapshot['registered_source_hashes']['identity/fifth-domain-subject-registry.json'] = '0' * 64
previous_for_hash = dict(snapshot)
previous_for_hash.pop('freshness_token')
snapshot['freshness_token'] = MODULE.digest(MODULE.stable(previous_for_hash))
output.mkdir(parents=True)
current = output / 'CURRENT.json'
current.write_text(json.dumps(snapshot), encoding='utf-8')
before = current.read_bytes()
with self.assertRaisesRegex(MODULE.RootError, 'UNADMITTED_NUMBERED_SOURCE_DRIFT'):
MODULE.refresh(ROOT, output, pointer, 'negative-test')
self.assertEqual(current.read_bytes(), before)
if __name__ == '__main__':
unittest.main()