From 48347211becc22360e919592964882cc45e0bd94 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Sat, 21 Nov 2026 11:03:41 +0800 Subject: [PATCH] fix(steward): tolerate minor JSON delimiter glitches and recover saved report candidates Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../native_chat/steward_reports.py | 8 +- .../capabilities/native_chat/steward_turn.py | 229 ++++++++++++++++-- .../collaboration/steward_managed_mode.ts | 25 +- .../turn_driver/host_candidate.py | 74 +++++- .../control_plane_ts/steward_reports.test.ts | 42 ++++ tests/test_dsh_goal_mode.py | 37 +++ tests/test_native_steward_reports.py | 99 ++++++++ 7 files changed, 480 insertions(+), 34 deletions(-) diff --git a/loopx/capabilities/native_chat/steward_reports.py b/loopx/capabilities/native_chat/steward_reports.py index 473b5e4ab7..44ee3e3d2d 100644 --- a/loopx/capabilities/native_chat/steward_reports.py +++ b/loopx/capabilities/native_chat/steward_reports.py @@ -426,7 +426,8 @@ def _frontier(self, owner_key: str, actor: dict[str, Any]) -> tuple[dict[str, An rows = [row for row in StewardMonitor(self.controller).todos(actor) if row.get("target_key") == window["window_id"]] folder = attempt_directory(self.root, window) if len(rows) == 1 and (rows[0]["status"] == "done" - or rows[0]["status"] == "open" and (folder / "private-generation.json").exists()): + or rows[0]["status"] == "open" and ((folder / "private-generation.json").exists() + or StewardTurnHost(self.controller).has_recoverable_stopped_report(actor, window, rows[0]))): StewardTurnHost(self.controller).recover_report_settlement(actor, window, rows[0]) return None, False if len(rows) != 1 or rows[0]["status"] != "open": @@ -581,10 +582,11 @@ def frontier(self, session_id: str) -> tuple[dict[str, Any] | None, bool]: subscription, window = self.current_window(session_id) if subscription is None: return None, False - if window and (attempt_directory(self.root, window) / "private-generation.json").exists(): + if window: todo = next((row for row in StewardMonitor(self.controller).todos(actor) if row.get("target_key") == window["window_id"]), None) - if todo: + if todo and ((attempt_directory(self.root, window) / "private-generation.json").exists() + or StewardTurnHost(self.controller).has_recoverable_stopped_report(actor, window, todo)): StewardTurnHost(self.controller).recover_report_settlement(actor, window, todo) self.publish(session_id, window) return self._frontier(session_id, actor) diff --git a/loopx/capabilities/native_chat/steward_turn.py b/loopx/capabilities/native_chat/steward_turn.py index 23e2f0a48e..51fbffbed1 100644 --- a/loopx/capabilities/native_chat/steward_turn.py +++ b/loopx/capabilities/native_chat/steward_turn.py @@ -624,6 +624,185 @@ def _frozen_report_contract_recovery( return proof, _normalize_report_kernel_candidate(parsed) return None + def _stopped_report_candidate_recovery( + self, + actor: Mapping[str, Any], + window: Mapping[str, Any], + original: Mapping[str, Any], + *, + key: str, + folder: Path, + root: Path, + ) -> tuple[dict[str, Any], dict[str, Any]] | None: + """Recover a stopped report Turn only when its saved final answer already holds a valid candidate.""" + from ...control_plane.turn_driver.host_candidate import parse_model_json + from ...control_plane.turn_driver.journal_store import load_turn_journal, turn_journal_path + + req_path = folder / "editorial-request.json" + if not req_path.is_file() or not (folder / "source.json").is_file(): + return None + if (folder / "editorial-response.json").exists() and not (folder / "private-generation.json").exists(): + return None + try: + request_digest = json.loads(req_path.read_text(encoding="utf-8")).get("request_digest") + except (OSError, ValueError): + return None + if not isinstance(request_digest, str) or not request_digest: + return None + journal = load_turn_journal(turn_journal_path(root, goal_id=actor["goal_id"], turn_key=key)) + if ( + not isinstance(journal, Mapping) + or journal.get("status") != "stopped" + or journal.get("result_kind") != "wait" + or journal.get("host_attempt_count") != 1 + or (journal.get("host_result") or {}).get("classification") != "no_typed_host_result" + ): + return None + events = self.store.events_after(actor["session_id"], original["turn_id"], None) + stopped_event = any( + row.get("kind") == "native_loopx.result" + and isinstance(row.get("payload"), Mapping) + and row["payload"].get("resume_turn_key") == key + and row["payload"].get("status") == "stopped" + and row["payload"].get("result_kind") == "wait" + for row in events + ) + if not stopped_event: + return None + for row in reversed(events): + if row.get("kind") != "answer.final": + continue + message = ((row.get("payload") or {}).get("response") or {}).get("message") + parsed = parse_model_json(str(message or "")) + if not isinstance(parsed, dict) or parsed.get("result_kind") not in {"validated_progress", "validated_completion"}: + continue + editorial = parsed.get("editorial_response") + if not isinstance(editorial, Mapping) or editorial.get("request_digest") != request_digest: + continue + proof = { + "turn_key": key, + "result_kind": "wait", + "classification": "no_typed_host_result", + "host_attempt_count": 1, + "generation_verified": True, + "saved_candidate_verified": True, + } + return proof, dict(parsed) + return None + + def has_recoverable_stopped_report( + self, + actor: Mapping[str, Any], + window: Mapping[str, Any], + todo: Mapping[str, Any], + ) -> bool: + """Read-only gate check before acquiring the recovery fence.""" + if todo.get("status") != "open": + return False + original = report_turn(self.store, actor, window) + if not original: + return False + if any(row.get("kind") == "native_loopx.result" and (row.get("payload") or {}).get("status") == "committed" + for row in self.store.events_after(actor["session_id"], original["turn_id"], None)): + return False + request = original.get("loopx_request") or {} + turn_instance_id = request.get("turn_instance_id") + if not isinstance(turn_instance_id, str) or not turn_instance_id: + return False + from ...control_plane.turn_driver.journal_store import find_loopx_turn_key_by_settlement_identity + from ..periodic_report.private_generation import attempt_directory + from .steward_reports import report_service + reports, _ = report_service(self.controller, dict(actor)) + folder = attempt_directory(reports.root, dict(window)) + saved_key = find_loopx_turn_key_by_settlement_identity( + reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"], + todo_id=todo["todo_id"], turn_instance_id=turn_instance_id + ".saved-candidate", + ) + if saved_key is not None and (folder / "private-generation.json").exists(): + return True + key = find_loopx_turn_key_by_settlement_identity( + reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"], + todo_id=todo["todo_id"], turn_instance_id=turn_instance_id, + ) + return bool(key is not None and self._stopped_report_candidate_recovery( + actor, window, original, key=key, folder=folder, root=reports.root, + ) is not None) + + @staticmethod + def _build_replay_runner(normalized_candidate: Mapping[str, Any]): + from ...control_plane.turn_driver.host_candidate import build_result + + replayed = False + + def replay_frozen_candidate(native_request: Mapping[str, Any], _launch_scope: Any) -> dict[str, Any]: + nonlocal replayed + if replayed: + raise ValueError("frozen report candidate replay may execute at most once") + replayed = True + return build_result(native_request, dict(normalized_candidate), host_name="Codex app-server", allow_completion=True) + + return replay_frozen_candidate + + def _settle_stopped_report_candidate( + self, + *, + actor: Mapping[str, Any], + window: Mapping[str, Any], + todo: Mapping[str, Any], + original: Mapping[str, Any], + request: dict[str, Any], + observed: dict[str, Any] | None, + reports: Any, + owner_key: str, + folder: Path, + original_key: str, + stopped_proof: dict[str, Any], + raw_candidate: dict[str, Any], + ) -> dict[str, Any]: + from ...cli import build_parser + from ...cli_commands.turn import handle_turn_command + from ...control_plane.turn_driver import inspect_loopx_turn_journal + from ..periodic_report.private_generation import validate_private_generation + + inspection = inspect_loopx_turn_journal( + reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"], + turn_key=original_key, retry_failed=True, + ) + admitted = self._admit( + request, observed=observed, settlement_recovery=inspection, + frozen_contract_recovery=stopped_proof, + ) + if not (folder / "private-generation.json").exists(): + reports.freeze(request, dict(raw_candidate)) + generation = validate_private_generation(folder) + if raw_candidate["editorial_response"]["request_digest"] != generation["request_digest"]: + raise ValueError("the recovered candidate digest does not match the frozen report") + if not reports.delivery_current(owner_key, window): + raise ValueError("the original report authorization changed before settlement recovery") + saved_instance_id = request["turn_instance_id"] + ".saved-candidate" + argv = ["turn", "run-once", "--goal-id", actor["goal_id"], "--agent-id", actor["agent_id"], + "--project", admitted["workspace_path"], "--host", "generic-cli", + "--turn-instance-id", saved_instance_id, "--todo-id", todo["todo_id"], "--execute"] + if request.get("validation_command") is not None: + argv += ["--validation-command-json", json.dumps(request["validation_command"])] + args = build_parser().parse_args(argv) + args.host, args.session_scope = "codex-app-server", "agent" + result: dict[str, Any] = {} + host_runner = self._build_replay_runner(_normalize_report_kernel_candidate(raw_candidate)) + with self._task_execution_lease(request, original["turn_id"], recovery=True) as lease: + handle_turn_command( + args, registry_path=self.controller.registry_path, + runtime_root_arg=str(reports.root), output_format=lambda *_a, **_kw: "json", + print_payload=lambda value, *_a: result.update(value), native_host_runner=host_runner, + native_session_binding_resolver=lambda _envelope: { + "schema_version": "loopx_turn_session_binding_v0", + "goal_id": actor["goal_id"], "agent_id": actor["agent_id"], + }, + native_task_validator=self._report_validator(request), + native_completion_lease_resolver=lambda: self._task_lease_proof(request, lease), + ) + return result + def recover_report_settlement(self, actor: dict[str, Any], window: dict[str, Any], todo: dict[str, Any]) -> None: """Resume only cached, validated native effects of the original report. @@ -650,13 +829,32 @@ def recover_report_settlement(self, actor: dict[str, Any], window: dict[str, Any for row in self.store.events_after(actor["session_id"], original["turn_id"], None)): return reports, owner_key = report_service(self.controller, actor) - generation = validate_private_generation(attempt_directory(reports.root, window)) - if not reports.delivery_current(owner_key, window): - raise ValueError("the original report authorization changed before settlement recovery") - key = find_loopx_turn_key_by_settlement_identity(reports.root, goal_id=actor["goal_id"], - agent_id=actor["agent_id"], todo_id=todo["todo_id"], turn_instance_id=request["turn_instance_id"]) + folder = attempt_directory(reports.root, window) + saved_key = find_loopx_turn_key_by_settlement_identity( + reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"], + todo_id=todo["todo_id"], turn_instance_id=request["turn_instance_id"] + ".saved-candidate", + ) + key = saved_key or find_loopx_turn_key_by_settlement_identity( + reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"], + todo_id=todo["todo_id"], turn_instance_id=request["turn_instance_id"], + ) if key is None: return + if saved_key is None: + stopped_recovery = self._stopped_report_candidate_recovery( + actor, window, original, key=key, folder=folder, root=reports.root, + ) + if stopped_recovery is not None: + result = self._settle_stopped_report_candidate( + actor=actor, window=window, todo=todo, original=original, request=request, + observed=observed, reports=reports, owner_key=owner_key, folder=folder, + original_key=key, stopped_proof=stopped_recovery[0], raw_candidate=stopped_recovery[1], + ) + self.store.append_event(actor["session_id"], original["turn_id"], kind="native_loopx.result", payload=result) + return + generation = validate_private_generation(folder) + if not reports.delivery_current(owner_key, window): + raise ValueError("the original report authorization changed before settlement recovery") inspection = inspect_loopx_turn_journal(reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"], turn_key=key, retry_failed=True) contract_recovery = ( @@ -674,22 +872,11 @@ def recover_report_settlement(self, actor: dict[str, Any], window: dict[str, Any ) def no_model(*_args): raise ValueError("report settlement recovery must never invoke a model") - if contract_recovery is not None: - from ...control_plane.turn_driver.host_candidate import build_result - - normalized_candidate = contract_recovery[1] - replayed = False - - def replay_frozen_candidate(native_request: Mapping[str, Any], _launch_scope: Any) -> dict[str, Any]: - nonlocal replayed - if replayed: - raise ValueError("frozen report candidate replay may execute at most once") - replayed = True - return build_result(native_request, normalized_candidate, host_name="Codex app-server", allow_completion=True) - - host_runner = replay_frozen_candidate - else: - host_runner = no_model + host_runner = ( + self._build_replay_runner(contract_recovery[1]) + if contract_recovery is not None + else no_model + ) args = build_parser().parse_args(["turn", "run-once", "--goal-id", actor["goal_id"], "--agent-id", actor["agent_id"], "--project", admitted["workspace_path"], "--host", "generic-cli", "--resume-turn-key", key, "--retry-failed-turn", "--execute"]) diff --git a/loopx/control_plane/collaboration/steward_managed_mode.ts b/loopx/control_plane/collaboration/steward_managed_mode.ts index 6a5ece9a0d..db7b409348 100644 --- a/loopx/control_plane/collaboration/steward_managed_mode.ts +++ b/loopx/control_plane/collaboration/steward_managed_mode.ts @@ -502,19 +502,27 @@ function reportSettlementOnly(params: JsonObject, request: JsonObject, todo: Jso if (params.report_frozen_contract_recovery != null) { const frozen = requireJsonObject(params.report_frozen_contract_recovery, "frozen report contract recovery"); const effects = requireJsonObject(recovery.recorded_effects, "native recorded effects"); - const acceptedFrozen = recovery.schema_version === "loopx_turn_journal_inspection_v1" && recovery.ok === true + const commonProof = recovery.schema_version === "loopx_turn_journal_inspection_v1" && recovery.ok === true && recovery.goal_matches === true && recovery.owner_matches === true && recovery.turn_key_matches === true - && recovery.journal_consistent === true && todo.status === "open" && recovery.journal_status === "failed" - && Array.isArray(recovery.completed_phases) && recovery.completed_phases.length === 2 - && recovery.completed_phases[0] === "host_execute" && recovery.completed_phases[1] === "typed_result" - && decision.action === "continue" && decision.resume_from === "host_execute" && decision.reinvoke_host === true + && recovery.journal_consistent === true && todo.status === "open" && effects.host_invoked === true && effects.state_written === false && effects.quota_spent === false && effects.scheduler_acknowledged === false && typeof frozen.turn_key === "string" && ENVELOPED_SHA256_PATTERN.test(frozen.turn_key) - && frozen.result_kind === "validation_failed" && frozen.validation_stage === "host_result_contract" && frozen.host_attempt_count === 1 && frozen.generation_verified === true && frozen.saved_candidate_verified === true; - if (!acceptedFrozen || params.preparing_model_context === true) { + const acceptedFrozen = commonProof && recovery.journal_status === "failed" + && Array.isArray(recovery.completed_phases) && recovery.completed_phases.length === 2 + && recovery.completed_phases[0] === "host_execute" && recovery.completed_phases[1] === "typed_result" + && decision.action === "continue" && decision.resume_from === "host_execute" && decision.reinvoke_host === true + && frozen.result_kind === "validation_failed" && frozen.validation_stage === "host_result_contract"; + const acceptedStoppedCandidate = commonProof && recovery.journal_status === "stopped" + && Array.isArray(recovery.completed_phases) && recovery.completed_phases.length === 3 + && recovery.completed_phases[0] === "host_execute" && recovery.completed_phases[1] === "typed_result" + && recovery.completed_phases[2] === "validation" + && decision.action === "return_existing" && decision.resume_from === null && decision.reinvoke_host === false + && frozen.result_kind === "wait" && frozen.classification === "no_typed_host_result" + && !Object.hasOwn(frozen, "validation_stage"); + if ((!acceptedFrozen && !acceptedStoppedCandidate) || params.preparing_model_context === true) { throw new EffectRuntimeRequestError("original report recovery requires validated remaining effects without a model"); } return true; @@ -523,7 +531,8 @@ function reportSettlementOnly(params: JsonObject, request: JsonObject, todo: Jso && decision.action === "continue" && decision.resume_from === "validation" && Array.isArray(recovery.completed_phases) && recovery.completed_phases.length === 2 && recovery.completed_phases[0] === "host_execute" && recovery.completed_phases[1] === "typed_result"; - const completedGeneration = todo.status === "done" && todo.completion_turn_key === request.turn_instance_id + const completedGeneration = todo.status === "done" + && [request.turn_instance_id, `${String(request.turn_instance_id)}.saved-candidate`].includes(String(todo.completion_turn_key)) && (decision.action === "continue" && ["durable_writeback", "quota_spend", "scheduler_apply", "scheduler_ack"].includes(String(decision.resume_from)) || decision.action === "return_existing" && recovery.journal_status === "committed"); const accepted = recovery.schema_version === "loopx_turn_journal_inspection_v1" && recovery.ok === true diff --git a/loopx/control_plane/turn_driver/host_candidate.py b/loopx/control_plane/turn_driver/host_candidate.py index 829f5b49df..dccadcb2cb 100644 --- a/loopx/control_plane/turn_driver/host_candidate.py +++ b/loopx/control_plane/turn_driver/host_candidate.py @@ -470,6 +470,69 @@ def render_prompt(authority: Mapping[str, Any], *, allow_completion: bool = Fals ) +def _repair_minor_json_delimiters(raw: str) -> str | None: + """Repair only unquoted trailing commas and <=2 extra terminal closers.""" + out: list[str] = [] + stack: list[str] = [] + in_str = False + escaped = False + n = len(raw) + i = 0 + while i < n: + ch = raw[i] + if in_str: + out.append(ch) + if escaped: + escaped = False + elif ch == "\\": + escaped = True + elif ch == '"': + in_str = False + i += 1 + continue + if ch == '"': + in_str = True + out.append(ch) + elif ch == ",": + j = i + 1 + while j < n and raw[j] in " \t\r\n": + j += 1 + if j >= n or raw[j] not in "}]": + out.append(ch) + elif ch in "{[": + stack.append(ch) + out.append(ch) + elif ch in "}]": + expected = ("}" if stack[-1] == "{" else "]") if stack else None + if expected == ch: + stack.pop() + out.append(ch) + else: + tail = raw[i:] + if not all(c in "}] \t\r\n" for c in tail): + return None + tail_closers = [c for c in tail if c in "}]"] + expected_closers = ["}" if o == "{" else "]" for o in reversed(stack)] + if not (0 <= len(tail_closers) - len(expected_closers) <= 2): + return None + pos = 0 + for ec in expected_closers: + while pos < len(tail_closers) and tail_closers[pos] != ec: + pos += 1 + if pos >= len(tail_closers): + return None + pos += 1 + out.extend(expected_closers) + stack.clear() + break + else: + out.append(ch) + i += 1 + if in_str or stack: + return None + return "".join(out) + + def parse_model_json(text: str) -> dict[str, Any] | None: """Parse the host final assistant message as one JSON object. @@ -499,10 +562,17 @@ def parse_model_json(text: str) -> dict[str, Any] | None: end = value.rfind("}") if start == -1 or end == -1 or end <= start: return None + candidate = value[start : end + 1] try: - parsed = json.loads(value[start : end + 1]) + parsed = json.loads(candidate) except json.JSONDecodeError: - return None + repaired = _repair_minor_json_delimiters(candidate) + if repaired is None: + return None + try: + parsed = json.loads(repaired) + except json.JSONDecodeError: + return None return parsed if isinstance(parsed, dict) else None diff --git a/tests/control_plane_ts/steward_reports.test.ts b/tests/control_plane_ts/steward_reports.test.ts index 2419f15714..cb762d67c3 100644 --- a/tests/control_plane_ts/steward_reports.test.ts +++ b/tests/control_plane_ts/steward_reports.test.ts @@ -117,6 +117,23 @@ test("a local calendar report retains its exact original Session, subscription, assert.equal(admitManagedSessionTurn({ ...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: frozenProof, }).observation_only, false); + const stoppedInspection = {schema_version: "loopx_turn_journal_inspection_v1", ok: true, + goal_matches: true, owner_matches: true, turn_key_matches: true, journal_consistent: true, + journal_status: "stopped", completed_phases: ["host_execute", "typed_result", "validation"], + recorded_effects: {host_invoked: true, state_written: false, quota_spent: false, scheduler_acknowledged: false}, + recovery_decision: {action: "return_existing", resume_from: null, reinvoke_host: false}}; + const stoppedProof = {turn_key: "sha256:" + "d".repeat(64), result_kind: "wait", + classification: "no_typed_host_result", host_attempt_count: 1, + generation_verified: true, saved_candidate_verified: true}; + assert.equal(admitManagedSessionTurn({ + ...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof, + }).observation_only, false); + assert.equal(admitManagedSessionTurn({ + ...input, report_todo: {...todo, status: "done", completion_turn_key: request.turn_instance_id + ".saved-candidate"}, + report_settlement_recovery: {schema_version: "loopx_turn_journal_inspection_v1", ok: true, + goal_matches: true, owner_matches: true, turn_key_matches: true, journal_consistent: true, + journal_status: "failed", recovery_decision: {action: "continue", reinvoke_host: false, resume_from: "quota_spend"}}, + }).observation_only, false); for (const invalid of [ {...input, report_frozen_contract_recovery: frozenProof}, {...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: {...frozenProof, saved_candidate_verified: false}}, @@ -124,6 +141,11 @@ test("a local calendar report retains its exact original Session, subscription, {...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: {...frozenProof, host_attempt_count: 2}}, {...input, report_settlement_recovery: {...frozenInspection, recorded_effects: {...frozenInspection.recorded_effects, state_written: true}}, report_frozen_contract_recovery: frozenProof}, {...input, report_todo: {...todo, status: "done"}, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: frozenProof}, + {...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: {...stoppedProof, classification: "other"}}, + {...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: {...stoppedProof, host_attempt_count: 2}}, + {...input, report_settlement_recovery: {...stoppedInspection, recorded_effects: {...stoppedInspection.recorded_effects, state_written: true}}, report_frozen_contract_recovery: stoppedProof}, + {...input, report_todo: {...todo, status: "done"}, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof}, + {...input, preparing_model_context: true, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof}, ]) { assert.throws(() => admitManagedSessionTurn(invalid)); } @@ -303,6 +325,21 @@ test("a model report requires the exact live calendar window and original task, assert.equal(admitStewardManagedTurn({ ...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: frozenProof, }).observation_only, false); + const stoppedInspection = {schema_version: "loopx_turn_journal_inspection_v1", ok: true, + goal_matches: true, owner_matches: true, turn_key_matches: true, + journal_consistent: true, journal_status: "stopped", completed_phases: ["host_execute", "typed_result", "validation"], + recorded_effects: {host_invoked: true, state_written: false, quota_spent: false, scheduler_acknowledged: false}, + recovery_decision: {action: "return_existing", reinvoke_host: false, resume_from: null}}; + const stoppedProof = {turn_key: "sha256:" + "e".repeat(64), result_kind: "wait", + classification: "no_typed_host_result", host_attempt_count: 1, + generation_verified: true, saved_candidate_verified: true}; + assert.equal(admitStewardManagedTurn({ + ...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof, + }).observation_only, false); + assert.equal(admitStewardManagedTurn({ + ...input, report_todo: {...todo, status: "done", completion_turn_key: request.turn_instance_id + ".saved-candidate"}, + report_settlement_recovery: recovery.report_settlement_recovery, + }).observation_only, false); for (const invalid of [ {...input, report_frozen_contract_recovery: frozenProof}, {...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: {...frozenProof, saved_candidate_verified: false}}, @@ -310,6 +347,11 @@ test("a model report requires the exact live calendar window and original task, {...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: {...frozenProof, host_attempt_count: 2}}, {...input, report_settlement_recovery: {...frozenInspection, recorded_effects: {...frozenInspection.recorded_effects, state_written: true}}, report_frozen_contract_recovery: frozenProof}, {...input, report_todo: {...todo, status: "done"}, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: frozenProof}, + {...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: {...stoppedProof, classification: "other"}}, + {...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: {...stoppedProof, host_attempt_count: 2}}, + {...input, report_settlement_recovery: {...stoppedInspection, recorded_effects: {...stoppedInspection.recorded_effects, state_written: true}}, report_frozen_contract_recovery: stoppedProof}, + {...input, report_todo: {...todo, status: "done"}, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof}, + {...input, preparing_model_context: true, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof}, ]) assert.throws(() => admitStewardManagedTurn(invalid)); const changed = {...input, binding: {...p.binding, goal_scope: "all_registered"}, session: {...p.session, active_turn_id: "running", manager_authorization_scope_id: "old_scope", diff --git a/tests/test_dsh_goal_mode.py b/tests/test_dsh_goal_mode.py index d36ccf6ef1..bdb93f6c7e 100644 --- a/tests/test_dsh_goal_mode.py +++ b/tests/test_dsh_goal_mode.py @@ -408,6 +408,43 @@ def test_parse_model_json_tolerates_prose_and_fences() -> None: assert turn_host_adapter.parse_model_json("no json at all") is None +def test_parse_model_json_repairs_minor_delimiters_and_preserves_strings() -> None: + complex_payload = { + "result_kind": "validated_completion", + "details": [ + {"label": "literal", "text": "Keep ,}, and }]}}]}} intact inside string literals."}, + {"label": "second", "text": "Trailing text"}, + ], + } + raw_trailing_comma = ( + '{"result_kind": "validated_completion", "details": [' + '{"label": "literal", "text": "Keep ,}, and }]}}]}} intact inside string literals.",}, ' + '{"label": "second", "text": "Trailing text",},' + '],}' + ) + assert turn_host_adapter.parse_model_json(raw_trailing_comma) == complex_payload + # Extra } inside terminal closer run when outer expectation is ] followed by } + raw_extra_before_array_close = ( + '{"result_kind": "validated_completion", "nested": {"sections": [' + '{"items": [{"summary": "ok",}]}]}]}}' + ) + assert turn_host_adapter.parse_model_json(raw_extra_before_array_close) == { + "result_kind": "validated_completion", + "nested": {"sections": [{"items": [{"summary": "ok"}]}]}, + } + raw_extra_outer_brace = '{"result_kind": "wait", "nested": {"a": [1,]}}}' + assert turn_host_adapter.parse_model_json(raw_extra_outer_brace) == { + "result_kind": "wait", + "nested": {"a": [1]}, + } + # Truncated, unclosed string, mid-object mismatch, or >2 stray closers must fail closed. + assert turn_host_adapter.parse_model_json('{"result_kind": "wait", "items": [{"a": 1}') is None + assert turn_host_adapter.parse_model_json('{"result_kind": "wait", "items": [{"a": 1}}') is None + assert turn_host_adapter.parse_model_json('{"result_kind": "wait", "text": "unterminated}') is None + assert turn_host_adapter.parse_model_json('{"a": [1}, "b": 2}') is None + assert turn_host_adapter.parse_model_json('{"result_kind": "wait"}}}}') is None + + def test_build_result_fails_closed_without_a_typed_candidate() -> None: request = _signed_request() result = turn_host_adapter.build_result(request, None) diff --git a/tests/test_native_steward_reports.py b/tests/test_native_steward_reports.py index 2ea2975782..70af53558a 100644 --- a/tests/test_native_steward_reports.py +++ b/tests/test_native_steward_reports.py @@ -2112,3 +2112,102 @@ def interrupted_owner(*args, **kwargs): assert body == (folder / "report.md").read_bytes() assert read_periodic_report_publication_cursor(runtime_root=reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"])["covered_until"] == window["next_due_at"] + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_stopped_no_typed_result_report_recovers_saved_editorial_candidate_without_model_replay(steward, tmp_path, monkeypatch, provider): + from loopx.capabilities.native_chat.steward_monitor import StewardMonitor + from loopx.control_plane.turn_driver import host_candidate + from loopx.control_plane.turn_driver.journal_store import find_loopx_turn_key_by_settlement_identity, load_turn_journal, turn_journal_path + + store, runtime, bot, transport, binding, capture, _ = steward + sid, actor, reports, _ = configured(steward, tmp_path, monkeypatch, provider) + install_model(steward, tmp_path, sid) + + # Simulate an older parser build failing on one trailing comma + stray outer + # closer, leaving the Turn journal stopped with `no_typed_host_result` while + # `answer.final` preserved the complete `editorial_response` text. + helper = tmp_path / "report_model.py" + raw_source = helper.read_text() + helper.write_text( + raw_source.replace( + ' return json.dumps({"result_kind":', + ' encoded = json.dumps({"result_kind":', + ).replace( + '"editorial_response": response}, ensure_ascii=False)', + '"editorial_response": response}, ensure_ascii=False)\n' + ' assert encoded.endswith("}]}}")\n' + ' return encoded[:-3].replace(\'"tone": "neutral"}\', \'"tone": "neutral",}\', 1) + "}]}}"', + ) + ) + + real_parse = host_candidate.parse_model_json + with monkeypatch.context() as old_parser: + old_parser.setattr(host_candidate, "parse_model_json", lambda _text: None) + first = finish(runtime, pump_steward_monitors(runtime)[0]) + + native = first["response"]["native_loopx"] + assert native["status"] == "stopped" and native["result_kind"] == "wait" + orig_journal = load_turn_journal(turn_journal_path(reports.root, goal_id=actor["goal_id"], turn_key=native["resume_turn_key"])) + assert orig_journal["host_result"]["classification"] == "no_typed_host_result" + window = read_cadence_journal(runtime_root=reports.root, goal_id=actor["goal_id"])["window"] + folder = attempt_directory(reports.root, window) + assert not (folder / "private-generation.json").exists() + assert not (folder / "editorial-response.json").exists() + todo = next(row for row in StewardMonitor(runtime).todos(actor) if row["todo_id"] == first["loopx_request"]["todo_id"]) + assert todo["status"] == "open" + assert transport.steward_reports.reconcile() == 0 + + calls, writes = capture.read_bytes(), list(bot.writes) + runtime._steward_monitor_next_check.clear() + assert real_parse('{"items": [{"ok": true,}]}}') == {"items": [{"ok": True}]} + assert not pump_steward_monitors(runtime) + assert capture.read_bytes() == calls + assert bot.writes == writes + + assert validate_private_generation(folder) + assert (folder / "report.md").is_file() + todo_after = next(row for row in StewardMonitor(runtime).todos(actor) if row["todo_id"] == todo["todo_id"]) + assert todo_after["status"] == "done" + assert todo_after["completion_turn_key"] == first["loopx_request"]["turn_instance_id"] + ".saved-candidate" + saved_key = find_loopx_turn_key_by_settlement_identity( + reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"], + todo_id=todo["todo_id"], turn_instance_id=todo_after["completion_turn_key"], + ) + assert saved_key and load_turn_journal(turn_journal_path(reports.root, goal_id=actor["goal_id"], turn_key=saved_key))["status"] == "committed" + + assert transport.steward_reports.reconcile() == 1 + assert len(bot.writes) > len(writes) + cursor = read_periodic_report_publication_cursor(runtime_root=reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"]) + assert cursor and cursor["covered_until"] == window["due_at"] + runtime._steward_monitor_next_check.clear() + assert not pump_steward_monitors(runtime) + assert transport.steward_reports.reconcile() == 0 + assert capture.read_bytes() == calls + + +def test_stopped_report_without_valid_saved_editorial_candidate_stays_closed(steward, tmp_path, monkeypatch): + from loopx.capabilities.native_chat.steward_monitor import StewardMonitor + from loopx.control_plane.turn_driver import host_candidate + + store, runtime, bot, transport, binding, capture, _ = steward + sid, actor, reports, _ = configured(steward, tmp_path, monkeypatch, "sqlite") + install_model(steward, tmp_path, sid) + helper = tmp_path / "report_model.py" + helper.write_text('def fake_report_model(_params):\n return "{\\"result_kind\\": \\"validated_completion\\", \\"truncated\\": "\n') + + with monkeypatch.context() as old_parser: + old_parser.setattr(host_candidate, "parse_model_json", lambda _text: None) + first = finish(runtime, pump_steward_monitors(runtime)[0]) + + assert first["response"]["native_loopx"]["status"] == "stopped" + window = read_cadence_journal(runtime_root=reports.root, goal_id=actor["goal_id"])["window"] + folder = attempt_directory(reports.root, window) + calls, writes = capture.read_bytes(), list(bot.writes) + runtime._steward_monitor_next_check.clear() + assert not pump_steward_monitors(runtime) + assert transport.steward_reports.reconcile() == 0 + assert capture.read_bytes() == calls and bot.writes == writes + assert not (folder / "private-generation.json").exists() + todo = next(row for row in StewardMonitor(runtime).todos(actor) if row["todo_id"] == first["loopx_request"]["todo_id"]) + assert todo["status"] == "open"