From e36280b7bbe4f8fd88acb7bf86434c01eb886d40 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:02:14 +0800 Subject: [PATCH] Bind saved C1 replan reads to the original capture digest Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/cli_commands/quota_capture.py | 6 +-- loopx/cli_commands/quota_native_context.py | 24 +++++++++--- loopx/control_plane/quota/turn_envelope.ts | 7 ++-- .../turn_driver/codex_context.py | 7 ++-- .../turn_driver/decision_capture.py | 4 +- .../test_replan_progress_native_cli.py | 38 +++++++++++-------- 6 files changed, 53 insertions(+), 33 deletions(-) diff --git a/loopx/cli_commands/quota_capture.py b/loopx/cli_commands/quota_capture.py index ee21bae681..f0e621f996 100644 --- a/loopx/cli_commands/quota_capture.py +++ b/loopx/cli_commands/quota_capture.py @@ -276,7 +276,7 @@ def progressive_context_capture_view(payload: dict[str, Any], args: argparse.Nam runtime_root: Path, directory: Path | None, surface: str = "quota_packet", reader_argv: list[str] | None = None, - captured_decision_path: str | None = None, + captured_decision: dict[str, str] | None = None, page_attached: bool = False) -> dict[str, Any]: """Same-capture presentation for every caller, without a native journal. @@ -289,7 +289,7 @@ def progressive_context_capture_view(payload: dict[str, Any], args: argparse.Nam # Only a fresh stateless source can supply this route. Saved and explicitly # captured decisions keep the reader chosen by their existing callers. - stage_from_projection = (directory is None and captured_decision_path is None + stage_from_projection = (directory is None and captured_decision is None and "vision" not in quota_detail_sections_from_args(args)) if directory is None: root = Path(_private_transport_root(runtime_root)) @@ -298,7 +298,7 @@ def progressive_context_capture_view(payload: dict[str, Any], args: argparse.Nam surface = "complete_turn" transport = _capture_selection_transport_template(payload, args, capture_root=directory.parent) required, reference = capture_progressive_context(payload, directory=directory, surface=surface, - selection_command_args_template=transport, captured_decision_path=captured_decision_path, + selection_command_args_template=transport, captured_decision=captured_decision, replan_reader_argv=reader_argv if stage_from_projection else None) view = native_context_capture_view(required, directory, captured_recovery=reference, metadata={"context_delivery": native_context_activation(args), diff --git a/loopx/cli_commands/quota_native_context.py b/loopx/cli_commands/quota_native_context.py index 756d428d78..478882e7f7 100644 --- a/loopx/cli_commands/quota_native_context.py +++ b/loopx/cli_commands/quota_native_context.py @@ -2,7 +2,6 @@ from __future__ import annotations import argparse -import hashlib import json import os import stat @@ -110,8 +109,8 @@ def project_saved_context_capture(args: argparse.Namespace, *, hook_session: str else min(output_limit, NATIVE_CONTEXT_PAGE_OUTPUT_BYTES)) if saved_page and args.page_bytes is not None: _validate_context_page_window(args.offset, args.page_bytes, output_limit) - path, raw, payload = _read_capture(args.capture_file, args.capture_sha256, - private=hook_session is not None) + path, _, payload = _read_capture(args.capture_file, args.capture_sha256, + private=hook_session is not None) identity = payload.get("agent_identity") receipt = payload.get("heartbeat_receipt") if (payload.get("ok") is not True or not isinstance(identity, dict) or not isinstance(receipt, dict) @@ -147,6 +146,18 @@ def project_saved_context_capture(args: argparse.Namespace, *, hook_session: str return _saved_context_transport_failure(exc, args=args, receipt=receipt, recovery=full_reference, reader_argv=reader_argv, source_unverified=True) + captured_decision = {"path": str(path)} + if complete and activation["progressive_enabled"]: + from ..control_plane.runtime.private_permissions import require_private_path + + try: + require_private_path(path) + except OSError: + # A manually restored capture can still use the checked historical reader. + pass + else: + captured_decision["sha256"] = args.capture_sha256 + mode = "full" target = None native_prepared = False @@ -159,7 +170,7 @@ def project_saved_context_capture(args: argparse.Namespace, *, hook_session: str try: navigation = quota_capture.progressive_context_capture_view(payload, args, runtime_root=Path(getattr(args, "_context_delivery_machine_runtime", path.parent)), - directory=None, reader_argv=reader_argv, captured_decision_path=str(path), page_attached=saved_page) + directory=None, reader_argv=reader_argv, captured_decision=captured_decision, page_attached=saved_page) if saved_page: return _saved_context_read_page(args, navigation, initial_reference=None, initial_read_plan=None, output_limit=output_limit, reader_argv=reader_argv) @@ -219,10 +230,11 @@ def project_saved_context_capture(args: argparse.Namespace, *, hook_session: str presentation["host_poll_receipt"] = payload["host_poll_receipt"] prepared = prepare_codex_context(presentation, directory=directory, capture_directory=cache, goal_id=args.goal_id, agent_id=args.agent_id, surface="complete_turn" if complete else "quota_packet", - capture_id=hashlib.sha256(raw).hexdigest(), + capture_id=args.capture_sha256, continuity_generation=getattr(args, "_context_delivery_generation", None), progressive_enabled=activation["progressive_enabled"], selection_command_args_template=transport, - captured_decision_path=str(path) if args.initial_read and activation["progressive_enabled"] else None, + captured_decision=captured_decision if activation["progressive_enabled"] + and (args.initial_read or "sha256" in captured_decision) else None, hook_session=hook_session, saved_capture=True) projected = prepared.delivery initial_read_plan = prepared.initial_read_plan diff --git a/loopx/control_plane/quota/turn_envelope.ts b/loopx/control_plane/quota/turn_envelope.ts index a3ffe17e47..69201bda34 100644 --- a/loopx/control_plane/quota/turn_envelope.ts +++ b/loopx/control_plane/quota/turn_envelope.ts @@ -2497,8 +2497,9 @@ function progressivePreparationScopes(payload: JsonObject): PreparationScopeDeta function nativeReplanPresentation(request: JsonObject): JsonObject | undefined { const projection = object(request.native_projection); - const stateless = projection.captured_decision_path === undefined && projection.replan_reader_argv !== undefined; - const path = stateless ? projection.source_path : projection.captured_decision_path; + const captured = object(projection.captured_decision); + const stateless = captured.path === undefined && projection.replan_reader_argv !== undefined; + const path = stateless ? projection.source_path : captured.path; if (typeof path !== "string") return undefined; const source = requireJsonObject(request.context, "captured replan source"); if (stateless) { @@ -2520,7 +2521,7 @@ function nativeReplanPresentation(request: JsonObject): JsonObject | undefined { const identity = precision?.identity ?? contextSourceIdentity(request); const view = replanCliView(source, (value, field) => identity(value, ["context", ...field]), replanReadCommands(source, path, stateless ? projection.replan_reader_argv : undefined, - undefined, stateless ? projection.sha256 : undefined), precision); + undefined, stateless ? projection.sha256 : captured.sha256), precision); return view === source ? undefined : {staged_context: view, deferred: [], instructions: []}; } diff --git a/loopx/control_plane/turn_driver/codex_context.py b/loopx/control_plane/turn_driver/codex_context.py index a9300f09ab..1514deb42d 100644 --- a/loopx/control_plane/turn_driver/codex_context.py +++ b/loopx/control_plane/turn_driver/codex_context.py @@ -909,7 +909,7 @@ def prepare_codex_context(payload: dict[str, Any], *, directory: str | None, cap include_initial_read_plan: bool = True, progressive_enabled: bool | None = None, selection_command_args_template: str | None = None, - captured_decision_path: str | None = None, + captured_decision: dict[str, str] | None = None, hook_session: str | None = None, saved_capture: bool = False) -> NativeContextPreparation: """Commit current recovery before optional projection or candidate updates. @@ -996,8 +996,7 @@ def publish_recovery(reference: dict[str, Any]) -> None: "source_path": recovery["path"], **({"selection_command_args_template": selection_command_args_template} if selection_command_args_template is not None else {}), - **({"captured_decision_path": captured_decision_path} - if captured_decision_path is not None else {}), + **({"captured_decision": captured_decision} if captured_decision is not None else {}), **({"progressive_enabled": False} if not progressive else {}), "command_template": context_capture_read_command(Path(recovery["path"]), recovery["sha256"], page_bytes=page_bytes, max_page_output_bytes=output_bytes) @@ -1007,7 +1006,7 @@ def publish_recovery(reference: dict[str, Any]) -> None: except (EffectRuntimeRemoteError, EffectRuntimeStartupError) as exc: if check_saved_source and isinstance(exc, EffectRuntimeRejected): raise QuotaDecisionTransportError(str(exc)) from exc - if check_saved_source or (captured_decision_path is not None + if check_saved_source or (captured_decision is not None and isinstance(payload.get("replan_read_path"), str)): raise QuotaDecisionTransportError( "Saved context presentation could not be prepared. Read the original capture and do not rerun admission." diff --git a/loopx/control_plane/turn_driver/decision_capture.py b/loopx/control_plane/turn_driver/decision_capture.py index 876e078f4e..8616702714 100644 --- a/loopx/control_plane/turn_driver/decision_capture.py +++ b/loopx/control_plane/turn_driver/decision_capture.py @@ -544,7 +544,7 @@ def capture_context_projection(payload: dict[str, Any], *, directory: Path, def capture_progressive_context(payload: dict[str, Any], *, directory: Path, surface: str = "quota_packet", selection_command_args_template: str | None = None, - captured_decision_path: str | None = None, + captured_decision: dict[str, str] | None = None, replan_reader_argv: list[str] | None = None) -> tuple[dict[str, Any], dict[str, Any]]: """Publish the complete current source before optional stateless presentation. @@ -559,7 +559,7 @@ def capture_progressive_context(payload: dict[str, Any], *, directory: Path, **{key: source[key] for key in ("path", "sha256")}, **({"selection_command_args_template": selection_command_args_template} if selection_command_args_template is not None else {}), - **({"captured_decision_path": captured_decision_path} if captured_decision_path is not None else {}), + **({"captured_decision": captured_decision} if captured_decision is not None else {}), **({"replan_reader_argv": replan_reader_argv} if replan_reader_argv is not None else {}), "source_path": source["path"], "command_template": source["command"] + " --select ''", diff --git a/tests/control_plane/test_replan_progress_native_cli.py b/tests/control_plane/test_replan_progress_native_cli.py index 4042e4f895..9ea2edb670 100644 --- a/tests/control_plane/test_replan_progress_native_cli.py +++ b/tests/control_plane/test_replan_progress_native_cli.py @@ -253,25 +253,13 @@ def review_for(body): try: initial = call_progress_cli(fixture, "quota", "should-run", *scope, "--todo-id", TODO, - "--decision-output-dir", str(capture), *delivery) + "--decision-output-dir", str(capture), "--context-delivery", "off") + assert initial["ok"] original = capture / "decision.json" raw = original.read_bytes() source = json.loads(raw) receipt = source["heartbeat_receipt"] authority = _authority(fixture) - review_command = review_for(read_pages(read_stage(shlex.split(initial["first_read_command"])))) - assert review_command[review_command.index("--decision-file") + 1] == str(original) - review = read_stage(review_command) - assert review["planning_guidance"] == source["replan_action_packet"]["planning_guidance"] - writer = read_stage(shlex.split(review["next"]["writeback"])) - assert writer["writeback"]["settlement_plan"]["identity"] == receipt["settlement_identity"] - assert writer["required_outcome"] == source["replan_action_packet"]["required_outcome"] - - explore_command = review_command.copy() - explore_command[explore_command.index("--replan-stage") + 1] = "explore" - explore = read_stage(explore_command) - assert explore["explore_basis"] is None - assert explore["source_hash"] == review["source_hash"] recovered_path = original.with_name("recovered.json") original.rename(recovered_path) @@ -286,7 +274,27 @@ def review_for(body): assert read_pages(read_stage(shlex.split(nav["first_read_command"])), 8192) == body recovered_review = review_for(body) assert recovered_review[recovered_review.index("--decision-file") + 1] == str(recovered_path) - assert read_stage(recovered_review)["source_hash"] == review["source_hash"] + assert recovered_review[recovered_review.index("--capture-sha256") + 1] == digest + recovered = read_stage(recovered_review) + assert recovered["source_hash"] == source["contract_binding"]["content_hash"] + assert recovered["planning_guidance"] == source["replan_action_packet"]["planning_guidance"] + recovered_writer = shlex.split(recovered["next"]["writeback"]) + assert recovered_writer[recovered_writer.index("--capture-sha256") + 1] == digest + writer = read_stage(recovered_writer) + assert writer["writeback"]["settlement_plan"]["identity"] == receipt["settlement_identity"] + assert writer["required_outcome"] == source["replan_action_packet"]["required_outcome"] + explore_command = recovered_review.copy() + explore_command[explore_command.index("--replan-stage") + 1] = "explore" + explore = read_stage(explore_command) + assert explore["explore_basis"] is None + assert explore["source_hash"] == recovered["source_hash"] + try: + # A different encoding of the same JSON keeps the semantic source hash. + recovered_path.write_bytes(raw + b"\n") + wrong_bytes = read_stage(recovered_review, status=1) + assert wrong_bytes["ok"] is False and "capture digest mismatch" in wrong_bytes["reason"] + finally: + recovered_path.write_bytes(raw) recovered_review[recovered_review.index("--source-hash") + 1] = "sha256:" + "0" * 64 refused = read_stage(recovered_review, status=1) assert refused["ok"] is False and "matching source hash" in refused["reason"]