From 12145f85774bddf5bc06df9f05aebb02280ecce3 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Sat, 21 Nov 2026 09:29:53 +0800 Subject: [PATCH] fix(control-plane): stabilize v2.4.0 explore, context, and checkpoint tail slice (Slice B4) Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../control_plane/projects/registry_codec.py | 3 +- .../quota/accounting_artifact_transaction.ts | 29 +++++++- ...t_context_capture_registry_independence.py | 7 +- .../control_plane/test_context_first_read.py | 13 +++- .../test_declared_terminal_settlement_cli.py | 4 +- .../test_dependency_dispatch_cli.py | 4 +- .../test_explore_capture_identity.py | 3 + .../test_explore_durable_links.py | 3 + .../test_explore_result_writeback.py | 66 ++++++++++++++----- .../test_explore_settlement_attachment.py | 17 +++-- .../test_goal_create_completion.py | 5 +- .../test_goal_outcome_diagnostics.py | 3 +- .../test_material_feedback_checkpoint.py | 25 +++++-- .../test_model_behavior_qualification.py | 8 ++- .../control_plane/test_open_outcome_replan.py | 6 +- .../test_quota_vision_audit_references.py | 24 ++++++- .../test_research_checkpoint_capture.py | 17 ++++- .../test_research_checkpoint_cli.py | 3 +- .../test_settlement_input_discovery_cli.py | 28 +++++--- .../test_work_acceptance_context.py | 19 ++++-- .../control_plane/test_work_yardstick_cli.py | 10 +-- .../quota_spend_commit.test.ts | 65 ++++++++++++++++++ 22 files changed, 299 insertions(+), 63 deletions(-) diff --git a/loopx/control_plane/projects/registry_codec.py b/loopx/control_plane/projects/registry_codec.py index f4a86be3af..15442a1d10 100644 --- a/loopx/control_plane/projects/registry_codec.py +++ b/loopx/control_plane/projects/registry_codec.py @@ -14,7 +14,6 @@ from ..content_digest import ENVELOPED_SHA256_PATTERN from ..goals.activation import require_current_registry_goal_activation -from ..runtime.document_io import fsync_state_directory from ..runtime.private_permissions import protect_private_path, require_private_path from ...file_lock import exclusive_cross_runtime_file_lock from ...paths import GLOBAL_REGISTRY_FILENAME @@ -437,6 +436,8 @@ def backup_preimage(self) -> Path: protect_private_path(directory, directory=True) target = directory / f"{self._path.name}.{hashlib.sha256(raw).hexdigest()}.bak" if not target.exists(): + from ..runtime.document_io import fsync_state_directory + with target.open("xb") as stream: protect_private_path(target) stream.write(raw) diff --git a/loopx/control_plane/quota/accounting_artifact_transaction.ts b/loopx/control_plane/quota/accounting_artifact_transaction.ts index e70a5c7bb9..51a8f7b914 100644 --- a/loopx/control_plane/quota/accounting_artifact_transaction.ts +++ b/loopx/control_plane/quota/accounting_artifact_transaction.ts @@ -538,16 +538,41 @@ export async function readQuotaSpendEventFromRun( return inline; } if (typeof run.json_path !== "string" || !run.json_path.trim()) return null; + const runsDirReal = await realpath(runsDir); + const rawPath = run.json_path.trim(); + const relocatedCandidate = (() => { + if (!isAbsolute(rawPath)) return null; + const normalized = resolve(rawPath); + const fileName = basename(normalized); + const runsParent = dirname(normalized); + const goalDir = dirname(runsParent); + const goalsDir = dirname(goalDir); + if ( + !fileName || fileName === "." || fileName === ".." || extname(fileName) !== ".json" || + basename(runsParent) !== "runs" || basename(goalDir) !== goalId || basename(goalsDir) !== "goals" + ) { + return null; + } + return join(runsDirReal, fileName); + })(); + const initialRelative = relative(runsDirReal, resolve(rawPath)); + const staysInsideInitial = Boolean( + initialRelative && initialRelative !== ".." && + !initialRelative.startsWith(`..${sep}`) && !isAbsolute(initialRelative) + ); + const candidatePath = staysInsideInitial || relocatedCandidate === null + ? resolve(rawPath) + : relocatedCandidate; let targetPath: string; try { - targetPath = await realpath(resolve(run.json_path.trim())); + targetPath = await realpath(candidatePath); } catch (error) { if (error instanceof Error && "code" in error && error.code === "ENOENT") { return null; } throw error; } - const relativePath = relative(await realpath(runsDir), targetPath); + const relativePath = relative(runsDirReal, targetPath); if ( !relativePath || relativePath === ".." || diff --git a/tests/control_plane/test_context_capture_registry_independence.py b/tests/control_plane/test_context_capture_registry_independence.py index afd04c2d54..e516ae47f3 100644 --- a/tests/control_plane/test_context_capture_registry_independence.py +++ b/tests/control_plane/test_context_capture_registry_independence.py @@ -209,6 +209,9 @@ def test_replan_stages_read_original_authority_capture_without_registry( assert page["next_offset"] == len(observed) command = page["next_command"] guard = json.loads(observed) + from loopx.control_plane.quota.turn_envelope import _turn_envelope_result + assert "review" not in guard["replan_action_packet"] + staged = _turn_envelope_result("replan_cli_view", payload=source)["replan_action_packet"] missing = tmp_path / "unrelated-missing-registry.json" def snapshot(): @@ -228,7 +231,7 @@ def read(command): assert value["obligation_id"] == source["replan_action_packet"]["obligation_id"] return value - review = read(guard["replan_action_packet"]["review"]) + review = read(staged["review"]) writer = read(review["next"]["writeback"]) assert writer["context_id"] == review["context_id"] assert writer["source_hash"] == review["source_hash"] @@ -237,7 +240,7 @@ def read(command): assert len(index["outcomes"]) == 1 acceptance = read(index["read_action_template"].replace("{outcome_ref}", index["outcomes"][0]["outcome_ref"])) assert acceptance["outcome"]["requirements"][0]["description"] == "Stop before publication." - explore = read(guard["replan_action_packet"]["review"].replace("--replan-stage review", "--replan-stage explore")) + explore = read(staged["review"].replace("--replan-stage review", "--replan-stage explore")) assert explore["explore_basis"] == source["autonomous_replan_obligation"]["replan_context"].get("explore_basis") assert snapshot() == before, "captured readers cannot admit, settle, repair routes or rewrite authority" assert not missing.exists() diff --git a/tests/control_plane/test_context_first_read.py b/tests/control_plane/test_context_first_read.py index c511cbf7a1..5bc6202bc9 100644 --- a/tests/control_plane/test_context_first_read.py +++ b/tests/control_plane/test_context_first_read.py @@ -758,7 +758,8 @@ def read(command): if warm: prior = capture("wide-prior") _, prior_ref = read_context_read_reference(prior["recovery"]["reference_file"]) - rows = json.loads(Path(prior_ref["path"]).read_bytes())["interaction_contract"]["agent_channel"]["work_context"]["sources"] + prior_decision = json.loads(Path(prior_ref["path"]).read_bytes()) + rows = prior_decision["interaction_contract"]["agent_channel"]["work_context"]["sources"] index = next(i for i, source in enumerate(rows) if source["source"] == "goal_state") body_pointer = f"/interaction_contract/agent_channel/work_context/sources/{index}/content/text" prior_read = prior["recovery"]["command"] + " --select " + body_pointer @@ -769,8 +770,11 @@ def read(command): if requested_scopes: assert prior_facts["/goal_boundary/write_scope"] == scopes deliver(journal, event, prior_pages) + proof = prior_decision["execution_preparation"]["lease_proof"] cli("refresh-state", *binding, "--turn-instance-id", "wide-prior", "--classification", "fixture_verified", "--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress", + "--task-lease-idempotency-key", proof["idempotency_key"], + "--task-lease-expected-version", str(proof["expected_version"]), "--delivery-boundary", "in_flight_continuation", "--no-global-sync", "--suppress-external-sinks") cli("quota", "spend-slot", *binding, "--turn-instance-id", "wide-prior", "--slots", "1", "--source", "heartbeat", "--execute") @@ -2097,9 +2101,13 @@ def capture(turn, registry_path=registry): registry_path=registry_path) def settle(turn, registry_path=registry): + lease = cli("task-lease", "inspect", "--goal-id", GOAL_ID, "--todo-id", TODO_ID, + registry_path=registry_path)["lease"] cli("refresh-state", *binding(turn), "--classification", "fixture_validated", "--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress", "--delivery-boundary", "in_flight_continuation", "--delivery-workspace-path", str(project), + "--task-lease-idempotency-key", lease["idempotency_key"], + "--task-lease-expected-version", str(lease["version"]), "--no-global-sync", "--suppress-external-sinks", registry_path=registry_path) spent = cli("quota", "spend-slot", *binding(turn), "--slots", "1", "--source", "heartbeat", "--execute", registry_path=registry_path) @@ -2261,9 +2269,12 @@ def capture(turn, todo_id=TODO_ID): "--decision-output-root", str(captures), "--codex-context-dir", str(journal)) def settle(turn): + lease = cli("task-lease", "inspect", "--goal-id", GOAL_ID, "--todo-id", TODO_ID)["lease"] cli("refresh-state", *binding(turn), "--classification", "fixture_validated", "--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress", "--delivery-boundary", "in_flight_continuation", "--delivery-workspace-path", str(project), + "--task-lease-idempotency-key", lease["idempotency_key"], + "--task-lease-expected-version", str(lease["version"]), "--no-global-sync", "--suppress-external-sinks") spent = cli("quota", "spend-slot", *binding(turn), "--slots", "1", "--source", "heartbeat", "--execute") assert spent["appended"] diff --git a/tests/control_plane/test_declared_terminal_settlement_cli.py b/tests/control_plane/test_declared_terminal_settlement_cli.py index f262d12ffa..5341282176 100644 --- a/tests/control_plane/test_declared_terminal_settlement_cli.py +++ b/tests/control_plane/test_declared_terminal_settlement_cli.py @@ -130,7 +130,9 @@ def test_declared_leased_completion_writeback_spend_terminal_and_replay( completion_command = recovery["ordered_steps"][0]["command_template"] assert "--project" in recovery["ordered_steps"][1]["command_template"] assert "--state-file" in recovery["ordered_steps"][1]["command_template"] - assert "--project" not in recovery["ordered_steps"][2]["command_template"] + assert "--settle-turn" in recovery["ordered_steps"][1]["command_template"] + assert recovery["ordered_steps"][2]["owner"] == "runtime" + assert "command_template" not in recovery["ordered_steps"][2] assert "--no-follow-up" not in shlex.split(completion_command) assert completion_command in render_todo_markdown(premature) completion_command = completion_command.replace("", complete_args[-1]) diff --git a/tests/control_plane/test_dependency_dispatch_cli.py b/tests/control_plane/test_dependency_dispatch_cli.py index 5c69ccfc91..3080170341 100644 --- a/tests/control_plane/test_dependency_dispatch_cli.py +++ b/tests/control_plane/test_dependency_dispatch_cli.py @@ -152,7 +152,9 @@ def test_local_wait_and_user_gates_preserve_only_verified_independent_work(publi host.run() assert not host.counts.exists() - update(goal, gate, "--unblocks-todo-id", waiting) + second_gated = add(goal, "Validate another gated artifact after owner approval.", + "--task-class", "advancement_task", "--action-kind", "validate", "--status", "blocked") + update(goal, gate, "--unblocks-todo-id", second_gated) fresh = guard(goal) assert fresh["should_run"] is True and fresh["requires_user_action"] is True assert fresh["selected_todo"]["todo_id"] == independent diff --git a/tests/control_plane/test_explore_capture_identity.py b/tests/control_plane/test_explore_capture_identity.py index 3371f1c5d5..a2a88c6a02 100644 --- a/tests/control_plane/test_explore_capture_identity.py +++ b/tests/control_plane/test_explore_capture_identity.py @@ -35,6 +35,7 @@ def test_first_capture_allocates_identity_and_recovers_without_rewriting_scope( rc, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app", *binding, cwd=project) assert rc == 0, guard + proof = guard["execution_preparation"]["lease_proof"] attachment = deepcopy(ATTACHMENT) del attachment["node_id"] packet = {"schema_version": "goal_vision_replan_contract_v0", @@ -52,6 +53,8 @@ def test_first_capture_allocates_identity_and_recovers_without_rewriting_scope( vision = tmp_path / "vision.json" args = ("refresh-state", *binding, "--classification", "validated_change", "--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress", + "--task-lease-idempotency-key", proof["idempotency_key"], + "--task-lease-expected-version", str(proof["expected_version"]), "--no-global-sync", "--suppress-external-sinks", "--agent-vision-json", str(vision)) log = explore_result_log_path(runtime, GOAL_ID) if conflict: diff --git a/tests/control_plane/test_explore_durable_links.py b/tests/control_plane/test_explore_durable_links.py index 2a32c18a3b..17ee50f6c7 100644 --- a/tests/control_plane/test_explore_durable_links.py +++ b/tests/control_plane/test_explore_durable_links.py @@ -51,12 +51,15 @@ def test_ninth_capture_keeps_history_and_survives_real_cli_replay(tmp_path, prov rc, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app", *binding, cwd=project) assert rc == 0, guard + proof = guard["execution_preparation"]["lease_proof"] vision = tmp_path / "vision.json" vision.write_text(json.dumps({"schema_version": "goal_vision_replan_contract_v0", "vision_patch": {"acceptance_summary": "Require a uniform tail bound."}, "explore_result": deepcopy(ATTACHMENT)})) command = ("refresh-state", *binding, "--classification", "validated_change", "--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress", + "--task-lease-idempotency-key", proof["idempotency_key"], + "--task-lease-expected-version", str(proof["expected_version"]), "--no-global-sync", "--suppress-external-sinks", "--agent-vision-json", str(vision)) rc, captured = _run_cli(registry, runtime, *command, cwd=project) assert rc == 0 and captured["explore_result_delivery"]["ok"], captured diff --git a/tests/control_plane/test_explore_result_writeback.py b/tests/control_plane/test_explore_result_writeback.py index 7670e037d8..aa32ac690a 100644 --- a/tests/control_plane/test_explore_result_writeback.py +++ b/tests/control_plane/test_explore_result_writeback.py @@ -374,8 +374,6 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r cwd=project, ) assert rc == 0, claimed - if terminal: - from loopx.todos import complete_goal_todo binding = ( "--goal-id", @@ -398,14 +396,21 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r ) assert rc == 0, guard if terminal: - complete_goal_todo( - registry_path=path, - goal_id=GOAL_ID, - todo_id=TODO_ID, - agent_id=AGENT_ID, - evidence="Synthetic validation passed", - **terminal_proof, + rc, completed = _run_cli( + path, + runtime, + "todo", + "complete", + *binding, + "--evidence", + "Synthetic validation passed", + "--task-lease-idempotency-key", + terminal_proof["task_lease_idempotency_key"], + "--task-lease-expected-version", + str(terminal_proof["task_lease_expected_version"]), + cwd=project, ) + assert rc == 0, completed source = tmp_path / "result.json" plan = guard["interaction_contract"]["cli_channel"]["settlement_plan"] template = plan["ordered_steps"][1]["optional_attachments"][0]["attachment_template"] @@ -452,6 +457,12 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r attachment_args += ("--explore-result-json", str(source)) if source_mode in {"vision", "both", "path_delta", "wide_path_delta", "linked_path_delta"}: attachment_args += ("--agent-vision-json", str(vision)) + proof = guard.get("execution_preparation", {}).get("lease_proof") + lease_args = ( + ("--task-lease-idempotency-key", proof["idempotency_key"], + "--task-lease-expected-version", str(proof["expected_version"])) + if proof else () + ) args = ( "refresh-state", *binding, @@ -461,6 +472,7 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r "implementation", "--delivery-outcome", "outcome_progress", + *lease_args, "--no-global-sync", "--suppress-external-sinks", *attachment_args, @@ -884,10 +896,30 @@ def run(*command): assert not log.exists() assert list_goal_todos(registry_path=path, goal_id=GOAL_ID)["todos"] == todos - # Supported recovery: fresh admission and an active lease, then the same capture. - run(*guard_command) - current = run("task-lease", "acquire", *lease_binding, "--idempotency-key", "recovery")["lease"] - captured = run(*refresh) + # Supported recovery after retiring the original generation: supersede onto a + # fresh Todo/Turn with an active lease, then commit and test replay after release. + reclaimed = run("todo", "claim", "--goal-id", GOAL_ID, "--todo-id", TODO_ID, + "--agent-id", AGENT_ID, "--claimed-by", AGENT_ID) + superseded = run("todo", "supersede", "--goal-id", GOAL_ID, "--todo-id", TODO_ID, + "--agent-id", AGENT_ID, "--claim-operation-id", reclaimed["claim_operation_id"], + "--reason", "Original lease was released before writeback", + "--next-agent-todo", "[P1] Continue tail bound check", + "--next-claimed-by", AGENT_ID) + recovery_todo_id = superseded["successor_todo_ids"][0] + recovery_lease_binding = ["--goal-id", GOAL_ID, "--todo-id", recovery_todo_id, "--owner", AGENT_ID] + current = run("task-lease", "acquire", *recovery_lease_binding, "--idempotency-key", "recovery")["lease"] + recovery_binding = ["--goal-id", GOAL_ID, "--agent-id", AGENT_ID, + "--todo-id", recovery_todo_id, "--turn-instance-id", f"{TURN_ID}-recovery"] + recovery_guard = run("quota", "should-run", "--codex-app", *recovery_binding) + proof = recovery_guard["execution_preparation"]["lease_proof"] + recovery_refresh = [ + "refresh-state", *recovery_binding, "--classification", "validated_change", + "--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress", + "--task-lease-idempotency-key", proof["idempotency_key"], + "--task-lease-expected-version", str(proof["expected_version"]), + "--agent-vision-json", str(vision), "--no-global-sync", "--suppress-external-sinks", + ] + captured = run(*recovery_refresh) assert captured["appended"] and captured["explore_result_delivery"]["ok"] display_input = captured["explore_result_delivery"]["hook"]["intents"][0]["payload"] assert display_input == {"attachment_ref": "$.explore_result"} @@ -895,15 +927,15 @@ def run(*command): assert len(load_explore_result_events(log)) == 2 committed_index, committed_log = index.read_bytes(), log.read_bytes() rows = list_goal_todos(registry_path=path, goal_id=GOAL_ID)["todos"] - target = next(row for row in rows if row["todo_id"] == TODO_ID) + target = next(row for row in rows if row["todo_id"] == recovery_todo_id) assert target["status"] == "open" and target["claimed_by"] == AGENT_ID assert target["explore_result_node_refs"] == [ATTACHMENT["node_id"]] - run("task-lease", "release", *lease_binding, "--idempotency-key", "recovery", + run("task-lease", "release", *recovery_lease_binding, "--idempotency-key", "recovery", "--expected-version", str(current["version"])) leases = read_canonical_todos_if_promoted( runtime_root=runtime, goal_id=GOAL_ID, include_leases=True)["leases"] - assert leases[0]["status"] == "released" - replay = run(*refresh) + assert all(lease["status"] == "released" for lease in leases), leases + replay = run(*recovery_refresh) assert replay["idempotent_replay"] and replay["explore_result_delivery"]["idempotent_replay"] assert replay["explore_result"] == captured["explore_result"] assert replay["explore_result_delivery"]["hook"]["intents"][0]["payload"] == display_input diff --git a/tests/control_plane/test_explore_settlement_attachment.py b/tests/control_plane/test_explore_settlement_attachment.py index 026b44b37c..e75bda46b0 100644 --- a/tests/control_plane/test_explore_settlement_attachment.py +++ b/tests/control_plane/test_explore_settlement_attachment.py @@ -72,7 +72,7 @@ def test_real_cli_settlement_exposes_optional_attachment(tmp_path, monkeypatch, assert "--explore-result-json" not in writeback["command_template"] rc, compact = _run_cli(path, runtime, *command, "--turn-envelope", cwd=project) assert rc == 0, compact - assert compact["writeback"]["settlement_plan"] == plan + assert compact["interaction_contract"]["cli_channel"]["settlement_plan"] == plan rc, completed = _run_cli(path, runtime, "todo", "complete", *binding, "--evidence", "Synthetic deliverable validated", "--no-follow-up", cwd=project) assert rc != 0 and completed["settlement_blocked_completion"], completed @@ -80,7 +80,7 @@ def test_real_cli_settlement_exposes_optional_attachment(tmp_path, monkeypatch, # affordance; evidence attachment never bypasses the ordinary receipt gate. def plans(value): if isinstance(value, dict): - if value.get("schema_version") == "quota_settlement_plan_v1": + if value.get("schema_version") in {"quota_settlement_plan_v1", "quota_settlement_plan_v2"}: yield value for child in value.values(): yield from plans(child) @@ -483,14 +483,17 @@ def test_ordinary_cli_settlement_without_evidence_does_not_create_graph(tmp_path assert rc == 0, guard steps = guard["interaction_contract"]["cli_channel"]["settlement_plan"]["ordered_steps"] assert steps[1]["optional_attachments"][0]["required"] is False + assert steps[2]["owner"] == "runtime" and "command_template" not in steps[2] command = steps[1]["command_template"].replace("", "validated_change").replace( "", "single_surface").replace("", "outcome_progress") - rc, refreshed = _run_generated_cli(command + " --progress-result-class advanced" + refresh_cmd = (command + " --progress-result-class advanced" " --progress-evidence-id validation:ordinary-result --delivery-boundary in_flight_continuation" - " --no-global-sync --suppress-external-sinks", registry_path=path) - assert rc == 0, refreshed - rc, spent = _run_generated_cli(steps[2]["command_template"], registry_path=path) - assert rc == 0, spent + " --no-global-sync --suppress-external-sinks") + rc, refreshed = _run_generated_cli(refresh_cmd, registry_path=path) + assert rc == 0 and refreshed["settlement_progress"]["state"] == "settled", refreshed + assert refreshed["settlement_result"]["ok"] is True + rc, replayed = _run_generated_cli(refresh_cmd, registry_path=path) + assert rc == 0 and replayed["idempotent_replay"] is True, replayed assert _spend_run_count(runtime) == 1 assert not explore_result_log_path(runtime, GOAL_ID).exists() diff --git a/tests/control_plane/test_goal_create_completion.py b/tests/control_plane/test_goal_create_completion.py index d105a7dbe8..4762457298 100644 --- a/tests/control_plane/test_goal_create_completion.py +++ b/tests/control_plane/test_goal_create_completion.py @@ -425,7 +425,10 @@ def read_action(command): assert result.returncode == 0, result.stdout + result.stderr return json.loads(read_cli_context_for_oracle(result.stdout)[0]) - review = read_action(decision["replan_action_packet"]["review"]) + from loopx.control_plane.quota.turn_envelope import _turn_envelope_result + assert "review" not in decision["replan_action_packet"] + staged = _turn_envelope_result("replan_cli_view", payload=decision)["replan_action_packet"] + review = read_action(staged["review"]) writeback = read_action(review["next"]["writeback"]) stride = {**writeback["stride_control"]["minimal_example"], "basis": "unexplained_gap", "action": "verify", "axes": { diff --git a/tests/control_plane/test_goal_outcome_diagnostics.py b/tests/control_plane/test_goal_outcome_diagnostics.py index a4d1833760..59ebd19bb1 100644 --- a/tests/control_plane/test_goal_outcome_diagnostics.py +++ b/tests/control_plane/test_goal_outcome_diagnostics.py @@ -134,7 +134,8 @@ def test_review_capacity_diagnosis_and_lossless_review_recovery(acceptance_goal, assert "outcome review capacity exceeded" in detail assert "declaration and review budgets are independent" in detail assert "all outcomes in the Goal, not the source count" in detail - assert "report the capacity blocker" in detail + assert "storage_format: records" in detail + assert "recover ambiguous operations unchanged" in detail assert declaration["source"]["summary"] not in json.dumps(rejected) assert "保留证据" not in json.dumps(rejected, ensure_ascii=False) assert cli("inspect")[1] == before diff --git a/tests/control_plane/test_material_feedback_checkpoint.py b/tests/control_plane/test_material_feedback_checkpoint.py index 087027a989..e887530916 100644 --- a/tests/control_plane/test_material_feedback_checkpoint.py +++ b/tests/control_plane/test_material_feedback_checkpoint.py @@ -149,14 +149,26 @@ def read_stage(command): assert fixture._spend_run_count(runtime) == 0 refreshed = execute(result_command, validated_progress="route_feedback_verified", scale="implementation", outcome="outcome_progress", file=vision) - execute(result_command, validated_progress="route_feedback_verified", + replayed = execute(result_command, validated_progress="route_feedback_verified", scale="implementation", outcome="outcome_progress", file=vision) assert fixture._classification_count(runtime, "route_feedback_verified") == 1 - persisted = json.loads(Path(refreshed["json_path"]).read_text()) + assert refreshed["settlement_progress"]["state"] == "settled" + assert refreshed["settlement_result"]["ok"] is True + assert replayed["idempotent_replay"] is True + if "json_path" in refreshed: + persisted_path = refreshed["json_path"] + else: + selected_result = subprocess.run( + shlex.split(refreshed["result_details"]["command"] + " --select /json_path"), + cwd=project, capture_output=True, text=True, check=True, + ) + selected_page = json.loads(selected_result.stdout) + persisted_path = json.loads(selected_page["content"])["selections"][0]["value"] + persisted = json.loads(Path(persisted_path).read_text()) assert persisted["vision_checkpoint"]["delivery_boundary"] == "semantic_closeout" assert persisted["agent_vision"]["path_delta"]["evidence_refs"] == [evidence] - execute(plan["ordered_steps"][2]["command_template"]) - execute(plan["ordered_steps"][2]["command_template"]) + assert plan["ordered_steps"][2]["owner"] == "runtime" + assert "command_template" not in plan["ordered_steps"][2] assert fixture._spend_run_count(runtime) == 1 retained_todo = cli("todo", "list", "--goal-id", fixture.GOAL_ID, "--todo-id", fixture.TODO_ID)["todo"] assert retained_todo["status"] == "open" and retained_todo["completion_validation_required"] @@ -214,7 +226,10 @@ def read_stage(command): + " --agent-vision-json " + shlex.quote(str(vision)) + " --no-global-sync --suppress-external-sinks", validated_progress="route_comparison_decided", scale="implementation", outcome="outcome_progress") assert acknowledged["autonomous_replan_ack"]["semantic_delta"]["accepted"] - execute(review_plan["ordered_steps"][2]["command_template"]) + if "command_template" in review_plan["ordered_steps"][2]: + execute(review_plan["ordered_steps"][2]["command_template"]) + else: + assert acknowledged["settlement_progress"]["state"] == "settled" next_guard = cli("quota", "should-run", *binding, "--runtime-profile", "generic_cli", "--todo-id", fixture.TODO_ID, "--turn-instance-id", "route-feedback-2") assert next_guard["should_run"] and next_guard["selected_todo"]["todo_id"] == fixture.TODO_ID diff --git a/tests/control_plane/test_model_behavior_qualification.py b/tests/control_plane/test_model_behavior_qualification.py index 7df96eff00..64876a2216 100644 --- a/tests/control_plane/test_model_behavior_qualification.py +++ b/tests/control_plane/test_model_behavior_qualification.py @@ -1032,6 +1032,9 @@ def test_complete_candidate_scans_actual_scheduler_source_paths(operation: str, @pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_real_cli_private_source_is_refused_before_provider_io(tmp_path, monkeypatch, provider) -> None: + from loopx.control_plane.scheduler.execution_context import ( + scheduler_execution_context_for_runtime_profile, + ) from loopx.control_plane.testing.model_behavior_qualification import ModelBehaviorPairValidationError from tests.control_plane.canonical_authority_fixture import isolate_sqlite_runtime from tests.control_plane.test_quota_settlement_cli import ( @@ -1044,7 +1047,10 @@ def test_real_cli_private_source_is_refused_before_provider_io(tmp_path, monkeyp "--agent-id", AGENT_ID, "--codex-app", "--context-delivery", "off", "--turn-instance-id", "qualification-private-source", cwd=project) assert rc == 0 and source["ok"] is True - candidate = compose_turn_decision(source) + candidate = compose_turn_decision( + source, + scheduler_execution_context=scheduler_execution_context_for_runtime_profile("codex_app_heartbeat"), + ) before = json.dumps(candidate, sort_keys=True) calls = [] # Real CLI context includes private runtime routing. Complete-source diff --git a/tests/control_plane/test_open_outcome_replan.py b/tests/control_plane/test_open_outcome_replan.py index 3eccd94f11..4a7b2ec5ba 100644 --- a/tests/control_plane/test_open_outcome_replan.py +++ b/tests/control_plane/test_open_outcome_replan.py @@ -90,7 +90,11 @@ def guard(turn): envelope = call("quota", "should-run", "--codex-app", "--turn-envelope", "--goal-id", GOAL, "--agent-id", AGENT, "--todo-id", TODO, "--turn-instance-id", TURN, ) - delivered = next(source for source in envelope["work_context"]["sources"] if source["source"] == "goal_acceptance") + delivered = next( + source + for source in envelope["interaction_contract"]["agent_channel"]["work_context"]["sources"] + if source["source"] == "goal_acceptance" + ) assert acceptance == delivered["content"]["goal_acceptance_contract"] assert inspect() == before, "review admission does not rewrite acceptance" diff --git a/tests/control_plane/test_quota_vision_audit_references.py b/tests/control_plane/test_quota_vision_audit_references.py index 1e617aa306..b51d982e9c 100644 --- a/tests/control_plane/test_quota_vision_audit_references.py +++ b/tests/control_plane/test_quota_vision_audit_references.py @@ -11,6 +11,7 @@ from loopx.control_plane.quota.turn_envelope import ( build_turn_envelope, ) +from test_complete_turn_decision import _resign_contract, _source_digest from test_replan_host_context_projection import _quota_payload @@ -239,6 +240,7 @@ def test_real_guard_capture_and_recovery_preserve_vision_before_effects( invalid["goal_frontier_projection"]["vision_continuation_audit"][ "required" ] = not audit["required"] + invalid, _ = _resign_contract(invalid) invalid_path = tmp_path / "invalid.json" invalid_path.write_text(json.dumps(invalid)) rc, refused = _run_cli( @@ -249,7 +251,27 @@ def test_real_guard_capture_and_recovery_preserve_vision_before_effects( "--decision-file", str(invalid_path), "--source-hash", - build_turn_envelope(invalid)["action_signature"]["source_decision_hash"], + _source_digest(invalid), + "--replan-stage", + "review", + cwd=project, + ) + assert rc != 0 and "Vision audit reference" in json.dumps(refused) + historical = { + key: value + for key, value in invalid.items() + if key not in {"schema_version", "contract_binding"} + } + invalid_path.write_text(json.dumps(historical)) + rc, refused = _run_cli( + registry, + runtime, + "quota", + "replan-view", + "--decision-file", + str(invalid_path), + "--source-hash", + _source_digest(historical), "--replan-stage", "review", cwd=project, diff --git a/tests/control_plane/test_research_checkpoint_capture.py b/tests/control_plane/test_research_checkpoint_capture.py index fa7bc154fe..4aace9fa22 100644 --- a/tests/control_plane/test_research_checkpoint_capture.py +++ b/tests/control_plane/test_research_checkpoint_capture.py @@ -307,6 +307,9 @@ def test_real_cli_settlement_capture_verification_and_inert_materialization( tmp_path, monkeypatch, opportunity, corrupt, collisions): isolate_sqlite_runtime(tmp_path, monkeypatch) project, runtime, registry = cli._write_fixture(tmp_path, provider="sqlite") + config = json.loads(registry.read_text()) + config["goals"][0]["quota"]["allowed_slots"] = 4 + registry.write_text(json.dumps(config)) binding = ("--agent-id", cli.AGENT_ID, "--todo-id", cli.TODO_ID, "--turn-instance-id", cli.TURN_ID) def run(*args): code, result = cli._run_cli(registry, runtime, *args) @@ -439,6 +442,9 @@ def test_real_native_cursor_must_match_sealed_store_basis(tmp_path, monkeypatch, isolate_sqlite_runtime(tmp_path, monkeypatch) project, runtime, registry = cli._write_fixture(tmp_path, provider="sqlite") + config = json.loads(registry.read_text()) + config["goals"][0]["quota"]["allowed_slots"] = 4 + registry.write_text(json.dumps(config)) binding = ("--agent-id", cli.AGENT_ID, "--todo-id", cli.TODO_ID, "--turn-instance-id", cli.TURN_ID) def run(*args): code, result = cli._run_cli(registry, runtime, *args, cwd=project) @@ -591,6 +597,9 @@ def test_real_pre_replan_capture_is_before_decision_and_same_node_is_not_recaptu isolate_sqlite_runtime(tmp_path, monkeypatch) project, runtime, registry = cli._write_fixture(tmp_path, provider="sqlite") + config = json.loads(registry.read_text()) + config["goals"][0]["quota"]["allowed_slots"] = 4 + registry.write_text(json.dumps(config)) source_turn_id = "wake-100-prior" binding = ("--agent-id", cli.AGENT_ID, "--todo-id", cli.TODO_ID, "--turn-instance-id", source_turn_id) @@ -741,7 +750,10 @@ def run(*args): 'observed_reality':'Isolated source work was verified.', 'retained':['Participant capture'], 'evidence_refs':['evidence:controller-fixture']}})) run('refresh-state', '--goal-id', cli.GOAL_ID, *binding, '--classification','controller_fixture_verified', - '--delivery-batch-scale','multi_surface','--delivery-outcome','outcome_progress','--agent-vision-json',str(vision)) + '--delivery-batch-scale','multi_surface','--delivery-outcome','outcome_progress', + '--task-lease-idempotency-key', lease['lease']['idempotency_key'], + '--task-lease-expected-version', str(lease['lease']['version']), + '--agent-vision-json',str(vision)) run('quota','spend-slot','--goal-id',cli.GOAL_ID,*binding,'--source','heartbeat','--execute') before = run('todo','list','--goal-id',cli.GOAL_ID,'--todo-id',cli.TODO_ID) from loopx.capabilities.explore.result_log import ( @@ -904,6 +916,9 @@ def test_failed_replan_index_cannot_relabel_later_work_after_permissions_recover pytest.skip("Real POSIX permission denial requires an unprivileged test user") isolate_sqlite_runtime(tmp_path, monkeypatch) project, runtime, registry = cli._write_fixture(tmp_path, provider="sqlite") + config = json.loads(registry.read_text()) + config["goals"][0]["quota"]["allowed_slots"] = 4 + registry.write_text(json.dumps(config)) skills = tmp_path / "skills" skills.mkdir() binary = tmp_path / "codex" diff --git a/tests/control_plane/test_research_checkpoint_cli.py b/tests/control_plane/test_research_checkpoint_cli.py index cc0cefae20..21a51cd346 100644 --- a/tests/control_plane/test_research_checkpoint_cli.py +++ b/tests/control_plane/test_research_checkpoint_cli.py @@ -41,8 +41,8 @@ def cli(*args, ok=True): todo = cli('todo', 'add', '--goal-id', 'source', '--role', 'agent', '--claimed-by', 'researcher', '--task-class', 'advancement_task', '--text', 'Retain rejected candidate evidence.')['todo_id'] actor = ['--goal-id', 'source', '--agent-id', 'researcher', '--todo-id', todo, '--turn-instance-id', 'captured-work'] + lease = cli('task-lease', 'acquire', '--goal-id', 'source', '--todo-id', todo, '--owner', 'researcher', '--idempotency-key', 'source-work')['lease'] cli('quota', 'should-run', *actor, '--runtime-profile', 'generic_cli', '--available-capability', 'shell') - cli('task-lease', 'acquire', '--goal-id', 'source', '--todo-id', todo, '--owner', 'researcher', '--idempotency-key', 'source-work') code = workspace / 'candidate.txt' code.write_text('7') from loopx.capabilities.explore.result_log import ( @@ -58,6 +58,7 @@ def cli(*args, ok=True): vision.write_text(json.dumps({'schema_version': 'goal_vision_replan_contract_v0', 'state': 'vision_patch_proposed', 'vision_patch': {'acceptance_summary': 'Preserve rejected candidates and continue complete experiments.'}})) cli('refresh-state', *actor, '--classification', 'captured_research', '--delivery-outcome', 'outcome_progress', + '--task-lease-idempotency-key', lease['idempotency_key'], '--task-lease-expected-version', str(lease['version']), '--agent-vision-json', str(vision), '--no-global-sync', '--suppress-external-sinks') cli('quota', 'spend-slot', *actor, '--slots', '1', '--source', 'heartbeat', '--execute') pending = {'command': ''} diff --git a/tests/control_plane/test_settlement_input_discovery_cli.py b/tests/control_plane/test_settlement_input_discovery_cli.py index c2c2565dfe..0a0fb7c00e 100644 --- a/tests/control_plane/test_settlement_input_discovery_cli.py +++ b/tests/control_plane/test_settlement_input_discovery_cli.py @@ -97,7 +97,9 @@ def run(*args): assert build_turn_envelope(guard)["writeback"]["settlement_plan"] == plan rc, premature = run("checkpoint-context", *binding) assert rc != 0 and "original committed Turn writeback" in premature["error"] - rc, no_writeback = _run_generated_cli(plan["ordered_steps"][2]["command_template"], registry_path=registry) + assert plan["ordered_steps"][2]["owner"] == "runtime" + assert "command_template" not in plan["ordered_steps"][2] + rc, no_writeback = run("quota", "spend-slot", *binding, "--slots", "1", "--source", "heartbeat", "--execute") assert rc != 0 and _spend_run_count(runtime) == 0, no_writeback if not in_flight: @@ -124,12 +126,14 @@ def run(*args): rc, rejected = _run_generated_cli(refresh, registry_path=registry) assert rc != 0 and _spend_run_count(runtime) == 0, rejected path.write_text(json.dumps(vision)) - rc, written = _run_generated_cli(refresh, registry_path=registry) - assert rc == 0 and written["vision_checkpoint"]["satisfied"], written - assert written["settlement_identity"] == plan["identity"] for replay in (False, True): - rc, spent = _run_generated_cli(written["settlement_owed"]["command"], registry_path=registry) - assert rc == 0 and spent["appended"] is not replay, spent + rc, written = _run_generated_cli(refresh, registry_path=registry) + assert rc == 0 and written["settlement_progress"]["state"] == "settled", written + assert written["settlement_identity"] == plan["identity"] + assert written["settlement_result"]["ok"] is True + assert written.get("idempotent_replay", False) is replay + if not replay: + assert written["vision_checkpoint"]["satisfied"], written assert _spend_run_count(runtime) == 1 rc, final_todo = _run_generated_cli(readback, registry_path=registry) assert rc == 0 and final_todo["todo"]["status"] == ("open" if in_flight else "done") @@ -240,9 +244,12 @@ def run(*args): rc, guard = _run_generated_cli(command, registry_path=registry) assert rc == 0, (stage, guard.get("error"), guard.get("reason")) if stage == "review": + from loopx.control_plane.quota.turn_envelope import _turn_envelope_result channel = guard["interaction_contract"]["agent_channel"] assert channel["must_attempt"] and not channel["delivery_allowed"] - read_command = guard["replan_action_packet"]["writeback"] + assert "writeback" not in guard["replan_action_packet"] + staged_packet = _turn_envelope_result("replan_cli_view", payload=guard)["replan_action_packet"] + read_command = staged_packet["writeback"] result = subprocess.run( shlex.split(read_command), cwd=project, capture_output=True, text=True, check=True, timeout=60, ) @@ -274,8 +281,11 @@ def run(*args): args += ["--no-global-sync", "--suppress-external-sinks"] rc, written = run(*args) assert rc == 0, written - rc, spent = _run_generated_cli(written["settlement_owed"]["command"], registry_path=registry) - assert rc == 0, spent + if written.get("settlement_progress", {}).get("state") == "settled": + assert written["settlement_result"]["ok"] is True + else: + rc, spent = _run_generated_cli(written["settlement_owed"]["command"], registry_path=registry) + assert rc == 0, spent # Read the real immutable CLI history, not a hand-authored decision fixture. status = {"registry": str(registry), "runtime_root": str(runtime), "run_history": {"runs": []}} diff --git a/tests/control_plane/test_work_acceptance_context.py b/tests/control_plane/test_work_acceptance_context.py index 6de0c9e551..6eb658a732 100644 --- a/tests/control_plane/test_work_acceptance_context.py +++ b/tests/control_plane/test_work_acceptance_context.py @@ -252,19 +252,26 @@ def configure(*arguments): first = read_cli_policy_payload(json.dumps(first), returncode=0) saved = (capture / "decision.json").read_bytes() full = json.loads(saved) - basis = next(row["content"] for row in first["work_context"]["sources"] + from loopx.control_plane.quota.turn_envelope import build_turn_envelope + first_envelope = build_turn_envelope(first, captured_decision_path=str(capture / "decision.json")) + assert first["schema_version"] == "loopx_turn_envelope_v1" + assert first == full + basis = next(row["content"] for row in first_envelope["work_context"]["sources"] if row.get("source") == "goal_acceptance") - assert first["action"]["delivery_allowed"] is True + assert first["interaction_contract"]["agent_channel"]["delivery_allowed"] is True assert basis["tasks"][0]["state"] == "ready" assert basis["goal_acceptance_contract"]["outcome_requirements"] == original - request = build_loopx_turn_host_request(build_loopx_turn_plan(first, + request = build_loopx_turn_host_request(build_loopx_turn_plan(first_envelope, work_contract=first, host="codex-cli", execution_mode="isolated-headless", turn_instance_id="original-delivery")) result = subprocess.run(_recording_host(project), input=json.dumps(request), capture_output=True, text=True, timeout=30) assert result.returncode == 0, result.stdout + result.stderr prompt = (project / "received.txt").read_text() - authority = json.loads(prompt.split("Turn authority JSON:\n", 1)[1].split("\n\nWhen finished", 1)[0]) - assert authority["work_context"] == first["work_context"] + authority = json.loads(prompt.split("Turn request:\n", 1)[1].split("\n\nWhen finished", 1)[0]) + assert authority["schema_version"] == "loopx_turn_model_context_v0" + assert authority["source_schema_version"] == "loopx_turn_host_request_v1" + assert (authority["task_context"]["interaction_contract"]["agent_channel"]["work_context"] + == first["interaction_contract"]["agent_channel"]["work_context"]) # Finish the original synthetic work before attempting another managed # Turn. An unsettled receipt is its own legitimate recovery boundary. @@ -343,7 +350,7 @@ def configure(*arguments): if source.get("source") == "goal_acceptance": assert source["content"]["goal_acceptance_contract"] == cold["goal_acceptance_contract"] before_read = {p: p.read_bytes() for p in capture.rglob("*") if p.is_file()} - detail = subprocess.run(shlex.split(first["detail_ref"]["full_decision"]), + detail = subprocess.run(shlex.split(first_envelope["detail_ref"]["full_decision"]), capture_output=True, check=True).stdout assert detail == saved and json.loads(detail) == full assert {p: p.read_bytes() for p in capture.rglob("*") if p.is_file()} == before_read diff --git a/tests/control_plane/test_work_yardstick_cli.py b/tests/control_plane/test_work_yardstick_cli.py index 524ace8aa9..00eb5fe4f2 100644 --- a/tests/control_plane/test_work_yardstick_cli.py +++ b/tests/control_plane/test_work_yardstick_cli.py @@ -7,6 +7,7 @@ from datetime import datetime, timezone from itertools import groupby import json +from pathlib import Path import shlex import pytest @@ -584,17 +585,18 @@ def test_yardstick_opening_keeps_the_current_cue_with_a_multi_page_write_scope(t assert guidance == graph["work_yardstick_capture"]["guidance"] assert cue == graph["work_yardsticks"][0] and cue["todo_id"] == TODO_ID - _, reference = read_context_read_reference(opening["reference_file"]) - assert reference["sha256"] == hashlib.sha256(original).hexdigest() + expected_sha256 = hashlib.sha256(original).hexdigest() + assert opening["sha256"] == opening["navigation"]["recovery"]["sha256"] == expected_sha256 + assert Path(opening["navigation"]["recovery"]["path"]) == saved chunks = [content] offset = len(content.encode()) page = opening while not page["eof"]: assert page["next_offset"] == offset words = shlex.split(page["next_command"]) + assert words[words.index("--capture-sha256") + 1] == expected_sha256 page = run(project, runtime, path, *words[words.index("quota"):]) - _, follow = read_context_read_reference(page["reference_file"]) - assert follow["path"] == reference["path"] and follow["sha256"] == reference["sha256"] + assert page["sha256"] == expected_sha256 assert page["offset"] == offset chunks.append(page["content"]) offset += len(page["content"].encode()) diff --git a/tests/control_plane_ts/quota_spend_commit.test.ts b/tests/control_plane_ts/quota_spend_commit.test.ts index dac67a0493..acf9f4d2f6 100644 --- a/tests/control_plane_ts/quota_spend_commit.test.ts +++ b/tests/control_plane_ts/quota_spend_commit.test.ts @@ -33,6 +33,9 @@ import { quotaSpendIndexDigest, QUOTA_SPEND_COMMIT_REQUEST_SCHEMA, } from "../../loopx/control_plane/quota/spend_commit.ts"; +import { + readQuotaSpendEventFromRun, +} from "../../loopx/control_plane/quota/accounting_artifact_transaction.ts"; const goalId = "quota-spend-transaction"; const instanceA = "ginst_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; @@ -1148,3 +1151,65 @@ test("concurrent exact-effect retries serialize to one spend", async (t) => { const indexPath = String(results[0].payload.index_path); assert.equal((await readFile(indexPath, "utf8")).trim().split("\n").length, 1); }); + +test("readQuotaSpendEventFromRun resolves checkpoint-relocated run json_path and rejects arbitrary outside paths", async (t) => { + const originalRoot = await tempRuntime(t); + const relocatedRoot = await tempRuntime(t); + const originalRunsDir = join(originalRoot, "goals", goalId, "runs"); + const relocatedRunsDir = join(relocatedRoot, "goals", goalId, "runs"); + await mkdir(originalRunsDir, { recursive: true }); + await mkdir(relocatedRunsDir, { recursive: true }); + + const fileName = "2026-08-25T12-00-00Z-quota-spend.json"; + const originalJson = join(originalRunsDir, fileName); + const relocatedJson = join(relocatedRunsDir, fileName); + await writeFile( + originalJson, + JSON.stringify({ + classification: "quota_slot_spent", + goal_id: goalId, + effect_ref: "stale-original", + quota_event: { schema_version: "quota_event_v0", effect_id: "stale-original" }, + }), + "utf8", + ); + await writeFile( + relocatedJson, + JSON.stringify({ + classification: "quota_slot_spent", + goal_id: goalId, + effect_ref: "quota-spend-effect-1", + quota_event: { schema_version: "quota_event_v0", effect_id: "quota-spend-effect-1" }, + }), + "utf8", + ); + + const event = await readQuotaSpendEventFromRun( + relocatedRunsDir, + { effect_ref: "quota-spend-effect-1", json_path: originalJson }, + goalId, + ); + assert.equal(event?.effect_id, "quota-spend-effect-1"); + + const otherGoalRuns = join(originalRoot, "goals", "other-goal", "runs"); + await mkdir(otherGoalRuns, { recursive: true }); + const otherGoalJson = join(otherGoalRuns, fileName); + const outsideJson = join(originalRoot, "outside.json"); + await writeFile( + otherGoalJson, + JSON.stringify({ classification: "quota_slot_spent", goal_id: goalId, effect_ref: "quota-spend-effect-1" }), + "utf8", + ); + await writeFile( + outsideJson, + JSON.stringify({ classification: "quota_slot_spent", goal_id: goalId, effect_ref: "quota-spend-effect-1" }), + "utf8", + ); + + for (const unsafePath of [otherGoalJson, outsideJson]) { + await assert.rejects( + () => readQuotaSpendEventFromRun(relocatedRunsDir, { effect_ref: "quota-spend-effect-1", json_path: unsafePath }, goalId), + /quota spend json_path must stay inside the goal runs directory/, + ); + } +});