"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(governance): verify the whole history before sealing a ledger segment
Rudy Celekli8079de9200e3
diff --git a/agent/src/governance/ledger.py b/agent/src/governance/ledger.pyindex 41d1a6600..03bfc31ba 100644--- a/agent/src/governance/ledger.py+++ b/agent/src/governance/ledger.py@@ -642,15 +642,21 @@ def rotate_if_needed(Raises:ValueError: If ``max_bytes`` is not positive.- LedgerCorruptionError: If the active chain is broken -- a corrupt- ledger is sealed by nobody; fix or quarantine it deliberately.+ LedgerCorruptionError: If the history is broken, sealed segments+ included -- a corrupt ledger is sealed by nobody; fix or+ quarantine it deliberately."""if max_bytes <= 0:raise ValueError(f"max_bytes must be positive, got {max_bytes}")if not path.exists() or path.stat().st_size < max_bytes:return None- result = verify_chain(path)+ # The whole history, not just the active file: after an earlier rotation+ # the active file legitimately starts mid-chain (its first record carries+ # the sealed segment's seq+1 and prev_record_hash), so a single-file walk+ # reads that healthy continuation as a seq_gap and would refuse every+ # rotation after the first.+ result = verify_chain_with_archives(path)if not result.ok:raise LedgerCorruptionError(result.first_break)diff --git a/agent/tests/test_governance.py b/agent/tests/test_governance.pyindex c9dd44030..546ea4dab 100644--- a/agent/tests/test_governance.py+++ b/agent/tests/test_governance.py@@ -720,6 +720,35 @@ def test_the_chain_continues_across_a_rotation(tmp_path):assert result.record_count == 9+def test_a_second_rotation_is_not_refused_as_corruption(tmp_path):+ # Rotation guards itself by verifying the chain first, which is right --+ # but the active file after the FIRST rotation legitimately starts mid+ # chain (seq 6 above), so verifying that file alone reports a seq_gap and+ # a healthy ledger can never be sealed twice. The guard has to ask the+ # same question the module's own whole-history verifier asks.+ from src.governance.ledger import (+ archive_segments,+ rotate_if_needed,+ verify_chain_with_archives,+ )++ led = tmp_path / "audit_chain.jsonl"+ for i in range(5):+ append_record(led, {"action": "order", "n": i})+ first = rotate_if_needed(led, max_bytes=100)+ for i in range(5, 10):+ append_record(led, {"action": "order", "n": i})++ assert verify_chain_with_archives(led).ok is True # nothing is wrong here++ second = rotate_if_needed(led, max_bytes=100)++ assert second is not None+ assert second != first+ assert archive_segments(led) == [first, second]+ assert verify_chain_with_archives(led).ok is True++def test_deleting_a_whole_sealed_segment_is_detected(tmp_path):# This is what rotation would have cost us if the chain restarted: a# segment could be removed wholesale and each remaining file would still