From 0d9ec0e904ffe776154fd88a2f76fb62fdcf9c42 Mon Sep 17 00:00:00 2001 From: huangruiteng Date: Thu, 19 Nov 2026 16:26:35 +0800 Subject: [PATCH] Use the selected Goal runtime for Chat task reads and writes Signed-off-by: huangruiteng --- .../dashboard/src/data/action-source-basis.ts | 1 + .../personal-workspace/channel-timeline.tsx | 2 +- .../personal-workspace/context-drawer.tsx | 38 +- .../src/features/personal-workspace/i18n.tsx | 2 + docs/project-agent-todo-contract.md | 40 ++ loopx/chat.py | 49 +- loopx/chat/monitor_actions.py | 10 +- loopx/chat/status_api.py | 7 +- loopx/chat/todo_actions.py | 337 +++++++++-- loopx/chat/todo_detail.py | 16 +- loopx/chat/todo_source.py | 198 +++++++ loopx/chat_action_normalization.py | 22 +- loopx/chat_actions.py | 176 +++--- loopx/chat_server.py | 25 +- .../coordination/authority_source.ts | 26 +- .../coordination/authority_source_capture.py | 9 +- .../coordination/local_authority.py | 20 +- .../coordination/local_authority_runtime.ts | 29 +- .../control_plane/coordination/todo_claim.ts | 10 +- .../control_plane/coordination/todo_create.ts | 18 +- .../presentation/action_review_plan.ts | 42 +- .../runtime/runtime_projection_route.py | 16 +- loopx/control_plane/todos/mutation_api.py | 15 + loopx/control_plane/todos/provider_create.py | 15 +- .../todos/provider_terminal_lifecycle.py | 4 +- loopx/control_plane/todos/provider_update.py | 3 +- .../test_chat_reviewed_create.py | 545 +++++++++++++++++- .../test_chat_task_start_recovery.py | 121 ++++ .../test_native_user_completion_update.py | 73 ++- .../action_review_plan.test.ts | 26 +- .../local_authority_provider.test.ts | 7 +- .../managed_claim_execution_conformance.ts | 16 + tests/test_chat_todo_detail.py | 67 +++ 33 files changed, 1702 insertions(+), 283 deletions(-) create mode 100644 loopx/chat/todo_source.py diff --git a/apps/presentation/dashboard/src/data/action-source-basis.ts b/apps/presentation/dashboard/src/data/action-source-basis.ts index 1f2baff596..a5d38cb6ac 100644 --- a/apps/presentation/dashboard/src/data/action-source-basis.ts +++ b/apps/presentation/dashboard/src/data/action-source-basis.ts @@ -5,6 +5,7 @@ const todoBasis = { provider_revision: z.string().min(1), source_authority: z.enum(["file_v0", "sqlite_v0"]), registry_sha256: digest, + source_identity: digest.optional(), }; // Decode existing backend contracts; effect authority and stale-source checks 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 2645c89c58..d2f0f8f7b3 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx @@ -251,7 +251,7 @@ export const ChannelTimeline = memo(function ChannelTimeline({ return ( : null} : null} {selection.item.actionKind === "team.plan" && selection.item.status === "applied" ? :
- {!taskExecution ? {selection.item.reviewPlan?.operationFrame?.kind === "pending" + {!taskExecution ? {(selection.item.reviewPlan?.operationFrame?.kind === "pending" || recoveringTodoCreation) ? t(`proposal.kind.${selection.item.actionKind}`) : `${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") ?
: null} - {selection.item.impact ?

{selection.item.impact}

: null} - {selection.item.reviewPlan && !selection.item.reviewPlan.creationExecution && !selection.item.reviewPlan.retryOriginal && !taskExecution && selection.item.actionKind !== "team.plan" && !(decisionFrame && ["ready", "applied"].includes(selection.item.status)) ?

{operationUnknown + {retainedTodoGate ?

+ {retainedTodoGate.summary}{retainedTodoGate.nextAction ? {retainedTodoGate.nextAction} : null} +
: selection.item.impact ?

{selection.item.impact}

: null} + {selection.item.reviewPlan && !retainedTodoGate && !selection.item.reviewPlan.creationExecution && !selection.item.reviewPlan.retryOriginal && !taskExecution && selection.item.actionKind !== "team.plan" && !(decisionFrame && ["ready", "applied"].includes(selection.item.status)) ?

{operationUnknown ? t("actionReview.operation_reconcile_original") : selection.item.reviewPlan.operationFrame?.kind === "inactive" ? t(`actionReview.${selection.item.reviewPlan.operationFrame.reason}`) @@ -1245,7 +1256,7 @@ export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention : t(`actionReview.${selection.item.reviewPlan.reason}`)}

: null} {selection.item.status === "ready" && !selection.item.reviewPlan?.creationExecution && selection.item.actionKind !== "team.plan" && !decisionFrame ?

{t("drawer.proposalExplainer")}

: null} {selection.item.reviewPlan?.creationExecution ? : null} - {!taskExecution && visibleProposalFields.length ?
{visibleProposalFields.map((field) =>
{field.label}
{field.value}
)}
: null} + {!taskExecution && shownProposalFields.length ?
{shownProposalFields.map((field) =>
{field.label}
{field.value}
)}
: null}
} {selection.item.reviewPlan?.creationProgress?.goalId && selection.item.status !== "applied" ?
{t("proposal.field.goalId")}: {selection.item.reviewPlan.creationProgress.goalId} · {t("proposal.status.applied")} @@ -1266,10 +1277,23 @@ export function ContextDrawer({ agents, attentionHistory = [], onSelectAttention ?

{t("proposal.primary.operationCancelled")}

: null} {selection.item.status === "deferred" ?

{t("drawer.proposalDeferred")}

: null} {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} + : selection.item.status === "gated" && !retainedTodoGate ?
{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 || 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.actionKind !== "operation.execute" && selection.item.status === "error" + && ((selection.item.reviewPlan?.retryOriginal && selection.item.reviewPlan.canApply) + || (!preserveOriginalProposal && !["team.plan", "goal.create"].includes(selection.item.actionKind))) + ? + : !readOnly && selection.item.actionKind !== "operation.execute" && selection.item.status !== "gated" + && !((preserveOriginalProposal || ["team.plan", "goal.create"].includes(selection.item.actionKind)) + && ["error", "stale"].includes(selection.item.status)) + && !((selection.item.actionKind === "team.plan" || decisionFrame || taskExecution || completedTodoCreation) + && selection.item.status === "applied") + ? + : null} + {!readOnly && selection.item.actionKind !== "operation.execute" && !preserveOriginalProposal + && !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} diff --git a/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx b/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx index ce9b6dbff1..71e75b04b5 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/i18n.tsx @@ -961,6 +961,7 @@ const en = { "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.todo_creation_retry": "Confirmation submitted. Retry to recover the original result.", "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.", @@ -2587,6 +2588,7 @@ const zhCN: Record = { "actionReview.canonical_update_retry": "编辑结果尚未确认。重试此操作以恢复原结果。", "actionReview.goal_creation_retry": "Goal 初始化尚未完成。请重试原创建操作,已记录的存储选择会保留。", "actionReview.task_start_retry": "任务已保存。重试原操作以继续启动这个任务。", + "actionReview.todo_creation_retry": "已提交创建确认。重试本次确认以取回结果。", "actionReview.canonical_update_projection_pending": "操作已提交,展示尚未同步。重试此操作以恢复当前视图。", "actionReview.team_plan_retry": "可以从同一提案重试分配。系统会重新核验当前状态,并恢复已记录的结果,不会重复创建任务。", "actionReview.apply_failed": "执行未完成。请检查失败原因并重新生成预览后再试。", diff --git a/docs/project-agent-todo-contract.md b/docs/project-agent-todo-contract.md index 9256b99c24..ad4f6c150f 100644 --- a/docs/project-agent-todo-contract.md +++ b/docs/project-agent-todo-contract.md @@ -90,6 +90,15 @@ same operation ID is a conflict. No provider promotion or receipt rewrite occurs ## Write Contract +When a Chat confirmation has recorded its Todo, the original card keeps the +retry. It verifies the original create receipt and store history. Recovery +can read a saved assignment or an already dispatched Turn. A pending dispatch +also checks the current Goal, claimant and lease. A proposal that recorded a +Todo cannot be regenerated. + +Chat 确认一旦记录 Todo,就从原卡重试,以验证原创建回执和存储历史。重试可取回已保存的 +指派或已启动的 Turn。仍待启动时须检查当前 Goal、认领者和租约。已记录 Todo 的确认不能重新生成。 + For a caller-owned runtime that already registered its Goal/Agent, generate the model planning checkpoint before writing executable task Todos: @@ -1390,6 +1399,33 @@ create/update/poll/terminal receipt is recovered before checking current source contents. Claim replay retains its current acceptance check and rejects a stale source while preserving `original_receipt`; this adds no new claim grant. +Chat Task detail and reviewed actions resolve the Goal's source registry and +runtime before reading or writing. The review basis carries the source identity, +source registry digest and provider revision. A created Task checkpoint also +retains the authority store identity and original operation. +If an unrelated write advances the same store, assignment and managed claim can +refresh their provider revision after the native owner rejects the previous one. +The current Todo must match the record in the original native create receipt. +Chat saves the new revision before dispatch so an interrupted response replays +the same assignment. + +For an older Chat confirmation whose assignment evidence is missing, the card +can show the verified creation and current assignee from the original store. +A matching assignee and update reason do not establish that the original +confirmation made the assignment. Open the existing Todo to review ownership +when the card cannot verify that step. + +When a separate directory supplies the Goal route, Chat Task, Monitor and Gate +mutations carry its byte witness as `registry_source.routing_registry`. The +native transaction checks both registry files at admission and before commit. +Restore the original source and inspect the retained Task when either source +changes or the original Todo no longer matches. + +The local v3 Todo create and managed claim wires require both +`expected_provider_revision` and `expected_registry_sha256`. The source check +uses the existing registry witness. Earlier wire versions retain their original +behavior. + On `authority_source_changed`, inspect current registration and the Todo again before issuing new work. The rejected attempt writes no canonical head or receipt; an external validator already executed may have its own effects. This is an @@ -1413,6 +1449,10 @@ registry 来源校验。Python 在采集注册/grant 事实前后比较来源摘 继续负责状态与回执的原子持久化,不改变默认 provider、promotion 或 PostgreSQL 的 service-owned 认证边界。 +旧 Chat 确认缺少指派凭证时,原卡可以展示经核验的创建结果和原存储中的当前负责人。 +负责人和更新理由一致也无法证明指派来自原确认。原卡无法核验这一步时,可打开已有 +Todo 查看归属。 + ### Canonical nonterminal planning updates An explicitly promoted agent Todo also accepts a bounded planning update through diff --git a/loopx/chat.py b/loopx/chat.py index d3b31f13d3..4c713baae6 100644 --- a/loopx/chat.py +++ b/loopx/chat.py @@ -1053,7 +1053,7 @@ def _compact_todo_payload(payload: dict[str, Any], *, applied: bool) -> dict[str } -def _todo_preview_fingerprint(payload: dict[str, Any]) -> str: +def _todo_preview_fingerprint(payload: dict[str, Any], *, source_identity: str | None = None) -> str: compact = _compact_todo_payload(payload, applied=False) if payload.get("decision_read_from_provider") is True: # Bind the transport operation to reviewed intent and provider head. @@ -1066,6 +1066,7 @@ def _todo_preview_fingerprint(payload: dict[str, Any]) -> str: "added": compact["added"], "already_exists": compact["already_exists"], "expected_revision": _todo_revision(payload), + **({"source_identity": source_identity} if source_identity is not None else {}), } ) @@ -1099,6 +1100,7 @@ def _todo_no_write_receipt( current_preview: dict[str, Any], write_attempted: bool = False, state_revision: str | None = None, + source_identity: str | None = None, ) -> dict[str, Any]: receipt = { "schema_version": CHAT_TODO_NO_WRITE_RECEIPT_SCHEMA_VERSION, @@ -1107,7 +1109,7 @@ def _todo_no_write_receipt( "outcome": "preview_stale", "write_attempted": write_attempted, **( - {"current_preview_id": _todo_preview_fingerprint(current_preview)} + {"current_preview_id": _todo_preview_fingerprint(current_preview, source_identity=source_identity)} if not write_attempted and state_revision is None else {} ), @@ -1121,15 +1123,19 @@ def _add_review_todo( *, registry_path: Path, goal_id: str, + runtime_root_arg: str | None, text: str, priority: str | None = None, dry_run: bool, expected_provider_revision: str | None = None, operation_id: str | None = None, + expected_registry_sha256: str | None = None, + routing_registry_witness: dict[str, str] | None = None, ) -> dict[str, Any]: return add_goal_todo( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, role="agent", text=_normalize_todo_text(text), priority=priority, @@ -1142,6 +1148,10 @@ def _add_review_todo( if expected_provider_revision is not None else {} ), + **({"expected_registry_sha256": expected_registry_sha256} + if expected_registry_sha256 is not None else {}), + **({"routing_registry_witness": routing_registry_witness} + if routing_registry_witness is not None else {}), ) @@ -1149,12 +1159,15 @@ def _bound_todo_review_preview( *, registry_path: Path, goal_id: str, + runtime_root_arg: str | None, text: str, priority: str | None, + source_identity: str | None, ) -> dict[str, Any]: payload = _add_review_todo( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, dry_run=True, @@ -1168,10 +1181,11 @@ def _bound_todo_review_preview( return _add_review_todo( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, dry_run=True, - operation_id="chat-todo:" + _todo_preview_fingerprint(payload), + operation_id="chat-todo:" + _todo_preview_fingerprint(payload, source_identity=source_identity), expected_provider_revision=_todo_revision(payload), ) except LocalCoordinationAuthorityUnavailable as exc: @@ -1193,15 +1207,19 @@ def build_todo_review_preview( goal_id: str, text: str, priority: str | None = None, + runtime_root_arg: str | None = None, + source_identity: str | None = None, ) -> dict[str, Any]: payload = _bound_todo_review_preview( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, + source_identity=source_identity, ) compact = _compact_todo_payload(payload, applied=False) - compact["preview_id"] = _todo_preview_fingerprint(payload) + compact["preview_id"] = _todo_preview_fingerprint(payload, source_identity=source_identity) return compact @@ -1212,28 +1230,41 @@ def apply_todo_review_preview( text: str, priority: str | None = None, preview_id: str, + runtime_root_arg: str | None = None, + expected_registry_sha256: str | None = None, + routing_registry_witness: dict[str, str] | None = None, + source_identity: str | None = None, + return_authority_write: bool = False, ) -> dict[str, Any]: current_preview = _bound_todo_review_preview( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, + source_identity=source_identity, ) - if not preview_id or preview_id != _todo_preview_fingerprint(current_preview): + if not preview_id or preview_id != _todo_preview_fingerprint(current_preview, source_identity=source_identity): raise TodoReviewPreviewConflict( "stale todo preview; preview the proposal again before applying", receipt=_todo_no_write_receipt( goal_id=goal_id, current_preview=current_preview, + source_identity=source_identity, ), ) try: applied = _add_review_todo( registry_path=registry_path, goal_id=goal_id, + runtime_root_arg=runtime_root_arg, text=text, priority=priority, dry_run=False, + **({"expected_registry_sha256": expected_registry_sha256} + if expected_registry_sha256 is not None else {}), + **({"routing_registry_witness": routing_registry_witness} + if routing_registry_witness is not None else {}), **( { "expected_provider_revision": _todo_revision(current_preview), @@ -1244,7 +1275,7 @@ def apply_todo_review_preview( ), ) except LocalCoordinationAuthorityUnavailable as exc: - if exc.code != "provider_revision_mismatch": + if exc.code not in {"provider_revision_mismatch", "authority_source_changed"}: raise # Only a conclusive owner rejection proves no write. Unknown commit # responses keep their existing original-operation recovery contract. @@ -1264,4 +1295,10 @@ def apply_todo_review_preview( preview_payload=current_preview, applied_payload=applied, ) + if return_authority_write and applied.get("decision_read_from_provider") is True: + compact["authority_write"] = { + "provider_revision": applied["provider_revision"], + "source_authority": applied["source_authority"], + "operation_id": applied["operation_id"], + } return compact diff --git a/loopx/chat/monitor_actions.py b/loopx/chat/monitor_actions.py index d723d76636..92cbb9b872 100644 --- a/loopx/chat/monitor_actions.py +++ b/loopx/chat/monitor_actions.py @@ -7,6 +7,7 @@ from ..control_plane.runtime.time import parse_timestamp from ..control_plane.todos.mutation_api import complete_goal_todo, update_goal_todo +from .todo_source import resolve_chat_todo_source class ChatMonitorActionMixin: @@ -20,9 +21,13 @@ def _run_monitor_update( goal_id = str(parameters["goal_id"]) operation = str(parameters["operation"]) + source = resolve_chat_todo_source(self, goal_id) + if basis is not None: + source.require_review(basis) if operation == "stop": result = complete_goal_todo( - registry_path=self.registry_path, + **source.writer_scope(), + routing_registry_witness=source.routing_registry_witness, goal_id=goal_id, todo_id=str(parameters["todo_id"]), evidence="Owner stopped the continuous monitor from typed Chat action.", @@ -35,7 +40,8 @@ def _run_monitor_update( ) else: kwargs: dict[str, Any] = { - "registry_path": self.registry_path, + **source.writer_scope(), + "routing_registry_witness": source.routing_registry_witness, "goal_id": goal_id, "todo_id": str(parameters["todo_id"]), "agent_id": str(parameters["agent_id"]), diff --git a/loopx/chat/status_api.py b/loopx/chat/status_api.py index c43b0e48d0..770fb85afb 100644 --- a/loopx/chat/status_api.py +++ b/loopx/chat/status_api.py @@ -53,10 +53,11 @@ from ..control_plane.todos.list_readback import list_goal_todos -def goal_read_runtime_root(server: Any) -> str | None: +def goal_read_runtime_root(server: Any, *, registry: dict[str, Any] | None = None) -> str | None: """Keep registry source routing; Chat storage is only an unbound fallback.""" - registry = _discovery_registry(server.registry_path) - if registry.get("common_runtime_root"): + if registry is None: + registry = _discovery_registry(server.registry_path) + if registry.get("common_runtime_root") or registry.get("registry_role") == "global-local": # Passing the coordinator's resolved root as an explicit override # suppresses the collector's per-Goal source_registry routing. A # global directory may contain Goals owned by different runtimes. diff --git a/loopx/chat/todo_actions.py b/loopx/chat/todo_actions.py index 4bdc1150f2..9b4826f69e 100644 --- a/loopx/chat/todo_actions.py +++ b/loopx/chat/todo_actions.py @@ -2,26 +2,25 @@ from __future__ import annotations -from collections.abc import Iterator +from collections.abc import Callable, Iterator from contextlib import ExitStack, contextmanager from pathlib import Path from typing import Any +from ..agent_registry import registered_agent_ids_for_goal +from ..chat import apply_todo_review_preview, build_todo_review_preview from ..chat_store import ChatSessionStore, TERMINAL_TURN_STATES from ..file_lock import exclusive_cross_runtime_file_lock -from ..history import load_registry -from ..registry import find_registry_goal from ..control_plane.goals.activation import goal_is_stopped from ..control_plane.coordination.shadow_management import shadow_maintenance_lock_target -from ..control_plane.runtime.runtime_projection_route import resolve_goal_source_runtime_route from ..control_plane.todos.mutation_api import complete_goal_todo, update_goal_todo from ..control_plane.todos.completion_validation import COMPLETION_VALIDATION_IN_PROGRESS from ..control_plane.coordination.local_authority import ( LocalCoordinationAuthorityUnavailable, read_canonical_todos_if_promoted, ) -from ..paths import effective_runtime_root from ..control_plane.todos.provider_projection import projection_delivery_requires_ack from ..control_plane.work_items.task_lease import lease_is_active +from .todo_source import ChatTodoSource, ChatTodoSourceChanged, resolve_chat_todo_source, task_start_claim_operation_id def _decision_readback(result: dict[str, Any]) -> dict[str, Any]: @@ -36,29 +35,220 @@ def _decision_readback(result: dict[str, Any]) -> dict[str, Any]: class ChatTodoActionMixin: """Keep Todo preview/apply parity separate from general orchestration.""" + def _apply_todo_create( + self, proposal_id: str, proposal: dict[str, Any], parameters: dict[str, Any], + ) -> dict[str, Any]: + from ..chat_actions import ProtectedActionGate, _opaque + + goal_id = str(parameters["goal_id"]) + source = resolve_chat_todo_source(self, goal_id) + agent_id = parameters.get("agent_id") + start_execution = parameters.get("start_execution") is True + checkpoint = proposal.get("checkpoint") + steps = checkpoint.get("steps") if isinstance(checkpoint, dict) else {} + steps = steps if isinstance(steps, dict) else {} + assignment_completed = isinstance(steps.get("todo_assigned"), dict) + todo_step = steps.get("todo_created") + if isinstance(todo_step, dict) and todo_step.get("todo_id"): + todo_id = _opaque(todo_step.get("todo_id"), field="todo_id") + canonical_receipt = todo_step.get("canonical_receipt") + if not isinstance(canonical_receipt, dict): + raise ValueError("Todo creation checkpoint is malformed") + binding = todo_step.get("source") + current_fingerprint = str( + proposal.get("expected_state_fingerprint") or "" + ) + if "source" not in todo_step: + try: + assignment = steps.get("todo_assigned") + if isinstance(assignment, dict) and (not agent_id or assignment.get("todo_id") != todo_id + or assignment.get("agent_id") != str(agent_id)): + raise ChatTodoSourceChanged("The saved assignment does not identify this Task and Agent.") + authority = read_canonical_todos_if_promoted(runtime_root=source.runtime_root, goal_id=goal_id) + if start_execution and agent_id and authority: + # A committed claim can predate the corresponding Chat checkpoint. + claim_completed = source.has_managed_task_claim( + proposal_id, todo_id, str(agent_id), authority["source_authority"], + ) + if assignment_completed and not claim_completed: + # Older releases submitted the Turn after a plain assignment. + _, turn = self._read_confirmed_task(proposal_id, parameters, todo_id, steps) + if turn is None or not self._task_turn_dispatched(turn): + raise ChatTodoSourceChanged("The saved Task start claim is unavailable.") + assignment_completed = assignment_completed or claim_completed + binding = source.recover_legacy_creation( + todo_id=todo_id, preview_id=current_fingerprint, receipt=canonical_receipt, authority=authority, + assignment_pending=bool(agent_id) and not assignment_completed, + ) + if not assignment_completed and agent_id and agent_id not in registered_agent_ids_for_goal(source.goal): + raise ChatTodoSourceChanged("The selected Agent is no longer registered for the Goal") + except (ChatTodoSourceChanged, LocalCoordinationAuthorityUnavailable) as error: + summary = f"The original confirmation recorded Todo {todo_id}. Its assignment source could not be verified." + next_action = "Restore the original Goal source and retry, or review the assignment from the existing Todo." + current = error.verified_todo if isinstance(error, ChatTodoSourceChanged) else None + if current is not None: + assignee = current.get("claimed_by") + assignment_state = f"Its current assignee is {assignee}." if assignee else "It is currently unassigned." + summary = ( + f"The original confirmation created Todo {todo_id}. {assignment_state} " + "The original assignment could not be verified." + ) + next_action = "Open the existing Todo to review its current assignment." + raise ProtectedActionGate("todo.create", gate={ + "kind": "canonical_authority_required", + "summary": summary, + "next_action": next_action, + }) from error + self.store.save_checkpoint(proposal_id, step="todo_created", receipt={**todo_step, "source": binding}) + if not isinstance(binding, dict): + raise ValueError("Todo creation checkpoint source is malformed") + source.require_review(binding) + else: + if agent_id and agent_id not in registered_agent_ids_for_goal(source.goal): + raise ValueError("the selected Agent is no longer registered for this Goal") + authority = read_canonical_todos_if_promoted(runtime_root=source.runtime_root, goal_id=goal_id) + if not isinstance(authority.get("store_identity"), str): + raise ValueError("The Goal's Todo authority identity is unavailable") + current = build_todo_review_preview( + **source.writer_scope(), + goal_id=goal_id, + text=str(parameters["text"]), + priority=parameters.get("priority"), + source_identity=source.source_identity, + ) + current_fingerprint = str(current["preview_id"]) + if current_fingerprint != proposal.get("expected_state_fingerprint"): + stale = self.store.apply( + proposal_id, + current_state_fingerprint=current_fingerprint, + receipt={}, + ) + return {"proposal": stale, "turn": None} + applied = apply_todo_review_preview( + **source.writer_scope(), + goal_id=goal_id, + text=str(parameters["text"]), + priority=parameters.get("priority"), + preview_id=current_fingerprint, + expected_registry_sha256=source.registry_sha256, + routing_registry_witness=source.routing_registry_witness, + source_identity=source.source_identity, + return_authority_write=True, + ) + canonical_receipt = applied["receipt"] + todo_id = _opaque(canonical_receipt["todo_id"], field="todo_id") + binding = { + **applied["authority_write"], + "source_identity": source.source_identity, + "store_identity": authority["store_identity"], + "registry_sha256": source.registry_sha256, + } + self.store.save_checkpoint( + proposal_id, + step="todo_created", + receipt={ + "todo_id": todo_id, + "canonical_receipt": canonical_receipt, + "source": binding, + }, + ) + if agent_id and not start_execution and not assignment_completed: + def apply_assignment(current_source: ChatTodoSource, revision: str) -> dict[str, Any]: + return update_goal_todo( + **current_source.writer_scope(), + goal_id=goal_id, + todo_id=todo_id, + claimed_by=str(agent_id), + agent_id=str(agent_id), + authority_reason="owner-confirmed typed Chat action", + update_operation_id=f"chat-assign:{proposal_id}", + update_expected_provider_revision=revision, + update_expected_registry_sha256=binding["registry_sha256"], + routing_registry_witness=current_source.routing_registry_witness, + dry_run=False, + ) + + self._apply_created_todo_assignment( + proposal_id, goal_id=goal_id, todo_id=todo_id, binding=binding, steps=steps, apply=apply_assignment, + ) + self.store.save_checkpoint( + proposal_id, + step="todo_assigned", + receipt={"todo_id": todo_id, "agent_id": str(agent_id)}, + ) + receipt = { + "receipt_id": str(canonical_receipt["receipt_id"]), + "outcome": str(canonical_receipt["outcome"]), + "projection_verified": True, + "resource_ids": { + "goal_id": str(canonical_receipt["goal_id"]), + "todo_id": todo_id, + **({"agent_id": str(agent_id)} if agent_id else {}), + }, + "canonical_receipt": canonical_receipt, + } + turn_result = None + if start_execution: + source = resolve_chat_todo_source(self, goal_id) + source.require_review(binding) + turn_result = self._start_confirmed_task( + proposal_id, parameters, todo_id, steps, source=source, binding=binding, + ) + receipt["resource_ids"].update( + {"session_id": turn_result["session_id"], "turn_id": turn_result["turn_id"]} + ) + receipt["outcome"] = "task_execution_started" + stored = self.store.apply( + proposal_id, + current_state_fingerprint=current_fingerprint, + receipt=receipt, + ) + return {"proposal": stored, "turn": turn_result} + + def _apply_created_todo_assignment( + self, proposal_id: str, *, goal_id: str, todo_id: str, binding: dict[str, Any], + steps: dict[str, Any], apply: Callable[[ChatTodoSource, str], dict[str, Any]], + ) -> dict[str, Any]: + def observe() -> tuple[ChatTodoSource, dict[str, Any]]: + source = resolve_chat_todo_source(self, goal_id) + source.require_review(binding) + authority = read_canonical_todos_if_promoted(runtime_root=source.runtime_root, goal_id=goal_id) + source.require_created_todo(binding, authority) + return source, authority + + source, _ = observe() + basis = steps.get("todo_assignment_basis", binding) + try: + return apply(source, basis["provider_revision"]) + except LocalCoordinationAuthorityUnavailable as error: + if error.code != "provider_revision_mismatch": + raise + + # Only a definitive stale-revision rejection permits a new CAS basis. + # Preserve it before dispatch so a lost response replays the same intent. + source, authority = observe() + revision = source.current_assignment_revision(binding, authority, todo_id) + self.store.save_checkpoint(proposal_id, step="todo_assignment_basis", receipt={"provider_revision": revision}) + return apply(source, revision) + @contextmanager def _confirmed_task_commit( self, *, goal_id: str, todo_id: str, endpoint: str, project: Path, - claim: dict[str, Any], + claim: dict[str, Any], source: ChatTodoSource, binding: dict[str, Any], ) -> Iterator[None]: """Fence acceptance with the same registry and canonical writers as Stop and Todo edits.""" invoked = self.registry_path.resolve() - route = resolve_goal_source_runtime_route(registry_path=invoked, goal_id=goal_id) - source = Path(route["source_registry"]) - # Claim used this runtime. Recheck it after taking the same maintenance - # guard as canonical writers, in the order used by Goal deletion. - runtime = effective_runtime_root(invoked, None).resolve() - targets: tuple[Path, ...] = (shadow_maintenance_lock_target(runtime, goal_id), source) - if source != invoked: + runtime = source.runtime_root + targets = (shadow_maintenance_lock_target(runtime, goal_id), source.registry_path) + if source.registry_path != invoked: targets += (invoked,) with ExitStack() as locks: for target in targets: locks.enter_context(exclusive_cross_runtime_file_lock(target, operation="confirm_task_admission")) - if (route != resolve_goal_source_runtime_route(registry_path=invoked, goal_id=goal_id) - or runtime != effective_runtime_root(invoked, None).resolve()): - raise ValueError("The reviewed Task's Goal source changed") - goal = find_registry_goal(load_registry(source), goal_id) - if goal is None or goal.get("status", "active") not in {"active", "ready"} or goal_is_stopped(goal): + current_source = resolve_chat_todo_source(self, goal_id) + current_source.require_review(binding) + goal = current_source.goal + if goal.get("status", "active") not in {"active", "ready"} or goal_is_stopped(goal): raise ValueError("The Task's Goal is no longer active") if Path(str(goal.get("repo") or ".")).expanduser().resolve() != project: raise ValueError("The reviewed Task's project root changed") @@ -66,6 +256,7 @@ def _confirmed_task_commit( if self._resolve_goal_agent(goal_id, endpoint, goal=goal) != owner: raise ValueError("The reviewed Agent endpoint now belongs to another Goal Agent") authority = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id=goal_id, include_leases=True) + current_source.require_created_todo(binding, authority) todo = next((row for row in authority["todos"] if row["todo_id"] == todo_id), None) if (todo is None or todo.get("role") != "agent" or todo.get("status") != "open" or todo.get("claimed_by") != owner or authority["handoff_mode"] != claim["handoff_mode"]): @@ -106,15 +297,37 @@ def _read_task_start( raise ValueError("The original Task Session is ambiguous") return (sessions[0] if sessions else None), None + def _read_confirmed_task( + self, proposal_id: str, parameters: dict[str, Any], todo_id: str, steps: dict[str, Any], + ) -> tuple[dict[str, Any] | None, dict[str, Any] | None]: + controller = self.runtime_controller + chat = getattr(controller, "store", None) or self.chat_store + if chat is None: + raise ValueError("Chat Runtime Controller is unavailable") + execution = steps.get("execution_started") + session, turn = self._read_task_start( + chat, goal_id=str(parameters["goal_id"]), + agent_id=str(parameters.get("endpoint_id") or parameters["agent_id"]), channel_id=f"task.{todo_id}", + client_turn_id=f"task-start-{proposal_id}", + checkpoint=execution if execution is not None else steps.get("task_session_opened"), + ) + if execution is not None and (turn is None or turn["turn_id"] != execution.get("turn_id")): + raise ValueError("The original Task Turn is unavailable") + if turn is not None and turn.get("message") != str(parameters["text"]): + raise ValueError("The original Task Turn request changed") + return session, turn + + @staticmethod + def _task_turn_dispatched(turn: dict[str, Any]) -> bool: + return turn.get("status") in TERMINAL_TURN_STATES or bool(turn.get("started_at") or turn.get("upstream_turn_id")) + def _start_confirmed_task( self, proposal_id: str, parameters: dict[str, Any], todo_id: str, steps: dict[str, Any], + *, source: ChatTodoSource, binding: dict[str, Any], ) -> dict[str, Any]: from ..chat_actions import _opaque controller = self.runtime_controller - chat = getattr(controller, "store", None) or self.chat_store - if chat is None: - raise ValueError("Chat Runtime Controller is unavailable") goal_id = str(parameters["goal_id"]) owner = str(parameters["agent_id"]) endpoint = str(parameters.get("endpoint_id") or owner) @@ -123,43 +336,45 @@ def _start_confirmed_task( client_turn_id = f"task-start-{proposal_id}" opened = steps.get("task_session_opened") execution = steps.get("execution_started") - session, turn = self._read_task_start( - chat, goal_id=goal_id, agent_id=endpoint, channel_id=channel_id, - client_turn_id=client_turn_id, checkpoint=execution if execution is not None else opened, - ) - if execution is not None and (turn is None or turn["turn_id"] != execution.get("turn_id")): - raise ValueError("The original Task Turn is unavailable") - if turn is not None and turn.get("message") != message: - raise ValueError("The original Task Turn request changed") + session, turn = self._read_confirmed_task(proposal_id, parameters, todo_id, steps) # An already dispatched or terminal Turn is readback. Only work that # still needs dispatch passes through the current Goal and claim gates. created = False - if turn is None or ( - turn.get("status") not in TERMINAL_TURN_STATES - and not (turn.get("started_at") or turn.get("upstream_turn_id")) - ): + if turn is None or not self._task_turn_dispatched(turn): if controller is None: raise ValueError("Chat Runtime Controller is unavailable") if session is not None and session.get("status") == "closed": raise ValueError("The original Task Session is closed") - goal = self._goal(goal_id) + goal = source.goal 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(endpoint, project=project) - if parameters.get("endpoint_id") and self._resolve_goal_agent(goal_id, endpoint) != owner: + if parameters.get("endpoint_id") and self._resolve_goal_agent(goal_id, endpoint, goal=goal) != owner: raise ValueError("The reviewed Agent endpoint now belongs to another Goal Agent") - claim_args = { - "registry_path": self.registry_path, "goal_id": goal_id, "todo_id": todo_id, + claim_args: dict[str, Any] = { + "goal_id": goal_id, "todo_id": todo_id, "claimed_by": owner, "agent_id": owner, "claim_only": True, "manage_execution": True, } - claim = update_goal_todo(**claim_args, claim_operation_id=f"chat-task-start:{proposal_id}", dry_run=False) + + def apply_claim(current_source: ChatTodoSource, revision: str) -> dict[str, Any]: + return update_goal_todo( + **current_source.writer_scope(), **claim_args, + claim_operation_id=task_start_claim_operation_id(proposal_id), dry_run=False, + claim_expected_provider_revision=revision, + claim_expected_registry_sha256=binding["registry_sha256"], + routing_registry_witness=current_source.routing_registry_witness, + ) + + claim = self._apply_created_todo_assignment( + proposal_id, goal_id=goal_id, todo_id=todo_id, binding=binding, steps=steps, apply=apply_claim, + ) if claim["handoff_mode"] == "soft_claim" and claim["status"] in {"recovered", "replayed"}: # A past soft claim still needs a read-only current-owner check. - if update_goal_todo(**claim_args, dry_run=True)["changed"]: + if update_goal_todo(**source.writer_scope(), **claim_args, dry_run=True)["changed"]: raise ValueError("The reviewed Task assignment changed before launch") if not isinstance(steps.get("todo_assigned"), dict): self.store.save_checkpoint( @@ -177,6 +392,7 @@ def _start_confirmed_task( session_id=sid, client_turn_id=client_turn_id, message=message, work_dir=project, objective=message, _goal_commit=lambda: self._confirmed_task_commit( goal_id=goal_id, todo_id=todo_id, endpoint=endpoint, project=project, claim=claim, + source=source, binding=binding, ), ) @@ -194,8 +410,13 @@ def _run_todo_update( ) -> dict[str, Any]: goal_id = str(parameters["goal_id"]) operation = str(parameters.get("operation") or "edit") - if parameters.get("review_basis") is not None and parameters["review_basis"] != basis: + reviewed = parameters.get("review_basis") + if dry_run and reviewed is not None and ( + basis is None or any(basis.get(key) != value for key, value in reviewed.items())): raise ValueError("The Task source changed. Reload its complete requirements before saving.") + source = resolve_chat_todo_source(self, goal_id) + if basis is not None: + source.require_review(basis) # Transport only caller-supplied proof. The terminal owner revalidates # the execution; a lease read or a Todo claim is never substitute proof. proof = {key: parameters[key] for key in ( @@ -204,7 +425,8 @@ def _run_todo_update( if operation == "complete" and (basis is None or basis.get("schema_version") == "loopx_chat_canonical_terminal_basis_v0"): return complete_goal_todo( - registry_path=self.registry_path, + **source.writer_scope(), + routing_registry_witness=source.routing_registry_witness, goal_id=goal_id, todo_id=str(parameters["todo_id"]), note=parameters.get("note"), @@ -224,7 +446,8 @@ def _run_todo_update( elif operation == "defer": status = "deferred" return update_goal_todo( - registry_path=self.registry_path, + **source.writer_scope(), + routing_registry_witness=source.routing_registry_witness, goal_id=goal_id, todo_id=str(parameters["todo_id"]), text=parameters.get("text"), @@ -259,9 +482,14 @@ def _run_gate_resolve( ) -> dict[str, Any]: # The typed User completion owner decides whether this source may carry # the outcome and what it does to the dependent; the App only records it. + goal_id = str(parameters["goal_id"]) + source = resolve_chat_todo_source(self, goal_id) + if basis is not None: + source.require_review(basis) return complete_goal_todo( - registry_path=self.registry_path, - goal_id=str(parameters["goal_id"]), + **source.writer_scope(), + routing_registry_witness=source.routing_registry_witness, + goal_id=goal_id, todo_id=str(parameters["todo_id"]), role="user", decision_outcome=str(parameters["decision"]), @@ -303,12 +531,14 @@ def _canonical_update_basis( self, goal_id: str, *, completion_todo_id: str | None = None, decision: bool = False, ) -> dict[str, Any] | None: - registry_sha256 = self._registry_fingerprint() + source = resolve_chat_todo_source(self, goal_id) authority = read_canonical_todos_if_promoted( - runtime_root=effective_runtime_root(self.registry_path, None), - goal_id=goal_id, include_leases=True, + runtime_root=source.runtime_root, + goal_id=goal_id, ) - if self._registry_fingerprint() != registry_sha256: + current = resolve_chat_todo_source(self, goal_id) + if (current.source_identity, current.registry_sha256) != ( + source.source_identity, source.registry_sha256): raise ValueError("Todo authority registration changed while reading; retry") if authority is None: return None @@ -323,7 +553,8 @@ def _canonical_update_basis( "loopx_chat_canonical_update_basis_v0"), "provider_revision": authority["provider_revision"], "source_authority": authority["source_authority"], - "registry_sha256": registry_sha256, + "registry_sha256": source.registry_sha256, + "source_identity": source.source_identity, } def _apply_reviewed_todo_edit( @@ -345,6 +576,12 @@ def _apply_reviewed_todo_edit( terminal = action_kind == "gate.resolve" or parameters.get("operation") in {"complete", "stop"} try: result = run(parameters, dry_run=False, basis=basis, operation_id=operation_id) + except ChatTodoSourceChanged as error: + failed = self.store.mark_failed( + proposal_id, error_code="canonical_update_source_changed", message=str(error), + details={"operation_id": operation_id}, + ) + return {"proposal": failed, "turn": None} except LocalCoordinationAuthorityUnavailable as error: if error.code == COMPLETION_VALIDATION_IN_PROGRESS: # The original caller still owns its result and proposal. @@ -363,7 +600,7 @@ def _apply_reviewed_todo_edit( # The native transaction first established that no matching # historical receipt exists. This is a rejected new edit. stale = self.store.apply(proposal_id, current_state_fingerprint=_digest({ - "current": self._goal_state_fingerprint(str(parameters["goal_id"])), + "expected": proposal["expected_state_fingerprint"], "conflict": error.code, }), receipt={}) return {"proposal": stale, "turn": None} diff --git a/loopx/chat/todo_detail.py b/loopx/chat/todo_detail.py index 08def79434..43fd4cad99 100644 --- a/loopx/chat/todo_detail.py +++ b/loopx/chat/todo_detail.py @@ -1,9 +1,8 @@ """Loopback-only exact Task request reads over the existing Todo owner.""" -import hashlib from urllib.parse import parse_qs, urlparse -from .status_api import goal_read_runtime_root +from .todo_source import resolve_chat_todo_source from ..status_server import is_loopback_host from ..control_plane.todos.list_readback import list_goal_todos @@ -23,12 +22,10 @@ def _todo_detail(self) -> None: try: if not self.server.registry_path.is_file(): raise OSError("Task registry is unavailable") - registry_digest = hashlib.sha256(self.server.registry_path.read_bytes()).hexdigest() - self._registry_and_goal(goal_id) + source = resolve_chat_todo_source(self.server, goal_id) payload = list_goal_todos( - registry_path=self.server.registry_path, goal_id=goal_id, + **source.writer_scope(), goal_id=goal_id, role="agent", todo_id=todo_id, - runtime_root_arg=goal_read_runtime_root(self.server), ) if payload.get("ambiguous"): self._send_error("The Task source is ambiguous. Refresh its Goal.", status=409) @@ -37,7 +34,9 @@ def _todo_detail(self) -> None: if item is None: self._send_error("The Task is no longer available in this Goal.", status=404) return - if hashlib.sha256(self.server.registry_path.read_bytes()).hexdigest() != registry_digest: + current = resolve_chat_todo_source(self.server, goal_id) + if (current.source_identity, current.registry_sha256) != ( + source.source_identity, source.registry_sha256): raise OSError("Task registration changed during the read") authority = payload.get("authority_read") or {} # Echo the same canonical CAS used by the existing Chat edit owner. @@ -45,7 +44,8 @@ def _todo_detail(self) -> None: basis = ({"schema_version": "loopx_chat_canonical_update_basis_v0", "provider_revision": authority["provider_revision"], "source_authority": authority["source_authority"], - "registry_sha256": registry_digest} + "source_identity": source.source_identity, + "registry_sha256": source.registry_sha256} if authority.get("decision_read_from_provider") is True else None) self._send_json({ "ok": True, "goal_id": goal_id, "todo_id": item["todo_id"], diff --git a/loopx/chat/todo_source.py b/loopx/chat/todo_source.py new file mode 100644 index 0000000000..bb285c52b8 --- /dev/null +++ b/loopx/chat/todo_source.py @@ -0,0 +1,198 @@ +"""Route Chat's Todo reads and reviewed mutations to the same Goal source.""" + +from dataclasses import dataclass +from hashlib import sha256 +from pathlib import Path +from typing import Any + +from ..chat import CHAT_TODO_RECEIPT_SCHEMA_VERSION, _stable_digest, apply_todo_review_preview, build_todo_review_preview +from ..control_plane.effect_runtime import effect_runtime_result +from ..control_plane.runtime.runtime_projection_route import resolve_goal_source_runtime_route +from ..history import load_registry +from ..registry import find_registry_goal +from .status_api import goal_read_runtime_root + + +class ChatTodoSourceChanged(ValueError): + """The current route cannot recover a reviewed operation's historical receipt.""" + + def __init__(self, message: str, *, verified_todo: dict[str, Any] | None = None) -> None: + super().__init__(message) + self.verified_todo = verified_todo + + +def task_start_claim_operation_id(proposal_id: str) -> str: + return f"chat-task-start:{proposal_id}" + + +@dataclass(frozen=True) +class ChatTodoSource: + registry_path: Path + runtime_root: Path + source_identity: str + registry_sha256: str + goal: dict[str, Any] + routing_registry_witness: dict[str, str] | None = None + + def writer_scope(self) -> dict[str, Any]: + return {"registry_path": self.registry_path, "runtime_root_arg": str(self.runtime_root)} + + def require_review(self, basis: dict[str, Any]) -> None: + identity = basis.get("source_identity") + if identity is None: + # Older reviewed actions carry an exact provider revision and + # registry digest. The same canonical transaction still verifies + # both before it can authorize a new mutation. + return + if identity != self.source_identity: + raise ChatTodoSourceChanged("The Task's Goal source changed. Restore its original source before retrying.") + + def require_created_todo(self, binding: dict[str, Any], authority: dict[str, Any]) -> None: + self.require_review(binding) + if (binding.get("source_authority") != authority["source_authority"] + or binding.get("store_identity") != authority["store_identity"]): + raise ChatTodoSourceChanged("The created Task's Todo store changed. Restore its original store before retrying.") + + def _operation_receipt(self, operation_id: str, source_authority: str) -> dict[str, Any]: + result = effect_runtime_result("coordination.local_authority.operation_receipt", { + "schema_version": "loopx_local_coordination_operation_receipt_request_v0", + "runtime_root": str(self.runtime_root), + "goal_id": str(self.goal["id"]), + "operation_id": operation_id, + }, timeout=15.0) + if (not isinstance(result, dict) or result.get("source_authority") != source_authority + or result.get("status") not in {"found", "missing"}): + raise ChatTodoSourceChanged("The original Task operation receipt could not be read from its source.") + return result + + def _created_todo_operation(self, operation_id: str, todo_id: str, source_authority: str) -> dict[str, Any]: + result = self._operation_receipt(operation_id, source_authority) + receipts = result.get("receipts", []) if result["status"] == "found" else [] + if (not isinstance(receipts, list) or len(receipts) != 1 or not isinstance(receipts[0], dict) + or not isinstance(result.get("provider_revision"), str)): + raise ChatTodoSourceChanged("The original Task creation receipt is unavailable. Inspect the original source before retrying.") + created = receipts[0] + if (created.get("schema_version") != "loopx_coordination_todo_create_receipt_v0" + or created.get("goal_id") != str(self.goal["id"]) or created.get("todo_id") != todo_id + or created.get("operation_id") != operation_id or not isinstance(created.get("todo"), dict)): + raise ChatTodoSourceChanged("The original Task creation receipt does not identify this Todo.") + return result + + def has_managed_task_claim(self, proposal_id: str, todo_id: str, owner: str, source_authority: str) -> bool: + operation_id = task_start_claim_operation_id(proposal_id) + result = self._operation_receipt(operation_id, source_authority) + if result["status"] == "missing": + return False + receipts = result.get("receipts") + original = receipts[0] if isinstance(receipts, list) and len(receipts) == 1 else None + if (not isinstance(original, dict) or original.get("schema_version") != "loopx_coordination_todo_claim_receipt_v0" + or original.get("operation_id") != operation_id or original.get("goal_id") != str(self.goal["id"]) + or not isinstance(claim := original.get("result"), dict) + or claim.get("todo_id") != todo_id or claim.get("claimed_by") != owner + or claim.get("execution_managed") is not True or claim.get("claim_operation_id") != operation_id): + raise ChatTodoSourceChanged("The original claim receipt does not identify this Task and Agent.") + return True + + def recover_legacy_creation( + self, *, todo_id: str, preview_id: str, receipt: dict[str, Any], authority: dict[str, Any], + assignment_pending: bool, + ) -> dict[str, Any]: + goal_id = str(self.goal["id"]) + if (not preview_id or receipt.get("schema_version") != CHAT_TODO_RECEIPT_SCHEMA_VERSION + or receipt.get("preview_id") != preview_id or receipt.get("goal_id") != goal_id + or receipt.get("todo_id") != todo_id or receipt.get("status") != "applied" + or receipt.get("receipt_id") != _stable_digest({k: v for k, v in receipt.items() if k != "receipt_id"})): + raise ValueError("Todo creation checkpoint is malformed") + preview_revision = receipt.get("preview_revision") + if (receipt.get("outcome") != "todo_added" or not isinstance(preview_revision, str) or not preview_revision + or not authority or not authority.get("store_identity") or not authority.get("source_authority")): + raise ChatTodoSourceChanged("The original Task creation authority is unavailable.") + operation_id = f"chat-todo:{preview_id}" + result = self._created_todo_operation(operation_id, todo_id, authority["source_authority"]) + cursor = result.get("cursor") + if not isinstance(cursor, str) or not cursor.isdecimal() or int(cursor) < 2: + raise ChatTodoSourceChanged("The original Task creation revision is unavailable.") + + # The preview revision must be the predecessor of this operation in + # the same verified store, even if a replacement reused the Todo id. + original = effect_runtime_result("coordination.local_authority.todo_source", { + "schema_version": "loopx_local_coordination_todo_source_request_v0", + "runtime_root": str(self.runtime_root), + "goal_id": goal_id, + "source": { + "source_authority": authority["source_authority"], + "store_identity": authority["store_identity"], + "provider_revision": preview_revision, + "cursor": str(int(cursor) - 1), + }, + }, timeout=15.0) + if not isinstance(original, dict) or original.get("status") != "loaded": + raise ChatTodoSourceChanged("The original Task store could not be verified.") + # The verified historical receipt proves creation. A pending + # assignment still needs the original Todo snapshot before it can write. + if assignment_pending: + todo = next((row for row in authority["todos"] if row["todo_id"] == todo_id), None) + if result["receipts"][0]["todo"] != todo: + raise ChatTodoSourceChanged("The original Task could not be verified.", verified_todo=todo) + return { + "operation_id": operation_id, + "provider_revision": result["provider_revision"], + "source_authority": authority["source_authority"], + "source_identity": self.source_identity, + "store_identity": authority["store_identity"], + "registry_sha256": self.registry_sha256, + } + + def current_assignment_revision(self, binding: dict[str, Any], authority: dict[str, Any], todo_id: str) -> str: + operation_id = binding.get("operation_id") + if not isinstance(operation_id, str) or not operation_id: + raise ChatTodoSourceChanged("The original Task creation operation is unavailable.") + result = self._created_todo_operation(operation_id, todo_id, binding["source_authority"]) + todo = next((row for row in authority["todos"] if row["todo_id"] == todo_id), None) + if result["provider_revision"] != binding["provider_revision"] or result["receipts"][0]["todo"] != todo: + raise ChatTodoSourceChanged("The created Task no longer matches its original receipt. Review it before assigning.") + return str(authority["provider_revision"]) + + +def resolve_chat_todo_source(host: Any, goal_id: str) -> ChatTodoSource: + invoked = host.registry_path.expanduser().resolve() + snapshots: dict[Path, bytes | None] = {} + directory = load_registry(invoked, source_snapshots=snapshots) + if find_registry_goal(directory, goal_id) is None: + raise ValueError("goal_id was not found in the active LoopX registry") + route = resolve_goal_source_runtime_route( + registry_path=invoked, goal_id=goal_id, registry=directory, source_snapshots=snapshots, + runtime_root_override=goal_read_runtime_root(host, registry=directory), + ) + source = Path(route["source_registry"]) + registry = load_registry(source, source_snapshots=snapshots) + goal = find_registry_goal(registry, goal_id) + if goal is None: + raise ValueError("The Goal is unavailable in its source registry") + for path in {invoked, source}: + if path.read_bytes() != snapshots[path]: + raise ValueError("Todo authority registration changed while reading; retry") + runtime = Path(route["source_runtime_root"]) + return ChatTodoSource( + source, runtime, _stable_digest({"registry": str(source), "runtime": str(runtime)}, length=64), + sha256(snapshots[source] or b"").hexdigest(), goal, + routing_registry_witness=( + {"path": str(invoked), "sha256": sha256(snapshots[invoked] or b"").hexdigest()} + if invoked != source else None + ), + ) + + +def review_todo_create( + host: Any, *, goal_id: str, text: str, preview_id: str | None, +) -> dict[str, Any]: + source = resolve_chat_todo_source(host, goal_id) + if preview_id is None: + return build_todo_review_preview( + **source.writer_scope(), goal_id=goal_id, text=text, source_identity=source.source_identity, + ) + return apply_todo_review_preview( + **source.writer_scope(), goal_id=goal_id, text=text, preview_id=preview_id, + source_identity=source.source_identity, expected_registry_sha256=source.registry_sha256, + routing_registry_witness=source.routing_registry_witness, + ) diff --git a/loopx/chat_action_normalization.py b/loopx/chat_action_normalization.py index 2a6aa1b0cc..272cc09b7f 100644 --- a/loopx/chat_action_normalization.py +++ b/loopx/chat_action_normalization.py @@ -40,6 +40,7 @@ def _normalize( _opaque, _text, ) + from .chat.todo_source import resolve_chat_todo_source if action_kind == "goal.acceptance.check": values = self._allowed_parameters(parameters, allowed={"goal_id", "document", "prepared_by"}) @@ -360,7 +361,7 @@ def _normalize( }, ) goal_id = _opaque(values.get("goal_id"), field="goal_id") - self._goal(goal_id) + goal = resolve_chat_todo_source(self, goal_id).goal result = { "goal_id": goal_id, "text": _text(values.get("text"), field="text", limit=400), @@ -370,10 +371,10 @@ def _normalize( if values.get("endpoint_id"): endpoint_id = _opaque(values.get("endpoint_id"), field="endpoint_id") result["endpoint_id"] = endpoint_id - result["agent_id"] = self._resolve_goal_agent(goal_id, endpoint_id) + result["agent_id"] = self._resolve_goal_agent(goal_id, endpoint_id, goal=goal) elif values.get("agent_id"): agent_id = _opaque(values.get("agent_id"), field="agent_id") - if agent_id not in registered_agent_ids_for_goal(self._goal(goal_id)): + if agent_id not in registered_agent_ids_for_goal(goal): raise ProtectedActionGate( "agent.bind", gate={ @@ -415,7 +416,7 @@ def _normalize( }, ) goal_id = _opaque(values.get("goal_id"), field="goal_id") - self._goal(goal_id) + goal = resolve_chat_todo_source(self, goal_id).goal result: dict[str, Any] = { "goal_id": goal_id, "todo_id": _opaque(values.get("todo_id"), field="todo_id"), @@ -436,10 +437,13 @@ def _normalize( if "review_basis" in values: basis = values["review_basis"] if (operation != "edit" or not values.get("text") or not isinstance(basis, dict) - or set(basis) != {"schema_version", "provider_revision", "source_authority", "registry_sha256"} + or set(basis) - {"source_identity"} != { + "schema_version", "provider_revision", "source_authority", "registry_sha256", + } or basis.get("schema_version") != "loopx_chat_canonical_update_basis_v0" or any(not isinstance(x, str) or not x for x in basis.values()) - or not BARE_SHA256_PATTERN.fullmatch(basis["registry_sha256"])): + or not BARE_SHA256_PATTERN.fullmatch(basis["registry_sha256"]) + or ("source_identity" in basis and not BARE_SHA256_PATTERN.fullmatch(basis["source_identity"]))): raise ValueError("Task edit requires its complete current source basis") result["review_basis"] = dict(basis) if values.get("text"): @@ -460,7 +464,7 @@ def _normalize( if values.get("endpoint_id"): endpoint_id = _opaque(values["endpoint_id"], field="endpoint_id") result["endpoint_id"] = endpoint_id - result["agent_id"] = self._resolve_goal_agent(goal_id, endpoint_id) + result["agent_id"] = self._resolve_goal_agent(goal_id, endpoint_id, goal=goal) elif values.get("agent_id"): result["agent_id"] = _opaque(values["agent_id"], field="agent_id") if values.get("resume_when"): @@ -817,7 +821,7 @@ def _normalize( }, ) goal_id = _opaque(values.get("goal_id"), field="goal_id") - self._goal(goal_id) + goal = resolve_chat_todo_source(self, goal_id).goal operation = str(values.get("operation") or "").strip().lower() if operation not in {"pause", "resume", "stop", "run_now", "edit"}: raise ValueError( @@ -832,7 +836,7 @@ def _normalize( if values.get("endpoint_id"): endpoint_id = _opaque(values["endpoint_id"], field="endpoint_id") result["endpoint_id"] = endpoint_id - result["agent_id"] = self._resolve_goal_agent(goal_id, endpoint_id) + result["agent_id"] = self._resolve_goal_agent(goal_id, endpoint_id, goal=goal) if values.get("target"): result["target"] = _text(values["target"], field="target", limit=400) if values.get("target_key"): diff --git a/loopx/chat_actions.py b/loopx/chat_actions.py index 919e074635..e36e35be39 100644 --- a/loopx/chat_actions.py +++ b/loopx/chat_actions.py @@ -8,10 +8,13 @@ import re from typing import Any, Mapping, Sequence -from .agent_registry import agent_profile_for_goal, registered_agent_ids_for_goal +from .agent_registry import ( + agent_profile_for_goal, + registered_agent_ids_for_goal, +) from .control_plane.projects.identity import GoalCreationConflictError from .control_plane.coordination.new_goal_storage_transport import initialize_goal_storage_target -from .chat import apply_todo_review_preview, build_todo_review_preview +from .chat import build_todo_review_preview from .chat_action_normalization import ChatActionNormalizationMixin from .chat_action_store import ( ACTION_KINDS as SUPPORTED_ACTION_KINDS, @@ -22,6 +25,7 @@ from .chat.monitor_actions import ChatMonitorActionMixin from .chat_store import ChatSessionStore, TERMINAL_TURN_STATES from .chat.todo_actions import ChatTodoActionMixin +from .chat.todo_source import ChatTodoSource, resolve_chat_todo_source from .control_plane.goals.acceptance_actions import GoalAcceptanceActionMixin from .control_plane.goals.goal_objective_actions import GoalObjectiveActionMixin from .configure_goal import configure_goal @@ -40,7 +44,7 @@ from .public_safe_text import OPAQUE_ID_PATTERN as _OPAQUE_ID from .quota import build_quota_should_run from .registry import registry_goals -from .control_plane.todos.mutation_api import add_goal_todo, update_goal_todo +from .control_plane.todos.mutation_api import add_goal_todo from .file_lock import try_exclusive_file_lock @@ -325,6 +329,13 @@ def _registry_fingerprint(self) -> str: raise ValueError("the active LoopX registry is unavailable") from exc return hashlib.sha256(content).hexdigest() + @staticmethod + def _monitor_create_fingerprint(source: ChatTodoSource) -> str: + return _digest({ + "source_identity": source.source_identity, + "registry_sha256": source.registry_sha256, + }) + def _creation_agent_eligibility( self, agent_id: str, *, project: Path | None = None ) -> dict[str, Any]: @@ -1198,16 +1209,21 @@ def _heartbeat_gate(self, parameters: dict[str, Any]) -> ProtectedActionGate: def _apply_monitor_create( self, proposal_id: str, proposal: dict[str, Any], parameters: dict[str, Any] ) -> dict[str, Any]: - current_fingerprint = self._registry_fingerprint() + goal_id = str(parameters["goal_id"]) + source = resolve_chat_todo_source(self, goal_id) + current_fingerprint = self._monitor_create_fingerprint(source) if current_fingerprint != proposal.get("expected_state_fingerprint"): stale = self.store.apply( proposal_id, current_state_fingerprint=current_fingerprint, receipt={} ) return {"proposal": stale, "turn": None} - goal_id = str(parameters["goal_id"]) agent_id = str(parameters["agent_id"]) - if agent_id not in registered_agent_ids_for_goal(self._goal(goal_id)): + if agent_id not in registered_agent_ids_for_goal(source.goal): raise ValueError("monitor Agent must be registered for the Goal") + authority = read_canonical_todos_if_promoted(runtime_root=source.runtime_root, goal_id=goal_id) + revision = authority.get("provider_revision") if authority else None + if not isinstance(revision, str) or not revision: + raise ValueError("The Goal's Todo authority revision is unavailable") stop_condition = str(parameters.get("stop_condition") or "").strip() resume_when = None if ( @@ -1217,25 +1233,36 @@ def _apply_monitor_create( not in {"watch_only", "watch-only", "watch", "continuous", "never"} ): resume_when = stop_condition - metadata = _monitor_metadata(parameters) + metadata = _monitor_metadata(parameters, schedule=False) if not ( metadata.get("expires_at") or resume_when or metadata.get("watch_only") ): metadata["watch_only"] = "true" - result = add_goal_todo( - registry_path=self.registry_path, - goal_id=goal_id, - role="agent", - text=_monitor_text(parameters) or str(parameters["target"]), - task_class="continuous_monitor", - action_kind="observe", - task_repository=parameters.get("task_repository"), - claimed_by=agent_id, - agent_id=agent_id, - resume_when=resume_when, - monitor_metadata=metadata, - dry_run=False, - ) + try: + result = add_goal_todo( + **source.writer_scope(), + routing_registry_witness=source.routing_registry_witness, + expected_registry_sha256=source.registry_sha256, + expected_provider_revision=revision, + goal_id=goal_id, + role="agent", + text=_monitor_text(parameters) or str(parameters["target"]), + task_class="continuous_monitor", + action_kind="observe", + task_repository=parameters.get("task_repository"), + claimed_by=agent_id, + agent_id=agent_id, + resume_when=resume_when, + monitor_metadata=metadata, + dry_run=False, + ) + except LocalCoordinationAuthorityUnavailable as error: + if error.code not in {"provider_revision_mismatch", "authority_source_changed"}: + raise + stale = self.store.apply(proposal_id, current_state_fingerprint=_digest({ + "conflict": error.code, "expected": current_fingerprint, + }), receipt={}) + return {"proposal": stale, "turn": None} todo_id = _opaque(result.get("todo_id"), field="todo_id") receipt = { "receipt_id": _digest( @@ -1411,15 +1438,22 @@ def preview(self, request: Mapping[str, Any]) -> dict[str, Any]: ] permission = "protected" elif action_kind == "todo.create": + source = resolve_chat_todo_source(self, normalized["goal_id"]) canonical_preview = build_todo_review_preview( - registry_path=self.registry_path, + **source.writer_scope(), goal_id=normalized["goal_id"], text=normalized["text"], priority=normalized.get("priority"), + source_identity=source.source_identity, ) fingerprint = str(canonical_preview["preview_id"]) evidence = ["Canonical LoopX Todo dry-run validated the proposal."] permission = "durable_write" + elif action_kind == "monitor.create": + source = resolve_chat_todo_source(self, normalized["goal_id"]) + fingerprint = self._monitor_create_fingerprint(source) + evidence = ["Canonical LoopX contracts validated the bounded request shape."] + permission = "durable_write" elif action_kind == "run.correct": fingerprint = self._session_fingerprint( normalized["session_id"], normalized["goal_id"] @@ -1607,7 +1641,11 @@ def regenerate(self, proposal_id: str) -> dict[str, Any]: "next_action": "Prepare a new operation instead of regenerating this one.", }, ) - if proposal.get("action_kind") == "goal.create" and (proposal.get("checkpoint") or {}).get("steps"): + checkpoint = proposal.get("checkpoint") + steps = checkpoint.get("steps") if isinstance(checkpoint, dict) else None + action_kind = proposal.get("action_kind") + if (action_kind == "goal.create" and steps) or ( + action_kind == "todo.create" and isinstance(steps, dict) and "todo_created" in steps): raise ActionConflictError("Creation already recorded effects; recover the original operation instead of regenerating") if proposal.get("status") not in {"stale", "failed", "gated", "rejected"}: raise ActionConflictError( @@ -1666,8 +1704,10 @@ def _apply_owner_action(self, proposal_id: str, *, steward_context: dict[str, ob }, ) parameters = proposal.get("normalized_parameters") - if (proposal.get("action_kind") in {"todo.create", "goal.create"} and isinstance(parameters, dict) - and parameters.get("start_execution") is True): + action = proposal.get("action_kind") + if isinstance(parameters, dict) and ( + action == "todo.create" or (action == "goal.create" and parameters.get("start_execution") is True) + ): key = _opaque(proposal_id, field="proposal_id") with try_exclusive_file_lock( self.store.root / "task-starts" / f"{key}.json", operation="confirm_task_start", @@ -1725,91 +1765,7 @@ def _apply_confirmed_action(self, proposal_id: str) -> dict[str, object]: }, ) if action_kind == "todo.create": - checkpoint = proposal.get("checkpoint") - steps = checkpoint.get("steps") if isinstance(checkpoint, dict) else {} - steps = steps if isinstance(steps, dict) else {} - todo_step = steps.get("todo_created") - if isinstance(todo_step, dict) and todo_step.get("todo_id"): - todo_id = _opaque(todo_step.get("todo_id"), field="todo_id") - canonical_receipt = todo_step.get("canonical_receipt") - if not isinstance(canonical_receipt, dict): - raise ValueError("Todo creation checkpoint is malformed") - current_fingerprint = str( - proposal.get("expected_state_fingerprint") or "" - ) - else: - current = build_todo_review_preview( - registry_path=self.registry_path, - goal_id=str(parameters["goal_id"]), - text=str(parameters["text"]), - priority=parameters.get("priority"), - ) - current_fingerprint = str(current["preview_id"]) - if current_fingerprint != proposal.get("expected_state_fingerprint"): - stale = self.store.apply( - proposal_id, - current_state_fingerprint=current_fingerprint, - receipt={}, - ) - return {"proposal": stale, "turn": None} - applied = apply_todo_review_preview( - registry_path=self.registry_path, - goal_id=str(parameters["goal_id"]), - text=str(parameters["text"]), - priority=parameters.get("priority"), - preview_id=current_fingerprint, - ) - canonical_receipt = applied["receipt"] - todo_id = _opaque(canonical_receipt["todo_id"], field="todo_id") - self.store.save_checkpoint( - proposal_id, - step="todo_created", - receipt={ - "todo_id": todo_id, - "canonical_receipt": canonical_receipt, - }, - ) - agent_id = parameters.get("agent_id") - 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"]), - todo_id=todo_id, - claimed_by=str(agent_id), - agent_id=str(agent_id), - authority_reason="owner-confirmed typed Chat action", - dry_run=False, - ) - self.store.save_checkpoint( - proposal_id, - step="todo_assigned", - receipt={"todo_id": todo_id, "agent_id": str(agent_id)}, - ) - receipt = { - "receipt_id": str(canonical_receipt["receipt_id"]), - "outcome": str(canonical_receipt["outcome"]), - "projection_verified": True, - "resource_ids": { - "goal_id": str(canonical_receipt["goal_id"]), - "todo_id": todo_id, - **({"agent_id": str(agent_id)} if agent_id else {}), - }, - "canonical_receipt": canonical_receipt, - } - turn_result = None - if start_execution: - turn_result = self._start_confirmed_task(proposal_id, parameters, todo_id, steps) - receipt["resource_ids"].update( - {"session_id": turn_result["session_id"], "turn_id": turn_result["turn_id"]} - ) - receipt["outcome"] = "task_execution_started" - stored = self.store.apply( - proposal_id, - current_state_fingerprint=current_fingerprint, - receipt=receipt, - ) - return {"proposal": stored, "turn": turn_result} + return self._apply_todo_create(proposal_id, proposal, parameters) if action_kind == "run.correct": if self.runtime_controller is None or self.chat_store is None: raise ValueError("Chat Runtime Controller is unavailable") diff --git a/loopx/chat_server.py b/loopx/chat_server.py index 947afd3e83..ada926bce6 100644 --- a/loopx/chat_server.py +++ b/loopx/chat_server.py @@ -20,12 +20,7 @@ from .capabilities.native_chat.transports import ChatConversationTransports from .attached_session_api import AttachedSessionRequestMixin from .zcode_goal_mode.api import ZCodeGoalRequestMixin -from .chat import ( - TodoReviewPreviewConflict, - apply_todo_review_preview, - build_todo_review_preview, - redact_local_paths, -) +from .chat import TodoReviewPreviewConflict, redact_local_paths from .chat_agent import CodexChatAgentError from .capabilities.native_chat.project_context import ChatProjectContexts from .chat_attachments import ( @@ -45,6 +40,7 @@ from .capabilities.native_chat.model_settings_api import ChatModelSettingsRequestMixin from .capabilities.native_chat.queue_api import ChatQueueRequestMixin from .chat.status_api import ChatStatusRequestMixin +from .chat.todo_source import review_todo_create from .chat_runtime import ( STEERING_NOT_DELIVERED_CODES, ChatRuntimeController, @@ -1179,19 +1175,10 @@ def _todo(self, *, apply: bool) -> None: if not goal_id: raise ValueError("goal_id is required") self._registry_and_goal(goal_id) - if apply: - payload = apply_todo_review_preview( - registry_path=self.server.registry_path, - goal_id=goal_id, - text=text, - preview_id=str(body.get("preview_id") or ""), - ) - else: - payload = build_todo_review_preview( - registry_path=self.server.registry_path, - goal_id=goal_id, - text=text, - ) + payload = review_todo_create( + self.server, goal_id=goal_id, text=text, + preview_id=str(body.get("preview_id") or "") if apply else None, + ) except TodoReviewPreviewConflict as exc: self._send_error(str(exc), status=409, todo_receipt=exc.receipt) return diff --git a/loopx/control_plane/coordination/authority_source.ts b/loopx/control_plane/coordination/authority_source.ts index 2779466d32..859cca97f7 100644 --- a/loopx/control_plane/coordination/authority_source.ts +++ b/loopx/control_plane/coordination/authority_source.ts @@ -15,6 +15,19 @@ export const AUTHORITY_SOURCE_CHANGED = { reason: "Todo authority registration changed; review the current state before continuing", } as const; +function decodeRegistryWitness(value: unknown, field: string): {path: string; sha256: string} { + const witness = requireJsonObject(value, field); + if (typeof witness.path !== "string" || !isAbsolute(witness.path) || + typeof witness.sha256 !== "string" || !BARE_SHA256_PATTERN.test(witness.sha256)) { + throw new TypeError(`${field} requires an absolute path and SHA-256 digest`); + } + return {path: witness.path, sha256: witness.sha256}; +} + +async function registryMatches(witness: {path: string; sha256: string}): Promise { + return createHash("sha256").update(await readFile(witness.path)).digest("hex") === witness.sha256; +} + export function registryAuthoritySourceCheck( input: JsonObject, witnessed: boolean, expectedDigest: unknown = null, ): AuthoritySourceCheck { @@ -25,16 +38,15 @@ export function registryAuthoritySourceCheck( return uncheckedAuthoritySource; } const source = requireJsonObject(input.registry_source, "registry_source"); - if (typeof source.path !== "string" || !isAbsolute(source.path) || - typeof source.sha256 !== "string" || !BARE_SHA256_PATTERN.test(source.sha256)) { - throw new TypeError("registry_source requires an absolute path and SHA-256 digest"); - } // Copy primitive values: later mutation of the decoded request cannot change // the witness while an awaited store operation or validation effect runs. - const path = source.path, digest = source.sha256; + const witness = decodeRegistryWitness(source, "registry_source"); + const routing = Object.hasOwn(source, "routing_registry") + ? decodeRegistryWitness(source.routing_registry, "registry_source.routing_registry") + : undefined; return async () => { - if (expectedDigest != null && expectedDigest !== digest) return false; - try { return createHash("sha256").update(await readFile(path)).digest("hex") === digest; } + if (expectedDigest != null && expectedDigest !== witness.sha256) return false; + try { return await registryMatches(witness) && (routing === undefined || await registryMatches(routing)); } catch (error) { if (error instanceof Error && "code" in error && error.code === "ENOENT") return false; throw error; diff --git a/loopx/control_plane/coordination/authority_source_capture.py b/loopx/control_plane/coordination/authority_source_capture.py index 673917b47a..c401a2750c 100644 --- a/loopx/control_plane/coordination/authority_source_capture.py +++ b/loopx/control_plane/coordination/authority_source_capture.py @@ -20,10 +20,15 @@ @contextmanager -def authority_registry_source(registry_path: Path) -> Iterator[dict[str, str]]: +def authority_registry_source( + registry_path: Path, *, routing_registry_witness: dict[str, str] | None = None, +) -> Iterator[dict[str, object]]: path = registry_path.resolve() digest = hashlib.sha256(path.read_bytes()).hexdigest() - yield {"path": str(path), "sha256": digest} + yield { + "path": str(path), "sha256": digest, + **({"routing_registry": routing_registry_witness} if routing_registry_witness is not None else {}), + } if hashlib.sha256(path.read_bytes()).hexdigest() != digest: raise ValueError("Todo authority registration changed while reading; retry") diff --git a/loopx/control_plane/coordination/local_authority.py b/loopx/control_plane/coordination/local_authority.py index 582771331c..5717d8331b 100644 --- a/loopx/control_plane/coordination/local_authority.py +++ b/loopx/control_plane/coordination/local_authority.py @@ -192,11 +192,17 @@ def claim_canonical_todo_if_promoted( task_lease_idempotency_key: str | None = None, task_lease_expected_version: int | None = None, manage_execution: bool = False, + expected_provider_revision: str | None = None, + expected_registry_sha256: str | None = None, + routing_registry_witness: dict[str, str] | None = None, project: Path | None = None, state_file: Path | None = None, ) -> dict[str, Any]: """Route a claim to the canonical TypeScript transaction owner.""" - with authority_registry_source(registry_path) as registry_source: + reviewed = expected_provider_revision is not None or expected_registry_sha256 is not None + if reviewed and not (manage_execution and expected_provider_revision and expected_registry_sha256): + raise ValueError("A reviewed managed claim requires both canonical revision and registry digest") + with authority_registry_source(registry_path, routing_registry_witness=routing_registry_witness) as registry_source: registered = registered_agent_ids_from_registry(registry_path, goal_id) # This is only a candidate location. The lease owner observes physical # isolation and the current canonical scopes before granting execution. @@ -204,8 +210,11 @@ def claim_canonical_todo_if_promoted( result = effect_runtime_result( LOCAL_COORDINATION_TODO_CLAIM_METHOD, { - "schema_version": ("loopx_local_coordination_todo_claim_request_v2" if manage_execution - else LOCAL_COORDINATION_TODO_CLAIM_WITNESSED_REQUEST_SCHEMA), + "schema_version": ( + "loopx_local_coordination_todo_claim_request_v3" if reviewed + else "loopx_local_coordination_todo_claim_request_v2" if manage_execution + else LOCAL_COORDINATION_TODO_CLAIM_WITNESSED_REQUEST_SCHEMA + ), "runtime_root": str(runtime_root.expanduser().resolve(strict=False)), "goal_id": goal_id, "todo_id": todo_id, @@ -220,6 +229,11 @@ def claim_canonical_todo_if_promoted( else f"todo-claim:{goal_id}:{todo_id}:{uuid4().hex}" ), **({"manage_execution": True} if manage_execution else {}), + **( + {"expected_provider_revision": expected_provider_revision, + "expected_registry_sha256": expected_registry_sha256} + if reviewed else {} + ), **({"write_worktree": str(worktree)} if worktree is not None else {}), "lease_request": ( { diff --git a/loopx/control_plane/coordination/local_authority_runtime.ts b/loopx/control_plane/coordination/local_authority_runtime.ts index defde2dcb7..9889a97e27 100644 --- a/loopx/control_plane/coordination/local_authority_runtime.ts +++ b/loopx/control_plane/coordination/local_authority_runtime.ts @@ -102,12 +102,14 @@ export const LOCAL_COORDINATION_TODO_CLAIM_REQUEST_SCHEMA = "loopx_local_coordination_todo_claim_request_v0"; export const LOCAL_COORDINATION_TODO_CLAIM_WITNESSED_REQUEST_SCHEMA = "loopx_local_coordination_todo_claim_request_v1"; export const LOCAL_COORDINATION_TODO_MANAGED_CLAIM_REQUEST_SCHEMA = "loopx_local_coordination_todo_claim_request_v2"; +const REVIEWED_MANAGED_CLAIM_REQUEST_SCHEMA = "loopx_local_coordination_todo_claim_request_v3"; export const LOCAL_COORDINATION_TODO_EXECUTION_UPDATE_REQUEST_SCHEMA = "loopx_local_coordination_todo_update_request_v6"; export const LOCAL_COORDINATION_TODO_EXECUTION_TERMINAL_REQUEST_SCHEMA = "loopx_local_coordination_todo_terminal_lifecycle_request_v4"; export const LOCAL_COORDINATION_TODO_CREATE_REQUEST_SCHEMA = "loopx_local_coordination_todo_create_request_v0"; export const LOCAL_COORDINATION_TODO_CREATE_WITNESSED_REQUEST_SCHEMA = "loopx_local_coordination_todo_create_request_v1"; export const LOCAL_COORDINATION_TODO_CREATE_REVIEWED_REQUEST_SCHEMA = "loopx_local_coordination_todo_create_request_v2"; +const SOURCE_BOUND_TODO_CREATE_REQUEST_SCHEMA = "loopx_local_coordination_todo_create_request_v3"; export const LOCAL_COORDINATION_TODO_TERMINAL_LIFECYCLE_REQUEST_SCHEMA = "loopx_local_coordination_todo_terminal_lifecycle_request_v3"; export const LOCAL_COORDINATION_TODO_ARCHIVE_REQUEST_SCHEMA = @@ -1046,20 +1048,28 @@ export async function claimLocalCoordinationTodo( let sourceAuthority = "file_v0"; try { const input = requireJsonObject(value, "local coordination Todo claim request"); + const reviewed = input.schema_version === REVIEWED_MANAGED_CLAIM_REQUEST_SCHEMA; + const managed = reviewed || input.schema_version === LOCAL_COORDINATION_TODO_MANAGED_CLAIM_REQUEST_SCHEMA; if (input.schema_version !== LOCAL_COORDINATION_TODO_CLAIM_REQUEST_SCHEMA && input.schema_version !== LOCAL_COORDINATION_TODO_CLAIM_WITNESSED_REQUEST_SCHEMA && - input.schema_version !== LOCAL_COORDINATION_TODO_MANAGED_CLAIM_REQUEST_SCHEMA) { + !managed) { throw new Error("local coordination Todo claim request schema mismatch"); } - if (Object.hasOwn(input, "manage_execution") && input.schema_version !== LOCAL_COORDINATION_TODO_MANAGED_CLAIM_REQUEST_SCHEMA) + if (Object.hasOwn(input, "manage_execution") && !managed) throw new TypeError("Managed execution requires claim request v2"); + if (reviewed ? input.manage_execution !== true || typeof input.expected_registry_sha256 !== "string" + : Object.hasOwn(input, "expected_registry_sha256")) + throw new TypeError("A Todo claim review basis requires a reviewed managed claim"); + // Earlier claim wires did not bind caller-supplied provider revisions. + const expectedRevision = reviewed + ? requireAuthorityStoreId(input.expected_provider_revision, "expected provider revision") : undefined; const worktree = input.write_worktree; - if (worktree !== undefined && (input.schema_version !== LOCAL_COORDINATION_TODO_MANAGED_CLAIM_REQUEST_SCHEMA || + if (worktree !== undefined && (!managed || input.manage_execution !== true || typeof worktree !== "string" || !isAbsolute(worktree))) { throw new TypeError("Worktree observation requires a managed claim and an absolute caller path"); } const authoritySourcesCurrent = registryAuthoritySourceCheck(input, - input.schema_version !== LOCAL_COORDINATION_TODO_CLAIM_REQUEST_SCHEMA); + input.schema_version !== LOCAL_COORDINATION_TODO_CLAIM_REQUEST_SCHEMA, input.expected_registry_sha256); if (typeof input.dry_run !== "boolean") { throw new Error("dry_run must be a JSON boolean"); } @@ -1106,6 +1116,7 @@ export async function claimLocalCoordinationTodo( operation_id: requireAuthorityStoreId(input.operation_id, "operation id"), lease_request: leaseRequest, ...(input.manage_execution === undefined ? {} : {manage_execution: input.manage_execution as boolean}), + ...(expectedRevision === undefined ? {} : {expected_provider_revision: expectedRevision}), dry_run: input.dry_run === true, now: claimObservedAt(input.observed_at), }, authoritySourcesCurrent, claimWorkspaceObserver(worktree)); @@ -1144,17 +1155,21 @@ export async function createLocalCoordinationTodo( }; try { const input = requireJsonObject(value, "local coordination Todo create request"); + const sourceBound = input.schema_version === SOURCE_BOUND_TODO_CREATE_REQUEST_SCHEMA; + const reviewed = sourceBound || input.schema_version === LOCAL_COORDINATION_TODO_CREATE_REVIEWED_REQUEST_SCHEMA; if (input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_REQUEST_SCHEMA && input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_WITNESSED_REQUEST_SCHEMA && - input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_REVIEWED_REQUEST_SCHEMA) { + !reviewed) { throw new TypeError("local coordination Todo create request schema mismatch"); } - const reviewed = input.schema_version === LOCAL_COORDINATION_TODO_CREATE_REVIEWED_REQUEST_SCHEMA; if (!reviewed && Object.hasOwn(input, "expected_provider_revision")) { throw new TypeError("expected_provider_revision requires Todo create request v2"); } + if (sourceBound ? typeof input.expected_registry_sha256 !== "string" : Object.hasOwn(input, "expected_registry_sha256")) { + throw new TypeError("expected_registry_sha256 requires a source-bound Todo create request"); + } const authoritySourcesCurrent = registryAuthoritySourceCheck(input, - input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_REQUEST_SCHEMA); + input.schema_version !== LOCAL_COORDINATION_TODO_CREATE_REQUEST_SCHEMA, input.expected_registry_sha256); const expectedRevision = reviewed ? requireAuthorityStoreId(input.expected_provider_revision, "expected provider revision") : undefined; if (typeof input.dry_run !== "boolean") { diff --git a/loopx/control_plane/coordination/todo_claim.ts b/loopx/control_plane/coordination/todo_claim.ts index 6f5dc5e383..12e5a8b0ef 100644 --- a/loopx/control_plane/coordination/todo_claim.ts +++ b/loopx/control_plane/coordination/todo_claim.ts @@ -477,8 +477,16 @@ async function executeClaimAttempt( }; const recoverProof = async (result: CoordinationTodoClaimResult): Promise => { if (input.manage_execution && result.reason_code === "coordination_operation_identity_mismatch") { - const original = await store.readReceipt(input.operation_id); const legacyIntent = {...requestIdentity}; + if (input.expected_provider_revision !== undefined) { + // An earlier managed claim had no reviewed revision. Decode its exact + // historical intent without submitting a new claim without a revision. + delete legacyIntent.expected_provider_revision; + const historical = new CoordinationCommandReceipt({...receipt.contract, + identity: {...receipt.contract.identity, request_sha256: canonicalAuthoritySha256(legacyIntent)}}); + return currentProof(await historical.read(store) ?? result); + } + const original = await store.readReceipt(input.operation_id); delete legacyIntent.manage_execution; if (original.status === "found" && original.receipts.length === 1 && original.receipts[0]?.schema_version === COORDINATION_TODO_CLAIM_RECEIPT_SCHEMA && diff --git a/loopx/control_plane/coordination/todo_create.ts b/loopx/control_plane/coordination/todo_create.ts index 028b3e3060..92ef68755b 100644 --- a/loopx/control_plane/coordination/todo_create.ts +++ b/loopx/control_plane/coordination/todo_create.ts @@ -290,17 +290,17 @@ export async function executeCoordinationTodoCreate( } const todoId = requireAuthorityStoreId(input.todo.todo_id, "todo id"); const readModel = canonicalAuthorityObject(head.head.todo_read_model, "Todo read model"); - // A derived due time belongs to the first result, never to the retry identity. - const {todo} = input; - const needsInitialDue = todo.role === "agent" && todo.task_class === "continuous_monitor" && - todo.next_due_at == null && todo.cadence != null; - const initialDue = needsInitialDue - ? planMonitorSchedule(input.now.toISOString(), todo.cadence).next_due_at : null; - const plannedInput = initialDue === null ? input : {...input, todo: {...todo, next_due_at: initialDue}}; - const plan = planCoordinationTodoCreate(plannedInput, projection.todos, readModel.schema_version); + const plan = planCoordinationTodoCreate(input, projection.todos, readModel.schema_version); if (!await authoritySourcesCurrent()) return failure(AUTHORITY_SOURCE_CHANGED.code, AUTHORITY_SOURCE_CHANGED.reason); if (plan.status !== "planned") return {...plan, provider_revision: head.provider_revision, cursor: head.cursor}; - const created = canonicalAuthorityObject(plan.todo, "created Todo"); + // An implicit due time belongs to a new record. Duplicate matching compares + // the submitted configuration and respects any explicitly requested due time. + const planned = canonicalAuthorityObject(plan.todo, "created Todo"); + const needsInitialDue = planned.role === "agent" && planned.task_class === "continuous_monitor" && + planned.next_due_at == null && planned.cadence != null; + const initialDue = needsInitialDue + ? planMonitorSchedule(input.now.toISOString(), planned.cadence).next_due_at : null; + const created = initialDue === null ? planned : {...planned, next_due_at: initialDue}; if (input.dry_run) { return { schema_version: COORDINATION_TODO_CREATE_RESULT_SCHEMA, diff --git a/loopx/control_plane/presentation/action_review_plan.ts b/loopx/control_plane/presentation/action_review_plan.ts index 9bd7518488..c444e0a09c 100644 --- a/loopx/control_plane/presentation/action_review_plan.ts +++ b/loopx/control_plane/presentation/action_review_plan.ts @@ -17,7 +17,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" | "task_start_retry"; + | "canonical_update_retry" | "canonical_update_projection_pending" | "goal_creation_retry" | "task_start_retry" | "todo_creation_retry"; export type OperationReviewContent = { title: string; @@ -94,7 +94,8 @@ export type ActionReviewPlan = ActionReviewIdentity & ActionReviewState & { decisionFrame?: DecisionReviewFrame; /** Recover this exact canonical command; generating a new preview loses its receipt identity. */ retryOriginal?: true; - + /** Retained Todo creation effects keep their original confirmation. */ + prohibitRegeneration?: true; creationProgress?: GoalCreationProgress; creationOutcome?: GoalCreationOutcome; /** New Session routing only. Recorded Sessions recover through creationProgress; @@ -558,6 +559,22 @@ export function compileActionReviewPlan(proposalValue: unknown, nowMs?: number): || creationProgress.completionBasis) ? goalCreationOutcome(identity.proposalId, parameters ?? {}, creationProgress.todoIds) : undefined; const creationSteps = objectValue(objectValue(proposal.checkpoint)?.steps); + const retainedTodoCreate = proposal.action_kind === "todo.create" && Object.hasOwn(creationSteps ?? {}, "todo_created"); + const createdTodo = objectValue(creationSteps?.todo_created); + const createReceipt = objectValue(createdTodo?.canonical_receipt); + const originalTodoMatches = retainedTodoCreate && textValue(createdTodo?.todo_id) + && createdTodo?.todo_id === createReceipt?.todo_id && textValue(parameters?.goal_id) + && createReceipt?.goal_id === parameters?.goal_id + && createReceipt?.preview_id === identity.sourceFingerprint && createReceipt.status === "applied"; + const creationContext = objectValue(proposal.context); + const sameOwnerContext = !textValue(creationContext?.binding_id) + && (creationContext?.goal_id == null || creationContext.goal_id === parameters?.goal_id); + const legacyCreateMatches = originalTodoMatches && createdTodo !== null && !Object.hasOwn(createdTodo, "source") + && createReceipt?.schema_version === "loopx_chat_todo_receipt_v0" + && textValue(createReceipt.receipt_id) && textValue(createReceipt.preview_revision); + const legacySourceGate = proposal.status === "gated" && legacyCreateMatches && sameOwnerContext + && proposal.permission_classification === "durable_write" + && objectValue(proposal.gate)?.kind === "canonical_authority_required"; // A persisted Session/Turn, including a malformed checkpoint, always owns // recovery. Never infer a new managed identity from its old creation fields. const creationSession = proposal.action_kind === "goal.create" && parameters?.agent_id === "codex" @@ -576,6 +593,7 @@ export function compileActionReviewPlan(proposalValue: unknown, nowMs?: number): ...(reviewCardFrame ? { reviewCardFrame } : {}), ...(decisionFrame ? { decisionFrame } : {}), ...(creationProgress ? { creationProgress } : {}), + ...(retainedTodoCreate ? { prohibitRegeneration: true as const } : {}), ...(creationOutcome ? { creationOutcome } : {}), ...(creationSession ? { creationSession } : {}), ...(creationExecution ? { creationExecution } : {}), @@ -588,7 +606,9 @@ export function compileActionReviewPlan(proposalValue: unknown, nowMs?: number): if (operationFrame?.kind === "inactive") return held("inactive", operationFrame.reason); // Lifecycle uses conservative fact precedence. Generic deferred proposals may // retain a historical gate; their existing status-based retry path is preserved. - if ((lifecycle && proposal.gate != null) || proposal.status === "gated") return held("gated", "authority_gate"); + if ((lifecycle && proposal.gate != null) || (proposal.status === "gated" && !legacySourceGate)) { + return held("gated", "authority_gate"); + } if ((lifecycle && proposal.stale != null) || proposal.status === "stale") return held("refresh", "stale_proposal"); // Host readback may return an immutable terminal replay which never started. // Its acceptance id is still original, but cannot certify first-Turn startup. @@ -621,17 +641,13 @@ 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 + const taskStart = parameters?.start_execution === true; + if (originalTodoMatches && (taskStart || sameOwnerContext) && 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}; - } + && (proposal.status === "applying" || legacySourceGate + || (proposal.status === "failed" && objectValue(proposal.failure)?.retry_safe === true))) { + const reason = taskStart ? "task_start_retry" : "todo_creation_retry"; + return {...finish({interaction: "review", canApply: true, reason}), retryOriginal: true}; } const isCanonicalUpdate = basis?.schema_version === "loopx_chat_canonical_update_basis_v0" && textValue(basis.provider_revision) !== null && textValue(basis.registry_sha256) !== null diff --git a/loopx/control_plane/runtime/runtime_projection_route.py b/loopx/control_plane/runtime/runtime_projection_route.py index e0fe8eefe9..85876c2897 100644 --- a/loopx/control_plane/runtime/runtime_projection_route.py +++ b/loopx/control_plane/runtime/runtime_projection_route.py @@ -104,19 +104,21 @@ def resolve_goal_source_runtime_route( ``source_registry``, goal-scoped capabilities must use that registry's runtime rather than treating the shared projection as the source of truth. An explicit runtime override remains authoritative for diagnostics and - recovery commands. + recovery commands. It also supplies the invoked runtime for an unbound + registry. """ invoked_registry = registry_path.expanduser().resolve() registry_payload = registry if registry is not None else load_registry( invoked_registry, source_snapshots=source_snapshots, ) - invoked_runtime = _registry_runtime_root( - registry_payload, - registry_path=invoked_registry, - ) if runtime_root_override: runtime_root = Path(runtime_root_override).expanduser().resolve() + invoked_runtime = ( + _registry_runtime_root(registry_payload, registry_path=invoked_registry) + if registry_payload.get("common_runtime_root") + else runtime_root + ) return _goal_source_route_packet( status="explicit_override", declaration_source="cli.runtime_root_override", @@ -127,6 +129,10 @@ def resolve_goal_source_runtime_route( source_runtime=runtime_root, ) + invoked_runtime = _registry_runtime_root( + registry_payload, + registry_path=invoked_registry, + ) registry_is_global = bool(registry_payload.get("registry_role") == "global-local") or _same_path( invoked_registry, global_registry_path(invoked_runtime), diff --git a/loopx/control_plane/todos/mutation_api.py b/loopx/control_plane/todos/mutation_api.py index dde21871f9..e5f4da8db0 100644 --- a/loopx/control_plane/todos/mutation_api.py +++ b/loopx/control_plane/todos/mutation_api.py @@ -76,6 +76,8 @@ def add_goal_todo( dry_run: bool = False, operation_id: str | None = None, expected_provider_revision: str | None = None, + expected_registry_sha256: str | None = None, + routing_registry_witness: dict[str, str] | None = None, ) -> dict[str, Any]: shadow_runtime_root = effective_runtime_root(registry_path, runtime_root_arg) if role not in TODO_SECTION_HEADINGS: @@ -149,6 +151,8 @@ def add_goal_todo( return create_canonical_todo_if_promoted( operation_id=operation_id, expected_provider_revision=expected_provider_revision, + expected_registry_sha256=expected_registry_sha256, + routing_registry_witness=routing_registry_witness, registry_path=registry_path, runtime_root=shadow_runtime_root, goal_id=goal_id, @@ -301,9 +305,12 @@ def update_goal_todo( claim_only: bool = False, claim_operation_id: str | None = None, manage_execution: bool = False, + claim_expected_provider_revision: str | None = None, + claim_expected_registry_sha256: str | None = None, update_operation_id: str | None = None, update_expected_provider_revision: str | None = None, update_expected_registry_sha256: str | None = None, + routing_registry_witness: dict[str, str] | None = None, task_lease_idempotency_key: str | None = None, task_lease_expected_version: int | None = None, project: Path | None = None, @@ -323,6 +330,9 @@ def update_goal_todo( update_operation_id, update_expected_provider_revision, update_expected_registry_sha256, )): raise ValueError("Todo update identity and review basis cannot be used for todo claim") + if not (claim_only and manage_execution) and ( + claim_expected_provider_revision is not None or claim_expected_registry_sha256 is not None): + raise ValueError("A Todo claim review basis requires a managed claim") if excluded_agents and clear_excluded_agents: raise ValueError( "todo update accepts either excluded_agents or clear_excluded_agents, not both" @@ -375,6 +385,9 @@ def update_goal_todo( dry_run=dry_run, operation_id=claim_operation_id, manage_execution=manage_execution, + expected_provider_revision=claim_expected_provider_revision, + expected_registry_sha256=claim_expected_registry_sha256, + routing_registry_witness=routing_registry_witness, task_lease_idempotency_key=task_lease_idempotency_key, task_lease_expected_version=task_lease_expected_version, project=project, @@ -423,6 +436,7 @@ def update_goal_todo( authority_reason=authority_reason, expected_provider_revision=update_expected_provider_revision, expected_registry_sha256=update_expected_registry_sha256, + routing_registry_witness=routing_registry_witness, task_lease_idempotency_key=task_lease_idempotency_key, task_lease_expected_version=task_lease_expected_version, monitor_observation=monitor_metadata if isinstance(monitor_metadata, todo_monitor_metadata.MonitorPollObservation) else None, @@ -446,6 +460,7 @@ def complete_goal_todo( completion_turn_key: str | None = None, completion_identity_source: str | None = None, terminal_review_basis: Mapping[str, Any] | None = None, + routing_registry_witness: dict[str, str] | None = None, expected_todo_text_sha256: str | None = None, require_captured_todo_text: bool = False, completion_delivery_workspace: Mapping[str, Any] | None = None, diff --git a/loopx/control_plane/todos/provider_create.py b/loopx/control_plane/todos/provider_create.py index 8386b1b018..803a029e10 100644 --- a/loopx/control_plane/todos/provider_create.py +++ b/loopx/control_plane/todos/provider_create.py @@ -47,11 +47,15 @@ def create_canonical_todo_if_promoted( project: Path | None = None, state_file: Path | None = None, operation_id: str | None = None, expected_provider_revision: str | None = None, + expected_registry_sha256: str | None = None, + routing_registry_witness: dict[str, str] | None = None, ) -> dict[str, Any]: # The native writer guard selects the route under the same maintenance # lock as provider open and commit. A separate source read grants nothing. if operation_id is not None and not re.fullmatch(r"[A-Za-z0-9_.:-]+", operation_id): raise ValueError("operation_id must be a non-empty public-safe token") + if expected_registry_sha256 is not None and expected_provider_revision is None: + raise ValueError("A reviewed Todo create requires a canonical provider revision and registry digest") operation_id = operation_id if operation_id is not None else f"todo-create:{uuid4().hex}" # Identity is independent of current Todo count: exact retries still address # the original operation after other creates, edits, completion or archive. @@ -90,7 +94,7 @@ def create_canonical_todo_if_promoted( **{key: metadata[key] for key in ("priority", "title") if key in metadata and metadata.get("priority") is not None}, **({"claimed_by": claimed_by} if claimed_by else {}), } - with authority_registry_source(registry_path) as registry_source: + with authority_registry_source(registry_path, routing_registry_witness=routing_registry_witness) as registry_source: registered = registered_agent_ids_from_registry(registry_path, goal_id) if validation_declaration is not None and not dry_run: prepare_completion_validation_declaration(runtime_root=runtime_root, @@ -99,10 +103,15 @@ def create_canonical_todo_if_promoted( result = effect_runtime_result( "coordination.local_authority.todo_create", { - "schema_version": ("loopx_local_coordination_todo_create_request_v2" - if expected_provider_revision is not None else "loopx_local_coordination_todo_create_request_v1"), + "schema_version": ( + "loopx_local_coordination_todo_create_request_v3" if expected_registry_sha256 is not None + else "loopx_local_coordination_todo_create_request_v2" if expected_provider_revision is not None + else "loopx_local_coordination_todo_create_request_v1" + ), **({"expected_provider_revision": expected_provider_revision} if expected_provider_revision is not None else {}), + **({"expected_registry_sha256": expected_registry_sha256} + if expected_registry_sha256 is not None else {}), "runtime_root": str(runtime_root.resolve()), "goal_id": goal_id, "todo": todo, diff --git a/loopx/control_plane/todos/provider_terminal_lifecycle.py b/loopx/control_plane/todos/provider_terminal_lifecycle.py index ee9ba20287..076bf3150c 100644 --- a/loopx/control_plane/todos/provider_terminal_lifecycle.py +++ b/loopx/control_plane/todos/provider_terminal_lifecycle.py @@ -130,6 +130,7 @@ def route_terminal_call(command: str, call: Mapping[str, Any]) -> dict[str, Any] call.get("completion_identity_source") if complete else None ), review_basis=call.get("terminal_review_basis"), + routing_registry_witness=call.get("routing_registry_witness"), expected_todo_text_sha256=call.get("expected_todo_text_sha256"), require_captured_todo_text=call.get("require_captured_todo_text") is True, completion_delivery_workspace=( @@ -354,6 +355,7 @@ def terminal_canonical_todo_if_promoted( self_merged: bool, dry_run: bool, review_basis: Mapping[str, Any] | None = None, + routing_registry_witness: dict[str, str] | None = None, expected_todo_text_sha256: str | None = None, require_captured_todo_text: bool = False, project: Path | None = None, @@ -402,7 +404,7 @@ def terminal_canonical_todo_if_promoted( )["result"] result_descriptor = {key: bound[key] for key in ("provider", "sha256", "size_bytes", "content_type")} - with authority_registry_source(registry_path) as registry_source: + with authority_registry_source(registry_path, routing_registry_witness=routing_registry_witness) as registry_source: registered, grants = todo_lifecycle_facts(registry_path, goal_id) successor_intents = build_successor_intents( next_agent_todo=next_agent_todo, diff --git a/loopx/control_plane/todos/provider_update.py b/loopx/control_plane/todos/provider_update.py index f78775a3b8..7a849feae5 100644 --- a/loopx/control_plane/todos/provider_update.py +++ b/loopx/control_plane/todos/provider_update.py @@ -266,12 +266,13 @@ def update_canonical_todo_if_promoted( authority_reason: str | None = None, expected_provider_revision: str | None = None, expected_registry_sha256: str | None = None, + routing_registry_witness: dict[str, str] | None = None, monitor_observation: MonitorPollObservation | None = None, completion_validation_revision: dict[str, Any] | None = None, ) -> dict[str, Any]: # The witness brackets fact projection and is rechecked by the native # transaction. Reviewed edits additionally bind the preview's registry hash. - with authority_registry_source(registry_path) as registry_source: + with authority_registry_source(registry_path, routing_registry_witness=routing_registry_witness) as registry_source: registered, grants = todo_lifecycle_facts(registry_path, goal_id) patch: dict[str, Any] = {} if text is not None: diff --git a/tests/control_plane/test_chat_reviewed_create.py b/tests/control_plane/test_chat_reviewed_create.py index c29a70d3fa..45ad437528 100644 --- a/tests/control_plane/test_chat_reviewed_create.py +++ b/tests/control_plane/test_chat_reviewed_create.py @@ -1,10 +1,184 @@ """Reviewed App creates must retain their revision through the actual write.""" +import json +from datetime import datetime, timedelta, timezone +from http.client import HTTPConnection +from threading import Thread + import pytest import loopx.chat as chat -from canonical_authority_fixture import isolate_sqlite_runtime, promoted_create_fixture +from canonical_authority_fixture import ( + initialize_canonical_authority, isolate_sqlite_runtime, promoted_create_fixture, +) +from loopx.chat_action_store import ChatActionStore +from loopx.chat_actions import ChatActionService +from loopx.chat_server import ChatHTTPServer, ChatRequestHandler from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted -from loopx.control_plane.todos.mutation_api import add_goal_todo +from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection +from loopx.control_plane.todos.mutation_api import add_goal_todo, update_goal_todo + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("route", ["serving", "registered", "shared-directory", "shared-unbound"]) +@pytest.mark.parametrize("kind", ["todo.create", "monitor.create", "todo-review"]) +def test_http_create_uses_goal_runtime(tmp_path, monkeypatch, provider, route, kind): + isolate_sqlite_runtime(tmp_path, monkeypatch) + source_registry, runtime, state = promoted_create_fixture(tmp_path, provider=provider) + registry = source_registry + monkeypatch.setattr("loopx.paths.DEFAULT_RUNTIME_ROOT", tmp_path / "ambient-current") + monkeypatch.setattr("loopx.paths.LEGACY_RUNTIME_ROOT", tmp_path / "ambient-legacy") + serving_root = runtime + decoy = None + if route == "serving": + registration = json.loads(registry.read_text()) + del registration["common_runtime_root"] + registry.write_text(json.dumps(registration)) + else: + (tmp_path / "other").mkdir() + _, decoy_root, _ = promoted_create_fixture(tmp_path / "other", provider=provider) + decoy = read_canonical_todos_if_promoted(runtime_root=decoy_root, goal_id="goal-a") + serving_root = decoy_root if route == "registered" else tmp_path / "chat-storage" + if route.startswith("shared"): + registration = json.loads(registry.read_text()) + registration.update(registry_role="global-local", common_runtime_root=str(decoy_root)) + if route == "shared-unbound": + del registration["common_runtime_root"] + registration["goals"][0]["source_registry"] = str(source_registry) + registry = tmp_path / "directory.json" + registry.write_text(json.dumps(registration)) + before = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") + store = ChatActionStore(serving_root / "chat" / "actions") + server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler) + server.registry_path, server.runtime_root_override, server.verbose = registry, str(serving_root), False + server.action_store = store + server.action_service = ChatActionService(store=store, registry_path=registry, + runtime_root_override=str(serving_root)) + worker = Thread(target=server.serve_forever, daemon=True) + worker.start() + connection = HTTPConnection(*server.server_address, timeout=30) + + def request(method, path, body=None): + connection.request(method, path, None if body is None else json.dumps(body), + {"Content-Type": "application/json"}) + response = connection.getresponse() + return response.status, json.loads(response.read()) + + text = "Review the project index" + after_interrupted_create = None + try: + if kind == "todo-review": + intent = {"goal_id": "goal-a", "text": text} + code, preview = request("POST", "/api/chat/todo/dry-run", intent) + assert code == 200, preview + apply_path, confirmation = "/api/chat/todo/apply", {**intent, "preview_id": preview["preview_id"]} + else: + parameters = {"goal_id": "goal-a", "agent_id": "agent-a"} + if kind == "monitor.create": + parameters.update(target=text, target_key="index-check", cadence="2h", + timezone="UTC", stop_condition="watch_only") + else: + parameters.update(text=text, priority="P2") + code, preview = request("POST", "/api/actions/preview", { + "action_kind": kind, "summary": text, "normalized_parameters": parameters, + "context": {"kind": "goal", "goal_id": "goal-a"}, "idempotency_key": "review-index", + }) + assert code == 201, preview + action_path = f"/api/actions/{preview['proposal']['proposal_id']}" + if kind == "todo.create" and route == "shared-directory": + original = source_registry.read_text() + changed = json.loads(original) + changed["goals"][0]["coordination"]["registered_agents"] = ["another-agent"] + source_registry.write_text(json.dumps(changed)) + refused_code, refused = request("POST", f"{action_path}/apply", {}) + assert refused_code == 400 and "no longer registered" in refused["error"], refused + assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") == before + source_registry.write_text(original) + if kind == "monitor.create" and route == "shared-directory": + original = source_registry.read_text() + source_registry.unlink() + unavailable_code, unavailable = request("POST", f"{action_path}/apply", {}) + assert unavailable_code == 400 and unavailable["error_code"] == "invalid_action_apply", unavailable + assert str(tmp_path) not in json.dumps(unavailable) + assert read_canonical_todos_if_promoted(runtime_root=decoy_root, goal_id="goal-a") == decoy + source_registry.write_text(json.dumps({**json.loads(original), + "common_runtime_root": str(decoy_root)})) + refused_code, refused = request("POST", f"{action_path}/apply", {}) + assert refused_code == 409 and refused["proposal"]["status"] == "stale", refused + assert read_canonical_todos_if_promoted(runtime_root=decoy_root, goal_id="goal-a") == decoy + source_registry.write_text(original) + code, preview = request("POST", f"{action_path}/regenerate", {}) + assert code == 201, preview + action_path = f"/api/actions/{preview['proposal']['proposal_id']}" + apply_path, confirmation = f"{action_path}/apply", {} + + assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") == before + assert text not in state.read_text() + if kind == "monitor.create" and route == "registered": + original_registration = registry.read_text() + + def retarget_at_write(**kwargs): + changed = json.loads(original_registration) + changed["common_runtime_root"] = str(decoy_root) + registry.write_text(json.dumps(changed)) + return add_goal_todo(**kwargs) + + with monkeypatch.context() as fault: + fault.setattr("loopx.chat_actions.add_goal_todo", retarget_at_write) + code, refused = request("POST", apply_path, confirmation) + assert code == 409 and refused["proposal"]["status"] == "stale", refused + assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") == before + assert read_canonical_todos_if_promoted(runtime_root=decoy_root, goal_id="goal-a") == decoy + registry.write_text(original_registration) + code, preview = request("POST", f"{action_path}/regenerate", {}) + assert code == 201, preview + apply_path = f"/api/actions/{preview['proposal']['proposal_id']}/apply" + + def interrupt_after_create(*args, **kwargs): + raise OSError("Interrupted after the reviewed Monitor was created") + + monitor_time = datetime.now(timezone.utc).replace(microsecond=0) + monkeypatch.setattr("loopx.chat_actions.now_utc", lambda: monitor_time) + monkeypatch.setattr("loopx.control_plane.todos.provider_create.now_local", lambda: monitor_time.isoformat()) + with monkeypatch.context() as fault: + fault.setattr(store, "apply", interrupt_after_create) + code, interrupted = request("POST", apply_path, confirmation) + assert code == 424 and interrupted["error_code"] == "canonical_action_failed", interrupted + after_interrupted_create = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") + assert int(after_interrupted_create["cursor"]) == int(before["cursor"]) + 1 + created, = after_interrupted_create["todos"] + assert datetime.fromisoformat(created["next_due_at"]) == monitor_time + timedelta(hours=2) + monitor_time += timedelta(minutes=1) + code, applied = request("POST", apply_path, confirmation) + assert code == 200, applied + receipt = applied["receipt"] if kind == "todo-review" else applied["proposal"]["receipt"] + todo_id = receipt["todo_id"] if kind == "todo-review" else receipt["resource_ids"]["todo_id"] + saved = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") + if after_interrupted_create is not None: + assert saved == after_interrupted_create + assert len(saved["todos"]) == 1 + todo = saved["todos"][0] + assert todo["todo_id"] == todo_id and text in todo["text"] + assert text in state.read_text() + if kind != "todo-review": + assert todo["claimed_by"] == "agent-a" + if kind == "monitor.create": + assert (todo["task_class"], todo["cadence"], todo["target_key"]) == ( + "continuous_monitor", "2h", "index-check") + assert todo["next_due_at"] + else: + assert todo["priority"] == "P2" + assert request("POST", apply_path, confirmation) == (code, applied) + assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") == saved + detail_code, detail = request("GET", f"/api/chat/todo/detail?goal_id=goal-a&todo_id={todo_id}") + assert detail_code == 200, detail + assert detail["review_basis"]["provider_revision"] == saved["provider_revision"] + if decoy is not None: + assert read_canonical_todos_if_promoted(runtime_root=decoy_root, goal_id="goal-a") == decoy + finally: + connection.close() + server.shutdown() + server.server_close() + worker.join(timeout=5) @pytest.mark.parametrize("provider", ["file", "sqlite"]) @@ -82,3 +256,370 @@ def test_changed_intent_cannot_apply_another_preview(tmp_path, monkeypatch, prov chat.apply_todo_review_preview(registry_path=registry, goal_id="goal-a", preview_id=preview["preview_id"], **changed) assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") == before + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("change", [ + "same", "directory", "runtime", "registration", "store", "unrelated_todo", + "edited_todo", "lost_assignment_checkpoint", "directory_during_assignment", + "legacy_same", "legacy_runtime", "legacy_registration", "legacy_store", + "legacy_unrelated_todo", "legacy_edited_todo", "legacy_saved_assignment", + "legacy_wrong_assignment", "legacy_assigned_store", + "legacy_lost_assignment_checkpoint", "legacy_independent_reassignment", "legacy_creation_only_note", +]) +def test_interrupted_assignment_keeps_the_created_todo_source(tmp_path, monkeypatch, provider, change): + legacy = change.startswith("legacy_") + change = change.removeprefix("legacy_") + creation_only = change == "creation_only_note" + legacy_saved = change in {"saved_assignment", "wrong_assignment", "assigned_store"} + legacy_unverified_assignment = legacy and change in {"lost_assignment_checkpoint", "independent_reassignment"} + legacy_assigned = legacy_saved or legacy_unverified_assignment + if change == "assigned_store": + change = "store" + isolate_sqlite_runtime(tmp_path, monkeypatch) + goal_id = "goal-a" + original_registry, original_root, _ = promoted_create_fixture( + tmp_path / "original", provider=provider, registered_agents=["agent-a", "agent-b"], + ) + original_registration = original_registry.read_text() + replacement_root = tmp_path / "replacement" / "runtime" + replacement_state = replacement_root.parent / "state.md" + replacement_state.parent.mkdir() + replacement_state.write_text("# Other Goal\n\n## Agent Todo\n\n## Completed Work Archive\n") + replacement_registry = replacement_root.parent / "registry.json" + replacement_doc = json.loads(original_registration) + replacement_doc["common_runtime_root"] = str(replacement_root) + replacement_doc["goals"][0].update(repo=str(replacement_state.parent), state_file=replacement_state.name) + replacement_registry.write_text(json.dumps(replacement_doc)) + directory = tmp_path / "directory.json" + directory_doc = json.loads(replacement_registry.read_text()) + directory_doc["registry_role"] = "global-local" + directory_doc["goals"][0]["source_registry"] = str(original_registry) + original_directory = json.dumps(directory_doc) + directory.write_text(original_directory) + store = ChatActionStore(tmp_path / "chat" / "actions") + server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler) + server.registry_path, server.runtime_root_override, server.verbose = directory, str(tmp_path / "chat"), False + server.action_store = store + server.action_service = ChatActionService(store=store, registry_path=directory) + worker = Thread(target=server.serve_forever, daemon=True) + worker.start() + connection = HTTPConnection(*server.server_address, timeout=40) + checkpoint = store.save_checkpoint + + def write_checkpoint(proposal_id, *, step, receipt): + saved = checkpoint(proposal_id, step=step, receipt=receipt) + if step == "todo_created": + raise OSError("Interrupted after creating the Todo and before assigning it") + return saved + + def post(path, body): + connection.request("POST", path, json.dumps(body), {"Content-Type": "application/json"}) + response = connection.getresponse() + return response.status, json.loads(response.read()) + + def read(runtime): + return read_canonical_todos_if_promoted(runtime_root=runtime, goal_id=goal_id) + + try: + text = "Prepare the original delivery" + request = { + "action_kind": "todo.create", + "summary": text, + "normalized_parameters": {"goal_id": goal_id, "text": text}, + "context": {"kind": "goal", "goal_id": goal_id}, "idempotency_key": "reviewed-original", + } + if not creation_only: + request["normalized_parameters"]["agent_id"] = "agent-a" + if legacy: + # Use the original operation identity and checkpoint format. + preview = chat.build_todo_review_preview(registry_path=original_registry, goal_id=goal_id, text=text) + proposal = store.create_preview(**request, expected_state_fingerprint=preview["preview_id"], + permission_classification="durable_write", validation_evidence=[], available_transitions=["apply", "cancel"]) + proposal_id = proposal["proposal_id"] + store.start_apply(proposal_id) + receipt = chat.apply_todo_review_preview(registry_path=original_registry, goal_id=goal_id, + text=text, preview_id=preview["preview_id"])["receipt"] + checkpoint(proposal_id, step="todo_created", receipt={"todo_id": receipt["todo_id"], "canonical_receipt": receipt}) + if change == "lost_assignment_checkpoint": + add_goal_todo(registry_path=original_registry, goal_id=goal_id, + role="agent", text="Independent work in the same Goal") + if legacy_assigned: + # An independently authorized update can have the same assignee, + # actor and authority reason as the legacy confirmation. + operation_id = "independent-assignment" if change == "independent_reassignment" else None + update_goal_todo(registry_path=original_registry, goal_id=goal_id, + todo_id=receipt["todo_id"], claimed_by="agent-a", agent_id="agent-a", + authority_reason="owner-confirmed typed Chat action", update_operation_id=operation_id) + if change == "independent_reassignment": + update_goal_todo(registry_path=original_registry, goal_id=goal_id, todo_id=receipt["todo_id"], + claimed_by="agent-b", agent_id="agent-a", update_operation_id="independent-reassignment") + if legacy_saved: + checkpoint(proposal_id, step="todo_assigned", receipt={ + "todo_id": receipt["todo_id"], + "agent_id": "another-agent" if change == "wrong_assignment" else "agent-a", + }) + store.mark_failed(proposal_id, error_code="canonical_action_failed", message="Interrupted before confirmation") + else: + code, preview = post("/api/actions/preview", request) + assert code == 201, preview + proposal_id = preview["proposal"]["proposal_id"] + with monkeypatch.context() as fault: + fault.setattr(store, "save_checkpoint", write_checkpoint) + code, interrupted = post(f"/api/actions/{proposal_id}/apply", {}) + assert code == 424 and interrupted["error_code"] == "canonical_action_failed", interrupted + path = f"/api/actions/{proposal_id}/apply" + created = store.load(proposal_id)["checkpoint"]["steps"]["todo_created"] + todo_id = created["todo_id"] + original = read(original_root) + original_todo = next(row for row in original["todos"] if row["todo_id"] == todo_id) + expected_assignee = "agent-b" if change == "independent_reassignment" else ("agent-a" if legacy_assigned else None) + assert original_todo.get("claimed_by") == expected_assignee + if creation_only: + note = "The export now includes the backup manifest." + update_goal_todo(registry_path=original_registry, goal_id=goal_id, todo_id=todo_id, + agent_id="agent-a", note=note) + edited = read(original_root) + assert edited["todos"][0]["note"] == note + for _ in range(2): + code, applied = post(path, {}) + assert code == 200 and applied["proposal"]["status"] == "applied", applied + receipt = applied["proposal"]["receipt"] + assert receipt["canonical_receipt"] == created["canonical_receipt"] + assert receipt["resource_ids"] == {"goal_id": goal_id, "todo_id": todo_id} + assert "todo_assigned" not in applied["proposal"]["checkpoint"]["steps"] + assert read(original_root) == edited + return + if change == "saved_assignment": + code, refused = post(f"/api/actions/{proposal_id}/regenerate", {}) + assert code == 409 and refused["error_code"] == "action_conflict", refused + assert read(original_root) == original + + # Same public ids, independently initialized authority and different content. + replacement_record = {**original_todo, "text": "[P1] Unrelated replacement work", + "title": "Unrelated replacement work"} + initialize_canonical_authority(replacement_root, goal_id, + build_todo_runtime_shadow_projection(goal_id=goal_id, todos=[replacement_record], + handoff_mode="soft_claim"), + state_path=replacement_state, provider=provider) + replacement = read(replacement_root) + saved_root = original_root.with_name("saved-runtime") + if change == "directory": + directory_doc["goals"][0]["source_registry"] = str(replacement_registry) + directory.write_text(json.dumps(directory_doc)) + elif change == "runtime": + changed = json.loads(original_registration) + changed["common_runtime_root"] = str(replacement_root) + original_registry.write_text(json.dumps(changed)) + elif change == "registration": + revoked = json.loads(original_registration) + revoked["goals"][0]["coordination"]["registered_agents"] = ["another-agent"] + original_registry.write_text(json.dumps(revoked)) + elif change == "store": + original_root.rename(saved_root) + replacement_root.rename(original_root) + if change in {"directory", "runtime", "registration", "store"}: + code, denied = post(path, {}) + if legacy: + assert code == 409 and denied["error_code"] == "protected_action", denied + assert denied["proposal"]["checkpoint"]["steps"]["todo_created"] == created + assert todo_id in denied["gate"]["summary"] + else: + assert code == 400 and "receipt" not in denied, denied + if change == "store": + assert read(original_root) == replacement + original_root.rename(replacement_root) + saved_root.rename(original_root) + assert read(original_root) == original and read(replacement_root) == replacement + saved_steps = store.load(proposal_id)["checkpoint"]["steps"] + assert set(saved_steps) == ({"todo_created", "todo_assigned"} if legacy_saved else {"todo_created"}) + assert saved_steps["todo_created"] == created + directory.write_text(original_directory) + original_registry.write_text(original_registration) + if change == "directory_during_assignment": + from loopx.control_plane.todos import provider_update + + dispatch = provider_update._dispatch_canonical_todo_update + + def retarget_before_dispatch(request, *, registry_path): + directory_doc["goals"][0]["source_registry"] = str(replacement_registry) + directory.write_text(json.dumps(directory_doc)) + return dispatch(request, registry_path=registry_path) + + with monkeypatch.context() as fault: + fault.setattr(provider_update, "_dispatch_canonical_todo_update", retarget_before_dispatch) + code, denied = post(path, {}) + assert code == 400 and "receipt" not in denied, denied + assert directory.read_text() != original_directory + assert read(original_root) == original and read(replacement_root) == replacement + directory.write_text(original_directory) + if change == "edited_todo": + update_goal_todo(registry_path=original_registry, goal_id=goal_id, todo_id=todo_id, + agent_id="agent-a", text="A different assignment") + edited = read(original_root) + code, denied = post(path, {}) + if legacy: + assert code == 409 and denied["error_code"] == "protected_action", denied + assert denied["proposal"]["checkpoint"]["steps"]["todo_created"] == created + else: + assert code == 400 and "receipt" not in denied, denied + assert "original receipt" in str(denied) + assert read(original_root) == edited and read(replacement_root) == replacement + return + if not legacy_unverified_assignment and change in {"unrelated_todo", "lost_assignment_checkpoint"}: + added = add_goal_todo(registry_path=original_registry, goal_id=goal_id, + role="agent", text="Independent work in the same Goal") + original = read(original_root) + unrelated_todo = next(row for row in original["todos"] if row["todo_id"] == added["todo_id"]) + if not legacy and change == "lost_assignment_checkpoint": + def lose_assignment_checkpoint(proposal_id, *, step, receipt): + if step == "todo_assigned": + raise OSError("Interrupted after assigning the original Todo") + return checkpoint(proposal_id, step=step, receipt=receipt) + + with monkeypatch.context() as fault: + fault.setattr(store, "save_checkpoint", lose_assignment_checkpoint) + code, failed = post(path, {}) + assert code == 424 and failed["error_code"] == "canonical_action_failed", failed + assigned_without_checkpoint = read(original_root) + assert int(assigned_without_checkpoint["cursor"]) == int(original["cursor"]) + 1 + if legacy_unverified_assignment: + for _ in range(2): + code, result = post(path, {}) + assert code == 409 and result["error_code"] == "protected_action", result + assert f"created Todo {todo_id}" in result["gate"]["summary"], result + assert f"current assignee is {expected_assignee}" in result["gate"]["summary"], result + assert result["proposal"]["checkpoint"]["steps"] == {"todo_created": created} + assert not result["proposal"].get("receipt") + assert read(original_root) == original and read(replacement_root) == replacement + return + code, applied = post(path, {}) + if change == "wrong_assignment": + assert code == 409 and applied["error_code"] == "protected_action", applied + assert read(original_root) == original and read(replacement_root) == replacement + return + assert code == 200 and applied["proposal"]["status"] == "applied", applied + assert applied["proposal"]["receipt"]["canonical_receipt"] == created["canonical_receipt"] + final = read(original_root) + assigned = next(row for row in final["todos"] if row["todo_id"] == todo_id) + assert assigned["claimed_by"] == "agent-a" + assert len(final["todos"]) == len(original["todos"]) + if change in {"unrelated_todo", "lost_assignment_checkpoint"}: + assert next(row for row in final["todos"] if row["todo_id"] == unrelated_todo["todo_id"]) == unrelated_todo + if not legacy and change == "lost_assignment_checkpoint": + assert final == assigned_without_checkpoint + assert int(final["cursor"]) == int(original["cursor"]) + (0 if legacy_assigned else 1) + assert post(path, {}) == (code, applied) + assert read(original_root) == final and read(replacement_root) == replacement + finally: + connection.close() + server.shutdown() + server.server_close() + worker.join(timeout=5) + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("operation", ["edit", "complete", "monitor.stop", "gate.resolve"]) +def test_existing_chat_actions_and_detail_follow_the_same_goal_source(tmp_path, monkeypatch, provider, operation): + isolate_sqlite_runtime(tmp_path, monkeypatch) + goal_id = "goal-a" + source, root, _ = promoted_create_fixture(tmp_path / "source", provider=provider) + shared, shared_root, _ = promoted_create_fixture(tmp_path / "shared", provider=provider) + kind = {"monitor.stop": "monitor.update", "gate.resolve": "gate.resolve"}.get(operation, "todo.update") + + def add(registry, text): + if kind == "monitor.update": + intent = {"task_class": "continuous_monitor", "claimed_by": "agent-a", + "monitor_metadata": {"cadence": "2h", "target_key": text, "watch_only": "true"}} + elif kind == "gate.resolve": + intent = {"task_class": "user_gate", "blocks_agent": "agent-a"} + else: + intent = {"task_class": "advancement_task", "claimed_by": "agent-a"} + return add_goal_todo(registry_path=registry, goal_id=goal_id, text=text, + role="user" if kind == "gate.resolve" else "agent", **intent)["todo_id"] + + todo_id = add(source, "Review source task") + shared_id = add(shared, "Shared decoy task") + def read(runtime): + return read_canonical_todos_if_promoted(runtime_root=runtime, goal_id=goal_id) + + source_before, shared_before = read(root), read(shared_root) + directory = tmp_path / "directory.json" + doc = json.loads(shared.read_text()) + doc["registry_role"] = "global-local" + doc["goals"][0]["source_registry"] = str(source) + directory.write_text(json.dumps(doc)) + store = ChatActionStore(tmp_path / "chat" / "actions") + server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler) + server.registry_path, server.runtime_root_override, server.verbose = directory, str(tmp_path / "chat"), False + server.action_store = store + server.action_service = ChatActionService(store=store, registry_path=directory) + worker = Thread(target=server.serve_forever, daemon=True) + worker.start() + connection = HTTPConnection(*server.server_address, timeout=40) + + def request(method, path, body=None): + connection.request(method, path, None if body is None else json.dumps(body), + {"Content-Type": "application/json"}) + response = connection.getresponse() + return response.status, json.loads(response.read()) + + def detail(task_id): + return request("GET", f"/api/chat/todo/detail?goal_id={goal_id}&todo_id={task_id}") + + def preview(task_id, key, review_basis=None): + intent = {"goal_id": goal_id, "todo_id": task_id, "agent_id": "agent-a"} + if kind == "gate.resolve": + intent.update(decision="approve") + elif kind == "monitor.update": + intent.update(operation="stop") + else: + intent.update(operation=operation) + if operation == "edit": + intent.update(text="Reviewed authoritative requirements") + if review_basis: + intent["review_basis"] = review_basis + else: + intent.update(note="Completed the selected task") + return request("POST", "/api/actions/preview", { + "action_kind": kind, "summary": "Apply a source Task action", + "context": {"kind": "goal", "goal_id": goal_id}, + "normalized_parameters": intent, "idempotency_key": key, + }) + + try: + basis = None + if kind != "gate.resolve": + code, shown = detail(todo_id) + assert code == 200, shown + basis = shown["review_basis"] + assert shown["text"] == source_before["todos"][0]["text"] + assert detail(shared_id)[0] == 404 + code, refused = preview(shared_id, "shared-only") + assert code == 400 and refused.get("ok") is False, refused + + code, proposal = preview(todo_id, "authoritative", basis) + assert code == 201, proposal + preview_basis = proposal["proposal"]["canonical_update_basis"] + assert preview_basis["provider_revision"] == source_before["provider_revision"] + if basis is not None: + assert preview_basis["source_identity"] == basis["source_identity"] + assert read(root) == source_before and read(shared_root) == shared_before + path = f"/api/actions/{proposal['proposal']['proposal_id']}/apply" + code, response = request("POST", path, {}) + assert code == 200 and response["proposal"]["status"] == "applied", response + after = read(root) + assert int(after["cursor"]) == int(source_before["cursor"]) + 1 + (updated,) = after["todos"] + if operation == "edit": + assert updated["text"] == "[P1] Reviewed authoritative requirements" + assert detail(todo_id)[1]["text"] == updated["text"] + else: + assert updated["status"] == "done" + assert request("POST", path, {}) == (code, response) + assert read(root) == after and read(shared_root) == shared_before + finally: + connection.close() + server.shutdown() + server.server_close() + worker.join(timeout=5) diff --git a/tests/control_plane/test_chat_task_start_recovery.py b/tests/control_plane/test_chat_task_start_recovery.py index 56dd2af5cf..25197b7f76 100644 --- a/tests/control_plane/test_chat_task_start_recovery.py +++ b/tests/control_plane/test_chat_task_start_recovery.py @@ -13,9 +13,11 @@ from loopx.capabilities.machine_configuration.store import configure_machine_configuration from loopx.capabilities.native_chat.machine_defaults import CODEX_CHAT_DEFAULTS_SCHEMA_VERSION from loopx.capabilities.native_chat.project_context import ChatProjectContexts +from loopx.chat import apply_todo_review_preview, build_todo_review_preview 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 +from loopx.control_plane.todos.mutation_api import add_goal_todo, update_goal_todo from test_goal_create_completion import preview_goal from test_new_goal_storage_defaults import app, environment # noqa: F401 @@ -173,6 +175,45 @@ def pause(proposal_id, *, step, receipt): assert calls.count("thread/start") == calls.count("turn/start") == 1 +def test_task_start_recovers_after_unrelated_todo_write(task_start, monkeypatch): + actions, post, _, controller, _, runtime, capture, proposal_id = task_start + path = f"/api/actions/{proposal_id}/apply" + save = actions.save_checkpoint + + def interrupt(proposal_id, *, step, receipt): + stored = save(proposal_id, step=step, receipt=receipt) + if step == "todo_created": + raise OSError("Interrupted before the managed claim") + return stored + + with monkeypatch.context() as fault: + fault.setattr(actions, "save_checkpoint", interrupt) + code, result = post(path, {}) + assert code == 424, result + original = actions.load(proposal_id)["checkpoint"]["steps"]["todo_created"] + other = add_goal_todo(registry_path=controller.registry_path, runtime_root_arg=str(runtime), + goal_id="report", text="Follow up on the separate budget check", role="agent") + before = {row["todo_id"]: row for row in _snapshot(runtime)["todos"]} + assert before[other["todo_id"]].get("claimed_by") is None + code, applied = post(path, {}) + assert code == 202, applied + receipt = applied["proposal"]["receipt"] + assert receipt["canonical_receipt"] == original["canonical_receipt"] + ids = receipt["resource_ids"] + assert ids["todo_id"] == original["todo_id"] + assert controller.wait_for_turn(session_id=ids["session_id"], turn_id=ids["turn_id"], timeout_sec=10)["status"] == "completed" + final = _snapshot(runtime) + rows = {row["todo_id"]: row for row in final["todos"]} + assert rows[original["todo_id"]].get("claimed_by") == "codex" + assert {key: row for key, row in rows.items() if key != original["todo_id"]} == { + key: row for key, row in before.items() if key != original["todo_id"] + } + assert post(path, {})[1]["proposal"]["receipt"] == receipt + assert _snapshot(runtime) == final + calls = [json.loads(line)["method"] for line in capture.read_text().splitlines()] + assert calls.count("turn/start") == 1 + + @pytest.mark.parametrize("change", ["defaults", "close"]) def test_task_session_is_pinned_before_its_checkpoint(task_start, monkeypatch, change): actions, post, chat, controller, reopen, runtime, capture, proposal_id = task_start @@ -292,3 +333,83 @@ def after_completion(proposal_id, *, step, receipt): assert calls.count("thread/start") == 1 assert calls.count("turn/start") == (0 if interruption == "accepted-then-stopped" else 1) assert _snapshot(runtime) == before + + +@pytest.mark.parametrize(("task_start", "interruption"), [ + pytest.param((provider, "soft_claim" if stage == "turn-completed-v23" else "hard_lease"), stage, + id=f"{provider}-{stage}") + for provider in ("file", "sqlite") for stage in ( + "claim-committed", "turn-completed", "turn-completed-v23", "claim-missing", "edited-before-claim", + ) +], indirect=["task_start"]) +def test_legacy_task_confirmation_recovers_the_original_execution(task_start, environment, interruption): # noqa: F811 + actions, post, _, controller, reopen, runtime, capture, example_id = task_start + params = actions.load(example_id)["normalized_parameters"] + intent = dict(registry_path=controller.registry_path, goal_id=params["goal_id"], text=params["text"]) + preview = build_todo_review_preview(**intent) + proposal = actions.create_preview( + action_kind="todo.create", summary=params["text"], normalized_parameters=params, + context={"kind": "goal", "goal_id": params["goal_id"]}, expected_state_fingerprint=preview["preview_id"], + permission_classification="durable_write", validation_evidence=[], available_transitions=["apply", "cancel"], + idempotency_key="legacy-start-appendix", + ) + proposal_id = proposal["proposal_id"] + actions.start_apply(proposal_id) + receipt = apply_todo_review_preview(**intent, preview_id=preview["preview_id"])["receipt"] + todo_id = receipt["todo_id"] + actions.save_checkpoint(proposal_id, step="todo_created", receipt={"todo_id": todo_id, "canonical_receipt": receipt}) + owner = params["agent_id"] + completed = interruption.startswith("turn-completed") + if interruption == "turn-completed-v23": + # 2.3.0 assigned the Todo without a managed claim before submitting its Turn. + update_goal_todo(registry_path=controller.registry_path, goal_id=params["goal_id"], todo_id=todo_id, + claimed_by=owner, agent_id=owner, authority_reason="owner-confirmed typed Chat action", dry_run=False) + elif interruption in {"claim-committed", "turn-completed"}: + # The old native claim request had the same operation ID without a reviewed revision. + claim = update_goal_todo(registry_path=controller.registry_path, goal_id=params["goal_id"], + todo_id=todo_id, agent_id=owner, claimed_by=owner, claim_only=True, manage_execution=True, + claim_operation_id=f"chat-task-start:{proposal_id}", dry_run=False) + assert claim["status"] == "applied" + if completed or interruption == "claim-missing": + actions.save_checkpoint(proposal_id, step="todo_assigned", receipt={"todo_id": todo_id, "agent_id": owner}) + if interruption == "edited-before-claim": + update_goal_todo(registry_path=controller.registry_path, goal_id=params["goal_id"], + todo_id=todo_id, text="Review a different appendix", agent_id=owner, dry_run=False) + if completed: + _, _, _, project, _ = environment + session, _ = controller.open_session(goal_id=params["goal_id"], agent_id=owner, work_dir=project, + objective=params["text"], mode="resume_latest", channel_id=f"task.{todo_id}", agent_goal_id=params["goal_id"]) + sid = session["session_id"] + if interruption != "turn-completed-v23": + actions.save_checkpoint(proposal_id, step="task_session_opened", receipt={"session_id": sid}) + turn, _ = controller.submit_turn(session_id=sid, client_turn_id=f"task-start-{proposal_id}", + message=params["text"], work_dir=project, objective=params["text"]) + identity = {"session_id": sid, "turn_id": turn["turn_id"]} + assert controller.wait_for_turn(**identity, timeout_sec=10)["status"] == "completed" + actions.save_checkpoint(proposal_id, step="execution_started", receipt=identity) + _stop_goal(post) + actions.mark_failed(proposal_id, error_code="canonical_action_failed", message="Interrupted before confirmation") + before = _snapshot(runtime) + chat, controller = reopen() + path = f"/api/actions/{proposal_id}/apply" + + code, result = post(path, {}) + if interruption in {"claim-missing", "edited-before-claim"}: + assert code == 409, result + assert not capture.exists() + else: + assert code == 202, result + applied = result["proposal"]["receipt"] + ids = applied["resource_ids"] + assert applied["canonical_receipt"] == receipt and ids["todo_id"] == todo_id + resumed = {"session_id": ids["session_id"], "turn_id": ids["turn_id"]} + if completed: + assert resumed == identity and result["turn"]["created"] is False + assert controller.wait_for_turn(**resumed, timeout_sec=10)["status"] == "completed" + assert post(path, {})[1]["proposal"]["receipt"] == applied + assert [row["session_id"] for row in chat.session_candidates( + goal_id=params["goal_id"], agent_id=owner, channel_id=f"task.{todo_id}", + )] == [ids["session_id"]] + methods = [json.loads(line)["method"] for line in capture.read_text().splitlines()] + assert methods.count("thread/start") == methods.count("turn/start") == 1 + assert _snapshot(runtime) == before diff --git a/tests/control_plane/test_native_user_completion_update.py b/tests/control_plane/test_native_user_completion_update.py index 3dfa3d3f19..3fafd8a755 100644 --- a/tests/control_plane/test_native_user_completion_update.py +++ b/tests/control_plane/test_native_user_completion_update.py @@ -78,6 +78,17 @@ def validation_command(source: str) -> str: return shlex.join([sys.executable, "-c", source]) +def chat_runtime_routes( + registry: Path, *, host_fallback: bool, +) -> tuple[str, str | None]: + if not host_fallback: + return str(registry.parent / "chat-history"), None + registration = json.loads(registry.read_text(encoding="utf-8")) + runtime = str(registration.pop("common_runtime_root")) + registry.write_text(json.dumps(registration), encoding="utf-8") + return runtime, runtime + + def blocking_validator(root: Path) -> str: script = root / "check_completion.py" script.write_text( @@ -108,10 +119,14 @@ def wait_for_validation(root: Path) -> None: def test_exited_validator_stops_redirected_same_group_child_before_retry( tmp_path: Path, provider: str, entrypoint: str, exit_code: int, ) -> None: + # The leader exits on this signal. Publish the PID only after it is complete. child = ( - "import os, time; from pathlib import Path; " - f"root=Path({str(tmp_path)!r}); " - "(root/'child-pid').write_text(str(os.getpid()))\n" + "import os, time\nfrom pathlib import Path\n" + f"root = Path({str(tmp_path)!r})\n" + "pid_file = root / 'child-pid'\n" + "pending_pid = pid_file.with_suffix('.tmp')\n" + "pending_pid.write_text(str(os.getpid()), encoding='utf-8')\n" + "pending_pid.replace(pid_file)\n" "while not (root/'allow-child-write').exists(): time.sleep(0.01)\n" "(root/'late-child-write').touch()\n" "while True: time.sleep(0.1)\n" @@ -468,14 +483,26 @@ def test_invalid_input_and_permission_do_not_run_validation(tmp_path: Path, prov @pytest.mark.parametrize("provider", ["file", "sqlite"]) -@pytest.mark.parametrize("failure_boundary", ["display", "action_receipt"]) -def test_reviewed_chat_completion_recovers_projection(tmp_path: Path, provider: str, monkeypatch: pytest.MonkeyPatch, failure_boundary: str) -> None: +@pytest.mark.parametrize( + ("failure_boundary", "host_fallback"), + [("display", False), ("action_receipt", False), ("action_receipt", True)], + ids=["registered-display", "registered-receipt", "host-fallback-receipt"], +) +def test_reviewed_chat_completion_recovers_projection( + tmp_path: Path, provider: str, monkeypatch: pytest.MonkeyPatch, + failure_boundary: str, host_fallback: bool, +) -> None: from loopx.chat_action_store import ChatActionStore from loopx.chat_actions import ChatActionService from loopx.control_plane.todos import provider_projection registry, state, todo_id = fixture(tmp_path, provider) - service = ChatActionService(store=ChatActionStore(tmp_path / "actions"), registry_path=registry) + chat_root, todo_root = chat_runtime_routes(registry, host_fallback=host_fallback) + route = {"registry_path": registry, "goal_id": "goal-a", "runtime_root_arg": todo_root} + service = ChatActionService( + store=ChatActionStore(tmp_path / "actions"), registry_path=registry, + runtime_root_override=chat_root, + ) proposal = service.preview({"action_kind": "todo.update", "summary": "Record an observed outcome", "normalized_parameters": {"goal_id": "goal-a", "todo_id": todo_id, "agent_id": "agent-a", "operation": "complete", "note": "Reviewed observation"}, @@ -494,13 +521,16 @@ def lost_response(*args, **kwargs): patch.setattr(service.store, "apply", lost_response) with pytest.raises(ConnectionError): service.apply(proposal["proposal_id"]) - assert list_goal_todos(registry_path=registry, goal_id="goal-a")["todos"][0]["status"] == "done" + committed = list_goal_todos(**route) + assert committed["todos"][0]["status"] == "done" state.unlink() recovered = service.apply(proposal["proposal_id"])["proposal"] assert recovered["status"] == "applied" assert recovered["receipt"]["outcome"] == "todo_completed" assert state.exists() - dependent = next(row for row in list_goal_todos(registry_path=registry, goal_id="goal-a")["todos"] if row["role"] == "agent") + after = list_goal_todos(**route) + assert after["authority_read"]["provider_revision"] == committed["authority_read"]["provider_revision"] + dependent = next(row for row in after["todos"] if row["role"] == "agent") assert (dependent["status"], dependent["claimed_by"]) == ("open", "agent-a") @@ -523,7 +553,10 @@ def test_chat_user_validation_failure_never_creates_success_receipt(tmp_path: Pa @pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("case", ["multiline_command", "multiline_argv", "failed"]) -def test_packaged_chat_http_user_completion(tmp_path: Path, provider: str, case: str) -> None: +@pytest.mark.parametrize("host_fallback", [False, True], ids=["registered", "host-fallback"]) +def test_packaged_chat_http_user_completion( + tmp_path: Path, provider: str, case: str, host_fallback: bool, +) -> None: from http.client import HTTPConnection from threading import Thread from loopx.chat_action_store import ChatActionStore @@ -551,12 +584,20 @@ def test_packaged_chat_http_user_completion(tmp_path: Path, provider: str, case: validation_argv=argv if use_argv else None, validation_label=label if passed else None, ) + chat_root, todo_root = chat_runtime_routes(registry, host_fallback=host_fallback) + route = {"registry_path": registry, "goal_id": "goal-a", "runtime_root_arg": todo_root} + before = list_goal_todos(**route) + dependent_id = next(row["todo_id"] for row in before["todos"] if row["role"] == "agent") store = ChatActionStore(tmp_path / "actions") server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler) server.verbose = False server.assets_dir = default_chat_assets_dir() + server.registry_path = registry + server.runtime_root_override = chat_root server.action_store = store - server.action_service = ChatActionService(store=store, registry_path=registry) + server.action_service = ChatActionService( + store=store, registry_path=registry, runtime_root_override=chat_root, + ) thread = Thread(target=server.serve_forever, daemon=True) thread.start() connection = HTTPConnection("127.0.0.1", server.server_address[1], timeout=45) @@ -565,6 +606,11 @@ def test_packaged_chat_http_user_completion(tmp_path: Path, provider: str, case: response = connection.getresponse() assert response.status == 200 assert b" { 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("a retained Todo confirmation can recover the original result after a source gate", () => { + const receipt = {schema_version: "loopx_chat_todo_receipt_v0", status: "applied", todo_id: "todo-original", + goal_id: "report", preview_id: "reviewed", preview_revision: "original-revision", receipt_id: "saved-receipt"}; + const created = {todo_id: receipt.todo_id, canonical_receipt: receipt}; + const proposal = {proposal_id: "create-task", expected_state_fingerprint: "reviewed", action_kind: "todo.create", + permission_classification: "durable_write", normalized_parameters: {goal_id: "report", start_execution: false}, + context: {goal_id: "report"}, checkpoint: {steps: {todo_created: created}}}; + const sourceGate = {status: "gated", gate: {kind: "canonical_authority_required"}}; + for (const state of [{status: "failed", failure: {retry_safe: true}}, {status: "applying"}, sourceGate]) { + const plan = compileActionReviewPlan({...proposal, ...state}); + assert.equal(plan.reason, "todo_creation_retry"); + assert.equal(plan.proposalId, "create-task"); + assert.equal(plan.retryOriginal, true); + assert.equal(plan.prohibitRegeneration, true); + } + for (const change of [{context: {goal_id: "other"}}, {context: {binding_id: "steward-binding"}}, + {gate: {kind: "unrelated_gate"}}, {expected_state_fingerprint: "other"}, + {checkpoint: {steps: {todo_created: {...created, source: null}}}}]) { + const plan = compileActionReviewPlan({...proposal, ...sourceGate, ...change}); + assert.equal(plan.canApply, false); + assert.equal(plan.prohibitRegeneration, true); + } + assert.equal(compileActionReviewPlan({...proposal, status: "stale", checkpoint: null}).prohibitRegeneration, undefined); +}); + 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"}; diff --git a/tests/control_plane_ts/local_authority_provider.test.ts b/tests/control_plane_ts/local_authority_provider.test.ts index 3705b2fe87..fef3448a23 100644 --- a/tests/control_plane_ts/local_authority_provider.test.ts +++ b/tests/control_plane_ts/local_authority_provider.test.ts @@ -111,6 +111,7 @@ function providerCalls(directory: string, revision: string, dryRun: boolean) { // witness; keep it out of legacy requests, which intentionally reject it. const witnessed = {...input, registry_source: {path: join(directory, "registry.json"), sha256: createHash("sha256").update("{}").digest("hex")}}; + const sourceBound = {...witnessed, expected_registry_sha256: witnessed.registry_source.sha256}; type Entrypoint = {[K in keyof typeof runtime]: typeof runtime[K] extends (value: unknown) => Promise ? K : never}[keyof typeof runtime]; // A new exported runtime action must deliberately enter this failure matrix. @@ -137,10 +138,12 @@ function providerCalls(directory: string, revision: string, dryRun: boolean) { {...updateInput, schema_version: runtime.LOCAL_COORDINATION_TODO_CREATE_REQUEST_SCHEMA, todo: {}}, {...updateInput, registry_source: witnessed.registry_source, schema_version: runtime.LOCAL_COORDINATION_TODO_CREATE_WITNESSED_REQUEST_SCHEMA, todo: {}}, - {...witnessed, schema_version: runtime.LOCAL_COORDINATION_TODO_CREATE_REVIEWED_REQUEST_SCHEMA, todo: {}}], + {...witnessed, schema_version: runtime.LOCAL_COORDINATION_TODO_CREATE_REVIEWED_REQUEST_SCHEMA, todo: {}}, + {...sourceBound, schema_version: "loopx_local_coordination_todo_create_request_v3", todo: {}}], claimLocalCoordinationTodo: [ {...input, schema_version: runtime.LOCAL_COORDINATION_TODO_CLAIM_REQUEST_SCHEMA}, - {...witnessed, schema_version: runtime.LOCAL_COORDINATION_TODO_CLAIM_WITNESSED_REQUEST_SCHEMA}], + {...witnessed, schema_version: runtime.LOCAL_COORDINATION_TODO_CLAIM_WITNESSED_REQUEST_SCHEMA}, + {...sourceBound, schema_version: "loopx_local_coordination_todo_claim_request_v3", manage_execution: true}], updateLocalCoordinationTodo: [ {...updateInput, schema_version: "loopx_local_coordination_todo_update_request_v0"}, {...updateInput, schema_version: "loopx_local_coordination_todo_update_request_v1", planning_intent: {status: "blocked"}}, diff --git a/tests/control_plane_ts/managed_claim_execution_conformance.ts b/tests/control_plane_ts/managed_claim_execution_conformance.ts index a73afda9d9..54bda5ab23 100644 --- a/tests/control_plane_ts/managed_claim_execution_conformance.ts +++ b/tests/control_plane_ts/managed_claim_execution_conformance.ts @@ -437,6 +437,22 @@ export function registerManagedClaimExecutionConformance(provider: string, facto "coordination_operation_identity_mismatch", "other changed intent remains rejected"); assert.deepEqual(await loaded(store), before); }); + test(`${provider} managed execution: reviewed replay preserves the original unreviewed claim`, async t => { + const {store, request} = await setup(t); + const reviewed = {...request, expected_provider_revision: (await loaded(store)).provider_revision}; + const accepted = await claim(store, request); + assert.equal(accepted.status, "applied"); + const before = await loaded(store); + const replay = await claim(store, reviewed); + assert.equal(replay.status, "replayed"); + assert.deepEqual(replay.lease, accepted.lease); + assert.deepEqual(replay.original_receipt, accepted.original_receipt); + assert.equal((await claim(store, {...reviewed, claimed_by: "agent-b", actor_agent_id: "agent-b"})).reason_code, + "coordination_operation_identity_mismatch"); + assert.equal((await claim(store, reviewed, async () => false)).reason_code, "authority_source_changed"); + assert.equal((await claim(store, {...reviewed, now: new Date("2026-09-13T12:00:00Z")})).status, "failed"); + assert.deepEqual(await loaded(store), before, "historical replay never renews the lease or writes a new claim"); + }); test(`${provider} managed execution: completion releases once and the original Turn closes without new execution`, async t => { const {store, request} = await setup(t); assert.equal((await claim(store, request)).status, "applied"); diff --git a/tests/test_chat_todo_detail.py b/tests/test_chat_todo_detail.py index 14dafc2da5..dc97fb2707 100644 --- a/tests/test_chat_todo_detail.py +++ b/tests/test_chat_todo_detail.py @@ -251,6 +251,73 @@ def cli(*args, expected_code=0): worker.join() +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_explicit_task_read_with_conflicting_defaults(tmp_path, monkeypatch, capsys, provider): + from loopx import paths + from loopx.cli import main + from tests.control_plane.canonical_authority_fixture import ( + isolate_sqlite_runtime, + promoted_create_fixture, + ) + + isolate_sqlite_runtime(tmp_path, monkeypatch) + defaults = (tmp_path / "current", tmp_path / "legacy") + for root in defaults: + root.mkdir() + (root / "machine.json").write_text("{}", encoding="utf-8") + monkeypatch.setattr(paths, "DEFAULT_RUNTIME_ROOT", defaults[0]) + monkeypatch.setattr(paths, "LEGACY_RUNTIME_ROOT", defaults[1]) + + registry, runtime, state = promoted_create_fixture(tmp_path, provider=provider) + registration = json.loads(registry.read_text(encoding="utf-8")) + del registration["common_runtime_root"] + registry.write_text(json.dumps(registration), encoding="utf-8") + initial_state = state.read_bytes() + + def cli(*args, explicit=True): + override = ["--runtime-root", str(runtime)] if explicit else [] + code = main(["--registry", str(registry), *override, "--format", "json", + "todo", *args, "--goal-id", "goal-a"]) + return code, json.loads(capsys.readouterr().out) + + body = "[P1] Read the committed task when both machine roots contain state." + code, created = cli("add", "--role", "agent", "--text", body) + assert code == 0 and created["ok"], created + todo_id = created["todo_id"] + assert created["projection_delivery"] == "pending" + assert created["projection_outbox"]["retry_business_mutation"] is False + canonical = read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") + + code, refused = cli("list", "--todo-id", todo_id, explicit=False) + assert code == 1 and not refused["ok"] + assert "Both default LoopX runtime roots contain state" in refused["error"] + + code, listed = cli("list", "--todo-id", todo_id) + assert code == 0 and listed["todo"]["text"] == body, listed + assert listed["authority_read"]["provider_revision"] == canonical["provider_revision"] + + with ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler) as server: + server.registry_path, server.runtime_root_override, server.verbose = registry, str(runtime), False + worker = threading.Thread(target=server.serve_forever, daemon=True) + worker.start() + url = (f"http://127.0.0.1:{server.server_port}/api/chat/todo/detail" + f"?goal_id=goal-a&todo_id={todo_id}") + try: + with urlopen(url, timeout=30) as response: + detail = json.load(response) + assert detail["todo_id"] == todo_id and detail["text"] == body + assert detail["review_basis"]["provider_revision"] == canonical["provider_revision"] + finally: + server.shutdown() + worker.join() + + assert read_canonical_todos_if_promoted(runtime_root=runtime, goal_id="goal-a") == canonical + assert state.read_bytes() == initial_state + for root in defaults: + assert list(root.iterdir()) == [root / "machine.json"] + assert (root / "machine.json").read_text(encoding="utf-8") == "{}" + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_source_bound_requirements_use_real_canonical_writer(tmp_path, monkeypatch, provider): monkeypatch.syspath_prepend(str(Path(__file__).parent / "control_plane"))