diff --git a/apps/presentation/dashboard/src/features/personal-workspace/goal-workspace-work.tsx b/apps/presentation/dashboard/src/features/personal-workspace/goal-workspace-work.tsx index 794ca4ae2c..076f07ee04 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/goal-workspace-work.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/goal-workspace-work.tsx @@ -1,6 +1,6 @@ import {ContextHelp} from "./context-help"; import {ArrowRight, ChevronDown, Circle, CircleAlert, CircleCheck, CircleHelp, Clock, Hand, Info, List, Play, Plus, Search} from "lucide-react"; -import {useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode} from "react"; +import {Component, createRef, useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode} from "react"; import type {GoalWorkDisplayClass, GoalWorkspaceDigest} from "../../../../../../loopx/control_plane/presentation/goal_workspace_digest.js"; import type {WorkspaceActionPreview, WorkspaceActionPreviewRequest, WorkspaceDrawerSelection, WorkspaceGoal, WorkspaceModel, WorkspaceTimelineItem} from "./personal-workspace-model"; import {localizedSessionStatus, useWorkspaceI18n} from "./i18n"; @@ -223,7 +223,7 @@ export function GoalWorkspaceWork({goal, digest, items, userTodos, selectedTodoI : null} {!readOnly ? : null} - + {visited.has("board") || lens === "board" ? : null} {visited.has("map") || lens === "map" ? : null} @@ -254,6 +254,35 @@ export function GoalWorkspaceWork({goal, digest, items, userTodos, selectedTodoI ; } +type OwnerWorkGroupsProps = {hidden: boolean; children: ReactNode}; + +class OwnerWorkGroups extends Component { + private readonly groups = createRef(); + + getSnapshotBeforeUpdate(): string | null { + const groups = this.groups.current; + const focused = groups?.ownerDocument.activeElement; + return focused instanceof HTMLButtonElement && groups?.contains(focused) + ? focused.dataset.todoId ?? null : null; + } + + componentDidUpdate(_previousProps: OwnerWorkGroupsProps, _previousState: object, todoId: string | null) { + // Crossing the summary boundary replaces the button. Restore its focus if + // the browser dropped it during this commit and the same task is still here. + const groups = this.groups.current; + if (!todoId || !groups || groups.closest("[hidden]") || groups.ownerDocument.activeElement !== groups.ownerDocument.body) return; + const row = groups.querySelector(`button[data-todo-id="${CSS.escape(todoId)}"]`); + if (!row) return; + const disclosure = row.closest("details.goal-work-more"); + if (disclosure) disclosure.open = true; + row.focus(); + } + + render() { + return ; + } +} + function RemainingWork({selectedTodoId, label, children}: {selectedTodoId: string | null; label: string; children: ReactNode}) { const disclosure = useRef(null); useEffect(() => {if (selectedTodoId && disclosure.current) disclosure.current.open = true;}, [selectedTodoId]); diff --git a/docs/product/runtimes/codex-cli/codex-cli-automation-driver.md b/docs/product/runtimes/codex-cli/codex-cli-automation-driver.md index 45cd648097..1ed9c2a254 100644 --- a/docs/product/runtimes/codex-cli/codex-cli-automation-driver.md +++ b/docs/product/runtimes/codex-cli/codex-cli-automation-driver.md @@ -93,6 +93,32 @@ and `--host-adapter-command-json ...` for a compatible Trae or another CLI after its wrapper implements the typed stdin/stdout contract in the host-neutral protocol. +The built-in Codex result schema accepts `validated_completion` for a fully +delivered selected Todo. After independent validation, the adapter invokes the +existing Todo lifecycle, including its controller-declared validator, before +accountable refresh and one quota debit. It retains the original launch lease +generation in the Turn journal for completion and cached-result recovery; +lease loss or a newer same-Agent generation cannot authorize the old result. +`validated_progress` remains partial progress and never silently completes work. +For a predeclared terminal Todo, closeout after debit uses the original +completion receipt and closes the same Turn. +Goal acceptance remains a separate owner check. + +After independent validation, the adapter checks the original Goal's running +authority before settling either result. When the owner stops the Goal during +that validation, the Turn retains its validation checkpoint and leaves the +Todo and quota untouched. + +内置 Codex 的结果格式支持 `validated_completion`,用于已满足全部要求的已选 Todo。 +独立验证通过后,adapter 先调用现有 Todo 生命周期及 controller 声明的完成验证器, +再写回可核算进展并扣额一次。Turn journal 保留启动时的原租约代次,完成和缓存结果 +恢复都使用该代次;租约丢失或同 Agent 的新代次不能授权旧结果。`validated_progress` +仍表示部分进展,不会被静默升级为完成。对于预声明的终态 Todo,扣额后按原完成回执 +收尾同一轮 Turn。Goal 验收仍由原 owner 单独检查。 + +独立验证结束后,adapter 会在结算前重新检查原 Goal 的运行权限。验证期间若 owner +停止 Goal,Turn 保留验证记录,Todo 和额度不变。 + That wrapper is deliberately thin: it translates one Turn request into one bounded Agent CLI invocation, preserves only an opaque local resume handle, and returns one typed candidate result. LoopX remains responsible for the control diff --git a/docs/reference/canonical-todo-completion-update.md b/docs/reference/canonical-todo-completion-update.md index 291ece5652..9de8a4a3b9 100644 --- a/docs/reference/canonical-todo-completion-update.md +++ b/docs/reference/canonical-todo-completion-update.md @@ -214,6 +214,28 @@ Python 只复用 OS I/O;准入、声明、期限、源/租约见证和完成 ## Linked User completion effects +### Monitor observations and User gates + +A Monitor observation and a User gate completion have different identities and +different owners. An observation is evidence: a file appearing, a changed hash or +a successful external job never completes or approves a User gate, and the +Monitor contract projects no gate completion command. When a due Monitor is +admitted while the agent has open User gates, its actions carry a read-only +notice instead. + +Two attempts stay rejected by their existing owners: completing the gate with +the Monitor Turn (`settlement binding does not match the original quota +guard`), and attaching evidence through a plain `todo update` +(`handoff_mode_requires_lease`). The agent records the observation with +`quota monitor-poll` and yields. An observation is not tied to any gate, so it is +not reported as one. The gate owner completes a gate through the User lifecycle +with their own decision; a linked target then resumes for a fresh guarded Turn. + +Work that may continue on an observed change without an owner decision does not +belong behind a User gate. Emit it from the observation itself with +`quota monitor-poll --material-change --next-agent-todo ...`: the successor is an +independent runnable Agent Todo, admitted by its own fresh guard. + An admitted `todo complete --decision-outcome approve|reject|cancel` now commits its exact linked Agent Todo effects in the **same** provider transaction as the User completion and receipt. Ordinary User actions completed through update or diff --git a/examples/personal-workspace-browser/goal-workspace-m2.mjs b/examples/personal-workspace-browser/goal-workspace-m2.mjs index ef5b6bc191..dc1ef830e9 100644 --- a/examples/personal-workspace-browser/goal-workspace-m2.mjs +++ b/examples/personal-workspace-browser/goal-workspace-m2.mjs @@ -286,6 +286,7 @@ async function runM2Pass({browser, collectCoverage, url}) { const renderBudget = {coverageEnabled:collectCoverage,initialMs,expandMs,switchMs,switchSamplesMs,...lensBudget, longTaskSupported,switchLongTasks,nodeCount:200}; console.log(JSON.stringify({m2RenderBudget:renderBudget})); + await verifyFocusAfterReprioritization(page, now, many[4]); assert.equal(writes.length,0,`Rendering created effects: ${writes}`); assert.equal(session.errors.length,0,session.errors.join('\n')); if (!collectCoverage) assert.ok(lensBudget.withinBudget, `Loaded lens switches exceeded repeated 1s target or 2s ceiling: ${JSON.stringify({switchSamplesMs,...lensBudget})}`); @@ -295,6 +296,40 @@ async function runM2Pass({browser, collectCoverage, url}) { finally {await session.close();} } +async function verifyFocusAfterReprioritization(page, now, todo) { + const byOwner = now.getByRole("button", {name:"按负责人", exact:true}); + await byOwner.click(); + const owner = now.locator('.goal-owner-group[data-owner-id="worker-0"]'); + const remaining = owner.locator(".goal-work-more"); + await remaining.locator(":scope > summary").press("Enter"); + assert.equal(await remaining.evaluate(node => node.open), false); + + const row = `[data-todo-id="${todo.todo_id}"]`; + const leading = owner.locator(`:scope > ${row}`); + const moved = remaining.locator(`${row}[data-work-class="blocked"]`); + const refresh = page.getByRole("button", {name:"刷新状态", exact:true}); + // Simulate a status refresh while the keyboard user remains on the page. + const refreshWithoutMovingFocus = () => refresh.evaluate(button => button.click()); + + await byOwner.focus(); + todo.status = "blocked"; + await refreshWithoutMovingFocus(); + await moved.waitFor({state:"attached"}); + assert.equal(await moved.isVisible(), false, "An unfocused task does not open its disclosure on refresh"); + assert.equal(await byOwner.evaluate(node => document.activeElement === node), true, "Refresh leaves focus outside the task list alone"); + + todo.status = "open"; + await refreshWithoutMovingFocus(); + await leading.focus(); + todo.status = "blocked"; + await refreshWithoutMovingFocus(); + await moved.waitFor({state:"attached"}); + assert.equal(await moved.evaluate(node => document.activeElement === node), true, "The same task keeps keyboard focus after it moves"); + await page.keyboard.press("Enter"); + await page.waitForURL(value => value.searchParams.get("todo") === todo.todo_id); + await page.locator(".personal-context-drawer").waitFor(); +} + async function verifyRemoteSelection(browser, url) { const snapshot = JSON.parse(await readFile(resolve(repoRoot,"examples/status.example.json"),"utf8")); diff --git a/loopx/cli_commands/turn_run_once.py b/loopx/cli_commands/turn_run_once.py index fa28ea033e..80e46f402f 100644 --- a/loopx/cli_commands/turn_run_once.py +++ b/loopx/cli_commands/turn_run_once.py @@ -4,6 +4,7 @@ from functools import partial import sys as _sys from collections.abc import Callable, Mapping +from contextlib import contextmanager, nullcontext from pathlib import Path from typing import TYPE_CHECKING as _TYPE_CHECKING from typing import Any @@ -476,8 +477,29 @@ def terminal_closeout( effect_ref: str, ) -> dict[str, object]: require_effect_ref(effect_ref, SettlementStepKind.TERMINAL_CLOSEOUT) + if args.host == "codex-cli": + # Builtin completion has already retired the original lease. + # Verify the exact terminal receipt; never start another completion. + observed = terminal_closeout_resolver(effect_ref) + receipt = observed.get("payload") + if observed.get("kind") == "committed" and isinstance(receipt, dict): + return receipt + return { + "ok": False, + "appended": False, + "reason": observed.get("reason") or "Builtin terminal completion receipt is missing", + } return todo_completion(result, effect_ref=effect_ref) + def codex_writeback( + result: dict[str, object], *, effect_ref: str, + ) -> dict[str, object]: + # Accountable refresh requires durable controller validation even for + # a predeclared terminal Todo. Other hosts keep their existing order. + if result.get("result_kind") == "validated_completion": + return completion_writeback(result, effect_ref=effect_ref) + return writeback(result, effect_ref=effect_ref) + def current_status() -> dict[str, object]: return collect_turn_status_payload( args, @@ -847,6 +869,19 @@ def settlement_admission(): return request_settlement_lifetime(request_locator, settlement_identity, registry_path=registry_path, runtime_root=runtime_root, workspace=project) + if args.host == "codex-cli" and goal_admission is not None: + request_lifetime = settlement_admission or nullcontext + + @contextmanager + def codex_settlement_admission(): + with request_lifetime(): + # Independent validation can outlive the running Goal. + # Check the original authority before any settlement effect. + goal_admission.require_running(operation="codex_cli_turn_settle") + yield + + settlement_admission = codex_settlement_admission + def on_managed_start_admitted() -> None: if settlement_identity.request_subject is not None: require_current_request_operation(request_locator, settlement_identity, @@ -898,7 +933,8 @@ def on_managed_start_admitted() -> None: retry_failed=bool(args.retry_failed_turn), task_validator=task_validator, settlement_admission=settlement_admission, - writeback=writeback if args.execute else None, + writeback=(codex_writeback if args.host == "codex-cli" else writeback) + if args.execute else None, completion_writeback=completion_writeback if args.execute else None, completion_intent=completion_intent if args.execute else None, terminal_closeout=terminal_closeout if args.execute else None, diff --git a/loopx/control_plane/quota/accounting_artifact_transaction.ts b/loopx/control_plane/quota/accounting_artifact_transaction.ts index 54867f699d..e70a5c7bb9 100644 --- a/loopx/control_plane/quota/accounting_artifact_transaction.ts +++ b/loopx/control_plane/quota/accounting_artifact_transaction.ts @@ -508,13 +508,35 @@ export function resolveQuotaAccountingEffect( return { kind: "absent" }; } +function spendIdentityMismatch(index: JsonObject, retained: JsonObject): boolean { + // The spend writers retain these bindings even before commit metadata was + // introduced. Missing historical fields are unknown; explicit conflicts are + // never authenticated by a matching timestamp or billing terms. + return ["agent_id", "effect_ref", "turn_instance_id", "todo_id", + "replan_obligation_id", "settlement_identity", "goal_ref"].some(field => { + const expected = index[field], actual = retained[field]; + return expected !== undefined && expected !== null && expected !== "" && + actual !== undefined && actual !== null && actual !== "" && + canonicalJson(expected) !== canonicalJson(actual); + }); +} + export async function readQuotaSpendEventFromRun( runsDir: string, run: JsonObject, goalId: string, ): Promise { + const contract = contractFor("spend"); + const indexCommit = run[contract.metadataField]; const inline = jsonObject(run.quota_event); - if (inline) return inline; + if (inline && indexCommit === undefined) { + if (spendIdentityMismatch(run, inline)) { + throw new EffectRuntimeRequestError( + "quota spend JSON artifact identity does not match its index row", + ); + } + return inline; + } if (typeof run.json_path !== "string" || !run.json_path.trim()) return null; let targetPath: string; try { @@ -550,15 +572,30 @@ export async function readQuotaSpendEventFromRun( throw new EffectRuntimeRequestError("quota spend JSON artifact is malformed"); } const record = requiredObject(value, "quota spend JSON artifact"); + const artifactCommit = jsonObject(record[contract.metadataField]); + // The writer repeats its commit identity in the index and artifact. An + // in-directory file can still describe a different debit for the same Goal. + const commitMismatch = indexCommit !== undefined && ( + jsonObject(indexCommit) === null || artifactCommit === null || + canonicalJson(indexCommit) !== canonicalJson(artifactCommit) + ); + // A historical row without a commit reference can still have an explicit + // Run timestamp. Preserve omitted historical fields, but reject conflicts. + const timestampMismatch = typeof run.generated_at === "string" && run.generated_at !== "" && + typeof record.generated_at === "string" && record.generated_at !== "" && + run.generated_at !== record.generated_at; + const event = jsonObject(record.quota_event); if ( - record.classification !== contractFor("spend").classification || - (record.goal_id !== undefined && record.goal_id !== goalId) + record.classification !== contract.classification || + (record.goal_id !== undefined && record.goal_id !== goalId) || + commitMismatch || timestampMismatch || spendIdentityMismatch(run, record) || + (event !== null && spendIdentityMismatch(run, event)) ) { throw new EffectRuntimeRequestError( "quota spend JSON artifact identity does not match its index row", ); } - return jsonObject(record.quota_event); + return event; } export async function lookupQuotaAccountingReplay( diff --git a/loopx/control_plane/quota/live_decision.py b/loopx/control_plane/quota/live_decision.py index 4105e66a1a..fde3e9c491 100644 --- a/loopx/control_plane/quota/live_decision.py +++ b/loopx/control_plane/quota/live_decision.py @@ -3,12 +3,15 @@ import shlex from collections.abc import Callable, Mapping, Sequence +from functools import partial from pathlib import Path from typing import Any from ...quota import build_quota_should_run from ...agent_registry import load_goal_from_registry, registered_agent_ids_for_goal +from ...command_invocation import loopx_cli_argv from ..agent_context import project_agent_context, project_goal_agent_context +from ..agents.identity import build_quota_agent_identity from ..runtime.time import now_utc_iso from ..goals.activation import goal_owner_resume_id from ..goals.goal_frontier.fallback_source import live_fallback_authority_items @@ -34,10 +37,13 @@ replan_obligation_id_from_packet, ) from ..todos.contract import normalize_todo_id +from ..scheduler.arbitration import SchedulerArbitration from ..scheduler.execution_context import ( SchedulerExecutionContextResolution, resolve_scheduler_execution_context, ) +from ..scheduler.scheduler_hint import build_scheduler_hint +from ..scheduler.state import load_app_automation_scheduler_state from .unsettled_host_turn import ( apply_unsettled_host_turn_recovery_if_required, apply_receipt_bound_wait_recovery, @@ -486,6 +492,7 @@ def bind_scheduler_followup_cli_routes( scheduler_hint = payload.get("scheduler_hint") if not isinstance(scheduler_hint, dict): return + launcher_args = loopx_cli_argv()[1:] app_packets = [ packet for packet_key in ("app_automation", "codex_app") @@ -532,13 +539,13 @@ def bind_scheduler_followup_cli_routes( } continue bound_cli_args = list(cli_args) - if bound_cli_args[0] != "--registry": - bound_cli_args = [ - "--registry", - str(registry_path.expanduser().resolve()), - "--runtime-root", - str(runtime_root.expanduser().resolve()), - *bound_cli_args, + # Standalone Core emits an interpreter route. Its launcher + # arguments must precede the CLI's global options. + option_index = len(launcher_args) if cli_args[:len(launcher_args)] == launcher_args else 0 + if bound_cli_args[option_index:option_index + 1] != ["--registry"]: + bound_cli_args[option_index:option_index] = [ + "--registry", str(registry_path.expanduser().resolve()), + "--runtime-root", str(runtime_root.expanduser().resolve()), ] safe_turn_instance_id = str(turn_instance_id or "").strip() if safe_turn_instance_id and "--turn-instance-id" not in bound_cli_args: @@ -737,7 +744,7 @@ def build_live_quota_should_run_decision( if remote_config is not None: # Use the same hard-pause precedence and keep fresh local selection # outside the remote route, even when a TLS read succeeds. - return _build_remote_authority_wait_decision( + payload = _build_remote_authority_wait_decision( decision_status_payload, configured_goal=registered_goal or {}, goal_id=goal_id, agent_id=agent_id, remote_config=remote_config, registry_path=registry_path, runtime_root=runtime_root, goal_ref=goal_ref, @@ -747,6 +754,11 @@ def build_live_quota_should_run_decision( turn_instance_id=turn_instance_id, receipt_bound_todo_id=receipt_bound_todo_id, receipt_bound_replan_obligation_id=receipt_bound_replan_obligation_id, ) + bind_scheduler_followup_cli_routes( + payload, registry_path=registry_path, runtime_root=runtime_root, + turn_instance_id=turn_instance_id, source=route_source, + ) + return payload settlement_readback = read_heartbeat_settlement( runtime_root, goal_id=goal_id, @@ -831,6 +843,13 @@ def build_live_quota_should_run_decision( goal_id=goal_id, agent_id=agent_id, ) + app_scheduler_state: dict[str, Any] | None = None + + def observe_app_scheduler_state(readback: Mapping[str, Any]) -> None: + # A later recovery wait uses the same state this decision actually read. + nonlocal app_scheduler_state + app_scheduler_state = readback.get("state") + payload: dict[str, Any] = build_quota_should_run( decision_status_payload, goal_id=goal_id, @@ -858,6 +877,7 @@ def build_live_quota_should_run_decision( **receipt_bound_guard_fields, turn_instance_id=turn_instance_id, runtime_root=runtime_root, + app_scheduler_state_observer=observe_app_scheduler_state, workspace_path=workspace_path, goal_ref=goal_ref, authoritative_fallback_todo_items=authoritative_fallback_todo_items, @@ -928,6 +948,14 @@ def build_live_quota_should_run_decision( agent_id=agent_id, current_turn_instance_id=turn_instance_id, available_capabilities=available_capabilities, + project_scheduler_hint=partial( + build_scheduler_hint, include_detail=include_scheduler_detail, + scheduler_execution_context=resolved_context, + available_capabilities=available_capabilities, + codex_app_scheduler_state=app_scheduler_state, + codex_app_current_rrule=observed_rrule, + codex_app_automation_id=observed_automation_id or None, + ), scheduler_execution_context=resolved_context, goal_ref=goal_ref, owner_action_id=goal_owner_resume_id(registered_goal), @@ -1050,6 +1078,24 @@ def _build_remote_authority_wait_decision( } if goal_ref is not None: payload["goal_ref"] = dict(goal_ref) + + def project_recovery_scheduler_hint( + decision: dict[str, Any], *, arbitration: SchedulerArbitration, + ) -> dict[str, Any]: + decision["agent_identity"] = build_quota_agent_identity(configured_goal, agent_id=agent_id) + host = scheduler_context.context + state = None + if scheduler_context.ok and host is not None and host.app_automation_applicable: + state = load_app_automation_scheduler_state( + runtime_root, goal_id=goal_id, agent_id=agent_id, surface=host.host_surface.value, + ) + return build_scheduler_hint( + decision, arbitration=arbitration, include_detail=include_scheduler_detail, + available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context, + codex_app_scheduler_state=state, codex_app_current_rrule=codex_app_rrule, + codex_app_automation_id=codex_app_automation_id, + ) + if scope_matches and not settled: if (receipt_bound_todo_id and agent_id and turn_instance_id and readback is not None and readback.replay_phase is ReceiptBoundReplayPhase.SETTLEMENT_PENDING): @@ -1068,6 +1114,7 @@ def _build_remote_authority_wait_decision( payload, registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id, agent_id=agent_id, current_turn_instance_id=turn_instance_id, available_capabilities=available_capabilities, scheduler_execution_context=scheduler_context, + project_scheduler_hint=project_recovery_scheduler_hint, goal_ref=goal_ref, owner_action_id=goal_owner_resume_id(configured_goal), ): diff --git a/loopx/control_plane/quota/settlement_readback.ts b/loopx/control_plane/quota/settlement_readback.ts index e0b3f977b5..e8b00ac642 100644 --- a/loopx/control_plane/quota/settlement_readback.ts +++ b/loopx/control_plane/quota/settlement_readback.ts @@ -134,6 +134,19 @@ interface ResultBundle extends JsonObject { payload: JsonObject; } +interface MatchedSpendRecords { + run: JsonObject | null; + receipt: JsonObject | null; + admissionSource?: unknown; +} + +interface SpendTerms { + slots: unknown; + source: unknown; +} + +const ORIGINAL_TURN_SPEND_SLOTS = 1; + type SettlementProgressState = "identity_required" | "writeback_required" | "writeback_receipt_required" | "spend_required" | "spend_receipt_required" | "settled"; @@ -1206,6 +1219,7 @@ function readQuotaSettlementFromRequest( request: ReadbackRequest, snapshot: QuotaSettlementReadbackSnapshot, nameOwedPriorTurn = true, + matchedSpend?: MatchedSpendRecords, ): JsonObject { if ( snapshot.runtimeRoot !== request.runtime_root || @@ -1292,6 +1306,11 @@ function readQuotaSettlementFromRequest( identity, ); const spendEvent = findStepEvent(events, identity, "quota_spend"); + if (matchedSpend) { + matchedSpend.run = spendRun; + matchedSpend.receipt = spendEvent; + matchedSpend.admissionSource = receiptDetails.quota_spend_source; + } const completionEvent = findStepEvent(events, identity, "todo_complete"); const supersedeEvent = findStepEvent(events, identity, "todo_supersede"); const closeoutStarted = [writebackRun, writebackEvent, spendRun, spendEvent, completionEvent, supersedeEvent] @@ -1511,9 +1530,33 @@ export async function readQuotaSettlement(value: unknown): Promise { return await withOwner( request.owner, async () => { + const matchedSpend: MatchedSpendRecords = {run: null, receipt: null}; const result = readQuotaSettlementFromRequest(request, - await readQuotaSettlementSnapshot(request.runtime_root, request.goal_id)); + await readQuotaSettlementSnapshot(request.runtime_root, request.goal_id), true, matchedSpend); const identity = jsonObject(jsonObject(jsonObject(result.identity)?.result)?.value); + const receipt = spendReceiptTerms(matchedSpend.receipt); + // Only exact readback opens its matched debit artifact. Check its + // identity even when the receipt is missing or omits billing terms. + // Snapshot readers for history and guard discovery use the bounded index. + const retained = matchedSpend.run !== null + ? await readQuotaSpendEventFromRun(join(request.runtime_root, "goals", request.goal_id, "runs"), + matchedSpend.run, request.goal_id) + : null; + const ledger = {slots: retained?.slots ?? undefined, source: retained?.source ?? undefined}; + let conflict = spendTermsConflict(ledger, receipt); + if (retained === null && matchedSpend.run !== null && matchedSpend.receipt !== null) { + // An index row identifies the original debit, but does not provide its + // terms. Check explicit receipt terms against the matching admission. + const source = matchedSpend.admissionSource ?? undefined; + conflict = spendTermsConflict( + {slots: ORIGINAL_TURN_SPEND_SLOTS, source}, receipt, "the original Turn admission", + ) ?? (source === undefined && receipt.source !== undefined + ? "the original Turn admission has no verifiable billing source for the quota spend receipt" + : null); + } + if (conflict !== null && spendRequest === null) { + throw new EffectRuntimeRequestError(conflict, "malformed_settlement_state"); + } if (identity && request.refresh_retry && jsonObject(result.refresh_recovery)?.decision !== "reject" && isTurnScopedSettlementOutcome(request.refresh_retry.delivery_outcome, @@ -1529,49 +1572,68 @@ export async function readQuotaSettlement(value: unknown): Promise { // only its receipts and whether a spend Run already needs receipt repair. const settlement = requireJsonObject(result.settlement, "quota settlement bundle"); const outcome = requireJsonObject(settlement.result, "quota settlement result"); - const spendRun = jsonObject(result.spend_run); - const spendRejection = spendRequest && identity - ? await originalSpendRejection(request, result, spendRequest, spendRun) - : null; + const rejection = conflict === null + ? spendRequest && identity ? originalSpendRejection(result, spendRequest, ledger, receipt) : null + : rejectedSpend(conflict); return { schema_version: result.schema_version, found: true, identity: result.identity, settlement: {...settlement, result: {...outcome, value: null}}, progress: result.progress, - spend_run_present: spendRun !== null, - ...(spendRejection === null ? {} : {spend_request_rejection: bundle(spendRejection)}), + spend_run_present: matchedSpend.run !== null, + ...(rejection === null ? {} : {spend_request_rejection: bundle(rejection)}), }; }, ); } -async function originalSpendRejection( - request: ReadbackRequest, readback: JsonObject, proposed: JsonObject, - spendRun: JsonObject | null, -): Promise | null> { +function spendReceiptTerms(event: JsonObject | null): SpendTerms { + const {slots, source} = event === null ? {} : details(event); + // Older rollout envelopes and repaired receipts can omit billing terms. + return { + slots: slots == null || slots === "" ? undefined : slots, + source: source == null || source === "" ? undefined : source, + }; +} + +function spendTermsConflict( + recorded: SpendTerms, + receipt: SpendTerms, + description = "the recorded debit", +): string | null { + for (const field of ["slots", "source"] as const) { + if (recorded[field] !== undefined && receipt[field] !== undefined && recorded[field] !== receipt[field]) { + return `${description} and quota spend receipt disagree on ${field}`; + } + } + return null; +} + +function rejectedSpend(reason: string): SettlementResult { + return settlementFailed({kind: "quota_spend_rejected", step_kind: "quota_spend", reason}); +} + +function originalSpendRejection( + readback: JsonObject, proposed: JsonObject, + ledger: SpendTerms, receipt: SpendTerms, +): SettlementResult | null { const progress = requireJsonObject(readback.progress, "quota settlement progress"); const guard = jsonObject(readback.heartbeat_receipt); const authorizedSource = guard ? details(guard).quota_spend_source : null; // Older ledgers can have a rollout receipt but no readable artifact. Keep // their receipt facts available and never repair an already mismatched debit. const spendEvent = jsonObject(readback.spend_event); - const receipt = spendEvent ? details(spendEvent) : {}; - const retained = spendRun === null ? null : await readQuotaSpendEventFromRun( - join(request.runtime_root, "goals", request.goal_id, "runs"), spendRun, request.goal_id, - ); - const recordedSlots = retained?.slots ?? (receipt.slots === "" ? undefined : receipt.slots); - const recordedSource = retained?.source ?? (receipt.source === "" ? undefined : receipt.source); + const spendRun = jsonObject(readback.spend_run); + const recordedSlots = ledger.slots ?? receipt.slots; + const recordedSource = ledger.source ?? receipt.source; const fallbackSource = recordedSource === "heartbeat" || recordedSource === "visible-goal" ? recordedSource : progress.quota_spend_source; const source = authorizedSource ?? fallbackSource; // Every admitted CLI settlement plan authorizes one slot and the original // source. The same check gates initial spend, receipt repair, and replay. - if (proposed.slots !== 1 || proposed.source !== source) { - return settlementFailed({ - kind: "quota_spend_rejected", step_kind: "quota_spend", - reason: `original Turn settlement requires --slots 1 --source ${String(source)}`, - }); + if (proposed.slots !== ORIGINAL_TURN_SPEND_SLOTS || proposed.source !== source) { + return rejectedSpend(`original Turn settlement requires --slots ${ORIGINAL_TURN_SPEND_SLOTS} --source ${String(source)}`); } const slotsConflict = recordedSlots !== undefined && recordedSlots !== proposed.slots; const sourceConflict = recordedSource !== undefined && recordedSource !== source; @@ -1579,10 +1641,7 @@ async function originalSpendRejection( // debit needs the original charge facts before we can certify its receipt. const unverifiableRepair = spendRun !== null && spendEvent === null && (recordedSlots === undefined || recordedSource === undefined); - return slotsConflict || sourceConflict || unverifiableRepair ? settlementFailed({ - kind: "quota_spend_rejected", step_kind: "quota_spend", - reason: unverifiableRepair + return slotsConflict || sourceConflict || unverifiableRepair ? rejectedSpend(unverifiableRepair ? "the recorded debit has no verifiable billing terms for receipt repair" - : "the recorded debit differs from the original Turn settlement plan", - }) : null; + : "the recorded debit differs from the original Turn settlement plan") : null; } diff --git a/loopx/control_plane/quota/should_run.py b/loopx/control_plane/quota/should_run.py index 1f85af9df8..962f4540e7 100644 --- a/loopx/control_plane/quota/should_run.py +++ b/loopx/control_plane/quota/should_run.py @@ -194,6 +194,7 @@ def _attach_quota_should_run_envelope( def _app_scheduler_state_read_observer( direct_sources: dict[str, dict[str, Any]], + observe_state: Callable[[Mapping[str, Any]], None] | None, ) -> Callable[[Mapping[str, Any]], None]: def record(readback: Mapping[str, Any]) -> None: previous = direct_sources.get("app_scheduler_state", {}) @@ -211,6 +212,8 @@ def record(readback: Mapping[str, Any]) -> None: item_count=item_count, unreadable_count=unreadable_count, ) + if observe_state is not None: + observe_state(readback) return record @@ -577,6 +580,7 @@ def build_quota_should_run( goal_ref: Mapping[str, object] | None = None, authoritative_fallback_todo_items: FallbackTodoSource = None, replan_explore_reader: Callable[[str, str | None], Mapping[str, Any] | None] | None = None, + app_scheduler_state_observer: Callable[[Mapping[str, Any]], None] | None = None, ) -> dict[str, Any]: safe_goal_id = str(goal_id or "").strip() direct_sources: dict[str, dict[str, Any]] = {} @@ -687,7 +691,7 @@ def build_quota_should_run( runtime_root=runtime_root, goal_ref=goal_ref, scheduler_state_read_observer=_app_scheduler_state_read_observer( - direct_sources + direct_sources, app_scheduler_state_observer, ), ) _apply_missing_state_health_guidance( diff --git a/loopx/control_plane/quota/unsettled_host_turn.py b/loopx/control_plane/quota/unsettled_host_turn.py index 44ee6885f7..eda16f9d2c 100644 --- a/loopx/control_plane/quota/unsettled_host_turn.py +++ b/loopx/control_plane/quota/unsettled_host_turn.py @@ -22,6 +22,7 @@ EffectRuntimeResponseAmbiguous, effect_runtime_result, ) +from ..scheduler.arbitration import SchedulerArbitration from ..scheduler.execution_context import SchedulerExecutionContextResolution from ..agents.agent_scope_frontier import AgentScopeFrontierAction from ..todos.todo_semantics import todo_item_task_class @@ -410,6 +411,7 @@ def apply_unsettled_host_turn_recovery_if_required( agent_id: str | None, current_turn_instance_id: str | None, available_capabilities: list[str] | None, + project_scheduler_hint: Callable[..., dict[str, Any]], scheduler_execution_context: ( Mapping[str, Any] | SchedulerExecutionContextResolution | None ), @@ -432,9 +434,14 @@ def apply_unsettled_host_turn_recovery_if_required( ) if verdict is None: return False - return _apply_recovery_projection(payload, verdict=verdict, registry_path=registry_path, runtime_root=runtime_root, + arbitration = _apply_recovery_projection( + payload, verdict=verdict, registry_path=registry_path, runtime_root=runtime_root, current_turn_instance_id=current_turn_instance_id, available_capabilities=available_capabilities, - scheduler_execution_context=scheduler_execution_context) + scheduler_execution_context=scheduler_execution_context, + ) + if verdict["recovery"].get("state") == "attention_required": + payload["scheduler_hint"] = project_scheduler_hint(payload, arbitration=arbitration) + return True def apply_receipt_bound_wait_recovery( @@ -525,7 +532,7 @@ def _apply_recovery_projection( payload: dict[str, Any], *, verdict: Mapping[str, Any], registry_path: Path, runtime_root: Path, current_turn_instance_id: str | None, available_capabilities: list[str] | None, scheduler_execution_context: Mapping[str, Any] | SchedulerExecutionContextResolution | None, -) -> bool: +) -> SchedulerArbitration: recovery = verdict["recovery"] obligation = verdict["obligation"] attention = recovery.get("state") == "attention_required" @@ -614,11 +621,4 @@ def _apply_recovery_projection( cli_channel["spend_allowed_now"] = obligation.get("spend_allowed_now") is True cli_channel["recovery_ref"] = "$.unsettled_host_turn_recovery" payload["interaction_contract"] = interaction_contract - if attention: - from ..scheduler.scheduler_hint import build_scheduler_hint - - payload["scheduler_hint"] = build_scheduler_hint( - payload, arbitration=arbitration, scheduler_execution_context=scheduler_execution_context, - available_capabilities=available_capabilities, - ) - return True + return arbitration diff --git a/loopx/control_plane/turn_driver/codex_cli.py b/loopx/control_plane/turn_driver/codex_cli.py index dd4d453b5e..e2433070b0 100644 --- a/loopx/control_plane/turn_driver/codex_cli.py +++ b/loopx/control_plane/turn_driver/codex_cli.py @@ -58,6 +58,7 @@ CODEX_STDIO_MCP_SERVER_SCHEMA_VERSION = "codex_stdio_mcp_server_v0" CODEX_CLI_RESULT_KINDS = ( "validated_progress", + "validated_completion", "repair_required", "replan_required", "user_action_required", @@ -395,7 +396,7 @@ def _render_prompt(request: Mapping[str, Any], *, checked: Mapping[str, Any] | N ("Use task_context for the admitted work. Its interaction_contract.agent_channel carries work_context, required_reads and primary_action; the outer adapter retains the complete original source and Core effect instructions. " if complete else ("Use turn_authority as the verified authority for this Turn. " if staged else "Use the TurnEnvelope as the source of truth. ")) + "Read work_context and remaining required_reads before primary_action; do not repeat fulfilled reads; perform work only when its contract allows it. The selected Todo summary is not its full requirements, and completing that work does not prove Goal completion.", "Do not write LoopX state, spend quota, or apply scheduler changes; the adapter owns those effects.", - "Return only the schema-constrained result. For validated_progress, repair_required, or replan_required, fill every material field with public-safe evidence.", + "Return only the schema-constrained result. Use validated_completion when the selected Todo's full requirements are met; the adapter runs its declared completion validation and durably completes it before accountable writeback. Use validated_progress for partial work; progress alone never completes a Todo. Neither result proves Goal closure. For validated_progress, validated_completion, repair_required, or replan_required, fill every material field with public-safe evidence.", "For those material results, preserve the actual delivery_batch_scale and delivery_outcome selected from the signed execution_profile's work and outcome requirements. Return work_evidence and validation_evidence as separate non-empty arrays of short public-safe strings; name the artifact or observed work in the first and the independent command or readback in the second. For legacy requests without execution_profile, use null when evidence is not supplied. For stop results, set both evidence fields to null.", "For those material results, set path_delta_mode=material_replan and result_kind=replan_required only when this Turn changes a prior assumption, route, scope, acceptance rule, or stops prior work; then provide a complete bounded agent vision packet with goal_path_delta_v0 in agent_vision_json and leave vision_unchanged_reason empty.", "For routine continuation, retry, successor creation, or no-change replanning, explicitly set path_delta_mode=unchanged. Reuse the current checkpoint with vision_unchanged_reason and empty agent_vision_json, or record new progress/completion evidence in a fresh agent_vision_json packet with path_delta.outcome continue or no_change and empty vision_unchanged_reason.", diff --git a/loopx/control_plane/work_items/interaction_contract.py b/loopx/control_plane/work_items/interaction_contract.py index 73070b5732..5bcaf1e388 100644 --- a/loopx/control_plane/work_items/interaction_contract.py +++ b/loopx/control_plane/work_items/interaction_contract.py @@ -47,7 +47,7 @@ normalize_todo_replan_obligation_id, ) from ..todos.todo_semantics import todo_item_task_class -from ..todos.user_gate import has_only_open_user_actions, open_todo_count +from ..todos.user_gate import has_only_open_user_actions, open_todo_count, open_user_gate_todo_items from ..todos.write_hint import build_capability_resolution_writeback_actions from .autonomous_replan_obligation import ( build_autonomous_replan_cli_actions, @@ -463,6 +463,17 @@ def _due_monitor_cli_actions( ] +# Observation is evidence, never an owner decision: no completion command is projected. +# Keep the text free of quote characters; consumers shlex-split every action. +MONITOR_USER_GATE_NOTICE = ( + "A Monitor observation never completes or approves a User gate. Do not pass the id or claim proof " + "of this Turn to a gate completion, and do not attach evidence to a gate through todo update. " + "Record the observation with the poll below and yield. Work that may continue on an observed change " + "without an owner decision belongs in the successor of that poll (--material-change with " + "--next-agent-todo), not behind a User gate." +) + + def _turn_scoped_cli_settlement_context( payload: dict[str, Any], *, @@ -1436,6 +1447,10 @@ def _build_interaction_cli_channel( work_lane = payload.get("work_lane_contract") if not isinstance(work_lane, Mapping): work_lane = {} + if primary_monitor is not None and turn_instance_id and open_user_gate_todo_items(payload.get("user_todo_summary")): + # Keep the Monitor instruction first; the notice precedes the poll it qualifies. + actions = channel["next_cli_actions"] + channel["next_cli_actions"] = [*actions[:1], MONITOR_USER_GATE_NOTICE, *actions[1:]] auxiliary_monitor = work_lane.get("auxiliary_monitor_poll") if not isinstance(auxiliary_monitor, Mapping): auxiliary_monitor = None diff --git a/loopx/control_plane/work_items/replan_semantics.ts b/loopx/control_plane/work_items/replan_semantics.ts index 748e4ca126..69aaef6387 100644 --- a/loopx/control_plane/work_items/replan_semantics.ts +++ b/loopx/control_plane/work_items/replan_semantics.ts @@ -35,7 +35,7 @@ const ACCEPTANCE_HOLD_TRIGGERS = new Set(["goal_acceptance_stale", "goal_accepta // These review kinds carry no evidence window, capability or lifecycle duty. // A vision gap dominates the ordinary outcome gap's discharge policy, so the // same scalar receipt can retain a mixed review after its work links change. -const SCALAR_REVIEW_KINDS = new Set(["goal_outcome_uncovered", "vision_acceptance_gap"]); +const SCALAR_REVIEW_KINDS = new Set(["goal_outcome_uncovered", "vision_acceptance_gap", "monitor_no_change_streak"]); const EXTERNAL_REVIEW_TRIGGERS = new Set(["external_progress_review_drift"]); export const FRESH_PATH_DISPOSITIONS = new Set(["continue", "no_change", "replan"]); const PROGRESS_CLI_ARGS = "--progress-result-class --progress-surface-id --progress-hypothesis-id --progress-probe-kind --progress-evidence-id "; @@ -549,7 +549,7 @@ export function projectReplanSemantics(value: unknown): JsonObject { if (request.operation === "receipt_review_source") { const kinds = triggerKinds(obligation); const triggers = Array.isArray(obligation.triggers) ? obligation.triggers : []; - // Plain vision/outcome reviews share a scalar receipt source. Other mixed, + // Plain frontier reviews share a scalar receipt source. Other mixed, // restricted, lifecycle and capability obligations must retain their // source-specific evidence and discharge rules through their own owners. return {review_source: obligation.schema_version === "autonomous_replan_obligation_v0" && @@ -563,7 +563,8 @@ export function projectReplanSemantics(value: unknown): JsonObject { obligation.progress_baseline == null && obligation.progress_window == null && triggers.every(trigger => object(trigger).progress_baseline == null && object(trigger).progress_window == null) && typeof obligation.stop_condition === "string" && obligation.stop_condition.trim() && obligation.stop_condition.length <= 240 - ? {kind: kinds.includes("vision_acceptance_gap") ? "vision_acceptance_gap" : "goal_outcome_uncovered", + // Fixed precedence keeps the selected kind independent of trigger order. + ? {kind: ["vision_acceptance_gap", "goal_outcome_uncovered", "monitor_no_change_streak"].find(kind => kinds.includes(kind)), stop_condition: obligation.stop_condition} : null}; } if (request.operation === "transition_eligibility") { diff --git a/loopx/quota.py b/loopx/quota.py index 68f27bd509..0c8bbfbfb6 100644 --- a/loopx/quota.py +++ b/loopx/quota.py @@ -1088,6 +1088,7 @@ def build_quota_should_run( workspace_path: Path | None = None, goal_ref: Mapping[str, object] | None = None, authoritative_fallback_todo_items: FallbackTodoSource = None, + app_scheduler_state_observer: Callable[[Mapping[str, Any]], None] | None = None, ) -> dict[str, Any]: from .control_plane.quota.should_run import ( build_quota_should_run as _build_quota_should_run, @@ -1134,6 +1135,7 @@ def read_explore_context(read_goal_id: str, read_agent_id: str | None) -> dict[s goal_ref=goal_ref, authoritative_fallback_todo_items=authoritative_fallback_todo_items, replan_explore_reader=read_explore_context, + app_scheduler_state_observer=app_scheduler_state_observer, ) @@ -1835,12 +1837,15 @@ def spend_quota_slot( ), } if settlement_readback.spend_run_present: + # The precheck verified these terms against the retained debit. + # Carry them to the existing receipt writer without charging again. return { "ok": True, "mode": "spend-slot", "dry_run": not execute, "appended": False, "receipt_repair_required": bool(execute), + **spend_request, "goal_id": safe_goal_id, "agent_id": identity.agent_id, "todo_id": identity.todo_id, diff --git a/packages/dsh-loopx-plugin/src/driver.ts b/packages/dsh-loopx-plugin/src/driver.ts index 5f144538cf..bdafac88a7 100644 --- a/packages/dsh-loopx-plugin/src/driver.ts +++ b/packages/dsh-loopx-plugin/src/driver.ts @@ -1011,6 +1011,7 @@ export class LoopXContinuationDriver { private async evaluate(state: DriverState): Promise { if (!this.ready(state)) return false + const session = state.agent.session const startedAt = performance.now() let stage: AdmissionStage = 'maintenance-slot' let admissionSignal: AbortSignal | undefined @@ -1056,6 +1057,9 @@ export class LoopXContinuationDriver { if (!waitingForIdle && state.controller === controller) state.controller = undefined } + // Cancellation can land after the maintenance callback has built its plan. + if (admissionSignal?.aborted || state.agent.session !== session) return false + if (plan.kind === 'wait') { if (plan.schedulerToken !== undefined) state.schedulerToken = plan.schedulerToken if (plan.unchangedPolls !== undefined) state.unchangedPolls = plan.unchangedPolls diff --git a/packages/dsh-loopx-plugin/tests/driver.spec.ts b/packages/dsh-loopx-plugin/tests/driver.spec.ts index 19e11c9443..537e78776d 100644 --- a/packages/dsh-loopx-plugin/tests/driver.spec.ts +++ b/packages/dsh-loopx-plugin/tests/driver.spec.ts @@ -2340,6 +2340,49 @@ describe('GoalBar exact-Session Driver bridge', () => { await driver.dispose() }) + it.each([ + { plan: 'queue', shouldRun: true, interruption: 'Pause' }, + { plan: 'wait', shouldRun: false, interruption: 'Pause' }, + { plan: 'queue', shouldRun: true, interruption: 'maintenance cancellation' }, + { plan: 'wait', shouldRun: false, interruption: 'maintenance cancellation' }, + ])('discards a $plan plan returned after $interruption', async ({ shouldRun, interruption }) => { + const fixture = runnerFixture({ shouldRun, waitMinutes: 5 }) + const clock = schedulerClock() + const host = fakeAgent() + const driver = new LoopXContinuationDriver({ + runner: fixture.runner, + isLiveAgent: () => true, + retryDelaysMs: [0, 0], + clock, + }) + const capture = { agent: host.agent, session: host.agent.session } + vi.spyOn(host.agent, 'runMaintenance').mockImplementationOnce(async task => { + const maintenance = new AbortController() + const plan = await task(maintenance.signal) + if (interruption === 'Pause') { + await expect(driver.cancelQueued(capture)).resolves.toEqual({ kind: 'applied' }) + } else { + maintenance.abort() + } + return plan + }) + try { + driver.observeAgent(host.agent) + host.appendEvent(userSkillInvocationEvent()) + const interrupted = await driver.evaluateActivatedSession(capture) + expect(host.nextTurn).toHaveLength(0) + expect(clock.timers).toHaveLength(0) + expect(interrupted).toEqual({ kind: 'unavailable', reason: 'evaluation_unavailable' }) + + await expect(driver.evaluateActivatedSession(capture)).resolves.toEqual({ kind: 'applied' }) + expect(fixture.quotaCalls).toHaveLength(2) + expect(host.nextTurn).toHaveLength(shouldRun ? 1 : 0) + expect(clock.timers).toHaveLength(shouldRun ? 0 : 1) + } finally { + await driver.dispose() + } + }) + it('Pause removes only the captured Session scheduled continuation', async () => { const fixture = runnerFixture({ shouldRun: false, waitMinutes: 5 }) const first = fakeAgent() diff --git a/skills/loopx-self-repair/references/repair-patterns.md b/skills/loopx-self-repair/references/repair-patterns.md index 99e2de3f79..9a8765a4ba 100644 --- a/skills/loopx-self-repair/references/repair-patterns.md +++ b/skills/loopx-self-repair/references/repair-patterns.md @@ -126,7 +126,7 @@ teaches a reusable control-plane lesson. | `normal_no_user_todo_misflagged` | `action_required=false` and user open count is zero, but the agent reports a state projection fault. | quota payload and user todo summary. | Rule over-applied the "todo not projected" fallback. | Allow "无用户待办/无需通知"; update heartbeat/prompt smoke if needed. | | `todo_no_followup_global_terminal_overreach` | A completed scoped Todo records `done + no_followup`, but `refresh-state --repair-delta-kind no_followup` becomes `replan_noop` because unrelated advancement or long-running monitor Todos remain open. | rejected repair-delta claim, complete Agent Todo projection, scoped Todo metadata, and the latest local no-follow-up completion. | The repair-delta validator reused Goal-level terminal-frontier completeness to validate a Todo-local closure. | Keep Goal `terminal_no_followup` strict, but validate repair-delta no-follow-up against the latest scoped completed Todo in a complete parser projection; reject other-agent, continuation-required, malformed, or truncated evidence and keep the emitted Todo evidence bounded. Cover the real CLI path with an open monitor regression. | | `terminal_settlement_ordering_gap` | Final Todo completion succeeds, then the matching refresh or spend is rejected because the Goal is already terminal; different hosts may expose different closeout orders or retry the material effect. | Typed settlement plan and effect identity, Todo completion event, accountable refresh and spend receipts, Turn journal phase prefix, and host task-body projection. | Final `no_followup` was modeled like ordinary pre-settlement Todo completion even though it mutates Goal terminal state and activates the strict no-more-spend guard. | Keep Goal terminal guards strict. Treat ordinary successor completion as Todo lifecycle work, but make final `no_followup` a conditional post-spend terminal closeout under the same effect identity. Require matching writeback and spend receipts at the mutation boundary, prove the closeout event itself carries `no_followup`, and journal it so closeout-only retry cannot repeat writeback or spend. Cover CLI, visible Goal, Codex App, Turn driver, adapter order, failure short-circuit, and replay. | -| `receipt_bound_lifecycle_source_gap` | A lifecycle review is admitted, then closing the original Todo through its settled Turn makes same-review reentry fail with an identity conflict; a blocked refresh may commit without a qualified replan ACK. | Exact original completion identity and closeout receipt, admitted review identity/time, canonical Todo readback, typed blocked observation and writeback/debit receipts. | Lifecycle reconstruction recognized only the completion-key reentry form; writeback could treat a missing selected source like an unrelated future obligation. | Reuse the canonical completion-recovery vocabulary for both legal paths and retain exact actor, identity and timestamp checks. Revalidate every selected replan through the shared owner before writeback. For an already committed unqualified blocked observation with no debit, permit only an exact additive checkpoint requalification; preserve the original run and all receipt history, reject payload changes or later superseding vision, and never infer acceptance or erase a committed debit. | +| `receipt_bound_lifecycle_source_gap` | An admitted lifecycle or Monitor review loses its live source after Todo closeout, so same-Turn guard reentry or refresh fails with an identity conflict; a blocked refresh may commit without a qualified replan ACK. | Exact original completion identity and closeout receipt, admitted review identity/time, canonical Todo readback, typed observation and writeback/debit receipts. | Lifecycle reconstruction recognized only the completion-key reentry form; scalar receipts omitted Monitor review sources; writeback could treat a missing selected source like an unrelated future obligation. | Reuse the canonical completion-recovery vocabulary for both legal paths and retain exact actor, identity and timestamp checks. Retain unqualified Monitor review kinds and their stop conditions through the existing scalar receipt source; lifecycle closeout alone never discharges the required semantic review or authorizes debit. Revalidate every selected replan through the shared owner before writeback. For an already committed unqualified blocked observation with no debit, permit only an exact additive checkpoint requalification; preserve the original run and all receipt history, reject payload changes or later superseding vision, and never infer acceptance or erase a committed debit. | | `host_settlement_source_projection_gap` | A visible Goal host packet requires `source=visible-goal` without an explicit Turn receipt, but the nested interaction contract tells the agent to spend as `heartbeat` and binds the currently selected Todo. The command can fail after Todo completion or misattribute interactive usage. | Host runtime profile, canonical host quota commands, interaction-contract CLI actions, selected Todo lifecycle order, and the failed spend preview. | The generic interaction projection hard-coded heartbeat accounting and selected-Todo binding instead of deriving the accounting route from the scheduler execution context. | Route spend actions from the typed runtime profile. Native Goal runtimes preserve `source=visible-goal` and host-level attribution; heartbeat automation keeps its Todo- and Turn-bound settlement plan. Cover the exact CLI actions for each native Goal profile and retain the heartbeat regression. | | `visible_goal_settlement_replan_attribution_gap` | An accountable visible-Goal delivery writeback moves the frontier into capability or control-plane repair; the following unbound spend is recorded against the new repair state with no Todo, Turn, effect, or delivery-workspace causality, and an exact retry cannot replay the committed effect. | Newest same-Agent accountable run, its complete nested settlement identity, top-level Todo/Turn fields, original heartbeat receipt, delivery-workspace causality, current quota frontier, spend classification, and retry run count. | The unbound visible-Goal path used the mutable current frontier as settlement authority and did not recover the completed delivery identity; commit classification also represented eligible, repair, and delivery semantics as overlapping booleans, allowing a later repair projection to overwrite attribution. | Allow unbound recovery only for visible-Goal spend from the newest fully typed same-Agent accountable run; require its top-level and nested identities to match the TypeScript-generated settlement payload and revalidate the original heartbeat receipt. Preserve Todo/Turn/effect/workspace causality, write the CLI receipt under the recovered Turn, and reduce commit semantics to one closed TypeScript disposition where proven delivery completion precedes later frontier repair. Cover missing/legacy identity, capability and control-plane replan, and exact one-run replay. | | `runtime_capability_user_gate_overreach` | A missing launcher/runtime capability such as `network` becomes `capability_gate.action=ask_owner`, `requires_user_action=true`, or a user notification even though no credential, protected access, or production authority is needed. | quota `capability_gate`, heartbeat `--available-capability` inputs, launcher/runtime observations, interaction contract user channel. | Capability availability and permission authority were collapsed into one owner-held class. | Keep runtime provisioning gaps in the agent repair lane, require truthful runtime observation before declaring availability, and reserve `ask_owner` for credentials or protected/production authority. Cover all-blocked and mixed-capability cases with capability-gate and monitor smokes. | diff --git a/tests/control_plane/test_monitor_gate_handoff.py b/tests/control_plane/test_monitor_gate_handoff.py new file mode 100644 index 0000000000..541c21620e --- /dev/null +++ b/tests/control_plane/test_monitor_gate_handoff.py @@ -0,0 +1,104 @@ +"""A due Monitor is told that its observation cannot complete a User gate.""" +from __future__ import annotations + +import json + +import pytest +from canonical_authority_fixture import isolate_sqlite_runtime + +from loopx.control_plane.testing.canary_harness import run_json_cli, run_json_cli_result +from loopx.control_plane.work_items.interaction_contract import MONITOR_USER_GATE_NOTICE +from tests.control_plane.test_monitor_followthrough_contract import ( + AGENT_ID, + GOAL_ID, + _write_fixture, +) + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_monitor_observation_never_projects_gate_completion(tmp_path, tmp_path_factory, monkeypatch, provider): + isolate_sqlite_runtime(tmp_path, monkeypatch) + registry, runtime, state = _write_fixture(tmp_path_factory.mktemp("g") / "a b", provider=provider, + handoff_mode="hard_lease") + + def cli(*args): + return run_json_cli(*args, registry_path=registry, runtime_root=runtime) + + def rejected(*args): + rc, result = run_json_cli_result(*args, registry_path=registry, runtime_root=runtime) + assert rc == 1 and result["ok"] is False, result + return result + + def actions(guard): + return guard["interaction_contract"]["cli_channel"]["next_cli_actions"] + + actor = ("--goal-id", GOAL_ID, "--agent-id", AGENT_ID) + scope = (*actor, "--runtime-profile", "generic_cli") + monitor_args = ("todo", "add", "--goal-id", GOAL_ID, "--role", "agent", "--task-class", "continuous_monitor", + "--claimed-by", AGENT_ID, "--target-key", "prices-input", "--cadence", "1m", + "--expires-at", "2099-01-01T00:00:00Z", "--next-due-at", "2000-01-01T00:00:00Z", + "--text", "Observe inputs/prices.json once when due.") + monitor = cli(*monitor_args)["todo_id"] + claim = cli("todo", "claim", *actor, "--todo-id", monitor, "--claimed-by", AGENT_ID) + # Without an open User gate the projection is unchanged. + plain = cli("quota", "should-run", *scope, "--turn-instance-id", "no-gate") + assert plain["selected_todo"]["todo_id"] == monitor + assert MONITOR_USER_GATE_NOTICE not in actions(plain) + + work = cli("todo", "add", "--goal-id", GOAL_ID, "--role", "agent", "--task-class", "advancement_task", + "--claimed-by", AGENT_ID, "--status", "blocked", + "--text", "Compute value from the supplied prices.")["todo_id"] + gate = cli("todo", "add", *actor, "--role", "user", "--task-class", "user_gate", + "--unblocks-todo-id", work, "--text", "Provide inputs/prices.json.")["todo_id"] + # An unrelated owner decision for the same agent must never receive an approval recipe either. + release = cli("todo", "add", *actor, "--role", "user", "--task-class", "user_gate", + "--decision-scope", "production:goal:release", "--text", "Approve the release.")["todo_id"] + + guard = cli("quota", "should-run", *scope, "--turn-instance-id", "no-gate") + assert guard["selected_todo"]["todo_id"] == monitor + projected = actions(guard) + assert "condition unmet" in projected[0] + assert projected[1] == MONITOR_USER_GATE_NOTICE + # The notice never points an observation at a gate, linked or unrelated. + assert "pending user action" not in MONITOR_USER_GATE_NOTICE and "report" not in MONITOR_USER_GATE_NOTICE + commands = [action for action in projected if action.startswith("loopx ")] + assert len(commands) == 1 and " quota monitor-poll " in commands[0], projected + assert not any("todo complete" in action or "--decision-outcome" in action for action in projected) + assert gate not in " ".join(projected) and release not in " ".join(projected) + + price_file = state.parents[3] / "inputs" / "prices.json" + price_file.parent.mkdir() + price_file.write_text('{"A":199}', encoding="utf-8") + poll = cli("quota", "monitor-poll", *scope, "--turn-instance-id", "no-gate", "--todo-id", monitor, + "--target-key", "prices-input", "--result-hash", "prices-present", "--material-change", + "--use-current-task-lease", "--execute") + assert poll["material_change"] is True + assert poll["turn_continuation"]["current_turn_settled"] is True + # The two attempts the notice rules out stay rejected by their existing owners. + denied = rejected("todo", "complete", *actor, "--role", "user", "--todo-id", gate, + "--turn-instance-id", "no-gate", "--decision-outcome", "approve", "--evidence", "Actual input observed") + assert "settlement binding does not match the original quota guard" in denied["error"] + denied = rejected("todo", "update", *actor, "--todo-id", gate, "--evidence", "Actual input observed") + assert denied["error_code"] == "handoff_mode_requires_lease" + rows = {row["todo_id"]: row for row in cli("todo", "list", "--goal-id", GOAL_ID)["todos"]} + assert rows[gate]["status"] == "open" and rows[release]["status"] == "open" + assert rows[work]["status"] == "blocked" + + # A later unchanged check still carries the notice and still offers no completion. + cli("todo", "update", *actor, "--todo-id", monitor, "--claim-operation-id", claim["claim_operation_id"], + "--next-due-at", "2000-01-01T00:00:00Z") + unchanged = cli("quota", "should-run", *scope, "--turn-instance-id", "unchanged-monitor") + assert unchanged["selected_todo"]["todo_id"] == monitor + assert MONITOR_USER_GATE_NOTICE in actions(unchanged) + assert not any("todo complete" in action for action in actions(unchanged)) + + # The owner's own decision remains the only route, and resumes the linked work for a fresh Turn. + completed = cli("todo", "complete", *actor, "--role", "user", "--todo-id", gate, + "--decision-outcome", "approve", "--evidence", "Owner confirmed the supplied prices") + assert completed["unblock_resume"]["state"] == "resumed" + rows = {row["todo_id"]: row for row in cli("todo", "list", "--goal-id", GOAL_ID)["todos"]} + assert rows[work]["status"] == "open" and rows[release]["status"] == "open" + index = runtime / "goals" / GOAL_ID / "runs" / "index.jsonl" + events = [json.loads(line) for line in index.read_text(encoding="utf-8").splitlines()] + assert sum(row["classification"] == "quota_monitor_poll" for row in events) == 1 + assert not any(row["classification"] == "quota_slot_spent" for row in events) diff --git a/tests/control_plane/test_monitor_replan_closeout.py b/tests/control_plane/test_monitor_replan_closeout.py new file mode 100644 index 0000000000..da2c312ce1 --- /dev/null +++ b/tests/control_plane/test_monitor_replan_closeout.py @@ -0,0 +1,94 @@ +"""Closing a Monitor cannot orphan the review Turn which admitted its closeout.""" +from __future__ import annotations + +import json +import shlex +from pathlib import Path + +import pytest + +from loopx.control_plane.testing.quota_fixtures import quota_todo_item +from test_replan_successor_guard_reentry import AGENT, GOAL, TURN, _fixture + + +def _guard(call, turn: str = TURN): + return call("quota", "should-run", "--goal-id", GOAL, "--agent-id", AGENT, + "--runtime-profile", "generic_cli", "--turn-instance-id", turn) + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("first", ["guard", "writeback"]) +def test_monitor_closeout(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, first: str) -> None: + monitor = quota_todo_item( + todo_id="todo_monitor", title="Observe the external gate", archive_state="active", + task_class="continuous_monitor", claimed_by=AGENT, action_kind="monitor", + target_key="external-gate", cadence="30m", consecutive_no_change="5", + next_due_at="2099-01-01T00:00:00Z", expires_at="2099-01-02T00:00:00Z", + ) + call, _, index = _fixture(tmp_path, monkeypatch, provider, initial_todos=[monitor]) + # Only the live Monitor's current streak admits this review, not run history. + index.write_text("") + state = tmp_path / "project" / "ACTIVE_GOAL_STATE.md" + state.write_text(state.read_text() + "\n## User Todo / Owner Review Reading Queue\n") + admitted = _guard(call) + assert admitted["decision"] == "autonomous_replan_required" + assert admitted["autonomous_replan_obligation"]["triggers"][0]["kind"] == "monitor_no_change_streak" + identity = admitted["heartbeat_receipt"]["settlement_identity"] + assert identity["binding_kind"] == "autonomous_replan" + actions = admitted["interaction_contract"]["cli_channel"]["next_cli_actions"] + + claim = call("todo", "claim", "--goal-id", GOAL, "--todo-id", monitor["todo_id"], + "--agent-id", AGENT, "--claimed-by", AGENT) + completed = call("todo", "complete", "--goal-id", GOAL, "--todo-id", monitor["todo_id"], + "--agent-id", AGENT, "--claim-operation-id", claim["claim_operation_id"], + "--evidence", "The declared external gate is covered.", "--no-follow-up") + assert completed["ok"] is True + listed = call("todo", "list", "--goal-id", GOAL, "--todo-id", monitor["todo_id"], "--include-archived") + assert listed["todo"]["status"] == "done" + + if first == "guard": + pending = _guard(call) + assert pending["heartbeat_receipt"]["settlement_identity"] == identity + assert pending["autonomous_replan_obligation"]["obligation_id"] == identity["replan_obligation_id"] + assert pending["normal_delivery_allowed"] is False + + # A lifecycle transition is not itself the semantic review or a debit. + premature = call(*shlex.split(actions[1])[1:], expected_code=1) + assert premature.get("appended") is not True + vision = tmp_path / "vision.json" + vision.write_text(json.dumps({ + "schema_version": "goal_vision_replan_contract_v0", "state": "no_followup", + "vision_patch": {"acceptance_summary": "The independently accepted source outcome is covered."}, + "path_delta": {"schema_version": "goal_path_delta_v0", "outcome": "stop", + "prior_assumption": "The external gate still needs observation.", + "observed_reality": "The declared gate is covered and no successor remains.", + "retained": ["the independently accepted source contract"], + "evidence_refs": ["evidence:gate-coverage"]}, + })) + refresh = shlex.split(actions[0])[1:] + values = { + "--progress-result-class": "no_followup", "--progress-surface-id": "gate-coverage", + "--progress-hypothesis-id": "covered-gate", "--progress-probe-kind": "closeout-review", + "--progress-evidence-id": "evidence:gate-coverage", + } + for flag, value in values.items(): + refresh[refresh.index(flag) + 1] = value + refreshed = call(*refresh, "--agent-vision-json", str(vision), + "--progress-coverage-scope-id", "external-gate", "--progress-coverage-complete", + "--no-global-sync", "--suppress-external-sinks") + assert refreshed["settlement_progress"]["state"] == "spend_required" + pending = _guard(call) + assert pending["heartbeat_receipt"]["settlement_identity"] == identity + assert pending["settlement_progress"]["state"] == "spend_required" + spend = pending["interaction_contract"]["cli_channel"]["next_cli_actions"] + assert len(spend) == 1 + spent = call(*shlex.split(spend[0])[1:]) + assert spent["appended"] is True + assert spent["settlement_progress"]["state"] == "settled" + assert call(*shlex.split(spend[0])[1:])["appended"] is False + assert _guard(call)["effective_action"] == "heartbeat_settled_skip" + next_turn = _guard(call, "turn-after-monitor-review") + assert next_turn["ok"] is True + assert next_turn["effective_action"] == "terminal_no_followup" + rows = [json.loads(line) for line in index.read_text().splitlines()] + assert sum(row.get("classification") == "quota_slot_spent" for row in rows) == 1 diff --git a/tests/control_plane/test_refresh_settle_turn.py b/tests/control_plane/test_refresh_settle_turn.py index 5c3d86da21..4941264756 100644 --- a/tests/control_plane/test_refresh_settle_turn.py +++ b/tests/control_plane/test_refresh_settle_turn.py @@ -169,6 +169,229 @@ def fail_child(argv, **kwargs): assert replay["idempotent_replay"] and _spend_run_count(runtime) == 1 +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_exact_spend_and_combined_replay_reject_disagreeing_billing_terms(tmp_path, monkeypatch, provider): + isolate_sqlite_runtime(tmp_path, monkeypatch) + project, runtime, registry = _write_fixture(tmp_path) + _initialize_fixture_authority(project, runtime, provider=provider) + binding = ["--goal-id", GOAL_ID, "--agent-id", AGENT_ID, + "--todo-id", TODO_ID, "--turn-instance-id", TURN_ID] + rc, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app", *binding, cwd=project) + assert rc == 0, guard + original = guard["heartbeat_receipt"]["settlement_identity"] + refresh = ["refresh-state", *binding, "--classification", "verified_billing_replay", + "--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress", + "--delivery-boundary", "in_flight_continuation", "--no-global-sync", "--suppress-external-sinks"] + combined = [*refresh, "--settle-turn", "--settlement-receipt"] + spend = ["quota", "spend-slot", *binding, "--execute"] + debit = [*spend, "--slots", "1", "--source", "heartbeat"] + rc, delivery = _run_cli(registry, runtime, *refresh, cwd=project) + assert rc == 0 and delivery["settlement_progress"]["state"] == "spend_required", delivery + for slots, source in (("2", "heartbeat"), ("1", "visible-goal")): + rc, wrong = _run_cli(registry, runtime, *spend, "--slots", slots, "--source", source, cwd=project) + assert rc != 0 and not wrong["ok"] and not wrong["appended"], wrong + assert _spend_run_count(runtime) == 0 + + rc, settled = _run_cli(registry, runtime, *combined, cwd=project) + assert rc == 0 and settled["settlement_progress"]["state"] == "settled", settled + assert settled["settlement_identity"]["effect_id"] == original["effect_id"] + log = runtime / "goals" / GOAL_ID / "rollout-event-log.jsonl" + index = log.parent / "runs" / "index.jsonl" + receipt_log = log.read_text(encoding="utf-8") + initial_index = index.read_bytes() + debit_record, = (record for line in initial_index.splitlines() + if (record := json.loads(line))["classification"] == "quota_slot_spent") + artifact = Path(debit_record["json_path"]) + assert artifact.resolve().is_relative_to(runtime.resolve()) + for field, bad_value in ((None, None), ("slots", 2), ("source", "visible-goal"), + ("missing", None), ("legacy_empty", ""), ("legacy_empty", None), + ("missing_artifact", None), ("missing", None), ("legacy_empty", None), + ("slots", 2), ("source", "visible-goal"), + ("unknown_admission_source", None), ("unknown_legacy_terms", None)): + rows = [json.loads(line) for line in receipt_log.splitlines()] + receipt, = (row for row in rows if row["event_kind"] == "quota_spend" and row["agent_id"] == AGENT_ID) + details = receipt["details"] + assert details["settlement_effect_id"] == original["effect_id"] + assert details["slots"] == 1 and details["source"] == "heartbeat" + if field == "missing": + details.pop("slots") + details.pop("source") + elif field == "legacy_empty": + details.update(slots=bad_value, source=bad_value) + elif field == "missing_artifact": + artifact.rename(tmp_path / "legacy-unavailable-spend.json") + elif field in ("unknown_admission_source", "unknown_legacy_terms"): + admission, = (row for row in rows if row["event_kind"] == "quota_should_run") + assert admission["details"].pop("quota_spend_source") == "heartbeat" + if field == "unknown_legacy_terms": + details.pop("slots") + details.pop("source") + elif field is not None: + details[field] = bad_value + log.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8") + before = log.read_bytes() + missing_artifact = not artifact.exists() + # A caller cannot make a conflicting source valid by supplying that + # same source on replay once only the index and receipt remain. + commands = (combined, debit) if missing_artifact else (debit, combined) + if field == "source" and missing_artifact: + commands += ((*spend, "--slots", "1", "--source", bad_value),) + for command in commands: + rc, replay = _run_cli(registry, runtime, *command, cwd=project) + if field in ("slots", "source", "unknown_admission_source"): + assert rc != 0 and not replay["ok"], replay + basis = "original Turn admission" if missing_artifact else "recorded debit" + diagnostic = replay.get("reason") or replay.get("error") or "" + expected = ("original Turn admission has no verifiable billing source" + if field == "unknown_admission_source" + else f"{basis} and quota spend receipt disagree on {field}") + assert expected in diagnostic, replay + assert replay.get("settlement_progress", {}).get("state") != "settled", replay + assert not replay.get("settlement_owed"), replay + else: + assert rc == 0 and replay["ok"] and replay["idempotent_replay"], replay + assert replay["settlement_identity"]["effect_id"] == original["effect_id"] + assert not replay.get("appended"), replay + assert log.read_bytes() == before and index.read_bytes() == initial_index + assert _heartbeat_receipt_count(runtime, TURN_ID) == 1 and _spend_run_count(runtime) == 1 + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("repair_entry", ["spend", "refresh"]) +def test_missing_spend_receipt_preserves_the_original_debit(tmp_path, monkeypatch, provider, repair_entry): + isolate_sqlite_runtime(tmp_path, monkeypatch) + project, runtime, registry = _write_fixture(tmp_path) + _initialize_fixture_authority(project, runtime, provider=provider) + binding = ["--goal-id", GOAL_ID, "--agent-id", AGENT_ID, + "--todo-id", TODO_ID, "--turn-instance-id", TURN_ID] + rc, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app", *binding, cwd=project) + assert rc == 0, guard + original = guard["heartbeat_receipt"]["settlement_identity"] + combined = ["refresh-state", *binding, "--classification", "verified_billing_repair", + "--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress", + "--delivery-boundary", "in_flight_continuation", "--no-global-sync", "--suppress-external-sinks", + "--settle-turn", "--settlement-receipt"] + debit = ["quota", "spend-slot", *binding, "--slots", "1", "--source", "heartbeat", "--execute"] + rc, settled = _run_cli(registry, runtime, *combined, cwd=project) + assert rc == 0 and settled["settlement_progress"]["state"] == "settled", settled + + log = runtime / "goals" / GOAL_ID / "rollout-event-log.jsonl" + index = log.parent / "runs" / "index.jsonl" + original_index = index.read_bytes() + debit_record, = (row for line in original_index.splitlines() + if (row := json.loads(line))["classification"] == "quota_slot_spent") + artifact = Path(debit_record["json_path"]) + original_artifact = artifact.read_bytes() + records = [json.loads(line) for line in log.read_text(encoding="utf-8").splitlines()] + receipt, = (row for row in records if row["event_kind"] == "quota_spend") + retained = [row for row in records if row is not receipt] + log.write_text("".join(json.dumps(row) + "\n" for row in retained), encoding="utf-8") + + rc, repaired = _run_cli(registry, runtime, *(debit if repair_entry == "spend" else combined), cwd=project) + assert rc == 0 and repaired["ok"], repaired + assert repaired["settlement_progress"]["state"] == "settled", repaired + assert repaired["settlement_identity"]["effect_id"] == original["effect_id"] + assert not repaired.get("settlement_owed"), repaired + recovered_log = log.read_bytes() + after = [json.loads(line) for line in recovered_log.splitlines()] + assert [row for row in after if row["event_kind"] != "quota_spend"] == retained + recovered, = (row for row in after if row["event_kind"] == "quota_spend") + assert recovered["details"]["settlement_effect_id"] == original["effect_id"] + assert recovered["details"]["slots"] == receipt["details"]["slots"] == 1 + assert recovered["details"]["source"] == receipt["details"]["source"] == "heartbeat" + for command in (debit, combined): + rc, replay = _run_cli(registry, runtime, *command, cwd=project) + assert rc == 0 and replay["ok"] and replay["idempotent_replay"], replay + assert replay["settlement_identity"]["effect_id"] == original["effect_id"] + assert not replay.get("appended") and not replay.get("settlement_owed"), replay + assert log.read_bytes() == recovered_log + assert index.read_bytes() == original_index and artifact.read_bytes() == original_artifact + assert _heartbeat_receipt_count(runtime, TURN_ID) == 1 and _spend_run_count(runtime) == 1 + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("artifact_format", ["current", "legacy"]) +def test_exact_spend_and_refresh_reject_another_turns_debit_file( + tmp_path, monkeypatch, provider, artifact_format, +): + isolate_sqlite_runtime(tmp_path, monkeypatch) + project, runtime, registry = _write_fixture(tmp_path) + _initialize_fixture_authority(project, runtime, provider=provider) + commands, identities = {}, {} + for turn in (TURN_ID, f"{TURN_ID}-second"): + binding = ["--goal-id", GOAL_ID, "--agent-id", AGENT_ID, + "--todo-id", TODO_ID, "--turn-instance-id", turn] + rc, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app", *binding, cwd=project) + assert rc == 0 and guard["should_run"], guard + identities[turn] = guard["heartbeat_receipt"]["settlement_identity"] + combined = ["refresh-state", *binding, "--classification", "verified_debit_identity", + "--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress", + "--delivery-boundary", "in_flight_continuation", "--no-global-sync", "--suppress-external-sinks", + "--settle-turn", "--settlement-receipt"] + spend = ["quota", "spend-slot", *binding, "--slots", "1", "--source", "heartbeat", "--execute", "--verbose"] + rc, settled = _run_cli(registry, runtime, *combined, cwd=project) + assert rc == 0 and settled["settlement_progress"]["state"] == "settled", settled + assert settled["settlement_identity"] == identities[turn] + commands[turn] = (combined, spend) + + log = runtime / "goals" / GOAL_ID / "rollout-event-log.jsonl" + index = log.parent / "runs" / "index.jsonl" + rows = [json.loads(line) for line in index.read_bytes().splitlines()] + target, sibling = (row for row in rows if row["classification"] == "quota_slot_spent") + assert target["settlement_identity"] == identities[TURN_ID] + assert sibling["settlement_identity"] == identities[f"{TURN_ID}-second"] + artifacts = [Path(row["json_path"]) for row in (target, sibling)] + if artifact_format == "legacy": + # The historical Python writer retained Turn/effect bindings, but no + # commit metadata. Equal timestamps must not authenticate another Turn. + for row, path in zip((target, sibling), artifacts): + record = json.loads(path.read_bytes()) + row.pop("quota_spend_commit") + record.pop("quota_spend_commit") + row["generated_at"] = record["generated_at"] = "2026-11-01T12:00:00Z" + path.write_text(json.dumps(record) + "\n", encoding="utf-8") + index.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8") + original_artifacts = [path.read_bytes() for path in artifacts] + assert [(json.loads(data)["quota_event"]["slots"], json.loads(data)["quota_event"]["source"]) + for data in original_artifacts] == [(1, "heartbeat"), (1, "heartbeat")] + original_index = index.read_bytes() + original_log = log.read_bytes() + target["json_path"] = sibling["json_path"] + index.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8") + redirected_index = index.read_bytes() + + for receipt_kind in ("current", "legacy", "missing"): + if receipt_kind != "current": + retained = [] + for line in original_log.splitlines(): + row = json.loads(line) + if row["event_kind"] == "quota_spend" and row["run_id"] == TURN_ID: + if receipt_kind == "missing": + continue + row["details"].pop("slots") + row["details"].pop("source") + retained.append(row) + log.write_text("".join(json.dumps(row) + "\n" for row in retained), encoding="utf-8") + before = log.read_bytes() + for command in commands[TURN_ID]: + rc, rejected = _run_cli(registry, runtime, *command, cwd=project) + assert rc != 0 and not rejected["ok"], rejected + reason = (rejected.get("verbose_debug", {}).get("error") + or rejected.get("error") or rejected.get("reason") or "") + assert "quota spend JSON artifact identity does not match its index row" in reason, rejected + assert rejected.get("settlement_progress", {}).get("state") != "settled", rejected + assert log.read_bytes() == before and index.read_bytes() == redirected_index + assert _spend_run_count(runtime) == 2 + + assert [path.read_bytes() for path in artifacts] == original_artifacts + index.write_bytes(original_index) + rc, recovered = _run_cli(registry, runtime, *commands[TURN_ID][0], cwd=project) + assert rc == 0 and recovered["settlement_progress"]["state"] == "settled", recovered + assert recovered["settlement_identity"] == identities[TURN_ID] + assert _spend_run_count(runtime) == 2 and index.read_bytes() == original_index + + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_receipt_capture_failure_keeps_committed_receipts_and_exact_retry(tmp_path, monkeypatch, provider): isolate_sqlite_runtime(tmp_path, monkeypatch) diff --git a/tests/control_plane/test_scheduler_fallback_hint.py b/tests/control_plane/test_scheduler_fallback_hint.py index 2c20e5eeed..1ef74314e8 100644 --- a/tests/control_plane/test_scheduler_fallback_hint.py +++ b/tests/control_plane/test_scheduler_fallback_hint.py @@ -1,8 +1,12 @@ from __future__ import annotations +import sys from datetime import datetime, timezone from pathlib import Path +import pytest + +from loopx.command_invocation import loopx_cli_route from loopx.control_plane.heartbeat.rules import ( SCHEDULER_HINT_APPLICATION_RULE, SCHEDULER_HINT_COMPACT_RULE, @@ -345,12 +349,16 @@ def test_fallback_hint_keeps_a_concrete_projected_turn(tmp_path: Path) -> None: assert fallback["route_binding"]["turn_instance_bound"] is False -def test_scheduler_followup_routes_preserve_turn_lineage(tmp_path: Path) -> None: +@pytest.mark.parametrize("launcher", [(), ("-I", "-m", "loopx.entrypoint")]) +def test_scheduler_followup_routes_preserve_turn_lineage( + tmp_path: Path, launcher: tuple[str, ...], +) -> None: payload = { "scheduler_hint": { "app_automation": { "ack_hint": { "cli_args": [ + *launcher, "quota", "scheduler-ack-current", "--execute", @@ -359,6 +367,7 @@ def test_scheduler_followup_routes_preserve_turn_lineage(tmp_path: Path) -> None }, "failure_hint": { "cli_args": [ + *launcher, "quota", "scheduler-fail-current", "--execute", @@ -369,16 +378,23 @@ def test_scheduler_followup_routes_preserve_turn_lineage(tmp_path: Path) -> None } turn_instance_id = "turn-scheduler-followup-001" - bind_scheduler_followup_cli_routes( - payload, - registry_path=tmp_path / "registry.json", - runtime_root=tmp_path / "runtime", - turn_instance_id=turn_instance_id, - ) + with loopx_cli_route(None, default_argv=[sys.executable, *launcher]): + bind_scheduler_followup_cli_routes( + payload, + registry_path=tmp_path / "registry.json", + runtime_root=tmp_path / "runtime", + turn_instance_id=turn_instance_id, + ) app_automation = payload["scheduler_hint"]["app_automation"] for hint_name in ("ack_hint", "failure_hint"): hint = app_automation[hint_name] + assert hint["cli_args"][:len(launcher) + 5] == [ + *launcher, + "--registry", str((tmp_path / "registry.json").resolve()), + "--runtime-root", str((tmp_path / "runtime").resolve()), + "quota", + ] assert hint["cli_args"][-3:] == [ "--turn-instance-id", turn_instance_id, diff --git a/tests/control_plane/test_unsettled_recovery_admission_bound.py b/tests/control_plane/test_unsettled_recovery_admission_bound.py index 2f1dad56b7..e75dfefa25 100644 --- a/tests/control_plane/test_unsettled_recovery_admission_bound.py +++ b/tests/control_plane/test_unsettled_recovery_admission_bound.py @@ -1,4 +1,5 @@ """Recovery admissions are bounded per original debt, never treated as spend.""" +import json from concurrent.futures import ThreadPoolExecutor from pathlib import Path from threading import Barrier @@ -41,13 +42,20 @@ def test_unchanged_recovery_waits_then_accepts_progress_or_owner_resume( # Repeating a committed recovery identity is not a fresh admission. rc, replay = _run_cli(registry, runtime, *guard, "--turn-instance-id", "recovery-2") assert rc == 0 and replay["should_run"] is True, replay - for turn in ("wait-1", "wait-2"): - rc, wait = _run_cli(registry, runtime, *guard, "--turn-instance-id", turn) + for turn, detail in (("wait-1", ()), ("wait-2", ("--include-detail", "scheduler"))): + rc, wait = _run_cli(registry, runtime, *guard, "--turn-instance-id", turn, *detail) assert rc == 0 and wait["should_run"] is False, wait assert wait["reason_code"] == "unsettled_recovery_admission_limit" assert wait["unsettled_host_turn_recovery"]["state"] == "attention_required" assert wait["unsettled_host_turn_recovery"]["admissions_without_progress"] == 3 - assert wait["scheduler_hint"]["action"] != "run_now" + hint = wait["scheduler_hint"] + assert hint["action"] == "backoff_waiting_for_user" + if detail: + scheduler = hint["cold_path_detail"]["local_scheduler"] + assert scheduler["recommended_interval_minutes"] > 0 + assert hint["unchanged_poll"]["limits"]["local_scheduler"] == 3 + else: + assert "cold_path_detail" not in hint channel = wait["interaction_contract"]["agent_channel"] assert channel["must_attempt"] is False and channel["delivery_allowed"] is False assert wait["interaction_contract"]["cli_channel"]["spend_allowed_now"] is False @@ -108,6 +116,89 @@ def test_unchanged_recovery_waits_then_accepts_progress_or_owner_resume( assert _spend_run_count(runtime) == 0 +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize("remote_binding", [False, True]) +def test_recovery_wait_keeps_observed_app_schedule_and_acknowledgement( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, remote_binding: bool, +) -> None: + isolate_sqlite_runtime(tmp_path, monkeypatch) + project, runtime, registry = _write_fixture(tmp_path, provider=provider) + codex_home = tmp_path / "codex" + monkeypatch.setenv("CODEX_HOME", str(codex_home)) + monkeypatch.setenv("CODEX_THREAD_ID", "recovery-thread") + automation = codex_home / "automations" / "recovery-heartbeat" / "automation.toml" + automation.parent.mkdir(parents=True) + + def set_host_rrule(rrule: str) -> None: + automation.write_text( + 'version = 1\nid = "recovery-heartbeat"\nkind = "heartbeat"\n' + 'name = "Recovery schedule fixture"\nstatus = "ACTIVE"\n' + f'prompt = "Advance `{GOAL_ID}` from active state. Agent: `{AGENT_ID}`."\n' + f'rrule = "{rrule}"\ntarget_thread_id = "recovery-thread"\n', + encoding="utf-8", + ) + + def guard(turn: str, *extra: str) -> dict: + rc, result = _run_cli( + registry, runtime, "quota", "should-run", "--codex-app", + "--goal-id", GOAL_ID, "--agent-id", AGENT_ID, + "--turn-instance-id", turn, *extra, cwd=project, + ) + assert rc == 0, result + return result + + matching_rrule = "FREQ=MINUTELY;INTERVAL=30" + short_rrule = "FREQ=MINUTELY;INTERVAL=3" + set_host_rrule(matching_rrule) + original = guard("owed-original", "--todo-id", TODO_ID) + assert original["heartbeat_receipt"]["closeout_required"] + for index in range(3): + assert guard(f"recovery-{index}")["should_run"] + + if remote_binding: + # A local recovery receipt still owns closeout after the Goal opts into + # the remote route. This fixture has no remote credentials or endpoint. + document = json.loads(registry.read_text(encoding="utf-8")) + goal, = (goal for goal in document["goals"] if goal["id"] == GOAL_ID) + goal["remote_authority"] = {"enabled": True} + registry.write_text(json.dumps(document), encoding="utf-8") + + wait = guard("matching-wait", "--include-detail", "scheduler") + assert wait["should_run"] is False + assert wait["reason_code"] == "unsettled_recovery_admission_limit" + hint = wait["scheduler_hint"] + assert hint["cold_path_detail"]["local_scheduler"]["recommended_interval_minutes"] == 30 + app = hint["app_automation"] + backoff = app["stateful_backoff"] + assert backoff["host_observation"]["current_rrule"] == matching_rrule + assert backoff["host_observation"]["status"] == "matches_recommended" + assert backoff["apply_needed"] is False and backoff["ack_needed"] is True + assert app["host_action"] == "ack_observed_rrule_without_update" + ack_hint = app["ack_hint"] + assert ack_hint["route_binding"]["turn_instance_bound"] is True + assert ack_hint["args"]["turn_instance_id"] == "matching-wait" + host_config = automation.read_bytes() + rc, ack = _run_cli(registry, runtime, *ack_hint["cli_args"], cwd=project) + assert rc == 0 and ack["scheduler_state_mutated"] is True, ack + assert automation.read_bytes() == host_config + + replay = guard("matching-wait") + assert replay["heartbeat_receipt"]["event_id"] == wait["heartbeat_receipt"]["event_id"] + app = replay["scheduler_hint"]["app_automation"] + backoff = app["stateful_backoff"] + assert backoff["apply_needed"] is False and backoff["ack_needed"] is False + assert app["host_action"] == "none" + assert "cold_path_detail" not in replay["scheduler_hint"] + + set_host_rrule(short_rrule) + drifted = guard("drifted-wait")["scheduler_hint"]["app_automation"] + assert drifted["stateful_backoff"]["host_observation"]["current_rrule"] == short_rrule + assert drifted["stateful_backoff"]["host_observation"]["status"] == "drift_detected" + assert drifted["host_action"] == "update_current_heartbeat_rrule" + assert drifted["recommended_rrule"] == matching_rrule + assert _spend_run_count(runtime) == 0 + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("intermediate_guard", [False, True]) def test_owner_resume_survives_controller_lifecycle( diff --git a/tests/control_plane_ts/quota_void_commit.test.ts b/tests/control_plane_ts/quota_void_commit.test.ts index 9f1e9592ba..b70c4abc99 100644 --- a/tests/control_plane_ts/quota_void_commit.test.ts +++ b/tests/control_plane_ts/quota_void_commit.test.ts @@ -304,6 +304,81 @@ test("target lookup falls back to the bounded legacy JSON artifact", async (t) = assert.equal((event.after as Record).spent_slots, 1); }); +test("void does not use another debit's artifact even at the same timestamp", async (t) => { + const fixture = await targetFixture(t, { inline: false, slots: 4 }); + const record = JSON.parse(await readFile(fixture.targetJsonPath, "utf8")); + const commit = { schema_version: "quota_spend_commit_receipt_v0", effect_id: "original-debit", + request_digest: `sha256:${"a".repeat(64)}` }; + const index = JSON.parse(fixture.indexContent); + const otherDebit = `${JSON.stringify({ ...record, + quota_spend_commit: { ...commit, effect_id: "another-debit" } })}\n`; + assert.equal(index.generated_at, record.generated_at); + await writeFile(fixture.targetJsonPath, otherDebit, "utf8"); + + for (const inline of [false, true]) { + const retainedIndex = `${JSON.stringify({ ...index, quota_spend_commit: commit, + ...(inline ? { quota_event: record.quota_event } : {}) })}\n`; + await writeFile(fixture.indexPath, retainedIndex, "utf8"); + await assert.rejects( + async () => evaluateQuotaVoidCommit(await request(fixture, { execute: false })), + /quota spend JSON artifact identity does not match its index row/, + ); + assert.equal(await readFile(fixture.indexPath, "utf8"), retainedIndex); + } + assert.equal(await readFile(fixture.targetJsonPath, "utf8"), otherDebit); +}); + +test("legacy debit bindings must agree in the index, artifact and event", async (t) => { + const fixture = await targetFixture(t, { inline: false }); + const record = JSON.parse(await readFile(fixture.targetJsonPath, "utf8")); + const index = JSON.parse(fixture.indexContent); + // Each identity was persisted by the legacy writer independently of its + // timestamp. Exercise both artifact locations through the actual void path. + const bindings = { + agent_id: ["agent-a", "agent-b"], + effect_ref: ["debit-a", "debit-b"], + turn_instance_id: ["turn-a", "turn-b"], + todo_id: ["todo-a", "todo-b"], + replan_obligation_id: ["replan-a", "replan-b"], + settlement_identity: [{ effect_id: "turn-a" }, { effect_id: "turn-b" }], + }; + for (const [field, [original, other]] of Object.entries(bindings)) { + for (const nested of [false, true]) { + const retainedIndex = `${JSON.stringify({ ...index, [field]: original })}\n`; + await writeFile(fixture.indexPath, retainedIndex); + const conflict = nested + ? { ...record, quota_event: { ...record.quota_event, [field]: other } } + : { ...record, [field]: other }; + const artifact = `${JSON.stringify(conflict)}\n`; + await writeFile(fixture.targetJsonPath, artifact); + await assert.rejects( + async () => evaluateQuotaVoidCommit(await request(fixture, { execute: false })), + /quota spend JSON artifact identity does not match its index row/, + ); + assert.equal(await readFile(fixture.indexPath, "utf8"), retainedIndex); + assert.equal(await readFile(fixture.targetJsonPath, "utf8"), artifact); + if (nested) { + const inlineIndex = JSON.stringify({ ...index, [field]: original, + quota_event: conflict.quota_event }); + await writeFile(fixture.indexPath, inlineIndex); + await assert.rejects( + async () => evaluateQuotaVoidCommit(await request(fixture, { execute: false })), + /quota spend JSON artifact identity does not match its index row/, + ); + assert.equal(await readFile(fixture.indexPath, "utf8"), inlineIndex); + await writeFile(fixture.indexPath, retainedIndex); + } + } + // Omitted or empty historical identity fields remain compatible. An + // explicit conflict is stronger evidence than a matching timestamp. + for (const absent of [undefined, null, ""]) { + await writeFile(fixture.targetJsonPath, JSON.stringify({ ...record, [field]: absent })); + const result = await evaluateQuotaVoidCommit(await request(fixture, { execute: false })); + assert.equal(result.status, "preview"); + } + } +}); + test("legacy index rows without goal identity use the requested goal", async (t) => { const fixture = await targetFixture(t, { inline: false }); const indexRecord = JSON.parse(fixture.indexContent) as Record; diff --git a/tests/control_plane_ts/replan_semantics.test.ts b/tests/control_plane_ts/replan_semantics.test.ts index 274fa81016..54ff89e5cd 100644 --- a/tests/control_plane_ts/replan_semantics.test.ts +++ b/tests/control_plane_ts/replan_semantics.test.ts @@ -175,7 +175,7 @@ test("an open Vision reconstruction must come from the exact scoped receipt sour } }); -test("only plain vision and outcome reviews have a scalar receipt source", () => { +test("scalar review sources exclude evidence windows and restricted duties", () => { const source = {schema_version: "autonomous_replan_obligation_v0", required: true, obligation_id: "replan-1111111111111111", agent_id: "reviewer", trigger_count: 1, stop_condition: "Stop at owner-only decisions.", @@ -228,6 +228,35 @@ test("mixed vision/outcome recovery preserves the dominant policy without discha } }); +test("Monitor lifecycle changes retain the admitted review without discharging it", () => { + const kind = "monitor_no_change_streak"; + const source = {schema_version: "autonomous_replan_obligation_v0", required: true, + obligation_id: "replan-1111111111111111", agent_id: "reviewer", trigger_count: 1, + triggers: [{kind, todo_id: "todo_monitor", run_count: 5, threshold: 5}], + stop_condition: "Stop at owner-only decisions."}; + const review = projectReplanSemantics({operation: "receipt_review_source", agent_id: "reviewer", + obligation: source}).review_source; + assert.deepEqual(review, {kind, stop_condition: source.stop_condition}); + const request = {operation: "receipt_bound_obligation", guard_scoped: true, + replay_phase: "open", selected_obligation_id: source.obligation_id, current_obligation: null, + transition_candidates: [], agent_id: "reviewer", receipt_review: review}; + const recovered = projectReplanSemantics(request).obligation as JsonObject; + assert.equal(recovered.obligation_id, source.obligation_id); + assert.equal(recovered.selection_binding, "heartbeat_receipt"); + assert.deepEqual(recovered.triggers, [{kind}]); + assert.deepEqual(requiredSemanticOutcomes(recovered), requiredSemanticOutcomes(source)); + assert.equal(projectReplanSemantics({operation: "qualify", obligation: recovered}).accepted, false); + assert.equal(projectReplanSemantics({operation: "qualify", obligation: recovered, + observation_delta: {delta_kinds: ["new_surface"]}}).accepted, true); + assert.throws(() => projectReplanSemantics({...request, guard_scoped: false}), /identity conflicts/); + for (const restriction of [{progress_baseline: {}}, {progress_window: []}, + {capability_guard: {}}, {resolution_mode: "todo_lifecycle_settlement"}, + {satisfying_semantic_outcomes: ["new_runnable_successor"]}]) { + assert.equal(projectReplanSemantics({operation: "receipt_review_source", agent_id: "reviewer", + obligation: {...source, ...restriction}}).review_source, null); + } +}); + test("a recorded outcome review survives work-link refinement without cached context or discharge", () => { const id = "replan-1111111111111111"; const request = {operation: "receipt_bound_obligation", guard_scoped: true, diff --git a/tests/test_codex_turn_completion.py b/tests/test_codex_turn_completion.py new file mode 100644 index 0000000000..614cebc3de --- /dev/null +++ b/tests/test_codex_turn_completion.py @@ -0,0 +1,512 @@ +"""Builtin completion uses the real Todo validator and canonical lease fence.""" + +from __future__ import annotations + +import contextlib +import io +import json +import sys +from pathlib import Path +from types import SimpleNamespace + +import pytest + +from loopx.capabilities.native_chat.steward_turn import StewardTurnHost +from loopx.cli import build_parser, main +from loopx.cli_commands import turn_run_once +from loopx.cli_commands.turn import handle_turn_command +from loopx.control_plane.todos.mutation_api import add_goal_todo +from loopx.control_plane.turn_driver.codex_cli import codex_cli_result_schema +from tests.control_plane.canonical_authority_fixture import isolate_sqlite_runtime +from tests.test_loopx_turn_codex_cli import _fake_codex +from tests.test_loopx_turn_driver import ( + _completion_host_and_validation_scripts, + _turn_journal, + _write_live_fixture, +) + + +def _cli(registry: Path, runtime: Path, *args: str) -> tuple[int, dict]: + output = io.StringIO() + with contextlib.redirect_stdout(output): + code = main( + [ + "--registry", + str(registry), + "--runtime-root", + str(runtime), + "--format", + "json", + *args, + ] + ) + return code, json.loads(output.getvalue()) + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize( + "scenario", + [ + "completion", + "terminal", + "progress", + "independent_validation", + "declared_validation", + "stale_lease", + "stop_after_progress_validation", + "stop_after_completion_validation", + ], +) +def test_builtin_codex_completes_before_accountable_writeback( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + provider: str, + scenario: str, +) -> None: + isolate_sqlite_runtime(tmp_path, monkeypatch) + terminal = scenario == "terminal" + progress = scenario in {"progress", "stop_after_progress_validation"} + stopped_after_validation = scenario in { + "stop_after_progress_validation", + "stop_after_completion_validation", + } + project, runtime, registry = _write_live_fixture( + tmp_path, + provider=provider, + handoff_mode="hard_lease", + todo_metadata_extra="no_followup=true" if terminal else "", + ) + added = ( + {"todo_id": "todo_fixture0001"} + if terminal + else add_goal_todo( + registry_path=registry, + goal_id="loopx-turn-fixture", + role="agent", + text="Deliver the independently checked artifact.", + task_class="advancement_task", + action_kind="fixture", + claimed_by="codex-fixture", + agent_id="codex-fixture", + validation_command_json=None + if progress + else json.dumps( + [ + sys.executable, + "-c", + "raise SystemExit(1)" + if scenario == "declared_validation" + else "raise SystemExit(0)", + ] + ), + ) + ) + todo_id = added["todo_id"] + claim_code, claim = _cli( + registry, + runtime, + "todo", + "claim", + "--goal-id", + "loopx-turn-fixture", + "--todo-id", + todo_id, + "--agent-id", + "codex-fixture", + "--claimed-by", + "codex-fixture", + ) + assert claim_code == 0, claim + if terminal: + code, current = _cli( + registry, runtime, "todo", "list", "--goal-id", "loopx-turn-fixture" + ) + assert code == 0, current + code, declaration = _cli( + registry, + runtime, + "todo", + "update", + "--goal-id", + "loopx-turn-fixture", + "--todo-id", + todo_id, + "--agent-id", + "codex-fixture", + "--claim-operation-id", + claim["claim_operation_id"], + "--validation-command-json", + json.dumps([sys.executable, "-c", "raise SystemExit(0)"]), + "--update-operation-id", + "declare-terminal-validation", + "--update-expected-provider-revision", + current["authority_read"]["provider_revision"], + ) + assert code == 0, declaration + binary, host_log = _fake_codex(tmp_path) + codex_home = tmp_path / "codex-home" + codex_home.mkdir() + monkeypatch.setenv("CODEX_HOME", str(codex_home)) + monkeypatch.setenv("LOOPX_RELEASE_ROOT", str(Path(__file__).resolve().parents[1])) + monkeypatch.setenv("FAKE_CODEX_LOG", str(host_log)) + monkeypatch.setenv( + "FAKE_CODEX_RESULT_FIELDS", + json.dumps( + { + "result_kind": "validated_progress" if progress else "validated_completion", + } + ), + ) + if scenario == "stale_lease": + code, inspection = _cli( + registry, + runtime, + "task-lease", + "inspect", + "--goal-id", + "loopx-turn-fixture", + "--todo-id", + todo_id, + ) + assert code == 0 and inspection["active"] is True, inspection + lease = inspection["lease"] + run_codex = turn_run_once.run_codex_cli_host + + def host(request, **kwargs): + # Renew after launch capture. A newer generation of the same owner + # still cannot authorize this result. + code, renewed = _cli( + registry, + runtime, + "task-lease", + "renew", + "--goal-id", + "loopx-turn-fixture", + "--todo-id", + todo_id, + "--owner", + "codex-fixture", + "--idempotency-key", + lease["idempotency_key"], + "--expected-version", + str(lease["version"]), + ) + assert code == 0, renewed + return run_codex(request, **kwargs) + + monkeypatch.setattr(turn_run_once, "run_codex_cli_host", host) + if stopped_after_validation: + build_validator = turn_run_once.build_loopx_turn_command_validator + + def validate_then_stop(*args, **kwargs): + validate = build_validator(*args, **kwargs) + + def checked(plan, result): + observation = validate(plan, result) + assert observation["status"] == "passed", observation + lifecycle = ("goal-lifecycle", "--goal-id", "loopx-turn-fixture", "--operation", "stop") + code, preview = _cli(registry, runtime, *lifecycle) + assert code == 0 and not preview["written"], preview + code, stopped = _cli( + registry, runtime, *lifecycle, "--actor-kind", "owner", + "--expected-state-fingerprint", preview["observed_state_fingerprint"], "--execute", + ) + assert code == 0 and stopped["readback"]["verified"], stopped + assert stopped["after_state"] == "stopped", stopped + return observation + + return checked + + monkeypatch.setattr(turn_run_once, "build_loopx_turn_command_validator", validate_then_stop) + argv = [ + "turn", + "run-once", + "--goal-id", + "loopx-turn-fixture", + "--agent-id", + "codex-fixture", + "--todo-id", + todo_id, + "--host", + "codex-cli", + "--project", + str(project), + "--codex-bin", + str(binary), + "--codex-model", + "fixture-model", + "--codex-reasoning-effort", + "high", + "--codex-sandbox", + "read-only", + "--validation-command-json", + json.dumps( + [ + sys.executable, + "-c", + "raise SystemExit(1)" + if scenario == "independent_validation" + else "raise SystemExit(0)", + ] + ), + "--no-global-sync", + "--execute", + ] + code, result = _cli(registry, runtime, *argv) + journals = list((runtime / "goals" / "loopx-turn-fixture" / "turns").glob("*.json")) + assert len(journals) == 1 + journal = json.loads(journals[0].read_text()) + assert len(host_log.read_text().splitlines()) == 1 + if stopped_after_validation: + assert code == 1 and result["error_code"] == "goal_stopped", result + assert journal["completed_phases"] == ["host_execute", "typed_result", "validation"] + read_code, readback = _cli( + registry, runtime, "todo", "list", "--goal-id", "loopx-turn-fixture", "--todo-id", todo_id, + ) + assert read_code == 0 and readback["todo"]["status"] == "open", readback + return + if scenario in {"independent_validation", "declared_validation", "stale_lease"}: + assert result["status"] != "committed", result + assert "quota_spend" not in journal["completed_phases"] + assert "durable_writeback" not in journal["completed_phases"] + if scenario == "stale_lease": + recovery_args = argv[:-1] + todo_option = recovery_args.index("--todo-id") + del recovery_args[todo_option : todo_option + 2] + code, retried = _cli( + registry, + runtime, + *recovery_args, + "--resume-turn-key", + journal["turn_key"], + "--execute", + ) + assert retried.get("status") != "committed", retried + assert len(host_log.read_text().splitlines()) == 1 + return + assert code == 0 and result["status"] == "committed", result + kinds = _effect_kinds(runtime, journal) + if progress: + assert "completion" not in journal["writeback"] + assert kinds == ["refresh_state", "quota_spend"] + read_code, readback = _cli( + registry, runtime, "todo", "list", "--goal-id", "loopx-turn-fixture" + ) + assert read_code == 0, readback + assert ( + next(todo for todo in readback["todos"] if todo["todo_id"] == todo_id)[ + "status" + ] + == "open" + ) + else: + completion = { + "todo_id": todo_id, + "continuation": "no_followup" if terminal else "active_goal", + } + assert journal["writeback"]["completion"] == completion + assert kinds == ["todo_complete", "refresh_state", "quota_spend"] + if terminal: + assert journal["terminal_closeout"]["completion"] == completion + replay_args = argv[:-1] + todo_option = replay_args.index("--todo-id") + del replay_args[todo_option : todo_option + 2] + code, replay = _cli( + registry, + runtime, + *replay_args, + "--resume-turn-key", + journal["turn_key"], + "--execute", + ) + assert code == 0 and replay["replayed"] is True, replay + assert len(host_log.read_text().splitlines()) == 1 + assert not any(replay["effects"].values()) + assert _effect_kinds(runtime, journal) == kinds + + +def _effect_kinds(runtime: Path, journal: dict) -> list[str]: + identity = journal["plan"]["transaction"] + run_id = identity.get("turn_instance_id", journal["turn_key"]) + path = runtime / "goals" / "loopx-turn-fixture" / "rollout-event-log.jsonl" + rows = (json.loads(line) for line in path.read_text().splitlines()) + return [ + row["event_kind"] + for row in rows + if row.get("run_id") == run_id + and row["event_kind"] in {"refresh_state", "quota_spend", "todo_complete"} + ] + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +@pytest.mark.parametrize( + "interruption", [None, "after_debit", "lost_completion_receipt"] +) +def test_native_terminal_closeout_keeps_the_original_lease_and_turn( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + provider: str, + interruption: str | None, +) -> None: + isolate_sqlite_runtime(tmp_path, monkeypatch) + project, runtime, registry = _write_live_fixture( + tmp_path, + provider=provider, + handoff_mode="hard_lease", + todo_metadata_extra="no_followup=true", + ) + goal_id, agent_id, todo_id = ( + "loopx-turn-fixture", + "codex-fixture", + "todo_fixture0001", + ) + code, claim = _cli( + registry, + runtime, + "todo", + "claim", + "--goal-id", + goal_id, + "--todo-id", + todo_id, + "--agent-id", + agent_id, + "--claimed-by", + agent_id, + ) + assert code == 0, claim + lease_argv = ("task-lease", "inspect", "--goal-id", goal_id, "--todo-id", todo_id) + code, inspection = _cli(registry, runtime, *lease_argv) + assert code == 0 and inspection["active"] is True, inspection + lease = inspection["lease"] + adapter = SimpleNamespace( + controller=SimpleNamespace( + registry_path=registry, coordination_runtime_root=runtime + ) + ) + host_calls, lease_reads = [], [] + + def resolve_lease(): + lease_reads.append(lease["version"]) + return StewardTurnHost._task_lease_proof( + adapter, {"actor": {"goal_id": goal_id}, "todo_id": todo_id}, lease + ) + + def host(request, launch_lifetime): + with launch_lifetime(): + host_calls.append(request["turn_key"]) + (project / "completion-artifact.txt").write_text( + "completed", encoding="utf-8" + ) + return { + "schema_version": "loopx_turn_result_v0", + "turn_key": request["turn_key"], + "result_kind": "validated_completion", + "completed_phases": ["host_execute", "typed_result"], + "classification": "fixture_completion", + "recommended_action": "Refresh the active Goal.", + "next_action": "Read the fresh decision.", + "delivery_batch_scale": "implementation", + "delivery_outcome": "outcome_progress", + "vision_unchanged_reason": "The remaining Goal is unchanged.", + "work_evidence": ["artifact:completion-artifact"], + "validation_evidence": ["check:artifact-contents"], + } + + _, validation = _completion_host_and_validation_scripts() + base = [ + "turn", + "run-once", + "--host", + "generic-cli", + "--goal-id", + goal_id, + "--agent-id", + agent_id, + "--project", + str(project), + "--no-global-sync", + "--validation-command-json", + json.dumps([sys.executable, "-c", validation]), + "--execute", + ] + + def invoke(*extra: str): + args = build_parser().parse_args([*base, *extra]) + args.host, args.session_scope = "codex-app-server", "agent" + result: dict = {} + code = handle_turn_command( + args, + registry_path=registry, + runtime_root_arg=str(runtime), + output_format=lambda *_args, **_kwargs: "json", + print_payload=lambda value, *_args: result.update(value), + native_host_runner=host, + native_completion_lease_resolver=resolve_lease, + native_session_binding_resolver=lambda _: { + "schema_version": "loopx_turn_session_binding_v0", + "goal_id": goal_id, + "agent_id": agent_id, + }, + ) + return code, result + + complete = turn_run_once.write_turn_validated_completion + if interruption is not None: + + def lose_terminal_attempt(**kwargs): + assert _effect_kinds(runtime, _turn_journal(runtime)) == [ + "refresh_state", + "quota_spend", + ] + if interruption == "lost_completion_receipt": + applied = complete(**kwargs) + assert applied["ok"] and applied["completed"], applied + raise OSError("injected terminal completion interruption") + + monkeypatch.setattr( + turn_run_once, "write_turn_validated_completion", lose_terminal_attempt + ) + code, result = invoke("--todo-id", todo_id) + journal = _turn_journal(runtime) + if interruption is not None: + assert code == 1 and journal["status"] == "in_progress", result + assert journal["completed_phases"][-1] == "quota_spend" + assert _effect_kinds(runtime, journal) == ["refresh_state", "quota_spend"] + assert _cli(registry, runtime, *lease_argv)[1]["active"] is ( + interruption == "after_debit" + ) + monkeypatch.setattr(turn_run_once, "write_turn_validated_completion", complete) + code, result = invoke( + "--resume-turn-key", journal["turn_key"], "--retry-failed-turn" + ) + assert code == 0 and result["status"] == "committed", result.get( + "error" + ) or result.get("reason") + assert _turn_journal(runtime)["terminal_closeout"]["completion"] == { + "todo_id": todo_id, + "continuation": "no_followup", + } + assert len(lease_reads) == (2 if interruption == "after_debit" else 1) + assert _cli(registry, runtime, *lease_argv)[1]["active"] is False + assert _effect_kinds(runtime, journal) == [ + "refresh_state", + "quota_spend", + "todo_complete", + ] + code, replay = invoke("--resume-turn-key", journal["turn_key"]) + assert code == 0 and replay["replayed"] is True, replay + assert not any(replay["effects"].values()) + assert host_calls == [journal["turn_key"]] + assert _effect_kinds(runtime, journal) == [ + "refresh_state", + "quota_spend", + "todo_complete", + ] + + +def test_builtin_codex_advertises_explicit_completion_without_promoting_progress() -> ( + None +): + kinds = codex_cli_result_schema()["properties"]["result_kind"]["enum"] + assert "validated_completion" in kinds and "validated_progress" in kinds diff --git a/tests/test_loopx_turn_driver.py b/tests/test_loopx_turn_driver.py index b5b0b3464d..65682ba1ff 100644 --- a/tests/test_loopx_turn_driver.py +++ b/tests/test_loopx_turn_driver.py @@ -1850,6 +1850,7 @@ def _write_live_fixture( }, "quota": {"compute": 1.0, "window_hours": 24}, "coordination": { + **({"handoff_mode": handoff_mode} if handoff_mode != "soft_claim" else {}), "agent_model": "peer_v1", "registered_agents": ["codex-fixture"], "agent_profiles": {