From 2ed89fdc029644be35f254982f689ed0d78de22b Mon Sep 17 00:00:00 2001 From: "lusendong.6789" Date: Wed, 11 Nov 2026 12:09:20 +0800 Subject: [PATCH 001/267] feat(reward-memory): skip duplicate turn reflections Signed-off-by: lusendong.6789 Co-authored-by: TRAE CLI --- loopx/capabilities/reward_memory/README.md | 13 +++++++++++-- loopx/capabilities/reward_memory/README.zh-CN.md | 8 +++++++- loopx/capabilities/reward_memory/turn_hooks.py | 4 ++++ tests/control_plane/test_quota_memory_context.py | 8 ++++++++ .../test_reward_memory_host_isolation.py | 10 ++++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/loopx/capabilities/reward_memory/README.md b/loopx/capabilities/reward_memory/README.md index 6ca4517fef..9ad3265354 100644 --- a/loopx/capabilities/reward_memory/README.md +++ b/loopx/capabilities/reward_memory/README.md @@ -287,8 +287,17 @@ engineering source kind, and opaque evidence refs; an ordinary Turn summary is not evidence. It also carries `procedural_experience_contract_v0` with applicability, observed outcome, attribution, complete future behavior, limitations, and the same evidence refs. Legacy v0 reflections remain -audit-only and cannot become durable memory. Ambiguous provider commits and -unverified readbacks remain in a +audit-only and cannot become durable memory. When the same Turn delivers recall +guidance and enables ingest, reflection compares new independent evidence with +comparable procedural experience guidance. Without a comparable experience, the +ordinary eligibility rule remains in force. Mere confirmation leaves the +reflection empty; a candidate is +produced only for distinct reusable applicability or a material change to its +trigger, action, validation, stop condition, or limitations. Distinct +applicability remains separate rather than being blended. Reflection never +merges, updates, retires, or supersedes a provider record; configured lifecycle +owners retain those decisions. Ambiguous +provider commits and unverified readbacks remain in a mode-0600 Goal+Agent+event sidecar. The next executing Turn retries the same deterministic event before recall, so the provider can deduplicate it and LoopX can require exact readback. Explicit disable suppresses reconciliation and all diff --git a/loopx/capabilities/reward_memory/README.zh-CN.md b/loopx/capabilities/reward_memory/README.zh-CN.md index 74d04970c1..b9bb501b59 100644 --- a/loopx/capabilities/reward_memory/README.zh-CN.md +++ b/loopx/capabilities/reward_memory/README.zh-CN.md @@ -187,7 +187,13 @@ quota settlement 均完成后,才接受 outcome ingest。Reflection 必须使 research/simulation/real/engineering 来源类型和 opaque evidence refs;还必须携带 `procedural_experience_contract_v0`,分别写出适用情境、已观察结果、归因、未来行为(触发、动作、 验证、停止条件)、失效边界,并绑定相同 evidence refs。只有事实复述、没有未来行为改变的普通 -Turn summary 不算经验。旧的 v0 reflection 只保留审计兼容,不会写成可持久召回的记忆。 +Turn summary 不算经验。旧的 v0 reflection 只保留审计兼容,不会写成可持久召回的记忆。同一 +Turn 同时交付 recall guidance 并启用 ingest 时,reflection 会把新的独立证据与其中可比较的 +procedural experience 对照;没有可比较经验时,仍使用普通资格规则。若只是确认相同的未来行为 +与失效边界,就保持为空。只有触发、动作、验证、停止条件或 +失效边界发生实质变化,或者形成可复用的独立适用情境时才生成候选;适用情境不同的经验保持 +分开,不混成更宽泛的规则。 +Reflection 不合并、更新、退役或取代 Provider 记录,这些决定仍属于已配置的生命周期 owner。 Provider commit 含糊或精确读回失败时,会保留一份权限为 0600、按 Goal+Agent+事件隔离的 sidecar。下一个执行 Turn 会在 recall 前以同一 deterministic event 重试,使 Provider 可以去重, LoopX 再要求精确读回。显式关闭会停止 reconciliation,并保持零 Provider 调用。 diff --git a/loopx/capabilities/reward_memory/turn_hooks.py b/loopx/capabilities/reward_memory/turn_hooks.py index 836e3ae870..57cd43394d 100644 --- a/loopx/capabilities/reward_memory/turn_hooks.py +++ b/loopx/capabilities/reward_memory/turn_hooks.py @@ -30,6 +30,10 @@ def _reward_memory_prompt_instructions(*, has_guidance: bool, ingest_enabled: bo instructions.append( "Set reward_memory_reflection_json to an empty string unless independent task evidence established a reusable experience. For eligible evidence, return one compact JSON object using schema_version=turn_reward_memory_reflection_v1, status=eligible, a configured surface_id, outcome_kind in research|simulation|real|engineering, content_summary, reasoning_summary, confidence in low|medium|high, and 1-5 opaque evidence_refs. Also include experience using schema_version=procedural_experience_contract_v0 with non-empty applicability and limitations lists, observed_outcome, attribution, the same evidence_refs, and future_behavior containing trigger, action, validation, and stop_condition. A fact recap without a future behavior change and non-generalization boundary is not eligible memory. Legacy v0 reflections are audit-only and cannot become durable memory. Never use your own summary as evidence. Settlement may ingest it only when the caller-declared Todo validator attests the exact reflection digest and evidence; ordinary validator success remains awaiting and makes no provider write." ) + if has_guidance and ingest_enabled: + instructions.append( + "Before proposing a reflection, compare current independent task evidence with any comparable procedural_experience in the delivered reward_memory_recall guidance. If no comparable procedural experience was delivered, apply the ordinary eligibility rule above. Leave reward_memory_reflection_json empty when the evidence only confirms the same future behavior and limitations; emit a candidate only when it establishes distinct reusable applicability or materially changes the trigger, action, validation, stop condition, or limitations. Keep distinct applicability conditions separate rather than blending them. Do not claim to merge, update, retire, or supersede provider records; those lifecycle decisions remain with their configured owners." + ) return instructions diff --git a/tests/control_plane/test_quota_memory_context.py b/tests/control_plane/test_quota_memory_context.py index f821104087..1946d39e60 100644 --- a/tests/control_plane/test_quota_memory_context.py +++ b/tests/control_plane/test_quota_memory_context.py @@ -86,6 +86,10 @@ def test_real_app_guard_delivers_scoped_memory_once( assert project_turn_context(first)["reward_memory_recall"]["context"]["guidance"] == guidance assert contribution["result_fields"]["reward_memory_reflection_json"]["maxLength"] == 2400 assert "private, non-authoritative" in contribution["instructions"][0] + assert any( + "compare current independent task evidence" in item + for item in contribution["instructions"] + ) repeated = run_cli(args) assert repeated["ok"] and memory.calls == 1 assert not repeated.get("reward_memory_recall", {}).get("external_writes_performed") @@ -206,6 +210,10 @@ def test_quota_recall_size_limit_preserves_independently_enabled_ingest(size: in delivered = result["context_fields"]["reward_memory_recall"] assert (delivered["context"] is not None) is (size == 40_000) assert result["result_fields"]["reward_memory_reflection_json"]["maxLength"] == 2400 + assert any( + "compare current independent task evidence" in item + for item in result["instructions"] + ) is (size == 40_000) assert len(json.dumps(result, separators=(",", ":")).encode()) <= 65536 diff --git a/tests/control_plane/test_reward_memory_host_isolation.py b/tests/control_plane/test_reward_memory_host_isolation.py index 7d8142a36c..e4c7c0c4d6 100644 --- a/tests/control_plane/test_reward_memory_host_isolation.py +++ b/tests/control_plane/test_reward_memory_host_isolation.py @@ -88,6 +88,16 @@ def test_recall_and_reflection_are_independently_opted_in(recall: bool, ingest: prompt = _prompt(request) assert ("When reward_memory_recall contains guidance" in prompt) is recall assert ("Set reward_memory_reflection_json" in prompt) is ingest + assert ("compare current independent task evidence" in prompt) is (recall and ingest) + for consolidation_rule in ( + "any comparable procedural_experience", + "If no comparable procedural experience was delivered", + "only confirms the same future behavior and limitations", + "establishes distinct reusable applicability or materially changes", + "Keep distinct applicability conditions separate", + "Do not claim to merge, update, retire, or supersede provider records", + ): + assert (consolidation_rule in prompt) is (recall and ingest) assert ("reward_memory_reflection_json" in codex_cli_result_schema(request)["properties"]) is ingest assert bool(request["reward_memory_recall"].get("context")) is recall if ingest: From 3a9cff187518f011dff6ee25bc9038697753e2b5 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:06:13 +0800 Subject: [PATCH 002/267] fix(benchmark): preserve scoring when terminal worker capture fails Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- benchmark/edgebench/README.md | 8 ++++ benchmark/edgebench/checkpoint_supervisor.py | 4 ++ benchmark/edgebench/run.py | 2 + benchmark/edgebench/terminal_capture.py | 32 ++++++++++++++++ benchmark/tests/test_terminal_preservation.py | 37 +++++++++++++++++++ 5 files changed, 83 insertions(+) diff --git a/benchmark/edgebench/README.md b/benchmark/edgebench/README.md index cf5997a900..b70588d630 100644 --- a/benchmark/edgebench/README.md +++ b/benchmark/edgebench/README.md @@ -896,6 +896,14 @@ requires a running container; latest files remain in the stopped source and, on successful export, the terminal point. Native graded history/results remain independent and are not discarded with that optional archive. +If the joint export failed, a confirmed stopped worker with a completed grading +drain gets one independent native scorer capture before the original scorer +exits. The receipt links this separate checkpoint, including the released results, +incumbent and consumed rounds. It carries no worker clock or resume qualification; +a receiver still needs to prove the original workspace, session and remaining +allowance together. An unknown grading outcome or unconfirmed stop does not +trigger this capture. + If the optional preservation receipt cannot be written, a redacted warning reports that gap; observer shutdown, one capture attempt and verified source stopping still proceed. The known solver exit and native grading remain authoritative. diff --git a/benchmark/edgebench/checkpoint_supervisor.py b/benchmark/edgebench/checkpoint_supervisor.py index 3bbe88134e..74e19e72d0 100644 --- a/benchmark/edgebench/checkpoint_supervisor.py +++ b/benchmark/edgebench/checkpoint_supervisor.py @@ -94,6 +94,10 @@ def finish_solver(self, execution): else: self.preservation.finish(execution) + def finish_grading(self, status): + if self.preservation is not None: + self.preservation.finish_grading(status) + def close(self): self.stop.set() if self.thread is not None: diff --git a/benchmark/edgebench/run.py b/benchmark/edgebench/run.py index 52c5d17667..c320c1dba5 100644 --- a/benchmark/edgebench/run.py +++ b/benchmark/edgebench/run.py @@ -613,6 +613,8 @@ def main(argv=None): finally: if feedback is not None: feedback.close() + if checkpoint is not None: + checkpoint.finish_grading(getattr(result, "grading_status", None)) # Native cancellation and swallowed Docker failures return runtime=0. # Observe the signal independently; never infer cancellation from output prose. execution = backend.execution_receipt or {} diff --git a/benchmark/edgebench/terminal_capture.py b/benchmark/edgebench/terminal_capture.py index 320775df5b..1e63903f1b 100644 --- a/benchmark/edgebench/terminal_capture.py +++ b/benchmark/edgebench/terminal_capture.py @@ -168,6 +168,38 @@ def stop_source(self, container): finally: self._persist_receipt() + def finish_grading(self, grading_status): + """Seal the drained native registration if the joint export failed.""" + receipt = self.receipt + if (grading_status != "complete" or receipt is None + or not receipt["capture_attempted"] or receipt["capture_succeeded"] + or receipt.get("source_stopped") is not True + or "native_scoring_checkpoint" in receipt): + return + + # This point is captured after grading, with no worker phase clock. + # It is deliberately not the earlier joint attempt or a resume grant. + capture_id = uuid.uuid4().hex + retained = {"capture_id": capture_id, "capture_succeeded": False} + receipt["native_scoring_checkpoint"] = retained + self._persist_receipt() + try: + from .checkpoint_controller import capture + + retained["capture"] = capture( + self.supervisor.controller_file, + token=self.supervisor.token, + capture_id=capture_id, + destination=self.receipt_path.parent / f"terminal-native-{capture_id}", + ) + retained["capture_succeeded"] = True + except Exception as error: + # A failed or uncertain export remains evidence, never a retry + # under a different capture identity or a changed solver outcome. + retained["error_kind"] = type(error).__name__ + finally: + self._persist_receipt() + def _stop_retained_worker(container, receipt): if container.id != receipt["container_id"]: diff --git a/benchmark/tests/test_terminal_preservation.py b/benchmark/tests/test_terminal_preservation.py index 3558631d1e..e00f6e9378 100644 --- a/benchmark/tests/test_terminal_preservation.py +++ b/benchmark/tests/test_terminal_preservation.py @@ -78,6 +78,27 @@ def test_success_retains_normal_cleanup_unless_writer_barrier_is_uncertain(tmp_p assert "capture" not in calls +def test_native_fallback_failure_keeps_original_result_and_does_not_retry(tmp_path, monkeypatch): + preservation, calls = fixture_preservation(tmp_path, monkeypatch, "capture") + preservation.finish({"exit_code": 2, "execution_started": True}) + + def failed_export(*args, **kwargs): + calls.append("native") + raise OSError("private scorer transport details") + + monkeypatch.setattr("benchmark.edgebench.checkpoint_controller.capture", failed_export) + preservation.finish_grading("unknown") + assert "native_scoring_checkpoint" not in preservation.receipt + preservation.finish_grading("complete") + preservation.finish_grading("complete") + receipt = json.loads((tmp_path / "terminal-preservation.json").read_text()) + assert calls == ["close", "capture", "native"] + assert receipt["error_kind"] == "TimeoutError" and receipt["source_stopped"] is True + assert not receipt["capture_succeeded"] and not receipt["restore_qualified"] + assert receipt["native_scoring_checkpoint"]["error_kind"] == "OSError" + assert "private scorer" not in json.dumps(receipt) + + def test_component_declaration_captures_full_workspace_and_no_auth(): original = {"components": [{"id": "submitted", "source_path": "/task/solution.py"}, {"id": "notes", "source_path": "/private-notes"}]} @@ -314,6 +335,8 @@ def registered(token): assert result.solver_execution["status"] == "nonzero_exit" assert result.best_score == 7 and result.agent_submissions == 1 assert drained == ["complete"] and result.archive == b"" + supervisor.finish_grading(result.grading_status) + supervisor.finish_grading(result.grading_status) # A second notification cannot reseal. assert json.loads((trial / "run_history.json").read_text())["best_score"] == 7 container.reload() assert not container.attrs["State"]["Running"] and container.attrs["State"]["Pid"] == 0 @@ -327,6 +350,20 @@ def registered(token): assert not receipt["restore_qualified"] assert receipt["capture_succeeded"] is not capture_failure assert len(grades) == 1 # Saving the tail did not create an evaluation opportunity. + if capture_failure: + native_capture = receipt["native_scoring_checkpoint"] + assert native_capture["capture_succeeded"] + assert native_capture["capture_id"] != receipt["capture_id"] + point, = tmp_path.glob("terminal-native-*") + manifest = json.loads((point / "components/native_judge/manifest.json").read_text()) + assert [entry["score"] for entry in manifest["history"]] == [7] + assert manifest["opportunity"]["next_agent"] == 2 + assert 0 < manifest["opportunity"]["remaining_cooldown_seconds"] <= 3600 + assert (point / "components/native_judge/incumbent.tar.gz").read_bytes() == b"7" + assert not (point / "components/host_phase_deadline").exists() + assert not native_capture["capture"]["research_point_qualified"] + else: + assert "native_scoring_checkpoint" not in receipt if not capture_failure: point = supervisor.destination / receipt["capture_id"] manifest = json.loads((point / "checkpoint.json").read_text()) From c5071f40bbcb213df9a42e14c47b3a4539dee65a Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:17:33 +0800 Subject: [PATCH 003/267] fix(benchmark): preserve drained scoring after native runner errors Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- benchmark/edgebench/README.md | 2 ++ benchmark/edgebench/checkpoint_supervisor.py | 14 ++++++++++++++ benchmark/edgebench/managed_run.py | 4 ++-- benchmark/edgebench/run.py | 2 ++ benchmark/tests/test_terminal_preservation.py | 7 ++++++- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/benchmark/edgebench/README.md b/benchmark/edgebench/README.md index b70588d630..6345da39d6 100644 --- a/benchmark/edgebench/README.md +++ b/benchmark/edgebench/README.md @@ -903,6 +903,8 @@ incumbent and consumed rounds. It carries no worker clock or resume qualificatio a receiver still needs to prove the original workspace, session and remaining allowance together. An unknown grading outcome or unconfirmed stop does not trigger this capture. +If the native runner raises after writing its drain receipt, the runner +checks the matching run history before attempting the same capture. If the optional preservation receipt cannot be written, a redacted warning reports that gap; observer shutdown, one capture attempt and verified source stopping diff --git a/benchmark/edgebench/checkpoint_supervisor.py b/benchmark/edgebench/checkpoint_supervisor.py index 74e19e72d0..1a3393132f 100644 --- a/benchmark/edgebench/checkpoint_supervisor.py +++ b/benchmark/edgebench/checkpoint_supervisor.py @@ -98,6 +98,20 @@ def finish_grading(self, status): if self.preservation is not None: self.preservation.finish_grading(status) + def finish_grading_from_receipt(self, trial, *, run_id, task_id): + """Recover the drain observation when the native runner raises.""" + if self.preservation is None or self.preservation.receipt is None: + return + from .managed_run import native_grading_drained + + try: + drained = native_grading_drained( + trial, history_file="run_history.json", run_id=run_id, task_id=task_id) + except (OSError, ValueError): + return # Bad optional evidence must not mask the original exception. + if drained: + self.finish_grading("complete") + def close(self): self.stop.set() if self.thread is not None: diff --git a/benchmark/edgebench/managed_run.py b/benchmark/edgebench/managed_run.py index dcee10cfbb..17ee0cce47 100644 --- a/benchmark/edgebench/managed_run.py +++ b/benchmark/edgebench/managed_run.py @@ -200,7 +200,7 @@ def _terminal_history_entry(entry, *, task_id: str) -> bool: ) -def _grading_drained(trial: Path, *, history_file: str, run_id: str, task_id: str) -> bool: +def native_grading_drained(trial: Path, *, history_file: str, run_id: str, task_id: str) -> bool: try: grading, _ = _read_record(trial / "grading_status.json") except FileNotFoundError: @@ -272,7 +272,7 @@ def publish_managed_terminal(trial: Path, *, execute: bool = False) -> bool: if not solver_matches or not _solver_ended(execution, admitted_at=admitted.timestamp(), observed_at=datetime.now(UTC).timestamp()): raise ValueError("Retained solver termination evidence differs") - if not _grading_drained(trial, history_file=history_file, run_id=run_id, task_id=task_id): + if not native_grading_drained(trial, history_file=history_file, run_id=run_id, task_id=task_id): return False continuity = build_benchmark_runtime_continuity( launch_runtime_digest=retained.get("launch_runtime_sha256"), diff --git a/benchmark/edgebench/run.py b/benchmark/edgebench/run.py index c320c1dba5..a7ca89ce95 100644 --- a/benchmark/edgebench/run.py +++ b/benchmark/edgebench/run.py @@ -609,6 +609,8 @@ def main(argv=None): if provider_failure := getattr(backend, "provider_failure_receipt", None): receipt["provider_failure"] = provider_failure receipt_path.write_text(json.dumps(receipt, indent=2)) + if checkpoint is not None: + checkpoint.finish_grading_from_receipt(trial, run_id=args.run_id, task_id=task.task_id) raise finally: if feedback is not None: diff --git a/benchmark/tests/test_terminal_preservation.py b/benchmark/tests/test_terminal_preservation.py index e00f6e9378..4b2a99830c 100644 --- a/benchmark/tests/test_terminal_preservation.py +++ b/benchmark/tests/test_terminal_preservation.py @@ -335,7 +335,12 @@ def registered(token): assert result.solver_execution["status"] == "nonzero_exit" assert result.best_score == 7 and result.agent_submissions == 1 assert drained == ["complete"] and result.archive == b"" - supervisor.finish_grading(result.grading_status) + if capture_failure: + supervisor.finish_grading_from_receipt(trial, run_id="other-run", task_id="fixture") + assert "native_scoring_checkpoint" not in supervisor.preservation.receipt + supervisor.finish_grading_from_receipt(trial, run_id="source", task_id="fixture") + else: + supervisor.finish_grading(result.grading_status) supervisor.finish_grading(result.grading_status) # A second notification cannot reseal. assert json.loads((trial / "run_history.json").read_text())["best_score"] == 7 container.reload() From 667d6da51bfb984efb3c8c0612d5cde896e82c5e Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Wed, 11 Nov 2026 20:01:37 +0800 Subject: [PATCH 004/267] Load bound steward history images on demand Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- apps/presentation/dashboard/src/data/chat.ts | 21 +++- .../personal-workspace/channel-timeline.tsx | 2 +- .../personal-workspace/personal-workspace.css | 2 +- .../app-conversation-and-async-inbox-v0.md | 7 ++ ...p-conversation-and-async-inbox-v0.zh-CN.md | 5 + loopx/chat/journal.py | 22 ++++ loopx/chat/result_files_api.py | 105 +++++++++++++++++- loopx/chat_server.py | 4 +- loopx/chat_store.py | 37 +++--- tests/test_manager_context_roundtrip.py | 61 +++++++++- 10 files changed, 230 insertions(+), 36 deletions(-) create mode 100644 loopx/chat/journal.py diff --git a/apps/presentation/dashboard/src/data/chat.ts b/apps/presentation/dashboard/src/data/chat.ts index d7c740c9f6..469d61ab94 100644 --- a/apps/presentation/dashboard/src/data/chat.ts +++ b/apps/presentation/dashboard/src/data/chat.ts @@ -1093,7 +1093,8 @@ export async function fetchChatSessionHistory(session: ChatSessionSummary | stri } async function readChatSessionHistory(session: ChatSessionSummary | string, signal?: AbortSignal, turnId?: string, - progress?: ChatHistoryReadProgress, checkpoint?: (progress: ChatHistoryReadProgress) => void): Promise { + progress?: ChatHistoryReadProgress, checkpoint?: (progress: ChatHistoryReadProgress) => void, + linkImages = false): Promise { const sessionId = typeof session === "string" ? session : session.session_id; // A delegation has the original Session ID; pin its first verified page's // scope. History discovery callers keep validating against their own source. @@ -1113,7 +1114,8 @@ async function readChatSessionHistory(session: ChatSessionSummary | string, sign if (cursor !== null) query.set("message_cursor", cursor); const snapshot = await requestJson( `/api/chat/sessions/${encodeURIComponent(sessionId)}?${query}`, - {signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(CHAT_HISTORY_READ_TIMEOUT_MS)]) : AbortSignal.timeout(CHAT_HISTORY_READ_TIMEOUT_MS)}); + {signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(CHAT_HISTORY_READ_TIMEOUT_MS)]) : AbortSignal.timeout(CHAT_HISTORY_READ_TIMEOUT_MS), + headers: historyImageHeaders(linkImages)}); signal?.throwIfAborted(); const expected = expectedSession; const sameSource = snapshot.session?.session_id === sessionId && (!expected || sameHistorySource(snapshot.session, expected)); @@ -1167,7 +1169,13 @@ function historyBasisMatches(basis: string, count: number) { && match[2].length === Math.ceil(count / 200) * 64; } -async function tryResumeChatSessionHistory(session: ChatSessionSummary, saved: ChatSessionSnapshot) { +function historyImageHeaders(linkImages: boolean): HeadersInit | undefined { + // The linked bytes are same-origin resources. A separately configured Chat + // server still uses the compatible inline representation. + return linkImages && !configuredChatOrigin ? {"X-LoopX-History-Images": "linked"} : undefined; +} + +async function tryResumeChatSessionHistory(session: ChatSessionSummary, saved: ChatSessionSnapshot, linkImages: boolean) { const basis = saved.verifiedHistoryBasis; if (!basis || !sameHistorySource(saved.session, session) || !historyBasisMatches(basis, saved.messages.length)) return null; const query = new URLSearchParams({message_limit: "200", known_history: basis}); @@ -1175,7 +1183,7 @@ async function tryResumeChatSessionHistory(session: ChatSessionSummary, saved: C try { snapshot = await requestJson( "/api/chat/sessions/" + encodeURIComponent(session.session_id) + "?" + query, - {signal: AbortSignal.timeout(CHAT_HISTORY_READ_TIMEOUT_MS)}); + {signal: AbortSignal.timeout(CHAT_HISTORY_READ_TIMEOUT_MS), headers: historyImageHeaders(linkImages)}); } catch (error) { // A service rollback may no longer accept this optional argument. if (error instanceof ChatApiError && error.payload.http_status === 400) return null; @@ -1351,9 +1359,10 @@ export async function fetchChatHistory(options: { const saved = options.bound && !progress && previous?.snapshots.find(row => row.session.session_id === session.session_id); let snapshot: ChatSessionSnapshot; try { - snapshot = (saved ? await tryResumeChatSessionHistory(session, saved) : null) + const linkImages = Boolean(options.bound); + snapshot = (saved ? await tryResumeChatSessionHistory(session, saved, linkImages) : null) ?? await readChatSessionHistory(session, undefined, undefined, progress, - checkpoint => pendingReads.set(session.session_id, checkpoint)); + checkpoint => pendingReads.set(session.session_id, checkpoint), linkImages); } catch (error) { // Reuse the hook's existing retry schedule only for a lost transport. // Revoked sources, invalid pages and stale cursors must start a fresh read. diff --git a/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx b/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx index 3fe4f5dda2..41cf98d720 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx @@ -210,7 +210,7 @@ export function ChannelTimeline({
{item.message.role === "user" ? item.message.agentLabel ?? t("common.you") : item.message.agentLabel ?? t("header.manager")}{item.message.time ? : null}
{item.message.queueInputCancelled ? {locale === "zh-CN" ? "已取消排队" : "Cancelled before starting"} : null} - {item.message.attachments?.length ?
{item.message.attachments.map((attachment) => isDocumentAttachment(attachment.mimeType) ? : {attachment.name})}
: null} + {item.message.attachments?.length ?
{item.message.attachments.map((attachment) => isDocumentAttachment(attachment.mimeType) ? : {attachment.name})}
: null} {item.message.role === "user" ?

{item.message.text}

: item.message.text ? : null} diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css index 2c88ae0b46..e5fa4fe3f1 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css @@ -543,7 +543,7 @@ button.personal-execution-chip:focus-visible { outline: 2px solid #0070f3; outli .personal-return-delivery.is-verification_required { color: var(--pw-blue-ink); } .personal-return-delivery.is-explicit_unverified { color: var(--pw-amber); } .personal-message-images { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 8px; } -.personal-message-images img { width: min(220px, 100%); max-height: 180px; object-fit: cover; border: 1px solid var(--pw-line-strong); border-radius: 10px; background: #fff; } +.personal-message-images img { width: min(220px, 100%); max-height: 180px; aspect-ratio: auto 4 / 3; object-fit: cover; border: 1px solid var(--pw-line-strong); border-radius: 10px; background: #fff; } /* Agent prose: card bubble + rendered Markdown */ .personal-message:not(.is-user) > div { diff --git a/docs/architecture/rfcs/app-conversation-and-async-inbox-v0.md b/docs/architecture/rfcs/app-conversation-and-async-inbox-v0.md index 2746a0e2ee..235aae9182 100644 --- a/docs/architecture/rfcs/app-conversation-and-async-inbox-v0.md +++ b/docs/architecture/rfcs/app-conversation-and-async-inbox-v0.md @@ -352,6 +352,13 @@ invalidate any affected prefix, and a changed notice refreshes the App's transcript even when no Turn runs. The notice carries no resource bytes or credentials, creates no Turn, and does not resend the original reply. +For same-origin, bound steward history, the App can request image addresses in +place of inline base64. The history proof includes the address derived from the +exact original image. The image read checks the Session, message, slot and +content digest before returning the original bytes. The App loads images near +the viewport; legacy clients, documents, and Turn input keep their existing +formats. + The implementation decision follows primary-source comparisons, not a claim that shared context already guarantees two-way delivery: diff --git a/docs/architecture/rfcs/app-conversation-and-async-inbox-v0.zh-CN.md b/docs/architecture/rfcs/app-conversation-and-async-inbox-v0.zh-CN.md index b2c7cb0de8..2b2114194e 100644 --- a/docs/architecture/rfcs/app-conversation-and-async-inbox-v0.zh-CN.md +++ b/docs/architecture/rfcs/app-conversation-and-async-inbox-v0.zh-CN.md @@ -247,6 +247,11 @@ App 身份替换;安装态 provider 读回另行验收。 历史分页,迟到记录会使受影响的旧游标失效;即使没有新 Turn,提示变化也会触发 App 刷新。提示不携带资源字节或凭据,不创建 Turn,也不重发原回复。 +同源 App 读取已绑定的管家历史时,可请求图片地址,不必在历史分页内传输 base64。 +历史校验包含由原图内容导出的地址。读取图片时核对原 Session、消息、附件位置和 +内容摘要,再返回原字节。App 在图片接近可见区域时加载;旧客户端、文档和 Turn +输入继续使用原有格式。 + 实现判断来自一手源码比较,不把共享上下文视为双向送达已经成立: - [OpenClaw 历史增量](https://github.com/openclaw/openclaw/blob/b3ba4d622638ac23f64c0cec415bf5bfe81f5cf5/src/gateway/server-methods/chat-history-delta.ts) diff --git a/loopx/chat/journal.py b/loopx/chat/journal.py new file mode 100644 index 0000000000..5bc29b27fb --- /dev/null +++ b/loopx/chat/journal.py @@ -0,0 +1,22 @@ +"""Decode Chat journal rows for both complete and scoped history reads.""" + +import json +from typing import Any + + +def decode_chat_journal(lines: list[bytes], *, strict: bool) -> list[dict[str, Any]]: + rows: list[dict[str, Any]] = [] + for line in lines: + if not line.strip(): + continue + try: + item = json.loads(line) + except (UnicodeDecodeError, json.JSONDecodeError) as exc: + if strict: + raise ValueError("invalid Chat journal record") from exc + continue + if isinstance(item, dict): + rows.append(item) + elif strict: + raise ValueError("invalid Chat journal record") + return rows diff --git a/loopx/chat/result_files_api.py b/loopx/chat/result_files_api.py index 8a59a6f2e7..12fab1242d 100644 --- a/loopx/chat/result_files_api.py +++ b/loopx/chat/result_files_api.py @@ -1,14 +1,89 @@ -"""Download an immutable returned file through its existing original Chat row.""" +"""Read media from its original Chat message.""" -from urllib.parse import quote +import re +from base64 import b64decode +from hashlib import sha256 +from urllib.parse import quote, unquote from ..capabilities.manager_context.roundtrip import read_chat_return_file +from ..chat_attachments import CHAT_IMAGE_MAX_BYTES, CHAT_IMAGE_TYPES from ..control_plane.effect_runtime import effect_runtime_request_scope +def is_bound_steward_history(session): + context = session.get("steward_context") if session else None + return isinstance(context, dict) and context.get("kind") == "bound_steward" + + +def _inline_image(attachment): + if not isinstance(attachment, dict): + return None + mime, data, size = (attachment.get(k) for k in ("mime_type", "data_url", "size")) + if (not isinstance(mime, str) or mime not in CHAT_IMAGE_TYPES or not isinstance(data, str) + or type(size) is not int or not 0 < size <= CHAT_IMAGE_MAX_BYTES + or not data.startswith(f"data:{mime};base64,") + or len(data.partition(",")[2]) != ((size + 2) // 3) * 4): + return None + return mime, data, size + + +def link_chat_history_images(session_id, messages): + """Project history images as scoped URLs without changing the stored rows.""" + result = [] + for message in messages: + attachments = message.get("attachments") + if not isinstance(attachments, list): + result.append(message) + continue + projected = [] + for index, attachment in enumerate(attachments): + image = _inline_image(attachment) + if image is None: + projected.append(attachment) + continue + _, data, _ = image + # The URL changes if the image bytes change, so the existing visible + # page digest and history resume proof still bind the original image. + identity = sha256(data.encode()).hexdigest() + route = (f"/api/chat/sessions/{quote(session_id, safe='')}/messages/" + f"{quote(message['message_id'], safe='')}/images/{index}/{identity}") + projected.append({**attachment, "data_url": route}) + result.append({**message, "attachments": projected}) + return result + + +def read_chat_history_image(store, session_id, message_id, index, identity): + session = store.load_session(session_id) + if not is_bound_steward_history(session): + raise KeyError("session unavailable") + attachments = store.read_history_message(session_id, message_id).get("attachments") + if not isinstance(attachments, list) or not 0 <= index < len(attachments): + raise KeyError("image unavailable") + image = _inline_image(attachments[index]) + if image is None: + raise KeyError("image unavailable") + mime, data, size = image + if sha256(data.encode()).hexdigest() != identity: + raise KeyError("image version unavailable") + body = b64decode(data.partition(",")[2], validate=True) + if len(body) != size: + raise KeyError("image unavailable") + return mime, body + + class ChatResultFilesRequestMixin: + def _link_history_images(self, session_id, session, selection, rows): + if (self.headers.get("X-LoopX-History-Images") == "linked" + and selection["message_limit"] is not None and selection["turn_id"] is None + and is_bound_steward_history(session)): + return link_chat_history_images(session_id, rows) + return rows + def _dispatch_result_file_read(self, path: str) -> bool: parts = path.strip("/").split("/") + if (len(parts) == 9 and parts[:3] == ["api", "chat", "sessions"] + and parts[4] == "messages" and parts[6] == "images"): + return self._serve_chat_history_image(parts) if (len(parts) != 8 or parts[:3] != ["api", "chat", "sessions"] or parts[4] != "messages" or parts[6] != "files"): return False @@ -33,3 +108,29 @@ def _dispatch_result_file_read(self, path: str) -> bool: self.end_headers() self.wfile.write(body) return True + + def _serve_chat_history_image(self, parts) -> bool: + if not self._require_loopback_origin(): + return True + if self.headers.get("Sec-Fetch-Site") == "cross-site": + self._send_error("Conversation images are only available to the original App.", status=403) + return True + try: + if not re.fullmatch(r"[0-9]{1,3}", parts[7]) or not re.fullmatch(r"[a-f0-9]{64}", parts[8]): + raise KeyError("image unavailable") + mime, body = read_chat_history_image( + self.server.chat_store, unquote(parts[3]), unquote(parts[5]), int(parts[7]), parts[8]) + except (KeyError, ValueError): + self._send_error("Image was not found in this conversation.", status=404) + except (OSError, RuntimeError): + self._send_error("Conversation image is temporarily unavailable.", status=503) + else: + self.send_response(200) + self.send_header("Content-Type", mime) + self.send_header("Content-Length", str(len(body))) + self.send_header("Cache-Control", "no-store") + self.send_header("Cross-Origin-Resource-Policy", "same-origin") + self.send_header("X-Content-Type-Options", "nosniff") + self.end_headers() + self.wfile.write(body) + return True diff --git a/loopx/chat_server.py b/loopx/chat_server.py index 711818a2a4..a11f618a77 100644 --- a/loopx/chat_server.py +++ b/loopx/chat_server.py @@ -942,8 +942,8 @@ def _session_snapshot(self, session_id: str) -> None: self.server.runtime_controller.coordination_runtime_root, store, session_id, registry=self.server.registry_path, selection=selection, project_history_messages=(lambda messages: media.insert(messages, markers)) if markers else None, - project_provider=lambda rows: project_private_reply_history( - store, session, rows)) + project_provider=lambda rows: self._link_history_images( + session_id, session, selection, project_private_reply_history(store, session, rows))) if markers and "session" in payload: payload["session"] = media.summary(payload["session"], markers) self._send_json(payload) diff --git a/loopx/chat_store.py b/loopx/chat_store.py index f85ddfc332..0fd9ab8516 100644 --- a/loopx/chat_store.py +++ b/loopx/chat_store.py @@ -26,6 +26,7 @@ normalize_session_model_settings, session_model_settings_supported, normalize_session_model_changes, ) from .chat.event_cache import ChatEventCache +from .chat.journal import decode_chat_journal from .chat_ingress import ( ChatIngressStore, SESSION_QUEUE_MAX_PENDING as SESSION_QUEUE_MAX_PENDING, @@ -100,25 +101,7 @@ def _read_jsonl( if raise_on_error: raise return [] - return _decode_jsonl(lines, strict=strict) - - -def _decode_jsonl(lines: list[bytes], *, strict: bool) -> list[dict[str, Any]]: - rows: list[dict[str, Any]] = [] - for line in lines: - if not line.strip(): - continue - try: - item = json.loads(line) - except (UnicodeDecodeError, json.JSONDecodeError) as exc: - if strict: - raise ValueError("invalid Chat journal record") from exc - continue - if isinstance(item, dict): - rows.append(item) - elif strict: - raise ValueError("invalid Chat journal record") - return rows + return decode_chat_journal(lines, strict=strict) def _session_channel(payload: dict[str, Any]) -> str: @@ -901,7 +884,9 @@ def messages(self, session_id: str, *, strict: bool = False) -> list[dict[str, A return _read_jsonl(self._session_dir(session_id) / "messages.jsonl", raise_on_error=strict, strict=strict) - def _paged_history_messages(self, session_id: str) -> list[dict[str, Any]]: + def _paged_history_messages( + self, session_id: str, *, copy_cached: bool = True, + ) -> list[dict[str, Any]]: """Reuse one bounded parse only after a fresh read of identical bytes.""" try: raw = (self._session_dir(session_id) / "messages.jsonl").read_bytes() @@ -910,17 +895,27 @@ def _paged_history_messages(self, session_id: str) -> list[dict[str, Any]]: digest = hashlib.sha256(raw).digest() if len(raw) <= 64 * 1024 * 1024 else None cached = self._paged_history if cached is not None and cached[:2] == (session_id, digest): + if not copy_cached: + return cached[2] with suppress(RecursionError): return deepcopy(cached[2]) self._paged_history = None lines = raw.split(b"\n") del raw - rows = _decode_jsonl(lines, strict=True) + rows = decode_chat_journal(lines, strict=True) if digest is not None and len(rows) <= 8192: with suppress(RecursionError): self._paged_history = (session_id, digest, deepcopy(rows)) return rows + def read_history_message(self, session_id: str, message_id: str) -> dict[str, Any]: + """Read one original row, sharing the verified page parse for image reads.""" + matches = [row for row in self._paged_history_messages(session_id, copy_cached=False) + if row.get("message_id") == message_id] + if len(matches) != 1: + raise KeyError("message unavailable") + return deepcopy(matches[0]) + def prepared_managed_turn_request( self, session_id: str, diff --git a/tests/test_manager_context_roundtrip.py b/tests/test_manager_context_roundtrip.py index f7bb0b280d..6b9af51f99 100644 --- a/tests/test_manager_context_roundtrip.py +++ b/tests/test_manager_context_roundtrip.py @@ -95,12 +95,16 @@ def private_snapshot(frozen_private_reply, tmp_path): thread = threading.Thread(target=server.serve_forever, daemon=True) thread.start() - def read(*, expected_status=200, **query): + def read(*, expected_status=200, headers=None, path=None, raw=False, **query): connection = http.client.HTTPConnection(*server.server_address, timeout=15) try: - connection.request("GET", f"/api/chat/sessions/{session['session_id']}?{urlencode(query)}") + connection.request("GET", path or f"/api/chat/sessions/{session['session_id']}?{urlencode(query)}", + headers=headers or {}) response = connection.getresponse() - payload = json.loads(response.read()) + body = response.read() + if raw: + return response.status, dict(response.getheaders()), body + payload = json.loads(body) assert response.status == expected_status, payload return payload finally: @@ -1351,6 +1355,57 @@ def test_returned_file_http_rechecks_original_source_and_snapshot(returned_file_ assert json.loads(raw)["ok"] is False +def test_original_steward_history_links_images_but_preserves_bytes_and_cursor( + frozen_private_reply, private_snapshot): + from base64 import b64decode, b64encode + + store, session, *_ = frozen_private_reply + session_id = session["session_id"] + encoded = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jrWQAAAAASUVORK5CYII=" + body = b64decode(encoded) + image = {"id": "one", "name": "original.png", "mime_type": "image/png", + "size": len(body), "data_url": f"data:image/png;base64,{encoded}"} + row = store.append_message(session_id, role="user", text="Keep my original image", + origin="lark", attachments=[image]) + headers = {"X-LoopX-History-Images": "linked"} + + def message(payload): + return next(r for r in payload["messages"] if r["message_id"] == row["message_id"]) + + assert message(private_snapshot(message_limit=200))["attachments"] == [image] + snapshot = private_snapshot(message_limit=200, headers=headers) + projected = message(snapshot)["attachments"][0] + url = projected["data_url"] + assert url.startswith(f"/api/chat/sessions/{session_id}/messages/{row['message_id']}/images/0/") + assert projected == {**image, "data_url": url} + assert message(snapshot)["text"] == row["text"] + assert store.messages(session_id, strict=True)[-1] == row + + code, image_headers, saved_bytes = private_snapshot(path=url, raw=True) + assert code == 200 and saved_bytes == body + assert image_headers["Content-Type"] == "image/png" + assert image_headers["Cross-Origin-Resource-Policy"] == "same-origin" + assert private_snapshot(path=url, raw=True, headers={"Sec-Fetch-Site": "cross-site"})[0] == 403 + assert private_snapshot(path=url, raw=True, headers={"Origin": "https://unrelated.example"})[0] == 403 + assert private_snapshot(path=url.replace(row["message_id"], "unknown-message"), raw=True)[0] == 404 + assert private_snapshot(path=url.replace(session_id, "unknown-session"), raw=True)[0] == 404 + assert private_snapshot(path=url.rsplit("/", 1)[0] + "/" + "0" * 64, raw=True)[0] == 404 + + page = snapshot["message_page"] + basis = f"{page['total']}:{page['visible_page_sha256']}" + resumed = private_snapshot(message_limit=200, known_history=basis, headers=headers) + assert resumed["history_resume"]["next"] == basis and resumed["messages"] == [] + assert private_snapshot(message_limit=200, known_history=basis)["status"] == "restart" + + journal = store.sessions_root / session_id / "messages.jsonl" + changed_bytes = body[:-1] + bytes([body[-1] ^ 1]) + journal.write_bytes(journal.read_bytes().replace(encoded.encode(), b64encode(changed_bytes), 1)) + assert private_snapshot(path=url, raw=True)[0] == 404 + current_url = message(private_snapshot(message_limit=200, headers=headers))["attachments"][0]["data_url"] + assert current_url != url + assert private_snapshot(path=current_url, raw=True)[2] == changed_bytes + + def test_download_attachment_does_not_expand_chat_input_admission(returned_file_history): from loopx.chat_attachments import normalize_chat_attachments From 5860235b251cbd5887a2280f811ea0bb7de58a00 Mon Sep 17 00:00:00 2001 From: huangruiteng Date: Wed, 11 Nov 2026 20:18:27 +0800 Subject: [PATCH 005/267] fix(refresh): retain original settlement recovery through maintenance Signed-off-by: huangruiteng --- docs/quota-allocation.md | 28 ++++ .../project_lifecycle_refresh_state.py | 8 ++ loopx/cli_commands/refresh_recovery.py | 70 ++++++++++ loopx/control_plane/quota/settlement.py | 11 ++ .../quota/settlement_readback.ts | 41 +++++- .../references/autonomous-execution.md | 9 ++ .../test_effective_turn_replan.py | 15 +- .../test_refresh_settlement_recovery.py | 130 ++++++++++++++++++ .../quota_settlement_readback.test.ts | 80 ++++++++++- 9 files changed, 389 insertions(+), 3 deletions(-) create mode 100644 loopx/cli_commands/refresh_recovery.py create mode 100644 tests/control_plane/test_refresh_settlement_recovery.py diff --git a/docs/quota-allocation.md b/docs/quota-allocation.md index 20e25c1b01..6193c456e1 100644 --- a/docs/quota-allocation.md +++ b/docs/quota-allocation.md @@ -344,6 +344,34 @@ caller appends the refresh receipt and rereads the chain; it must not offer a spend command before that point. Older guards without a persisted spend source retain the existing heartbeat default. +A rejected `refresh-state`, or an Agent's state-only maintenance update, also +returns `settlement_recovery` when a persisted Turn still owes closeout. Its +identity and progress come from the same typed receipt reader as guard recovery. +The TS `pending_closeout` read selects the candidate, validates the original +binding and evaluates receipts under one admitted owner and immutable snapshot; +Python only adapts CLI input and renders the returned recovery. This read mode +cannot be combined with settlement admission or latest-Run inference. +`ok: true` on a state update describes that update only; it does not settle the +nested original Turn. Follow `settlement_recovery.next_cli_actions`: before a +verified writeback this reenters the original bound guard; afterwards it offers +only the remaining spend. A stale replan basis still requires reading the +returned `replan_transition.context_recovery` and correcting the evidence-linked +packet before retry. No recovery read creates a Turn or debits quota. Missing +or conflicting receipt authority is reported as `settlement_recovery_error`, +never inferred as successful settlement. Setup with no admitted Turn, other +Agents' Turns, and already settled work do not produce pending recovery. + +`refresh-state` 被拒绝,或 Agent 只更新升级/维护状态时,若已有 Turn 尚欠结算, +响应会返回 `settlement_recovery`,身份与进度沿用 guard 恢复的 typed receipt reader。 +TS `pending_closeout` 在同一准入 owner、同一不可变快照下完成候选选择、原绑定校验 +及收据判断;Python 只适配 CLI 参数和展示,不再分两次读取决定恢复对象。这个只读 +模式不能与结算准入或最新 Run 推断混用。 +状态更新的 `ok: true` 只表示该更新成功,不代表嵌套的原 Turn 已结清。执行其中的 +`next_cli_actions`:写回验证前回到原绑定 guard,验证后只补剩余扣账。重规划依据 +过期仍须读取 `replan_transition.context_recovery`,修正证据关联的 packet 后再试。 +恢复读取不新建 Turn、不扣额;收据不可读或身份冲突会显示 `settlement_recovery_error`, +不能视为已结算。没有已准入 Turn 的设置工作、其它 Agent 和已结清工作不产生待恢复项。 + ## Minimal Contract The compact status shape can start with a small object: diff --git a/loopx/cli_commands/project_lifecycle_refresh_state.py b/loopx/cli_commands/project_lifecycle_refresh_state.py index e535120fbd..5d35857339 100644 --- a/loopx/cli_commands/project_lifecycle_refresh_state.py +++ b/loopx/cli_commands/project_lifecycle_refresh_state.py @@ -82,6 +82,7 @@ ) from ..control_plane.quota.cli_projection import compact_refresh_state_cli_payload from .refresh_settlement import settle_requested_refresh +from .refresh_recovery import attach_refresh_settlement_recovery def register_refresh_state_command( @@ -668,6 +669,7 @@ def handle_refresh_state_command( "error": str(exc), **({"error_code": exc.code, **getattr(exc, "payload", {})} if isinstance(getattr(exc, "code", None), str) else {}), } + attach_refresh_settlement_recovery(payload, args, registry_path, goal_ref) if isinstance(exc, ReplanWritebackRejected): transition = project_replan_writeback_rejection( exc, @@ -686,6 +688,10 @@ def handle_refresh_state_command( "current_context_id": recovery["replan_context"]["context_id"], "instruction": recovery["instruction"], **reference, } + settlement_recovery = payload.get("settlement_recovery") + if isinstance(settlement_recovery, dict) and transition["host_action"] == "write_typed_semantic_delta": + transition["host_action"] = "read_current_replan_context" + transition["next_cli_actions"] = settlement_recovery["next_cli_actions"] payload["replan_transition"] = transition if transition["next_cli_actions"]: payload["error"] += " Required transition: " + "; ".join( @@ -884,6 +890,7 @@ def handle_refresh_state_command( ) if not material_refresh_ready: settle_requested_refresh(payload, args, registry_path, goal_ref) + attach_refresh_settlement_recovery(payload, args, registry_path, goal_ref) print_payload(compact_refresh_state_cli_payload(payload) if fmt == "json" else payload, fmt, render_state_refresh_markdown) return 0 if payload.get("ok") else 1 if payload.get("explore_result") is not None: @@ -974,5 +981,6 @@ def handle_refresh_state_command( ), ) settle_requested_refresh(payload, args, registry_path, goal_ref) + attach_refresh_settlement_recovery(payload, args, registry_path, goal_ref) print_payload(compact_refresh_state_cli_payload(payload) if fmt == "json" else payload, fmt, render_state_refresh_markdown) return 0 if payload.get("ok") else 1 diff --git a/loopx/cli_commands/refresh_recovery.py b/loopx/cli_commands/refresh_recovery.py new file mode 100644 index 0000000000..5d664836cf --- /dev/null +++ b/loopx/cli_commands/refresh_recovery.py @@ -0,0 +1,70 @@ +"""Keep a rejected write or maintenance refresh attached to its owed Turn. + +The shared TS closeout reader selects persisted debt; this adapter only exposes +its exact readback and CLI continuation. It never admits or settles a Turn. +""" +from __future__ import annotations + +import argparse +import shlex +from collections.abc import Mapping +from pathlib import Path +from typing import Any + +from ..control_plane.quota.settlement import attach_settlement_progress, read_heartbeat_settlement +from ..control_plane.work_items.action_selection_contract import render_cli_command_prefix +from ..history import load_registry +from ..paths import resolve_runtime_root + + +def attach_refresh_settlement_recovery( + payload: dict[str, Any], args: argparse.Namespace, registry_path: Path, + goal_ref: Mapping[str, Any] | None, +) -> None: + """Expose existing debt without turning a state update into a write receipt.""" + if (args.dry_run or not args.agent_id or isinstance(payload.get("settlement_progress"), dict) + or "settlement_recovery" in payload or "settlement_recovery_error" in payload): + return + try: + runtime_root = resolve_runtime_root(load_registry(registry_path), args.runtime_root, + registry_path=registry_path) + # TS selects persisted debt and validates every caller-supplied binding + # against one immutable snapshot; Python supplies no latest-Run guess. + readback = read_heartbeat_settlement(runtime_root, goal_id=args.goal_id, + agent_id=args.agent_id, todo_id=args.todo_id, + replan_obligation_id=args.replan_obligation_id, turn_instance_id=args.turn_instance_id, + pending_closeout=True, include_record_bodies=False, + registry_path=registry_path, goal_ref=goal_ref) + if readback is None: + return + if readback.identity.failure is not None: + raise ValueError(readback.identity.failure.reason) + identity = readback.identity.value + if identity is None: + raise RuntimeError("original Turn settlement identity is unavailable") + recovery: dict[str, Any] = {"ok": True} + attach_settlement_progress(recovery, readback, registry_path=registry_path, + runtime_root=runtime_root, goal_ref=goal_ref) + recovery.pop("ok") + owed = recovery.get("settlement_owed") + prefix = render_cli_command_prefix(registry_path=str(registry_path), runtime_root=str(runtime_root)) + # Select the complete CLI consumer explicitly: an unprofiled guard can + # return legacy unbound refresh templates. Replan guards recover their + # obligation from the Turn receipt (that flag is spend-only on quota). + command = prefix + " --format json quota should-run " + shlex.join([ + "--complete-turn", "--runtime-profile", "generic_cli", + "--goal-id", identity.goal_id, "--agent-id", identity.agent_id, + "--turn-instance-id", identity.turn_instance_id, + *(["--todo-id", identity.todo_id] if identity.todo_id else []), + *(["--goal-instance-id", str(goal_ref["goal_instance_id"])] if goal_ref is not None else []), + *[word for capability in args.available_capabilities or [] for word in ("--available-capability", capability)], + ]) + recovery.update(required=True, settlement_identity=identity.as_dict(), + next_cli_actions=[owed["command"] if isinstance(owed, dict) else command], + instruction="This response does not settle the original Turn. Follow its recovery with the same identity; " + "state-only updates and installation maintenance cannot replace accountable writeback.") + payload["settlement_recovery"] = recovery + except (OSError, RuntimeError, ValueError) as error: + # Preserve the original write result, including an already committed + # state update. An unavailable read is not evidence of no pending debt. + payload["settlement_recovery_error"] = str(error) diff --git a/loopx/control_plane/quota/settlement.py b/loopx/control_plane/quota/settlement.py index 4ea55d529d..cbf8a4d27a 100644 --- a/loopx/control_plane/quota/settlement.py +++ b/loopx/control_plane/quota/settlement.py @@ -226,6 +226,15 @@ def attach_settlement_progress( def render_settlement_progress_markdown(payload: dict[str, Any]) -> list[str]: + recovery = payload.get("settlement_recovery") + if isinstance(recovery, dict): + identity = recovery["settlement_identity"] + lines = [f"- Pending settlement: `{identity['turn_instance_id']}`", recovery["instruction"]] + for command in recovery["next_cli_actions"]: + lines.extend(["", "```sh", command, "```"]) + return lines + if payload.get("settlement_recovery_error"): + return [f"- Settlement recovery could not be read: {payload['settlement_recovery_error']}"] progress = payload.get("settlement_progress") if not isinstance(progress, dict): return [] @@ -443,6 +452,7 @@ def read_heartbeat_settlement( infer_turn_instance_id: bool = False, allow_unbound_binding: bool = False, resolve_original_binding: bool = False, + pending_closeout: bool = False, refresh_retry: dict[str, Any] | None = None, registry_path: Path | None = None, goal_ref: Mapping[str, Any] | None = None, @@ -469,6 +479,7 @@ def read_heartbeat_settlement( else {} ), **({"resolve_original_binding": True} if resolve_original_binding else {}), + **({"pending_closeout": True} if pending_closeout else {}), **({"refresh_retry": refresh_retry} if refresh_retry is not None else {}), **({"borrow_source_admission": True} if borrow_source_admission else {}), **( diff --git a/loopx/control_plane/quota/settlement_readback.ts b/loopx/control_plane/quota/settlement_readback.ts index d77a381bc3..87c8b617ce 100644 --- a/loopx/control_plane/quota/settlement_readback.ts +++ b/loopx/control_plane/quota/settlement_readback.ts @@ -108,6 +108,7 @@ interface ReadbackRequest { infer_turn_instance_id: boolean; allow_unbound_binding: boolean; resolve_original_binding: boolean; + pending_closeout: boolean; borrow_source_admission: boolean; include_record_bodies: boolean; refresh_retry: RefreshRetryRequest | null; @@ -227,6 +228,13 @@ function decodeRequest( if (request.resolve_original_binding === true && request.infer_turn_instance_id) { throw new EffectRuntimeRequestError("original binding requires an explicit Turn identity"); } + if (request.pending_closeout !== undefined && typeof request.pending_closeout !== "boolean") { + throw new EffectRuntimeRequestError("pending_closeout must be a boolean"); + } + if (request.pending_closeout === true && (request.infer_turn_instance_id || request.allow_unbound_binding + || request.refresh_retry != null || request.heartbeat_reentry_guard != null || request.spend_precheck === true)) { + throw new EffectRuntimeRequestError("pending closeout is a read-only receipt recovery, not inference or settlement admission"); + } if ( request.borrow_source_admission !== undefined && typeof request.borrow_source_admission !== "boolean" @@ -281,6 +289,7 @@ function decodeRequest( infer_turn_instance_id: request.infer_turn_instance_id, allow_unbound_binding: request.allow_unbound_binding, resolve_original_binding: request.resolve_original_binding === true, + pending_closeout: request.pending_closeout === true, borrow_source_admission: request.borrow_source_admission === true, include_record_bodies: request.include_record_bodies !== false, refresh_retry: decodeRefreshRetry(request.refresh_retry), @@ -1100,7 +1109,7 @@ function owedPriorTurnFromSnapshot( request: ReadbackRequest, snapshot: QuotaSettlementReadbackSnapshot, agentId: string, - turnInstanceId: string, + turnInstanceId: string | null, ): OwedPriorTurn | null { const receipts = heartbeatReceiptsFromEvents(snapshot.events, request.goal_id, agentId) .filter((event) => quotaOwnerOwnsProjection(request.owner, event.goal_ref)); @@ -1132,6 +1141,7 @@ function owedPriorTurnFromSnapshot( infer_turn_instance_id: false, allow_unbound_binding: false, resolve_original_binding: false, + pending_closeout: false, refresh_retry: null, heartbeat_reentry_guard: null, }, @@ -1146,6 +1156,34 @@ function owedPriorTurnFromSnapshot( return null; } +/** Resolve pending recovery without another provider read or a latest-Run heuristic. */ +function readPendingCloseoutFromRequest( + request: ReadbackRequest, + snapshot: QuotaSettlementReadbackSnapshot, +): JsonObject { + const explicitAgentId = normalizeAgentId(request.agent_id); + if (explicitAgentId === null) { + throw new EffectRuntimeRequestError("pending closeout requires an explicit Agent"); + } + const notFound = {schema_version: QUOTA_SETTLEMENT_READBACK_RESULT_SCHEMA, found: false}; + const turn = request.turn_instance_id ?? owedPriorTurnFromSnapshot( + request, snapshot, explicitAgentId, null, + )?.prior_turn_instance_id; + if (!turn) return notFound; + // Selection, original binding validation and receipt progress share this + // immutable snapshot and admitted owner. Maintenance Run classifications + // cannot conceal the guard's debt or select another Agent's work. + const result = readQuotaSettlementFromRequest({...request, turn_instance_id: turn, + pending_closeout: false, resolve_original_binding: true}, snapshot, false); + const identity = jsonObject(jsonObject(jsonObject(result.identity)?.result)?.value); + if (jsonObject(jsonObject(result.settlement)?.payload)?.ok === true || + (identity?.todo_id && committedMonitorPollFromSnapshot(snapshot, { + goal_id: request.goal_id, agent_id: explicitAgentId, turn_instance_id: turn, + todo_id: String(identity.todo_id), + }, request.owner) !== null)) return notFound; + return result; +} + function readQuotaSettlementFromRequest( request: ReadbackRequest, snapshot: QuotaSettlementReadbackSnapshot, @@ -1161,6 +1199,7 @@ function readQuotaSettlementFromRequest( ); } const explicitAgentId = normalizeAgentId(request.agent_id); + if (request.pending_closeout) return readPendingCloseoutFromRequest(request, snapshot); // Only latest-Turn inference needs the full run history. Explicit readback // must use the existing Turn indexes before applying the Goal owner fence; // otherwise a batch over N Turns repeatedly scans all N receipts. diff --git a/skills/loopx-project/references/autonomous-execution.md b/skills/loopx-project/references/autonomous-execution.md index 06d9079736..44612dac19 100644 --- a/skills/loopx-project/references/autonomous-execution.md +++ b/skills/loopx-project/references/autonomous-execution.md @@ -174,6 +174,15 @@ returns. Do not reconstruct static refresh/spend commands from this skill. Accountable progress precedes its spend; uncertain effects require readback and recovery before retry. State-only refresh cannot replace accountable writeback. +After a rejected refresh or installation maintenance, inspect +`settlement_recovery` even when the state update reports `ok: true`. Execute its +`next_cli_actions` with the original Agent/Todo or replan/Turn identity. If the +rejection also returns `replan_transition.context_recovery`, read that current +basis and revise the evidence-linked packet before retrying its writeback. Do +not repeat the upgrade or treat a subsequent `surface_only` success as closing +the admitted work. A `settlement_recovery_error` leaves settlement unknown; +preserve the original write result and resolve the reported read failure. + The absence of an adapter run or an unchanged artifact does not waive settlement. Only setup metadata work with no admitted delivery may use a state-only refresh when needed. After admitted delivery, an optional state-only refresh follows its diff --git a/tests/control_plane/test_effective_turn_replan.py b/tests/control_plane/test_effective_turn_replan.py index abbbd002b9..f1446f733a 100644 --- a/tests/control_plane/test_effective_turn_replan.py +++ b/tests/control_plane/test_effective_turn_replan.py @@ -479,11 +479,24 @@ def call(*args: str, expected_code: int = 0) -> dict: assert transition["reason_code"] == "stride_review_basis_mismatch" assert transition["host_action"] == "read_current_replan_context" assert transition["next_cli_actions"] == [] # No unbound repeat of the rejected write. + owed_recovery = foreign["settlement_recovery"] + assert owed_recovery["settlement_identity"] == reread["heartbeat_receipt"]["settlement_identity"] + from tests.control_plane.test_quota_settlement_cli import _run_generated_cli + recovered_code, recovered_turn = _run_generated_cli(owed_recovery["next_cli_actions"][0], + registry_path=registry, cwd=project) + assert recovered_code == 0, json.dumps(recovered_turn, indent=2) + assert recovered_turn["heartbeat_receipt"]["settlement_identity"] == owed_recovery["settlement_identity"] + assert _heartbeat_receipt_count(runtime, "stride-due-review") == receipt_count + assert _spend_run_count(runtime) == spend_count reference = transition["context_recovery"] # A rejected write must not eagerly import historical acceptance/evidence # into the model. Its exact write-time basis remains independently readable. if os.name == "posix": - assert len(json.dumps(foreign, ensure_ascii=False).encode()) < 4096 + # Preserve the old diagnostic budget; the additive original-Turn + # recovery has its own bounded allowance, not historical context. + assert len(json.dumps({key: value for key, value in foreign.items() if key != "settlement_recovery"}, ensure_ascii=False).encode()) < 4096 + assert len(json.dumps(owed_recovery, ensure_ascii=False).encode()) < 2048 + assert len(json.dumps(foreign, ensure_ascii=False).encode()) < 6144 from loopx.control_plane.turn_driver.decision_capture import read_context_capture_file saved, _, recovery = read_context_capture_file(reference["path"], reference["sha256"], private=True) assert saved.stat().st_mode & 0o077 == 0 diff --git a/tests/control_plane/test_refresh_settlement_recovery.py b/tests/control_plane/test_refresh_settlement_recovery.py new file mode 100644 index 0000000000..6abdad73bc --- /dev/null +++ b/tests/control_plane/test_refresh_settlement_recovery.py @@ -0,0 +1,130 @@ +"""Maintenance and rejected writes cannot conceal an admitted Turn's debt.""" +from __future__ import annotations + +import shlex +import json +import subprocess +from contextlib import chdir, redirect_stdout +from io import StringIO +from types import SimpleNamespace + +import pytest + +from canonical_authority_fixture import isolate_sqlite_runtime +from test_quota_settlement_cli import ( + AGENT_ID, GOAL_ID, TODO_ID, TURN_ID, ALTERNATIVE_TODO_ID, + _heartbeat_receipt_count, _initialize_fixture_authority, _run_cli, + _run_generated_cli, _spend_run_count, _write_fixture, +) + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("profile,spend_source", [("codex_app_heartbeat", "heartbeat"), ("codex_app_ssh_goal", "visible-goal")]) +def test_rejected_write_and_upgrade_state_recover_original_turn_once(tmp_path, monkeypatch, provider, profile, spend_source): + isolate_sqlite_runtime(tmp_path, monkeypatch) + project, runtime, registry = _write_fixture(tmp_path) + _initialize_fixture_authority(project, runtime, provider=provider) + binding = ["--goal-id", GOAL_ID, "--agent-id", AGENT_ID] + refresh = ["refresh-state", *binding, "--no-global-sync", "--suppress-external-sinks"] + maintenance = [*refresh, "--classification", "local_upgrade_status_updated", "--delivery-outcome", "surface_only"] + rc, setup = _run_cli(registry, runtime, *maintenance, cwd=project) + assert rc == 0 and setup["ok"] and "settlement_recovery" not in setup, setup + rc, guard = _run_cli(registry, runtime, "quota", "should-run", "--runtime-profile", profile, *binding, + "--todo-id", TODO_ID, "--turn-instance-id", TURN_ID, cwd=project) + assert rc == 0 and guard["normal_delivery_allowed"], guard + identity = guard["heartbeat_receipt"]["settlement_identity"] + receipts = _heartbeat_receipt_count(runtime, TURN_ID) + + # The original incident omitted --turn-instance-id, then wrote surface_only. + rc, refused = _run_cli(registry, runtime, *refresh, "--todo-id", TODO_ID, + "--classification", "verified_delivery", "--delivery-outcome", "outcome_progress", cwd=project) + assert rc == 1 and not refused["appended"], refused + assert "turn_instance_id" in refused["error"] + recovery = refused["settlement_recovery"] + assert recovery["required"] and recovery["settlement_identity"] == identity + assert recovery["settlement_progress"]["next_step"] == "durable_writeback" + assert "settlement_owed" not in recovery + rc, surface = _run_cli(registry, runtime, *maintenance, cwd=project) + assert rc == 0 and surface["appended"], surface + assert surface["settlement_recovery"] == recovery + assert "settlement_identity" not in surface # This update is not a Turn writeback. + assert _spend_run_count(runtime) == 0 and _heartbeat_receipt_count(runtime, TURN_ID) == receipts + from loopx.state_refresh import render_state_refresh_markdown + assert TURN_ID in render_state_refresh_markdown(surface) + assert recovery["next_cli_actions"][0] in render_state_refresh_markdown(surface) + + # A conflicting selection is never silently repaired to a different work item. + rc, foreign = _run_cli(registry, runtime, *refresh, "--todo-id", ALTERNATIVE_TODO_ID, + "--turn-instance-id", TURN_ID, "--delivery-outcome", "outcome_progress", cwd=project) + assert rc == 1 and "settlement_recovery" not in foreign, foreign + assert "binding" in foreign["settlement_recovery_error"] + rc, other_agent = _run_cli(registry, runtime, "refresh-state", "--goal-id", GOAL_ID, + "--agent-id", "another-agent", "--classification", "other_lane_status", "--delivery-outcome", "surface_only", + "--no-global-sync", "--suppress-external-sinks", cwd=project) + assert "settlement_recovery" not in other_agent + + # Execute the emitted recovery, not a hand-constructed replacement identity. + rc, recovered = _run_generated_cli(recovery["next_cli_actions"][0], registry_path=registry, cwd=project) + assert rc == 0 and recovered["normal_delivery_allowed"], recovered + assert recovered["heartbeat_receipt"]["settlement_identity"] == identity + assert _heartbeat_receipt_count(runtime, TURN_ID) == receipts + command = next(action for action in recovered["interaction_contract"]["cli_channel"]["next_cli_actions"] + if "refresh-state" in shlex.split(action)) + command = command.replace("", "verified_delivery").replace("", "implementation").replace("", "outcome_progress") + command += " --format json --no-global-sync --suppress-external-sinks" + assert "--turn-instance-id" in shlex.split(command), command + assert "<" not in shlex.join(shlex.split(command)), command + # Interrupt the generated combined operation between writeback and debit. + # Maintenance must recover this partial commit without rewriting progress. + from loopx.cli import main + from loopx.cli_commands import refresh_settlement + def unavailable_child(argv, **kwargs): + assert "spend-slot" in argv and TURN_ID in argv + raise OSError("fixture interrupt before quota debit") + stdout = StringIO() + with monkeypatch.context() as fault, chdir(project), redirect_stdout(stdout): + fault.setattr(refresh_settlement, "subprocess", SimpleNamespace( + run=unavailable_child, TimeoutExpired=subprocess.TimeoutExpired)) + rc = main(shlex.split(command)[1:]) + written = json.loads(stdout.getvalue()) + assert rc == 1 and written["settlement_identity"] == identity, written + assert written["settlement_progress"]["next_step"] == "quota_spend" + + # Maintenance after a committed write offers only the owed debit, no replan. + rc, after_write = _run_cli(registry, runtime, *maintenance, cwd=project) + assert rc == 0, after_write + owed = after_write["settlement_recovery"]["settlement_owed"] + assert owed["effect_id"] == identity["effect_id"] + assert after_write["settlement_recovery"]["settlement_progress"]["quota_spend_source"] == spend_source + spend = after_write["settlement_recovery"]["next_cli_actions"][0] + assert spend == owed["command"] and "spend-slot" in spend + for _ in range(2): + rc, settled = _run_generated_cli(spend, registry_path=registry, cwd=project) + assert rc == 0 and settled["settlement_result"]["ok"], settled + assert {receipt["step_kind"] for receipt in settled["settlement_result"]["receipts"]} >= {"durable_writeback", "quota_spend"} + assert _spend_run_count(runtime) == 1 + rc, after_settlement = _run_cli(registry, runtime, *maintenance, cwd=project) + assert rc == 0 and "settlement_recovery" not in after_settlement, after_settlement + + +def test_unavailable_recovery_read_preserves_committed_state_result(tmp_path, monkeypatch): + isolate_sqlite_runtime(tmp_path, monkeypatch) + project, runtime, registry = _write_fixture(tmp_path) + _initialize_fixture_authority(project, runtime) + from loopx.cli import main + from loopx.cli_commands import refresh_recovery + def unavailable_read(*args, **kwargs): + raise RuntimeError("fixture closeout reader unavailable") + monkeypatch.setattr(refresh_recovery, "read_heartbeat_settlement", unavailable_read) + stdout = StringIO() + with chdir(project), redirect_stdout(stdout): + rc = main(["--registry", str(registry), "--runtime-root", str(runtime), "--format", "json", + "refresh-state", "--goal-id", GOAL_ID, "--agent-id", AGENT_ID, + "--classification", "local_upgrade_status_updated", "--delivery-outcome", "surface_only", + "--no-global-sync", "--suppress-external-sinks"]) + result = json.loads(stdout.getvalue()) + assert rc == 0 and result["appended"], result + assert result["settlement_recovery_error"] == "fixture closeout reader unavailable" + assert "settlement_recovery" not in result and _spend_run_count(runtime) == 0 + from pathlib import Path + assert json.loads(Path(result["json_path"]).read_text())["classification"] == "local_upgrade_status_updated" diff --git a/tests/control_plane_ts/quota_settlement_readback.test.ts b/tests/control_plane_ts/quota_settlement_readback.test.ts index bcea7eb4c7..32e74d95ec 100644 --- a/tests/control_plane_ts/quota_settlement_readback.test.ts +++ b/tests/control_plane_ts/quota_settlement_readback.test.ts @@ -12,7 +12,7 @@ import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import test from "node:test"; -import { settlementIdentity } from "../../loopx/control_plane/effect_program.ts"; +import { settlementIdentity, settlementIdentityPayload } from "../../loopx/control_plane/effect_program.ts"; import { createEffectRuntimeHandlers, dispatchEffectRuntimeMethod, @@ -28,6 +28,7 @@ import { QUOTA_SETTLEMENT_READBACK_REQUEST_SCHEMA, readQuotaSettlement, readQuotaSettlementSnapshot, + readQuotaSettlementFromSnapshot, } from "../../loopx/control_plane/quota/settlement_readback.ts"; import { PRIOR_HOST_TURN_CLOSEOUT_PREFLIGHT_REQUEST_SCHEMA, @@ -462,6 +463,83 @@ function request(runtimeRoot: string, overrides: Record = {}) { }; } +test("pending closeout survives maintenance and reads one original receipt snapshot", async t => { + for (const entry of [ + {state: "writeback_required"}, + {writeback: true, state: "spend_required"}, + {writeback: true, spend: true, remove: "quota_spend", state: "spend_receipt_required"}, + {writeback: true, spend: true, state: null}, + {monitor: true, state: null}, + {guard: false, state: null}, + {legacy: true, state: null}, + ]) await t.test(entry.state ?? "no debt", async () => { + const root = await fixture(entry); + try { + const log = join(root, "goals", goalId, "rollout-event-log.jsonl"); + const index = join(root, "goals", goalId, "runs", "index.jsonl"); + const events = (await readFile(log, "utf8")).trim().split("\n").filter(Boolean).map(line => JSON.parse(line)); + for (const event of events) if (event.event_kind === "quota_should_run") event.details.closeout_required = entry.legacy !== true; + await writeFile(log, events.filter(event => event.event_kind !== entry.remove).map(event => JSON.stringify(event)).join("\n") + "\n"); + await appendFile(index, JSON.stringify({goal_id: goalId, agent_id: agentId, + classification: "local_upgrade_status_updated", delivery_outcome: "surface_only"}) + "\n"); + const input = request(root, {pending_closeout: true, turn_instance_id: null, todo_id: null}); + const snapshot = await readQuotaSettlementSnapshot(root, goalId); + const result = readQuotaSettlementFromSnapshot(input, snapshot); + assert.equal(result.found, entry.state !== null); + if (entry.state !== null) { + assert.deepEqual((result.identity as any).result.value, settlementIdentityPayload(identity)); + assert.equal((result.progress as any).state, entry.state); + const mismatch = readQuotaSettlementFromSnapshot({...input, todo_id: "todo_foreign"}, snapshot); + assert.equal((mismatch.identity as any).payload.ok, false); + assert.match((mismatch.identity as any).result.failure.reason, /binding differs/); + } + assert.equal(readQuotaSettlementFromSnapshot({...input, agent_id: "another-agent"}, snapshot).found, false); + assert.deepEqual(await readQuotaSettlement(input), result); + // Candidate selection and exact readback use the already acquired snapshot. + await writeFile(log, "corrupt live history\n"); + assert.deepEqual(readQuotaSettlementFromSnapshot(input, snapshot), result); + await assert.rejects(readQuotaSettlement(input), /malformed/); + } finally {await rm(root, {recursive: true, force: true});} + }); +}); + +test("pending closeout keeps replan identity and rejects query/admission mixing", async () => { + const root = await fixture(); + try { + const log = join(root, "goals", goalId, "rollout-event-log.jsonl"); + const events = (await readFile(log, "utf8")).trim().split("\n").map(line => JSON.parse(line)); + const obligation = "replan-0000000000000001"; + const bound = settlementIdentity({goal_id: goalId, agent_id: agentId, todo_id: null, + replan_obligation_id: obligation, turn_instance_id: turnId}); + events[0].details = {closeout_required: true, replan_obligation_id: obligation, + semantic_replan_obligation_id: obligation, settlement_effect_id: bound.effect_id}; + await writeFile(log, events.map(event => JSON.stringify(event)).join("\n") + "\n"); + const input = request(root, {pending_closeout: true, turn_instance_id: null, todo_id: null}); + assert.deepEqual(((await readQuotaSettlement(input)).identity as any).result.value, settlementIdentityPayload(bound)); + for (const patch of [{infer_turn_instance_id: true}, {allow_unbound_binding: true}, + {refresh_retry: {}}, {heartbeat_reentry_guard: {}}, {spend_precheck: true}]) { + await assert.rejects(readQuotaSettlement({...input, ...patch}), /read-only receipt recovery/); + } + await assert.rejects(readQuotaSettlement({...input, pending_closeout: "yes"}), /must be a boolean/); + await assert.rejects(readQuotaSettlement({...input, agent_id: null}), /explicit Agent/); + } finally {await rm(root, {recursive: true, force: true});} +}); + +test("pending closeout cannot adopt another Goal instance's receipts", async () => { + const root = await fixture({goalRef: {goal_id: goalId, goal_instance_id: instanceA}}); + try { + const log = join(root, "goals", goalId, "rollout-event-log.jsonl"); + const event = JSON.parse((await readFile(log, "utf8")).trim()); + event.details.closeout_required = true; + await writeFile(log, JSON.stringify(event) + "\n"); + await withSourceAdmission(root, instanceB, instanceB, async binding => { + const result = await readQuotaSettlement(request(root, {...binding, pending_closeout: true, + turn_instance_id: null, todo_id: null})); + assert.equal(result.found, false); + }); + } finally {await rm(root, {recursive: true, force: true});} +}); + function sourceGuardPath(registryPath: string): string { const digest = createHash("sha256").update(goalId, "utf8").digest("hex"); return join( From e6c718f010dbfb3f58a76a56ad4a0403bc1ddfbc Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Wed, 11 Nov 2026 20:48:16 +0800 Subject: [PATCH 006/267] Wait for the conversation viewport and retry history images in place Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../smoke/bound-conversation-http-fixture.py | 33 +++++++++- .../conversation-reply-browser-smoke.mjs | 62 ++++++++++++++++++- .../personal-workspace/channel-timeline.tsx | 45 +++++++++++--- .../personal-workspace-page.tsx | 16 ++++- 4 files changed, 145 insertions(+), 11 deletions(-) diff --git a/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py b/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py index 8bbd6259c4..2e0cc2edc6 100644 --- a/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py +++ b/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py @@ -5,6 +5,7 @@ """ from __future__ import annotations +import base64 import json import os from pathlib import Path @@ -155,12 +156,42 @@ def start(self, server): if "--returned-files" in sys.argv: returned_files, reply_evidence = self.seed_returned_files(server, original) bindings.select_app_steward(binding["binding_id"], expected_revision=bindings.read()["revision"]) + history_images = {} + if "--history-images" in sys.argv: + def image(name, encoded): + return server.chat_store.append_message(sid, role="user", text=f"Original {name}", origin="lark", + attachments=[{"id": name, "name": name + ".png", "mime_type": "image/png", + "size": len(base64.b64decode(encoded)), "data_url": "data:image/png;base64," + encoded}]) + + old = image("archived-image", "iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAEElEQVR4nGNQnLIKjhhwcgA6oBB17nqaVAAAAABJRU5ErkJggg==") + for index in range(75): + server.chat_store.append_message(sid, role="user", text=f"Original archived message {index:02d}.") + current = image("latest-image", "iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAIAAAASFvFNAAAAEElEQVR4nGP4v0gdghjgLABwBAqxX1jWkgAAAABJRU5ErkJggg==") + server.chat_store.append_message(sid, role="agent", text="Latest original result after archived images.") + history_images = {"historyImages": {"old": old["message_id"], "current": current["message_id"]}} + original_image_handler = server.RequestHandlerClass._serve_chat_history_image + failed = False + failure_lock = threading.Lock() + + def fail_first_image_read(handler, parts): + nonlocal failed + with failure_lock: + fail = parts[3] == sid and parts[5] == old["message_id"] and not failed + failed |= fail + if fail: + handler._send_error("Fixture image temporarily unavailable", status=503) + return True + return original_image_handler(handler, parts) + + server.RequestHandlerClass._serve_chat_history_image = fail_first_image_read + journal = server.chat_store.sessions_root / sid / "messages.jsonl" + reply_evidence[journal] = journal.read_bytes() other, _ = runtime.open_session(goal_id=None, agent_id="codex", objective="", mode="new", work_dir=self.root / "project", project_ref=binding["project_ref"]) server.chat_store.append_message(other["session_id"], role="agent", text="Unrelated desktop conversation — must remain separate.") print(json.dumps({"origin": f"http://127.0.0.1:{server.server_port}", "bindingId": binding["binding_id"], "sessionId": sid, "ordinarySessionId": other["session_id"], - **returned_files}), flush=True) + **returned_files, **history_images}), flush=True) settings_handler = server.RequestHandlerClass._private_conversations history_handler = server.RequestHandlerClass._session_snapshot sessions_handler = server.RequestHandlerClass._list_sessions diff --git a/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs b/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs index a47c2ca3b9..de7635516d 100644 --- a/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs +++ b/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs @@ -11,10 +11,12 @@ import {launchBrowser, loadPlaywright} from "../../../../examples/dashboard-brow const repo = resolve(import.meta.dirname, "../../../.."); const returnedFiles = process.argv.includes("--returned-files"); +const historyImages = process.argv.includes("--history-images"); const output = resolve(process.env.LOOPX_REPLY_OUTPUT ?? resolve(repo, "output/playwright/conversation-reply")); await mkdir(output, {recursive: true}); const child = spawn(resolveTestPython({repoRoot: repo}), ["-u", - "apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py", returnedFiles ? "--returned-files" : "--reply-context"], + "apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py", returnedFiles ? "--returned-files" : "--reply-context", + ...(historyImages ? ["--history-images"] : [])], {cwd: repo, env: {...process.env, LOOPX_USAGE_PING: "0"}, stdio: ["pipe", "pipe", "pipe"]}); const exited = once(child, "exit"), queue = [], waiting = []; let stderr = "", browser; @@ -99,6 +101,60 @@ async function checkReturnedFiles(ready, page, errors, writes) { console.log(baseline ? "returned-files-browser: baseline reproduced (published files omitted from history and full reader; zero model Turns or repeat sends)" : "returned-files-browser: passed (real publisher, bound source, original history/reader, byte-exact downloads, desktop/narrow, keyboard, English, zero model Turns or repeat sends)"); } + +async function checkHistoryImages(ready, page, errors, writes) { + const {old, current} = ready.historyImages; + const requests = []; + const responses = []; + page.on("request", request => { + const url = new URL(request.url()); + if (url.origin === ready.origin && url.pathname.includes("/images/")) requests.push(url.pathname); + }); + page.on("response", response => { + const url = new URL(response.url()); + if (url.origin === ready.origin && url.pathname.includes(`/messages/${old}/images/`)) + responses.push({path: url.pathname, status: response.status()}); + }); + const requested = id => requests.filter(path => path.includes(`/messages/${id}/images/`)).length; + const timeline = page.locator(".personal-channel-timeline"); + const image = name => timeline.locator(`img[alt="${name}.png"]`); + await page.goto(ready.origin + "/chat/"); + await page.getByRole("navigation", {name: "管家视图"}).getByRole("button", {name: /^(Chat|对话)$/}).click(); + await timeline.getByText("Latest original result after archived images.", {exact: true}).waitFor(); + await page.waitForFunction(() => document.querySelector('img[alt="latest-image.png"]')?.naturalWidth === 3); + assert.equal(await image("archived-image").evaluate(node => node.naturalWidth), 0); + assert.equal(requested(old), 0, "Opening the latest conversation must not request the offscreen image"); + assert.equal(requested(current), 1); + await image("archived-image").scrollIntoViewIfNeeded(); + const oldMessage = timeline.locator(`article[data-message-id="${old}"]`); + const retry = oldMessage.getByRole("button", {name: "重试图片"}); + await retry.waitFor(); + assert.equal(requested(old), 1, "Scrolling requests only the selected original image"); + assert.deepEqual(responses.map(response => response.status), [503]); + await retry.click(); + await page.waitForFunction(() => document.querySelector('img[alt="archived-image.png"]')?.naturalWidth === 4); + assert.equal(requested(old), 2, "An inline retry reads the same original image without leaving the conversation"); + assert.deepEqual(responses.map(response => response.status), [503, 200]); + assert.equal(responses[0].path, responses[1].path, "Retry must preserve the original message and content digest"); + + requests.length = 0; + const focus = new URL(ready.origin + "/chat/"); + focus.search = new URLSearchParams({view: "conversation", conversationBinding: ready.bindingId, + conversationSession: ready.sessionId, messageSession: ready.sessionId, message: old}); + await page.goto(focus.href); + await page.waitForFunction(id => document.activeElement?.getAttribute("data-message-id") === id + && document.querySelector('img[alt="archived-image.png"]')?.naturalWidth === 4, old); + assert.equal(requested(old), 1, "Opening an original message link positions and loads that image"); + assert.equal(requested(current), 0, "An unrelated offscreen image stays deferred when reading a link"); + assert.deepEqual(errors, []); + assert.ok(!writes.some(path => /\/turns(?:\/|$)/u.test(path)), "Reading history cannot submit a Turn"); + child.stdin.write("inspect\n"); + const inspection = await next(); + assert.equal(inspection.model_turns, 0); + assert.equal(inspection.reply_evidence_unchanged, true); + console.log("history-images-browser: passed (latest, scroll, inline retry and original message link; zero model Turns)"); +} + try { const ready = await next(); browser = await launchBrowser(loadPlaywright().chromium); @@ -107,7 +163,9 @@ try { const errors = [], writes = []; page.on("pageerror", error => errors.push(error.message)); page.on("request", request => {if (request.method() !== "GET") writes.push(new URL(request.url()).pathname);}); - if (returnedFiles) { + if (historyImages) { + await checkHistoryImages(ready, page, errors, writes); + } else if (returnedFiles) { await checkReturnedFiles(ready, page, errors, writes); } else { const snapshot = await (await page.request.get(`${ready.origin}/api/chat/sessions/${ready.sessionId}`)).json(); diff --git a/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx b/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx index 41cf98d720..fc07bce952 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx @@ -2,7 +2,7 @@ import {DocumentAttachment, isDocumentAttachment} from "./document-attachment"; import { MessageActivity } from "./message-activity"; import { GoalDraftCard } from "./goal-draft-card"; import type { GoalDraft } from "../../../../../../loopx/control_plane/collaboration/goal_draft.js"; -import {Fragment, useLayoutEffect, useRef, useState} from "react"; +import {Fragment, useEffect, useLayoutEffect, useRef, useState} from "react"; import { CollaborationCards } from "./collaboration-card"; import {ConversationReplyContext} from "./conversation-requests"; import { Activity, Bot, Sparkles } from "lucide-react"; @@ -30,6 +30,28 @@ function answerLink(sessionId: string, messageId: string) { return url.toString(); } +function LinkedHistoryImage({name, source, ready, zh}: {name: string; source: string; ready: boolean; zh: boolean}) { + const ref = useRef(null); + const [imageRead, setImageRead] = useState<{source: string; failed?: boolean}>(); + const requested = imageRead?.source === source; + useEffect(() => { + const image = ref.current; + if (!image || !ready || requested) return; + const observer = new IntersectionObserver(([entry]) => { + if (!entry?.isIntersecting) return; + observer.disconnect(); + setImageRead({source}); + }, {root: image.closest(".personal-channel-scroll"), rootMargin: "64px 0px"}); + observer.observe(image); + return () => observer.disconnect(); + }, [ready, requested, source]); + if (requested && imageRead.failed) return
+
{name}{zh ? "图片无法显示" : "Could not load image"}
+ +
; + return {name} setImageRead(current => current?.source === source ? {source, failed: true} : current)}/>; +} export function ChannelTimeline({ emptyState, @@ -46,6 +68,7 @@ export function ChannelTimeline({ onCancelPreparation, messageFocus, historyPhase = "ready", + historyPositionReady = true, onReadMessage, onRetryHistory, queuedTurnKeys, @@ -53,6 +76,7 @@ export function ChannelTimeline({ emptyState?: { title: string; description: string }; messageFocus?: {sessionId: string; messageId: string}; historyPhase?: ConversationHistoryStatus["phase"]; + historyPositionReady?: boolean; onReadMessage?: () => void; onRetryHistory?: () => void; queuedTurnKeys?: ReadonlySet; @@ -90,23 +114,24 @@ export function ChannelTimeline({ && !item.message.pending && item.message.sourceSessionId && item.message.sourceMessageId).map(item => messageKey(item.message)))); } const focusTarget = useRef(null); - const restoredFocus = useRef(null); + const [restoredFocus, setRestoredFocus] = useState(null); const focusKey = messageFocus ? JSON.stringify([messageFocus.sessionId, messageFocus.messageId]) : null; const focusPresent = Boolean(messageFocus && items.some(item => item.kind === "message" && item.message.sourceSessionId === messageFocus.sessionId && item.message.sourceMessageId === messageFocus.messageId)); useLayoutEffect(() => { - if (!focusKey) {restoredFocus.current = null; return;} - if (!focusPresent || restoredFocus.current === focusKey || !focusTarget.current) return; + if (!focusKey) {if (restoredFocus !== null) setRestoredFocus(null); return;} + if (!focusPresent || restoredFocus === focusKey || !focusTarget.current) return; onReadMessage?.(); const frame = window.requestAnimationFrame(() => { if (!focusTarget.current) return; onReadMessage?.(); focusTarget.current.scrollIntoView({block: "start"}); focusTarget.current.focus({preventScroll: true}); - restoredFocus.current = focusKey; + setRestoredFocus(focusKey); }); return () => window.cancelAnimationFrame(frame); - }, [focusKey, focusPresent, onReadMessage]); + }, [focusKey, focusPresent, onReadMessage, restoredFocus]); + const loadHistoryImages = historyPositionReady && (!focusPresent || restoredFocus === focusKey); const focusNotice = messageFocus && !focusPresent ?
{historyPhase === "loading" ? (locale === "zh-CN" ? "正在定位原消息…" : "Finding the original message…") : historyPhase === "ready" ? (locale === "zh-CN" ? "原消息不在当前已读取的对话中。" : "The original message is not in the loaded conversation.") @@ -210,7 +235,13 @@ export function ChannelTimeline({
{item.message.role === "user" ? item.message.agentLabel ?? t("common.you") : item.message.agentLabel ?? t("header.manager")}{item.message.time ? : null}
{item.message.queueInputCancelled ? {locale === "zh-CN" ? "已取消排队" : "Cancelled before starting"} : null} - {item.message.attachments?.length ?
{item.message.attachments.map((attachment) => isDocumentAttachment(attachment.mimeType) ? : {attachment.name})}
: null} + {item.message.attachments?.length ?
{item.message.attachments.map((attachment) => { + if (isDocumentAttachment(attachment.mimeType)) return ; + const linked = attachment.dataUrl.startsWith("/api/chat/sessions/") && attachment.dataUrl.includes("/images/"); + return linked ? + : {attachment.name}; + })}
: null} {item.message.role === "user" ?

{item.message.text}

: item.message.text ? : null} diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx index b78a4fe3be..ee1f8d44fc 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx @@ -1376,10 +1376,15 @@ export function PersonalWorkspacePage({ [items, managerSessionProposalIds, managerChannelProposalIds], ); const conversationOpen = (!agentWorkActive || managedWorkActive) && (selectedGoal ? selectedGoalTab === "chat" : managerChatOpen); + const boundHistory = Boolean(originalConversation); + const historyPositionScope = JSON.stringify([selectionScope, conversationKey, selectedAgentId, + conversationMessageFocus?.sessionId, conversationMessageFocus?.messageId]); + const [positionedHistoryScope, setPositionedHistoryScope] = useState(null); const timelineReading = { queuedTurnKeys, messageFocus: conversationMessageFocus, historyPhase: conversationHistoryState?.phase, + historyPositionReady: !boundHistory || (conversationOpen && positionedHistoryScope === historyPositionScope), onRetryHistory: conversationHistoryState?.retry, onReadMessage: () => { followConversationRef.current = false; }, }; @@ -1410,9 +1415,18 @@ export function PersonalWorkspacePage({ readReturnRef.current = undefined; followConversationRef.current = !conversationMessageFocus; conversationScrollTopRef.current = channelScrollRef.current?.scrollTop ?? 0; + setPositionedHistoryScope(null); setShowLatestMessage(false); if (conversationOpen && !conversationMessageFocus) readReturnedAnswer(); - }, [conversationKey, selectedAgentId, conversationOpen, conversationMessageFocus?.sessionId, conversationMessageFocus?.messageId]); + }, [historyPositionScope, conversationOpen]); + useLayoutEffect(() => { + if (!boundHistory || !conversationOpen || !managerMessages.length || conversationHistoryState?.phase === "loading" + || positionedHistoryScope === historyPositionScope) return; + // Linked images have no src until the scroll anchor is in place. Native + // lazy loading can otherwise start fetching at the top before we move. + if (followConversationRef.current && !readReturnedAnswer()) scrollToLatestMessage(); + setPositionedHistoryScope(historyPositionScope); + }, [boundHistory, conversationOpen, managerMessages.length, conversationHistoryState?.phase, historyPositionScope, positionedHistoryScope]); useEffect(() => { if (!conversationOpen) return; let frame = 0; From 7d810cc069e760d7f7fcd4debb734ecf57207ad7 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Wed, 11 Nov 2026 22:33:51 +0800 Subject: [PATCH 007/267] Recover admitted replan settlement when refresh omits the Turn Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- docs/quota-allocation.md | 5 ++ .../quota/prior_turn_closeout.ts | 36 +++++++++------ .../quota/settlement_readback.ts | 4 +- .../test_effective_turn_replan.py | 46 +++++++++++++++---- .../quota_settlement_readback.test.ts | 38 +++++++++++++-- 5 files changed, 99 insertions(+), 30 deletions(-) diff --git a/docs/quota-allocation.md b/docs/quota-allocation.md index 6193c456e1..870ab8698f 100644 --- a/docs/quota-allocation.md +++ b/docs/quota-allocation.md @@ -347,6 +347,9 @@ retain the existing heartbeat default. A rejected `refresh-state`, or an Agent's state-only maintenance update, also returns `settlement_recovery` when a persisted Turn still owes closeout. Its identity and progress come from the same typed receipt reader as guard recovery. +If the caller omits the Turn ID, the refresh can discover an autonomous replan +whose guard positively admitted work and recorded the original obligation and +effect. Later Turn admission still follows the guard's explicit closeout marker. The TS `pending_closeout` read selects the candidate, validates the original binding and evaluates receipts under one admitted owner and immutable snapshot; Python only adapts CLI input and renders the returned recovery. This read mode @@ -363,6 +366,8 @@ Agents' Turns, and already settled work do not produce pending recovery. `refresh-state` 被拒绝,或 Agent 只更新升级/维护状态时,若已有 Turn 尚欠结算, 响应会返回 `settlement_recovery`,身份与进度沿用 guard 恢复的 typed receipt reader。 +即使调用者漏传 Turn ID,只要原 guard 已明确准入自主重规划并记录义务和 effect, +刷新也能找回原轮次。后续 Turn 的准入仍依据 guard 明确记录的 closeout 标记。 TS `pending_closeout` 在同一准入 owner、同一不可变快照下完成候选选择、原绑定校验 及收据判断;Python 只适配 CLI 参数和展示,不再分两次读取决定恢复对象。这个只读 模式不能与结算准入或最新 Run 推断混用。 diff --git a/loopx/control_plane/quota/prior_turn_closeout.ts b/loopx/control_plane/quota/prior_turn_closeout.ts index e468e2755e..723f548e78 100644 --- a/loopx/control_plane/quota/prior_turn_closeout.ts +++ b/loopx/control_plane/quota/prior_turn_closeout.ts @@ -12,6 +12,7 @@ import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts"; import { jsonObject } from "../runtime_decode.ts"; import { heartbeatReceiptBinding, + heartbeatReceiptDetails, heartbeatReceiptFactFromEvent, selectEffectiveHeartbeatReceipt, type HeartbeatReceiptBinding, @@ -45,8 +46,18 @@ function candidate( }; } +function admitsAutonomousReplan(event: JsonObject, fact: HeartbeatReceiptFact): boolean { + if (fact.replan_obligation_id === null || fact.settlement_effect_id === null) return false; + const guard = heartbeatReceiptDetails(event); + return guard.semantic_replan_obligation_id === fact.replan_obligation_id + && guard.interaction_mode === "autonomous_replan" && guard.ok === true && guard.should_run === true + && guard.must_attempt_work === true && guard.delivery_allowed === false && guard.quiet_noop_allowed === false; +} + /** - * Select the prior must-attempt Turns that still require a host closeout. + * Select prior Turns eligible for closeout readback. Guard preflight requires + * an explicit closeout marker. Refresh recovery can also discover an admitted + * replan-only binding without making it a later Turn's admission prerequisite. * * Every prior Turn is identity-validated, so a conflicting receipt fails the * read instead of being hidden behind a newer Turn. Results are newest first @@ -63,8 +74,9 @@ export function selectCloseoutCandidates( goalId: string, agentId: string, excludeTurnInstanceId: string | null, + { includeAdmittedReplans } = { includeAdmittedReplans: false }, ): { candidates: PriorHostTurnCloseoutCandidate[]; turnsValidated: number } { - const perTurn = new Map(); + const perTurn = new Map(); const firstRecorded = new Map(); for (const event of receipts) { const fact = heartbeatReceiptFactFromEvent(event); @@ -74,16 +86,17 @@ export function selectCloseoutCandidates( firstRecorded.set(turnInstanceId, firstRecorded.size); } if (turnInstanceId === excludeTurnInstanceId) continue; + const entry = { fact, value: event }; const existing = perTurn.get(turnInstanceId); if (existing) { - existing.push(fact); + existing.push(entry); // Map insertion order is the append-order recency index. Moving an // existing Turn to the tail avoids an O(T) indexOf/splice for every // receipt while preserving "last receipt wins recency" exactly. perTurn.delete(turnInstanceId); perTurn.set(turnInstanceId, existing); } else { - perTurn.set(turnInstanceId, [fact]); + perTurn.set(turnInstanceId, [entry]); } } @@ -92,22 +105,17 @@ export function selectCloseoutCandidates( ? undefined : firstRecorded.get(excludeTurnInstanceId); let validated = 0; for (const turnInstanceId of [...perTurn.keys()].reverse()) { - const entries = (perTurn.get(turnInstanceId) ?? []).map((fact) => ({ - fact, - value: fact, - })); - const effective = selectEffectiveHeartbeatReceipt(goalId, agentId, entries); + const effective = selectEffectiveHeartbeatReceipt(goalId, agentId, perTurn.get(turnInstanceId)!); validated += 1; if (effective === null) continue; // Validate identities even outside the causal candidate set: filtering // must not conceal a conflicting persisted settlement binding. if (currentPosition !== undefined && firstRecorded.get(turnInstanceId)! >= currentPosition) continue; - // Closeout is opt-in on the persisted receipt, so older receipts from - // before this contract cannot become new recovery obligations. - if (!effective.closeout_required) continue; - const binding = heartbeatReceiptBinding(goalId, agentId, effective); + const fact = heartbeatReceiptFactFromEvent(effective); + if (!fact.closeout_required && !(includeAdmittedReplans && admitsAutonomousReplan(effective, fact))) continue; + const binding = heartbeatReceiptBinding(goalId, agentId, fact); if (binding === null) continue; - candidates.push(candidate(binding, turnInstanceId, effective)); + candidates.push(candidate(binding, turnInstanceId, fact)); } return {candidates, turnsValidated: validated}; } diff --git a/loopx/control_plane/quota/settlement_readback.ts b/loopx/control_plane/quota/settlement_readback.ts index 87c8b617ce..0d155bf6d5 100644 --- a/loopx/control_plane/quota/settlement_readback.ts +++ b/loopx/control_plane/quota/settlement_readback.ts @@ -1110,6 +1110,7 @@ function owedPriorTurnFromSnapshot( snapshot: QuotaSettlementReadbackSnapshot, agentId: string, turnInstanceId: string | null, + options?: { includeAdmittedReplans: boolean }, ): OwedPriorTurn | null { const receipts = heartbeatReceiptsFromEvents(snapshot.events, request.goal_id, agentId) .filter((event) => quotaOwnerOwnsProjection(request.owner, event.goal_ref)); @@ -1118,6 +1119,7 @@ function owedPriorTurnFromSnapshot( request.goal_id, agentId, turnInstanceId, + options, ); for (const selected of candidates) { // A committed monitor poll is its own exact no-spend closeout. This is the @@ -1167,7 +1169,7 @@ function readPendingCloseoutFromRequest( } const notFound = {schema_version: QUOTA_SETTLEMENT_READBACK_RESULT_SCHEMA, found: false}; const turn = request.turn_instance_id ?? owedPriorTurnFromSnapshot( - request, snapshot, explicitAgentId, null, + request, snapshot, explicitAgentId, null, { includeAdmittedReplans: true }, )?.prior_turn_instance_id; if (!turn) return notFound; // Selection, original binding validation and receipt progress share this diff --git a/tests/control_plane/test_effective_turn_replan.py b/tests/control_plane/test_effective_turn_replan.py index f1446f733a..0e449180e8 100644 --- a/tests/control_plane/test_effective_turn_replan.py +++ b/tests/control_plane/test_effective_turn_replan.py @@ -20,7 +20,10 @@ GOAL_ID, TODO_ID, _configure_selectable_alternative, + _heartbeat_receipt_count, _initialize_fixture_authority, + _run_generated_cli, + _spend_run_count, ALTERNATIVE_TODO_ID, ) from canonical_authority_fixture import isolate_sqlite_runtime @@ -407,6 +410,29 @@ def call(*args: str, expected_code: int = 0) -> dict: assert reread["autonomous_replan_obligation"].get("resolution_mode") != "receipt_bound_replan_settlement" assert reread["replan_action_packet"]["stride_control"]["required_on_periodic_review"] is True assert reread["replan_action_packet"]["stride_control"]["decision_value_review"] == value_review + replan_identity = reread["heartbeat_receipt"]["settlement_identity"] + receipt_count = _heartbeat_receipt_count(runtime, "stride-due-review") + spend_count = _spend_run_count(runtime) + refresh = ("refresh-state", "--goal-id", GOAL_ID, "--agent-id", AGENT_ID, + "--no-global-sync", "--suppress-external-sinks") + maintenance = (*refresh, "--classification", "local_upgrade_status_updated", + "--delivery-outcome", "surface_only") + missing_turn = call(*refresh, "--replan-obligation-id", obligation["obligation_id"], + "--classification", "bounded_replan_progress", "--delivery-outcome", "outcome_progress", + expected_code=1) + assert "turn_instance_id" in missing_turn["error"] + surface = call(*maintenance, expected_code=1) + assert not surface["appended"] and "settlement_identity" not in surface + assert surface["replan_transition"]["reason_code"] == "stride_decision_required" + assert "settlement_recovery" in missing_turn, missing_turn + assert "settlement_recovery" in surface, surface + pending = missing_turn["settlement_recovery"] + assert pending == surface["settlement_recovery"] + assert surface["replan_transition"]["next_cli_actions"] == pending["next_cli_actions"] + assert pending["settlement_identity"] == replan_identity + assert pending["settlement_progress"]["next_step"] == "durable_writeback" + assert _heartbeat_receipt_count(runtime, "stride-due-review") == receipt_count + assert _spend_run_count(runtime) == spend_count rejected = call("refresh-state", "--goal-id", GOAL_ID, "--agent-id", AGENT_ID, "--replan-obligation-id", obligation["obligation_id"], "--turn-instance-id", "stride-due-review", @@ -461,9 +487,6 @@ def call(*args: str, expected_code: int = 0) -> dict: state_path.write_text(state_path.read_text().replace( "Settle one standard Codex App delivery.", "Settle one standard delivery and verify the newly required source.")) - from tests.control_plane.test_quota_settlement_cli import _heartbeat_receipt_count, _spend_run_count - receipt_count = _heartbeat_receipt_count(runtime, "stride-due-review") - spend_count = _spend_run_count(runtime) rejected_reference = decision["reference_context_id"] if changed_context else "replan-context-" + "b" * 24 decision_path.write_text(json.dumps({**vision_packet, "stride_decision": {**decision, "reference_context_id": rejected_reference}})) @@ -480,9 +503,8 @@ def call(*args: str, expected_code: int = 0) -> dict: assert transition["host_action"] == "read_current_replan_context" assert transition["next_cli_actions"] == [] # No unbound repeat of the rejected write. owed_recovery = foreign["settlement_recovery"] - assert owed_recovery["settlement_identity"] == reread["heartbeat_receipt"]["settlement_identity"] - from tests.control_plane.test_quota_settlement_cli import _run_generated_cli - recovered_code, recovered_turn = _run_generated_cli(owed_recovery["next_cli_actions"][0], + assert owed_recovery == pending + recovered_code, recovered_turn = _run_generated_cli(pending["next_cli_actions"][0], registry_path=registry, cwd=project) assert recovered_code == 0, json.dumps(recovered_turn, indent=2) assert recovered_turn["heartbeat_receipt"]["settlement_identity"] == owed_recovery["settlement_identity"] @@ -534,13 +556,17 @@ def call(*args: str, expected_code: int = 0) -> dict: assert persisted["agent_vision"]["stride_decision"] == decision assert persisted["settlement_identity"]["schema_version"] == "quota_settlement_identity_v1" assert persisted["replan_obligation_id"] == obligation["obligation_id"] - assert persisted["settlement_identity"] == reread["heartbeat_receipt"]["settlement_identity"] + assert persisted["settlement_identity"] == replan_identity + after_write = call(*maintenance)["settlement_recovery"] + spend = after_write["settlement_owed"] + assert after_write["next_cli_actions"] == [spend["command"]] + assert spend["effect_id"] == replan_identity["effect_id"] for _ in range(2): - call("quota", "spend-slot", "--goal-id", GOAL_ID, "--agent-id", AGENT_ID, - "--replan-obligation-id", obligation["obligation_id"], "--turn-instance-id", "stride-due-review", - "--slots", "1", "--source", "heartbeat", "--execute") + rc, spent = _run_generated_cli(spend["command"], registry_path=registry, cwd=project) + assert rc == 0 and spent["settlement_result"]["ok"], spent assert _spend_run_count(runtime) == spend_count + 1 assert _heartbeat_receipt_count(runtime, "stride-due-review") == receipt_count + assert "settlement_recovery" not in call(*maintenance) # Continue through real authority/history, rather than injecting a crafted # context. The next periodic review must see the accepted autonomous decision. diff --git a/tests/control_plane_ts/quota_settlement_readback.test.ts b/tests/control_plane_ts/quota_settlement_readback.test.ts index 32e74d95ec..59c8ebcb39 100644 --- a/tests/control_plane_ts/quota_settlement_readback.test.ts +++ b/tests/control_plane_ts/quota_settlement_readback.test.ts @@ -503,7 +503,7 @@ test("pending closeout survives maintenance and reads one original receipt snaps }); }); -test("pending closeout keeps replan identity and rejects query/admission mixing", async () => { +test("pending closeout recovers positively admitted replan work without creating a preflight obligation", async () => { const root = await fixture(); try { const log = join(root, "goals", goalId, "rollout-event-log.jsonl"); @@ -511,11 +511,39 @@ test("pending closeout keeps replan identity and rejects query/admission mixing" const obligation = "replan-0000000000000001"; const bound = settlementIdentity({goal_id: goalId, agent_id: agentId, todo_id: null, replan_obligation_id: obligation, turn_instance_id: turnId}); - events[0].details = {closeout_required: true, replan_obligation_id: obligation, - semantic_replan_obligation_id: obligation, settlement_effect_id: bound.effect_id}; - await writeFile(log, events.map(event => JSON.stringify(event)).join("\n") + "\n"); + const admitted = {closeout_required: false, replan_obligation_id: obligation, + semantic_replan_obligation_id: obligation, settlement_effect_id: bound.effect_id, + ok: true, should_run: true, must_attempt_work: true, delivery_allowed: false, + quiet_noop_allowed: false, interaction_mode: "autonomous_replan"}; + const writeGuard = async (details: Record) => { + events[0].details = details; + await writeFile(log, events.map(event => JSON.stringify(event)).join("\n") + "\n"); + }; + await writeGuard(admitted); const input = request(root, {pending_closeout: true, turn_instance_id: null, todo_id: null}); - assert.deepEqual(((await readQuotaSettlement(input)).identity as any).result.value, settlementIdentityPayload(bound)); + for (const query of [input, {...input, replan_obligation_id: obligation}]) { + const readback = await readQuotaSettlement(query); + assert.equal(readback.found, true); + assert.deepEqual((readback.identity as any).result.value, settlementIdentityPayload(bound)); + } + assert.equal((await readQuotaSettlement({...input, agent_id: "another-agent"})).found, false); + const mismatched = await readQuotaSettlement({...input, replan_obligation_id: "replan-0000000000000002"}); + assert.equal((mismatched.identity as any).result.failure.kind, "identity_mismatch"); + const preflight = await preflightPriorHostTurnCloseout({ + ...input, schema_version: PRIOR_HOST_TURN_CLOSEOUT_PREFLIGHT_REQUEST_SCHEMA, + }); + assert.equal(preflight.reason, "no_prior_turn_requires_closeout"); + + for (const patch of [{should_run: false}, {must_attempt_work: undefined}, {quiet_noop_allowed: true}, + {interaction_mode: "control_plane_self_repair"}, {semantic_replan_obligation_id: "replan-0000000000000002"}, + {settlement_effect_id: undefined}, {replan_obligation_id: undefined, settlement_effect_id: undefined}]) { + await writeGuard({...admitted, ...patch}); + assert.equal((await readQuotaSettlement(input)).found, false, JSON.stringify(patch)); + } + await writeGuard({...admitted, settlement_effect_id: "another-effect"}); + const wrongEffect = await readQuotaSettlement(input); + assert.equal((wrongEffect.identity as any).result.failure.kind, "identity_mismatch"); + for (const patch of [{infer_turn_instance_id: true}, {allow_unbound_binding: true}, {refresh_retry: {}}, {heartbeat_reentry_guard: {}}, {spend_precheck: true}]) { await assert.rejects(readQuotaSettlement({...input, ...patch}), /read-only receipt recovery/); From 6929a2447080be10ba33496f67fa2cf5a7040312 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:36:18 +0800 Subject: [PATCH 008/267] fix(benchmark): identify missing checkpoint components Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- benchmark/README.md | 7 ++++++ benchmark/edgebench/checkpoint_supervisor.py | 6 +++-- benchmark/edgebench/checkpoint_worker.py | 25 +++++++++++++++++-- benchmark/edgebench/terminal_capture.py | 4 ++- benchmark/runtime/checkpoint_handoff.py | 8 +++++- .../tests/test_checkpoint_diagnostics.py | 10 +++++++- benchmark/tests/test_checkpoint_handoff.py | 13 ++++++++-- benchmark/tests/test_checkpoint_worker.py | 11 +++++++- benchmark/tests/test_terminal_preservation.py | 6 +++++ 9 files changed, 80 insertions(+), 10 deletions(-) diff --git a/benchmark/README.md b/benchmark/README.md index 8d6d26895f..fdd4141cda 100644 --- a/benchmark/README.md +++ b/benchmark/README.md @@ -823,6 +823,10 @@ and `runtime_failure.diagnostic_code`, without copying the error message or raw provider response into the worker reply. An expired artifact capture window is `request_rejected` / `checkpoint_capture_window_closed`. These diagnostics do not retry the capture, extend its deadline or grant execution authority. +When Docker cannot find a declared component during export, the supervisor and +terminal receipts include its declared `missing_component_id`. The worker reports +that ID when it matches the original request. The capture still fails and the +existing source-retention rules apply. This explicit command produces an **inert joint file snapshot**, still `research_point_qualified=false`. It does not snapshot process memory, freeze @@ -1149,6 +1153,9 @@ worker 保留原 guard,controller 暂停全部容器写入者,核对仍有 文件 witness,再封存原生评测机会、incumbent 及剩余预算。源变化、过期或失去 交接会拒绝;可恢复的捕获失败不取消原研究,也不得改用稍后的状态。解除暂停后, 源会话继续使用原 packet 和 deadline。 +Docker 导出找不到某个已声明组件时,supervisor 和终态回执会记录其 +`missing_component_id`。worker 在确认 ID 与原请求匹配后报告该 ID。 +捕获仍失败,来源仍按原规则保留。 原生交接先接收并读取一条有界请求,再校验捕获根目录的物理路径。目录缺失或为 符号链接时关闭该连接,原来源可纠正路径后经同一监听器继续;拒绝不捕获、不释放 旧 packet,不延长原 wake 截止时间,也不创建或跟随外来根目录。 diff --git a/benchmark/edgebench/checkpoint_supervisor.py b/benchmark/edgebench/checkpoint_supervisor.py index 3bbe88134e..27f0b1a672 100644 --- a/benchmark/edgebench/checkpoint_supervisor.py +++ b/benchmark/edgebench/checkpoint_supervisor.py @@ -15,7 +15,9 @@ from benchmark.runtime.layout import _ACTIVE_PHASE_DEADLINE, _PHASE_DEADLINE from .checkpoint_controller import _controller -from .checkpoint_worker import _archive, _guest_path, _validate_request, capture_worker, qualify_export_storage +from .checkpoint_worker import ( + _archive, _guest_path, _validate_request, capture_failure_fields, capture_worker, qualify_export_storage, +) class CheckpointSupervisor: @@ -202,7 +204,7 @@ def _run(self, container): except Exception as error: # Optional capture failure resumes the original source; # credentials and raw provider errors never enter it. - response["error_kind"] = type(error).__name__ + response.update(capture_failure_fields(error)) if isinstance(error, EffectRuntimeRemoteError): response["runtime_failure"] = { "error_kind": error.error_kind, diff --git a/benchmark/edgebench/checkpoint_worker.py b/benchmark/edgebench/checkpoint_worker.py index 927f0d74d6..92d6a4ea79 100644 --- a/benchmark/edgebench/checkpoint_worker.py +++ b/benchmark/edgebench/checkpoint_worker.py @@ -26,6 +26,21 @@ from .checkpoint_controller import _controller, _physical, _remaining, _phase_budget, capture +_COMPONENT_ID = re.compile(r"[A-Za-z][A-Za-z0-9_.-]{0,63}") + + +def capture_failure_fields(error: Exception) -> dict[str, str]: + """Keep a bounded diagnostic for a declared component's failed Docker read.""" + fields = {"error_kind": type(error).__name__} + component_id = getattr(error, "checkpoint_component_id", None) + if isinstance(component_id, str) and _COMPONENT_ID.fullmatch(component_id): + from docker.errors import NotFound + + if isinstance(error, NotFound): + fields["missing_component_id"] = component_id + return fields + + def qualify_export_storage(destination: Path) -> None: """Check filename fidelity before registration, without reserving a run. @@ -72,7 +87,7 @@ def _validate_request(value: dict) -> dict: for entry in value["components"]: if (not isinstance(entry, dict) or set(entry) != {"id", "source_path"} or not isinstance(entry["id"], str) or - not re.fullmatch(r"[A-Za-z][A-Za-z0-9_.-]{0,63}", entry["id"]) or entry["id"] in ids): + not _COMPONENT_ID.fullmatch(entry["id"]) or entry["id"] in ids): raise ValueError("Worker components require unique IDs") ids.add(entry["id"]) paths.append(_guest_path(entry["source_path"])) @@ -334,7 +349,13 @@ def capture_worker(controller_file: Path, *, token: str, capture_id: str, deadline = min(deadline, time.monotonic() + prepared["deadline_epoch"] - time.time()) for entry in request["components"]: client.api.timeout = _remaining(deadline) - copied = _archive_component(container, entry["source_path"], stage / "workers" / entry["id"], deadline=deadline) + try: + copied = _archive_component( + container, entry["source_path"], stage / "workers" / entry["id"], deadline=deadline) + except docker.errors.NotFound as error: + # Keep the provider exception for direct capture callers. + error.checkpoint_component_id = entry["id"] + raise components.append({"id": entry["id"], "source_path": str(copied)}) if prepared is not None: observed = effect_runtime_result("runtime.checkpoint_artifacts", { diff --git a/benchmark/edgebench/terminal_capture.py b/benchmark/edgebench/terminal_capture.py index 320775df5b..c8ff3a6a1d 100644 --- a/benchmark/edgebench/terminal_capture.py +++ b/benchmark/edgebench/terminal_capture.py @@ -150,7 +150,9 @@ def finish(self, execution): self.receipt.update(capture_succeeded=True, capture=point) except Exception as error: # No remote message, output, account information or token is retained. - self.receipt["error_kind"] = type(error).__name__ + from .checkpoint_worker import capture_failure_fields + + self.receipt.update(capture_failure_fields(error)) finally: # Native final-archive extraction and grading drain occur before its # cleanup callback. Stop here so orphaned work cannot run through diff --git a/benchmark/runtime/checkpoint_handoff.py b/benchmark/runtime/checkpoint_handoff.py index 19a77dc6a0..c8afb53253 100644 --- a/benchmark/runtime/checkpoint_handoff.py +++ b/benchmark/runtime/checkpoint_handoff.py @@ -77,7 +77,13 @@ def await_controller_capture(root, components, metadata, probe, deadline, *, pha if result.get("request_sha256") != hashlib.sha256(raw).hexdigest(): raise ValueError("checkpoint controller reply belongs to another boundary") if result.get("ok") is not True: - raise ValueError("controller checkpoint failed: " + str(result.get("error_kind"))) + error_kind = result.get("error_kind") + message = f"controller checkpoint failed: {error_kind}" + missing = result.get("missing_component_id") + if (error_kind == "NotFound" and isinstance(missing, str) and + any(entry["id"] == missing for entry in components)): + message += f" (missing component {missing})" + raise ValueError(message) return result["capture"] time.sleep(.05) raise TimeoutError("controller did not publish a sealed checkpoint before the completion deadline") diff --git a/benchmark/tests/test_checkpoint_diagnostics.py b/benchmark/tests/test_checkpoint_diagnostics.py index 2cd5c92bdf..b76259e08b 100644 --- a/benchmark/tests/test_checkpoint_diagnostics.py +++ b/benchmark/tests/test_checkpoint_diagnostics.py @@ -36,6 +36,7 @@ def isolated_runtime(tmp_path_factory): @pytest.mark.parametrize("fault,exception_kind,runtime_kind,code", [ ("expired", "EffectRuntimeRejected", "request_rejected", "checkpoint_capture_window_closed"), ("missing", "EffectRuntimePermanentIOError", "io_permanent", "io_not_found"), + ("archive_missing", "NotFound", None, None), ("directory", "EffectRuntimePermanentIOError", "io_permanent", "io_is_directory"), ("unexpected", "EffectRuntimeInternalError", "internal_failure", "unexpected_handler_error"), ("untyped", "ValueError", None, None), @@ -118,6 +119,9 @@ def held_archive(*args, **kwargs): def source_archive(*args, **kwargs): assert worker.attrs["State"]["Paused"] + if fault == "archive_missing": + events.append("archive_missing") + raise docker.errors.NotFound("provider body " + token + " " + str(tmp_path)) return candidate monkeypatch.setattr(worker_module, "_archive", held_archive) @@ -143,6 +147,7 @@ def failed_probe(method, request, **kwargs): error = ValueError("provider body " + token) # Attributes alone must not make an arbitrary provider exception trusted. error.error_kind, error.diagnostic_code = admin, token + error.checkpoint_component_id = "code" raise error if fault == "expired": request = {**request, "deadline_epoch_ms": 0} @@ -174,7 +179,8 @@ def failed_probe(method, request, **kwargs): assert supervisor.failure is None assert storage_probes == [points, points / nonce] assert worker.attrs["State"] == {"Running": True, "Paused": False} - assert events == ["pause", "probe", "unpause", "client_close", "reply_transfer", "reply_publish"] + failure_step = "archive_missing" if fault == "archive_missing" else "probe" + assert events == ["pause", failure_step, "unpause", "client_close", "reply_transfer", "reply_publish"] assert candidate.read_bytes() == b"original source bytes\n" assert not (points / nonce).exists() and not (bank / nonce).exists() assert (bank / ".loopx-worker-attempts" / nonce).is_dir() @@ -185,6 +191,8 @@ def failed_probe(method, request, **kwargs): "ok": False, "error_kind": exception_kind} if runtime_kind is not None: expected["runtime_failure"] = {"error_kind": runtime_kind, "diagnostic_code": code} + if fault == "archive_missing": + expected["missing_component_id"] = "code" assert receipt == expected for private in (token, admin, str(tmp_path), "provider body", "invalid JSON"): assert private not in json.dumps(receipt) diff --git a/benchmark/tests/test_checkpoint_handoff.py b/benchmark/tests/test_checkpoint_handoff.py index e395d34bc6..de6a4cf42d 100644 --- a/benchmark/tests/test_checkpoint_handoff.py +++ b/benchmark/tests/test_checkpoint_handoff.py @@ -124,7 +124,8 @@ def test_completed_attempt_survives_artifact_index_permission_recovery(tmp_path, assert not list(index.iterdir()) and witness.read_bytes() == original -@pytest.mark.parametrize("response", ["captured", "failed", "foreign", "timeout"]) +@pytest.mark.parametrize("response", ["captured", "failed", "missing_component", + "foreign_component", "foreign", "timeout"]) def test_original_request_only_and_cleanup_after_missed_point(tmp_path, response): mailbox = tmp_path / "checkpoint-controller" ready = mailbox / "ready.json" @@ -141,6 +142,9 @@ def controller(): result = {"request_sha256": hashlib.sha256(raw).hexdigest(), "ok": response == "captured", "error_kind": "SyntheticFailure", "capture": {"research_point_qualified": False, "sha256": "retained"}} + if response in {"missing_component", "foreign_component"}: + result["error_kind"] = "NotFound" + result["missing_component_id"] = "code" if response == "missing_component" else "other" if response == "foreign": result["request_sha256"] = "another-point" pending = mailbox / "reply.tmp" @@ -154,8 +158,13 @@ def controller(): if response == "captured": assert await_controller_capture(*args)["sha256"] == "retained" else: - with pytest.raises(TimeoutError if response == "timeout" else ValueError): + with pytest.raises(TimeoutError if response == "timeout" else ValueError) as error: await_controller_capture(*args, phase_deadline=args[-1]) + if response in {"missing_component", "foreign_component"}: + expected = "controller checkpoint failed: NotFound" + if response == "missing_component": + expected += " (missing component code)" + assert str(error.value) == expected finally: thread.join(3) assert not thread.is_alive() and not ready.exists() and len(observed) == 1 diff --git a/benchmark/tests/test_checkpoint_worker.py b/benchmark/tests/test_checkpoint_worker.py index 2d540c1855..55bef508d5 100644 --- a/benchmark/tests/test_checkpoint_worker.py +++ b/benchmark/tests/test_checkpoint_worker.py @@ -114,7 +114,7 @@ def test_archive_keeps_private_directory_modes_without_special_bits(writer, tmp_ assert (copied / "child").stat().st_mode & 0o7777 == 0o700 -@pytest.mark.parametrize("fault", [None, "source_moved", "expired", "read_race", "late_native", +@pytest.mark.parametrize("fault", [None, "source_moved", "source_missing", "expired", "read_race", "late_native", "retention", "retention_default", "retention_off", "retention_pin", "retention_failure", "continued_clock"]) def test_supervisor_captures_only_the_prepared_worker_boundary(controllers, writer, tmp_path, monkeypatch, fault): from loopx.control_plane.effect_runtime import effect_runtime_result @@ -145,6 +145,8 @@ def test_supervisor_captures_only_the_prepared_worker_boundary(controllers, writ "deadline_epoch": time.time() + (-1 if fault == "expired" else 8 if fault == "late_native" else 30)} if fault == "source_moved": assert container.exec_run(["python", "-c", "from pathlib import Path; Path('/candidate').write_text('later')"]).exit_code == 0 + if fault == "source_missing": + assert container.exec_run(["rm", "/candidate"]).exit_code == 0 assert container.exec_run(["python", "-c", "from pathlib import Path; " "Path('/logs/agent/checkpoint-controller/ready.json').write_text(" + repr(json.dumps(request)) + ")"]).exit_code == 0 if fault == "read_race": @@ -232,6 +234,13 @@ def with_old_point(*args, **kwargs): assert (tmp_path / "points/retention-error.json").exists() else: assert not unit.exists() + if fault == "source_missing": + reply = container.exec_run(["cat", "/logs/agent/checkpoint-controller/" + nonce + ".json"]) + assert reply.exit_code == 0 + assert json.loads(reply.output) == response + assert response["error_kind"] == "NotFound" + assert response["missing_component_id"] == "code" + assert b"/candidate" not in reply.output if fault != "late_native": assert not (Path(json.loads(files[0].read_bytes())["bank_root"]) / nonce).exists() container.reload() diff --git a/benchmark/tests/test_terminal_preservation.py b/benchmark/tests/test_terminal_preservation.py index 3558631d1e..638f3e7e3d 100644 --- a/benchmark/tests/test_terminal_preservation.py +++ b/benchmark/tests/test_terminal_preservation.py @@ -326,6 +326,12 @@ def registered(token): assert receipt["source_retained"] and receipt["source_stopped"] assert not receipt["restore_qualified"] assert receipt["capture_succeeded"] is not capture_failure + if capture_failure: + assert receipt["error_kind"] == "NotFound" + assert receipt["missing_component_id"] == "missing" + assert "/missing-participant" not in json.dumps(receipt) + else: + assert "missing_component_id" not in receipt assert len(grades) == 1 # Saving the tail did not create an evaluation opportunity. if not capture_failure: point = supervisor.destination / receipt["capture_id"] From 5ce63978b4cff5b6b75982bbcdc4301155b71e87 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Thu, 12 Nov 2026 04:35:19 +0800 Subject: [PATCH 009/267] Keep checkpoint capture on the admitted continuation clock Periodic and terminal checkpoint capture must read the same phase clock after scheduler continuation. Accept the decimal epoch published by the scheduler without rounding it or changing the receiver budget; preserve the legacy integer phase file and reject invalid clocks before native capture. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- benchmark/edgebench/checkpoint_controller.py | 10 +++--- benchmark/edgebench/terminal_capture.py | 4 +-- benchmark/runtime/scheduler.py | 12 +++++++ benchmark/tests/test_checkpoint_controller.py | 35 +++++++++++++------ benchmark/tests/test_checkpoint_worker.py | 9 +++-- .../tests/test_scheduler_phase_resume.py | 17 ++++++--- 6 files changed, 62 insertions(+), 25 deletions(-) diff --git a/benchmark/edgebench/checkpoint_controller.py b/benchmark/edgebench/checkpoint_controller.py index 859a59c7b2..a842f1882f 100644 --- a/benchmark/edgebench/checkpoint_controller.py +++ b/benchmark/edgebench/checkpoint_controller.py @@ -24,6 +24,7 @@ from urllib.request import HTTPRedirectHandler, ProxyHandler, Request, build_opener from loopx.control_plane.effect_runtime import effect_runtime_result +from benchmark.runtime.scheduler import read_phase_deadline from benchmark.runtime.worker import PHASE_WORK_RESERVE_SECONDS @@ -108,17 +109,14 @@ def _bound_bank(folder: Path, reference: dict) -> dict: def _phase_budget(raw: bytes, native: dict) -> dict: - """Adapt the existing SForge integer phase clock, not quota authority. + """Preserve the admitted Host phase clock without granting new authority. Native capture owns this time. The declared physical Host file owns its deadline. Neither fact qualifies complete Host/research-state capture. """ - if len(raw) > 64 or not re.fullmatch(rb"[0-9]+\n?", raw): - raise ValueError("Host phase deadline must be an integer epoch file") - epoch = int(raw) + epoch = read_phase_deadline(raw) captured = native.get("captured_at_epoch") - if (not 0 < epoch < 2 ** 53 or type(captured) not in (int, float) or - not math.isfinite(captured) or captured <= 0): + if type(captured) not in (int, float) or not math.isfinite(captured) or captured <= 0: raise ValueError("Host/native phase clocks cannot be represented exactly") return {"component_id": "host_phase_deadline", "source_deadline_epoch": epoch, "captured_at_epoch": captured, "remaining_seconds": max(0, epoch - captured)} diff --git a/benchmark/edgebench/terminal_capture.py b/benchmark/edgebench/terminal_capture.py index 320775df5b..3d9ed03914 100644 --- a/benchmark/edgebench/terminal_capture.py +++ b/benchmark/edgebench/terminal_capture.py @@ -53,7 +53,7 @@ def _scheduler_components_at_barrier(container, components, stage, *, deadline): import re import docker from benchmark.runtime.layout import _ACTIVE_PHASE_DEADLINE - from benchmark.runtime.scheduler import phase_state_name + from benchmark.runtime.scheduler import phase_state_name, read_phase_deadline from .checkpoint_worker import _archive root = PurePosixPath(_CONTROL, "scheduler-state.json") @@ -63,7 +63,7 @@ def _scheduler_components_at_barrier(container, components, stage, *, deadline): clock = _archive(container, _ACTIVE_PHASE_DEADLINE, stage / "active-clock", deadline=deadline) except docker.errors.NotFound: return components # A fresh worker has no admitted continuation. - current = root.with_name(phase_state_name(root, float(clock.read_bytes()))) + current = root.with_name(phase_state_name(root, read_phase_deadline(clock.read_bytes()))) chain = [] while current != root: if (current in chain or current.parent != root.parent diff --git a/benchmark/runtime/scheduler.py b/benchmark/runtime/scheduler.py index 0723d449e3..7a39919f2d 100644 --- a/benchmark/runtime/scheduler.py +++ b/benchmark/runtime/scheduler.py @@ -23,6 +23,18 @@ def phase_work_reserve(mode: str) -> int: return 0 if mode == "heartbeat" else PHASE_WORK_RESERVE_SECONDS +def read_phase_deadline(raw: bytes) -> int | float: + """Read the initial integer epoch or the scheduler's continued phase clock.""" + if len(raw) > 64 or not re.fullmatch(rb"[0-9]+(?:\.[0-9]+)?\n?", raw): + raise ValueError("Host phase deadline must be an integer or canonical decimal epoch file") + body = raw.removesuffix(b"\n") + deadline = float(body) if b"." in body else int(body) + if not 0 < deadline < 2 ** 53 or ( + isinstance(deadline, float) and body != repr(deadline).encode("ascii")): + raise ValueError("Host phase deadline cannot be represented exactly") + return deadline + + def phase_state_name(root: PurePath, phase_deadline: float) -> str: """Name a scheduler phase by the finite clock that owns its allowance.""" if (isinstance(phase_deadline, bool) or not isinstance(phase_deadline, (int, float)) diff --git a/benchmark/tests/test_checkpoint_controller.py b/benchmark/tests/test_checkpoint_controller.py index 3f99714d96..cf016eae40 100644 --- a/benchmark/tests/test_checkpoint_controller.py +++ b/benchmark/tests/test_checkpoint_controller.py @@ -23,6 +23,12 @@ from benchmark.edgebench.source import active_source_receipt +@pytest.mark.parametrize("clock", [b"nan\n", b"9007199254740992.0\n", b"2000.25000000000000001\n"]) +def test_phase_budget_rejects_invalid_or_rounded_clock(clock): + with pytest.raises(ValueError, match="Host phase deadline"): + transport._phase_budget(clock, {"captured_at_epoch": 1000}) + + @contextmanager def server(app): import uvicorn @@ -267,7 +273,7 @@ def reject_non_json_constant(value): def host_phase_command(tmp_path, monkeypatch): - """Real command builder; only installation/inference are outside this oracle.""" + """Real phase admission shell, isolated from installation and model access.""" pytest.importorskip("harbor") from sforge.harness.config import SForgeConfig from benchmark.runtime.sforge import BenchmarkCodex, SForgeWorker @@ -275,12 +281,17 @@ def host_phase_command(tmp_path, monkeypatch): async def installed(self, environment): pass monkeypatch.setattr(BenchmarkCodex, "install", installed) - monkeypatch.setenv("CODEX_AUTH_JSON_PATH", "/synthetic-not-read") logs = tmp_path / "phase-worker" logs.mkdir() - worker = SForgeWorker(SForgeConfig(agent_model="fixture", agent_effort="xhigh"), + # The model-free prefix must not try to inspect a desktop login. The + # custom endpoint uses the same phase shell and is never contacted. + config = SForgeConfig(agent_model="fixture", agent_effort="xhigh", + agent_api_base_url="http://127.0.0.1:1/v1", agent_api_key="fixture-not-used") + backend = Mock() + backend.exec_run.return_value = SimpleNamespace(exit_code=0, output=config.agent_api_base_url) + worker = SForgeWorker(config, profile="heartbeat-resume", cwd="/task", timeout_seconds=43200, task_entry="seeded-todo") - worker.install_stop_hook(None, None, logs, None) + worker.install_stop_hook(backend, None, logs, None) worker.prepared = True # The provider's own phase-file/admission shell executes. Its following # model command is excluded explicitly; candidate writing below is synthetic. @@ -295,17 +306,20 @@ def test_host_phase_and_native_opportunities_continue_without_offline_refill( token = register(source, first, max_agent_submissions=3, submission_cooldown=2) assert submit(first, token, 7).status_code == 200 before = copy.deepcopy((source.state.judge.tokens, source.state.judge.submissions, source.state.judge.run_history)) - # An already admitted Host has 2h left from its original 12h configured - # trial. Fixture seeding represents that prior elapsed research, not a new run. + # An admitted continuation retains about 2h from its original 12h trial. + # The scheduler publishes a decimal deadline for this phase. source_phase = tmp_path / "source-phase" - source_phase.write_text(f"{int(time.time()) + 7200}\n") + source_deadline = int(time.time()) + 7200.25 + source_phase.write_text(f"{source_deadline!r}\n") source_bytes = source_phase.read_bytes() unit = tmp_path / "unit" point = transport.capture(files[0], token=token, capture_id="c" * 32, destination=unit, phase_deadline_file=source_phase) body = json.loads((unit / "checkpoint.json").read_bytes()) - retained = body["point_metadata"]["host_phase_budget"]["remaining_seconds"] - assert 7190 < retained <= 7200 + budget = body["point_metadata"]["host_phase_budget"] + assert budget["source_deadline_epoch"] == source_deadline + retained = budget["remaining_seconds"] + assert 7190 < retained <= 7200.25 prefix = host_phase_command(tmp_path, monkeypatch) control = tmp_path / "host-control" control.mkdir() @@ -331,7 +345,8 @@ def test_host_phase_and_native_opportunities_continue_without_offline_refill( result = subprocess.run(["/bin/bash", "-c", shell + f"printf '8' > '{candidate}'; printf '%s' \"$remaining\""], env={"PATH": str(bin) + ":/usr/bin:/bin"}, capture_output=True, text=True, check=True) - assert 7180 < int(result.stdout) <= 7200 and candidate.read_bytes() == b"8" + assert int(result.stdout) == int(phase.read_bytes()) - int(clock) + assert candidate.read_bytes() == b"8" history = requests.get(second + "/api/v1/history", params={"token": fresh, "admin_secret": destination.state.judge.admin_secret}, timeout=5).json() assert history["agent_submissions"] == 1 and history["best_score"] == 7 assert submit(second, fresh, 8).status_code == 429 diff --git a/benchmark/tests/test_checkpoint_worker.py b/benchmark/tests/test_checkpoint_worker.py index 2d540c1855..27001e2abb 100644 --- a/benchmark/tests/test_checkpoint_worker.py +++ b/benchmark/tests/test_checkpoint_worker.py @@ -133,7 +133,7 @@ def test_supervisor_captures_only_the_prepared_worker_boundary(controllers, writ continued_clock = "/opt/loopx-benchmark/control/continued-phase-deadline" if fault == "continued_clock": assert container.exec_run(["python", "-c", "from pathlib import Path; " - f"Path({continued_clock!r}).write_bytes(Path('/phase').read_bytes()); " + f"Path({continued_clock!r}).write_text(str(int(Path('/phase').read_text()) + .25) + '\\n'); " "Path('/opt/loopx-benchmark/control/phase-deadline').write_text('1')"]).exit_code == 0 prepared = _archive(container, "/candidate", tmp_path / "prepared") probe = effect_runtime_result("runtime.checkpoint_artifacts", {"action": "probe", @@ -213,7 +213,12 @@ def with_old_point(*args, **kwargs): assert response["capture"]["research_point_qualified"] is False if fault == "continued_clock": capsule = unit / "components" / metadata["worker_file_snapshot"]["native_capsule_component_id"] - assert (capsule / "components/host_phase_deadline").read_bytes() == container.exec_run(["cat", "/phase"]).output + raw = container.exec_run(["cat", continued_clock]).output + assert (capsule / "components/host_phase_deadline").read_bytes() == raw + budget = json.loads((capsule / "checkpoint.json").read_bytes())["point_metadata"]["host_phase_budget"] + assert budget["source_deadline_epoch"] == float(raw) + assert budget["source_deadline_epoch"] % 1 == .25 + assert budget["remaining_seconds"] == float(raw) - budget["captured_at_epoch"] assert container.exec_run(["cat", "/opt/loopx-benchmark/control/phase-deadline"]).output == b"1" if fault in {"retention", "retention_default"}: assert not (tmp_path / "points/old").exists() diff --git a/benchmark/tests/test_scheduler_phase_resume.py b/benchmark/tests/test_scheduler_phase_resume.py index e535689490..6ff48ba5cb 100644 --- a/benchmark/tests/test_scheduler_phase_resume.py +++ b/benchmark/tests/test_scheduler_phase_resume.py @@ -20,6 +20,7 @@ def test_scheduler_continuation_clock_reaches_both_host_capture_paths(tmp_path, """Actual guest scheduler + archive IO; only native sealing is substituted.""" import docker from benchmark.edgebench import checkpoint_supervisor as host + from benchmark.edgebench.checkpoint_controller import _phase_budget from benchmark.edgebench.checkpoint_worker import _archive from benchmark.runtime.layout import _PHASE_DEADLINE @@ -45,7 +46,7 @@ def test_scheduler_continuation_clock_reaches_both_host_capture_paths(tmp_path, receipt=root/'wakes/scheduler/receipt.json';receipt.parent.mkdir(parents=True) receipt.write_text(json.dumps({'budget_exhausted':True,'host_invoked':False,'mode':'heartbeat','stage':'execute'})) if not continued:state.unlink() -os.environ.update(LOOPX_PHASE_DEADLINE_EPOCH=str(int(time.time())+800 if continued else original),LOOPX_AGENT_ID='agent', +os.environ.update(LOOPX_PHASE_DEADLINE_EPOCH=str(int(time.time())+800.25 if continued else original),LOOPX_AGENT_ID='agent', LOOPX_WAKE_LOG_DIR=str(root/'wakes'),LOOPX_EXECUTION_MODE='heartbeat',LOOPX_CLI='/bin/true', LOOPX_REGISTRY='/registry.json',LOOPX_RUNTIME_ROOT='/runtime',LOOPX_GOAL_ID='fixture') sys.argv=['scheduler','--python',sys.executable,'--source','/source','--state-file',str(state),'--host-timeout','30'] @@ -71,18 +72,24 @@ def test_scheduler_continuation_clock_reaches_both_host_capture_paths(tmp_path, def capture(*args, **kwargs): request = json.loads(kwargs["request_file"].read_text()) path = _archive(container, request["phase_deadline_path"], tmp_path / f"read-{len(observed)}") - observed.append((request["phase_deadline_path"], float(path.read_text()) - time.time())) - supervisor.stop.set() + try: + budget = _phase_budget(path.read_bytes(), {"captured_at_epoch": time.time()}) + observed.append((request["phase_deadline_path"], budget)) + finally: + supervisor.stop.set() return {"research_point_qualified": False} monkeypatch.setattr(host, "capture_worker", capture) monkeypatch.setattr("benchmark.edgebench.checkpoint_worker.capture_worker", capture) supervisor._run(container) + reply = json.loads((destination / ("b" * 32 + ".json")).read_bytes()) + assert reply["ok"], reply supervisor.finish_solver({"execution_started": True, "exit_code": 2}) expected_path = "/opt/loopx-benchmark/control/active-phase-deadline" if continued else _PHASE_DEADLINE assert [path for path, _ in observed] == [expected_path, expected_path] - limit = 800 if continued else 400 - assert all(limit - 100 < remaining <= limit for _, remaining in observed) + limit = 800.25 if continued else 400 + assert all(limit - 100 < budget["remaining_seconds"] <= limit for _, budget in observed) + assert all(budget["source_deadline_epoch"] % 1 == (.25 if continued else 0) for _, budget in observed) original = _archive(container, _PHASE_DEADLINE, tmp_path / "original") assert original.read_bytes() == original_bytes finally: From bc45ade8323dda4d0190d9e15cfbeb790547ee80 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Thu, 12 Nov 2026 11:42:08 +0800 Subject: [PATCH 010/267] fix(benchmark): recover a stopped worker after a completed host wake Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- benchmark/edgebench/README.md | 9 +- benchmark/edgebench/stopped_recovery.py | 91 +++++++++++++---- benchmark/runtime/RUNTIME.md | 34 ++++--- benchmark/runtime/interruption_recovery.py | 71 +++++++++----- benchmark/runtime/scheduler.py | 2 +- benchmark/tests/test_idle_host_handoff.py | 109 +++++++++++++++++++-- loopx/hosts/codex_checkpoint.py | 30 +++--- 7 files changed, 259 insertions(+), 87 deletions(-) diff --git a/benchmark/edgebench/README.md b/benchmark/edgebench/README.md index cf5997a900..35c034c6b8 100644 --- a/benchmark/edgebench/README.md +++ b/benchmark/edgebench/README.md @@ -290,15 +290,14 @@ An Official worker without a checkpoint must retain its existing session and use its authorized runtime owner's idle credential handoff; this preparation helper does not provide a live credential-replacement transaction. -An unchanged stopped heartbeat worker with an empty-output legacy Host failure -can use the explicit [idle-host handoff](../runtime/RUNTIME.md) after independent +An unchanged stopped heartbeat worker with a completed Host or an empty-output +legacy failure can use the explicit [idle-host handoff](../runtime/RUNTIME.md) after independent native-history, source, current Goal and account qualification. Seal with `python -m benchmark.edgebench.stopped_recovery --idle-host-handoff` using the original source declaration; then select the original scheduler digest with `benchmark.runtime.scheduler --idle-host-state-sha256`. Both options are off by -default. The original failure remains recorded. This route preserves the -existing completed session and source rather than manufacturing an interrupted -Turn. Native judge restoration, opportunity/cooldown conservation, the audited +default. The original return, session, and source remain recorded. +Native judge restoration, opportunity/cooldown conservation, the audited remaining allowance and a finite owner cutoff remain separate requirements. It does not apply to Official or native-Goal workers. diff --git a/benchmark/edgebench/stopped_recovery.py b/benchmark/edgebench/stopped_recovery.py index 16f59f3cfb..54556497b1 100644 --- a/benchmark/edgebench/stopped_recovery.py +++ b/benchmark/edgebench/stopped_recovery.py @@ -102,6 +102,47 @@ def validate_idle_host_return(state: dict, receipt: dict, *, stdout: bytes, stde raise ValueError('Idle handoff requires its original empty-output legacy Host return') + +def completed_host_window(state: dict, receipt: dict, *, lineage: dict, phase: float) -> tuple[float, float]: + """Verify a completed process at the retained clock without judging its work.""" + from benchmark.runtime.interruption_recovery import _number + + process = receipt.get("native_process") or {} + before = process.get("before_cleanup") or {} + after = process.get("after_cleanup") or {} + session = receipt.get("session") or {} + started = _number(process.get("started_at_unix")) + returned = _number(before.get("observed_at_unix")) + stopped = _number(after.get("observed_at_unix")) + try: + wake_phase = float(receipt.get("phase_deadline_epoch")) + except (TypeError, ValueError) as error: + raise ValueError("Completed Host requires its original phase clock") from error + if ("host_attempt" in state or state.get("last_wake_status") != "wake_ok" + or state.get("last_wake_failure_kind") is not None + or any(type(row.get(key)) is not int or row[key] != 0 + for row, key in ((state, "last_wake_rc"), (receipt, "return_code"), + (before, "return_code"), (after, "return_code"))) + or receipt.get("mode") != "heartbeat" or receipt.get("stage") != "execute" + or receipt.get("ok") is not True or receipt.get("host_invoked") is not True + or receipt.get("timed_out") is not False or receipt.get("budget_exhausted") + or "host_failure" in receipt or receipt.get("error_kind") is not None + or any(receipt.get(key) != lineage[key] for key in ("goal_id", "agent_id")) + or not re.fullmatch(r"[a-f0-9]{32}", receipt.get("host_attempt_id") or "") + or session.get("binding_scope") != "agent" + or not isinstance(session.get("session_id"), str) or not session["session_id"].strip() + or process.get("schema_version") != "native_codex_process_observation_v1" + or type(process.get("pid")) is not int or process["pid"] <= 1 + or process.get("cleanup_trigger") != "scope_completed" + or any(process.get(key) is not None for key in + ("first_failure", "scope_error_type", "cleanup_error_type", "persistence_error_type")) + or before.get("signal") is not None or after.get("signal") is not None + or not 0 < started <= returned <= stopped <= phase + or not math.isfinite(phase) or wake_phase != phase): + raise ValueError("Stopped source lacks its original completed Host return") + return started, stopped + + def prepare_stopped_recovery(client, *, container_id: str, declaration: dict, output: Path, timeout: float = 120, install: bool = False, idle_host_handoff: bool = False) -> dict: @@ -149,8 +190,12 @@ def prepare_stopped_recovery(client, *, container_id: str, declaration: dict, prior = state.get('host_attempt', {}) failed = prior.get('status') == 'failed' returned_budget_stop = prior.get('status') == 'stopped' - if (prior.get('status') not in {'started', 'failed', 'stopped'} - or json.loads(copied['host_pending'].read_bytes()) != {'pending': True}): + completed = idle_host_handoff and "host_attempt" not in state + pending = json.loads(copied['host_pending'].read_bytes()) + if completed: + if pending != {'pending': False}: + raise ValueError('Completed Host handoff requires its cleared pending witness') + elif prior.get('status') not in {'started', 'failed', 'stopped'} or pending != {'pending': True}: raise ValueError('Stopped legacy recovery requires the original unresolved Host attempt') wake = copied['wake_log_dir'] / declaration['wake_turn_id'] / 'receipt.json' raw = wake.read_bytes() @@ -177,7 +222,7 @@ def prepare_stopped_recovery(client, *, container_id: str, declaration: dict, or any(key in receipt and receipt[key] != declaration[key] for key in ('goal_id', 'agent_id'))): raise ValueError('Stopped source lacks its exact returned budget stop') - if idle_host_handoff: + if idle_host_handoff and not completed: from benchmark.runtime.worker import host_output_paths stdout, stderr = host_output_paths(wake.parent) @@ -198,8 +243,8 @@ def prepare_stopped_recovery(client, *, container_id: str, declaration: dict, or type(receipt.get('return_code')) is not int or receipt['return_code'] != 1 or receipt.get('budget_exhausted')): raise ValueError('Stopped source lacks its exact returned quota failure') - if failed or returned_budget_stop: - # Returned failures and budget stops are committed after their + if failed or returned_budget_stop or completed: + # Returned Hosts are committed after their # receipt. A still-started legacy attempt has the opposite order. # Neither path may borrow an earlier wake or skip a later one. all_receipts = list(copied['wake_log_dir'].glob('*/receipt.json')) @@ -209,27 +254,33 @@ def prepare_stopped_recovery(client, *, container_id: str, declaration: dict, candidates = [wake] if candidates != [wake]: raise ValueError('Stopped source has no unique returned wake after its Host attempt') - if (receipt.get('turn_id') != declaration['wake_turn_id'] or receipt.get('host_invoked') is not True - or (not failed and receipt.get('budget_exhausted') is not True) or receipt.get('mode') != 'heartbeat' - or receipt.get('stage') != 'execute' or receipt.get('ok') is not False - or (not failed and 'host_failure' in receipt)): - raise ValueError('Stopped source lacks an exact interrupted budget receipt') - start = int(declaration['wake_turn_id'].split('-')[1]) / 1_000_000_000 + wake_started = int(declaration['wake_turn_id'].split('-')[1]) / 1_000_000_000 phase = float(copied['phase_clock'].read_text()) - # A returned stop proves the process was reaped before its receipt. - # Legacy workers could reserve cleanup time before the hard deadline; - # do not enlarge that native Turn's window to a later container stop. - process_stopped = math.floor(wake.stat().st_mtime) if returned_budget_stop or idle_host_handoff else finished - if (not math.isfinite(phase) or not start <= min(phase, process_stopped) - or process_stopped > finished - or (not failed and not returned_budget_stop and phase > finished)): - raise ValueError('Stopped source phase did not expire during the original wake') + if completed: + start, process_stopped = completed_host_window(state, receipt, lineage=declaration, phase=phase) + if (receipt.get('turn_id') != declaration['wake_turn_id'] or start < wake_started + or process_stopped > min(finished, math.floor(wake.stat().st_mtime) + 1)): + raise ValueError('Completed Host process lies outside the retained wake') + else: + if (receipt.get('turn_id') != declaration['wake_turn_id'] or receipt.get('host_invoked') is not True + or (not failed and receipt.get('budget_exhausted') is not True) + or receipt.get('mode') != 'heartbeat' or receipt.get('stage') != 'execute' + or receipt.get('ok') is not False or (not failed and 'host_failure' in receipt)): + raise ValueError('Stopped source lacks an exact interrupted budget receipt') + start = wake_started + # Legacy workers could reserve cleanup time before the hard deadline. + process_stopped = math.floor(wake.stat().st_mtime) if returned_budget_stop or idle_host_handoff else finished + if (not math.isfinite(phase) or not start <= min(phase, process_stopped) + or process_stopped > finished + or (not failed and not returned_budget_stop and phase > finished)): + raise ValueError('Stopped source phase did not expire during the original wake') result = {'container_id': box.id, 'source_state': before, 'declaration': declaration, 'components': components, 'scheduler_state_sha256': hashlib.sha256(state_raw).hexdigest(), 'receipt_sha256': hashlib.sha256(raw).hexdigest(), 'process_started_at': start, 'process_stopped_at': process_stopped, 'source_phase_deadline_epoch': phase, 'session': receipt.get('session'), **({'idle_host_handoff': True} if idle_host_handoff else {}), + **({'completed_host': True} if completed else {}), 'execution_authority_granted': False} box.reload() if box.attrs['State'] != before: @@ -287,7 +338,7 @@ def main(): parser.add_argument('--output', type=Path, required=True) parser.add_argument('--install', action='store_true', help='Place the inert proof only in this same stopped worker') parser.add_argument('--idle-host-handoff', action='store_true', - help='Explicitly seal an empty-output legacy Host failure for separate idle-history qualification') + help='Explicitly seal a returned Host for separate idle-history qualification') args = parser.parse_args() import docker result = prepare_stopped_recovery(docker.from_env(timeout=30), diff --git a/benchmark/runtime/RUNTIME.md b/benchmark/runtime/RUNTIME.md index 5657ba18f6..e58a08fc67 100644 --- a/benchmark/runtime/RUNTIME.md +++ b/benchmark/runtime/RUNTIME.md @@ -440,17 +440,18 @@ Budget exhaustion is distinct from task completion: this clock does not override provider failures, Stop, blocked planning, or a valid terminal LoopX decision. Campaign duration coverage must still classify an early exit as missing time. -An operator may explicitly hand off an unchanged stopped legacy heartbeat Host -whose failed attempt produced no stdout/stderr and returned a phase timeout, -while the native session remained idle. This is disabled by default. First run -`benchmark.edgebench.stopped_recovery` with `--idle-host-handoff` and, when -needed, `--install` to place its inert proof in that same stopped worker. The -producer requires the exact failed scheduler, pending witness, latest unique -wake, original workspace, registry, runtime and native stores, including committed -SQLite WALs. It preserves the unknown failure and does not infer a quota failure -from diagnostic text. The proof grants no execution or evaluator authority. -The empty-output witness uses the worker's `stdout.jsonl` and `stderr.log` files; -the producer and receiver share that writer-owned layout. +An operator may explicitly hand off an unchanged stopped heartbeat worker when +its native session is idle. First run `benchmark.edgebench.stopped_recovery` +with `--idle-host-handoff` and, when needed, `--install` to place its inert +proof in that same stopped worker. The proof includes the latest unique wake, +the scheduler and its independent pending witness, the original workspace, +registry, runtime and native stores, including committed SQLite WALs. +This path is disabled by default and grants no execution or evaluator authority. + +A completed Host requires a successful process receipt at the recorded phase +deadline and a cleared pending witness. The producer also supports a legacy +empty-output phase timeout with a still-pending witness. It checks the worker's +`stdout.jsonl` and `stderr.log` and preserves the recorded unknown failure. After the owner restores compatible native evaluator authority separately, restart only the original inert container and retain its original environment. @@ -460,8 +461,9 @@ authorized finite `LOOPX_PHASE_DEADLINE_EPOCH`. Invoke `--idle-host-state-sha256 ORIGINAL_SCHEDULER_SHA256`. The consumer verifies the same namespace and complete unchanged source, current Goal/session/profile and fresh native account allowance. The native history owner must return a stable -complete history whose last completed cursor precedes the failed attempt, with -no pending effects. Its observer uses a disposable copy of the native store and +complete history with no pending effects. For a completed Host, the last +native Turn must fall inside the original process window. For a legacy timeout, +it must precede the attempted launch. The observer uses a disposable copy of the native store and committed WALs because native read APIs can update their own history index; it copies no authentication file. Every copied native base participant must match the producer's sealed set; an extra archived-session/index store or database @@ -474,9 +476,9 @@ existing lock owner's process/token witnesses; registry contents, SQLite/WALs, work files and unrelated lock files remain protected. An accepted handoff creates a new finite transport phase linked to the original -failed record. It retains the session, Goal, Todos, work and failure evidence; -it does not settle an old Turn, refill native opportunities/cooldown, or create -another task budget. Repeating the exact phase retains its clock and any later +scheduler record. It retains the session, Goal, Todos, work, and any pending +semantic settlement. The existing owners still enforce settlement, native +opportunities, cooldown and the total task budget. Repeating the exact phase retains its clock and any later failure; a changed deadline cannot replay the original handoff. The owner still enforces its audited remaining solver allowance and external cutoff, including setup and cleanup. Omit both new opt-ins to keep the ordinary refusal path. diff --git a/benchmark/runtime/interruption_recovery.py b/benchmark/runtime/interruption_recovery.py index 1c99f9917b..5f6c5c6f70 100644 --- a/benchmark/runtime/interruption_recovery.py +++ b/benchmark/runtime/interruption_recovery.py @@ -273,13 +273,10 @@ def read_interrupted_host(state: dict, *, state_file: Path, env: dict[str, str], def read_idle_host(state: dict, *, state_file: Path, env: dict[str, str], wake_log_dir: str) -> dict: - """Explicitly hand off one stopped legacy Host to its unchanged idle session. - - An empty output and unknown failure do not establish idleness. The native - owner must independently return its complete stable history before the - original attempt. No failure classification or original receipt is changed. - """ - from benchmark.edgebench.stopped_recovery import native_source_paths, validate_idle_host_return + """Qualify the stopped Host's unchanged idle session and current authority.""" + from benchmark.edgebench.stopped_recovery import ( + completed_host_window, native_source_paths, validate_idle_host_return, + ) from loopx.control_plane.turn_driver.codex_agent_session import CodexAgentSession from loopx.hosts.codex_checkpoint import native_idle_turn from .codex import process_environment @@ -288,20 +285,28 @@ def read_idle_host(state: dict, *, state_file: Path, env: dict[str, str], wake_l from .worker import host_output_paths env = process_environment(Path(env['LOOPX_CODEX_HOME']), base=env) - if _host_pending(state_file) is not True: - raise ValueError('Idle handoff requires its original pending Host witness') path, raw, receipt, source = _stopped_source(state_file, state, env, wake_log_dir) if source.get('idle_host_handoff') is not True: raise ValueError('Idle handoff requires its explicitly sealed stopped source') - stdout, stderr = host_output_paths(path.parent) - validate_idle_host_return(state, receipt, stdout=_physical_read(stdout), - stderr=_physical_read(stderr), - lineage={'goal_id': env['LOOPX_GOAL_ID'], 'agent_id': env['LOOPX_AGENT_ID']}) + completed = source.get('completed_host') is True + expected_pending = not completed + if _host_pending(state_file) is not expected_pending: + raise ValueError('Idle handoff differs from its original pending Host witness') + lineage = {'goal_id': env['LOOPX_GOAL_ID'], 'agent_id': env['LOOPX_AGENT_ID']} started = _number(source['process_started_at']) stopped = _number(source['process_stopped_at']) - if (receipt.get('turn_id') != path.parent.name or stopped != math.floor(path.stat().st_mtime) - or started > stopped): - raise ValueError('Idle handoff differs from its original returned Host window') + if completed: + clock = _number(source['source_phase_deadline_epoch']) + if completed_host_window(state, receipt, lineage=lineage, phase=clock) != (started, stopped): + raise ValueError('Idle handoff differs from its original completed Host window') + else: + stdout, stderr = host_output_paths(path.parent) + validate_idle_host_return(state, receipt, stdout=_physical_read(stdout), + stderr=_physical_read(stderr), lineage=lineage) + if stopped != math.floor(path.stat().st_mtime) or started > stopped: + raise ValueError('Idle handoff differs from its original returned Host window') + if receipt.get('turn_id') != path.parent.name: + raise ValueError('Idle handoff wake differs from its original receipt') session = receipt['session']['session_id'] current = CodexAgentSession(env, context='resume', sandbox=env.get('LOOPX_CODEX_SANDBOX', 'danger-full-access'), receipt={}) @@ -311,7 +316,8 @@ def read_idle_host(state: dict, *, state_file: Path, env: dict[str, str], wake_l raise ValueError('Idle handoff native session binding changed') allowance = require_native_receiver_allowance(env) native = native_idle_turn(env, session, process_started_at=started, - sealed_store_paths=tuple(native_source_paths(source['declaration']).values())) + sealed_store_paths=tuple(native_source_paths(source['declaration']).values()), + **({'process_stopped_at': stopped} if completed else {})) with current.admission.launch_lifetime(operation='benchmark_idle_host_handoff'): from loopx.file_lock import _effect_mutation_lock_path, lock_holder_paths from loopx.control_plane.goals.source_session_registry_state import guard_path @@ -334,12 +340,14 @@ def read_idle_host(state: dict, *, state_file: Path, env: dict[str, str], wake_l 'wake_turn_id': receipt['turn_id'], 'process_started_at': started, 'source_phase_deadline_epoch': source['source_phase_deadline_epoch'], 'session_profile_digest': current.digest, 'allowance': allowance, - 'original_host_failure': state['host_attempt']['host_failure'], 'native': native} + 'native': native, **({'process_stopped_at': stopped} if completed + else {'original_host_failure': state['host_attempt']['host_failure']})} def verify_idle_host_replay(state_file: Path, recovery: dict, *, env: dict[str, str]) -> None: """A repeated phase selection retains its original qualification and clock.""" - from benchmark.edgebench.stopped_recovery import validate_idle_host_return + from benchmark.edgebench.stopped_recovery import completed_host_window, validate_idle_host_return + from scripts.external_scheduler_worker import _host_pending_path from .worker import host_output_paths native, allowance = recovery.get('native'), recovery.get('allowance') @@ -352,14 +360,25 @@ def verify_idle_host_replay(state_file: Path, recovery: dict, *, env: dict[str, source = json.loads(_physical_read(state_file)) raw = _physical_read(Path(recovery['receipt_path'])) receipt = json.loads(raw) - stdout, stderr = host_output_paths(Path(recovery['receipt_path']).parent) - validate_idle_host_return(source, receipt, stdout=_physical_read(stdout), - stderr=_physical_read(stderr), - lineage={'goal_id': env['LOOPX_GOAL_ID'], 'agent_id': env['LOOPX_AGENT_ID']}) - if (hashlib.sha256(raw).hexdigest() != recovery['receipt_sha256'] - or source['host_attempt']['host_failure'] != recovery['original_host_failure'] + lineage = {'goal_id': env['LOOPX_GOAL_ID'], 'agent_id': env['LOOPX_AGENT_ID']} + started = _number(recovery.get('process_started_at')) + completed_at = _number(native.get('completed_at')) + if 'process_stopped_at' in recovery: + stopped = _number(recovery['process_stopped_at']) + clock = _number(recovery.get('source_phase_deadline_epoch')) + window_ok = ( + json.loads(_physical_read(_host_pending_path(state_file))) == {'pending': False} + and completed_host_window(source, receipt, lineage=lineage, phase=clock) == (started, stopped) + and math.floor(started) <= _number(native.get('started_at')) <= completed_at <= stopped + ) + else: + stdout, stderr = host_output_paths(Path(recovery['receipt_path']).parent) + validate_idle_host_return(source, receipt, stdout=_physical_read(stdout), + stderr=_physical_read(stderr), lineage=lineage) + window_ok = (source['host_attempt']['host_failure'] == recovery['original_host_failure'] + and completed_at < math.floor(started)) + if (not window_ok or hashlib.sha256(raw).hexdigest() != recovery['receipt_sha256'] or native.get('status') != 'completed' or native.get('session_id') != receipt['session']['session_id'] - or not _number(native.get('completed_at')) < math.floor(_number(recovery['process_started_at'])) or type(native.get('turn_count')) is not int or native['turn_count'] < 1 or not re.fullmatch(r'[a-f0-9]{64}', native.get('visible_history_sha256', ''))): raise ValueError('Idle handoff replay differs from the original qualified source') diff --git a/benchmark/runtime/scheduler.py b/benchmark/runtime/scheduler.py index 0723d449e3..d38fbf1fce 100644 --- a/benchmark/runtime/scheduler.py +++ b/benchmark/runtime/scheduler.py @@ -384,7 +384,7 @@ def main() -> int: parser.add_argument('--retry-failed-state-sha256', help='Explicitly continue only this exact returned failure after operator repair') parser.add_argument('--idle-host-state-sha256', - help='Explicitly hand off this sealed legacy empty-output Host to its verified idle native session') + help='Explicitly hand off this sealed returned Host to its verified idle native session') args = parser.parse_args() state = resume_state_file(args.state_file, phase_deadline=float(os.environ["LOOPX_PHASE_DEADLINE_EPOCH"]), diff --git a/benchmark/tests/test_idle_host_handoff.py b/benchmark/tests/test_idle_host_handoff.py index 80bbb9bd8f..66ac2fdd09 100644 --- a/benchmark/tests/test_idle_host_handoff.py +++ b/benchmark/tests/test_idle_host_handoff.py @@ -58,6 +58,77 @@ def _idle_return(declaration): return state, receipt + +def _completed_return(declaration): + state, receipt = _idle_return(declaration) + attempt = { + "goal_id": declaration["goal_id"], "agent_id": declaration["agent_id"], + "registry": declaration["registry"], "runtime_root": declaration["runtime_root"], + "obligation_id": "replan-0123456789abcdef", "status": "returned", "wake_rc": 0, + } + state.write_text(json.dumps({"last_wake_status": "wake_ok", "last_wake_rc": 0, + "replan_attempts": [attempt]})) + os.utime(state, (111, 111)) + state.with_name(state.name + ".host-attempt").write_text('{"pending": false}') + value = json.loads(receipt.read_text()) + for key in ("budget_exhausted", "error_kind", "error"): + value.pop(key, None) + value.update( + ok=True, return_code=0, timed_out=False, + goal_id=declaration["goal_id"], agent_id=declaration["agent_id"], + host_attempt_id="f" * 32, phase_deadline_epoch="200", + native_process={ + "schema_version": "native_codex_process_observation_v1", + "pid": 23, "started_at_unix": 101.1, "cleanup_trigger": "scope_completed", + "before_cleanup": {"return_code": 0, "observed_at_unix": 108.9}, + "after_cleanup": {"return_code": 0, "observed_at_unix": 109.1}, + }, + ) + receipt.write_text(json.dumps(value)) + os.utime(receipt, (110, 110)) + Path(declaration["phase_clock"]).write_text("200.0") + return state, receipt + + +@pytest.mark.parametrize("fault", [None, "default", "pending", "clock", "wrong_goal", "late_return", "later_wake"]) +def test_completed_host_source_requires_the_last_exact_return(tmp_path, monkeypatch, fault): + declaration = _source(tmp_path) + state, receipt = _completed_return(declaration) + box, client = _fake_container(monkeypatch) + if fault == "pending": + state.with_name(state.name + ".host-attempt").write_text('{"pending": true}') + elif fault == "clock": + Path(declaration["phase_clock"]).write_text("210") + elif fault in {"wrong_goal", "late_return"}: + value = json.loads(receipt.read_text()) + if fault == "wrong_goal": + value["goal_id"] = "other-goal" + else: + value["native_process"]["after_cleanup"]["observed_at_unix"] = 111.5 + receipt.write_text(json.dumps(value)) + os.utime(receipt, (110, 110)) + elif fault == "later_wake": + later = receipt.parent.with_name("later") / "receipt.json" + later.parent.mkdir() + later.write_bytes(receipt.read_bytes()) + os.utime(later, (111, 111)) + output = tmp_path / "proof.json" + before = state.read_bytes() + if fault: + with pytest.raises(ValueError): + prepare_stopped_recovery(client, container_id=box.id, declaration=declaration, + output=output, idle_host_handoff=fault != "default") + assert not output.exists() + else: + proof = prepare_stopped_recovery(client, container_id=box.id, declaration=declaration, + output=output, idle_host_handoff=True) + assert proof["completed_host"] is True + assert proof["execution_authority_granted"] is False + assert proof["process_started_at"] == 101.1 + assert proof["process_stopped_at"] == 109.1 + assert proof["source_phase_deadline_epoch"] == 200 + assert state.read_bytes() == before + def test_stopped_export_uses_output_created_by_the_real_worker(tmp_path, monkeypatch): from benchmark.runtime import worker from benchmark.tests.test_worker_phase_budget import _phase_worker @@ -301,8 +372,9 @@ def reader(*args, **kwargs): "unrelated_lock", ], ) +@pytest.mark.parametrize("completed_host", [False, True], ids=["legacy-empty", "completed-host"]) def test_real_sqlite_idle_handoff_keeps_source_and_runs_the_retained_session( - tmp_path, monkeypatch, fault + tmp_path, monkeypatch, fault, completed_host ): from benchmark.runtime import worker from benchmark.tests.test_worker_phase_budget import _phase_worker @@ -344,7 +416,10 @@ def test_real_sqlite_idle_handoff_keeps_source_and_runs_the_retained_session( elif method=='account/read': value={'account':{'type':'chatgpt'}} elif method=='account/rateLimits/read': value={'rateLimits':{'primary':{'usedPercent':100 if (markers/'exhausted').exists() else 40}}} elif method=='thread/read': value={'thread':{'id':'original-native','cwd':str(pathlib.Path.cwd())}} - elif method=='thread/turns/list': value={'data':[{'id':'last-completed','status':'completed','startedAt':80,'completedAt':int(time.time())+10 if (markers/'new-native-work').exists() else 90,'error':None,'items':[]}],'nextCursor':None} + elif method=='thread/turns/list': + started,completed=(102,108) if (markers/'completed-host').exists() else (80,90) + if (markers/'new-native-work').exists(): completed=int(time.time())+10 + value={'data':[{'id':'last-completed','status':'completed','startedAt':started,'completedAt':completed,'error':None,'items':[]}],'nextCursor':None} else: raise AssertionError(method) print(json.dumps({'jsonrpc':'2.0','id':request['id'],'result':value}),flush=True) sys.exit(0) @@ -389,17 +464,20 @@ def test_real_sqlite_idle_handoff_keeps_source_and_runs_the_retained_session( ) state.write_text("{}") state.with_name(state.name + ".host-attempt").write_text('{"pending":true}') - state, returned = _idle_return(declaration) + if completed_host: + (control / "completed-host").touch() + state, returned = _completed_return(declaration) + else: + state, returned = _idle_return(declaration) + clock.write_text("105") receipt = json.loads(returned.read_text()) receipt["session"]["session_id"] = "original-native" returned.write_text(json.dumps(receipt)) os.utime(returned, (110, 110)) - # The only latest source wake is the selected returned failure. + # The selected return must still be the latest source wake. for old in Path(env["LOOPX_WAKE_LOG_DIR"]).glob("*/receipt.json"): if old != returned: os.utime(old, (95, 95)) - # Readback fixtures use historical seconds, as does the retained Host proof. - clock.write_text("105") original_uts = os.uname() unrelated_lock = project / "research.lock" unrelated_lock.write_text("retained work evidence") @@ -482,8 +560,16 @@ def resume(): initial = phase.read_bytes() saved = json.loads(initial) assert saved["recovered_session_id"] == "original-native" - assert saved["idle_host_recovery"]["original_host_failure"]["kind"] == "unknown" - assert saved["idle_host_recovery"]["phase_deadline_epoch"] == deadline + recovery = saved["idle_host_recovery"] + if completed_host: + assert recovery["process_stopped_at"] == 109.1 + assert recovery["native"]["started_at"] == 102 + assert recovery["native"]["completed_at"] == 108 + assert "original_host_failure" not in recovery + assert saved["replan_attempts"] == json.loads(original)["replan_attempts"] + else: + assert recovery["original_host_failure"]["kind"] == "unknown" + assert recovery["phase_deadline_epoch"] == deadline assert resume() == str(phase) and phase.read_bytes() == initial for key in ("allowance", "native", "receipt_sha256", "session_profile_digest"): changed = copy.deepcopy(saved) @@ -492,6 +578,13 @@ def resume(): with pytest.raises(ValueError): resume() phase.write_bytes(initial) + if completed_host: + pending = state.with_name(state.name + ".host-attempt") + cleared = pending.read_bytes() + pending.write_text('{"pending": true}') + with pytest.raises(ValueError, match="replay differs"): + resume() + pending.write_bytes(cleared) assert worker.run_once(env)["ok"] assert ( project / "continued-candidate" diff --git a/loopx/hosts/codex_checkpoint.py b/loopx/hosts/codex_checkpoint.py index 2f2f1e79f7..0c3c449023 100644 --- a/loopx/hosts/codex_checkpoint.py +++ b/loopx/hosts/codex_checkpoint.py @@ -173,15 +173,18 @@ def _idle_native_reader(env: dict[str, str], *, timeout: float, deadline: float, def native_idle_turn(env: dict[str, str], session_id: str, *, process_started_at: float, - timeout: float = 30, sealed_store_paths: tuple[str, ...] | None = None) -> dict[str, Any]: - """Observe a stable completed history predating an uncertain Host launch. + process_stopped_at: float | None = None, timeout: float = 30, + sealed_store_paths: tuple[str, ...] | None = None) -> dict[str, Any]: + """Observe a stable idle session at a stopped Host's process boundary. - The caller separately proves the original namespace stopped, seals its - source, and explicitly requests a finite handoff. This read neither retries - the failed Host nor turns its failure into a completed native Turn. + With a verified completed process window, require its last native Turn + inside that window. Otherwise require completed history before the + uncertain launch. The caller owns source termination and phase admission. """ if (not session_id or any(type(v) not in (int, float) or not math.isfinite(v) - for v in (process_started_at, timeout)) or timeout <= 0): + for v in (process_started_at, timeout)) or timeout <= 0 + or (process_stopped_at is not None and (type(process_stopped_at) not in (int, float) + or not math.isfinite(process_stopped_at) or process_stopped_at < process_started_at))): raise ValueError("Native idle readback requires its original process boundary") deadline = time.monotonic() + timeout with _idle_native_reader(env, timeout=timeout, deadline=deadline, @@ -194,14 +197,18 @@ def request(method, params): return transport.request(method, params) _native_thread_readback(request, env, session_id) turns = _complete_history(request, session_id) + tail = turns[-1] for turn in turns: start, end = turn.get("startedAt"), turn.get("completedAt") if (any(type(v) not in (int, float) or not math.isfinite(v) for v in (start, end)) - or not start <= end < math.floor(process_started_at) - or any(item.get("status") in {"inProgress", "running", "pending"} - for item in turn.get("items", []))): + or start > end or any(item.get("status") in {"inProgress", "running", "pending"} + for item in turn.get("items", []))): + raise ValueError("Native history is not idle before the original Host") + if turn is tail and process_stopped_at is not None: + if not math.floor(process_started_at) <= start <= end <= process_stopped_at: + raise ValueError("Native completed Turn is outside the original Host process") + elif end >= math.floor(process_started_at): raise ValueError("Native history is not idle before the original Host") - tail = turns[-1] if tail.get("error") is not None: raise ValueError("Native idle cursor retains an error") encoded = json.dumps(turns, sort_keys=True, separators=(",", ":")).encode() @@ -210,7 +217,8 @@ def request(method, params): raise ValueError("Native idle history advanced during readback") return {"session_id": session_id, "turn_id": tail["id"], "status": "completed", "completed_at": tail["completedAt"], "turn_count": len(turns), - "visible_history_sha256": hashlib.sha256(encoded).hexdigest()} + "visible_history_sha256": hashlib.sha256(encoded).hexdigest(), + **({"started_at": tail["startedAt"]} if process_stopped_at is not None else {})} def native_failure_error_digest(error: Any, *, cli_projection: bool = False) -> str: From 68e5da4170ca10c1f1e5dbd113a1d8a8a069b9b7 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:55:22 +0800 Subject: [PATCH 011/267] fix(chat): recover confirmed task starts through canonical claims Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../src/features/personal-workspace/i18n.tsx | 2 + .../personal-workspace-page.tsx | 5 +- loopx/chat_actions.py | 85 ++++++++++++++----- .../presentation/action_review_plan.ts | 14 ++- .../test_goal_create_completion.py | 83 ++++++++++++++++++ .../action_review_plan.test.ts | 19 +++++ 6 files changed, 184 insertions(+), 24 deletions(-) diff --git a/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx b/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx index 5ed30c9b3d..0969d22ec4 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx @@ -941,6 +941,7 @@ const en = { "drawer.retryOriginal": "Retry original operation", "actionReview.canonical_update_retry": "The edit is not yet verified. Retry this operation to recover its result.", "actionReview.goal_creation_retry": "Goal initialization is incomplete. Retry the original creation; its recorded storage choice is preserved.", + "actionReview.task_start_retry": "The Task was saved. Retry its original start to continue with that Task.", "actionReview.canonical_update_projection_pending": "The operation was committed; display delivery is pending. Retry this operation to restore the current view.", "actionReview.team_plan_retry": "Retry this assignment using the original plan. Current state is checked again, and any tasks already created are recovered without duplication.", "actionReview.apply_failed": "Execution did not complete. Check the failure and regenerate the preview before retrying.", @@ -2547,6 +2548,7 @@ const zhCN: Record = { "drawer.retryOriginal": "重试原操作", "actionReview.canonical_update_retry": "编辑结果尚未确认。重试此操作以恢复原结果。", "actionReview.goal_creation_retry": "Goal 初始化尚未完成。请重试原创建操作,已记录的存储选择会保留。", + "actionReview.task_start_retry": "任务已保存。重试原操作以继续启动这个任务。", "actionReview.canonical_update_projection_pending": "操作已提交,展示尚未同步。重试此操作以恢复当前视图。", "actionReview.team_plan_retry": "可以从同一提案重试分配。系统会重新核验当前状态,并恢复已记录的结果,不会重复创建任务。", "actionReview.apply_failed": "执行未完成。请检查失败原因并重新生成预览后再试。", diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx index 72884d084e..e21aebcbfc 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx @@ -2146,14 +2146,15 @@ export function PersonalWorkspacePage({ const returned = error instanceof ChatApiError ? typedActionProposalSchema.safeParse(error.payload.proposal) : null; let stored = returned?.success ? returned.data : undefined; - if (!stored && ["team.plan", "goal.create", "todo.update"].includes(proposal.actionKind) && !readbackMismatch) { + const scopedReadback = ["team.plan", "goal.create", "todo.update", "todo.create"].includes(proposal.actionKind); + if (!stored && scopedReadback && !readbackMismatch) { const readback = await actionReadback.refetch(); // Query errors retain cached data; it cannot certify this attempt. if (readback.isSuccess) stored = readback.data?.find(item => item.proposal_id === proposal.previewId); } if (stored?.proposal_id === proposal.previewId && stored.action_kind === proposal.actionKind && matchesTodoTarget(stored) - && (!["team.plan", "goal.create", "todo.update"].includes(proposal.actionKind) || stored.normalized_parameters.goal_id === proposal.goalId)) { + && (!scopedReadback || stored.normalized_parameters.goal_id === proposal.goalId)) { await actionReadback.acceptProposal(stored); const observed = workspaceProposal(stored, t); const reconciled = { ...observed, errorMessage: observed.status === "error" diff --git a/loopx/chat_actions.py b/loopx/chat_actions.py index 0e2b1ba01d..b3004d7dd1 100644 --- a/loopx/chat_actions.py +++ b/loopx/chat_actions.py @@ -23,6 +23,7 @@ from .chat_store import ChatSessionStore from .chat.todo_actions import ChatTodoActionMixin from .control_plane.goals.acceptance_actions import GoalAcceptanceActionMixin +from .control_plane.goals.activation import goal_is_stopped from .control_plane.goals.goal_objective_actions import GoalObjectiveActionMixin from .configure_goal import configure_goal from .control_plane.goals.configure_goal_service import ( @@ -1632,7 +1633,8 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No }, ) agent_id = parameters.get("agent_id") - if agent_id and not isinstance(steps.get("todo_assigned"), dict): + start_execution = parameters.get("start_execution") is True + if agent_id and not start_execution and not isinstance(steps.get("todo_assigned"), dict): update_goal_todo( registry_path=self.registry_path, goal_id=str(parameters["goal_id"]), @@ -1659,7 +1661,7 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No "canonical_receipt": canonical_receipt, } turn_result = None - if parameters.get("start_execution"): + if start_execution: if self.runtime_controller is None: raise ValueError("Chat Runtime Controller is unavailable") execution_agent_id = str( @@ -1675,28 +1677,69 @@ def apply(self, proposal_id: str, *, steward_context: dict[str, Any] | None = No turn_id = _opaque(execution_step.get("turn_id"), field="turn_id") created = False else: - goal = self._goal(str(parameters["goal_id"])) + goal_id = str(parameters["goal_id"]) + owner = str(parameters["agent_id"]) + goal = self._goal(goal_id) + if goal.get("status", "active") not in {"active", "ready"} or goal_is_stopped(goal): + raise ValueError("The Task's Goal is no longer active") project = Path(str(goal.get("repo") or ".")).expanduser().resolve() if not project.is_dir(): raise ValueError("the Goal project root is unavailable") - self._agent_eligibility(execution_agent_id, project=project) - session, _resumed = self.runtime_controller.open_session( - goal_id=str(parameters["goal_id"]), - agent_id=execution_agent_id, - work_dir=project, - objective=str(parameters["text"]), - mode="resume_latest", - channel_id=f"task.{todo_id}", - agent_goal_id=str(parameters["goal_id"]), - ) - session_id = _opaque(session.get("session_id"), field="session_id") - turn, created = self.runtime_controller.submit_turn( - session_id=session_id, - client_turn_id=f"task-start-{proposal_id}", - message=str(parameters["text"]), - work_dir=project, - objective=str(parameters["text"]), - ) + channel_id = f"task.{todo_id}" + client_turn_id = f"task-start-{proposal_id}" + opened = steps.get("task_session_opened") + session_id = None + turn = None + if opened is not None: + session_id = _opaque(opened.get("session_id"), field="session_id") + session = self.runtime_controller.store.load_session(session_id) + if (session is None or session.get("goal_id") != goal_id + or session.get("agent_id") != execution_agent_id or session.get("channel_id") != channel_id): + raise ValueError("The original Task Session is unavailable") + observed = self.runtime_controller.store.turn_for_client(session_id, client_turn_id) + if observed and (observed.get("started_at") or observed.get("upstream_turn_id")): + turn = observed + + created = False + if turn is None: + self._agent_eligibility(execution_agent_id, project=project) + if parameters.get("endpoint_id") and self._resolve_goal_agent(goal_id, execution_agent_id) != owner: + raise ValueError("The reviewed Agent endpoint now belongs to another Goal Agent") + claim_operation_id = f"chat-task-start:{proposal_id}" + claim = update_goal_todo( + registry_path=self.registry_path, goal_id=goal_id, todo_id=todo_id, + claimed_by=owner, agent_id=owner, claim_only=True, manage_execution=True, + claim_operation_id=claim_operation_id, dry_run=False, + ) + if claim["handoff_mode"] == "soft_claim" and claim["status"] in {"recovered", "replayed"}: + # A soft-claim receipt records a past assignment. + # Let the same owner recheck it without acquiring or changing it. + current = update_goal_todo( + registry_path=self.registry_path, goal_id=goal_id, todo_id=todo_id, + claimed_by=owner, agent_id=owner, claim_only=True, manage_execution=True, + dry_run=True, + ) + if current["changed"]: + raise ValueError("The reviewed Task assignment changed before launch") + if not isinstance(steps.get("todo_assigned"), dict): + self.store.save_checkpoint( + proposal_id, step="todo_assigned", + receipt={"todo_id": todo_id, "agent_id": owner}, + ) + if session_id is None: + session, _resumed = self.runtime_controller.open_session( + goal_id=goal_id, agent_id=execution_agent_id, work_dir=project, + objective=str(parameters["text"]), mode="resume_latest", + channel_id=channel_id, agent_goal_id=goal_id, + ) + session_id = _opaque(session.get("session_id"), field="session_id") + self.store.save_checkpoint( + proposal_id, step="task_session_opened", receipt={"session_id": session_id}, + ) + turn, created = self.runtime_controller.submit_turn( + session_id=session_id, client_turn_id=client_turn_id, message=str(parameters["text"]), + work_dir=project, objective=str(parameters["text"]), + ) turn_id = _opaque(turn.get("turn_id"), field="turn_id") self.store.save_checkpoint( proposal_id, diff --git a/loopx/control_plane/presentation/action_review_plan.ts b/loopx/control_plane/presentation/action_review_plan.ts index cd6408a622..708755cff4 100644 --- a/loopx/control_plane/presentation/action_review_plan.ts +++ b/loopx/control_plane/presentation/action_review_plan.ts @@ -16,7 +16,7 @@ export type ActionReviewReason = | "apply_failed" | "inactive_proposal" | "operation_authorization_pending" | "operation_outcome_pending" | "operation_confirmation_expired" | "operation_expiry_unknown" | "team_plan_retry" - | "canonical_update_retry" | "canonical_update_projection_pending" | "goal_creation_retry"; + | "canonical_update_retry" | "canonical_update_projection_pending" | "goal_creation_retry" | "task_start_retry"; export type OperationReviewContent = { title: string; @@ -574,6 +574,18 @@ export function compileActionReviewPlan(proposalValue: unknown, nowMs?: number): && creationProgress) { return {...finish({interaction: "review", canApply: true, reason: "goal_creation_retry"}), retryOriginal: true}; } + if (proposal.action_kind === "todo.create" && parameters?.start_execution === true + && proposal.permission_classification === "durable_write" && identity.proposalId && identity.sourceFingerprint + && (proposal.status === "applying" || (proposal.status === "failed" && objectValue(proposal.failure)?.retry_safe === true))) { + const steps = objectValue(objectValue(proposal.checkpoint)?.steps); + const created = objectValue(steps?.todo_created); + const receipt = objectValue(created?.canonical_receipt); + if (textValue(created?.todo_id) && created?.todo_id === receipt?.todo_id + && textValue(parameters.goal_id) && receipt?.goal_id === parameters.goal_id + && receipt?.preview_id === identity.sourceFingerprint && receipt.status === "applied") { + return {...finish({interaction: "review", canApply: true, reason: "task_start_retry"}), retryOriginal: true}; + } + } const isCanonicalUpdate = basis?.schema_version === "loopx_chat_canonical_update_basis_v0" && textValue(basis.provider_revision) !== null && textValue(basis.registry_sha256) !== null && ((proposal.action_kind === "todo.update") diff --git a/tests/control_plane/test_goal_create_completion.py b/tests/control_plane/test_goal_create_completion.py index b0e25ec3b5..44911ceb76 100644 --- a/tests/control_plane/test_goal_create_completion.py +++ b/tests/control_plane/test_goal_create_completion.py @@ -239,6 +239,89 @@ def assert_retained_then_submit(*args, **kwargs): controller.close() +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("mode", ["soft_claim", "hard_lease"]) +def test_confirmed_task_start_recovers_the_same_claim_and_dispatch(environment, app, monkeypatch, provider, mode): + import runpy + + from loopx.capabilities.native_chat.project_context import ChatProjectContexts + from loopx.chat_runtime import ChatRuntimeController + from loopx.chat_store import ChatSessionStore + from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted + + configure, _, _, project, runtime = environment + store, request, service = app + configure(provider, mode=mode) + goal = preview_goal(request) + status, created = request(f"/api/actions/{goal['proposal_id']}/apply", {}) + assert status == 200, created + + capture, binary = runtime / "task-native-requests.jsonl", runtime / "task-codex" + source = runpy.run_path(str(Path(__file__).parents[2] / "examples/loopx-chat-runtime-smoke.py"))["FAKE_CODEX"] + binary.write_text(source.replace(' method = request.get("method")', + f' with open({str(capture)!r}, "a") as stream:\n stream.write(json.dumps(request) + "\\n")\n' + ' method = request.get("method")')) + binary.chmod(0o700) + chat = ChatSessionStore(runtime / "chat") + controller = ChatRuntimeController(store=chat, codex_bin=str(binary), registry_path=service.registry_path, + project_contexts=ChatProjectContexts([project])) + service.chat_store, service.runtime_controller = chat, controller + + try: + status, preview = request("/api/actions/preview", { + "action_kind": "todo.create", "summary": "Start the appendix check", "idempotency_key": "start-appendix", + "context": {"kind": "goal", "goal_id": "report"}, + "normalized_parameters": {"goal_id": "report", "text": "Verify the supporting appendix", + "agent_id": "codex", "start_execution": True}, + }) + assert status == 201, preview + proposal_id = preview["proposal"]["proposal_id"] + apply = f"/api/actions/{proposal_id}/apply" + save_checkpoint = store.save_checkpoint + + service.runtime_controller = None + status, unavailable = request(apply, {}) + assert status == 400 and "Runtime Controller" in unavailable["error"], unavailable + todo_id = store.load(proposal_id)["checkpoint"]["steps"]["todo_created"]["todo_id"] + partial = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="report", include_leases=True) + assert [todo.get("claimed_by") for todo in partial["todos"] if todo["todo_id"] == todo_id] == [None] + assert partial["leases"] == [] + service.runtime_controller = controller + + def lose_claim_response(*args, **kwargs): + if kwargs.get("step") == "todo_assigned": + raise OSError("Lost the response after committing the Task assignment") + return save_checkpoint(*args, **kwargs) + + monkeypatch.setattr(store, "save_checkpoint", lose_claim_response) + status, failed = request(apply, {}) + assert status == 424, failed + assert store.load(proposal_id)["checkpoint"]["steps"]["todo_created"]["todo_id"] == todo_id + authority = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="report", include_leases=True) + assert [todo["claimed_by"] for todo in authority["todos"] if todo["todo_id"] == todo_id] == ["codex"] + assert chat.list_sessions(goal_id="report") == [] + + monkeypatch.setattr(store, "save_checkpoint", save_checkpoint) + status, recovered = request(apply, {}) + assert status == 202, recovered + ids = recovered["proposal"]["receipt"]["resource_ids"] + assert ids["todo_id"] == todo_id + turn = controller.wait_for_turn(session_id=ids["session_id"], turn_id=ids["turn_id"], timeout_sec=10) + assert turn["status"] == "completed" + sessions = chat.list_sessions(goal_id="report") + assert [session["channel_id"] for session in sessions] == [f"task.{todo_id}"] + messages = [json.loads(line) for line in capture.read_text().splitlines()] + assert [row["params"]["sandbox"] for row in messages if row.get("method") == "thread/start"] == ["workspace-write"] + assert sum(row.get("method") == "turn/start" for row in messages) == 1 + status, replay = request(apply, {}) + assert status == 202 and replay["proposal"]["receipt"] == recovered["proposal"]["receipt"] + assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="report", include_leases=True) == authority + assert [(lease["todo_id"], lease["owner"]) for lease in authority["leases"]] == ( + [(todo_id, "codex")] if mode == "hard_lease" else []) + finally: + controller.close() + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("completion_surface", ["native-cli", "external-worker"]) def test_ordinary_goal_replans_after_first_todo_and_delivers_the_missing_section(environment, app, monkeypatch, provider, completion_surface): diff --git a/tests/control_plane_ts/action_review_plan.test.ts b/tests/control_plane_ts/action_review_plan.test.ts index b7137aa574..ff5e0425d3 100644 --- a/tests/control_plane_ts/action_review_plan.test.ts +++ b/tests/control_plane_ts/action_review_plan.test.ts @@ -27,6 +27,25 @@ test("interrupted Goal bootstrap retries its original identity before downstream } }); +test("an interrupted Task start retries only its original saved Task", () => { + const canonicalReceipt = {todo_id: "todo-original", goal_id: "report", preview_id: "reviewed", status: "applied"}; + const created = {todo_id: "todo-original", canonical_receipt: canonicalReceipt}; + const proposal = {proposal_id: "start-task", expected_state_fingerprint: "reviewed", action_kind: "todo.create", + permission_classification: "durable_write", status: "failed", failure: {retry_safe: true}, + normalized_parameters: {goal_id: "report", start_execution: true}, + checkpoint: {steps: {todo_created: created}}}; + const plan = compileActionReviewPlan(proposal); + assert.equal(plan.reason, "task_start_retry"); + assert.equal(plan.retryOriginal, true); + assert.equal(plan.canApply, true); + for (const change of [{checkpoint: null}, {status: "gated"}, {failure: {retry_safe: false}}, + {normalized_parameters: {goal_id: "report", start_execution: false}}, + {checkpoint: {steps: {todo_created: {...created, todo_id: "todo-other"}}}}, + {expected_state_fingerprint: "another-review"}, {normalized_parameters: {goal_id: "other", start_execution: true}}]) { + assert.equal(compileActionReviewPlan({...proposal, ...change}).retryOriginal, undefined); + } +}); + test("post-commit creation projects its effects without certifying Goal completion", () => { const steps = {workspace_validated: {outcome: "workspace_validated", workspace_digest: "a".repeat(64)}, goal_bootstrapped: {outcome: "goal_bootstrapped", goal_id: "new-goal"}, From cd35c8971c9e3296aa5d14ec6b4eeb886ab5e315 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:18:04 +0800 Subject: [PATCH 012/267] fix(chat): open the accepted task execution from its confirmation Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../personal-workspace/context-drawer.tsx | 26 ++++++++++------ .../src/features/personal-workspace/i18n.tsx | 6 ++++ .../personal-workspace-page.tsx | 3 +- .../todo-completion-feedback.ts | 5 ++- .../presentation/action_review_plan.ts | 31 ++++++++++++++++--- .../action_review_plan.test.ts | 26 ++++++++++++++++ 6 files changed, 82 insertions(+), 15 deletions(-) diff --git a/apps/presentation/dashboard/src/features/personal-workspace/context-drawer.tsx b/apps/presentation/dashboard/src/features/personal-workspace/context-drawer.tsx index 49283ad4e9..592b1aacbc 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/context-drawer.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/context-drawer.tsx @@ -123,10 +123,11 @@ const RUN_ACTION_LABEL_KEYS = { retry: "drawer.recoveryRetry", } as const; -export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention, callbacks, goalNotifications = [], goals = [], inspectorExpanded = false, larkConnections = [], onClose, onToggleInspectorSize, readOnly = false, proposalReadbackUnavailable = false, onRetryProposalReadback, proposalReadbackFetching = false, todoReadbackUnavailable = false, runs = [], selection }: { +export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention, onSelectRun, callbacks, goalNotifications = [], goals = [], inspectorExpanded = false, larkConnections = [], onClose, onToggleInspectorSize, readOnly = false, proposalReadbackUnavailable = false, onRetryProposalReadback, proposalReadbackFetching = false, todoReadbackUnavailable = false, runs = [], selection }: { agents: WorkspaceAgentOption[]; attentionHistory?: WorkspaceAttention[]; onSelectAttention?: (item: WorkspaceAttention) => void; + onSelectRun?: (run: WorkspaceRun) => void; callbacks: PersonalWorkspaceCallbacks; goalNotifications?: WorkspaceGoalNotification[]; goals?: WorkspaceGoal[]; @@ -184,6 +185,10 @@ export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention const visibleProposalFields = selection.kind === "proposal" ? selection.item.fields.filter(field => !completionProofFields.includes(field)) : []; const proposalTarget = selection.kind === "proposal" ? selection.item : null; + const taskExecution = proposalTarget?.status === "applied" && !proposalReadbackUnavailable + ? proposalTarget.reviewPlan?.taskExecution : undefined; + const startedTaskRun = taskExecution && runs.find(run => run.goalId === taskExecution.goalId + && run.todoId === taskExecution.todoId && run.sessionId === taskExecution.sessionId); const proposalRequests = proposalTarget?.todoId ? attentionHistory.filter(item => item.goalId === proposalTarget.goalId && item.todoId === proposalTarget.todoId) : []; @@ -343,7 +348,8 @@ export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention && selection.item.reviewPlan?.operationFrame?.kind === "result" && selection.item.reviewPlan.operationFrame.resultKind === "unknown"; const decisionFrame = selection.kind === "proposal" ? selection.item.reviewPlan?.decisionFrame : undefined; - const title = selection.kind === "attention" ? t("drawer.titleAttention") + const title = taskExecution ? t("drawer.taskLaunchReceipt") + : selection.kind === "attention" ? t("drawer.titleAttention") : selection.kind === "todo" ? t("drawer.taskDetails") : selection.kind === "run" ? t("drawer.runDetails") : selection.kind === "output" ? t("drawer.titleOutput") @@ -661,7 +667,7 @@ export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention role="dialog" >
-

{title}

{selection.kind !== "goal" ?

{contextLabel}

: null}
+

{title}

{selection.kind !== "goal" && !taskExecution ?

{contextLabel}

: null}
{selection.kind === "todo" && onToggleInspectorSize ? : null} + : selection.item.status === "applied" && selection.item.actionKind !== "team.plan" ?

{selection.item.actionKind === "operation.execute" ? selection.item.primaryLabel : t(taskExecution ? "feedback.taskStartRequested" : "drawer.proposalApplied")}

: null} + {startedTaskRun && onSelectRun ? + : selection.item.status === "applied" && selection.item.actionKind !== "operation.execute" && selection.item.goalId ? : null} {selection.item.status === "stale" ?

{t("drawer.proposalStale")}

: null} {selection.item.status === "error" && !selection.item.reviewPlan?.retryOriginal ?
{operationUnknown ? t("proposal.operationState.submission_unknown") : selection.item.actionKind === "team.plan" || selection.item.actionKind === "goal.create" || selection.item.reviewPlan?.reason === "readback_unverified" ? t("actionReview.readback_unverified") : t("drawer.proposalApplyFailed")}{selection.item.errorMessage ? {selection.item.errorMessage} : null}{selection.item.actionKind !== "goal.create" ? {t(operationUnknown ? "actionReview.operation_reconcile_original" : selection.item.actionKind === "team.plan" ? "proposal.teamPlan.retryUnavailable" : "drawer.proposalApplyFailedHint")} : null}
: null} {selection.item.status === "rejected" ?

{t("drawer.proposalRejected")}

: null} @@ -1259,7 +1267,7 @@ export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention {selection.item.status === "gated" && selection.item.actionKind === "gate.resolve" ?
{t("drawer.gateDecisionRecheckTitle")}{t("drawer.gateDecisionRecheck")}
: selection.item.status === "gated" ?
{selection.item.actionKind === "operation.execute" ? selection.item.primaryLabel : selection.item.workspaceCandidates?.length || selection.item.gate?.kind === "agent_unavailable" ? selection.item.title : t("drawer.gateRequiresHost")}{selection.item.actionKind === "operation.execute" || selection.item.workspaceCandidates?.length || selection.item.gate?.kind === "agent_unavailable" ? selection.item.impact : t("drawer.gateRequiresHostDescription")}{selection.item.gate?.nextAction ? {selection.item.gate.nextAction} : null}
: null} {!readOnly && selection.item.workspaceCandidates?.length ?
{selection.item.workspaceCandidates.map((candidate) => )}
: null} - {!readOnly && selection.item.actionKind !== "operation.execute" && selection.item.status === "error" && (!["team.plan", "goal.create"].includes(selection.item.actionKind) || (selection.item.reviewPlan?.retryOriginal && selection.item.reviewPlan.canApply)) ? : !readOnly && selection.item.actionKind !== "operation.execute" && selection.item.status !== "gated" && !(["team.plan", "goal.create"].includes(selection.item.actionKind) && ["error", "stale"].includes(selection.item.status)) && !((selection.item.actionKind === "team.plan" || decisionFrame) && selection.item.status === "applied") ? : null} + {!readOnly && selection.item.actionKind !== "operation.execute" && selection.item.status === "error" && (!["team.plan", "goal.create"].includes(selection.item.actionKind) || (selection.item.reviewPlan?.retryOriginal && selection.item.reviewPlan.canApply)) ? : !readOnly && selection.item.actionKind !== "operation.execute" && selection.item.status !== "gated" && !(["team.plan", "goal.create"].includes(selection.item.actionKind) && ["error", "stale"].includes(selection.item.status)) && !((selection.item.actionKind === "team.plan" || decisionFrame || taskExecution) && selection.item.status === "applied") ? : null} {!readOnly && selection.item.actionKind !== "operation.execute" && !selection.item.reviewPlan?.creationProgress?.goalId && (["stale", "gated", "rejected"].includes(selection.item.status) || (selection.item.status === "ready" && selection.item.reviewPlan?.canApply === false)) ? : null} {!readOnly && selection.item.persisted !== false && selection.item.actionKind !== "operation.execute" && selection.item.actionKind !== "gate.resolve" && ["ready", "gated"].includes(selection.item.status) ?
: null} {!["applied", "applying"].includes(selection.item.status) ? : null} @@ -1314,7 +1322,7 @@ export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention
goal_id: {goalId}kind: {selection.kind} {selection.kind === "run" ? <>session_id: {selection.item.sessionId ?? t("drawer.notLinked")}turn_id: {selection.item.turnId ?? t("common.none")}adapter: {selection.item.agentId}status: {selection.item.sessionStatus ?? selection.item.status} : selection.kind === "proposal" ? <>action: {selection.item.actionKind}status: {selection.item.status} : <>schedule_id: {selection.item.scheduleId}status: {selection.item.status ?? "active"}} - {completionProofFields.map(field => {field.key}: {field.value})} + {(taskExecution ? visibleProposalFields : completionProofFields).map(field => {taskExecution ? field.label : field.key}: {field.value})}
) : null} diff --git a/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx b/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx index 0969d22ec4..12913ea4fc 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx @@ -238,6 +238,9 @@ const en = { "feedback.applying": "Running: {title}", "feedback.cancelFailed": "Cancel failed: {error}", "feedback.completed": "Completed: {title}", + "feedback.taskCreated": "Task created", + "feedback.taskStartRequested": "Task submitted for execution", + "drawer.taskLaunchReceipt": "Task receipt", "feedback.todoMarkedComplete": "Marked complete", "feedback.executionFailed": "Execution failed: {error}", "feedback.gateRequired": "Your confirmation is required: {summary}", @@ -1845,6 +1848,9 @@ const zhCN: Record = { "feedback.applying": "正在执行:{title}", "feedback.cancelFailed": "取消失败:{error}", "feedback.completed": "已完成:{title}", + "feedback.taskCreated": "任务已创建", + "feedback.taskStartRequested": "任务已提交执行", + "drawer.taskLaunchReceipt": "任务回执", "feedback.todoMarkedComplete": "已标记完成", "feedback.executionFailed": "执行失败:{error}", "feedback.gateRequired": "需要你确认:{summary}", diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx index e21aebcbfc..f5ef71ffaa 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx @@ -2829,7 +2829,8 @@ export function PersonalWorkspacePage({ else setSelection(entry.destination.selection); }} />} notice={serviceNotice} - drawer={drawerSelection ? setSelection({ kind: "attention", item })} callbacks={effectiveDrawerCallbacks} goalNotifications={model.goalNotifications ?? []} goals={workspaceGoals} inspectorExpanded={taskInspectorExpanded} larkConnections={drawerReadOnly ? [] : larkConnections} + drawer={drawerSelection ? setSelection({ kind: "attention", item })} + onSelectRun={(item) => setSelection({ kind: "run", item })} callbacks={effectiveDrawerCallbacks} goalNotifications={model.goalNotifications ?? []} goals={workspaceGoals} inspectorExpanded={taskInspectorExpanded} larkConnections={drawerReadOnly ? [] : larkConnections} todoReadbackUnavailable={drawerSelection.kind === "todo" && !workspaceGoals.some((goal) => goal.goalId === drawerSelection.item.goalId && !goal.loadState && (drawerSelection.item.detailMode !== undefined || drawerSelection.item.done || goal.agentTodos.some((todo) => todo.todoId === drawerSelection.item.todoId)))} diff --git a/apps/presentation/dashboard/src/features/personal-workspace/todo-completion-feedback.ts b/apps/presentation/dashboard/src/features/personal-workspace/todo-completion-feedback.ts index 925a54c75b..5e7315bcda 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/todo-completion-feedback.ts +++ b/apps/presentation/dashboard/src/features/personal-workspace/todo-completion-feedback.ts @@ -39,9 +39,12 @@ export function todoCompletionPreviewFeedback( /** Call only after the action owner confirms completion of this exact proposal. */ export function completedActionFeedback( - proposal: Pick, + proposal: Pick, t: WorkspaceTranslate, ): string { + if (proposal.actionKind === "todo.create") { + return t(proposal.reviewPlan?.taskExecution ? "feedback.taskStartRequested" : "feedback.taskCreated"); + } return proposal.actionKind === "todo.update" && proposal.todoOperation === "complete" ? t("feedback.todoMarkedComplete") : t("feedback.completed", { title: proposal.title }); diff --git a/loopx/control_plane/presentation/action_review_plan.ts b/loopx/control_plane/presentation/action_review_plan.ts index 708755cff4..83db5b47b4 100644 --- a/loopx/control_plane/presentation/action_review_plan.ts +++ b/loopx/control_plane/presentation/action_review_plan.ts @@ -96,6 +96,8 @@ export type ActionReviewPlan = ActionReviewIdentity & ActionReviewState & { /** Recorded creation effects, not evidence of a completed Goal or model Turn. */ creationProgress?: GoalCreationProgress; creationOutcome?: GoalCreationOutcome; + /** Accepted dispatch bound to its Task Session. It does not establish a result. */ + taskExecution?: { goalId: string; todoId: string; sessionId: string }; }; export type GoalCreationProgress = { @@ -505,6 +507,23 @@ function compileDecisionReviewFrame(proposal: Record): Decision ? DEPENDENT_EFFECTS[state as keyof typeof DEPENDENT_EFFECTS] : "unknown" }; } +function taskExecutionReadback( + proposal: Record, receipt: Record, sourceFingerprint: string, +): ActionReviewPlan["taskExecution"] { + const parameters = objectValue(proposal.normalized_parameters); + if (proposal.action_kind !== "todo.create" || parameters?.start_execution !== true + || receipt.outcome !== "task_execution_started") return undefined; + const resources = objectValue(receipt.resource_ids); + const canonical = objectValue(receipt.canonical_receipt); + const goalId = textValue(parameters.goal_id); + const todoId = textValue(resources?.todo_id); + const sessionId = textValue(resources?.session_id); + if (!goalId || !todoId || !sessionId || !textValue(resources?.turn_id) + || resources?.goal_id !== goalId || canonical?.goal_id !== goalId || canonical?.todo_id !== todoId + || canonical?.status !== "applied" || !sourceFingerprint || canonical?.preview_id !== sourceFingerprint) return undefined; + return {goalId, todoId, sessionId}; +} + /** * Compile provider-neutral presentation semantics from a typed action proposal. * This reducer owns no action authority and performs no external effects. @@ -559,10 +578,14 @@ export function compileActionReviewPlan(proposalValue: unknown, nowMs?: number): return held("repair", "readback_unverified"); } const receipt = objectValue(proposal.receipt); - return receipt?.projection_verified === true - && (proposal.action_kind !== "operation.execute" || (operationFrame?.kind === "result" && operationFrame.resultDeliveryVerified)) - ? held("completed", "readback_verified") - : held("repair", "readback_unverified"); + if (receipt?.projection_verified !== true + || (proposal.action_kind === "operation.execute" + && (operationFrame?.kind !== "result" || !operationFrame.resultDeliveryVerified))) { + return held("repair", "readback_unverified"); + } + const taskExecution = taskExecutionReadback(proposal, receipt, identity.sourceFingerprint); + const completed = held("completed", "readback_verified"); + return taskExecution ? {...completed, taskExecution} : completed; } const basis = objectValue(proposal.canonical_update_basis); const parameters = objectValue(proposal.normalized_parameters); diff --git a/tests/control_plane_ts/action_review_plan.test.ts b/tests/control_plane_ts/action_review_plan.test.ts index ff5e0425d3..68209f5c3c 100644 --- a/tests/control_plane_ts/action_review_plan.test.ts +++ b/tests/control_plane_ts/action_review_plan.test.ts @@ -46,6 +46,32 @@ test("an interrupted Task start retries only its original saved Task", () => { } }); +test("confirmed Task dispatch resolves only to the receipt's own execution Session", () => { + const resources = {goal_id: "repair", todo_id: "todo-first", session_id: "session-first", turn_id: "turn-first"}; + const canonical = {goal_id: "repair", todo_id: "todo-first", preview_id: "reviewed", status: "applied"}; + const receipt = {projection_verified: true, outcome: "task_execution_started", resource_ids: resources, canonical_receipt: canonical}; + const proposal = {proposal_id: "start-task", expected_state_fingerprint: "reviewed", action_kind: "todo.create", + status: "applied", normalized_parameters: {goal_id: "repair", start_execution: true}, receipt}; + const result = compileActionReviewPlan(proposal); + assert.equal(result.canApply, false); + assert.deepEqual(result.taskExecution, {goalId: "repair", todoId: "todo-first", sessionId: "session-first"}); + for (const change of [ + {status: "applying"}, + {normalized_parameters: {goal_id: "repair", start_execution: false}}, + {receipt: {...receipt, projection_verified: false}}, + {receipt: {...receipt, resource_ids: {...resources, goal_id: "other"}}}, + {receipt: {...receipt, resource_ids: {...resources, turn_id: ""}}}, + {receipt: {...receipt, canonical_receipt: {...canonical, todo_id: "todo-other"}}}, + {receipt: {...receipt, canonical_receipt: {...canonical, preview_id: "another-review"}}}, + ]) { + assert.equal(compileActionReviewPlan({...proposal, ...change}).taskExecution, undefined); + } + // Older saved actions can still report creation while the Session is unknown. + const legacy = compileActionReviewPlan({...proposal, receipt: {projection_verified: true, outcome: "todo_added"}}); + assert.equal(legacy.interaction, "completed"); + assert.equal(legacy.taskExecution, undefined); +}); + test("post-commit creation projects its effects without certifying Goal completion", () => { const steps = {workspace_validated: {outcome: "workspace_validated", workspace_digest: "a".repeat(64)}, goal_bootstrapped: {outcome: "goal_bootstrapped", goal_id: "new-goal"}, From a5ec7db92250ef0dcb5d6cfd6c1ce4336dc2db88 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:34:20 +0800 Subject: [PATCH 013/267] fix(chat): suppress retries superseded by an accepted request Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../native_chat/external_conversations.py | 24 +++---- .../native_chat/file_discovery.py | 42 +++++++++++++ .../collaboration/conversation_binding.ts | 6 +- .../conversation_binding.test.ts | 4 +- tests/test_chat_conversation_bindings.py | 62 +++++++++++++++++++ 5 files changed, 122 insertions(+), 16 deletions(-) diff --git a/loopx/capabilities/native_chat/external_conversations.py b/loopx/capabilities/native_chat/external_conversations.py index 8e08e0f06e..69bb458ed9 100644 --- a/loopx/capabilities/native_chat/external_conversations.py +++ b/loopx/capabilities/native_chat/external_conversations.py @@ -8,6 +8,7 @@ from collections.abc import Callable, Mapping, Sequence from copy import deepcopy +from functools import cache from pathlib import Path import hashlib import json @@ -26,7 +27,7 @@ from ...file_lock import exclusive_file_lock, try_exclusive_file_lock from . import result_review_evidence from .codex_context import checked_session_home -from .file_discovery import FileChatDiscovery +from .file_discovery import FileChatDiscovery, later_equivalent_source_input, source_control_fields # Request journals are named by their 24-hex request_ref; lock metadata and # other siblings in this directory are not requests. @@ -187,6 +188,8 @@ def _context_recovery_plan(self, row: dict[str, Any], *, _, open_literal, _ = _review_openings(str((turn.get("response") or {}).get("message") or ""), 0) preparation = None later_equivalent = None + read_source_controls = (discovery_reads.source_controls if discovery_reads is not None + else cache(self.source_controls)) image_failure = turn.get("error_code") == "runtime_error" and bool(row.get("attachments")) runtime_failure = turn.get("error_code") in {"model_auth_unavailable", "resume_failed"} if turn.get("error_code") == "manager_authorization_unavailable" or image_failure or runtime_failure: @@ -207,6 +210,10 @@ def _context_recovery_plan(self, row: dict[str, Any], *, and datetime.fromisoformat(item["created_at"]) > created for item in (discovery_reads.user_messages(session["session_id"]) if discovery_reads is not None else store.messages(session["session_id"]))) + elif turn.get("status") == "failed" and session.get("agent_id") == "codex": + # Native history cannot see a newer request that is still in the FIFO. + # The source journal can prove acceptance before native history advances. + later_equivalent = later_equivalent_source_input(row, read_source_controls) from ...chat_manager import steward_machine_defaults recovery_row = {key: value for key, value in row.items() if key != "attachments"} review_preparation = None @@ -239,15 +246,14 @@ def _context_recovery_plan(self, row: dict[str, Any], *, "attachments_present": bool(row.get("attachments")), "steering_observed": any(event.get("kind") == "turn.steered" for event in events), "stop_observed": self._source_stop_observed(row, turn, events, - source_controls=discovery_reads.source_controls if discovery_reads is not None else None), + source_controls=read_source_controls), "preparation_observation": preparation, "later_equivalent_input_present": later_equivalent, "preparation_scope_available": preparation_scope_available, "later_source_input_present": any(item.get("binding_id") == row["binding_id"] and item.get("source") == row["source"] and item.get("command") is None and datetime.fromisoformat(item["created_at"]) > datetime.fromisoformat(row["created_at"]) - for item in (discovery_reads.source_controls() if discovery_reads is not None - else self.source_controls())) if turn.get("error_code") == "antigravity_step_error" else None, + for item in read_source_controls()) if turn.get("error_code") == "antigravity_step_error" else None, "native_input": anchors[0] if len(anchors) == 1 else None, **({"retained_result_review": retained_review} if retained_review is not None else {}), **({"live_work_owner": live_work_owner} if unfinished or related_review is not None else {}), @@ -1687,7 +1693,7 @@ def pending(self) -> list[dict[str, Any]]: if _REQUEST_JOURNAL_STEM.fullmatch(path.stem)] def source_controls(self) -> list[dict[str, Any]]: - """Observe exact source-control fields without reparsing unchanged bodies. + """Observe source controls and private text digests without reparsing unchanged bodies. Recovery/stop rules remain with their original typed owner and callers. A new, replaced or modified journal is read again; pending() and exact @@ -1696,14 +1702,8 @@ def source_controls(self) -> list[dict[str, Any]]: paths = (path for path in sorted(self.root.glob("*.json")) if _REQUEST_JOURNAL_STEM.fullmatch(path.stem)) - def project(_path, row): - fields = ("binding_id", "source", "command", "created_at") - # The tolerant reader can return {} after transient IO failure. - # An incomplete read must not seed an unchanged-file observation. - return {key: row[key] for key in fields} if all(key in row for key in fields) else None - return [row for _path, row in self._source_control_discovery.rows(self.root, paths, - read=_read_json, project=project)] + read=_read_json, project=source_control_fields)] def read_request(self, request_ref: str) -> dict[str, Any]: return _read_json(self.root / f"{request_ref}.json") diff --git a/loopx/capabilities/native_chat/file_discovery.py b/loopx/capabilities/native_chat/file_discovery.py index 625ee8ad6b..3f63a6c019 100644 --- a/loopx/capabilities/native_chat/file_discovery.py +++ b/loopx/capabilities/native_chat/file_discovery.py @@ -3,12 +3,54 @@ from collections.abc import Callable, Iterable, Iterator from copy import deepcopy +from datetime import datetime +import hashlib from pathlib import Path import sys from threading import Lock from typing import Any +def _ordinary_request_text_digest(row: dict[str, Any]) -> str | None: + """Match only canonical, admitted private text; omit the body from discovery.""" + message = row.get("message") + source = row.get("source") + if (row.get("status") != "accepted" or row.get("command") is not None + or row.get("capture_origin") is not None or row.get("agent_target") is not None + or row.get("attachments") not in (None, []) or not isinstance(message, str) or not message + or not row.get("session_id") or not row.get("turn_id") + or not isinstance(source, dict) or source.get("private_human_message") is not True): + return None + return hashlib.sha256(message.encode("utf-8", errors="surrogatepass")).hexdigest() + + +def source_control_fields(_path: Path, row: dict[str, Any]) -> dict[str, Any] | None: + """Keep exact source controls and a compact fingerprint of admitted text.""" + fields = ("binding_id", "source", "command", "created_at") + # A tolerant reader can return an incomplete journal after a transient + # failure. Do not cache that read as an unchanged-file observation. + if not all(key in row for key in fields): + return None + result = {key: row[key] for key in fields} + digest = _ordinary_request_text_digest(row) + if digest is not None: + result.update(text_sha256=digest, session_id=row["session_id"]) + return result + + +def later_equivalent_source_input(row: dict[str, Any], + read_controls: Callable[[], list[dict[str, Any]]]) -> bool | None: + """Check canonical acceptance in the same source and Session, even before dispatch.""" + digest = _ordinary_request_text_digest(row) + if digest is None: + return None + created = datetime.fromisoformat(row["created_at"]) + return any(item.get("text_sha256") == digest + and item["binding_id"] == row["binding_id"] and item["source"] == row["source"] + and item["session_id"] == row["session_id"] and datetime.fromisoformat(item["created_at"]) > created + for item in read_controls()) + + def file_change_fingerprint(path: Path) -> tuple[int, ...] | None: """A local mutation token, or no reuse when the platform lacks one.""" # Windows stat ctime can be creation time, not a mutation stamp. Keep diff --git a/loopx/control_plane/collaboration/conversation_binding.ts b/loopx/control_plane/collaboration/conversation_binding.ts index b951c2324e..7c3148aa2d 100644 --- a/loopx/control_plane/collaboration/conversation_binding.ts +++ b/loopx/control_plane/collaboration/conversation_binding.ts @@ -948,9 +948,9 @@ function planContextRecovery(params: JsonObject, row: JsonObject, current: JsonO admitted_turn_id: sessionIdentity(admitted.turn_id, "admitted recovery Turn"), session_id: current.session_id, native_thread_ids: []}; } - // A later source stop vetoes new execution. Correlation repair for an - // already admitted Turn above remains read-only and keeps its exact identity. - if (recovery.stop_observed === true) return no; + // A source stop or a newer admitted equivalent vetoes new execution. + // Correlation repair above remains read-only and keeps its exact identity. + if (recovery.stop_observed === true || recovery.later_equivalent_input_present === true) return no; const routing = planRoutingRecovery(recovery, row, current, failed, previous, version); if (routing !== null) return routing; if (!steward) return no; diff --git a/tests/control_plane_ts/conversation_binding.test.ts b/tests/control_plane_ts/conversation_binding.test.ts index 058aeafbd3..1be4b07419 100644 --- a/tests/control_plane_ts/conversation_binding.test.ts +++ b/tests/control_plane_ts/conversation_binding.test.ts @@ -1349,7 +1349,8 @@ for (const kind of ["project", "steward"]) for (const retryKind of ["routing", " context_recovery: {...packet.context_recovery, native_observation: null, admitted_turn: admitted}}; assert.equal(planBoundConversationRequest(crash).admitted_turn_id, "already-admitted"); assert.equal(planBoundConversationRequest({...crash, - context_recovery: {...crash.context_recovery, stop_observed: true}}).admitted_turn_id, "already-admitted"); + context_recovery: {...crash.context_recovery, stop_observed: true, + later_equivalent_input_present: true}}).admitted_turn_id, "already-admitted"); for (const change of [{session_id: "foreign"}, {client_turn_id: "foreign"}, {message: "another request"}, {attachments: [{type: "image"}]}]) { assert.equal(planBoundConversationRequest({...crash, @@ -1362,6 +1363,7 @@ for (const kind of ["project", "steward"]) for (const retryKind of ["routing", " assert.equal(planBoundConversationRequest({...next, request: {...next.request, context_recovery: {...reservation, [attemptKey]: 3}}}).recovery_candidate, false); for (const change of [{source_verified: false}, {steering_observed: true}, {stop_observed: true}, + {later_equivalent_input_present: true}, {observed_at: "2026-01-01T00:00:59Z"}, {observed_at: "invalid"}, {native_observation: {...observed, ...(retryKind === "quota" ? {only_original_user_item: false} : {only_pre_execution_items: false})}}, diff --git a/tests/test_chat_conversation_bindings.py b/tests/test_chat_conversation_bindings.py index b824abebb4..133cd3a7fd 100644 --- a/tests/test_chat_conversation_bindings.py +++ b/tests/test_chat_conversation_bindings.py @@ -186,6 +186,68 @@ def now(cls, tz=None): runtime.close() +def test_original_retry_yields_to_a_newer_equivalent_lark_request(ordinary, monkeypatch): + from datetime import datetime, timedelta, timezone + + from loopx.capabilities.native_chat import external_conversations + from loopx.capabilities.native_chat.codex_adapter import CodexAppServerAdapter + from test_lark_private_conversations import connect + + store, runtime, provider, transport = connect(ordinary) + fake = ordinary[-2] + fake.write_text(fake.read_text().replace('"codexErrorInfo": "cyberPolicy"', + '"codexErrorInfo": "usageLimitExceeded"').replace( + ' print(json.dumps({"method": "item/agentMessage/delta", "params": {"threadId": "durable-thread", "turnId": active_turn, "delta": "Partial answer."}}), flush=True)\n', '')) + + def admit(profile, name, text): + event = provider.event(profile, name, text) + transport.admit(profile, event) + provider_ref = transport._binding(profile)["provider_ref"] + return transport.core.read_request(identity_ref(provider_ref, event["message_id"])) + + text = "typed terminal failure" + try: + original = admit("notes-app", "quota-original", text) + sid, tid = original["session_id"], original["turn_id"] + failed = runtime.wait_for_turn(session_id=sid, turn_id=tid, timeout_sec=10) + assert failed["error_code"] == "usage_limit_exceeded" and failed["delta_count"] == 0 + transport.reconcile() + original = transport.core.read_request(original["request_ref"]) + failed_bytes = store._turn_path(sid, tid).read_bytes() + lark_failure_writes = list(provider.writes) + assert original["delivery_verified"] and lark_failure_writes + + # Leave new source inputs in the real FIFO before they become native Turns. + monkeypatch.setattr(runtime, "resume_session_queue", lambda **_: None) + unrelated = admit("steward-app", "other-source", text) + distinct = admit("notes-app", "different-text", "Another request") + assert unrelated["session_id"] != sid and distinct["session_id"] == sid + + class AfterBackoff(datetime): + @classmethod + def now(cls, tz=None): + return datetime.now(timezone.utc) + timedelta(seconds=61) + + monkeypatch.setattr(external_conversations, "datetime", AfterBackoff) + monkeypatch.setattr(CodexAppServerAdapter, "failed_request_observation", staticmethod(lambda **_: { + "thread_id": "durable-thread", "turn_id": failed["upstream_turn_id"], + "only_original_user_item": True, "codex_error_info": "usageLimitExceeded", + "quota_available": True, "quota_observed_at": AfterBackoff.now().timestamp() - 1})) + assert transport.core.context_recovery_candidate(original["request_ref"]) + + resent = admit("notes-app", "same-source-resend", text) + assert resent["session_id"] == sid and resent["status"] == "accepted" + assert {row["turn_id"] for row in store.queued_turns(sid)} == {distinct["turn_id"], resent["turn_id"]} + transport.reconcile_request(transport.root / f"{original['request_ref']}.json") + assert transport.core.read_request(original["request_ref"]) == original + assert not transport.core.context_recovery_candidate(original["request_ref"]) + assert {row["turn_id"] for row in store.queued_turns(sid)} == {distinct["turn_id"], resent["turn_id"]} + assert store._turn_path(sid, tid).read_bytes() == failed_bytes + assert provider.writes == lark_failure_writes + finally: + runtime.close() + + @pytest.fixture def recorded_return_continuation(private_return, monkeypatch): # noqa: F811 """Use the real admission/recovery owners; only model/provider IO is fake.""" From 70f0930b1c9fcbe3980e1c29e52bf3c21ed858f2 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:08:38 +0800 Subject: [PATCH 014/267] test(benchmark): seal expired fractional phase deadlines Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- benchmark/tests/test_checkpoint_controller.py | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/benchmark/tests/test_checkpoint_controller.py b/benchmark/tests/test_checkpoint_controller.py index daec8e28df..0b18557e4e 100644 --- a/benchmark/tests/test_checkpoint_controller.py +++ b/benchmark/tests/test_checkpoint_controller.py @@ -638,7 +638,7 @@ def lost(controller, route, body, timeout): assert calls == ["/api/v1/checkpoint/restore"] -@pytest.mark.parametrize("raw", [b"", b"nan\n", b"1.5.0\n", b"0.0\n", b"9007199254740992\n"]) +@pytest.mark.parametrize("raw", [b"", b"nan\n", b"1.50\n", b"1.5.0\n", b"0.0\n", b"9007199254740992\n"]) def test_invalid_host_epoch_cannot_create_native_point(controllers, tmp_path, raw): source, destination, first, second, files, grades = controllers token = register(source, first) @@ -652,6 +652,24 @@ def test_invalid_host_epoch_cannot_create_native_point(controllers, tmp_path, ra assert destination.state.judge.tokens == {} and grades == [] +def test_expired_fractional_epoch_seals_without_work_allowance(controllers, tmp_path): + source, destination, first, _, files, grades = controllers + token = register(source, first) + phase = tmp_path / "source-phase" + phase.write_bytes(b"1.5\n") + unit = tmp_path / "unit" + point = transport.capture(files[0], token=token, capture_id="f" * 32, + destination=unit, phase_deadline_file=phase) + manifest = json.loads((unit / "checkpoint.json").read_bytes()) + budget = manifest["point_metadata"]["host_phase_budget"] + assert budget["source_deadline_epoch"] == 1.5 and budget["remaining_seconds"] == 0 + assert (unit / "components" / "host_phase_deadline").read_bytes() == phase.read_bytes() + with pytest.raises(ValueError, match="no work allowance"): + transport.restore(files[1], root=unit, expected_sha256=point["sha256"], run_id="expired", + phase_deadline_file=tmp_path / "destination-phase") + assert destination.state.judge.tokens == {} and grades == [] + + def test_native_only_point_cannot_acquire_a_later_host_budget(controllers, tmp_path): source, destination, first, second, files, grades = controllers token = register(source, first) From f86e81d91ae06e4021f5b368d0cb9059d418bdf5 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:34:29 +0800 Subject: [PATCH 015/267] Honor the Core receipt for interrupted bot resends Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../native_chat/external_conversations.py | 4 +- .../native_chat/file_discovery.py | 49 ++++++++++++++----- tests/test_chat_conversation_bindings.py | 48 ++++++++++++++++-- 3 files changed, 82 insertions(+), 19 deletions(-) diff --git a/loopx/capabilities/native_chat/external_conversations.py b/loopx/capabilities/native_chat/external_conversations.py index 69bb458ed9..3ab8a0bae6 100644 --- a/loopx/capabilities/native_chat/external_conversations.py +++ b/loopx/capabilities/native_chat/external_conversations.py @@ -212,8 +212,8 @@ def _context_recovery_plan(self, row: dict[str, Any], *, if discovery_reads is not None else store.messages(session["session_id"]))) elif turn.get("status") == "failed" and session.get("agent_id") == "codex": # Native history cannot see a newer request that is still in the FIFO. - # The source journal can prove acceptance before native history advances. - later_equivalent = later_equivalent_source_input(row, read_source_controls) + # Read the Core receipt when the source journal has not settled yet. + later_equivalent = later_equivalent_source_input(row, read_source_controls, store.turn_for_client) from ...chat_manager import steward_machine_defaults recovery_row = {key: value for key, value in row.items() if key != "attachments"} review_preparation = None diff --git a/loopx/capabilities/native_chat/file_discovery.py b/loopx/capabilities/native_chat/file_discovery.py index 3f63a6c019..7fd1a5a27f 100644 --- a/loopx/capabilities/native_chat/file_discovery.py +++ b/loopx/capabilities/native_chat/file_discovery.py @@ -12,20 +12,21 @@ def _ordinary_request_text_digest(row: dict[str, Any]) -> str | None: - """Match only canonical, admitted private text; omit the body from discovery.""" + """Match ordinary private text without retaining its body in discovery.""" message = row.get("message") source = row.get("source") - if (row.get("status") != "accepted" or row.get("command") is not None + status = row.get("status") + if (status not in ("accepted", "prepared") or row.get("command") is not None or row.get("capture_origin") is not None or row.get("agent_target") is not None or row.get("attachments") not in (None, []) or not isinstance(message, str) or not message - or not row.get("session_id") or not row.get("turn_id") + or not row.get("session_id") or not isinstance(source, dict) or source.get("private_human_message") is not True): return None return hashlib.sha256(message.encode("utf-8", errors="surrogatepass")).hexdigest() -def source_control_fields(_path: Path, row: dict[str, Any]) -> dict[str, Any] | None: - """Keep exact source controls and a compact fingerprint of admitted text.""" +def source_control_fields(path: Path, row: dict[str, Any]) -> dict[str, Any] | None: + """Keep exact source controls and the metadata needed to prove admission.""" fields = ("binding_id", "source", "command", "created_at") # A tolerant reader can return an incomplete journal after a transient # failure. Do not cache that read as an unchanged-file observation. @@ -33,22 +34,46 @@ def source_control_fields(_path: Path, row: dict[str, Any]) -> dict[str, Any] | return None result = {key: row[key] for key in fields} digest = _ordinary_request_text_digest(row) - if digest is not None: + if digest is None: + return result + if row["status"] == "accepted" and row.get("turn_id"): result.update(text_sha256=digest, session_id=row["session_id"]) + elif (row["status"] == "prepared" and row.get("turn_id") is None + and row.get("request_ref") == path.stem): + result.update(text_sha256=digest, session_id=row["session_id"], + prepared_client_turn_id=f"external-{path.stem}", origin=row.get("origin", "lark")) return result -def later_equivalent_source_input(row: dict[str, Any], - read_controls: Callable[[], list[dict[str, Any]]]) -> bool | None: +def later_equivalent_source_input( + row: dict[str, Any], + read_controls: Callable[[], list[dict[str, Any]]], + read_turn_for_client: Callable[[str, str], dict[str, Any] | None], +) -> bool | None: """Check canonical acceptance in the same source and Session, even before dispatch.""" + if row.get("status") != "accepted" or not row.get("turn_id"): + return None digest = _ordinary_request_text_digest(row) if digest is None: return None created = datetime.fromisoformat(row["created_at"]) - return any(item.get("text_sha256") == digest - and item["binding_id"] == row["binding_id"] and item["source"] == row["source"] - and item["session_id"] == row["session_id"] and datetime.fromisoformat(item["created_at"]) > created - for item in read_controls()) + for item in read_controls(): + if (item.get("text_sha256") != digest or item["binding_id"] != row["binding_id"] + or item["source"] != row["source"] or item["session_id"] != row["session_id"] + or datetime.fromisoformat(item["created_at"]) <= created): + continue + client_turn_id = item.get("prepared_client_turn_id") + if client_turn_id is None: + return True + # Core owns the durable queue receipt even if the outer journal has + # not finished its write. Never treat preparation alone as admission. + turn = read_turn_for_client(item["session_id"], client_turn_id) + if (turn is not None and turn.get("origin") == item["origin"] + and turn.get("_queue_original_message", turn.get("message")) == row["message"] + and turn.get("attachments") in (None, []) + and turn.get("external_agent_target") is None and turn.get("collaboration_request") is None): + return True + return False def file_change_fingerprint(path: Path) -> tuple[int, ...] | None: diff --git a/tests/test_chat_conversation_bindings.py b/tests/test_chat_conversation_bindings.py index 133cd3a7fd..97ee9cf4d6 100644 --- a/tests/test_chat_conversation_bindings.py +++ b/tests/test_chat_conversation_bindings.py @@ -186,7 +186,8 @@ def now(cls, tz=None): runtime.close() -def test_original_retry_yields_to_a_newer_equivalent_lark_request(ordinary, monkeypatch): +@pytest.mark.parametrize("lost_receipt", [False, True], ids=["accepted", "lost-outer-receipt"]) +def test_original_retry_yields_to_a_newer_equivalent_lark_request(ordinary, monkeypatch, lost_receipt): from datetime import datetime, timedelta, timezone from loopx.capabilities.native_chat import external_conversations @@ -235,15 +236,52 @@ def now(cls, tz=None): "quota_available": True, "quota_observed_at": AfterBackoff.now().timestamp() - 1})) assert transport.core.context_recovery_candidate(original["request_ref"]) - resent = admit("notes-app", "same-source-resend", text) - assert resent["session_id"] == sid and resent["status"] == "accepted" - assert {row["turn_id"] for row in store.queued_turns(sid)} == {distinct["turn_id"], resent["turn_id"]} + resend = provider.event("notes-app", "same-source-resend", text) + resend_ref = identity_ref(transport._binding("notes-app")["provider_ref"], resend["message_id"]) + resend_client = f"external-{resend_ref}" + if lost_receipt: + def fail_before_queue(**_): + raise OSError("Core queue unavailable") + + with monkeypatch.context() as fault: + fault.setattr(runtime, "enqueue_turn", fail_before_queue) + with pytest.raises(OSError, match="Core queue unavailable"): + transport.admit("notes-app", resend) + assert transport.core.read_request(resend_ref)["session_id"] == sid + assert store.turn_for_client(sid, resend_client) is None + assert transport.core.context_recovery_candidate(original["request_ref"]) + + write = external_conversations._atomic_write_json + + def lose_outer_receipt(path, row, **kwargs): + if path.stem == resend_ref and row.get("status") == "accepted": + raise OSError("outer receipt unavailable") + write(path, row, **kwargs) + + with monkeypatch.context() as fault: + fault.setattr(external_conversations, "_atomic_write_json", lose_outer_receipt) + with pytest.raises(OSError, match="outer receipt unavailable"): + transport.admit("notes-app", resend) + prepared = transport.core.read_request(resend_ref) + assert prepared["status"] == "prepared" and prepared["turn_id"] is None + else: + transport.admit("notes-app", resend) + assert transport.core.read_request(resend_ref)["status"] == "accepted" + + resent = store.turn_for_client(sid, resend_client) + assert resent["session_id"] == sid and resent["status"] == "queued" + assert any(item["kind"] == "turn.queued" for item in store.events_after(sid, resent["turn_id"], None)) + expected_queue = {distinct["turn_id"], resent["turn_id"]} + assert {row["turn_id"] for row in store.queued_turns(sid)} == expected_queue transport.reconcile_request(transport.root / f"{original['request_ref']}.json") assert transport.core.read_request(original["request_ref"]) == original assert not transport.core.context_recovery_candidate(original["request_ref"]) - assert {row["turn_id"] for row in store.queued_turns(sid)} == {distinct["turn_id"], resent["turn_id"]} + assert {row["turn_id"] for row in store.queued_turns(sid)} == expected_queue assert store._turn_path(sid, tid).read_bytes() == failed_bytes assert provider.writes == lark_failure_writes + transport.admit("notes-app", resend) + assert transport.core.read_request(resend_ref)["turn_id"] == resent["turn_id"] + assert {row["turn_id"] for row in store.queued_turns(sid)} == expected_queue finally: runtime.close() From 13bd68bac660ba70f37765b6695a971dc4d11757 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 13 Nov 2026 08:14:10 +0800 Subject: [PATCH 016/267] Read the exact execution from a confirmed task receipt Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- apps/presentation/dashboard/src/data/chat.ts | 15 +++++ .../personal-workspace/channel-timeline.tsx | 6 +- .../confirmed-task-result.tsx | 62 +++++++++++++++++++ .../personal-workspace/context-drawer.tsx | 16 +++-- .../src/features/personal-workspace/i18n.tsx | 16 +++++ .../personal-workspace-contract.test.mjs | 2 - .../personal-workspace-page.tsx | 14 +++-- .../personal-workspace/personal-workspace.css | 2 + .../capabilities/manager_context/roundtrip.py | 14 ++++- .../presentation/action_review_plan.ts | 9 +-- .../action_review_plan.test.ts | 19 +++++- tests/test_chat_conversation_bindings.py | 13 +++- 12 files changed, 160 insertions(+), 28 deletions(-) create mode 100644 apps/presentation/dashboard/src/features/personal-workspace/confirmed-task-result.tsx diff --git a/apps/presentation/dashboard/src/data/chat.ts b/apps/presentation/dashboard/src/data/chat.ts index 1111dd55d9..3aa0f37c87 100644 --- a/apps/presentation/dashboard/src/data/chat.ts +++ b/apps/presentation/dashboard/src/data/chat.ts @@ -959,6 +959,7 @@ export type ChatSessionSnapshot = { session: ChatSessionSummary; messages: ChatVisibleMessage[]; active_turn: Record | null; + selected_turn?: {turn_id: string; status: string; terminal: boolean}; // Derived from one complete, verified read. Never persisted or an authority grant. verifiedHistoryBasis?: string; message_page?: { @@ -1092,6 +1093,20 @@ export async function fetchChatSessionHistory(session: ChatSessionSummary | stri return readChatSessionHistory(session, signal, turnId); } +export async function fetchConfirmedTaskTurn( + {goalId, todoId, sessionId, turnId}: {goalId: string; todoId: string; sessionId: string; turnId: string}, + signal: AbortSignal, +) { + const history = await fetchChatSessionHistory(sessionId, signal, turnId); + const turn = history.selected_turn; + if (history.session.goal_id !== goalId || history.session.channel_id !== `task.${todoId}` + || turn?.turn_id !== turnId || typeof turn.status !== "string" || !turn.status + || typeof turn.terminal !== "boolean") { + throw new Error("The original Task Turn could not be verified."); + } + return {turn, messages: history.messages}; +} + async function readChatSessionHistory(session: ChatSessionSummary | string, signal?: AbortSignal, turnId?: string, progress?: ChatHistoryReadProgress, checkpoint?: (progress: ChatHistoryReadProgress) => void): Promise { const sessionId = typeof session === "string" ? session : session.session_id; diff --git a/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx b/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx index 3fe4f5dda2..144f4bec37 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx @@ -185,14 +185,18 @@ export function ChannelTimeline({ } if (item.kind === "proposal") { const appliedTeamPlan = item.proposal.actionKind === "team.plan" && item.proposal.status === "applied"; + const taskReceipt = item.proposal.reviewPlan?.taskExecution; const pendingOperation = item.proposal.reviewPlan?.operationFrame?.kind === "pending"; return ( {showManagerTeamResults && onOpenGoalEvidence && appliedTeamPlan && item.proposal.goalId && item.proposal.teamPlanTodoIds?.length ? diff --git a/apps/presentation/dashboard/src/features/personal-workspace/confirmed-task-result.tsx b/apps/presentation/dashboard/src/features/personal-workspace/confirmed-task-result.tsx new file mode 100644 index 0000000000..e9797bb6b4 --- /dev/null +++ b/apps/presentation/dashboard/src/features/personal-workspace/confirmed-task-result.tsx @@ -0,0 +1,62 @@ +import {useEffect, useId, useState} from "react"; +import {fetchConfirmedTaskTurn} from "../../data/chat"; +import {visibleAgentMessage} from "./answer-text"; +import {localizedSessionStatus, useWorkspaceI18n} from "./i18n"; +import {MarkdownText} from "./markdown"; + +type TaskReceipt = Parameters[0]; +type Result = Awaited>; + +export function ConfirmedTaskResult({receipt}: {receipt: TaskReceipt}) { + const {t} = useWorkspaceI18n(); + const panelId = useId(); + const [open, setOpen] = useState(false); + const [read, setRead] = useState(null); + const [retry, setRetry] = useState(0); + const {goalId, todoId, sessionId, turnId} = receipt; + useEffect(() => { + if (!open) return; + const controller = new AbortController(); + let next: ReturnType | undefined; + async function load() { + try { + const result = await fetchConfirmedTaskTurn({goalId, todoId, sessionId, turnId}, controller.signal); + if (controller.signal.aborted) return; + setRead(result); + if (!result.turn.terminal) next = setTimeout(load, 2_500); + } catch { + if (!controller.signal.aborted) setRead(new Error()); + } + } + void load(); + return () => { controller.abort(); clearTimeout(next); }; + }, [open, goalId, todoId, sessionId, turnId, retry]); + + const result = read && !(read instanceof Error) ? read : null; + const messages = result?.turn.terminal + ? result.messages.filter(message => ["agent", "assistant", "error"].includes(message.role)) + .map(message => ({...message, text: visibleAgentMessage(message.text)})).filter(message => message.text) + : []; + return <> + + + ; +} diff --git a/apps/presentation/dashboard/src/features/personal-workspace/context-drawer.tsx b/apps/presentation/dashboard/src/features/personal-workspace/context-drawer.tsx index 592b1aacbc..407073ec4e 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/context-drawer.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/context-drawer.tsx @@ -46,6 +46,7 @@ import { localizedGoalState, localizedSessionStatus, useWorkspaceI18n } from "./ import { formatCostUsd, formatDurationMs, formatTokenCount, formatUsageValue, hasGoalUsage } from "./personal-workspace-model"; import { TeamPlanResult } from "./team-plan-result"; import { TaskRequest, taskRequestIdentity, type TaskRequestRead } from "./task-request"; +import { ConfirmedTaskResult } from "./confirmed-task-result"; import { parseTodoResumeCondition, todoResumeTransition } from "./todo-resume-condition"; import { MarkdownText } from "./markdown"; import { formatMonitorDate } from "./monitor-readback"; @@ -123,11 +124,10 @@ const RUN_ACTION_LABEL_KEYS = { retry: "drawer.recoveryRetry", } as const; -export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention, onSelectRun, callbacks, goalNotifications = [], goals = [], inspectorExpanded = false, larkConnections = [], onClose, onToggleInspectorSize, readOnly = false, proposalReadbackUnavailable = false, onRetryProposalReadback, proposalReadbackFetching = false, todoReadbackUnavailable = false, runs = [], selection }: { +export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention, callbacks, goalNotifications = [], goals = [], inspectorExpanded = false, larkConnections = [], onClose, onToggleInspectorSize, readOnly = false, proposalReadbackUnavailable = false, onRetryProposalReadback, proposalReadbackFetching = false, todoReadbackUnavailable = false, runs = [], selection }: { agents: WorkspaceAgentOption[]; attentionHistory?: WorkspaceAttention[]; onSelectAttention?: (item: WorkspaceAttention) => void; - onSelectRun?: (run: WorkspaceRun) => void; callbacks: PersonalWorkspaceCallbacks; goalNotifications?: WorkspaceGoalNotification[]; goals?: WorkspaceGoal[]; @@ -185,10 +185,8 @@ export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention const visibleProposalFields = selection.kind === "proposal" ? selection.item.fields.filter(field => !completionProofFields.includes(field)) : []; const proposalTarget = selection.kind === "proposal" ? selection.item : null; - const taskExecution = proposalTarget?.status === "applied" && !proposalReadbackUnavailable + const taskExecution = proposalTarget?.status === "applied" && !proposalReadbackUnavailable && !readOnly ? proposalTarget.reviewPlan?.taskExecution : undefined; - const startedTaskRun = taskExecution && runs.find(run => run.goalId === taskExecution.goalId - && run.todoId === taskExecution.todoId && run.sessionId === taskExecution.sessionId); const proposalRequests = proposalTarget?.todoId ? attentionHistory.filter(item => item.goalId === proposalTarget.goalId && item.todoId === proposalTarget.todoId) : []; @@ -1225,9 +1223,9 @@ export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention ?

{t("proposal.readbackUnavailable")}

{onRetryProposalReadback ? : null}
: null} {selection.item.actionKind === "team.plan" && selection.item.status === "applied" ? :
- {selection.item.reviewPlan?.operationFrame?.kind === "pending" + {!taskExecution ? {selection.item.reviewPlan?.operationFrame?.kind === "pending" ? t(`proposal.kind.${selection.item.actionKind}`) - : `${t(`proposal.kind.${selection.item.actionKind}`)} · ${t(`proposal.status.${selection.item.status}`)}`} + : `${t(`proposal.kind.${selection.item.actionKind}`)} · ${t(`proposal.status.${selection.item.status}`)}`} : null} {decisionFrame ? {t(`proposal.decision.${decisionFrame.decision}`)} : null}

{selection.item.title}

{selection.item.todoId && (selection.item.actionKind === "gate.resolve" || selection.item.actionKind === "todo.update") @@ -1254,8 +1252,8 @@ export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention
: null} {selection.item.status === "applied" && decisionFrame ?

{t(`proposal.applied.gate.${decisionFrame.decision}`)}{decisionFrame.dependentEffect ? {t(`proposal.dependentEffect.${decisionFrame.dependentEffect}`)} : null}

: selection.item.status === "applied" && selection.item.actionKind !== "team.plan" ?

{selection.item.actionKind === "operation.execute" ? selection.item.primaryLabel : t(taskExecution ? "feedback.taskStartRequested" : "drawer.proposalApplied")}

: null} - {startedTaskRun && onSelectRun ? + {taskExecution ? : selection.item.status === "applied" && selection.item.actionKind !== "operation.execute" && selection.item.goalId ? : null} {selection.item.status === "stale" ?

{t("drawer.proposalStale")}

: null} {selection.item.status === "error" && !selection.item.reviewPlan?.retryOriginal ?
{operationUnknown ? t("proposal.operationState.submission_unknown") : selection.item.actionKind === "team.plan" || selection.item.actionKind === "goal.create" || selection.item.reviewPlan?.reason === "readback_unverified" ? t("actionReview.readback_unverified") : t("drawer.proposalApplyFailed")}{selection.item.errorMessage ? {selection.item.errorMessage} : null}{selection.item.actionKind !== "goal.create" ? {t(operationUnknown ? "actionReview.operation_reconcile_original" : selection.item.actionKind === "team.plan" ? "proposal.teamPlan.retryUnavailable" : "drawer.proposalApplyFailedHint")} : null}
: null} diff --git a/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx b/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx index 12913ea4fc..435030e722 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx @@ -241,6 +241,14 @@ const en = { "feedback.taskCreated": "Task created", "feedback.taskStartRequested": "Task submitted for execution", "drawer.taskLaunchReceipt": "Task receipt", + "drawer.taskReceiptResult": "Result of this execution", + "drawer.taskReceiptShowResult": "View this result", + "drawer.taskReceiptHideResult": "Hide result", + "drawer.taskReceiptReading": "Reading the original execution…", + "drawer.taskReceiptPending": "Waiting for a result from this execution.", + "drawer.taskReceiptMissing": "The original execution is temporarily unavailable.", + "drawer.taskReceiptNoResult": "This execution ended without returning a result.", + "drawer.taskReceiptTimedOut": "Timed out", "feedback.todoMarkedComplete": "Marked complete", "feedback.executionFailed": "Execution failed: {error}", "feedback.gateRequired": "Your confirmation is required: {summary}", @@ -1851,6 +1859,14 @@ const zhCN: Record = { "feedback.taskCreated": "任务已创建", "feedback.taskStartRequested": "任务已提交执行", "drawer.taskLaunchReceipt": "任务回执", + "drawer.taskReceiptResult": "本次执行结果", + "drawer.taskReceiptShowResult": "查看本次结果", + "drawer.taskReceiptHideResult": "收起结果", + "drawer.taskReceiptReading": "正在读取原执行…", + "drawer.taskReceiptPending": "等待本次执行返回结果。", + "drawer.taskReceiptMissing": "暂时读不到原执行记录。", + "drawer.taskReceiptNoResult": "本次执行已结束,未返回结果。", + "drawer.taskReceiptTimedOut": "已超时", "feedback.todoMarkedComplete": "已标记完成", "feedback.executionFailed": "执行失败:{error}", "feedback.gateRequired": "需要你确认:{summary}", diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-contract.test.mjs b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-contract.test.mjs index ae3026be62..b232a83b46 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-contract.test.mjs +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-contract.test.mjs @@ -48,7 +48,6 @@ const dashboard = source("../../views/dashboard-page.tsx"); const tasks = source("./goal-tasks-view.tsx"); const status = source("../../data/status.ts"); const chatData = source("../../data/chat.ts"); -const actionReview = source("../../../../../../loopx/control_plane/presentation/action_review_plan.ts"); assert.match(model, /kind: "todo"/, "Todo has its own drawer selection"); for (const field of ["dependencies", "nextTransition", "ownerLabel", "todoId", "taskClass"]) { @@ -132,7 +131,6 @@ for (const state of ["delivered", "verification_required", "explicit_unverified" assert.match(returnDelivery, new RegExp(state), `Return delivery renders ${state}`); } assert.doesNotMatch(returnDelivery, /message_ref|provider_receipt|intent_digest/, "Provider-private locator facts never enter the return status badge"); -assert.match(actionReview, /proposal\.action_kind !== "operation\.execute" \|\| \(operationFrame\?\.kind === "result" && operationFrame\.resultDeliveryVerified\)/, "An operation is not complete in the Dashboard until the current result delivery is verified"); assert.match(page, /reviewPlan\.operationFrame/, "Dashboard operation details consume the shared TS review frame"); assert.match(page, /const restoreable = stored[\s\S]*\|\| proposal\.action_kind === "operation\.execute"/, "Canonical operation readback includes terminal and cancelled rows instead of retaining a stale gated card"); assert.match(page, /proposal\.action_kind !== "operation\.execute"[\s\S]*reviewPlan\.interaction !== "completed"/, "Pending operation result-card readback remains visible instead of becoming a generic apply error"); diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx index f5ef71ffaa..affc210cc9 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx @@ -125,7 +125,7 @@ function dedupeProposals(proposals: WorkspaceActionPreview[]): WorkspaceActionPr const latest = new Map(); proposals.forEach((proposal) => { const subject = proposal.fields.find((field) => field.key === "todo_id")?.value ?? ""; - const key = proposal.actionKind === "operation.execute" ? proposal.previewId + const key = proposal.actionKind === "operation.execute" || proposal.reviewPlan?.taskExecution ? proposal.previewId : [proposal.actionKind, proposal.goalId ?? "", subject, proposal.title].join(":"); // Two records can describe the same draft; keep the newest by its stored // time rather than whichever one this list happened to end with, since a @@ -1714,10 +1714,15 @@ export function PersonalWorkspacePage({ .filter((proposal) => ["preview_ready", "gated", "deferred", "applying"].includes(proposal.status) || compileActionReviewPlan(proposal).retryOriginal === true || (proposal.action_kind === "team.plan" && proposal.status === "applied") + || (proposal.action_kind === "todo.create" && proposal.status === "applied") // Terminal operations replace cached gated cards, including in an // already-open drawer. They never authorize local execution. || proposal.action_kind === "operation.execute") - .map((proposal) => workspaceProposal(proposal, t)); + .map((proposal) => workspaceProposal(proposal, t)) + // Keep a launch receipt reachable after a reload; plain creation does + // not have a Turn to revisit. + .filter(proposal => proposal.actionKind !== "todo.create" || proposal.status !== "applied" + || Boolean(proposal.reviewPlan?.taskExecution)); const restored = Object.fromEntries(restoreable.map((proposal) => [proposal.previewId, proposal])); const previousIds = restoredProposalIdsRef.current; restoredProposalIdsRef.current = new Set(Object.keys(restored)); @@ -2830,7 +2835,7 @@ export function PersonalWorkspacePage({ }} />} notice={serviceNotice} drawer={drawerSelection ? setSelection({ kind: "attention", item })} - onSelectRun={(item) => setSelection({ kind: "run", item })} callbacks={effectiveDrawerCallbacks} goalNotifications={model.goalNotifications ?? []} goals={workspaceGoals} inspectorExpanded={taskInspectorExpanded} larkConnections={drawerReadOnly ? [] : larkConnections} + callbacks={effectiveDrawerCallbacks} goalNotifications={model.goalNotifications ?? []} goals={workspaceGoals} inspectorExpanded={taskInspectorExpanded} larkConnections={drawerReadOnly ? [] : larkConnections} todoReadbackUnavailable={drawerSelection.kind === "todo" && !workspaceGoals.some((goal) => goal.goalId === drawerSelection.item.goalId && !goal.loadState && (drawerSelection.item.detailMode !== undefined || drawerSelection.item.done || goal.agentTodos.some((todo) => todo.todoId === drawerSelection.item.todoId)))} @@ -2842,7 +2847,8 @@ export function PersonalWorkspacePage({ onRetryProposalReadback={() => void (creationProposalId ? proposalReadback.refetch() : actionReadback.refetch())} onClose={() => { if (drawerSelection.kind === "proposal" && ["applied", "rejected"].includes(drawerSelection.item.status) - && !(drawerSelection.item.status === "applied" && ["heartbeat.bind", "team.plan"].includes(drawerSelection.item.actionKind))) { + && !(drawerSelection.item.status === "applied" && (drawerSelection.item.reviewPlan?.taskExecution + || ["heartbeat.bind", "team.plan"].includes(drawerSelection.item.actionKind)))) { setProposals((current) => { const next = { ...current }; delete next[drawerSelection.item.previewId]; diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css index 2c88ae0b46..184162951d 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css @@ -1366,6 +1366,8 @@ button.personal-execution-chip:focus-visible { outline: 2px solid #0070f3; outli .personal-run-drawer-tabs button { min-height: 36px; padding: 0 8px; border: 0; border-radius: 8px; background: transparent; color: var(--pw-muted); cursor: pointer; font-size: var(--text-caption); line-height: var(--leading-caption); font-weight: 500; } .personal-run-drawer-tabs button[aria-selected="true"] { background: #fff; color: var(--pw-text); box-shadow: 0 1px 4px rgb(30 28 20 / 12%); } .personal-session-summary { margin-top: 12px; } +.personal-task-receipt-result { margin-top: 12px; overflow-wrap: anywhere; } +.personal-task-receipt-answer p { color: var(--pw-text); font-size: var(--text-body); line-height: var(--leading-body); } .personal-session-message-record { margin-top: 14px; } .personal-session-message-record > h3 { margin: 0 0 10px; color: var(--pw-faint); font-size: var(--text-caption); line-height: var(--leading-caption); letter-spacing: .06em; text-transform: uppercase; } .personal-session-message-record ol { display: grid; gap: 0; padding: 0; margin: 0; list-style: none; } diff --git a/loopx/capabilities/manager_context/roundtrip.py b/loopx/capabilities/manager_context/roundtrip.py index 6c07c6c43e..da348c00a2 100644 --- a/loopx/capabilities/manager_context/roundtrip.py +++ b/loopx/capabilities/manager_context/roundtrip.py @@ -50,6 +50,7 @@ from ...control_plane.content_digest import BARE_SHA256_PATTERN from ...control_plane.collaboration.source_chat_observation import original_delivery_facts from ...chat import ChatMessagePayloadConflictError +from ...chat_store import TERMINAL_TURN_STATES class ReturnResolutionBlocked(ValueError, RuntimeError): """A return cannot be resolved; ``reason`` is the provider-neutral code. @@ -431,15 +432,22 @@ def project_chat_session_snapshot(root, store, session_id, *, registry, resume_from=None, project_history_messages=None): """Project return delivery state into one existing Chat snapshot.""" - strict = message_limit is not None or resume_from is not None - snapshot = (store.session_snapshot(session_id, strict_messages=True) if strict - else store.session_snapshot(session_id)) + turn = None if turn_id is not None: if message_limit is None: raise ValueError("a selected original Turn requires paged history") turn = store.load_turn(session_id, turn_id) if not turn or turn.get("session_id") != session_id or turn.get("turn_id") != turn_id: raise ValueError("original Turn is unavailable from this Session") + strict = message_limit is not None or resume_from is not None + # Read the status first: terminal completion persists its answer before + # publishing completed. An older message prefix must not appear final. + snapshot = (store.session_snapshot(session_id, strict_messages=True) if strict + else store.session_snapshot(session_id)) + if turn is not None: + status = turn["status"] + snapshot["selected_turn"] = {"turn_id": turn_id, "status": status, "terminal": status in TERMINAL_TURN_STATES} + snapshot["active_turn"] = None # OriginalRequest already displays only this Turn. Select it before # paging/projection, retaining every message and the strict journal read. snapshot["messages"] = [row for row in snapshot["messages"] if row.get("turn_id") == turn_id] diff --git a/loopx/control_plane/presentation/action_review_plan.ts b/loopx/control_plane/presentation/action_review_plan.ts index 83db5b47b4..ec6059c58c 100644 --- a/loopx/control_plane/presentation/action_review_plan.ts +++ b/loopx/control_plane/presentation/action_review_plan.ts @@ -96,8 +96,8 @@ export type ActionReviewPlan = ActionReviewIdentity & ActionReviewState & { /** Recorded creation effects, not evidence of a completed Goal or model Turn. */ creationProgress?: GoalCreationProgress; creationOutcome?: GoalCreationOutcome; - /** Accepted dispatch bound to its Task Session. It does not establish a result. */ - taskExecution?: { goalId: string; todoId: string; sessionId: string }; + /** Accepted dispatch bound to its original Turn. It does not establish a result. */ + taskExecution?: { goalId: string; todoId: string; sessionId: string; turnId: string }; }; export type GoalCreationProgress = { @@ -518,10 +518,11 @@ function taskExecutionReadback( const goalId = textValue(parameters.goal_id); const todoId = textValue(resources?.todo_id); const sessionId = textValue(resources?.session_id); - if (!goalId || !todoId || !sessionId || !textValue(resources?.turn_id) + const turnId = textValue(resources?.turn_id); + if (!goalId || !todoId || !sessionId || !turnId || resources?.goal_id !== goalId || canonical?.goal_id !== goalId || canonical?.todo_id !== todoId || canonical?.status !== "applied" || !sourceFingerprint || canonical?.preview_id !== sourceFingerprint) return undefined; - return {goalId, todoId, sessionId}; + return {goalId, todoId, sessionId, turnId}; } /** diff --git a/tests/control_plane_ts/action_review_plan.test.ts b/tests/control_plane_ts/action_review_plan.test.ts index 68209f5c3c..bc52c22407 100644 --- a/tests/control_plane_ts/action_review_plan.test.ts +++ b/tests/control_plane_ts/action_review_plan.test.ts @@ -46,7 +46,7 @@ test("an interrupted Task start retries only its original saved Task", () => { } }); -test("confirmed Task dispatch resolves only to the receipt's own execution Session", () => { +test("confirmed Task dispatch resolves only to the receipt's own execution Turn", () => { const resources = {goal_id: "repair", todo_id: "todo-first", session_id: "session-first", turn_id: "turn-first"}; const canonical = {goal_id: "repair", todo_id: "todo-first", preview_id: "reviewed", status: "applied"}; const receipt = {projection_verified: true, outcome: "task_execution_started", resource_ids: resources, canonical_receipt: canonical}; @@ -54,7 +54,7 @@ test("confirmed Task dispatch resolves only to the receipt's own execution Sessi status: "applied", normalized_parameters: {goal_id: "repair", start_execution: true}, receipt}; const result = compileActionReviewPlan(proposal); assert.equal(result.canApply, false); - assert.deepEqual(result.taskExecution, {goalId: "repair", todoId: "todo-first", sessionId: "session-first"}); + assert.deepEqual(result.taskExecution, {goalId: "repair", todoId: "todo-first", sessionId: "session-first", turnId: "turn-first"}); for (const change of [ {status: "applying"}, {normalized_parameters: {goal_id: "repair", start_execution: false}}, @@ -311,6 +311,21 @@ test("operation frame projects pending and verified result states", () => { assert.equal(completed.operationFrame.summary, "Simulation completed."); }); +test("an applied operation stays unverified until its current result delivery is verified", () => { + const proposal: Record = operationProposal("outcome_observed"); + proposal.status = "applied"; + proposal.receipt = {projection_verified: true}; + const verified = compileActionReviewPlan(proposal); + assert.equal(verified.interaction, "completed"); + assert.equal(verified.reason, "readback_verified"); + const {result_delivery, ...withoutDelivery} = proposal.operation; + assert.ok(result_delivery); + const pending = compileActionReviewPlan({...proposal, operation: withoutDelivery}); + assert.equal(pending.interaction, "repair"); + assert.equal(pending.reason, "readback_unverified"); + assert.equal(pending.canApply, false); +}); + test("operation frame rejects identity drift and malformed projection fields", () => { const mismatched = operationProposal("awaiting_confirmation"); mismatched.operation.operation_id = "another-operation"; diff --git a/tests/test_chat_conversation_bindings.py b/tests/test_chat_conversation_bindings.py index b824abebb4..27e53dba82 100644 --- a/tests/test_chat_conversation_bindings.py +++ b/tests/test_chat_conversation_bindings.py @@ -637,9 +637,14 @@ def test_original_turn_history_keeps_complete_rows_and_scope_fences(ordinary, mo store.append_message(sid, role="agent", text=f"Original response {i}", turn_id=tid, attachments=[{"name": "original.png", "data_url": "data:image/png;base64,AA=="}] if i == 201 else None) store.update_turn(sid, tid, status="completed", response={"message": "Original response 201"}) + later, _ = store.create_turn(sid, client_turn_id="later-correction", message="Check another case", origin="web") + store.update_turn(sid, later["turn_id"], status="running") + store.update_session(sid, active_turn_id=later["turn_id"]) path = f"/api/chat/sessions/{sid}" before = capture.read_bytes() whole = request(path)[1] + assert whole["active_turn"]["turn_id"] == later["turn_id"] + assert "selected_turn" not in whole expected = [row for row in whole["messages"] if row.get("turn_id") == tid] recovered, recover = [], runtime.recover_exited_turn def observe_recovery(source): @@ -649,19 +654,21 @@ def observe_recovery(source): query = {"message_limit": 200, "turn_id": tid} code, first = request(path + "?" + urlencode(query)) assert code == 200 and first["message_page"]["turn_id"] == tid + assert first["selected_turn"] == {"turn_id": tid, "status": "completed", "terminal": True} + assert first["active_turn"] is None, "The scoped response must not carry another Turn's details" assert len(first["messages"]) == 200 and first["message_page"]["total"] == len(expected) == 203 cursor = first["message_page"]["next_cursor"] store.append_message(sid, role="agent", text="Outside the frozen original prefix", turn_id=tid) code, rest = request(path + "?" + urlencode({**query, "message_cursor": cursor})) assert code == 200 and rest["message_page"]["next_cursor"] is None assert first["messages"] + rest["messages"] == expected + _, current = request(path + "?" + urlencode({**query, "turn_id": later["turn_id"]})) + assert current["selected_turn"] == {"turn_id": later["turn_id"], "status": "running", "terminal": False} assert recovered == [], "A historical Turn read must not recover the Session's active execution" assert capture.read_bytes() == before, "A scoped read cannot execute or recover model work" assert "turn_id" not in request(path + "?message_limit=200")[1]["message_page"] assert recovered == [sid], "An unscoped Chat refresh keeps its ordinary recovery behavior" - other, _ = store.create_turn(sid, client_turn_id="other-original", message="Other question", origin="web") - store.update_turn(sid, other["turn_id"], status="completed", response={"message": "Other answer"}) - for selected in [None, other["turn_id"]]: + for selected in [None, later["turn_id"]]: wrong = {"message_limit": 200, "message_cursor": cursor, **({"turn_id": selected} if selected else {})} assert request(path + "?" + urlencode(wrong))[0] == 400 for invalid in ["turn_id=missing", "message_limit=200&turn_id=missing", "message_limit=200&turn_id=..", From 42cc3dba9059ff7fcf68ea68fed401fe0c68cf6b Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 13 Nov 2026 11:24:10 +0800 Subject: [PATCH 017/267] fix(lark): drain private intake before reconnecting Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../lark/goal_topic_runtime_service.py | 133 +++++++++----- .../extensions/lark/private_conversations.py | 5 +- tests/test_lark_private_ingress.py | 172 +++++++++++++++--- 3 files changed, 238 insertions(+), 72 deletions(-) diff --git a/loopx/extensions/lark/goal_topic_runtime_service.py b/loopx/extensions/lark/goal_topic_runtime_service.py index a6a260fd30..ab3f437ef5 100644 --- a/loopx/extensions/lark/goal_topic_runtime_service.py +++ b/loopx/extensions/lark/goal_topic_runtime_service.py @@ -9,6 +9,7 @@ import hashlib import logging from pathlib import Path +from subprocess import Popen import threading import time from typing import Any @@ -30,6 +31,7 @@ ManagerRouteReconciler = Callable[[Mapping[str, Any]], Mapping[str, Any]] ProfileWorkerFingerprint = tuple[str, str, str, tuple[str, ...]] ProfileWorker = tuple[threading.Event, threading.Thread, ProfileWorkerFingerprint] +MessageConsumer = tuple[threading.Event, Popen[str], threading.Event] class LarkGoalTopicRuntimeService: @@ -55,6 +57,7 @@ def __init__( self.manager_route_reconciler = manager_route_reconciler self._lock = threading.Lock() self._workers: dict[str, ProfileWorker] = {} + self._message_consumers: dict[str, MessageConsumer] = {} self._health: dict[str, dict[str, Any]] = {} self._closed = threading.Event() self._delivery_wakeup = threading.Event() @@ -128,14 +131,23 @@ def step() -> None: if ingress is not None and ingress.done(): try: for profile in ingress.result(): - # Stop only this service's existing live worker. - # Its finally/refresh path releases the App lease - # before starting its replacement; Core work and - # historical messages are never replayed. with self._lock: worker = self._workers.get(profile) - if worker is not None: - worker[0].set() + current = self._message_consumers.get(profile) + if worker is None or current is None or current[0] is not worker[0]: + continue + stop, process, requested = current + if stop.is_set() or requested.is_set() or process.poll() is not None: + continue + # End this message pipe and let it drain under the + # existing App lease. Stopping the worker discards + # transient events already read by the stream. + requested.set() + try: + process.terminate() + except OSError: + requested.clear() + logging.getLogger(__name__).warning("Lark message consumer could not reconnect") except Exception: logging.getLogger(__name__).warning("Private Chat source observation is unavailable") ingress = None @@ -241,10 +253,12 @@ def step() -> None: on_error=lambda: logging.getLogger(__name__).warning( "Private Chat delivery discovery is pending")) - def _admit_private(self, profile: str, event: dict[str, Any]) -> dict[str, Any]: + def _admit_private(self, profile: str, event: dict[str, Any], *, + expected_binding_id: str | None = None) -> dict[str, Any]: if self._closed.is_set(): raise RuntimeError("Lark conversation service is closed") - result = self.private_conversations.admit(profile, event, defer_background_stop=True) + options = {"expected_binding_id": expected_binding_id} if expected_binding_id is not None else {} + result = self.private_conversations.admit(profile, event, defer_background_stop=True, **options) # Admission owns persistence and authorization. Only after it returns # may the existing reply worker look for its durable work; duplicates # and denied inputs still cannot manufacture a pending journal entry. @@ -308,9 +322,14 @@ def _profile_worker_fingerprint( if self.action_service is not None else () ) + app_ref = str(profile_config.get("bot_app_id") or "") + binding_id = str(profile_config.get("binding_id") or "") + # A same-App replacement changes which workspace receives live input. + # Keep the App lease identity, but retire the old worker on refresh. + worker_ref = f"{app_ref}:{binding_id}" if app_ref and binding_id else app_ref or binding_id return ( str(profile_config.get("cli_bin") or "lark-cli"), - str(profile_config.get("bot_app_id") or profile_config.get("binding_id") or ""), + worker_ref, str(profile_config.get("consumer_ref") or ""), callback_chats, ) @@ -411,12 +430,36 @@ def handle_review_callback( profile_config=profile_config, ) - profile_config = ( - runtime._active_profile_configs(self.snapshot_provider()).get( - profile - ) - or {} + snapshot = self.snapshot_provider() + profile_config = runtime._active_profile_configs(snapshot).get(profile) or {} + private_profiles = snapshot.get("private_profiles") + private_binding = private_profiles.get(profile) if isinstance(private_profiles, Mapping) else None + binding_id = private_binding.get("binding_id") if isinstance(private_binding, Mapping) else None + reconnect_requested = threading.Event() + private_admitter = self._admit_private if self.private_conversations is not None else None + bound_owner = ( + private_admitter is not None + and isinstance(binding_id, str) and bool(binding_id) + and isinstance(private_binding, Mapping) + and private_binding.get("audience") == "owner" ) + if bound_owner: + def admit_bound_private(profile_name: str, event: dict[str, Any]) -> dict[str, Any]: + # An event read by the old stream cannot follow a + # same-App rebind while it waits for admission. + return self._admit_private(profile_name, event, expected_binding_id=binding_id) + + private_admitter = admit_bound_private + + def start_consumer(args: list[str]) -> Popen[str]: + process = runtime._default_process_factory(args) + if args[3:6] == ["event", "consume", "im.message.receive_v1"]: + with self._lock: + worker = self._workers.get(profile) + if worker is not None and worker[0] is stop: + self._message_consumers[profile] = (stop, process, reconnect_requested) + return process + # A bot App can have several local profile aliases and Chat # servers can use different runtime roots or ports. The # consumer lease must therefore be machine/App scoped. @@ -447,28 +490,34 @@ def capture(route: Mapping[str, object]) -> dict[str, object]: route, self.runtime_controller ) - result = runtime.stream_lark_goal_topic_profile( - profile=profile, - snapshot_provider=self.snapshot_provider, - stop=stop, - runtime_root=self.runtime_root, - answer=answer, - # The listener checks current binding eligibility - # per event; a later saved Session must not require - # replacing a consumer with an active reply. - capture_mention=capture, - private_admitter=(self._admit_private - if self.private_conversations is not None else None), - proposal_deliverer=deliver_proposals, - review_callback_handler=( - handle_review_callback - if self.action_service is not None and profile in runtime._active_group_profile_configs(self.snapshot_provider()) - else None - ), - health_sink=lambda update: self._update_health( - profile, **dict(update) - ), - ) + process_options = {"process_factory": start_consumer} if bound_owner else {} + try: + result = runtime.stream_lark_goal_topic_profile( + profile=profile, + snapshot_provider=self.snapshot_provider, + stop=stop, + runtime_root=self.runtime_root, + answer=answer, + # Recheck binding eligibility per event, including + # events drained from an old provider connection. + capture_mention=capture, + private_admitter=private_admitter, + proposal_deliverer=deliver_proposals, + review_callback_handler=( + handle_review_callback + if self.action_service is not None and profile in runtime._active_group_profile_configs(self.snapshot_provider()) + else None + ), + health_sink=lambda update: self._update_health( + profile, **dict(update) + ), + **process_options, + ) + finally: + with self._lock: + current = self._message_consumers.get(profile) + if current is not None and current[0] is stop: + del self._message_consumers[profile] if result.get("status") == "configuration_removed": self._update_health( profile, @@ -480,16 +529,18 @@ def capture(route: Mapping[str, object]) -> dict[str, object]: if stop.is_set(): break restart_count += 1 + error_code = result.get("error_code") + requested_disconnect = ( + reconnect_requested.is_set() + and error_code == "lark_event_source_disconnected" + ) self._update_health( profile, status="retrying", error_code=( None - if result.get("ok") is True - else str( - result.get("error_code") - or "lark_event_listener_failed" - ) + if result.get("ok") is True or requested_disconnect + else str(error_code or "lark_event_listener_failed") ), restart_count=restart_count, ) diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py index a84c0687f8..7183fb2bde 100644 --- a/loopx/extensions/lark/private_conversations.py +++ b/loopx/extensions/lark/private_conversations.py @@ -822,10 +822,13 @@ def read(args: Sequence[str], **_kwargs: Any) -> subprocess.CompletedProcess[str @effect_runtime_request_scope() def admit(self, profile: str, event: dict[str, Any], *, - defer_background_stop: bool = False, history_review: bool = False) -> dict[str, Any]: + defer_background_stop: bool = False, history_review: bool = False, + expected_binding_id: str | None = None) -> dict[str, Any]: record_event_timing(profile, str(event.get("message_id") or ""), "admission_started") try: binding = self._binding(profile) + if expected_binding_id is not None and binding["binding_id"] != expected_binding_id: + return {"status": "audience_rejected"} source = lark_private_source(provider_ref=binding["provider_ref"], event=event) if history_review and (binding.get("context_kind") != "steward" or binding.get("audience", "owner") != "owner" or event.get("chat_type") != "p2p"): diff --git a/tests/test_lark_private_ingress.py b/tests/test_lark_private_ingress.py index d2d656ae3e..aaf21b2901 100644 --- a/tests/test_lark_private_ingress.py +++ b/tests/test_lark_private_ingress.py @@ -192,37 +192,149 @@ def test_stale_or_unreadable_health_is_not_verified(source_monitor): assert f.monitor.health(f.binding["binding_id"])["status"] == "unavailable" -def test_default_scanner_reconnects_only_its_owned_live_worker(tmp_path, monkeypatch): - from loopx.extensions.lark.goal_topic_runtime_service import LarkGoalTopicRuntimeService - +@pytest.mark.parametrize("transition", ["reconnect", "shutdown", "revoke", "rebind_during_reconnect"]) +def test_live_private_ingress_across_listener_transition(ordinary, tmp_path, monkeypatch, transition): # noqa: F811 + from concurrent.futures import ThreadPoolExecutor + from pathlib import Path + + from loopx.extensions.lark import goal_topic_runtime as runtime + from loopx.extensions.lark import goal_topic_runtime_service as lifecycle + from loopx.extensions.lark.conversation_identity import identity_ref + from test_lark_private_conversations import connect + + store, controller, provider, transport = connect(ordinary) + profile = "notes-app" + binding = next(row for row in transport.bindings.read()["bindings"] if row["transport_ref"] == profile) + config = transport.profiles()[profile] + active = True + first, second, third = [provider.event(profile, key, f"Live request {key}") + for key in ("first", "second", "third")] + in_admission, release, waiting, received_third = (threading.Event() for _ in range(4)) clock = [0.0] - monkeypatch.setattr("loopx.extensions.lark.goal_topic_runtime_service.time.monotonic", lambda: clock[0]) - observed = [] - source = SimpleNamespace(observe=lambda **_kwargs: observed.append(True) or ["owner-app"]) - - def pending(): - clock[0] = 61 - return [] - - service = LarkGoalTopicRuntimeService(snapshot_provider=lambda: {}, runtime_root=tmp_path, - runtime_controller=object(), private_conversations=SimpleNamespace(pending_delivery_paths=pending, ingress=source)) - own, other = threading.Event(), threading.Event() - service._workers = {"owner-app": (own, threading.current_thread(), ("", "", ())), - "other-app": (other, threading.current_thread(), ("", "", ()))} - waited = threading.Event() - waits = 0 - - def wait(_seconds): - nonlocal waits - waits += 1 - if own.is_set() or waits >= 100: - service._closed.set() - else: - waited.wait(0.01) - - monkeypatch.setattr(service._delivery_wakeup, "wait", wait) - service._reconcile_private() - assert observed == [True] and own.is_set() and not other.is_set() + admitted = [] + consumers = [] + + def admit(profile_name, event, **kwargs): + if event["message_id"] == first["message_id"]: + in_admission.set() + assert release.wait(8) + result = transport.admit(profile_name, event, **kwargs) + admitted.append((event["message_id"], result["status"])) + if event["message_id"] == third["message_id"]: + received_third.set() + return result + + owner = SimpleNamespace(admit=admit, pending_delivery_paths=lambda: [], + ingress=SimpleNamespace(observe=lambda **_: [profile, "unowned-app"])) + service = lifecycle.LarkGoalTopicRuntimeService( + snapshot_provider=lambda: {"private_profiles": {profile: config}} if active else {}, + runtime_root=store.root.parent, runtime_controller=controller, private_conversations=owner) + monkeypatch.setattr(lifecycle, "time", SimpleNamespace(monotonic=lambda: clock[0])) + monkeypatch.setattr(Path, "home", lambda: tmp_path) + + class Consumer: + def __init__(self, initial): + self.initial = initial + self.exited, self.waited = threading.Event(), threading.Event() + self.stdout = self.lines() + + def lines(self): + yield "[event] ready event_key=im.message.receive_v1\n" + if self.initial: + yield json.dumps(first) + assert in_admission.wait(8) + yield json.dumps(second) + waiting.set() # The reader advanced past submission of the second line. + else: + if transition == "reconnect": + yield json.dumps({**second, "event_id": "synthetic_provider_redelivery"}) + yield json.dumps(third) + assert self.exited.wait(8) + yield "[event] exited (reason: signal)\n" + + def poll(self): + return 0 if self.exited.is_set() else None + + def terminate(self): + self.exited.set() + + def wait(self, timeout=None): + assert self.exited.is_set() + self.waited.set() + return 0 + + def spawn(args): + assert args[3:6] == ["event", "consume", "im.message.receive_v1"] + if consumers: + assert len(consumers) == 1 and consumers[0].waited.is_set() + consumer = Consumer(not consumers) + consumers.append(consumer) + return consumer + + stream = runtime.stream_lark_goal_topic_profile + + def isolated_stream(**kwargs): + kwargs.setdefault("process_factory", spawn) + return stream(**kwargs) + + monkeypatch.setattr(runtime, "_default_process_factory", spawn) + monkeypatch.setattr(runtime, "stream_lark_goal_topic_profile", isolated_stream) + service.start() + with ThreadPoolExecutor(max_workers=1) as pool: + try: + assert waiting.wait(8) + if transition in ("reconnect", "rebind_during_reconnect"): + clock[0] = 61 + service._delivery_wakeup.set() + elif transition == "shutdown": + pool.submit(service.close) + else: + active = False + transport.bindings.disconnect(binding["binding_id"], expected_revision=transport.bindings.read()["revision"]) + service.refresh() + assert consumers[0].exited.wait(8) + assert len(consumers) == 1 + if transition == "rebind_during_reconnect": + projects = transport.bindings.projects + other = tmp_path / "other-workspace" + other.mkdir() + projects.roots.append((other, other)) + target = next(x for x in projects.available() if x["workspace_path"] == str(other)) + replacement = transport.bindings.configure(transport_ref=profile, project_ref=target["project_ref"], + executor_endpoint_id="codex") + assert replacement["binding_id"] != binding["binding_id"] + config = transport.profiles()[profile] + release.set() + if transition in ("reconnect", "rebind_during_reconnect"): + assert received_third.wait(8) + else: + assert consumers[0].waited.wait(8) + finally: + release.set() + service.close() + controller.close() + + second_ref = identity_ref(binding["provider_ref"], second["message_id"]) + if transition == "reconnect": + assert [event_id for event_id, _ in admitted[:2]] == [first["message_id"], second["message_id"]] + assert consumers[0].waited.is_set() and len(consumers) == 2 + assert [event_id for event_id, _ in admitted].count(second["message_id"]) == 2 + original = transport.core.read_request(second_ref) + assert original["source"]["source_ref"] == transport.core.read_request( + identity_ref(binding["provider_ref"], first["message_id"]))["source"]["source_ref"] + messages = store.messages(original["session_id"]) + assert sum(m.get("text") == second["content"] for m in messages) == 1 + elif transition == "rebind_during_reconnect": + assert admitted[:2] == [(first["message_id"], "audience_rejected"), + (second["message_id"], "audience_rejected")] + assert not (transport.core.root / f"{second_ref}.json").exists() + assert not (transport.root / f"{second_ref}.json").exists() + third_ref = identity_ref(binding["provider_ref"], third["message_id"]) + assert transport.core.read_request(third_ref)["binding_id"] == replacement["binding_id"] + assert consumers[0].waited.is_set() and len(consumers) == 2 + else: + assert not any(event_id == second["message_id"] for event_id, _ in admitted) + assert len(consumers) == 1 def test_unknown_failed_capture_is_not_replayed(source_monitor): From 8d76419078daaeae1d10070f0bb12cd2dc7c0572 Mon Sep 17 00:00:00 2001 From: huangruiteng Date: Fri, 13 Nov 2026 12:05:45 +0800 Subject: [PATCH 018/267] Deliver native child results on the next Turn Signed-off-by: huangruiteng --- .../host-native-child-receipts.md | 42 ++- .../capabilities/manager_context/__init__.py | 3 + loopx/capabilities/multi_subagent/cli.py | 48 +++- .../native_child_result_inbox.py | 49 ++++ .../capabilities/native_child_result_inbox.ts | 257 ++++++++++++++++++ .../control_plane/collaboration/inbox_read.ts | 34 ++- loopx/control_plane/collaboration/peers.py | 26 +- .../control_plane/effect_runtime_handlers.ts | 1 + loopx/extensions/codex_native_child.py | 18 +- .../test_codex_native_child_receipts.py | 8 + .../test_native_child_receipts.py | 47 ++++ .../native_child_result_inbox.test.ts | 121 +++++++++ tests/test_manager_context_tracking.py | 36 +++ 13 files changed, 672 insertions(+), 18 deletions(-) create mode 100644 loopx/capabilities/multi_subagent/native_child_result_inbox.py create mode 100644 loopx/control_plane/capabilities/native_child_result_inbox.ts create mode 100644 tests/control_plane_ts/native_child_result_inbox.test.ts diff --git a/docs/integrations/host-native-child-receipts.md b/docs/integrations/host-native-child-receipts.md index fb3a7c0144..c928c77f00 100644 --- a/docs/integrations/host-native-child-receipts.md +++ b/docs/integrations/host-native-child-receipts.md @@ -92,7 +92,9 @@ Agent 明确记录验收;宿主完成不代表证据已被采纳。 公开安全的证据与验证引用。结果和不采用评审的原因码可选,只需是紧凑的 不透明诊断标识,不限定供应商词表,也不授予权限;未知时省略。结果与评审 可重复携带一致的 `operation`、`entrypoint-id`,冲突值包括重放时仍被拒绝。 - 原始提示、宿主错误、对话、本地路径和凭据不进入回执。 + 原始提示、宿主错误、对话、本地路径和凭据不进入公开回执。支持的宿主若能 + 直接观察最终结果正文,会把正文写入按 Goal 实例和主 Agent 隔离的本机私有 + inbox;公开活动投影仍只保留类型化终态和不透明身份。 4. 同一身份和内容重放幂等,内容冲突会被拒绝。`read` 与带 Turn ID 的 `agent-context` 读取同一模型。Goal 状态的 JSON 与 Markdown 只在能力启用且确有决策时投影 最近一轮;仪表板读取该投影,未配置或无关 Goal 不显示活动行。 @@ -102,6 +104,38 @@ Agent 明确记录验收;宿主完成不代表证据已被采纳。 新写入在既有事件流锁内重新核对读回,每次报告只调用一个粗粒度 TS 边界, 不增加第二套 Python 阶段判断。 +## Next-Turn result delivery / 下一 Turn 结果投递 + +Supported managed hosts persist an observed child's final text before exposing +the matching typed terminal receipt. The text is stored only in the +owner-private local runtime, partitioned by the captured Goal lifetime and +coordinator (or by Goal ID for a legacy registry without lifetime identity). It +never enters the public rollout log, status projection, telemetry, or repository +state. + +The existing `manager-inbox` turn-start hook detects unread native results and +adds one `before_work` read. A normal `manager-inbox read` returns short results +inline and gives a digest-bound private file reference for larger results. That +read records delivery but does not consume the result. Until the parent records +an `accepted`, `deferred`, or `rejected` disposition with the supplied +`native-child acknowledge-result` command, a restart may deliver the same stable +result ID again. Exact ingest and acknowledgement replay are idempotent; +conflicting text or disposition is rejected. A child conclusion remains +candidate evidence and never performs parent work, validation, or adoption. + +支持的托管宿主会先把观察到的子 Agent 最终正文写入主 Agent 的本机私有 +runtime,再公开对应的类型化终态回执。存储按已捕获的 Goal 生命周期和主 Agent +隔离;缺少生命周期身份的旧 registry 按 Goal ID 隔离。正文不会进入公开 rollout +日志、状态投影、遥测或仓库状态。 + +既有 `manager-inbox` Turn-start hook 会发现未读结果,并在下一 Turn 的普通工作 +之前要求读取。`manager-inbox read` 对短结果内联返回,对长结果返回带摘要校验的 +私有文件引用。读取只记录 delivery,不会消费结果。主 Agent 使用返回的 +`native-child acknowledge-result` 命令明确记录 `accepted`、`deferred` 或 +`rejected` 之前,重启后仍可能看到同一个稳定结果 ID。相同写入与确认重放幂等, +正文或 disposition 冲突会被拒绝。子 Agent 结论始终只是候选证据,不会替主 +Agent 执行工作、验证或采纳。 + After a lawful deferred-Todo recovery, rerun `quota should-run` with the same explicit Turn and Todo. If the original bound guard denied delivery and the current guard explicitly admits work, quota appends a work-qualification receipt @@ -196,7 +230,8 @@ replaying the same binding and item remains idempotent. Direct enabled CLI adapter calls require that attempt; feature-off calls retain their original request. Only compact hashed child references are retained for correlation; they do not enter public activity rows or replace independent parent review. -No prompt or raw result is added to a receipt. +No prompt or raw result is added to a public receipt. Supported managed hosts +retain final result text only in the private next-Turn inbox described above. 恢复 CLI 时可能只收到新的 `wait` 完成事件。适配器从同一已准入 Goal 实例、 主 Agent、LoopX Turn 的最近一次已启动宿主 spawn 或 followup 回执恢复关联, @@ -211,7 +246,8 @@ No prompt or raw result is added to a receipt. app-server 使用其原生 Turn ID。真实重试在启动前递增尝试次数;同一绑定与 事件的重放仍幂等。直接调用已启用的 CLI 适配器须提供该尝试次数,关闭能力 时请求不变。关联只保留紧凑的子代理哈希引用,不进入公开活动行, -也不替代独立父任务验收。回执不增加原始提示或结果内容。 +也不替代独立父任务验收。公开回执不增加原始提示或结果内容;支持的托管宿主仅在 +上述私有下一 Turn inbox 中保留最终结果正文。 A live isolated Codex 0.142.5 test observed one successful spawn, a second failed spawn at `agents.max_threads=1`, child completion and independent parent diff --git a/loopx/capabilities/manager_context/__init__.py b/loopx/capabilities/manager_context/__init__.py index 82a3d41d5f..4b67e6cf90 100644 --- a/loopx/capabilities/manager_context/__init__.py +++ b/loopx/capabilities/manager_context/__init__.py @@ -257,6 +257,9 @@ def produce(): count = ( max(len(inbox["items"]), agenda["incoming_count"]) + max(len(inbox.get("peer_returns", {}).get("items", [])), agenda["outgoing_count"]) + + len(inbox.get("native_child_results", {}).get("items", [])) + + int(inbox.get("native_child_results", {}).get("unavailable_count", 0)) + + int(inbox.get("native_child_results", {}).get("scan_complete") is False) + len(inbox.get("operation_handoffs", [])) ) incomplete = not agenda["scan_complete"] diff --git a/loopx/capabilities/multi_subagent/cli.py b/loopx/capabilities/multi_subagent/cli.py index 420a2005ce..5e4fceadec 100644 --- a/loopx/capabilities/multi_subagent/cli.py +++ b/loopx/capabilities/multi_subagent/cli.py @@ -11,6 +11,7 @@ from ...control_plane.goals.source_session_registry_state import exact_goal_ref from ...orchestration import compact_orchestration_policy from .native_child_receipts import load_native_child_activity, record_native_child +from .native_child_result_inbox import acknowledge_native_child_result def register_native_child_commands(subparsers, add_format): @@ -18,7 +19,7 @@ def register_native_child_commands(subparsers, add_format): "native-child", help="Record or read typed host-native child activity for an admitted Turn." ) add_format(parser) - parser.add_argument("native_child_action", choices=("record", "read")) + parser.add_argument("native_child_action", choices=("record", "read", "acknowledge-result")) parser.add_argument("--goal-id", required=True) parser.add_argument("--agent-id", required=True) parser.add_argument("--turn-instance-id", required=True) @@ -35,6 +36,9 @@ def register_native_child_commands(subparsers, add_format): "for result/deferred/rejected. Accepted reviews use evidence/validation refs instead.") parser.add_argument("--evidence-ref", help="Public-safe opaque reference for accepted evidence.") parser.add_argument("--validation-ref", help="Public-safe opaque reference for parent validation.") + parser.add_argument("--result-id", help="Stable native child result identity returned by manager-inbox read.") + parser.add_argument("--disposition", choices=("accepted", "deferred", "rejected"), + help="Parent disposition after the result was delivered and reviewed.") parser.add_argument("--execute", action="store_true", help="Append the receipt; otherwise preview only.") @@ -56,27 +60,46 @@ def handle_native_child_command(args, registry_path, runtime_root, print_payload goal = load_goal_from_registry(registry_path, args.goal_id) if goal is None or args.agent_id not in registered_agent_ids_for_goal(goal): raise ValueError("coordinator is not registered for this Goal") - orchestration = compact_orchestration_policy(goal.get("spawn_policy")) - if not ( - orchestration.get("mode") == "multi_subagent" - and orchestration.get("spawn_allowed") is True - and int(orchestration.get("max_children") or 0) > 0 - ): - raise ValueError("native child receipts require enabled multi_subagent policy") - configured_limit = int(orchestration["max_children"]) + configured_limit = None + if args.native_child_action != "acknowledge-result": + orchestration = compact_orchestration_policy(goal.get("spawn_policy")) + if not ( + orchestration.get("mode") == "multi_subagent" + and orchestration.get("spawn_allowed") is True + and int(orchestration.get("max_children") or 0) > 0 + ): + raise ValueError("native child receipts require enabled multi_subagent policy") + configured_limit = int(orchestration["max_children"]) if args.native_child_action == "read": if any((args.operation_id, args.stage, args.operation, args.outcome, args.entrypoint_id, args.reason_code, args.evidence_ref, - args.validation_ref, args.execute)): + args.validation_ref, args.result_id, args.disposition, args.execute)): raise ValueError("read accepts only goal, agent and Turn identity") payload = {"ok": True, "native_child_activity": load_native_child_activity( runtime_root, goal_id=args.goal_id, agent_id=args.agent_id, turn_instance_id=args.turn_instance_id, configured_limit=configured_limit, registry_path=registry_path, goal_ref=goal_ref, )} + elif args.native_child_action == "acknowledge-result": + if any((args.operation_id, args.stage, args.operation, args.outcome, + args.entrypoint_id, args.reason_code, args.evidence_ref, + args.validation_ref)) or not args.result_id or not args.disposition: + raise ValueError("acknowledge-result requires only result-id and disposition") + if not args.execute: + payload = {"ok": True, "preview": True, "result_id": args.result_id, + "disposition": args.disposition} + else: + payload = acknowledge_native_child_result( + runtime_root=runtime_root, goal_id=args.goal_id, + agent_id=args.agent_id, turn_instance_id=args.turn_instance_id, + result_id=args.result_id, + disposition=args.disposition, goal_ref=goal_ref, + ) else: if not args.operation_id or not args.stage or not args.outcome: raise ValueError("record requires operation-id, stage and outcome") + if args.result_id or args.disposition: + raise ValueError("record does not accept result acknowledgement fields") payload = record_native_child( runtime_root=runtime_root, goal_id=args.goal_id, agent_id=args.agent_id, turn_instance_id=args.turn_instance_id, operation_id=args.operation_id, @@ -96,6 +119,11 @@ def handle_native_child_command(args, registry_path, runtime_root, print_payload def render_native_child(payload): if not payload["ok"]: return str(payload["error"]) + if "native_child_activity" not in payload: + return ( + f"Native child result {payload['result_id']} " + f"{'would be ' if payload.get('preview') else ''}{payload['disposition']}." + ) activity = payload["native_child_activity"] return ( f"Native child activity for {activity['turn_instance_id']}: " diff --git a/loopx/capabilities/multi_subagent/native_child_result_inbox.py b/loopx/capabilities/multi_subagent/native_child_result_inbox.py new file mode 100644 index 0000000000..3ac1a99a77 --- /dev/null +++ b/loopx/capabilities/multi_subagent/native_child_result_inbox.py @@ -0,0 +1,49 @@ +"""Thin Python adapter for the TypeScript native-child result inbox owner.""" + +from __future__ import annotations + +from collections.abc import Mapping +from datetime import datetime, timezone +from pathlib import Path +from typing import Any + +from ...control_plane.effect_runtime import EffectRuntimeRejected, effect_runtime_result + + +def _now() -> str: + return datetime.now(timezone.utc).isoformat() + + +def ingest_native_child_result( + *, runtime_root: Path, goal_id: str, agent_id: str, + turn_instance_id: str, operation_id: str, outcome: str, text: str, + goal_ref: Mapping[str, Any] | None = None, +) -> dict[str, Any]: + try: + return effect_runtime_result("multi_subagent.native_result_inbox", { + "operation": "ingest", "runtime_root": str(runtime_root.resolve()), + "goal_id": goal_id, "agent_id": agent_id, + "goal_ref": dict(goal_ref) if goal_ref is not None else None, + "turn_instance_id": turn_instance_id, "operation_id": operation_id, + "outcome": outcome, "text": text, "observed_at": _now(), + }) + except EffectRuntimeRejected as exc: + raise ValueError(str(exc)) from exc + + +def acknowledge_native_child_result( + *, runtime_root: Path, goal_id: str, agent_id: str, + turn_instance_id: str, result_id: str, disposition: str, + goal_ref: Mapping[str, Any] | None = None, +) -> dict[str, Any]: + try: + return effect_runtime_result("multi_subagent.native_result_inbox", { + "operation": "acknowledge", "runtime_root": str(runtime_root.resolve()), + "goal_id": goal_id, "agent_id": agent_id, + "goal_ref": dict(goal_ref) if goal_ref is not None else None, + "turn_instance_id": turn_instance_id, + "result_id": result_id, "disposition": disposition, + "observed_at": _now(), + }) + except EffectRuntimeRejected as exc: + raise ValueError(str(exc)) from exc diff --git a/loopx/control_plane/capabilities/native_child_result_inbox.ts b/loopx/control_plane/capabilities/native_child_result_inbox.ts new file mode 100644 index 0000000000..d20f10dd12 --- /dev/null +++ b/loopx/control_plane/capabilities/native_child_result_inbox.ts @@ -0,0 +1,257 @@ +/** Durable, owner-private delivery for host-native child results. + * + * The public rollout log keeps only typed lifecycle receipts. Result text stays + * in this local private store and is projected to the parent on a later Turn. + */ +import {createHash} from "node:crypto"; +import {constants} from "node:fs"; +import {open, readdir} from "node:fs/promises"; +import {isAbsolute, join, toNamespacedPath} from "node:path"; + +import type {JsonObject} from "../effect_program.ts"; +import {EffectRuntimeRequestError} from "../effect_runtime_errors.ts"; +import {atomicWriteJson, atomicWriteText, withFileMutationLock} from "../effect_runtime_io.ts"; +import {jsonObject, requireJsonObject, requireNonEmptyString, requireStringLiteral} from "../runtime_decode.ts"; + +export const NATIVE_CHILD_RESULT_ENTRY_SCHEMA = "native_child_result_entry_v0"; +export const NATIVE_CHILD_RESULT_INBOX_SCHEMA = "native_child_result_inbox_v0"; + +const ID = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,191}$/u; +const RESULT_ID = /^ncr_[0-9a-f]{32}$/u; +const OUTCOMES = ["completed", "failed", "cancelled"] as const; +const DISPOSITIONS = ["accepted", "deferred", "rejected"] as const; +const MAX_CONTENT_BYTES = 4 * 1024 * 1024; +const INLINE_CONTENT_BYTES = 32 * 1024; +const MAX_INLINE_PAGE_BYTES = 128 * 1024; +const MAX_SCAN = 2_000; +const PAGE_SIZE = 20; + +function fail(message: string): never { throw new EffectRuntimeRequestError(message); } +function opaque(value: unknown, label: string): string { + const result = requireNonEmptyString(value, label); + if (!ID.test(result)) fail(`${label} is invalid`); + return result; +} +function instant(value: unknown, label: string): string { + const result = requireNonEmptyString(value, label); + if (result.length > 80 || !/^\d{4}-\d{2}-\d{2}T[^\s]{1,64}$/u.test(result)) fail(`${label} is invalid`); + return result; +} +function goalRef(value: unknown, goalId: string): JsonObject | null { + if (value === null || value === undefined) return null; + const ref = requireJsonObject(value, "Goal reference"); + if (Object.keys(ref).sort().join(",") !== "goal_id,goal_instance_id" + || ref.goal_id !== goalId) fail("Goal reference is invalid"); + return {goal_id: goalId, goal_instance_id: opaque(ref.goal_instance_id, "Goal instance")}; +} +function canonical(value: unknown): string { + if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`; + const row = jsonObject(value); + if (row) return `{${Object.keys(row).sort().map(key => `${JSON.stringify(key)}:${canonical(row[key])}`).join(",")}}`; + return JSON.stringify(value ?? null); +} +function digest(value: unknown): string { return createHash("sha256").update(canonical(value)).digest("hex"); } +function scope(params: JsonObject): {root: string; goalId: string; agentId: string; goalRef: JsonObject | null} { + const runtime = requireNonEmptyString(params.runtime_root, "runtime root"); + if (!isAbsolute(runtime)) fail("native child result runtime root must be absolute"); + const goalId = opaque(params.goal_id, "Goal"), agentId = opaque(params.agent_id, "Agent"); + const ref = goalRef(params.goal_ref, goalId); + const key = digest({goal_ref: ref, goal_id: ref ? null : goalId, agent_id: agentId}); + return {root: toNamespacedPath(join(runtime, ".local", "native-child-result-inbox", key)), + goalId, agentId, goalRef: ref}; +} +function resultIdentity(source: ReturnType, turn: string, operation: string): string { + return `ncr_${digest({goal_ref: source.goalRef, goal_id: source.goalRef ? null : source.goalId, + agent_id: source.agentId, turn_instance_id: turn, operation_id: operation}).slice(0, 32)}`; +} +function metadataPath(root: string, id: string): string { return join(root, `${id}.json`); } +function contentPath(root: string, id: string): string { return join(root, `${id}.txt`); } +async function readJson(path: string): Promise { + const handle = await open(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0) + | (constants.O_NOFOLLOW ?? 0)); + try { + const info = await handle.stat(); + if (!info.isFile() || info.size > 128_000) fail("native child result metadata is invalid"); + const text = await handle.readFile("utf8"); + return requireJsonObject(JSON.parse(text), "native child result metadata"); + } catch (error) { + if (error instanceof SyntaxError || error instanceof TypeError) fail("native child result metadata is invalid"); + throw error; + } finally { await handle.close(); } +} +function identityMatches(row: JsonObject, source: ReturnType): boolean { + return row.schema_version === NATIVE_CHILD_RESULT_ENTRY_SCHEMA + && row.goal_id === source.goalId && row.agent_id === source.agentId + && canonical(row.goal_ref ?? null) === canonical(source.goalRef); +} +async function readEntry(source: ReturnType, id: string): Promise { + if (!RESULT_ID.test(id)) fail("native child result id is invalid"); + const row = await readJson(metadataPath(source.root, id)); + if (!identityMatches(row, source) || row.result_id !== id) fail("native child result scope mismatch"); + const expected = ["acknowledgement", "agent_id", "content_bytes", "content_sha256", "created_at", + "delivery", "goal_id", "goal_ref", "operation_id", "outcome", "result_id", "schema_version", + "turn_instance_id"]; + if (Object.keys(row).sort().join(",") !== expected.join(",")) fail("native child result metadata is invalid"); + opaque(row.turn_instance_id, "Turn"); + opaque(row.operation_id, "operation"); + requireStringLiteral(row.outcome, [...OUTCOMES], "native child result outcome"); + if (typeof row.content_sha256 !== "string" || !/^sha256:[0-9a-f]{64}$/u.test(row.content_sha256) + || !Number.isSafeInteger(row.content_bytes) || Number(row.content_bytes) < 0 + || Number(row.content_bytes) > MAX_CONTENT_BYTES) fail("native child result content identity is invalid"); + instant(row.created_at, "result creation time"); + const delivery = row.delivery === null ? null : jsonObject(row.delivery); + if (row.delivery !== null && (!delivery + || Object.keys(delivery).sort().join(",") !== "delivery_count,first_delivered_at,last_delivered_at" + || !Number.isSafeInteger(delivery.delivery_count) || Number(delivery.delivery_count) < 1 + || Number(delivery.delivery_count) > 1_000_000)) fail("native child result delivery is invalid"); + if (delivery) { + instant(delivery.first_delivered_at, "first delivery time"); + instant(delivery.last_delivered_at, "last delivery time"); + } + const acknowledgement = row.acknowledgement === null ? null : jsonObject(row.acknowledgement); + if (row.acknowledgement !== null && (!acknowledgement + || Object.keys(acknowledgement).sort().join(",") !== "acknowledged_at,disposition")) { + fail("native child result acknowledgement is invalid"); + } + if (acknowledgement) { + if (!delivery) fail("native child result acknowledgement has no delivery"); + instant(acknowledgement.acknowledged_at, "acknowledgement time"); + requireStringLiteral(acknowledgement.disposition, [...DISPOSITIONS], "native child result disposition"); + } + return row; +} +async function readContent(path: string): Promise { + const handle = await open(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0) + | (constants.O_NOFOLLOW ?? 0)); + try { + const info = await handle.stat(); + if (!info.isFile() || info.size > MAX_CONTENT_BYTES) fail("native child result content is invalid"); + return await handle.readFile("utf8"); + } finally { await handle.close(); } +} +async function ingest(params: JsonObject): Promise { + const source = scope(params); + const turn = opaque(params.turn_instance_id, "Turn"), operation = opaque(params.operation_id, "operation"); + const outcome = requireStringLiteral(params.outcome, [...OUTCOMES], "native child result outcome"); + const observedAt = instant(params.observed_at, "result observation time"); + if (typeof params.text !== "string") fail("native child result text is required"); + const text = params.text; + if (/\p{Surrogate}/u.test(text)) fail("native child result text encoding is invalid"); + const bytes = Buffer.byteLength(text, "utf8"); + if (bytes > MAX_CONTENT_BYTES) fail("native child result text is too large"); + const id = resultIdentity(source, turn, operation), path = metadataPath(source.root, id); + const contentSha256 = `sha256:${createHash("sha256").update(text, "utf8").digest("hex")}`; + return withFileMutationLock(path, async () => { + let existing: JsonObject | null = null; + try { existing = await readEntry(source, id); } + catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; } + const identity: JsonObject = {schema_version: NATIVE_CHILD_RESULT_ENTRY_SCHEMA, result_id: id, + goal_id: source.goalId, agent_id: source.agentId, goal_ref: source.goalRef, + turn_instance_id: turn, operation_id: operation, outcome, + content_sha256: contentSha256, content_bytes: bytes}; + if (existing) { + if (Object.entries(identity).some(([key, value]) => canonical(existing![key]) !== canonical(value))) { + fail("native child result identity already has conflicting content"); + } + return {schema_version: NATIVE_CHILD_RESULT_INBOX_SCHEMA, ok: true, result_id: id, + created: false, replay: true}; + } + await atomicWriteText(contentPath(source.root, id), text); + await atomicWriteJson(path, {...identity, created_at: observedAt, delivery: null, acknowledgement: null}); + return {schema_version: NATIVE_CHILD_RESULT_INBOX_SCHEMA, ok: true, result_id: id, + created: true, replay: false}; + }); +} +async function listEntries(params: JsonObject): Promise { + const source = scope(params); + let names: string[] = []; + try { names = (await readdir(source.root)).filter(name => /^ncr_[0-9a-f]{32}\.json$/u.test(name)).sort(); } + catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; } + const scanComplete = names.length <= MAX_SCAN; + names = names.slice(0, MAX_SCAN); + const rows: JsonObject[] = [], unavailable: string[] = []; + for (const name of names) { + const id = name.slice(0, -5); + try { + const row = await readEntry(source, id); + if (row.acknowledgement === null) rows.push(row); + } catch { unavailable.push(id); } + } + rows.sort((left, right) => Number(left.delivery !== null) - Number(right.delivery !== null) + || String(left.created_at).localeCompare(String(right.created_at)) || String(left.result_id).localeCompare(String(right.result_id))); + const items: JsonObject[] = []; + let inlineBytes = 0; + let inspected = 0; + for (const row of rows) { + if (items.length >= PAGE_SIZE) break; + inspected += 1; + const file = contentPath(source.root, String(row.result_id)); + const base: JsonObject = Object.fromEntries(["result_id", "turn_instance_id", "operation_id", "outcome", + "content_sha256", "content_bytes", "created_at", "delivery"].map(key => [key, row[key]])); + const size = Number(row.content_bytes); + try { + if (Number.isSafeInteger(size) && size >= 0 && size <= INLINE_CONTENT_BYTES + && inlineBytes + size <= MAX_INLINE_PAGE_BYTES) { + const text = await readContent(file); + if (`sha256:${createHash("sha256").update(text, "utf8").digest("hex")}` !== row.content_sha256) { + unavailable.push(String(row.result_id)); continue; + } + base.text = text; inlineBytes += size; + } else { + const text = await readContent(file); + if (Buffer.byteLength(text, "utf8") !== size + || `sha256:${createHash("sha256").update(text, "utf8").digest("hex")}` !== row.content_sha256) { + unavailable.push(String(row.result_id)); continue; + } + base.content_file = file; + } + } catch { + unavailable.push(String(row.result_id)); continue; + } + items.push(base); + } + return {schema_version: NATIVE_CHILD_RESULT_INBOX_SCHEMA, ok: true, items, + has_more: !scanComplete || inspected < rows.length, pending_count: rows.length, + scan_complete: scanComplete, + unavailable_count: unavailable.length}; +} +async function markDelivered(params: JsonObject): Promise { + const source = scope(params), deliveredAt = instant(params.observed_at, "delivery time"); + if (!Array.isArray(params.result_ids) || params.result_ids.length > PAGE_SIZE) fail("bounded native child result ids are required"); + if (new Set(params.result_ids.map(id => requireNonEmptyString(id, "native child result id"))).size + !== params.result_ids.length) fail("native child result ids must be unique"); + for (const raw of params.result_ids) { + const id = requireNonEmptyString(raw, "native child result id"), path = metadataPath(source.root, id); + await withFileMutationLock(path, async () => { + const row = await readEntry(source, id), delivery = jsonObject(row.delivery); + await atomicWriteJson(path, {...row, delivery: {first_delivered_at: delivery?.first_delivered_at ?? deliveredAt, + last_delivered_at: deliveredAt, delivery_count: Math.min(Number(delivery?.delivery_count ?? 0) + 1, 1_000_000)}}); + }); + } + return {schema_version: NATIVE_CHILD_RESULT_INBOX_SCHEMA, ok: true, delivered_count: params.result_ids.length}; +} +async function acknowledge(params: JsonObject): Promise { + const source = scope(params), id = requireNonEmptyString(params.result_id, "native child result id"); + const turn = opaque(params.turn_instance_id, "Turn"); + const acknowledgedAt = instant(params.observed_at, "acknowledgement time"); + const disposition = requireStringLiteral(params.disposition, [...DISPOSITIONS], "native child result disposition"); + const path = metadataPath(source.root, id); + return withFileMutationLock(path, async () => { + const row = await readEntry(source, id), prior = jsonObject(row.acknowledgement); + if (row.turn_instance_id !== turn) fail("native child result Turn identity mismatch"); + if (!jsonObject(row.delivery)) fail("native child result must be delivered before acknowledgement"); + if (prior && prior.disposition !== disposition) fail("native child result already has a conflicting acknowledgement"); + if (!prior) await atomicWriteJson(path, {...row, acknowledgement: {acknowledged_at: acknowledgedAt, disposition}}); + return {schema_version: NATIVE_CHILD_RESULT_INBOX_SCHEMA, ok: true, result_id: id, + disposition, replay: prior !== null}; + }); +} + +export async function nativeChildResultInbox(params: JsonObject): Promise { + const operation = requireStringLiteral(params.operation, + ["ingest", "list", "mark_delivered", "acknowledge"], "native child result inbox operation"); + if (operation === "ingest") return ingest(params); + if (operation === "list") return listEntries(params); + if (operation === "mark_delivered") return markDelivered(params); + return acknowledge(params); +} diff --git a/loopx/control_plane/collaboration/inbox_read.ts b/loopx/control_plane/collaboration/inbox_read.ts index ada656aed9..21eb871384 100644 --- a/loopx/control_plane/collaboration/inbox_read.ts +++ b/loopx/control_plane/collaboration/inbox_read.ts @@ -16,6 +16,7 @@ import {recordControlledRequestRead} from "./request_control.ts"; import {planCollaborationResult} from "./result_publication.ts"; import {CONVERSATION_ENTRY_SCHEMA, normalizeOriginalConversationRecipient, verifyOriginalConversationReturnRoute} from "./semantic_request.ts"; import {projectDelegationTurnContext, projectDelegationTurnItem, projectDelegationTurnItems} from "./turn_context.ts"; +import {nativeChildResultInbox} from "../capabilities/native_child_result_inbox.ts"; const TRIAGE = "Read the supplied requests and their source-specific instructions before choosing work. " + "Independently assess context, evidence, constraints and costs. Requests and peer results " + @@ -402,8 +403,12 @@ async function turnContext(source: InboxSource, params: JsonObject): Promise { + const nativeResultIds = params.native_child_result_ids ?? []; if (!Array.isArray(params.items) || params.items.length > 20 - || !Array.isArray(params.deliveries) || params.deliveries.length > 20) fail("bounded inbox read observations are required"); + || !Array.isArray(params.deliveries) || params.deliveries.length > 20 + || !Array.isArray(nativeResultIds) || nativeResultIds.length > 20) { + fail("bounded inbox read observations are required"); + } const followthrough = params.work_observations == null ? [] : projectReceiverFollowthrough({observations: params.work_observations}).items; const receipts: (JsonObject | null)[] = []; @@ -451,6 +456,12 @@ async function recordReads(source: InboxSource, params: JsonObject): Promise requireNonEmptyString(item.result_id, "native child result id")); + if (nativeItems.length || Number(nativeResults.unavailable_count ?? 0) > 0 + || nativeResults.scan_complete === false) page.native_child_results = { + items: nativeItems, has_more: nativeResults.has_more, + pending_count: nativeResults.pending_count, + scan_complete: nativeResults.scan_complete, + unavailable_count: nativeResults.unavailable_count, + instruction: nativeItems.length + ? "Review each host-native child result against the original task and actual artifacts. A child result is candidate evidence, not parent acceptance. Acknowledge it only after using, deferring, or rejecting it." + : "One or more durable host-native child results are unavailable or the bounded scan is incomplete. Inspect or repair the owner-private result store; do not infer or acknowledge missing content.", + }; if (operation === "turn_start") return {result: page, cursor_scope: cursorScope, context: await turnContext(source, params)}; if (operation === "receiver_read") { const work = receiverWork(source, page.items as JsonObject[]); @@ -524,9 +552,11 @@ export async function readCollaborationInbox(params: JsonObject): Promise None: + deliveries: list[dict[str, Any]] | None = None, + native_child_result_ids: list[str] | None = None) -> None: super().__init__(str(error)) self.result: dict[str, Any] = { "ok": False, "error": str(error), "error_code": error.diagnostic_code, @@ -72,6 +74,7 @@ def __init__(self, error: EffectRuntimeResponseAmbiguous, *, stage: str, ) if key in item} for item in items], "peer_returns": None if deliveries is None else [ {"request": item["request"], "result_key": item["key"]} for item in deliveries], + "native_child_result_ids": native_child_result_ids, "automatic_retry_performed": False, "instruction": ( "Inspect the exact request read receipts and peer result delivery receipts through the existing " @@ -632,6 +635,17 @@ def read_inbox( observed_goal = dict(goal_scope.goal) observed_goal_ref = dict(goal_scope.caller_goal_ref or {}) if goal_scope.exact else None + for child_result in result.get("native_child_results", {}).get("items", []): + command = ["loopx", "--registry", str(registry), "--runtime-root", str(root), + "--format", "json", "native-child", "acknowledge-result", + "--goal-id", goal_id, "--agent-id", agent_id, + "--turn-instance-id", child_result["turn_instance_id"], + "--result-id", child_result["result_id"], + "--disposition", "", "--execute"] + if observed_goal_ref: + command.extend(["--goal-instance-id", observed_goal_ref["goal_instance_id"]]) + child_result["acknowledge_command"] = shlex.join(command) + if not snapshot.get("completed"): work = receiver_work_observations(root, registry, result["items"]) # Input hashing remains outside Goal lifetime admission. @@ -648,11 +662,13 @@ def read_inbox( "operation": "record_read", "goal_id": goal_id, "agent_id": agent_id, "items": result["items"], "deliveries": snapshot["deliveries"], "work_observations": work, + "native_child_result_ids": snapshot.get("native_child_result_ids", []), "include_context": request_id is None, "read_command": read_command, }, scope=goal_scope) except EffectRuntimeResponseAmbiguous as exc: raise InboxReadResponseAmbiguous(exc, stage="record_read", scope=read_scope, - read_arguments=read_arguments, items=result["items"], deliveries=snapshot["deliveries"]) from exc + read_arguments=read_arguments, items=result["items"], deliveries=snapshot["deliveries"], + native_child_result_ids=snapshot.get("native_child_result_ids", [])) from exc else: final = snapshot for item in result["items"]: @@ -677,6 +693,12 @@ def read_inbox( "Finish the original request with return_result, including evidence and remaining gaps. " "Adoption, file hashes and returned opinions are not independent acceptance or Todo completion." ) + if result.get("native_child_results", {}).get("items"): + result["followthrough"] += ( + " For each native_child_result, inspect the result and its evidence, then run native-child " + "acknowledge-result using its acknowledge_command and accepted/deferred/rejected disposition. " + "Until acknowledgement, the same stable result may be delivered again after restart." + ) if request_id is not None: result["followthrough"] = ( "Inspect this exact request's current source, revisions and receipts. " diff --git a/loopx/control_plane/effect_runtime_handlers.ts b/loopx/control_plane/effect_runtime_handlers.ts index 8e3922cb15..5e45fc2eda 100644 --- a/loopx/control_plane/effect_runtime_handlers.ts +++ b/loopx/control_plane/effect_runtime_handlers.ts @@ -669,6 +669,7 @@ export function createEffectRuntimeHandlers( ["collaboration.request.amendment_readback", lazyHandler(() => import("./collaboration/request_control.ts"), ({requestAmendmentReadback}) => requestAmendmentReadback)], ["goal.amendment.request_receipt", lazyHandler(() => import("./goals/goal_amendment_authority.ts"), ({projectRequestAmendmentReceipt}) => projectRequestAmendmentReceipt)], ["collaboration.inbox.read", lazyHandler(() => import("./collaboration/inbox_read.ts"), ({readCollaborationInbox}) => readCollaborationInbox)], + ["multi_subagent.native_result_inbox", lazyHandler(() => import("./capabilities/native_child_result_inbox.ts"), ({nativeChildResultInbox}) => nativeChildResultInbox)], ["collaboration.result.plan_publication", lazyHandler(() => import("./collaboration/result_publication.ts"), ({planCollaborationResult}) => planCollaborationResult)], ["collaboration.result.attachment_refs", lazyHandler(() => import("./collaboration/result_publication.ts"), ({resultAttachmentRefs}) => resultAttachmentRefs)], ["collaboration.result.delivery_ready", lazyHandler(() => import("./collaboration/result_publication.ts"), ({collaborationResultDeliveryReady}) => collaborationResultDeliveryReady)], diff --git a/loopx/extensions/codex_native_child.py b/loopx/extensions/codex_native_child.py index e0fdc9b889..a0cf5a448f 100644 --- a/loopx/extensions/codex_native_child.py +++ b/loopx/extensions/codex_native_child.py @@ -1,7 +1,8 @@ """Transient Codex host events adapted to native-child receipts. Only opaque identities and typed outcomes reach the existing multi_subagent -log. Prompts, child messages and raw tool output are never retained. +log. A supported host may retain the final child message in the coordinator's +owner-private local inbox; prompts and raw tool output are never retained. """ from __future__ import annotations @@ -14,6 +15,9 @@ from ..capabilities.multi_subagent.native_child_receipts import ( _load_native_child_events, record_native_child, ) +from ..capabilities.multi_subagent.native_child_result_inbox import ( + ingest_native_child_result, +) def configured_native_child_limit(request: Mapping[str, Any]) -> int | None: @@ -160,6 +164,18 @@ def observe(self, item: Mapping[str, Any], *, session_id: str, invocation_id: st wait_ref = "codex-wait-" + hashlib.sha256(identity.encode()).hexdigest()[:32] operation_id = self._restore_operation(child, wait_ref=wait_ref) if wait_ref else decision_id if operation_id is not None: + message = state.get("message") + if isinstance(message, str): + ingest_native_child_result( + runtime_root=self.runtime_root, + goal_id=self.lineage["goal_id"], + agent_id=self.lineage["agent_id"], + turn_instance_id=self.turn_instance_id, + operation_id=operation_id, + outcome=outcome, + text=message, + goal_ref=self.goal_ref, + ) self._record(stage="result", operation_id=operation_id, outcome=outcome, _host_wait_ref=wait_ref) diff --git a/tests/capabilities/test_codex_native_child_receipts.py b/tests/capabilities/test_codex_native_child_receipts.py index ee6d13ffae..321c33e58d 100644 --- a/tests/capabilities/test_codex_native_child_receipts.py +++ b/tests/capabilities/test_codex_native_child_receipts.py @@ -56,6 +56,14 @@ def test_host_spawn_result_parent_review_and_restart(tmp_path: Path): assert len(events) == 4 assert "private child" not in json.dumps(events) assert all(event.get("event_kind") != "quota_spend" for event in events) + from loopx.control_plane.effect_runtime import effect_runtime_result + inbox = effect_runtime_result("multi_subagent.native_result_inbox", { + "operation": "list", "runtime_root": str(tmp_path), + "goal_id": GOAL, "agent_id": AGENT, "goal_ref": None, + }) + assert len(inbox["items"]) == 1 + assert inbox["items"][0]["text"] == "private child result" + assert inbox["items"][0]["operation_id"] == operation["operation_id"] @pytest.mark.parametrize("items", [[], [{"type": "agentMessage", "text": "I spawned three children"}], diff --git a/tests/capabilities/test_native_child_receipts.py b/tests/capabilities/test_native_child_receipts.py index 34d91f8786..a571cbe3dc 100644 --- a/tests/capabilities/test_native_child_receipts.py +++ b/tests/capabilities/test_native_child_receipts.py @@ -304,6 +304,53 @@ def test_cli_readback_and_disabled_policy_do_not_mutate(tmp_path: Path): assert "enabled multi_subagent" in json.loads(rejected.stdout)["error"] +def test_cli_acknowledges_only_a_delivered_native_child_result(tmp_path: Path): + from loopx.capabilities.multi_subagent.native_child_result_inbox import ( + ingest_native_child_result, + ) + from loopx.control_plane.effect_runtime import effect_runtime_result + + registry = tmp_path / "registry.json" + registry.write_text(json.dumps({"schema_version": 1, "goals": [{ + "id": GOAL, "repo": str(tmp_path), "status": "active", + "registered_agents": [AGENT], + "spawn_policy": {"mode": "multi_subagent", "allowed": True, "max_children": 6}, + }]})) + runtime_root = tmp_path / "runtime" + created = ingest_native_child_result( + runtime_root=runtime_root, goal_id=GOAL, agent_id=AGENT, + turn_instance_id=TURN, operation_id="op-result-1", + outcome="completed", text="Review result.", + ) + effect_runtime_result("multi_subagent.native_result_inbox", { + "operation": "mark_delivered", "runtime_root": str(runtime_root), + "goal_id": GOAL, "agent_id": AGENT, "goal_ref": None, + "result_ids": [created["result_id"]], + "observed_at": "2026-11-13T00:00:00.000000+00:00", + }) + command = [sys.executable, "-m", "loopx.cli", "--registry", str(registry), + "--runtime-root", str(runtime_root), "--format", "json", + "native-child", "--goal-id", GOAL, "--agent-id", AGENT, + "--turn-instance-id", TURN, "acknowledge-result", + "--result-id", created["result_id"], "--disposition", "accepted"] + preview = subprocess.run(command, text=True, capture_output=True) + assert preview.returncode == 0, preview.stderr + assert json.loads(preview.stdout)["preview"] is True + assert len(effect_runtime_result("multi_subagent.native_result_inbox", { + "operation": "list", "runtime_root": str(runtime_root), + "goal_id": GOAL, "agent_id": AGENT, "goal_ref": None, + })["items"]) == 1 + disabled = json.loads(registry.read_text()) + disabled["goals"][0]["spawn_policy"]["allowed"] = False + registry.write_text(json.dumps(disabled)) + applied = subprocess.run([*command, "--execute"], text=True, capture_output=True) + assert applied.returncode == 0, applied.stderr + assert effect_runtime_result("multi_subagent.native_result_inbox", { + "operation": "list", "runtime_root": str(runtime_root), + "goal_id": GOAL, "agent_id": AGENT, "goal_ref": None, + })["items"] == [] + + def test_goal_status_only_attaches_reported_activity_to_enabled_goal( tmp_path: Path, monkeypatch, ): diff --git a/tests/control_plane_ts/native_child_result_inbox.test.ts b/tests/control_plane_ts/native_child_result_inbox.test.ts new file mode 100644 index 0000000000..b067c82ed3 --- /dev/null +++ b/tests/control_plane_ts/native_child_result_inbox.test.ts @@ -0,0 +1,121 @@ +import assert from "node:assert/strict"; +import {readFile, rm, writeFile} from "node:fs/promises"; +import {mkdtempSync} from "node:fs"; +import {tmpdir} from "node:os"; +import {join} from "node:path"; +import test from "node:test"; + +import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; +import {nativeChildResultInbox} from "../../loopx/control_plane/capabilities/native_child_result_inbox.ts"; +import {readCollaborationInbox} from "../../loopx/control_plane/collaboration/inbox_read.ts"; + +const goalRef = {goal_id: "research", goal_instance_id: "ginst_" + "a".repeat(32)}; +const base = {goal_id: "research", agent_id: "coordinator", goal_ref: goalRef}; +const at = "2026-11-13T00:00:00.000000+00:00"; + +function fixture(t: test.TestContext) { + const runtime_root = mkdtempSync(join(tmpdir(), "loopx-native-result-")); + t.after(() => rm(runtime_root, {recursive: true, force: true})); + const call = (operation: string, extra: JsonObject = {}) => nativeChildResultInbox({ + operation, runtime_root, ...base, ...extra, + }); + const inbox = (operation: string, extra: JsonObject = {}) => readCollaborationInbox({ + operation, runtime_root, ...base, observed_at: at, + read_command: "loopx manager-inbox read", + scope: {profile_id: "source_session_v1", caller_goal_ref: goalRef, current_goal_ref: goalRef}, + ...extra, + }); + return {runtime_root, call, inbox}; +} + +test("a completed host result survives replay and enters the next Turn unread", async t => { + const f = fixture(t), input = {turn_instance_id: "turn-1", operation_id: "operation-1", + outcome: "completed", text: "Independent review passed.", observed_at: at}; + const created = await f.call("ingest", input), replay = await f.call("ingest", input); + assert.equal(created.created, true); + assert.equal(replay.replay, true); + assert.equal(replay.result_id, created.result_id); + + const turn = await f.inbox("turn_start"), page = turn.result as JsonObject; + const [result] = (page.native_child_results as JsonObject).items as JsonObject[]; + assert.equal(result.result_id, created.result_id); + assert.equal(result.text, input.text); + assert.equal(result.delivery, null); + + const delivered = await f.inbox("receiver_read"); + assert.equal(delivered.completed, true); + const next = await f.call("list"), [again] = next.items as JsonObject[]; + assert.equal((again.delivery as JsonObject).delivery_count, 1); + assert.equal(again.result_id, created.result_id); +}); + +test("acknowledgement follows delivery and restart replay does not duplicate the effect", async t => { + const f = fixture(t), input = {turn_instance_id: "turn-1", operation_id: "operation-1", + outcome: "completed", text: "Use this once.", observed_at: at}; + const created = await f.call("ingest", input); + await assert.rejects(f.call("acknowledge", {result_id: created.result_id, + turn_instance_id: "turn-1", disposition: "accepted", observed_at: at}), /delivered before acknowledgement/); + await f.inbox("receiver_read"); + const first = await f.call("acknowledge", {result_id: created.result_id, + turn_instance_id: "turn-1", disposition: "accepted", observed_at: at}); + const replay = await f.call("acknowledge", {result_id: created.result_id, + turn_instance_id: "turn-1", disposition: "accepted", observed_at: "2026-11-13T00:01:00.000000+00:00"}); + assert.equal(first.replay, false); + assert.equal(replay.replay, true); + assert.deepEqual((await f.call("list")).items, []); + await assert.rejects(f.call("acknowledge", {result_id: created.result_id, + turn_instance_id: "turn-1", disposition: "rejected", observed_at: at}), /conflicting acknowledgement/); +}); + +test("conflicting content cannot replace an operation result", async t => { + const f = fixture(t), input = {turn_instance_id: "turn-1", operation_id: "operation-1", + outcome: "completed", text: "Original", observed_at: at}; + await f.call("ingest", input); + await assert.rejects(f.call("ingest", {...input, text: "Replacement"}), /conflicting content/); + const [result] = (await f.call("list")).items as JsonObject[]; + assert.equal(result.text, "Original"); +}); + +test("large result text stays in a private file while the inbox keeps a verified reference", async t => { + const f = fixture(t), text = "结果".repeat(40_000); + await f.call("ingest", {turn_instance_id: "turn-1", operation_id: "operation-1", + outcome: "completed", text, observed_at: at}); + const [result] = (await f.call("list")).items as JsonObject[]; + assert.equal(result.text, undefined); + assert.equal(await readFile(String(result.content_file), "utf8"), text); + assert.match(String(result.content_sha256), /^sha256:[0-9a-f]{64}$/u); +}); + +test("corrupt content stays visibly unavailable and cannot starve later results", async t => { + const f = fixture(t), text = "a".repeat(40_000); + const first = await f.call("ingest", {turn_instance_id: "turn-1", operation_id: "operation-1", + outcome: "completed", text, observed_at: at}); + for (let index = 2; index <= 22; index += 1) { + await f.call("ingest", {turn_instance_id: `turn-${index}`, operation_id: `operation-${index}`, + outcome: "completed", text: `result-${index}`, observed_at: at}); + } + const listed = await f.call("list"), large = (listed.items as JsonObject[]) + .find(item => item.result_id === first.result_id); + assert.ok(large?.content_file); + await writeFile(String(large.content_file), "b".repeat(40_000), "utf8"); + const after = await f.call("list"); + assert.equal(after.unavailable_count, 1); + assert.equal((after.items as JsonObject[]).length, 20); + assert.equal(after.has_more, true); + const turn = await f.inbox("turn_start"), page = turn.result as JsonObject; + assert.equal((page.native_child_results as JsonObject).unavailable_count, 1); + await rm(String(large.content_file)); + const missing = await f.call("list"); + assert.equal(missing.unavailable_count, 1); + assert.equal((missing.items as JsonObject[]).length, 20); +}); + +test("Goal lifetime and Agent scope isolate private native results", async t => { + const f = fixture(t); + await f.call("ingest", {turn_instance_id: "turn-1", operation_id: "operation-1", + outcome: "completed", text: "Scoped", observed_at: at}); + assert.deepEqual((await nativeChildResultInbox({operation: "list", runtime_root: f.runtime_root, + ...base, agent_id: "another"})).items, []); + assert.deepEqual((await nativeChildResultInbox({operation: "list", runtime_root: f.runtime_root, + ...base, goal_ref: {...goalRef, goal_instance_id: "ginst_" + "b".repeat(32)}})).items, []); +}); diff --git a/tests/test_manager_context_tracking.py b/tests/test_manager_context_tracking.py index 4e486fb240..855fa0935e 100644 --- a/tests/test_manager_context_tracking.py +++ b/tests/test_manager_context_tracking.py @@ -67,6 +67,42 @@ def status(): assert status()["delivery"]["at"] == first["delivery"]["at"] +def test_next_turn_requires_reading_a_durable_native_child_result(fixture, capsys): + from loopx.capabilities.multi_subagent.native_child_result_inbox import ( + ingest_native_child_result, + ) + + root, registry, _session, _turn, _target = fixture + created = ingest_native_child_result( + runtime_root=root, + goal_id="research", + agent_id="worker", + turn_instance_id="turn-native-result", + operation_id="operation-native-result", + outcome="completed", + text="The independent check found one actionable boundary.", + ) + hook = turn_start_hook(root, registry, "research", "worker") + observed = hook.producer() + assert observed["status"] == "observed" + assert observed["agent_read_required"] is True + assert observed["observation_count"] == 1 + + args = Namespace(manager_inbox_action="read", goal_id="research", agent_id="worker") + assert handle_manager_inbox(args, registry, root) == 0 + read = json.loads(capsys.readouterr().out) + [result] = read["native_child_results"]["items"] + assert result["result_id"] == created["result_id"] + assert result["text"] == "The independent check found one actionable boundary." + assert "native-child acknowledge-result" in result["acknowledge_command"] + + # Delivery does not consume the result; without acknowledgement, restart + # projects the same stable identity again on the next Turn. + replay = turn_start_hook(root, registry, "research", "worker").producer() + assert replay["agent_read_required"] is True + assert replay["observation_count"] == 1 + + def test_legacy_decision_is_not_a_fabricated_read(fixture): root, registry, session, turn, target = fixture rid = deliver(root, registry, session=session, turn=turn, request=target)[ From 37b6add48b8bd09cbe82b273a1e286960bd21520 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:38:04 +0800 Subject: [PATCH 019/267] fix(lark): preserve group intake across private binding changes Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../lark/goal_topic_runtime_service.py | 108 +++++++--- tests/test_lark_private_conversations.py | 2 +- tests/test_lark_private_ingress.py | 200 +++++++++++++++--- 3 files changed, 249 insertions(+), 61 deletions(-) diff --git a/loopx/extensions/lark/goal_topic_runtime_service.py b/loopx/extensions/lark/goal_topic_runtime_service.py index ab3f437ef5..1933ea8410 100644 --- a/loopx/extensions/lark/goal_topic_runtime_service.py +++ b/loopx/extensions/lark/goal_topic_runtime_service.py @@ -5,6 +5,7 @@ from collections.abc import Callable, Mapping from concurrent.futures import Future, ThreadPoolExecutor from contextlib import ExitStack, nullcontext +from dataclasses import dataclass from datetime import datetime, timezone import hashlib import logging @@ -29,7 +30,23 @@ SnapshotProvider = Callable[[], Mapping[str, Any]] ProfilePoller = Callable[[str, threading.Event], None] ManagerRouteReconciler = Callable[[Mapping[str, Any]], Mapping[str, Any]] -ProfileWorkerFingerprint = tuple[str, str, str, tuple[str, ...]] + + +@dataclass(frozen=True) +class ProfileTransport: + cli_bin: str + app_id: str + consumer_lease_key: str + callback_chats: tuple[str, ...] + other_binding_id: str + + +@dataclass(frozen=True) +class ProfileWorkerFingerprint: + transport: ProfileTransport + owner_binding_id: str = "" + + ProfileWorker = tuple[threading.Event, threading.Thread, ProfileWorkerFingerprint] MessageConsumer = tuple[threading.Event, Popen[str], threading.Event] @@ -134,20 +151,11 @@ def step() -> None: with self._lock: worker = self._workers.get(profile) current = self._message_consumers.get(profile) - if worker is None or current is None or current[0] is not worker[0]: - continue - stop, process, requested = current - if stop.is_set() or requested.is_set() or process.poll() is not None: - continue - # End this message pipe and let it drain under the - # existing App lease. Stopping the worker discards - # transient events already read by the stream. - requested.set() - try: - process.terminate() - except OSError: - requested.clear() - logging.getLogger(__name__).warning("Lark message consumer could not reconnect") + if ( + worker is not None and worker[2].owner_binding_id + and current is not None and current[0] is worker[0] + ): + self._disconnect_message_consumer(current) except Exception: logging.getLogger(__name__).warning("Private Chat source observation is unavailable") ingress = None @@ -311,6 +319,28 @@ def health_snapshot(self) -> dict[str, dict[str, Any]]: with self._lock: return {profile: dict(health) for profile, health in self._health.items()} + @staticmethod + def _disconnect_message_consumer(consumer: MessageConsumer) -> bool: + """End the current pipe under the service lock and let the worker drain.""" + + stop, process, requested = consumer + if stop.is_set(): + return False + if not requested.is_set() and process.poll() is None: + requested.set() + try: + process.terminate() + except OSError: + requested.clear() + logging.getLogger(__name__).warning("Lark message consumer could not reconnect") + return False + return True + + @staticmethod + def _consumer_lease_key(profile: str, config: Mapping[str, str]) -> str: + identity = config.get("bot_app_id") or profile + return str(config.get("consumer_ref") or hashlib.sha256(identity.encode("utf-8")).hexdigest()[:32]) + def _profile_worker_fingerprint( self, snapshot: Mapping[str, Any], @@ -322,16 +352,18 @@ def _profile_worker_fingerprint( if self.action_service is not None else () ) - app_ref = str(profile_config.get("bot_app_id") or "") + app_id = str(profile_config.get("bot_app_id") or "") binding_id = str(profile_config.get("binding_id") or "") - # A same-App replacement changes which workspace receives live input. - # Keep the App lease identity, but retire the old worker on refresh. - worker_ref = f"{app_ref}:{binding_id}" if app_ref and binding_id else app_ref or binding_id - return ( - str(profile_config.get("cli_bin") or "lark-cli"), - worker_ref, - str(profile_config.get("consumer_ref") or ""), - callback_chats, + owner_binding_id = binding_id if app_id and profile_config.get("audience") == "owner" else "" + return ProfileWorkerFingerprint( + transport=ProfileTransport( + cli_bin=str(profile_config.get("cli_bin") or "lark-cli"), + app_id=app_id, + consumer_lease_key=self._consumer_lease_key(profile, profile_config), + callback_chats=callback_chats, + other_binding_id="" if owner_binding_id else binding_id, + ), + owner_binding_id=owner_binding_id, ) def _poll_profile(self, profile: str, stop: threading.Event) -> None: @@ -436,7 +468,10 @@ def handle_review_callback( private_binding = private_profiles.get(profile) if isinstance(private_profiles, Mapping) else None binding_id = private_binding.get("binding_id") if isinstance(private_binding, Mapping) else None reconnect_requested = threading.Event() - private_admitter = self._admit_private if self.private_conversations is not None else None + # A group-only connection must not pick up private work + # read before a newly added owner binding. + private_admitter = (self._admit_private if self.private_conversations is not None + and isinstance(private_binding, Mapping) else None) bound_owner = ( private_admitter is not None and isinstance(binding_id, str) and bool(binding_id) @@ -466,9 +501,7 @@ def start_consumer(args: list[str]) -> Popen[str]: app_id = str(profile_config.get("bot_app_id") or "") # A composed legacy route explicitly retains its original # profile lease. Otherwise both paths use the App digest. - digest = str(profile_config.get("consumer_ref") or ( - hashlib.sha256((app_id or profile).encode("utf-8")).hexdigest()[:32])) - digests = {digest} + digests = {self._consumer_lease_key(profile, profile_config)} if app_id: digests.add(hashlib.sha256(app_id.encode("utf-8")).hexdigest()[:32]) # A legacy/shared consumer holds both identities: wait for @@ -490,7 +523,7 @@ def capture(route: Mapping[str, object]) -> dict[str, object]: route, self.runtime_controller ) - process_options = {"process_factory": start_consumer} if bound_owner else {} + process_options = {"process_factory": start_consumer} if self.private_conversations is not None else {} try: result = runtime.stream_lark_goal_topic_profile( profile=profile, @@ -611,7 +644,22 @@ def refresh(self) -> None: if self._workers[profile][2] != desired[profile] } for profile in stale: - stop, _thread, _fingerprint = self._workers.pop(profile) + stop, thread, previous = self._workers[profile] + replacement = desired.get(profile) + consumer = self._message_consumers.get(profile) + if ( + replacement is not None + and previous.transport == replacement.transport + and consumer is not None + and consumer[0] is stop + ): + # Group routes still own their queued messages. The old + # private admission closure fences its revoked binding + # while the same worker drains under the existing App lease. + if self._disconnect_message_consumer(consumer): + self._workers[profile] = (stop, thread, replacement) + continue + self._workers.pop(profile) stop.set() missing = set(desired) - set(self._workers) for profile in sorted(missing): diff --git a/tests/test_lark_private_conversations.py b/tests/test_lark_private_conversations.py index b3e1d8cc59..0e35a7c3b1 100644 --- a/tests/test_lark_private_conversations.py +++ b/tests/test_lark_private_conversations.py @@ -2813,7 +2813,7 @@ def test_app_messages_and_answers_reach_the_original_chat_once_without_new_execu runtime.close() -def test_app_projection_discovers_a_restored_transcript_with_the_same_size_and_mtime(ordinary): +def test_app_projection_discovers_a_restored_transcript_with_the_same_size_and_mtime(ordinary): # noqa: F811 import os store, runtime, provider, transport, original, turns, before = _app_projection_journey(ordinary) diff --git a/tests/test_lark_private_ingress.py b/tests/test_lark_private_ingress.py index aaf21b2901..384a676609 100644 --- a/tests/test_lark_private_ingress.py +++ b/tests/test_lark_private_ingress.py @@ -192,6 +192,33 @@ def test_stale_or_unreadable_health_is_not_verified(source_monitor): assert f.monitor.health(f.binding["binding_id"])["status"] == "unavailable" +class _ListenerProcess: + """Yield fixture events and let the real service end the stream.""" + + def __init__(self, events): + self.exited = threading.Event() + self.waited = threading.Event() + self.stdout = self.lines(events) + + def lines(self, events): + yield "[event] ready event_key=im.message.receive_v1\n" + for event in events: + yield json.dumps(event) + assert self.exited.wait(8) + yield "[event] exited (reason: signal)\n" + + def poll(self): + return 0 if self.exited.is_set() else None + + def terminate(self): + self.exited.set() + + def wait(self, timeout=None): + assert self.exited.wait(timeout or 8) + self.waited.set() + return 0 + + @pytest.mark.parametrize("transition", ["reconnect", "shutdown", "revoke", "rebind_during_reconnect"]) def test_live_private_ingress_across_listener_transition(ordinary, tmp_path, monkeypatch, transition): # noqa: F811 from concurrent.futures import ThreadPoolExecutor @@ -232,42 +259,22 @@ def admit(profile_name, event, **kwargs): monkeypatch.setattr(lifecycle, "time", SimpleNamespace(monotonic=lambda: clock[0])) monkeypatch.setattr(Path, "home", lambda: tmp_path) - class Consumer: - def __init__(self, initial): - self.initial = initial - self.exited, self.waited = threading.Event(), threading.Event() - self.stdout = self.lines() - - def lines(self): - yield "[event] ready event_key=im.message.receive_v1\n" - if self.initial: - yield json.dumps(first) - assert in_admission.wait(8) - yield json.dumps(second) - waiting.set() # The reader advanced past submission of the second line. - else: - if transition == "reconnect": - yield json.dumps({**second, "event_id": "synthetic_provider_redelivery"}) - yield json.dumps(third) - assert self.exited.wait(8) - yield "[event] exited (reason: signal)\n" - - def poll(self): - return 0 if self.exited.is_set() else None + def first_connection(): + yield first + assert in_admission.wait(8) + yield second + waiting.set() # The reader advanced past submission of the second line. - def terminate(self): - self.exited.set() - - def wait(self, timeout=None): - assert self.exited.is_set() - self.waited.set() - return 0 + def replacement_connection(): + if transition == "reconnect": + yield {**second, "event_id": "synthetic_provider_redelivery"} + yield third def spawn(args): assert args[3:6] == ["event", "consume", "im.message.receive_v1"] if consumers: assert len(consumers) == 1 and consumers[0].waited.is_set() - consumer = Consumer(not consumers) + consumer = _ListenerProcess(replacement_connection() if consumers else first_connection()) consumers.append(consumer) return consumer @@ -337,6 +344,139 @@ def isolated_stream(**kwargs): assert len(consumers) == 1 +@pytest.mark.parametrize("binding_change", ["replace", "add", "remove"]) +def test_shared_listener_drains_group_inbox_when_private_workspace_changes( + ordinary, # noqa: F811 + tmp_path, monkeypatch, binding_change, +): + from pathlib import Path + + from extensions.test_lark_goal_topic_runtime import _reply_runner, _seed_legacy_topic + from loopx.extensions.lark import goal_topic_runtime as runtime + from loopx.extensions.lark.event_inbox import inspect_lark_event_inbox + from loopx.extensions.lark.goal_channel_contracts import read_goal_channel_binding + from loopx.extensions.lark.goal_channel_targets import read_goal_channel_targets + from test_lark_private_conversations import connect + + store, controller, provider, transport = connect(ordinary) + profile = "notes-app" + target_path, binding_path = tmp_path / "targets.json", tmp_path / "group-binding.json" + _seed_legacy_topic(target_path, binding_path) + targets = read_goal_channel_targets(target_path) + targets["targets"]["fixture"]["identity"].update( + sender_profile=profile, bot_app_id=provider.profile_apps[profile], + ) + bindings = {"goal-alpha": read_goal_channel_binding(binding_path)} + before = transport.profiles()[profile]["binding_id"] + if binding_change == "add": + transport.bindings.disconnect(before, expected_revision=transport.bindings.read()["revision"]) + config = transport.profiles().get(profile) + + def snapshot(): + return {"target_payload": targets, "binding_payloads": bindings, + "private_profiles": {profile: config} if config is not None else {}} + + group_events = [ + dict(provider.topic(name, f"@linkmacbot group {name}", root="om_topic_alpha", + chat="oc_public_fixture"), mentioned=True) + for name in ("first", "second", "third") + ] + new_private = provider.event(profile, "after_rebind", "/status") + in_group_answer, release, queued, next_stream_ready = (threading.Event() for _ in range(4)) + answered, consumer_processes = [], [] + replies = {} + + def answer(*, route, **_kwargs): + message_id = route["message_id"] + if message_id == group_events[0]["message_id"]: + in_group_answer.set() + assert release.wait(8) + answered.append(message_id) + return "Group answer" + + def admit(profile_name, event, **kwargs): + result = transport.admit(profile_name, event, **kwargs) + if event["message_id"] == new_private["message_id"]: + assert result["status"] == "command_recorded" + next_stream_ready.set() + return result + + def first_connection(): + yield group_events[0] + assert in_group_answer.wait(8) + yield group_events[1] + queued.set() + + def replacement_connection(): + yield group_events[2] if binding_change == "remove" else new_private + + def spawn(args): + assert args[3:6] == ["event", "consume", "im.message.receive_v1"] + if consumer_processes: + assert len(consumer_processes) == 1 and consumer_processes[0].waited.is_set() + consumer = _ListenerProcess(replacement_connection() if consumer_processes else first_connection()) + consumer_processes.append(consumer) + return consumer + + stream = runtime.stream_lark_goal_topic_profile + + def isolated_stream(**kwargs): + kwargs.setdefault("process_factory", spawn) + health_sink = kwargs["health_sink"] + + def observe_health(update): + health_sink(update) + if binding_change == "remove" and len(consumer_processes) == 2 and update.get("replied_count") == 1: + next_stream_ready.set() + + return stream(**{**kwargs, "health_sink": observe_health}, reply_runner=_reply_runner(replies)) + + monkeypatch.setattr(Path, "home", lambda: tmp_path) + monkeypatch.setattr(runtime, "_default_process_factory", spawn) + monkeypatch.setattr(runtime, "stream_lark_goal_topic_profile", isolated_stream) + monkeypatch.setattr(runtime, "answer_lark_goal_topic", answer) + service = runtime.LarkGoalTopicRuntimeService( + snapshot_provider=snapshot, runtime_root=store.root.parent, + runtime_controller=controller, private_conversations=SimpleNamespace(admit=admit), + ) + + try: + service.refresh() + assert queued.wait(8), service.health_snapshot() + if binding_change == "remove": + transport.bindings.disconnect(before, expected_revision=transport.bindings.read()["revision"]) + config = None + else: + projects = transport.bindings.projects + other = tmp_path / "other-workspace" + other.mkdir() + projects.roots.append((other, other)) + project = next(row for row in projects.available() if row["workspace_path"] == str(other)) + replacement = transport.bindings.configure( + transport_ref=profile, project_ref=project["project_ref"], executor_endpoint_id="codex", + ) + config = transport.profiles()[profile] + assert config["binding_id"] == replacement["binding_id"] != before + service.refresh() + assert consumer_processes[0].exited.wait(8) + assert len(consumer_processes) == 1 + release.set() + assert next_stream_ready.wait(8), service.health_snapshot() + expected = group_events if binding_change == "remove" else group_events[:2] + assert answered == [event["message_id"] for event in expected] + reply_targets = [call[call.index("--message-id") + 1] for call in replies["calls"] + if "+messages-reply" in call and "--dry-run" not in call] + assert reply_targets == answered + config_path, = (store.root.parent / ".loopx/config/lark-goal-topics").glob("*.json") + inbox = inspect_lark_event_inbox(project=store.root.parent, config_path=config_path) + assert inbox["processed_count"] == len(expected) + assert inbox["pending_count"] == 0 + finally: + release.set() + service.close() + controller.close() + + def test_unknown_failed_capture_is_not_replayed(source_monitor): f = source_monitor native = {**f.carrier, "status": "failed", "event": {**f.carrier["event"], "message_id": "om_missed"}} From eda1daf98b7c6755e5e0b263bf3a04f21c1c7601 Mon Sep 17 00:00:00 2001 From: "qinxiaojun.l" Date: Fri, 13 Nov 2026 13:46:58 +0800 Subject: [PATCH 020/267] fix: preserve intentional watches in replan history Signed-off-by: qinxiaojun.l Co-authored-by: TRAE CLI --- docs/quota-allocation.md | 10 ++++ .../control_plane/work_items/project_asset.py | 10 ++-- .../work_items/replan_history.ts | 15 +++++- .../work_items/replan_history_codec.py | 6 ++- .../test_replan_history_policy.py | 13 +++++ .../test_replan_history_transport.py | 30 +++++++++++ tests/control_plane_ts/replan_history.test.ts | 51 +++++++++++++++++++ 7 files changed, 130 insertions(+), 5 deletions(-) diff --git a/docs/quota-allocation.md b/docs/quota-allocation.md index 20e25c1b01..911b03561a 100644 --- a/docs/quota-allocation.md +++ b/docs/quota-allocation.md @@ -1177,6 +1177,16 @@ receipts have no concrete executed-monitor identity and do not count toward this threshold; distinct heartbeat turn ids are idempotency evidence, not monitor execution evidence. +A current open Monitor explicitly registered with `watch_only=true` represents +an intentional continuing watch. Matching unchanged history does not trigger +`dead_monitor_repeat`: the Todo id (when recorded), target key and agent scope +must match, and an expiring watch must still be active at the latest observation. +The same watch is excluded from current `monitor_no_change_streak` selection. +Missing or false watch state keeps ordinary finite-monitor detection; another +Todo, target or peer cannot discharge it. Independent typed-progress, +blocked-successor and periodic work reviews remain effective. This does not +change cadence, expiry, observation counters, receipts or spend rules. + The current Todo `consecutive_no_change` detector shares the Monitor lifetime rule with scheduling. At one explicit runtime observation, `expires_at` at or before that instant excludes that Monitor from `monitor_no_change_streak`. diff --git a/loopx/control_plane/work_items/project_asset.py b/loopx/control_plane/work_items/project_asset.py index c518bcb1ba..7b07aa09ee 100644 --- a/loopx/control_plane/work_items/project_asset.py +++ b/loopx/control_plane/work_items/project_asset.py @@ -319,6 +319,9 @@ def attach_active_state_project_asset_fields( else None ) if replan_obligation is None and autonomous_replan_obligation_from_runs is not None: + shared_todos = item.get("agent_todos") + monitor_todos = {"monitor_open_items": + shared_todos.get("monitor_open_items", []) if isinstance(shared_todos, dict) else []} agent_ids = sorted( { agent_id @@ -333,9 +336,10 @@ def attach_active_state_project_asset_fields( if ( obligation := autonomous_replan_obligation_from_runs( latest_runs or [], - # The shared todo summary is goal-scoped here. Quota derives - # todo-based replans later from its agent-filtered summary. - agent_todos=None, + # The TS history owner matches monitor intent by Todo, + # target and lane. Share those facts without importing + # goal-scoped advancement/resume decisions into this lane. + agent_todos=monitor_todos, agent_id=agent_id, ) ) diff --git a/loopx/control_plane/work_items/replan_history.ts b/loopx/control_plane/work_items/replan_history.ts index 53f30c0ccf..5692587b32 100644 --- a/loopx/control_plane/work_items/replan_history.ts +++ b/loopx/control_plane/work_items/replan_history.ts @@ -46,6 +46,7 @@ interface MonitorTodo { claim: string | null; publicClaim: string | null; target: string; + watchOnly: boolean; count: number; due: number | null; expiry: number | null; @@ -176,6 +177,7 @@ function decode(value: unknown): Request { id: optionalNonEmptyString(row.id, "monitor.id"), claim: optionalNonEmptyString(row.claim, "claim"), publicClaim: optionalNonEmptyString(row.public_claim, "public_claim"), target: text(row.target, "monitor.target"), + watchOnly: row.watch_only === undefined ? false : requireBoolean(row.watch_only, "watch_only"), count: integer(row.no_change_count, "no_change_count", Number.MIN_SAFE_INTEGER), due: time(row.due_at, "due_at"), expiry: time(row.expires_at, "expires_at"), })), @@ -323,6 +325,7 @@ function monitorTrigger(runs: readonly Run[], request: Request): Trigger | null }; } const repeated = signals.filter(run => (run.monitor.todo || run.monitor.key) && + !isIntentionalWatch(run, request, signals[0]!.observedAt) && ["due_monitor_observed_without_material_transition", "external_monitor_observed_without_material_transition"] .includes(run.monitor.mode!)).slice(0, request.monitor); const target = sole(repeated.map(run => run.monitor.target)); @@ -333,9 +336,19 @@ function monitorTrigger(runs: readonly Run[], request: Request): Trigger | null latest_generated_at: repeated[0]!.generatedAt, agent_id: sole(repeated.map(run => run.agent)), }; } +function isIntentionalWatch(run: Run, request: Request, observedAt: number | null): boolean { + const agent = run.monitorAgent ?? request.monitorAgent; + if (!agent) return false; + // Poll target_id is a digest; target_key (or a legacy plain target) names + // the canonical target. A key match cannot override a conflicting Todo id. + const target = run.monitor.key ?? run.monitor.target; + return request.todos.monitors.some(row => row.watchOnly && (!row.claim || row.claim === agent) && + row.target === target && (run.monitor.todo ? row.id === run.monitor.todo : !!run.monitor.key) && + (row.expiry === null || observedAt !== null && monitorIsActive({expiresAt: row.expiry}, observedAt))); +} function monitorStreak(request: Request): Trigger | null { const frontier = request.operation === "frontier_monitor_streak"; - const stalled = request.todos.monitors.filter(row => row.count >= request.streak && + const stalled = request.todos.monitors.filter(row => !row.watchOnly && row.count >= request.streak && (request.observedAt === null || monitorIsActive({expiresAt: row.expiry}, request.observedAt))) .sort((a, b) => b.count - a.count || (frontier ? authorityUnicodeCompare(a.target, b.target) : 0))[0]; if (!stalled || !frontier && request.todos.advancements.some(row => row.status === "open" && diff --git a/loopx/control_plane/work_items/replan_history_codec.py b/loopx/control_plane/work_items/replan_history_codec.py index c2f2d446d9..18fad82150 100644 --- a/loopx/control_plane/work_items/replan_history_codec.py +++ b/loopx/control_plane/work_items/replan_history_codec.py @@ -16,7 +16,10 @@ from ..effect_runtime import MAX_REQUEST_BYTES, EffectRuntimeRejected, effect_runtime_result from ..runtime.time import now_utc, parse_timestamp from ..goals.goal_vision_policy import normalize_effective_turn_replan_threshold -from ..todos.contract import normalize_todo_claimed_by, normalize_todo_id, normalize_todo_id_list +from ..todos.contract import ( + normalize_todo_claimed_by, normalize_todo_id, normalize_todo_id_list, + normalize_todo_watch_only, +) from ..todos.resume_planning import build_todo_resume_planning_request REPLAN_HISTORY_NEUTRAL_CLASSIFICATIONS = { @@ -119,6 +122,7 @@ def _todo_facts(value: Any, *, include_resume: bool, frontier: bool = False) -> "claim": normalize_todo_claimed_by(item.get("claimed_by")), "public_claim": str(item.get("claimed_by") or "").strip() or None, "target": str(item.get("target_key") or item.get("todo_id") or "monitor").strip(), + "watch_only": normalize_todo_watch_only(item.get("watch_only")) is True, "no_change_count": count, "due_at": _timestamp(item.get("next_due_at")), "expires_at": _timestamp(item.get("expires_at")), diff --git a/tests/control_plane/test_replan_history_policy.py b/tests/control_plane/test_replan_history_policy.py index 666127473a..85b50f8093 100644 --- a/tests/control_plane/test_replan_history_policy.py +++ b/tests/control_plane/test_replan_history_policy.py @@ -134,6 +134,19 @@ def test_six_distinct_polls_retain_dead_monitor_contract() -> None: assert result["dead_monitor_detector"]["run_count"] == 6 +def test_watch_only_state_reaches_the_history_policy_owner() -> None: + rows = [monitor(n, turn=f"poll-{n}") for n in range(6, 0, -1)] + for watch_only in (True, "true", False, "false", None): + todos = {"monitor_open_items": [{"todo_id": "todo_monitor_fixture", + "target_key": "target-fixture", "claimed_by": AGENT, + "watch_only": watch_only}]} + result = autonomous_replan_obligation_from_runs(rows, agent_todos=todos, agent_id=AGENT) + if watch_only in (True, "true"): + assert result is None + else: + assert result["triggers"][0]["kind"] == "dead_monitor_repeat" + + def test_genuinely_unknown_work_still_breaks_progress_streak() -> None: rows = [{**run(3), "progress_observation": observation()}, run(2), {**run(1), "progress_observation": observation()}] diff --git a/tests/control_plane/test_replan_history_transport.py b/tests/control_plane/test_replan_history_transport.py index d6ecd90831..73dfcc3c46 100644 --- a/tests/control_plane/test_replan_history_transport.py +++ b/tests/control_plane/test_replan_history_transport.py @@ -1,11 +1,41 @@ """Production CLI settlement with a synthetic long-lived history, no live state.""" import json +import pytest from tests.control_plane.test_quota_settlement_cli import ( AGENT_ID, GOAL_ID, TODO_ID, _run_cli, _spend_run_count, _write_fixture, + DUE_MONITOR_TODO_ID, _append_newly_due_monitor, _initialize_fixture_authority, ) +@pytest.mark.parametrize("watch_only", [True, False]) +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_cli_history_preserves_registered_watch_intent(tmp_path, watch_only, provider): + project, runtime, registry = _write_fixture(tmp_path) + _append_newly_due_monitor(project, watch_only=watch_only) + _initialize_fixture_authority(project, runtime, provider=provider) + index = runtime / "goals" / GOAL_ID / "runs" / "index.jsonl" + index.parent.mkdir(parents=True, exist_ok=True) + original = "".join(json.dumps({ + "agent_id": AGENT_ID, "classification": "quota_monitor_poll", + "generated_at": f"2026-01-01T00:00:{n:02d}Z", "turn_instance_id": f"poll-{n}", + "todo_id": DUE_MONITOR_TODO_ID, "target_key": "due-monitor-fixture", + "monitor_target": {"target_id": "target-digest", "agent_id": AGENT_ID, + "monitor_mode": "due_monitor_observed_without_material_transition"}, + "monitor_event": {"material_change": False}, + }) + "\n" for n in range(1, 7)).encode() + index.write_bytes(original) + code, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app", + "--goal-id", GOAL_ID, "--agent-id", AGENT_ID, "--turn-instance-id", "inspect-watch", + "--available-capability", "network", "--available-capability", "external_evidence_poll", + cwd=project) + assert code == 0, guard + kinds = [row["kind"] for row in (guard.get("autonomous_replan_obligation") or {}).get("triggers", [])] + assert ("dead_monitor_repeat" in kinds) is (not watch_only) + assert _spend_run_count(runtime) == 0 + assert index.read_bytes().startswith(original) + + def test_large_history_refresh_replay_and_spend_once(tmp_path): project, runtime, registry = _write_fixture(tmp_path, provider="sqlite") index = runtime / "goals" / GOAL_ID / "runs" / "index.jsonl" diff --git a/tests/control_plane_ts/replan_history.test.ts b/tests/control_plane_ts/replan_history.test.ts index 9f251af020..1c33c5d0b3 100644 --- a/tests/control_plane_ts/replan_history.test.ts +++ b/tests/control_plane_ts/replan_history.test.ts @@ -239,6 +239,57 @@ function monitorTodo(patch: JsonObject = {}): JsonObject { return { id: "todo_watch", claim: "worker-a", public_claim: "worker-a", target: "watch", no_change_count: 5, due_at: null, expires_at: null, ...patch }; } +test("intentional watches do not become dead monitors after unchanged observations", () => { + for (const watch_only of [true, false, undefined]) { + const todos = {monitors: [monitorTodo({watch_only})], advancements: [], resume: null}; + const before = structuredClone(todos); + assert.equal(trigger(many(6, poll), {todos})?.kind ?? null, + watch_only === true ? null : "dead_monitor_repeat"); + assert.deepEqual(todos, before); + } +}); + +test("watch exemptions require the observed Todo, target and lane", () => { + for (const patch of [{id: "other"}, {target: "other"}, {claim: "peer"}, {expires_at: 6}]) { + const todos = {monitors: [monitorTodo({watch_only: true, ...patch})], advancements: [], resume: null}; + assert.equal(trigger(many(6, poll), {todos})?.kind, "dead_monitor_repeat"); + } + const todos = {monitors: [monitorTodo({watch_only: true})], advancements: [], resume: null}; + assert.equal(trigger(many(6, n => poll(n, {target_id: "target-digest", target_key: "watch"})), {todos}), null, + "the recorded target digest is distinct from the canonical target key"); + assert.equal(trigger(many(6, n => poll(n, {todo_id: null, target_key: "watch"})), {todos}), null); + assert.equal(trigger(many(6, n => poll(n, {todo_id: "other", target_key: "watch"})), {todos})?.kind, + "dead_monitor_repeat", "a matching key cannot override a different Todo"); +}); + +test("intentional watch state does not discharge independent work reviews", () => { + const todos = {monitors: [monitorTodo({watch_only: true})], advancements: [], resume: null}; + assert.equal(trigger(many(2, n => run(n, {progress: observation})), {todos})?.kind, + "typed_progress_repeat"); + assert.equal(trigger(many(20), {todos})?.kind, "periodic_review_due"); + assert.equal(trigger(many(2, n => poll(n, { + mode: "blocked_successor_wait_without_material_transition", frontier: "frontier-a", + })), {todos})?.kind, "blocked_successor_no_progress_repeat"); +}); + +test("both current streak projections skip intentional watches and retain finite monitors", () => { + for (const operation of ["monitor_streak", "frontier_monitor_streak"]) { + const todos = {monitors: [monitorTodo({watch_only: true, no_change_count: 99})], + advancements: [], resume: null}; + assert.equal(trigger([], {operation, observed_at: 100, todos}), null); + assert.equal(trigger([], {operation, observed_at: 100, todos: {...todos, + monitors: [...todos.monitors, monitorTodo({id: "finite", target: "finite", watch_only: false})], + }})?.monitor_target_id, "finite"); + } +}); + +test("malformed watch state is rejected at the typed history boundary", () => { + for (const watch_only of ["true", 1, null]) { + const todos = {monitors: [monitorTodo({watch_only})], advancements: [], resume: null}; + assert.throws(() => trigger(many(6, poll), {todos}), /watch_only/); + } +}); + test("persisted monitor streak retains stable tie order and advancement ownership", () => { const todos = { monitors: [monitorTodo(), monitorTodo({ target: "second" })], advancements: [], resume: null }; assert.equal(trigger([], { operation: "monitor_streak", todos })?.monitor_target_id, "watch"); From af1540f9fec5c178905dc06d3d7028e438dfaf55 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:51:55 +0800 Subject: [PATCH 021/267] Read evaluated readiness for due Monitor polls Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/control_plane/quota/monitor_poll.py | 26 ++++++ .../test_settled_monitor_user_gate.py | 87 +++++++++++++++++-- 2 files changed, 108 insertions(+), 5 deletions(-) diff --git a/loopx/control_plane/quota/monitor_poll.py b/loopx/control_plane/quota/monitor_poll.py index bc4824b6d4..e4e8422b3f 100644 --- a/loopx/control_plane/quota/monitor_poll.py +++ b/loopx/control_plane/quota/monitor_poll.py @@ -21,6 +21,7 @@ TODO_TASK_CLASS_MONITOR, normalize_todo_claimed_by, normalize_todo_id, + normalize_todo_resume_when, ) from ..todos.external_wait_contract import ( build_monitor_advancement_authoring_contract, @@ -131,6 +132,31 @@ def resolve_due_monitor_candidate( ) except ValueError: return {} + resume_when = normalize_todo_resume_when(item.get("resume_when")) + if resume_when and item.get("resume_ready") is None: + from ..todos.list_readback import list_goal_todos + + # Target selection returns the stored Todo, where readiness may not be + # materialized. Read the canonical evaluation for that same target. + selected_id = normalize_todo_id(item.get("todo_id")) + if selected_id is None: + return {} + evaluated = list_goal_todos( + registry_path=registry_path, + goal_id=goal_id, + role="agent", + todo_id=selected_id, + runtime_root_arg=str(runtime_root) if runtime_root else None, + ).get("todo") + if ( + not evaluated + or evaluated.get("todo_id") != selected_id + or evaluated.get("target_key") != item.get("target_key") + or evaluated.get("archive_state") == "archive" + or normalize_todo_resume_when(evaluated.get("resume_when")) != resume_when + ): + return {} + item = evaluated if not monitor_todo_is_due(item): return {} return { diff --git a/tests/control_plane/test_settled_monitor_user_gate.py b/tests/control_plane/test_settled_monitor_user_gate.py index 7e04703e4c..96abd539d9 100644 --- a/tests/control_plane/test_settled_monitor_user_gate.py +++ b/tests/control_plane/test_settled_monitor_user_gate.py @@ -105,17 +105,24 @@ def call(*args): assert not any(row["classification"] in {"state_refreshed", "quota_slot_spent"} for row in rows) -def _primary_monitor_fixture(tmp_path, monkeypatch, provider, route="source"): +def _primary_monitor_fixture( + tmp_path, monkeypatch, provider, route="source", *, + resume_when=None, handoff_mode="soft_claim", +): isolate_sqlite_runtime(tmp_path, monkeypatch) - source, runtime, _state = _write_fixture(tmp_path, provider=provider) + source, runtime, _state = _write_fixture( + tmp_path, provider=provider, handoff_mode=handoff_mode) gated = add_goal_todo(registry_path=source, goal_id=GOAL_ID, role="agent", text="Await a scoped owner decision", task_class="advancement_task", status="blocked", claimed_by=AGENT_ID, agent_id=AGENT_ID) gate = add_goal_todo(registry_path=source, goal_id=GOAL_ID, role="user", text="Decide the dependent work", task_class="user_gate", bound_agent=AGENT_ID, blocks_agent=AGENT_ID, unblocks_todo_id=gated["todo_id"], agent_id=AGENT_ID) - monitor = _add_monitor(source, text="Observe a synthetic public target", - target_key="primary-target", next_due_at="2000-01-01T00:00:00Z") + monitor = add_goal_todo(registry_path=source, goal_id=GOAL_ID, role="agent", + text="Observe a synthetic public target", task_class="continuous_monitor", + claimed_by=AGENT_ID, resume_when=resume_when, + monitor_metadata={"target_key": "primary-target", "cadence": "1h", + "next_due_at": "2000-01-01T00:00:00Z", "watch_only": "true"}) registry = source if route == "global": registry = runtime / "registry.global.json" @@ -134,6 +141,76 @@ def _primary_monitor_args(command, turn, monitor): "--result-hash", "primary-observation", "--execute"] if command == "monitor-poll" else [])] +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("route", ["source", "global"]) +@pytest.mark.parametrize("condition", [ + "resume_at:1999-01-01T00:00:00Z", + "resume_at:2099-01-01T00:00:00Z", + "todo_done:todo_missing", +], ids=["elapsed", "pending", "unresolved"]) +def test_primary_monitor_poll_uses_canonical_resume_readiness( + tmp_path, monkeypatch, provider, route, condition, +): + elapsed = "resume_at:1999-01-01T00:00:00Z" + source, registry, runtime, _gated, gate, monitor = _primary_monitor_fixture( + tmp_path, monkeypatch, provider, route, resume_when=elapsed, handoff_mode="hard_lease") + turn = "primary-monitor-resume-readiness" + + def call(*args): + return run_json_cli_result(*args, registry_path=registry, runtime_root=runtime) + + rc, admitted = call(*_primary_monitor_args("should-run", turn, monitor)) + binding = admitted["heartbeat_receipt"]["settlement_identity"] + assert rc == 0 and binding["todo_id"] == monitor["todo_id"] + assert admitted["safe_bypass_kind"] == "scoped_user_gate_fallback" + rc, acquired = call("task-lease", "acquire", "--goal-id", GOAL_ID, + "--todo-id", monitor["todo_id"], "--owner", AGENT_ID, "--idempotency-key", "monitor-resume-lease") + assert rc == 0 and acquired["acquired"], acquired + lease = acquired["lease"] + before = list_goal_todos(registry_path=source, goal_id=GOAL_ID, todo_id=monitor["todo_id"])["todo"] + assert before["resume_ready"] is True + if condition != before["resume_when"]: + update_goal_todo(registry_path=source, goal_id=GOAL_ID, todo_id=monitor["todo_id"], + agent_id=AGENT_ID, resume_when=condition, + task_lease_idempotency_key=lease["idempotency_key"], task_lease_expected_version=lease["version"]) + before = list_goal_todos(registry_path=source, goal_id=GOAL_ID) + row = next(item for item in before["todos"] if item["todo_id"] == monitor["todo_id"]) + index = runtime / "goals" / GOAL_ID / "runs" / "index.jsonl" + args = _primary_monitor_args("monitor-poll", turn, monitor) + # Cover the exact-id source route and target-key-only global route. + if route == "global": + pos = args.index("--todo-id") + del args[pos:pos + 2] + if condition == elapsed: + rc, denied = call(*args) + assert rc != 0 and "lease proof" in denied["reason"], denied + assert_todo_read_unchanged(list_goal_todos(registry_path=source, goal_id=GOAL_ID), before) + assert not index.exists() + args += ["--task-lease-idempotency-key", lease["idempotency_key"], + "--task-lease-expected-version", str(lease["version"])] + rc, result = call(*args) + if condition != elapsed: + assert row["resume_ready"] is False + assert rc != 0 and result["appended"] is False, result + assert_todo_read_unchanged(list_goal_todos(registry_path=source, goal_id=GOAL_ID), before) + assert not index.exists() + return + + assert rc == 0 and result["appended"], result + assert result["settlement_todo_id"] == binding["todo_id"] + assert result["turn_continuation"]["current_turn_settled"] is True + committed = list_goal_todos(registry_path=source, goal_id=GOAL_ID) + after = {item["todo_id"]: item for item in committed["todos"]} + assert after[monitor["todo_id"]]["result_hash"] == "primary-observation" + assert after[monitor["todo_id"]]["resume_when"] == condition + assert after[gate["todo_id"]]["status"] == "open" + ledger = index.read_bytes() + rc, replay = call(*args) + assert rc == 0 and replay["replayed"] and not replay["appended"], replay + assert index.read_bytes() == ledger + assert [json.loads(line)["classification"] for line in ledger.splitlines()] == ["quota_monitor_poll"] + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("route", ["source", "global"]) @pytest.mark.parametrize("gate_kind", ["linked", "unknown", "global"]) @@ -205,7 +282,7 @@ def test_primary_monitor_gate_change_after_preflight_prevents_business_write( ): from loopx.control_plane.quota import monitor_poll source, registry, runtime, _gated, gate, monitor = _primary_monitor_fixture( - tmp_path, monkeypatch, provider) + tmp_path, monkeypatch, provider, resume_when="resume_at:1999-01-01T00:00:00Z") invoke = _primary_record_invocation(registry, runtime, monitor, "primary-gate-race") original = monitor_poll._provider_writeback def interleaved(plan, **kwargs): From fec483a290c1a394fe05b9ce471770c2cf127b68 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 13 Nov 2026 14:16:53 +0800 Subject: [PATCH 022/267] fix(lark): retain profile-only group intake during private setup Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../lark/goal_topic_runtime_service.py | 41 ++++++-- .../extensions/lark/private_conversations.py | 25 +++-- tests/test_lark_private_ingress.py | 94 ++++++++++++++----- 3 files changed, 121 insertions(+), 39 deletions(-) diff --git a/loopx/extensions/lark/goal_topic_runtime_service.py b/loopx/extensions/lark/goal_topic_runtime_service.py index 1933ea8410..b696f3790d 100644 --- a/loopx/extensions/lark/goal_topic_runtime_service.py +++ b/loopx/extensions/lark/goal_topic_runtime_service.py @@ -341,6 +341,37 @@ def _consumer_lease_key(profile: str, config: Mapping[str, str]) -> str: identity = config.get("bot_app_id") or profile return str(config.get("consumer_ref") or hashlib.sha256(identity.encode("utf-8")).hexdigest()[:32]) + def _listener_configs(self, snapshot: Mapping[str, Any]) -> dict[str, dict[str, str]]: + from . import goal_topic_runtime as runtime + + profiles = runtime._active_profile_configs(snapshot) + legacy = { + profile for profile, config in runtime._active_group_profile_configs(snapshot).items() + if not config.get("bot_app_id") and not profiles[profile].get("bot_app_id") + } + inventory = getattr(self.private_conversations, "configured_profile_apps", None) + if not legacy or not callable(inventory): + return profiles + try: + apps = inventory() + except OSError: + # Unknown inventory does not establish continuity across a binding + # change. The ordinary stop path still handles unknown identities. + return profiles + for profile in legacy: + app_id = apps.get(profile) + if app_id: + # Fence the original profile and the resolved App on both + # sides of a private binding change. Routing keeps the original + # snapshot and its separate group and owner grants. + config = profiles[profile] + profiles[profile] = { + **config, + "bot_app_id": app_id, + "consumer_ref": self._consumer_lease_key(profile, config), + } + return profiles + def _profile_worker_fingerprint( self, snapshot: Mapping[str, Any], @@ -463,7 +494,7 @@ def handle_review_callback( ) snapshot = self.snapshot_provider() - profile_config = runtime._active_profile_configs(snapshot).get(profile) or {} + profile_config = self._listener_configs(snapshot).get(profile) or {} private_profiles = snapshot.get("private_profiles") private_binding = private_profiles.get(profile) if isinstance(private_profiles, Mapping) else None binding_id = private_binding.get("binding_id") if isinstance(private_binding, Mapping) else None @@ -499,8 +530,8 @@ def start_consumer(args: list[str]) -> Popen[str]: # servers can use different runtime roots or ports. The # consumer lease must therefore be machine/App scoped. app_id = str(profile_config.get("bot_app_id") or "") - # A composed legacy route explicitly retains its original - # profile lease. Otherwise both paths use the App digest. + # Legacy group listeners retain their original profile + # lease alongside the resolved App lease. digests = {self._consumer_lease_key(profile, profile_config)} if app_id: digests.add(hashlib.sha256(app_id.encode("utf-8")).hexdigest()[:32]) @@ -616,12 +647,10 @@ def capture(route: Mapping[str, object]) -> dict[str, object]: self.refresh() def refresh(self) -> None: - from . import goal_topic_runtime as runtime - if self._closed.is_set(): return snapshot = self.snapshot_provider() - profile_configs = runtime._active_profile_configs(snapshot) + profile_configs = self._listener_configs(snapshot) desired = { profile: self._profile_worker_fingerprint( snapshot, diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py index 7183fb2bde..e9a942eef8 100644 --- a/loopx/extensions/lark/private_conversations.py +++ b/loopx/extensions/lark/private_conversations.py @@ -254,6 +254,20 @@ def profiles(self) -> dict[str, dict[str, str]]: bindings = self.bindings.read()["bindings"] if not bindings: return {} + apps = self.configured_profile_apps() + profiles = {} + for row in bindings: + app_id = apps.get(row["transport_ref"], "") + if (row.get("enabled") is not True or not APP_ID_PATTERN.fullmatch(app_id) + or identity_ref(app_id) != row["provider_ref"]): + continue + profiles[row["transport_ref"]] = {"cli_bin": self.cli_bin, "provider_ref": row["provider_ref"], + "bot_app_id": app_id, "audience": row.get("audience", "owner"), + "binding_id": row["binding_id"], "consumer_ref": hashlib.sha256(app_id.encode("utf-8")).hexdigest()[:32]} + return profiles + + def configured_profile_apps(self) -> dict[str, str]: + """Read local App identities for listener leases, without granting access.""" result = call(self.runner, [self.cli_bin, "profile", "list"]) try: configured = json.loads(str(result.get("stdout") or "")) @@ -271,16 +285,7 @@ def profiles(self) -> dict[str, dict[str, str]]: # profile names take precedence over the App-ID fallback. for app_id in list(apps.values()): apps.setdefault(app_id, app_id) - profiles = {} - for row in bindings: - app_id = apps.get(row["transport_ref"], "") - if (row.get("enabled") is not True or not APP_ID_PATTERN.fullmatch(app_id) - or identity_ref(app_id) != row["provider_ref"]): - continue - profiles[row["transport_ref"]] = {"cli_bin": self.cli_bin, "provider_ref": row["provider_ref"], - "bot_app_id": app_id, "audience": row.get("audience", "owner"), - "binding_id": row["binding_id"], "consumer_ref": hashlib.sha256(app_id.encode("utf-8")).hexdigest()[:32]} - return profiles + return {profile: app_id for profile, app_id in apps.items() if APP_ID_PATTERN.fullmatch(app_id)} @effect_runtime_request_scope() def health(self) -> dict[str, dict[str, int]]: diff --git a/tests/test_lark_private_ingress.py b/tests/test_lark_private_ingress.py index 384a676609..b6024f15ef 100644 --- a/tests/test_lark_private_ingress.py +++ b/tests/test_lark_private_ingress.py @@ -344,15 +344,23 @@ def isolated_stream(**kwargs): assert len(consumers) == 1 -@pytest.mark.parametrize("binding_change", ["replace", "add", "remove"]) +@pytest.mark.parametrize(("stored_app", "binding_change"), [ + (True, "replace"), (True, "add"), (True, "remove"), + (False, "replace"), (False, "add"), (False, "remove"), + (False, "add_after_retarget"), (False, "remove_after_retarget"), +]) def test_shared_listener_drains_group_inbox_when_private_workspace_changes( ordinary, # noqa: F811 - tmp_path, monkeypatch, binding_change, + tmp_path, monkeypatch, stored_app, binding_change, ): + import http.client from pathlib import Path from extensions.test_lark_goal_topic_runtime import _reply_runner, _seed_legacy_topic + from loopx.chat_server import ChatHTTPServer, ChatRequestHandler from loopx.extensions.lark import goal_topic_runtime as runtime + from loopx.extensions.lark.cli_resolution import LarkCliResolution + from loopx.extensions.lark.conversation_identity import identity_ref from loopx.extensions.lark.event_inbox import inspect_lark_event_inbox from loopx.extensions.lark.goal_channel_contracts import read_goal_channel_binding from loopx.extensions.lark.goal_channel_targets import read_goal_channel_targets @@ -363,18 +371,26 @@ def test_shared_listener_drains_group_inbox_when_private_workspace_changes( target_path, binding_path = tmp_path / "targets.json", tmp_path / "group-binding.json" _seed_legacy_topic(target_path, binding_path) targets = read_goal_channel_targets(target_path) - targets["targets"]["fixture"]["identity"].update( - sender_profile=profile, bot_app_id=provider.profile_apps[profile], - ) + identity = targets["targets"]["fixture"]["identity"] + original_app = provider.profile_apps[profile] + identity.update(sender_profile=profile, bot_open_id="ou_notes_app_bot") + if stored_app: + identity["bot_app_id"] = original_app + else: + identity.pop("bot_app_id") bindings = {"goal-alpha": read_goal_channel_binding(binding_path)} before = transport.profiles()[profile]["binding_id"] - if binding_change == "add": + adding = binding_change.startswith("add") + removing = binding_change.startswith("remove") + retargeted = binding_change.endswith("_retarget") + if adding: transport.bindings.disconnect(before, expected_revision=transport.bindings.read()["revision"]) - config = transport.profiles().get(profile) + + group_config = {"target_payload": targets, "binding_payloads": bindings} def snapshot(): - return {"target_payload": targets, "binding_payloads": bindings, - "private_profiles": {profile: config} if config is not None else {}} + private = transport.profiles().get(profile) + return {**group_config, "private_profiles": {profile: private} if private is not None else {}} group_events = [ dict(provider.topic(name, f"@linkmacbot group {name}", root="om_topic_alpha", @@ -394,8 +410,10 @@ def answer(*, route, **_kwargs): answered.append(message_id) return "Group answer" + original_admit = transport.admit + def admit(profile_name, event, **kwargs): - result = transport.admit(profile_name, event, **kwargs) + result = original_admit(profile_name, event, **kwargs) if event["message_id"] == new_private["message_id"]: assert result["status"] == "command_recorded" next_stream_ready.set() @@ -408,7 +426,7 @@ def first_connection(): queued.set() def replacement_connection(): - yield group_events[2] if binding_change == "remove" else new_private + yield group_events[2] if removing else new_private def spawn(args): assert args[3:6] == ["event", "consume", "im.message.receive_v1"] @@ -426,7 +444,7 @@ def isolated_stream(**kwargs): def observe_health(update): health_sink(update) - if binding_change == "remove" and len(consumer_processes) == 2 and update.get("replied_count") == 1: + if removing and len(consumer_processes) == 2 and update.get("replied_count") == 1: next_stream_ready.set() return stream(**{**kwargs, "health_sink": observe_health}, reply_runner=_reply_runner(replies)) @@ -435,34 +453,61 @@ def observe_health(update): monkeypatch.setattr(runtime, "_default_process_factory", spawn) monkeypatch.setattr(runtime, "stream_lark_goal_topic_profile", isolated_stream) monkeypatch.setattr(runtime, "answer_lark_goal_topic", answer) + monkeypatch.setattr(transport, "admit", admit) + monkeypatch.setattr("loopx.chat_lark_api.build_lark_goal_topic_runtime_snapshot", lambda **_: group_config) + monkeypatch.setattr(ChatRequestHandler, "_lark_runner", lambda _: provider) service = runtime.LarkGoalTopicRuntimeService( snapshot_provider=snapshot, runtime_root=store.root.parent, - runtime_controller=controller, private_conversations=SimpleNamespace(admit=admit), + runtime_controller=controller, private_conversations=transport, ) + server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler) + server.chat_store, server.runtime_controller = store, controller + server.registry_path, server.runtime_root_override = controller.registry_path, store.root.parent + server.verbose = False + server.lark_cli_resolution = LarkCliResolution( + command="lark-cli", available=True, source="explicit", version="fixture", error_code=None, + ) + server.lark_private_conversations = transport + server.lark_goal_topic_runtime = service + server_thread = threading.Thread(target=server.serve_forever, daemon=True) + server_thread.start() + + def change_private_binding(method, body): + connection = http.client.HTTPConnection(*server.server_address, timeout=15) + try: + connection.request(method, "/api/chat/lark/private-conversations", json.dumps(body), + headers={"Content-Type": "application/json"}) + response = connection.getresponse() + data = json.loads(response.read()) + assert response.status == 200, data + finally: + connection.close() try: service.refresh() assert queued.wait(8), service.health_snapshot() - if binding_change == "remove": - transport.bindings.disconnect(before, expected_revision=transport.bindings.read()["revision"]) - config = None + if retargeted: + provider.profile_apps[profile] = "cli_retargeted_app" + if removing: + change_private_binding("DELETE", {"binding_id": before, "revision": transport.bindings.read()["revision"]}) else: projects = transport.bindings.projects other = tmp_path / "other-workspace" other.mkdir() projects.roots.append((other, other)) project = next(row for row in projects.available() if row["workspace_path"] == str(other)) - replacement = transport.bindings.configure( - transport_ref=profile, project_ref=project["project_ref"], executor_endpoint_id="codex", - ) - config = transport.profiles()[profile] - assert config["binding_id"] == replacement["binding_id"] != before - service.refresh() + change_private_binding("POST", {"app_ref": profile, "project_ref": project["project_ref"], + "executor_endpoint_id": "codex"}) + replacement = next(row for row in transport.bindings.read()["bindings"] if row["transport_ref"] == profile) + assert transport.profiles()[profile]["binding_id"] == replacement["binding_id"] != before + assert replacement["provider_ref"] == identity_ref(provider.profile_apps[profile]) assert consumer_processes[0].exited.wait(8) assert len(consumer_processes) == 1 release.set() assert next_stream_ready.wait(8), service.health_snapshot() - expected = group_events if binding_change == "remove" else group_events[:2] + expected = group_events[:1] if retargeted else group_events[:2] + if removing: + expected += group_events[2:] assert answered == [event["message_id"] for event in expected] reply_targets = [call[call.index("--message-id") + 1] for call in replies["calls"] if "+messages-reply" in call and "--dry-run" not in call] @@ -474,6 +519,9 @@ def observe_health(update): finally: release.set() service.close() + server.shutdown() + server.server_close() + server_thread.join(3) controller.close() From 970f631c5a167c83079438e7c8c5dc02c02b6064 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 13 Nov 2026 14:31:54 +0800 Subject: [PATCH 023/267] fix(lark): recognize the same CLI during listener handoff Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../lark/goal_topic_runtime_service.py | 29 ++++++++++++------- .../extensions/lark/private_conversations.py | 4 +-- tests/test_lark_private_ingress.py | 19 ++++++++++-- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/loopx/extensions/lark/goal_topic_runtime_service.py b/loopx/extensions/lark/goal_topic_runtime_service.py index b696f3790d..e7fbe0c913 100644 --- a/loopx/extensions/lark/goal_topic_runtime_service.py +++ b/loopx/extensions/lark/goal_topic_runtime_service.py @@ -10,6 +10,7 @@ import hashlib import logging from pathlib import Path +import shutil from subprocess import Popen import threading import time @@ -18,6 +19,7 @@ from ...chat_manager import MANAGER_AGENT_OBJECTIVE from ...file_lock import try_exclusive_file_lock from ..background_runtime import HostedWorker +from .cli_resolution import LarkCliUnavailableError from .goal_channel_contracts import bindings_for_goal from .manager_context import session_turn_effect from .team_plan_confirmation import ( @@ -350,21 +352,24 @@ def _listener_configs(self, snapshot: Mapping[str, Any]) -> dict[str, dict[str, if not config.get("bot_app_id") and not profiles[profile].get("bot_app_id") } inventory = getattr(self.private_conversations, "configured_profile_apps", None) - if not legacy or not callable(inventory): - return profiles - try: - apps = inventory() - except OSError: - # Unknown inventory does not establish continuity across a binding - # change. The ordinary stop path still handles unknown identities. + if not callable(inventory): return profiles + apps_by_cli: dict[str, dict[str, str]] = {} for profile in legacy: - app_id = apps.get(profile) + config = profiles[profile] + cli_bin = config["cli_bin"] + if cli_bin not in apps_by_cli: + try: + apps_by_cli[cli_bin] = inventory(cli_bin=cli_bin) + except (OSError, LarkCliUnavailableError): + # Keep the ordinary stop path if App continuity cannot + # be established from this group's CLI configuration. + apps_by_cli[cli_bin] = {} + app_id = apps_by_cli[cli_bin].get(profile) if app_id: # Fence the original profile and the resolved App on both # sides of a private binding change. Routing keeps the original # snapshot and its separate group and owner grants. - config = profiles[profile] profiles[profile] = { **config, "bot_app_id": app_id, @@ -386,9 +391,13 @@ def _profile_worker_fingerprint( app_id = str(profile_config.get("bot_app_id") or "") binding_id = str(profile_config.get("binding_id") or "") owner_binding_id = binding_id if app_id and profile_config.get("audience") == "owner" else "" + cli_bin = str(profile_config.get("cli_bin") or "lark-cli") + executable = shutil.which(cli_bin) return ProfileWorkerFingerprint( transport=ProfileTransport( - cli_bin=str(profile_config.get("cli_bin") or "lark-cli"), + # Legacy settings can name the same CLI that Chat resolved + # to a full path when configuring the private binding. + cli_bin=str(Path(executable).absolute()) if executable else cli_bin, app_id=app_id, consumer_lease_key=self._consumer_lease_key(profile, profile_config), callback_chats=callback_chats, diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py index e9a942eef8..2419fbc1c6 100644 --- a/loopx/extensions/lark/private_conversations.py +++ b/loopx/extensions/lark/private_conversations.py @@ -266,9 +266,9 @@ def profiles(self) -> dict[str, dict[str, str]]: "binding_id": row["binding_id"], "consumer_ref": hashlib.sha256(app_id.encode("utf-8")).hexdigest()[:32]} return profiles - def configured_profile_apps(self) -> dict[str, str]: + def configured_profile_apps(self, *, cli_bin: str | None = None) -> dict[str, str]: """Read local App identities for listener leases, without granting access.""" - result = call(self.runner, [self.cli_bin, "profile", "list"]) + result = call(self.runner, [cli_bin or self.cli_bin, "profile", "list"]) try: configured = json.loads(str(result.get("stdout") or "")) except json.JSONDecodeError as exc: diff --git a/tests/test_lark_private_ingress.py b/tests/test_lark_private_ingress.py index b6024f15ef..b02bf57d93 100644 --- a/tests/test_lark_private_ingress.py +++ b/tests/test_lark_private_ingress.py @@ -348,12 +348,16 @@ def isolated_stream(**kwargs): (True, "replace"), (True, "add"), (True, "remove"), (False, "replace"), (False, "add"), (False, "remove"), (False, "add_after_retarget"), (False, "remove_after_retarget"), + (False, "add_cli_alias"), (False, "remove_cli_alias"), + (False, "add_different_cli"), (False, "remove_different_cli"), ]) def test_shared_listener_drains_group_inbox_when_private_workspace_changes( ordinary, # noqa: F811 tmp_path, monkeypatch, stored_app, binding_change, ): import http.client + import os + import sys from pathlib import Path from extensions.test_lark_goal_topic_runtime import _reply_runner, _seed_legacy_topic @@ -378,11 +382,20 @@ def test_shared_listener_drains_group_inbox_when_private_workspace_changes( identity["bot_app_id"] = original_app else: identity.pop("bot_app_id") + if binding_change.endswith(("_cli_alias", "_different_cli")): + transport.cli_bin = sys.executable + if binding_change.endswith("_cli_alias"): + # Legacy targets may save a PATH command while Chat resolves the + # same executable to a full path for its private transport. + executable = Path(sys.executable) + monkeypatch.setenv("PATH", str(executable.parent) + os.pathsep + os.environ.get("PATH", "")) + identity["cli_bin"] = executable.name bindings = {"goal-alpha": read_goal_channel_binding(binding_path)} before = transport.profiles()[profile]["binding_id"] adding = binding_change.startswith("add") removing = binding_change.startswith("remove") retargeted = binding_change.endswith("_retarget") + different_transport = retargeted or binding_change.endswith("_different_cli") if adding: transport.bindings.disconnect(before, expected_revision=transport.bindings.read()["revision"]) @@ -465,7 +478,7 @@ def observe_health(update): server.registry_path, server.runtime_root_override = controller.registry_path, store.root.parent server.verbose = False server.lark_cli_resolution = LarkCliResolution( - command="lark-cli", available=True, source="explicit", version="fixture", error_code=None, + command=transport.cli_bin, available=True, source="explicit", version="fixture", error_code=None, ) server.lark_private_conversations = transport server.lark_goal_topic_runtime = service @@ -505,7 +518,7 @@ def change_private_binding(method, body): assert len(consumer_processes) == 1 release.set() assert next_stream_ready.wait(8), service.health_snapshot() - expected = group_events[:1] if retargeted else group_events[:2] + expected = group_events[:1] if different_transport else group_events[:2] if removing: expected += group_events[2:] assert answered == [event["message_id"] for event in expected] @@ -516,6 +529,8 @@ def change_private_binding(method, body): inbox = inspect_lark_event_inbox(project=store.root.parent, config_path=config_path) assert inbox["processed_count"] == len(expected) assert inbox["pending_count"] == 0 + if not stored_app and (adding or removing): + assert [identity["cli_bin"], "profile", "list"] in provider.calls finally: release.set() service.close() From 1d05f09273bd40ffe8ee8bc0dc6740b724ffe177 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:27:29 +0800 Subject: [PATCH 024/267] fix(lark): retain live listener on unreadable profile inventory Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../lark/goal_topic_runtime_service.py | 99 +++++++++++++++---- tests/test_lark_private_ingress.py | 63 +++++++++--- 2 files changed, 132 insertions(+), 30 deletions(-) diff --git a/loopx/extensions/lark/goal_topic_runtime_service.py b/loopx/extensions/lark/goal_topic_runtime_service.py index e7fbe0c913..6964ea360b 100644 --- a/loopx/extensions/lark/goal_topic_runtime_service.py +++ b/loopx/extensions/lark/goal_topic_runtime_service.py @@ -81,6 +81,7 @@ def __init__( self._closed = threading.Event() self._delivery_wakeup = threading.Event() self._startup_thread: threading.Thread | None = None + self._refresh_retry: threading.Timer | None = None self._delivery_thread: threading.Thread | None = None self._delivery_worker = HostedWorker(target=self._reconcile_private, stop=self._closed, name="loopx-lark-private-delivery", retry_seconds=5) @@ -286,6 +287,27 @@ def _refresh_on_start(self) -> None: ) self._closed.wait(5) + def _retry_profile_refresh(self) -> None: + try: + unavailable = self._refresh_profile_workers(resume_queues=False) + except (OSError, LarkCliUnavailableError): + unavailable = True + except Exception: + logging.getLogger(__name__).warning("Lark profile refresh failed") + unavailable = False + with self._lock: + self._refresh_retry = None + if unavailable: + self._schedule_profile_refresh() + + def _schedule_profile_refresh(self) -> None: + # Called under the service lock. Only the configuration read is retried. + if self._closed.is_set() or self._refresh_retry is not None: + return + self._refresh_retry = threading.Timer(5, self._retry_profile_refresh) + self._refresh_retry.daemon = True + self._refresh_retry.start() + @staticmethod def _now() -> str: return datetime.now(timezone.utc).isoformat() @@ -343,7 +365,7 @@ def _consumer_lease_key(profile: str, config: Mapping[str, str]) -> str: identity = config.get("bot_app_id") or profile return str(config.get("consumer_ref") or hashlib.sha256(identity.encode("utf-8")).hexdigest()[:32]) - def _listener_configs(self, snapshot: Mapping[str, Any]) -> dict[str, dict[str, str]]: + def _listener_configs(self, snapshot: Mapping[str, Any]) -> tuple[dict[str, dict[str, str]], set[str]]: from . import goal_topic_runtime as runtime profiles = runtime._active_profile_configs(snapshot) @@ -352,20 +374,23 @@ def _listener_configs(self, snapshot: Mapping[str, Any]) -> dict[str, dict[str, if not config.get("bot_app_id") and not profiles[profile].get("bot_app_id") } inventory = getattr(self.private_conversations, "configured_profile_apps", None) + unavailable: set[str] = set() if not callable(inventory): - return profiles - apps_by_cli: dict[str, dict[str, str]] = {} - for profile in legacy: + return profiles, unavailable + apps_by_cli: dict[str, dict[str, str] | None] = {} + for profile in sorted(legacy): config = profiles[profile] cli_bin = config["cli_bin"] if cli_bin not in apps_by_cli: try: apps_by_cli[cli_bin] = inventory(cli_bin=cli_bin) except (OSError, LarkCliUnavailableError): - # Keep the ordinary stop path if App continuity cannot - # be established from this group's CLI configuration. - apps_by_cli[cli_bin] = {} - app_id = apps_by_cli[cli_bin].get(profile) + apps_by_cli[cli_bin] = None + apps = apps_by_cli[cli_bin] + if apps is None: + unavailable.add(profile) + continue + app_id = apps.get(profile) if app_id: # Fence the original profile and the resolved App on both # sides of a private binding change. Routing keeps the original @@ -375,7 +400,7 @@ def _listener_configs(self, snapshot: Mapping[str, Any]) -> dict[str, dict[str, "bot_app_id": app_id, "consumer_ref": self._consumer_lease_key(profile, config), } - return profiles + return profiles, unavailable def _profile_worker_fingerprint( self, @@ -503,15 +528,27 @@ def handle_review_callback( ) snapshot = self.snapshot_provider() - profile_config = self._listener_configs(snapshot).get(profile) or {} + profile_configs, unavailable = self._listener_configs(snapshot) + if profile in unavailable: + # A new subprocess must acquire locks for a fresh App + # observation. Only an already running pipe may stay. + self._update_health( + profile, status="retrying", error_code="lark_profile_inventory_unavailable" + ) + stop.wait(5) + continue + profile_config = profile_configs.get(profile) or {} private_profiles = snapshot.get("private_profiles") private_binding = private_profiles.get(profile) if isinstance(private_profiles, Mapping) else None binding_id = private_binding.get("binding_id") if isinstance(private_binding, Mapping) else None reconnect_requested = threading.Event() # A group-only connection must not pick up private work - # read before a newly added owner binding. - private_admitter = (self._admit_private if self.private_conversations is not None - and isinstance(private_binding, Mapping) else None) + # read before a newly added owner binding. A provider with + # no separate group route still owns its private admission. + private_admitter = self._admit_private if self.private_conversations is not None and ( + isinstance(private_binding, Mapping) + or profile not in runtime._active_group_profile_configs(snapshot) + ) else None bound_owner = ( private_admitter is not None and isinstance(binding_id, str) and bool(binding_id) @@ -656,10 +693,16 @@ def capture(route: Mapping[str, object]) -> dict[str, object]: self.refresh() def refresh(self) -> None: + if self._refresh_profile_workers(): + with self._lock: + self._schedule_profile_refresh() + + def _refresh_profile_workers(self, *, resume_queues: bool = True) -> bool: + """Apply known listener changes and report whether discovery must retry.""" if self._closed.is_set(): - return + return False snapshot = self.snapshot_provider() - profile_configs = self._listener_configs(snapshot) + profile_configs, unavailable = self._listener_configs(snapshot) desired = { profile: self._profile_worker_fingerprint( snapshot, @@ -669,13 +712,29 @@ def refresh(self) -> None: for profile, profile_config in profile_configs.items() } if self._closed.is_set(): - return - self._resume_session_queues(snapshot) + return False + if resume_queues: + self._resume_session_queues(snapshot) # A filesystem read may outlive server shutdown (for example, while # waiting for OS directory consent). Never start effects after close. with self._lock: if self._closed.is_set(): - return + return False + for profile in unavailable: + replacement = desired.pop(profile) + worker = self._workers.get(profile) + if worker is None: + continue + previous = worker[2] + # Keep the original pipe and locks while the same group's + # local App inventory is unavailable. Routing still checks + # current grants; an observed removal uses the stop path. + if ( + previous.transport.cli_bin == replacement.transport.cli_bin + and previous.transport.callback_chats == replacement.transport.callback_chats + and not previous.transport.other_binding_id + ): + desired[profile] = previous stale = (set(self._workers) - set(desired)) | { profile for profile in set(self._workers) & set(desired) @@ -719,6 +778,7 @@ def refresh(self) -> None: ) self._workers[profile] = (stop, thread, desired[profile]) thread.start() + return bool(unavailable) def _resume_session_queues(self, snapshot: Mapping[str, Any]) -> None: binding_payloads = snapshot.get("binding_payloads") @@ -770,6 +830,9 @@ def close(self) -> None: self._closed.set() self._delivery_wakeup.set() with self._lock: + if self._refresh_retry is not None: + self._refresh_retry.cancel() + self._refresh_retry = None workers = list(self._workers.values()) self._workers.clear() for stop, _thread, _fingerprint in workers: diff --git a/tests/test_lark_private_ingress.py b/tests/test_lark_private_ingress.py index b02bf57d93..104d4b79b7 100644 --- a/tests/test_lark_private_ingress.py +++ b/tests/test_lark_private_ingress.py @@ -350,6 +350,7 @@ def isolated_stream(**kwargs): (False, "add_after_retarget"), (False, "remove_after_retarget"), (False, "add_cli_alias"), (False, "remove_cli_alias"), (False, "add_different_cli"), (False, "remove_different_cli"), + (False, "profile_read_fails"), (False, "profile_read_fails_then_retargets"), ]) def test_shared_listener_drains_group_inbox_when_private_workspace_changes( ordinary, # noqa: F811 @@ -396,8 +397,13 @@ def test_shared_listener_drains_group_inbox_when_private_workspace_changes( removing = binding_change.startswith("remove") retargeted = binding_change.endswith("_retarget") different_transport = retargeted or binding_change.endswith("_different_cli") + unreadable = binding_change.startswith("profile_read_fails") + retarget_on_retry = binding_change == "profile_read_fails_then_retargets" if adding: transport.bindings.disconnect(before, expected_revision=transport.bindings.read()["revision"]) + if unreadable: + for row in transport.bindings.read()["bindings"]: + transport.bindings.disconnect(row["binding_id"], expected_revision=transport.bindings.read()["revision"]) group_config = {"target_payload": targets, "binding_payloads": bindings} @@ -412,6 +418,7 @@ def snapshot(): ] new_private = provider.event(profile, "after_rebind", "/status") in_group_answer, release, queued, next_stream_ready = (threading.Event() for _ in range(4)) + second_replied, inventory_retried = threading.Event(), threading.Event() answered, consumer_processes = [], [] replies = {} @@ -439,7 +446,7 @@ def first_connection(): queued.set() def replacement_connection(): - yield group_events[2] if removing else new_private + yield group_events[2] if removing or unreadable else new_private def spawn(args): assert args[3:6] == ["event", "consume", "im.message.receive_v1"] @@ -457,8 +464,11 @@ def isolated_stream(**kwargs): def observe_health(update): health_sink(update) - if removing and len(consumer_processes) == 2 and update.get("replied_count") == 1: - next_stream_ready.set() + if update.get("replied_count") == 1: + if (removing or unreadable) and len(consumer_processes) == 2: + next_stream_ready.set() + if answered == [event["message_id"] for event in group_events[:2]]: + second_replied.set() return stream(**{**kwargs, "health_sink": observe_health}, reply_runner=_reply_runner(replies)) @@ -499,9 +509,28 @@ def change_private_binding(method, body): try: service.refresh() assert queued.wait(8), service.health_snapshot() - if retargeted: + if retargeted or retarget_on_retry: provider.profile_apps[profile] = "cli_retargeted_app" - if removing: + if unreadable: + original_runner = transport.runner + inventory_failed = threading.Event() + + def read_profile(args, cwd=None, timeout=None): + if args[1:] == ["profile", "list"]: + if not inventory_failed.is_set(): + inventory_failed.set() + return {"returncode": 1, "stdout": "", "stderr": "temporary profile read failure"} + inventory_retried.set() + return original_runner(args, cwd, timeout) + + monkeypatch.setattr(transport, "runner", read_profile) + original_worker = service._workers[profile] + service.refresh() + assert inventory_failed.is_set() + assert service._workers[profile] == original_worker + assert not original_worker[0].is_set() + assert not consumer_processes[0].exited.is_set() + elif removing: change_private_binding("DELETE", {"binding_id": before, "revision": transport.bindings.read()["revision"]}) else: projects = transport.bindings.projects @@ -514,13 +543,23 @@ def change_private_binding(method, body): replacement = next(row for row in transport.bindings.read()["bindings"] if row["transport_ref"] == profile) assert transport.profiles()[profile]["binding_id"] == replacement["binding_id"] != before assert replacement["provider_ref"] == identity_ref(provider.profile_apps[profile]) - assert consumer_processes[0].exited.wait(8) - assert len(consumer_processes) == 1 - release.set() - assert next_stream_ready.wait(8), service.health_snapshot() - expected = group_events[:1] if different_transport else group_events[:2] - if removing: - expected += group_events[2:] + if unreadable and not retarget_on_retry: + release.set() + assert second_replied.wait(8), service.health_snapshot() + assert inventory_retried.wait(8) + assert service._workers[profile] == original_worker + assert len(consumer_processes) == 1 + expected = group_events[:2] + else: + assert consumer_processes[0].exited.wait(8) + assert len(consumer_processes) == 1 + release.set() + assert next_stream_ready.wait(8), service.health_snapshot() + expected = group_events[:1] if different_transport or retarget_on_retry else group_events[:2] + if removing or unreadable: + expected += group_events[2:] + if unreadable: + assert inventory_retried.is_set() assert answered == [event["message_id"] for event in expected] reply_targets = [call[call.index("--message-id") + 1] for call in replies["calls"] if "+messages-reply" in call and "--dry-run" not in call] From f8287cd14f152b448d63071ac781a842cc4c2102 Mon Sep 17 00:00:00 2001 From: huangruiteng Date: Fri, 13 Nov 2026 16:31:31 +0800 Subject: [PATCH 025/267] fix(chat): honor the configured steward across default entrypoints Signed-off-by: huangruiteng --- .../dashboard/smoke/app-steward-smoke.ts | 23 +++++++++++++++++- .../smoke/bound-conversation-http-fixture.py | 8 +++++++ .../personal-workspace/app-steward-entry.tsx | 24 ++++++++++++------- .../personal-workspace-model.ts | 2 +- .../personal-workspace-page.tsx | 3 ++- .../dashboard/src/views/dashboard-page.tsx | 2 +- .../app-conversation-and-async-inbox-v0.md | 7 ++++++ ...p-conversation-and-async-inbox-v0.zh-CN.md | 4 ++++ loopx/chat/session_open.py | 19 +++++++++++++++ .../extensions/lark/conversation_identity.py | 11 +++++++-- .../lark/private_conversation_api.py | 4 ++++ tests/test_lark_private_conversations.py | 3 ++- 12 files changed, 95 insertions(+), 15 deletions(-) diff --git a/apps/presentation/dashboard/smoke/app-steward-smoke.ts b/apps/presentation/dashboard/smoke/app-steward-smoke.ts index fe2e48c9bd..91e9a07821 100644 --- a/apps/presentation/dashboard/smoke/app-steward-smoke.ts +++ b/apps/presentation/dashboard/smoke/app-steward-smoke.ts @@ -4,7 +4,7 @@ import {once} from "node:events"; import {resolve} from "node:path"; import {createInterface} from "node:readline"; import {resolveTestPython} from "../../../../scripts/test-python.mjs"; -import {fetchAppStewardTarget, fetchPrivateConversations, selectAppSteward, +import {ChatApiError, fetchAppStewardTarget, fetchPrivateConversations, selectAppSteward, fetchChatHistory, resolveBoundChatSession} from "../src/data/chat"; const repoRoot = resolve(process.cwd(), "../../.."); @@ -31,6 +31,12 @@ try { globalThis.fetch = (input, init) => originalFetch(new URL(String(input), origin), init); let connections = await fetchPrivateConversations(); const inspect = async () => {child.stdin.write("inspect\n"); return next();}; + const openDefault = async (overrides = {}) => { + const response = await fetch("/api/chat/sessions", {method: "POST", + headers: {"Content-Type": "application/json"}, + body: JSON.stringify({context_kind: "manager", ...overrides})}); + return {status: response.status, body: await response.json()}; + }; const before = await inspect(); assert.equal(before.sessions, 3); assert.equal(await fetchAppStewardTarget(), null, "No implicit selection between two original stewards"); @@ -39,6 +45,12 @@ try { connections = await selectAppSteward(bindingId, connections.revision); const target = {bindingId, sessionId}; assert.deepEqual(await fetchAppStewardTarget(), target); + const opened = await openDefault(); + assert.equal(opened.status, 200); + assert.equal(opened.body.session_id, sessionId, "Default API entry must reuse the configured private conversation"); + for (const overrides of [{mode: "new"}, {agent_model_id: "different-model"}]) { + assert.equal((await openDefault(overrides)).status, 400, "Explicit overrides cannot silently fork the selected source"); + } await assert.rejects(() => selectAppSteward(peer.binding_id, connections.revision - 1), /selection could not/); assert.deepEqual(await fetchAppStewardTarget(), target, "Stale selection cannot retarget the App"); const history = await fetchChatHistory({bound: target}); @@ -53,8 +65,14 @@ try { child.stdin.write("deny\n"); await next(); await assert.rejects(() => fetchAppStewardTarget(), /selected original steward is unavailable/); await assert.rejects(() => selectAppSteward(bindingId, connections.revision), /selection could not/); + assert.equal((await openDefault()).status, 400, "Failed identity verification must not fall back to a local manager"); + child.stdin.write("owner-login-missing\n"); await next(); + await assert.rejects(() => fetchAppStewardTarget(), error => error instanceof ChatApiError + && error.payload.error_code === "lark_owner_identity_unavailable"); + assert.equal((await openDefault()).status, 400); child.stdin.write("recover\n"); await next(); assert.deepEqual(await fetchAppStewardTarget(), target); + assert.equal((await openDefault()).body.session_id, sessionId, "Login recovery returns to the same original Session"); const after = await inspect(); assert.equal(after.sessions, before.sessions); assert.deepEqual(after.messages, before.messages); @@ -76,6 +94,9 @@ try { assert.equal((await fetchPrivateConversations()).connections[0].binding_id, peer.binding_id); connections = await selectAppSteward(null, connections.revision); assert.equal(await fetchAppStewardTarget(), null, "Only an explicit clear restores the ordinary entry"); + const local = await openDefault(); + assert.equal(local.status, 201); + assert.equal(local.body.session.channel_id, "manager", "Cleared selection preserves the existing local manager path"); const routedFetch = globalThis.fetch; for (const malformed of [{ok: true}, {ok: true, binding_id: null, session_id: sessionId}, {ok: true, binding_id: bindingId, session_id: "../other"}]) { diff --git a/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py b/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py index 8bbd6259c4..862b468eaf 100644 --- a/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py +++ b/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py @@ -30,6 +30,7 @@ class SyntheticPrivateSource: def __init__(self, root, *, steward=False, peer=False): self.root, self.verified = root, True + self.owner_login_missing = False self.steward, self.peer = steward, peer self.peer_provider = None default_compact_reply = "0" if os.environ.get("LOOPX_REPLY_BEFORE") == "1" else "1" @@ -38,6 +39,9 @@ def __init__(self, root, *, steward=False, peer=False): self.transport_ref = "synthetic-peer" def observe(self): + if self.owner_login_missing: + from loopx.extensions.lark.conversation_identity import LarkOwnerIdentityUnavailable + raise LarkOwnerIdentityUnavailable() provider = ("e" if self.peer else "c") * 24 operator = ("f" if self.peer else "d") * 24 if "--reply-context" in sys.argv and not self.peer: @@ -186,7 +190,11 @@ def control(): command = line.strip() if command in {"deny", "recover"}: self.verified = command == "recover" + self.owner_login_missing = False result = {"source_verified": self.verified} + elif command == "owner-login-missing": + self.owner_login_missing = True + result = {"owner_login_missing": True} elif command == "source": accepted, _ = runtime.enqueue_turn(session_id=sid, message="A new synthetic message from the original private source.", client_turn_id="synthetic-source-update", origin="lark", work_dir=self.root / "project", objective="") diff --git a/apps/presentation/dashboard/src/features/personal-workspace/app-steward-entry.tsx b/apps/presentation/dashboard/src/features/personal-workspace/app-steward-entry.tsx index 1a23420147..f24833ff38 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/app-steward-entry.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/app-steward-entry.tsx @@ -1,15 +1,22 @@ import {useEffect, useRef, useState, type ReactNode} from "react"; import {Button} from "../../components/ui/button"; -import {fetchAppStewardTarget, type BoundConversationTarget} from "../../data/chat"; +import {ChatApiError, fetchAppStewardTarget, type BoundConversationTarget} from "../../data/chat"; import {useWorkspaceI18n} from "./i18n"; +export function appStewardUnavailableMessage(ownerLoginUnavailable: boolean, zh: boolean) { + return ownerLoginUnavailable + ? (zh ? "已连接飞书管家,但本人登录不可用。请用原账号为同一飞书应用重新登录,再重试;原会话仍保留。" + : "The Lark steward is connected, but its owner login is unavailable. Sign in to the same App with the original account, then retry. Your original conversation is retained.") + : (zh ? "原管家会话暂不可用。请检查所选连接后重试。" : "The original steward conversation is unavailable. Check the selected connection and retry."); +} + /** One read-only resolver for both App entrypoints. Pending/failed discovery * never exposes the ordinary manager queue. Explicit bound routes bypass it. */ export function useAppStewardTarget(enabled: boolean) { const [read, setRead] = useState<{enabled: boolean; revision: number; ready: boolean; - target: BoundConversationTarget | null; unavailable: boolean}>( - {enabled, revision: 0, ready: false, target: null, unavailable: false}); + target: BoundConversationTarget | null; unavailable: boolean; ownerLoginUnavailable: boolean}>( + {enabled, revision: 0, ready: false, target: null, unavailable: false, ownerLoginUnavailable: false}); // Invalidate in the render that changes scope, before a composer can submit // using a previous ordinary result. React retries this component immediately. if (read.enabled !== enabled) setRead({...read, enabled, ready: false, target: null, unavailable: false}); @@ -24,17 +31,18 @@ export function useAppStewardTarget(enabled: boolean) { let active = true; const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 5000); - const settle = (target: BoundConversationTarget | null, unavailable: boolean) => { + const settle = (target: BoundConversationTarget | null, unavailable: boolean, ownerLoginUnavailable = false) => { if (active) setRead(value => value.enabled === enabled && value.revision === revision - ? {...value, ready: true, target, unavailable} : value); + ? {...value, ready: true, target, unavailable, ownerLoginUnavailable} : value); }; void fetchAppStewardTarget(controller.signal).then(target => settle(target, false)) - .catch(() => settle(null, true)).finally(() => clearTimeout(timeout)); + .catch(error => settle(null, true, error instanceof ChatApiError + && error.payload.error_code === "lark_owner_identity_unavailable")).finally(() => clearTimeout(timeout)); return () => {active = false; controller.abort(); clearTimeout(timeout);}; }, [enabled, revision]); const ready = enabled && read.enabled === enabled && read.ready; return {target: ready ? read.target : null, loading: enabled && !ready, - unavailable: ready && read.unavailable, retry}; + unavailable: ready && read.unavailable, ownerLoginUnavailable: ready && read.ownerLoginUnavailable, retry}; } export function AppStewardEntry({children, enabled, intercept, onOpen}: { @@ -61,7 +69,7 @@ export function AppStewardEntry({children, enabled, intercept, onOpen}: { {blocked ?

{zh ? "LoopX 管家" : "LoopX steward"}

{!entry.unavailable ?

{zh ? "正在打开管家对话…" : "Opening the steward conversation…"}

: <> -

{zh ? "原管家会话暂不可用。请检查所选连接后重试。" : "The original steward conversation is unavailable. Check the selected connection and retry."}

+

{appStewardUnavailableMessage(entry.ownerLoginUnavailable, zh)}

{zh ? "打开会话设置" : "Open conversation settings"} diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-model.ts b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-model.ts index 744f28456a..a7b9fc82cc 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-model.ts +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-model.ts @@ -115,7 +115,7 @@ export type WorkspaceConversationDirectory = { onSelect: (projectRef: string | null) => void; onOpenOriginal?: (target: BoundConversationTarget) => void; /** Local manager entry discovery blocks sending, never Goal navigation. */ - entryGate?: {unavailable: boolean; retry: () => void}; + entryGate?: {unavailable: boolean; ownerLoginUnavailable?: boolean; retry: () => void}; bound?: { target: BoundConversationTarget; session?: ChatSessionSummary; source?: BoundConversationSource; error?: string; retry: () => void }; managed?: {target: import("../../data/chat").ManagedConversationTarget; session?: import("../../data/chat").ChatSessionSummary; error?: string; retry: () => void}; diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx index 72884d084e..1acb29c6f7 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace-page.tsx @@ -1,4 +1,5 @@ import {DocumentAttachment, isTextDocument} from "./document-attachment"; +import {appStewardUnavailableMessage} from "./app-steward-entry"; import {AlertCircle, Bot, CalendarClock, CircleHelp, FileText, MessageCircleQuestion, Paperclip, Plus, RefreshCw, Send, Square, X} from "lucide-react"; import {ContextHelp} from "./context-help"; import {readSettingsRoute, writeSettingsRoute} from "./workspace-settings-route"; @@ -3044,7 +3045,7 @@ export function PersonalWorkspacePage({ {!rootUnavailable ?
-
{copy.density}{axisLabel} - {hasRecordingGaps ?