Skip to content

Commit 38e3576

Browse files
Pigbibicodex
andcommitted
fix: scope ledger history safety checks
Co-Authored-By: Codex <noreply@openai.com>
1 parent af40391 commit 38e3576

4 files changed

Lines changed: 11 additions & 8 deletions

File tree

service/ai_gateway_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ def _automation_control_snapshot(
556556
)
557557
failure_history_complete = (
558558
not ledger_unavailable
559-
and not bool(retention.get("history_completeness_unknown"))
559+
and (not bool(retention.get("history_completeness_unknown")) or repo_history_has_terminal_boundary)
560560
and (repo_evictions <= 0 or repo_history_has_terminal_boundary)
561561
)
562562
execution = decide_automation_execution(

service/automation_run_ledger.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ def _persist_with_owner_guard_locked(
351351
self._storage_file_seen = True
352352
if not disk_read_ok:
353353
self._history_completeness_unknown = True
354-
self._evict_old_runs_locked()
355-
return
354+
raise OSError("automation ledger could not be refreshed from disk")
356355
if guard_run_id:
357356
disk_entry = disk_runs.get(guard_run_id)
358357
disk_owner = _entry_owner_repository(disk_entry) if isinstance(disk_entry, dict) else ""

tests/test_ai_gateway_automation_control.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,20 @@ def test_control_snapshot_fails_closed_after_ledger_eviction(self) -> None:
228228
self.assertFalse(control["execution"]["failure_history_complete"])
229229
self.assertEqual(control["execution"]["consecutive_failures"], 1)
230230

231-
def test_control_snapshot_ignores_other_repo_ledger_eviction(self) -> None:
231+
def test_control_snapshot_allows_known_repo_boundary_when_history_unknown(self) -> None:
232232
health = type("Health", (), {"status": "healthy"})()
233233
quota = type("Quota", (), {"runtime_status": lambda self, repo: {"status": "ok"}})()
234234
ledger = type(
235235
"Ledger",
236236
(),
237237
{
238238
"snapshot": lambda self, limit=None: {
239-
"runs": [],
239+
"runs": [
240+
{"run_id": "merged-1", "task_state": "merged", "metadata": {"source_repository": "QuantStrategyLab/TargetRepo"}}
241+
],
240242
"summary": {
241243
"retention": {
244+
"history_completeness_unknown": True,
242245
"may_be_truncated": True,
243246
"evicted_runs": 1,
244247
"evicted_runs_by_repo": {"quantstrategylab/otherrepo": 1},

tests/test_automation_run_ledger.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,19 @@ def test_corrupt_persisted_ledger_marks_history_completeness_unknown(self) -> No
320320

321321
self.assertTrue(snapshot["summary"]["retention"]["history_completeness_unknown"])
322322

323-
def test_corrupt_disk_read_preserves_local_runs_without_overwriting_file(self) -> None:
323+
def test_corrupt_disk_read_fails_record_without_overwriting_file(self) -> None:
324324
with TemporaryDirectory() as tmp:
325325
path = Path(tmp) / "automation_runs.json"
326326
ledger = AutomationRunLedger(max_runs=3, storage_path=path)
327327
ledger.record("run-1", "queued")
328328
path.write_text("{not-json", encoding="utf-8")
329-
ledger.record("run-2", "queued")
329+
with self.assertRaises(OSError):
330+
ledger.record("run-2", "queued")
330331
snapshot = ledger.snapshot(limit=None)
331332

332333
self.assertEqual(path.read_text(encoding="utf-8"), "{not-json")
333334

334-
self.assertEqual({run["run_id"] for run in snapshot["runs"]}, {"run-1", "run-2"})
335+
self.assertEqual({run["run_id"] for run in snapshot["runs"]}, {"run-1"})
335336
self.assertTrue(snapshot["summary"]["retention"]["history_completeness_unknown"])
336337

337338
def test_update_preserves_control_fields_when_omitted(self) -> None:

0 commit comments

Comments
 (0)