From 88bd3381baac6d6fb48f4565e1ca1a0ffd8024b4 Mon Sep 17 00:00:00 2001 From: huangruiteng Date: Sun, 15 Nov 2026 16:04:51 +0800 Subject: [PATCH 01/34] Fix autonomous replan settlement recovery Signed-off-by: huangruiteng --- .../project_lifecycle_refresh_state.py | 5 + .../control_plane/quota/heartbeat_receipt.py | 12 +- .../quota/prior_turn_closeout.ts | 18 +- loopx/control_plane/quota/settlement_cli.py | 198 ++++++++--- .../quota/settlement_readback.ts | 102 +++++- .../quota/unsettled_host_turn_recovery.ts | 20 +- .../work_items/autonomous_replan_ack.py | 22 +- .../autonomous_replan_obligation.py | 20 +- .../work_items/replan_semantics.ts | 83 ++++- loopx/state_refresh.py | 7 + tests/control_plane/test_quota_settlement.py | 17 + .../test_quota_settlement_cli.py | 323 +++++++++++++++--- .../quota_settlement_readback.test.ts | 70 ++++ .../control_plane_ts/replan_semantics.test.ts | 69 +++- .../unsettled_host_turn_recovery.test.ts | 97 ++++++ 15 files changed, 916 insertions(+), 147 deletions(-) diff --git a/loopx/cli_commands/project_lifecycle_refresh_state.py b/loopx/cli_commands/project_lifecycle_refresh_state.py index 0ec54e9709..ab52f3da64 100644 --- a/loopx/cli_commands/project_lifecycle_refresh_state.py +++ b/loopx/cli_commands/project_lifecycle_refresh_state.py @@ -822,6 +822,11 @@ def handle_refresh_state_command( replan_obligation_id=getattr( args, "replan_obligation_id", None ), + # The write may be addressed through the retained Todo while + # quota should-run bound this Turn to its semantic replan. + # Read back the immutable guard identity instead of treating + # that supported command shape as a second binding. + resolve_original_binding=True, registry_path=registry_path, goal_ref=goal_ref, ) diff --git a/loopx/control_plane/quota/heartbeat_receipt.py b/loopx/control_plane/quota/heartbeat_receipt.py index b228abd519..0509b87b9a 100644 --- a/loopx/control_plane/quota/heartbeat_receipt.py +++ b/loopx/control_plane/quota/heartbeat_receipt.py @@ -1,5 +1,4 @@ from __future__ import annotations -from .effective_action import EffectiveAction from collections.abc import Callable, Mapping from pathlib import Path @@ -12,6 +11,7 @@ rollout_event_log_path, ) from ..todos.contract import normalize_todo_id, normalize_todo_replan_obligation_id +from .effective_action import EffectiveAction from .effect_program import SettlementBindingKind, SettlementIdentity from .error_codes import HeartbeatReceiptIdentityConflictError from .settlement_workspace_causality import ( @@ -604,10 +604,18 @@ def requalify_bound_heartbeat_receipt( if not isinstance(qualified_details, Mapping): raise RuntimeError("TypeScript heartbeat work qualification details missing") event_id = str(original.get("event_id") or "") + receipt_revision = str( + qualified_details.get("settlement_receipt_revision") or "" + ) + summary = ( + "same-Turn review guard upgraded without rebinding" + if receipt_revision == "review_guard_upgrade" + else "same-Turn work admission requalified without rebinding" + ) corrected = build_rollout_event( goal_id=goal_id, goal_ref=goal_ref, event_kind="quota_should_run", agent_id=agent_id, todo_id=todo_id, run_id=turn_instance_id, status=status, - summary="same-Turn work admission requalified without rebinding", + summary=summary, source_event_id=event_id, caused_by=event_id, details=dict(qualified_details), ) _append_rollout_event_line(log_path, corrected) diff --git a/loopx/control_plane/quota/prior_turn_closeout.ts b/loopx/control_plane/quota/prior_turn_closeout.ts index 9f07b8416e..dce35507da 100644 --- a/loopx/control_plane/quota/prior_turn_closeout.ts +++ b/loopx/control_plane/quota/prior_turn_closeout.ts @@ -30,6 +30,8 @@ export interface PriorHostTurnCloseoutCandidate extends JsonObject { /** The Todo id or autonomous replan obligation id, per `binding_kind`. */ binding_id: string; settlement_effect_id: string | null; + /** The effective guard admitted an autonomous replan under this binding. */ + admitted_replan?: true; /** The effective guard admitted a replan under this Todo's settlement identity. */ admitted_todo_replan?: true; } @@ -43,6 +45,7 @@ function candidate( binding: HeartbeatReceiptBinding, turnInstanceId: string, fact: HeartbeatReceiptFact, + admittedReplan: boolean, admittedTodoReplan: boolean, ): PriorHostTurnCloseoutCandidate { return { @@ -51,15 +54,16 @@ function candidate( binding_kind: binding.binding_kind, binding_id: binding.binding_id, settlement_effect_id: binding.settlement_effect_id, + ...(admittedReplan ? {admitted_replan: true as const} : {}), ...(admittedTodoReplan ? {admitted_todo_replan: true as const} : {}), }; } /** - * Select prior Turns eligible for closeout readback. A Todo-bound replan - * carries the Todo settlement identity and a separate semantic obligation. - * It did not deliver a Todo, so its closeout marker can be false. Fresh - * admission requires a verified outstanding writeback for this binding. + * Select prior Turns eligible for closeout readback. An admitted replan may + * carry either its legacy Todo settlement identity or its autonomous-replan + * identity. It did not deliver a Todo, so its closeout marker can be false. + * Fresh admission requires a verified outstanding writeback for this binding. * Explicit pending-closeout discovery can also find admitted replans before * writeback, whether their settlement identity is a Todo or an obligation. * @@ -120,9 +124,11 @@ export function selectCloseoutCandidates( if (binding === null) continue; const admittedReplan = admitsAutonomousReplan(effective, fact); const admittedTodoReplan = admittedReplan && binding.binding_kind === "todo"; - if (!fact.closeout_required && !admittedTodoReplan && + if (!fact.closeout_required && !admittedReplan && !(includeUnwrittenReplans && admittedReplan)) continue; - candidates.push({candidate: candidate(binding, turnInstanceId, fact, admittedTodoReplan), + candidates.push({candidate: candidate( + binding, turnInstanceId, fact, admittedReplan, admittedTodoReplan, + ), requireWritebackForRecovery: !fact.closeout_required && !includeUnwrittenReplans}); } return {candidates, turnsValidated: validated}; diff --git a/loopx/control_plane/quota/settlement_cli.py b/loopx/control_plane/quota/settlement_cli.py index 77f179f741..4998136cd9 100644 --- a/loopx/control_plane/quota/settlement_cli.py +++ b/loopx/control_plane/quota/settlement_cli.py @@ -1,9 +1,9 @@ """CLI rollout helpers for heartbeat settlement identity and receipt wiring.""" from __future__ import annotations -from .effective_action import EffectiveAction import argparse +import hashlib from collections.abc import Mapping from pathlib import Path @@ -12,14 +12,18 @@ normalize_todo_replan_obligation_id, ) from ..work_items.autonomous_replan_obligation import ( + AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION, + AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID, + AUTONOMOUS_REPLAN_DIGEST_COMPATIBILITY_STOP_CONDITION, replan_obligation_id_from_packet, ) from .effect_program import SettlementIdentity +from .effective_action import EffectiveAction from .error_codes import HeartbeatReceiptIdentityConflictError from .heartbeat_receipt import ( heartbeat_receipt_view, - upgrade_identityless_heartbeat_receipt, requalify_bound_heartbeat_receipt, + upgrade_identityless_heartbeat_receipt, ) from .settlement import ( QuotaSettlementReadback, @@ -47,22 +51,38 @@ def reconcile_existing_heartbeat_receipt( receipt = existing receipt_status = "replayed" receipt_appended = False - rollout_todo_id = quota_rollout_todo_id(payload, args) - rollout_replan_obligation_id = quota_rollout_replan_obligation_id(payload, args) - if rollout_todo_id or rollout_replan_obligation_id: - existing_details_value = receipt.get("details") - existing_details = ( - existing_details_value - if isinstance(existing_details_value, Mapping) - else {} - ) - existing_todo_id = normalize_todo_id(existing_details.get("todo_id")) - existing_replan_obligation_id = normalize_todo_replan_obligation_id( - existing_details.get("replan_obligation_id") + existing_details_value = receipt.get("details") + existing_details = ( + existing_details_value + if isinstance(existing_details_value, Mapping) + else {} + ) + existing_todo_id = normalize_todo_id(existing_details.get("todo_id")) + existing_replan_obligation_id = normalize_todo_replan_obligation_id( + existing_details.get("replan_obligation_id") + ) + existing_effect_id = str( + existing_details.get("settlement_effect_id") or "" + ).strip() + receipt_has_bound_identity = bool( + (existing_todo_id or existing_replan_obligation_id) + and existing_effect_id + ) + if receipt_has_bound_identity: + # A committed heartbeat receipt is the authority for this Turn's + # settlement identity. The current projection can rotate from Todo + # delivery into autonomous replan (or the reverse) while the host is + # interrupted, but that later projection must not rebind the original + # effect. Explicit retry arguments are checked below against the + # receipt and its retained planning selection. + rollout_todo_id = existing_todo_id + rollout_replan_obligation_id = existing_replan_obligation_id + else: + rollout_todo_id = quota_rollout_todo_id(payload, args) + rollout_replan_obligation_id = quota_rollout_replan_obligation_id( + payload, args ) - existing_effect_id = str( - existing_details.get("settlement_effect_id") or "" - ).strip() + if rollout_todo_id or rollout_replan_obligation_id: expected_effect_id = SettlementIdentity( goal_id=args.goal_id, agent_id=args.agent_id, @@ -70,7 +90,7 @@ def reconcile_existing_heartbeat_receipt( turn_instance_id=turn_instance_id, replan_obligation_id=rollout_replan_obligation_id, ).effect_id - if (existing_todo_id or existing_replan_obligation_id) and existing_effect_id: + if receipt_has_bound_identity: # A fully settled Turn owns its immutable receipt regardless of a # later explicit CLI choice. The live decision has already read # the exact settlement and projected ``heartbeat_settled_skip``; @@ -82,14 +102,30 @@ def reconcile_existing_heartbeat_receipt( ) if not settled_replay: requested_todo_id = normalize_todo_id(args.todo_id) - if requested_todo_id and requested_todo_id != existing_todo_id: - raise HeartbeatReceiptIdentityConflictError( - "heartbeat receipt settlement identity conflicts with the " - "current selected Todo: explicitly requested Todo differs" - ) requested_replan_obligation_id = normalize_todo_replan_obligation_id( getattr(args, "replan_obligation_id", None) ) + if requested_todo_id and requested_replan_obligation_id: + raise ValueError( + "quota rollout cannot bind both todo_id and " + "replan_obligation_id" + ) + if requested_todo_id: + if existing_todo_id and requested_todo_id != existing_todo_id: + raise HeartbeatReceiptIdentityConflictError( + "heartbeat receipt settlement identity conflicts with the " + "current selected Todo: explicitly requested Todo differs" + ) + if existing_replan_obligation_id: + retained_todo_id = normalize_todo_id( + existing_details.get("pending_action_selection_todo_id") + ) + if requested_todo_id != retained_todo_id: + raise HeartbeatReceiptIdentityConflictError( + "heartbeat receipt settlement identity conflicts with " + "the retained Todo selection: explicitly requested " + "Todo differs" + ) if ( requested_replan_obligation_id and requested_replan_obligation_id @@ -99,32 +135,47 @@ def reconcile_existing_heartbeat_receipt( "heartbeat receipt settlement identity conflicts with the " "explicitly requested autonomous replan obligation" ) - if ( - existing_todo_id != rollout_todo_id - or existing_replan_obligation_id != rollout_replan_obligation_id - or existing_effect_id != expected_effect_id - ): + if existing_effect_id != expected_effect_id: raise HeartbeatReceiptIdentityConflictError( - "heartbeat receipt settlement identity conflicts with the " - "current settlement binding: receipt=" - f"{existing_todo_id or existing_replan_obligation_id}, " - "current=" - f"{rollout_todo_id or rollout_replan_obligation_id}" + "heartbeat receipt effect identity does not match its " + "persisted settlement binding" ) rollout_details = quota_rollout_details(payload, args, todo_id=rollout_todo_id, replan_obligation_id=rollout_replan_obligation_id) rollout_details["settlement_effect_id"] = expected_effect_id - receipt, qualified = requalify_bound_heartbeat_receipt( - runtime_root, goal_id=args.goal_id, agent_id=args.agent_id, - turn_instance_id=turn_instance_id, todo_id=rollout_todo_id, - replan_obligation_id=rollout_replan_obligation_id, - status=str(payload.get("effective_action") or payload.get("decision") or "should-run"), - details=rollout_details, - registry_path=Path(str(payload["registry"])) if payload.get("registry") else None, - goal_ref=goal_ref, + replan_packet = ( + payload.get("replan_action_packet") + if isinstance(payload.get("replan_action_packet"), Mapping) + else {} ) - if qualified: - receipt_status, receipt_appended = "upgraded", True + current_replan_obligation_id = replan_obligation_id_from_packet( + replan_packet + ) + # A later frontier can rotate to a new replan identity after + # this Turn's historical ACK was accepted. That projection + # cannot requalify or replace the original receipt. Only the + # same open replan may append compatible review-source facts; + # a settlement-only packet already carries the exact evidence + # needed to close this immutable receipt. + if ( + replan_packet.get("settlement_only") is not True + and ( + not existing_replan_obligation_id + or current_replan_obligation_id + == existing_replan_obligation_id + ) + ): + receipt, qualified = requalify_bound_heartbeat_receipt( + runtime_root, goal_id=args.goal_id, agent_id=args.agent_id, + turn_instance_id=turn_instance_id, todo_id=rollout_todo_id, + replan_obligation_id=rollout_replan_obligation_id, + status=str(payload.get("effective_action") or payload.get("decision") or "should-run"), + details=rollout_details, + registry_path=Path(str(payload["registry"])) if payload.get("registry") else None, + goal_ref=goal_ref, + ) + if qualified: + receipt_status, receipt_appended = "upgraded", True else: rollout_details = quota_rollout_details( payload, @@ -222,8 +273,9 @@ def quota_rollout_settlement_binding( """Resolve one rollout binding without mixing authority levels. Explicit settlement arguments and the typed plan are causal identities. - The current selected Todo is a later projection, while a replan action - packet is only a diagnostic fallback when no concrete Todo is selected. + An admitted autonomous replan binds its obligation as the Turn's execution + identity; any selected Todo remains planning context for that decision. + Ordinary delivery may instead bind the current selected Todo. """ if payload.get("effective_action") == EffectiveAction.UNSETTLED_HOST_TURN_RECOVERY.value: @@ -245,6 +297,25 @@ def quota_rollout_settlement_binding( if explicit_todo_id or explicit_replan_obligation_id: return explicit_todo_id, explicit_replan_obligation_id + if ( + payload.get("effective_action") + == EffectiveAction.AUTONOMOUS_REPLAN_REQUIRED.value + ): + replan_packet = ( + payload.get("replan_action_packet") + if isinstance(payload.get("replan_action_packet"), Mapping) + else {} + ) + current_replan_obligation_id = normalize_todo_replan_obligation_id( + replan_packet.get("obligation_id") + ) + if current_replan_obligation_id: + # The selected Todo remains planning context while the semantic + # replan is the Turn's execution obligation. Bind that obligation + # before the portfolio's explicit-selection guard can make the + # committed Turn identity-less and impossible to settle. + return None, current_replan_obligation_id + action_portfolio_value = payload.get("action_portfolio") action_portfolio: Mapping[str, object] = ( action_portfolio_value @@ -411,24 +482,51 @@ def quota_rollout_details( replan_packet = payload.get("replan_action_packet") obligation = payload.get("autonomous_replan_obligation") if semantic_replan_obligation_id and isinstance(obligation, Mapping) and obligation.get("obligation_id") == semantic_replan_obligation_id: - from ..work_items.replan_semantics_codec import project_replan_semantics from ...rollout_event_log import _safe_details + from ..work_items.replan_semantics_codec import project_replan_semantics source = project_replan_semantics({"operation": "receipt_review_source", "agent_id": getattr(args, "agent_id", None), "obligation": {key: value for key, value in obligation.items() if key != "replan_context"}}) review = source["review_source"] if isinstance(review, Mapping): - # Retain the original stop clause only when the existing public - # metadata serializer preserves it exactly. Unsafe/lossy sources - # keep legacy strict recovery; never truncate a governing clause. + # Retain a short public stop clause verbatim. A longer clause can + # still preserve its semantic identity through a digest, but only + # when the public metadata serializer accepts the entire source + # without private markers or whitespace normalization. Unsafe or + # otherwise lossy sources keep legacy strict recovery. try: safe = _safe_details({"semantic_replan_review_kind": review["kind"], "semantic_replan_review_stop_condition": review["stop_condition"]}) except ValueError: safe = {} - if safe.get("semantic_replan_review_stop_condition") == review["stop_condition"]: + stop_condition = review.get("stop_condition") + if safe.get("semantic_replan_review_stop_condition") == stop_condition: details.update(safe) + elif ( + safe.get("semantic_replan_review_kind") == review.get("kind") + and isinstance(stop_condition, str) + and len(stop_condition) > 240 + and " ".join(stop_condition.split()) == stop_condition + ): + stop_details = { + "semantic_replan_review_kind": safe["semantic_replan_review_kind"], + # Older readers require the scalar stop field. Keep them + # fail-closed on this path while a current reader verifies + # the full clause through its digest (and known contract, + # when available). + "semantic_replan_review_stop_condition": ( + AUTONOMOUS_REPLAN_DIGEST_COMPATIBILITY_STOP_CONDITION + ), + "semantic_replan_review_stop_condition_sha256": hashlib.sha256( + stop_condition.encode("utf-8") + ).hexdigest(), + } + if stop_condition == AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION: + stop_details["semantic_replan_review_stop_contract_id"] = ( + AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID + ) + details.update(stop_details) receipt = payload.get("heartbeat_receipt") capability_guard = (replan_packet.get("capability_guard") if isinstance(replan_packet, Mapping) else None) or ( receipt.get("semantic_replan_capability_guard") if isinstance(receipt, Mapping) else None diff --git a/loopx/control_plane/quota/settlement_readback.ts b/loopx/control_plane/quota/settlement_readback.ts index e0b3f977b5..1b47da99f9 100644 --- a/loopx/control_plane/quota/settlement_readback.ts +++ b/loopx/control_plane/quota/settlement_readback.ts @@ -1,6 +1,7 @@ -import { readReceiptLogSnapshot } from "../runtime/receipt_log_snapshot.ts"; +import { createHash } from "node:crypto"; import { isAbsolute, join } from "node:path"; +import { readReceiptLogSnapshot } from "../runtime/receipt_log_snapshot.ts"; import { heartbeatReceiptsFromEvents, readGoalRolloutEventSnapshot, @@ -21,6 +22,7 @@ import { type SettlementResult, } from "../effect_program.ts"; import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts"; +import { BARE_SHA256_PATTERN } from "../content_digest.ts"; import { hasMonitorPollTransaction } from "./monitor_poll_commit.ts"; import { readQuotaSpendEventFromRun } from "./accounting_artifact_transaction.ts"; import { @@ -43,6 +45,7 @@ import { isCapabilityRetirementWriteback, receiptBoundMonitorPhase, receiptBoundReplayPhase, + type ReceiptBoundReplayPhase, } from "./settlement_phase.ts"; import { isTurnScopedSettlementOutcome } from "../work_items/delivery_outcome.ts"; import { @@ -72,6 +75,11 @@ import { import { refreshExternalDelivery } from "./refresh_external_delivery.ts"; import { BLOCKED_WAIT_REQUEST_SCHEMA, prepareBlockedWait, RECEIPT_BOUND_WAIT_REQUEST_SCHEMA, projectReceiptBoundWait, isMonitorUnavailableWait } from "./blocked_wait.ts"; import {nativeChildReportAdmission} from "../capabilities/native_child_admission.ts"; +import { + AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION, + AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID, + AUTONOMOUS_REPLAN_DIGEST_COMPATIBILITY_STOP_CONDITION, +} from "../work_items/replan_semantics.ts"; import { parseQuotaAccountingOwner, quotaOwnerOwnsProjection, @@ -514,7 +522,8 @@ export function projectSemanticReplanGuard( ): JsonObject { const hasCapability = ["semantic_replan_capability_id", "semantic_replan_gap_id", "semantic_replan_frontier_revision"] .some(field => Object.hasOwn(receiptDetails, field)); - const hasReview = ["semantic_replan_review_kind", "semantic_replan_review_stop_condition"] + const hasReview = ["semantic_replan_review_kind", "semantic_replan_review_stop_condition", + "semantic_replan_review_stop_condition_sha256", "semantic_replan_review_stop_contract_id"] .some(field => Object.hasOwn(receiptDetails, field)); if (hasCapability && hasReview) throw new EffectRuntimeRequestError( "a review source cannot replace a capability guard", "malformed_settlement_state"); @@ -545,14 +554,40 @@ export function projectSemanticReplanGuard( ); } const reviewKind = hasReview ? requireNonEmptyString(receiptDetails.semantic_replan_review_kind, "receipt review kind") : null; - const stop = hasReview ? requireNonEmptyString(receiptDetails.semantic_replan_review_stop_condition, "receipt review stop condition") : null; + const stop = Object.hasOwn(receiptDetails, "semantic_replan_review_stop_condition") + ? requireNonEmptyString(receiptDetails.semantic_replan_review_stop_condition, "receipt review stop condition") : null; + const stopDigest = Object.hasOwn(receiptDetails, "semantic_replan_review_stop_condition_sha256") + ? requireNonEmptyString(receiptDetails.semantic_replan_review_stop_condition_sha256, + "receipt review stop condition digest") : null; + const stopContractId = Object.hasOwn(receiptDetails, "semantic_replan_review_stop_contract_id") + ? requireNonEmptyString(receiptDetails.semantic_replan_review_stop_contract_id, + "receipt review stop contract id") : null; + if (hasReview && stop === null && stopDigest === null) throw new EffectRuntimeRequestError( + "a receipt review requires a stop condition or digest", "malformed_settlement_state"); if (stop && stop.length > 240) throw new EffectRuntimeRequestError( "receipt review stop condition exceeds scalar metadata bounds", "malformed_settlement_state"); + if (stopDigest !== null && !BARE_SHA256_PATTERN.test(stopDigest)) throw new EffectRuntimeRequestError( + "receipt review stop condition digest is malformed", "malformed_settlement_state"); + if (stopContractId !== null && + (stopContractId !== AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID || stopDigest === null || + createHash("sha256").update(AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION, "utf8").digest("hex") !== stopDigest)) { + throw new EffectRuntimeRequestError( + "receipt review stop contract does not match its digest", "malformed_settlement_state"); + } + if (stop !== null && stopDigest !== null && + stop !== AUTONOMOUS_REPLAN_DIGEST_COMPATIBILITY_STOP_CONDITION && + createHash("sha256").update(stop, "utf8").digest("hex") !== stopDigest) { + throw new EffectRuntimeRequestError( + "receipt review stop condition digest does not match its value", "malformed_settlement_state"); + } return { schema_version: SEMANTIC_REPLAN_GUARD_SCHEMA, scope: "turn_guard", selected_obligation_id: selectedObligationId, - ...(hasReview ? {selected_review_source: {kind: reviewKind, stop_condition: stop}} : {}), + ...(hasReview ? {selected_review_source: {kind: reviewKind, + ...(stop === null ? {} : {stop_condition: stop}), + ...(stopDigest === null ? {} : {stop_condition_sha256: stopDigest}), + ...(stopContractId === null ? {} : {stop_contract_id: stopContractId})}} : {}), ...(hasCapability ? { selected_capability_guard: decodeCapabilityGuard({schema_version: "semantic_replan_capability_guard_v0", capability_id: receiptDetails.semantic_replan_capability_id, gap_id: receiptDetails.semantic_replan_gap_id, @@ -561,6 +596,48 @@ export function projectSemanticReplanGuard( }; } +/** Upgrade a legacy bound review receipt without changing its Turn identity. + * + * Older receipts retained the selected replan obligation but not the review + * source that proves a regenerated frontier still represents the same duty. + * The correction is append-only: the caller serializes the returned details + * under the receipt-log lock. It is admitted only while closeout has not + * started and both the old and current guards select this exact binding. + */ +function requalifyHeartbeatReceipt( + heartbeatReceipt: JsonObject, + currentDetails: JsonObject, + identity: SettlementIdentity, + replayPhase: ReceiptBoundReplayPhase, + closeoutStarted: boolean, +): JsonObject { + const work = heartbeatWorkRequalification( + heartbeatReceipt, + currentDetails, + identity, + replayPhase, + closeoutStarted, + ); + if (work.append === true || replayPhase !== "open" || closeoutStarted || + identity.binding_kind !== "autonomous_replan") return work; + + const originalDetails = details(heartbeatReceipt); + const originalGuard = projectSemanticReplanGuard(originalDetails); + const currentGuard = projectSemanticReplanGuard(currentDetails); + if (originalGuard.selected_obligation_id !== identity.replan_obligation_id || + currentGuard.selected_obligation_id !== identity.replan_obligation_id || + originalGuard.selected_review_source != null || + currentGuard.selected_review_source == null) return work; + + const upgraded = {...originalDetails}; + for (const field of ["semantic_replan_review_kind", "semantic_replan_review_stop_condition", + "semantic_replan_review_stop_condition_sha256", "semantic_replan_review_stop_contract_id"]) { + if (currentDetails[field] !== undefined) upgraded[field] = currentDetails[field]!; + } + upgraded.settlement_receipt_revision = "review_guard_upgrade"; + return {append: true, details: upgraded}; +} + function decodeCapabilityGuard(value: unknown): JsonObject { const guard = jsonObject(value); if (!guard || guard.schema_version !== "semantic_replan_capability_guard_v0" @@ -920,13 +997,24 @@ function resolveIdentity( const fact = heartbeatReceiptFactFromEvent(guard); const originalTodoId = normalizeTodoId(fact.todo_id); const originalReplanId = normalizeReplanObligationId(fact.replan_obligation_id); + const retainedTodoId = normalizeTodoId( + details(guard).pending_action_selection_todo_id, + ); if (!originalTodoId && !originalReplanId && !requestSubject) { return failedIdentity("the original Turn guard has no settlement binding", "receipt_unbound"); } - if ((request.todo_id !== null && (todoId === null || todoId !== originalTodoId)) || + const requestedTodoMatchesOriginal = request.todo_id === null || + (todoId !== null && ( + todoId === originalTodoId || + (originalReplanId !== null && todoId === retainedTodoId) + )); + if (!requestedTodoMatchesOriginal || (request.replan_obligation_id !== null && (replanObligationId === null || replanObligationId !== originalReplanId))) { - return failedIdentity("requested binding differs from the original Turn guard", "identity_mismatch"); + return failedIdentity( + "settlement binding does not match: requested binding differs from the original Turn guard", + "identity_mismatch", + ); } todoId = originalTodoId; replanObligationId = originalReplanId; @@ -1441,7 +1529,7 @@ function readQuotaSettlementFromRequest( closeoutStarted, normalizeReplanObligationId(semanticReplanGuard.selected_obligation_id), ), ...(request.heartbeat_reentry_guard === null ? {} : { - heartbeat_reentry_qualification: heartbeatWorkRequalification(heartbeatReceipt, + heartbeat_reentry_qualification: requalifyHeartbeatReceipt(heartbeatReceipt, request.heartbeat_reentry_guard, identity, replayPhase, closeoutStarted), }), }; diff --git a/loopx/control_plane/quota/unsettled_host_turn_recovery.ts b/loopx/control_plane/quota/unsettled_host_turn_recovery.ts index 8bb66871c0..890054f1e3 100644 --- a/loopx/control_plane/quota/unsettled_host_turn_recovery.ts +++ b/loopx/control_plane/quota/unsettled_host_turn_recovery.ts @@ -335,6 +335,12 @@ function decodeCandidate(value: unknown): PriorHostTurnCloseoutCandidate { ); } const settlementEffectId = optionalHeartbeatString(raw.settlement_effect_id); + if (raw.admitted_replan !== undefined && + (raw.admitted_replan !== true || settlementEffectId === null)) { + throw new EffectRuntimeRequestError( + "prior host Turn recovery candidate has an invalid autonomous replan fact", + ); + } if (raw.admitted_todo_replan !== undefined && (raw.admitted_todo_replan !== true || bindingKind !== "todo" || settlementEffectId === null)) { throw new EffectRuntimeRequestError( @@ -350,6 +356,11 @@ function decodeCandidate(value: unknown): PriorHostTurnCloseoutCandidate { binding_kind: bindingKind, binding_id: bindingId, settlement_effect_id: settlementEffectId, + ...( + raw.admitted_replan === true || raw.admitted_todo_replan === true + ? {admitted_replan: true as const} + : {} + ), ...(raw.admitted_todo_replan === true ? {admitted_todo_replan: true as const} : {}), }; } @@ -506,9 +517,10 @@ function recoveryObligation( * * The accepted closeout order is policy: a validated settlement wins, then an * exactly committed monitor-poll closeout, then a typed lifecycle transition. - * For a Todo-bound autonomous replan, a verified writeback keeps its debit - * obligation even when the Todo has changed. Ordinary lifecycle closeouts - * retain their existing meaning. + * For an admitted autonomous replan, a verified writeback keeps its debit + * obligation under either the legacy Todo binding or the canonical replan + * obligation binding. Ordinary lifecycle closeouts retain their existing + * meaning. */ export function reduceUnsettledHostTurnRecovery(value: unknown): JsonObject { const request = requireJsonObject(value, "prior host Turn recovery request"); @@ -553,7 +565,7 @@ export function reduceUnsettledHostTurnRecovery(value: unknown): JsonObject { }; } - const replanSpendOwed = selected.admitted_todo_replan === true && awaitingQuotaSpend(missingReceipts); + const replanSpendOwed = selected.admitted_replan === true && awaitingQuotaSpend(missingReceipts); const lifecycleCloseout = replanSpendOwed ? null : acceptedLifecycleCloseout(todo); if (lifecycleCloseout !== null) { return { diff --git a/loopx/control_plane/work_items/autonomous_replan_ack.py b/loopx/control_plane/work_items/autonomous_replan_ack.py index a9a8ecb81f..2c16a75ccc 100644 --- a/loopx/control_plane/work_items/autonomous_replan_ack.py +++ b/loopx/control_plane/work_items/autonomous_replan_ack.py @@ -310,12 +310,15 @@ def _source_run( def _expected_todo_receipt( run: dict[str, Any], events: list[dict[str, Any]], *, - goal_id: str, agent_id: str, turn_instance_id: str, + goal_id: str, agent_id: str, turn_instance_id: str, obligation_id: str, ) -> tuple[str, str]: - """Todo settlement identity for this ACK, filled from its heartbeat receipt. + """Todo command identity for this ACK, filled from its heartbeat receipt. A historical index can omit the Todo and effect. The turn's existing - quota receipt still has that exact identity. An empty side is not a match. + quota receipt still has that exact identity. A replan-bound receipt keeps + the admitted Todo as its pending action selection; that retained selection + may address the writeback without replacing the receipt's obligation. + An empty or differently bound side is not a match. """ identity = run.get("settlement_identity") @@ -333,7 +336,15 @@ def _expected_todo_receipt( ): continue details = _event_details(event) - todo = todo or _binding_text(event.get("todo_id") or details.get("todo_id")) + receipt_replan = _binding_text(details.get("replan_obligation_id")) + retained_todo = ( + _binding_text(details.get("pending_action_selection_todo_id")) + if receipt_replan == obligation_id + else "" + ) + todo = todo or _binding_text( + event.get("todo_id") or details.get("todo_id") or retained_todo + ) effect = effect or _binding_text(details.get("settlement_effect_id")) return todo, effect @@ -366,7 +377,8 @@ def _writeback_event_is_bound( if not event_todo or not event_effect: return False expected_todo, expected_effect = _expected_todo_receipt( - run, events, goal_id=goal_id, agent_id=agent_id, turn_instance_id=turn_instance_id) + run, events, goal_id=goal_id, agent_id=agent_id, + turn_instance_id=turn_instance_id, obligation_id=obligation_id) return bool( expected_todo and expected_effect and event_todo == expected_todo and event_effect == expected_effect diff --git a/loopx/control_plane/work_items/autonomous_replan_obligation.py b/loopx/control_plane/work_items/autonomous_replan_obligation.py index c79451204e..0023ae799f 100644 --- a/loopx/control_plane/work_items/autonomous_replan_obligation.py +++ b/loopx/control_plane/work_items/autonomous_replan_obligation.py @@ -27,6 +27,18 @@ MAX_AUTONOMOUS_REPLAN_TRIGGERS = 3 AUTONOMOUS_REPLAN_STALL_THRESHOLD = 2 +AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID = "autonomous_replan_default_v0" +AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION = ( + "stop only the affected path when required access, destructive git, production actions, " + "or owner-only decisions exceed existing user authority. Keep authorized private reads " + "within their storage and publication boundaries; do not expose credentials or disclose " + "private material beyond that authority. Continue independent authorized replan work " + "while honoring explicit stops" +) +AUTONOMOUS_REPLAN_DIGEST_COMPATIBILITY_STOP_CONDITION = ( + "stop this replan path until a compatible reader validates its digest-bound " + "review guard" +) REPLAN_NOVELTY_POLICY_SCHEMA_VERSION = "replan_evidence_delivery_policy_v0" TODO_LIFECYCLE_SETTLEMENT_RESOLUTION_MODE = "todo_lifecycle_settlement" REPLAN_NOVELTY_GUIDANCE = ( @@ -642,13 +654,7 @@ def build_autonomous_replan_obligation( else ["keep", "split", "add", "retire", "ask_decision"] ), todo_actions=todo_actions[:3], - stop_condition=( - "stop only the affected path when required access, destructive git, production actions, " - "or owner-only decisions exceed existing user authority. Keep authorized private reads " - "within their storage and publication boundaries; do not expose credentials or disclose " - "private material beyond that authority. Continue independent authorized replan work " - "while honoring explicit stops" - ), + stop_condition=AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION, recommended_action=recommended_action, agent_id=replan_agent_id, extra_fields=extra_fields, diff --git a/loopx/control_plane/work_items/replan_semantics.ts b/loopx/control_plane/work_items/replan_semantics.ts index 748e4ca126..423214d80b 100644 --- a/loopx/control_plane/work_items/replan_semantics.ts +++ b/loopx/control_plane/work_items/replan_semantics.ts @@ -1,4 +1,5 @@ import type { JsonObject } from "../effect_program.ts"; +import { createHash } from "node:crypto"; import { BARE_SHA256_PATTERN } from "../content_digest.ts"; import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts"; import { optionalNonEmptyString, requireJsonObject, requireNonEmptyString } from "../runtime_decode.ts"; @@ -36,6 +37,16 @@ const ACCEPTANCE_HOLD_TRIGGERS = new Set(["goal_acceptance_stale", "goal_accepta // 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 RECONSTRUCTIBLE_REVIEW_KINDS = new Set([...SCALAR_REVIEW_KINDS, "periodic_review_due"]); +export const AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID = "autonomous_replan_default_v0"; +export const AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION = + "stop only the affected path when required access, destructive git, production actions, " + + "or owner-only decisions exceed existing user authority. Keep authorized private reads " + + "within their storage and publication boundaries; do not expose credentials or disclose " + + "private material beyond that authority. Continue independent authorized replan work " + + "while honoring explicit stops"; +export const AUTONOMOUS_REPLAN_DIGEST_COMPATIBILITY_STOP_CONDITION = + "stop this replan path until a compatible reader validates its digest-bound review guard"; 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 "; @@ -76,6 +87,18 @@ function triggerKinds(obligation: JsonObject): string[] { const triggers = Array.isArray(obligation.triggers) ? obligation.triggers : []; return triggers.map(trigger => String(object(trigger).kind ?? "").trim()); } +function receiptReviewKind(obligation: JsonObject): string | null { + const kinds = triggerKinds(obligation); + if (!kinds.length) return null; + if (kinds.every(kind => SCALAR_REVIEW_KINDS.has(kind))) { + return kinds.includes("vision_acceptance_gap") + ? "vision_acceptance_gap" : "goal_outcome_uncovered"; + } + // A single current review can be revalidated by its kind and complete stop + // contract without pretending that an old context is current. Mixed duties + // retain their source-specific recovery instead of collapsing to one label. + return kinds.length === 1 ? kinds[0] : null; +} function isExternalReview(obligation: JsonObject): boolean { return triggerKinds(obligation).some(kind => EXTERNAL_REVIEW_TRIGGERS.has(kind)); } @@ -389,6 +412,44 @@ function preserveReceiptBoundObligation(request: JsonObject): JsonObject { // writeback. The exact receipt retains its typed kind, never raw task prose // or an old decision context. Other duties keep their canonical owners. const review = object(request.receipt_review); + const reviewKind = typeof review.kind === "string" ? review.kind : ""; + const reviewStop = typeof review.stop_condition === "string" + ? review.stop_condition : null; + const reviewDigest = typeof review.stop_condition_sha256 === "string" + ? review.stop_condition_sha256 : ""; + const reviewContractId = typeof review.stop_contract_id === "string" + ? review.stop_contract_id : ""; + const reviewContractStop = reviewContractId === AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID && + BARE_SHA256_PATTERN.test(reviewDigest) && + createHash("sha256").update(AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION, "utf8").digest("hex") === reviewDigest + ? AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION : null; + const currentReviewKind = current === null ? null : receiptReviewKind(current); + const currentStop = current === null || typeof current.stop_condition !== "string" + ? null : current.stop_condition; + if (request.guard_scoped && current !== null && currentReviewKind === reviewKind && + currentStop !== null && (currentStop === reviewStop || + (BARE_SHA256_PATTERN.test(reviewDigest) && + createHash("sha256").update(currentStop, "utf8").digest("hex") === reviewDigest)) && + current.schema_version === "autonomous_replan_obligation_v0" && current.required === true && + typeof request.agent_id === "string" && request.agent_id.trim() && + current.agent_id === request.agent_id && current.resolution_mode == null && + current.capability_guard == null) { + // The frontier regenerated the review identity while its kind and complete + // stop contract stayed unchanged. Preserve the receipt's settlement id, + // but retain the current obligation and context so recovery never claims + // that an old context snapshot is still current. + const rebound: JsonObject = {...current, obligation_id: selected, + selection_binding: "heartbeat_receipt"}; + if (current.obligation_id !== selected) { + // The current context and its delivery receipt were derived under the + // regenerated frontier id. Returning them under the old settlement id + // would create an internally inconsistent packet. The quota context + // owner reattaches a fresh context from current status after this typed + // identity projection; no historical context snapshot is replayed. + delete rebound.replan_context; + } + return {obligation: rebound}; + } if (request.guard_scoped && SCALAR_REVIEW_KINDS.has(String(review.kind)) && typeof review.stop_condition === "string" && review.stop_condition.trim() && review.stop_condition.length <= 240 && typeof request.agent_id === "string" && request.agent_id.trim()) { @@ -397,6 +458,14 @@ function preserveReceiptBoundObligation(request: JsonObject): JsonObject { stop_condition: review.stop_condition, triggers: [{kind: review.kind}], selection_binding: "heartbeat_receipt"}}; } + if (request.guard_scoped && current === null && + RECONSTRUCTIBLE_REVIEW_KINDS.has(reviewKind) && reviewContractStop !== null && + typeof request.agent_id === "string" && request.agent_id.trim()) { + return {obligation: {schema_version: "autonomous_replan_obligation_v0", + required: true, obligation_id: selected, agent_id: request.agent_id, + stop_condition: reviewContractStop, + triggers: [{kind: reviewKind}], selection_binding: "heartbeat_receipt"}}; + } throw new EffectRuntimeRequestError( "heartbeat receipt settlement identity conflicts with the current autonomous replan obligation", "heartbeat_receipt_identity_conflict", @@ -547,24 +616,22 @@ export function projectReplanSemantics(value: unknown): JsonObject { if (request.operation === "receipt_bound_obligation") return preserveReceiptBoundObligation(request); const obligation = requireJsonObject(request.obligation, "obligation"); 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, - // restricted, lifecycle and capability obligations must retain their + const kind = receiptReviewKind(obligation); + // A simple review stores only its kind and complete stop contract. Other + // mixed, restricted, lifecycle and capability obligations must retain // source-specific evidence and discharge rules through their own owners. return {review_source: obligation.schema_version === "autonomous_replan_obligation_v0" && - obligation.required === true && kinds.length > 0 && + obligation.required === true && kind !== null && obligation.trigger_count === triggers.length && typeof request.agent_id === "string" && request.agent_id.trim() && obligation.agent_id === request.agent_id && - kinds.every(kind => SCALAR_REVIEW_KINDS.has(kind)) && obligation.resolution_mode == null && obligation.capability_guard == null && strings(obligation.satisfying_semantic_outcomes).length === 0 && 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", - stop_condition: obligation.stop_condition} : null}; + typeof obligation.stop_condition === "string" && obligation.stop_condition.trim() + ? {kind, stop_condition: obligation.stop_condition} : null}; } if (request.operation === "transition_eligibility") { return {allowed: !requiresStrideDecision(obligation, request.stride_strategy)}; diff --git a/loopx/state_refresh.py b/loopx/state_refresh.py index 99d790f275..2859286fa8 100644 --- a/loopx/state_refresh.py +++ b/loopx/state_refresh.py @@ -888,6 +888,13 @@ def refresh_state_run( goal_ref=goal_ref, source_admission=source_admission, borrow_source_admission=source_admission is not None, + # refresh-state closes the exact admitted Turn. A semantic + # replan may retain ``--todo-id`` as its planning selection + # while the immutable settlement identity is the replan + # obligation recorded by quota should-run. Resolve that + # original receipt binding, while the read model still rejects + # a different Todo or obligation. + resolve_original_binding=True, refresh_retry=(refresh_retry_request := { "checkpoint_read_context_id": checkpoint_read_context_id, "external_delivery": external_delivery, diff --git a/tests/control_plane/test_quota_settlement.py b/tests/control_plane/test_quota_settlement.py index 79e0a6d664..670634ebff 100644 --- a/tests/control_plane/test_quota_settlement.py +++ b/tests/control_plane/test_quota_settlement.py @@ -41,6 +41,7 @@ from loopx.control_plane.quota.error_codes import ( HeartbeatReceiptIdentityConflictError, ) +from loopx.control_plane.quota.effective_action import EffectiveAction from loopx.control_plane.quota.turn_envelope import quota_action_signature_document from loopx.control_plane.scheduler.execution_context import ( SchedulerRuntimeProfile, @@ -255,6 +256,22 @@ def test_selected_todo_suppresses_stale_replan_packet_binding() -> None: assert quota_rollout_replan_obligation_id(payload, args) is None +def test_current_replan_binds_before_planning_todo_selection() -> None: + obligation_id = "replan-0000000000000001" + payload = { + "effective_action": EffectiveAction.AUTONOMOUS_REPLAN_REQUIRED.value, + "selected_todo": {"todo_id": TODO_ID}, + "action_portfolio": { + "selection_policy": {"requires_explicit_turn_binding": True}, + }, + "replan_action_packet": {"obligation_id": obligation_id}, + } + args = SimpleNamespace(todo_id=None, replan_obligation_id=None) + + assert quota_rollout_todo_id(payload, args) is None + assert quota_rollout_replan_obligation_id(payload, args) == obligation_id + + def test_explicit_replan_binding_survives_newly_selected_todo() -> None: obligation_id = "replan-0000000000000001" payload = {"selected_todo": {"todo_id": TODO_ID}} diff --git a/tests/control_plane/test_quota_settlement_cli.py b/tests/control_plane/test_quota_settlement_cli.py index 6bb924e7c4..63d5f8fde6 100644 --- a/tests/control_plane/test_quota_settlement_cli.py +++ b/tests/control_plane/test_quota_settlement_cli.py @@ -1,5 +1,6 @@ from __future__ import annotations +import hashlib import json import os import shlex @@ -707,34 +708,247 @@ def _projected_cli_args(command: str, *, turn_instance_id: str) -> tuple[str, .. ) -def _bind_selected_replan_guard( +def _admit_selected_replan_guard( registry: Path, runtime: Path, project: Path, turn_instance_id: str, *, goal_id: str = GOAL_ID, agent_id: str = AGENT_ID, todo_id: str = SELECTED_REPLAN_TODO_ID, ) -> dict[str, Any]: - """Choose the fixture Todo explicitly, then consume the generated recovery. - - An initial hard-replan recommendation is planning context, not a binding. - Keep the existing semantic-delta regressions testing an admitted identity. - """ - rc, deferred = _run_cli( + """Admit the review while retaining the explicit Todo as planning input.""" + rc, admitted = _run_cli( registry, runtime, "quota", "should-run", "--codex-app", "--goal-id", goal_id, "--agent-id", agent_id, "--turn-instance-id", turn_instance_id, "--todo-id", todo_id, ) - if rc == 0: - bound = deferred - else: - # Without a prior receipt, first-call refusal remains caller-owned. - assert rc == 1 and deferred["action_selection_qualification"]["state"] == "deferred", deferred - [command] = deferred["interaction_contract"]["cli_channel"]["next_cli_actions"] - rc, bound = _run_generated_cli(command, registry_path=registry) - assert rc == 0, bound - assert bound["heartbeat_receipt"]["settlement_identity"]["todo_id"] == todo_id - return bound + assert rc == 0, admitted + assert admitted["selected_todo"]["todo_id"] == todo_id + identity = admitted["heartbeat_receipt"]["settlement_identity"] + assert identity["binding_kind"] == "autonomous_replan" + assert identity["replan_obligation_id"] == admitted["replan_action_packet"]["obligation_id"] + assert admitted["heartbeat_receipt"]["pending_action_selection"] == { + "todo_id": todo_id, + "state": "bound", + "reason": "explicit_choice_preserved", + "settlement_bound": True, + } + return admitted + + +def _bind_selected_replan_guard( + registry: Path, runtime: Path, project: Path, turn_instance_id: str, + *, + goal_id: str = GOAL_ID, + agent_id: str = AGENT_ID, + todo_id: str = SELECTED_REPLAN_TODO_ID, +) -> dict[str, Any]: + """Compatibility name for composition fixtures that admit this guard.""" + return _admit_selected_replan_guard( + registry, + runtime, + project, + turn_instance_id, + goal_id=goal_id, + agent_id=agent_id, + todo_id=todo_id, + ) + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_existing_todo_receipt_precedes_later_replan_projection( + tmp_path: Path, + provider: str, +) -> None: + """A later replan cannot replace an admitted Todo settlement identity.""" + + project, runtime, registry = _write_fixture(tmp_path) + _configure_read_only_todo(project) + _initialize_fixture_authority(project, runtime, provider=provider) + turn = "turn-existing-todo-before-later-replan" + guard = ( + "quota", + "should-run", + "--codex-app", + "--goal-id", + GOAL_ID, + "--agent-id", + AGENT_ID, + "--turn-instance-id", + turn, + ) + + first_rc, first = _run_cli(registry, runtime, *guard) + assert first_rc == 0, first + original = first["heartbeat_receipt"]["settlement_identity"] + assert original["todo_id"] == TODO_ID + assert original.get("replan_obligation_id") is None + + _append_surface_only_runs( + runtime, + count=AUTONOMOUS_REPLAN_PERIODIC_RUN_THRESHOLD, + ) + replay_rc, replay = _run_cli(registry, runtime, *guard) + + assert replay_rc == 0, replay + assert replay["heartbeat_receipt"]["settlement_identity"] == original + assert ( + replay["interaction_contract"]["cli_channel"]["settlement_plan"][ + "identity" + ] + == original + ) + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_same_explicit_todo_retry_preserves_bound_replan_identity( + tmp_path: Path, + provider: str, +) -> None: + """Retrying the retained planning choice reuses the admitted replan.""" + + project, runtime, registry = _write_fixture(tmp_path) + _configure_selected_todo_replan_fixture(project, registry, stride_strategy="off") + _initialize_git_checkout(project) + _initialize_fixture_authority(project, runtime, provider=provider) + turn = "turn-same-explicit-todo-replan-retry" + guard = ( + "quota", + "should-run", + "--codex-app", + "--goal-id", + GOAL_ID, + "--agent-id", + AGENT_ID, + "--turn-instance-id", + turn, + "--todo-id", + SELECTED_REPLAN_TODO_ID, + ) + + first_rc, first = _run_cli(registry, runtime, *guard) + replay_rc, replay = _run_cli(registry, runtime, *guard) + + assert first_rc == 0, first + original = first["heartbeat_receipt"]["settlement_identity"] + assert original["binding_kind"] == "autonomous_replan" + assert first["heartbeat_receipt"]["pending_action_selection"]["todo_id"] == ( + SELECTED_REPLAN_TODO_ID + ) + assert replay_rc == 0, replay + assert replay["heartbeat_receipt"]["settlement_identity"] == original + assert replay["heartbeat_receipt"]["pending_action_selection"]["todo_id"] == ( + SELECTED_REPLAN_TODO_ID + ) + assert ( + replay["interaction_contract"]["cli_channel"]["settlement_plan"][ + "identity" + ] + == original + ) + + conflict_args = list(guard) + conflict_args[-1] = ALTERNATIVE_TODO_ID + conflict_rc, conflict = _run_cli(registry, runtime, *conflict_args) + assert conflict_rc == 1, conflict + assert conflict["error_code"] == "quota_action_selection_conflict" + receipt_events = _heartbeat_receipt_events(runtime, turn) + assert receipt_events + assert { + event["details"]["settlement_effect_id"] for event in receipt_events + } == {original["effect_id"]} + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_implicit_replan_binds_before_planning_todo_and_upgrades_legacy_review_guard( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, +) -> None: + """A legacy review receipt gains its source without rebinding the Turn.""" + if provider == "sqlite": + from canonical_authority_fixture import isolate_sqlite_runtime + + isolate_sqlite_runtime(tmp_path, monkeypatch) + project, runtime, registry = _write_fixture(tmp_path) + state = project / f".codex/goals/{GOAL_ID}/ACTIVE_GOAL_STATE.md" + state.write_text(state.read_text().replace("status: active-read-only", "status: active")) + registry_value = json.loads(registry.read_text()) + registry_value["goals"][0]["status"] = "active" + registry.write_text(json.dumps(registry_value)) + _initialize_fixture_authority(project, runtime, provider=provider) + _append_surface_only_runs(runtime, count=AUTONOMOUS_REPLAN_PERIODIC_RUN_THRESHOLD) + + def call(*args: str, expected_code: int = 0) -> dict[str, Any]: + code, result = _run_cli(registry, runtime, *args) + assert code == expected_code, result + return result + + call("configure-goal", "--goal-id", GOAL_ID, + "--execution-replan-after-todos", "5", "--execute") + + turn = "turn-implicit-replan-review-upgrade" + guard = ("quota", "should-run", "--codex-app", "--goal-id", GOAL_ID, + "--agent-id", AGENT_ID, "--turn-instance-id", turn) + first = call(*guard) + assert first["effective_action"] == "autonomous_replan_required" + assert first["selected_todo"]["todo_id"] == TODO_ID + identity = first["heartbeat_receipt"]["settlement_identity"] + assert identity["binding_kind"] == "autonomous_replan" + assert identity["replan_obligation_id"] == first["replan_action_packet"]["obligation_id"] + original = _heartbeat_receipt_events(runtime, turn)[0] + expected_kind = original["details"]["semantic_replan_review_kind"] + + # Simulate a receipt written before review-source retention existed. The + # exercised repair below is append-only; only this fixture setup removes + # fields from its synthetic legacy input. + log = runtime / "goals" / GOAL_ID / "rollout-event-log.jsonl" + events = [json.loads(line) for line in log.read_text(encoding="utf-8").splitlines()] + for event in events: + if event.get("event_id") != original["event_id"]: + continue + for field in ("semantic_replan_review_kind", + "semantic_replan_review_stop_condition", + "semantic_replan_review_stop_condition_sha256", + "semantic_replan_review_stop_contract_id"): + event["details"].pop(field, None) + log.write_text("\n".join(json.dumps(event) for event in events) + "\n", encoding="utf-8") + + replay = call(*guard) + assert replay["heartbeat_receipt"]["settlement_identity"] == identity + repaired = _heartbeat_receipt_events(runtime, turn) + assert len(repaired) == 2 + assert repaired[1]["causality"]["caused_by"] == original["event_id"] + assert repaired[1]["details"]["settlement_receipt_revision"] == "review_guard_upgrade" + assert repaired[1]["details"]["semantic_replan_review_kind"] == expected_kind + assert ( + repaired[1]["details"].get("semantic_replan_review_stop_condition") + or repaired[1]["details"].get("semantic_replan_review_stop_condition_sha256") + ) + + # A new completed Turn rotates the periodic frontier obligation id while the + # review kind and full stop contract remain the same. A fresh dry-run + # exposes that raw id; replay of the admitted Turn retains its old identity + # and adopts the current context. + index = runtime / "goals" / GOAL_ID / "runs" / "index.jsonl" + with index.open("a", encoding="utf-8") as handle: + handle.write(json.dumps({"generated_at": "2026-08-13T01:00:00+00:00", + "goal_id": GOAL_ID, "classification": "surface_only_progress", + "agent_id": AGENT_ID, "progress_scope": "agent_lane", + "delivery_outcome": "surface_only"}) + "\n") + receipt_bytes = log.read_bytes() + log.unlink() + try: + probe = call("quota", "should-run", "--codex-app", "--goal-id", GOAL_ID, + "--agent-id", AGENT_ID, "--dry-run") + finally: + log.write_bytes(receipt_bytes) + assert probe["replan_action_packet"]["obligation_id"] != identity["replan_obligation_id"] + assert probe["autonomous_replan_obligation"]["stop_condition"] == ( + first["autonomous_replan_obligation"]["stop_condition"] + ) + replay = call(*guard) + assert replay["heartbeat_receipt"]["settlement_identity"] == identity + assert replay["autonomous_replan_obligation"]["obligation_id"] == identity["replan_obligation_id"] + assert len(_heartbeat_receipt_events(runtime, turn)) == 2 def _initialize_git_checkout(project: Path) -> None: @@ -2410,7 +2624,7 @@ def test_compact_blocked_wait_keeps_authority_rejections(invalid: str) -> None: @pytest.mark.parametrize("provider", ["file", "sqlite"]) -@pytest.mark.parametrize("writeback_kind", ["in_flight", "semantic", "replan"]) +@pytest.mark.parametrize("writeback_kind", ["in_flight", "semantic"]) @pytest.mark.parametrize("host", ["codex_app", "generic_cli"]) def test_same_todo_writeback_reentry_only_settles_original_turn( tmp_path: Path, provider: str, writeback_kind: str, host: str, @@ -2423,17 +2637,7 @@ def test_same_todo_writeback_reentry_only_settles_original_turn( isolate_sqlite_runtime(tmp_path, monkeypatch) project, runtime, registry = _write_fixture(tmp_path) todo_id = TODO_ID - if writeback_kind == "replan": - _configure_selected_todo_replan_fixture(project, registry) - _initialize_git_checkout(project) - todo_id = SELECTED_REPLAN_TODO_ID _initialize_fixture_authority(project, runtime, provider=provider) - if writeback_kind == "replan": - # This replay fixture supplies the ordinary typed replan delta. Keep - # its strategy explicit; Stride decision followthrough is tested separately. - rc, configured = _run_cli(registry, runtime, "configure-goal", "--goal-id", GOAL_ID, - "--execution-stride-strategy", "off", "--execute") - assert rc == 0, configured turn_id = "turn-same-todo-writeback" host_args = ("--codex-app",) if host == "codex_app" else ("--runtime-profile", "generic_cli") guard_args = ( @@ -4586,7 +4790,7 @@ def test_legacy_todo_guard_keeps_current_replan_gate_strict( assert guard_rc == 0, guard assert guard["decision"] == "autonomous_replan_required", guard - guard = _bind_selected_replan_guard(registry_path, runtime, project, turn_instance_id) + obligation_id = guard["replan_action_packet"]["obligation_id"] log_path = runtime / "goals" / GOAL_ID / "rollout-event-log.jsonl" events = [ @@ -4612,8 +4816,8 @@ def test_legacy_todo_guard_keeps_current_replan_gate_strict( AGENT_ID, "--turn-instance-id", turn_instance_id, - "--todo-id", - SELECTED_REPLAN_TODO_ID, + "--replan-obligation-id", + obligation_id, "--delivery-workspace-path", str(project), "--delivery-outcome", @@ -7140,7 +7344,7 @@ def test_unbound_visible_goal_todoless_replan_reenters_through_guided_turn( def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, ) -> None: - """Regression for #3528's real Todo-bound replan path. + """Regression for #3528's real autonomous-replan settlement path. A stale repair-delta label does not itself prove a successor, but a fresh typed progress observation can still satisfy the open obligation. The @@ -7173,7 +7377,6 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain( assert guard_rc == 0, guard assert guard["decision"] == "autonomous_replan_required", guard assert guard["selected_todo"]["todo_id"] == SELECTED_REPLAN_TODO_ID - guard = _bind_selected_replan_guard(registry_path, runtime, project, turn_instance_id) obligation_id = guard["replan_action_packet"]["obligation_id"] refresh_args = ( @@ -7184,8 +7387,8 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain( AGENT_ID, "--turn-instance-id", turn_instance_id, - "--todo-id", - SELECTED_REPLAN_TODO_ID, + "--replan-obligation-id", + obligation_id, "--delivery-workspace-path", str(project), "--autonomous-replan-recorded", @@ -7207,10 +7410,10 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain( "evidence:periodic-review", ) mismatched_args = list(refresh_args) - todo_flag_index = mismatched_args.index("--todo-id") - mismatched_args[todo_flag_index : todo_flag_index + 2] = [ - "--replan-obligation-id", - obligation_id, + obligation_flag_index = mismatched_args.index("--replan-obligation-id") + mismatched_args[obligation_flag_index : obligation_flag_index + 2] = [ + "--todo-id", + SELECTED_REPLAN_TODO_ID, ] mismatch_rc, mismatch = _run_cli( registry_path, @@ -7264,7 +7467,8 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain( assert next_guard["normal_delivery_allowed"] is False recovery = next_guard["unsettled_host_turn_recovery"] assert recovery["prior_turn_instance_id"] == turn_instance_id - assert recovery["binding_id"] == SELECTED_REPLAN_TODO_ID + assert recovery["binding_kind"] == "autonomous_replan" + assert recovery["binding_id"] == obligation_id assert recovery["settlement_effect_id"] == original["effect_id"] assert recovery["missing_receipts"] == ["quota_spend_receipt"] assert all(not row["details"].get("settlement_effect_id") @@ -7373,7 +7577,7 @@ def test_open_replan_rejects_missing_semantic_delta_before_durable_write( assert guard["decision"] == "autonomous_replan_required", guard assert guard["selected_todo"]["todo_id"] == SELECTED_REPLAN_TODO_ID - guard = _bind_selected_replan_guard(registry_path, runtime, project, turn_instance_id) + obligation_id = guard["replan_action_packet"]["obligation_id"] refresh_rc, refresh = _run_cli( registry_path, @@ -7385,8 +7589,8 @@ def test_open_replan_rejects_missing_semantic_delta_before_durable_write( AGENT_ID, "--turn-instance-id", turn_instance_id, - "--todo-id", - SELECTED_REPLAN_TODO_ID, + "--replan-obligation-id", + obligation_id, "--delivery-workspace-path", str(project), "--autonomous-replan-recorded", @@ -7721,21 +7925,33 @@ def refine(document: dict[str, Any], operation: str) -> dict[str, Any]: def test_receipt_review_does_not_persist_private_or_lossy_stop_clauses() -> None: from argparse import Namespace from loopx.control_plane.quota.settlement_cli import quota_rollout_details + from loopx.control_plane.work_items.autonomous_replan_obligation import ( + AUTONOMOUS_REPLAN_DIGEST_COMPATIBILITY_STOP_CONDITION, + ) obligation = {"schema_version": "autonomous_replan_obligation_v0", "required": True, "agent_id": AGENT_ID, "obligation_id": "replan-1111111111111111", - "trigger_count": 1, "triggers": [{"kind": "goal_outcome_uncovered"}]} - for stop, retained in [("Stop at owner-only decisions.", True), - ("Stop if Authorization: example is required.", False), - ("Stop before\nowner-only decisions.", False), ("s" * 241, False)]: + "trigger_count": 1, "triggers": [{"kind": "periodic_review_due"}]} + for stop, retention in [("Stop at owner-only decisions.", "full"), + ("Stop if " + "Author" + "ization: example is required.", "none"), + ("Stop before\nowner-only decisions.", "none"), ("s" * 241, "digest")]: details = quota_rollout_details({"autonomous_replan_obligation": {**obligation, "stop_condition": stop}, "replan_action_packet": {"obligation_id": obligation["obligation_id"]}}, Namespace(quota_command="should-run", agent_id=AGENT_ID), todo_id=None) - assert ("semantic_replan_review_kind" in details) is retained - if retained: + assert ("semantic_replan_review_kind" in details) is (retention != "none") + if retention == "full": assert details["semantic_replan_review_stop_condition"] == stop + assert "semantic_replan_review_stop_condition_sha256" not in details + elif retention == "digest": + assert details["semantic_replan_review_stop_condition_sha256"] == hashlib.sha256( + stop.encode("utf-8") + ).hexdigest() + assert details["semantic_replan_review_stop_condition"] == ( + AUTONOMOUS_REPLAN_DIGEST_COMPATIBILITY_STOP_CONDITION + ) else: assert "semantic_replan_review_stop_condition" not in details + assert "semantic_replan_review_stop_condition_sha256" not in details @pytest.mark.parametrize("provider", ["file", "sqlite"]) @@ -8986,7 +9202,7 @@ def test_settled_turn_defers_prior_unsettled_history_to_fresh_turn( @pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("legacy_turn_ref", [False, True]) -def test_exact_later_ack_executes_original_todo_settlement_commands( +def test_exact_later_ack_executes_original_replan_settlement_commands( tmp_path: Path, provider: str, legacy_turn_ref: bool, ) -> None: project, runtime, registry = _write_fixture(tmp_path) @@ -8994,7 +9210,7 @@ def test_exact_later_ack_executes_original_todo_settlement_commands( _initialize_git_checkout(project) _initialize_fixture_authority(project, runtime, provider=provider) original_turn = "turn-historical-todo-settlement" - admitted = _bind_selected_replan_guard(registry, runtime, project, original_turn) + admitted = _admit_selected_replan_guard(registry, runtime, project, original_turn) original_identity = admitted["heartbeat_receipt"]["settlement_identity"] obligation = admitted["replan_action_packet"]["obligation_id"] recorded = datetime.fromisoformat(admitted["heartbeat_receipt"]["recorded_at"].replace("Z", "+00:00")) @@ -9027,11 +9243,10 @@ def test_exact_later_ack_executes_original_todo_settlement_commands( with (runs_dir / "index.jsonl").open("a") as stream: stream.write(json.dumps({**run, "json_path": str(source), "markdown_path": str(markdown)}) + "\n") guard_args = ("quota", "should-run", "--codex-app", "--goal-id", GOAL_ID, - "--agent-id", AGENT_ID, "--turn-instance-id", original_turn, - "--todo-id", SELECTED_REPLAN_TODO_ID) + "--agent-id", AGENT_ID, "--turn-instance-id", original_turn) rc, recovery = _run_cli(registry, runtime, *guard_args) assert rc == 0 and recovery["replan_action_packet"]["decision"] == "historical_ack_recorded", json.dumps({key: recovery.get(key) for key in ["error", "error_code", "decision", "action_selection_qualification", "health"]}) - rc, automatic = _run_cli(registry, runtime, *guard_args[:-2]) + rc, automatic = _run_cli(registry, runtime, *guard_args) assert rc == 0, automatic assert automatic["heartbeat_receipt"]["settlement_identity"] == original_identity actions = recovery["interaction_contract"]["cli_channel"]["next_cli_actions"] diff --git a/tests/control_plane_ts/quota_settlement_readback.test.ts b/tests/control_plane_ts/quota_settlement_readback.test.ts index 47e74e0911..a7ab75ae78 100644 --- a/tests/control_plane_ts/quota_settlement_readback.test.ts +++ b/tests/control_plane_ts/quota_settlement_readback.test.ts @@ -35,6 +35,11 @@ import { preflightPriorHostTurnCloseout, } from "../../loopx/control_plane/quota/unsettled_host_turn_recovery.ts"; import { requireJsonObject } from "../../loopx/control_plane/runtime_decode.ts"; +import { + AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION, + AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID, + AUTONOMOUS_REPLAN_DIGEST_COMPATIBILITY_STOP_CONDITION, +} from "../../loopx/control_plane/work_items/replan_semantics.ts"; const goalId = "settlement-goal"; const agentId = "codex-settlement"; @@ -94,9 +99,36 @@ test("recorded review source retains its stop clause under the exact selected gu semantic_replan_review_stop_condition: "Stop at owner-only decisions."}; assert.deepEqual(projectSemanticReplanGuard(details).selected_review_source, { kind: details.semantic_replan_review_kind, stop_condition: details.semantic_replan_review_stop_condition}); + const digest = createHash("sha256").update(details.semantic_replan_review_stop_condition, "utf8").digest("hex"); + assert.deepEqual(projectSemanticReplanGuard({semantic_replan_obligation_id: details.semantic_replan_obligation_id, + semantic_replan_review_kind: details.semantic_replan_review_kind, + semantic_replan_review_stop_condition_sha256: digest}).selected_review_source, { + kind: details.semantic_replan_review_kind, stop_condition_sha256: digest}); + assert.deepEqual(projectSemanticReplanGuard({...details, + semantic_replan_review_stop_condition_sha256: digest}).selected_review_source, { + kind: details.semantic_replan_review_kind, + stop_condition: details.semantic_replan_review_stop_condition, + stop_condition_sha256: digest}); + const defaultDigest = createHash("sha256").update( + AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION, "utf8").digest("hex"); + assert.deepEqual(projectSemanticReplanGuard({ + semantic_replan_obligation_id: details.semantic_replan_obligation_id, + semantic_replan_review_kind: "periodic_review_due", + semantic_replan_review_stop_condition: AUTONOMOUS_REPLAN_DIGEST_COMPATIBILITY_STOP_CONDITION, + semantic_replan_review_stop_condition_sha256: defaultDigest, + semantic_replan_review_stop_contract_id: AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID, + }).selected_review_source, { + kind: "periodic_review_due", + stop_condition: AUTONOMOUS_REPLAN_DIGEST_COMPATIBILITY_STOP_CONDITION, + stop_condition_sha256: defaultDigest, + stop_contract_id: AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID, + }); for (const patch of [{semantic_replan_obligation_id: ""}, {semantic_replan_obligation_id: undefined}, {semantic_replan_review_kind: undefined}, {semantic_replan_review_stop_condition: undefined}, {semantic_replan_review_stop_condition: "s".repeat(241)}, + {semantic_replan_review_stop_condition_sha256: "bad"}, + {semantic_replan_review_stop_condition_sha256: "0".repeat(64)}, + {semantic_replan_review_stop_contract_id: AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID}, {semantic_replan_capability_id: "explore"}]) { assert.throws(() => projectSemanticReplanGuard({...details, ...patch})); } @@ -1967,6 +1999,44 @@ test("rejects a guard bound to another Todo", async () => { ); }); +test("a retained Todo selects its original replan guard without rebinding it", async () => { + const obligation = "replan-0000000000000001"; + const bound = settlementIdentity({ + goal_id: goalId, + agent_id: agentId, + todo_id: null, + replan_obligation_id: obligation, + turn_instance_id: turnId, + }); + const runtimeRoot = await fixture(); + try { + const log = join(runtimeRoot, "goals", goalId, "rollout-event-log.jsonl"); + const events = (await readFile(log, "utf8")).trim().split("\n") + .map(line => JSON.parse(line)); + Object.assign(events[0].details, { + todo_id: "", + replan_obligation_id: obligation, + settlement_effect_id: bound.effect_id, + pending_action_selection_todo_id: todoId, + semantic_replan_obligation_id: obligation, + }); + await writeFile(log, events.map(event => JSON.stringify(event)).join("\n") + "\n"); + + const result = await readQuotaSettlement(request(runtimeRoot, { + resolve_original_binding: true, + })); + assert.deepEqual((result.identity as any).result.value, settlementIdentityPayload(bound)); + + const mismatch = await readQuotaSettlement(request(runtimeRoot, { + todo_id: "todo_other", + resolve_original_binding: true, + })); + assert.equal((mismatch.identity as any).result.failure.kind, "identity_mismatch"); + } finally { + await rm(runtimeRoot, {recursive: true, force: true}); + } +}); + test("rejects dual Todo and replan bindings before reading settlement facts", async () => { const runtimeRoot = await fixture({ writeback: true, diff --git a/tests/control_plane_ts/replan_semantics.test.ts b/tests/control_plane_ts/replan_semantics.test.ts index 274fa81016..a549e254f4 100644 --- a/tests/control_plane_ts/replan_semantics.test.ts +++ b/tests/control_plane_ts/replan_semantics.test.ts @@ -1,6 +1,12 @@ import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; import test from "node:test"; -import { projectReplanSemantics, requiredSemanticOutcomes } from "../../loopx/control_plane/work_items/replan_semantics.ts"; +import { + AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION, + AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID, + projectReplanSemantics, + requiredSemanticOutcomes, +} from "../../loopx/control_plane/work_items/replan_semantics.ts"; test("capability evidence is not a default exit or a generic progress claim", () => { for (const outcome of ["capability_evidence_observed", "capability_duty_retired"]) { @@ -175,7 +181,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("only simple reviews have a scalar or digest receipt source", () => { 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.", @@ -184,9 +190,11 @@ test("only plain vision and outcome reviews have a scalar receipt source", () => const request = {operation: "receipt_review_source", agent_id: "reviewer", obligation: source}; assert.deepEqual(projectReplanSemantics(request), {review_source: { kind: "goal_outcome_uncovered", stop_condition: source.stop_condition}}); + assert.deepEqual(projectReplanSemantics({...request, obligation: {...source, + stop_condition: "s".repeat(241), triggers: [{kind: "periodic_review_due"}]}}), {review_source: { + kind: "periodic_review_due", stop_condition: "s".repeat(241)}}); for (const patch of [{required: false}, {schema_version: "unknown"}, {agent_id: "another-agent"}, {triggers: []}, {trigger_count: 2}, {trigger_count: undefined}, - {triggers: [{kind: "periodic_review_due"}]}, {triggers: [{kind: "goal_outcome_uncovered"}, {kind: "external_progress_review_drift"}]}, {trigger_count: 2, triggers: [{kind: "vision_acceptance_gap"}, {kind: "periodic_review_due"}]}, {trigger_count: 2, triggers: [{kind: "vision_acceptance_gap"}, {kind: "external_progress_review_drift"}]}, @@ -195,13 +203,66 @@ test("only plain vision and outcome reviews have a scalar receipt source", () => {progress_baseline: {}}, {progress_window: []}, {triggers: [{kind: "goal_outcome_uncovered", progress_baseline: {}}]}, {triggers: [{kind: "goal_outcome_uncovered", progress_window: []}]}, - {stop_condition: null}, {stop_condition: "s".repeat(241)}, + {stop_condition: null}, {resolution_mode: "todo_lifecycle_settlement"}, {capability_guard: {schema_version: "semantic_replan_capability_guard_v0"}}]) { assert.deepEqual(projectReplanSemantics({...request, obligation: {...source, ...patch}}), {review_source: null}); } }); +test("a digest-bound review keeps its receipt id across equivalent frontier regeneration", () => { + const originalId = "replan-1111111111111111"; + const currentId = "replan-2222222222222222"; + const stop = "Review current acceptance evidence before changing the route. ".repeat(6).trim(); + const digest = createHash("sha256").update(stop, "utf8").digest("hex"); + const current = {schema_version: "autonomous_replan_obligation_v0", required: true, + obligation_id: currentId, agent_id: "reviewer", trigger_count: 1, + triggers: [{kind: "periodic_review_due"}], stop_condition: stop, + replan_context: {context_id: "latest-review-context"}}; + const request = {operation: "receipt_bound_obligation", guard_scoped: true, + replay_phase: "open", selected_obligation_id: originalId, agent_id: "reviewer", + current_obligation: current, transition_candidates: [], receipt_review: { + kind: "periodic_review_due", stop_condition_sha256: digest}}; + const recovered = projectReplanSemantics(request).obligation as JsonObject; + assert.equal(recovered.obligation_id, originalId); + assert.equal(recovered.stop_condition, stop); + assert.equal(recovered.replan_context, undefined, + "the caller must rebuild current context under the retained settlement id"); + assert.equal(recovered.selection_binding, "heartbeat_receipt"); + for (const patch of [ + {receipt_review: {...request.receipt_review, kind: "vision_acceptance_gap"}}, + {receipt_review: {...request.receipt_review, stop_condition_sha256: "0".repeat(64)}}, + {current_obligation: {...current, stop_condition: `${stop} changed`}}, + {current_obligation: {...current, triggers: [{kind: "goal_outcome_uncovered"}]}}, + {current_obligation: {...current, agent_id: "another-agent"}}, + ]) { + assert.throws(() => projectReplanSemantics({...request, ...patch}), /identity conflicts/); + } +}); + +test("a canonical periodic review reconstructs after its live frontier disappears", () => { + const originalId = "replan-1111111111111111"; + const digest = createHash("sha256").update( + AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION, "utf8").digest("hex"); + const request = {operation: "receipt_bound_obligation", guard_scoped: true, + replay_phase: "open", selected_obligation_id: originalId, agent_id: "reviewer", + current_obligation: null, transition_candidates: [], receipt_review: { + kind: "periodic_review_due", stop_condition_sha256: digest, + stop_contract_id: AUTONOMOUS_REPLAN_DEFAULT_STOP_CONTRACT_ID}}; + assert.deepEqual(projectReplanSemantics(request).obligation, { + schema_version: "autonomous_replan_obligation_v0", required: true, + obligation_id: originalId, agent_id: "reviewer", + stop_condition: AUTONOMOUS_REPLAN_DEFAULT_STOP_CONDITION, + triggers: [{kind: "periodic_review_due"}], selection_binding: "heartbeat_receipt"}); + for (const receipt_review of [ + {...request.receipt_review, stop_condition_sha256: "0".repeat(64)}, + {...request.receipt_review, stop_contract_id: "unknown"}, + {...request.receipt_review, kind: "external_progress_review_drift"}, + ]) { + assert.throws(() => projectReplanSemantics({...request, receipt_review}), /identity conflicts/); + } +}); + test("mixed vision/outcome recovery preserves the dominant policy without discharging review", () => { for (const triggers of [[{kind: "vision_acceptance_gap"}], [{kind: "goal_outcome_uncovered"}, {kind: "vision_acceptance_gap"}], diff --git a/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts b/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts index 4bd8ef4448..edff1eface 100644 --- a/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts +++ b/tests/control_plane_ts/unsettled_host_turn_recovery.test.ts @@ -508,6 +508,38 @@ test("an admitted Todo-bound replan without verified writeback does not hold a l } }); +test("an admitted obligation-bound replan without verified writeback does not hold a later guard", async () => { + const turn = "turn-replan"; + const replanId = `replan-${"c".repeat(16)}`; + const identity = settlementIdentity({ + goal_id: GOAL, + agent_id: AGENT, + replan_obligation_id: replanId, + turn_instance_id: turn, + }); + const runtime = await runtimeWith([ + receipt(turn, { + replan_obligation_id: replanId, + semantic_replan_obligation_id: replanId, + settlement_effect_id: identity.effect_id, + closeout_required: false, + ok: true, + should_run: true, + interaction_mode: "autonomous_replan", + must_attempt_work: true, + delivery_allowed: false, + quiet_noop_allowed: false, + }), + ]); + try { + const result = await preflight(runtime.root); + assert.equal(result.status, "none"); + assert.equal(result.reason, "no_prior_turn_requires_closeout"); + } finally { + await runtime.close(); + } +}); + test("the current Turn, other goals, other agents, and other event kinds are ignored", async () => { const runtime = await runtimeWith([ receipt("current-turn", closeoutRequired("current-turn", "todo_current")), @@ -1096,6 +1128,71 @@ test("an autonomous replan binding recovers without a Todo binding", async () => } }); +test("an admitted obligation-bound replan with writeback resumes its original debit", async () => { + const turn = "turn-replan-writeback"; + const replanId = `replan-${"d".repeat(16)}`; + const identity = settlementIdentity({ + goal_id: GOAL, + agent_id: AGENT, + replan_obligation_id: replanId, + turn_instance_id: turn, + }); + const runtime = await runtimeWith([ + receipt(turn, { + replan_obligation_id: replanId, + semantic_replan_obligation_id: replanId, + settlement_effect_id: identity.effect_id, + closeout_required: false, + ok: true, + should_run: true, + interaction_mode: "autonomous_replan", + must_attempt_work: true, + delivery_allowed: false, + quiet_noop_allowed: false, + }), + { + schema_version: "loopx_rollout_event_v0", + event_id: "event-replan-writeback", + event_kind: "refresh_state", + goal_id: GOAL, + agent_id: AGENT, + run_id: turn, + details: {settlement_effect_id: identity.effect_id}, + }, + ]); + try { + const runsRoot = join(runtime.root, "goals", GOAL, "runs"); + await mkdir(runsRoot, {recursive: true}); + await writeFile(join(runsRoot, "index.jsonl"), `${JSON.stringify({ + classification: "state_refreshed", + delivery_outcome: "outcome_progress", + goal_id: GOAL, + agent_id: AGENT, + replan_obligation_id: replanId, + turn_instance_id: turn, + settlement_identity: identity, + })}\n`); + + const result = await preflight(runtime.root); + const candidate = candidateFrom(result); + assert.equal(candidate.binding_kind, "autonomous_replan"); + assert.equal(candidate.binding_id, replanId); + assert.equal(candidate.admitted_replan, true); + assert.deepEqual(result.missing_receipts, ["quota_spend_receipt"]); + + const verdict = reduce( + candidate, + result.missing_receipts as string[], + READ_TODO(null), + ); + assert.equal(verdict.status, "recovery_required"); + assert.equal((verdict.recovery as JsonObject).repair, "resume_prior_turn"); + assert.equal((verdict.obligation as JsonObject).repair, "resume_prior_turn"); + } finally { + await runtime.close(); + } +}); + test("candidate bindings are revalidated before any verdict", async () => { assert.throws( () => reduce({ From dfa6de7a6476e1b485d3f6874fa1a77d004bda93 Mon Sep 17 00:00:00 2001 From: huangruiteng Date: Mon, 16 Nov 2026 15:00:28 +0800 Subject: [PATCH 02/34] fix: isolate agent settlement from peer semantic receipts (#8853) Signed-off-by: huangruiteng --- docs/quota-allocation.md | 6 ++ loopx/control_plane/status/collection.py | 1 + .../work_items/attention_queue.py | 2 + .../control_plane/work_items/project_asset.py | 7 ++- .../work_items/replan_history_settlement.ts | 11 ++-- loopx/status.py | 4 +- .../test_effective_turn_replan.py | 58 +++++++++++++++++++ .../quota_settlement_readback.test.ts | 47 +++++++++++++++ 8 files changed, 129 insertions(+), 7 deletions(-) diff --git a/docs/quota-allocation.md b/docs/quota-allocation.md index aaea5084de..2a853eddd6 100644 --- a/docs/quota-allocation.md +++ b/docs/quota-allocation.md @@ -124,6 +124,12 @@ same Agent after its latest accepted replan acknowledgement. An accepted negativ work result can count; observation-only polls, duplicate retries, unspent writebacks and missing settlement receipts cannot. At the threshold, the next quota evaluation creates `periodic_review_due` through the existing replan path. +For an explicitly selected Agent, receipt qualification uses that Agent's scope +before interpreting semantic settlement fields, including during status collection +for refresh and spend. A peer's invalid semantic receipt +does not block this count; selecting the affected Agent or an unscoped history +still reports the invalid receipt. This does not rewrite receipts or relax Goal +instance ownership and shared log integrity checks. An evidence-linked review may retain the current approach; it does not require inventing a new plan, completing the open Todo, or declaring the Goal achieved. Review evidence from settled work for current source and acceptance applicability diff --git a/loopx/control_plane/status/collection.py b/loopx/control_plane/status/collection.py index 7ddd538a4b..8d65923da3 100644 --- a/loopx/control_plane/status/collection.py +++ b/loopx/control_plane/status/collection.py @@ -187,6 +187,7 @@ def canonical_runtime_for_goal(current_goal_id: str) -> Path: current_registry=registry, registry_path=registry_path, include_goal_channel_projection=include_presentation_projections, + agent_lane_id=agent_lane_id, ) # No later projection consumes the shared status snapshot. del todo_snapshot diff --git a/loopx/control_plane/work_items/attention_queue.py b/loopx/control_plane/work_items/attention_queue.py index fb9976b9b9..a9c21a41e7 100644 --- a/loopx/control_plane/work_items/attention_queue.py +++ b/loopx/control_plane/work_items/attention_queue.py @@ -190,6 +190,7 @@ def build_attention_queue( goal_id_filter: str | None = None, include_stopped_goal_context: bool = False, include_goal_channel_projection: bool = True, + agent_lane_id: str | None = None, ) -> dict[str, Any]: """Build the executable attention queue or a stopped-Goal archive view. @@ -336,6 +337,7 @@ def build_attention_queue( autonomous_replan_obligation_from_runs=context.autonomous_replan_obligation_from_runs, external_progress_review=external_progress_review, effective_turn_cadence=effective_turn_cadence_context(goal, runtime_root, registry_path=context.registry_path), + agent_lane_id=agent_lane_id, ) item["quota"] = context.quota_status( goal, diff --git a/loopx/control_plane/work_items/project_asset.py b/loopx/control_plane/work_items/project_asset.py index 7b07aa09ee..44dc44bc65 100644 --- a/loopx/control_plane/work_items/project_asset.py +++ b/loopx/control_plane/work_items/project_asset.py @@ -259,6 +259,7 @@ def attach_active_state_project_asset_fields( autonomous_replan_obligation_from_runs: Callable[..., dict[str, Any] | None] | None = None, external_progress_review: dict[str, Any] | None = None, effective_turn_cadence: dict[str, Any] | None = None, + agent_lane_id: str | None = None, ) -> dict[str, Any]: project_asset = item.get("project_asset") if not isinstance(project_asset, dict): @@ -322,7 +323,10 @@ def attach_active_state_project_asset_fields( shared_todos = item.get("agent_todos") monitor_todos = {"monitor_open_items": shared_todos.get("monitor_open_items", []) if isinstance(shared_todos, dict) else []} - agent_ids = sorted( + # Status retains shared Goal history for ownership and health checks. + # A lane caller only needs that Agent's semantic review obligations; + # peer receipts must not become a prerequisite for its settlement. + agent_ids = [agent_lane_id] if agent_lane_id is not None else sorted( { agent_id for run in latest_runs or [] @@ -351,6 +355,7 @@ def attach_active_state_project_asset_fields( replan_obligation = autonomous_replan_obligation_from_runs( latest_runs or [], agent_todos=item.get("agent_todos") if isinstance(item.get("agent_todos"), dict) else None, + agent_id=agent_lane_id, ) if replan_obligation: item["autonomous_replan_obligation"] = replan_obligation diff --git a/loopx/control_plane/work_items/replan_history_settlement.ts b/loopx/control_plane/work_items/replan_history_settlement.ts index b09652f49e..be0578ff4b 100644 --- a/loopx/control_plane/work_items/replan_history_settlement.ts +++ b/loopx/control_plane/work_items/replan_history_settlement.ts @@ -1,6 +1,6 @@ /** Replan history IO: qualify work through the existing settlement owner. */ import type { JsonObject } from "../effect_program.ts"; -import { jsonObject, requireJsonObject } from "../runtime_decode.ts"; +import { jsonObject, optionalNonEmptyString, requireJsonObject } from "../runtime_decode.ts"; import { readQuotaSettlementSnapshot, readQuotaSettlementForAdmittedOwnerFromSnapshot, @@ -17,6 +17,7 @@ export async function projectSettledReplanHistory(value: unknown): Promise { const row = jsonObject(raw); return row !== null && typeof row.agent_id === "string" && row.agent_id !== "" && + (agentId === null || row.agent_id === agentId) && typeof row.turn_id === "string" && row.turn_id !== "" && receiptKeys.has(JSON.stringify([row.agent_id, row.turn_id])); }); - // History rows without a matching should-run receipt cannot be qualified - // by settlement. Avoid parsing the strict quota run ledger unless at least - // one exact current-owner Turn can contribute to effective-cadence counts. + // Apply explicit Agent scope before interpreting settlement receipts, as + // the pure history owner does before counting work. Unscoped requests still + // validate all contributing Agents; a scoped read cannot repair peer data. + // Rows without a matching should-run receipt cannot contribute either. if (settlementRuns.length === 0) { return projectReplanHistory({...request, runs}, new Set()); } diff --git a/loopx/status.py b/loopx/status.py index c64de931c4..cbb5528f2f 100644 --- a/loopx/status.py +++ b/loopx/status.py @@ -228,8 +228,6 @@ attach_dependency_blockers, compact_todo_group as compact_todo_group, compact_todo_item as compact_todo_item, - open_todo_items as open_todo_items, - todo_item_task_class as todo_item_task_class, first_open_todo_text, open_todo_items as open_todo_items, project_asset_todo_summary, @@ -1093,6 +1091,7 @@ def build_attention_queue( current_registry: dict[str, Any] | None = None, registry_path: Path | None = None, include_goal_channel_projection: bool = True, + agent_lane_id: str | None = None, ) -> dict[str, Any]: def request_active_state_todo_fields( goal: dict[str, Any], @@ -1161,6 +1160,7 @@ def request_active_state_todo_fields( goal_id_filter=goal_id_filter, include_stopped_goal_context=include_stopped_goal_context, include_goal_channel_projection=include_goal_channel_projection, + agent_lane_id=agent_lane_id, ) if runtime_root is not None: for item in queue.get("items") or []: diff --git a/tests/control_plane/test_effective_turn_replan.py b/tests/control_plane/test_effective_turn_replan.py index 0e449180e8..03fba2571b 100644 --- a/tests/control_plane/test_effective_turn_replan.py +++ b/tests/control_plane/test_effective_turn_replan.py @@ -36,6 +36,64 @@ ) +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_refresh_and_spend_survive_an_unrelated_invalid_review_receipt(tmp_path, monkeypatch, provider): + isolate_sqlite_runtime(tmp_path, monkeypatch) + project, runtime, registry = _write_fixture(tmp_path, provider=provider) + rc, configured = _run_cli(registry, runtime, "configure-goal", "--goal-id", GOAL_ID, + "--execution-stride-strategy", "off", "--execution-replan-after-turns", "6", "--execute") + assert rc == 0, configured + turn = "peer-isolation-turn" + 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) + assert rc == 0 and guard["should_run"], guard + log = runtime / "goals" / GOAL_ID / "rollout-event-log.jsonl" + original = log.read_text() + receipt = next(json.loads(line) for line in original.splitlines() + if json.loads(line)["event_id"] == guard["heartbeat_receipt"]["event_id"]) + peer = "codex-peer-fixture" + peer_turn = "peer-invalid-review" + peer_effect = f"{GOAL_ID}:{peer}:{TODO_ID}:{peer_turn}" + peer_receipt = {**receipt, "event_id": "peer-invalid-receipt", "agent_id": peer, + "run_id": peer_turn, "details": {**receipt["details"], + "settlement_effect_id": peer_effect, + "semantic_replan_obligation_id": "replan-0000000000000001", + "semantic_replan_review_kind": "periodic_review_due", + "semantic_replan_review_stop_condition": None}} + with log.open("a") as stream: + stream.write(json.dumps(peer_receipt) + "\n") + index = runtime / "goals" / GOAL_ID / "runs" / "index.jsonl" + index.parent.mkdir(parents=True, exist_ok=True) + peer_run_path = index.parent / "peer-run.json" + peer_markdown_path = index.parent / "peer-run.md" + peer_run = {"goal_id": GOAL_ID, "agent_id": peer, + "turn_instance_id": peer_turn, "classification": "state_refreshed", + "generated_at": "2026-01-01T00:00:00Z", "delivery_outcome": "outcome_progress", + "json_path": str(peer_run_path), "markdown_path": str(peer_markdown_path), + **({"goal_ref": receipt["goal_ref"]} if "goal_ref" in receipt else {})} + peer_run_path.write_text(json.dumps(peer_run)) + peer_markdown_path.write_text("Synthetic peer work.\n") + with index.open("a") as stream: + stream.write(json.dumps(peer_run) + "\n") + rc, refreshed = _run_cli(registry, runtime, "refresh-state", *binding, + "--classification", "validated_progress", "--delivery-batch-scale", "implementation", + "--delivery-outcome", "outcome_progress", "--delivery-boundary", "in_flight_continuation", + "--no-global-sync", "--suppress-external-sinks", cwd=project) + assert rc == 0 and refreshed["appended"], refreshed + for _ in range(2): + rc, spent = _run_cli(registry, runtime, "quota", "spend-slot", *binding, + "--slots", "1", "--source", "heartbeat", "--execute") + assert rc == 0, json.dumps(spent) + assert _spend_run_count(runtime) == 1 + assert _heartbeat_receipt_count(runtime, turn) == 1 + for agent_args in ([], ["--agent-id", peer]): + rc, rejected = _run_cli(registry, runtime, "status", "--goal-id", GOAL_ID, *agent_args) + assert rc == 1 and "receipt review stop condition" in rejected["error"], rejected + final_receipts = [json.loads(line) for line in log.read_text().splitlines()] + assert receipt in final_receipts and peer_receipt in final_receipts + + @pytest.mark.parametrize("threshold_override,device_count,threshold,declare_outcome", [ (None, None, 6, False), (None, None, 6, True), (2, 3, 2, False), (None, 3, 3, False), (5, 6, 5, False), (None, 5, 5, False), (6, None, 6, False), (None, 6, 6, False), diff --git a/tests/control_plane_ts/quota_settlement_readback.test.ts b/tests/control_plane_ts/quota_settlement_readback.test.ts index 47e74e0911..c356e00846 100644 --- a/tests/control_plane_ts/quota_settlement_readback.test.ts +++ b/tests/control_plane_ts/quota_settlement_readback.test.ts @@ -2220,6 +2220,53 @@ test("effective cadence scopes settlement and ACKs to its admitted Goal instance } }); +test("agent cadence does not interpret another agent's malformed semantic receipt", async () => { + const { projectSettledReplanHistory } = await import( + "../../loopx/control_plane/work_items/replan_history_settlement.ts"); + const root = await fixture({writeback: true, spend: true, completion: true}); + const peer = "codex-peer"; + const peerTurn = "peer-turn"; + const peerIdentity = settlementIdentity({goal_id: goalId, agent_id: peer, + todo_id: todoId, turn_instance_id: peerTurn}); + const log = join(root, "goals", goalId, "rollout-event-log.jsonl"); + const row = (agent: string, turn: string) => ({agent_id: agent, public_agent_id: agent, + monitor_agent_id: agent, turn_id: turn, classification: "state_refreshed", + generated_at: "2026-01-01T00:00:00Z", observed_at: 1, + accepted_ack: false, progress: null, monitor: {}}); + const cadence = {schema_version: "replan_history_request_v0", operation: "periodic", + agent_id: agentId, monitor_agent_id: agentId, neutral_classifications: [], + stall_threshold: 2, periodic_threshold: 1, monitor_threshold: 6, + streak_threshold: 5, monitor_schema: "dead_monitor_repeat_v0", + todos: {monitors: [], advancements: [], resume: null}, + settlement_source: {runtime_root: root, goal_id: goalId}, + runs: [row(peer, peerTurn), row(agentId, turnId)]}; + try { + const baseline = await projectSettledReplanHistory({...cadence, runs: cadence.runs.slice(1)}); + assert.equal(requireJsonObject(baseline.trigger, "trigger").run_count, 1); + await appendFile(log, JSON.stringify({schema_version: "loopx_rollout_event_v0", + event_id: "malformed-peer-review", event_kind: "quota_should_run", + goal_id: goalId, agent_id: peer, run_id: peerTurn, details: { + todo_id: todoId, settlement_effect_id: peerIdentity.effect_id, + semantic_replan_obligation_id: "replan-0000000000000001", + semantic_replan_review_kind: "periodic_review_due", + semantic_replan_review_stop_condition: null, + }}) + "\n"); + const originalLog = await readFile(log, "utf8"); + assert.deepEqual(await projectSettledReplanHistory(cadence), baseline); + assert.equal((await projectSettledReplanHistory({...cadence, + runs: cadence.runs.slice(0, 1)})).trigger, null, "peer work cannot qualify this agent"); + await assert.rejects(projectSettledReplanHistory({...cadence, agent_id: peer}), + /receipt review stop condition/, "the affected agent still sees its own invalid receipt"); + await assert.rejects(projectSettledReplanHistory({...cadence, agent_id: null}), + /receipt review stop condition/, "unscoped history still validates every contributing agent"); + await assert.rejects(projectSettledReplanHistory({...cadence, agent_id: 42}), + /agent_id/, "invalid scope must not silently become unscoped"); + assert.equal(await readFile(log, "utf8"), originalLog, "readback cannot repair historical receipts"); + } finally { + await rm(root, {recursive: true, force: true}); + } +}); + test("effective cadence skips settlement read without a matching should-run receipt", async () => { const { projectSettledReplanHistory } = await import( "../../loopx/control_plane/work_items/replan_history_settlement.ts"); From f599a60ce64fd78db80d6318c84e3af58bfc1df9 Mon Sep 17 00:00:00 2001 From: Ikko Eltociear Ashimine Date: Mon, 16 Nov 2026 16:04:16 +0900 Subject: [PATCH 03/34] docs: add Japanese README (#8839) * docs: add Japanese README Signed-off-by: Ikko Eltociear Ashimine * docs: sync Japanese README with Goal lifecycle and route it through README checks Signed-off-by: Ikko Eltociear Ashimine --------- Signed-off-by: Ikko Eltociear Ashimine --- .github/workflows/frontstage-pages.yml | 2 + README.ja.md | 978 +++++++++++++++++++++++++ README.md | 2 +- README.zh-CN.md | 2 +- examples/readme-star-history-smoke.py | 1 + scripts/ci/impact_plan.py | 2 +- scripts/ci/test_impact_plan.py | 8 + tests/test_readme_presentation.py | 2 +- 8 files changed, 993 insertions(+), 4 deletions(-) create mode 100644 README.ja.md diff --git a/.github/workflows/frontstage-pages.yml b/.github/workflows/frontstage-pages.yml index 80e1783366..0ef56a9f1d 100644 --- a/.github/workflows/frontstage-pages.yml +++ b/.github/workflows/frontstage-pages.yml @@ -12,6 +12,7 @@ on: - ".github/workflows/frontstage-pages.yml" - "README.md" - "README.zh-CN.md" + - "README.ja.md" - "apps/presentation/dashboard/**" - "scripts/chat_bundle.py" - "scripts/chat_bundle_launcher.mjs" @@ -56,6 +57,7 @@ on: - ".github/workflows/frontstage-pages.yml" - "README.md" - "README.zh-CN.md" + - "README.ja.md" - "apps/presentation/dashboard/**" - "scripts/chat_bundle.py" - "scripts/chat_bundle_launcher.mjs" diff --git a/README.ja.md b/README.ja.md new file mode 100644 index 0000000000..b6cfa16a38 --- /dev/null +++ b/README.ja.md @@ -0,0 +1,978 @@ +
+ +

LoopX 2.0

+ +LoopX — Trendshift が記録した GitHub Trending #1 + +**1 つのゴールから始めましょう。長時間にわたるエージェントの作業を検証可能に保ちます。** + +すでに使っている Agent に、やってほしいことを伝えるだけです。LoopX はゴール、 +進捗、意思決定、検証可能な結果を保持するため、セッションをまたいで作業を続けられます。
+Codex、Claude Code、DeepSeek Harness などのランタイムにまたがる 24 時間 365 日常駐エージェントのための、オープンでハーネス非依存のコントロールプレーンです。ローカルの SQLite ワークフローからマルチエージェントチームまでスケールします。 + +[![Release](https://img.shields.io/github/v/release/loopx-project/loopx?color=brightgreen)](https://github.com/loopx-project/loopx/releases/latest) [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE) [![TypeScript core](https://img.shields.io/badge/core-TypeScript-3178C6?logo=typescript&logoColor=white)](docs/architecture/rfcs/typescript-control-plane-migration-v0.md) [![Python](https://img.shields.io/badge/python-3.11%2B-blue.svg)](pyproject.toml) + +[![Local-first state](https://img.shields.io/badge/local--first-state-brightgreen.svg)](docs/public-private-boundary.md) [![Loop Agents](https://img.shields.io/badge/status-loop%20agents%20supported-brightgreen.svg)](docs/product/release-readiness.md) [![GitHub contributors](https://img.shields.io/github/contributors-anon/loopx-project/loopx?color=blue)](https://github.com/loopx-project/loopx/graphs/contributors) [![Discord](https://img.shields.io/badge/Discord-Join-5865F2?logo=discord&logoColor=white)](https://discord.gg/XmGgQyCFZd) [![Listed on dsh-plugin.org](https://dsh-plugin.org/badges/listed.svg)](https://dsh-plugin.org/plugins/loopx-project/loopx) + +[はじめる](#quick-start) · [なぜ LoopX なのか](#なぜ-loopx-なのか) · [24/7 常駐](#always-on-agents) · [動作を見る](#loopx-の動作を見る) · [エビデンス](#エビデンス) · [コントリビュート](#contributing) · [ドキュメント](https://loopx-project.github.io/loopx/docs/) · [English](README.md) · [简体中文](README.zh-CN.md) + +
+ +--- + + + +## 1 つのゴールから始める + +**1. LoopX を一度インストールします。** Python 3.11 以降と +Node.js 22.22.3 以降(24 LTS を推奨)が必要です。 + +```bash +python3 -m pip install --upgrade loopx && loopx workflow-skills --install +``` + +[インストールオプションとアップグレード](docs/guides/installing-loopx.md) + +**2. Agent に望む成果を伝えます。** Agent ホストを再起動してプロジェクトを開き、 +自然言語のゴールを 1 つ送ります: + +```text +$loopx speed up this project's slowest workflow; compare approaches and verify the gains +``` + +LoopX はそれを、目に見える計画、あなたの判断が必要な意思決定、そして必要に応じた検証へと変換します。 +Claude Code では `/loopx ` を使います。その他のランタイムについては +[ホストのセットアップ](#使っている-agent-から始める)をご覧ください。ガイド付きで最初の +実行を試したい場合は、[下のタスクから選んでください](#loopx-を試す)。 + +## 📰 ニュース + + + +- **2026-11-01 · 📘 [24/7 エージェントのための段階的コンテキスト配信](https://loopx-project.github.io/loopx/blog/context-delivery/)** — コンパクションサイクル単位の構造化差分と段階的ビューにより、モデル向けの 213 回の起床にわたってコントロールプレーンのバイト数を削減しました(単層の段階的ビューで `32.5%`、サイクル内の 112 回の継続にわたる積み重ね型の初回読み取りエントリで本文 `3.0%` / CLI `5.0%`)。同時に、ロスレスなベースライン復元も維持しています([47 日間の鮮度に関するポストモーテム](https://loopx-project.github.io/loopx/blog/five-freshness-incidents/))。 +- **2026-10-25 · 🤝 [長期タスク向けエージェント研究:共同研究者を募集中](https://loopx-project.github.io/loopx/blog/edgebench-feedback-and-memory/)** — EdgeBench と実世界のタスクにおけるフィードバック、再計画、再現可能な評価を通じて、**同一予算下での長期的な成果のブレークスルー**を探求しています。学術研究室や企業のチームとの共同研究を歓迎し、成果と総コストを比較していきます。[共同研究について話し合う](https://github.com/loopx-project/loopx/discussions)。 +- **2026-10-20 · 🚀 [LoopX 2.0](https://github.com/loopx-project/loopx/releases/tag/v2.0.0)** — 共有の TypeScript 状態レイヤーと、長期研究用ワークベンチ。[移行ガイド](docs/product/releases/v2.0.0.md) · [エンジニアリングの舞台裏](https://loopx-project.github.io/loopx/blog/typescript-control-plane-refactor/)。 +- **2026-10-14 · 🤖 [エージェント型ソフトウェア](https://loopx-project.github.io/loopx/blog/semantic-software-development/)** — ゴール、エビデンス、人間のフィードバックをエンジニアリングの成果物につなげます。 +- **2026-10-02 · 🧠 [長期ブートストラップと RSI](https://loopx-project.github.io/loopx/blog/long-horizon-rsi/)** — LoopX を使って LoopX を開発した 4 か月間:公開されているプロジェクトの歴史と、システムレベルの自己改善に関する設計上の視点。 +- **2026-09-18 · 🧪 [LoopX × LHTB](https://loopx-project.github.io/loopx/benchmarks/lhtb/)** — 同一モデルによる 46 タスクの研究が、**より多くの計算資源をより良い成果に変える**ことを実証的に裏付けました。LoopX 1.0.3 Heartbeat による継続実行の平均報酬は、Plain Codex より 17.3%、ネイティブ Goal より 10.6% 高くなりました。 +- **2026-09-06 · 🎉 [LoopX 1.0](https://github.com/loopx-project/loopx/releases/tag/v1.0.0)** — セッションをまたいでエージェントの作業を追跡し、意思決定に対応し、納品された結果を確認できる Personal Agent Workspace。[Workspace ガイド](https://github.com/loopx-project/loopx/blob/v1.0.0/docs/guides/personal-workspace-user-guide.md)。 +- **2026-08-02 · ⏱️ [200 時間超のトラジェクトリを X で公開](https://x.com/huangruiteng/status/2083904257494024425)** — 経過時間 200 時間超にわたるオーナー実行の初期事例が X で大きな注目を集め、実行、待機、人間の判断、復旧にまたがる作業を示しました。[トラジェクトリとその限界](#オープンソースの-issue-修正)。 + +[すべてのリリース](https://github.com/loopx-project/loopx/releases) + + + +## なぜ LoopX なのか + +最初の有用なアクションの前に、LoopX の内部を学ぶ必要はありません。 +デフォルトの進め方はゴールファーストです。望む成果を記述し、Agent に計画を +準備させ、意思決定やデバッグが必要になったときにだけ内部の仕組みを確認します。 + +エージェントは 1 つのセッションでタスクを完了できます。しかし長時間にわたる作業はもっと難しくなります。 +目的は変わり、オーナーの判断が必要になり、エビデンスは古くなり、エージェントは +作業を仲間に引き継ぎます。タイマーはループを動かし続けられますが、それだけでは有用になりません。 + +- **最初からやり直さずに続けられます。** ゴール、受け入れ済みの要件、 + 意思決定、エビデンスを、セッションや再起動をまたいで保持します。 +- **[コンテキストを失わずに連携できます。](https://loopx-project.github.io/loopx/blog/agent-facing-kanban/#walkthrough)** 委任された作業、依存する + 成果物、レビュー、返された結果を、責任の所在を明確にしたまま結び付けます。 +- **何が変わったかで進捗を判断できます。** 単なる活動量ではなく、成果物、裏付けとなる + エビデンスと反証となるエビデンス、コスト、次の意思決定を確認します。 + +モデルとツールは Agent ランタイムが提供します。LoopX の永続的な状態レイヤーは、 +エンジニアリング、研究、運用のために、ゴール、受け入れ基準、意思決定、オーナーシップ、エビデンスを +セッションやランタイムをまたいで保持します。エージェントと人間はそれらの事実を使って、 +次に何をすべきかを見直します。保護されたアクションは、引き続き人間の承認によって統制されます。 + + + +| やりたいこと | ここから始める | +| --- | --- | +| コーディングや研究のエージェントをセッションをまたいで働かせ続ける | [インストールして始める](#quick-start) | +| 個人のプロジェクト、スケジュール、意思決定を 1 か所で管理する | [Personal Agent Workspace](#personal-agent-workspace-の紹介) | +| インフラを自作せずに 24/7 常駐エージェントを構築する | [24/7 常駐エージェントのコントロールプレーン](#always-on-agents) | +| エージェント同士を連携させ、検証可能な結果を納品させる | [エージェント連携ガイド](docs/product/use-cases/cross-runtime/README.md) | + +LoopX は、永続的な作業状態を 1 つのコンパクトなレイヤーに保持します: + + + + ハーネスがターンを実行し、LoopX が作業を引き継ぎます。 + + +作業を実行するのは Agent ランタイムです。LoopX は、エンジニアリング、研究、探索、 +運用のループが実行をまたいで継続できるようにするための状態を保持します。これは +新たなエージェントフレームワークでも、特定プロバイダー向けのオーケストレーションランタイムでもありません。 + +
+詳細:コントロールレイヤーの仕組みと位置付け + + + + 単なるタイマーではなく、統制された状態遷移の詳細ビュー。 + + +わかりやすいメンタルモデルは、 +**[長時間作業のためのエージェントネイティブなカンバン](docs/development/control-plane-course/00-concept-primer.md)** です。 +カードはアイデンティティ、権限、エビデンス、継続情報を持ちます。カードの移動は、claim、gate、monitor、 +writeback などの検証済みオペレーターです。ボードは投影(プロジェクション)であり、 +信頼できる唯一の情報源は LoopX の状態です。 + +登録されたエージェントは対等な仲間です。claim、リース、タスク境界、ケイパビリティ、 +型付きの継続によって次に誰が動くかが決まり、永続的なリーダーのアイデンティティは +必要ありません。 + +LoopX は、次のようなものを実行するときに役立ちます: + +- 複数日にわたるエンジニアリング、研究、ベンチマーク、実験の目標 +- スコープ、エビデンス、レビュー状態を保持しなければならない Issue や PR のループ +- 定期的な heartbeat や監視の作業 +- オーナー、安全性、公開、プライベートデータのゲートがあるプロジェクト +- オーナーシップ、リース、引き継ぎが重要になる対等なエージェントのチーム +- 進捗がエンジニアではないオペレーターにも理解できる状態でなければならない、 + クリエイター、研究、運用のワークフロー + +LoopX は自律的な本番コントローラーではありません。危険な権限、 +公開、本番環境への書き込み、最終的なオーナーシップは人間に残ります。 + +
+ + + +### 5 つのサブシステムを自作せずに 24/7 常駐エージェントを構築する + +[Anthropic の *Building always-on agents*](https://claude.com/resources/articles/building-always-on-agents) +では、効果的な常駐エージェントは実時間のほとんどをトークン消費ゼロで +スリープして過ごし、周囲の 5 つのサブシステム(**Event Inbox**、**Timers**、**Memory**、**Permissions**、 +**Outbox**)にまたがる限定的な実行のために起床すると指摘しています。実際には、チームはエージェントホストごとに、 +cron スクリプト、JSON キュー、プロンプトのコンパクション、場当たり的な承認チェックといった壊れやすい +つなぎのコードを作り直しがちで、2 週目には気付かないうちの陳腐化、重複書き込み、ノイズの多いステータス通知に +悩まされることになります。 + +LoopX はそのオープンでハーネス非依存のコントロールレイヤーをパッケージ化しているため、 +すでに使っているランタイム(Codex CLI、Claude Code、DeepSeek Harness、カスタムランナー)を、 +明示的な永続性と人間によるゲートを備えた状態で 24 時間 365 日運用できます: + +| 常駐のためのプリミティブ | LoopX コントロールプレーンのサブシステム | 数週間にわたる実行で防げること | +| --- | --- | --- | +| **Event Inbox** | シングルフライトの **Goal / Todo / Hard Lease** キュー、非同期のアプリ受信箱、接続済みの Lark / GitHub リスナー | 同じリポジトリの状態を重複したワーカーが変更すること、実行中に人間の入力が失われること | +| **Timers & Wakeups** | **Guarded Heartbeat** と安全な定期プリセット(`daily-triage`、`changelog-draft`、`pr-watch`。`loopx preset list` で確認) | 何も変わっていない、またはゲートがまだ人間を待っているのに、やみくもな cron ループがトークンを浪費すること | +| **Memory & Compaction** | [`Decision Context`](loopx/capabilities/decision_context/README.md) によるソース追跡、コンパクションサイクル単位の **Progressive + Delta** [`context_delivery`](https://loopx-project.github.io/loopx/blog/context-delivery/)(213 回の起床にわたるビューで `32.5%`、サイクル内の 112 回の起床にわたる初回読み取りエントリで `3.0% / 5.0%`)、およびオプションの [`Reward Memory`](loopx/capabilities/reward_memory/README.md) | 気付かないうちのソースの陳腐化([47 日間・253 ターンで 5 件のインシデント](https://loopx-project.github.io/loopx/blog/five-freshness-incidents/))、コンパクションをまたいだプロンプトの再送の累積や差分の破損 | +| **Permissions & Deferred Calls** | 明示的な**オーナー、安全性、公開、プライベートデータのゲート**、分離されたワークツリー、レビュー可能な引き継ぎ | 自律的な cron 実行による外部への公開、強制マージ、プライベートなコンテキストの漏洩 | +| **Outbox** | 本当の完了、ブロッカー、未解決のゲートを示す、共有 Workspace と Lark の Goal/Decision/Evidence ビュー | 断片化したターミナルログや、人間に実行の子守りを強いるノイズの多い通知 | + +**個人のローカル利用からチーム・組織規模まで、1 つの契約セットで:** + +| 規模 | LoopX の動かし方 | 権限、連携、ガバナンスの姿勢 | +| --- | --- | --- | +| **個人開発者とローカルエージェント** | 外部インフラなしで `python3 -m pip install --upgrade loopx`。既存の CLI/デスクトップのランタイムを接続します | ハードリースを備えた正規のローカル **SQLite** ストア(既存の File セットアップも利用可)。アイドル時のトークン消費ゼロで、再起動やコンパクションを乗り越えます | +| **エンジニアリングチームとエンタープライズへの道筋** | 対等なワーカーが todo を claim し、Desktop Workspace、GitHub、Lark を通じて共有ゴール上でレビュー済みの成果物を返します([3 エージェントのデモ](examples/collaboration-delivery/README.md)) | ワーカーのオーナーシップと人間の承認ゲートを維持します。段階的な **PostgreSQL** リモート権限([プロバイダー](loopx/control_plane/coordination/postgresql_authority_store.ts))や、計画中の組織ガバナンス(RBAC、SSO、クォータ、署名付き監査)へと拡張できます。詳しくは[導入と SaaS のロードマップ](docs/product/roadmaps/saas-opportunity-assessment.md)をご覧ください | + +## LoopX の動作を見る + +企業のチーム、研究組織、個人開発者のための長期研究ワークベンチである LoopX 2.0。記録された研究履歴と、縮尺どおりに描き直したカウントリボンを、ラベル付きのアプリフレーム内に表示
+研究ワークベンチの動画を見る · 86 秒 · 文字起こし、字幕、出典。録画したアプリの抜粋、合成例、イラストにはラベルを付けています。 + + + +### 継続できる研究 + +ゴールを開くと、その作業、担当の Agent、記録された Explore の履歴を確認できます。 +**Deep analysis** を使うと、登録されたトラジェクトリ、その引用、ネイティブな +結果の読み戻し、個別に計測されたコストを確認できます。研究画面では、出典に紐付いた +[人間の注意](docs/reference/trajectory-assessment.md#human-attention-in-research-optional)も、 +記録された入力、タイミングのカバレッジ、確認可能な最適化ポイントとともに表示されます。ソースは Agent が準備します。 +ページを開いたり結果を読んだりしても、モデル呼び出しは始まりません。明示的な分析では、 +設定されたホストとアカウントが使われます。 + +裏付けられた発見、反証された発見、不明な発見を見える状態に保ちます。アプローチが +変更された理由や継続された理由を確認し、そのエビデンスを次の試行に持ち込みます。 + +### Personal Agent Workspace の紹介 + +長期的なゴールを 1 つのローカルファーストなワークスペースに保持します。ゴール、注意、 +会話、タスク、ファイル、スケジュール、復旧は、日をまたいでも、再起動しても、 +ハーネスが変わっても失われません。プロジェクトを再び開き、前のターンの状態と +エビデンスを確認して、次に許可されたアクションを続けます。 + +新しいデフォルトの Codex ゴールでは、タスクとワークスペースの権限を確認してから +**Create Goal and start first run** を確定します。元のタスクは、独立したチェックと実行枠の準備が整うと +元の会話の中で続行されます。通常のメッセージは読み取り専用のままです。待機、結果の読み戻し、 +復旧については[ワークスペースガイド](docs/guides/personal-workspace-user-guide.md#在原对话执行已检查的任务--run-the-checked-original-task)に +従ってください。 + +LoopX は、こうした長期的なコントロール状態を Personal Workspace にまとめます。1 か所で次のことができます: + +- 何があなたを必要としているか、何が実行中か、何が監視中か、何が + スケジュール済みまたは停止中かを確認する +- ゴールのケイパビリティを設定し、マシンのデフォルトとゴールごとの上書きを区別し、 + 適用前に変更をプレビューする +- 実行中のターンを誘導したり、メッセージをキューに入れたり、接続済みの Lark 会話から + 明示的な Goal/Agent/セッションのルーティングで非同期受信箱を使ったりする +- 納品されたファイル、定期レポート、それらを裏付けるエビデンスを確認する +- Codex、Claude Code、モデル直接利用、その他の登録済み Agent セッションをまたいで、 + ゴールの状態やエビデンスを失わずに作業を続ける +- 型付きのプレビュー、明示的な確認、受領記録を通じて保護された変更をレビューする。 + その間も、権威を持つのはブラウザーではなく LoopX の状態です + +Manager のグループ会話では、LoopX はメッセージの可視性とターンの権限を分けて +扱います。バイリンガルの [Lark Manager のコンテキストと権限の +契約](docs/reference/protocols/lark-manager-context-authority-v0.md)をご覧ください。 + +```bash +loopx dashboard +``` + +`loopx dashboard` がサポートされているブラウザー/PWA の起動方法です。ネイティブのデスクトップ版 +プレビューを[現在のリリース](https://github.com/loopx-project/loopx/releases/latest)からダウンロードすることもできます。 +これらは同じループバックサービスとゴールの状態を再利用します。Apple Silicon の macOS では、 +シェルと同梱のランタイムを組み合わせる署名付きのアプリ更新に加え、修復と +復旧にも対応しています。Python 3.11 以降が必要です。アプリは ad-hoc 署名のみで、公証はされていません。 +Windows の NSIS プレビューインストーラーは main チャネルで同じ署名付きの更新フローに対応しており、 +CLI は別途インストールします。stable チャネルと MSI でのインストールは +手動のままで、アプリのロールバックと修復は利用できず、インストーラーには +Authenticode 証明書がありません(Windows で SmartScreen の確認が表示される場合があります)。 +[デスクトップ版のインストール、更新、ソースからの開発](apps/desktop/loopx-control-plane/README.md)。 + +実際の Workspace の録画:子タスクの容量と、許可された責任範囲を設定する + +
+ケイパビリティ設定と再現可能なワークスペースのシナリオ + +ソースをチェックアウトした状態で `python -m demo.workspace serve` を実行すると、コミュニティ +イベント、家庭のエネルギー比較、地域の Web サイト公開の 3 つを試せます。それぞれに 4 つの +作業ロール、18 個のタスク、2 つの意思決定、2 つの監視があります。上のスクリーンショットは +この再現可能なワークスペースのものです。[シナリオとリプレイの手順](demo/workspace/README.md)。 + +
+ +[32 秒のウォークスルー全編を見る](docs/assets/personal-workspace/loopx-dashboard-launch.mp4) +· [ワークスペースガイドを読む](docs/guides/personal-workspace-user-guide.md) +· [5 分間のツアーを試す](docs/guides/personal-workspace-trial-guide.md) + +## LoopX を試す + +確認できる結果が得られるウォークスルーを選んでください: + +| 始めるもの | やること | +| --- | --- | +| [初めてのバグ修正ループ](docs/guides/first-bugfix-loop.md) | 小さな Python プロジェクトで 2 つのバグを再現し、Agent に修正させてから、テストとパッチを確認します。 | +| [長時間のコーディング作業](docs/guides/long-running-coding-agents.md) | Issue を修正したり、自分のリポジトリを改善し続けたりします。レビュー、エビデンス、復旧はセッションをまたいで引き継がれます。 | +| [Personal Agent Workspace(中国語)](docs/guides/personal-workspace-user-guide.md) | 図解ガイドに沿って、アプリでプロジェクト、会話、タスク、納品された結果を管理します。 | + +### 使っている Agent から始める + +[2 ステップのセットアップ](#quick-start)の後、プロジェクトで開いたセッションで +お使いのホスト用のエントリを使います: + +| ホスト | 開始と継続の確認 | +| --- | --- | +| Codex App | `$loopx `。[heartbeat オートメーションが保存されていること](docs/guides/first-bugfix-loop.md#confirm-that-it-actually-started)を確認します。 | +| Codex CLI | `$loopx `。生成された[ネイティブの `/goal`](docs/product/runtimes/codex-cli/codex-cli-packaged-install.md#current-user-path) を同じセッションで有効化します。 | +| Claude Code | `/loopx `。定期的な作業には[ループドライバーのセットアップ](docs/guides/newcomer-command-path.md#choose-the-loop-driver)に従います。 | +| DeepSeek Harness | [ネイティブの DSH プラグイン](packages/dsh-loopx-plugin/README.md)をインストールし、`loopx` スキルを選択してから、タスクを記述します | + +LoopX は既存のプロジェクト状態を再利用するか、プロジェクトを接続し、作業を +Todo として計画して、ホストの有効化をガイドします。アクティブなゴール、 +次の Todo、継続の仕組みを Agent に確認してもらってください。セットアップパケットだけではアクティブなループになりません。 +作業を続けるあいだは、ホストを利用可能な状態にしておいてください。`loopx status` はゴール、 +あなたを待っている意思決定、次の Todo を表示します。`loopx doctor` はインストールを診断します。 + +[LoopX のインストール](docs/guides/installing-loopx.md)では、ネイティブの Windows、`pipx`、 +アップグレード、ロールバック、アンインストールについて説明しています。 + +**1.x からアップグレードする場合は?** 既存のゴールを続ける前に、[2.0 の移行と復旧のガイド](docs/product/releases/v2.0.0.md)をお読みください。 + +
+その他のホスト、ループドライバー、カスタムランナー + +| ホスト | 推奨される開始方法 | ループドライバー | +| --- | --- | --- | +| Codex App | プロジェクトで開いたスレッドで `$loopx ` を実行するか、`/skills` から `loopx` を選びます。 | Codex App の heartbeat オートメーション。`quota should-run.scheduler_hint` から更新されます | +| Codex App over SSH | `loopx agent-onboard --agent-type codex-app-ssh --project .` | 返される可視の `/goal ` | +| Codex CLI | プロジェクトで `codex` を起動し、`$loopx ` または `/skills` を使います。 | 可視の `/goal `。デフォルトでは隠れたヘッドレス実行はありません | +| Claude Code | オプトインのアダプターをインストールし、`/loopx ` に続けて `/loop` を実行します。 | LoopX によってゲートされた、Claude Code ネイティブの `/loop` | +| KunlunCode | `loopx-kunluncode connect --project . --goal-id --agent-id ` を実行し、範囲を限定した todo を追加してから、`loopx-kunluncode run --project .` を実行します。 | app-server を介したネイティブの Goal Pro。LoopX は厳密な検証の後にのみ、完了とクォータを書き込みます | +| OpenCode | 静的なコマンドファサードをインストールし、定期的なゴールには `--with-goal-bridge` でオプトインします。 | OpenCode のコマンドファサードと明示的なゴールブリッジ | +| Pi | `loopx slash-commands --install --surface pi` でオプトインのゴール拡張をインストールし、信頼できる Pi セッションから `/loopx ` を使います。 | LoopX のクォータによってゲートされた可視の Pi ゴール拡張(`loopx_goal_activate` + `agent_settled` による継続) | +| ZCode | `loopx slash-commands --install --surface zcode` でスキルファサードをインストールし、プロジェクトの ZCode セッションから `$loopx` スキル(または `/loopx `)を呼び出します。 | ZCode セッション自身のターンループ。すべての継続は `quota should-run` を経由します | +| Antigravity CLI (agy) | `loopx slash-commands --install --surface agy` でスキルファサードをインストールし、プロジェクトの `agy` セッションから `loopx` スキル(または `/loopx `)を呼び出します。 | セッションのネイティブな `/goal` ループ(`` まで監査されます)と、セッションが生きている間の `schedule` による自己起床。ファサードは各ターン/起床で `quota should-run` を経由して再入するよう指示します。これは助言的なペース配分であり、ホストが強制するゲートではありません | +| Kiro CLI | `loopx slash-commands --install --surface kiro-cli` でスキルファサードをインストールし、プロジェクトの `kiro-cli` セッションから `/loopx ` を実行します。 | セッションのネイティブな `/goal --max Done when: ` ループ。ホストがゴール文から受け入れ基準を導出するため、基準はゴール文の中に記述します。ホスト自身の反復予算(デフォルトは 5)で制限され、組み込みの `goal` 完了契約によって確定されます。ファサードは各ターンと反復で `quota should-run` を経由して再入するよう指示します。これは助言的なペース配分であり、ホストが強制するゲートではありません | +| DeepSeek Harness (dsh) | [ネイティブの DSH プラグイン](packages/dsh-loopx-plugin/README.md)をインストールし、`loopx` スキルを選択して、タスクを記述します。ヘッドレスなターンには、引き続き [dsh goal-mode アダプター](loopx/dsh_goal_mode/README.md)を利用できます。 | ネイティブな同一セッション内での継続と GoalBar、またはヘッドレスな dsh セグメント。どちらも LoopX の権限によってゲートされます | +| Cursor、シェル、カスタムランナー | `loopx start-goal --guided --project . --goal-text ""` を実行するか、ランナーから LoopX を呼び出します。 | お使いのシェル、スケジューラー、ランナー | + +そのままコピーできるセットアップメッセージとホストの復旧手順は、 +[Getting Started](docs/guides/getting-started.md) にあります。ホスト連携では、 +[Codex App のホストコマンドレジストリ契約](docs/reference/protocols/codex-app-host-command-registry-v0.md)、 +[Codex CLI のパッケージインストール手順](docs/product/runtimes/codex-cli/codex-cli-packaged-install.md)、 +[Claude Code アダプター](loopx/claude_goal_mode/README.md)、 +[KunlunCode のネイティブ Goal アダプター](loopx/kunluncode_goal_mode/README.md)、 +[Kiro CLI の goal-mode アダプター](loopx/kiro_cli_goal_mode/README.md)、または +[DeepSeek Harness のターンアダプター](loopx/dsh_goal_mode/README.md)を確認できます。 + +ネイティブな手順については、[60 秒の DSH × LoopX Replan の録画と再現可能な +フィクスチャ](docs/showcases/cases/dsh-loopx-replan-demo.md)をご覧ください: +プラグインをインストールし、スキルを 1 つ選択し、重要な制約を変更して、 +保持された意思決定の記録を確認します。 + +カスタムランナーの場合は、まず +[最小限のカスタムランタイムの例](docs/guides/minimal-custom-runtime-example.md) +(`python3 examples/custom-runtime-minimal-cli-turn-smoke.py`)から始め、次に完全な +[Embed LoopX in Your Agent Runner](docs/guides/custom-agent-runner-integration.md) +ガイドと[ワーカーブリッジのインストール契約](docs/integrations/worker-bridge-install-contract.md)をご覧ください。 +コアとなる 1 回の tick は、意図的に小さくしています: + +```text +loopx quota should-run # should this registered agent act now? +loopx todo claim # who owns this slice? +loopx todo update # what changed? +loopx refresh-state # what should the next turn see? +loopx quota spend-slot # account for a completed, validated slice +``` + +クローンによるインストールは、ライブの canary ラッパーを使いたいコントリビューター向けです: + +```bash +git clone https://github.com/loopx-project/loopx ~/loopx +~/loopx/scripts/install-local.sh +loopx doctor +``` + +
+ +### 初回実行のフィードバック + +LoopX がうまく動いたら、1 分で書ける公開 Issue が、実際の初回実行がどのようなものかを +知る助けになります。これは任意で、テレメトリは含まれません。ログ、パス、認証情報、 +社内のプロジェクト名、ゴールの内容は含めないでください: + +- [初回実行のフィードバック](https://github.com/loopx-project/loopx/issues/new?template=first_run.yml) +- [長時間の実行についての利用事例](https://github.com/loopx-project/loopx/issues/new?template=usage_story.yml) + +`loopx first-run-report` は、何も送信せずに、同じ入力済みのリンクをローカルで +表示します。 + +基本的な利用統計は、初回利用時の告知の後、デフォルトで有効になります。プラットフォームのサポート状況と継続利用を把握するための、 +ランダム ID による 1 日 1 回の heartbeat と、それとは別の ID なしの CLI 利用回数です。 +コンテンツは収集しません。どちらも Settings → Capability Center、 +または `loopx usage-ping disable` / `LOOPX_USAGE_PING=0` で無効化できます。送信内容は +`loopx usage-ping status` で確認できます。[基本的な利用統計](docs/reference/usage-ping.md)をご覧ください。 + + + +## エビデンス + + + +### LHTB:同じモデルでより高い平均報酬 + +**LoopX 1.0.3 Heartbeat は、対応する 46 タスクで平均報酬 0.4948 を達成しました:** +**Plain Codex を 17.3% 上回り**、**ネイティブの Codex Goal を 10.6% 上回っています**。 +3 つとも GPT-5.6 Sol、最大の推論努力、Web Search 無効の条件で実行しています。 +[Long-Horizon Terminal-Bench](https://zli12321.github.io/LHTB/index.html) はコーディングにとどまりません。これらのタスクは、研究の +再現、科学シミュレーション、マルチモーダル分析、専門的な +ワークフロー、ゲーム、システム関連の作業にまたがります。 + +| 実行モード | 平均報酬 ↑ | 厳格な合格率(≥0.95) | 合格率(≥0.80) | +| --- | ---: | ---: | ---: | +| Plain Codex | 0.4218 | 7/46 (15.2%) | 12/46 (26.1%) | +| ネイティブの Codex Goal | 0.4475 | 4/46 (8.7%) | 14/46 (30.4%) | +| **LoopX 1.0.3 Heartbeat** | **0.4948** | **7/46 (15.2%)** | **15/46 (32.6%)** | + +≥0.80 のしきい値は補足的な事後の見方です。ベンチマークの厳格な解決しきい値は +引き続き ≥0.95 です。この研究にはちょうど 0.80 のスコアがないため、 +これらの件数は研究概要の >0.80 の見方とも一致します。 + +ネイティブの Goal と比べると、タスク単位の報酬は **23** タスクで改善、**13** タスクで同等、 +**10** タスクで悪化しました。厳格な解決数は Plain Codex と同じであるため、平均の向上は、 +すべてのタスクが改善したことや、Plain より多くのタスクが完全に解決されたことを +主張するものではありません。平均報酬は部分的な進捗も評価に含めます。 + +タスクとモードごとに有効な試行は 1 回です。指定された代替試行があり、 +ランタイムや予算は均等ではありません(一部の Heartbeat の代替試行にはより長い予算を含みます)。 +これらは観測されたシステムとしての結果であり、同一予算での効率の結果でも、 +LoopX の効果を単独で推定した因果的な推定値でもありません。 + +[インタラクティブな結果、タスクの比較、方法論](https://loopx-project.github.io/loopx/benchmarks/lhtb/) +· [5 アームの研究とその限界](benchmark/LHTB/studies/five-arm-gpt56sol-max/README.md) +· [公開されているタスク単位のスコア](benchmark/LHTB/studies/five-arm-gpt56sol-max/data.json) + +ベンチマーク以外にも、LoopX には確認可能な長期プロジェクトのエビデンスがあります。 +公開されている OpenViking へのコントリビューションの流れは、 +**ループの経過時間で 200 時間超**にわたり、 +範囲を限定したターン、意思決定、エビデンスの更新を保持しています。これはプロジェクトの実時間を +計測したものであり、連続したモデルの実行時間や無人での本番自律運用を示すものではありません。 +図を開くと、公開しても安全なタスクグラフ、エビデンスの分岐、 +ターンをまたいだ意思決定を確認できます。この事例には、出典と再現性の範囲が記載されています。 + +### オープンソースの Issue 修正 + +**200 時間超の公開コントリビューションの軌跡:PR の納品と再利用可能な修正知識が +ともに進化します。** + + + 焦点を絞った PR の納品と、再利用可能な LoopX のケイパビリティを結び付けた、オープンソースの Issue 修正のトラジェクトリ + + +
+コントリビューションの出典とエビデンスの範囲 + +LoopX の作者は、この手順を +[OpenViking のコントリビューター](https://github.com/volcengine/OpenViking/pulls?q=is%3Apr+author%3Ahuangruiteng)として使っています。 +示されている公開コントリビューションの流れは、最初の PR の作成から、示されている最新の +レビューまたは更新まで、経過時間で 200 時間を超えています。 +[Issue-Fix ケイパビリティ](loopx/capabilities/issue_fix/README.md)は、継続的に更新される +リポジトリのコンテキスト、リビジョンが刻まれた修正知識、レビュアー向けの +好みを分けて保持します。権威を持つのは、リンクされた PR と、現在のチェックアウトのソースおよびテストです。 + +
+ +### 実際のプロジェクトでの利用 + +- **独立したユーザー · `>13h` の C++ 精度改善の実行。** ユーザーの報告によると、 + 複数段階のタスクが一貫性を保ち、公開情報の調査を行い、 + [公開されているコードメモリツール](https://github.com/DeusData/codebase-memory-mcp)を採用し、 + 最終的な精度が向上しました。[エビデンスの範囲を読む](docs/showcases/cases/independent-cpp-accuracy-long-run.md)。 +- **独立したユーザー · `4d` の無人実行。** ユーザーの報告によると、4 日間 + 人間の介入なしに有用な作業が続き、定期レポートの画面も + 機能しました。[マスキング済みの事例を読む](docs/showcases/cases/independent-four-day-unattended-agent.md)。 +- **独立したユーザー · `7` 件のマージ済み PR。** LoopX によるものとされる Engine のリファクタリングが、 + [公開 Issue](https://github.com/zilliztech/mfs/issues/166) と + 7 件のマージ済み PR で確認できます。LoopX によるものという帰属と、報告された `1B+` トークンという規模は、ユーザーの + 報告に基づくものです。[事例を確認する](docs/showcases/cases/independent-public-engine-refactor.md)。 +- **独立したユーザー · vLLM の最適化がアップストリームにマージ。** + [Zheng Cai](https://x.com/zigzagcai/status/2109952174822408443) は、プロファイリング、調査、実験、 + 正確性チェックのワークフローエンジンとして LoopX を使ったと報告しています。[vLLM PR #54951](https://github.com/vllm-project/vllm/pull/54951) + では sparse-indexer の最適化がマージされました。報告されている総トークンスループットの **32.59%** の向上は、 + GLM-5.3-Flash、入力 1M トークン、4×H200、TP4、C1 の条件でのものです。 + [ワークフロー、すべての条件、エビデンスの範囲を確認する](docs/showcases/cases/independent-vllm-long-context-optimization.md)。 + +これらは注目の独立ユーザー事例 4 件であり、すべてではありません。コントリビューターの事例、 +作者自身によるドッグフーディング、再現可能なデモ、明示的なエビデンスの強度ラベルについては、 +[Showcase の完全なカタログ](docs/showcases/README.md)をご覧ください。 + +### 探索的なベンチマーク研究 + +- **[SWE-Marathon](https://loopx-project.github.io/loopx/benchmarks/swe-marathon/):** + 対応する 15 タスクで 5 つの実行モードを比較し、自己検証、スコア、 + コストを比べています。自己検証を増やしても、一貫して高いスコアにはなりませんでした。 +- **[LHTB × LoopX](https://loopx-project.github.io/loopx/benchmarks/lhtb/):** + [上記の結果](#lhtb-results)から、5 つの実行メカニズム、 + タスク単位の比較、悪化した事例、探索的なタスクタイプ分析へと続きます。 +- **[DeepSWE の行動分析](https://loopx-project.github.io/loopx/benchmarks/deepswe/behavior-discovery/)**(中国語): + 選ばれた事例をもとに、ドメインのヒントが要件の保持や + 検証の選択とどう関係するかを調べ、さらなる検証のためのメカニズムの仮説を示しています。 + +SWE-Marathon と LHTB は、タスクとモードごとに有効な試行が 1 回です。DeepSWE は +選ばれた事例と事後分析を用いています。 + +その他の確認できる情報: + +- 製品のストーリー、クイックスタート、長期実行のエビデンスについては + [公開ホームページ](https://loopx-project.github.io/loopx/) +- [Showcase の完全なカタログ](docs/showcases/README.md)と、その + [バイリンガルのホスト版インデックス](docs/showcases/index.html) +- 依存する成果物、独立したレビュー、返された結果については[エージェント連携ガイド](docs/product/use-cases/cross-runtime/README.md) +- 公開されている[ユーザーマニュアル](https://my.feishu.cn/wiki/CaL5wMk9ui17ngkWzeUcMlAYnZg) + + + +## ケイパビリティ + +必要な成果から始めましょう。オプションのケイパビリティは、同じゴール、 +作業のオーナーシップ、エビデンスを再利用します。それぞれのサポート状況と権限の範囲を確認してください。 + +### 製品ケイパビリティの進め方 + +ケイパビリティは、汎用的なプリミティブを、成果に責任を持つ作業レーンに変えます。 +成果から始め、現在登録されている実装とその書き込み範囲を +確認してください: + +| やりたいこと | ケイパビリティ | 始め方 | +| --- | --- | --- | +| 公開 Issue を、レビュー可能でエビデンスに裏付けられた変更にする | [Issue Fix](loopx/capabilities/issue_fix/README.md) | `loopx capability show issue-fix --format json` | +| 納品前に、最終的な差分そのものを適格性確認する | [Change Quality](loopx/capabilities/change_quality/README.md) | `loopx capability show change-quality-qualification --format json` | +| 定期レビューの前に、忙しいのにゴールから外れている作業ラウンドに気付く | [Progress-Review Sentinel](loopx/capabilities/progress_review/README.md) | `loopx capability show progress-review-sentinel --format json` | +| すでにレビュー済みのブランチの、変化し続けるスタックを保持する | [Integration Branch](loopx/capabilities/integration_branch/README.md) | `loopx capability show integration-branch-reconcile --format json` | +| 仮説や発見を失わずに、不確実な研究を探索する | [Explore](loopx/capabilities/explore/README.md) | `loopx capability show explore --format json` | +| 現在のエビデンスと検証済みの結果に基づいて意思決定を見直す | [Decision Context](loopx/capabilities/decision_context/README.md) | `loopx capability show decision-context --format json` | +| 受領記録付きで、定期的または進捗をきっかけにしたレポートを作成する | [Periodic Report](loopx/capabilities/periodic_report/README.md) | `loopx capability show periodic-report --format json` | + +インストール済みのリリースにおける正式なカタログは、`loopx capability list --format json` を実行すると確認できます。 +ケイパビリティの詳細には、ユーザーにとっての価値、成熟度、 +プロバイダーの準備状況、エントリコマンド、書き込み範囲、プロトコル、永続的な +検証が含まれます。成果から選ぶには[人が読めるケイパビリティのインデックス](loopx/capabilities/README.md)を参照し、 +プロバイダーをインストールまたは構築するときは [Extensions and Capabilities](docs/reference/extensions.md) を +使ってください。 + +### コントロールプレーンの中核となる約束 + +Kernel は、その仕組みを 5 つの問いにまとめています。それぞれの問いが、あらゆるエージェントハーネスの上で +1 つの製品上の約束を果たします:目的 → 長期的な状態、 +次 → 意味的な意思決定、人間の判断 → 人間とエージェントの協働、 +エビデンス → 復旧、継続 → ガバナンス。 + +| 問い | LoopX が見える状態に保つもの | +| --- | --- | +| 目的は何か? | アクティブなゴール、明示的なスコープ、現在の権限。 | +| 次に何が起こるか? | 順序付けられたユーザーとエージェントの todo、オーナーシップ、claim、リース。 | +| 人間の判断が必要なものは何か? | 曖昧な「オーナー待ち」ではなく、具体的なユーザーゲート。 | +| どのエビデンスが変わったか? | コンパクトな実行履歴、検証、ブロッカー、受け入れられた writeback。 | +| ループを続けてよいか? | クォータ、ケイパビリティ、安全なフォールバック、スケジューラーのヒント、停止条件。 | + +Goal の停止は `loopx goal-lifecycle --goal-id --operation stop` でプレビューできます。 +資格を元に戻すには `--operation resume` を使います。遷移を適用するには +`--actor-kind owner --execute` を使い、Goal のアクションビューから提供された場合は最新の `--expected-state-fingerprint` を付けたままにします。 +通常の CLI とスタンドアロンの +[`loopx-core`](packages/loopx-core/README.md) CLI は、同じライフサイクルオーナーを使います。 + +
+アーキテクチャ、コントロールプレーンの画面、ランタイムの責任 + +LoopX はアーキテクチャを明示的に保つことで、エージェントハーネスや外部プロバイダーが +変わっても、同じ統制された成果が生き残れるようにしています。以下の用語は、交換可能な +プラグインの種類ではなく、異なる境界を表しています: + +| 境界 | 意味 | 詳しく見る | +| --- | --- | --- | +| **Kernel** | 永続的なゴール、todo、ゲート、エビデンス、クォータ、復旧、スケジューリングの真実を管理します。 | [アーキテクチャ](docs/architecture.md) | +| **Capability** | LoopX の状態から、範囲が限定され検証可能な呼び出し元の成果を 1 つ生み出すための、安定したプロバイダー非依存の契約を定義します。 | [ケイパビリティのカタログ](loopx/capabilities/README.md) | +| **Provider** | 外部システムやローカルの実装を呼び出し、範囲が限定された観測、効果の結果、読み戻しを返します。 | [プロバイダーの責任](docs/reference/extensions.md#runtime-responsibilities) | +| **Extension** | 明示的なインストール、準備状況、有効化、アップグレード、無効化、ロールバックのライフサイクルを通じて、オプションのプロバイダーをパッケージ化して運用します。 | [Extension のライフサイクル](docs/reference/extensions.md#runtime-lifecycle) | + +`--available-capability shell` のようなホストの宣言は、観測された +実行サポートを表します。この製品マップでは、これらはランタイムの能力であり、製品の +ケイパビリティでも権限の付与でもありません。有効なケイパビリティは、遷移を提案する前に、 +引き続き独自のポリシーと権限のチェックを適用します。 + +### コントロールプレーンの画面 + +| 画面 | 役割 | 始め方 | +| --- | --- | --- | +| ゴールの状態とステータス | アクティブな状態、todo、claim、ゲート、エビデンス、実行履歴、最初の画面で注意すべき点を追跡します。 | `loopx status`、`loopx diagnose`、`loopx review-packet` | +| クォータとインタラクションの契約 | ターンで納品するか、尋ねるか、待つか、自己修復するか、静かにしているかを決定します。 | `loopx quota should-run`、[クォータの割り当て](docs/quota-allocation.md) | +| Agent ランタイムのブリッジ | Codex App、Codex CLI、Claude Code、汎用ワーカーを同じガードにそろえます。 | `loopx heartbeat-prompt`、`loopx codex-cli-bootstrap-message`、`loopx worker-bridge` | +| オペレーター向け画面 | ブラウザーを状態の権威にすることなく、コンパクトなステータスを表示します。 | `loopx serve-status`、[ダッシュボード](apps/presentation/dashboard/README.md) | +| セッションダッシュ | セッション、そのゴール、各ゴールのステータス/todo の進捗といったフリート全体の進捗を、結果の統計とともに追跡するライブの単一ページパネルを起動します。その場で自動更新されます。 | `loopx dash`、[セッションダッシュの設計](docs/product/surfaces/session-dash-panel-design.md) | +| 外部への投影 | LoopX を権威として保ったまま、todo とゲートをコラボレーション画面に投影します。 | `loopx lark-kanban`、[Lark Kanban アダプター](docs/integrations/lark-kanban-control-plane-adapter.md) | +| ドメインケイパビリティ | Issue 修正、コンテンツ運用、価値コネクターの計画、ML 実験のアドバイス、ベンチマークのエビデンス、Explore など、繰り返し可能な作業レーンをパッケージ化します。 | `loopx issue-fix`、`loopx content-ops`、`loopx value-connectors`、`loopx ml-experiment`、`loopx benchmark`、[Explore](loopx/capabilities/explore/README.md) | +| 実験的なコンテキスト学習 | 名前付きの登録済みエージェントが、無視される・デフォルトでオフのプロジェクト設定を通じて、プロバイダー非依存の Reward Memory を試せるようにします。OpenViking はプロバイダーの選択肢の 1 つであり、グローバルな依存関係ではありません。 | `loopx reward-memory experiment-status`、[Reward Memory のアーキテクチャ](loopx/capabilities/reward_memory/README.md) | +| ガバナンスのパターン | 再利用可能なルーティング、ゲート、エビデンス、投影、計画の形を記録します。 | [インタラクションパターン](docs/concepts/interaction-pattern-catalog.md)、[状態モデル](docs/state-interaction-model.md) | + +提供済みのプリミティブには、ライフタイムゴール、具体的なユーザーゲート、監査済みの安全な +フォールバック、対等な todo のオーナーシップ、クォータとステアリング、コンパクトな実行履歴、 +エビデンスに裏付けられた引き継ぎ、読み取り優先の管理画面、プロジェクトレベルの価値 +シグナル、公開/非公開の境界チェックが含まれます。 + +### ランタイムの責任 + +| 役割 | 責任 | +| --- | --- | +| **Agent** | ホスト/ランタイムを通じて、計画、分析、ツールの使用を行い、範囲を限定したアクションを 1 つ実行します。 | +| **Provider** | 外部システムを呼び出し、観測、効果の結果、読み戻しを返します。 | +| **Capability** | 呼び出し元の成果を定義し、プロバイダーの出力を正規化して検証し、型付きの遷移を提案します。 | +| **Kernel** | 永続的な todo、ゲート、監視、受け入れられた writeback、クォータ、復旧、スケジューリングを管理します。 | + +実行の経路は `Agent -> Capability -> Provider` で、制御の経路は +`Provider readback -> Capability transition -> Kernel` と戻ってきます。Extension は、 +オプションのプロバイダーをパッケージ化して管理する方法であり、コントロールプレーンの別の +オーナーではありません。[アーキテクチャ](docs/architecture.md)と +[Extensions and Capabilities](docs/reference/extensions.md) をご覧ください。 + +
+ +
+パーソナルエージェント、チーム、自己改善するワークフロー + +### パーソナルエージェント、チーム、自己改善するワークフロー + +[Meta Muse](https://about.fb.com/news/2026/09/introducing-muse-personal-ai-agent/)、 +[Grok Bot](https://x.ai/news/introducing-grok-bot)、そして +[Anthropic の *Building always-on agents*](https://claude.com/resources/articles/building-always-on-agents) +によって、常駐するパーソナルエージェントや、スリープ/起床によるイベント駆動の実行は、おなじみの製品 +およびアーキテクチャのモデルになっています。LoopX はこの領域に、すでに実行しているエージェントのための、オープンでプロバイダー非依存の +[24/7 コントロールプレーン](#always-on-agents)としてアプローチします。いずれかの製品を +置き換えるホスト型サービスではありません。 + +- **パーソナルエージェント:** 提供済みの Workspace と接続済みの Lark の画面を使って、 + ゴールを確認し、作業を誘導し、意思決定を解決します。 + [常駐スチュワードと意味的な引き継ぎの RFC](docs/architecture/rfcs/capable-manager-semantic-handoff-v0.md) + は、これをチームのための有能な 1 つの窓口へと拡張するものです。完全な体験は + まだ適格性確認中です。 +- **エージェントチーム:** ワーカーのオーナーシップを消すことなくゴールを共有します。claim、エビデンス、 + 明示的な返却によって、実装とレビューがつながった状態に保たれます。修正と 2 回のレビューラウンドを含む + 範囲を限定した例については、[3 エージェントの連携デモ](examples/collaboration-delivery/README.md)を + ご覧ください。 +- **自己改善するワークフロー:** [Reward Memory](loopx/capabilities/reward_memory/README.md) を通じて + フィードバックを次の試行につなげ、[Explore](loopx/capabilities/explore/README.md) を通じて + 変更候補を評価します。どちらもオプションです。**再帰的自己改善(RSI)** とのつながりは + 提案、テスト、レビュー、保持というエンジニアリングのループであり、LoopX が + 自律的なモデルの改善や複利的な能力向上を実証したと主張するものではありません。 + +パーソナルスチュワードはインタラクション上の役割であり、2 つ目の権威の源ではありません。 +バックグラウンドでの実行には、引き続き利用可能なホストと設定済みのランタイムが必要です。 +[全体のロードマップ](docs/architecture/rfcs/loopx-overall-roadmap-v0.md)では、 +提供済みの基盤と、次のエンドツーエンドの受け入れマイルストーンを分けて示しています。 + +
+ + + + +
+2.0 の背景と移行 + +| 共有のコントロールプレーン | 長期研究用ワークベンチ | +| --- | --- | +| 移行済みのトランザクション、権限、復旧のルールは TypeScript が担います。Python は実際のホスト/プロバイダーの IO と特殊な計算を引き続き担当します。 | 委任された作業、Explore の履歴、成果物、トラジェクトリ分析、次の意思決定を通じて、研究上の問いを追跡します。 | +| [2 か月間のエンジニアリング、ソースの履歴、主要な統計](https://loopx-project.github.io/loopx/blog/typescript-control-plane-refactor/) | [86 秒の動画を見る](https://github.com/user-attachments/assets/543af0b1-c3d4-4cfb-aedd-a68563a20601) · [分析の契約を読む](docs/reference/trajectory-assessment.md) | + +**1.x からアップグレードする場合は?** 2.0 では、昇格されていない Markdown のゴールへの通常の書き込みを廃止しています。 +それらのゴールを続ける前に、[移行と復旧のガイド](docs/product/releases/v2.0.0.md)を +確認してください。インストールによって既存の状態が黙って変換されることはありません。 + +
+ +## 高度な使い方 + +最初の有用なループには、すべてのオプション機能が必要なわけではありません。これらは作業で +必要になったときにだけ追加してください。 + +高度な機能を有効にする前に、現在のゴールの読み取り専用のケイパビリティカタログを +確認してください: + +```bash +loopx configure-goal --goal-id +``` + +`--execute` を付けなければ、このコマンドはプロジェクトの状態を変更せずに、現在/デフォルトの状態、適合性、範囲、 +コピーできるコマンドを報告します。 + +### 定期作業のプリセット + +安全なプリセットは、日次のトリアージ、changelog の下書き、PR の監視をカバーしています。まずは +[初心者向けプリセットガイド](docs/product/foundations/beginner-loop-presets.md)から始めてください。 + +```bash +loopx preset list +loopx preset show daily-triage +``` + +プリセットの確認は読み取り専用です。接続済みの定期的なゴールについては、 +`loopx ready-score --goal-id --agent-id ` が、 +ループを繰り返し実行する準備ができているかどうかを報告します。 + +### 統制されたターン + +LoopX は、検証済みの受領記録、最新のクォータ状態、プロバイダー非依存の予算から、 +純粋で範囲が限定されたターンの決定を 1 つ生成できます。現在の Codex CLI の +クイックスタートと有効化の契約は、 +[LoopX Turn for Codex CLI](docs/product/runtimes/codex-cli/loopx-turn-codex-cli-quickstart.md) に記載されています。 + +### Explore のグラフとハーネス + +Explore はサポートされているオプション機能で、デフォルトではオフです。タスクに +測定可能なオフライン評価、ベースライン、施策、ガードレールがある場合に最も効果を発揮します。 +本番環境の承認の代わりにはなりません。まずは +[Explore ケイパビリティ](loopx/capabilities/explore/README.md)と、その +[Lark での表示のマッピング](loopx/capabilities/explore/README.md#presentation-sink-lark-mapping)から始めてください。 + +### エージェントの作業をレビューする + +意思決定、エビデンス、検証、未解決のゲートを、オーナー向けにコンパクトに確認するには +`loopx review-packet` を使います。 +[インテリジェントな管理画面](docs/product/surfaces/intelligent-management-surface.md) +ではオペレーターのモデルを説明しており、 +[プロジェクトレベルの報酬モデル](docs/product/foundations/project-level-reward-model.md) +では、出力の量、品質、トークン +コスト、ユーザーの注意のコストにまたがる、控えめな価値シグナルを説明しています。 + +具体的な対等ワークフローについては、まず +[エージェント連携ガイド](docs/product/use-cases/cross-runtime/README.md)をご覧ください: +ビルダーがアナリストの成果物を使い、独立したレビューを受け、 +オーナーからの修正に対応して、確認済みの結果を返します。このガイドでは、 +実行可能な同一ホストの例と、以前のクロスランタイムの設計スケッチを区別しています。 + +### アプリと投影の使い方 + +- ローカルの読み取り優先 UI:[ダッシュボードガイド](apps/presentation/dashboard/README.md) +- 公開されている製品概要:[公開ホームページ](https://loopx-project.github.io/loopx/) +- ドキュメントポータル:[ホスト版ドキュメント](https://loopx-project.github.io/loopx/docs/) +- Feishu/Lark への投影:[Lark Kanban アダプター](docs/integrations/lark-kanban-control-plane-adapter.md) +- 汎用的なホスト連携:[連携ガイド](docs/integration.md) +- カスタムのマルチエージェントランナー: + [最小限のカスタムランタイムの例](docs/guides/minimal-custom-runtime-example.md)、 + 次に[カスタムランナーの連携](docs/guides/custom-agent-runner-integration.md) + +オプションの投影は状態を確認しやすくしますが、信頼できる情報源に +なるわけではありません。 + +### 運用と復旧 + +日々の確認は次のコマンドから始めます: + +```bash +loopx status +loopx history --goal-id your-project-goal +loopx quota should-run --goal-id your-project-goal +``` + +自動のターンは、まずクォータを確認し、検証済みの writeback の後にのみ消費を記録しなければなりません。 +静かなスキップ、事前チェックの失敗、ドライランのプレビューでは消費しません。 +ユーザーゲートが 1 つのレーンをブロックしている場合、別途監査された安全なフォールバックで +続行できることがありますが、ゲートを迂回してはいけません。 + +対等なエージェントは、納品の前に `loopx todo claim` を、検証の後に `loopx todo update` を使い、 +オーナーシップとエビデンスが見える状態を保ちます。 + +スケジューラーの周期は `quota should-run.scheduler_hint` に従います。インストール済みの Codex +App のオートメーションは、返された `ack_hint.cli_args` を通じて現在のヒントを確認応答します。 +衝突からの復旧、監視のセマンティクス、自己修復、 +正確なオペレーターコマンドは、 +[Getting Started](docs/guides/getting-started.md)、 +[Quota Allocation](docs/quota-allocation.md)、 +[Long-Task Cadence Policy](docs/operations/long-task-cadence-policy.md) で管理されています。 + +公開ドキュメントや例を公開する前に: + +```bash +loopx check \ + --scan-path README.md \ + --scan-path docs/ \ + --scan-path examples/ +``` + +## 現在の技術的方向性 + +LoopX には、3 つの進行中の戦略プログラムと、アーキテクチャおよび研究の +インキュベーターがあります。これらは方向性を示すものであり、納品の約束ではありません。提供済みの事実の情報源は、引き続き `main`、リリース済みの +成果物、安定したリファレンス契約です。 + +- **長期ベンチマークとエビデンス:** 相補的なベンチマーク環境にまたがる、 + 再現可能な能力のエビデンスと、制御されたメカニズムの研究。[方向性トラッカー](https://github.com/loopx-project/loopx/issues/3243) +- **オペレーター画面と IM 連携:** オペレーターのワークスペース、セッションの + 記録、範囲を限定したコラボレーション画面。Personal Workspace は + 提供済みで、統合されたスチュワード、実行、復旧の体験はまだ適格性確認中です。 + 実装リードは `@maxliux5` です。 + [方向性トラッカー](https://github.com/loopx-project/loopx/issues/3244) +- **共有ゴールの権限とクロスホストの連携:** 明示的に共有されたゴールのための、プロバイダー非依存の + 連携。NoKV は、新たなコントロールプレーンの権威ではなく、昇格前の + プロバイダー候補です。 + [方向性トラッカー](https://github.com/loopx-project/loopx/issues/3245) +- **アーキテクチャと研究のインキュベーター:** Effect Program の堅牢化、 + TypeScript への同等性を保った移行、階層的なストライド、研究の探索、 + 人間の注意、成果物のライフサイクル、メモリの有用性に関する作業。それぞれ成熟度は明示的に + 異なります。 + [方向性トラッカー](https://github.com/loopx-project/loopx/issues/3246) + +ステージ、昇格のゲート、コントリビューターが安全に取り組める切り口、オーナーシップの範囲については、正式な +[Technical Directions マップ](docs/project/technical-directions.md)をお読みください。コミュニティでの議論には、 +ピン留めされた [GitHub Discussion](https://github.com/loopx-project/loopx/discussions/2851) +を使ってください。コアとなるコントロールプレーンの信頼性は、これらのプログラムを支える +共通の基盤として継続されます。 + +## 高度なドキュメント + +[製品 Web サイト](https://loopx-project.github.io/loopx/) · [ブログ](https://loopx-project.github.io/loopx/blog/) · [Developer Book](https://loopx-project.github.io/loopx/docs/book/en/) + +現在のタスクに合った道筋から始めてください。公開されているドキュメントサイトには、ホスト版の +[ドキュメントポータル](https://loopx-project.github.io/loopx/docs/)を使ってください。 +[ドキュメントのインデックス](docs/README.md)は、引き続き完全なソースマップです。この一覧は +厳選したものです。より詳しいドキュメントやバージョン管理されたプロトコルは、各カテゴリのインデックスが管理しています。 + +### 使う・運用する + +- [Getting Started](docs/guides/getting-started.md):インストール、接続、 + 診断、日々のワークフロー、heartbeat、ダッシュボード、開発、コマンド。 +- [ユーザーマニュアル](https://my.feishu.cn/wiki/CaL5wMk9ui17ngkWzeUcMlAYnZg): + 公開されているオンボーディング、概念、FAQ、厳選した事例。 +- [運用](docs/operations/README.md):ゴールの継続、todo、周期、 + 注意、権限のワークフロー。 +- [Quota Allocation](docs/quota-allocation.md) と + [Heartbeat Automation Prompt](docs/heartbeat-automation-prompt.md):スケジューラーの + 実行可否、消費、スケジュールされた継続。 +- [ダッシュボード](apps/presentation/dashboard/README.md)と + [Status Data Contract](docs/status-data-contract.md):オペレーター向けの状態 + と投影の契約。 +- [Release Readiness](docs/product/release-readiness.md):インストール/更新の手順、 + 互換性のゲート、リリースノート、安心して依存できる画面。 + +### コントロールプレーンを理解する + +- [アーキテクチャ](docs/architecture.md):ライフタイムゴールの不変条件とカーネル。 +- [State Interaction Model](docs/state-interaction-model.md):アクター、ストア、 + インタラクションの契約、writeback。 +- [概念](docs/concepts/README.md):再利用可能なルーティング、ゲート、エビデンス、 + 投影、計画のパターン。 +- [Product Foundations](docs/product/foundations/README.md):Loop Engineering の + 原則、プロジェクトレベルの報酬、報酬スタイルの再計画。 +- [Product Vision](docs/product/vision.md):より広い Loop Agent の方向性。 + +### 連携する・拡張する + +- [連携ガイド](docs/integration.md) +- [最小限のカスタムランタイムの例](docs/guides/minimal-custom-runtime-example.md) +- [カスタムエージェントランナーの連携](docs/guides/custom-agent-runner-integration.md) +- [Integrations](docs/integrations/README.md):ワーカーブリッジや Lark を含む、ランタイム、ホスト、コラボレーション、 + 外部システムのアダプター。 +- [Extensions and Capabilities](docs/reference/extensions.md) + +### LoopX をビルドする・レビューする + +- [開発者ガイド](docs/development/README.md):コントリビューターのワークフロー、 + ベンチマークの開発、ドキュメントの構成、品質のゲート。 +- [リファレンスとプロトコル](docs/reference/README.md):ホストコマンドや Reward Memory の + アーキテクチャを含む、安定した契約とバージョン管理された実装プロトコル。 +- [コントロールプレーン開発者コース](docs/development/control-plane-course/README.md): + コードを中心とした中国語の講義 9 回。 +- [テストと品質](docs/development/testing-and-quality.md):検証の + レイヤーとリスクに基づくチェック。 +- [公開/非公開の境界](docs/public-private-boundary.md):安全なフィクスチャ、 + 例、エビデンス、公開。 + +### 成果を確認する + +- [Showcase カタログ](docs/showcases/README.md):公開しても安全な事例とエビデンスの + ラベル。 +- [研究とエビデンス](docs/research/README.md):ベンチマークの調査 + と出典に裏付けられた発見。 +- [Update Notes](docs/update-notes/README.md):公開しても安全な進捗メモ。 + +### プロジェクトとコミュニティ + +- [現在の技術的方向性](docs/project/technical-directions.md) +- [プロジェクトのガバナンス](.github/GOVERNANCE.md) +- [コントリビューション](CONTRIBUTING.md)と[コントリビューター向けタスク](docs/development/contributor-tasks.md) +- [作者とコントリビューター](docs/project/authors.md) +- [プロジェクトの歴史](docs/project/history.md) +- [名称と商標](docs/project/trademarks.md) +- [外部利用のためのブランドガイド](docs/project/brand-guide.md):プロジェクトや企業が LoopX、その連携、アートワークに言及する方法 +- [ADOPTERS](ADOPTERS.md):任意の自己申告による導入事例ディレクトリ +- [エコシステムでの導入](docs/community/ecosystem-adoption.md) - 私たちが観測・追跡している連携、 + サンプリング、派生プロジェクト + +## パートナープロジェクト + +LoopX は、長時間稼働するエージェントのエコシステムを築くために、他のオープンソースプロジェクトとの +協力を歓迎しています。確定しているパートナーは次のとおりです: + +- [OpenViking](https://github.com/volcengine/OpenViking) - AI エージェントのための + 自己進化するコンテキストデータベース +- [NoKV](https://github.com/NoKV-Lab/NoKV) - AI ネイティブな分散ファイルシステム + +## 現在のステータス + +LoopX は、長時間にわたるエージェントの作業のための実用的なローカルコントロールプレーンであり、 +より広い導入の段階に入りつつあります。完全なエージェントプラットフォーム、エージェントランタイム、 +自律的な本番コントローラーではありません。 + +現在の LoopX は、ゴール、型付きの todo と意思決定の +スコープ、対等な claim とリース、エビデンスと writeback、クォータを考慮したスケジューリング、 +ターンをまたいだ継続のための永続的な状態カーネルを提供しています。ガイド付きの開始、定期的な heartbeat、分離された Codex +CLI のターン、エビデンスに裏付けられた Issue-Fix の受け入れ、オプションの Explore と自動 +研究の手順、公開検証用の canary、複数プロジェクトの Personal +Workspace は、いずれもその共有のコントロール状態の上に構築されています。 + +サポートレベルは引き続き明示されています。状態と CLI の契約が安定した +中心であり、いくつかのホスト連携や高度な使い方は、オプション、デフォルトでオフ、 +または実験的なものです。LoopX は、認証情報を付与したり、破壊的な操作や +本番環境での操作を承認したり、承認なしにユーザーに代わって公開したり、 +検証されていない実行を成功のエビデンスに変えたりはしません。 + +現在の投資は、 +[Technical Directions マップ](docs/project/technical-directions.md)を通じて整理されています:長期 +ベンチマークのエビデンス、オペレーター画面と IM 連携、共有ゴールのクロスホスト +連携、そして明示的に段階分けされたアーキテクチャおよび研究のインキュベーターです。 + + + + +## コミュニティとコントリビューション + +長期的な Agent チームの働き方を一緒に形作りましょう。ワークフローを共有したり、 +バグを再現したり、連携を構築したり、Agent が成し遂げたことを紹介したりしてください。コード、 +ドキュメント、例、レビュー、慎重に範囲を限定したエビデンスの研究は、 +いずれも歓迎します。 + +- **学ぶ:** [ドキュメント](https://loopx-project.github.io/loopx/docs/) · + [ブログ](https://loopx-project.github.io/loopx/blog/) · + [技術的方向性](docs/project/technical-directions.md)。 +- **議論・共有する:** [GitHub Discussions](https://github.com/loopx-project/loopx/discussions) で、 + 質問したり、ワークフローを比較したり、プロジェクトを共有したりしてください。 +- **バグを報告する・機能を提案する:** バージョン、ホスト/ランタイム、期待される動作、公開しても安全な再現手順を添えて、 + [GitHub Issue](https://github.com/loopx-project/loopx/issues/new/choose) を + 作成してください。 +- **最初のコントリビューションをする:** [コントリビューター向けタスク](docs/development/contributor-tasks.md)から + 公開タスクを選ぶか、自己完結した修正を持ち込んでください。[コントリビューションガイド](CONTRIBUTING.md)では、セットアップ、 + 検証、PR のプロセスを説明しています。大きな変更は議論から始めてください。 +- **会話に参加する:** [Discord](https://discord.gg/XmGgQyCFZd) · + [Lark](docs/assets/loopx-lark-developer-group.png) · + [WeChat](docs/assets/loopx-wechat-contact.png)。 + +チャネルの使い分けと公式の情報源については [Support](.github/SUPPORT.md) を、コミュニティへの参加については +[行動規範](CODE_OF_CONDUCT.md)をご覧ください。 + +
+コード、ログ、例を共有する前に + +`.loopx/`、レガシーの `.codex/goals/`、ライブの `ACTIVE_GOAL_STATE.md`、生の Agent +セッションやベンチマークのトレース、認証情報、非公開のログ、社内リンク、 +機密性の高いユーザー/チームのデータ、オペレーターの成果物は、公開のコントリビューションに含めないでください。 + +
+ +### コントリビューター + +コード、ドキュメント、バグ報告、レビュー、実際の利用に基づくフィードバックで +貢献してくださっているすべての方に感謝します。皆さんのおかげで、長期的な Agent の作業は、より実行しやすく、 +確認しやすく、信頼しやすいものになっています。 + +[![LoopX GitHub contributors](https://contrib.rocks/image?repo=loopx-project/loopx&columns=12&max=120)](https://github.com/loopx-project/loopx/graphs/contributors) + +[すべての GitHub コントリビューター](https://github.com/loopx-project/loopx/graphs/contributors) · +[作者とコントリビューションの記録](docs/project/authors.md) · +[ガバナンス](.github/GOVERNANCE.md) · [プロジェクトの歴史](docs/project/history.md) · +[名称と商標](docs/project/trademarks.md)。 + +### コミュニティチャット + +

+ LoopX の Lark 開発者グループの QR コード + LoopX の WeChat 連絡先の QR コード +

+

+ Lark:スキャンして直接参加できます
WeChat:huangrt00 · 友だち申請の際に LoopX と書き添えてください
+

+ +## Star の推移 + +

+ 検証済みのスナップショットに基づく LoopX の GitHub スター数の推移
+ リポジトリで承認されたワークフローを使い、GitHub 公式の stargazer のタイムスタンプから 6 時間ごとに生成しています。スナップショットは、取得した行数が GitHub の現在のスター数と一致した場合にのみ公開されます。GitHub の画像キャッシュにより、更新が遅れることがあります。 +

+ +## ライセンス + +Copyright 2026 LoopX contributors. 作者は +[`@huangruiteng`](https://github.com/huangruiteng) です。公開されている +[作者とコントリビューターの記録](docs/project/authors.md)をご覧ください。 + +`v0.4.8` 以降は Apache License 2.0 です。[LICENSE](LICENSE) と +[NOTICE](NOTICE) をご覧ください。`v0.4.7` までのリリースは、元の MIT の +条件のままです。その歴史的なテキストと告知は +[LICENSE-MIT](LICENSE-MIT) に保存されています。[ライセンスポリシー](docs/project/licensing.md)では、 +バージョン、コントリビューション、特許許諾、オープンコアの範囲について説明しています。 + +AI による生成、コードの書き換え、言語間の翻訳、 +プロジェクト名の変更を根拠に、適用されるライセンスの下での著作権、帰属表示、 +NOTICE の保持の要件を削除したり回避したりしてはなりません。配布される +素材が LoopX の派生物である場合、それらの義務は引き続き適用されます。この +説明は、適用されるライセンスで許可されている改変や再配布を制限するものではありません。[再利用と帰属表示](docs/project/licensing.md#reuse-ports-and-attribution) +と[名称と商標のガイダンス](docs/project/trademarks.md)をご覧ください。派生物の +配布物を LoopX の公式リリースとして提示してはなりません。 + +[osai-verify: eb42dd9cf910399988f0]: # diff --git a/README.md b/README.md index bbdcd18b88..cee2c637f4 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ progress, decisions and verifiable results so work can continue across sessions. [![Local-first state](https://img.shields.io/badge/local--first-state-brightgreen.svg)](docs/public-private-boundary.md) [![Loop Agents](https://img.shields.io/badge/status-loop%20agents%20supported-brightgreen.svg)](docs/product/release-readiness.md) [![GitHub contributors](https://img.shields.io/github/contributors-anon/loopx-project/loopx?color=blue)](https://github.com/loopx-project/loopx/graphs/contributors) [![Discord](https://img.shields.io/badge/Discord-Join-5865F2?logo=discord&logoColor=white)](https://discord.gg/XmGgQyCFZd) [![Listed on dsh-plugin.org](https://dsh-plugin.org/badges/listed.svg)](https://dsh-plugin.org/plugins/loopx-project/loopx) -[Get started](#quick-start) · [Why LoopX](#why-loopx) · [24/7 Always-On](#always-on-agents) · [See it in action](#see-loopx-in-action) · [Evidence](#evidence) · [Contribute](#contributing) · [Docs](https://loopx-project.github.io/loopx/docs/) · [简体中文](README.zh-CN.md) +[Get started](#quick-start) · [Why LoopX](#why-loopx) · [24/7 Always-On](#always-on-agents) · [See it in action](#see-loopx-in-action) · [Evidence](#evidence) · [Contribute](#contributing) · [Docs](https://loopx-project.github.io/loopx/docs/) · [简体中文](README.zh-CN.md) · [日本語](README.ja.md) diff --git a/README.zh-CN.md b/README.zh-CN.md index ed24b4d2ba..408af31681 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -14,7 +14,7 @@ [![Local first](https://img.shields.io/badge/control--plane-local--first-brightgreen.svg)](docs/public-private-boundary.md) [![Loop Agents](https://img.shields.io/badge/status-loop%20agents%20supported-brightgreen.svg)](docs/product/release-readiness.md) [![GitHub contributors](https://img.shields.io/github/contributors-anon/loopx-project/loopx?color=blue)](https://github.com/loopx-project/loopx/graphs/contributors) [![Discord](https://img.shields.io/badge/Discord-Join-5865F2?logo=discord&logoColor=white)](https://discord.gg/XmGgQyCFZd) [![Listed on dsh-plugin.org](https://dsh-plugin.org/badges/listed.svg)](https://dsh-plugin.org/plugins/loopx-project/loopx) -[快速上手](#快速开始) · [为什么需要 LoopX](#为什么需要-loopx) · [7×24 常驻架构](#always-on-agents) · [看看实际使用](#看看-loopx-如何工作) · [证据与案例](#证据) · [参与贡献](#贡献) · [文档](https://loopx-project.github.io/loopx/docs/) · [English](README.md) +[快速上手](#快速开始) · [为什么需要 LoopX](#为什么需要-loopx) · [7×24 常驻架构](#always-on-agents) · [看看实际使用](#看看-loopx-如何工作) · [证据与案例](#证据) · [参与贡献](#贡献) · [文档](https://loopx-project.github.io/loopx/docs/) · [English](README.md) · [日本語](README.ja.md) diff --git a/examples/readme-star-history-smoke.py b/examples/readme-star-history-smoke.py index f74ed31703..de3d782f0a 100644 --- a/examples/readme-star-history-smoke.py +++ b/examples/readme-star-history-smoke.py @@ -101,6 +101,7 @@ def main() -> int: readme_sections = { ROOT / "README.md": ("## Current Status", "## Star History", "## License"), ROOT / "README.zh-CN.md": ("## 当前状态", "## Star 趋势", "## License"), + ROOT / "README.ja.md": ("## 現在のステータス", "## Star の推移", "## ライセンス"), } for readme, sections in readme_sections.items(): text = readme.read_text(encoding="utf-8") diff --git a/scripts/ci/impact_plan.py b/scripts/ci/impact_plan.py index 4992b47e26..596d01def9 100644 --- a/scripts/ci/impact_plan.py +++ b/scripts/ci/impact_plan.py @@ -9,7 +9,7 @@ import subprocess SCHEMA = "loopx_ci_job_plan_v1" -ROOT_DOCS = {"README.md", "README.zh-CN.md", "CHANGELOG.md", "CONTRIBUTING.md"} +ROOT_DOCS = {"README.md", "README.zh-CN.md", "README.ja.md", "CHANGELOG.md", "CONTRIBUTING.md"} POLICY_PATHS = ("scripts/ci/impact_plan.py", "scripts/ci/review_gate.py", ".github/workflows/python-tests.yml") OUTPUTS = ("core_tests", "backend_tests", "python_tests", "stage2c_tests", "presentation_tests") PRESENTATION_ROOTS = ("apps/presentation/dashboard/src/", "apps/presentation/dashboard/public/", "loopx/web/chat/") diff --git a/scripts/ci/test_impact_plan.py b/scripts/ci/test_impact_plan.py index f52b2353ce..6001700e01 100644 --- a/scripts/ci/test_impact_plan.py +++ b/scripts/ci/test_impact_plan.py @@ -28,6 +28,14 @@ def test_presentation_and_docs_union(self): "apps/presentation/dashboard/public/icon.svg"): self.assertEqual(candidate([Change(status, path), Change("M", "docs/design.md")])[0], "presentation") + def test_root_readme_locales_are_documents(self): + readmes = ("README.md", "README.zh-CN.md", "README.ja.md") + for path in readmes: + for status in ("A", "M", "D"): + with self.subTest(path=path, status=status): + self.assertEqual(candidate([Change(status, path)])[0], "docs") + self.assertEqual(candidate([Change("M", path) for path in readmes])[0], "docs") + def test_one_shared_unknown_prompt_test_or_dependency_forces_full(self): for path in ("loopx/cli.py", "loopx/control_plane/goals/vision_checkpoint.ts", "loopx/claude_goal_mode/commands/loopx.md", "tests/test_ui.py", diff --git a/tests/test_readme_presentation.py b/tests/test_readme_presentation.py index a338c54009..bb2df59f4d 100644 --- a/tests/test_readme_presentation.py +++ b/tests/test_readme_presentation.py @@ -29,7 +29,7 @@ def handle_endtag(self, tag: str) -> None: self.link = None -@pytest.mark.parametrize("readme", ["README.md", "README.zh-CN.md"]) +@pytest.mark.parametrize("readme", ["README.md", "README.zh-CN.md", "README.ja.md"]) def test_trending_achievement_is_linked_once_in_readme_hero(readme: str) -> None: text = (REPO_ROOT / readme).read_text(encoding="utf-8") document = LinkedImages() From d4fb46742f55071c1e5072e19223ca967b223ade Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:18:50 +0800 Subject: [PATCH 04/34] Reduce private App mirror delivery latency Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../extensions/lark/private_conversations.py | 45 +++++++---- tests/test_lark_private_conversations.py | 78 +++++++++++++++++++ 2 files changed, 108 insertions(+), 15 deletions(-) diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py index 8ec48634ef..099019c0ee 100644 --- a/loopx/extensions/lark/private_conversations.py +++ b/loopx/extensions/lark/private_conversations.py @@ -33,7 +33,7 @@ from ...control_plane.collaboration import conversation_reply_context from ...control_plane.content_digest import BARE_SHA256_PATTERN from ...control_plane.effect_runtime import effect_runtime_request_scope -from ...file_lock import exclusive_file_lock +from ...file_lock import exclusive_file_lock, try_exclusive_file_lock from ...presentation.markdown import markdown_scalar from ...presentation.renderers.conversation_status_markdown import render_conversation_status from .conversation_identity import identity_ref, lark_private_source @@ -41,7 +41,9 @@ from .event_collector_runtime import enrich_lark_event_reply_context from .goal_channel_transport import call, json_payload, lark_args, lark_provider_mention_identities, record_event_timing from .identity_shapes import LARK_APP_ID_PATTERN as APP_ID_PATTERN, LARK_MESSAGE_ID_PATTERN -from .inbox_reply import _message, reply_lark_event_inbox, update_lark_inbox_reply, verify_lark_inbox_reply +from .inbox_reply import ( + SourceAuthorityVerifier, _message, reply_lark_event_inbox, update_lark_inbox_reply, verify_lark_inbox_reply, +) from .manager_context import manager_failure_reply from .inbox_reactions import (_reconcile_outbound_input_reactions, mark_lark_event_inbox_processing, mark_lark_event_inbox_received) @@ -1262,6 +1264,7 @@ def _feedback(self, path: Path, record: dict[str, Any], *, inbox: Callable[[], P def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *, inbox: Callable[[], Path], edit_only: bool = False, authorization_check: Callable[[], bool] | None = None, + source_authority_verifier: SourceAuthorityVerifier | None = None, reply_placement: str | None = None, delivery_id: str | None = None, finalize_source: bool = True, image_attachments: list[dict[str, Any]] | None = None, historical_source_check: Callable[[], bool] | None = None) -> bool: @@ -1276,9 +1279,8 @@ def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *, if historical_source_check is not None and phase != "answer_images.historical": raise ValueError("historical source verification requires a media repair") phase = presentation_phase(record, phase) - source_authority = partial(self._source_authority, record) - if kind == "answer_images": - source_authority = partial(source_authority, expected_text=record["event"]["content"]) + expected_text = record["event"]["content"] if kind == "answer_images" else None + source_authority = source_authority_verifier or partial(self._source_authority, record, expected_text=expected_text) phase_state = record["deliveries"].get(phase, {}) if phase_state.get("verified"): return True @@ -1289,10 +1291,12 @@ def _deliver(self, path: Path, record: dict[str, Any], phase: str, text: str, *, text = _presentation_text(text) if phase_state.get("text") not in (None, text): raise ValueError("delivery content changed after an attempt") - if image_attachments and (authorization_check is None or edit_only - or not ((kind == "web" and reply_placement == "chat_root") + if image_attachments and (edit_only + or not ((kind == "web" and reply_placement == "chat_root" + and (authorization_check is not None or source_authority_verifier is not None)) or (kind == "answer_images" and record["event"].get("chat_type") == "p2p" - and record["source"].get("private_human_message") is True))): + and record["source"].get("private_human_message") is True + and authorization_check is not None))): raise ValueError("Image projection requires its original private outbox") record_event_timing(record["profile"], record["event"]["message_id"], "delivery_started") config = inbox() @@ -1318,8 +1322,9 @@ def verified_source() -> bool: def image_authorized() -> bool: # A provider read can overlap Stop, Close, or an edit to the old reply. - return authorization_check is not None and (provider_images_current() and authorization_check() - if kind == "answer_images" else authorization_check() and verified_source()) + if kind == "answer_images": + return provider_images_current() and authorization_check is not None and authorization_check() + return (authorization_check is None or authorization_check()) and verified_source() image_keys = () if image_attachments: @@ -2114,11 +2119,17 @@ def _reconcile_web_projection(self, path: Path) -> int: for message in messages) else "delivered" _atomic_write_json(path, row) return 0 - def authorized() -> bool: + def authorized_source(profile: str, chat_id: str, message_id: str) -> tuple[str | None, bool]: + # Check the original provider source first. The fresh App + # grant and canonical body checks then cover any changes + # during that read, both before preview and before dispatch. + name, verified = self._source_authority(row, profile, chat_id, message_id) + if not name or not verified: + return None, False try: - return pending in self._web_projection_messages(row) + return name, pending in self._web_projection_messages(row) except (OSError, ValueError, KeyError): - return False + return None, False who = "你" if pending["role"] == "user" else "LoopX" text = f"**{who}(App)**\n\n{pending['text']}" if self.reaction_feedback and pending["role"] == "user": @@ -2127,7 +2138,7 @@ def authorized() -> bool: row["deliveries"].setdefault("web." + pending["message_id"], {}).setdefault("feedback", {}) _atomic_write_json(path, row) delivered = self._deliver(path, row, "web." + pending["message_id"], text, - inbox=lambda: self._inbox(row), authorization_check=authorized, + inbox=lambda: self._inbox(row), source_authority_verifier=authorized_source, reply_placement="chat_root", delivery_id=self._web_projection_delivery_id(row, pending), finalize_source=False, image_attachments=pending.get("attachments")) if delivered: @@ -2151,7 +2162,11 @@ def reconcile_app_feedback(self) -> int: for path in sorted((self.root / "web-projections").glob("*.json")): with exclusive_file_lock(path.with_suffix(".feedback"), operation="reconcile_web_input_feedback"): try: - with exclusive_file_lock(path, operation="read_web_input_feedback"): + # Leave a busy body outbox for the next feedback pass so + # independent Sessions can still acknowledge their inputs. + with try_exclusive_file_lock(path, operation="read_web_input_feedback") as lock: + if lock is None: + continue row = self._read_web_projection_journal(path) except (OSError, ValueError): continue diff --git a/tests/test_lark_private_conversations.py b/tests/test_lark_private_conversations.py index 393ee609af..1e8d7c13a1 100644 --- a/tests/test_lark_private_conversations.py +++ b/tests/test_lark_private_conversations.py @@ -4040,6 +4040,84 @@ def test_app_projection_refuses_a_replaced_provider_identity(ordinary): # noqa: runtime.close() +@pytest.mark.parametrize("fault", ["revoked", "provider_changed", "body_changed"]) +def test_app_projection_checks_current_authority_after_reading_original_source(ordinary, monkeypatch, fault): # noqa: F811 + store, runtime, provider, transport, original, turns, before = _app_projection_journey(ordinary) + sid = original["session_id"] + transcript = store.sessions_root / sid / "messages.jsonl" + request_path = transport.root / f"{original['request_ref']}.json" + source_id = json.loads(request_path.read_text())["event"]["message_id"] + call = Provider.__call__ + previewed = changed = False + + def change_after_source_read(self, args, *pos, **kwargs): + nonlocal previewed, changed + result = call(self, args, *pos, **kwargs) + if "+messages-send" in args and "--dry-run" in args: + previewed = True + elif (previewed and not changed and "+messages-mget" in args + and args[args.index("--message-ids") + 1] == source_id): + if fault == "revoked": + transport.bindings.disconnect(original["binding_id"], expected_revision=transport.bindings.read()["revision"]) + elif fault == "provider_changed": + provider.profile_apps["notes-app"] = "cli_replaced_app" + else: + messages = store.messages(sid) + next(message for message in messages if message.get("turn_id") == turns[0])["text"] = "Changed during source read" + transcript.write_text("".join(json.dumps(message) + "\n" for message in messages)) + changed = True + return result + + try: + starts = ordinary[4].read_bytes() + monkeypatch.setattr(Provider, "__call__", change_after_source_read) + transport._web_projection_scan_at = 0 + path, = transport._web_projection_paths() + assert transport.reconcile_request(path) == 0 + assert changed and len(provider.writes) == before + assert ordinary[4].read_bytes() == starts + assert not any(phase.get("started") for phase in transport._read_web_projection_journal(path)["deliveries"].values()) + finally: + runtime.close() + + +def test_busy_app_body_does_not_delay_feedback_for_another_session(ordinary): # noqa: F811 + from loopx.file_lock import exclusive_file_lock, lock_incident_path + + _store, runtime, provider, transport = connect(ordinary) + try: + for profile in ("notes-app", "steward-app"): + event = provider.event(profile, f"feedback-source-{profile}", "Admit the original private conversation") + transport.admit(profile, event) + request = transport.core.read_request(identity_ref(transport._binding(profile)["provider_ref"], event["message_id"])) + sid = request["session_id"] + assert runtime.wait_for_turn(session_id=sid, turn_id=request["turn_id"], timeout_sec=10)["status"] == "completed" + transport.reconcile_request(transport.root / f"{request['request_ref']}.json") + status, turn = ordinary[3](f"/api/chat/sessions/{sid}/turns", {"message": "New App input", "client_turn_id": "feedback"}) + assert status == 202 + assert runtime.wait_for_turn(session_id=sid, turn_id=turn["turn_id"], timeout_sec=10)["status"] == "completed" + + transport._web_projection_scan_at = 0 + busy, ready = sorted(transport._web_projection_paths()) + targets = {} + for path in (busy, ready): + assert transport.reconcile_request(path) == 1 + row = transport._read_web_projection_journal(path) + phase, = row["deliveries"].values() + assert phase["verified"] and phase["feedback"] == {} + targets[path] = (row["profile"], phase["attempt"]["message_ref"], "Get") + + with exclusive_file_lock(busy, operation="deliver_web_conversation_projection"): + transport.reconcile_app_feedback() + assert not lock_incident_path(busy).exists() + assert targets[busy] not in provider.reaction_creates + assert targets[ready] in provider.reaction_creates + transport.reconcile_app_feedback() + assert all(provider.reaction_creates.count(target) == 1 for target in targets.values()) + finally: + runtime.close() + + @pytest.mark.parametrize("fault", ["revoked", "app", "source", "session", "turn", "context_recovery"]) def test_app_feedback_rechecks_exact_source_and_turn_after_mirror_delivery(ordinary, monkeypatch, fault): # noqa: F811 store, runtime, provider, transport, original, turns, before = _app_projection_journey(ordinary) From 35c22c492fb44d5eecf4e8dae483cb218fc37719 Mon Sep 17 00:00:00 2001 From: xiaobaifly7 Date: Mon, 16 Nov 2026 16:34:00 +0800 Subject: [PATCH 05/34] fix(replan): keep a Monitor-streak replan's receipt source after closeout (#8854) A replan admitted for a Monitor's no-change streak lost its receipt source once the agent completed the Monitor inside that Turn, so the same Turn's writeback and guard re-entry failed with heartbeat_receipt_identity_conflict. Treat the streak as a scalar review source, with a fixed kind precedence independent of trigger order. Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-authored-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .../work_items/replan_semantics.ts | 7 +- .../references/repair-patterns.md | 2 +- .../test_monitor_replan_closeout.py | 94 +++++++++++++++++++ .../control_plane_ts/replan_semantics.test.ts | 31 +++++- 4 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 tests/control_plane/test_monitor_replan_closeout.py 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/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_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_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, From d22a49720a2cedadec88e39c159878efbaa34be5 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:34:09 +0800 Subject: [PATCH 06/34] Preserve scheduler reconciliation during recovery waits (#8851) * Preserve requested scheduler detail during recovery waits Prior-Turn recovery rebuilds the scheduler hint when the unchanged admission limit is reached. Forward the CLI detail selector to that projection so the DSH Driver receives the bounded recheck cadence it requested. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> * Keep App scheduler reconciliation executable during recovery waits Reuse observed host cadence and persisted backoff when projecting a prior-Turn recovery wait. Preserve the registered scheduler scope and place CLI options after the standalone interpreter prefix so the matched host can be acknowledged using the original receipt. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --------- Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/control_plane/quota/live_decision.py | 63 ++++++++++-- loopx/control_plane/quota/should_run.py | 6 +- .../quota/unsettled_host_turn.py | 22 ++--- loopx/quota.py | 2 + .../test_scheduler_fallback_hint.py | 30 ++++-- ...test_unsettled_recovery_admission_bound.py | 97 ++++++++++++++++++- 6 files changed, 190 insertions(+), 30 deletions(-) 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/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/quota.py b/loopx/quota.py index 68f27bd509..4d774c4980 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, ) 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( From ab95b72e5856be4a4d976795ea67cac4337f1f01 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:34:19 +0800 Subject: [PATCH 07/34] Fence DSH maintenance plans returned after cancellation (#8856) Revalidate the captured Session and combined admission signal before publishing a queue or scheduler wait plan. Cover Pause and native maintenance cancellation for both plans, followed by a fresh evaluation. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- packages/dsh-loopx-plugin/src/driver.ts | 4 ++ .../dsh-loopx-plugin/tests/driver.spec.ts | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+) 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() From 2998d5e16f12f8090ab9074367c37e5713b4b423 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:34:30 +0800 Subject: [PATCH 08/34] Preserve keyboard focus when Goal work is reprioritized (#8849) Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../goal-workspace-work.tsx | 35 +++++++++++++++++-- .../goal-workspace-m2.mjs | 35 +++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) 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/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")); From 7c6c94298295123a2da4a63d134ec4e2069a6c3b Mon Sep 17 00:00:00 2001 From: xiaobaifly7 Date: Mon, 16 Nov 2026 16:34:40 +0800 Subject: [PATCH 09/34] fix(monitor): never project a User gate completion from an observation (#8770) * fix(monitor): project User gate completion for an observed prerequisite An admitted due Monitor that still has open User gates now gets the existing User lifecycle completion command ahead of its poll actions, on unchanged observations too. The Monitor Turn's settlement identity stays separate; observation never grants approval. Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-Authored-By: Claude * fix(monitor): never project a User gate completion from an observation Replace the projected completion command with a read-only notice. A due Monitor that is admitted while the agent has open User gates is told that its observation cannot complete or approve a gate, which attempts stay rejected, and that work continuing on an observed change belongs in the poll's own successor. Unrelated owner decisions receive no command. Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-Authored-By: Claude * fix(monitor): do not point an observation at any User gate The notice told the agent to report the observed evidence as the pending user action of an open gate, even when the gate was unrelated to the observation. Keep only the boundary (an observation never completes or approves a gate) and the original poll. Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --------- Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-authored-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-authored-by: Claude --- .../canonical-todo-completion-update.md | 22 ++++ .../work_items/interaction_contract.py | 17 ++- .../test_monitor_gate_handoff.py | 104 ++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 tests/control_plane/test_monitor_gate_handoff.py 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/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/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) From cac86bbad92ba99c3430b3f7eaa9632727573821 Mon Sep 17 00:00:00 2001 From: xiaobaifly7 Date: Mon, 16 Nov 2026 16:34:50 +0800 Subject: [PATCH 10/34] fix(turn): complete validated Codex Todos before accountable writeback (#8584) * fix(turn): enable builtin Codex durable Todo completion Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> * fix(turn): validate terminal Todo before accountable refresh Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> * refactor(turn): remove redundant lease shape check Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> * Recheck the original Goal after built-in Codex validation Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --------- Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Co-authored-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-authored-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../codex-cli/codex-cli-automation-driver.md | 26 + loopx/cli_commands/turn_run_once.py | 38 +- loopx/control_plane/turn_driver/codex_cli.py | 3 +- tests/test_codex_turn_completion.py | 512 ++++++++++++++++++ tests/test_loopx_turn_driver.py | 1 + 5 files changed, 578 insertions(+), 2 deletions(-) create mode 100644 tests/test_codex_turn_completion.py 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/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/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/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": { From 3d30531c8fac4d37ebae377b0cd925ff4a960c2f Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:15:52 +0800 Subject: [PATCH 11/34] fix(lark): keep App feedback writeback nonblocking Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../extensions/lark/private_conversations.py | 13 ++++---- tests/test_lark_private_conversations.py | 30 +++++++++++++++++-- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py index 099019c0ee..0adb10bf31 100644 --- a/loopx/extensions/lark/private_conversations.py +++ b/loopx/extensions/lark/private_conversations.py @@ -2151,12 +2151,7 @@ def authorized_source(profile: str, chat_id: str, message_id: str) -> tuple[str return 0 def reconcile_app_feedback(self) -> int: - """Reconcile optional reactions independently of the body outbox. - - The live service reserves at most one existing reply worker for this - pass. A slow reaction must never own the body-delivery fence or prevent - a later canonical answer from reaching the same audience. - """ + """Reconcile optional reactions independently of the body outbox.""" if not self.reaction_feedback: return 0 for path in sorted((self.root / "web-projections").glob("*.json")): @@ -2191,7 +2186,11 @@ def reconcile_app_feedback(self) -> int: self._web_projection_feedback(row, message) if "feedback" not in phase or phase["feedback"] == prior_feedback: continue - with exclusive_file_lock(path, operation="record_web_input_feedback"): + # The reaction has its own receipt. Defer writeback when + # a body worker acquired the journal during provider I/O. + with try_exclusive_file_lock(path, operation="record_web_input_feedback") as lock: + if lock is None: + continue try: current = self._read_web_projection_journal(path) except (OSError, ValueError): diff --git a/tests/test_lark_private_conversations.py b/tests/test_lark_private_conversations.py index 1e8d7c13a1..bef4c20103 100644 --- a/tests/test_lark_private_conversations.py +++ b/tests/test_lark_private_conversations.py @@ -4081,7 +4081,10 @@ def change_after_source_read(self, args, *pos, **kwargs): runtime.close() -def test_busy_app_body_does_not_delay_feedback_for_another_session(ordinary): # noqa: F811 +@pytest.mark.parametrize("lock_at", ["read", "writeback"]) +def test_busy_app_body_does_not_delay_feedback_for_another_session(ordinary, monkeypatch, lock_at): # noqa: F811 + from contextlib import ExitStack + from loopx.file_lock import exclusive_file_lock, lock_incident_path _store, runtime, provider, transport = connect(ordinary) @@ -4107,13 +4110,34 @@ def test_busy_app_body_does_not_delay_feedback_for_another_session(ordinary): # assert phase["verified"] and phase["feedback"] == {} targets[path] = (row["profile"], phase["attempt"]["message_ref"], "Get") - with exclusive_file_lock(busy, operation="deliver_web_conversation_projection"): + with ExitStack() as body_worker, monkeypatch.context() as patch: + body_lock = exclusive_file_lock(busy, operation="deliver_web_conversation_projection") + if lock_at == "read": + body_worker.enter_context(body_lock) + else: + feedback = transport._web_projection_feedback + busy_session = transport._read_web_projection_journal(busy)["session_id"] + + def hold_body_after_feedback(row, message): + feedback(row, message) + if row["session_id"] == busy_session: + assert targets[busy] in provider.reaction_creates + body_worker.enter_context(body_lock) + + patch.setattr(transport, "_web_projection_feedback", hold_body_after_feedback) + transport.reconcile_app_feedback() assert not lock_incident_path(busy).exists() - assert targets[busy] not in provider.reaction_creates + busy_phase, = transport._read_web_projection_journal(busy)["deliveries"].values() + assert busy_phase["feedback"] == {} + assert (targets[busy] in provider.reaction_creates) == (lock_at == "writeback") assert targets[ready] in provider.reaction_creates + ready_phase, = transport._read_web_projection_journal(ready)["deliveries"].values() + assert ready_phase["feedback"]["ok"] transport.reconcile_app_feedback() assert all(provider.reaction_creates.count(target) == 1 for target in targets.values()) + busy_phase, = transport._read_web_projection_journal(busy)["deliveries"].values() + assert busy_phase["feedback"]["ok"] finally: runtime.close() From cd3bf61fbbab6c8a32a0f96012391e517160cd01 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:16:11 +0800 Subject: [PATCH 12/34] fix(chat): scope saved answer reads to the original Turn Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../conversation-reply-browser-smoke.mjs | 21 +++++++++++++++++++ .../personal-workspace/answer-report-page.tsx | 13 ++++++++---- .../personal-workspace/channel-timeline.tsx | 6 ++++-- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs b/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs index 784a95aa7c..f00e756fc0 100644 --- a/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs +++ b/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs @@ -80,11 +80,20 @@ async function checkReturnedFiles(ready, page, errors, writes) { } } await page.screenshot({path: resolve(output, `${baseline ? "before" : "after"}-files-desktop.png`), animations: "disabled"}); + const historyPath = `/api/chat/sessions/${ready.sessionId}`; + const readerRequest = page.context().waitForEvent("request", {predicate: request => + new URL(request.url()).pathname === historyPath + && new URL(request.frame().url()).searchParams.get("reportMessageId") === ready.fileMessageId}); const opened = page.context().waitForEvent("page"); await timeline.getByRole("link", {name: "单独阅读完整答复"}).click(); const report = await opened; report.on("pageerror", error => errors.push(error.message)); await report.getByRole("heading", {name: "结果已核验 / Verified result", exact: true}).waitFor(); + const readerQuery = new URL((await readerRequest).url()).searchParams; + if (!baseline) { + assert.equal(readerQuery.get("turn_id"), answer.turn_id, "The saved answer reads its original Turn"); + assert.equal(readerQuery.get("message_limit"), "200"); + } assert.equal(await report.locator("a[download]").count(), baseline ? 0 : ready.files.length); await report.screenshot({path: resolve(output, `${baseline ? "before" : "after"}-files-reader.png`), animations: "disabled"}); if (!baseline) { @@ -109,6 +118,18 @@ async function checkReturnedFiles(ready, page, errors, writes) { const url = ready.origin + answer.attachments[0].data_url; assert.equal((await page.request.get(url.replace(ready.sessionId, "unrelated-session"))).status(), 404); assert.equal((await page.request.get(url, {headers: {Origin: "https://unrelated.example"}})).status(), 403); + + const legacy = new URL(report.url()); + legacy.searchParams.delete("reportTurnId"); + const legacyRead = report.waitForRequest(request => new URL(request.url()).pathname === historyPath); + await report.goto(legacy.toString()); + await report.getByRole("link", {name: "Download file: source.bin", exact: false}).waitFor(); + assert.equal(new URL((await legacyRead).url()).searchParams.has("turn_id"), false); + + legacy.searchParams.set("reportTurnId", "unrelated-turn"); + await report.goto(legacy.toString()); + await report.getByText(/This answer was not found|Could not load this answer/).waitFor(); + assert.equal(await report.locator("a[download]").count(), 0); } child.stdin.write("inspect\n"); const inspection = await next(); diff --git a/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx b/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx index 5f38dc2034..5dbac41da2 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx @@ -1,6 +1,6 @@ import {useEffect, useMemo, useRef, useState} from "react"; -import {fetchChatSession, type ChatSessionSnapshot, type ChatVisibleMessage} from "../../data/chat"; +import {fetchChatSession, fetchChatSessionHistory, type ChatSessionSnapshot, type ChatVisibleMessage} from "../../data/chat"; import {visibleAgentMessage} from "./answer-text"; import {readWorkspaceLocale} from "./i18n"; import {resolveConversationScope} from "../../../../../../loopx/control_plane/collaboration/conversation_scope"; @@ -24,7 +24,8 @@ export function AnswerReportPage({sessionId, messageId, statusUrl}: { messageId: string; statusUrl: string; }) { - const scope = JSON.stringify([statusUrl || "/status.json", sessionId, messageId]); + const turnId = new URLSearchParams(window.location.search).get("reportTurnId"); + const scope = JSON.stringify([statusUrl || "/status.json", sessionId, messageId, turnId]); const [loaded, setLoaded] = useState<{scope: string; value: ReportState} | null>(null); // A late read from the preceding source never appears in the new reader. const state: ReportState = loaded?.scope === scope ? loaded.value : {kind: "loading"}; @@ -44,7 +45,10 @@ export function AnswerReportPage({sessionId, messageId, statusUrl}: { setLoaded({scope, value: {kind: "loading"}}); setCopied(false); setCopyFailed(false); - void fetchChatSession(sessionId, controller.signal).then((snapshot) => { + const read = turnId === null + ? fetchChatSession(sessionId, controller.signal) + : fetchChatSessionHistory(sessionId, controller.signal, turnId); + void read.then((snapshot) => { if (!live) return; if (snapshot.ok !== true || snapshot.schema_version !== "loopx_chat_store_v1" || snapshot.session.session_id !== sessionId) throw new Error("The saved Session changed during read."); @@ -53,7 +57,7 @@ export function AnswerReportPage({sessionId, messageId, statusUrl}: { setLoaded({scope, value: message ? {kind: "ready", snapshot, message} : {kind: "missing", snapshot}}); }).catch(() => { if (live) setLoaded({scope, value: {kind: "error"}}); }); return () => {live = false; controller.abort(); copyAttempt.current += 1; document.title = previousTitle;}; - }, [scope, sessionId, messageId, zh, readRevision]); + }, [scope, sessionId, messageId, turnId, zh, readRevision]); const navigation = answerReportNavigation( (state.kind === "ready" || state.kind === "missing") ? state.snapshot.session : null, window.location.href, statusUrl, zh, @@ -180,6 +184,7 @@ export function answerReportNavigation( const managed = scope.kind === "owner_goal" && session?.managed_agent_id; url.searchParams.delete("reportSessionId"); url.searchParams.delete("reportMessageId"); + url.searchParams.delete("reportTurnId"); url.searchParams.delete("todo"); url.searchParams.delete("messageSession"); url.searchParams.delete("message"); diff --git a/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx b/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx index 43549a1332..2645c89c58 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/channel-timeline.tsx @@ -23,10 +23,12 @@ import { conversationOrder } from "./conversation-order"; import { ConversationFind, conversationMessageKey } from "./conversation-find"; import type { WorkspaceActionPreview, WorkspaceDrawerSelection, WorkspaceGoal, WorkspaceTimelineItem } from "./personal-workspace-model"; -function answerLink(sessionId: string, messageId: string) { +function answerLink(sessionId: string, messageId: string, turnId?: string) { const url = new URL(window.location.href); url.searchParams.set("reportSessionId", sessionId); url.searchParams.set("reportMessageId", messageId); + if (turnId) url.searchParams.set("reportTurnId", turnId); + else url.searchParams.delete("reportTurnId"); url.searchParams.set("view", "conversation"); url.hash = ""; return url.toString(); @@ -288,7 +290,7 @@ export const ChannelTimeline = memo(function ChannelTimeline({ {item.message.role === "assistant" && !item.message.pending && item.message.text.length >= MIN_SEPARATE_ANSWER_LENGTH && item.message.sourceSessionId && item.message.sourceMessageId - ? {locale === "zh-CN" ? "单独阅读完整答复" : "Read full answer separately"} : null} {item.message.role !== "user" && (item.message.pending || item.message.sourceTurnId || item.message.activity?.length || item.message.steps?.length) ? : null} From c8122fd2bb2daf2961ff8918c99c7ae39882798e Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:57:53 +0800 Subject: [PATCH 13/34] fix(chat): bind cold import to the registered Goal source Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../coordination/cold_source_import.ts | 13 ++- loopx/presentation/goal_storage_api.py | 81 ++++++++++++++----- .../test_chat_cold_source_import.py | 55 ++++++++++++- 3 files changed, 124 insertions(+), 25 deletions(-) diff --git a/loopx/control_plane/coordination/cold_source_import.ts b/loopx/control_plane/coordination/cold_source_import.ts index 4a37736fb1..209804f008 100644 --- a/loopx/control_plane/coordination/cold_source_import.ts +++ b/loopx/control_plane/coordination/cold_source_import.ts @@ -234,13 +234,16 @@ export async function executeColdSourceImport(value: unknown, try { const input = requireJsonObject(value, "cold source import request"); const action = input.action; + const bindSourceRegistry = (action === "apply" || action === "readback") + && Object.hasOwn(input, "expected_source_registry"); + const sourceRegistryKeys = bindSourceRegistry ? ["expected_source_registry"] : []; if (input.schema_version !== COLD_SOURCE_IMPORT_REQUEST_SCHEMA || (action !== "prepare" && action !== "apply" && action !== "recover" && action !== "readback") || !hasExactAuthorityKeys(input, action === "prepare" ? ["schema_version", "action", "runtime_root", "goal_id", "operation_id", "projection", "source_snapshot", "target_handoff_mode", "target_provider", "source_backup"] : action === "apply" - ? ["schema_version", "action", "runtime_root", "goal_id", "operation_id", "expected_plan_sha256", "writers_stopped"] - : ["schema_version", "action", "runtime_root", "goal_id", "operation_id", "expected_plan_sha256"])) { + ? ["schema_version", "action", "runtime_root", "goal_id", "operation_id", "expected_plan_sha256", "writers_stopped", ...sourceRegistryKeys] + : ["schema_version", "action", "runtime_root", "goal_id", "operation_id", "expected_plan_sha256", ...sourceRegistryKeys])) { reject("cold_import_request_invalid"); } const root = requireLocalAuthorityRuntimeRoot(input.runtime_root); @@ -312,6 +315,12 @@ export async function executeColdSourceImport(value: unknown, fence_id: `cold-import:${operation}`, import_operation_id: operation, import_plan_sha256: raw.plan_sha256}; if (prior.status === "loaded" && !same(prior.fence, fence)) reject("cold_import_fence_conflict"); const source = plan.source as ShadowRequest; + if (bindSourceRegistry) { + // The host resolves the current source before entering the effect. + // The immutable plan does not authorize a source the host no longer selects. + const registration = requireJsonObject(source.source_snapshot.registry_source, "retained registry source"); + if (registration.path !== input.expected_source_registry) reject("source_registry_changed_retry"); + } const projection = requireJsonObject(plan.target_projection, "import target projection"); const identity = {operation_id: operation, projection_sha256: canonicalAuthoritySha256(projection), receipt: {schema_version: "loopx_cold_source_import_receipt_v0", goal_id: goal, diff --git a/loopx/presentation/goal_storage_api.py b/loopx/presentation/goal_storage_api.py index 10630b6e1b..11c50c71ed 100644 --- a/loopx/presentation/goal_storage_api.py +++ b/loopx/presentation/goal_storage_api.py @@ -119,6 +119,52 @@ def _storage_send(self, result: dict[str, Any], goal_id: str, preview_id: str | payload["preview_id"] = preview_id self._send_json(payload, status=200 if result.get("ok") else 409) + def _storage_source_registry( + self, goal_id: str, registry: dict[str, Any], goal: dict[str, Any], root: Path, + ) -> Path: + invoked = Path(self.server.registry_path).resolve() + if not goal.get("source_registry"): + return invoked + + from ..control_plane.runtime.runtime_projection_route import resolve_goal_source_runtime_route + + route = resolve_goal_source_runtime_route( + registry_path=invoked, registry=registry, goal_id=goal_id, + ) + # A Chat runtime can be bound directly to the source behind a global + # index. Keep the host's runtime scope and existing same-root plans. + if (route["routed_to_source_registry"] + and route["invoked_runtime_root"] != str(root) + and route["source_runtime_root"] == str(root)): + return Path(route["source_registry"]) + return invoked + + def _storage_cold_snapshot( + self, goal_id: str, registry: dict[str, Any], goal: dict[str, Any], + root: Path, source_registry: Path, + ) -> tuple[Path, dict[str, object], dict[str, object]]: + from ..control_plane.coordination.authority_source_capture import build_runtime_shadow_source_snapshot + from ..control_plane.goals.state_resolution import resolve_goal_state + from ..control_plane.projects.registry_codec import load_project_registry + from ..registry import find_registry_goal + + if source_registry != Path(self.server.registry_path).resolve(): + registry = load_project_registry(source_registry) + selected_goal = find_registry_goal(registry, goal_id) + if selected_goal is None: + raise ValueError("registered Goal source changed") + goal = selected_goal + _, project, state = resolve_goal_state( + registry=registry, goal_id=goal_id, project_override=None, state_file_override=None, + ) + if project is None: + raise ValueError("registered project is required") + projection, snapshot = build_runtime_shadow_source_snapshot( + goal=goal, runtime_root=root, state_path=state, registry_path=source_registry, + include_all_archived_todos=True, + ) + return project, projection, snapshot + def _storage_inspect(self) -> None: try: query = parse_qs(urlparse(self.path).query, keep_blank_values=True) @@ -133,18 +179,14 @@ def _storage_inspect(self) -> None: result = self._storage_owner(goal_id, action="migration-readback") current = result.get("current") if result.get("ok") and isinstance(current, dict) and current.get("canonical") is False: - from ..control_plane.coordination.local_authority_shadow_projection import source_effect_runtime_result - from ..control_plane.coordination.authority_source_capture import build_runtime_shadow_source_snapshot - from ..state_refresh import resolve_goal_state - - _, _, state_path = resolve_goal_state(registry=registry, goal_id=goal_id, - project_override=None, state_file_override=None) - projection, snapshot = build_runtime_shadow_source_snapshot(goal=goal, - runtime_root=self.server.runtime_root, state_path=state_path, - registry_path=self.server.registry_path, include_all_archived_todos=True) + root = self.server.runtime_root.resolve() + source_registry = self._storage_source_registry(goal_id, registry, goal, root) + _, projection, snapshot = self._storage_cold_snapshot( + goal_id, registry, goal, root, source_registry, + ) result = source_effect_runtime_result("coordination.source.inspect_storage", { "schema_version": "loopx_cold_source_inspection_request_v0", - "runtime_root": str(self.server.runtime_root.expanduser().absolute()), + "runtime_root": str(root), "goal_id": goal_id, "projection": projection, "source_snapshot": snapshot, }) _, saved = self._storage_preview_read(self._storage_preview_path(goal_id), goal_id) @@ -274,20 +316,12 @@ def _storage_import(self, *, action: str) -> None: request: dict[str, Any] = {"schema_version": "loopx_cold_source_import_request_v0", "action": action, "runtime_root": str(root), "goal_id": goal_id, "operation_id": operation} if action == "prepare": - from ..control_plane.goals.state_resolution import resolve_goal_state - from ..control_plane.coordination.authority_source_capture import build_runtime_shadow_source_snapshot from ..control_plane.coordination.cold_source_backup import create_cold_source_backup - _, project, state = resolve_goal_state(registry=registry, goal_id=goal_id, - project_override=None, state_file_override=None) - if project is None: - raise ValueError("registered project is required") - # Source capture witnesses physical filenames. Give backup IO - # the same registry route so directory aliases cannot split - # its saved member map from the source's byte witness. - registry_path = Path(self.server.registry_path).resolve() - projection, snapshot = build_runtime_shadow_source_snapshot(goal=goal, runtime_root=root, - state_path=state, registry_path=registry_path, include_all_archived_todos=True) + registry_path = self._storage_source_registry(goal_id, registry, goal, root) + project, projection, snapshot = self._storage_cold_snapshot( + goal_id, registry, goal, root, registry_path, + ) source_paths = [ Path(snapshot["state_path"]), Path(snapshot["registry_source"]["path"]), *(Path(item["path"]) for item in snapshot["evidence_files"]), @@ -302,6 +336,9 @@ def _storage_import(self, *, action: str) -> None: request["expected_plan_sha256"] = _token(body, "plan_sha256", 64) if action == "apply": request["writers_stopped"] = body["writers_stopped"] + request["expected_source_registry"] = str( + self._storage_source_registry(goal_id, registry, goal, root) + ) path = self._storage_preview_path(goal_id) path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) with exclusive_file_lock(path): diff --git a/tests/control_plane/test_chat_cold_source_import.py b/tests/control_plane/test_chat_cold_source_import.py index af755f0892..e9cc334562 100644 --- a/tests/control_plane/test_chat_cold_source_import.py +++ b/tests/control_plane/test_chat_cold_source_import.py @@ -32,7 +32,18 @@ def cold_api(tmp_path, monkeypatch, request, source_modules_present): # of this same set before the cold receiver is copied. (receiver / "loopx" / name).unlink(missing_ok=True) registry = tmp_path / "project/.loopx/registry.json" - if request.param: + if request.param == "global-source": + # Keep Chat bound to the source runtime, but invoke it through the + # global registry whose own runtime holds the machine index. + global_root = tmp_path / "machine-runtime" + global_root.mkdir() + payload = json.loads(registry.read_text()) + payload["common_runtime_root"] = str(global_root) + payload["registry_role"] = "global-local" + payload["goals"][0]["source_registry"] = str(registry) + registry = global_root / "registry.global.json" + registry.write_text(json.dumps(payload)) + elif request.param: # The configured registry may live outside conventional Goal folders. # Only its explicit backup member can cover this observed source. external = tmp_path / "project/source-registry.json" @@ -213,6 +224,48 @@ def action(kind, parameters, key): assert read() == later +@pytest.mark.parametrize("cold_api", ["global-source"], indirect=True) +def test_cold_http_binds_global_mirror_to_the_registered_source(cold_api): + call, read, state, body, runtime, registry, _, restart = cold_api + original_state = state.read_bytes() + original_mirror = registry.read_bytes() + mirror = json.loads(original_mirror) + source = runtime.parent / "project/.loopx/registry.json" + + code, observed = call() + assert code == 200 and observed["current"]["canonical"] is False, observed + assert observed["cold_source"]["active_todo_count"] == 1 + plan = prepare(call, handoff_mode="soft_claim") + saved = carrier(plan) + assert plan["coordination_source_backup_verified"] is True + assert plan["source_inventory"]["archived_todo_count"] == 1 + restart() + code, pending = call() + assert code == 200 and pending["pending_preview"] == saved + assert call("recover", saved)[1]["status"] == "prepared" + code, refused = call("apply", {**saved, "writers_stopped": False}) + assert code == 409 and refused["reason_code"] == "cold_import_operator_stop_confirmation_required" + + # The saved plan still has an intact source file, the same Goal and the + # same target store. An explicit rebinding must still invalidate Apply. + other_registry = source.with_name("other-registry.json") + other_registry.write_bytes(source.read_bytes()) + mirror["goals"][0]["source_registry"] = str(other_registry) + registry.write_text(json.dumps(mirror)) + code, refused = call("apply", {**saved, "writers_stopped": True}) + assert code == 409 and refused["reason_code"] == "source_registry_changed_retry" + assert not list(runtime.rglob("writer-fence.json")) + + registry.write_bytes(original_mirror) + code, applied = call("apply", {**saved, "writers_stopped": True}) + assert code == 200 and applied["status"] == "applied", applied + assert applied["current"]["provider"] == "sqlite" + assert applied["execution_authority_granted"] is False + assert next(row["text"] for row in read()["todos"] if row["todo_id"] == "todo_current") == body + assert state.read_bytes() == original_state + assert not list(registry.parent.rglob("writer-fence.json")) + + def test_cold_http_changed_source_backup_and_untrusted_inputs(cold_api): call, read, state, _, runtime, _, _, _ = cold_api plan = prepare(call) From 97002bedd9827d0aa5c3bfb988ede22be40772da Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:12:42 +0800 Subject: [PATCH 14/34] fix(chat): read the chosen saved answer instead of its history Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../conversation-reply-browser-smoke.mjs | 55 +++++++++++++++++-- apps/presentation/dashboard/src/data/chat.ts | 20 +++++++ .../personal-workspace/answer-report-page.tsx | 6 +- .../capabilities/manager_context/roundtrip.py | 54 ++++++++++++++---- loopx/chat_server.py | 3 +- tests/test_manager_context_roundtrip.py | 28 ++++++++++ 6 files changed, 147 insertions(+), 19 deletions(-) diff --git a/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs b/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs index f00e756fc0..7c35dd485f 100644 --- a/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs +++ b/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs @@ -91,8 +91,9 @@ async function checkReturnedFiles(ready, page, errors, writes) { await report.getByRole("heading", {name: "结果已核验 / Verified result", exact: true}).waitFor(); const readerQuery = new URL((await readerRequest).url()).searchParams; if (!baseline) { - assert.equal(readerQuery.get("turn_id"), answer.turn_id, "The saved answer reads its original Turn"); - assert.equal(readerQuery.get("message_limit"), "200"); + assert.equal(readerQuery.get("turn_id"), answer.turn_id, "The saved answer checks its original Turn"); + assert.equal(readerQuery.get("report_message_id"), answer.message_id, "The reader requests only the chosen answer"); + assert.equal(readerQuery.has("message_limit"), false); } assert.equal(await report.locator("a[download]").count(), baseline ? 0 : ready.files.length); await report.screenshot({path: resolve(output, `${baseline ? "before" : "after"}-files-reader.png`), animations: "disabled"}); @@ -121,10 +122,56 @@ async function checkReturnedFiles(ready, page, errors, writes) { const legacy = new URL(report.url()); legacy.searchParams.delete("reportTurnId"); - const legacyRead = report.waitForRequest(request => new URL(request.url()).pathname === historyPath); + const savedAnswerRequest = url => url.pathname === historyPath && url.searchParams.get("report_message_id") === ready.fileMessageId; + const legacyRead = report.waitForResponse(response => new URL(response.url()).pathname === historyPath); await report.goto(legacy.toString()); await report.getByRole("link", {name: "Download file: source.bin", exact: false}).waitFor(); - assert.equal(new URL((await legacyRead).url()).searchParams.has("turn_id"), false); + const legacyResponse = await legacyRead; + assert.ok(savedAnswerRequest(new URL(legacyResponse.url())), "An existing link selects its saved message without loading the transcript"); + const legacySnapshot = await legacyResponse.json(); + assert.deepEqual(legacySnapshot.messages.filter(row => row.role !== "user").map(row => row.message_id), [ready.fileMessageId]); + const legacyAnswer = legacySnapshot.messages.find(row => row.message_id === ready.fileMessageId); + assert.equal(legacyAnswer.text, answer.text); + assert.deepEqual(legacyAnswer.attachments, answer.attachments); + + await report.route(savedAnswerRequest, route => route.fulfill({status: 400, json: {ok: false, error: "invalid history page arguments"}})); + const fallbackRead = report.waitForRequest(request => { + const url = new URL(request.url()); + return url.pathname === historyPath && url.search === ""; + }); + await report.reload(); + await report.getByRole("link", {name: "Download file: source.bin", exact: false}).waitFor(); + await fallbackRead; + + const fallbackTurnRead = report.waitForRequest(request => { + const url = new URL(request.url()); + return url.pathname === historyPath && url.searchParams.get("turn_id") === answer.turn_id && url.searchParams.has("message_limit"); + }); + const withTurn = new URL(legacy); + withTurn.searchParams.set("reportTurnId", answer.turn_id); + await report.goto(withTurn.toString()); + await report.getByRole("link", {name: "Download file: source.bin", exact: false}).waitFor(); + await fallbackTurnRead; + await report.unroute(savedAnswerRequest); + await report.goto(legacy.toString()); + await report.getByRole("link", {name: "Download file: source.bin", exact: false}).waitFor(); + + const refusedReads = []; + const observeRefusal = request => { + if (new URL(request.url()).pathname === historyPath) refusedReads.push(new URL(request.url())); + }; + report.on("request", observeRefusal); + for (const [status, error] of [[403, "Source access was revoked"], [400, "invalid Chat journal record"]]) { + refusedReads.length = 0; + await report.route(savedAnswerRequest, route => route.fulfill({status, json: {ok: false, error}})); + await report.reload(); + await report.getByText("Could not load this answer.").waitFor(); + assert.equal(await report.locator("a[download]").count(), 0); + assert.equal(refusedReads.length, 1); + assert.ok(savedAnswerRequest(refusedReads[0]), "A refused or invalid source must never trigger a wider history read"); + await report.unroute(savedAnswerRequest); + } + report.off("request", observeRefusal); legacy.searchParams.set("reportTurnId", "unrelated-turn"); await report.goto(legacy.toString()); diff --git a/apps/presentation/dashboard/src/data/chat.ts b/apps/presentation/dashboard/src/data/chat.ts index ed9ebf8896..51394abb28 100644 --- a/apps/presentation/dashboard/src/data/chat.ts +++ b/apps/presentation/dashboard/src/data/chat.ts @@ -972,6 +972,26 @@ export async function fetchChatSession(sessionId: string, signal?: AbortSignal) return requestJson(`/api/chat/sessions/${encodeURIComponent(sessionId)}`, { signal }); } +export async function fetchSavedChatAnswer(sessionId: string, messageId: string, signal: AbortSignal, turnId?: string) { + const query = new URLSearchParams({report_message_id: messageId}); + if (turnId !== undefined) query.set("turn_id", turnId); + const path = `/api/chat/sessions/${encodeURIComponent(sessionId)}?${query}`; + try { + const snapshot = await requestJson(path, {signal}); + if (snapshot.report_message_id !== messageId) throw new Error("The saved answer selection changed during read."); + return snapshot; + } catch (error) { + // Older Chat builds reject unknown history selectors before reading the + // Session. Only that response enables the earlier history read paths. + if (!(error instanceof ChatApiError) || error.payload.http_status !== 400 + || error.message !== "invalid history page arguments") throw error; + signal.throwIfAborted(); + return turnId === undefined + ? fetchChatSession(sessionId, signal) + : fetchChatSessionHistory(sessionId, signal, turnId); + } +} + export const chatQueueInputRequestSchema = z.discriminatedUnion("action", [ z.object({action: z.literal("edit"), operation_id: z.string().min(1).max(160), expected_revision: z.number().int().nonnegative(), message: z.string().min(1).max(24000)}).strict(), diff --git a/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx b/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx index 5dbac41da2..9c43f894a8 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx @@ -1,6 +1,6 @@ import {useEffect, useMemo, useRef, useState} from "react"; -import {fetchChatSession, fetchChatSessionHistory, type ChatSessionSnapshot, type ChatVisibleMessage} from "../../data/chat"; +import {fetchSavedChatAnswer, type ChatSessionSnapshot, type ChatVisibleMessage} from "../../data/chat"; import {visibleAgentMessage} from "./answer-text"; import {readWorkspaceLocale} from "./i18n"; import {resolveConversationScope} from "../../../../../../loopx/control_plane/collaboration/conversation_scope"; @@ -45,9 +45,7 @@ export function AnswerReportPage({sessionId, messageId, statusUrl}: { setLoaded({scope, value: {kind: "loading"}}); setCopied(false); setCopyFailed(false); - const read = turnId === null - ? fetchChatSession(sessionId, controller.signal) - : fetchChatSessionHistory(sessionId, controller.signal, turnId); + const read = fetchSavedChatAnswer(sessionId, messageId, controller.signal, turnId ?? undefined); void read.then((snapshot) => { if (!live) return; if (snapshot.ok !== true || snapshot.schema_version !== "loopx_chat_store_v1" diff --git a/loopx/capabilities/manager_context/roundtrip.py b/loopx/capabilities/manager_context/roundtrip.py index 3ce56200f9..8359b41340 100644 --- a/loopx/capabilities/manager_context/roundtrip.py +++ b/loopx/capabilities/manager_context/roundtrip.py @@ -427,24 +427,46 @@ def observe_result(result_path, reply): ] +def _saved_answer_rows(messages, message_id, expected_turn_id): + matches = [row for row in messages if row.get("message_id") == message_id] + if len(matches) > 1: + raise ValueError("saved answer has conflicting message identities") + if not matches or matches[0].get("role") not in {"agent", "assistant"}: + return [], [] + answer = matches[0] + turn_id = answer.get("turn_id") + if expected_turn_id is not None and turn_id != expected_turn_id: + return [], [] + if not turn_id: + return [answer], [answer] + original = [row for row in messages if row.get("turn_id") == turn_id] + question = next((row for row in original if row.get("role") == "user"), None) + return ([question, answer] if question else [answer]), original + + def project_chat_session_snapshot(root, store, session_id, *, registry, - message_limit=None, message_cursor=None, turn_id=None, + message_limit=None, message_cursor=None, turn_id=None, report_message_id=None, resume_from=None, project_history_messages=None, include_turn_error=False): """Project return delivery state into one existing Chat snapshot.""" turn = None - if turn_id is not None: + if turn_id is not None and report_message_id is None: if message_limit is None: raise ValueError("a selected original Turn requires paged history") turn = store.load_turn(session_id, turn_id) if not turn or turn.get("session_id") != session_id or turn.get("turn_id") != turn_id: raise ValueError("original Turn is unavailable from this Session") - strict = message_limit is not None or resume_from is not None + strict = message_limit is not None or resume_from is not None or report_message_id is not None # Read the status first: terminal completion persists its answer before # publishing completed. An older message prefix must not appear final. snapshot = (store.session_snapshot(session_id, strict_messages=True) if strict else store.session_snapshot(session_id)) - if turn is not None: + report_context = None + if report_message_id is not None: + snapshot["messages"], report_context = _saved_answer_rows(snapshot["messages"], report_message_id, turn_id) + snapshot["report_message_id"] = report_message_id + snapshot["active_turn"] = None + elif turn is not None: status = turn["status"] snapshot["selected_turn"] = {"turn_id": turn_id, "status": status, "terminal": status in TERMINAL_TURN_STATES} # Failure records persist the reason with the status, before appending the message. @@ -464,7 +486,7 @@ def project_chat_session_snapshot(root, store, session_id, *, registry, from .chat_history import page_chat_messages page = (page_chat_messages(session_id, snapshot["messages"], limit=message_limit, cursor=message_cursor, turn_id=turn_id) if message_limit is not None else None) - context_messages = snapshot["messages"] + context_messages = snapshot["messages"] if report_context is None else report_context if page is not None: context_messages = context_messages[:page["message_page"]["total"]] snapshot["messages"] = page["messages"] @@ -475,8 +497,14 @@ def project_chat_session_snapshot(root, store, session_id, *, registry, # Use the frozen context to choose the carrier, projecting only this page. # A question and answer can straddle pages without duplicating a commitment # or re-reading every off-page Turn for each page. - snapshot["messages"] = project_collaboration(store, root, session_id, snapshot["messages"], + projected = project_collaboration(store, root, session_id, snapshot["messages"], registry=registry, context_messages=context_messages) + if report_message_id is not None: + # Collaboration may include the original receipt carrier. The saved + # answer only exposes its precise answer and the question it displays. + visible_ids = {row.get("message_id") for row in snapshot["messages"]} + projected = [row for row in projected if row.get("message_id") in visible_ids] + snapshot["messages"] = projected # Provider-owned history joins the selected page at the composition root. # Core capabilities never import an extension projection sink. if page is not None: @@ -487,9 +515,13 @@ def project_chat_session_snapshot(root, store, session_id, *, registry, def parse_chat_history_selection(query): """Validate one HTTP history selection before any recovery observation.""" - if (set(query) - {"message_limit", "message_cursor", "turn_id", "known_history", "task_goal_id", "task_todo_id"} + keys = set(query) + report = "report_message_id" in keys + if (keys - {"message_limit", "message_cursor", "turn_id", "known_history", + "task_goal_id", "task_todo_id", "report_message_id"} or any(len(values) != 1 or not values[0] for values in query.values()) - or ("message_limit" not in query and set(query) & {"message_cursor", "turn_id", "known_history"})): + or (report and keys - {"report_message_id", "turn_id"}) + or (not report and "message_limit" not in keys and keys & {"message_cursor", "turn_id", "known_history"})): raise ValueError("invalid history page arguments") limit = int(query["message_limit"][0]) if "message_limit" in query else None if limit is not None and not 1 <= limit <= 200: @@ -512,7 +544,8 @@ def parse_chat_history_selection(query): raise ValueError("invalid known history proof") parsed_basis = (basis, count, proof) return {"message_limit": limit, "message_cursor": query.get("message_cursor", [None])[0], - "turn_id": query.get("turn_id", [None])[0], "known_history": parsed_basis, "task_scope": task_scope} + "turn_id": query.get("turn_id", [None])[0], "known_history": parsed_basis, "task_scope": task_scope, + "report_message_id": query.get("report_message_id", [None])[0]} def prepare_chat_history_session(store, session_id, selection, *, recover_turn): @@ -520,7 +553,7 @@ def prepare_chat_history_session(store, session_id, selection, *, recover_turn): task_scope = selection["task_scope"] turn_id = selection["turn_id"] if task_scope is None: - if turn_id is None: + if turn_id is None and selection["report_message_id"] is None: recover_turn(session_id) return store.load_session(session_id) or {} @@ -553,6 +586,7 @@ def read_chat_history_selection(root, store, session_id, *, registry, selection, snapshot = project_chat_session_snapshot(root, store, session_id, registry=registry, message_limit=selection["message_limit"], message_cursor=selection["message_cursor"], turn_id=selection["turn_id"], project_history_messages=project_history_messages, + report_message_id=selection["report_message_id"], include_turn_error=selection["task_scope"] is not None) snapshot["messages"] = project_provider(snapshot["messages"]) if selection["message_limit"] == 200 and selection["turn_id"] is None: diff --git a/loopx/chat_server.py b/loopx/chat_server.py index ab9a4425ea..65337c4b63 100644 --- a/loopx/chat_server.py +++ b/loopx/chat_server.py @@ -936,7 +936,8 @@ def _session_snapshot(self, session_id: str) -> None: media = self.server.private_media_history contexts = getattr(self.server.runtime_controller, "project_contexts", None) bindings = getattr(contexts, "conversation_bindings", None) - markers = [] if selection["turn_id"] is not None else media.read(store, bindings, session) + scoped = selection["turn_id"] is not None or selection["report_message_id"] is not None + markers = [] if scoped else media.read(store, bindings, session) payload = read_chat_history_selection( self.server.runtime_controller.coordination_runtime_root, store, session_id, registry=self.server.registry_path, selection=selection, diff --git a/tests/test_manager_context_roundtrip.py b/tests/test_manager_context_roundtrip.py index 2a396f4c1c..60ab6e7d72 100644 --- a/tests/test_manager_context_roundtrip.py +++ b/tests/test_manager_context_roundtrip.py @@ -136,6 +136,34 @@ def test_frozen_private_reply_projects_original_text_without_mutating_model_inpu assert {str(p): p.read_bytes() for p in store.root.rglob("*") if p.is_file()} == before +def test_saved_answer_reads_only_its_original_question_and_exact_answer(frozen_private_reply, private_snapshot): + store, session, turn, _, _, _, source_text, _ = frozen_private_reply + sid, tid = session["session_id"], turn["turn_id"] + answer = store.append_message(sid, role="agent", text="The saved answer", turn_id=tid) + store.append_message(sid, role="agent", text="Later answer from the same Turn", turn_id=tid) + no_turn = store.append_message(sid, role="agent", text="An older saved answer without a Turn") + other_session = store.create_session(goal_id="notes", agent_id="codex", adapter_kind="codex_app_server", + upstream_thread_id="another-conversation") + foreign = store.append_message(other_session["session_id"], role="agent", text="An answer from another Session") + journal = store.sessions_root / sid / "messages.jsonl" + before = journal.read_bytes() + + response = private_snapshot(report_message_id=answer["message_id"]) + assert response["session"]["session_id"] == sid + assert response["report_message_id"] == answer["message_id"] + assert response["active_turn"] is None + question, saved = response["messages"] + assert question["reply_context"]["source_text"] == source_text + assert saved == answer + assert private_snapshot(report_message_id=answer["message_id"], turn_id=tid) == response + assert private_snapshot(report_message_id=answer["message_id"], turn_id="unrelated-turn")["messages"] == [] + assert private_snapshot(report_message_id=no_turn["message_id"])["messages"] == [no_turn] + for unavailable in (foreign["message_id"], question["message_id"]): + assert private_snapshot(report_message_id=unavailable)["messages"] == [] + private_snapshot(report_message_id=answer["message_id"], message_limit=200, expected_status=400) + assert journal.read_bytes() == before + + def test_history_pages_reuse_unchanged_journal_but_reread_provider_receipts( frozen_private_reply, private_snapshot, monkeypatch): store, session, turn, request, *_ = frozen_private_reply From 4ab155851a8f94a7c5f7299c45c3b16050394b71 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:04:35 +0800 Subject: [PATCH 15/34] fix(chat): preserve complete legacy saved-answer snapshots Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../conversation-reply-browser-smoke.mjs | 27 +++++++++++++++++++ apps/presentation/dashboard/src/data/chat.ts | 8 +++++- .../personal-workspace/answer-report-page.tsx | 3 ++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs b/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs index 7c35dd485f..67bf327169 100644 --- a/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs +++ b/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs @@ -156,6 +156,33 @@ async function checkReturnedFiles(ready, page, errors, writes) { await report.goto(legacy.toString()); await report.getByRole("link", {name: "Download file: source.bin", exact: false}).waitFor(); + // Earlier history readers accept unknown query arguments and return their + // complete v1 snapshot. Both existing and Turn-pinned links still open it. + await report.route(savedAnswerRequest, route => route.fulfill({json: snapshot})); + for (const address of [legacy, withTurn]) { + await report.goto(address.toString()); + await report.getByRole("link", {name: "Download file: source.bin", exact: false}).waitFor(); + assert.equal(await report.locator("a[download]").count(), ready.files.length); + } + const wrongTurn = new URL(withTurn); + wrongTurn.searchParams.set("reportTurnId", "unrelated-turn"); + await report.goto(wrongTurn.toString()); + await report.getByText("This answer was not found. It may have been removed, or the link may be incorrect.").waitFor(); + assert.equal(await report.locator("a[download]").count(), 0); + await report.unroute(savedAnswerRequest); + + for (const invalid of [ + {...snapshot, session: {...snapshot.session, session_id: "another-session"}}, + {...snapshot, report_message_id: "another-message"}, + {...snapshot, message_page: {next_cursor: "another-page"}}, + ]) { + await report.route(savedAnswerRequest, route => route.fulfill({json: invalid})); + await report.goto(legacy.toString()); + await report.getByText("Could not load this answer.").waitFor(); + assert.equal(await report.locator("a[download]").count(), 0); + await report.unroute(savedAnswerRequest); + } + const refusedReads = []; const observeRefusal = request => { if (new URL(request.url()).pathname === historyPath) refusedReads.push(new URL(request.url())); diff --git a/apps/presentation/dashboard/src/data/chat.ts b/apps/presentation/dashboard/src/data/chat.ts index 51394abb28..c0fdf5234c 100644 --- a/apps/presentation/dashboard/src/data/chat.ts +++ b/apps/presentation/dashboard/src/data/chat.ts @@ -978,7 +978,13 @@ export async function fetchSavedChatAnswer(sessionId: string, messageId: string, const path = `/api/chat/sessions/${encodeURIComponent(sessionId)}?${query}`; try { const snapshot = await requestJson(path, {signal}); - if (snapshot.report_message_id !== messageId) throw new Error("The saved answer selection changed during read."); + // A legacy service can ignore the selector and return its complete v1 + // snapshot. A conflicting marker or a partial history remains invalid. + if (Object.hasOwn(snapshot, "report_message_id")) { + if (snapshot.report_message_id !== messageId) throw new Error("The saved answer selection changed during read."); + } else if ("message_page" in snapshot || "history_resume" in snapshot) { + throw new Error("The saved answer history is incomplete."); + } return snapshot; } catch (error) { // Older Chat builds reject unknown history selectors before reading the diff --git a/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx b/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx index 9c43f894a8..167c0a129c 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/answer-report-page.tsx @@ -51,7 +51,8 @@ export function AnswerReportPage({sessionId, messageId, statusUrl}: { if (snapshot.ok !== true || snapshot.schema_version !== "loopx_chat_store_v1" || snapshot.session.session_id !== sessionId) throw new Error("The saved Session changed during read."); const message = snapshot.messages.find((item) => item.message_id === messageId - && ["agent", "assistant"].includes(item.role)); + && ["agent", "assistant"].includes(item.role) + && (turnId === null || item.turn_id === turnId)); setLoaded({scope, value: message ? {kind: "ready", snapshot, message} : {kind: "missing", snapshot}}); }).catch(() => { if (live) setLoaded({scope, value: {kind: "error"}}); }); return () => {live = false; controller.abort(); copyAttempt.current += 1; document.title = previousTitle;}; From 02cc3381d32ca3763a60ca18f16e2ff0d1357299 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:48:26 +0800 Subject: [PATCH 16/34] fix(manager): preserve global todos across scoped health failures (#8865) Keep healthy Goal work visible after a typed, registered Goal status failure. Preserve incomplete coverage and source warnings; global and unverifiable failures remain unavailable. Quota remains the execution-eligibility owner. Exact head bb59d1827bf13ac4bf5bec6ae0f2232a96e98dc9 has a published independent Agent approval and clear review closeout. Source checks, real File/SQLite CLI comparisons and packaged validation establish the changed invariant. The final standard premerge passed six direct and five selected checks. The two historical fixed-archive failures also occur on the immutable base. Hosted CI was not consulted under the current Goal policy; installed-host adoption and large-registry latency remain separate qualifications. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/global_todos.py | 84 ++++++++++++++++--- .../test_unmigrated_goal_reads.py | 62 ++++++++++++++ tests/test_global_todos.py | 32 +++++++ 3 files changed, 168 insertions(+), 10 deletions(-) diff --git a/loopx/global_todos.py b/loopx/global_todos.py index 71103af9b1..11aa3d97f7 100644 --- a/loopx/global_todos.py +++ b/loopx/global_todos.py @@ -4,6 +4,10 @@ from pathlib import Path from typing import Any +from .control_plane.goals.contract_health import ( + CONTRACT_ERROR_DIAGNOSTIC_SCHEMA_VERSION, + contract_error_views, +) from .control_plane.projection_envelope_facts import ( render_projection_envelope_markdown, seal_projection_envelope, @@ -392,10 +396,50 @@ def _deduplicate_todos( return list(deduplicated.values()) +def _scoped_status_failure( + status_payload: dict[str, Any], +) -> tuple[set[str], set[str]] | None: + registry = as_dict(status_payload.get("global_registry")) + contract = as_dict(status_payload.get("contract")) + diagnostics = contract.get("error_diagnostics") + if ( + registry.get("available") is not True + or registry.get("ok") is not True + or contract.get("ok") is not False + or contract.get("global_errors") + or not isinstance(diagnostics, list) + or not diagnostics + or not all( + isinstance(row, dict) + and row.get("schema_version") == CONTRACT_ERROR_DIAGNOSTIC_SCHEMA_VERSION + for row in diagnostics + ) + ): + return None + views = contract_error_views(diagnostics) + if ( + views["global_errors"] + or views["errors"] != contract.get("errors") + or len(views["error_diagnostics"]) != len(diagnostics) + ): + return None + failed_goals = set(views["goal_errors"]) + registered_goals = { + row["id"] + for row in as_list(as_dict(status_payload.get("run_history")).get("goals")) + if isinstance(row, dict) and row.get("registry_member") is True + and isinstance(row.get("id"), str) and row["id"] + } + if not failed_goals or not failed_goals <= registered_goals: + return None + return registered_goals, failed_goals + + def _queue_goal_ids( status_payload: dict[str, Any], *, scan_limit: int, + eligible_goals: set[str] | None = None, ) -> tuple[list[str], int]: queue = as_dict(status_payload.get("attention_queue")) unique_goal_ids: list[str] = [] @@ -412,6 +456,12 @@ def _queue_goal_ids( reported_count = max(0, int(queue.get("item_count") or 0)) except (TypeError, ValueError): reported_count = 0 + # A degraded status queue can include a synthetic contract warning in + # place of the failed Goal. Only the observed registry can name read scopes. + if eligible_goals is not None: + eligible = [goal_id for goal_id in unique_goal_ids if goal_id in eligible_goals] + reported_count -= len(unique_goal_ids) - len(eligible) + unique_goal_ids = eligible available_count = max(len(unique_goal_ids), reported_count) return unique_goal_ids[:scan_limit], available_count @@ -426,6 +476,7 @@ def _todos_envelope( goal_scan_omitted: int, shown_count: int, available_count: int, + failed_status_goals: set[str] | None = None, ) -> dict[str, Any]: global_registry = as_dict(status_payload.get("global_registry")) status_envelope = as_dict(status_payload.get("projection_envelope")) @@ -467,7 +518,13 @@ def _todos_envelope( omitted.append( {"reason": "attention_goals_not_scanned", "count": goal_scan_omitted} ) - if status_payload and status_payload.get("ok") is not True: + if failed_status_goals: + omitted.append({ + "reason": "status_unavailable", + "count": len(failed_status_goals), + "refs": [_redact_text(goal_id, limit=120) for goal_id in sorted(failed_status_goals)[:8]], + }) + elif status_payload and status_payload.get("ok") is not True: omitted.append({"reason": "status_unavailable", "count": 1}) queue = status_payload.get("attention_queue") queue_read = isinstance(queue, dict) and isinstance(queue.get("items"), list) @@ -516,7 +573,7 @@ def _todos_envelope( "scope": "global", "expected_count": expected, "included_count": ( - max(0, expected - excluded - goal_scan_omitted) + max(0, expected - excluded - goal_scan_omitted - len(failed_status_goals or ())) if expected is not None else 0 ), @@ -554,21 +611,27 @@ def build_global_todos( limit=scan_limit, ) status_read_at = now_utc_iso() - if status_payload.get("ok") is not True: + status_ok = status_payload.get("ok") is True + scoped_failure = None if status_ok else _scoped_status_failure(status_payload) + registered_goals, failed_status_goals = scoped_failure or (set(), set()) + goal_ids, available_goal_count = _queue_goal_ids( + status_payload, + scan_limit=scan_limit, + eligible_goals=registered_goals - failed_status_goals if scoped_failure else None, + ) + if not status_ok and (scoped_failure is None or not goal_ids): return build_global_todos_error( "Global status source unavailable.", status_payload=status_payload, status_read_at=status_read_at, ) - - goal_ids, available_goal_count = _queue_goal_ids( - status_payload, - scan_limit=scan_limit, - ) - warnings: list[dict[str, Any]] = [] + warnings = [ + _warning("quota_unavailable", goal_id=goal_id, detail="The Goal's status contract check failed.") + for goal_id in sorted(failed_status_goals) + ] classified: list[dict[str, Any]] = [] quota_read_at = now_utc_iso() - quota_unavailable = 0 + quota_unavailable = len(failed_status_goals) for goal_id in goal_ids: quota_payloads, was_unavailable = _quotas_for_goal( status_payload, @@ -655,6 +718,7 @@ def build_global_todos( goal_scan_omitted=max(0, available_goal_count - len(goal_ids)), shown_count=returned_count, available_count=matched_count, + failed_status_goals=failed_status_goals, ), "omissions": [ ( diff --git a/tests/control_plane/test_unmigrated_goal_reads.py b/tests/control_plane/test_unmigrated_goal_reads.py index f7720c3b5f..6d4d5a52b0 100644 --- a/tests/control_plane/test_unmigrated_goal_reads.py +++ b/tests/control_plane/test_unmigrated_goal_reads.py @@ -11,6 +11,8 @@ from loopx.control_plane.coordination.legacy_writer_fence import ( legacy_coordination_writer_fence_path, ) +from loopx.control_plane.testing.canary_harness import run_json_cli_result +from canonical_authority_fixture import initialize_canonical_source_fixture, isolate_sqlite_runtime from test_cold_source_import_cli import canonical_todos, workspace @@ -66,6 +68,66 @@ def test_ordinary_cli_refuses_unmigrated_goal_without_importing_it( assert not (runtime / "authority").exists() +def test_global_todos_retains_healthy_lanes_with_a_stopped_unmigrated_goal(tmp_path, monkeypatch): + isolate_sqlite_runtime(tmp_path, monkeypatch) + runtime = tmp_path / "runtime" + runtime.mkdir() + registry = runtime / "registry.global.json" + goals = [] + state_by_goal = {} + for goal_id in ("ready", "stopped"): + project = tmp_path / goal_id + project.mkdir() + state = project / "ACTIVE_GOAL_STATE.md" + state.write_text( + "---\nstatus: active\nowner_mode: goal\n---\n\n" + "## Agent Todo\n\n- [ ] Verify the registered work.\n" + f" \n" + ) + goal = { + "id": goal_id, "repo": str(project), "state_file": state.name, "status": "active", + "domain": "software", "adapter": {"kind": "read_only_project_map_v0", "status": "connected-read-only"}, + "coordination": {"registered_agents": ["agent-a"]}, + } + if goal_id == "stopped": + goal["activation"] = { + "schema_version": "loopx_goal_activation_v1", "state": "stopped", + "updated_at": "2026-01-01T00:00:00Z", "actor_kind": "owner", "reason": "Stop this fixture.", + } + else: + initialize_canonical_source_fixture(runtime, goal_id, state_path=state, provider="sqlite") + goals.append(goal) + state_by_goal[goal_id] = state + registry.write_text(json.dumps({ + "schema_version": "0.1", "registry_role": "global-local", + "common_runtime_root": str(runtime), "goals": goals, + })) + original = registry.read_bytes(), state_by_goal["stopped"].read_bytes() + + code, status = run_json_cli_result("status", registry_path=registry) + assert code == 1 and status["ok"] is False + assert [(e["goal_id"], e["code"]) for e in status["contract"]["error_diagnostics"]] == [ + ("stopped", "canonical_authority_required"), + ] + assert any(row["goal_id"] == "loopx-contract" for row in status["attention_queue"]["items"]) + + code, observed = run_json_cli_result("global-todos", registry_path=registry) + assert code == 0 and observed["ok"] is True, observed + assert {(todo["goal_id"], todo["todo_id"]) for todo in observed["todos"]} == {("ready", "todo_ready")} + assert [(row["goal_id"], row["reason_code"]) for row in observed["source_warnings"]] == [ + ("stopped", "quota_unavailable"), + ] + envelope = observed["projection_envelope"] + assert envelope["complete"] is False + assert envelope["coverage"]["expected_count"] == 2 + assert envelope["coverage"]["included_count"] == 1 + quota = next(row for row in envelope["sources"] if row["source_id"] == "goal_quota") + assert (quota["read_status"], quota["item_count"], quota["unreadable_count"]) == ("read", 1, 1) + assert (registry.read_bytes(), state_by_goal["stopped"].read_bytes()) == original + assert not legacy_coordination_writer_fence_path(runtime_root=runtime, goal_id="stopped").exists() + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) def test_explicit_import_retains_full_source_and_enables_ordinary_reads( tmp_path, monkeypatch, provider, diff --git a/tests/test_global_todos.py b/tests/test_global_todos.py index 1f2b578a63..a6d3c6bb6a 100644 --- a/tests/test_global_todos.py +++ b/tests/test_global_todos.py @@ -1,7 +1,13 @@ from pathlib import Path from typing import Any +import pytest + import loopx.global_todos as global_todos +from loopx.control_plane.goals.contract_health import ( + contract_error_diagnostic, + contract_error_views, +) def build_payload( @@ -137,6 +143,32 @@ def test_global_todos_error_redacts_local_paths(tmp_path) -> None: assert "" in payload["error"] +@pytest.mark.parametrize("failure", ["global_contract", "unknown_goal", "registry"]) +def test_global_todos_keeps_unverified_scope_unavailable(monkeypatch, tmp_path, failure) -> None: + diagnostic = contract_error_diagnostic(code="canonical_authority_required", + message="Unavailable canonical source.", goal_id="unknown" if failure == "unknown_goal" else "cold") + diagnostics = [diagnostic] + if failure == "global_contract": + diagnostics.append(contract_error_diagnostic(code="registry_contract", message="Global contract failed.")) + status = { + **status_for("ready"), + "ok": False, + "global_registry": {"available": True, "ok": failure != "registry"}, + "contract": {"ok": False, **contract_error_views(diagnostics)}, + "run_history": {"goals": [ + {"id": "ready", "registry_member": True}, + {"id": "cold", "registry_member": True}, + ]}, + } + calls = patch_manager_reads(monkeypatch, status=status, quotas={}) + + payload = build_payload(tmp_path) + + assert payload["ok"] is False + assert "todos" not in payload + assert calls == {"status": 1, "quota": []} + + def test_global_todos_error_survives_projection_runtime_failure(monkeypatch) -> None: monkeypatch.setattr( global_todos, From d0ca48db02846be61a9b683f0efcd5c706381b66 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:47:10 +0800 Subject: [PATCH 17/34] fix(quota): verify retained debit identity before replay (#8860) Reject mismatched retained debit identity before replay or receipt repair. The shared TypeScript reader checks persisted bindings and billing terms for modern, legacy file and inline evidence. Both existing CLI entrypoints retain legitimate historical omissions and repair a missing receipt from the original debit without charging twice. Exact reviewed head: 75d0ccdf43bd015646359bab41f4a8f79a878cc9. Independent exact-head approval: review 5920595494. Approval closeout is clear. Validation: 267 CLI cases passed (10 platform skips), 49 TypeScript spend/void cases, control-plane typecheck and standard premerge (6 direct + 18 selected) passed. Independent review additionally passed 45 runtime transaction cases and 20 isolated wheel cases covering same-second, nested and inline contradictions with unchanged retained bytes. No intervening target-main changes affect the five reviewed paths. Required bilingual diff and hygiene checks pass at the unchanged head. The configured workflow skips hosted CI waiting. The owner authorized related reviewed fixes to merge; any admin bypass applies only to GitHub's author-approval/branch policy. Native Windows, real PostgreSQL, installed-host adoption and complete C1 cost/benefit qualification remain separate. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../quota/accounting_artifact_transaction.ts | 45 +++- .../quota/settlement_readback.ts | 113 ++++++--- loopx/quota.py | 3 + .../control_plane/test_refresh_settle_turn.py | 223 ++++++++++++++++++ .../quota_void_commit.test.ts | 75 ++++++ 5 files changed, 428 insertions(+), 31 deletions(-) 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/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/quota.py b/loopx/quota.py index 4d774c4980..0c8bbfbfb6 100644 --- a/loopx/quota.py +++ b/loopx/quota.py @@ -1837,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/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_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; From 812241c0126f5c28929a83c18a12006fd0126c97 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:33:19 +0800 Subject: [PATCH 18/34] test(quota): exercise routed hard-lease settlement and replay (#8852) Follow the composed writeback for a hard lease and inspect the runtime-owned quota receipt. Keep the legacy split route under test and prove that both settle the original Turn once through the selected CLI. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- tests/control_plane/test_cli_program_route.py | 56 ++++++++++++++++--- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/tests/control_plane/test_cli_program_route.py b/tests/control_plane/test_cli_program_route.py index 3a5adbfb94..e2d0f51f6e 100644 --- a/tests/control_plane/test_cli_program_route.py +++ b/tests/control_plane/test_cli_program_route.py @@ -132,28 +132,66 @@ def run(argv, *, expected_code=0): assert guard["decision"] == "run", guard identity = guard["heartbeat_receipt"]["settlement_identity"] assert identity["todo_id"] == cli.TODO_ID and identity["turn_instance_id"] == cli.TURN_ID - commands = {s["kind"]: s["command_template"] for s in channel["settlement_plan"]["ordered_steps"]} - for command in [*channel["next_cli_actions"], *commands.values()]: + plan = channel["settlement_plan"] + steps = {step["kind"]: step for step in plan["ordered_steps"]} + commands = [step["command_template"] for step in steps.values() if step["owner"] == "agent"] + for command in [*channel["next_cli_actions"], *commands]: assert shlex.split(command)[0] == program for read in guard["interaction_contract"]["agent_channel"].get("required_reads", []): assert shlex.split(read["command"])[0] != "loopx" - refresh = commands["durable_writeback"].replace("", "validated_progress") - refresh = refresh.replace("", "implementation").replace("", "outcome_progress") - refreshed = run([ + refresh = ( + steps["durable_writeback"]["command_template"] + .replace("", "validated_progress") + .replace("", "implementation") + .replace("", "outcome_progress") + ) + refresh_argv = [ *shlex.split(refresh), "--vision-state", "vision_on_track", "--vision-summary", "Continue eligible work.", "--vision-acceptance", "The original runtime settles its selected work once.", "--no-global-sync", "--suppress-external-sinks", - ]) + ] + spend_step = steps["quota_spend"] + if handoff_mode == "hard_lease": + assert plan["schema_version"] == "quota_settlement_plan_v2" + assert spend_step["owner"] == "runtime" + assert "command_template" not in spend_step + assert all("spend-slot" not in shlex.split(action) for action in channel["next_cli_actions"]) + assert {"--settle-turn", "--settlement-receipt"} <= set(refresh_argv) + else: + assert spend_step["owner"] == "agent" + + refreshed = run(refresh_argv) assert refreshed["vision_checkpoint"]["satisfied"] - owed = refreshed["settlement_owed"]["command"] - assert shlex.split(owed)[0] == program - first, replay = run(shlex.split(owed)), run(shlex.split(owed)) + if handoff_mode == "hard_lease": + assert refreshed["settlement_result"]["ok"] + receipts = refreshed["settlement_result"]["receipts"] + assert [receipt["step_kind"] for receipt in receipts] == [ + "validation", "durable_writeback", "quota_spend", + ] + assert all( + receipt["effect_id"] == identity["effect_id"] and receipt["status"] == "committed" + for receipt in receipts + ) + assert refreshed["quota_spend"]["appended"] + first = refreshed + settle_argv = refresh_argv + else: + assert cli._spend_run_count(runtime) == 0 + settle_argv = shlex.split(refreshed["settlement_owed"]["command"]) + assert settle_argv[0] == program + first = run(settle_argv) assert first["settlement_progress"]["state"] == "settled" and first["appended"] + assert cli._spend_run_count(runtime) == 1 + replay = run(settle_argv) assert replay["idempotent_replay"] and not replay["appended"] assert first["settlement_identity"] == identity + if handoff_mode == "hard_lease": + assert replay["settlement_identity"] == identity + assert replay["settlement_result"] == first["settlement_result"] assert cli._spend_run_count(runtime) == 1 + assert cli._classification_count(runtime, "validated_progress") == 1 if lease: released = run([ *prefix, "task-lease", "release", "--goal-id", cli.GOAL_ID, From eb81f44a191d3e8b0ed20511df614e21539d8007 Mon Sep 17 00:00:00 2001 From: huangruiteng Date: Mon, 16 Nov 2026 23:33:34 +0800 Subject: [PATCH 19/34] fix(quota): type the heterogeneous interaction CLI channel (#8869) Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Co-authored-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/control_plane/work_items/interaction_contract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loopx/control_plane/work_items/interaction_contract.py b/loopx/control_plane/work_items/interaction_contract.py index 5bcaf1e388..149d105516 100644 --- a/loopx/control_plane/work_items/interaction_contract.py +++ b/loopx/control_plane/work_items/interaction_contract.py @@ -1407,7 +1407,7 @@ def _build_interaction_cli_channel( registry_path=registry_path, ) ) - channel = { + channel: dict[str, Any] = { "next_cli_actions": _admitted_interaction_next_cli_actions( payload, mode=mode, From 3f45839544769d9ec494e3141c0fc4e5aa276dc6 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:39:59 +0800 Subject: [PATCH 20/34] Verify pending replan checkpoints in the artifact owner (#8797) Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- benchmark/runtime/RUNTIME.md | 11 + benchmark/runtime/checkpoint_continuation.py | 28 +-- .../runtime/checkpoint_session_refresh.py | 26 +-- .../runtime/checkpoint_artifacts.ts | 215 +++++++++++++----- .../test_checkpoint_restore_capture.py | 169 +++++++++++++- 5 files changed, 339 insertions(+), 110 deletions(-) diff --git a/benchmark/runtime/RUNTIME.md b/benchmark/runtime/RUNTIME.md index 91ae2b658d..60b7c40607 100644 --- a/benchmark/runtime/RUNTIME.md +++ b/benchmark/runtime/RUNTIME.md @@ -176,6 +176,17 @@ a complete-mode binding cannot verify a pending command that uses `--complete-turn`. Historical ordinary and envelope guards remain readable. The existing Darwin native-checkpoint refusal remains in force. +Restoring a pending replan for a fresh native conversation and checking whether +a restored session may capture again use the same checkpoint artifact reader. +It verifies the sealed decision member and original replan binding. Present +complete-source versions are verified; an unversioned decision is readable +only as historical evidence in this sealed path. A refusal holds that recovery +before a session reset or a new capture. A successful read does not replace +current native admission, current semantic settlement, or full archive checking. +Empty historical replan annotations keep the ordinary restore path. A declared +held-decision member still requires its original intent and sealed bytes, even +when an annotation is empty. + The host-side checkpoint preflight captures the complete original decision. With context delivery enabled, its prompt supplies `quota context-project` with that original path and hash. The shared presentation owner prepares the diff --git a/benchmark/runtime/checkpoint_continuation.py b/benchmark/runtime/checkpoint_continuation.py index 60035e4cc5..251f64de52 100644 --- a/benchmark/runtime/checkpoint_continuation.py +++ b/benchmark/runtime/checkpoint_continuation.py @@ -6,7 +6,6 @@ from __future__ import annotations import hashlib -import json from pathlib import Path from loopx.control_plane.effect_runtime import effect_runtime_result @@ -63,32 +62,13 @@ def _restored_context(env, current_obligation_id=None, *, verify_native, include raise ValueError("restored checkpoint lacks its immutable source; capture remains disabled") unit = Path(source) manifest = effect_runtime_result("runtime.checkpoint_artifacts", { - "action": "read_basis", "root": str(unit), "expected_sha256": sha, + "action": "read_basis", "root": str(unit), "expected_sha256": sha, "include_pending_replan": True, }, large_local_snapshot=True) # Admission already verified all artifact bytes. Here only the immutable # declaration, original pending packet and native history are inputs; do # not rehash large code/incumbent archives on every ordinary work wake. metadata = manifest["point_metadata"] - pending = metadata.get("replan") or metadata.get("operator_metadata", {}).get("replan") - packet = None - if pending: - component = metadata.get("held_decision_component_id") - if component not in {entry["id"] for entry in manifest["components"]}: - raise ValueError("restored replan point lacks its original pending packet; capture remains disabled") - path = unit / "components" / component - if path.resolve(strict=True) != path: - raise ValueError("restored pending packet must be a physical artifact member") - raw = path.read_bytes() - if hashlib.sha256(raw).hexdigest() != metadata.get("decision_sha256"): - raise ValueError("restored pending replan packet digest differs") - packet = json.loads(raw) - intent = effect_runtime_result("runtime.checkpoint_artifacts", { - "action": "select_replan", "quota": packet, - "goal_id": metadata["loopx_state"]["goal_id"], "agent_id": metadata["loopx_state"]["agent_id"], - "turn_instance_id": packet["heartbeat_receipt"]["turn_instance_id"], - }, large_local_snapshot=True) - if intent != pending or intent.get("capture") is not True: - raise ValueError("restored original replan binding differs") + pending = manifest["pending_replan"] result = effect_runtime_result("runtime.checkpoint_artifacts", { "action": "restore_capture", **lineage, "checkpoint_sha256": sha, "current_obligation_id": current_obligation_id, "review": None, @@ -149,8 +129,8 @@ def _restored_context(env, current_obligation_id=None, *, verify_native, include "rebind the old Turn. Capture eligibility requires an accepted, settled current semantic " "replan and a completed destination Codex conversation turn, not a benchmark evaluation; " "it does not grant execution.\n\n" - + ("Original pending packet (read this immutable file in full as historical basis):\n" + str(path) + "\n\n" - if packet is not None else "This source has no retained pending replan packet.\n\n") + + ("Original pending packet (read this immutable file in full as historical basis):\n" + pending["decision_path"] + "\n\n" + if pending is not None else "This source has no retained pending replan packet.\n\n") ) return result diff --git a/benchmark/runtime/checkpoint_session_refresh.py b/benchmark/runtime/checkpoint_session_refresh.py index d64d7cf808..399bd51c21 100644 --- a/benchmark/runtime/checkpoint_session_refresh.py +++ b/benchmark/runtime/checkpoint_session_refresh.py @@ -6,7 +6,6 @@ """ from __future__ import annotations -import hashlib import json from pathlib import Path @@ -49,27 +48,12 @@ def _prepare_fresh_replan(env, session, wake, declaration): unit = Path(declaration["root"]) manifest = effect_runtime_result("runtime.checkpoint_artifacts", { - "action": "read_basis", "root": str(unit), "expected_sha256": declaration["sha256"]}, large_local_snapshot=True) - metadata = manifest["point_metadata"] - component = metadata.get("held_decision_component_id") - if component not in {entry["id"] for entry in manifest["components"]}: - raise ValueError("fresh replan requires a sealed pre-decision packet") - packet_path = unit / "components" / component - if packet_path.resolve(strict=True) != packet_path: - raise ValueError("fresh replan packet must be a physical sealed member") - raw = packet_path.read_bytes() - if hashlib.sha256(raw).hexdigest() != metadata.get("decision_sha256"): - raise ValueError("fresh replan pending packet digest differs") - packet = json.loads(raw) - selected = effect_runtime_result("runtime.checkpoint_artifacts", { - "action": "select_replan", "quota": packet, - "goal_id": metadata["loopx_state"]["goal_id"], - "agent_id": metadata["loopx_state"]["agent_id"], - "turn_instance_id": packet["heartbeat_receipt"]["turn_instance_id"]}, large_local_snapshot=True) - if selected.get("capture") is not True or selected != ( - metadata.get("replan") or metadata.get("operator_metadata", {}).get("replan")): + "action": "read_basis", "root": str(unit), "expected_sha256": declaration["sha256"], + "include_pending_replan": True}, large_local_snapshot=True) + pending = manifest["pending_replan"] + if pending is None: raise ValueError("fresh replan requires the original pending decision boundary") - basis = {"decision_sha256": metadata["decision_sha256"], "pending_replan": selected} + basis = {"decision_sha256": pending["decision_sha256"], "pending_replan": pending["intent"]} attempt = Path(env["LOOPX_WARM_START"]).parent / "context-refresh" / "pre-replan.json" # A lost launch reply is recovered only by the session owner's original # output. Re-admitting an advanced fork would hide that outcome. diff --git a/loopx/control_plane/runtime/checkpoint_artifacts.ts b/loopx/control_plane/runtime/checkpoint_artifacts.ts index afc6bffd56..d2ea3977ba 100644 --- a/loopx/control_plane/runtime/checkpoint_artifacts.ts +++ b/loopx/control_plane/runtime/checkpoint_artifacts.ts @@ -3,11 +3,11 @@ * Native sessions, canonical state adoption and evaluator opportunities retain owners. */ import {createHash} from "node:crypto"; -import {createReadStream} from "node:fs"; +import {constants, createReadStream} from "node:fs"; import {chmod, copyFile, lstat, mkdir, mkdtemp, open, readFile, readdir, rm} from "node:fs/promises"; import {dirname, isAbsolute, join, resolve, sep} from "node:path"; import type {JsonObject} from "../effect_program.ts"; -import {jsonObject, requireJsonObject, requireNonEmptyString} from "../runtime_decode.ts"; +import {jsonObject, parseJsonSource, parsedSourceNumberTokens, requireJsonObject, requireNonEmptyString} from "../runtime_decode.ts"; import {canonicalAuthoritySha256, hasExactAuthorityKeys} from "../coordination/authority_store_codec.ts"; import {snapshotSqliteDatabase} from "../coordination/sqlite_backup.ts"; import {durableWriteJson, publishNewDirectory} from "../effect_runtime_io.ts"; @@ -15,6 +15,7 @@ import {settlementIdentity} from "../effect_program.ts"; import {normalizeHeartbeatReplanObligationId, normalizeHeartbeatTodoId} from "../quota/heartbeat_receipt_identity.ts"; import {EffectRuntimeConflictError, EffectRuntimeRequestError} from "../effect_runtime_errors.ts"; import {EffectiveAction} from "../quota/effective_action.generated.ts"; +import {MAX_LOCAL_SNAPSHOT_BYTES} from "../effect_runtime_snapshot.ts"; import {BARE_SHA256_PATTERN} from "../content_digest.ts"; import {checkpointRetentionPlan} from "./checkpoint_retention.ts"; @@ -249,6 +250,144 @@ async function verify(root: string, expected: unknown): Promise { // guard, a no-write reread, or the selection hop that appended its binding. // A pending/failed/uncommitted view is not an admitted identity. const ADMITTED_RECEIPT_STATUSES: ReadonlySet = new Set(["committed", "replayed", "upgraded"]); +function checkpointReplanIntent(quota: JsonObject, goal: string, agent: string, turn: string): JsonObject { + // A refused handoff should retain its causal mismatch diagnosis. + const mismatch = () => new EffectRuntimeRequestError("checkpoint replan does not match the current admitted identity"); + if (quota.ok !== true || quota.goal_id !== goal) throw new EffectRuntimeRequestError("checkpoint requires a matching current quota readback"); + if (quota.effective_action !== EffectiveAction.AUTONOMOUS_REPLAN_REQUIRED) { + return {capture: false, reason: "no_fresh_replan", execution_authority_granted: false}; + } + const packet = requireJsonObject(quota.replan_action_packet, "checkpoint replan packet"); + const id = normalizeHeartbeatReplanObligationId(packet.obligation_id); + const receipt = requireJsonObject(quota.heartbeat_receipt, "checkpoint heartbeat receipt"); + // The replan owner marks receipt-owned follow-through with settlement_only. + // These packets carry the original writer's response, not a new research node. + const settlementOnly = packet.settlement_only === true; + if (packet.schema_version !== "replan_action_packet_v0" || + typeof packet.decision !== "string" || packet.decision.trim() === "" || + (!settlementOnly && packet.decision !== "replan_required") || + !id || !ADMITTED_RECEIPT_STATUSES.has(receipt.status as string) || receipt.turn_instance_id !== turn) { + throw mismatch(); + } + const semanticReplan = normalizeHeartbeatReplanObligationId(receipt.semantic_replan_obligation_id); + const closeoutPending = receipt.closeout_required === true; + if (!closeoutPending && !settlementOnly && receipt.settlement_identity === undefined && receipt.settlement_binding_owed === true) { + // The original guard owns the semantic node before the Agent chooses + // work. An inert pre-decision capture does not invent that later work's + // causal settlement identity or grant permission to execute it. + const actor = requireJsonObject(quota.agent_identity, "checkpoint source Agent"); + requireNonEmptyString(receipt.event_id, "checkpoint committed guard event"); + if (actor.agent_id !== agent || actor.registered !== true || semanticReplan !== id) { + throw mismatch(); + } + return {capture: true, obligation_id: id, execution_authority_granted: false}; + } + const identity = requireJsonObject(receipt.settlement_identity, "checkpoint settlement identity"); + const todo = normalizeHeartbeatTodoId(identity.todo_id); + const boundReplan = normalizeHeartbeatReplanObligationId(identity.replan_obligation_id); + if ((todo !== null && boundReplan !== null) || + identity.goal_id !== goal || identity.agent_id !== agent || identity.turn_instance_id !== turn || + identity.effect_id !== settlementIdentity({goal_id: goal, agent_id: agent, todo_id: todo, + replan_obligation_id: boundReplan, turn_instance_id: turn}).effect_id) { + throw mismatch(); + } + // A replay can expose the next obligation while this Turn still owes its + // original Todo settlement. Release that response to its writer; it is not + // a new capture boundary and must not interrupt the pending closeout. + if (closeoutPending) return {capture: false, reason: "turn_closeout_pending", execution_authority_granted: false}; + if (settlementOnly) { + if (semanticReplan !== id || (boundReplan !== null && boundReplan !== id)) { + throw mismatch(); + } + return {capture: false, reason: "turn_closeout_pending", execution_authority_granted: false}; + } + if (todo ? boundReplan !== null || semanticReplan !== id : boundReplan !== id) { + throw mismatch(); + } + return {capture: true, obligation_id: id, settlement_effect_id: identity.effect_id, + execution_authority_granted: false}; +} + +function hasReplanAnnotation(value: unknown): boolean { + // Historical JSON notes used empty values for absence. Keep that selection + // rule here; a declared held member still requires verification below. + if (!value) return false; + return typeof value !== "object" || Object.keys(value).length > 0; +} + +async function readPendingReplanBasis(root: string, manifest: JsonObject): Promise { + const metadata = requireJsonObject(manifest.point_metadata, "checkpoint point metadata"); + const operator = jsonObject(metadata.operator_metadata); + const pending = hasReplanAnnotation(metadata.replan) ? metadata.replan : operator?.replan; + if (!hasReplanAnnotation(pending) && metadata.held_decision_component_id === undefined) return null; + const intent = requireJsonObject(pending, "checkpoint pending replan"); + const id = metadata.held_decision_component_id; + if (typeof id !== "string" || !components(manifest.components).some(entry => entry.id === id)) { + throw new EffectRuntimeRequestError("restored replan point lacks its original pending packet"); + } + const expected = requireNonEmptyString(metadata.decision_sha256, "checkpoint pending packet digest"); + if (!BARE_SHA256_PATTERN.test(expected)) { + throw new EffectRuntimeRequestError("checkpoint pending packet digest is invalid"); + } + const path = join(root, "components", id); + await physical(path); + const entry = await lstat(path); + const witness = requireJsonObject(manifest.artifact_witness, "checkpoint artifact witness")[id]; + const sealed = Array.isArray(witness) && witness.length === 1 ? jsonObject(witness[0]) : null; + if (!entry.isFile() || !sealed || sealed.kind !== "file" || sealed.path !== "") { + throw new EffectRuntimeRequestError("checkpoint pending packet must be a regular sealed member"); + } + if (entry.size <= 0 || entry.size > MAX_LOCAL_SNAPSHOT_BYTES) { + throw new EffectRuntimeRequestError("checkpoint pending packet size is outside the local source budget"); + } + if (sealed.sha256 !== expected || sealed.size !== entry.size) { + throw new EffectRuntimeRequestError("checkpoint pending packet digest differs"); + } + const file = await open(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0)); + let raw: Buffer; + try { + const observed = await file.stat(); + if (!observed.isFile() || observed.dev !== entry.dev || observed.ino !== entry.ino || observed.size !== entry.size) { + throw new EffectRuntimeRequestError("checkpoint pending packet changed while opening"); + } + raw = Buffer.alloc(entry.size); + let offset = 0; + while (offset < raw.length) { + const {bytesRead} = await file.read(raw, offset, raw.length - offset, offset); + if (bytesRead === 0) break; + offset += bytesRead; + } + if (offset !== raw.length || (await file.stat()).size !== raw.length) { + throw new EffectRuntimeRequestError("checkpoint pending packet changed while reading"); + } + } finally { + await file.close(); + } + const actual = createHash("sha256").update(raw).digest("hex"); + if (actual !== expected) { + throw new EffectRuntimeRequestError("checkpoint pending packet digest differs"); + } + let parsed: ReturnType; + try { parsed = parseJsonSource(new TextDecoder("utf-8", {fatal: true}).decode(raw)); } + catch { throw new EffectRuntimeRequestError("checkpoint pending packet is not valid UTF-8 JSON"); } + const quota = requireJsonObject(parsed.value, "checkpoint quota readback"); + // Only this sealed historical reader permits an unversioned decision. A + // complete carrier, including malformed version/binding fields, is checked. + if (Object.hasOwn(quota, "schema_version") || Object.hasOwn(quota, "contract_binding")) { + const {requireCompleteTurnDecision} = await import("../quota/turn_envelope.ts"); + requireCompleteTurnDecision(quota, parsedSourceNumberTokens(quota, parsed.numbers)); + } + const source = requireJsonObject(metadata.loopx_state, "checkpoint source Goal/Agent"); + const receipt = requireJsonObject(quota.heartbeat_receipt, "checkpoint heartbeat receipt"); + const observed = checkpointReplanIntent(quota, requireNonEmptyString(source.goal_id, "checkpoint goal"), + requireNonEmptyString(source.agent_id, "checkpoint agent"), + requireNonEmptyString(receipt.turn_instance_id, "checkpoint Turn")); + if (observed.capture !== true || canonicalAuthoritySha256(observed) !== canonicalAuthoritySha256(intent)) { + throw new EffectRuntimeRequestError("restored original replan binding differs"); + } + return {intent: observed, decision_sha256: actual, decision_path: path}; +} + export async function checkpointArtifacts(value: unknown): Promise { const request = requireJsonObject(value, "checkpoint artifacts request"); if (request.action === "retention_plan") { @@ -341,77 +480,27 @@ export async function checkpointArtifacts(value: unknown): Promise { execution_authority_granted: false}; } if (request.action === "select_replan") { - // Typed refusals keep their reason through the runtime boundary, so a - // refused handoff is diagnosable instead of an opaque internal failure. - const mismatch = () => new EffectRuntimeRequestError("checkpoint replan does not match the current admitted identity"); if (!hasExactAuthorityKeys(request, ["action", "quota", "goal_id", "agent_id", "turn_instance_id"])) { throw new Error("checkpoint replan selection fields are invalid"); } - const quota = requireJsonObject(request.quota, "checkpoint quota readback"); - const goal = requireNonEmptyString(request.goal_id, "checkpoint goal"); - const agent = requireNonEmptyString(request.agent_id, "checkpoint agent"); - const turn = requireNonEmptyString(request.turn_instance_id, "checkpoint Turn"); - if (quota.ok !== true || quota.goal_id !== goal) throw new EffectRuntimeRequestError("checkpoint requires a matching current quota readback"); - if (quota.effective_action !== EffectiveAction.AUTONOMOUS_REPLAN_REQUIRED) { - return {capture: false, reason: "no_fresh_replan", execution_authority_granted: false}; - } - const packet = requireJsonObject(quota.replan_action_packet, "checkpoint replan packet"); - const id = normalizeHeartbeatReplanObligationId(packet.obligation_id); - const receipt = requireJsonObject(quota.heartbeat_receipt, "checkpoint heartbeat receipt"); - // The replan owner marks receipt-owned follow-through with settlement_only. - // These packets carry the original writer's response, not a new research node. - const settlementOnly = packet.settlement_only === true; - if (packet.schema_version !== "replan_action_packet_v0" || - typeof packet.decision !== "string" || packet.decision.trim() === "" || - (!settlementOnly && packet.decision !== "replan_required") || - !id || !ADMITTED_RECEIPT_STATUSES.has(receipt.status as string) || receipt.turn_instance_id !== turn) { - throw mismatch(); - } - const semanticReplan = normalizeHeartbeatReplanObligationId(receipt.semantic_replan_obligation_id); - const closeoutPending = receipt.closeout_required === true; - if (!closeoutPending && !settlementOnly && receipt.settlement_identity === undefined && receipt.settlement_binding_owed === true) { - // The original guard owns the semantic node before the Agent chooses - // work. An inert pre-decision capture does not invent that later work's - // causal settlement identity or grant permission to execute it. - const actor = requireJsonObject(quota.agent_identity, "checkpoint source Agent"); - requireNonEmptyString(receipt.event_id, "checkpoint committed guard event"); - if (actor.agent_id !== agent || actor.registered !== true || semanticReplan !== id) { - throw mismatch(); - } - return {capture: true, obligation_id: id, execution_authority_granted: false}; - } - const identity = requireJsonObject(receipt.settlement_identity, "checkpoint settlement identity"); - const todo = normalizeHeartbeatTodoId(identity.todo_id); - const boundReplan = normalizeHeartbeatReplanObligationId(identity.replan_obligation_id); - if ((todo !== null && boundReplan !== null) || - identity.goal_id !== goal || identity.agent_id !== agent || identity.turn_instance_id !== turn || - identity.effect_id !== settlementIdentity({goal_id: goal, agent_id: agent, todo_id: todo, - replan_obligation_id: boundReplan, turn_instance_id: turn}).effect_id) { - throw mismatch(); - } - // A replay can expose the next obligation while this Turn still owes its - // original Todo settlement. Release that response to its writer; it is not - // a new capture boundary and must not interrupt the pending closeout. - if (closeoutPending) return {capture: false, reason: "turn_closeout_pending", execution_authority_granted: false}; - if (settlementOnly) { - if (semanticReplan !== id || (boundReplan !== null && boundReplan !== id)) { - throw mismatch(); - } - return {capture: false, reason: "turn_closeout_pending", execution_authority_granted: false}; - } - if (todo ? boundReplan !== null || semanticReplan !== id : boundReplan !== id) { - throw mismatch(); - } - return {capture: true, obligation_id: id, settlement_effect_id: identity.effect_id, - execution_authority_granted: false}; + return checkpointReplanIntent(requireJsonObject(request.quota, "checkpoint quota readback"), + requireNonEmptyString(request.goal_id, "checkpoint goal"), + requireNonEmptyString(request.agent_id, "checkpoint agent"), + requireNonEmptyString(request.turn_instance_id, "checkpoint Turn")); } if (request.action === "read_basis") { - if (!hasExactAuthorityKeys(request, ["action", "root", "expected_sha256"])) throw new Error("checkpoint basis fields are invalid"); + const keys = ["action", "root", "expected_sha256"]; + if (Object.hasOwn(request, "include_pending_replan")) keys.push("include_pending_replan"); + if (!hasExactAuthorityKeys(request, keys) || + (Object.hasOwn(request, "include_pending_replan") && request.include_pending_replan !== true)) { + throw new EffectRuntimeRequestError("checkpoint basis fields are invalid"); + } const root = absolute(request.root, "checkpoint root"); const manifest = await readReviewedManifest(root, request.expected_sha256); // This reads a digest-bound declaration after admission's full member // verification. It does not claim current integrity of every component. return {point_metadata: manifest.point_metadata, components: manifest.components, + ...(request.include_pending_replan === true ? {pending_replan: await readPendingReplanBasis(root, manifest)} : {}), integrity_verified: false, execution_authority_granted: false}; } const fields = request.action === "probe" ? ["action", "components"] : diff --git a/tests/control_plane/test_checkpoint_restore_capture.py b/tests/control_plane/test_checkpoint_restore_capture.py index 310a06502a..4c08087b34 100644 --- a/tests/control_plane/test_checkpoint_restore_capture.py +++ b/tests/control_plane/test_checkpoint_restore_capture.py @@ -1,10 +1,12 @@ """A restored fork needs a current, settled semantic decision before recapture.""" import copy +import hashlib +import json import pytest from loopx.control_plane.effect_program import SettlementIdentity -from loopx.control_plane.effect_runtime import effect_runtime_result +from loopx.control_plane.effect_runtime import EffectRuntimeRemoteError, effect_runtime_result def test_nonrestored_conversation_has_no_completed_checkpoint_review(tmp_path): @@ -78,12 +80,175 @@ def test_readonly_participant_capture_and_limited_basis_read(tmp_path): basis = effect_runtime_result("runtime.checkpoint_artifacts", {"action": "read_basis", "root": str(unit), "expected_sha256": captured["sha256"]}) assert basis["integrity_verified"] is False and basis["execution_authority_granted"] is False - from loopx.control_plane.effect_runtime import EffectRuntimeRemoteError with pytest.raises(EffectRuntimeRemoteError): effect_runtime_result("runtime.checkpoint_artifacts", {"action": "verify", "root": str(unit), "expected_sha256": captured["sha256"]}) +@pytest.mark.parametrize("location", ["root", "operator"]) +@pytest.mark.parametrize("note", [None, {}, False, [], 0, ""]) +def test_empty_replan_annotations_keep_the_ordinary_restore_path(tmp_path, monkeypatch, location, note): + from types import SimpleNamespace + from benchmark.runtime.checkpoint import parse_checkpoint_request + from benchmark.runtime.checkpoint_continuation import restored_capture_gate, restored_checkpoint_review + from benchmark.runtime.checkpoint_session_refresh import prepare_session_refresh + + source = tmp_path / "code" + source.write_text("retained work") + unit = tmp_path / "point" + request = parse_checkpoint_request(json.dumps({ + "destination": str(unit), "components": [{"id": "code", "source_path": str(source)}], + "point_metadata": {"replan": note}, + }).encode()) + metadata = {"loopx_state": {"goal_id": "checkpoint-source", "agent_id": "worker"}, + "operator_metadata": request["point_metadata"]} + if location == "root": + metadata["replan"] = metadata["operator_metadata"].pop("replan") + components = request["components"] + probe = effect_runtime_result("runtime.checkpoint_artifacts", {"action": "probe", "components": components}) + captured = effect_runtime_result("runtime.checkpoint_artifacts", { + "action": "capture", "components": components, "destination": str(unit), + "point_metadata": metadata, "expected_source_sha256": probe["source_sha256"], + }, retry_safe=False) + assert effect_runtime_result("runtime.checkpoint_artifacts", { + "action": "verify", "root": str(unit), "expected_sha256": captured["sha256"], + })["integrity_verified"] + before = {path.relative_to(unit): path.read_bytes() for path in unit.rglob("*") if path.is_file()} + binding = {"session_id": "retained-session", "checkpoint_artifact_sha256": captured["sha256"], + "checkpoint_artifact_root": str(unit)} + env = {"LOOPX_RUNTIME_ROOT": str(tmp_path / "runtime"), "LOOPX_GOAL_ID": "checkpoint-source", + "LOOPX_AGENT_ID": "worker", "LOOPX_WAKE_LOG_DIR": str(tmp_path / "wakes")} + monkeypatch.setattr("benchmark.runtime.checkpoint_continuation.load_codex_cli_session", + lambda *args, **kwargs: binding) + monkeypatch.setattr("benchmark.runtime.sessions.BenchmarkSessionWake", + lambda *args: SimpleNamespace(session_id=binding["session_id"])) + review = restored_checkpoint_review(env) + gate = restored_capture_gate(env) + assert review["semantic_review_accepted"] is False + assert gate["capture_allowed"] is False # Ordinary recovery still requires a current settled review. + assert review["instructions"] == gate["instructions"] + assert "This source has no retained pending replan packet." in gate["instructions"] + declaration = tmp_path / "warm-start.json" + declaration.write_text(json.dumps({ + "root": str(unit), "sha256": captured["sha256"], "fresh_replan": True, + "goal_id": "checkpoint-source", "agent_id": "worker", + })) + resets = [] + session = SimpleNamespace(session_id=binding["session_id"], binding=binding, + prepare_checkpoint_refresh=lambda *args, **kwargs: resets.append((args, kwargs))) + with pytest.raises(ValueError, match="fresh replan requires the original pending decision boundary"): + prepare_session_refresh({**env, "LOOPX_WARM_START": str(declaration), + "LOOPX_ITERATION_CONTEXT": "resume"}, session, tmp_path) + assert resets == [] # Empty advice cannot authorize a fresh pending-replan treatment. + basis = effect_runtime_result("runtime.checkpoint_artifacts", { + "action": "read_basis", "root": str(unit), "expected_sha256": captured["sha256"], + "include_pending_replan": True, + }) + assert basis["pending_replan"] is None and basis["execution_authority_granted"] is False + assert {path.relative_to(unit): path.read_bytes() for path in unit.rglob("*") if path.is_file()} == before + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_sealed_pending_basis_reuses_original_guard_and_rejects_replacements(tmp_path, monkeypatch, provider): + from loopx.control_plane.quota.turn_envelope import compose_turn_decision + from benchmark.runtime.checkpoint_continuation import restored_checkpoint_review + from benchmark.runtime.checkpoint_session_refresh import prepare_session_refresh + from types import SimpleNamespace + from test_replan_successor_guard_reentry import AGENT, GOAL, TURN, _fixture + + call, runtime, _ = _fixture(tmp_path, monkeypatch, provider) + original = call("quota", "should-run", "--runtime-profile", "generic_cli", "--goal-id", GOAL, + "--agent-id", AGENT, "--turn-instance-id", TURN, + "--decision-output-dir", str(tmp_path / "original")) + assert original["effective_action"] == "autonomous_replan_required" + retained = tmp_path / "original" / "decision.json" + source_bytes = retained.read_bytes() + source = json.loads(source_bytes) + identity = source["heartbeat_receipt"]["settlement_identity"] + runs = runtime / "goals" / GOAL / "runs" / "index.jsonl" + original_runs = runs.read_bytes() + pending = {"capture": True, "obligation_id": identity["replan_obligation_id"], + "settlement_effect_id": identity["effect_id"], "execution_authority_granted": False} + complete = compose_turn_decision({**source, "future_observation": {"exact": 2**53 + 1, "decimal": 1.0}}) + packet = tmp_path / "held-decision.json" + packet.touch(mode=0o600) + evidence = [{"id": "held_decision", "source_path": str(packet)}] + + def seal(name, payload, *, wanted=pending, decision_digest=None, operator_fallback=False): + raw = json.dumps(payload, ensure_ascii=False).encode() + packet.write_bytes(raw) + digest = hashlib.sha256(raw).hexdigest() + request = {"action": "capture", "components": evidence, "destination": str(tmp_path / name), + "point_metadata": {"held_decision_component_id": "held_decision", "loopx_state": { + "goal_id": GOAL, "agent_id": AGENT}, "replan": wanted, "decision_sha256": decision_digest or digest}, + "expected_source_sha256": effect_runtime_result("runtime.checkpoint_artifacts", { + "action": "probe", "components": evidence})["source_sha256"]} + if operator_fallback: + request["point_metadata"].update(replan=False, operator_metadata={"replan": wanted}) + sealed = effect_runtime_result("runtime.checkpoint_artifacts", request, retry_safe=False) + return {"action": "read_basis", "root": request["destination"], "expected_sha256": sealed["sha256"], + "include_pending_replan": True}, digest + + for name, payload in (("historical", source), ("complete", complete), ("operator-fallback", source)): + request, digest = seal(name, payload, operator_fallback=name == "operator-fallback") + selected = effect_runtime_result("runtime.checkpoint_artifacts", request) + assert selected["pending_replan"] == {"intent": pending, "decision_sha256": digest, + "decision_path": str(tmp_path / name / "components" / "held_decision")} + assert not selected["integrity_verified"] and not selected["execution_authority_granted"] + + # Exercise both actual receivers with a sealed source and an already + # admitted native binding. Only the external native transport is fake. + binding = {"session_id": "synthetic-session", "checkpoint_artifact_sha256": request["expected_sha256"], + "checkpoint_artifact_root": request["root"]} + declaration = tmp_path / "warm-start.json" + declaration.write_text(json.dumps({"root": request["root"], "sha256": request["expected_sha256"], + "goal_id": GOAL, "agent_id": AGENT, "fresh_replan": True})) + partial = tmp_path / "context-refresh" / "pre-replan.json" + partial.parent.mkdir(exist_ok=True) + partial.touch() # Recovery of a lost external reply must not re-admit. + env = {"LOOPX_RUNTIME_ROOT": str(runtime), "LOOPX_GOAL_ID": GOAL, "LOOPX_AGENT_ID": AGENT, + "LOOPX_WARM_START": str(declaration), "LOOPX_ITERATION_CONTEXT": "resume", + "LOOPX_WAKE_LOG_DIR": str(tmp_path / "wakes")} + observations = [] + session = SimpleNamespace(**binding, binding=binding, prepare_checkpoint_refresh=lambda *args, **kw: + observations.append((args, kw))) + with monkeypatch.context() as m: + m.setattr("benchmark.runtime.checkpoint_continuation.load_codex_cli_session", lambda *a, **kw: binding) + m.setattr("benchmark.runtime.sessions.BenchmarkSessionWake", lambda *a: session) + notice = prepare_session_refresh(env, session, tmp_path) + assert "BEFORE a pending replan" in notice and observations == [( + (None, partial, tmp_path / "stdout.jsonl"), + {"pre_replan_basis": {"pending_replan": pending, "decision_sha256": digest}}, + )] + review = restored_checkpoint_review(env) + assert not review["semantic_review_accepted"] + assert selected["pending_replan"]["decision_path"] in review["instructions"] + + for name, payload, wanted, expected in ( + ("tampered", {**complete, "future_observation": {"exact": 2**53}}, pending, "integrity mismatch"), + ("unknown", {**complete, "schema_version": "unknown"}, pending, "unsupported complete Turn decision version"), + ("wrong-intent", source, {**pending, "obligation_id": "replan-" + "b" * 16}, "binding differs"), + ("missing-intent", source, None, "checkpoint pending replan must be an object"), + ("empty-intent", source, {}, "checkpoint pending replan must be an object"), + ("false-intent", source, False, "checkpoint pending replan must be an object"), + ): + request, _ = seal(name, payload, wanted=wanted) + with pytest.raises(EffectRuntimeRemoteError, match=expected): + effect_runtime_result("runtime.checkpoint_artifacts", request) + + request, _ = seal("wrong-member", source, decision_digest="a" * 64) + with pytest.raises(EffectRuntimeRemoteError, match="packet digest differs"): + effect_runtime_result("runtime.checkpoint_artifacts", request) + request, _ = seal("replaced-member", source) + member = tmp_path / "replaced-member" / "components" / "held_decision" + saved = member.read_bytes() + member.write_bytes(b" " + saved[1:]) # Same size as the sealed file. + with pytest.raises(EffectRuntimeRemoteError, match="packet digest differs"): + effect_runtime_result("runtime.checkpoint_artifacts", request) + assert retained.read_bytes() == source_bytes + assert runs.read_bytes() == original_runs + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("cited", [False, True]) def test_successor_review_needs_checkpoint_linked_settlement(tmp_path, monkeypatch, provider, cited): From 2f7b4c0ace589c7c2cd4dfea5169a13380405512 Mon Sep 17 00:00:00 2001 From: xiaobaifly7 Date: Mon, 16 Nov 2026 23:41:06 +0800 Subject: [PATCH 21/34] fix(replan): project exact closeout commands for a pure replan Turn (#8855) The successor closeout guard appended settlement flags that should-run rejects, and the coverage-backed no-follow-up option did not list its required inputs, so agents assembled them by trial and error. Render the guard with the original Turn only, and project the exact no-follow-up arguments and Vision fields. Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-authored-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .../work_items/interaction_contract.py | 12 +- .../work_items/replan_semantics.ts | 13 ++ .../test_replan_successor_guard_reentry.py | 120 ++++++++++++++++++ .../test_replan_writeback_projection.py | 7 +- .../control_plane_ts/replan_semantics.test.ts | 15 +++ 5 files changed, 161 insertions(+), 6 deletions(-) diff --git a/loopx/control_plane/work_items/interaction_contract.py b/loopx/control_plane/work_items/interaction_contract.py index 149d105516..7bc65511b2 100644 --- a/loopx/control_plane/work_items/interaction_contract.py +++ b/loopx/control_plane/work_items/interaction_contract.py @@ -13,6 +13,8 @@ import typing from collections.abc import Collection, Mapping +from ...install_contract import render_host_cli_command + from ..agents.agent_scope_frontier import AgentScopeFrontierAction, agent_scope_frontier_action as _agent_scope_frontier_action from ..agents.capability_gate import runtime_capabilities_for_cli_projection from ..agents.workspace_guard import delivery_workspace_refresh_hint @@ -644,7 +646,7 @@ def _selection_recovery_command( def _render_replan_successor_closeout_guard( *, settlement_plan: Mapping[str, Any] | None, - scheduler_args: str, typed_quota_guard: str, settlement_args: str, + scheduler_args: str, typed_quota_guard: str, ) -> str | None: """Render this host's guard for an existing TS-owned pure replan identity.""" if not scheduler_args or not settlement_plan: @@ -653,7 +655,11 @@ def _render_replan_successor_closeout_guard( if not isinstance(identity, Mapping) or identity.get("binding_kind") != "autonomous_replan": # Todo-bound replans retain their distinct closeout path. return None - return typed_quota_guard + settlement_args + # The receipt recovers the obligation; should-run accepts only its Turn. + return render_host_cli_command([ + *shlex.split(typed_quota_guard), + "--turn-instance-id", str(identity["turn_instance_id"]), + ]) def _deferred_resume_continuation( @@ -1030,7 +1036,7 @@ def _admitted_interaction_next_cli_actions( ), successor_closeout_guard=_render_replan_successor_closeout_guard( settlement_plan=settlement_plan, scheduler_args=scheduler_args, - typed_quota_guard=typed_quota_guard, settlement_args=settlement_args, + typed_quota_guard=typed_quota_guard, ), ) return _terminal_cli_actions( diff --git a/loopx/control_plane/work_items/replan_semantics.ts b/loopx/control_plane/work_items/replan_semantics.ts index 69aaef6387..ddf38a9b55 100644 --- a/loopx/control_plane/work_items/replan_semantics.ts +++ b/loopx/control_plane/work_items/replan_semantics.ts @@ -575,6 +575,19 @@ export function projectReplanSemantics(value: unknown): JsonObject { if (request.operation === "requirements") { const strideControl = strideControlContract(object(obligation.replan_context), obligation.obligation_id); const writeback = writebackProjection(required, externalReview, strideControl !== null); + if (required.includes("coverage_backed_no_followup")) { + writeback.writeback_contract = {...object(writeback.writeback_contract), + no_followup_requirements: { + cli_args: ["--progress-result-class", "no_followup", "--progress-coverage-scope-id", "", + "--progress-evidence-id", "", + "--agent-vision-json", ""], + vision_fields: {state: "no_followup", "path_delta.outcome": "stop"}, + preserve: ["vision_patch"], + rule: "Use verified scope coverage and the vision_authoring path_delta fields with evidence_refs. " + + "Retain the current complete vision_patch: changing durable fields requires outcome=replan and cannot be combined with stop. " + + "Write back and spend with the original settlement_plan identity, not a later obligation.", + }}; + } if (requiresStrideDecision(obligation)) { const contract = object(writeback.writeback_contract); writeback.cli_semantic_args = VISION_CLI_ARGS; diff --git a/tests/control_plane/test_replan_successor_guard_reentry.py b/tests/control_plane/test_replan_successor_guard_reentry.py index fd5586492a..48508cd15a 100644 --- a/tests/control_plane/test_replan_successor_guard_reentry.py +++ b/tests/control_plane/test_replan_successor_guard_reentry.py @@ -2,7 +2,9 @@ from __future__ import annotations import json +import os import shlex +import shutil import subprocess import sys from pathlib import Path @@ -107,6 +109,124 @@ def _add(call, obligation_id: str): "--operation-id", "periodic-source-successor", "--replan-obligation-id", obligation_id) +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_projected_successor_closeout_guard_executes_original_turn(tmp_path, monkeypatch, provider): + from loopx.command_invocation import loopx_cli_route + from loopx.control_plane.scheduler.execution_context import scheduler_execution_context_for_runtime_profile + from loopx.control_plane.work_items.interaction_contract import interaction_next_cli_actions + from loopx.control_plane.work_items.progress_observation import build_replan_action_packet + + call, _, _ = _fixture(tmp_path, monkeypatch, provider) + original = _guard(call) + identity = original["heartbeat_receipt"]["settlement_identity"] + # Exercise the existing bounded-research successor projection over a real guard. + original["replan_action_packet"] = build_replan_action_packet( + original["autonomous_replan_obligation"], goal_id=GOAL, agent_id=AGENT, + bounded_research_frontier={"selected_gap": { + "successor_summary": "Independently verify the source artifact", + "successor_binding": {"action_kind": "validate", "target_key": "independent-source-artifact"}, + }}, + ) + cli = Path(sys.executable).parent / ("loopx.exe" if os.name == "nt" else "loopx") + with loopx_cli_route(str(cli)): + actions = interaction_next_cli_actions( + original, mode="autonomous_replan", capability_reentry_resolved=True, + settlement_plan=original["interaction_contract"]["cli_channel"]["settlement_plan"], + scheduler_execution_context=scheduler_execution_context_for_runtime_profile("codex_app_heartbeat"), + runtime_root=original["runtime_root"], registry_path=str(tmp_path / "registry.json"), + turn_instance_id=TURN, + ) + command = actions[1] + tokens = shlex.split(command) + if os.name == "nt": + assert tokens.pop(0) == "&" + _add(call, identity["replan_obligation_id"]) + result = subprocess.run( + [*tokens, "--context-delivery", "off"], + capture_output=True, text=True, timeout=60, + ) + reread = json.loads(result.stdout) + assert result.returncode == 0, reread.get("error") or reread.get("reason") or reread + assert reread["heartbeat_receipt"]["settlement_identity"] == identity + assert reread["replan_action_packet"]["settlement_only"] is True + if os.name == "nt": + shell = shutil.which("pwsh") or shutil.which("powershell") + assert shell is not None + result = subprocess.run( + [shell, "-NoProfile", "-Command", command + "; exit $LASTEXITCODE"], + capture_output=True, text=True, timeout=60, + env={**os.environ, "PATH": str(Path(sys.executable).parent) + os.pathsep + os.environ["PATH"]}, + ) + native = read_cli_policy_payload(result.stdout, returncode=result.returncode) + assert result.returncode == 0, native + assert native["heartbeat_receipt"]["settlement_identity"] == identity + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_projected_terminal_replan_fields_execute_without_guessed_flags(tmp_path, monkeypatch, provider): + from copy import deepcopy + from loopx.install_contract import render_host_cli_command + + call, _, index = _fixture(tmp_path, monkeypatch, provider, future_monitor=False) + call("configure-goal", "--goal-id", GOAL, "--execution-stride-strategy", "off", "--execute") + # The complete delivery has no remaining Todo; retain its accepted mainline. + rows = [json.loads(row) for row in index.read_text().splitlines()] + vision = deepcopy(next(row["agent_vision"] for row in rows if "agent_vision" in row)) + original = _guard(call) + identity = original["heartbeat_receipt"]["settlement_identity"] + contract = original["replan_action_packet"]["writeback_contract"] + context = original["autonomous_replan_obligation"]["replan_context"] + assert context["evidence"] + for row in context["evidence"]: + read = row.get("read_action") or context["evidence_read_action_template"].replace("{evidence_ref}", row["evidence_ref"]) + detail = call(*shlex.split(read)[1:]) + assert detail["evidence"]["evidence_ref"] == row["evidence_ref"] + terminal = contract["no_followup_requirements"] + assert terminal["cli_args"] == ["--progress-result-class", "no_followup", + "--progress-coverage-scope-id", "", "--progress-evidence-id", "", + "--agent-vision-json", ""] + assert terminal["vision_fields"] == {"state": "no_followup", "path_delta.outcome": "stop"} + assert "vision_patch" in terminal["preserve"] + vision["state"] = terminal["vision_fields"]["state"] + vision["path_delta"] = { + "schema_version": "goal_path_delta_v0", "outcome": terminal["vision_fields"]["path_delta.outcome"], + "prior_assumption": "The bounded source scope could need more work.", + "observed_reality": "Independent validation covers the complete source scope.", + "stopped": ["Repeated source checks"], "evidence_refs": ["validation:complete-source"], + } + path = tmp_path / "author's terminal.json" + path.write_text(json.dumps(vision), encoding="utf-8") + command = next(action for action in original["interaction_contract"]["cli_channel"]["next_cli_actions"] + if "refresh-state" in action) + argv = shlex.split(command) + # Replace only the caller-authored values in the projected template. + argv[argv.index("--agent-vision-json") + 1] = str(path) + values = {"": str(path), "": "scope:complete-source", + "": "validation:complete-source"} + for flag, value in zip(terminal["cli_args"][::2], terminal["cli_args"][1::2], strict=True): + if flag in argv: + argv[argv.index(flag) + 1] = values.get(value, value) + else: + argv.extend([flag, values.get(value, value)]) + argv.extend(["--no-global-sync", "--suppress-external-sinks"]) + if os.name == "nt": + shell = shutil.which("pwsh") or shutil.which("powershell") + assert shell is not None + # Exercise the native shell, including a quoted executable and apostrophe. + argv[0] = str(Path(sys.executable).parent / "loopx.exe") + invocation = [shell, "-NoProfile", "-Command", render_host_cli_command(argv) + "; exit $LASTEXITCODE"] + else: + invocation = [sys.executable, "-m", "loopx.cli", *argv[1:]] + result = subprocess.run(invocation, capture_output=True, text=True, timeout=60) + written = json.loads(result.stdout) + assert result.returncode == 0, json.dumps(written, ensure_ascii=False) + assert written["settlement_identity"] == identity + assert written["autonomous_replan_ack"]["semantic_delta"]["satisfying_outcomes"] == ["coverage_backed_no_followup"] + spent = call(*shlex.split(written["settlement_owed"]["command"])[1:]) + assert spent["settlement_progress"]["state"] == "settled" + assert call(*shlex.split(written["settlement_owed"]["command"])[1:])["idempotent_replay"] is True + + @pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("context_delivery", ["off", "on"]) def test_successor_cli_reads_policy_with_context_delivery( diff --git a/tests/control_plane/test_replan_writeback_projection.py b/tests/control_plane/test_replan_writeback_projection.py index 96258b7975..ecff0fda03 100644 --- a/tests/control_plane/test_replan_writeback_projection.py +++ b/tests/control_plane/test_replan_writeback_projection.py @@ -155,7 +155,7 @@ def test_declared_outcomes_not_trigger_name_determine_writeback_input() -> None: def test_guarded_successor_guidance_closes_original_turn_before_stopping(profile: str) -> None: payload = {"replan_action_packet": {"writeback_contract": {"successor_command": "loopx todo add"}}} binding = "--replan-obligation-id replan-1111111111111111 --turn-instance-id turn-original" - guard = f"loopx --format json quota should-run {profile} --goal-id example --agent-id agent-example {binding}" + guard = f"loopx --format json quota should-run {profile} --goal-id example --agent-id agent-example --turn-instance-id turn-original" actions = build_autonomous_replan_cli_actions( payload, goal_id="example", settlement_args=f" {binding}", scoped_cli_args=" --agent-id agent-example", quota_spend_action="spend-bound-turn", @@ -243,10 +243,11 @@ def test_successor_guard_entrypoint_retains_host_and_only_pure_replan_binding( tokens = shlex.split(actions[1]) assert tokens[tokens.index("--runtime-root") + 1] == "/tmp/public-safe-replan-runtime" assert tokens[tokens.index("--turn-instance-id") + 1] == "turn-original" - assert tokens[tokens.index("--replan-obligation-id") + 1] == identity["replan_obligation_id"] + assert "--replan-obligation-id" not in tokens assert tokens[tokens.index("--agent-id") + 1] == "agent-example" assert "--todo-id" not in tokens - assert render_scheduler_execution_args(scheduler_execution_context=context).strip() in actions[1] + for arg in shlex.split(render_scheduler_execution_args(scheduler_execution_context=context)): + assert arg in tokens assert "do not execute or select its successor" in actions[2] diff --git a/tests/control_plane_ts/replan_semantics.test.ts b/tests/control_plane_ts/replan_semantics.test.ts index 54ff89e5cd..87e92fabdd 100644 --- a/tests/control_plane_ts/replan_semantics.test.ts +++ b/tests/control_plane_ts/replan_semantics.test.ts @@ -370,6 +370,21 @@ test("no-followup cannot hide an inconsistent vision behind another accepted out assert.equal(valid.accepted, true); }); +test("terminal input projection supplies coverage and vision without widening restricted exits", () => { + const result = projectReplanSemantics({operation: "requirements", obligation}); + const contract = result.writeback_contract as JsonObject; + const terminal = contract.no_followup_requirements as JsonObject; + assert.deepEqual(terminal.cli_args, ["--progress-result-class", "no_followup", + "--progress-coverage-scope-id", "", "--progress-evidence-id", "", + "--agent-vision-json", ""]); + assert.deepEqual(terminal.vision_fields, {state: "no_followup", "path_delta.outcome": "stop"}); + assert.deepEqual(terminal.preserve, ["vision_patch"]); + const restricted = projectReplanSemantics({operation: "requirements", obligation: { + ...obligation, satisfying_semantic_outcomes: ["new_concrete_blocker"], + }}); + assert.equal((restricted.writeback_contract as JsonObject).no_followup_requirements, undefined); +}); + test("external progress review: renamed identifiers discharge only behind new evidence; the plan may stand on evidence", () => { const review = {triggers: [{kind: "external_progress_review_drift", model_authority: "none"}]}; const projection = projectReplanSemantics({operation: "requirements", obligation: review}); From d0f3ea951d9bf18e8b332740d88731bbdc3b9f82 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:41:20 +0800 Subject: [PATCH 22/34] docs(rfc): update benchmark portfolio priority ranking and Bitter-Lesson boundary (#8835) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add §2.2 summarizing the Bitter-Lesson raw-artifact reset boundary and five failure regimes requiring governed control planes, require a Raw-Artifact Reset Baseline under §5.2 Native baseline for episodic scalar/RSI tracks, add §3.8 with the 2026-11 benchmark portfolio priority matrix (P0/P1/P2/Control-Only) and platform constraints, update §17/§18 in EN/ZH mirrors without touching §6.4, and link the updated ranking from docs/research/README.md. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- ...n-harness-benchmark-research-program-v0.md | 291 +++++++++++++++++- ...ess-benchmark-research-program-v0.zh-CN.md | 228 +++++++++++++- docs/research/README.md | 7 + 3 files changed, 524 insertions(+), 2 deletions(-) diff --git a/docs/architecture/rfcs/long-horizon-harness-benchmark-research-program-v0.md b/docs/architecture/rfcs/long-horizon-harness-benchmark-research-program-v0.md index 61d7d40611..30688e2ce7 100644 --- a/docs/architecture/rfcs/long-horizon-harness-benchmark-research-program-v0.md +++ b/docs/architecture/rfcs/long-horizon-harness-benchmark-research-program-v0.md @@ -104,6 +104,121 @@ proved. Private trajectories and audit artifacts remain local evidence. Only their generalized typed contracts and public-safe conclusions may be promoted to the repository. +### 2.2 Bitter-Lesson boundary: raw-artifact resets versus governed control + +As frontier coding models improve at autonomous filesystem navigation and +self-authored inspection scripts, heavy hand-written context compression and +multi-layered outer prompts risk incurring net protocol tax ($\Delta J(B \mid +s_0) \le 0$). On **episodic, resettable, well-structured tasks with dense or +continuous isolated scalar verification**, a minimal outer loop that retains raw +prior artifacts and scalar scores across clean per-generation context resets—letting +the agent author its own Python/filesystem retrieval probes—can match or +outperform lossy hand-written summaries: + +- [Meta-Harness](https://arxiv.org/html/2603.28052v1) lets an outer proposer + selectively inspect prior source code, scores, and execution traces. Its + TerminalBench-2 search reads a median of `82` files per proposer iteration + (Appendix A.1); separately, one artifact evaluation can **generate up to `10M` + tokens of diagnostic information** (§1, Table 1). The latter is not measured + proposer consumption per iteration. Classification accuracy improves by `7.7` + points over ACE with `4x` fewer context tokens; the discovered Opus 4.6 harness + reaches `76.4%` on TerminalBench-2. This retains a propose/evaluate/log outer + loop; it does not establish that scalar scores alone or removing all harness + mechanisms produces those gains. +- [Vercel `d0`](https://vercel.com/blog/we-removed-80-percent-of-our-agents-tools) + shows removing ~80% of custom tools (`17 -> 2`: `bash` + SQL) over a clean, + well-documented semantic filesystem improved success `80% -> 100%` while + cutting tokens `37%` and latency `3.5x`. +- [AlphaEvolve](https://arxiv.org/abs/2506.13131) succeeds on algorithmic code + discovery precisely by keeping a fixed immutable outer loop over deterministic + programmatic verifiers without letting the solver alter its own harness or + grading contract. +- Published LoopX context studies ([context delivery](https://loopx-project.github.io/loopx/blog/context-delivery/) + and [EdgeBench feedback and memory](https://loopx-project.github.io/loopx/blog/edgebench-feedback-and-memory/)) + likewise show that verbose serialization and post-peak replan loops can consume + substantial uncached context when raw-code inspection and local probes would + suffice. + +However, empirical evidence establishes **five failure regimes** where raw +artifacts, scalar rewards, and clean resets cannot replace a governed control +plane: + +1. **Goodharting and verifier tampering:** Whenever the solver shares process, + filesystem, network, or secret boundaries with the grader, unguarded + optimization exploits shortcut channels—observed in `LHTB 2.0` (`14/17` early + perfect scores came from grader leaks prior to process freezing and verifier + isolation), the [Reward Hacking Benchmark](https://arxiv.org/abs/2605.02964) + (`0%` to `13.9%` exploit rates across 13 frontier models, `72%` with explicit + chain-of-thought rationalizations, and alignment breakdown past intractability + thresholds), [SWE-Marathon](https://arxiv.org/abs/2606.07682) (`13.8%` + rollout reward hacking), [Darwin Gödel Machine](https://arxiv.org/abs/2505.22954) + (Node 114 falsifying verification logs), and + [METR reward-hacking evaluations](https://metr.org/blog/2025-06-05-recent-reward-hacking/). + In-agent Python probes cannot enforce OS/container verifier isolation or + `benchmark_integrity_audit_v0` (§6.5, §7.1). +2. **Out-of-distribution (OOD) overfitting and harness bloat:** Unregularized + evolution over raw artifacts memorizes training traces and degrades OOD. + [RRSI](https://arxiv.org/abs/2609.24972) finds unregularized Meta-Harness + gains only `+0.9` OOD (`40.6` vs `39.7`) and `TTHE` drops `-1.7` (`38.0` with + an `8,500`-word bloated prompt), whereas critic-pruned, budget-annealed + regularization lifts OOD to `43.6` (`+3.9`) with `30%` fewer policy tokens; + [Harness Updating Is Not Harness Benefit](https://arxiv.org/abs/2605.30621) + similarly shows harness edits mainly lift mid-tier models while saturating or + regressing frontier models without OOD gating. Sequential validity, held-out + splits, and decision-preserving rate-distortion memory are governed alongside + this program in the [Frontier Science Research Program](./frontier-science-research-program-v0.md#2-problem-and-motivation) + (§2 and §5 `T01`–`T07`). +3. **Premature termination and sparse or binary terminal verifiers:** Fresh + retrieval over local files cannot stop an agent from declaring completion as + soon as local smoke fixtures pass, nor extract dense guidance from `0/1` + terminal rewards. In LoopX's published `SWE-Marathon` study + ([`benchmark/swe-marathon/README.md`](../../../benchmark/swe-marathon/README.md)), + on `zstd-decoder`, `plain` Codex stopped after visible fixtures (`0.721` + partial, reward `0`) whereas governed `heartbeat` continued to protocol + self-tests and memory-safety verification (`1.000`, reward `1`); on `LHTB` + `covshift` ([EdgeBench long-horizon repairs](https://loopx-project.github.io/loopx/blog/edgebench-long-horizon-repairs/)), + default completion exited at minute 38 (`0.000`) while an owner-locked + acceptance gate forced completion to `0.82` (five-arm `heartbeat` `0.743`). + External completion rejection, `stopped_recovery.py`, and hidden-coverage + checks remain necessary. +4. **Proxy misalignment and tail TLE cliffs:** Uncalibrated local yardsticks and + aggregate averages invert native rankings or trigger timeout cliffs. LoopX's + [`benchmark/proxy_order.py`](../../../benchmark/proxy_order.py) analysis + ([behavior findings](../../reference/benchmark-behavior-findings.md#compare-a-local-proxy-with-released-scores)) + shows local proxies reversing native acceptance ordering; + [DuoDiscover Table 4](https://arxiv.org/html/2610.17184v1#S4.T4) separates + SWE-fficiency speedup (`x`) from SOL-Exec score (`% SOL`): adding untested + yardsticks to target selection reduces `12.71x / 69.68%` to + `10.33x / 66.91%`; the next row adds fidelity tests and reaches + `13.25x / 72.57%`. Co-discovery then reaches `13.76x / 74.43%`, and adding + the orchestrator produces the full system's `14.93x / 80.80%`. The full + result is not the isolated contribution of fidelity gating. Separately, + `41%`–`45%` of yardsticks become invalid as code changes; and + [LangChain's Terminal-Bench 2.0 study](https://www.langchain.com/blog/improving-deep-agents-with-harness-engineering) + showed uniform `xhigh` reasoning dropped accuracy (`63.6% -> 53.9%`) via + timeout cliffs without phase-aware effort control. Governed yardstick + calibration and tail-latency pressure gating remain necessary safeguards around + native optimization. +5. **Stateful dependency-DAG, coverage, and workspace/settlement invariants:** + Multi-unit dependency frontiers, non-episodic environments, linked-worktree + patch normalization, and idempotent settlement cannot be repaired by solver + scripts inside the container. [LoopsBench](https://arxiv.org/abs/2608.00267) + (`112` tasks, `>5,300` dependency-DAG units) holds even top loop-enabled SOTA + at `25.00%`; one `EdgeBench` `New Foundations Consistency` five-arm, + Astra max, `18h` study reports Explore with a declaration ledger and direction + reviews reaching `92.3419`, above the `91.9412` plateau of Official and Resume + ([report source](https://github.com/loopx-project/loopx/blob/d0ca48db02846be61a9b683f0efcd5c706381b66/apps/presentation/site/public/blog/edgebench-feedback-and-memory/index.html#L63)). + This is single-run descriptive evidence for a combined profile, not an + isolated causal effect of the ledger or a C2/C3 result; + and real benchmark infrastructure requires host-owned invariants such as + [`workspace_delivery.py`](../../../benchmark/deepswe-gptxhigh-v1/workspace_delivery.py) + (normalizing linked-worktree diffs back to `/app`), + [`frontiercs2/README.md`](../../../benchmark/frontiercs2/README.md) (`v2.patch` + tmpfs `docker exec` streaming and durable admission recovery), and + [`widesearch/pier/README.md`](../../../benchmark/widesearch/pier/README.md) + (`$HOME` bind mounts, numeric `reward.json` validation, and + `provider_credential_shell_excluded`). + ## 3. Benchmark Primer and Fit Analysis Benchmark counts and leaderboard values in this section are an informative @@ -364,6 +479,97 @@ The six must still be reported in their native metric spaces; StartupBench rubrics, LoopsBench verifier tiers, and WideSearch Item F1 must not be averaged into one number or into the ALE/LHTB/DeepSWE results. +### 3.8 External Benchmark Survey and Priority Ranking (2026-11) + +This 2026-11-15 audit updates the August 2026 six-benchmark primer (§3.1–§3.7) +and guides integration sequencing (§9, §11) across EdgeBench and 20 external +benchmarks. Rankings weigh four empirical criteria: (1) remaining frontier +headroom against pure base-model scaling, (2) verifier determinism, resistance to +Goodharting, and availability of continuous or multi-unit progress signals, (3) +direct stress on the five governed failure regimes (§2.2), and (4) repository +readiness (`benchmark/` holds `52,031` `.py` LOC across `177` files: `27,338` +tests, `9,082` `runtime/`, `6,899` `edgebench/`, `4,433` `deepswe*`, `1,430` +`swe-marathon/`, `1,285` `frontiercs2*`, `596` `LHTB/`, `592` `widesearch/`, +`376` root helpers) versus Linux x86\_64 Docker, GPU, and cloud requirements. + +**Shared prerequisite before scaling multi-arm studies:** Scaling new Harbor, +cloud, or GPU benchmark runs is gated on converging adapters onto the unified +`benchmark/runtime/` Turn/Harbor entrypoint with matching native Stop-hook +baseline parity and clean E0–E2 lifecycle, integrity, and countability gates +(§11.2), rather than cloning bespoke per-benchmark runners. + +#### Priority Matrix + +| Tier | Benchmark | Tasks & Horizon | Reward / Verifier Shape | Frontier Headroom & Regime Signal | Engineering Readiness & Platform Constraints | +|---|---|---|---|---|---| +| **P0** | **EdgeBench** | Continuous algorithmic optimization (`12h–18h` runs) | Deterministic online cooldown judge; scalar task metrics (higher/lower by task) | Task-specific descriptive signals: `New Foundations 92.3419` (single five-arm study), `ANN 7,159 QPS` (separate `18h` study); Official leads in other task comparisons. These are not pooled equal-budget Pareto evidence; see the source boundaries below. | **Ready & evaluated** (`edgebench/` `6,899` LOC); requires **Linux x86\_64 Docker + SForge `iptables`** (macOS Docker socket lacks network boundary) | +| **P0** | **FrontierCS-2** | `48` public / `150` total (`13` domains); `5h`, `3–100` capped train calls | Deterministic hidden-workload Driver/Judge; binary `PASS` (must beat reference on *every* metric/workload) + `FCI`/`FDI` scalar | Top of 12 evaluated agents passes **`58%`**, **`16/48` at `0%`**; directly tests proxy calibration and multi-metric resistance | **Pilot bridge ready** (`frontiercs2*` `1,285` LOC, `v2.patch` transport repair; frozen v1 `batch_local_counting` archive is `score_countable=false`, `integrity_qualified=false`; `0` countable LoopX outcomes yet); **Linux x86\_64 Docker required** (macOS ARM Rosetta/QEMU breaks seccomp); EKS+Modal eval `~$1,400`/48 tasks | +| **P0** | **LHTB / LHTB 2.0** | `v1: 46` (`90m`); `v2.0: 77` (`46` orig + `31` reconstruction, `3h` w/ `30m` ckpts) | Continuous scalar `[0,1]` + 6-stage ladder (`R>=0.95`/`1.0`) + frozen-process Harbor binary rejection loop | `v1` LoopX `0.4948` vs Goal `0.4475` / Plain `0.4218` (unequal budgets); `v2.0` top `0.692` (`37/77`); `14/17` early perfect leaks proved verifier-isolation need | **`v1` Harbor adapter ready** (`LHTB/` `596` LOC); **`LHTB 2.0` has `0` LOC** in repo; non-GPU Docker / Daytona | +| **P0** | **LoopsBench** | `112` dependency-DAG tasks (`>5,300` units, `8` langs) | Deterministic unit + regression tests along ready DAG frontier (`incomplete` / `partial` / `complete`) | Top loop-enabled SOTA **`25.00%`** (`Opus-4.7` + Claude Code + outer loop); built specifically for outer-loop DAG & regression invariants | **`0` active code** in `benchmark/` (planned in §3.5/§9.5); non-GPU Linux x86\_64 Docker preferred | +| **P1** | **SWE-Marathon** | `20` ultra-long tasks (`15`-task v1.1 subset in-repo); `avg 27.2M tok`/run | Multi-layer adversarial verifier; binary solve + continuous `partial_score` | `v1` `<30%` frontier solve, **`13.8%` rollout reward hacking**; in-repo `30%` timeout `0.333 (5/15)` HB vs `0.267 (4/15)` Plain/Goal | **Ready & evaluated** (`swe-marathon/` `1,430` LOC); `amd64` multi-lang Docker; high token & wall-time cost (`1,592m–2,375m` per 15-task arm) | +| **P1** | **PaperBenchX** *(preferred over PaperBench)* | `93` papers (`12` areas / `6` non-ML sciences, `>3,000` criteria); `4–24h` (med `7h`) | Clean-from-scratch replay + deterministic checks & shared LLM-Judge (`>=90%` full pass) | Mean rubric `27.53–59.08%`, **`14%` full reproduction** (`>=90%`) — sharper validation gap & broader stacks than `PaperBench` (`20` ICML'24 papers, `24–36h`, `26.0%` SOTA) | **`0` code** in repo (`~2–4 wk` lift); heavy Linux Docker + domain stacks (`HFSS`, `Meep`, `GPU4PySCF`, `Habitat`) + Judge API secret isolation | +| **P1** | **ALE (Agents' Last Exam)** *(distinct from `ALE-Bench`)* | `1,500+` taxonomy (`v1.1: 122`: `119` CPU / `3` GPU, `78` Docker); multi-hour VMs | Post-run hidden reference `evaluate()`, scalar `[0,1]` | `<1%` full pass on hardest tier; tests cross-surface CLI+GUI professional transfer | **`0` active code** in repo (planned §3.1/§9.1; `~2–4 wk` lift); hard **GCP/GCE VM + CUA MCP** dependency (not locally runnable on macOS) | +| **P1** | **WideSearch** | `200` (`100 EN + 100 ZH`, `18` industries); `44+` pages / `~2.3h` human time | Automated atomic table grader (`Item F1` / `Row F1` + binary `SR`); non-deterministic live web drift | Frontier `SR <= 5.1%` (`o3` multi-agent `5.1%`, single-agent `4.5%`, `Gemini 2.5 Pro 4.3%`); high fit for completeness/fan-out, low–med for iterative RSI | **Adapter + `pier` templates ready** (`widesearch/` `592` LOC, `0` executed runs); requires Linux/cloud `$HOME` bind mounts (macOS `/tmp` fails Colima) & `permitted_solving` network | +| **P1** | **KernelBench / AlgoTune** | `KernelBench`: `250` PyTorch/CUDA ops (`L1–L3`+`L4`); `AlgoTune`: `154` SciPy/sklearn/CVXPY funcs | Deterministic correctness + GPU (`fast_p`) or CPU geom-mean speedup | `KernelBench` `<20% fast_1`; `AlgoTune` `AlgoTuner 1.72x` (`~2.05x` frontier); pure objective speedup for evolutionary/RLVR loops | **`0` code** in repo; `KernelBench` requires **NVIDIA CUDA/Triton GPU Linux/Modal** (impossible on Mac); `AlgoTune` requires **pinned multi-core Linux x86\_64** timing host | +| **P2** | **ALE-Bench (SakanaAI)** *(distinct from `ALE`)* | `40` AtCoder Heuristic Contest tasks (`C++/Rust/Py`, `300+` cases); `150`-step loops | Deterministic relative heuristic score | Strong 1-shot vs iterative separation; trails human consistency | **`0` active code** in repo; heavy multi-core Linux/AWS Docker; upstream rules bar bundling private seeds/inputs | +| **P2** | **StartupBench** | `97` deliverable tasks (`DOCX/XLSX/PPTX/PDF`, `6` domains, `25.3` rubrics/task) | Evidence views + weighted Agent-as-Judge `0–100` (`pass >= 90`, `92%` human agr.) | Top avg `Kimi-k3 73.67`, top `pass@1` `GPT-5.6-sol 31.27%`; low–med RSI fit (open-ended deliverable rubric) | **`0` active code** in repo (planned §3.4/§9.4); multi-turn office-artifact API + Judge boundary; non-GPU | +| **P2** | **MLE-bench** | `75` Kaggle tasks (`22` Lite `158 GB`, `3.3 TB` total); `24h` std (LB paused `2026-04`) | Deterministic Kaggle metric + medal thresholds | Main SOTA `64.44±1.18%`; contamination risk on older Kaggle comps | **`0` active code** in repo; prohibitive infra (`36 vCPU`, `440 GB RAM`, `24 GB A10 GPU`, `3.3 TB` disk, Kaggle auth/LFS) | +| **P2** | **tb-science** *(episodic transfer samples only; **excluded from RSI**)* | `96` merged tasks + `366` open PRs; Harbor format | **100% binary `1/0` `reward.txt`** (`public` agent vs `no-network` verifier) | High frontier difficulty on selected PRs, but **unsuitable for RSI** (zero per-gen gradient, no train/held-out split, CI-locked single horizon) | **`0` code** in repo (`~1–2 wk` Harbor lift); `ci_checks/check-task-timeout.sh` hard-locks **`28800s` (`8h`)**; Linux x86\_64 Docker preferred | +| **Control-Only / Deprioritized** | **DeepSWE** | `113` original tasks (`91` repos, `5` langs) | Hand-written behavioral verifiers (`0/1` binary, `1.4%` judge disagreement) | `v1.1` **approaching saturation around a `74%` three-way frontier tie**; retain only as sanity/regression control | **Ready** (`4,433` LOC w/ `workspace_delivery.py`); Linux Docker host worker required; historical v1 arms unequal-budget | +| **Control-Only / Deprioritized** | **SWE-bench Pro** | `v1: 1,865` (`731` pub); `v2: 642` + `HARD-51` | Patch + unit tests (`0/1` binary; `32.4%` judge disagreement) | **`HARD-51` up to `98.0%`** — saturating under base-model scaling | **`0` active code** in repo; `linux/amd64` Docker images (`ghcr.io`), non-native on Mac ARM | +| **Control-Only / Deprioritized** | **CORE-Bench** | `270` (`90` papers, `3` diffs); via HAL | Output-question binary match | Rose `21% -> 95%` (Opus 4.5 after Anthropic fixed tolerances/scaffold bugs) — saturated | **`0` code** in repo; Docker/Azure (`Python`/`R`) | +| **Control-Only / Deprioritized** | **ScienceAgentBench** | `102` (`44` papers); `30m` Docker runs | Valid Execution + rubric/code check | `32.4%–42.2%` (`o1-preview`); single-script scope limits long-horizon control separation | **`0` code** in repo; `30m` / 8-thread Docker | +| **Control-Only / Deprioritized** | **BixBench** | `60` capsules / `205` Qs (`v1.5`); `24–48h` 5-rep | Binary exact-match MCQ / LLM-Judge open answer | `17%` open-answer (older baselines, lacks 2026 SOTA); high 5-replica binary cost | **`0` code** in repo; high multi-replica API cost | +| **Control-Only / Deprioritized** | **FrontierScience** | `160` QA items (`100` Olympiad + `60` Research) | Short-answer + 10-pt rubric | `GPT-5.2 77%` Olympiad / `25%` Research; no interactive repo/sandbox loop | **`0` code** in repo; API reasoning only | +| **Control-Only / Deprioritized** | **RE-Bench** *(deprecated)* | `7` ML R&D tasks (`2h–32h`) | Deterministic ML eval (clock/memory exploit history) | **Deprecated upstream** (`METR/RE-Bench`); superseded by `FrontierCS-2`, `PaperBenchX`, and `KernelBench` | **`0` code** in repo; Linux x86\_64 + NVIDIA CUDA GPU required | + +The EdgeBench figures come from [distinct reported studies](https://github.com/loopx-project/loopx/blob/d0ca48db02846be61a9b683f0efcd5c706381b66/apps/presentation/site/public/blog/edgebench-feedback-and-memory/index.html#L43). +The New Foundations observation uses Astra max; the ANN `18h` study uses +Sol/xhigh with `Recall@10 >= 0.95`. Reported counterexamples include Portfolio +(`Official 53.87` versus Explore `31.19`, higher is better) and Historical Grid +(`Official 1,967` versus Context on + Stride `2,088`, lower is better) +([ANN and counterexample source](https://github.com/loopx-project/loopx/blob/d0ca48db02846be61a9b683f0efcd5c706381b66/apps/presentation/site/public/blog/edgebench-feedback-and-memory/index.html#L131)). +Retain each task's model, starting state, feedback rights, observation window, +and full-cost coverage. These observations guide task selection; a C2/C3 or +equal-budget Pareto claim requires a matched comparison under §5.1/§5.3 and +complete cost accounting under §6.2. + +#### Key Operational Disambiguations and Exclusions + +1. **`ALE` versus `SakanaAI/ALE-Bench` (and non-existent `"ALE Science"`):** + No public benchmark or track named `"ALE Science"` exists. `ALE` + ([`rdi-berkeley/agents-last-exam`](https://github.com/rdi-berkeley/agents-last-exam)) + is a broad professional CLI+GUI suite containing science subdomains, whereas + `ALE-Bench` ([`SakanaAI/ALE-Bench`](https://github.com/SakanaAI/ALE-Bench)) + covers AtCoder Heuristic Contest algorithmic optimization. They are ranked + separately above (`P1` vs `P2`). +2. **Why `tb-science` is excluded from RSI and retained only for episodic transfer:** + Following the `laude-institute` to `harbor-framework` migration, + [`harbor-framework/terminal-bench-science`](https://github.com/harbor-framework/terminal-bench-science) + is a scientific suite separate from `terminal-bench-2` (`89` general tasks at + `900s` default timeout). Despite `96` merged tasks and `366` open PRs, + `tb-science` is **not suited for iterative RSI loops**: CI + (`ci_checks/check-task-timeout.sh`) hard-locks `[agent] timeout_sec == 28800.0` + (`8h`), `reward.txt` is **100% binary `1/0`** (even when internal scorers compute + continuous metrics), there is no train/held-out split, `[agent] network_mode` is + `public`, and `rubrics/task-implementation.toml` rejects open-ended + hypothesis tasks. Retain only hard episodic transfer samples (e.g., Lean + formalization `#2530`, `#2197`, `#2653`, `#2629`, biology `#1518`, physics + `#2804`, or hosted GPU control `#2531`). +3. **`PaperBenchX` over legacy `PaperBench`, and `DeepSWE` terminology:** + `PaperBenchX` (`93` cross-discipline non-ML science papers with clean replay) + replaces legacy `PaperBench` (`20` ICML'24 ML papers) as the primary `P1` + reproduction target. `DeepSWE` here designates Datacurve's `113`-task + benchmark ([`datacurve-ai/deep-swe`](https://deepswe.datacurve.ai/)), + distinct from Agentica's `DeepSWE-Preview` model; its saturation near `74%` + shifts new `C2`/`C3` causal investment toward `P0`/`P1` benchmarks. +4. **Platform execution constraints:** Do not run score-countable benchmarks for Linux-seccomp, + `iptables`, CUDA, or multi-core timing suites (`EdgeBench`, `FrontierCS-2`, + `KernelBench`, `AlgoTune`, `SWE-bench Pro`, `tb-science`) on macOS ARM64 + Rosetta/QEMU emulation; run them on pinned Linux x86\_64 (and dedicated CUDA + or cloud hosts where required), and avoid macOS `/tmp` bind mounts on Colima + (`widesearch/pier/README.md`). + ## 4. Claim Ladder Every public result must name the highest claim level it supports. @@ -421,7 +627,13 @@ family” are not identity. The program distinguishes four arm types: 1. **Native baseline**: the benchmark's supported reference scaffold or a - declared CLI harness with no LoopX control. + declared CLI harness with no LoopX control. On **episodic RSI and scalar + optimization tracks**, a Governed LoopX C2/C3 claim must include a matched + **Raw-Artifact Reset Baseline**, run for this study or reused under the + qualification below. It retains all prior solver-visible artifacts and scalar + scores across clean per-generation context resets, using the model's native + filesystem/Python retrieval without injected LoopX packets or governance + ledgers (§2.2). This defines a treatment, not zero execution cost. 2. **Passive LoopX**: identical worker decisions and benchmark contract, with read-only LoopX observation and compact settlement. 3. **Governed LoopX**: a declared LoopX profile may affect continuation, @@ -429,6 +641,27 @@ The program distinguishes four arm types: 4. **Mechanism ablation**: one named mechanism differs from a governed parent profile while every other controllable field is held fixed. +For the Raw-Artifact Reset Baseline: + +- Pin the comparison identity in §5.1 and satisfy §5.3, including the same task, + model revision/effort, starting state, environment/verifier, budget and + feedback/tool/network/authority envelope. Declare reset cadence, stopping + rules and any intended treatment differences before inspecting outcomes. +- “Prior artifacts” means only material the solver was authorized to see at + that point in its run. Hidden references, answers, verifier internals, + operator-only diagnostics and post-run grading material remain inaccessible; + a retained raw file does not grant access. +- Reuse a historical arm only when its pinned inputs, treatment, permitted + artifacts, integrity, outcomes and complete costs can be independently read + back and establish the same matched comparison. A scalar score or model-family + label alone is insufficient. Historical reuse does not make its costs zero. +- Charge retrieval/probe generation, context resets, archive/orchestration, + model use, evaluation/confirmation, cooldown, failures/retries and human + intervention under §6.2. Preserve missing coverage as unknown. With no + qualifying baseline or incomplete matching/cost evidence, retain descriptive + observations and any independently supported C0/C1 result; do not claim C2/C3 + uplift or full-cost Pareto superiority. + Assisted human or simulator intervention is a separate study family. It must never be presented as an autonomous leaderboard arm. @@ -1599,6 +1832,7 @@ This is a living research RFC, not a frozen benchmark snapshot. | 2026-08-23 | Add an engineering readiness ladder and bounded delivery-slice rules so repository construction is tracked separately from C0--C4 research claims. | | 2026-08-25 | Add StartupBench, LoopsBench, and WideSearch to the benchmark primer and integration plan as a second, complementary trio; require native metrics per benchmark and keep benchmark material out of reusable LoopX state. | | 2026-10-16 | Archive [feedback-first observations](https://github.com/loopx-project/loopx/blob/1d60e7edb2a05fe048767444f872934c3adef992/deprecate/benchmark-legacy/edgebench/feedback-first-observations-20261016.md) separately from active practice. Runner source-subset and configuration-projection corrections remain E0/E1 work; mixed-profile, interrupted and running observations do not close E2/E3 or isolate a planning effect. General guard re-entry relaxation is deferred pending affected-caller validation. | +| 2026-11-15 | Codify the Bitter-Lesson boundary and five governed control-plane failure regimes (§2.2), require a matched, fully costed Raw-Artifact Reset Baseline (newly run or qualified historical reuse) for C2/C3 claims on episodic RSI / scalar optimization tracks (§5.2), and adopt the 2026-11 benchmark portfolio ranking (§3.8): **P0** (`EdgeBench`, `FrontierCS-2`, `LHTB / LHTB 2.0`, `LoopsBench`), **P1** (`SWE-Marathon`, `PaperBenchX` over `PaperBench`, `ALE` distinct from `ALE-Bench`, `WideSearch`, `KernelBench` / `AlgoTune`), **P2** (`ALE-Bench`, `StartupBench`, `MLE-bench`, `tb-science` episodic transfer samples only—excluded from RSI due to CI-locked `28800s` budget and 100% binary `reward.txt`), and **Control-Only / Deprioritized** (`DeepSWE`, `SWE-bench Pro`, `CORE-Bench`, `ScienceAgentBench`, `BixBench`, `FrontierScience`, deprecated `RE-Bench`), gating multi-arm expansion on unified `benchmark/runtime/` + Stop-hook E0–E2 readiness and Linux x86\_64 / GPU hosts. | ## 18. References @@ -1607,11 +1841,13 @@ This is a living research RFC, not a frozen benchmark snapshot. - [Agents' Last Exam evaluation framework](https://github.com/rdi-berkeley/agents-last-exam) - [Long-Horizon Terminal Benchmark repository](https://github.com/zli12321/LHTB) - [LHTB benchmark report](https://zli12321.github.io/LHTB/index.html) +- [LHTB paper](https://arxiv.org/abs/2607.08964) - [DeepSWE project and leaderboard](https://deepswe.datacurve.ai/) - [DeepSWE paper](https://arxiv.org/abs/2607.07946) - [DeepSWE run guide](https://deepswe.datacurve.ai/run) - [StartupBench project](https://startupbench.github.io/) - [StartupBench paper](https://startupbench.github.io/assets/StartupBench.pdf) +- [StartupBench arXiv](https://arxiv.org/abs/2608.17800) - [LoopsBench project](https://loopsbench.ai/) - [LoopsBench repository](https://github.com/microsoft/Loopsbench) - [LoopsBench paper](https://arxiv.org/abs/2608.00267) @@ -1620,6 +1856,59 @@ This is a living research RFC, not a frozen benchmark snapshot. - [WideSearch repository](https://github.com/ByteDance-Seed/WideSearch) - [WideSearch paper](https://arxiv.org/abs/2508.07999) - [WideSearch dataset](https://huggingface.co/datasets/ByteDance-Seed/WideSearch) +- [EdgeBench repository](https://github.com/ByteDance-Seed/EdgeBench) +- [FrontierCS-2 project](https://frontier-cs.org/) +- [FrontierCS-2 repository](https://github.com/FrontierCS/FrontierCS-2) +- [FrontierCS-2 paper](https://arxiv.org/abs/2610.29932) +- [SWE-Marathon repository](https://github.com/abundant-ai/swe-marathon) +- [SWE-Marathon paper](https://arxiv.org/abs/2606.07682) +- [PaperBenchX repository](https://github.com/UniPat-AI/PaperBenchX) +- [PaperBenchX paper](https://arxiv.org/abs/2610.39744) +- [PaperBench reference implementation](https://github.com/openai/frontier-evals/tree/main/project/paperbench) +- [PaperBench paper](https://arxiv.org/abs/2504.01848) +- [KernelBench repository](https://github.com/ScalingIntelligence/KernelBench) +- [KernelBench paper](https://arxiv.org/abs/2502.10517) +- [AlgoTune repository](https://github.com/oripress/AlgoTune) +- [AlgoTune paper](https://arxiv.org/abs/2507.15887) +- [ALE-Bench (SakanaAI) repository](https://github.com/SakanaAI/ALE-Bench) +- [ALE-Bench paper](https://arxiv.org/abs/2506.09050) +- [MLE-bench repository](https://github.com/openai/mle-bench) +- [MLE-bench paper](https://arxiv.org/abs/2410.07095) +- [Terminal-Bench Science project](https://www.terminal-bench-science.ai/) +- [Terminal-Bench Science repository](https://github.com/harbor-framework/terminal-bench-science) +- [SWE-bench Pro repository](https://github.com/scaleapi/SWE-bench_Pro-os) +- [SWE-bench Pro paper](https://arxiv.org/abs/2509.16941) +- [CORE-Bench repository](https://github.com/siegelz/core-bench) +- [CORE-Bench paper](https://arxiv.org/abs/2409.11363) +- [ScienceAgentBench repository](https://github.com/OSU-NLP-Group/ScienceAgentBench) +- [ScienceAgentBench paper](https://arxiv.org/abs/2410.05080) +- [BixBench repository](https://github.com/Future-House/BixBench) +- [BixBench paper](https://arxiv.org/abs/2503.00096) +- [RE-Bench (deprecated) repository](https://github.com/METR/RE-Bench) +- [RE-Bench paper](https://arxiv.org/abs/2411.15114) +- [Meta-Harness paper](https://arxiv.org/abs/2603.28052) +- [Meta-Harness repository](https://github.com/stanford-iris-lab/meta-harness) +- [RRSI paper](https://arxiv.org/abs/2609.24972) +- [RRSI repository](https://github.com/google-research/rrsi) +- [Harness Updating Is Not Harness Benefit](https://arxiv.org/abs/2605.30621) +- [Agentic Harness Engineering (AHE)](https://arxiv.org/abs/2604.25850) +- [Self-Harness](https://arxiv.org/abs/2606.09498) +- [Reward Hacking Benchmark (RHB)](https://arxiv.org/abs/2605.02964) +- [DuoDiscover paper](https://arxiv.org/abs/2610.17184) +- [AlphaEvolve paper](https://arxiv.org/abs/2506.13131) +- [Darwin Gödel Machine (DGM) paper](https://arxiv.org/abs/2505.22954) +- [Darwin Gödel Machine repository](https://github.com/jennyzzt/dgm) +- [LangChain: Improving Deep Agents with harness engineering](https://www.langchain.com/blog/improving-deep-agents-with-harness-engineering) +- [Vercel `d0`: We removed 80% of our agent's tools](https://vercel.com/blog/we-removed-80-percent-of-our-agents-tools) +- [METR: Recent frontier models are reward hacking](https://metr.org/blog/2025-06-05-recent-reward-hacking/) +- [Anthropic: Demystifying evals for AI agents](https://www.anthropic.com/research/demystifying-evals-for-ai-agents) +- [Anthropic: Effective harnesses for long-running agents](https://www.anthropic.com/research/effective-harnesses-for-long-running-agents) +- [Anthropic: Harness design for long-running application development](https://www.anthropic.com/research/harness-design-long-running-apps) +- [LoopX: Context delivery study](https://loopx-project.github.io/loopx/blog/context-delivery/) +- [LoopX: EdgeBench feedback and memory study](https://loopx-project.github.io/loopx/blog/edgebench-feedback-and-memory/) +- [LoopX: EdgeBench long-horizon repairs](https://loopx-project.github.io/loopx/blog/edgebench-long-horizon-repairs/) +- [Frontier Science Research Program v0](./frontier-science-research-program-v0.md) +- [Exploratory benchmark behavior findings](../../reference/benchmark-behavior-findings.md) - [Benchmark research workspace](https://github.com/huangruiteng/loopx/blob/main/benchmark/README.md) - [DeepSWE research practice](https://github.com/huangruiteng/loopx/blob/main/benchmark/deepswe/README.md) - [Legacy benchmark history](https://github.com/loopx-project/loopx/tree/54d42da44283bb414b24f71d16642c25cbcaf5dd/deprecate/benchmark-legacy) diff --git a/docs/architecture/rfcs/long-horizon-harness-benchmark-research-program-v0.zh-CN.md b/docs/architecture/rfcs/long-horizon-harness-benchmark-research-program-v0.zh-CN.md index 6510e90498..eb08713df7 100644 --- a/docs/architecture/rfcs/long-horizon-harness-benchmark-research-program-v0.zh-CN.md +++ b/docs/architecture/rfcs/long-horizon-harness-benchmark-research-program-v0.zh-CN.md @@ -84,6 +84,81 @@ validation 后才能 accountable settlement、精简 run reduction,以及 trea 无法证明时显式判无效。私有 trajectory 与 audit artifact 只保留为本地证据;只有泛化后的 typed contract 与 public-safe 结论可以进入仓库。 +### 2.2 Bitter-Lesson 边界:原始产物重置与受治理控制面 + +随着前沿编码模型在自主文件系统检索与自编探针脚本上持续增强,厚重的手工上下文压缩与多层 +外置提示词容易带来净协议税($\Delta J(B \mid s_0) \le 0$)。在**可重置的 episode 型、结构清晰、 +且具备稠密或连续隔离标量验证的任务**上,仅保留原始历史产物与标量分数、并在每轮间执行干净 +上下文重置(让 Agent 自行编写 Python / 文件系统检索脚本)的极简外循环,可以打平甚至超越 +有损手工摘要: + +- [Meta-Harness](https://arxiv.org/html/2603.28052v1) 让外层 proposer 选择性检索此前的源码、 + 分数与执行轨迹。TerminalBench-2 搜索中,proposer 每轮读取文件数的中位数为 `82` + (附录 A.1);另一个量是单次产物评估**最多可生成 `10M` tokens 的诊断资料**(§1、表 1), + 并非实测的 proposer 每轮消费量。分类准确率比 ACE 高 `7.7` 分且上下文 token 少 `4x`; + 找到的 Opus 4.6 harness 在 TerminalBench-2 达到 `76.4%`。系统仍保留提出候选、评估、 + 保存日志的外循环,不能据此认定仅靠标量分数或移除全部 harness 机制就能取得这些收益。 +- [Vercel `d0`](https://vercel.com/blog/we-removed-80-percent-of-our-agents-tools) 表明, + 在结构清晰的语义文件系统上删去约 80% 自定义工具(`17 -> 2`:`bash` 与 SQL),使成功率从 + `80%` 升至 `100%`,同时减少 `37%` token 并将延迟降低 `3.5x`。 +- [AlphaEvolve](https://arxiv.org/abs/2506.13131) 在算法代码发现上的成功,恰恰依赖固定不变的 + 外层循环与确定性程序 verifier,而不允许求解器改写自身 harness 或评分契约。 +- LoopX 已发布的上下文研究([上下文交付研究](https://loopx-project.github.io/loopx/blog/context-delivery/) + 与 [EdgeBench 反馈与记忆研究](https://loopx-project.github.io/loopx/blog/edgebench-feedback-and-memory/)) + 同样表明,冗余序列化与见顶后的重复 replan 循环会在原始代码检查与本地探针即可解决的场景下消耗大量 + uncached 上下文。 + +然而,实证研究确立了**五类失败机制**,证明原始产物、标量奖励与干净重置无法替代受治理控制面: + +1. **Goodhart 与 verifier 篡改:** 当求解器与评分器共享进程、文件系统、网络或密钥边界时, + 缺乏防护的优化会利用捷径通道——实证见于 `LHTB 2.0`(在冻结 agent 进程并隔离 verifier 之前, + 早期 `14/17` 个满分属于评分器泄漏)、[Reward Hacking Benchmark](https://arxiv.org/abs/2605.02964) + (13 个前沿模型利用漏洞比例为 `0%` 至 `13.9%`,其中 `72%` 带有显式思维链合理化,且在任务复杂度 + 超过诚实求解阈值后生产对齐保护失效)、[SWE-Marathon](https://arxiv.org/abs/2606.07682) + (观测到 `13.8%` rollout 存在 reward hacking)、[Darwin Gödel Machine](https://arxiv.org/abs/2505.22954) + (Node 114 伪造验证日志),以及 [METR reward-hacking 评估](https://metr.org/blog/2025-06-05-recent-reward-hacking/)。 + Agent 容器内的 Python 探针无法提供 OS/容器级 verifier 隔离与 `benchmark_integrity_audit_v0`(§6.5、§7.1)。 +2. **分布外(OOD)过拟合与 harness 膨胀:** 在原始产物上做无正则进化会死记训练轨迹并导致 OOD 退化。 + [RRSI](https://arxiv.org/abs/2609.24972) 发现未正则的 Meta-Harness 在 OOD 仅提升 `+0.9` + (`40.6` vs `39.7`),`TTHE` 甚至下降 `-1.7`(`38.0` 并膨胀出 `8,500` 词提示词),而加入 critic + 剪枝与退火预算正则化后 OOD 升至 `43.6`(`+3.9`)且策略 token 减少 `30%`; + [Harness Updating Is Not Harness Benefit](https://arxiv.org/abs/2605.30621) 同样显示 harness 修改 + 主要提升中等模型,在缺少 OOD 门控时前沿模型会饱和或回退。连续有效统计推断、留出集隔离及保留 + 决策差异的率失真记忆由本计划协同 [前沿科学研究计划](./frontier-science-research-program-v0.zh-CN.md#2-问题与动机) + (第 2 节与第 5 节 `T01`–`T07`)共同治理。 +3. **过早终止与稀疏 / 二值终局 verifier:** 本地文件重读无法阻止模型在可见冒烟测试通过后立即宣布完成, + 也无法从 `0/1` 终局奖励中提取中间梯度。在 LoopX 已发布的 `SWE-Marathon` 研究 + ([`benchmark/swe-marathon/README.md`](../../../benchmark/swe-marathon/README.md))中, + `zstd-decoder` 任务上 `plain` Codex 在可见测试通过后早停(partial `0.721`,reward `0`),而受控 + `heartbeat` 持续推进协议自测与内存安全验证(partial `1.000`,reward `1`);在 `LHTB` `covshift` + ([EdgeBench 长程修复研究](https://loopx-project.github.io/loopx/blog/edgebench-long-horizon-repairs/))中, + 默认完成判断在第 38 分钟退出(`0.000`),而由 owner 锁定的验收门控将其推至 `0.82`(五臂 `heartbeat` 为 + `0.743`)。外部完成拒绝、`stopped_recovery.py` 与隐藏覆盖压力仍然不可或缺。 +4. **代理指标偏移(Proxy Misalignment)与尾部 TLE 悬崖:** 未校准的本地 yardstick 与均值指标会倒挂 + 原生排名或触碰超时悬崖。LoopX 的 [`benchmark/proxy_order.py`](../../../benchmark/proxy_order.py) 分析 + ([行为发现说明](../../reference/benchmark-behavior-findings.md#compare-a-local-proxy-with-released-scores)) + 展示了本地代理排序与原生接受顺序的反转;[DuoDiscover 表 4](https://arxiv.org/html/2610.17184v1#S4.T4) + 区分 SWE-fficiency 加速比(`x`)与 SOL-Exec 分数(`% SOL`):在目标选择上加入未检验的 + yardstick 后,`12.71x / 69.68%` 降至 `10.33x / 66.91%`;相邻下一行加入保真度检验后, + 达到 `13.25x / 72.57%`。再加入 co-discovery 为 `13.76x / 74.43%`,加入 orchestrator 后才是 + 完整系统的 `14.93x / 80.80%`,不能把最终结果全部归给保真度门控。另有 `41%`–`45%` 的 + yardstick 随代码演进失效;而 + [LangChain 的 Terminal-Bench 2.0 研究](https://www.langchain.com/blog/improving-deep-agents-with-harness-engineering) + 显示,在缺乏分阶段推理深度控制时,统一使用 `xhigh` 推理深度因超时悬崖从 `63.6%` 跌至 `53.9%`。 + 受治理的 yardstick 校准与尾延迟压力门控是原生优化的必要保护。 +5. **有状态依赖 DAG、覆盖台账与 workspace / 结算不变量:** 多单元依赖前沿、非 episode 持续世界、 + linked-worktree 补丁归一化与幂等结算无法靠容器内的 Agent 脚本自行修复。[LoopsBench](https://arxiv.org/abs/2608.00267) + (`112` 个任务、`>5,300` 个依赖 DAG 单元)使当前最强外循环 SOTA 仍停留在 `25.00%`;一组 + `EdgeBench` `New Foundations Consistency` 的 Astra max、`18h` 五臂研究中,带声明台账和方向 + 复盘的 Explore 达到 `92.3419`,高于 Official 与 Resume 的 `91.9412` 平台 + ([报告源码](https://github.com/loopx-project/loopx/blob/d0ca48db02846be61a9b683f0efcd5c706381b66/apps/presentation/site/public/blog/zh/edgebench-feedback-and-memory/index.html#L63))。 + 这是组合 profile 的单次描述性证据,不是台账的独立因果效应,也不是 C2/C3 结果; + 实际评测基建同样依赖宿主侧不变量,例如 [`workspace_delivery.py`](../../../benchmark/deepswe-gptxhigh-v1/workspace_delivery.py) + (将 linked worktree 改动归一化回 `/app`)、[`frontiercs2/README.md`](../../../benchmark/frontiercs2/README.md) + (`v2.patch` 的 tmpfs `docker exec` 流式上传与持久准入恢复),以及 + [`widesearch/pier/README.md`](../../../benchmark/widesearch/pier/README.md)(`$HOME` 绑定挂载、纯数值 + `reward.json` 校验与 `provider_credential_shell_excluded`)。 + ## 3. Benchmark 介绍与适配分析 本节的 benchmark 数量和 leaderboard 值是 2026-08-16 审阅时的信息快照,不是规范。 @@ -291,6 +366,84 @@ DeepSWE pass rate 平均成一个 LoopX 数字。 六个 benchmark 仍须在各自主语空间报告;StartupBench rubric、LoopsBench verifier 分层与 WideSearch Item F1 不得互相平均,也不得并入 ALE/LHTB/DeepSWE 的结果。 +### 3.8 外部 Benchmark 调研与优先级排序(2026-11 更新) + +本次 2026-11-15 调研更新 2026-08 的六基准组合入门(§3.1–§3.7),并为后续集成排序 +(§9、§11)覆盖 EdgeBench 及 20 个外部 benchmark。排序依据四项实证标准:(1)相对纯基模 +scaling 仍保留的前沿区分度与上升空间;(2)verifier 确定性、抗 Goodhart 能力以及是否具备 +连续或多单元进度信号;(3)对第 2.2 节五类受治理失败机制的直接压力测试;(4)仓库就绪度 +(`benchmark/` 目录现有 `177` 个 `.py` 文件、共 `52,031` 行:`tests` `27,338` 行、`runtime/` +`9,082` 行、`edgebench/` `6,899` 行、`deepswe*` `4,433` 行、`swe-marathon/` `1,430` 行、 +`frontiercs2*` `1,285` 行、`LHTB/` `596` 行、`widesearch/` `592` 行、根目录辅助 `376` 行)与 +Linux x86\_64 Docker、GPU 及云资源约束之间的权衡。 + +**扩大多臂对照前的共同前置条件:** 在大规模扩展新的 Harbor、云端或 GPU benchmark 多臂 +对比前,应先将各适配器收敛至统一的 `benchmark/runtime/` Turn/Harbor 入口,对齐原生 +Stop-hook 基线对等性,并完成 E0–E2 生命周期、完整性与可计分门控(§11.2),避免继续复制 +烟囱式独立 runner。 + +#### 优先级矩阵 + +| 优先级 | Benchmark | 任务规模与时长 | 奖励与 Verifier 形态 | 前沿余量与机制信号 | 工程就绪度与平台约束 | +|---|---|---|---|---|---| +| **P0** | **EdgeBench** | 连续算法优化(`12h–18h` 长程运行) | 确定性在线冷却裁判;任务原生标量指标(按任务最大化或最小化) | 仅作按任务区分的描述性信号:`New Foundations 92.3419`(单次五臂研究)、`ANN 7,159 QPS`(另一组 `18h` 研究);其他任务比较中 Official 领先。这些结果不是合并后的等预算帕累托证据,来源边界见下文。 | **已就绪并已评测**(`edgebench/` `6,899` 行);需 **Linux x86\_64 Docker + SForge `iptables`**(macOS Docker socket 缺少网络边界) | +| **P0** | **FrontierCS-2** | `48` 公开 / `150` 总计(`13` 个领域);`5h`,限 `3–100` 次 train 调用 | 确定性隐藏工作负载 Driver/Judge;二值 `PASS`(须在*全部*指标与负载上胜过参考解)+ `FCI`/`FDI` 标量 | 12 个参评 agent 中最高通过率 **`58%`**,**`16/48` 零通过**;直接检验代理指标校准与抗刷分能力 | **单机桥接已就绪**(`frontiercs2*` `1,285` 行、`v2.patch` 传输修复;冻结 v1 `batch_local_counting` 归档为 `score_countable=false`、`integrity_qualified=false`;尚无有效 LoopX 计分结果);**强制要求 Linux x86\_64 Docker**(macOS ARM Rosetta/QEMU 下 seccomp 失败);官方 EKS+Modal 全测约 `$1,400`/48 题 | +| **P0** | **LHTB / LHTB 2.0** | `v1: 46`(`90m`);`v2.0: 77`(`46` 原题 + `31` 重构题,`3h` 含 `30m` 检查点) | 连续标量 `[0,1]` + 6 级阶梯(`R>=0.95`/`1.0`)+ 冻结进程 Harbor 二值拒止循环 | `v1` LoopX `0.4948` vs Goal `0.4475` / Plain `0.4218`(非等预算);`v2.0` 最高 `0.692`(`37/77`);早期 `14/17` 满分泄漏实证了 verifier 隔离必要性 | **`v1` Harbor 适配器已就绪**(`LHTB/` `596` 行);**`LHTB 2.0` 尚无代码**;非 GPU Docker / Daytona | +| **P0** | **LoopsBench** | `112` 个依赖 DAG 任务(`>5,300` 单元,`8` 种语言) | 沿就绪 DAG 前沿释放的确定性单元测试 + 回归测试(`incomplete` / `partial` / `complete`) | 最强带外循环 SOTA 仅 **`25.00%`**(`Opus-4.7` + Claude Code + 外循环);专为外循环 DAG 与回归不变量设计 | **仓库暂无活跃代码**(§3.5/§9.5 规划中);非 GPU,建议 Linux x86\_64 Docker | +| **P1** | **SWE-Marathon** | `20` 超长程任务(仓内已评 `15` 题 v1.1 子集);平均 `27.2M tok`/次 | 多层对抗式 verifier;二值求解 + 连续 `partial_score` | `v1` 前沿通过率 `<30%`,**`13.8%` rollout 出现 reward hacking**;仓内 `30%` 预算实验 `heartbeat 0.333 (5/15)` vs `Plain/Goal 0.267 (4/15)` | **已就绪并已评测**(`swe-marathon/` `1,430` 行);`amd64` 多语言 Docker;token 与墙钟成本高(每组 15 题 `1,592m–2,375m`) | +| **P1** | **PaperBenchX** *(优先于 PaperBench)* | `93` 篇论文(`12` 方向 / `6` 个非 ML 学科,`>3,000` 条细则);`4–24h`(中位 `7h`) | 从零干净重放 + 确定性检查与共享 LLM-Judge(`>=90%` 为完整复现) | 细则均分 `27.53–59.08%`,**完整复现仅 `14%`**(`>=90%`)——比旧版 `PaperBench`(`20` 篇 ICML'24 论文、`24–36h`、SOTA `26.0%`)学科更广且验证缺口更锐利 | **仓库 `0` 代码**(约 `2–4` 周适配);需重型 Linux Docker + 专业科学软件栈(`HFSS`、`Meep`、`GPU4PySCF`、`Habitat`)+ Judge API 密钥隔离 | +| **P1** | **ALE (Agents' Last Exam)** *(区别于 `ALE-Bench`)* | `1,500+` 分类体系(`v1.1: 122` 题:`119` CPU / `3` GPU、`78` Docker);多小时 VM | 运行后注入隐藏参考并执行 `evaluate()`,标量 `[0,1]` | 最难层级完整通过率 `<1%`;检验跨 CLI+GUI 专业工作迁移 | **仓库暂无活跃代码**(§3.1/§9.1 规划中,约 `2–4` 周适配);强依赖 **GCP/GCE VM 与 CUA MCP**(无法在 macOS 本地直接运行) | +| **P1** | **WideSearch** | `200`(`100 EN + 100 ZH`,`18` 个行业);`44+` 网页 / 人类均值约 `2.3h` | 自动化原子表格评分(`Item F1` / `Row F1` + 二值 `SR`);存在实时网页漂移非确定性 | 前沿 `SR <= 5.1%`(`o3` 多智能体 `5.1%`、单智能体 `4.5%`、`Gemini 2.5 Pro 4.3%`);高适配扇出/完备性 harness,迭代 RSI 适配度低至中 | **适配器与 `pier` 模板已就绪**(`widesearch/` `592` 行,尚无实跑记录);需 Linux/云端 `$HOME` 挂载(macOS `/tmp` 在 Colima 下失效)及 `permitted_solving` 网络 | +| **P1** | **KernelBench / AlgoTune** | `KernelBench`: `250` PyTorch/CUDA 算子(`L1–L3`+`L4`);`AlgoTune`: `154` 个 SciPy/sklearn/CVXPY 函数 | 确定性正确性校验 + GPU(`fast_p`)或 CPU 几何平均加速比 | `KernelBench` `<20% fast_1`;`AlgoTune` `AlgoTuner 1.72x`(前沿约 `2.05x`);纯客观加速比适合进化/RLVR 循环 | **仓库 `0` 代码**;`KernelBench` 强制依赖 **NVIDIA CUDA/Triton GPU Linux/Modal**(Mac 无法运行);`AlgoTune` 需**固定绑核的独占多核 Linux x86\_64** 计时宿主机 | +| **P2** | **ALE-Bench (SakanaAI)** *(区别于 `ALE`)* | `40` 道 AtCoder 启发式竞赛题(`C++/Rust/Py`,`300+` 用例);`150` 步迭代 | 确定性相对启发式得分 | 单发相对迭代反馈差距显著;跨题稳定性仍落后人类高手 | **仓库暂无活跃代码**;重型多核 Linux/AWS Docker;上游赛题协议禁止打包私有测试种子与输入 | +| **P2** | **StartupBench** | `97` 交付物任务(`DOCX/XLSX/PPTX/PDF`,`6` 域,均 `25.3` 细则) | Evidence view + 加权 Agent-as-Judge `0–100`(`>=90` 通过,`92%` 人类一致性) | 最高均分 `Kimi-k3 73.67`,最高 `pass@1` `GPT-5.6-sol 31.27%`;RSI 适配度低至中(开放交付细则) | **仓库暂无活跃代码**(§3.4/§9.4 规划中);多轮办公产物 API + Judge 边界;非 GPU | +| **P2** | **MLE-bench** | `75` 场 Kaggle 竞赛(`22` Lite `158 GB`,全量 `3.3 TB`);`24h` 标准(榜单于 `2026-04` 暂停) | 确定性 Kaggle 指标 + 奖牌阈值 | 主榜 SOTA `64.44±1.18%`;旧竞赛存在训练污染风险 | **仓库暂无活跃代码**;基础设施门槛极高(`36 vCPU`、`440 GB RAM`、`24 GB A10 GPU`、`3.3 TB` 磁盘、Kaggle 鉴权与 Git LFS) | +| **P2** | **tb-science** *(仅保留单次迁移样本;**排除出 RSI**)* | `96` 已合并任务 + `366` 开放 PR;Harbor 格式 | **100% 二值 `1/0` `reward.txt`**(agent `public` vs verifier `no-network`) | 部分 PR 前沿难度极高,但**不适合 RSI**(无每代连续梯度、无 train/held-out 划分、CI 锁死单轮预算) | **仓库 `0` 代码**(约 `1–2` 周 Harbor 接入);`ci_checks/check-task-timeout.sh` 硬锁 **`28800s`(`8h`)**;建议 Linux x86\_64 Docker | +| **仅作对照 / 降级** | **DeepSWE** | `113` 原创任务(`91` 仓库,`5` 语言) | 手写行为 verifier(`0/1` 二值,`1.4%` judge 分歧率) | `v1.1` 在前沿三模型间呈现约 **`74%` 并列趋近饱和**;仅保留作回归与基线对照 | **已就绪**(`4,433` 行含 `workspace_delivery.py`);需 Linux Docker 宿主机 worker;历史 v1 存在非等预算限制 | +| **仅作对照 / 降级** | **SWE-bench Pro** | `v1: 1,865`(`731` 公开);`v2: 642` + `HARD-51` | 补丁 + 单元测试(`0/1` 二值;`32.4%` judge 分歧率) | **`HARD-51` 已达 `98.0%`**——受基模 scaling 快速饱和 | **仓库暂无活跃代码**;`linux/amd64` Docker 镜像(`ghcr.io`),Mac ARM 非原生支持 | +| **仅作对照 / 降级** | **CORE-Bench** | `270`(`90` 论文,`3` 难度);经 HAL 运行 | 输出问答二值匹配 | 修复容差与脚手架缺陷后,Claude Opus 4.5 从 `21%` 升至 **`95%`**——已饱和 | **仓库 `0` 代码**;Docker/Azure(`Python`/`R`) | +| **仅作对照 / 降级** | **ScienceAgentBench** | `102`(`44` 论文);`30m` Docker 运行 | 有效执行 + 细则/代码检查 | `32.4%–42.2%`(`o1-preview`);单脚本范围限制长程控制面区分度 | **仓库 `0` 代码**;`30m` / 8 线程 Docker | +| **仅作对照 / 降级** | **BixBench** | `60` 胶囊 / `205` 问题(`v1.5`);`24–48h` 5 次重复 | 二值精确匹配 MCQ / LLM-Judge 开放问答 | 开放问答 `17%`(早期基线,缺 2026 前沿 SOTA);5 副本二值评估成本高 | **仓库 `0` 代码**;高昂多副本 API 成本 | +| **仅作对照 / 降级** | **FrontierScience** | `160` 问答项(`100` 奥赛 + `60` 研究) | 简答 + 10 分研究细则 | `GPT-5.2` 奥赛 `77%` / 研究 `25%`;无交互仓库与沙箱闭环 | **仓库 `0` 代码**;纯 API 推理 | +| **仅作对照 / 降级** | **RE-Bench** *(已废弃)* | `7` ML 研发任务(`2h–32h`) | 确定性 ML 评测(含历史时钟/内存作弊案例) | **上游已标废弃**(`METR/RE-Bench`);由 `FrontierCS-2`、`PaperBenchX` 与 `KernelBench` 替代 | **仓库 `0` 代码**;需 Linux x86\_64 + NVIDIA CUDA GPU | + +EdgeBench 数字来自[不同的已报告研究](https://github.com/loopx-project/loopx/blob/d0ca48db02846be61a9b683f0efcd5c706381b66/apps/presentation/site/public/blog/zh/edgebench-feedback-and-memory/index.html#L43)。 +New Foundations 观察使用 Astra max;ANN `18h` 研究使用 Sol/xhigh,约束为 +`Recall@10 >= 0.95`。报告中的反例包括 Portfolio(`Official 53.87` 对 Explore `31.19`,越高越好) +及 Historical Grid(`Official 1,967` 对 Context on + Stride `2,088`,越低越好) +([ANN 与反例来源](https://github.com/loopx-project/loopx/blob/d0ca48db02846be61a9b683f0efcd5c706381b66/apps/presentation/site/public/blog/zh/edgebench-feedback-and-memory/index.html#L131))。 +各任务保留自己的模型、起点、反馈权限、观察窗口和完整成本覆盖。这些观察支持选题; +C2/C3 或等预算帕累托主张仍须满足 §5.1/§5.3 的匹配比较与 §6.2 的完整成本记账。 + +#### 关键口径辨析与排除说明 + +1. **`ALE` 与 `SakanaAI/ALE-Bench` 区分(及不存在公开的 `"ALE Science"`):** + 公开文献与仓库中不存在名为 `"ALE Science"` 的独立 benchmark 或赛道。`ALE` + ([`rdi-berkeley/agents-last-exam`](https://github.com/rdi-berkeley/agents-last-exam)) + 是包含科学子域的广谱 CLI+GUI 专业工作集,而 `ALE-Bench` + ([`SakanaAI/ALE-Bench`](https://github.com/SakanaAI/ALE-Bench))面向 AtCoder + 启发式算法竞赛优化。两者在上表分别归入 `P1` 与 `P2`。 +2. **`tb-science` 排除出 RSI、仅留单次迁移样本的原因:** + 随着 `laude-institute` 迁移至 `harbor-framework`, + [`harbor-framework/terminal-bench-science`](https://github.com/harbor-framework/terminal-bench-science) + 成为独立于 `terminal-bench-2`(`89` 个通用任务,默认 `900s` 超时)的科学领域套件。尽管已有 `96` 个 + 已合并任务和 `366` 个开放 PR,**`tb-science` 不适合迭代式 RSI 循环**:CI + (`ci_checks/check-task-timeout.sh`)硬性锁定 `[agent] timeout_sec == 28800.0`(`8h`), + 终局 `reward.txt` 为 **100% 二值 `1/0`**(即使任务内部计算连续分),缺少 train/held-out 划分, + `[agent] network_mode` 为 `public`,且 `rubrics/task-implementation.toml` 明确拒绝开放式假说任务。 + 本计划仅保留高难度单次迁移样本(如 Lean 形式化 `#2530`、`#2197`、`#2653`、`#2629`,生物 + `#1518`,物理 `#2804` 或托管 GPU 控制 `#2531`)。 +3. **`PaperBenchX` 优先于旧版 `PaperBench`,及 `DeepSWE` 术语边界:** + `PaperBenchX`(`93` 篇跨学科非 ML 科学论文、从零干净重放)替代旧版 `PaperBench`(`20` 篇 + ICML'24 论文)成为 `P1` 主复现目标。本文中的 `DeepSWE` 指 Datacurve 的 `113` 题 benchmark + ([`datacurve-ai/deep-swe`](https://deepswe.datacurve.ai/)),不同于 Agentica 的 + `DeepSWE-Preview` 模型;其在 `74%` 附近的饱和趋势使新的 `C2`/`C3` 因果投入转向 `P0`/`P1`。 +4. **平台执行约束:** 勿在 macOS ARM64 Rosetta/QEMU 模拟下对依赖 Linux seccomp、`iptables`、 + CUDA 或多核稳定计时的套件(`EdgeBench`、`FrontierCS-2`、`KernelBench`、`AlgoTune`、 + `SWE-bench Pro`、`tb-science`)运行可计分对比;统一在固定配置的 Linux x86\_64(及所需专用 + CUDA/云主机)上执行,并避免在 Colima 上使用 macOS `/tmp` 绑定挂载(见 + `widesearch/pier/README.md`)。 + ## 4. 主张阶梯 每个公开结果必须声明其支持的最高主张等级。 @@ -343,7 +496,11 @@ overhead 与 authority budget,并通过非 benchmark product canary。只有 计划区分四种 arm: 1. **Native baseline**:benchmark 支持的 reference scaffold,或声明过的、没有 LoopX - 控制的 CLI harness。 + 控制的 CLI harness。在 **episode 型 RSI 与标量优化赛道**上,Governed LoopX 的 C2/C3 主张 + 必须包含一个匹配的 **Raw-Artifact Reset Baseline(原始产物重置基线)**,可以为本研究实跑, + 或按下述资格复用历史运行。它在每代干净重置上下文,保留此前求解器可见的全部产物与标量分数, + 由模型使用原生文件系统/Python 检索,不注入 LoopX packet 或治理台账(§2.2)。 + 这定义的是 treatment,不意味着执行成本为零。 2. **Passive LoopX**:worker decision 和 benchmark 合同完全相同,只增加只读 LoopX observation 与精简 settlement。 3. **Governed LoopX**:声明过的 LoopX profile 可以影响 continuation、checkpoint、 @@ -351,6 +508,19 @@ overhead 与 authority budget,并通过非 benchmark product canary。只有 4. **Mechanism ablation**:只让一个具名机制与 governed parent profile 不同,其他所有 可控字段保持一致。 +Raw-Artifact Reset Baseline 的资格要求: + +- 固定 §5.1 的比较 identity,并满足 §5.3,包括相同任务、模型 revision/effort、起点、环境与 + verifier、预算,以及反馈/工具/网络/权限范围。在观察结果前声明重置频率、停止规则和有意改变的 + treatment 条件。 +- “历史产物”仅指求解器在该次运行相应时点有权读取的材料。隐藏 reference、答案、verifier + 内部、仅操作者可见的诊断与事后评分材料继续不可读;文件被保留不产生访问权。 +- 只有固定输入、treatment、获准产物、完整性、成果和完整成本可独立读回,且足以证明同一匹配 + 比较时,才复用历史 arm。只有标量分数或 model family 标签不够;复用历史也不会让原成本归零。 +- 按 §6.2 计入检索/探针生成、上下文重置、归档/编排、模型使用、评测/确认、冷却、失败/重试及 + 人工介入。覆盖缺失保留 unknown。缺少合格基线、匹配或成本证据不完整时,保留描述性观察与 + 另有独立证据的 C0/C1 结果,不声称 C2/C3 增益或完整成本下的帕累托优势。 + assisted human 或 simulator intervention 是独立 study family,绝不能呈现为 autonomous leaderboard arm。 @@ -1304,6 +1474,7 @@ promotion 或修改 LoopX default。 | 2026-08-23 | 增加工程 readiness 阶梯与有界 delivery-slice 规则,将仓库建设与 C0--C4 研究主张分开跟踪。 | | 2026-08-25 | 把 StartupBench、LoopsBench 与 WideSearch 加入 benchmark primer 与集成计划,作为第二组互补 trio;每个 benchmark 必须在原生 metric space 报告,且 benchmark 材料不得进入可复用的 LoopX state。 | | 2026-10-16 | 将[反馈优先观察](https://github.com/loopx-project/loopx/blob/1d60e7edb2a05fe048767444f872934c3adef992/deprecate/benchmark-legacy/edgebench/feedback-first-observations-20261016.md)归入历史报告;运行器源码子集与配置投影修复仍属 E0/E1,混合处理、中断和进行中的观察不关闭 E2/E3,也不分离单一规划效应。通用 guard 重入放宽待受影响调用方独立验证。 | +| 2026-11-15 | 确立 Bitter-Lesson 边界与五类受治理控制面失败机制(§2.2),要求 episode 型 RSI / 标量优化赛道的 C2/C3 主张纳入匹配且保留完整成本记账的 Raw-Artifact Reset Baseline(新实跑或合格历史复用,§5.2),并采纳 2026-11 Benchmark 组合优先级排序(§3.8):**P0**(`EdgeBench`、`FrontierCS-2`、`LHTB / LHTB 2.0`、`LoopsBench`)、**P1**(`SWE-Marathon`、`PaperBenchX` 优先于 `PaperBench`、`ALE` 区别于 `ALE-Bench`、`WideSearch`、`KernelBench` / `AlgoTune`)、**P2**(`ALE-Bench`、`StartupBench`、`MLE-bench`、`tb-science` 仅保留单次迁移样本——因 CI 锁定 `28800s` 预算与 100% 二值 `reward.txt` 排除出 RSI),以及 **仅作对照 / 降级**(`DeepSWE`、`SWE-bench Pro`、`CORE-Bench`、`ScienceAgentBench`、`BixBench`、`FrontierScience`、已废弃 `RE-Bench`);多臂扩展以统一 `benchmark/runtime/` + Stop-hook E0–E2 就绪及 Linux x86\_64 / GPU 宿主机为前置门槛。 | ## 18. 参考资料 @@ -1312,11 +1483,13 @@ promotion 或修改 LoopX default。 - [Agents' Last Exam 评估框架](https://github.com/rdi-berkeley/agents-last-exam) - [Long-Horizon Terminal Benchmark 仓库](https://github.com/zli12321/LHTB) - [LHTB benchmark report](https://zli12321.github.io/LHTB/index.html) +- [LHTB 论文](https://arxiv.org/abs/2607.08964) - [DeepSWE 项目与 leaderboard](https://deepswe.datacurve.ai/) - [DeepSWE 论文](https://arxiv.org/abs/2607.07946) - [DeepSWE 运行指南](https://deepswe.datacurve.ai/run) - [StartupBench 项目](https://startupbench.github.io/) - [StartupBench 论文](https://startupbench.github.io/assets/StartupBench.pdf) +- [StartupBench arXiv](https://arxiv.org/abs/2608.17800) - [LoopsBench 项目](https://loopsbench.ai/) - [LoopsBench 仓库](https://github.com/microsoft/Loopsbench) - [LoopsBench 论文](https://arxiv.org/abs/2608.00267) @@ -1325,6 +1498,59 @@ promotion 或修改 LoopX default。 - [WideSearch 仓库](https://github.com/ByteDance-Seed/WideSearch) - [WideSearch 论文](https://arxiv.org/abs/2508.07999) - [WideSearch 数据集](https://huggingface.co/datasets/ByteDance-Seed/WideSearch) +- [EdgeBench 仓库](https://github.com/ByteDance-Seed/EdgeBench) +- [FrontierCS-2 项目](https://frontier-cs.org/) +- [FrontierCS-2 仓库](https://github.com/FrontierCS/FrontierCS-2) +- [FrontierCS-2 论文](https://arxiv.org/abs/2610.29932) +- [SWE-Marathon 仓库](https://github.com/abundant-ai/swe-marathon) +- [SWE-Marathon 论文](https://arxiv.org/abs/2606.07682) +- [PaperBenchX 仓库](https://github.com/UniPat-AI/PaperBenchX) +- [PaperBenchX 论文](https://arxiv.org/abs/2610.39744) +- [PaperBench 参考实现](https://github.com/openai/frontier-evals/tree/main/project/paperbench) +- [PaperBench 论文](https://arxiv.org/abs/2504.01848) +- [KernelBench 仓库](https://github.com/ScalingIntelligence/KernelBench) +- [KernelBench 论文](https://arxiv.org/abs/2502.10517) +- [AlgoTune 仓库](https://github.com/oripress/AlgoTune) +- [AlgoTune 论文](https://arxiv.org/abs/2507.15887) +- [ALE-Bench (SakanaAI) 仓库](https://github.com/SakanaAI/ALE-Bench) +- [ALE-Bench 论文](https://arxiv.org/abs/2506.09050) +- [MLE-bench 仓库](https://github.com/openai/mle-bench) +- [MLE-bench 论文](https://arxiv.org/abs/2410.07095) +- [Terminal-Bench Science 项目](https://www.terminal-bench-science.ai/) +- [Terminal-Bench Science 仓库](https://github.com/harbor-framework/terminal-bench-science) +- [SWE-bench Pro 仓库](https://github.com/scaleapi/SWE-bench_Pro-os) +- [SWE-bench Pro 论文](https://arxiv.org/abs/2509.16941) +- [CORE-Bench 仓库](https://github.com/siegelz/core-bench) +- [CORE-Bench 论文](https://arxiv.org/abs/2409.11363) +- [ScienceAgentBench 仓库](https://github.com/OSU-NLP-Group/ScienceAgentBench) +- [ScienceAgentBench 论文](https://arxiv.org/abs/2410.05080) +- [BixBench 仓库](https://github.com/Future-House/BixBench) +- [BixBench 论文](https://arxiv.org/abs/2503.00096) +- [RE-Bench(已废弃)仓库](https://github.com/METR/RE-Bench) +- [RE-Bench 论文](https://arxiv.org/abs/2411.15114) +- [Meta-Harness 论文](https://arxiv.org/abs/2603.28052) +- [Meta-Harness 仓库](https://github.com/stanford-iris-lab/meta-harness) +- [RRSI 论文](https://arxiv.org/abs/2609.24972) +- [RRSI 仓库](https://github.com/google-research/rrsi) +- [Harness Updating Is Not Harness Benefit](https://arxiv.org/abs/2605.30621) +- [Agentic Harness Engineering (AHE)](https://arxiv.org/abs/2604.25850) +- [Self-Harness](https://arxiv.org/abs/2606.09498) +- [Reward Hacking Benchmark (RHB)](https://arxiv.org/abs/2605.02964) +- [DuoDiscover 论文](https://arxiv.org/abs/2610.17184) +- [AlphaEvolve 论文](https://arxiv.org/abs/2506.13131) +- [Darwin Gödel Machine (DGM) 论文](https://arxiv.org/abs/2505.22954) +- [Darwin Gödel Machine 仓库](https://github.com/jennyzzt/dgm) +- [LangChain: Improving Deep Agents with harness engineering](https://www.langchain.com/blog/improving-deep-agents-with-harness-engineering) +- [Vercel `d0`: We removed 80% of our agent's tools](https://vercel.com/blog/we-removed-80-percent-of-our-agents-tools) +- [METR: Recent frontier models are reward hacking](https://metr.org/blog/2025-06-05-recent-reward-hacking/) +- [Anthropic: Demystifying evals for AI agents](https://www.anthropic.com/research/demystifying-evals-for-ai-agents) +- [Anthropic: Effective harnesses for long-running agents](https://www.anthropic.com/research/effective-harnesses-for-long-running-agents) +- [Anthropic: Harness design for long-running application development](https://www.anthropic.com/research/harness-design-long-running-apps) +- [LoopX:上下文交付研究](https://loopx-project.github.io/loopx/blog/context-delivery/) +- [LoopX:EdgeBench 反馈与记忆研究](https://loopx-project.github.io/loopx/blog/edgebench-feedback-and-memory/) +- [LoopX:EdgeBench 长程修复研究](https://loopx-project.github.io/loopx/blog/edgebench-long-horizon-repairs/) +- [前沿科学研究计划 v0](./frontier-science-research-program-v0.zh-CN.md) +- [探索性 benchmark 行为发现](../../reference/benchmark-behavior-findings.md) - [Benchmark 研究工作区](https://github.com/huangruiteng/loopx/blob/main/benchmark/README.md) - [DeepSWE 研究实践](https://github.com/huangruiteng/loopx/blob/main/benchmark/deepswe/README.md) - [旧 Benchmark 历史](https://github.com/loopx-project/loopx/tree/54d42da44283bb414b24f71d16642c25cbcaf5dd/deprecate/benchmark-legacy) diff --git a/docs/research/README.md b/docs/research/README.md index 8d4d12eb8e..c32aa3e7b9 100644 --- a/docs/research/README.md +++ b/docs/research/README.md @@ -27,6 +27,13 @@ establish an equal-budget efficiency gain or a general performance guarantee. ## Research Index +- [Benchmark portfolio priority ranking and Bitter-Lesson boundary (2026-11)](../architecture/rfcs/long-horizon-harness-benchmark-research-program-v0.md#38-external-benchmark-survey-and-priority-ranking-2026-11) + ([中文](../architecture/rfcs/long-horizon-harness-benchmark-research-program-v0.zh-CN.md#38-外部-benchmark-调研与优先级排序2026-11-更新)): + updated P0/P1/P2/Control-Only ranking across EdgeBench and 20 external + benchmarks, engineering readiness and platform constraints, and the + Raw-Artifact Reset Baseline contract requiring matched, fully costed evidence + for causal claims while separating self-authored retrieval from the five + governed control-plane failure regimes. - [Budgeted research planning: goals, theory and design rationale](budgeted-research-planning.md) ([中文](budgeted-research-planning.zh-CN.md)): why conditional plans, costly measurements and causal comparisons serve better long-budget outcomes; links From 57807a974f4c5a4632a5d7f3f8772c192f98e109 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:42:05 +0800 Subject: [PATCH 23/34] fix(cli): preserve Goal source routing in runtime conflict guidance (#8866) Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../protocols/global-manager-command-v0.md | 6 ++++ loopx/paths.py | 10 +++--- .../test_runtime_projection_explicit_route.py | 35 +++++++++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/docs/reference/protocols/global-manager-command-v0.md b/docs/reference/protocols/global-manager-command-v0.md index 5c88889837..30c38bac06 100644 --- a/docs/reference/protocols/global-manager-command-v0.md +++ b/docs/reference/protocols/global-manager-command-v0.md @@ -104,6 +104,12 @@ Request rules: ## Source Reads +If both default runtime roots contain state, select the intended global +registry by path with `--registry`. When that registry declares a runtime, +use the registry alone so each Goal can route to its registered source. +An explicit `--runtime-root` pins Goal authority lookup to a single directory. +Use that override for diagnostics that intentionally inspect one runtime. + Implementations may read only compact LoopX control-plane surfaces: - global registry and project-local registry entries; diff --git a/loopx/paths.py b/loopx/paths.py index 9a5a41cf5a..84dc5eb3cd 100644 --- a/loopx/paths.py +++ b/loopx/paths.py @@ -236,8 +236,9 @@ def default_runtime_route() -> dict[str, object]: recommended_action = None if status == "conflict": recommended_action = ( - "Select one registry with --registry/--runtime-root; " - "inspect both roots before migration." + "Use --registry alone with a concrete registry file that declares its runtime " + "to follow the registered Goal sources. Use --runtime-root for an explicit " + "single-runtime operation. Inspect both roots before migration." ) elif status == "invalid": recommended_action = ( @@ -271,10 +272,7 @@ def select_default_runtime_root() -> Path: if route["target_registry_exists"] and route["legacy_registry_exists"] else "Both default LoopX runtime roots contain state." ) - raise ValueError( - f"{conflict} Select an explicit --registry and " - "--runtime-root; resolve the route conflict before using implicit defaults." - ) + raise ValueError(f"{conflict} {route['recommended_action']}") if route["status"] == "invalid": raise ValueError(str(route["recommended_action"])) return Path(str(route["selected_runtime_root"])) diff --git a/tests/control_plane/test_runtime_projection_explicit_route.py b/tests/control_plane/test_runtime_projection_explicit_route.py index eaf9b56171..f011810cd8 100644 --- a/tests/control_plane/test_runtime_projection_explicit_route.py +++ b/tests/control_plane/test_runtime_projection_explicit_route.py @@ -137,6 +137,41 @@ def unexpected_read(): ) == [root] +def test_default_conflict_guides_status_to_each_registered_goal_source( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys, +): + current, _legacy = _conflicting_default_roots(tmp_path, monkeypatch) + registry, _source_runtime, _state = promoted_create_fixture(tmp_path / "source") + source_payload = json.loads(registry.read_bytes()) + source_payload["goals"][0].update( + domain="software", adapter={"kind": "read_only_project_map_v0", "status": "connected-read-only"}, + ) + registry.write_text(json.dumps(source_payload), encoding="utf-8") + global_registry = _declare_target(current, registry) + global_payload = json.loads(global_registry.read_bytes()) + global_payload["common_runtime_root"] = str(current) + global_registry.write_text(json.dumps(global_payload), encoding="utf-8") + monkeypatch.delenv("LOOPX_REGISTRY", raising=False) + monkeypatch.chdir(tmp_path) + + args = ["--format", "json", "status", "--goal-id", DEFAULT_CONFLICT_GOAL] + with pytest.raises(SystemExit, match="Both default LoopX runtime roots") as conflict: + main(args) + assert main(["--registry", str(global_registry), *args]) == 0 + status = json.loads(capsys.readouterr().out) + assert status["ok"] is True and status["contract"]["errors"] == [] + + assert main(["--registry", str(global_registry), "--runtime-root", str(current), *args]) == 1 + forced = json.loads(capsys.readouterr().out) + assert forced.get("error_code") == "canonical_authority_required", forced + assert forced["authority_read"]["goal_id"] == DEFAULT_CONFLICT_GOAL + + advice = paths.default_runtime_route()["recommended_action"] + assert "Use --registry alone" in advice + assert "explicit single-runtime" in advice + assert str(conflict.value).endswith(advice) + + @pytest.mark.parametrize("missing_path_key", ["json_path", "markdown_path"]) def test_route_projection_with_missing_artifact_is_not_current( tmp_path: Path, From 6cb54995171fa18cb32db3c3891fcb155292f916 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:42:32 +0800 Subject: [PATCH 24/34] Keep saved research cases in place across desktop reopening (#8868) * Defer original research history when opening a saved case Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> (cherry picked from commit 75d819552c099b52e517538ddf8d957104500716) * Preserve scoped case reading when reopening the desktop workspace Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> (cherry picked from commit 61ec7db796f3aa70aec2305ea3b1a3161c433caf) --------- Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- apps/desktop/loopx-control-plane/README.md | 7 + .../src-tauri/src/workspace_location.rs | 231 +++++++++++++++++- .../goal-research-history.tsx | 19 +- 3 files changed, 246 insertions(+), 11 deletions(-) diff --git a/apps/desktop/loopx-control-plane/README.md b/apps/desktop/loopx-control-plane/README.md index dc3df5bb77..deac5411c4 100644 --- a/apps/desktop/loopx-control-plane/README.md +++ b/apps/desktop/loopx-control-plane/README.md @@ -449,6 +449,13 @@ directly in Research after a normal quit and a loopback-port change. Window close/reopen, an explicit return to Manager and a corrupt preference were also checked. It does not demonstrate model execution or an official App upgrade. +Within a local Goal's Research view, the preference also retains an explicit +case/history choice, selected registered analysis pack and chapter or citation +bookmark. The dashboard rechecks the pack and source when reopening. A missing +or inaccessible pack keeps its existing reader recovery path. The bookmark +must match the selected pack and the dashboard's chapter grammar. Existing +version-1 preferences without research selections continue to open as before. + ![Packaged QA reopening the same synthetic Goal in Research](docs/assets/research-reopen-qa.jpg) For native qualification, use a separately identified packaged App with the diff --git a/apps/desktop/loopx-control-plane/src-tauri/src/workspace_location.rs b/apps/desktop/loopx-control-plane/src-tauri/src/workspace_location.rs index 2a17e5ae6d..3171e602e3 100644 --- a/apps/desktop/loopx-control-plane/src-tauri/src/workspace_location.rs +++ b/apps/desktop/loopx-control-plane/src-tauri/src/workspace_location.rs @@ -15,6 +15,66 @@ struct LocalWorkspace { view: Option, work_agent: Option, todo: Option, + research: Option, +} + +/// Selection within the existing dashboard reader, scoped to this local Goal. +#[derive(Debug, PartialEq)] +struct ResearchReading { + mode: String, + pack_id: Option, + chapter: Option, +} + +fn digest_id(value: &str) -> bool { + value.len() == 64 + && value + .bytes() + .all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c)) +} + +impl ResearchReading { + fn selections(mode: &str, pack: &str, chapter: &str) -> Option { + if !matches!(mode, "case" | "history") { + return None; + } + let pack_id = digest_id(pack).then(|| pack.to_owned()); + // The frontend resolves this bookmark against the saved study. Carry + // only its declared chapter/citation grammar, never an arbitrary URL. + let chapter = pack_id.as_deref().and_then(|pack| { + let body = chapter.strip_prefix("case-section-")?; + let prefix = format!("{pack}-"); + let section = body.strip_prefix(&prefix).or_else(|| { + let (namespace, rest) = body.split_once('-')?; + digest_id(namespace).then_some(rest)?.strip_prefix(&prefix) + })?; + let (id, citation) = match section.split_once('~') { + Some((id, ordinal)) => (id, Some(ordinal)), + None => (section, None), + }; + let valid_id = id.len() <= 80 + && id + .bytes() + .all(|c| c.is_ascii_alphanumeric() || b"_-".contains(&c)) + && (!id.is_empty() || citation.is_some()); + let valid_citation = citation.is_none_or(|ordinal| { + ordinal.len() <= 2 + && !ordinal.starts_with('0') + && ordinal.bytes().all(|c| c.is_ascii_digit()) + && !ordinal.is_empty() + }); + (mode == "case" && valid_id && valid_citation).then(|| chapter.to_owned()) + }); + Some(Self { + mode: mode.to_owned(), + pack_id, + chapter, + }) + } + + fn value(&self) -> Value { + json!({"mode": self.mode, "pack_id": self.pack_id, "chapter": self.chapter}) + } } // Match the existing Goal instance ID and frontend Agent/Todo search boundary. @@ -44,6 +104,7 @@ impl LocalWorkspace { .then(|| view.to_owned()), work_agent: selection_id(agent), todo: selection_id(todo), + research: None, }) } @@ -71,7 +132,23 @@ impl LocalWorkspace { // authority. Reopening that unsupported context returns Manager. return Some(Self::default()); } - Self::selections(get("goalId"), get("view"), get("workAgent"), get("todo")) + let mut selection = + Self::selections(get("goalId"), get("view"), get("workAgent"), get("todo"))?; + if selection.view.as_deref() == Some("research") + && get("research_goal") == selection.goal_id + { + let pack = if get("research_analysis_goal") == selection.goal_id { + get("research_pack") + } else { + "" + }; + selection.research = ResearchReading::selections( + get("research_reading"), + pack, + current.fragment().unwrap_or(""), + ); + } + Some(selection) } fn destination(&self, origin: &Url) -> Url { @@ -90,7 +167,20 @@ impl LocalWorkspace { query.append_pair(key, value); } } + if let Some(research) = &self.research { + query.append_pair("research_goal", &self.goal_id); + query.append_pair("research_reading", &research.mode); + if let Some(pack) = &research.pack_id { + query.append_pair("research_analysis_goal", &self.goal_id); + query.append_pair("research_pack", pack); + } + } drop(query); + destination.set_fragment( + self.research + .as_ref() + .and_then(|research| research.chapter.as_deref()), + ); destination } @@ -98,17 +188,30 @@ impl LocalWorkspace { if value["version"] != 1 { return None; } - Self::selections( + let mut selection = Self::selections( value["goal_id"].as_str()?, value["view"].as_str().unwrap_or(""), value["work_agent"].as_str().unwrap_or(""), value["todo"].as_str().unwrap_or(""), - ) + )?; + if !selection.goal_id.is_empty() && selection.view.as_deref() == Some("research") { + let research = &value["research"]; + selection.research = ResearchReading::selections( + research["mode"].as_str().unwrap_or(""), + research["pack_id"].as_str().unwrap_or(""), + research["chapter"].as_str().unwrap_or(""), + ); + } + Some(selection) } fn value(&self) -> Value { - json!({"version": 1, "goal_id": self.goal_id, "view": self.view, - "work_agent": self.work_agent, "todo": self.todo}) + let mut value = json!({"version": 1, "goal_id": self.goal_id, "view": self.view, + "work_agent": self.work_agent, "todo": self.todo}); + if let Some(research) = &self.research { + value["research"] = research.value(); + } + value } } @@ -211,6 +314,124 @@ mod tests { format!("http://127.0.0.1:{port}/chat/").parse().unwrap() } + fn research_page(port: u16) -> Url { + let mut page = origin(port); + page.query_pairs_mut().extend_pairs([ + ("goalId", "example"), + ("view", "research"), + ("research_goal", "example"), + ("research_reading", "case"), + ("research_analysis_goal", "example"), + ("research_pack", &"a".repeat(64)), + ]); + page + } + + #[test] + fn research_reopen_keeps_registered_pack_and_chapter_or_citation_on_the_new_port() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("workspace-location.json"); + for namespace in [String::new(), format!("{}-", "b".repeat(64))] { + for section in ["turning-point", "turning-point~2", "~1"] { + let mut page = research_page(49123); + let chapter = format!("case-section-{namespace}{}-{section}", "a".repeat(64)); + page.set_fragment(Some(&chapter)); + let saved = LocalWorkspace::capture(&page, &origin(49123)).unwrap(); + write(&path, &saved).unwrap(); + let reopened = read(&path).unwrap().destination(&origin(59234)); + assert_eq!(reopened.port(), Some(59234)); + let reopened_query = reopened + .query_pairs() + .into_owned() + .collect::>(); + for (key, value) in page.query_pairs().into_owned() { + assert_eq!(reopened_query.get(&key), Some(&value)); + } + assert_eq!(reopened.fragment(), Some(chapter.as_str())); + let bytes = fs::read_to_string(&path).unwrap(); + assert!(!bytes.contains("49123") && !bytes.contains("http")); + assert_eq!(read(&path), Some(saved)); + } + } + } + + #[test] + fn research_selections_stay_with_their_goal_view_and_pack() { + let mut page = research_page(49123); + page.set_fragment(Some(&format!( + "case-section-{}-turning-point", + "a".repeat(64) + ))); + for (key, value) in [ + ("goalId", "other"), + ("view", "tasks"), + ("research_goal", "other"), + ] { + let mut changed = page.clone(); + let pairs = changed + .query_pairs() + .into_owned() + .map(|(k, v)| { + let v = if k == key { value.to_owned() } else { v }; + (k, v) + }) + .collect::>(); + changed.query_pairs_mut().clear().extend_pairs(pairs); + assert!(LocalWorkspace::capture(&changed, &origin(49123)) + .unwrap() + .research + .is_none()); + } + for pack in ["wrong", &"c".repeat(64)] { + let mut changed = page.clone(); + changed.query_pairs_mut().clear().extend_pairs([ + ("goalId", "example"), + ("view", "research"), + ("research_goal", "example"), + ("research_reading", "case"), + ("research_analysis_goal", "other"), + ("research_pack", pack), + ]); + let saved = LocalWorkspace::capture(&changed, &origin(49123)).unwrap(); + let research = saved.research.unwrap(); + assert!(research.pack_id.is_none() && research.chapter.is_none()); + } + let history = + ResearchReading::selections("history", &"a".repeat(64), page.fragment().unwrap()) + .unwrap(); + assert_eq!(history.mode, "history"); + assert!(history.chapter.is_none()); + assert!(ResearchReading::selections("retired", "", "").is_none()); + } + + #[test] + fn invalid_bookmarks_and_unrelated_preference_fields_are_not_restored() { + let pack = "a".repeat(64); + for chapter in [ + "https://example.org/secret".to_owned(), + format!("case-section-{}-chapter", "c".repeat(64)), + format!("case-section-{pack}-../secret"), + format!("case-section-{pack}-chapter~0"), + format!("case-section-{pack}-chapter~01"), + format!("case-section-{pack}-chapter~100"), + format!("case-section-{pack}-{}", "x".repeat(81)), + ] { + assert!(ResearchReading::selections("case", &pack, &chapter) + .unwrap() + .chapter + .is_none()); + } + let legacy = json!({"version": 1, "goal_id": "example", "view": "research"}); + let saved = LocalWorkspace::from_value(&legacy).unwrap(); + assert!(saved.research.is_none()); + let mut modified = legacy; + modified["research"] = json!({"mode": "case", "pack_id": pack, "chapter": "secret", "url": "https://example.org"}); + let restored = LocalWorkspace::from_value(&modified).unwrap(); + assert!(restored.research.as_ref().unwrap().chapter.is_none()); + assert!(!restored.value().to_string().contains("secret")); + assert!(!restored.value().to_string().contains("example.org")); + } + #[test] fn ordinary_reopen_keeps_selection_across_ports_without_replaying_other_inputs() { let old = origin(49123); diff --git a/apps/presentation/dashboard/src/features/personal-workspace/goal-research-history.tsx b/apps/presentation/dashboard/src/features/personal-workspace/goal-research-history.tsx index 6d5ca91119..3b2ff20e73 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/goal-research-history.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/goal-research-history.tsx @@ -67,6 +67,14 @@ export function GoalResearchHistory({goalId, originalGoalId = goalId, preferHist const [caseMode, setCaseReading] = useState(readCaseMode); const requestedCase = caseMode.goalId === goalId ? caseMode.requested : null; const caseReading = requestedCase ?? !preferHistory; + const hasCases = (analysis.snapshot?.catalog.packs ?? []).some(pack => pack.case_study_summary); + // Preserve the saved reading during catalogue loading or failure. A + // successful catalogue without a study falls back to the original history. + const awaitingRequestedCase = requestedCase === true && (analysis.loading || Boolean(analysis.readError)); + const deferHistory = requestedCase === true && (hasCases || awaitingRequestedCase); + const historySource = JSON.stringify([goalId, originalGoalId, readSourceUrl]); + const [openedHistorySource, setOpenedHistorySource] = useState(null); + const readHistory = active && (!deferHistory || openedHistorySource === historySource); useEffect(() => { const changed = () => setCaseReading(readCaseMode()); changed(); window.addEventListener("popstate", changed); @@ -200,7 +208,10 @@ export function GoalResearchHistory({goalId, originalGoalId = goalId, preferHist const updateRecords = (events: readonly ExploreHistoryEvent[]) =>
    {events.map(event =>
  1. {stateLabel(event.status)} · {event.title}

    {event.summary ? findingBody(event) : null}
    {event.parent_id ? {copy.group}: {event.parent_id} : null}{event.evidence_refs.map(ref => {ref})}{event.run_id ? {event.run_id} : null}
  2. )}
; useEffect(() => { - if (!active) return; + if (!readHistory) return; + // Once history has opened, switching back to the saved case should not + // cancel a page in flight or start the same history scan again. + setOpenedHistorySource(historySource); let current = true; const controller = new AbortController(); setBusy(true); setError(""); setLoaded(0); @@ -245,7 +256,7 @@ export function GoalResearchHistory({goalId, originalGoalId = goalId, preferHist } finally {if (current) setBusy(false);} })(); return () => {current = false; controller.abort();}; - }, [goalId, originalGoalId, active, revision, refreshRevision, readSourceUrl]); + }, [goalId, originalGoalId, readHistory, historySource, revision, refreshRevision, readSourceUrl]); const history = useMemo(() => { try { const canonical = projectExploreHistory(read.events, originalGoalId); @@ -518,10 +529,6 @@ export function GoalResearchHistory({goalId, originalGoalId = goalId, preferHist : null; const emptyHistoryMessage = zh ? "这个 Goal 尚无研究历程" : "No research history for this Goal yet"; - const hasCases = (analysis.snapshot?.catalog.packs ?? []).some(p => p.case_study_summary); - // Keep an explicitly opened study in place during a read failure. A complete - // catalogue with no study still uses the ordinary history view. - const awaitingRequestedCase = requestedCase === true && (analysis.loading || Boolean(analysis.readError)); const selectedStrategy = hasNativeSource && !emptySourceHistory ? strategies.selection : null; const readingCase = caseReading && (hasCases || awaitingRequestedCase) && (requestedCase === true || selectedStrategy === null); const alternateReading = readingCase ? From b9f2e2a516b58e62cbb0da6978625388322b5a90 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:46:45 +0800 Subject: [PATCH 25/34] docs(blog): qualify the EdgeBench Context ON/OFF snapshot (#8862) * docs(blog): qualify the reported Context ON/OFF scores Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> * docs(issue-fix): keep publication link labels on one line Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --------- Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../public/blog/context-delivery/index.html | 96 ++-- .../public/blog/data/long-horizon-audit.json | 30 +- .../stride-context-on-vs-off-en.svg | 501 +++++++++--------- .../stride-context-on-vs-off-zh.svg | 501 +++++++++--------- apps/presentation/site/public/blog/index.html | 2 +- .../blog/zh/context-delivery/index.html | 74 ++- .../site/public/blog/zh/index.html | 2 +- .../scripts/generate-long-horizon-audit.py | 253 +++++---- examples/blog-bilingual-index-smoke.mjs | 236 +++++++-- loopx/capabilities/issue_fix/README.md | 9 +- 10 files changed, 921 insertions(+), 783 deletions(-) diff --git a/apps/presentation/site/public/blog/context-delivery/index.html b/apps/presentation/site/public/blog/context-delivery/index.html index b97d221904..40cc7986dc 100644 --- a/apps/presentation/site/public/blog/context-delivery/index.html +++ b/apps/presentation/site/public/blog/context-delivery/index.html @@ -7,7 +7,7 @@
← All articles

Context delivery

An Agent needs a way to look things up, too

Read necessary facts first, reuse verified structures within a compaction cycle, and recover the complete baseline when continuity is lost.

-

Long-running work accumulates material: the original goal, later constraints, attempted routes, failed experiments and current progress. Sending everything again on each continuation repeats more and more content; keeping only a summary can make the original basis of a decision difficult to recover.

+

Long-running work accumulates material: the original goal, later constraints, attempted routes, failed experiments and current progress. Sending everything again on each continuation repeats more and more content; keeping only a summary can make the original basis of a decision difficult to recover.

Selective JSON Pointer lookup by itself is straightforward—it folds long-tail history not needed for the immediate decision. The core mechanism in LoopX's context delivery is compaction-cycle-bounded structured delta context: complete sources are saved first, unchanged typed subtrees are reused exactly against a verified baseline within the active context_epoch, and any host compaction or digest mismatch atomically resets to a fresh complete baseline rather than chaining dangling deltas.

Evidence: separate context comparisons, cycle deltas and task ablation

Four mechanisms, with separate measurement baselines

@@ -111,101 +111,99 @@

Eight tasks: single-layer payloads (Progr

Basis and source: the published eight task rows sum to 7,285,951 B / 22,408,690 B = 32.5% for Progressive across all 213 wakes. Delta and first-screen shares of approximately 35.8% and 3.0% / 5.0% cover only the 112 in-cycle wakes. Their Full denominator was published rounded as 12.53 MB; it is not the 213-wake total. This is an aggregate recalculation, not a new experiment or an independent audit of per-wake logs. Public aggregate data and provenance.

Implementation status: #8266 remains an open C1 proposal. The final merged scope of #8304 preserves precision while reusing ordering and a verified digest. These cohort bytes do not establish a shipped unified Controller/Worker gain.

-

Ten-task controlled comparison: Context ON vs Context OFF under fixed Explore + Stride

-

Cutting packet bytes only matters if long-horizon task performance holds up or improves. From the EdgeBench 10-task evaluation (2026-11-12 22:40 CST snapshot), we isolate the controlled two-arm contrast holding the same Explore + Stride harness fixed: Explore + context-on (progressive reading + compaction-cycle structured delta enabled) vs Explore + context-off (both disabled), testing only what switching Context Delivery on vs off changes within that chassis.

+

EdgeBench scores reported for Context ON and OFF

+

The November 12, 2026 snapshot (22:40 CST) brings together scores labeled Context ON and OFF on ten EdgeBench tasks. The recorded scores favor ON on 7 of 10 tasks and OFF on 3. The public record is missing per-arm evidence for the actual configuration, code revision, evaluator and budget across these pairs, so the tally cannot isolate the effect of Context Delivery.

    -
  • Ten-task tally (7 ahead or slight edge vs 3 OFF ahead): under fixed Explore + Stride, enabling Context Delivery puts Explore + context-on ahead of or slightly ahead of context-off on 7 of 10 tasks—including Borden (80 vs 45, +77.8%), ANN (7,159 vs 3,746 QPS, +91.1%), and Grid (2,197 vs 2,311 cost, −114)—while explicitly preserving all 3 counterexamples where context-off scores higher.
  • -
  • Strictly duration-matched ~18h pairs (4 tasks within the 15-minute tolerance): 3 wins to 1—Borden rises from 45 to 80 (+35 pts / +77.8%); Grid total cost (lower is better) improves from 2,311 to 2,197 (−114 / −4.9%, +2.35 normalized pts); Rust (both 17h57m) shows a slight native edge for context-on (27.1801 vs 27.1757); the matched 18h counterexample is Portfolio (both 18h00m, 13.62 vs 39.44, OFF wins).
  • -
  • Historical Stride slice & incomplete-duration pairs (6 tasks, runtimes stated explicitly): using the matching Stride pair from the historical seven-arm ANN cohort, context-on reaches 7,159 QPS vs context-off 3,746 QPS (+91.1%, +33.51 normalized pts); FFmpeg reaches 2.7086 vs 1.9929 (+35.9%, OFF stopped early pending rerun); VLIW cycles (lower is better) improve to 1,042 vs 1,097 (−55 cycles / −5.0%, OFF historical 14h45m); Ad reaches 49.67B vs 49.63B (ON 18h00m vs OFF 8h47m host-startup hang). The two remaining counterexamples are Codec (ON full 18h00m 68.89 vs OFF 70.79 in 12h38m despite a model-service block) and OpenTTD (both Explore arms <18h, ON 30,266,729 vs OFF 33,090,430).
  • +
  • Four pairs list runtimes close to 18 hours. Both listed durations fall within 15 minutes of 18 hours and within 15 minutes of each other. ON leads on Borden and Grid (lower cost is better). On Rust the native scores are 27.1801 and 27.1757, displayed as 27.18 on both sides of the /100 chart. OFF leads on Portfolio.
  • +
  • The other six include historical and incomplete runs. Four favor ON and two favor OFF (Codec and OpenTTD). The aggregate reports historical ANN at 7,159 vs 3,746 QPS. The earlier public ANN table shows 7,159 QPS, and leaves the 3,746 QPS value and both active durations unverified. The 6,020 and 5,766 QPS farther down belong to a different Explore pair without Stride. The older Grid results below also come from a different pair.
-

Reconciling two figures with the ablation section below: (1) this section's ANN pair (3,746 vs 7,159 QPS) comes from the two Stride-enabled Explore arms in the seven-arm cohort (Explore + Stride + context-off / on); #ablation-evidence separately lists the without-Stride Explore baseline (Explore · Context off 5,766 and Explore · Context on 6,020) from that study, so the two tables describe distinct arm pairs. (2) Grid here (2,197 vs 2,311) reports the completed ~18h pair within 15-minute tolerance (with context-on via same-session continuation), whereas #ablation-evidence retains the earlier unmatched-duration “Historical Grid” counterexample (Off 2054 < On+Stride 2088) for historical continuity.

-

EdgeBench 10-task controlled score comparison (fixed Explore + Stride: Context ON vs OFF)

EdgeBench 10-task controlled Explore + Stride Context ON vs OFF scores: 7 tasks ahead or slight edge (ANN +91.1%, Borden +77.8%, Grid −114), retaining Portfolio, Codec, and OpenTTD counterexamples
A: Nine tasks with normalized EdgeBench scores (/100, direction-normalized so higher is better), ordered by ON − OFF delta, showing arm runtime status. B: Native-point summary including task #10 Borden (no /100 scale, matched ~18h: 80 vs 45), key native deltas, and counterexample disclosures.
-
Expand the 10-task controlled score and runtime table -
+

Reported EdgeBench scores for Context ON and OFF

Reported Context ON and OFF scores. Panel A shows nine normalized pairs. Panel B shows Borden in native points and highlights Portfolio, Codec, and OpenTTD.
A shows nine pairs with /100 scores, ordered by ON minus OFF after direction is normalized (higher is better). B shows Borden in its native units and highlights the three tasks where OFF leads.
+
Expand the ten-task score and runtime table +
- - - - - + + + + + - - + + - + - - + + - + - - + + - + - - + + - + - - - - + + + + - - + + - - + + - - + + - + - - + + - + - - + + - + - - + + - - + +
Task (metric & direction)Duration parityExplore + context-on
Native (/100 · duration · valid)
Explore + context-off
Native (/100 · duration · valid)
ON vs OFF deltaTask (metric and direction)Reported pairContext ON
Native (/100 · duration · valid)
Context OFF
Native (/100 · duration · valid)
ON − OFF (native and /100)
Borden (score ↑)Matched ~18hBorden (Native score ↑)Near 18h 80 (— · 18h02m · 17 valid) 45 (— · 17h59m · 17 valid)+35 (+77.8%)+35 / —
Grid (total cost ↓)Matched ~18hGrid (Total cost ↓)Near 18h 2,197 (67.20 · 17h57m · 17 valid) 2,311 (64.85 · 17h59m · 13 valid)−114 (−4.9%, +2.35 pts)−114 / +2.35
Rust (score ↑)Matched ~18hRust (Native score ↑)Near 18h 27.1801 (27.18 · 17h57m · 10 valid) 27.1757 (27.18 · 17h57m · 10 valid)+0.0044 (tied / slight edge)+0.0044 / 0
Portfolio (score / 100 ↑)Matched 18h counterex.Portfolio (Score / 100 ↑)Near 18h · OFF leads 13.62 (13.62 · 18h00m · 17 valid) 39.44 (39.44 · 18h00m · 17 valid)−25.82 pts (OFF wins)−25.82 / −25.82
ANN (QPS ↑)7-arm Stride cohort7,159 (43.17 · 6 valid)3,746 (9.66 · 4 valid)+3,413 (+91.1%, +33.51 pts)Historical7,159 (43.17 · unverified · 6 valid)3,746 (9.66 · unverified · 4 valid)+3,413 / +33.51
FFmpeg (score ↑)OFF early stopFFmpeg (Native score ↑)Shorter or early-stop OFF 2.7086 (16.17 · 18h01m · 16 valid)1.9929 (11.19 · early stop · 6 valid)+0.7157 (+35.9%, +4.98 pts)1.9929 (11.19 · stopped early · 6 valid)+0.7157 / +4.98
VLIW (cycles ↓)OFF 14h45m hist.VLIW (Cycles ↓)Shorter or early-stop OFF 1,042 (97.25 · 17h57m · 14 valid) 1,097 (93.82 · 14h45m · 12 valid)−55 cycles (−5.0%, +3.43 pts)−55 / +3.43
Ad (total score ↑)OFF 8h47m hangAd (Total score ↑)Shorter or early-stop OFF 49,673,040,827 (79.30 · 18h00m · 16 valid) 49,631,904,944 (78.33 · 8h47m · 7 valid)+41.14M (+0.97 pts)+41,135,883 / +0.97
Codec (score ↑)OFF 12h38m counterex.Codec (Native score ↑)Shorter or early-stop OFF · OFF leads 68.89 (68.89 · 18h00m · 13 valid) 70.79 (70.79 · 12h38m · 11 valid)−1.90 pts (OFF wins)−1.9 / −1.9
OpenTTD (score ↑)Both <18h counterex.OpenTTD (Native score ↑)Both below 18h · OFF leads 30,266,729 (62.02 · 10h47m · 10 valid)33,090,430 (62.99 · early stop · 7 valid)−2.82M (−0.97 pts, OFF wins)33,090,430 (62.99 · stopped early · 7 valid)−2,823,701 / −0.97
-

Measurement and scope boundary: these are C1 mixed-cohort exploratory observations (active solver duration accumulated after excluding downtime). This section holds Explore + Stride constant solely to examine the directional effect of toggling context delivery (Context ON vs Context OFF) within the harness; because resumed sessions and incomplete durations remain, it does not establish an isolated C2 causal proof. Whole-system iso-budget Pareto comparisons against zero-overhead external baselines belong to a separate question addressed in Layer 2 of EdgeBench feedback and memory. Structured records live in public aggregate data.

+

The original published aggregate preserves the score and runtime records. The structured data documents their limits and sources. See EdgeBench feedback and memory for whole-system comparisons under comparable budgets.

Multi-task and layered ablation: benefit chain and boundary counterexamples

In EdgeBench ANN vector search optimization (gpt-6.1-sol / xhigh, 18h, subject to Recall@10 ≥ 0.95) and cross-task ablations, we separate Context Delivery (progressive selection and cycle deltas) from Stride (evidence-conditioned experiment sizing), auditing selected control-packet bytes, main-session model input tokens, and local/early-window counterexamples side by side (reconciled from archived trajectories and receipts by @shangzh0):

Multi-task ablation and layered accounting

Progressive Context Delivery and Stride Ablation: ANN 18h Peak QPS Progression and Layered Token/Byte Savings vs Boundary Counterexamples
Top (A): ANN 18h six-arm peak valid throughput QPS (Recall@10 ≥ 0.95). Bottom (B): Layered control-packet byte and main-session input token savings vs Context off (left, negative) alongside disclosed local/early-window overhead and cross-task counterexamples (right, positive). Open image for full size.
diff --git a/apps/presentation/site/public/blog/data/long-horizon-audit.json b/apps/presentation/site/public/blog/data/long-horizon-audit.json index 1557febc5b..2da9f0cc4b 100644 --- a/apps/presentation/site/public/blog/data/long-horizon-audit.json +++ b/apps/presentation/site/public/blog/data/long-horizon-audit.json @@ -160,7 +160,9 @@ "stride_context_on_off_comparison": { "snapshot_cst": "2026-11-12 22:40 CST", "benchmark": "EdgeBench", - "design": "Controlled two-arm comparison under fixed Explore + Stride: Explore + Stride + context-on (progressive reading and compaction-cycle structured delta enabled) vs Explore + Stride + context-off (both disabled).", + "design": "Descriptive snapshot of scores reported as Context ON and OFF. Four pairs list durations within 15 minutes of 18 hours, with no more than 15 minutes between arms. Six pairs are historical or have incomplete or unverified durations. Public evidence does not establish common treatment, budget, version or evaluator across the ten pairs.", + "source_url": "https://github.com/loopx-project/loopx/blob/4a15d6e6fe0148f801ed40d7bfd9d5e5025274a5/apps/presentation/site/public/blog/data/long-horizon-audit.json#L160", + "prior_public_ann_url": "https://github.com/loopx-project/loopx/blob/9ff3b5a7628da7f7c6776bb4e1eeb011aa47eca0/apps/presentation/site/public/blog/context-delivery/index.html#L225-L239", "summary": { "total_tasks": 10, "context_on_ahead_or_edge": 7, @@ -170,8 +172,8 @@ "duration_matched_context_off_wins": 1 }, "reconciliation_notes": [ - "ANN uses the Stride-enabled pair from the historical seven-arm cohort (Explore + Stride + context-on 7,159 QPS vs Explore + Stride + context-off 3,746 QPS); the ablation section separately records the without-Stride Explore pair (6,020 vs 5,766 QPS).", - "Grid reflects the completed ~18h Explore pair within 15m tolerance (context-on 2,197 at 17h57m vs context-off 2,311 at 17h59m); the ablation section retains an earlier unmatched-duration Historical Grid counterexample (Off 2054 < On+Stride 2088)." + "The aggregate reports historical ANN as ON 7,159 and OFF 3,746 QPS with both active durations unverified. The earlier public table confirms the ON value but does not list the OFF value. Its 6,020 and 5,766 QPS belong to a different Explore pair without Stride.", + "This table reports the near-18h Grid pair (ON 2,197 at 17h57m, OFF 2,311 at 17h59m). The ablation section retains an earlier unequal-duration Grid observation (OFF 2,054, ON plus Stride 2,088)." ], "tasks": [ { @@ -190,14 +192,14 @@ "context_off_valid": 17, "winner_explore": "context-on", "delta_label": "+35 (+77.8%)", - "note_en": "Matched ~18h (both within 15m tolerance).", - "note_zh": "等时长约 18h(均在 15m 容差内)。" + "note_en": "Both listed durations are within 15 minutes of 18 hours.", + "note_zh": "两臂列出的时长均在 18 小时前后 15 分钟内。" }, { "task": "Grid", "direction": "lower", "metric_label_en": "Total cost", - "metric_label_zh": "原生总分", + "metric_label_zh": "总代价", "cohort_group": "matched_18h", "context_on_duration": "17h57m", "context_off_duration": "17h59m", @@ -209,8 +211,8 @@ "context_off_valid": 13, "winner_explore": "context-on", "delta_label": "-114 (-4.9%, +2.35 pts)", - "note_en": "Matched ~18h within 15m tolerance (same-session continuation on context-on).", - "note_zh": "等时长约 18h(容差内免补;context-on 沿同会话续跑)。" + "note_en": "Two near-18h reported durations. The aggregate records a same-session continuation on the ON side.", + "note_zh": "两臂列出的时长均接近 18 小时,汇总记录 ON 在原会话续跑。" }, { "task": "Rust", @@ -228,8 +230,8 @@ "context_off_valid": 10, "winner_explore": "context-on-edge", "delta_label": "+0.0044 (tied / slight edge)", - "note_en": "Matched 17h57m each; context-on holds slight native edge.", - "note_zh": "等时长均为 17h57m;context-on 原生分微弱占先。" + "note_en": "Both durations are listed as 17h57m. ON leads in native score and ties at two decimal places on the /100 scale.", + "note_zh": "两臂均列为 17h57m。ON 原生分微弱占先,百分制保留两位小数时持平。" }, { "task": "Portfolio", @@ -247,8 +249,8 @@ "context_off_valid": 17, "winner_explore": "context-off", "delta_label": "-25.82 pts (OFF wins)", - "note_en": "Matched 18h00m counterexample with corrected evaluator; all evaluations drained.", - "note_zh": "修正评测器下的等时长 18h 反例;两 Explore 臂全部评分排空。" + "note_en": "OFF leads. The aggregate lists 18h00m on both sides and reports a corrected evaluator with all evaluations drained.", + "note_zh": "OFF 领先。汇总列出两臂均为 18h00m,并记录修正后的评测器与全部已完成的评分。" }, { "task": "ANN", @@ -266,8 +268,8 @@ "context_off_valid": 4, "winner_explore": "context-on", "delta_label": "+3,413 QPS (+91.1%, +33.51 pts)", - "note_en": "Corresponding Explore + Stride pair from the historical seven-arm ANN cohort; active solver intervals unverified.", - "note_zh": "历史七臂中同开 Stride 的对应两臂配置;原生执行区间尚待核实。" + "note_en": "Historical ANN pair as labeled in the aggregate. Both durations and the OFF score remain unverified by the cited earlier public table.", + "note_zh": "沿用汇总中的历史 ANN 配对标签。所引旧版公开表无法核对两臂时长及 OFF 分数。" }, { "task": "FFmpeg", diff --git a/apps/presentation/site/public/blog/images/context-delivery/stride-context-on-vs-off-en.svg b/apps/presentation/site/public/blog/images/context-delivery/stride-context-on-vs-off-en.svg index 1fba6fdf83..4ccd6e62b4 100644 --- a/apps/presentation/site/public/blog/images/context-delivery/stride-context-on-vs-off-en.svg +++ b/apps/presentation/site/public/blog/images/context-delivery/stride-context-on-vs-off-en.svg @@ -29,406 +29,405 @@ z - - + - - 0 + + 0 - + - - 25 + + 25 - + - - 50 + + 50 - + - - 75 + + 75 - + - - 100 + + 100 - - EdgeBench normalized score (/100, higher is better) + + EdgeBench normalized score (/100, higher is better) - - ANN (QPS↑ · 7-arm Stride) + + ANN · historical, time unverified - - FFmpeg (score↑ · OFF early stop) + + FFmpeg · shorter OFF - - VLIW (cycles↓ · OFF 14h45m) + + VLIW · shorter OFF - - Grid (cost↓ · ~18h matched) + + Grid · near 18h - - Ad (score↑ · OFF 8h47m) + + Ad · shorter OFF - - Rust (score↑ · ~18h matched) + + Rust · near 18h - - OpenTTD (score↑ · both <18h) + + OpenTTD · both under 18h - - Codec (score↑ · OFF 12h38m) + + Codec · shorter OFF - - Portfolio (score↑ · 18h matched) + + Portfolio · near 18h - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - +33.51 (43.17 vs 9.66) + + 43.17 / 9.66 (+33.51) - - +4.98 (16.17 vs 11.19) + + 16.17 / 11.19 (+4.98) - - +3.43 (97.25 vs 93.82) + + 97.25 / 93.82 (+3.43) - - +2.35 (67.20 vs 64.85) + + 67.20 / 64.85 (+2.35) - - +0.97 (79.30 vs 78.33) + + 79.30 / 78.33 (+0.97) - - 27.18 vs 27.18 (+0.0044) + + 27.18 / 27.18 (+0.00) - - -0.97 (62.02 vs 62.99) + + 62.02 / 62.99 (-0.97) - - -1.90 (68.89 vs 70.79) + + 68.89 / 70.79 (-1.90) - - -25.82 (13.62 vs 39.44) + + 13.62 / 39.44 (-25.82) - - A. Fixed Stride: context-on vs context-off (/100) + + A. Nine normalized pairs (ON / OFF) - - - + + - Task #10 (no /100 scale): Borden (~18h matched) - • context-on (18h02m): 80 (17 valid) - • context-off (17h59m): 45 (17 valid) - → +35 pts (+77.8%, duration-matched gain). - - - - Borden has no /100 score + Borden (Native score): 80 / 45 + ON 18h02m / OFF 17h59m + + + + - Key native deltas under fixed Explore + Stride - • ANN (QPS ↑): 7,159 vs 3,746 (+91.1%, +33.51 pts) - • FFmpeg (score ↑): 2.7086 vs 1.9929 (+35.9%, OFF early stop) - • VLIW (cycles ↓): 1,042 vs 1,097 (−55 cycles / −5.0%) - • Grid (cost ↓): 2,197 vs 2,311 (−114 / −4.9%, ~18h) - - - - Examples (ANN OFF unverified) + ANN (QPS): 7,159 / 3,746 + FFmpeg (Native score): 2.7086 / 1.9929 + VLIW (Cycles): 1,042 / 1,097 + Grid (Total cost): 2,197 / 2,311 + + + + - 10-task summary: 7 ON ahead/edge vs 3 OFF ahead - • Matched ~18h (4): 3 ON wins (Borden/Grid/Rust), - 1 counterexample (Portfolio 13.62 vs 39.44) - • Historical / <18h (6): 4 ON ahead, 2 OFF ahead - (Codec 68.89 vs 70.79; OpenTTD 30.27M vs 33.09M) + 10 reported pairs: ON 7 / OFF 3 + 4 near-18h pairs: ON 3 / OFF 1 + Tasks where OFF leads + Portfolio (Score / 100): 13.62 / 39.44 + Codec (Native score): 68.89 / 70.79 + OpenTTD (Native score): 30,266,729 / 33,090,430 - - B. Native score deltas (Borden) & counterexamples + + B. Native scores (ON / OFF) - - EdgeBench 10-task controlled comparison: Context ON vs OFF under Explore + Stride + + EdgeBench scores reported for Context ON and OFF - - Holding Explore + Stride constant (context-on adds progressive reads + cycle deltas). Ahead on 7/10 tasks, retaining 3 counterexamples and runtime gaps. + + 4 pairs list runtimes near 18 hours. The other 6 include historical or incomplete runs. - - Basis: EdgeBench controlled two-arm snapshot at 2026-11-12 22:40 CST. Borden has no /100 scale and appears in native points in Panel B (80 vs 45). - Grid, Borden, Rust, and Portfolio are ~18h duration-matched (within 15m); ANN uses the matching Stride pair from the 7-arm study; remaining 5 pairs have <18h runs. Checks within-harness direction only. + + Snapshot 2026-11-12 22:40 CST. The public records cannot isolate the effect of Context Delivery. - - + - - Explore + Stride + context-on + + Context ON - - + - - Explore + Stride + context-off + + Context OFF - - + + diff --git a/apps/presentation/site/public/blog/images/context-delivery/stride-context-on-vs-off-zh.svg b/apps/presentation/site/public/blog/images/context-delivery/stride-context-on-vs-off-zh.svg index fcea8cf5f0..dda4debee7 100644 --- a/apps/presentation/site/public/blog/images/context-delivery/stride-context-on-vs-off-zh.svg +++ b/apps/presentation/site/public/blog/images/context-delivery/stride-context-on-vs-off-zh.svg @@ -29,406 +29,405 @@ z - - + - - 0 + + 0 - + - - 25 + + 25 - + - - 50 + + 50 - + - - 75 + + 75 - + - - 100 + + 100 - - EdgeBench 百分制归一分 (/100,越高越优;含 ↓ 指标已方向归一化) + + EdgeBench 百分制分数(越高越好) - - ANN (QPS↑ · 历史7臂Stride) + + ANN · 历史时长待核 - - FFmpeg (原生↑ · OFF早停) + + FFmpeg · OFF 时长较短 - - VLIW (周期↓ · OFF 14h45m) + + VLIW · OFF 时长较短 - - Grid (总分↓ · 等时长 ~18h) + + Grid · 接近 18h - - Ad (总分↑ · OFF 8h47m) + + Ad · OFF 时长较短 - - Rust (原生↑ · 等时长 ~18h) + + Rust · 接近 18h - - OpenTTD (原生↑ · 均<18h) + + OpenTTD · 均未满 18h - - Codec (原生↑ · OFF 12h38m) + + Codec · OFF 时长较短 - - Portfolio (分↑ · 等时长 18h) + + Portfolio · 接近 18h - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #0f766e"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - + +" clip-path="url(#pd81db1c1d1)" style="fill: #94a3b8"/> - - +33.51 (43.17 vs 9.66) + + 43.17 / 9.66 (+33.51) - - +4.98 (16.17 vs 11.19) + + 16.17 / 11.19 (+4.98) - - +3.43 (97.25 vs 93.82) + + 97.25 / 93.82 (+3.43) - - +2.35 (67.20 vs 64.85) + + 67.20 / 64.85 (+2.35) - - +0.97 (79.30 vs 78.33) + + 79.30 / 78.33 (+0.97) - - 27.18 vs 27.18 (+0.0044) + + 27.18 / 27.18 (+0.00) - - -0.97 (62.02 vs 62.99) + + 62.02 / 62.99 (-0.97) - - -1.90 (68.89 vs 70.79) + + 68.89 / 70.79 (-1.90) - - -25.82 (13.62 vs 39.44) + + 13.62 / 39.44 (-25.82) - - A. 固定 Stride 下 context-on vs context-off 百分制对照 + + A. 九组百分制分数(ON / OFF) - - - + + - 第 10 题(无 /100 标度):Borden 等时长 ~18h - • context-on(18h02m):80(17 次有效) - • context-off(17h59m):45(17 次有效) - → +35 分(+77.8%,同构等时长显著拉升)。 - - - - Borden 无百分制分数 + Borden (原生分): 80 / 45 + ON 18h02m / OFF 17h59m + + + + - 关键原生分数增幅(固定 Explore + Stride) - • ANN (QPS ↑):7,159 vs 3,746(+91.1%, +33.51 百分分) - • FFmpeg (原生 ↑):2.7086 vs 1.9929(+35.9%, OFF 早停) - • VLIW (周期 ↓):1,042 vs 1,097(−55 周期 / −5.0%) - • Grid (代价 ↓):2,197 vs 2,311(−114 / −4.9%, ~18h) - - - - 部分原生分(ANN 的 OFF 分数待核) + ANN (QPS): 7,159 / 3,746 + FFmpeg (原生分): 2.7086 / 1.9929 + VLIW (周期数): 1,042 / 1,097 + Grid (总代价): 2,197 / 2,311 + + + + - 10 题总盘:ON 7 项占先 vs OFF 3 项领先 - • 等时长 ~18h(4 题):3 胜(Borden/Grid/Rust 微先), - 1 反例(Portfolio 13.62 vs 39.44) - • 非等时长或历史对照(6 题):ON 领先 4 题, - OFF 领先 2 题(Codec 70.79 / OpenTTD 33.09M) + 10 组记录:ON 7 / OFF 3 + 接近 18h 的 4 组:ON 3 / OFF 1 + OFF 占先的题目 + Portfolio (原生分 / 100): 13.62 / 39.44 + Codec (原生分): 68.89 / 70.79 + OpenTTD (原生分): 30,266,729 / 33,090,430 - - B. 原生关键差值(含 Borden)与反例边界 + + B. 原生分(ON / OFF) - - EdgeBench 十题同构效果对照:固定 Explore + Stride 下的 Context ON vs OFF + + EdgeBench 十题记录中的 Context ON 与 OFF 分数 - - 固定 Explore + Stride 框架(context-on 开启 Progressive 与周期内差分)。10 题中 context-on 7 题占先,完整保留 3 项反例与真实时长差。 + + 4 组列出的时长接近 18 小时,其余 6 组涉及历史运行或未跑满的记录。 - - 口径说明:截取自 EdgeBench 同构两臂实测(2026-11-12 22:40 CST 快照)。Borden 无百分制换算故在右栏 B 单列原生分(80 vs 45)。 - Grid、Borden、Rust、Portfolio 四题双臂均已跑满约 18h(15m 容差内);ANN 取自历史七臂对应 Stride 配置;其余 5 题存在至少一臂未满 18h。本图仅检验开关的组内方向性效果。 + + 快照 2026-11-12 22:40 CST。现有逐臂记录不足以单独衡量 Context Delivery 的影响。 - - + - - Explore + Stride + context-on(开启上下文交付) + + Context ON - - + - - Explore + Stride + context-off(关闭上下文交付) + + Context OFF - - + + diff --git a/apps/presentation/site/public/blog/index.html b/apps/presentation/site/public/blog/index.html index 7fffa21f9f..eaa47487cd 100644 --- a/apps/presentation/site/public/blog/index.html +++ b/apps/presentation/site/public/blog/index.html @@ -36,7 +36,7 @@

Context delivery · Mechanisms and examples

· Updated November 14

-

An Agent needs a way to look things up, too

Compare 10-task controlled Explore + Stride scores under Context ON vs OFF (7/10 ahead or edged out—Borden 80 vs 45, ANN 7,159 vs 3,746—with Portfolio/Codec/OpenTTD counterexamples retained), alongside separate 32.5% Progressive, −74.25% ANN in-cycle delta, and ≈ 3.0% / 5.0% first-read baselines.

Read article
+

An Agent needs a way to look things up, too

Ten reported EdgeBench score pairs include four with runtimes close to 18 hours, plus historical and incomplete runs. The article also traces the separate baselines for the 32.5% progressive view, the 74.25% in-cycle ANN reduction and the 3.0% / 5.0% first read.

Read article

Research notes · EdgeBench and LHTB

diff --git a/apps/presentation/site/public/blog/zh/context-delivery/index.html b/apps/presentation/site/public/blog/zh/context-delivery/index.html index 33f1f5e126..c630067518 100644 --- a/apps/presentation/site/public/blog/zh/context-delivery/index.html +++ b/apps/presentation/site/public/blog/zh/context-delivery/index.html @@ -7,7 +7,7 @@
← 全部文章

Context delivery

Agent 读上下文,也需要一套“查书”的方法

先读必要事实,在有效 compaction 周期内复用已验证的结构;连续性丢失时恢复完整基线。

-

Agent 做长任务,资料会越来越多:原目标、后来追加的约束、试过的方案、失败的实验、当前做到哪里。每次继续都把整份材料再塞一遍,重复会不断累积;只留一段摘要,又可能找不回决定所依赖的原话。

+

Agent 做长任务,资料会越来越多:原目标、后来追加的约束、试过的方案、失败的实验、当前做到哪里。每次继续都把整份材料再塞一遍,重复会不断累积;只留一段摘要,又可能找不回决定所依赖的原话。

按 JSON Pointer 渐进选读本身并不复杂,主要用来折叠当前决定暂不需要的历史长尾;LoopX 上下文交付真正的机制重心,是受 compaction 周期约束的结构化差分上下文(compaction-cycle structured delta):完整来源先落盘存证,在同一个已核验的 context_epoch 内精确复用未变化的结构子树、只编码本轮结构增删改;一旦宿主发生上下文压缩或基线摘要失配,立即原子切换回新的完整基线,绝不悬空叠加旧差分。

先看数据:独立上下文对照、周期内结构化差分与任务消融

四种机制,分别核对测量基线

@@ -111,101 +111,99 @@

八题对照:区分单层载荷(Progr

口径与来源:上表逐题数据复算自已公开的八题表,Progressive 合计为 7,285,951 B / 22,408,690 B = 32.5%,覆盖全部 213 次唤醒。Delta 与首屏的 35.8%、3.0% / 5.0% 仅对应 112 次周期内唤醒;其 Full 分母原文仅报告约 12.53 MB,不能与 213 次总量混用。这里没有新增实验,也未独立核对逐 wake 原始记录。公开聚合数据与来源。

实现状态:#8266 仍是开放的 C1 提案;#8304 的最终合入范围是保留精度的排序与摘要复用。上述字节数据不归因为统一 Controller/Worker 已上线的收益。

-

十题同构两臂效果对比:固定 Explore + Stride 下的 Context ON 与 Context OFF

-

在压降上下文传输载荷的同时,我们进一步检验这套交付机制在长程求解中的实际得分表现:从 EdgeBench 十题实测(2026-11-12 22:40 CST 快照)中取出固定同一 Explore + Stride 框架的两臂对照:Explore + context-on(开启 Progressive 按需读取与周期内结构化 Delta)对比 Explore + context-off(两项均关闭),专门检验 Context Delivery 单一开关在同构底盘下的组内表现。

+

EdgeBench 十题记录中的 Context ON 与 OFF 分数

+

这份快照汇集了截至 2026 年 11 月 12 日 22:40(北京时间)标为 Context ON 和 OFF 的十道 EdgeBench 题目。按记录的原生分及指标方向计算,7 题 ON 占先,3 题 OFF 占先。公开资料没有逐题给出两臂的实际配置、代码版本、评测器和预算,因此这组数据无法单独衡量 Context Delivery 的影响。

    -
  • 十题整体盘面(7 题领先或微弱占先 vs 3 题 OFF 领先):固定 Explore + Stride 开启 Context Delivery 后,10 题中有 7 题取得更高或微弱占先的成绩(包括 Borden 80 vs 45 [+77.8%]、ANN 7,159 vs 3,746 QPS [+91.1%]、Grid 2,197 vs 2,311 [−114 代价]);同时完整保留 3 项 Context OFF 领先的反例,不做选择性截取。
  • -
  • 严格等时长约 18h 配对(4 题,15 分钟容差内):3 胜 1 负——Borden 从 45 升至 80(+35 分 / +77.8%);Grid 代价(越低越好)从 2,311 降至 2,197(−114 / −4.9%,+2.35 百分分);Rust(均 17h57m)维持在 27.1801 vs 27.1757 的微弱占先;等时长反例则是 Portfolio(均为 18h00m,13.62 vs 39.44,OFF 领先)。
  • -
  • 历史同构与非等时长对照(6 题,逐题标明实际运行时间):ANN 取历史七臂中同开 Stride 的两臂配置,context-on 达 7,159 QPS 对 context-off 3,746 QPS(+91.1%,+33.51 百分分);FFmpeg 2.7086 vs 1.9929(+35.9%,OFF 早停待重跑);VLIW 周期数(越低越好)1,042 vs 1,097(−55 周期 / −5.0%,OFF 沿用 14h45m 历史轮);Ad 49.67B vs 49.63B(ON 满 18h00m,OFF 因宿主启动空挂仅累计 8h47m)。另外两项反例为 Codec(ON 满 18h00m 得 68.89,不敌模型服务拦截下仅跑 12h38m 的 OFF 70.79)与 OpenTTD(两臂均未满 18h,ON 30,266,729 vs OFF 33,090,430)。
  • +
  • 四组列出的双臂时长接近 18 小时。每臂距 18 小时不超过 15 分钟,双臂相差也不超过 15 分钟。Borden 和 Grid 的 ON 占先(Grid 的代价越低越好)。Rust 两臂原生分是 27.1801 和 27.1757,百分制图上都显示为 27.18。Portfolio 的 OFF 占先。
  • +
  • 其余六组涉及历史运行或未跑满的记录。其中四题 ON 占先,两题 OFF 占先(Codec 和 OpenTTD)。汇总记录的历史 ANN 分数为 7,159 和 3,746 QPS。旧版公开 ANN 表能核对 7,159 QPS,3,746 QPS 和两臂实际运行时长仍待核实。后文的 6,020 和 5,766 QPS 来自未开启 Stride 的另一组 Explore 运行,较早的 Grid 分数也来自另一组。
-

与下方消融表两处口径衔接说明:(1)本节 ANN 对照 3,746 vs 7,159 QPS 来自历史七臂里同开 Stride 的两臂(Explore + Stride + context-off / on);下文 #ablation-evidence 另行列出该研究中未开 Stride 的 Explore 基线(Explore · Context off 5,766 与 Explore · Context on 6,020),二者对照条件不同,互不冲突。(2)本节 Grid(2,197 vs 2,311)反映 15 分钟容差内均跑满 ~18h 的最新完成对比(其中 context-on 沿同一会话续跑);下文仍保留更早一轮未匹配时长与资源的「Historical Grid」反例(Off 2054 < On+Stride 2088)作历史记录。

-

EdgeBench 十题同构两臂效果对照(固定 Explore + Stride:Context ON vs OFF)

EdgeBench 十题同构 Explore + Stride 下 Context ON vs OFF 分数对照:7 题领先或占先(ANN +91.1%、Borden +77.8%、Grid −114),完整保留 Portfolio、Codec、OpenTTD 三项反例
A:9 道有 EdgeBench 百分制归一分的题目按 ON − OFF 分差排序(已方向归一化为越高越优),标出各自时长状态。B:无百分制标度的第 10 题 Borden(等时长 ~18h,80 vs 45)、关键原生分增幅,以及 3 项负向反例口径。
-
展开十题同构两臂逐题分数与运行时长明细 -
+

EdgeBench 记录中的 Context ON 与 OFF 分数

记录中的 Context ON 与 OFF 分数。左图显示 9 组百分制分数。右图单列 Borden 原生分,标出 Portfolio、Codec 和 OpenTTD。
A 显示有百分制分数的九组记录,按 ON 减 OFF 排序(方向已归一为越高越好)。B 单列 Borden 原生分,标出 OFF 占先的三题。
+
展开十题分数与时长 +
- - - - + + + + - + - + - - + + - + - + - + - + - + - - - - + + + + - + - + - + - + - + - + - + - + - + - +
题目(指标与方向)时长配对状态Explore + context-on
原生(/100 · 时长 · 有效次)
Explore + context-off
原生(/100 · 时长 · 有效次)
ON vs OFF 差值记录类别Context ON
原生(/100 · 时长 · 有效次数)
Context OFF
原生(/100 · 时长 · 有效次数)
ON − OFF(原生与百分制)
Borden(原生分 ↑)等时长 ~18h接近 18 小时 80(— · 18h02m · 17 次) 45(— · 17h59m · 17 次)+35(+77.8%)+35 / —
Grid(原生总分 ↓)等时长 ~18hGrid(总代价 ↓)接近 18 小时 2,197(67.20 · 17h57m · 17 次) 2,311(64.85 · 17h59m · 13 次)−114(−4.9%, +2.35 分)−114 / +2.35
Rust(原生分 ↑)等时长 ~18h接近 18 小时 27.1801(27.18 · 17h57m · 10 次) 27.1757(27.18 · 17h57m · 10 次)+0.0044(微先 / 持平)+0.0044 / 0
Portfolio(原生分 / 100 ↑)等时长 18h 反例接近 18 小时 · OFF 占先 13.62(13.62 · 18h00m · 17 次) 39.44(39.44 · 18h00m · 17 次)−25.82 分(OFF 胜)−25.82 / −25.82
ANN(QPS ↑)历史七臂 Stride7,159(43.17 · 6 次)3,746(9.66 · 4 次)+3,413(+91.1%, +33.51 分)历史记录7,159(43.17 · 待核实 · 6 次)3,746(9.66 · 待核实 · 4 次)+3,413 / +33.51
FFmpeg(原生分 ↑)OFF 早停待重跑OFF 时长较短或早停 2.7086(16.17 · 18h01m · 16 次) 1.9929(11.19 · 早停 · 6 次)+0.7157(+35.9%, +4.98 分)+0.7157 / +4.98
VLIW(周期数 ↓)OFF 14h45m 历史OFF 时长较短或早停 1,042(97.25 · 17h57m · 14 次) 1,097(93.82 · 14h45m · 12 次)−55 周期(−5.0%, +3.43 分)−55 / +3.43
Ad(原生总分 ↑)OFF 8h47m 空挂OFF 时长较短或早停 49,673,040,827(79.30 · 18h00m · 16 次) 49,631,904,944(78.33 · 8h47m · 7 次)+41.14M(+0.97 分)+41,135,883 / +0.97
Codec(原生分 ↑)OFF 12h38m 反例OFF 时长较短或早停 · OFF 占先 68.89(68.89 · 18h00m · 13 次) 70.79(70.79 · 12h38m · 11 次)−1.90 分(OFF 胜)−1.9 / −1.9
OpenTTD(原生分 ↑)两臂均未满 反例两臂均未满 18 小时 · OFF 占先 30,266,729(62.02 · 10h47m · 10 次) 33,090,430(62.99 · 早停 · 7 次)−2.82M(−0.97 分, OFF 胜)−2,823,701 / −0.97
-

实验口径与边界说明:以上属于 C1 多轮探索观测(有效运行时间按求解进程扣除停机后累计)。本节固定 Explore + Stride 框架,仅隔离检验开启上下文交付(Context ON vs Context OFF)在组内的方向性表现;因存在续跑会话与部分未补满时长差异,不单独构成剥离全部交互项后的纯 C2 因果证明。控制面整体相对零协议税外部基线的等预算帕累托对比属另一层独立议题,详见 《EdgeBench 反馈与记忆》中的第二层目标讨论。原始聚合结构化数据见 公开聚合数据。

+

原来的公开汇总快照保留了比分和时长记录。结构化数据列明取舍与出处。全系统在等预算下的比较另见EdgeBench 反馈与记忆。

真实模型与多题消融实证:收益链路与边界反例

在 EdgeBench ANN 向量检索优化(gpt-6.1-sol / xhigh,18h,约束 Recall@10 ≥ 0.95)与跨题消融中,我们将 Context Delivery(渐进读取与周期差分)与 Stride(基于证据差分的步长控制)拆开对照,同时统计控制包选中字节、主会话模型输入 Token 与局部/早窗反例(基于 @shangzh0 归档轨迹与控制面回执复算):

多题消融与分层计量审计

Context 渐进式交付与 Stride 步幅控制消融:ANN 18h 峰值吞吐 QPS 对比与分层 Token/字节节省及边界反例
上图(A):ANN 18h 六臂峰值有效吞吐 QPS(Recall@10 ≥ 0.95)。下图(B):相对 Context off 的分层字节与主会话输入 Token 节省(左,负值)及诚实披露的局部/早窗开销与跨题反例(右,正值)。点击查看原图。
diff --git a/apps/presentation/site/public/blog/zh/index.html b/apps/presentation/site/public/blog/zh/index.html index f2b74e838c..6110174755 100644 --- a/apps/presentation/site/public/blog/zh/index.html +++ b/apps/presentation/site/public/blog/zh/index.html @@ -36,7 +36,7 @@

Context delivery · 机制与实例

· 11 月 14 日更新

-

Agent 读上下文,也需要一套“查书”的方法

拆解十题同构两臂(固定 Explore + Stride)下的 Context ON vs OFF 跑分对比(7/10 题占先、Borden 80 vs 45、ANN 7,159 vs 3,746,并保留 Portfolio/Codec/OpenTTD 反例),同时核对渐进视图 32.5%、ANN 周期内增量轮 −74.25% 与首读 3.0% / 5.0% 的载荷基线。

阅读全文
+

Agent 读上下文,也需要一套“查书”的方法

核对 EdgeBench 的十组记录分数,其中四组的时长接近 18 小时,其余涉及历史运行或未跑满的记录。文章也分别交代渐进视图 32.5%、ANN 周期内增量轮缩减 74.25% 和首读 3.0% / 5.0% 各自的测量基线。

阅读全文

研究札记 · EdgeBench 与 LHTB

diff --git a/apps/presentation/site/scripts/generate-long-horizon-audit.py b/apps/presentation/site/scripts/generate-long-horizon-audit.py index 48d837c18a..1fb4c26288 100644 --- a/apps/presentation/site/scripts/generate-long-horizon-audit.py +++ b/apps/presentation/site/scripts/generate-long-horizon-audit.py @@ -5,6 +5,7 @@ """ from pathlib import Path import json +import re import matplotlib as mpl mpl.use("Agg") @@ -30,6 +31,23 @@ def save(fig, relative): plt.close(fig) +def listed_times_near_18_hours(row): + times = [] + for side in ("on", "off"): + match = re.fullmatch(r"(\d+)h([0-5]\d)m", row[f"context_{side}_duration"]) + if match is None: + return False + times.append(int(match[1]) * 60 + int(match[2])) + return all(abs(minutes - 18 * 60) <= 15 for minutes in times) and abs(times[0] - times[1]) <= 15 + + +def reported_side_ahead(row): + on, off = row["context_on_native"], row["context_off_native"] + if on == off: + return None + return "on" if (on > off) == (row["direction"] == "higher") else "off" + + for language in ("en", "zh"): zh = language == "zh" # 1. Independent archived tutorial comparisons, not one measured cascade. @@ -324,165 +342,132 @@ def save(fig, relative): fig.text(.03,.025,footer,fontsize=10,color="#475569",linespacing=1.6) save(fig, f"images/edgebench-feedback-and-memory/stride-yardstick-closed-loop-{language}.svg") - # 4. EdgeBench Controlled Two-Arm Explore + Stride: context-on vs context-off score comparison (10 tasks) - two_arm = DATA["stride_context_on_off_comparison"] - arm_tasks = {row["task"]: row for row in two_arm["tasks"]} - ordered_norm_names = ["ANN", "FFmpeg", "VLIW", "Grid", "Ad", "Rust", "OpenTTD", "Codec", "Portfolio"] - norm_rows = [arm_tasks[name] for name in ordered_norm_names] + # 4. Reported EdgeBench score pairs and their observation limits. + comparison = DATA["stride_context_on_off_comparison"] + rows = comparison["tasks"] + norm_rows = sorted( + (row for row in rows if row["context_on_norm"] is not None), + key=lambda row: row["context_on_norm"] - row["context_off_norm"], + reverse=True, + ) + native_rows = {row["task"]: row for row in rows} fig = plt.figure(figsize=(14.6, 7.8)) gs = fig.add_gridspec( 1, 2, width_ratios=[1.25, 1.05], - left=.18, right=.98, bottom=.175, top=.805, wspace=.11, + left=.17, right=.97, bottom=.175, top=.805, wspace=.14, ) left = fig.add_subplot(gs[0, 0]) right = fig.add_subplot(gs[0, 1]) - - y_pos = list(range(len(norm_rows))) - on_vals = [r["context_on_norm"] for r in norm_rows] - off_vals = [r["context_off_norm"] for r in norm_rows] - off_bar_color = "#94a3b8" - - left.barh([y - .18 for y in y_pos], on_vals, height=.32, - color=TEAL, label=("Explore + Stride + context-on" if not zh else "Explore + Stride + context-on(开启上下文交付)")) - left.barh([y + .18 for y in y_pos], off_vals, height=.32, - color=off_bar_color, label=("Explore + Stride + context-off" if not zh else "Explore + Stride + context-off(关闭上下文交付)")) - - tag_map_zh = { - "ANN": "ANN (QPS↑ · 历史7臂Stride)", - "FFmpeg": "FFmpeg (原生↑ · OFF早停)", - "VLIW": "VLIW (周期↓ · OFF 14h45m)", - "Grid": "Grid (总分↓ · 等时长 ~18h)", - "Ad": "Ad (总分↑ · OFF 8h47m)", - "Rust": "Rust (原生↑ · 等时长 ~18h)", - "OpenTTD": "OpenTTD (原生↑ · 均<18h)", - "Codec": "Codec (原生↑ · OFF 12h38m)", - "Portfolio": "Portfolio (分↑ · 等时长 18h)", - } - tag_map_en = { - "ANN": "ANN (QPS↑ · 7-arm Stride)", - "FFmpeg": "FFmpeg (score↑ · OFF early stop)", - "VLIW": "VLIW (cycles↓ · OFF 14h45m)", - "Grid": "Grid (cost↓ · ~18h matched)", - "Ad": "Ad (score↑ · OFF 8h47m)", - "Rust": "Rust (score↑ · ~18h matched)", - "OpenTTD": "OpenTTD (score↑ · both <18h)", - "Codec": "Codec (score↑ · OFF 12h38m)", - "Portfolio": "Portfolio (score↑ · 18h matched)", + positions = list(range(len(norm_rows))) + on_bars = left.barh( + [y - .18 for y in positions], [row["context_on_norm"] for row in norm_rows], + height=.32, color=TEAL, label="Context ON", + ) + off_bars = left.barh( + [y + .18 for y in positions], [row["context_off_norm"] for row in norm_rows], + height=.32, color="#94a3b8", label="Context OFF", + ) + tags = { + "matched_18h": "接近 18h" if zh else "near 18h", + "historical_stride": "历史时长待核" if zh else "historical, time unverified", + "incomplete_off": "OFF 时长较短" if zh else "shorter OFF", + "both_incomplete": "均未满 18h" if zh else "both under 18h", } - left.set_yticks(y_pos, [(tag_map_zh if zh else tag_map_en)[r["task"]] for r in norm_rows], fontsize=9.7) + suffix = "zh" if zh else "en" + left.set_yticks( + positions, + [f"{row['task']} · {tags[row['cohort_group']]}" for row in norm_rows], + fontsize=9.5, + ) left.invert_yaxis() - left.set_xlim(0, 134) - left.set_xticks([0, 25, 50, 75, 100]) - left.set_xlabel("EdgeBench 百分制归一分 (/100,越高越优;含 ↓ 指标已方向归一化)" if zh else "EdgeBench normalized score (/100, higher is better)", fontsize=9.5) + left.set_xlim(0, 137) + ticks = [0, 25, 50, 75, 100] + left.set_xticks(ticks) + for tick_value, text in zip(ticks, left.get_xticklabels()): + text.set_gid(f"context-axis-{tick_value}") + left.set_xlabel("EdgeBench 百分制分数(越高越好)" if zh else "EdgeBench normalized score (/100, higher is better)", fontsize=9.5) left.set_title( - "A. 固定 Stride 下 context-on vs context-off 百分制对照" if zh else "A. Fixed Stride: context-on vs context-off (/100)", - loc="left", pad=14, fontsize=11.0, fontweight="bold", + "A. 九组百分制分数(ON / OFF)" if zh else "A. Nine normalized pairs (ON / OFF)", + loc="left", pad=14, fontsize=11, fontweight="bold", ) left.set_axisbelow(True) left.grid(axis="x", color="#e2e8f0", linewidth=.7) left.tick_params(axis="both", length=0) - for idx, r in enumerate(norm_rows): - diff = r["context_on_norm"] - r["context_off_norm"] - anchor = max(r["context_on_norm"], r["context_off_norm"]) - if r["task"] == "Rust": - txt = f"{r['context_on_norm']:.2f} vs {r['context_off_norm']:.2f} (+0.0044)" - col = TEAL - fw = "bold" - elif diff > 0: - txt = f"+{diff:.2f} ({r['context_on_norm']:.2f} vs {r['context_off_norm']:.2f})" - col = TEAL - fw = "bold" - else: - txt = f"{diff:.2f} ({r['context_on_norm']:.2f} vs {r['context_off_norm']:.2f})" - col = "#b45309" - fw = "bold" - left.text(anchor + 2.2, idx, txt, va="center", fontsize=8.8, fontweight=fw, color=col) + for index, (row, on_bar, off_bar) in enumerate(zip(norm_rows, on_bars, off_bars)): + task = row["task"] + on_bar.set_gid(f"context-bar-{task}-on") + off_bar.set_gid(f"context-bar-{task}-off") + on, off = row["context_on_norm"], row["context_off_norm"] + delta = on - off + color = TEAL if delta > 0 else ("#b45309" if delta < 0 else SLATE) + left.text(max(on, off) + 2.2, index, f"{on:.2f} / {off:.2f} ({delta:+.2f})", + va="center", fontsize=8.8, fontweight="bold", color=color).set_gid(f"context-score-{task}") right.axis("off") right.set_title( - "B. 原生关键差值(含 Borden)与反例边界" if zh else "B. Native score deltas (Borden) & counterexamples", - loc="left", pad=14, fontsize=11.0, fontweight="bold", + "B. 原生分(ON / OFF)" if zh else "B. Native scores (ON / OFF)", + loc="left", pad=14, fontsize=11, fontweight="bold", ) - borden = arm_tasks["Borden"] - cards_two_arm = ( - [ - ( - "第 10 题(无 /100 标度):Borden 等时长 ~18h\n" - f"• context-on(18h02m):{borden['context_on_native']}(17 次有效)\n" - f"• context-off(17h59m):{borden['context_off_native']}(17 次有效)\n" - "→ +35 分(+77.8%,同构等时长显著拉升)。", - "#f0fdfa", "#99f6e4", - ), - ( - "关键原生分数增幅(固定 Explore + Stride)\n" - "• ANN (QPS ↑):7,159 vs 3,746(+91.1%, +33.51 百分分)\n" - "• FFmpeg (原生 ↑):2.7086 vs 1.9929(+35.9%, OFF 早停)\n" - "• VLIW (周期 ↓):1,042 vs 1,097(−55 周期 / −5.0%)\n" - "• Grid (代价 ↓):2,197 vs 2,311(−114 / −4.9%, ~18h)", - "#f8fafc", "#cbd5e1", - ), - ( - "10 题总盘:ON 7 项占先 vs OFF 3 项领先\n" - "• 等时长 ~18h(4 题):3 胜(Borden/Grid/Rust 微先),\n" - " 1 反例(Portfolio 13.62 vs 39.44)\n" - "• 非等时长或历史对照(6 题):ON 领先 4 题,\n" - " OFF 领先 2 题(Codec 70.79 / OpenTTD 33.09M)", - "#fffbeb", "#fcd34d", - ), - ] - if zh else - [ - ( - "Task #10 (no /100 scale): Borden (~18h matched)\n" - f"• context-on (18h02m): {borden['context_on_native']} (17 valid)\n" - f"• context-off (17h59m): {borden['context_off_native']} (17 valid)\n" - "→ +35 pts (+77.8%, duration-matched gain).", - "#f0fdfa", "#99f6e4", - ), - ( - "Key native deltas under fixed Explore + Stride\n" - "• ANN (QPS ↑): 7,159 vs 3,746 (+91.1%, +33.51 pts)\n" - "• FFmpeg (score ↑): 2.7086 vs 1.9929 (+35.9%, OFF early stop)\n" - "• VLIW (cycles ↓): 1,042 vs 1,097 (−55 cycles / −5.0%)\n" - "• Grid (cost ↓): 2,197 vs 2,311 (−114 / −4.9%, ~18h)", - "#f8fafc", "#cbd5e1", - ), - ( - "10-task summary: 7 ON ahead/edge vs 3 OFF ahead\n" - "• Matched ~18h (4): 3 ON wins (Borden/Grid/Rust),\n" - " 1 counterexample (Portfolio 13.62 vs 39.44)\n" - "• Historical / <18h (6): 4 ON ahead, 2 OFF ahead\n" - " (Codec 68.89 vs 70.79; OpenTTD 30.27M vs 33.09M)", - "#fffbeb", "#fcd34d", - ), - ] - ) - y_starts = [.98, .63, .28] - for y_box, (txt, face, edge) in zip(y_starts, cards_two_arm): - right.text(.01, y_box, txt, transform=right.transAxes, va="top", fontsize=9.1, - linespacing=1.50, bbox={"boxstyle": "round,pad=.60", "facecolor": face, "edgecolor": edge}) + borden = native_rows["Borden"] + winners = {row["task"]: reported_side_ahead(row) for row in rows} + near = [row for row in rows if listed_times_near_18_hours(row)] + on_count = list(winners.values()).count("on") + off_count = list(winners.values()).count("off") + near_on = sum(winners[row["task"]] == "on" for row in near) + near_off = sum(winners[row["task"]] == "off" for row in near) + examples = [native_rows[task] for task in ("ANN", "FFmpeg", "VLIW", "Grid")] + counterexamples = [row for row in rows if winners[row["task"]] == "off"] + + def native_line(row): + return f"{row['task']} ({row[f'metric_label_{suffix}']}): {row['context_on_native']:,} / {row['context_off_native']:,}" + + borden_label = "Borden 无百分制分数" if zh else "Borden has no /100 score" + near_line = (f"接近 18h 的 {len(near)} 组:ON {near_on} / OFF {near_off}" if zh + else f"{len(near)} near-18h pairs: ON {near_on} / OFF {near_off}") + total_line = (f"{len(rows)} 组记录:ON {on_count} / OFF {off_count}" if zh + else f"{len(rows)} reported pairs: ON {on_count} / OFF {off_count}") + cards = [ + ("context-native-borden", "\n".join([ + borden_label, native_line(borden), + f"ON {borden['context_on_duration']} / OFF {borden['context_off_duration']}", + ]), "#f0fdfa", "#99f6e4"), + ("context-native-examples", "\n".join([ + "部分原生分(ANN 的 OFF 分数待核)" if zh else "Examples (ANN OFF unverified)", + *(native_line(row) for row in examples), + ]), "#f8fafc", "#cbd5e1"), + ("context-native-off", "\n".join([ + total_line, near_line, "OFF 占先的题目" if zh else "Tasks where OFF leads", + *(native_line(row) for row in counterexamples), + ]), "#fffbeb", "#fcd34d"), + ] + for top, (group_id, text, face, edge) in zip([.98, .72, .36], cards): + right.text( + .01, top, text, transform=right.transAxes, va="top", fontsize=9.1, + linespacing=1.45, bbox={"boxstyle": "round,pad=.60", "facecolor": face, "edgecolor": edge}, + ).set_gid(group_id) fig.suptitle( - "EdgeBench 十题同构效果对照:固定 Explore + Stride 下的 Context ON vs OFF" - if zh else - "EdgeBench 10-task controlled comparison: Context ON vs OFF under Explore + Stride", + "EdgeBench 十题记录中的 Context ON 与 OFF 分数" if zh else + "EdgeBench scores reported for Context ON and OFF", fontsize=14.6, fontweight="bold", y=.955, ) - sub_two_arm = ( - "固定 Explore + Stride 框架(context-on 开启 Progressive 与周期内差分)。10 题中 context-on 7 题占先,完整保留 3 项反例与真实时长差。" + other_count = len(rows) - len(near) + fig.text( + .5, .88, + f"{len(near)} 组列出的时长接近 18 小时,其余 {other_count} 组涉及历史运行或未跑满的记录。" if zh else - "Holding Explore + Stride constant (context-on adds progressive reads + cycle deltas). Ahead on 7/10 tasks, retaining 3 counterexamples and runtime gaps." + f"{len(near)} pairs list runtimes near 18 hours. The other {other_count} include historical or incomplete runs.", + ha="center", fontsize=10, color="#475569", ) - fig.text(.5, .88, sub_two_arm, ha="center", fontsize=9.9, color="#475569") - fig.legend(*left.get_legend_handles_labels(), loc="lower center", ncol=2, frameon=False, bbox_to_anchor=(.45, .078), fontsize=9.6) - footer_two_arm = ( - "口径说明:截取自 EdgeBench 同构两臂实测(2026-11-12 22:40 CST 快照)。Borden 无百分制换算故在右栏 B 单列原生分(80 vs 45)。\n" - "Grid、Borden、Rust、Portfolio 四题双臂均已跑满约 18h(15m 容差内);ANN 取自历史七臂对应 Stride 配置;其余 5 题存在至少一臂未满 18h。本图仅检验开关的组内方向性效果。" + fig.legend(*left.get_legend_handles_labels(), loc="lower center", ncol=2, frameon=False, + bbox_to_anchor=(.45, .081), fontsize=9.6) + fig.text( + .03, .028, + f"快照 {comparison['snapshot_cst']}。现有逐臂记录不足以单独衡量 Context Delivery 的影响。" if zh else - "Basis: EdgeBench controlled two-arm snapshot at 2026-11-12 22:40 CST. Borden has no /100 scale and appears in native points in Panel B (80 vs 45).\n" - "Grid, Borden, Rust, and Portfolio are ~18h duration-matched (within 15m); ANN uses the matching Stride pair from the 7-arm study; remaining 5 pairs have <18h runs. Checks within-harness direction only." + f"Snapshot {comparison['snapshot_cst']}. The public records cannot isolate the effect of Context Delivery.", + fontsize=8.9, color="#475569", ) - fig.text(.03, .018, footer_two_arm, fontsize=8.9, color="#475569", linespacing=1.45) save(fig, f"images/context-delivery/stride-context-on-vs-off-{language}.svg") diff --git a/examples/blog-bilingual-index-smoke.mjs b/examples/blog-bilingual-index-smoke.mjs index 1c2235fdb6..f32ace408c 100644 --- a/examples/blog-bilingual-index-smoke.mjs +++ b/examples/blog-bilingual-index-smoke.mjs @@ -76,10 +76,161 @@ function listingDates(html, language, articleSlugs) { return dates; } +const SNAPSHOT_TASKS = ["Borden", "Grid", "Rust", "Portfolio", "ANN", "FFmpeg", "VLIW", "Ad", "Codec", "OpenTTD"]; +const SNAPSHOT_SCOPES = { + matched_18h: ["Near 18h", "接近 18 小时"], + historical_stride: ["Historical", "历史记录"], + incomplete_off: ["Shorter or early-stop OFF", "OFF 时长较短或早停"], + both_incomplete: ["Both below 18h", "两臂均未满 18 小时"], +}; +const reportedNumber = value => value.toLocaleString("en-US", { maximumFractionDigits: 4 }); +const signed = value => (value < 0 ? "−" : value > 0 ? "+" : "") + reportedNumber(Math.abs(value)); +const delta = (on, off) => Number((on - off).toFixed(4)); +const durationParts = value => value.match(/^(\d+)h([0-5]\d)m$/)?.slice(1).map(Number); +const plainText = svgOrHtml => svgOrHtml.replace(/<[^>]*>/g, "").replaceAll("&", "&").replaceAll("<", "<").trim(); + +function scoreSnapshotRows(snapshot) { + for (const source of [snapshot.source_url, snapshot.prior_public_ann_url]) { + if (!/^https:\/\/github\.com\/loopx-project\/loopx\/blob\/[a-f0-9]{40}\//.test(source)) { + throw new Error("Score snapshot must link immutable public sources"); + } + } + deepStrictEqual(snapshot.tasks.map(row => row.task), SNAPSHOT_TASKS, "Score snapshot task order and coverage"); + const pairs = snapshot.tasks.map(row => { + const { task, direction } = row; + const arms = ["on", "off"].map(side => ({ + native: row["context_" + side + "_native"], + norm: row["context_" + side + "_norm"], + duration: row["context_" + side + "_duration"], + valid: row["context_" + side + "_valid"], + })); + const [on, off] = arms; + if (!["higher", "lower"].includes(direction) || !Object.hasOwn(SNAPSHOT_SCOPES, row.cohort_group) || + !arms.every(arm => Number.isFinite(arm.native) && + (task === "Borden" ? arm.norm === null : Number.isFinite(arm.norm) && arm.norm >= 0 && arm.norm <= 100) && + typeof arm.duration === "string" && + (durationParts(arm.duration) || ["unverified", "stopped early"].includes(arm.duration)) && + Number.isInteger(arm.valid) && arm.valid >= 0)) { + throw new Error("Invalid score snapshot row: " + task); + } + const sign = direction === "lower" ? -1 : 1; + const nativeWinner = Math.sign((on.native - off.native) * sign); + const normWinner = on.norm === null ? 0 : Math.sign(on.norm - off.norm); + const onLeads = nativeWinner > 0; + const winner = onLeads ? (on.norm !== null && normWinner === 0 ? "context-on-edge" : "context-on") : "context-off"; + if (nativeWinner === 0 || (normWinner !== 0 && normWinner !== nativeWinner) || row.winner_explore !== winner) { + throw new Error("Score snapshot direction or winner conflicts with the reported values: " + task); + } + const reportedMinutes = arms.map(arm => { + const time = durationParts(arm.duration); + return time ? time[0] * 60 + time[1] : Infinity; + }); + const near = reportedMinutes.every(time => Math.abs(time - 1080) <= 15) && + Math.abs(reportedMinutes[0] - reportedMinutes[1]) <= 15; + deepStrictEqual(near, row.cohort_group === "matched_18h", "Near-18h classification: " + task); + return { row, on, off, onLeads, near }; + }); + const near = pairs.filter(pair => pair.near); + const ahead = pairs.filter(pair => pair.onLeads); + const offTasks = pairs.filter(pair => !pair.onLeads).map(pair => pair.row.task); + deepStrictEqual(offTasks, ["Portfolio", "Codec", "OpenTTD"], "Keep the three reported OFF counterexamples"); + deepStrictEqual(snapshot.summary, { + total_tasks: pairs.length, + context_on_ahead_or_edge: ahead.length, + context_off_ahead: offTasks.length, + duration_matched_18h_pairs: near.length, + duration_matched_context_on_wins: near.filter(pair => pair.onLeads).length, + duration_matched_context_off_wins: near.filter(pair => !pair.onLeads).length, + }, "Score snapshot tally must follow native direction and the listed runtimes"); + return pairs; +} + +function assertScoreTable(section, pairs, lang) { + const zh = lang === "zh"; + const table = section.match(/([\s\S]*?)<\/tbody>/)?.[1] ?? ""; + const rows = [...table.matchAll(/([\s\S]*?)<\/tr>/g)]; + deepStrictEqual(rows.length, pairs.length, "Score table must keep all ten task rows: " + lang); + const armText = ({ native, norm, duration, valid }) => { + const time = zh ? ({ unverified: "待核实", "stopped early": "早停" }[duration] ?? duration) : duration; + const cells = (norm?.toFixed(2) ?? "—") + " · " + time + " · " + valid; + return reportedNumber(native) + (zh ? "(" + cells + " 次)" : " (" + cells + " valid)"); + }; + for (const [index, { row, on, off, onLeads }] of pairs.entries()) { + const raw = [...rows[index][1].matchAll(/]*>([\s\S]*?)<\/td>/g)].map(match => match[1]); + const metric = row["metric_label_" + lang] + (row.direction === "higher" ? " ↑" : " ↓"); + const title = row.task + (zh ? "(" + metric + ")" : " (" + metric + ")"); + const scope = SNAPSHOT_SCOPES[row.cohort_group][Number(zh)] + (onLeads ? "" : (zh ? " · OFF 占先" : " · OFF leads")); + const changes = signed(delta(on.native, off.native)) + " / " + + (on.norm === null ? "—" : signed(delta(on.norm, off.norm))); + deepStrictEqual(raw.map(plainText), [title, scope, armText(on), armText(off), changes], + "Score table row differs from the published aggregate: " + lang + "/" + row.task); + deepStrictEqual([raw[2].startsWith(""), raw[3].startsWith("")], + [onLeads, !onLeads], "Score table emphasizes the correct side: " + lang + "/" + row.task); + } +} + +function assertScoreFigure(svg, pairs, lang) { + // Matplotlib emits named groups for each actual label and bar, with child groups indented. + const group = id => { + const contents = svg.match(new RegExp('^( *)\\n([\\s\\S]*?)^\\1$', "m"))?.[2]; + if (!contents) throw new Error("Score chart is missing " + lang + "/" + id); + return contents; + }; + const labels = value => [...value.matchAll(/]*>([\s\S]*?)<\/text>/g)].map(match => plainText(match[1])); + const tickX = n => Number(group("context-axis-" + n).match(/]*\bx="([^"]+)"/)?.[1]); + const origin = tickX(0); + const unit = (tickX(100) - origin) / 100; + const normalized = pairs.filter(pair => pair.on.norm !== null) + .sort((a, b) => delta(b.on.norm, b.off.norm) - delta(a.on.norm, a.off.norm)); + deepStrictEqual([...svg.matchAll(//g)].map(match => match[1]), + normalized.map(pair => pair.row.task), "Score chart must retain the normalized delta order: " + lang); + for (const { row, on, off } of normalized) { + const change = delta(on.norm, off.norm); + const label = on.norm.toFixed(2) + " / " + off.norm.toFixed(2) + + " (" + (change >= 0 ? "+" : "") + change.toFixed(2) + ")"; + deepStrictEqual(labels(group("context-score-" + row.task)), [label], + "Score chart label differs from the published aggregate: " + lang + "/" + row.task); + for (const [side, arm] of [["on", on], ["off", off]]) { + const points = [...group("context-bar-" + row.task + "-" + side).matchAll(/[ML] ([\d.]+) /g)] + .map(match => Number(match[1])); + if (!points.length || !(Math.abs(Math.min(...points) - origin) < 0.02) || + !(Math.abs(Math.max(...points) - (origin + arm.norm * unit)) < 0.02)) { + throw new Error("Score chart bar differs from the published aggregate: " + lang + "/" + row.task + "/" + side); + } + } + } + + const nativeLine = ({ row, on, off }) => + row.task + " (" + row["metric_label_" + lang] + "): " + reportedNumber(on.native) + " / " + reportedNumber(off.native); + const borden = pairs[0]; + deepStrictEqual(labels(group("context-native-borden")).slice(1), + [nativeLine(borden), "ON " + borden.on.duration + " / OFF " + borden.off.duration], + "Score chart Borden native pair differs: " + lang); + const examples = ["ANN", "FFmpeg", "VLIW", "Grid"].map(task => pairs.find(pair => pair.row.task === task)); + deepStrictEqual(labels(group("context-native-examples")).slice(1), examples.map(nativeLine), + "Score chart native example differs: " + lang); + const counters = pairs.filter(pair => !pair.onLeads); + const near = pairs.filter(pair => pair.near); + const nearOn = near.filter(pair => pair.onLeads).length; + const counts = (total, off) => "ON " + (total - off) + " / OFF " + off; + const total = counts(pairs.length, counters.length); + const nearTotal = counts(near.length, near.length - nearOn); + const expectedCard = lang === "zh" + ? [pairs.length + " 组记录:" + total, "接近 18h 的 " + near.length + " 组:" + nearTotal, "OFF 占先的题目"] + : [pairs.length + " reported pairs: " + total, near.length + " near-18h pairs: " + nearTotal, "Tasks where OFF leads"]; + deepStrictEqual(labels(group("context-native-off")), [...expectedCard, ...counters.map(nativeLine)], + "Score chart observed tally or native counterexample differs: " + lang); +} + export async function validateBilingualBlog(blogDir) { const auditPath = resolve(blogDir, "data/long-horizon-audit.json"); + if (!existsSync(auditPath) && existsSync(resolve(blogDir, "context-delivery/index.html"))) { + throw new Error("The context delivery article requires its public audit data"); + } if (existsSync(auditPath)) { const audit = JSON.parse(await readFile(auditPath, "utf8")); + const snapshot = audit.stride_context_on_off_comparison; + const snapshotRows = scoreSnapshotRows(snapshot); const tasks = audit.context.tasks; const sum = key => tasks.reduce((total, task) => total + task[key], 0); deepStrictEqual([tasks.length, sum("wakes"), sum("baseline_wakes"), sum("delta_wakes")], @@ -132,48 +283,25 @@ export async function validateBilingualBlog(blogDir) { "utf8", ); assertIncludes(multitaskSvg, "100.0%", `Eight-task byte audit chart must anchor at 100% Full: ${langSuffix}`); - const twoArm = audit.stride_context_on_off_comparison; - deepStrictEqual( - [ - twoArm?.summary?.total_tasks, - twoArm?.summary?.context_on_ahead_or_edge, - twoArm?.summary?.context_off_ahead, - twoArm?.tasks?.length, - ], - [10, 7, 3, 10], - "Controlled two-arm comparison summary must match the 10-task tally (7 ahead/edge, 3 OFF ahead)", + const snapshotSection = article.split('

')[1]?.split('

')[0] ?? ""; + const snapshotSvg = await readFile( + resolve(blogDir, "images/context-delivery/stride-context-on-vs-off-" + langSuffix + ".svg"), "utf8", ); - assertIncludes( - article, - '

', - `Both published editions must expose the controlled Context ON vs OFF score section: ${locale}`, - ); - assertIncludes( - article, - `stride-context-on-vs-off-${langSuffix}.svg`, - `Both published editions must embed the controlled Context ON vs OFF chart: ${langSuffix}`, - ); - const twoArmSvg = await readFile( - resolve(blogDir, `images/context-delivery/stride-context-on-vs-off-${langSuffix}.svg`), - "utf8", - ); - const twoArmSection = article.split('

')[1]?.split('

')[0] ?? ""; - const twoArmTable = twoArmSection.match(/([\s\S]*?)<\/tbody>/)?.[1]; - const twoArmRows = [...(twoArmTable ?? "").matchAll(/([\s\S]*?)<\/tr>/g)]; - deepStrictEqual( - twoArmRows.length, - twoArm.tasks.length, - `Controlled two-arm score table must retain all 10 task rows including counterexamples: ${locale}`, - ); - for (const requiredToken of ["7,159", "3,746", "2,197", "2,311", "27.1801", "13.62", "39.44", "68.89", "70.79"]) { - assertIncludes(twoArmSection, requiredToken, `Controlled score section must preserve ${requiredToken} (${locale})`); + assertIncludes(snapshotSection, "stride-context-on-vs-off-" + langSuffix + ".svg", "Both editions link the reported score figure"); + for (const source of [snapshot.source_url, snapshot.prior_public_ann_url]) { + assertIncludes(snapshotSection, source, "Both editions distinguish the original snapshot from earlier ANN evidence"); } - for (const isolatedText of [twoArmSection, twoArmSvg, JSON.stringify(twoArm)]) { - if (/four-arm|four arm|四臂/i.test(isolatedText)) { - throw new Error(`Controlled two-arm section must remain isolated without four-arm framing (${locale})`); + assertScoreTable(snapshotSection, snapshotRows, langSuffix); + assertScoreFigure(snapshotSvg, snapshotRows, langSuffix); + const index = await readFile(resolve(blogDir, locale, "index.html"), "utf8"); + const listing = index.match(/

[^<]+<\/a><\/h2>

([^<]+)<\/p>/)?.[1] ?? ""; + assertIncludes(listing, langSuffix === "zh" ? "记录分数" : "reported EdgeBench", "Blog catalog describes the recorded score pair scope"); + for (const observedText of [snapshotSection, snapshotSvg, JSON.stringify(snapshot), listing]) { + if (/controlled|fixed Explore|holding Explore|同构|受控|固定 Explore|four.arm|四臂/i.test(observedText)) { + throw new Error("Reported score snapshot restored a controlled or mixed-arm claim: " + locale); } } - for (const publicText of [article, twoArmSvg, JSON.stringify(audit)]) { + for (const publicText of [article, snapshotSvg, JSON.stringify(audit)]) { if (/sforge/i.test(publicText)) { throw new Error(`Internal benchmark name leaked into public context-delivery blog artifact (${locale})`); } @@ -466,6 +594,37 @@ async function assertClockLabelRejected(blogDir, { figure, declared, regressed } } } +async function assertScoreMismatchRejected(blogDir) { + const temporaryRoot = await mkdtemp(join(tmpdir(), "loopx-blog-score-parity-")); + const fixtureBlog = join(temporaryRoot, "blog"); + try { + await cp(blogDir, fixtureBlog, { recursive: true }); + // Each mutation changes just one published representation (or its source provenance). + const data = "data/long-horizon-audit.json"; + const en = "context-delivery/index.html"; + const zh = "zh/context-delivery/index.html"; + const svg = "images/context-delivery/stride-context-on-vs-off-en.svg"; + for (const [path, before, after, expected] of [ + [data, '"context_on_native": 80,', '"context_on_native": 5,', /snapshot direction or winner/], + [data, '"context_on_native": 80,', '"context_on_native": 81,', /table row differs.*Borden/], + [svg, "7,159 / 3,746", "7,159 / 3,747", /chart native example differs/], + [en, "33,090,430 (62.99", "13,090,430 (62.99", /table row differs.*OpenTTD/], + [zh, "(27.18 · 17h57m · 10 次)", "(27.18 · 17h57m · 1 次)", /table row differs.*Rust/], + [data, '"prior_public_ann_url": "https://github.com/', '"prior_public_ann_url": "https://example.invalid/', /immutable public sources/], + [data, '"direction": "lower",\n "metric_label_en": "Total cost"', '"direction": "higher",\n "metric_label_en": "Total cost"', /snapshot direction or winner/], + ]) { + const file = resolve(fixtureBlog, path); + const source = await readFile(file, "utf8"); + assertIncludes(source, before, "Negative fixture must contain the original value: " + path); + await writeFile(file, source.replace(before, after)); + await rejects(() => validateBilingualBlog(fixtureBlog), expected); + await writeFile(file, source); + } + } finally { + await rm(temporaryRoot, { recursive: true, force: true }); + } +} + const modulePath = fileURLToPath(import.meta.url); if (process.argv[1] && resolve(process.argv[1]) === modulePath) { const articleUrl = "https://loopx-project.github.io/loopx/blog/example/"; @@ -552,5 +711,6 @@ if (process.argv[1] && resolve(process.argv[1]) === modulePath) { }, "中文主图恢复反馈口径也必须被配对标签检查拦下", ); + await assertScoreMismatchRejected(blogDir); console.log(`blog-bilingual-index-smoke: ok (${articleSlugs.length} paired articles)`); } diff --git a/loopx/capabilities/issue_fix/README.md b/loopx/capabilities/issue_fix/README.md index 3338cccdbe..c8b4d3c8de 100644 --- a/loopx/capabilities/issue_fix/README.md +++ b/loopx/capabilities/issue_fix/README.md @@ -345,14 +345,11 @@ candidate so a maintainer can audit why that person was selected. `CODEOWNERS` remains the strongest repository-native signal. Commit volume is only evidence of familiarity; it is not proof of maintainership, availability, -or review authority. See the [reviewer recommendation -contract](docs/protocols/issue-fix-reviewer-recommendation-v0.md) for scoring, -identity, and future-signal details, and the [reviewer request -contract](docs/protocols/issue-fix-reviewer-request-v0.md) for the external-write, +or review authority. See the [reviewer recommendation contract](docs/protocols/issue-fix-reviewer-recommendation-v0.md) for scoring, +identity, and future-signal details, and the [reviewer request contract](docs/protocols/issue-fix-reviewer-request-v0.md) for the external-write, idempotency, and verification rules. Secondary delivery, dedicated-bot isolation, local-private identity mapping, and readback are defined by the -[reviewer notification sink -contract](docs/protocols/issue-fix-reviewer-notification-sinks-v0.md). +[reviewer notification sink contract](docs/protocols/issue-fix-reviewer-notification-sinks-v0.md). ### 6. PR publication and public-write boundary From f15ebdd16b5eb8cd01b2d112a3a0fe866b0f3486 Mon Sep 17 00:00:00 2001 From: "Wang, Xiang-Han" Date: Tue, 17 Nov 2026 01:40:44 +0800 Subject: [PATCH 26/34] fix(coordination): let an owner close an exact linked user gate on a single-agent goal (#8844) --- .../coordination/todo_lifecycle_decision.ts | 29 ++- .../test_canonical_handoff_invariants.py | 76 ++++++- .../todo_terminal_decision.test.ts | 193 ++++++++++++++++++ 3 files changed, 296 insertions(+), 2 deletions(-) diff --git a/loopx/control_plane/coordination/todo_lifecycle_decision.ts b/loopx/control_plane/coordination/todo_lifecycle_decision.ts index 1a0d63bc71..dbaff1eda3 100644 --- a/loopx/control_plane/coordination/todo_lifecycle_decision.ts +++ b/loopx/control_plane/coordination/todo_lifecycle_decision.ts @@ -391,7 +391,17 @@ function authority(request: LifecycleDecisionRequest): if (actor !== null) { const rejection = registeredTodoMutationRejection(todo, actor, registered); if (rejection !== null) return result("rejected", rejection); - } else if (todo.claimed_by !== null || todo.bound_agent !== null || + return { mode: "single_agent_compatibility", ownershipGate: "not_required" }; + } + // Single-agent goals have no second agent to act for the owner, and the + // terminal CLI has no human owner actor, so an actorless owner decision + // closing an exact linked user gate uses the same override as multi-agent + // mode. The override requires the exact decision scope link; unlinked + // owned/bound/excluded records still require an accountable actor. + if (exactUserGateOverride(request)) { + return { mode: "exact_user_gate_decision_scope_override", ownershipGate: "not_required" }; + } + if (todo.claimed_by !== null || todo.bound_agent !== null || todo.blocks_agent !== null || todo.excluded_agents.length > 0) { // Actorless compatibility is limited to genuinely unowned Todo records; // ownership, binding, and exclusion facts require an accountable actor. @@ -468,6 +478,23 @@ function terminalFence( ? {...lease, active: false, status: "released"} : null, }); } + // In single-agent goals there is no peer set to attribute an execution + // lease across, and the terminal CLI has no human owner actor: an owner + // decision on an exact linked user gate records a decision rather than + // executing work, and retires any expired lease lineage with the Todo + // transition. Multi-agent hard-lease goals keep their attributed-actor + // gate contract, registered actors keep the ordinary lease requirement, and + // an active holder or an explicitly supplied fence is never bypassed. + if (request.registered_agents.length <= 1 && request.command === "complete" && + authorityMode === "exact_user_gate_decision_scope_override" && + request.todo.role === "user" && request.todo.task_class === "user_gate" && + request.actor_agent_id === null && !timeActive && !explicitFence) { + return result("apply", "terminal_fence_not_required", { + authority_mode: authorityMode, lease_fence: "not_required", + next_lease: lease?.present && lease.status !== "released" + ? {...lease, active: false, status: "released"} : null, + }); + } // Deferred work cannot acquire a lease. Superseding a retired wait is a // terminal lifecycle edit, not execution, and the provider CAS retires any // expired lease lineage together with the Todo transition. diff --git a/tests/control_plane/test_canonical_handoff_invariants.py b/tests/control_plane/test_canonical_handoff_invariants.py index 1159645ece..88b10abc4d 100644 --- a/tests/control_plane/test_canonical_handoff_invariants.py +++ b/tests/control_plane/test_canonical_handoff_invariants.py @@ -58,6 +58,7 @@ def _write_workspace( *, handoff_mode: str | None = None, lifecycle_authority: list[dict[str, Any]] | None = None, + registered_agents: list[str] | None = None, ) -> tuple[Path, Path]: repo = tmp_path / "repo" repo.mkdir(exist_ok=True) @@ -76,7 +77,11 @@ def _write_workspace( ) coordination: dict[str, Any] = { "agent_model": "peer_v1", - "registered_agents": [AGENT_A, AGENT_B, ORCHESTRATOR], + "registered_agents": ( + [AGENT_A, AGENT_B, ORCHESTRATOR] + if registered_agents is None + else list(registered_agents) + ), } if lifecycle_authority is not None: coordination["todo_lifecycle_authority"] = lifecycle_authority @@ -777,6 +782,75 @@ def test_exact_user_gate_completion_preserves_execution_fence( assert result["decision_scope_resolution"]["state"] == "resolved" +@pytest.mark.parametrize("mode", ["soft_claim", "hard_lease"]) +@pytest.mark.parametrize( + "agents", + [[AGENT_A], [AGENT_A, AGENT_B, ORCHESTRATOR]], + ids=["single-agent", "multi-agent"], +) +def test_owner_closes_exact_user_gate_without_an_agent_actor( + tmp_path, canonical_workspace, mode, agents +): + """The owner who is not registered as an agent can close a single-agent gate. + + A single-agent goal has no second agent to act for the owner, and the + terminal CLI has no human owner actor. Multi-agent hard-lease goals retain + their attributed-actor execution fence contract when no actor is named. + """ + registry, state = canonical_workspace( + tmp_path, handoff_mode=mode, registered_agents=agents + ) + target, gate = _add_exact_user_gate_pair(registry) + unlinked = add_goal_todo( + registry_path=registry, + goal_id=GOAL_ID, + role="user", + text="Confirm one unlinked owner decision.", + task_class="user_gate", + blocks_agent=AGENT_A, + ) + before, display = _snapshot(registry), state.read_bytes() + with pytest.raises(LocalCoordinationAuthorityRejection) as error: + _terminal( + registry, + unlinked["todo_id"], + role="user", + decision_outcome="approve", + agent_id=None, + ) + assert error.value.code == "actor_required" + _unchanged(registry, state, before, display) + + if mode == "hard_lease" and len(agents) > 1: + with pytest.raises(LocalCoordinationAuthorityRejection) as error: + _terminal( + registry, + gate["todo_id"], + role="user", + decision_outcome="approve", + agent_id=None, + ) + assert error.value.code == "handoff_mode_requires_lease" + _unchanged(registry, state, before, display) + return + + result = _terminal( + registry, + gate["todo_id"], + role="user", + decision_outcome="approve", + agent_id=None, + ) + assert ( + result["mutation_authority"]["authority_mode"] + == "exact_user_gate_decision_scope_override" + ) + assert result["task_lease_fence"]["lease_fence"] == "not_required" + assert _agent_todo(state, gate["todo_id"])["status"] == "done" + assert not _agent_todo(state, target["todo_id"]).get("required_decision_scopes") + assert result["decision_scope_resolution"]["state"] == "resolved" + + def test_bound_user_action_completes_without_gate_or_execution_claim( tmp_path, canonical_workspace ): diff --git a/tests/control_plane_ts/todo_terminal_decision.test.ts b/tests/control_plane_ts/todo_terminal_decision.test.ts index 553a066ef9..a3e25a9900 100644 --- a/tests/control_plane_ts/todo_terminal_decision.test.ts +++ b/tests/control_plane_ts/todo_terminal_decision.test.ts @@ -349,6 +349,199 @@ test("exact linked user gate decision scope does not invent Agent authority", () assert.equal(mismatch.code, "actor_required"); }); +test("single-agent goals consult the exact user gate override for owner decisions", () => { + const scope = {kind: "direction", granularity: "action", scope_key: "release"}; + const gate = { + todo_id: "todo_gate", + status: "open", + role: "user", + task_class: "user_gate", + claimed_by: null, + excluded_agents: [], + bound_agent: "agent-a", + blocks_agent: null, + decision_scope: scope, + required_decision_scopes: [], + unblocks_todo_id: "todo_target", + }; + const target = { + ...request().todo as object, + todo_id: "todo_target", + required_decision_scopes: [scope], + }; + // Single registered agent: an owner decision (no agent actor) closing the + // exact linked gate must use the override instead of failing actor admission. + const accepted = evaluateCoordinationTodoTerminalDecision(request({ + registered_agents: ["agent-a"], + todo: gate, + decision_target: target, + actor_agent_id: null, + decision_outcome: "approve", + })); + assert.equal(accepted.outcome, "apply"); + assert.equal(accepted.authority_mode, "exact_user_gate_decision_scope_override"); + + // Without the exact scope link the override does not fire: the ownership + // facts still require an accountable actor. + const unlinked = evaluateCoordinationTodoTerminalDecision(request({ + registered_agents: ["agent-a"], + todo: gate, + decision_target: { ...target, required_decision_scopes: [] }, + actor_agent_id: null, + decision_outcome: "approve", + })); + assert.equal(unlinked.outcome, "rejected"); + assert.equal(unlinked.code, "actor_required"); + + // Registered actors keep ordinary single-agent compatibility, while + // unregistered or excluded actors remain rejected even when the scope link + // matches. + const viaActor = evaluateCoordinationTodoTerminalDecision(request({ + registered_agents: ["agent-a"], + todo: gate, + decision_target: target, + actor_agent_id: "agent-a", + decision_outcome: "approve", + })); + assert.equal(viaActor.outcome, "apply"); + assert.equal(viaActor.authority_mode, "single_agent_compatibility"); + + const unregistered = evaluateCoordinationTodoTerminalDecision(request({ + registered_agents: ["agent-a"], + todo: gate, + decision_target: target, + actor_agent_id: "bogus", + decision_outcome: "approve", + })); + assert.equal(unregistered.outcome, "rejected"); + assert.equal(unregistered.code, "actor_not_registered"); + + const excluded = evaluateCoordinationTodoTerminalDecision(request({ + registered_agents: ["agent-a"], + todo: { ...gate, excluded_agents: ["agent-a"] }, + decision_target: target, + actor_agent_id: "agent-a", + decision_outcome: "approve", + })); + assert.equal(excluded.outcome, "rejected"); + assert.equal(excluded.code, "actor_excluded"); +}); + +test("actorless owner gate decisions do not require an execution lease", () => { + const scope = {kind: "direction", granularity: "action", scope_key: "release"}; + const gate = { + todo_id: "todo_gate", + status: "open", + role: "user", + task_class: "user_gate", + claimed_by: null, + excluded_agents: [], + bound_agent: null, + blocks_agent: "agent-a", + decision_scope: scope, + required_decision_scopes: [], + unblocks_todo_id: "todo_target", + }; + const target = { + ...request().todo as object, + todo_id: "todo_target", + required_decision_scopes: [scope], + }; + // A single-agent hard-lease goal has no peer set or agent actor to hold a + // lease for an owner decision: the gate records a decision, it does not execute work. + const owner = evaluateCoordinationTodoTerminalDecision(request({ + registered_agents: ["agent-a"], + handoff_mode: "hard_lease", + todo: gate, + decision_target: target, + actor_agent_id: null, + decision_outcome: "approve", + })); + assert.equal(owner.outcome, "apply"); + assert.equal(owner.authority_mode, "exact_user_gate_decision_scope_override"); + assert.equal(owner.lease_fence, "not_required"); + assert.equal(owner.next_lease, null); + + // A registered actor keeps the ordinary lease requirement: the owner + // exception never widens an agent's execution admission. + const actor = evaluateCoordinationTodoTerminalDecision(request({ + registered_agents: ["agent-a"], + handoff_mode: "hard_lease", + todo: gate, + decision_target: target, + actor_agent_id: "agent-a", + decision_outcome: "approve", + })); + assert.equal(actor.outcome, "rejected"); + assert.equal(actor.code, "handoff_mode_requires_lease"); + assert.equal(actor.lease_fence, "required"); + + // Any expired lease lineage is retired together with the Todo transition, + // while an active holder or explicit lease fence is never bypassed. + const expiredLease = { + present: true, + active: false, + status: "active", + owner: "agent-a", + idempotency_key: "old-lease", + version: 2, + lease_epoch: 1, + write_scopes: [], + }; + const retired = evaluateCoordinationTodoTerminalDecision(request({ + registered_agents: ["agent-a"], + handoff_mode: "hard_lease", + todo: gate, + decision_target: target, + lease: expiredLease, + actor_agent_id: null, + decision_outcome: "approve", + })); + assert.equal(retired.outcome, "apply"); + assert.equal(retired.lease_fence, "not_required"); + assert.equal(retired.next_lease?.status, "released"); + + const activeHolder = evaluateCoordinationTodoTerminalDecision(request({ + registered_agents: ["agent-a"], + handoff_mode: "hard_lease", + todo: gate, + decision_target: target, + lease: { ...expiredLease, active: true }, + actor_agent_id: null, + decision_outcome: "approve", + })); + assert.equal(activeHolder.outcome, "rejected"); + assert.equal(activeHolder.code, "lease_fence_required"); + + const explicitFence = evaluateCoordinationTodoTerminalDecision(request({ + registered_agents: ["agent-a"], + handoff_mode: "hard_lease", + todo: gate, + decision_target: target, + actor_agent_id: null, + decision_outcome: "approve", + lease_idempotency_key: "stale-key", + })); + assert.equal(explicitFence.outcome, "rejected"); + assert.equal(explicitFence.code, "handoff_mode_requires_lease"); + + // Multi-agent hard-lease goals preserve their attributed-actor contract + // even when auto-acquire is enabled for the gate. + for (const allowAuto of [false, true]) { + const multiAgent = evaluateCoordinationTodoTerminalDecision(request({ + registered_agents: ["agent-a", "agent-b"], + handoff_mode: "hard_lease", + allow_user_gate_auto_acquire: allowAuto, + todo: gate, + decision_target: target, + actor_agent_id: null, + decision_outcome: "approve", + })); + assert.equal(multiAgent.outcome, "rejected"); + assert.equal(multiAgent.code, "handoff_mode_requires_lease"); + } +}); + test("hard-lease terminal transition releases the exact owned generation", () => { const lease = { present: true, From c706caad2c9eca8fb54c7da6557cc8952324ab83 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Tue, 17 Nov 2026 03:01:17 +0800 Subject: [PATCH 27/34] fix(authority): scope remote Todo reads to the active Goal (#8861) Bind scoped Todo reads to the active Goal lifetime and original creation or first-bind evidence. Keep nested read projections minimal and retain trusted v0 behavior. Independent exact-head review and local PostgreSQL/TLS, package, and integration qualification completed before merge. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../remote-authority-task-service-v0.md | 77 +++++ .../coordination/remote-authority-service.ts | 4 +- .../coordination/remote_authority_contract.ts | 69 +++-- .../coordination/remote_authority_policy.ts | 86 +++++- .../coordination/remote_authority_service.ts | 144 +++++++-- .../goals/acceptance_authority.ts | 4 +- .../goals/acceptance_lifecycle.ts | 1 + .../remote_authority.integration.test.ts | 2 + .../control_plane_ts/remote_authority.test.ts | 62 +++- .../remote_authority_fixture.ts | 3 +- .../remote_authority_read_acl_conformance.ts | 277 ++++++++++++++++++ 11 files changed, 671 insertions(+), 58 deletions(-) create mode 100644 tests/control_plane_ts/remote_authority_read_acl_conformance.ts diff --git a/docs/reference/remote-authority-task-service-v0.md b/docs/reference/remote-authority-task-service-v0.md index 3e9ec583c2..aa4063fa51 100644 --- a/docs/reference/remote-authority-task-service-v0.md +++ b/docs/reference/remote-authority-task-service-v0.md @@ -56,6 +56,83 @@ Revocation prevents subsequent admissions. A request already admitted under a policy snapshot may finish and retains that snapshot's revision in its receipt; revocation does not claim to undo an in-flight commit. +### Scoped Todo reads + +For a read-only listener with resource authorization, set `schema_version` to +`loopx_remote_authority_scoped_read_policy_v1` in the existing service policy. +Each scope requires `todo_read_acl`, an array of entries with `goal_ref`, +`todo_id`, `principal_id` and `actor_id`. Each credential grant also requires +`goal_ref` with the exact `goal_id` and `goal_instance_id`. Grant `read` or +`snapshot` for the actions the caller needs. The operator must approve each +Todo and audience for that lifetime. Grant and ACL entries must agree with the +service scope, authenticated principal and actor. An empty ACL grants no access. + +For current Todo or snapshot reads, each entry also needs +`todo_origin_operation_id`: the canonical operation id of that Todo's successful +creation under this exact lifetime, or the trusted source owner's successful +first `bind_existing` transaction that adopted the existing Todo. The service +verifies that original receipt and projection through the existing PostgreSQL +checkpoint owner, bounded by the admitted current head. An ACL is permission, +not proof of ownership: a claim, update, successor activation, no-op rebind, +missing receipt or predecessor's receipt cannot move retained work into a new +lifetime. Entries without a valid origin are excluded; a direct/targeted read +without proof, or a snapshot with no proven resources, is rejected. Shared +first-bind proofs are read once per request; no authorization is cached between +requests. The origin operation id stays in the private policy and is not +returned to callers. Existing bound work without such evidence stays unavailable +until the source owner supplies valid historical proof; do not rebind or rewrite +its history to manufacture one. + +Launch the service with the scoped policy and use the current TLS client with +the existing v0 request JSON. The server supplies the exact Goal reference +from its private policy and verifies it against the active lifecycle on the +same PostgreSQL head used to compose the response. Unbound, retiring and +mismatched lifetimes deny access. A direct read or targeted snapshot outside +the ACL returns rejection without revealing whether the Todo exists. The +scoped listener denies write commands. + +Scoped successes use `loopx_remote_authority_scoped_read_response_v1`. They +return the exact Goal reference, authorized Todo status and assignment, and +matching lease metadata. Snapshots also include role and priority. Free-form +notes and lease keys are withheld. Scoped clients reject unknown nested Todo +and lease fields even when a snapshot digest matches; the trusted v0 decoder +keeps its existing compatibility behavior. The snapshot receipt digest covers only the +authorized projection and its scope. The global provider revision is withheld, +so changing a hidden Todo leaves the returned view and digest intact. Clients +that lack support for this response version report an unavailable read. + +Revoke access by replacing the policy with a new version of the same schema +that removes the matching ACL entry or grant. The service pins the policy +schema at startup and fails closed on a schema change during reload. Stop the +listener to disable it. Keep the v0 policy for an explicitly configured +legacy listener with trusted access to the whole Goal. + +#### Historical receipt proof + +To authorize recovery of a specific remote operation, add its canonical +`operation_id` to `receipt_operation_ids` on the Todo's read ACL entry. The +credential also needs `read` in its grant. The ID comes from the original +operation or its trusted service receipt. An omitted or empty list grants +no receipt access. Use `--recover` with the complete original command. + +The service checks the active Goal and allowed Todo before it opens the +receipt. The original request digest, principal and actor must match the +receipt. The original committed projection must prove the same Goal lifetime. +The service reuses the provider's bounded checkpoint and delta reader for that +exact cursor. Missing, unbound or cross-lifetime receipts are rejected, even +with a matching operation ID. The scoped service role needs read access to the +existing tenant-scoped `authority_checkpoints` table as part of this journal read. + +A scoped success returns the exact Goal, Todo and operation, `historical_only` +and the original receipt digest. It withholds the original result payload, +lease key, cursor and provider revision. The original receipt confirms +recorded history. Execution authority still comes from a current lease and +the existing mutation owner. Remove the operation ID to revoke recovery while +leaving the authorized Todo read available. + +Artifact versions, delegated sources and cross-person writes require separate +qualification before they can be exposed. + ## Commands and recovery POST JSON to `/v0/tasks/execute` with a `Bearer` credential. Every command includes diff --git a/examples/coordination/remote-authority-service.ts b/examples/coordination/remote-authority-service.ts index 45a98cab92..f4b9507dee 100644 --- a/examples/coordination/remote-authority-service.ts +++ b/examples/coordination/remote-authority-service.ts @@ -12,12 +12,12 @@ const port = Number(values.port); if (!values.policy || !values.cert || !values.key || !Number.isSafeInteger(port) || port < 0 || port > 65535 || !process.env.LOOPX_AUTHORITY_DATABASE_URL) throw new Error("Provide policy, TLS certificate/key and service-owned database configuration"); // Validate before listening; updates are read again for each admission. -await readRemoteAuthorityPolicy(values.policy); +const policy = await readRemoteAuthorityPolicy(values.policy); const pool = new Pool({connectionString: process.env.LOOPX_AUTHORITY_DATABASE_URL, max: 8, connectionTimeoutMillis: 2000, query_timeout: 6000, idleTimeoutMillis: 10000, statement_timeout: 5000, lock_timeout: 2000, idle_in_transaction_session_timeout: 10000}); pool.on("error", () => process.stderr.write("Authority database connection unavailable\n")); -const service = new RemoteAuthorityService({admit: fileRemoteAuthorityAdmission(values.policy), +const service = new RemoteAuthorityService({admit: fileRemoteAuthorityAdmission(values.policy, String(policy.schema_version)), database: {connect: async () => { const connection = await pool.connect(); return {query: (sql, args) => connection.query(sql, args ? [...args] : undefined), diff --git a/loopx/control_plane/coordination/remote_authority_contract.ts b/loopx/control_plane/coordination/remote_authority_contract.ts index d37ef5f7bb..131b35fb73 100644 --- a/loopx/control_plane/coordination/remote_authority_contract.ts +++ b/loopx/control_plane/coordination/remote_authority_contract.ts @@ -1,7 +1,7 @@ /** Candidate wire contract. Clients submit intentions, never authority facts. */ import type {JsonObject} from "../effect_program.ts"; import {canonicalAuthorityObject, canonicalAuthoritySha256, - requireAuthorityStoreId} from "./authority_store_codec.ts"; + hasExactAuthorityKeys, requireAuthorityStoreId} from "./authority_store_codec.ts"; import {POSTGRESQL_STORE_IDENTITY_PATTERN} from "./postgresql_authority_store.ts"; import {taskLeaseOperationIdentity} from "../work_items/task_lease_operation_identity.ts"; import {normalizeTodoId, normalizeGoalId, normalizeOwner} from "../work_items/task_lease_acquire.ts"; @@ -9,9 +9,11 @@ import {BARE_SHA256_PATTERN} from "../content_digest.ts"; import {parseIsoTimestamp} from "../runtime_timestamp.ts"; import {normalizeTodoResumeWhen, TODO_RESUME_NORMALIZE_REQUEST_SCHEMA_VERSION} from "../todos/resume_condition.ts"; import {normalizeTodoPriority} from "../todos/priority.ts"; +import {parseWireExactGoalRef, type WireExactGoalRef} from "../goals/acceptance_lifecycle.ts"; export const REMOTE_AUTHORITY_REQUEST = "loopx_remote_authority_request_v0"; export const REMOTE_AUTHORITY_RESPONSE = "loopx_remote_authority_response_v0"; +export const REMOTE_AUTHORITY_SCOPED_READ_RESPONSE = "loopx_remote_authority_scoped_read_response_v1"; export const REMOTE_AUTHORITY_OPERATIONS = [ "read", "claim", "progress", "renew", "release", "complete", "block", "defer", "todo_add", "snapshot", @@ -173,16 +175,18 @@ export function remoteAuthorityOperationId(request: RemoteAuthorityRequest, prin request_id: requestId ?? ("request_id" in request ? request.request_id : "")})}`; } export function remoteAuthorityResponse(request: unknown, mode: RemoteAuthorityMode, - payload: JsonObject): JsonObject { - return {...payload, schema_version: REMOTE_AUTHORITY_RESPONSE, + payload: JsonObject, schema: typeof REMOTE_AUTHORITY_RESPONSE | typeof REMOTE_AUTHORITY_SCOPED_READ_RESPONSE = REMOTE_AUTHORITY_RESPONSE): JsonObject { + return {...payload, schema_version: schema, request_sha256: canonicalAuthoritySha256({mode, request})}; } export function remoteAuthoritySnapshotDigest(request: RemoteAuthorityScope, revision: unknown, - todos: readonly unknown[], leases: readonly unknown[]): string { - return canonicalAuthoritySha256({schema_version: "loopx_goal_snapshot_receipt_v0", - tenant_id: request.tenant_id, goal_id: request.goal_id, store_identity: request.store_identity, - provider_revision: revision, todos, leases}); + todos: readonly unknown[], leases: readonly unknown[], goalRef?: WireExactGoalRef): string { + const evidence = goalRef + ? {schema_version: REMOTE_AUTHORITY_SCOPED_READ_RESPONSE, goal_ref: goalRef} + : {schema_version: "loopx_goal_snapshot_receipt_v0", provider_revision: revision}; + return canonicalAuthoritySha256({...evidence, tenant_id: request.tenant_id, + goal_id: request.goal_id, store_identity: request.store_identity, todos, leases}); } function responseRequires(condition: unknown): asserts condition { @@ -199,8 +203,14 @@ function responseOperationId(value: unknown, request: RemoteAuthorityRequest): s : id.startsWith("remote-task:") && BARE_SHA256_PATTERN.test(id.slice("remote-task:".length))); return id; } -function responseLease(raw: unknown, request: RemoteAuthorityRequest, mutation: boolean): JsonObject { +function responseFields(value: JsonObject, allowed: readonly string[]): void { + responseRequires(Object.keys(value).every(key => allowed.includes(key))); +} +function responseLease(raw: unknown, request: RemoteAuthorityRequest, mutation: boolean, + scoped: boolean = false, snapshot: boolean = false): JsonObject { const lease = canonicalAuthorityObject(raw, "response lease"); + if (scoped) responseFields(lease, ["owner", "status", "expires_at", "version", "lease_epoch", + ...(snapshot ? ["goal_id", "todo_id"] : [])]); responseRequires(typeof lease.owner === "string" && normalizeOwner(lease.owner) === lease.owner && (lease.status === "active" || lease.status === "released") && Number.isSafeInteger(lease.version) && Number(lease.version) >= 0 && @@ -220,8 +230,9 @@ function responseLease(raw: unknown, request: RemoteAuthorityRequest, mutation: return lease; } -function responseTodo(raw: unknown, todoId?: string): string { +function responseTodo(raw: unknown, todoId?: string, scoped: boolean = false, snapshot: boolean = false): string { const todo = canonicalAuthorityObject(raw, "response Todo"); + if (scoped) responseFields(todo, ["todo_id", "status", "claimed_by", ...(snapshot ? ["role", "priority"] : [])]); const id = normalizeTodoId(todo.todo_id); responseRequires(todo.todo_id === id && (todoId === undefined || id === todoId)); remoteAuthorityId(todo.status, "Todo status"); @@ -231,22 +242,23 @@ function responseTodo(raw: unknown, todoId?: string): string { return id; } -function responseSnapshot(value: JsonObject, request: Extract): void { +function responseSnapshot(value: JsonObject, request: Extract, + goalRef?: WireExactGoalRef): void { responseRequires(value.status === "snapshot" && value.goal_id === request.goal_id && value.store_identity === request.store_identity && Array.isArray(value.todos) && Array.isArray(value.leases)); - responseRevision(value.provider_revision, request); - const todoIds = value.todos.map(todo => responseTodo(todo, request.todo_id)); + if (!goalRef) responseRevision(value.provider_revision, request); + const todoIds = value.todos.map(todo => responseTodo(todo, request.todo_id, goalRef !== undefined, true)); const todoSet = new Set(todoIds); responseRequires(todoSet.size === todoIds.length); const leaseIds = new Set(); for (const raw of value.leases) { - const lease = responseLease(raw, request, false); + const lease = responseLease(raw, request, false, goalRef !== undefined, true); const id = normalizeTodoId(lease.todo_id); responseRequires(lease.todo_id === id && lease.goal_id === request.goal_id && todoSet.has(id) && !leaseIds.has(id) && !Object.hasOwn(lease, "idempotency_key")); leaseIds.add(id); } - responseRequires(value.receipt_sha256 === remoteAuthoritySnapshotDigest(request, value.provider_revision, value.todos, value.leases)); + responseRequires(value.receipt_sha256 === remoteAuthoritySnapshotDigest(request, value.provider_revision, value.todos, value.leases, goalRef)); } function responseCommand(raw: unknown, request: RemoteAuthorityRequest, operationId: string, historical: boolean): void { @@ -301,9 +313,28 @@ function responseCommand(raw: unknown, request: RemoteAuthorityRequest, operatio export function decodeRemoteAuthorityResponse(raw: unknown, request: RemoteAuthorityRequest, mode: RemoteAuthorityMode): JsonObject { const value = canonicalAuthorityObject(raw, "remote authority response"); - responseRequires(value.schema_version === REMOTE_AUTHORITY_RESPONSE && + const scoped = value.schema_version === REMOTE_AUTHORITY_SCOPED_READ_RESPONSE; + responseRequires((value.schema_version === REMOTE_AUTHORITY_RESPONSE || scoped) && value.request_sha256 === canonicalAuthoritySha256({mode, request})); responseRequires(typeof value.status === "string"); + let goalRef: WireExactGoalRef | undefined; + if (scoped) { + goalRef = parseWireExactGoalRef(value.goal_ref, "scoped read Goal"); + responseRequires(goalRef.goal_id === request.goal_id); + if (mode === "recover") { + responseRequires(request.operation !== "read" && request.operation !== "snapshot" && + value.status === "found" && value.historical_only === true && value.todo_id === request.todo_id && + typeof value.receipt_sha256 === "string" && BARE_SHA256_PATTERN.test(value.receipt_sha256)); + responseRequires(hasExactAuthorityKeys(value, ["schema_version", "request_sha256", "status", "goal_ref", + "historical_only", "todo_id", "operation_id", "receipt_sha256"])); + responseOperationId(value.operation_id, request); + return value; + } + responseRequires(mode === "execute" && (request.operation === "read" && value.status === "found" + || request.operation === "snapshot" && value.status === "snapshot")); + const fields = value.status === "found" ? ["todo", "lease"] : ["goal_id", "store_identity", "receipt_sha256", "todos", "leases"]; + responseRequires(hasExactAuthorityKeys(value, ["schema_version", "request_sha256", "status", "goal_ref", ...fields])); + } if (["rejected", "unavailable", "ambiguous"].includes(value.status)) { remoteAuthorityId(value.reason_code, "response reason"); responseRequires(value.status !== "ambiguous" || mode === "execute" && request.operation !== "read" && request.operation !== "snapshot"); @@ -319,12 +350,12 @@ export function decodeRemoteAuthorityResponse(raw: unknown, request: RemoteAutho } else if (mode === "execute" && request.operation === "read") { if (value.status !== "missing") { responseRequires(value.status === "found"); - responseRevision(value.provider_revision, request); - responseTodo(value.todo, request.todo_id); - if (value.lease !== null) responseLease(value.lease, request, false); + if (!goalRef) responseRevision(value.provider_revision, request); + responseTodo(value.todo, request.todo_id, scoped); + if (value.lease !== null) responseLease(value.lease, request, false, scoped); } } else if (mode === "execute" && request.operation === "snapshot") { - responseSnapshot(value, request); + responseSnapshot(value, request, goalRef); } else { responseRequires(mode === "execute" && request.operation !== "read" && request.operation !== "snapshot" && value.status === "result"); responseCommand(value.result, request, responseOperationId(value.operation_id, request), false); diff --git a/loopx/control_plane/coordination/remote_authority_policy.ts b/loopx/control_plane/coordination/remote_authority_policy.ts index 712d22a2e8..79b09459e6 100644 --- a/loopx/control_plane/coordination/remote_authority_policy.ts +++ b/loopx/control_plane/coordination/remote_authority_policy.ts @@ -4,30 +4,63 @@ import {createHash, timingSafeEqual} from "node:crypto"; import {open} from "node:fs/promises"; import type {JsonObject} from "../effect_program.ts"; import {BARE_SHA256_PATTERN} from "../content_digest.ts"; +import {parseWireExactGoalRef, sameExactGoalRef, type WireExactGoalRef} from "../goals/acceptance_lifecycle.ts"; +import {normalizeTodoId} from "../work_items/task_lease_acquire.ts"; import {canonicalAuthorityObject, canonicalAuthoritySha256, hasExactAuthorityKeys} from "./authority_store_codec.ts"; import {POSTGRESQL_STORE_IDENTITY_PATTERN} from "./postgresql_authority_store.ts"; -import {REMOTE_AUTHORITY_OPERATIONS, remoteAuthorityId, type RemoteAuthorityRequest} from "./remote_authority_contract.ts"; +import {REMOTE_AUTHORITY_OPERATIONS, remoteAuthorityId, remoteAuthorityOperationId, + type RemoteAuthorityMode, type RemoteAuthorityRequest} from "./remote_authority_contract.ts"; import type {RemoteAuthorityAdmission} from "./remote_authority_service.ts"; export const REMOTE_AUTHORITY_POLICY = "loopx_remote_authority_policy_v0"; +export const REMOTE_AUTHORITY_SCOPED_READ_POLICY = "loopx_remote_authority_scoped_read_policy_v1"; const sameScope = (a: JsonObject, b: {tenant_id: string; goal_id: string; store_identity: string}) => a.tenant_id === b.tenant_id && a.goal_id === b.goal_id && a.store_identity === b.store_identity; -function object(value: unknown, keys: string[], name: string): JsonObject { +function object(value: unknown, keys: string[], name: string, optionalKeys: string[] = []): JsonObject { const result = canonicalAuthorityObject(value, name); - if (!hasExactAuthorityKeys(result, keys)) throw new TypeError(`Invalid ${name}`); + if (!hasExactAuthorityKeys(result, [...keys, ...optionalKeys.filter(key => Object.hasOwn(result, key))])) { + throw new TypeError(`Invalid ${name}`); + } return result; } export function decodeRemoteAuthorityPolicy(value: unknown): JsonObject { const policy = object(value, ["schema_version", "scopes", "principals"], "service policy"); - if (policy.schema_version !== REMOTE_AUTHORITY_POLICY || !Array.isArray(policy.scopes) || + const scoped = policy.schema_version === REMOTE_AUTHORITY_SCOPED_READ_POLICY; + if (!(scoped || policy.schema_version === REMOTE_AUTHORITY_POLICY) || !Array.isArray(policy.scopes) || !Array.isArray(policy.principals) || policy.scopes.length > 128 || policy.principals.length > 256) throw new TypeError("Invalid service policy"); const scopes = policy.scopes.map(raw => { - const scope = object(raw, ["tenant_id", "goal_id", "store_identity", "registered_agents"], "service scope"); + const scope = object(raw, ["tenant_id", "goal_id", "store_identity", "registered_agents", + ...(scoped ? ["todo_read_acl"] : [])], "service scope"); remoteAuthorityId(scope.tenant_id, "tenant"); remoteAuthorityId(scope.goal_id, "Goal"); if (typeof scope.store_identity !== "string" || !POSTGRESQL_STORE_IDENTITY_PATTERN.test(scope.store_identity) || !Array.isArray(scope.registered_agents) || scope.registered_agents.length === 0 || scope.registered_agents.length > 128) throw new TypeError("Invalid service scope"); scope.registered_agents.forEach(a => remoteAuthorityId(a, "registered actor")); if (new Set(scope.registered_agents).size !== scope.registered_agents.length) throw new TypeError("Duplicate registered actor"); + if (scoped) { + if (!Array.isArray(scope.todo_read_acl)) throw new TypeError("Invalid Todo read ACL"); + const actors = new Set(scope.registered_agents), entries = new Set(); + for (const rawEntry of scope.todo_read_acl) { + const entry = object(rawEntry, ["goal_ref", "todo_id", "principal_id", "actor_id"], + "Todo read ACL entry", ["receipt_operation_ids", "todo_origin_operation_id"]); + const goal = parseWireExactGoalRef(entry.goal_ref, "Todo read ACL Goal"); + remoteAuthorityId(entry.principal_id, "ACL principal"); + remoteAuthorityId(entry.todo_id, "ACL Todo"); + if (goal.goal_id !== scope.goal_id || !actors.has(entry.actor_id) || normalizeTodoId(entry.todo_id) !== entry.todo_id) { + throw new TypeError("Invalid Todo read ACL scope"); + } + const key = JSON.stringify([goal.goal_instance_id, entry.todo_id, entry.principal_id, entry.actor_id]); + if (entries.has(key)) throw new TypeError("Duplicate Todo read ACL entry"); + entries.add(key); + if (Object.hasOwn(entry, "todo_origin_operation_id")) { + remoteAuthorityId(entry.todo_origin_operation_id, "Todo origin operation"); + } + if (Object.hasOwn(entry, "receipt_operation_ids")) { + const ids = entry.receipt_operation_ids; + if (!Array.isArray(ids) || new Set(ids).size !== ids.length) throw new TypeError("Invalid receipt read ACL"); + ids.forEach(id => remoteAuthorityId(id, "receipt ACL operation")); + } + } + } return scope; }); const scopeKeys = scopes.map(s => JSON.stringify([s.tenant_id, s.goal_id])); @@ -39,12 +72,15 @@ export function decodeRemoteAuthorityPolicy(value: unknown): JsonObject { !Array.isArray(principal.grants) || principal.grants.length > 128) throw new TypeError("Invalid principal"); const seen = new Set(); principal.grants.forEach(rawGrant => { - const grant = object(rawGrant, ["tenant_id", "goal_id", "actor_id", "operations"], "grant"); + const grant = object(rawGrant, ["tenant_id", "goal_id", "actor_id", "operations", + ...(scoped ? ["goal_ref"] : [])], "grant"); const scope = scopes.find(s => s.tenant_id === grant.tenant_id && s.goal_id === grant.goal_id); if (!scope || !(scope.registered_agents as string[]).includes(String(grant.actor_id)) || !Array.isArray(grant.operations) || grant.operations.length === 0 || grant.operations.some(op => !REMOTE_AUTHORITY_OPERATIONS.includes(op as typeof REMOTE_AUTHORITY_OPERATIONS[number])) || new Set(grant.operations).size !== grant.operations.length) throw new TypeError("Invalid grant"); + if (scoped && (parseWireExactGoalRef(grant.goal_ref, "read grant Goal").goal_id !== grant.goal_id || + grant.operations.some(op => op !== "read" && op !== "snapshot"))) throw new TypeError("Invalid scoped read grant"); const key = JSON.stringify([grant.tenant_id, grant.goal_id, grant.actor_id]); if (seen.has(key)) throw new TypeError("Duplicate grant"); seen.add(key); @@ -58,24 +94,50 @@ export function decodeRemoteAuthorityPolicy(value: unknown): JsonObject { } export function remoteAuthorityPolicyAdmission(policy: JsonObject, credential: string, - request: RemoteAuthorityRequest): RemoteAuthorityAdmission { + request: RemoteAuthorityRequest, mode: RemoteAuthorityMode = "execute"): RemoteAuthorityAdmission { const digest = createHash("sha256").update(credential).digest(); const principal = (policy.principals as JsonObject[]).find(p => timingSafeEqual(Buffer.from(String(p.credential_sha256), "hex"), digest)); if (!principal) return {status: "rejected", reason_code: "principal_unauthenticated"}; const scope = (policy.scopes as JsonObject[]).find(s => sameScope(s, request)); + const scoped = policy.schema_version === REMOTE_AUTHORITY_SCOPED_READ_POLICY; + const recoverScoped = scoped && mode === "recover"; + const operation = recoverScoped ? "read" : request.operation; const grant = (principal.grants as JsonObject[]).find(g => g.tenant_id === request.tenant_id && - g.goal_id === request.goal_id && g.actor_id === request.actor_id && (g.operations as string[]).includes(request.operation)); + g.goal_id === request.goal_id && g.actor_id === request.actor_id && (g.operations as string[]).includes(operation)); if (!scope || !grant) return {status: "rejected", reason_code: "remote_authority_scope_denied"}; - return {status: "allowed", principal_id: String(principal.principal_id), + const allowed = {status: "allowed" as const, principal_id: String(principal.principal_id), policy_revision: canonicalAuthoritySha256(policy), registered_agents: [...scope.registered_agents as string[]]}; + if (!scoped) return allowed; + + const goalRef = grant.goal_ref as WireExactGoalRef; + const entries = (scope.todo_read_acl as JsonObject[]) + .filter(entry => entry.principal_id === principal.principal_id && entry.actor_id === request.actor_id && + sameExactGoalRef(entry.goal_ref as WireExactGoalRef, goalRef)); + const todoIds = entries.map(entry => String(entry.todo_id)); + if (todoIds.length === 0 || request.todo_id !== undefined && !todoIds.includes(request.todo_id)) { + return {status: "rejected", reason_code: "remote_authority_scope_denied"}; + } + const readScope = {goal_ref: goalRef, todo_ids: todoIds, + todo_origins: Object.fromEntries(entries.filter(entry => entry.todo_origin_operation_id !== undefined) + .map(entry => [String(entry.todo_id), String(entry.todo_origin_operation_id)]))}; + if (!recoverScoped) return {...allowed, read_scope: readScope}; + const id = remoteAuthorityOperationId(request, allowed.principal_id); + if (!entries.some(entry => entry.todo_id === request.todo_id && + (entry.receipt_operation_ids as string[] | undefined)?.includes(id))) { + return {status: "rejected", reason_code: "remote_authority_scope_denied"}; + } + return {...allowed, read_scope: {...readScope, receipt_operation_id: id}}; } -export function fileRemoteAuthorityAdmission(path: string) { - return async (credential: string, request: RemoteAuthorityRequest): Promise => { +export function fileRemoteAuthorityAdmission(path: string, expectedSchema: string = REMOTE_AUTHORITY_POLICY) { + return async (credential: string, request: RemoteAuthorityRequest, + mode: RemoteAuthorityMode = "execute"): Promise => { // A rename publishes one complete policy. Missing, malformed or unreadable // policy fails closed; never retain a stale last-known-good grant. - return remoteAuthorityPolicyAdmission(await readRemoteAuthorityPolicy(path), credential, request); + const policy = await readRemoteAuthorityPolicy(path); + if (policy.schema_version !== expectedSchema) throw new TypeError("Service policy schema changed; restart the listener"); + return remoteAuthorityPolicyAdmission(policy, credential, request, mode); }; } diff --git a/loopx/control_plane/coordination/remote_authority_service.ts b/loopx/control_plane/coordination/remote_authority_service.ts index 75f045e7e0..eff348b4d3 100644 --- a/loopx/control_plane/coordination/remote_authority_service.ts +++ b/loopx/control_plane/coordination/remote_authority_service.ts @@ -1,32 +1,36 @@ /** Authenticated task commands over the existing semantic and persistence owners. */ import type {JsonObject} from "../effect_program.ts"; import type {AuthorityStore, AuthorityStoreReadFailure} from "./authority_store.ts"; -import {canonicalAuthoritySha256} from "./authority_store_codec.ts"; +import {canonicalAuthoritySha256, parseAuthorityCursor} from "./authority_store_codec.ts"; import {PostgreSqlAuthorityService} from "./postgresql_authority_service.ts"; import type {PostgreSqlAuthorityDatabase} from "./postgresql_authority_store.ts"; import {executeCoordinationTodoClaim} from "./todo_claim.ts"; import {executeCoordinationTodoUpdate} from "./todo_update.ts"; -import {executeCoordinationTodoCreate} from "./todo_create.ts"; +import {executeCoordinationTodoCreate, COORDINATION_TODO_CREATE_RECEIPT_SCHEMA} from "./todo_create.ts"; import {executeCoordinationTodoTerminalLifecycle, type CoordinationTodoTerminalLifecycleInput} from "./todo_terminal_lifecycle.ts"; import {executeCanonicalTaskLeaseLifecycle} from "./task_lease_lifecycle.ts"; import {leaseVersion, leaseEpoch} from "../work_items/task_lease_acquire.ts"; import {TODO_DOMAIN_ITEM_SCHEMA} from "./coordination_state_contract.ts"; -import {indexCoordinationProjection, validateCoordinationTodoReadModel} from "./coordination_projection.ts"; -import {decodeRemoteAuthorityRequest, remoteAuthorityOperationId, remoteAuthorityResponse, remoteAuthoritySnapshotDigest, +import {CoordinationProjectionRead} from "./coordination_projection.ts"; +import {GOAL_ACCEPTANCE_LIFECYCLE_OPERATION_SCHEMA, readGoalAcceptanceLifecycle, sameExactGoalRef, type WireExactGoalRef} from "../goals/acceptance_lifecycle.ts"; +import {decodeRemoteAuthorityRequest, remoteAuthorityOperationId, remoteAuthorityResponse, remoteAuthoritySnapshotDigest, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE, type RemoteAuthorityMode, type RemoteAuthorityRequest} from "./remote_authority_contract.ts"; export type RemoteAuthorityAdmission = - | {status: "allowed"; principal_id: string; policy_revision: string; registered_agents: readonly string[]} + | {status: "allowed"; principal_id: string; policy_revision: string; registered_agents: readonly string[]; + read_scope?: {goal_ref: WireExactGoalRef; todo_ids: readonly string[]; + todo_origins: Readonly>; receipt_operation_id?: string}} | {status: "rejected"; reason_code: string}; export interface RemoteAuthorityDependencies { database: PostgreSqlAuthorityDatabase; /** One immutable, server-owned authorization decision per request. Revocation * affects later admissions; it never lies about an already admitted commit. */ - admit(credential: string, request: RemoteAuthorityRequest): Promise; + admit(credential: string, request: RemoteAuthorityRequest, mode: RemoteAuthorityMode): Promise; now?: () => Date; } const incarnationFailure: AuthorityStoreReadFailure = {status: "unavailable", reason_code: "remote_authority_incarnation_changed", reason: "Rebind the authorized service scope before continuing"}; +const scopeDenied = {status: "rejected", reason_code: "remote_authority_scope_denied"} as const; const MAX_READ_NOTE_BYTES = 4096; /** Pin every observation and write to the admitted incarnation. The provider's @@ -82,8 +86,9 @@ function pinnedStore(store: AuthorityStore, identity: string, admission: JsonObj }; } -function projectReadTodo(todo: JsonObject, includePlanning: boolean): JsonObject { - const fields = ["todo_id", "status", "claimed_by", "note", ...(includePlanning ? ["role", "priority"] : [])]; +function projectReadTodo(todo: JsonObject, includePlanning: boolean, includeNote: boolean): JsonObject { + const fields = ["todo_id", "status", "claimed_by", ...(includeNote ? ["note"] : []), + ...(includePlanning ? ["role", "priority"] : [])]; const output = Object.fromEntries(fields.filter(field => Object.hasOwn(todo, field)).map(field => [field, todo[field]])); if (Object.hasOwn(output, "note") && Buffer.byteLength(JSON.stringify(output.note)) > MAX_READ_NOTE_BYTES) { delete output.note; @@ -92,6 +97,69 @@ function projectReadTodo(todo: JsonObject, includePlanning: boolean): JsonObject return output; } +function isActiveGoal(head: JsonObject, goalRef: WireExactGoalRef): boolean { + const lifecycle = readGoalAcceptanceLifecycle(head, goalRef.goal_id); + return lifecycle?.state === "active" && sameExactGoalRef(lifecycle.goal_ref, goalRef); +} + +/** Reconstruct one exact committed operation through the existing checkpoint + * owner. Proof is bounded by the admitted head; neither current state nor a + * later receipt can substitute for missing historical evidence. */ +async function readScopedTransaction(store: AuthorityStore, headCursor: string, + goalRef: WireExactGoalRef, operationId: string) { + const unavailable = {status: "unavailable", reason_code: "remote_authority_read_unavailable"} as const; + const receipt = await store.readReceipt(operationId); + if (receipt.status !== "found") return receipt.status === "missing" || + receipt.reason_code === "remote_authority_operation_mismatch" ? scopeDenied : unavailable; + const cursor = parseAuthorityCursor(receipt.cursor); + if (cursor > parseAuthorityCursor(headCursor) || receipt.receipts.length !== 1) return scopeDenied; + const offset = cursor - 1n; + const page = await store.scanCommitted(offset === 0n ? null : offset.toString(), 1); + if (page.status !== "page" || page.transactions.length !== 1) return unavailable; + const transaction = page.transactions[0]!; + const original = receipt.receipts[0]!; + const digest = canonicalAuthoritySha256(original); + if (transaction.cursor !== receipt.cursor || transaction.provider_revision !== receipt.provider_revision || + transaction.operation_id !== operationId || transaction.receipts.length !== 1 || + canonicalAuthoritySha256(transaction.receipts[0]) !== digest) return unavailable; + if (!isActiveGoal(transaction.projection, goalRef)) return scopeDenied; + return {status: "found" as const, original, projection: transaction.projection, digest}; +} + +/** The policy selects evidence, not resource ownership. Only a successful Todo + * creation or the trusted first bind of existing work can prove its lifetime. + * A claim/update, successor activation or idempotent rebind cannot relabel it. */ +function provesTodoOrigin(proof: Extract>, {status: "found"}>, + goalRef: WireExactGoalRef, todoId: string): boolean { + const read = new CoordinationProjectionRead(proof.projection, goalRef.goal_id); + read.validateTodoReadModel(); + const todo = read.todoIndex.todos.get(todoId); + if (!todo || proof.original.goal_id !== goalRef.goal_id) return false; + if (proof.original.schema_version === COORDINATION_TODO_CREATE_RECEIPT_SCHEMA) { + return proof.original.todo_id === todoId && canonicalAuthoritySha256(proof.original.todo) === canonicalAuthoritySha256(todo); + } + const result = proof.original.result; + return proof.original.schema_version === GOAL_ACCEPTANCE_LIFECYCLE_OPERATION_SCHEMA && + proof.original.request_sha256 === canonicalAuthoritySha256({transition: {kind: "bind_existing", goal_ref: goalRef}}) && + result !== null && typeof result === "object" && !Array.isArray(result) && (result as JsonObject).changed === true; +} + +/** Authorize from the current pinned head, then prove the original transaction + * belonged to the same Goal lifetime. Operation access stays separate from + * current Todo access and never returns the original execution payload. */ +async function readScopedReceipt(store: AuthorityStore, goalRef: WireExactGoalRef, + todoId: string, operationId: string): Promise { + const head = await store.loadAuthority(); + if (head.status !== "loaded") return {status: "unavailable", reason_code: "remote_authority_read_unavailable"}; + if (!isActiveGoal(head.head, goalRef)) return scopeDenied; + const current = new CoordinationProjectionRead(head.head, goalRef.goal_id); + current.validateTodoReadModel(); + if (!current.todoIndex.todos.has(todoId)) return scopeDenied; + const proof = await readScopedTransaction(store, head.cursor, goalRef, operationId); + if (proof.status !== "found") return proof; + return {status: "found", historical_only: true, todo_id: todoId, operation_id: operationId, receipt_sha256: proof.digest}; +} + /** Deliberately bounded response projection: no SQL errors, paths, arbitrary * canonical fields, workspace observations or provider credentials cross here. */ function commandResult(result: JsonObject): JsonObject { @@ -135,9 +203,16 @@ export class RemoteAuthorityService { return reply({status: "rejected", reason_code: "invalid_remote_authority_mode"}); } let admission: RemoteAuthorityAdmission; - try { admission = structuredClone(await this.#dependencies.admit(credential, request)); } + try { admission = structuredClone(await this.#dependencies.admit(credential, request, mode)); } catch { return reply({status: "unavailable", reason_code: "remote_authority_policy_unavailable"}); } if (admission.status !== "allowed") return reply(admission); + const isRead = request.operation === "read" || request.operation === "snapshot"; + const readScope = admission.read_scope; + const allowedTodos = readScope && new Set(readScope.todo_ids); + const operationId = remoteAuthorityOperationId(request, admission.principal_id); + if (allowedTodos && (allowedTodos.size === 0 || request.todo_id && !allowedTodos.has(request.todo_id))) return reply(scopeDenied); + if (readScope && !isRead && (mode !== "recover" || request.todo_id === undefined || + readScope.receipt_operation_id !== operationId)) return reply(scopeDenied); // Open through the established service seam with the already authenticated // snapshot. Never re-evaluate a different policy halfway through a request. const service = new PostgreSqlAuthorityService({database: this.#dependencies.database, @@ -145,7 +220,6 @@ export class RemoteAuthorityService { authorizeTenant: () => ({status: "allowed"})}); const opened = await service.openStore({...request, credential: null}); if (opened.status !== "opened") return reply({status: "unavailable", reason_code: opened.reason_code}); - const operationId = remoteAuthorityOperationId(request, admission.principal_id); // Explicitly reauthorizing a restored incarnation may recover old history. // The incarnation gates current access; it is not part of business intent. const {store_identity: admittedIdentity, ...intent} = request; @@ -155,7 +229,13 @@ export class RemoteAuthorityService { const store = pinnedStore(opened.store, request.store_identity, binding, operationId); let dispatched = false; try { - const previous = (request.operation === "read" || request.operation === "snapshot") ? null : await store.readReceipt(operationId); + if (readScope && mode === "recover" && request.todo_id !== undefined) { + const result = await readScopedReceipt(store, readScope.goal_ref, request.todo_id, operationId); + return result.status === "found" + ? remoteAuthorityResponse(request, mode, {...result, goal_ref: readScope.goal_ref}, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE) + : reply(result); + } + const previous = isRead ? null : await store.readReceipt(operationId); if (previous?.status === "found") { const original = previous.receipts[0]; const recorded = original?.remote_admission as JsonObject | undefined; @@ -175,22 +255,44 @@ export class RemoteAuthorityService { if (mode === "recover") return reply({status: "missing", operation_id: operationId}); const head = await store.loadAuthority(); if (head.status !== "loaded") return reply({status: "unavailable", reason_code: "remote_authority_head_unavailable"}); - validateCoordinationTodoReadModel(head.head, request.goal_id); - const index = indexCoordinationProjection(head.head, request.goal_id); + if (readScope && !isActiveGoal(head.head, readScope.goal_ref)) return reply(scopeDenied); + const projection = new CoordinationProjectionRead(head.head, request.goal_id); + projection.validateTodoReadModel(); + const index = projection.coordinationIndex; + const provenTodos = readScope ? new Set() : undefined; + if (readScope && provenTodos) { + // One request-local proof per origin, shared by Todos first bound in + // the same transaction. Never cache authorization across admissions. + const proofs = new Map>>(); + for (const id of readScope.todo_ids) { + if (request.todo_id && request.todo_id !== id || !index.todos.has(id)) continue; + const origin = readScope.todo_origins[id]; + if (!origin) continue; + let proof = proofs.get(origin); + if (!proof) { proof = await readScopedTransaction(store, head.cursor, readScope.goal_ref, origin); proofs.set(origin, proof); } + if (proof.status === "unavailable") return reply(proof); + if (proof.status === "found" && provesTodoOrigin(proof, readScope.goal_ref, id)) provenTodos.add(id); + } + if (request.todo_id ? !provenTodos.has(request.todo_id) : provenTodos.size === 0) return reply(scopeDenied); + } + const replyRead = (payload: JsonObject) => readScope + ? remoteAuthorityResponse(request, mode, {...payload, goal_ref: readScope.goal_ref}, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE) + : reply({...payload, provider_revision: head.provider_revision}); if (request.operation === "read") { const todo = index.todos.get(request.todo_id), lease = index.leases.get(request.todo_id); if (!todo) return reply({status: "missing"}); - return reply({status: "found", provider_revision: head.provider_revision, - todo: projectReadTodo(todo, false), + return replyRead({status: "found", + todo: projectReadTodo(todo, false, readScope === undefined), lease: lease ? {...Object.fromEntries(["owner", "status", "expires_at"].filter(k => k in lease).map(k => [k, lease[k]])), version: leaseVersion(lease), lease_epoch: leaseEpoch(lease)} : null}); } if (request.operation === "snapshot") { + const visible = (id: string) => (!request.todo_id || id === request.todo_id) && (!allowedTodos || allowedTodos.has(id)) && (!provenTodos || provenTodos.has(id)); const todos = index.todo_ids - .filter(id => !request.todo_id || id === request.todo_id) - .map(id => projectReadTodo(index.todos.get(id)!, true)); + .filter(visible) + .map(id => projectReadTodo(index.todos.get(id)!, true, readScope === undefined)); const leases = index.lease_todo_ids - .filter(id => !request.todo_id || id === request.todo_id) + .filter(visible) .map(id => { const l = index.leases.get(id)!; return { @@ -199,12 +301,11 @@ export class RemoteAuthorityService { lease_epoch: leaseEpoch(l), }; }); - return reply({ + return replyRead({ status: "snapshot", - provider_revision: head.provider_revision, store_identity: request.store_identity, goal_id: request.goal_id, - receipt_sha256: remoteAuthoritySnapshotDigest(request, head.provider_revision, todos, leases), + receipt_sha256: remoteAuthoritySnapshotDigest(request, head.provider_revision, todos, leases, readScope?.goal_ref), todos, leases, }); @@ -290,6 +391,7 @@ export class RemoteAuthorityService { expected_version: request.expected_version, ttl_seconds: request.operation === "renew" ? request.ttl_seconds : null}); return reply({status: "result", operation_id: operationId, result: commandResult(result)}); } catch { + if (readScope) return reply({status: "unavailable", reason_code: "remote_authority_read_unavailable"}); return reply({status: dispatched ? "ambiguous" : "unavailable", operation_id: operationId, reason_code: dispatched ? "remote_authority_recovery_required" : "remote_authority_read_unavailable"}); } diff --git a/loopx/control_plane/goals/acceptance_authority.ts b/loopx/control_plane/goals/acceptance_authority.ts index d68cf6e30a..554d59b158 100644 --- a/loopx/control_plane/goals/acceptance_authority.ts +++ b/loopx/control_plane/goals/acceptance_authority.ts @@ -17,7 +17,7 @@ import {GOAL_ACCEPTANCE_SCHEMA, clearedGoalAcceptance, acceptanceKeys, acceptanc resolveGoalAcceptanceDocument, projectGoalAcceptance, readGoalAcceptance, readGoalAcceptanceAuthority, type AcceptanceDocument, type AcceptanceState, type AcceptanceVerification} from "./acceptance_contract.ts"; import { BARE_SHA256_PATTERN } from "../content_digest.ts"; -import {GOAL_ACCEPTANCE_LIFECYCLE_SCHEMA, parseGoalAcceptanceLifecycleTransition, parseWireExactGoalRef, +import {GOAL_ACCEPTANCE_LIFECYCLE_SCHEMA, GOAL_ACCEPTANCE_LIFECYCLE_OPERATION_SCHEMA, parseGoalAcceptanceLifecycleTransition, parseWireExactGoalRef, readGoalAcceptanceLifecycle, sameExactGoalRef, type GoalAcceptanceLifecycle, type GoalAcceptanceLifecycleTransition, type WireExactGoalRef} from "./acceptance_lifecycle.ts"; @@ -27,7 +27,7 @@ import {projectCoreGoal} from "../work_items/replan_context.ts"; const RESULT_SCHEMA = "loopx_goal_acceptance_result_v0"; const RECEIPT_SCHEMA = "loopx_goal_acceptance_operation_v0"; -const LIFECYCLE_RECEIPT_SCHEMA = "loopx_goal_acceptance_lifecycle_operation_v0"; +const LIFECYCLE_RECEIPT_SCHEMA = GOAL_ACCEPTANCE_LIFECYCLE_OPERATION_SCHEMA; const REQUEST_FIELDS = ["goal_id", "operation_id", "actor_agent_id", "expected_provider_revision"]; const LOCAL_FIELDS = ["runtime_root", "dry_run"]; diff --git a/loopx/control_plane/goals/acceptance_lifecycle.ts b/loopx/control_plane/goals/acceptance_lifecycle.ts index ab4a35294d..102de76144 100644 --- a/loopx/control_plane/goals/acceptance_lifecycle.ts +++ b/loopx/control_plane/goals/acceptance_lifecycle.ts @@ -3,6 +3,7 @@ import {AuthorityStoreProtocolError, canonicalAuthorityObject} from "../coordina import {parseExactGoalRef} from "./goal_instance_identity.ts"; export const GOAL_ACCEPTANCE_LIFECYCLE_SCHEMA = "loopx_goal_acceptance_lifecycle_v0"; +export const GOAL_ACCEPTANCE_LIFECYCLE_OPERATION_SCHEMA = "loopx_goal_acceptance_lifecycle_operation_v0"; export interface WireExactGoalRef extends JsonObject { goal_id: string; diff --git a/tests/control_plane_ts/remote_authority.integration.test.ts b/tests/control_plane_ts/remote_authority.integration.test.ts index 23c1cf5205..96436bc47c 100644 --- a/tests/control_plane_ts/remote_authority.integration.test.ts +++ b/tests/control_plane_ts/remote_authority.integration.test.ts @@ -21,10 +21,12 @@ import {authorityProjectionFixture} from "./authority_projection_fixture.ts"; import {resolveTestPython} from "../../scripts/test-python.mjs"; import {registerRemoteAuthorityModeConformance} from "./remote_authority_mode_conformance.ts"; import {registerRemoteAuthorityLifecycleConformance} from "./remote_authority_lifecycle_conformance.ts"; +import {registerRemoteAuthorityReadAclConformance} from "./remote_authority_read_acl_conformance.ts"; const connectionString = process.env.LOOPX_TEST_POSTGRES_URL; registerRemoteAuthorityModeConformance(connectionString); registerRemoteAuthorityLifecycleConformance(connectionString); +registerRemoteAuthorityReadAclConformance(connectionString); test("quota CLI verifies the exact remote binding and never grants local execution", {skip: !connectionString}, async t => { const fixture = await postgresRemoteFixture(t, connectionString!); await fixture.seed(["todo_gateway_probe"], {todo_gateway_probe: "private-synthetic-note"}); diff --git a/tests/control_plane_ts/remote_authority.test.ts b/tests/control_plane_ts/remote_authority.test.ts index e65dae3eb3..0c4d8012d7 100644 --- a/tests/control_plane_ts/remote_authority.test.ts +++ b/tests/control_plane_ts/remote_authority.test.ts @@ -9,7 +9,7 @@ import {tmpdir} from "node:os"; import {join} from "node:path"; import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; import {decodeRemoteAuthorityRequest, decodeRemoteAuthorityResponse, remoteAuthorityOperationId, - remoteAuthorityResponse} from "../../loopx/control_plane/coordination/remote_authority_contract.ts"; + remoteAuthorityResponse, remoteAuthoritySnapshotDigest, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE} from "../../loopx/control_plane/coordination/remote_authority_contract.ts"; import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts"; import {decodeRemoteAuthorityPolicy, remoteAuthorityPolicyAdmission, readRemoteAuthorityPolicy} from "../../loopx/control_plane/coordination/remote_authority_policy.ts"; import {RemoteAuthorityService} from "../../loopx/control_plane/coordination/remote_authority_service.ts"; @@ -76,6 +76,66 @@ test("missing-lease release responses require explicit no-op evidence", () => { } }); +test("scoped read responses require their Goal reference and exclude global authority metadata", () => { + const {request_id: _, ...scope} = claimRequest("a"); + const request = decodeRemoteAuthorityRequest({...scope, operation: "read"}); + const goal_ref = {goal_id: request.goal_id, goal_instance_id: "ginst_" + "a".repeat(32)}; + const payload = {status: "found", goal_ref, todo: {todo_id: request.todo_id, status: "open"}, lease: null}; + const wrap = (value: JsonObject) => remoteAuthorityResponse(request, "execute", value, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE); + assert.deepEqual(decodeRemoteAuthorityResponse(wrap(payload), request, "execute"), wrap(payload)); + for (const invalid of [ + {...payload, goal_ref: {goal_id: request.goal_id}}, + {...payload, goal_ref: {...goal_ref, goal_id: "other-goal"}}, + {...payload, provider_revision: `${request.store_identity}:2`}, + {...payload, cursor: "2"}, + ]) assert.throws(() => decodeRemoteAuthorityResponse(wrap(invalid), request, "execute")); +}); + +test("scoped response nested records reject fields outside their read projection", () => { + const {request_id: _, ...scope} = claimRequest("a"); + const goal_ref = {goal_id: scope.goal_id, goal_instance_id: "ginst_" + "a".repeat(32)}; + const todo = {todo_id: scope.todo_id, status: "open", claimed_by: "actor-a"}; + const lease = {owner: "actor-a", status: "active", expires_at: "2099-01-01T00:00:00Z", version: 1, lease_epoch: 1}; + const read = decodeRemoteAuthorityRequest({...scope, operation: "read"}); + const snapshot = decodeRemoteAuthorityRequest({...scope, operation: "snapshot"}); + const wrap = (request: typeof read, payload: JsonObject) => remoteAuthorityResponse(request, "execute", + {...payload, goal_ref}, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE); + const found = (row: JsonObject, held: JsonObject) => wrap(read, {status: "found", todo: row, lease: held}); + const snap = (row: JsonObject, held: JsonObject) => wrap(snapshot, {status: "snapshot", goal_id: scope.goal_id, + store_identity: scope.store_identity, todos: [row], leases: [held], + receipt_sha256: remoteAuthoritySnapshotDigest(snapshot, undefined, [row], [held], goal_ref)}); + const snapshotTodo = {...todo, role: "agent", priority: "P1"}; + const snapshotLease = {...lease, goal_id: scope.goal_id, todo_id: scope.todo_id}; + assert.deepEqual(decodeRemoteAuthorityResponse(found(todo, lease), read, "execute"), found(todo, lease)); + assert.deepEqual(decodeRemoteAuthorityResponse(snap(snapshotTodo, snapshotLease), snapshot, "execute"), snap(snapshotTodo, snapshotLease)); + for (const field of ["note", "note_omitted", "text", "idempotency_key", "provider_revision", "cursor", "unknown"]) { + const value = field === "note_omitted" ? true : "must-not-cross-the-read-boundary"; + assert.throws(() => decodeRemoteAuthorityResponse(found({...todo, [field]: value}, lease), read, "execute"), `read Todo ${field}`); + assert.throws(() => decodeRemoteAuthorityResponse(found(todo, {...lease, [field]: value}), read, "execute"), `read lease ${field}`); + // A matching digest cannot authorize extra fields in a scoped snapshot. + assert.throws(() => decodeRemoteAuthorityResponse(snap({...snapshotTodo, [field]: value}, snapshotLease), snapshot, "execute"), `snapshot Todo ${field}`); + assert.throws(() => decodeRemoteAuthorityResponse(snap(snapshotTodo, {...snapshotLease, [field]: value}), snapshot, "execute"), `snapshot lease ${field}`); + } + const trusted = remoteAuthorityResponse(read, "execute", {status: "found", provider_revision: `${scope.store_identity}:3`, + todo: {...todo, note: "trusted v0 note"}, lease: {...lease, idempotency_key: "trusted-v0-key"}}); + assert.deepEqual(decodeRemoteAuthorityResponse(trusted, read, "execute"), trusted); +}); + +test("scoped historical receipt responses expose proof for the original Todo without its execution payload", () => { + const request = claimRequest("a"); + const goal_ref = {goal_id: request.goal_id, goal_instance_id: "ginst_" + "a".repeat(32)}; + const payload = {status: "found", historical_only: true, goal_ref, todo_id: request.todo_id, + operation_id: remoteAuthorityOperationId(request, "principal-a"), receipt_sha256: "d".repeat(64)}; + const wrap = (value: JsonObject) => remoteAuthorityResponse(request, "recover", value, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE); + assert.deepEqual(decodeRemoteAuthorityResponse(wrap(payload), request, "recover"), wrap(payload)); + for (const invalid of [ + {...payload, todo_id: "todo_other"}, + {...payload, historical_only: false}, + {...payload, result: {lease: {idempotency_key: "out-of-scope"}}}, + {...payload, cursor: "2"}, + ]) assert.throws(() => decodeRemoteAuthorityResponse(wrap(invalid), request, "recover")); +}); + test("invalid policy cannot silently broaden or retain grants", async t => { for (const mutate of [(p: ReturnType) => p.principals.push(p.principals[0]), (p: ReturnType) => p.scopes.push(p.scopes[0]), diff --git a/tests/control_plane_ts/remote_authority_fixture.ts b/tests/control_plane_ts/remote_authority_fixture.ts index 93125f5136..892ebbf116 100644 --- a/tests/control_plane_ts/remote_authority_fixture.ts +++ b/tests/control_plane_ts/remote_authority_fixture.ts @@ -88,6 +88,7 @@ export async function postgresRemoteFixture(t: TestContext, connectionString: st await setup.query(`GRANT CONNECT ON DATABASE ${name} TO ${role}`); await setup.query(`GRANT USAGE ON SCHEMA loopx_control_plane TO ${role}`); await setup.query(`GRANT SELECT ON loopx_control_plane.authority_store_metadata TO ${role}`); + await setup.query(`GRANT SELECT ON loopx_control_plane.authority_checkpoints TO ${role}`); await setup.query(`GRANT SELECT, INSERT, UPDATE ON loopx_control_plane.authority_heads TO ${role}`); await setup.query(`GRANT SELECT, INSERT ON loopx_control_plane.authority_commits, loopx_control_plane.authority_events, loopx_control_plane.authority_receipts TO ${role}`); @@ -102,7 +103,7 @@ export async function postgresRemoteFixture(t: TestContext, connectionString: st }); } const policyPath = join(tls.root, "policy.json"); - async function writePolicy(policy = servicePolicy()) { + async function writePolicy(policy: unknown = servicePolicy()) { await writeFile(`${policyPath}.next`, JSON.stringify(policy), {mode: 0o600}); await rename(`${policyPath}.next`, policyPath); } diff --git a/tests/control_plane_ts/remote_authority_read_acl_conformance.ts b/tests/control_plane_ts/remote_authority_read_acl_conformance.ts new file mode 100644 index 0000000000..fe4260876e --- /dev/null +++ b/tests/control_plane_ts/remote_authority_read_acl_conformance.ts @@ -0,0 +1,277 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import {createServer} from "node:https"; +import type {AddressInfo} from "node:net"; +import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; +import {transitionGoalAcceptanceLifecycle} from "../../loopx/control_plane/goals/acceptance_authority.ts"; +import type {GoalAcceptanceLifecycleTransition} from "../../loopx/control_plane/goals/acceptance_lifecycle.ts"; +import {prepareCoordinationProjectionCommit} from "../../loopx/control_plane/coordination/coordination_projection.ts"; +import {TODO_DOMAIN_ITEM_SCHEMA} from "../../loopx/control_plane/coordination/coordination_state_contract.ts"; +import {executeCoordinationTodoCreate} from "../../loopx/control_plane/coordination/todo_create.ts"; +import {REMOTE_AUTHORITY_REQUEST, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE, + remoteAuthorityOperationId, remoteAuthoritySnapshotDigest} from "../../loopx/control_plane/coordination/remote_authority_contract.ts"; +import {REMOTE_AUTHORITY_SCOPED_READ_POLICY} from "../../loopx/control_plane/coordination/remote_authority_policy.ts"; +import {claimRequest, postgresRemoteFixture, serviceIdentity, servicePolicy} from "./remote_authority_fixture.ts"; + +export function registerRemoteAuthorityReadAclConformance(connectionString: string | undefined) { + test("remote read ACL pins the Goal lifetime and projects only the caller's resources", {skip: !connectionString}, async t => { + const fixture = await postgresRemoteFixture(t, connectionString!); + const goal_id = "goal-one"; + const a = {goal_id, goal_instance_id: "ginst_" + "a".repeat(32)}; + const b = {goal_id, goal_instance_id: "ginst_" + "b".repeat(32)}; + const {scopes, principals} = servicePolicy(); + const policy = {schema_version: REMOTE_AUTHORITY_SCOPED_READ_POLICY, + scopes: scopes.map(scope => ({...scope, todo_read_acl: [ + {goal_ref: a, todo_id: "todo_visible", principal_id: "principal-a", actor_id: "actor-a", + todo_origin_operation_id: "bind-first-goal"}, + {goal_ref: b, todo_id: "todo_next", principal_id: "principal-b", actor_id: "actor-b", + todo_origin_operation_id: "create-successor-todo"}, + // A new grant must not relabel retained predecessor resources. + {goal_ref: b, todo_id: "todo_visible", principal_id: "principal-b", actor_id: "actor-b", + todo_origin_operation_id: "bind-first-goal"}, + ]})), + principals: principals.map((principal, index) => ({...principal, grants: principal.grants.map(grant => ({ + ...grant, goal_ref: index === 0 ? a : b, operations: ["read", "snapshot"], + }))})), + }; + await fixture.seed(["todo_visible", "todo_hidden"], {todo_visible: "unqualified source text", todo_hidden: "private hidden note"}); + const legacy = await fixture.start(); + const originalClaim = claimRequest("a", "todo_visible"); + assert.equal((await fixture.client(legacy.endpoint, "a", originalClaim)).code, 0); + assert.equal((await fixture.client(legacy.endpoint, "b", claimRequest("b", "todo_hidden"))).code, 0); + await legacy.stop(); + await fixture.writePolicy(policy); + const service = await fixture.start(); + const scope = (actor: "a" | "b") => ({schema_version: REMOTE_AUTHORITY_REQUEST, goal_id, + tenant_id: "tenant-one", actor_id: `actor-${actor}`, store_identity: serviceIdentity} as const); + const snapshot = (actor: "a" | "b", todo_id?: string) => + fixture.client(service.endpoint, actor, {...scope(actor), operation: "snapshot", ...(todo_id ? {todo_id} : {})}); + const read = (actor: "a" | "b", todo_id: string) => + fixture.client(service.endpoint, actor, {...scope(actor), operation: "read", todo_id}); + function denied(response: Awaited>) { + assert.equal(response.code, 1); + assert.equal(response.value.status, "rejected"); + assert.equal(response.value.reason_code, "remote_authority_scope_denied"); + assert.deepEqual(Object.keys(response.value).sort(), ["reason_code", "request_sha256", "schema_version", "status"]); + } + async function transition(operation_id: string, step: GoalAcceptanceLifecycleTransition, expected = "applied") { + const result = await transitionGoalAcceptanceLifecycle(fixture.store, { + goal_id, actor_agent_id: null, operation_id, transition: step, + }); + assert.equal(result.status, expected); + } + + const unbound = await fixture.store.loadAuthority(); + denied(await read("a", "todo_visible")); + denied(await snapshot("a")); + assert.deepEqual(await fixture.store.loadAuthority(), unbound); + await transition("bind-first-goal", {kind: "bind_existing", goal_ref: a}); + const head = await fixture.store.loadAuthority(); + assert.equal(head.status, "loaded"); + if (head.status !== "loaded") throw new Error("Missing fixture head"); + + const visible = await read("a", "todo_visible"); + assert.equal(visible.code, 0); + assert.equal(visible.value.schema_version, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE); + assert.deepEqual(visible.value.goal_ref, a); + assert.deepEqual(visible.value.todo, {todo_id: "todo_visible", status: "open", claimed_by: "actor-a"}); + assert.equal((visible.value.lease as JsonObject).owner, "actor-a"); + assert(!("idempotency_key" in (visible.value.lease as JsonObject))); + const first = await snapshot("a"); + assert.equal(first.code, 0); + assert.deepEqual(first.value.goal_ref, a); + for (const field of ["todos", "leases"]) { + assert.deepEqual((first.value[field] as JsonObject[]).map(row => row.todo_id), ["todo_visible"]); + } + assert(!Object.hasOwn(first.value, "provider_revision")); + assert(!JSON.stringify(first.value).includes("unqualified source text")); + await t.test("TLS CLI rejects nested disclosure even with correlation and a recomputed snapshot digest", async () => { + let reply: JsonObject = visible.value; + const proxy = createServer({key: fixture.key, cert: fixture.cert}, (incoming, outgoing) => { + incoming.resume(); outgoing.setHeader("Content-Type", "application/json"); outgoing.end(JSON.stringify(reply)); + }); + await new Promise(resolve => proxy.listen(0, "127.0.0.1", resolve)); + try { + const endpoint = `https://127.0.0.1:${(proxy.address() as AddressInfo).port}`; + const request = {...scope("a"), operation: "read" as const, todo_id: "todo_visible"}; + assert.equal((await fixture.client(endpoint, "a", request)).code, 0); + for (const [record, field] of [["todo", "note"], ["lease", "idempotency_key"]]) { + reply = {...visible.value, [record!]: {...visible.value[record!] as JsonObject, [field!]: "private-response-marker"}}; + const rejected = await fixture.client(endpoint, "a", request); + assert.equal(rejected.code, 1); assert.equal(rejected.value.status, "unavailable"); + assert(!JSON.stringify(rejected.value).includes("private-response-marker")); + } + const snapshotRequest = {...scope("a"), operation: "snapshot" as const}; + for (const [collection, field] of [["todos", "note"], ["leases", "idempotency_key"]]) { + reply = structuredClone(first.value); + (reply[collection!] as JsonObject[])[0]![field!] = "private-response-marker"; + reply.receipt_sha256 = remoteAuthoritySnapshotDigest(snapshotRequest, undefined, + reply.todos as JsonObject[], reply.leases as JsonObject[], a); + const rejected = await fixture.client(endpoint, "a", snapshotRequest); + assert.equal(rejected.code, 1); assert.equal(rejected.value.status, "unavailable"); + assert(!JSON.stringify(rejected.value).includes("private-response-marker")); + } + reply = first.value; + assert.equal((await fixture.client(endpoint, "a", snapshotRequest)).code, 0); + } finally { await new Promise(resolve => proxy.close(() => resolve())); } + }); + for (const origin of [undefined, "missing-origin-operation", remoteAuthorityOperationId(originalClaim, "principal-a")]) { + await fixture.writePolicy({...policy, scopes: policy.scopes.map(s => ({...s, + todo_read_acl: s.todo_read_acl.map(entry => ({...entry, todo_origin_operation_id: origin}))}))}); + denied(await read("a", "todo_visible")); + denied(await snapshot("a")); + } + await fixture.writePolicy(policy); + denied(await read("a", "todo_hidden")); + denied(await read("a", "todo_missing")); + denied(await snapshot("a", "todo_hidden")); + denied(await snapshot("b")); + denied(await fixture.client(service.endpoint, "a", {...scope("b"), operation: "snapshot"})); + denied(await fixture.client(service.endpoint, "a", originalClaim)); + denied(await fixture.client(service.endpoint, "a", originalClaim, true)); + assert.deepEqual(await fixture.store.loadAuthority(), head); + + const hidden = (head.head.todos as JsonObject[]).find(todo => todo.todo_id === "todo_hidden")!; + assert.equal((await fixture.store.commitAuthority(prepareCoordinationProjectionCommit({ + goal_id, projection: head.head, expected_provider_revision: head.provider_revision, operation_id: "change-hidden-todo", + mutations: [{kind: "todo_upsert", todo: {...hidden, note: "different private hidden note"}}], + }))).status, "applied"); + assert.deepEqual((await snapshot("a")).value, first.value); + + await fixture.writePolicy({...policy, scopes: policy.scopes.map(s => ({...s, + todo_read_acl: s.todo_read_acl.filter(entry => entry.principal_id !== "principal-a")}))}); + denied(await snapshot("a")); + await fixture.writePolicy(servicePolicy()); + const downgrade = await snapshot("a"); + assert.equal(downgrade.value.status, "unavailable"); + assert.equal(downgrade.value.reason_code, "remote_authority_policy_unavailable"); + await fixture.writePolicy(policy); + assert.deepEqual((await snapshot("a")).value, first.value); + + await transition("retire-first-goal", {kind: "retire", goal_ref: a}); + denied(await snapshot("a")); + await transition("activate-second-goal", {kind: "activate_successor", retired_goal_ref: a, goal_ref: b}); + const added = await executeCoordinationTodoCreate(fixture.store, { + goal_id, actor_agent_id: "actor-b", registered_agents: ["actor-a", "actor-b"], + operation_id: "create-successor-todo", dry_run: false, now: new Date(), + todo: {schema_version: TODO_DOMAIN_ITEM_SCHEMA, todo_id: "todo_next", role: "agent", + status: "open", done: false, archive_state: "active", text: "Second lifetime task"}, + }); + assert.equal(added.status, "applied"); + const successor = await fixture.store.loadAuthority(); + denied(await snapshot("a")); + denied(await read("a", "todo_visible")); + denied(await read("a", "todo_next")); + denied(await read("b", "todo_visible")); + const second = await snapshot("b"); + assert.equal(second.code, 0); + assert.deepEqual(second.value.goal_ref, b); + assert.deepEqual((second.value.todos as JsonObject[]).map(todo => todo.todo_id), ["todo_next"]); + assert.deepEqual(second.value.leases, []); + assert.equal((await read("b", "todo_next")).code, 0); + assert.deepEqual(await fixture.store.loadAuthority(), successor); + await transition("bind-current-again", {kind: "bind_existing", goal_ref: b}, "no_change"); + for (const origin of ["activate-second-goal", "bind-current-again", "create-successor-todo"]) { + await fixture.writePolicy({...policy, scopes: policy.scopes.map(s => ({...s, + todo_read_acl: s.todo_read_acl.map(entry => entry.goal_ref === b && entry.todo_id === "todo_visible" + ? {...entry, todo_origin_operation_id: origin} : entry)}))}); + denied(await read("b", "todo_visible")); + assert.deepEqual(((await snapshot("b")).value.todos as JsonObject[]).map(todo => todo.todo_id), ["todo_next"]); + } + }); + + test("scoped receipt recovery proves the original Goal lifetime and current exact operation access", {skip: !connectionString}, async t => { + const fixture = await postgresRemoteFixture(t, connectionString!); + const goal_id = "goal-one"; + const a = {goal_id, goal_instance_id: "ginst_" + "a".repeat(32)}; + const b = {goal_id, goal_instance_id: "ginst_" + "b".repeat(32)}; + const unboundClaim = claimRequest("a", "todo_legacy"); + const boundClaim = claimRequest("a", "todo_bound"); + const missingClaim = claimRequest("a", "todo_bound", "never-committed"); + const id = (request: typeof boundClaim) => remoteAuthorityOperationId(request, "principal-a"); + async function transition(operation_id: string, step: GoalAcceptanceLifecycleTransition) { + const result = await transitionGoalAcceptanceLifecycle(fixture.store, {goal_id, actor_agent_id: null, + operation_id, transition: step}); + assert.equal(result.status, "applied"); + } + + await fixture.seed(["todo_legacy", "todo_bound", "todo_hidden"]); + const legacy = await fixture.start(); + assert.equal((await fixture.client(legacy.endpoint, "a", unboundClaim)).code, 0); + await transition("bind-receipt-goal", {kind: "bind_existing", goal_ref: a}); + assert.equal((await fixture.client(legacy.endpoint, "a", boundClaim)).code, 0); + const original = await fixture.client(legacy.endpoint, "a", boundClaim, true); + assert.equal(original.code, 0); + assert("cursor" in original.value); + assert("lease" in (original.value.result as JsonObject)); + await legacy.stop(); + + // Read an earlier receipt after the authority has crossed a checkpoint. + // This uses the normal journal and keeps the original receipt intact. + for (let sequence = 0; sequence < 64; sequence++) { + const head = await fixture.store.loadAuthority(); + if (head.status !== "loaded") throw new Error("Missing fixture head"); + const hidden = (head.head.todos as JsonObject[]).find(todo => todo.todo_id === "todo_hidden")!; + const result = await fixture.store.commitAuthority(prepareCoordinationProjectionCommit({ + goal_id, projection: head.head, expected_provider_revision: head.provider_revision, + operation_id: `append-receipt-fixture-${sequence}`, + mutations: [{kind: "todo_upsert", todo: {...hidden, note: `Hidden fixture update ${sequence}`}}], + })); + assert.equal(result.status, "applied"); + } + function policy(goalRef: typeof a, allowed: readonly string[], operations = ["read", "snapshot"]) { + const {scopes, principals} = servicePolicy(); + return {schema_version: REMOTE_AUTHORITY_SCOPED_READ_POLICY, + scopes: scopes.map(scope => ({...scope, todo_read_acl: [ + {goal_ref: goalRef, todo_id: "todo_bound", principal_id: "principal-a", actor_id: "actor-a", + todo_origin_operation_id: "bind-receipt-goal", receipt_operation_ids: allowed}, + {goal_ref: goalRef, todo_id: "todo_legacy", principal_id: "principal-a", actor_id: "actor-a", + receipt_operation_ids: [id(unboundClaim), id(boundClaim)]}, + ]})), + principals: principals.slice(0, 1).map(principal => ({...principal, grants: principal.grants.map(grant => + ({...grant, goal_ref: goalRef, operations}))})), + }; + } + await fixture.writePolicy(policy(a, [id(boundClaim), id(missingClaim)])); + const service = await fixture.start(); + const recover = (request: typeof boundClaim) => fixture.client(service.endpoint, "a", request, true); + function denied(result: Awaited>) { + assert.equal(result.code, 1); + assert.equal(result.value.status, "rejected"); + assert.equal(result.value.reason_code, "remote_authority_scope_denied"); + assert.deepEqual(Object.keys(result.value).sort(), ["reason_code", "request_sha256", "schema_version", "status"]); + } + const current = await fixture.store.loadAuthority(); + const allowed = await recover(boundClaim); + assert.equal(allowed.code, 0); + assert.deepEqual(allowed.value, { + schema_version: REMOTE_AUTHORITY_SCOPED_READ_RESPONSE, status: "found", historical_only: true, + todo_id: boundClaim.todo_id, goal_ref: a, operation_id: original.value.operation_id, + receipt_sha256: original.value.receipt_sha256, request_sha256: original.value.request_sha256, + }); + denied(await recover(unboundClaim)); + denied(await recover(missingClaim)); + denied(await recover({...boundClaim, todo_id: unboundClaim.todo_id})); + denied(await fixture.client(service.endpoint, "a", boundClaim)); // Execute stays outside the read grant. + await fixture.writePolicy(policy(a, [])); + denied(await recover(boundClaim)); + const currentTodo = await fixture.client(service.endpoint, "a", { + operation: "read", schema_version: REMOTE_AUTHORITY_REQUEST, tenant_id: boundClaim.tenant_id, + goal_id, actor_id: boundClaim.actor_id, store_identity: serviceIdentity, todo_id: boundClaim.todo_id, + }); + assert.equal(currentTodo.code, 0); + await fixture.writePolicy(policy(a, [id(boundClaim)], ["snapshot"])); + denied(await recover(boundClaim)); + assert.deepEqual(await fixture.store.loadAuthority(), current); + + await fixture.writePolicy(policy(a, [id(boundClaim)])); + await transition("retire-receipt-goal", {kind: "retire", goal_ref: a}); + denied(await recover(boundClaim)); + await transition("activate-next-receipt-goal", {kind: "activate_successor", retired_goal_ref: a, goal_ref: b}); + const successor = await fixture.store.loadAuthority(); + denied(await recover(boundClaim)); // Stale credential. + await fixture.writePolicy(policy(b, [id(boundClaim)])); + denied(await recover(boundClaim)); // Current ACL cannot reassign a receipt from A to B. + assert.deepEqual(await fixture.store.loadAuthority(), successor); + }); +} From 4d4ea894155aa998509af04289193ffef829d9a9 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Tue, 17 Nov 2026 03:21:27 +0800 Subject: [PATCH 28/34] Fix original quoted message reading in the persistent queue (#8880) Reuse the original-source history projection and shared quote reader in the persistent queue, preserving raw input and literal fallback. Scope queue container styles to keep original numbered lists readable. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../smoke/bound-conversation-http-fixture.py | 21 ++++-- .../conversation-reply-browser-smoke.mjs | 70 ++++++++++++++++++- apps/presentation/dashboard/src/data/chat.ts | 7 +- .../personal-workspace/conversation-queue.css | 4 +- .../personal-workspace/conversation-queue.tsx | 4 +- loopx/capabilities/native_chat/queue_api.py | 8 ++- 6 files changed, 102 insertions(+), 12 deletions(-) diff --git a/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py b/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py index 7f6c634bb4..426935f323 100644 --- a/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py +++ b/apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py @@ -141,15 +141,28 @@ def start(self, server): "position": 1, "content": body}]}} frozen = conversation_reply_context(raw)["context_text"] + "\n\nCurrent user message:\n" + body record.update(reply_context_message=frozen, reply_context_source_text=body) - turn, _ = server.chat_store.create_turn(sid, client_turn_id=f"external-{request}", message=frozen, origin="lark") + queued_reply = "--queued-reply" in sys.argv + if queued_reply: + server.chat_store.create_turn(sid, client_turn_id="synthetic-held-turn", message="An active synthetic task.", origin="web") + create = server.chat_store.create_queued_turn if queued_reply else server.chat_store.create_turn + attachments = [] + if queued_reply: + image = "iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAIAAAASFvFNAAAAEElEQVR4nGP4v0gdghjgLABwBAqxX1jWkgAAAABJRU5ErkJggg==" + attachments = [{"id": "queued-image", "name": "queued-image.png", "mime_type": "image/png", + "size": len(base64.b64decode(image)), "data_url": "data:image/png;base64," + image}] + turn, _ = create(sid, client_turn_id=f"external-{request}", message=frozen, origin="lark", attachments=attachments) correlation = {"request_ref": request, "binding_id": binding["binding_id"], "source": source, "session_id": sid, "turn_id": turn["turn_id"]} for directory, value in (("external-requests", {**correlation, "status": "accepted", "command": None, "message": frozen}), ("lark-private-deliveries", {**record, **correlation, "status": "delivered"})): _atomic_write_json(server.chat_store.root / directory / f"{request}.json", value) - server.chat_store.update_turn(sid, turn["turn_id"], status="completed") - server.chat_store.release_active_turn(sid, turn["turn_id"]) - server.chat_store.append_message(sid, role="agent", text="已保留原要求,当前核验结果待进一步确认。", turn_id=turn["turn_id"]) + if queued_reply: + # Identical prose without admission journals must stay literal. + server.chat_store.create_queued_turn(sid, client_turn_id="synthetic-literal-context", message=frozen, origin="web") + else: + server.chat_store.update_turn(sid, turn["turn_id"], status="completed") + server.chat_store.release_active_turn(sid, turn["turn_id"]) + server.chat_store.append_message(sid, role="agent", text="已保留原要求,当前核验结果待进一步确认。", turn_id=turn["turn_id"]) paths = [server.chat_store.root / directory / f"{request}.json" for directory in ("external-requests", "lark-private-deliveries")] paths.extend([server.chat_store.sessions_root / sid / "messages.jsonl", diff --git a/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs b/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs index 67bf327169..54f1f41596 100644 --- a/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs +++ b/apps/presentation/dashboard/smoke/conversation-reply-browser-smoke.mjs @@ -12,11 +12,12 @@ import {launchBrowser, loadPlaywright} from "../../../../examples/dashboard-brow const repo = resolve(import.meta.dirname, "../../../.."); const returnedFiles = process.argv.includes("--returned-files"); const historyImages = process.argv.includes("--history-images"); +const queuedReply = process.argv.includes("--queued-reply"); const output = resolve(process.env.LOOPX_REPLY_OUTPUT ?? resolve(repo, "output/playwright/conversation-reply")); await mkdir(output, {recursive: true}); const child = spawn(resolveTestPython({repoRoot: repo}), ["-u", "apps/presentation/dashboard/smoke/bound-conversation-http-fixture.py", returnedFiles ? "--returned-files" : "--reply-context", - ...(historyImages ? ["--history-images"] : [])], + ...(historyImages ? ["--history-images"] : []), ...(queuedReply ? ["--queued-reply"] : [])], {cwd: repo, env: {...process.env, LOOPX_USAGE_PING: "0"}, stdio: ["pipe", "pipe", "pipe"]}); const exited = once(child, "exit"), queue = [], waiting = []; let stderr = "", browser; @@ -279,6 +280,71 @@ async function checkHistoryImages(ready, page, errors, writes) { console.log("history-images-browser: passed (latest, scroll, inline retry and original message link; zero model Turns)"); } +async function checkQueuedReply(ready, page, errors, writes) { + const endpoint = `${ready.origin}/api/chat/sessions/${ready.sessionId}`; + const snapshot = await (await page.request.get(endpoint)).json(); + const original = snapshot.messages.find(row => row.origin === "lark" && row.reply_context); + assert.ok(original.text.startsWith("Referenced message (")); + const body = original.reply_context.source_text; + const queued = await (await page.request.get(`${endpoint}/queue`)).json(); + assert.equal(queued.turns.length, 2); + assert.equal(queued.turns[0].preview, body.slice(0, 240)); + assert.equal(queued.turns[0].character_count, [...body].length); + assert.equal(queued.turns[1].preview, [...original.text].slice(0, 240).join(""), + "An identical unbound literal must not become a source-backed quote"); + await openWhileCheckingOriginalConversation(ready, page); + await page.getByRole("navigation", {name: "管家视图"}).getByRole("button", {name: /^(Chat|对话)$/}).click(); + const queue = page.locator(".personal-conversation-queue"); + const message = queue.locator(":scope > ol > li").first(); + const summary = message.getByLabel("待处理消息 1", {exact: true}); + await summary.waitFor(); + assert.equal(await summary.getByText(body, {exact: true}).count(), 1); + assert.equal(queued.turns[0].attachment_count, 1); + await page.screenshot({path: resolve(output, "queue-default-desktop.png"), animations: "disabled"}); + child.stdin.write("history-unavailable\n"); + assert.equal((await next()).history_available, false); + await summary.focus(); await page.keyboard.press("Enter"); + await message.getByRole("alert").waitFor(); + child.stdin.write("history-available\n"); + assert.equal((await next()).history_available, true); + await message.getByRole("button", {name: "重试", exact: true}).click(); + const context = message.locator(".conversation-reply-context"); + await context.waitFor(); + await page.waitForFunction(() => document.querySelector('.personal-conversation-queue img[alt="queued-image.png"]')?.naturalWidth === 3); + assert.equal(await context.getAttribute("open"), null); + await context.locator("summary").focus(); await page.keyboard.press("Enter"); + await context.getByText("原要求:保留已有结果和失败记录。Original requirement: retain results and failed attempts.", {exact: true}).waitFor(); + assert.equal(await context.locator("section").count(), 1); + assert.equal(await context.locator("blockquote ol").first().evaluate(node => getComputedStyle(node).listStyleType), "decimal"); + assert.deepEqual(await context.locator("blockquote ol").evaluateAll(lists => lists.map(list => list.start)), [1, 2, 3, 7, 0]); + assert.equal(await message.locator(".personal-queue-message").getByText(body, {exact: true}).count(), 1); + await page.screenshot({path: resolve(output, "queue-expanded-desktop.png"), animations: "disabled"}); + await page.setViewportSize({width: 390, height: 844}); + await context.locator("summary").scrollIntoViewIfNeeded(); + assert.ok(await page.locator("body").evaluate(node => node.scrollWidth <= innerWidth + 1)); + await page.screenshot({path: resolve(output, "queue-expanded-narrow.png"), animations: "disabled"}); + const literal = queue.locator(":scope > ol > li").nth(1); + await literal.getByLabel("待处理消息 2", {exact: true}).click(); + await literal.locator(".personal-queue-message").getByText("Referenced message (", {exact: false}).waitFor(); + assert.equal(await literal.locator(".conversation-reply-context").count(), 0); + await page.evaluate(() => localStorage.setItem("loopx-pw-locale", "en")); + await page.reload(); + await queue.getByLabel("Queued message 1", {exact: true}).click(); + await queue.getByText("Reply and thread context", {exact: true}).waitFor(); + assert.deepEqual(errors, []); + assert.ok(!writes.some(path => /\/turns(?:\/|$)|\/queue\/[^/]+\/(?:input|steer)/u.test(path))); + child.stdin.write("inspect\n"); + const inspection = await next(); + assert.equal(inspection.model_turns, 0); + assert.equal(inspection.reply_evidence_unchanged, true); + child.stdin.write("revoke\n"); + assert.equal((await next()).revoked, ready.bindingId); + const denied = await page.request.get(`${endpoint}/queue`); + assert.ok(!denied.ok(), "Queue projection must not bypass revoked source access"); + assert.equal((await denied.json()).turns, undefined); + console.log("queued-reply-browser: passed (real HTTP/source journals, readable preview, full quote/thread, literal parity, read failure/retry, keyboard, narrow/English, no model Turns or queue mutations)"); +} + try { const ready = await next(); browser = await launchBrowser(loadPlaywright().chromium); @@ -291,6 +357,8 @@ try { await checkHistoryImages(ready, page, errors, writes); } else if (returnedFiles) { await checkReturnedFiles(ready, page, errors, writes); + } else if (queuedReply) { + await checkQueuedReply(ready, page, errors, writes); } else { const snapshot = await (await page.request.get(`${ready.origin}/api/chat/sessions/${ready.sessionId}`)).json(); const source = snapshot.messages.find(row => row.role === "user" && row.turn_id); diff --git a/apps/presentation/dashboard/src/data/chat.ts b/apps/presentation/dashboard/src/data/chat.ts index c0fdf5234c..8488f528ef 100644 --- a/apps/presentation/dashboard/src/data/chat.ts +++ b/apps/presentation/dashboard/src/data/chat.ts @@ -1009,15 +1009,16 @@ const chatQueuedInputSchema = z.object({input_revision: z.number().int().nonnega attachments: z.array(z.object({id: z.string(), name: z.string(), size: z.number(), mime_type: z.string(), data_url: z.string()})), }); -export type ChatQueuedInput = z.infer; +export type ChatQueuedInput = z.infer & Pick; export async function fetchQueuedChatInput(sessionId: string, turnId: string, signal?: AbortSignal) { // Keep original-source audience projection and complete paging. The visible // input and its revision come from one canonical user row, never raw model context. const history = await fetchChatSessionHistory(sessionId, signal, turnId); const original = history.messages.find(row => row.role === "user" && row.turn_id === turnId); if (!original) throw new Error("Original queued input is unavailable"); - return chatQueuedInputSchema.parse({input_revision: original.queue_input_revision ?? 0, - message: original.reply_context?.source_text ?? original.text, attachments: original.attachments ?? []}); + return {...chatQueuedInputSchema.parse({input_revision: original.queue_input_revision ?? 0, + message: original.reply_context?.source_text ?? original.text, attachments: original.attachments ?? []}), + reply_context: original.reply_context}; } const chatQueueSchema = z.object({ ok: z.literal(true), schema_version: z.literal("loopx_chat_store_v1"), session_id: z.string().min(1), diff --git a/apps/presentation/dashboard/src/features/personal-workspace/conversation-queue.css b/apps/presentation/dashboard/src/features/personal-workspace/conversation-queue.css index 273217988a..88a98a01dd 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/conversation-queue.css +++ b/apps/presentation/dashboard/src/features/personal-workspace/conversation-queue.css @@ -1,7 +1,7 @@ .personal-conversation-queue { min-width: 0; margin: 0 0 8px; font-size: var(--text-caption); color: var(--pw-text); } .personal-conversation-queue header { display: flex; align-items: center; gap: 8px; margin: 0 8px 4px; color: var(--pw-muted); } -.personal-conversation-queue ol { max-height: min(28dvh, 220px); overflow: auto; overscroll-behavior: contain; list-style: none; padding: 0; margin: 0; border: 1px solid var(--pw-line-strong); border-radius: 12px; background: #fff; } -.personal-conversation-queue li + li { border-top: 1px solid var(--pw-line-strong); } +.personal-conversation-queue > ol { max-height: min(28dvh, 220px); overflow: auto; overscroll-behavior: contain; list-style: none; padding: 0; margin: 0; border: 1px solid var(--pw-line-strong); border-radius: 12px; background: #fff; } +.personal-conversation-queue > ol > li + li { border-top: 1px solid var(--pw-line-strong); } .personal-queue-row { display: flex; align-items: start; gap: 8px; padding: 8px 10px; } .personal-queue-row details { min-width: 0; flex: 1; } .personal-queue-row summary { cursor: pointer; } diff --git a/apps/presentation/dashboard/src/features/personal-workspace/conversation-queue.tsx b/apps/presentation/dashboard/src/features/personal-workspace/conversation-queue.tsx index 333ebf0d19..7b3b361878 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/conversation-queue.tsx +++ b/apps/presentation/dashboard/src/features/personal-workspace/conversation-queue.tsx @@ -5,6 +5,8 @@ import { ChatApiError, chatQueueInputRequestSchema, fetchQueuedChatInput, mutate import { composerKeyAction } from "./composer-keyboard"; import { DocumentAttachment, isTextDocument } from "./document-attachment"; import { ContextHelp } from "./context-help"; +import { ConversationReplyContext } from "./conversation-requests"; +import { MarkdownText } from "./markdown"; import { queueInputKeys, readQueueInputRecovery, retireQueueInputRequest } from "./queue-input-recovery"; import { RetainedQueueInputs } from "./retained-queue-inputs"; import { useWorkspaceI18n } from "./i18n"; @@ -135,7 +137,7 @@ function QueuedMessage({sessionId, turn, index, targetTurnId, onChange, onInputC {open ?

{failed ?

{zh ? "完整消息读取失败" : "Full message unavailable"}

: !message ?

{zh ? "正在读取完整消息…" : "Reading the full message…"}

- : <>

{message.message}

+ : <> {message.attachments?.map(attachment => isTextDocument(attachment.mime_type) ? : {attachment.name}/)}} diff --git a/loopx/capabilities/native_chat/queue_api.py b/loopx/capabilities/native_chat/queue_api.py index 805fab86cb..ba4dd1d255 100644 --- a/loopx/capabilities/native_chat/queue_api.py +++ b/loopx/capabilities/native_chat/queue_api.py @@ -4,6 +4,7 @@ from urllib.parse import parse_qs, urlparse from ...chat_store import CHAT_STORE_SCHEMA_VERSION +from ...extensions.lark.private_ingress import project_private_reply_history class ChatQueueRequestMixin: @@ -63,7 +64,12 @@ def _session_queue(self, session_id: str) -> None: self._session_context(session) rows: list[dict[str, Any]] = [] for turn in store.queued_turns(session_id, strict=True, include_reserved=True): - text = str(turn.get("message") or "") + # Reuse history's exact admission/source checks before truncating; + # a preview cannot decode a partial context envelope. + original = {"role": "user", "origin": turn.get("origin"), + "turn_id": turn["turn_id"], "text": str(turn.get("message") or "")} + visible = project_private_reply_history(store, session, [original])[0] + text = visible.get("reply_context", {}).get("source_text", original["text"]) rows.append({ "turn_id": turn["turn_id"], "created_at": turn["created_at"], From 7057c18051d256de5313b1ad6632326db2a00b7a Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Tue, 17 Nov 2026 03:26:01 +0800 Subject: [PATCH 29/34] Fix phase-budget timeout test startup isolation (#8877) Fix the existing phase-budget test fixture without changing runtime behavior or the original communication budget. Independent exact-head review, focused tests, native checks and scoped quality receipt were verified. The benchmark-sensitive manual review hold is retained and addressed by independent review and existing maintainer authorization; final release qualification remains separate. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- benchmark/tests/test_worker_phase_budget.py | 33 ++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/benchmark/tests/test_worker_phase_budget.py b/benchmark/tests/test_worker_phase_budget.py index 5963aa76db..a8895db90f 100644 --- a/benchmark/tests/test_worker_phase_budget.py +++ b/benchmark/tests/test_worker_phase_budget.py @@ -67,6 +67,17 @@ def _phase_worker(tmp_path, monkeypatch, *, work_seconds=1, independent=None, if {output!r} == 'truncated_failure': sys.exit(1) time.sleep(60) """) + original = worker.child_process + + @contextmanager + def tracked(*args, **kwargs): + with original(*args, **kwargs) as process: + # Observe the actual owned child even when a deadline prevents the + # synthetic interpreter from writing its first fixture marker. + (project / "owned-pid").write_text(str(process.pid)) + yield process + + monkeypatch.setattr(worker, "child_process", tracked) return env, project, runtime, source, state, clock @@ -77,8 +88,13 @@ def _receipt(env): def _assert_reaped(project): + # In-process fixtures observe the parent's child handle; subprocess CLI + # fixtures report their own PID before executing any synthetic work. + path = project / "owned-pid" + if not path.exists(): + path = project / "pid" with pytest.raises(ProcessLookupError): - os.kill(int((project / "pid").read_text()), 0) + os.kill(int(path.read_text()), 0) def _listen_without_capture(monkeypatch): @@ -164,6 +180,21 @@ def wait(**kwargs): def test_independent_earlier_timeout_stays_unknown(tmp_path, monkeypatch): env, project, *_ = _phase_worker(tmp_path, monkeypatch, work_seconds=300, independent=.4) + original = worker.child_process + + @contextmanager + def started(*args, **kwargs): + with original(*args, **kwargs) as process: + # Qualify the independent communication timeout, as the adjacent + # phase-budget test does, without timing cold interpreter startup. + ready_deadline = time.monotonic() + 5 + while not (project / "native-ready").exists(): + assert process.poll() is None + assert time.monotonic() < ready_deadline, "native fixture did not start" + time.sleep(.01) + yield process + + monkeypatch.setattr(worker, "child_process", started) receipt = worker.run_once(env) assert receipt["timed_out"] and not receipt["ok"] assert not receipt.get("budget_exhausted") From 31944d835466c29f18f843ea92de75b49e7c0c62 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Tue, 17 Nov 2026 03:28:45 +0800 Subject: [PATCH 30/34] Prepare the LoopX 2.4.0 package and documentation identity (#8873) Align the seven existing package, CLI, manpage and bilingual book identity anchors for the 2.4.0 source candidate. Independent exact-head review, targeted tests, standard premerge and the scoped quality receipt pass. This is source identity preparation; final integrated release qualification and publication remain separate. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- docs/book/chapters/00-reading-guide.md | 2 +- docs/book/en/chapters/00-reading-guide.md | 4 ++-- docs/book/en/index.md | 2 +- docs/book/index.md | 2 +- loopx/__init__.py | 2 +- man/loopx.1 | 2 +- pyproject.toml | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/book/chapters/00-reading-guide.md b/docs/book/chapters/00-reading-guide.md index 0f1cde6858..aa32f61568 100644 --- a/docs/book/chapters/00-reading-guide.md +++ b/docs/book/chapters/00-reading-guide.md @@ -180,7 +180,7 @@ lifecycle 选择,不是所有贡献的默认终点。 ## 版本基线 -当前内容面向 LoopX release `v2.3.0`;本地命令示例已按 `loopx 2.3.0` 的 +当前内容面向 LoopX release `v2.4.0`;本地命令示例已按 `loopx 2.4.0` 的 公开 CLI 与协议表面复核。该版本要求 Python 3.11+ 与 Node.js 22.22.3+。LoopX 会自动管理 空闲后退出的 TypeScript Effect runtime,用户不需要手工维护 daemon。 diff --git a/docs/book/en/chapters/00-reading-guide.md b/docs/book/en/chapters/00-reading-guide.md index 029bcc7ec4..27ed08d881 100644 --- a/docs/book/en/chapters/00-reading-guide.md +++ b/docs/book/en/chapters/00-reading-guide.md @@ -199,8 +199,8 @@ Do not bypass a newer permission or lifecycle check just to make an older exampl ## Version baseline -This book targets LoopX release `v2.3.0`. Local command examples were checked against -the public `loopx 2.3.0` CLI and protocol surface. It requires Python 3.11+ and Node.js 22.22.3+. +This book targets LoopX release `v2.4.0`. Local command examples were checked against +the public `loopx 2.4.0` CLI and protocol surface. It requires Python 3.11+ and Node.js 22.22.3+. LoopX starts and reuses its managed, idle-exiting TypeScript Effect runtime automatically; users do not operate that runtime as a manual daemon. diff --git a/docs/book/en/index.md b/docs/book/en/index.md index 227db23bc9..8567677d75 100644 --- a/docs/book/en/index.md +++ b/docs/book/en/index.md @@ -79,7 +79,7 @@ versioned or optional functionality, not the default shape of every contribution - Source format: Markdown - Site generator: MkDocs Material - Hosting: GitHub Pages -- LoopX release anchor: `v2.3.0` +- LoopX release anchor: `v2.4.0` - Runtime prerequisites: Python 3.11+ and Node.js 22.22.3+ The official public protocols remain authoritative for protocol facts. Commands that change across diff --git a/docs/book/index.md b/docs/book/index.md index c43901c59c..6c2aa7f13f 100644 --- a/docs/book/index.md +++ b/docs/book/index.md @@ -70,7 +70,7 @@ Extension 只是可独立版本化和交付的一种路径。 - 正文格式:Markdown; - 站点生成器:MkDocs Material; - 在线发布:GitHub Pages; -- LoopX 发布锚点:`v2.3.0`; +- LoopX 发布锚点:`v2.4.0`; - 运行时前提:Python 3.11+ 与 Node.js 22.22.3+。 协议解释以 LoopX 官方公开合同为事实源。易变化的命令仍以对应发布物、官方文档和当前 diff --git a/loopx/__init__.py b/loopx/__init__.py index 2f01aed637..46099679c6 100644 --- a/loopx/__init__.py +++ b/loopx/__init__.py @@ -2,4 +2,4 @@ __all__ = ["__version__"] -__version__ = "2.3.0" +__version__ = "2.4.0" diff --git a/man/loopx.1 b/man/loopx.1 index fe854fe8d1..5a3c418659 100644 --- a/man/loopx.1 +++ b/man/loopx.1 @@ -1,4 +1,4 @@ -.TH LOOPX 1 "" "LoopX 2.3.0" "User Commands" +.TH LOOPX 1 "" "LoopX 2.4.0" "User Commands" .SH NAME loopx \- control-plane helper for long-running agent work .SH SYNOPSIS diff --git a/pyproject.toml b/pyproject.toml index fdc6cddab9..623d084858 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "loopx" -version = "2.3.0" +version = "2.4.0" description = "A Loop Engineering control plane for long-running agent goals (requires Node.js 22.22.3+)." readme = "README.md" requires-python = ">=3.11" From b12608d82bbf33d64f239e908d946cae360e556b Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:32:54 +0800 Subject: [PATCH 31/34] fix(extensions): follow the selected registry runtime (#8888) Forward the selected registry into the extension CLI and reuse the shared runtime root resolver, preserving explicit storage overrides and ambiguous-default refusal. Validated with 99 tests, 10 paired baseline/head CLI cases, standard premerge checks and the packaged Chat build. Exact-head author-owned review and native merge readiness passed. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/cli_commands/extension.py | 10 +- loopx/cli_runtime.py | 2 +- .../test_extension_readiness_contexts.py | 109 ++++++++++++++++++ 3 files changed, 118 insertions(+), 3 deletions(-) diff --git a/loopx/cli_commands/extension.py b/loopx/cli_commands/extension.py index 14cedbb5a3..77b86ce0af 100644 --- a/loopx/cli_commands/extension.py +++ b/loopx/cli_commands/extension.py @@ -21,6 +21,7 @@ run_standalone_extension, ) from ..extensions.scaffold import scaffold_extension +from ..paths import effective_runtime_root PrintPayload = Callable[ [dict[str, object], str, Callable[[dict[str, object]], str]], @@ -61,10 +62,14 @@ def _render(payload: dict[str, object]) -> str: return "\n".join(lines) + "\n" -def _state_file(args: argparse.Namespace, runtime_root_arg: str | None) -> Path: +def _state_file( + args: argparse.Namespace, runtime_root_arg: str | None, *, registry_path: Path, +) -> Path: override = getattr(args, "extension_state_file", None) if override: return Path(override).expanduser() + if runtime_root_arg is None: + runtime_root_arg = str(effective_runtime_root(registry_path, None)) return default_extension_state_file(runtime_root_arg) @@ -201,13 +206,14 @@ def _load_json_object(path_text: str) -> dict[str, object]: def handle_extension_command( args: argparse.Namespace, *, + registry_path: Path, runtime_root_arg: str | None, output_format: FormatSelector, print_payload: PrintPayload, ) -> int | None: if args.command != "extension": return None - state_file = _state_file(args, runtime_root_arg) + state_file = _state_file(args, runtime_root_arg, registry_path=registry_path) try: if args.extension_command == "init": payload = scaffold_extension( diff --git a/loopx/cli_runtime.py b/loopx/cli_runtime.py index 6cbd890bc8..3579b126dd 100644 --- a/loopx/cli_runtime.py +++ b/loopx/cli_runtime.py @@ -699,7 +699,7 @@ def _dispatch_common_command( from .cli_commands.extension import handle_extension_command return handle_extension_command( - args, runtime_root_arg=args.runtime_root, + args, registry_path=registry_path, runtime_root_arg=args.runtime_root, output_format=output_format, print_payload=print_payload, ) if args.command == "slash-commands": diff --git a/tests/extensions/test_extension_readiness_contexts.py b/tests/extensions/test_extension_readiness_contexts.py index c714edcb13..b59b265229 100644 --- a/tests/extensions/test_extension_readiness_contexts.py +++ b/tests/extensions/test_extension_readiness_contexts.py @@ -7,6 +7,8 @@ import subprocess import sys +import pytest + from tests.control_plane import test_refresh_external_delivery as settlement_fixtures from tests.control_plane.test_quota_settlement_cli import AGENT_ID, GOAL_ID, TODO_ID, TURN_ID from loopx.extensions.lark.goal_channel_contracts import ( @@ -139,6 +141,113 @@ def test_legacy_single_proof_is_read_only_then_migrated_by_real_doctor(tmp_path: assert extension_status(state_file=state_file)["extensions"][0]["doctor_verified"] +@pytest.mark.parametrize("selection", ["registry", "environment"]) +def test_real_extension_cli_follows_selected_registry_with_conflicting_defaults( + tmp_path: Path, selection: str, +) -> None: + home = tmp_path / "home" + defaults = [home / ".loopx", home / ".codex" / "loopx"] + for root in defaults: + root.mkdir(parents=True) + (root / "preserve.txt").write_text("existing independent state") + project = tmp_path / "project" + registry = project / ".loopx" / "registry.json" + registry.parent.mkdir(parents=True) + registry.write_text(json.dumps({"goals": [], "common_runtime_root": "selected state"})) + runtime = project / "selected state" + context = tmp_path / "unrelated working directory" + context.mkdir() + (context / f"{MODULE}.py").write_text("raise SystemExit(0)\n") + manifest = _manifest(tmp_path / "extension.toml") + repo = Path(__file__).resolve().parents[2] + environment = { + key: value for key, value in os.environ.items() + if not key.startswith(("LOOPX_", "PYTHON", "CODEX_")) + } + environment.update( + HOME=str(home), PYTHONPATH=os.pathsep.join((str(context), str(repo))), + LOOPX_USAGE_PING="0", DO_NOT_TRACK="1", + ) + prefix = [sys.executable, "-c", "from loopx.entrypoint import main; raise SystemExit(main())", + "--format", "json"] + + def run(*args: str, env: dict | None = None) -> dict: + result = subprocess.run( + [*prefix, *args], cwd=context, env=env or environment, + capture_output=True, text=True, timeout=30, + ) + assert result.returncode == 0, result.stderr + return json.loads(result.stdout) + + installed = run("--runtime-root", str(runtime), "extension", "install", + "--manifest", str(manifest), "--execute") + assert installed["doctor"]["verified"] is True + state = runtime / "extensions" / "state.json" + before = state.read_bytes() + route = ["--registry", str(registry)] if selection == "registry" else [] + selected_env = {**environment, "LOOPX_REGISTRY": str(registry)} + listed = run(*route, "extension", "list", env=selected_env if not route else None) + assert [item["id"] for item in listed["extensions"]] == [EXTENSION_ID] + assert listed["extensions"][0]["doctor_verified"] is True + assert state.read_bytes() == before + checked = run(*route, "extension", "doctor", "--all-enabled", "--execute", + env=selected_env if not route else None) + assert checked["status"] == "ready" and checked["verified_count"] == 1 + for root in defaults: + assert sorted(path.name for path in root.iterdir()) == ["preserve.txt"] + assert (root / "preserve.txt").read_text() == "existing independent state" + + +@pytest.mark.parametrize("override", ["runtime-root", "state-file"]) +def test_real_extension_cli_explicit_storage_does_not_open_unused_registry( + tmp_path: Path, override: str, +) -> None: + registry = tmp_path / "unreadable-registry.json" + registry.write_text("not registry JSON") + state = tmp_path / "selected" / "extensions" / "state.json" + state.parent.mkdir(parents=True) + state.write_text(json.dumps({"schema_version": "loopx_extension_state_v0", "extensions": {}})) + prefix = ["--runtime-root", str(state.parent.parent)] if override == "runtime-root" else [] + suffix = ["--state-file", str(state)] if override == "state-file" else [] + environment = {key: value for key, value in os.environ.items() + if not key.startswith(("LOOPX_", "PYTHON", "CODEX_"))} + environment.update(LOOPX_USAGE_PING="0", DO_NOT_TRACK="1", + PYTHONPATH=str(Path(__file__).resolve().parents[2])) + before = {path: path.read_bytes() for path in (registry, state)} + result = subprocess.run( + [sys.executable, "-c", "from loopx.entrypoint import main; raise SystemExit(main())", + "--format", "json", "--registry", str(registry), *prefix, + "extension", "list", *suffix], + cwd=tmp_path, env=environment, capture_output=True, text=True, timeout=30, + ) + assert result.returncode == 0, result.stderr + assert json.loads(result.stdout)["extensions"] == [] + assert {path: path.read_bytes() for path in before} == before + + +def test_real_extension_cli_does_not_choose_between_conflicting_defaults(tmp_path: Path) -> None: + home = tmp_path / "home" + for relative in (".loopx", ".codex/loopx"): + root = home / relative + root.mkdir(parents=True) + (root / "preserve.txt").write_text("independent state") + environment = {key: value for key, value in os.environ.items() + if not key.startswith(("LOOPX_", "PYTHON", "CODEX_"))} + environment.update(HOME=str(home), LOOPX_USAGE_PING="0", DO_NOT_TRACK="1", + PYTHONPATH=str(Path(__file__).resolve().parents[2])) + before = {str(path.relative_to(home)): path.read_bytes() + for path in home.rglob("*") if path.is_file()} + result = subprocess.run( + [sys.executable, "-c", "from loopx.entrypoint import main; raise SystemExit(main())", + "--format", "json", "extension", "doctor", "--all-enabled", "--execute"], + cwd=tmp_path, env=environment, capture_output=True, text=True, timeout=30, + ) + assert result.returncode != 0 + assert "Both default LoopX runtime roots contain state" in result.stderr + assert {str(path.relative_to(home)): path.read_bytes() + for path in home.rglob("*") if path.is_file()} == before + + settlement_session = settlement_fixtures.session From 4154712608a5b1402fc0aa801c1ae1940601ee40 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:40:13 +0800 Subject: [PATCH 32/34] Serialize cold import source selection with registry writers Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/presentation/goal_storage_api.py | 34 +++++- .../test_chat_cold_source_import.py | 104 ++++++++++++++++-- 2 files changed, 121 insertions(+), 17 deletions(-) diff --git a/loopx/presentation/goal_storage_api.py b/loopx/presentation/goal_storage_api.py index 11c50c71ed..77017193fa 100644 --- a/loopx/presentation/goal_storage_api.py +++ b/loopx/presentation/goal_storage_api.py @@ -2,6 +2,7 @@ from __future__ import annotations import hashlib +from contextlib import ExitStack import json import os import re @@ -13,7 +14,7 @@ from ..control_plane.effect_runtime import effect_runtime_result from ..control_plane.coordination.local_authority_shadow_projection import source_effect_runtime_result -from ..file_lock import exclusive_file_lock, write_json_atomic +from ..file_lock import exclusive_cross_runtime_file_lock, exclusive_file_lock, write_json_atomic CHAT_GOAL_STORAGE_PATH = "/api/chat/goal-storage" @@ -121,8 +122,9 @@ def _storage_send(self, result: dict[str, Any], goal_id: str, preview_id: str | def _storage_source_registry( self, goal_id: str, registry: dict[str, Any], goal: dict[str, Any], root: Path, + *, registry_path: Path | None = None, ) -> Path: - invoked = Path(self.server.registry_path).resolve() + invoked = registry_path if registry_path is not None else Path(self.server.registry_path).resolve() if not goal.get("source_registry"): return invoked @@ -336,12 +338,32 @@ def _storage_import(self, *, action: str) -> None: request["expected_plan_sha256"] = _token(body, "plan_sha256", 64) if action == "apply": request["writers_stopped"] = body["writers_stopped"] - request["expected_source_registry"] = str( - self._storage_source_registry(goal_id, registry, goal, root) - ) path = self._storage_preview_path(goal_id) path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) - with exclusive_file_lock(path): + with exclusive_file_lock(path), ExitStack() as source_lock: + if action == "apply": + from ..registry import find_registry_goal, load_registry + + invoked = Path(self.server.registry_path).resolve() + source_lock.enter_context(exclusive_cross_runtime_file_lock( + invoked, operation="cold_source_import", + )) + # A global index selects the source but is not in the + # typed cutover's source locks. Re-read its mapping under + # the registry writer's lock and retain it through Apply. + registry = load_registry(invoked) + current_goal = find_registry_goal(registry, goal_id) + if current_goal is None: + raise ValueError("registered Goal source changed") + selected = self._storage_source_registry( + goal_id, registry, current_goal, root, registry_path=invoked, + ) + request["expected_source_registry"] = str(selected) + if selected == invoked: + # The typed owner locks this same file and verifies + # its immutable plan witness before fencing. Do not + # hold a second process's lock across that call. + source_lock.close() if action == "readback": known, saved = os.path.lexists(path), None else: diff --git a/tests/control_plane/test_chat_cold_source_import.py b/tests/control_plane/test_chat_cold_source_import.py index e9cc334562..a72a5601f3 100644 --- a/tests/control_plane/test_chat_cold_source_import.py +++ b/tests/control_plane/test_chat_cold_source_import.py @@ -2,6 +2,7 @@ from __future__ import annotations import http.client +from concurrent.futures import ThreadPoolExecutor import json import subprocess import socket @@ -32,7 +33,7 @@ def cold_api(tmp_path, monkeypatch, request, source_modules_present): # of this same set before the cold receiver is copied. (receiver / "loopx" / name).unlink(missing_ok=True) registry = tmp_path / "project/.loopx/registry.json" - if request.param == "global-source": + if request.param in ("global-source", "global-source-race"): # Keep Chat bound to the source runtime, but invoke it through the # global registry whose own runtime holds the machine index. global_root = tmp_path / "machine-runtime" @@ -64,14 +65,33 @@ def cold_api(tmp_path, monkeypatch, request, source_modules_present): staged_ready = ready.with_suffix('.tmp') staged_ready.write_text(json.dumps({'port': port, 'package': loopx.__file__}), encoding='utf-8') staged_ready.replace(ready) +if len(sys.argv) > 5: + import time + import loopx.presentation.goal_storage_api as storage + original_effect = storage.source_effect_runtime_result + def paused_import(operation, payload, **kwargs): + pause = runtime / 'pause-apply' + if (operation == 'coordination.cold_source.import' + and payload.get('action') == 'apply' and pause.exists()): + pause.with_suffix('.selected').write_text(payload['expected_source_registry']) + deadline = time.monotonic() + 20 + while not pause.with_suffix('.resume').exists(): + if time.monotonic() >= deadline: + raise TimeoutError('test did not resume its paused Apply') + time.sleep(.01) + return original_effect(operation, payload, **kwargs) + storage.source_effect_runtime_result = paused_import serve_chat(registry_path=registry, runtime_root_override=runtime, scan_roots=[Path(json.loads(registry.read_text())['goals'][0]['repo'])], host='127.0.0.1', port=port, goal_id='cold', open_browser=False, verbose=False) """ + server_arguments = [str(registry), str(runtime), str(ready), str(requested_port)] + if request.param == "global-source-race": + server_arguments.append("pause") error_path = tmp_path / "server-errors.txt" error_stream = error_path.open("w") server = subprocess.Popen([sys.executable, "-u", "-c", script, - str(registry), str(runtime), str(ready), str(requested_port)], cwd=tmp_path, + *server_arguments], cwd=tmp_path, env={**env, "LOOPX_USAGE_PING": "0"}, stdout=subprocess.DEVNULL, stderr=error_stream) def execute(script): @@ -89,7 +109,7 @@ def restart(): server.wait(timeout=10) ready.unlink() server = subprocess.Popen([sys.executable, "-u", "-c", script, - str(registry), str(runtime), str(ready), str(requested_port)], cwd=tmp_path, + *server_arguments], cwd=tmp_path, env={**env, "LOOPX_USAGE_PING": "0"}, stdout=subprocess.DEVNULL, stderr=error_stream) port = wait_ready() @@ -177,7 +197,7 @@ def test_cold_http_preview_reload_confirm_original_recovery(cold_api, provider): assert observed["operation_id"] == plan["operation_id"] assert not observed["legacy_writer_fenced"] and not observed["current"]["canonical"] assert state.read_bytes() == original - assert not list(runtime.rglob("writer-fence.json")) + assert not list(runtime.rglob("legacy-writer-fence-*.json")) code, refused = call("apply", {**saved, "writers_stopped": False}) assert code == 409 and refused["reason_code"] == "cold_import_operator_stop_confirmation_required" code, applied = call("apply", {**saved, "writers_stopped": True}) @@ -254,7 +274,7 @@ def test_cold_http_binds_global_mirror_to_the_registered_source(cold_api): registry.write_text(json.dumps(mirror)) code, refused = call("apply", {**saved, "writers_stopped": True}) assert code == 409 and refused["reason_code"] == "source_registry_changed_retry" - assert not list(runtime.rglob("writer-fence.json")) + assert not list(runtime.rglob("legacy-writer-fence-*.json")) registry.write_bytes(original_mirror) code, applied = call("apply", {**saved, "writers_stopped": True}) @@ -263,7 +283,69 @@ def test_cold_http_binds_global_mirror_to_the_registered_source(cold_api): assert applied["execution_authority_granted"] is False assert next(row["text"] for row in read()["todos"] if row["todo_id"] == "todo_current") == body assert state.read_bytes() == original_state - assert not list(registry.parent.rglob("writer-fence.json")) + assert not list(registry.parent.rglob("legacy-writer-fence-*.json")) + + +@pytest.mark.parametrize("cold_api", ["global-source-race"], indirect=True) +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_cold_http_source_rebind_is_ordered_with_cutover(cold_api, provider): + from loopx.control_plane.coordination.legacy_writer_fence import legacy_coordination_writer_fence_path + from loopx.file_lock import exclusive_cross_runtime_file_lock + from loopx.global_registry import GlobalRegistryReduction, mutate_global_registry + + call, read, state, body, runtime, registry, _, _ = cold_api + plan = prepare(call, provider, handoff_mode="soft_claim") + original = state.read_bytes() + source = runtime.parent / "project/.loopx/registry.json" + replacement = source.with_name("replacement-registry.json") + replacement.write_bytes(source.read_bytes()) + pause = runtime / "pause-apply" + pause.touch() + + def rebind(): + def reduce(payload): + payload["goals"][0]["source_registry"] = str(replacement) + return GlobalRegistryReduction(payload, {"bound": "replacement"}) + return mutate_global_registry(registry, "test-source-rebind", reduce) + + with ThreadPoolExecutor(max_workers=2) as pool: + applying = pool.submit(call, "apply", {**carrier(plan), "writers_stopped": True}) + deadline = time.monotonic() + 15 + try: + while not pause.with_suffix(".selected").exists(): + assert not applying.done(), applying.result() if applying.done() else None + assert time.monotonic() < deadline, "Apply did not reach its effect boundary" + time.sleep(.01) + assert pause.with_suffix(".selected").read_text() == str(source) + # This is the existing registry writer's actual cross-runtime lock, + # not a sleep-based guess that the competing thread is blocked. + serialized = False + try: + with exclusive_cross_runtime_file_lock(registry, timeout_seconds=0): + pass + except TimeoutError: + serialized = True + if serialized: + writer = pool.submit(rebind) + else: + rebind() + finally: + pause.with_suffix(".resume").touch() + code, result = applying.result(timeout=90) + fence = legacy_coordination_writer_fence_path(runtime_root=runtime, goal_id="cold") + if serialized: + assert code == 200 and result["status"] == "applied", result + assert writer.result(timeout=10)["wrote"] + assert fence.exists() + assert next(row["text"] for row in read()["todos"] if row["todo_id"] == "todo_current") == body + else: + # A writer that commits first invalidates the old selected source. + # The original bug returned applied and fenced that old source. + assert code == 409, {"response": result, "legacy_fence_exists": fence.exists()} + assert result["authority_changed"] is False and not fence.exists() + assert call()[1]["current"]["canonical"] is False + assert state.read_bytes() == original + assert json.loads(registry.read_text())["goals"][0]["source_registry"] == str(replacement) def test_cold_http_changed_source_backup_and_untrusted_inputs(cold_api): @@ -280,7 +362,7 @@ def test_cold_http_changed_source_backup_and_untrusted_inputs(cold_api): state.write_text(state.read_text() + "\nChanged after preview\n") code, refused = call("apply", {**saved, "writers_stopped": True}) assert code == 409 and refused["reason_code"] == "source_changed_retry", refused - assert not list(runtime.rglob("writer-fence.json")) + assert not list(runtime.rglob("legacy-writer-fence-*.json")) fresh = prepare(call) archives = list((runtime / "backups/cold-import").glob(f"*{fresh['operation_id']}*.tar.gz")) @@ -289,7 +371,7 @@ def test_cold_http_changed_source_backup_and_untrusted_inputs(cold_api): code, refused = call("apply", {**carrier(fresh), "writers_stopped": True}) assert code == 409 and refused["reason_code"] == "cold_import_backup_changed", refused assert call()[1]["current"]["canonical"] is False - assert not list(runtime.rglob("writer-fence.json")) + assert not list(runtime.rglob("legacy-writer-fence-*.json")) @pytest.mark.parametrize("provider", ["file", "sqlite"]) @@ -345,7 +427,7 @@ def test_registered_runtime_alias_import_preserves_original_operation(cold_api, plan = prepare(call, provider) saved = carrier(plan) assert call("recover", saved)[1]["status"] == "prepared" - assert not list(runtime.rglob("writer-fence.json")) + assert not list(runtime.rglob("legacy-writer-fence-*.json")) assert call("apply", {**saved, "writers_stopped": False})[0] == 409 code, applied = call("apply", {**saved, "writers_stopped": True}) assert code == 200 and applied["status"] == "applied", applied @@ -382,8 +464,8 @@ def test_registered_runtime_alias_retarget_rejects_without_import(cold_api, prov before = state.read_bytes() code, refused = call("apply", {**carrier(plan), "writers_stopped": True}) assert code == 409 and refused["reason_code"] == "shadow_source_runtime_root_mismatch", refused - assert not list(runtime.rglob("writer-fence.json")) - assert not list(other.rglob("writer-fence.json")) + assert not list(runtime.rglob("legacy-writer-fence-*.json")) + assert not list(other.rglob("legacy-writer-fence-*.json")) assert state.read_bytes() == before From 19c47a39f10f1e8587e5bd688c3e955568d9ee29 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:52:33 +0800 Subject: [PATCH 33/34] Wrap storage receipt identifiers in narrow settings details Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../src/features/personal-workspace/personal-workspace.css | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css index 36eeacca20..c4a8a4324f 100644 --- a/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css +++ b/apps/presentation/dashboard/src/features/personal-workspace/personal-workspace.css @@ -705,6 +705,7 @@ button.personal-execution-chip:focus-visible { outline: 2px solid #0070f3; outli .personal-settings-card > footer { padding: 13px 20px; border-top: 1px solid var(--pw-line); background: #faf9f6; color: var(--pw-faint); font-size: var(--text-caption); line-height: var(--leading-caption); } .personal-cadence-settings { display: grid; gap: 22px; width: min(100%, 760px); padding: 28px; border: 1px solid var(--pw-line); border-radius: 12px; background: var(--pw-card); } .personal-cadence-form { display: grid; gap: 20px; } +.personal-cadence-form > details { min-width: 0; overflow-wrap: anywhere; } .personal-cadence-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; } .personal-cadence-target { grid-template-columns: 1fr; } .personal-cadence-fields label { display: grid; align-content: start; gap: 7px; min-width: 0; font-size: var(--text-caption); line-height: var(--leading-caption); font-weight: 600; } From fbda48745038fc4e5b7a1afa5b70db74a99499c7 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Tue, 17 Nov 2026 06:33:58 +0800 Subject: [PATCH 34/34] Preserve fresh selection gates while recovering original Turns Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/control_plane/quota/settlement_cli.py | 24 +--- tests/control_plane/test_quota_settlement.py | 4 +- .../test_quota_settlement_cli.py | 124 ++++++++---------- .../test_turn_host_context_delivery.py | 13 +- 4 files changed, 70 insertions(+), 95 deletions(-) diff --git a/loopx/control_plane/quota/settlement_cli.py b/loopx/control_plane/quota/settlement_cli.py index 4998136cd9..6883239ab5 100644 --- a/loopx/control_plane/quota/settlement_cli.py +++ b/loopx/control_plane/quota/settlement_cli.py @@ -273,9 +273,8 @@ def quota_rollout_settlement_binding( """Resolve one rollout binding without mixing authority levels. Explicit settlement arguments and the typed plan are causal identities. - An admitted autonomous replan binds its obligation as the Turn's execution - identity; any selected Todo remains planning context for that decision. - Ordinary delivery may instead bind the current selected Todo. + The current selected Todo is a later projection, while a replan action + packet is only a diagnostic fallback when no concrete Todo is selected. """ if payload.get("effective_action") == EffectiveAction.UNSETTLED_HOST_TURN_RECOVERY.value: @@ -297,25 +296,6 @@ def quota_rollout_settlement_binding( if explicit_todo_id or explicit_replan_obligation_id: return explicit_todo_id, explicit_replan_obligation_id - if ( - payload.get("effective_action") - == EffectiveAction.AUTONOMOUS_REPLAN_REQUIRED.value - ): - replan_packet = ( - payload.get("replan_action_packet") - if isinstance(payload.get("replan_action_packet"), Mapping) - else {} - ) - current_replan_obligation_id = normalize_todo_replan_obligation_id( - replan_packet.get("obligation_id") - ) - if current_replan_obligation_id: - # The selected Todo remains planning context while the semantic - # replan is the Turn's execution obligation. Bind that obligation - # before the portfolio's explicit-selection guard can make the - # committed Turn identity-less and impossible to settle. - return None, current_replan_obligation_id - action_portfolio_value = payload.get("action_portfolio") action_portfolio: Mapping[str, object] = ( action_portfolio_value diff --git a/tests/control_plane/test_quota_settlement.py b/tests/control_plane/test_quota_settlement.py index 670634ebff..ed2651f365 100644 --- a/tests/control_plane/test_quota_settlement.py +++ b/tests/control_plane/test_quota_settlement.py @@ -256,7 +256,7 @@ def test_selected_todo_suppresses_stale_replan_packet_binding() -> None: assert quota_rollout_replan_obligation_id(payload, args) is None -def test_current_replan_binds_before_planning_todo_selection() -> None: +def test_current_replan_preserves_explicit_planning_selection_gate() -> None: obligation_id = "replan-0000000000000001" payload = { "effective_action": EffectiveAction.AUTONOMOUS_REPLAN_REQUIRED.value, @@ -269,7 +269,7 @@ def test_current_replan_binds_before_planning_todo_selection() -> None: args = SimpleNamespace(todo_id=None, replan_obligation_id=None) assert quota_rollout_todo_id(payload, args) is None - assert quota_rollout_replan_obligation_id(payload, args) == obligation_id + assert quota_rollout_replan_obligation_id(payload, args) is None def test_explicit_replan_binding_survives_newly_selected_todo() -> None: diff --git a/tests/control_plane/test_quota_settlement_cli.py b/tests/control_plane/test_quota_settlement_cli.py index 63d5f8fde6..7c98cd6960 100644 --- a/tests/control_plane/test_quota_settlement_cli.py +++ b/tests/control_plane/test_quota_settlement_cli.py @@ -708,51 +708,35 @@ def _projected_cli_args(command: str, *, turn_instance_id: str) -> tuple[str, .. ) -def _admit_selected_replan_guard( +def _bind_selected_replan_guard( registry: Path, runtime: Path, project: Path, turn_instance_id: str, *, goal_id: str = GOAL_ID, agent_id: str = AGENT_ID, todo_id: str = SELECTED_REPLAN_TODO_ID, ) -> dict[str, Any]: - """Admit the review while retaining the explicit Todo as planning input.""" - rc, admitted = _run_cli( + """Choose the fixture Todo explicitly, then consume the generated recovery. + + An initial hard-replan recommendation is planning context, not a binding. + Keep the existing semantic-delta regressions testing an admitted identity. + """ + rc, deferred = _run_cli( registry, runtime, "quota", "should-run", "--codex-app", "--goal-id", goal_id, "--agent-id", agent_id, "--turn-instance-id", turn_instance_id, "--todo-id", todo_id, ) - assert rc == 0, admitted - assert admitted["selected_todo"]["todo_id"] == todo_id - identity = admitted["heartbeat_receipt"]["settlement_identity"] - assert identity["binding_kind"] == "autonomous_replan" - assert identity["replan_obligation_id"] == admitted["replan_action_packet"]["obligation_id"] - assert admitted["heartbeat_receipt"]["pending_action_selection"] == { - "todo_id": todo_id, - "state": "bound", - "reason": "explicit_choice_preserved", - "settlement_bound": True, - } - return admitted - + if rc == 0: + bound = deferred + else: + # Without a prior receipt, first-call refusal remains caller-owned. + assert rc == 1 and deferred["action_selection_qualification"]["state"] == "deferred", deferred + [command] = deferred["interaction_contract"]["cli_channel"]["next_cli_actions"] + rc, bound = _run_generated_cli(command, registry_path=registry) + assert rc == 0, bound + assert bound["heartbeat_receipt"]["settlement_identity"]["todo_id"] == todo_id + return bound -def _bind_selected_replan_guard( - registry: Path, runtime: Path, project: Path, turn_instance_id: str, - *, - goal_id: str = GOAL_ID, - agent_id: str = AGENT_ID, - todo_id: str = SELECTED_REPLAN_TODO_ID, -) -> dict[str, Any]: - """Compatibility name for composition fixtures that admit this guard.""" - return _admit_selected_replan_guard( - registry, - runtime, - project, - turn_instance_id, - goal_id=goal_id, - agent_id=agent_id, - todo_id=todo_id, - ) @pytest.mark.parametrize("provider", ["file", "sqlite"]) @@ -801,7 +785,7 @@ def test_existing_todo_receipt_precedes_later_replan_projection( @pytest.mark.parametrize("provider", ["file", "sqlite"]) -def test_same_explicit_todo_retry_preserves_bound_replan_identity( +def test_same_explicit_todo_retry_preserves_original_identity( tmp_path: Path, provider: str, ) -> None: @@ -831,15 +815,9 @@ def test_same_explicit_todo_retry_preserves_bound_replan_identity( assert first_rc == 0, first original = first["heartbeat_receipt"]["settlement_identity"] - assert original["binding_kind"] == "autonomous_replan" - assert first["heartbeat_receipt"]["pending_action_selection"]["todo_id"] == ( - SELECTED_REPLAN_TODO_ID - ) + assert original["todo_id"] == SELECTED_REPLAN_TODO_ID assert replay_rc == 0, replay assert replay["heartbeat_receipt"]["settlement_identity"] == original - assert replay["heartbeat_receipt"]["pending_action_selection"]["todo_id"] == ( - SELECTED_REPLAN_TODO_ID - ) assert ( replay["interaction_contract"]["cli_channel"]["settlement_plan"][ "identity" @@ -851,7 +829,7 @@ def test_same_explicit_todo_retry_preserves_bound_replan_identity( conflict_args[-1] = ALTERNATIVE_TODO_ID conflict_rc, conflict = _run_cli(registry, runtime, *conflict_args) assert conflict_rc == 1, conflict - assert conflict["error_code"] == "quota_action_selection_conflict" + assert conflict["error_code"] == "heartbeat_receipt_identity_conflict" receipt_events = _heartbeat_receipt_events(runtime, turn) assert receipt_events assert { @@ -860,7 +838,7 @@ def test_same_explicit_todo_retry_preserves_bound_replan_identity( @pytest.mark.parametrize("provider", ["file", "sqlite"]) -def test_implicit_replan_binds_before_planning_todo_and_upgrades_legacy_review_guard( +def test_todoless_replan_upgrades_legacy_review_guard( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, ) -> None: """A legacy review receipt gains its source without rebinding the Turn.""" @@ -870,7 +848,11 @@ def test_implicit_replan_binds_before_planning_todo_and_upgrades_legacy_review_g isolate_sqlite_runtime(tmp_path, monkeypatch) project, runtime, registry = _write_fixture(tmp_path) state = project / f".codex/goals/{GOAL_ID}/ACTIVE_GOAL_STATE.md" - state.write_text(state.read_text().replace("status: active-read-only", "status: active")) + state.write_text( + state.read_text().split("## Agent Todo")[0].replace( + "status: active-read-only", "status: active" + ) + "## Agent Todo\n\n" + ) registry_value = json.loads(registry.read_text()) registry_value["goals"][0]["status"] = "active" registry.write_text(json.dumps(registry_value)) @@ -885,12 +867,11 @@ def call(*args: str, expected_code: int = 0) -> dict[str, Any]: call("configure-goal", "--goal-id", GOAL_ID, "--execution-replan-after-todos", "5", "--execute") - turn = "turn-implicit-replan-review-upgrade" + turn = "turn-todoless-replan-review-upgrade" guard = ("quota", "should-run", "--codex-app", "--goal-id", GOAL_ID, "--agent-id", AGENT_ID, "--turn-instance-id", turn) first = call(*guard) assert first["effective_action"] == "autonomous_replan_required" - assert first["selected_todo"]["todo_id"] == TODO_ID identity = first["heartbeat_receipt"]["settlement_identity"] assert identity["binding_kind"] == "autonomous_replan" assert identity["replan_obligation_id"] == first["replan_action_packet"]["obligation_id"] @@ -2624,7 +2605,7 @@ def test_compact_blocked_wait_keeps_authority_rejections(invalid: str) -> None: @pytest.mark.parametrize("provider", ["file", "sqlite"]) -@pytest.mark.parametrize("writeback_kind", ["in_flight", "semantic"]) +@pytest.mark.parametrize("writeback_kind", ["in_flight", "semantic", "replan"]) @pytest.mark.parametrize("host", ["codex_app", "generic_cli"]) def test_same_todo_writeback_reentry_only_settles_original_turn( tmp_path: Path, provider: str, writeback_kind: str, host: str, @@ -2637,7 +2618,17 @@ def test_same_todo_writeback_reentry_only_settles_original_turn( isolate_sqlite_runtime(tmp_path, monkeypatch) project, runtime, registry = _write_fixture(tmp_path) todo_id = TODO_ID + if writeback_kind == "replan": + _configure_selected_todo_replan_fixture(project, registry) + _initialize_git_checkout(project) + todo_id = SELECTED_REPLAN_TODO_ID _initialize_fixture_authority(project, runtime, provider=provider) + if writeback_kind == "replan": + # This replay fixture supplies the ordinary typed replan delta. Keep + # its strategy explicit; Stride decision followthrough is tested separately. + rc, configured = _run_cli(registry, runtime, "configure-goal", "--goal-id", GOAL_ID, + "--execution-stride-strategy", "off", "--execute") + assert rc == 0, configured turn_id = "turn-same-todo-writeback" host_args = ("--codex-app",) if host == "codex_app" else ("--runtime-profile", "generic_cli") guard_args = ( @@ -4790,7 +4781,7 @@ def test_legacy_todo_guard_keeps_current_replan_gate_strict( assert guard_rc == 0, guard assert guard["decision"] == "autonomous_replan_required", guard - obligation_id = guard["replan_action_packet"]["obligation_id"] + guard = _bind_selected_replan_guard(registry_path, runtime, project, turn_instance_id) log_path = runtime / "goals" / GOAL_ID / "rollout-event-log.jsonl" events = [ @@ -4816,8 +4807,8 @@ def test_legacy_todo_guard_keeps_current_replan_gate_strict( AGENT_ID, "--turn-instance-id", turn_instance_id, - "--replan-obligation-id", - obligation_id, + "--todo-id", + SELECTED_REPLAN_TODO_ID, "--delivery-workspace-path", str(project), "--delivery-outcome", @@ -7344,7 +7335,7 @@ def test_unbound_visible_goal_todoless_replan_reenters_through_guided_turn( def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str, ) -> None: - """Regression for #3528's real autonomous-replan settlement path. + """Regression for #3528's real Todo-bound replan path. A stale repair-delta label does not itself prove a successor, but a fresh typed progress observation can still satisfy the open obligation. The @@ -7377,6 +7368,7 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain( assert guard_rc == 0, guard assert guard["decision"] == "autonomous_replan_required", guard assert guard["selected_todo"]["todo_id"] == SELECTED_REPLAN_TODO_ID + guard = _bind_selected_replan_guard(registry_path, runtime, project, turn_instance_id) obligation_id = guard["replan_action_packet"]["obligation_id"] refresh_args = ( @@ -7387,8 +7379,8 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain( AGENT_ID, "--turn-instance-id", turn_instance_id, - "--replan-obligation-id", - obligation_id, + "--todo-id", + SELECTED_REPLAN_TODO_ID, "--delivery-workspace-path", str(project), "--autonomous-replan-recorded", @@ -7410,10 +7402,10 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain( "evidence:periodic-review", ) mismatched_args = list(refresh_args) - obligation_flag_index = mismatched_args.index("--replan-obligation-id") - mismatched_args[obligation_flag_index : obligation_flag_index + 2] = [ - "--todo-id", - SELECTED_REPLAN_TODO_ID, + todo_flag_index = mismatched_args.index("--todo-id") + mismatched_args[todo_flag_index : todo_flag_index + 2] = [ + "--replan-obligation-id", + obligation_id, ] mismatch_rc, mismatch = _run_cli( registry_path, @@ -7467,8 +7459,7 @@ def test_autonomous_replan_semantic_delta_keeps_accountable_receipt_chain( assert next_guard["normal_delivery_allowed"] is False recovery = next_guard["unsettled_host_turn_recovery"] assert recovery["prior_turn_instance_id"] == turn_instance_id - assert recovery["binding_kind"] == "autonomous_replan" - assert recovery["binding_id"] == obligation_id + assert recovery["binding_id"] == SELECTED_REPLAN_TODO_ID assert recovery["settlement_effect_id"] == original["effect_id"] assert recovery["missing_receipts"] == ["quota_spend_receipt"] assert all(not row["details"].get("settlement_effect_id") @@ -7577,7 +7568,7 @@ def test_open_replan_rejects_missing_semantic_delta_before_durable_write( assert guard["decision"] == "autonomous_replan_required", guard assert guard["selected_todo"]["todo_id"] == SELECTED_REPLAN_TODO_ID - obligation_id = guard["replan_action_packet"]["obligation_id"] + guard = _bind_selected_replan_guard(registry_path, runtime, project, turn_instance_id) refresh_rc, refresh = _run_cli( registry_path, @@ -7589,8 +7580,8 @@ def test_open_replan_rejects_missing_semantic_delta_before_durable_write( AGENT_ID, "--turn-instance-id", turn_instance_id, - "--replan-obligation-id", - obligation_id, + "--todo-id", + SELECTED_REPLAN_TODO_ID, "--delivery-workspace-path", str(project), "--autonomous-replan-recorded", @@ -9202,7 +9193,7 @@ def test_settled_turn_defers_prior_unsettled_history_to_fresh_turn( @pytest.mark.parametrize("provider", ["file", "sqlite"]) @pytest.mark.parametrize("legacy_turn_ref", [False, True]) -def test_exact_later_ack_executes_original_replan_settlement_commands( +def test_exact_later_ack_executes_original_todo_settlement_commands( tmp_path: Path, provider: str, legacy_turn_ref: bool, ) -> None: project, runtime, registry = _write_fixture(tmp_path) @@ -9210,7 +9201,7 @@ def test_exact_later_ack_executes_original_replan_settlement_commands( _initialize_git_checkout(project) _initialize_fixture_authority(project, runtime, provider=provider) original_turn = "turn-historical-todo-settlement" - admitted = _admit_selected_replan_guard(registry, runtime, project, original_turn) + admitted = _bind_selected_replan_guard(registry, runtime, project, original_turn) original_identity = admitted["heartbeat_receipt"]["settlement_identity"] obligation = admitted["replan_action_packet"]["obligation_id"] recorded = datetime.fromisoformat(admitted["heartbeat_receipt"]["recorded_at"].replace("Z", "+00:00")) @@ -9243,10 +9234,11 @@ def test_exact_later_ack_executes_original_replan_settlement_commands( with (runs_dir / "index.jsonl").open("a") as stream: stream.write(json.dumps({**run, "json_path": str(source), "markdown_path": str(markdown)}) + "\n") guard_args = ("quota", "should-run", "--codex-app", "--goal-id", GOAL_ID, - "--agent-id", AGENT_ID, "--turn-instance-id", original_turn) + "--agent-id", AGENT_ID, "--turn-instance-id", original_turn, + "--todo-id", SELECTED_REPLAN_TODO_ID) rc, recovery = _run_cli(registry, runtime, *guard_args) assert rc == 0 and recovery["replan_action_packet"]["decision"] == "historical_ack_recorded", json.dumps({key: recovery.get(key) for key in ["error", "error_code", "decision", "action_selection_qualification", "health"]}) - rc, automatic = _run_cli(registry, runtime, *guard_args) + rc, automatic = _run_cli(registry, runtime, *guard_args[:-2]) assert rc == 0, automatic assert automatic["heartbeat_receipt"]["settlement_identity"] == original_identity actions = recovery["interaction_contract"]["cli_channel"]["next_cli_actions"] diff --git a/tests/control_plane/test_turn_host_context_delivery.py b/tests/control_plane/test_turn_host_context_delivery.py index 7b8435015a..c71b6a9299 100644 --- a/tests/control_plane/test_turn_host_context_delivery.py +++ b/tests/control_plane/test_turn_host_context_delivery.py @@ -145,11 +145,13 @@ def test_captured_context_reaches_actual_host_without_readmission(tmp_path, monk # The current producer's own immutable capture is the transport oracle; # the original long task and optional context remain independent inputs. detail = actual["detail_ref"]["captured_decision"] - assert json.loads(Path(detail["path"]).read_text()) == actual["work_contract"] - assert actual["work_contract"] == plan["work_contract"] - assert "Validate the complete acceptance: " + "preserve original evidence; " * 180 in json.dumps(actual["work_contract"]) + delivered_work = json.loads(Path(detail["path"]).read_text()) + assert delivered_work == plan["work_contract"] + assert actual["source_decision_hash"] == plan["source_decision_hash"] + assert "work_contract" not in actual + assert "Validate the complete acceptance: " + "preserve original evidence; " * 180 in json.dumps(delivered_work) assert "turn_envelope" not in actual and "turn_authority" not in actual - delivered_context = actual["work_contract"]["interaction_contract"].get("agent_context") + delivered_context = delivered_work["interaction_contract"].get("agent_context") assert delivered_context == context if context: assert "detail_ref" not in delivered_context @@ -234,7 +236,8 @@ def test_bound_replan_reaches_actual_host_with_original_evidence(tmp_path, monke prompt_path = project / "received.txt" prompt = prompt_path.read_text() actual, _ = json.JSONDecoder().raw_decode(prompt.split("Turn request:\n", 1)[1]) - assert actual["work_contract"] == plan["work_contract"] + assert actual["source_decision_hash"] == plan["source_decision_hash"] + assert "work_contract" not in actual # This Goal body is a pending original read, exercised below; a complete # contract does not require every retained source to be inline. assert "turn_envelope" not in actual and "turn_authority" not in actual