From c8877c21fbda45ea614646fdbc5668acec50dc54 Mon Sep 17 00:00:00 2001 From: Duang777 Date: Wed, 18 Nov 2026 02:55:45 +0000 Subject: [PATCH 1/4] fix(quota): fence historical ACK writeback order by GoalRef Same-goal instances share one rollout log. The historical ACK reader bound the first matching refresh, so a retired instance's earlier writeback could clear writeback_follows_admission and drop the current same-second ACK. Keep only the quota owner's own events and run-index rows. Signed-off-by: Duang777 --- loopx/control_plane/quota/live_decision.py | 3 +- .../work_items/autonomous_replan_ack.py | 94 +++++++++++++-- .../work_items/semantic_replan_writeback.py | 6 + loopx/state_refresh.py | 1 + .../test_autonomous_replan_ack.py | 107 ++++++++++++++++++ .../test_quota_settlement_cli.py | 98 ++++++++++++++++ 6 files changed, 299 insertions(+), 10 deletions(-) diff --git a/loopx/control_plane/quota/live_decision.py b/loopx/control_plane/quota/live_decision.py index fde3e9c491..29d65e8ed4 100644 --- a/loopx/control_plane/quota/live_decision.py +++ b/loopx/control_plane/quota/live_decision.py @@ -797,7 +797,8 @@ def build_live_quota_should_run_decision( runtime_root, goal_id=goal_id, agent_id=agent_id, obligation_id=receipt_bound_replan_obligation_id, guard_recorded_at=receipt_bound_guard_fields["receipt_bound_guard_recorded_at"], - guard_turn_instance_id=receipt_bound_guard_fields["receipt_bound_guard_turn_instance_id"]) + guard_turn_instance_id=receipt_bound_guard_fields["receipt_bound_guard_turn_instance_id"], + goal_ref=goal_ref) fresh_operator_inbox_read = _fresh_operator_inbox_read_required( turn_start_hook_dispatch ) diff --git a/loopx/control_plane/work_items/autonomous_replan_ack.py b/loopx/control_plane/work_items/autonomous_replan_ack.py index 2c16a75ccc..6ab384fb2b 100644 --- a/loopx/control_plane/work_items/autonomous_replan_ack.py +++ b/loopx/control_plane/work_items/autonomous_replan_ack.py @@ -1,9 +1,11 @@ from __future__ import annotations +from collections.abc import Mapping from pathlib import Path from typing import Any, NamedTuple from ..effect_runtime import effect_runtime_result +from ..goals.goal_ref_validation import exact_goal_ref from ..runtime.time import parse_timestamp from .progress_observation import FRESH_VISION_PATH_DISPOSITIONS @@ -272,14 +274,64 @@ def _event_details(event: dict[str, Any]) -> dict[str, Any]: return details if isinstance(details, dict) else {} +def _owner_goal_ref( + goal_ref: Mapping[str, Any] | None, *, goal_id: str, +) -> dict[str, str] | None: + """Normalize the quota owner. None is the alias owner.""" + if goal_ref is None: + return None + owner = exact_goal_ref( + str(goal_ref.get("goal_id") or ""), + str(goal_ref.get("goal_instance_id") or ""), + ) + if owner["goal_id"] != str(goal_id or "").strip(): + raise ValueError("historical ACK GoalRef does not match goal_id") + return owner + + +def _record_owned_by_goal_ref( + record: Mapping[str, Any], owner: dict[str, str] | None, +) -> bool: + """Match quotaOwnerOwnsProjection for one rollout or run-index row. + + An alias owner keeps only rows with no goal_ref. An exact owner keeps + only rows whose goal_ref is that exact Goal instance. + """ + if owner is None: + return "goal_ref" not in record + value = record.get("goal_ref") + if "goal_ref" not in record or not isinstance(value, Mapping): + return False + try: + parsed = exact_goal_ref( + str(value.get("goal_id") or ""), + str(value.get("goal_instance_id") or ""), + ) + except ValueError: + return False + return parsed == owner + + +def _owned_records( + records: list[dict[str, Any]] | None, owner: dict[str, str] | None, +) -> list[dict[str, Any]]: + return [ + record for record in records or [] + if isinstance(record, dict) and _record_owned_by_goal_ref(record, owner) + ] + + def _admission_event_index( events: list[dict[str, Any]], *, goal_id: str, agent_id: str, turn_instance_id: str, recorded_at: str, + owner_goal_ref: dict[str, str] | None, ) -> int | None: if not turn_instance_id or not recorded_at: return None found: int | None = None for index, event in enumerate(events): + if not _record_owned_by_goal_ref(event, owner_goal_ref): + continue if ( event.get("event_kind") == "quota_should_run" and event.get("goal_id") == goal_id @@ -297,8 +349,11 @@ def _binding_text(value: Any) -> str: def _source_run( runs: list[dict[str, Any]] | None, *, turn_instance_id: str, obligation_id: str, + owner_goal_ref: dict[str, str] | None, ) -> dict[str, Any]: for run in runs or []: + if not _record_owned_by_goal_ref(run, owner_goal_ref): + continue if _binding_text(run.get("turn_instance_id")) != turn_instance_id: continue ack = run.get("autonomous_replan_ack") @@ -311,6 +366,7 @@ def _source_run( def _expected_todo_receipt( run: dict[str, Any], events: list[dict[str, Any]], *, goal_id: str, agent_id: str, turn_instance_id: str, obligation_id: str, + owner_goal_ref: dict[str, str] | None, ) -> tuple[str, str]: """Todo command identity for this ACK, filled from its heartbeat receipt. @@ -328,6 +384,8 @@ def _expected_todo_receipt( if todo and effect: return todo, effect for event in events: + if not _record_owned_by_goal_ref(event, owner_goal_ref): + continue if ( event.get("event_kind") != "quota_should_run" or event.get("goal_id") != goal_id @@ -352,6 +410,7 @@ def _expected_todo_receipt( def _writeback_event_is_bound( event: dict[str, Any], run: dict[str, Any], events: list[dict[str, Any]], *, goal_id: str, agent_id: str, obligation_id: str, turn_instance_id: str, + owner_goal_ref: dict[str, str] | None, ) -> bool: """Require one positive identity shared with the accepted ACK. @@ -359,6 +418,8 @@ def _writeback_event_is_bound( must equal the ACK receipt. Missing fields do not count as agreement. """ + if not _record_owned_by_goal_ref(event, owner_goal_ref): + return False if ( event.get("event_kind") != "refresh_state" or event.get("goal_id") != goal_id @@ -378,7 +439,8 @@ def _writeback_event_is_bound( return False expected_todo, expected_effect = _expected_todo_receipt( run, events, goal_id=goal_id, agent_id=agent_id, - turn_instance_id=turn_instance_id, obligation_id=obligation_id) + turn_instance_id=turn_instance_id, obligation_id=obligation_id, + owner_goal_ref=owner_goal_ref) return bool( expected_todo and expected_effect and event_todo == expected_todo and event_effect == expected_effect @@ -388,14 +450,18 @@ def _writeback_event_is_bound( def _original_writeback_event_index( events: list[dict[str, Any]], runs: list[dict[str, Any]] | None, *, goal_id: str, agent_id: str, obligation_id: str, turn_instance_id: str, + owner_goal_ref: dict[str, str] | None, ) -> int | None: if not turn_instance_id or not obligation_id: return None - run = _source_run(runs, turn_instance_id=turn_instance_id, obligation_id=obligation_id) + run = _source_run( + runs, turn_instance_id=turn_instance_id, obligation_id=obligation_id, + owner_goal_ref=owner_goal_ref) for index, event in enumerate(events): if _writeback_event_is_bound( event, run, events, goal_id=goal_id, agent_id=agent_id, obligation_id=obligation_id, turn_instance_id=turn_instance_id, + owner_goal_ref=owner_goal_ref, ): return index return None @@ -406,6 +472,7 @@ def attach_historical_ack_writeback_order( goal_id: str, agent_id: str, obligation_id: str, guard_recorded_at: str | None, guard_turn_instance_id: str | None, runs: list[dict[str, Any]] | None = None, + goal_ref: Mapping[str, Any] | None = None, ) -> None: """Copy the original refresh event order onto matching ACK candidates. @@ -413,20 +480,25 @@ def attach_historical_ack_writeback_order( event with admission; this reader only selects the bound original event. """ + owner = _owner_goal_ref(goal_ref, goal_id=goal_id) + owned_events = _owned_records(events, owner) + owned_runs = _owned_records(runs, owner) admission_index = _admission_event_index( - events, goal_id=goal_id, agent_id=agent_id, + owned_events, goal_id=goal_id, agent_id=agent_id, turn_instance_id=str(guard_turn_instance_id or "").strip(), recorded_at=str(guard_recorded_at or "").strip(), + owner_goal_ref=owner, ) for candidate in candidates: turn_instance_id = _binding_text(candidate.get("turn_instance_id")) evidence_index = _original_writeback_event_index( - events, runs, goal_id=goal_id, agent_id=agent_id, obligation_id=obligation_id, - turn_instance_id=turn_instance_id, + owned_events, owned_runs, goal_id=goal_id, agent_id=agent_id, + obligation_id=obligation_id, turn_instance_id=turn_instance_id, + owner_goal_ref=owner, ) if evidence_index is None: continue - event = events[evidence_index] + event = owned_events[evidence_index] candidate["writeback_recorded_at"] = event.get("recorded_at") candidate["writeback_goal_id"] = event.get("goal_id") candidate["writeback_agent_id"] = event.get("agent_id") @@ -443,13 +515,16 @@ def historical_ack_candidates_with_writeback_order( guard_scoped: bool = True, runtime_root: Path | str | None = None, source_route: Any = None, guard_recorded_at: str | None = None, guard_turn_instance_id: str | None = None, + goal_ref: Mapping[str, Any] | None = None, ) -> list[dict[str, Any]]: """Project obligation candidates and attach their original writeback order.""" from ...rollout_event_log import load_rollout_events, rollout_event_log_path + owner = _owner_goal_ref(goal_ref, goal_id=goal_id) + owned_runs = _owned_records(newest_first_runs, owner) selected = _binding_text(obligation_id) if guard_scoped else "" candidates = autonomous_replan_ack_candidates_for_obligation( - newest_first_runs, goal_id=goal_id, agent_id=agent_id, obligation_id=selected or None) + owned_runs, goal_id=goal_id, agent_id=agent_id, obligation_id=selected or None) route = source_route if isinstance(source_route, dict) else {} root_text = runtime_root or route.get("runtime_root") root = Path(str(root_text)) if root_text else None @@ -459,7 +534,7 @@ def historical_ack_candidates_with_writeback_order( candidates, load_rollout_events(rollout_event_log_path(root, goal_id)), goal_id=goal_id, agent_id=agent_id, obligation_id=selected, guard_recorded_at=guard_recorded_at, guard_turn_instance_id=guard_turn_instance_id, - runs=newest_first_runs, + runs=owned_runs, goal_ref=owner, ) return candidates @@ -467,6 +542,7 @@ def historical_ack_candidates_with_writeback_order( def load_receipt_replan_ack_candidates( runtime_root: Path, *, goal_id: str, agent_id: str, obligation_id: str, guard_recorded_at: str | None = None, guard_turn_instance_id: str | None = None, + goal_ref: Mapping[str, Any] | None = None, ) -> list[dict[str, Any]]: """Read exact recovery candidates without widening ordinary status history.""" from ...history import load_index @@ -478,7 +554,7 @@ def load_receipt_replan_ack_candidates( return historical_ack_candidates_with_writeback_order( newest_first, goal_id=goal_id, agent_id=agent_id, obligation_id=obligation_id, runtime_root=runtime_root, guard_recorded_at=guard_recorded_at, - guard_turn_instance_id=guard_turn_instance_id, + guard_turn_instance_id=guard_turn_instance_id, goal_ref=goal_ref, ) diff --git a/loopx/control_plane/work_items/semantic_replan_writeback.py b/loopx/control_plane/work_items/semantic_replan_writeback.py index 22eee62fda..0db409bf04 100644 --- a/loopx/control_plane/work_items/semantic_replan_writeback.py +++ b/loopx/control_plane/work_items/semantic_replan_writeback.py @@ -235,6 +235,7 @@ def qualify_replan_writeback( receipt_bound_replan_review: Mapping[str, Any] | None = None, replan_source_route: Mapping[str, Any] | None = None, replan_explore_reader: Callable[[str, str | None], Mapping[str, Any] | None] | None = None, + goal_ref: Mapping[str, Any] | None = None, ) -> tuple[dict[str, Any] | None, dict[str, Any] | None]: """Return the shared open obligation and the writeback's typed delta. @@ -300,6 +301,7 @@ def qualify_replan_writeback( source_route=replan_source_route, guard_recorded_at=receipt_bound_guard_recorded_at, guard_turn_instance_id=receipt_bound_guard_turn_instance_id, + goal_ref=goal_ref, ) if capability_evidence is not None: status_payload["bounded_research_frontier"] = capability_evidence.frontier @@ -463,6 +465,7 @@ def enforce_open_replan_writeback( effective_turn_cadence: dict[str, Any] | None = None, replan_source_route: Mapping[str, Any] | None = None, replan_explore_reader: Callable[[str, str | None], Mapping[str, Any] | None] | None = None, + goal_ref: Mapping[str, Any] | None = None, ) -> dict[str, Any] | None: """Fail closed unless concrete typed evidence satisfies the selected replan. @@ -494,6 +497,7 @@ def enforce_open_replan_writeback( receipt_bound_guard_turn_instance_id=receipt_bound_guard_turn_instance_id, receipt_bound_replan_review=receipt_bound_replan_review, replan_source_route=replan_source_route, replan_explore_reader=replan_explore_reader, + goal_ref=goal_ref, ) if not obligation: if isinstance(semantic_delta, dict) and semantic_delta.get("accepted") is True: @@ -558,6 +562,7 @@ def qualify_refresh_replan_writeback( replan_explore_reader: Callable[[str, str | None], Mapping[str, Any] | None] | None = None, capability_evidence: CapabilityReplanEvidence | None = None, guard_capability: Mapping[str, Any] | None = None, + goal_ref: Mapping[str, Any] | None = None, ) -> RefreshReplanQualification: """Qualify one refresh's replan delta and accountable settlement outcome.""" @@ -630,6 +635,7 @@ def qualify_refresh_replan_writeback( receipt_bound_guard_turn_instance_id=receipt_bound_guard_turn_instance_id, receipt_bound_replan_review=receipt_bound_replan_review, replan_source_route=replan_source_route, replan_explore_reader=replan_explore_reader, + goal_ref=goal_ref, ) if semantic_delta: if semantic_delta.get("retirement") and delivery_outcome != "outcome_gap": diff --git a/loopx/state_refresh.py b/loopx/state_refresh.py index 2859286fa8..51139ea5b4 100644 --- a/loopx/state_refresh.py +++ b/loopx/state_refresh.py @@ -1139,6 +1139,7 @@ def read_explore_context(read_goal_id: str, read_agent_id: str | None): "explore_review", {}).get("result_pages")) replan_qualification = qualify_refresh_replan_writeback( + goal_ref=goal_ref, replan_explore_reader=read_explore_context, replan_source_route={"registry": str(registry_path), "runtime_root": str(runtime_root)}, capability_evidence=capability_evidence, diff --git a/tests/control_plane/test_autonomous_replan_ack.py b/tests/control_plane/test_autonomous_replan_ack.py index 70b0594896..fdf4029a13 100644 --- a/tests/control_plane/test_autonomous_replan_ack.py +++ b/tests/control_plane/test_autonomous_replan_ack.py @@ -1,5 +1,7 @@ from __future__ import annotations +import json +from pathlib import Path from typing import Any import pytest @@ -401,3 +403,108 @@ def test_same_second_candidate_keeps_the_todo_bound_writeback_order() -> None: runs=[sparse_run], ) assert "writeback_recorded_at" not in blank + + +def test_exact_goal_instance_same_second_ack_uses_its_own_writeback(tmp_path: Path) -> None: + """Two instances can mint one obligation id and reuse the ACK Turn. + + The exact owner records its same-second ACK only from its own refresh. + A retired instance's earlier refresh, and an alias row with no GoalRef, + stay out of that order. + """ + from loopx.control_plane.work_items.autonomous_replan_ack import ( + load_receipt_replan_ack_candidates, + ) + from loopx.control_plane.work_items.autonomous_replan_obligation import ( + ensure_replan_novelty_policy, + ) + from loopx.control_plane.work_items.replan_semantics_codec import ( + project_replan_semantics, + ) + from loopx.rollout_event_log import build_rollout_event + + obligation_source = { + "schema_version": "autonomous_replan_obligation_v0", + "agent_id": AGENT_ID, + "frontier_identity": "shared-frontier", + "triggers": [{"kind": "long_todo_chain", "frontier_revision": LONG_CHAIN_REVISION}], + } + obligation = ensure_replan_novelty_policy(dict(obligation_source))["obligation_id"] + assert obligation == ensure_replan_novelty_policy({ + **obligation_source, "goal_instance_id": "ginst_" + "b" * 32, + })["obligation_id"] + current = {"goal_id": GOAL_ID, "goal_instance_id": "ginst_" + "b" * 32} + retired = {"goal_id": GOAL_ID, "goal_instance_id": "ginst_" + "a" * 32} + guard_turn = "guided-start:original" + ack_turn = "guided-start:later" + admitted_at = "2026-08-01T00:00:01.500000Z" + runtime = tmp_path + log_path = runtime / "goals" / GOAL_ID / "rollout-event-log.jsonl" + index_path = runtime / "goals" / GOAL_ID / "runs" / "index.jsonl" + index_path.parent.mkdir(parents=True) + + def ack_run(goal_ref: dict[str, str], generated_at: str, outcome: str) -> dict[str, Any]: + return { + "goal_id": GOAL_ID, "agent_id": AGENT_ID, "turn_instance_id": ack_turn, + "generated_at": generated_at, "goal_ref": goal_ref, + "autonomous_replan_ack": { + "schema_version": "autonomous_replan_ack_v0", "recorded": True, + "source": "refresh_state", + "semantic_delta": { + "schema_version": "replan_semantic_delta_v0", "accepted": True, + "obligation_id": obligation, "outcomes": [outcome], + "satisfying_outcomes": [outcome], + }, + }, + } + + index_path.write_text("".join(json.dumps(row) + "\n" for row in ( + ack_run(current, "2026-08-01T00:00:01Z", FRESH_VISION_PATH_OUTCOME), + ack_run(retired, "2026-08-01T00:00:03Z", "new_surface"), + )), encoding="utf-8") + refresh = { + "autonomous_replan_recorded": True, "replan_obligation_id": obligation, + } + events = [ + build_rollout_event( + goal_id=GOAL_ID, event_kind="refresh_state", agent_id=AGENT_ID, + run_id=ack_turn, recorded_at="2026-08-01T00:00:00.100000Z", details=refresh, + ), + build_rollout_event( + goal_id=GOAL_ID, event_kind="refresh_state", goal_ref=retired, + agent_id=AGENT_ID, run_id=ack_turn, recorded_at="2026-08-01T00:00:00.200000Z", + details=refresh, + ), + build_rollout_event( + goal_id=GOAL_ID, event_kind="quota_should_run", goal_ref=current, + agent_id=AGENT_ID, run_id=guard_turn, recorded_at=admitted_at, + ), + build_rollout_event( + goal_id=GOAL_ID, event_kind="refresh_state", goal_ref=current, + agent_id=AGENT_ID, run_id=ack_turn, recorded_at="2026-08-01T00:00:01.900000Z", + details=refresh, + ), + ] + log_path.write_text("".join(json.dumps(event) + "\n" for event in events), encoding="utf-8") + + def decision_for(goal_ref: dict[str, str] | None) -> dict[str, Any]: + candidates = load_receipt_replan_ack_candidates( + runtime, goal_id=GOAL_ID, agent_id=AGENT_ID, obligation_id=obligation, + guard_recorded_at=admitted_at, guard_turn_instance_id=guard_turn, + goal_ref=goal_ref, + ) + return project_replan_semantics({ + "operation": "receipt_bound_obligation", "guard_scoped": True, + "replay_phase": "open", "selected_obligation_id": obligation, + "goal_id": GOAL_ID, "agent_id": AGENT_ID, "guard_recorded_at": admitted_at, + "guard_turn_instance_id": guard_turn, "current_obligation": None, + "transition_candidates": [ + {"historical_ack": candidate} for candidate in candidates + ], + }) + + packet = decision_for(current)["obligation"]["settlement_action_packet"] + assert packet["decision"] == "historical_ack_recorded" + assert packet["semantic_delta"]["outcomes"] == [FRESH_VISION_PATH_OUTCOME] + with pytest.raises(Exception, match="conflicts with the current autonomous replan"): + decision_for(None) diff --git a/tests/control_plane/test_quota_settlement_cli.py b/tests/control_plane/test_quota_settlement_cli.py index b6097877a9..14be911af0 100644 --- a/tests/control_plane/test_quota_settlement_cli.py +++ b/tests/control_plane/test_quota_settlement_cli.py @@ -9381,3 +9381,101 @@ def test_same_second_historical_ack_requires_writeback_after_admission( rc, spent = _run_generated_cli(spend, registry_path=registry) assert rc == 0 and spent["ok"] is True, spent assert _spend_run_count(runtime) == before + 1 + + +INSTANCE_A = "ginst_" + "a" * 32 + + +def test_retired_instance_writeback_cannot_drop_same_second_historical_ack( + tmp_path: Path, +) -> None: + """A retired Goal instance's earlier refresh must not hide the current ACK. + + Both instances share goals//rollout-event-log.jsonl. This + should-run is the alias owner: its own refresh has no goal_ref. The + retired refresh reuses the ACK Turn and obligation and sits before + admission. The current generated_at falls in the admission second, so + acceptance depends on the alias owner's own refresh following admission. + """ + project, runtime, registry = _write_fixture(tmp_path) + _configure_selected_todo_replan_fixture(project, registry, stride_strategy="off") + _initialize_git_checkout(project) + _initialize_fixture_authority(project, runtime, provider="file") + original_turn = "turn-historical-same-second" + admitted = _bind_selected_replan_guard(registry, runtime, project, original_turn) + obligation = admitted["replan_action_packet"]["obligation_id"] + recorded_at = admitted["heartbeat_receipt"]["recorded_at"] + recorded = datetime.fromisoformat(recorded_at.replace("Z", "+00:00")) + later_turn = "later-accepted-same-second" + later_admitted = _bind_selected_replan_guard(registry, runtime, project, later_turn) + assert later_admitted["replan_action_packet"]["obligation_id"] == obligation + vision = project / "vision.json" + vision.write_text(json.dumps({ + "schema_version": "goal_vision_replan_contract_v0", + "state": "vision_patch_proposed", + "vision_patch": {"acceptance_summary": "Verified fixture artifact; original work remains open."}, + "todo_delta": [f"retain:{SELECTED_REPLAN_TODO_ID}"], + "path_delta": { + "outcome": "replan", + "prior_assumption": "Fixture work needs a verified next route.", + "observed_reality": "A fixture check supports continuing the existing work.", + "retained": ["Original requirements and the existing Todo."], + "evidence_refs": ["evidence:fixture-check"], + }, + }), encoding="utf-8") + rc, refreshed = _run_cli( + registry, runtime, "refresh-state", "--goal-id", GOAL_ID, "--agent-id", AGENT_ID, + "--todo-id", SELECTED_REPLAN_TODO_ID, "--turn-instance-id", later_turn, + "--classification", "bounded_replan_progress", "--delivery-batch-scale", "single_surface", + "--delivery-outcome", "outcome_progress", "--delivery-workspace-path", str(project), + "--delivery-boundary", "semantic_closeout", "--agent-vision-json", str(vision), + "--autonomous-replan-recorded", "--repair-delta-kind", "goal_vision_patch", + "--no-global-sync", "--suppress-external-sinks", + ) + assert rc == 0 and refreshed["appended"] is True, refreshed + assert refreshed["autonomous_replan_ack"]["semantic_delta"]["obligation_id"] == obligation + same_second = recorded.replace(microsecond=0).astimezone(timezone.utc).isoformat().replace("+00:00", "Z") + index_path = runtime / "goals" / GOAL_ID / "runs" / "index.jsonl" + rows = [json.loads(line) for line in index_path.read_text(encoding="utf-8").splitlines() if line.strip()] + matched_runs = [row for row in rows if row.get("turn_instance_id") == later_turn] + assert matched_runs + for row in matched_runs: + row["generated_at"] = same_second + source = Path(row["json_path"]) + payload = json.loads(source.read_text(encoding="utf-8")) + payload["generated_at"] = same_second + source.write_text(json.dumps(payload), encoding="utf-8") + index_path.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8") + written_at = (recorded + timedelta(microseconds=1)).astimezone(timezone.utc).isoformat().replace("+00:00", "Z") + retired_at = (recorded - timedelta(seconds=1)).astimezone(timezone.utc).isoformat().replace("+00:00", "Z") + log_path = runtime / "goals" / GOAL_ID / "rollout-event-log.jsonl" + events = [json.loads(line) for line in log_path.read_text(encoding="utf-8").splitlines() if line.strip()] + event = next(item for item in events + if item.get("event_kind") == "refresh_state" and item.get("run_id") == later_turn) + assert not str((event.get("details") or {}).get("replan_obligation_id") or "").strip() + assert event["details"]["autonomous_replan_recorded"] is True + assert "goal_ref" not in event + events.remove(event) + event["recorded_at"] = written_at + admission_index = next(index for index, item in enumerate(events) + if item.get("event_kind") == "quota_should_run" and item.get("run_id") == original_turn + and item.get("recorded_at") == recorded_at) + retired = json.loads(json.dumps(event)) + retired["goal_ref"] = {"goal_id": GOAL_ID, "goal_instance_id": INSTANCE_A} + retired["recorded_at"] = retired_at + retired["event_id"] = "retired-instance-writeback" + retired["details"] = { + **retired["details"], + "autonomous_replan_recorded": True, + "replan_obligation_id": obligation, + } + events.insert(admission_index, retired) + events.append(event) + log_path.write_text("".join(json.dumps(item) + "\n" for item in events), encoding="utf-8") + guard_args = ("quota", "should-run", "--codex-app", "--goal-id", GOAL_ID, + "--agent-id", AGENT_ID, "--turn-instance-id", original_turn, + "--todo-id", SELECTED_REPLAN_TODO_ID) + rc, recovery = _run_cli(registry, runtime, *guard_args) + decision = (recovery.get("replan_action_packet") or {}).get("decision") + assert rc == 0, recovery + assert decision == "historical_ack_recorded", recovery.get("replan_action_packet") From 188a251724f8ebb25512eacd6485bcc52408576d Mon Sep 17 00:00:00 2001 From: Duang777 Date: Wed, 18 Nov 2026 04:23:01 +0000 Subject: [PATCH 2/4] fix(quota): ignore another instance's ACK during replan recovery A retired instance's newer confirmation was compacted without goal_ref, so receipt recovery treated it as this owner's latest ACK and stopped the original replan. Keep goal_ref on the run projection, drop unowned recorded ACKs before rebuilding the open review, and use the supplied candidate list even when that list is empty. Signed-off-by: Duang777 --- .../goals/goal_frontier/__init__.py | 25 +++-- loopx/control_plane/quota/live_decision.py | 70 ++++++++++++++ loopx/control_plane/runtime/run_compaction.py | 1 + .../work_items/autonomous_replan_ack.py | 62 +++++++++--- .../work_items/semantic_replan_writeback.py | 10 +- .../test_quota_settlement_cli.py | 96 +++++++++++++++++++ 6 files changed, 240 insertions(+), 24 deletions(-) diff --git a/loopx/control_plane/goals/goal_frontier/__init__.py b/loopx/control_plane/goals/goal_frontier/__init__.py index 8bf8ffd925..9eb33ddda9 100644 --- a/loopx/control_plane/goals/goal_frontier/__init__.py +++ b/loopx/control_plane/goals/goal_frontier/__init__.py @@ -1457,6 +1457,19 @@ def build_goal_frontier_projection_context_from_status( agent_id=agent_id, neutral_classifications=neutral_replan_ack_classifications, ) + # Receipt recovery supplies the owned ACK history, including an empty list. + # The status snapshot's newest ACK can belong to another Goal instance, so + # it must not satisfy or replace this Turn's review. + scoped_ack_history = ( + status_payload.get("receipt_bound_replan_ack_candidates") + if receipt_bound_replan_obligation_id + else None + ) + if isinstance(scoped_ack_history, list): + latest_agent_replan_ack = next( + (candidate for candidate in scoped_ack_history if isinstance(candidate, dict)), + None, + ) latest_replan_ack_feedback = latest_replan_ack_feedback_from_status_payload( status_payload, goal_id=goal_id, @@ -1667,11 +1680,7 @@ def build_goal_frontier_projection_context_from_status( # current-frontier transition: the settlement owner validates its exact # Goal, Agent, obligation, chronology, and Turn before it can close the old # writeback. Projected asset ACKs never enter this candidate. - raw_historical_ack_candidates = ( - status_payload.get("receipt_bound_replan_ack_candidates") - if receipt_bound_replan_obligation_id - else None - ) + raw_historical_ack_candidates = scoped_ack_history historical_ack_candidates = [ {"historical_ack": dict(candidate)} for candidate in ( @@ -1681,7 +1690,11 @@ def build_goal_frontier_projection_context_from_status( )[:8] if isinstance(candidate, dict) ] - if latest_agent_replan_ack is not None and receipt_bound_replan_obligation_id: + if ( + latest_agent_replan_ack is not None + and receipt_bound_replan_obligation_id + and not isinstance(raw_historical_ack_candidates, list) + ): historical_ack_candidates.append( {"historical_ack": latest_agent_replan_ack} ) diff --git a/loopx/control_plane/quota/live_decision.py b/loopx/control_plane/quota/live_decision.py index 29d65e8ed4..1f5ccc8c1c 100644 --- a/loopx/control_plane/quota/live_decision.py +++ b/loopx/control_plane/quota/live_decision.py @@ -666,6 +666,73 @@ def _project_unbound_work_fields(payload: dict[str, Any]) -> None: recommendation["spend_policy"] = reason +def _restore_replan_obligation_without_foreign_acks( + status_payload: dict[str, Any], *, goal_id: str, agent_id: str, + goal_ref: Mapping[str, Any] | None, +) -> None: + """Recompute the open review after removing another instance's ACK rows.""" + from ...status import autonomous_replan_obligation_from_runs + from ..work_items.autonomous_replan_ack import drop_unowned_recorded_replan_acks + from ..work_items.project_asset import attach_active_state_project_asset_fields + + run_history = status_payload.get("run_history") + goals = run_history.get("goals") if isinstance(run_history, dict) else None + goal = next( + ( + item for item in goals + if isinstance(item, dict) and str(item.get("id") or "") == goal_id + ), + None, + ) if isinstance(goals, list) else None + if not isinstance(goal, dict) or not isinstance(goal.get("latest_runs"), list): + return + filtered, changed = drop_unowned_recorded_replan_acks( + goal["latest_runs"], goal_id=goal_id, goal_ref=goal_ref, + ) + if not changed: + return + goal["latest_runs"] = filtered + kept_ack_times = { + run.get("generated_at") + for run in filtered + if isinstance(run.get("autonomous_replan_ack"), dict) + } + semantic = goal.get("semantic_history") + agents = semantic.get("agents") if isinstance(semantic, dict) else None + if isinstance(agents, list): + for context in agents: + if not isinstance(context, dict): + continue + ack_run = context.get("latest_autonomous_replan_ack_run") + if ( + isinstance(ack_run, dict) + and ack_run.get("generated_at") not in kept_ack_times + ): + context.pop("latest_autonomous_replan_ack_run", None) + queue = status_payload.get("attention_queue") + items = queue.get("items") if isinstance(queue, dict) else None + item = next( + ( + entry for entry in items + if isinstance(entry, dict) and entry.get("goal_id") == goal_id + ), + None, + ) if isinstance(items, list) else None + if not isinstance(item, dict): + return + for target in (item, item.get("project_asset")): + if not isinstance(target, dict): + continue + target.pop("autonomous_replan_obligation", None) + target.pop("autonomous_replan_obligations_by_agent", None) + target.pop("autonomous_replan_ack", None) + attach_active_state_project_asset_fields( + item, latest_runs=filtered, + autonomous_replan_obligation_from_runs=autonomous_replan_obligation_from_runs, + agent_lane_id=agent_id, + ) + + def build_live_quota_should_run_decision( status_payload: dict[str, Any], *, @@ -799,6 +866,9 @@ def build_live_quota_should_run_decision( guard_recorded_at=receipt_bound_guard_fields["receipt_bound_guard_recorded_at"], guard_turn_instance_id=receipt_bound_guard_fields["receipt_bound_guard_turn_instance_id"], goal_ref=goal_ref) + _restore_replan_obligation_without_foreign_acks( + decision_status_payload, goal_id=goal_id, agent_id=agent_id, goal_ref=goal_ref, + ) fresh_operator_inbox_read = _fresh_operator_inbox_read_required( turn_start_hook_dispatch ) diff --git a/loopx/control_plane/runtime/run_compaction.py b/loopx/control_plane/runtime/run_compaction.py index 52df8b7ba1..6d6d2f8adc 100644 --- a/loopx/control_plane/runtime/run_compaction.py +++ b/loopx/control_plane/runtime/run_compaction.py @@ -85,6 +85,7 @@ "turn_instance_id", "replan_obligation_id", "goal_id", + "goal_ref", "parent_run_id", "spawned_by_goal_id", "agent_role", diff --git a/loopx/control_plane/work_items/autonomous_replan_ack.py b/loopx/control_plane/work_items/autonomous_replan_ack.py index 6ab384fb2b..3dd8e16a89 100644 --- a/loopx/control_plane/work_items/autonomous_replan_ack.py +++ b/loopx/control_plane/work_items/autonomous_replan_ack.py @@ -274,17 +274,28 @@ def _event_details(event: dict[str, Any]) -> dict[str, Any]: return details if isinstance(details, dict) else {} +def _parsed_exact_goal_ref(value: object) -> dict[str, str] | None: + """Parse one exact GoalRef, keeping the native string-only rule.""" + if not isinstance(value, Mapping): + return None + goal_id = value.get("goal_id") + instance_id = value.get("goal_instance_id") + if not isinstance(goal_id, str) or not isinstance(instance_id, str): + return None + try: + return exact_goal_ref(goal_id, instance_id) + except ValueError: + return None + + def _owner_goal_ref( goal_ref: Mapping[str, Any] | None, *, goal_id: str, ) -> dict[str, str] | None: """Normalize the quota owner. None is the alias owner.""" if goal_ref is None: return None - owner = exact_goal_ref( - str(goal_ref.get("goal_id") or ""), - str(goal_ref.get("goal_instance_id") or ""), - ) - if owner["goal_id"] != str(goal_id or "").strip(): + owner = _parsed_exact_goal_ref(goal_ref) + if owner is None or owner["goal_id"] != str(goal_id or "").strip(): raise ValueError("historical ACK GoalRef does not match goal_id") return owner @@ -295,23 +306,44 @@ def _record_owned_by_goal_ref( """Match quotaOwnerOwnsProjection for one rollout or run-index row. An alias owner keeps only rows with no goal_ref. An exact owner keeps - only rows whose goal_ref is that exact Goal instance. + only rows whose goal_ref is that exact Goal instance. Non-string ids are + not owned, matching the strict GoalRef parser. """ if owner is None: return "goal_ref" not in record - value = record.get("goal_ref") - if "goal_ref" not in record or not isinstance(value, Mapping): - return False - try: - parsed = exact_goal_ref( - str(value.get("goal_id") or ""), - str(value.get("goal_instance_id") or ""), - ) - except ValueError: + if "goal_ref" not in record: return False + parsed = _parsed_exact_goal_ref(record.get("goal_ref")) return parsed == owner +def drop_unowned_recorded_replan_acks( + runs: list[dict[str, Any]] | None, *, goal_id: str, + goal_ref: Mapping[str, Any] | None, +) -> tuple[list[dict[str, Any]], bool]: + """Drop recorded replan ACKs that belong to another Goal instance. + + Other run rows stay. Alias owners keep ACKs with no goal_ref; an exact + owner keeps only its own goal_ref. + """ + owner = _owner_goal_ref(goal_ref, goal_id=goal_id) + kept: list[dict[str, Any]] = [] + changed = False + for run in runs or []: + if not isinstance(run, dict): + continue + ack = run.get("autonomous_replan_ack") + if ( + isinstance(ack, dict) + and ack.get("recorded") is True + and not _record_owned_by_goal_ref(run, owner) + ): + changed = True + continue + kept.append(run) + return kept, changed + + def _owned_records( records: list[dict[str, Any]] | None, owner: dict[str, str] | None, ) -> list[dict[str, Any]]: diff --git a/loopx/control_plane/work_items/semantic_replan_writeback.py b/loopx/control_plane/work_items/semantic_replan_writeback.py index 0db409bf04..d8b677cea7 100644 --- a/loopx/control_plane/work_items/semantic_replan_writeback.py +++ b/loopx/control_plane/work_items/semantic_replan_writeback.py @@ -24,6 +24,7 @@ ) from ..todos.succession_warning import todo_succession_gap_items from .autonomous_replan_ack import ( + drop_unowned_recorded_replan_acks, historical_ack_candidates_with_writeback_order, latest_monitor_replan_frontier_identity, watch_lane_continuation_todo_ids, @@ -276,8 +277,11 @@ def qualify_replan_writeback( if isinstance(raw_agent_todos, dict) else [] ) + owned_history, _ = drop_unowned_recorded_replan_acks( + list(newest_first_runs or []), goal_id=goal_id, goal_ref=goal_ref, + ) run_obligation = autonomous_replan_obligation_from_runs( - newest_first_runs, + owned_history, agent_todos=agent_todos, agent_id=safe_agent_id, external_progress_review=external_progress_review, @@ -290,13 +294,13 @@ def qualify_replan_writeback( { **(registry_goal or {}), "id": goal_id, - "latest_runs": list(newest_first_runs or []), + "latest_runs": owned_history, } ] } } status_payload["receipt_bound_replan_ack_candidates"] = historical_ack_candidates_with_writeback_order( - newest_first_runs, goal_id=goal_id, agent_id=safe_agent_id, + owned_history, goal_id=goal_id, agent_id=safe_agent_id, obligation_id=guard_semantic_replan_obligation_id, guard_scoped=guard_scoped, source_route=replan_source_route, guard_recorded_at=receipt_bound_guard_recorded_at, diff --git a/tests/control_plane/test_quota_settlement_cli.py b/tests/control_plane/test_quota_settlement_cli.py index 14be911af0..e9fdf09294 100644 --- a/tests/control_plane/test_quota_settlement_cli.py +++ b/tests/control_plane/test_quota_settlement_cli.py @@ -9479,3 +9479,99 @@ def test_retired_instance_writeback_cannot_drop_same_second_historical_ack( decision = (recovery.get("replan_action_packet") or {}).get("decision") assert rc == 0, recovery assert decision == "historical_ack_recorded", recovery.get("replan_action_packet") + + +def _decision_summary(rc: int, payload: dict[str, Any]) -> dict[str, Any]: + packet = payload.get("replan_action_packet") + packet = packet if isinstance(packet, dict) else {} + return { + "rc": rc, + "decision": payload.get("decision"), + "error_code": payload.get("error_code"), + "packet_decision": packet.get("decision"), + "effective_action": payload.get("effective_action"), + "should_run": payload.get("should_run"), + } + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_only_retired_instance_ack_keeps_the_original_replan( + tmp_path: Path, provider: str, +) -> None: + """A newer ACK from a retired instance must not stop the current recovery. + + With no owned historical ACK, the admitted Turn keeps its replan. The + same decision remains when the shared run index contains only the retired + instance's later confirmation. + """ + project, runtime, registry = _write_fixture(tmp_path) + _configure_selected_todo_replan_fixture(project, registry, stride_strategy="off") + _initialize_git_checkout(project) + _initialize_fixture_authority(project, runtime, provider=provider) + original_turn = "turn-retired-ack-only" + admitted = _bind_selected_replan_guard(registry, runtime, project, original_turn) + obligation = admitted["replan_action_packet"]["obligation_id"] + recorded = datetime.fromisoformat( + admitted["heartbeat_receipt"]["recorded_at"].replace("Z", "+00:00")) + guard_args = ("quota", "should-run", "--codex-app", "--goal-id", GOAL_ID, + "--agent-id", AGENT_ID, "--turn-instance-id", original_turn, + "--todo-id", SELECTED_REPLAN_TODO_ID) + baseline_rc, baseline = _run_cli(registry, runtime, *guard_args) + runs_dir = runtime / "goals" / GOAL_ID / "runs" + runs_dir.mkdir(parents=True, exist_ok=True) + generated_at = (recorded + timedelta(seconds=5)).isoformat() + run = { + "goal_id": GOAL_ID, "agent_id": AGENT_ID, + "turn_instance_id": "retired-later-ack", "generated_at": generated_at, + "goal_ref": {"goal_id": GOAL_ID, "goal_instance_id": INSTANCE_A}, + "classification": "bounded_replan_progress", + "autonomous_replan_ack": { + "schema_version": "autonomous_replan_ack_v0", "recorded": True, + "source": "refresh_state", + "semantic_delta": { + "schema_version": "replan_semantic_delta_v0", "accepted": True, + "obligation_id": obligation, + "outcomes": ["fresh_vision_path_outcome"], + "satisfying_outcomes": ["fresh_vision_path_outcome"], + }, + }, + } + source = runs_dir / "retired-later-ack.json" + source.write_text(json.dumps(run), encoding="utf-8") + markdown = source.with_suffix(".md") + markdown.write_text("# Retired instance confirmation\n", encoding="utf-8") + with (runs_dir / "index.jsonl").open("a", encoding="utf-8") as stream: + stream.write(json.dumps({ + **run, "json_path": str(source), "markdown_path": str(markdown), + }) + "\n") + retired_rc, retired = _run_cli(registry, runtime, *guard_args) + assert _decision_summary(retired_rc, retired) == _decision_summary(baseline_rc, baseline) + assert retired_rc == 0 + assert retired.get("error_code") != "heartbeat_receipt_identity_conflict" + assert (retired.get("replan_action_packet") or {}).get("decision") != "historical_ack_recorded" + vision = project / "vision.json" + vision.write_text(json.dumps({ + "schema_version": "goal_vision_replan_contract_v0", + "state": "vision_patch_proposed", + "vision_patch": {"acceptance_summary": "Verified fixture artifact; original work remains open."}, + "todo_delta": [f"retain:{SELECTED_REPLAN_TODO_ID}"], + "path_delta": { + "outcome": "replan", + "prior_assumption": "Fixture work needs a verified next route.", + "observed_reality": "A fixture check supports continuing the existing work.", + "retained": ["Original requirements and the existing Todo."], + "evidence_refs": ["evidence:fixture-check"], + }, + }), encoding="utf-8") + refresh_rc, refreshed = _run_cli( + registry, runtime, "refresh-state", "--goal-id", GOAL_ID, "--agent-id", AGENT_ID, + "--todo-id", SELECTED_REPLAN_TODO_ID, "--turn-instance-id", original_turn, + "--classification", "bounded_replan_progress", "--delivery-batch-scale", "single_surface", + "--delivery-outcome", "outcome_progress", "--delivery-workspace-path", str(project), + "--delivery-boundary", "semantic_closeout", "--agent-vision-json", str(vision), + "--autonomous-replan-recorded", "--repair-delta-kind", "goal_vision_patch", + "--no-global-sync", "--suppress-external-sinks", + ) + assert refresh_rc == 0, refreshed + assert refreshed.get("appended") is True + assert refreshed.get("error_code") != "heartbeat_receipt_identity_conflict" From 427950a43a9eeb3067bc66061d6a287f6beee202 Mon Sep 17 00:00:00 2001 From: Duang777 Date: Wed, 18 Nov 2026 05:02:04 +0000 Subject: [PATCH 3/4] fix(quota): move receipt ACK selection out of the frontier builder The receipt-owned ACK choice and empty-list fallback stay on the semantic history reader. The frontier builder keeps its existing decision-point budget. Signed-off-by: Duang777 --- .../goals/goal_frontier/__init__.py | 24 +++----------- .../goals/goal_frontier/semantic_history.py | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/loopx/control_plane/goals/goal_frontier/__init__.py b/loopx/control_plane/goals/goal_frontier/__init__.py index 9eb33ddda9..a399b8d130 100644 --- a/loopx/control_plane/goals/goal_frontier/__init__.py +++ b/loopx/control_plane/goals/goal_frontier/__init__.py @@ -90,7 +90,7 @@ _latest_runs_for_goal, latest_agent_vision_from_runs as latest_agent_vision_from_runs, latest_agent_vision_from_status_payload, - latest_autonomous_replan_ack_from_status_payload, + receipt_bound_replan_ack_history, latest_missing_vision_checkpoint_from_status_payload, latest_replan_ack_feedback_from_status_payload, ) @@ -1451,25 +1451,13 @@ def build_goal_frontier_projection_context_from_status( if replan_scope.get("required") and not replan_scope.get("applies"): replan_obligation = None - latest_agent_replan_ack = latest_autonomous_replan_ack_from_status_payload( + latest_agent_replan_ack, scoped_ack_history = receipt_bound_replan_ack_history( status_payload, goal_id=goal_id, agent_id=agent_id, neutral_classifications=neutral_replan_ack_classifications, + receipt_bound_replan_obligation_id=receipt_bound_replan_obligation_id, ) - # Receipt recovery supplies the owned ACK history, including an empty list. - # The status snapshot's newest ACK can belong to another Goal instance, so - # it must not satisfy or replace this Turn's review. - scoped_ack_history = ( - status_payload.get("receipt_bound_replan_ack_candidates") - if receipt_bound_replan_obligation_id - else None - ) - if isinstance(scoped_ack_history, list): - latest_agent_replan_ack = next( - (candidate for candidate in scoped_ack_history if isinstance(candidate, dict)), - None, - ) latest_replan_ack_feedback = latest_replan_ack_feedback_from_status_payload( status_payload, goal_id=goal_id, @@ -1690,11 +1678,7 @@ def build_goal_frontier_projection_context_from_status( )[:8] if isinstance(candidate, dict) ] - if ( - latest_agent_replan_ack is not None - and receipt_bound_replan_obligation_id - and not isinstance(raw_historical_ack_candidates, list) - ): + if latest_agent_replan_ack is not None and receipt_bound_replan_obligation_id: historical_ack_candidates.append( {"historical_ack": latest_agent_replan_ack} ) diff --git a/loopx/control_plane/goals/goal_frontier/semantic_history.py b/loopx/control_plane/goals/goal_frontier/semantic_history.py index d169b4a789..ae6aff3e78 100644 --- a/loopx/control_plane/goals/goal_frontier/semantic_history.py +++ b/loopx/control_plane/goals/goal_frontier/semantic_history.py @@ -232,6 +232,38 @@ def latest_autonomous_replan_ack_from_status_payload( ) +def receipt_bound_replan_ack_history( + status_payload: dict[str, Any], + *, + goal_id: str, + agent_id: str | None, + neutral_classifications: set[str], + receipt_bound_replan_obligation_id: str | None, +) -> tuple[dict[str, Any] | None, list[dict[str, Any]] | None]: + """Choose the ACK history a receipt-bound review may use. + + A supplied candidate list is the whole owned history, including when it + is empty. Without that list, the status snapshot's newest ACK remains + the existing fallback. + """ + + latest = latest_autonomous_replan_ack_from_status_payload( + status_payload, + goal_id=goal_id, + agent_id=agent_id, + neutral_classifications=neutral_classifications, + ) + supplied = ( + status_payload.get("receipt_bound_replan_ack_candidates") + if receipt_bound_replan_obligation_id + else None + ) + if not isinstance(supplied, list): + return latest, None + owned = [candidate for candidate in supplied if isinstance(candidate, dict)] + return (owned[0] if owned else None), owned + + def latest_replan_ack_feedback_from_status_payload( status_payload: dict[str, Any], *, From ac51bf333044f8232b13f355df760a0b9ac1064b Mon Sep 17 00:00:00 2001 From: Duang777 Date: Wed, 18 Nov 2026 05:24:26 +0000 Subject: [PATCH 4/4] fix(quota): keep progress-review context when restoring a replan Rebuilding the open review after a retired ACK must use the same assist policy and effective-turn cadence as the original status projection. Signed-off-by: Duang777 --- loopx/control_plane/quota/live_decision.py | 21 +++- .../test_quota_settlement_cli.py | 116 ++++++++++++++++++ 2 files changed, 136 insertions(+), 1 deletion(-) diff --git a/loopx/control_plane/quota/live_decision.py b/loopx/control_plane/quota/live_decision.py index 1f5ccc8c1c..1c56b37593 100644 --- a/loopx/control_plane/quota/live_decision.py +++ b/loopx/control_plane/quota/live_decision.py @@ -669,11 +669,18 @@ def _project_unbound_work_fields(payload: dict[str, Any]) -> None: def _restore_replan_obligation_without_foreign_acks( status_payload: dict[str, Any], *, goal_id: str, agent_id: str, goal_ref: Mapping[str, Any] | None, + registry_goal: Mapping[str, Any] | None, + registry_path: Path, + runtime_root: Path, ) -> None: """Recompute the open review after removing another instance's ACK rows.""" - from ...status import autonomous_replan_obligation_from_runs + from ...status import ( + autonomous_replan_obligation_from_runs, + external_progress_review_context, + ) from ..work_items.autonomous_replan_ack import drop_unowned_recorded_replan_acks from ..work_items.project_asset import attach_active_state_project_asset_fields + from ..work_items.replan_history_codec import effective_turn_cadence_context run_history = status_payload.get("run_history") goals = run_history.get("goals") if isinstance(run_history, dict) else None @@ -726,9 +733,20 @@ def _restore_replan_obligation_without_foreign_acks( target.pop("autonomous_replan_obligation", None) target.pop("autonomous_replan_obligations_by_agent", None) target.pop("autonomous_replan_ack", None) + source_goal = dict(registry_goal) if isinstance(registry_goal, Mapping) else {"id": goal_id} + source_goal.setdefault("id", goal_id) + review = external_progress_review_context(source_goal, runtime_root) + cadence_goal_ref = dict(goal_ref) if isinstance(goal_ref, Mapping) else None attach_active_state_project_asset_fields( item, latest_runs=filtered, autonomous_replan_obligation_from_runs=autonomous_replan_obligation_from_runs, + external_progress_review=review, + effective_turn_cadence=effective_turn_cadence_context( + source_goal, runtime_root, registry_path=registry_path, + goal_ref=cadence_goal_ref if all( + isinstance(value, str) for value in (cadence_goal_ref or {}).values() + ) else None, + ), agent_lane_id=agent_id, ) @@ -868,6 +886,7 @@ def build_live_quota_should_run_decision( goal_ref=goal_ref) _restore_replan_obligation_without_foreign_acks( decision_status_payload, goal_id=goal_id, agent_id=agent_id, goal_ref=goal_ref, + registry_goal=registered_goal, registry_path=registry_path, runtime_root=runtime_root, ) fresh_operator_inbox_read = _fresh_operator_inbox_read_required( turn_start_hook_dispatch diff --git a/tests/control_plane/test_quota_settlement_cli.py b/tests/control_plane/test_quota_settlement_cli.py index e9fdf09294..454f689733 100644 --- a/tests/control_plane/test_quota_settlement_cli.py +++ b/tests/control_plane/test_quota_settlement_cli.py @@ -9575,3 +9575,119 @@ def test_only_retired_instance_ack_keeps_the_original_replan( assert refresh_rc == 0, refreshed assert refreshed.get("appended") is True assert refreshed.get("error_code") != "heartbeat_receipt_identity_conflict" + + +def test_progress_review_reentry_keeps_the_admitted_obligation_beside_a_retired_ack( + tmp_path: Path, +) -> None: + """Recomputing the review must keep the assist context status already used. + + A retired instance ACK forces that recompute. The admitted progress-review + obligation stays the same on the next should-run for this Turn. + """ + from loopx.capabilities.progress_review.receipt import write_progress_review_receipt + + project, runtime, registry_path = _write_fixture(tmp_path) + pin = hashlib.sha256(b"contract-1").hexdigest() + registry = json.loads(registry_path.read_text(encoding="utf-8")) + goal = registry["goals"][0] + goal["status"] = "active" + goal["control_plane"] = { + "progress_review": { + "mode": "assist", + "signal": "noul", + "drift_threshold": 2, + "contract_revision": pin, + }, + } + registry_path.write_text(json.dumps(registry, indent=2, sort_keys=True) + "\n", encoding="utf-8") + _initialize_fixture_authority(project, runtime, provider="file") + runs_dir = runtime / "goals" / GOAL_ID / "runs" + runs_dir.mkdir(parents=True, exist_ok=True) + for sequence, turn in ((2, "review-turn-2"), (1, "review-turn-1")): + row = { + "goal_id": GOAL_ID, + "agent_id": AGENT_ID, + "turn_instance_id": turn, + "generated_at": f"2026-09-21T00:00:{sequence:02d}Z", + "classification": "bounded_delivery", + "progress_observation": { + "schema_version": "typed_progress_observation_v0", + "result_class": "advanced", + "hypothesis_id": f"hypothesis-{sequence}", + }, + } + source = runs_dir / f"{turn}.json" + source.write_text(json.dumps(row), encoding="utf-8") + with (runs_dir / "index.jsonl").open("a", encoding="utf-8") as stream: + stream.write(json.dumps({**row, "json_path": str(source), "markdown_path": str(source.with_suffix(".md"))}) + "\n") + source.with_suffix(".md").write_text("# Reviewed delivery\n", encoding="utf-8") + write_progress_review_receipt(runtime, GOAL_ID, { + "receipt_id": hashlib.sha256(f"event-{sequence}".encode()).hexdigest(), + "event_id": hashlib.sha256(f"event-{sequence}".encode()).hexdigest(), + "evidence_id": hashlib.sha256(f"evidence-{sequence}".encode()).hexdigest(), + "contract_revision": pin, + "sequence": sequence, + "status": "completed", + "goal_id": GOAL_ID, + "schema_version": "progress_review_receipt_v0", + "signal_rule_version": "progress_review_signal_rule_v1", + "question_version": "scoped-progress-sentinel-v2", + "model": "fixture-v1", + "run": { + "turn_instance_id": turn, + "generated_at": f"2026-09-21T00:00:{sequence:02d}Z", + "agent_id": AGENT_ID, + }, + "judgments": { + "choice": {"relation": "off_goal", "increment": "no_new_evidence"}, + "noul": {"behavior_change": 0.05, "serves_acceptance": 0.04, "evidence_increment": 0.1}, + }, + "drift_signal": {"noul": True, "choice": True}, + "label_probability_threshold": 0.6, + "recorded_at": float(sequence), + }) + turn = "turn-progress-review-reentry" + guard = ( + "quota", "should-run", "--codex-app", "--goal-id", GOAL_ID, + "--agent-id", AGENT_ID, "--turn-instance-id", turn, + "--todo-id", TODO_ID, + ) + first_rc, first = _run_cli(registry_path, runtime, *guard) + assert first_rc == 0, first + assert first["decision"] == "autonomous_replan_required", first.get("decision") + kinds = [ + trigger.get("kind") + for trigger in (first.get("autonomous_replan_obligation") or {}).get("triggers") or [] + if isinstance(trigger, dict) + ] + assert kinds == ["external_progress_review_drift"], kinds + obligation = first["replan_action_packet"]["obligation_id"] + retired = { + "goal_id": GOAL_ID, "agent_id": AGENT_ID, + "turn_instance_id": "retired-review-ack", + "generated_at": "2026-09-21T00:00:05Z", + "classification": "bounded_replan_progress", + "goal_ref": {"goal_id": GOAL_ID, "goal_instance_id": INSTANCE_A}, + "autonomous_replan_ack": { + "schema_version": "autonomous_replan_ack_v0", "recorded": True, + "semantic_delta": { + "schema_version": "replan_semantic_delta_v0", "accepted": True, + "obligation_id": obligation, + "outcomes": ["fresh_vision_path_outcome"], + "satisfying_outcomes": ["fresh_vision_path_outcome"], + }, + }, + } + source = runs_dir / "retired-review-ack.json" + source.write_text(json.dumps(retired), encoding="utf-8") + source.with_suffix(".md").write_text("# Retired review\n", encoding="utf-8") + with (runs_dir / "index.jsonl").open("a", encoding="utf-8") as stream: + stream.write(json.dumps({ + **retired, "json_path": str(source), "markdown_path": str(source.with_suffix(".md")), + }) + "\n") + second_rc, second = _run_cli(registry_path, runtime, *guard) + assert second_rc == 0, second + assert second["decision"] == "autonomous_replan_required" + assert second["replan_action_packet"]["obligation_id"] == obligation + assert second.get("error_code") != "heartbeat_receipt_identity_conflict"