Skip to content

Commit 9e9aada

Browse files
authored
Cover C1 first-read recovery after a native journal write failure (#9107)
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
1 parent 0da8dbd commit 9e9aada

1 file changed

Lines changed: 60 additions & 16 deletions

File tree

‎tests/control_plane/test_context_first_read.py‎

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,10 @@ def capture(turn):
22162216
assert facts[goal_text] == narrative
22172217

22182218
@pytest.mark.parametrize("provider", ["file", "sqlite"])
2219-
def test_native_first_read_reuses_only_observed_write_scope(tmp_path, monkeypatch, provider):
2219+
@pytest.mark.parametrize("invalidation", ["compact", "journal_write"])
2220+
def test_native_first_read_reuses_only_observed_write_scope(tmp_path, monkeypatch, provider, invalidation):
2221+
import errno
2222+
22202223
from test_codex_context_lifecycle import deliver, setup
22212224
from test_quota_settlement_cli import (
22222225
AGENT_ID, GOAL_ID, TODO_ID, _heartbeat_receipt_count,
@@ -2259,29 +2262,35 @@ def capture(turn, registry_path=registry):
22592262
proofs[turn] = source["interaction_contract"]["cli_channel"]["execution_preparation"]["lease_proof"]
22602263
return view
22612264

2262-
def settle(turn, registry_path=registry):
2265+
def writeback(turn, registry_path):
22632266
lease = cli("task-lease", "inspect", "--goal-id", GOAL_ID, "--todo-id", TODO_ID,
22642267
registry_path=registry_path)["lease"]
2265-
cli("refresh-state", *binding(turn), "--classification", "fixture_validated",
2266-
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
2267-
"--delivery-boundary", "in_flight_continuation", "--delivery-workspace-path", str(project),
2268-
"--task-lease-idempotency-key", lease["idempotency_key"],
2269-
"--task-lease-expected-version", str(lease["version"]),
2270-
"--no-global-sync", "--suppress-external-sinks", registry_path=registry_path)
2268+
return ("refresh-state", *binding(turn), "--classification", "fixture_validated",
2269+
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
2270+
"--delivery-boundary", "in_flight_continuation", "--delivery-workspace-path", str(project),
2271+
"--task-lease-idempotency-key", lease["idempotency_key"],
2272+
"--task-lease-expected-version", str(lease["version"]),
2273+
"--no-global-sync", "--suppress-external-sinks")
2274+
2275+
def settle(turn, registry_path=registry):
2276+
cli(*writeback(turn, registry_path), registry_path=registry_path)
22712277
spent = cli("quota", "spend-slot", *binding(turn), "--slots", "1", "--source", "heartbeat", "--execute",
22722278
registry_path=registry_path)
22732279
assert spent["appended"]
22742280

2281+
def read_page(command):
2282+
words = shlex.split(command)
2283+
result = subprocess.run([sys.executable, "-m", "loopx.entrypoint", "--format", "json",
2284+
*words[words.index("quota"):]], cwd=project, capture_output=True)
2285+
return result.returncode, json.loads(result.stdout), len(result.stdout)
2286+
22752287
def read(command):
22762288
pages, size, offset = [], 0, 0
22772289
while command:
2278-
words = shlex.split(command)
2279-
stdout = subprocess.check_output([sys.executable, "-m", "loopx.entrypoint", "--format", "json",
2280-
*words[words.index("quota"):]], cwd=project)
2281-
page = json.loads(stdout)
2282-
assert page["ok"] and page["offset"] == offset, page
2290+
code, page, output_bytes = read_page(command)
2291+
assert code == 0 and page["ok"] and page["offset"] == offset, page
22832292
pages.append(page)
2284-
size += len(stdout)
2293+
size += output_bytes
22852294
offset += len(page["content"].encode())
22862295
command = page.get("next_command")
22872296
assert pages[-1]["eof"] and offset == pages[-1]["total_bytes"]
@@ -2319,12 +2328,47 @@ def read_initial(view):
23192328
warm, warm_facts, warm_bytes = read_initial(current)
23202329
_, restored_scope, scope_bytes = read(current["recovery"]["command"] + " --select " + scope_path)
23212330
assert restored_scope[scope_path] == scopes
2322-
codex_context.codex_context_hook(str(journal), {**event, "hook_event_name": "PostCompact"})
2331+
recovered_values = {}
2332+
if invalidation == "journal_write":
2333+
partial = next(page for page in warm if page["offset"] == 0 and page.get("next_command")
2334+
and scope_path in page.get("previously_delivered_fields", []))
2335+
failed_writes = []
2336+
2337+
def exhausted(*_args):
2338+
failed_writes.append(True)
2339+
raise OSError(errno.ENOSPC, "synthetic native journal storage exhaustion")
2340+
2341+
with monkeypatch.context() as failing_journal:
2342+
failing_journal.setattr(codex_context, "_save", exhausted)
2343+
deliver(journal, event, [partial])
2344+
assert len(failed_writes) == 1 and not (journal / "session.json").exists()
2345+
code, refused, _ = read_page(partial["next_command"])
2346+
assert code != 0 and refused["ok"] is False and refused.get("recovery_command"), refused
2347+
recovered, recovered_values, _ = read(refused["recovery_command"])
2348+
assert recovered_values[scope_path] == scopes
2349+
assert not recovered[0].get("previously_delivered_fields")
2350+
_, fallback_ref = read_context_read_reference(recovered[0]["reference_file"])
2351+
assert fallback_ref["path"] == source_ref["path"] and fallback_ref["sha256"] == source_ref["sha256"]
2352+
else:
2353+
codex_context.codex_context_hook(str(journal), {**event, "hook_event_name": "PostCompact"})
23232354
restored, restored_facts, restored_bytes = read_initial(current)
23242355
assert scope_path not in restored[0].get("previously_delivered_fields", [])
23252356
assert restored_facts[scope_path] == scopes and original.read_bytes() == raw
2357+
assert all(restored_facts[key] == value for key, value in recovered_values.items())
23262358
deliver(journal, event, restored)
2327-
settle("scope-warm")
2359+
if invalidation == "journal_write":
2360+
assert _heartbeat_receipt_count(runtime, "scope-warm") == 1 and _spend_run_count(runtime) == 2
2361+
command = (*writeback("scope-warm", registry), "--settle-turn", "--settlement-receipt")
2362+
settled = cli(*command)["settlement_result"]
2363+
assert settled["ok"]
2364+
effect = json.loads(raw)["interaction_contract"]["cli_channel"]["settlement_plan"]["identity"]["effect_id"]
2365+
assert [(receipt["step_kind"], receipt["effect_id"]) for receipt in settled["receipts"]] == [
2366+
(kind, effect) for kind in ("validation", "durable_writeback", "quota_spend")
2367+
]
2368+
assert cli(*command)["settlement_result"]["receipts"] == settled["receipts"]
2369+
assert _heartbeat_receipt_count(runtime, "scope-warm") == 1 and _spend_run_count(runtime) == 3
2370+
else:
2371+
settle("scope-warm")
23282372

23292373
alternate_registry = registry.with_name("alternate-registry.json")
23302374
alternate_registry.write_bytes(registry.read_bytes())

0 commit comments

Comments
 (0)