',
"long-horizon goals",
- "long-running agent teams",
+ "multi-agent teams",
"## Meet the Personal Agent Workspace",
"docs/assets/personal-workspace/research-workbench-hero.webp",
"docs/architecture/rfcs/capable-manager-semantic-handoff-v0.md",
@@ -48,10 +48,8 @@ def main() -> int:
"## Advanced Paths",
"200+ hours of elapsed loop lifetime",
"200+ hour public contribution arc",
- "Redacted owner-run showcase",
"not continuous model execution or unattended production autonomy",
"docs/assets/long-running-loop-openviking-trajectory.png",
- "docs/assets/long-running-loop-ml-experiment-trajectory.png",
"### Recurring Work Presets",
"### Review Agent Work",
"### App and Projection Paths",
@@ -76,7 +74,6 @@ def main() -> int:
"docs/assets/control-plane-board.zh.svg",
"200+ 小时自然时长",
"超过 200 小时的公开贡献轨迹",
- "经过脱敏的 owner-run showcase",
"docs/assets/loopx-lark-developer-group.png",
"docs/assets/loopx-wechat-contact.png",
"微信:
huangrt00",
@@ -103,13 +100,8 @@ def main() -> int:
assert root.find(f".//{svg_ns}script") is None, asset
assert '
str:
" if False and normalized_next_action: # DELIBERATE MUTANT: bypass source recheck.")),),
"tests/control_plane/test_next_action_writeback.py::test_final_commit_rechecks_relevant_source_facts[task]"),
Case("public_refresh_writes_owned_paragraph", (("loopx/state_refresh.py", replacement(
- "if not dry_run:\n runs_dir.mkdir(parents=True, exist_ok=True)\n json_path, markdown_path = reserve_unique_run_paths(runs_dir, generated_at)",
- "if not dry_run:\n resolved_state_file.write_text(\n current_text + \"\\n- \" + normalized_next_action, encoding=\"utf-8\"\n )\n runs_dir.mkdir(parents=True, exist_ok=True)\n json_path, markdown_path = reserve_unique_run_paths(runs_dir, generated_at)")),),
+ "runs_dir.mkdir(parents=True, exist_ok=True)\n json_path, markdown_path = reserve_unique_run_paths(runs_dir, generated_at)",
+ "resolved_state_file.write_text(\n current_text + \"\\n- \" + normalized_next_action, encoding=\"utf-8\"\n )\n runs_dir.mkdir(parents=True, exist_ok=True)\n json_path, markdown_path = reserve_unique_run_paths(runs_dir, generated_at)")),),
WRITER_TEST + "test_public_refresh_step_does_not_write_owned_paragraph"),
Case("fence_unshared_state_lock", ((COORDINATION + "legacy_writer_fence.ts", replacement(
"withFileMutationLock(statePath, () =>",
diff --git a/loopx/bootstrap_command_pack.py b/loopx/bootstrap_command_pack.py
index 03806ebf73..810210e100 100644
--- a/loopx/bootstrap_command_pack.py
+++ b/loopx/bootstrap_command_pack.py
@@ -1365,6 +1365,8 @@ def _build_multi_goal_start_selection_packet(
"activation_state": "goal_selection_required",
"activation_allowed": False,
"activation_required_after_todo_write": False,
+ "host_surface": None,
+ "activation_method": None,
},
"available_slash_commands": slash_command_catalog,
"onboarding_hint": slash_command_catalog["onboarding"],
diff --git a/loopx/canary/module_metric_baseline.json b/loopx/canary/module_metric_baseline.json
index 9e224aaf1d..f141343d48 100644
--- a/loopx/canary/module_metric_baseline.json
+++ b/loopx/canary/module_metric_baseline.json
@@ -42,9 +42,6 @@
"any_count": 5,
"dict_any_count": 0
},
- "loopx/capabilities/issue_fix/pr_lifecycle.py": {
- "lines": 2056
- },
"loopx/capabilities/reward_memory/utility_reducer.py": {
"any_count": 0,
"dict_any_count": 0,
diff --git a/loopx/capabilities/configuration_ui.py b/loopx/capabilities/configuration_ui.py
index d6803d4cb0..77ac8dac60 100644
--- a/loopx/capabilities/configuration_ui.py
+++ b/loopx/capabilities/configuration_ui.py
@@ -242,7 +242,10 @@ def capability_configuration_editor(
"string_list",
description=(
"One authorized executor id per line. Required only for "
- "flexible selection; include the primary executor."
+ "flexible selection; include the primary executor. "
+ "Optional per-endpoint model and reasoning effort overrides "
+ "(endpoint_models) are authored in JSON mode and preserved "
+ "by guided saves."
),
),
_field(
@@ -252,7 +255,9 @@ def capability_configuration_editor(
nullable=True,
description=(
"Optional model for the selected executor. Leave blank to keep "
- "the executor's own default."
+ "the executor's own default. Configure per-endpoint overrides "
+ "(endpoint_models: {\"\": {\"model\": ..., "
+ "\"reasoning_effort\": ...}}) in JSON mode."
),
),
_field(
diff --git a/loopx/capabilities/issue_fix/correction_settlement.py b/loopx/capabilities/issue_fix/correction_settlement.py
index 55478c4316..d9908f5c16 100644
--- a/loopx/capabilities/issue_fix/correction_settlement.py
+++ b/loopx/capabilities/issue_fix/correction_settlement.py
@@ -1,26 +1,101 @@
-"""Maintainer-correction settlement and origin-request binding helpers."""
+"""Maintainer-correction admission, transition, settlement, and origin-request binding."""
from __future__ import annotations
+import hashlib
+import ipaddress
import json
import os
-from collections.abc import Mapping
-from pathlib import Path
-from typing import NotRequired, TypedDict
+import re
+from collections.abc import Mapping, Sequence
+from datetime import datetime, timezone
+from pathlib import Path, PurePosixPath
+from typing import Any, NotRequired, TypedDict
+from urllib.parse import urlsplit
-from ...agent_registry import load_goal_from_registry
-from ...control_plane.goals.activation import goal_is_stopped
from ...control_plane.projects.registry_codec import (
SOURCE_SESSION_PROFILE_ID,
load_project_registry,
)
+from ...control_plane.runtime.public_safety import public_safe_compact_text
from ...control_plane.runtime.time import now_utc_iso
-from ...control_plane.todos.mutation_api import add_goal_todo
from ...domain_packs.issue_fix import (
issue_fix_maintainer_correction_entries,
read_issue_fix_pr_lifecycle_ledger_row,
upsert_issue_fix_pr_lifecycle_ledger_jsonl,
)
-from .pr_lifecycle import recheck_issue_fix_maintainer_correction_effect_gates
+from .github_public import MAINTAINER_ASSOCIATIONS
+
+
+ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION = (
+ "issue_fix_maintainer_correction_input_v0"
+)
+ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION_V1 = (
+ "issue_fix_maintainer_correction_input_v1"
+)
+MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSIONS = {
+ ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION,
+ ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION_V1,
+}
+ISSUE_FIX_MAINTAINER_CORRECTION_ADMISSION_SCHEMA_VERSION = (
+ "issue_fix_maintainer_correction_admission_v0"
+)
+ISSUE_FIX_MAINTAINER_CORRECTION_LEDGER_ENTRY_SCHEMA_VERSION = (
+ "issue_fix_maintainer_correction_ledger_entry_v0"
+)
+MAINTAINER_CORRECTION_OPERATION_ID_PREFIX = "issue-fix-correction:"
+MAINTAINER_CORRECTION_KINDS = {
+ "actionable_patch",
+ "semantic_ambiguity",
+ "missing_authority",
+ "unchanged",
+}
+MAINTAINER_CORRECTION_SOURCE_KINDS = {"review", "maintainer_comment"}
+MAINTAINER_CORRECTION_SOURCE_EVENT_KINDS = {
+ "issue_comment",
+ "review",
+ "review_comment",
+}
+MAINTAINER_CORRECTION_SOURCE_EVENT_FIELDS = {
+ "kind",
+ "id",
+ "html_url",
+ "author_login",
+ "author_association",
+ "created_at",
+ "updated_at",
+}
+MAINTAINER_CORRECTION_ADMISSION_BASES = {"host_asserted", "provider_verified"}
+MAINTAINER_CORRECTION_DISPOSITIONS = {"accepted", "context_only", "superseded"}
+MAINTAINER_CORRECTION_REJECTION_REASONS = {
+ "source_event_not_observed",
+ "source_event_revision_mismatch",
+ "stale_revision",
+ "self_or_bot_author",
+ "author_not_maintainer",
+ "terminal_pr_state",
+ "goal_not_active",
+ "origin_request_cancelled",
+ "origin_request_unreadable",
+}
+# GitHub Apps post as `[bot]`; without a configured LoopX actor login this
+# suffix is the only self/bot signal available from body-free event metadata.
+BOT_LOGIN_SUFFIX = "[bot]"
+_GITHUB_LOGIN_PATTERN = re.compile(r"[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})(?:\[bot\])?")
+_PROVIDER_EVENT_ID_PATTERN = re.compile(r"[0-9]{1,20}")
+WRITE_SCOPES = {"write", "publish", "external_review_request"}
+MAINTAINER_CORRECTION_INPUT_FIELDS = {
+ "schema_version",
+ "correction_kind",
+ "source_kind",
+ "source_ref",
+ "summary",
+ "verification_plan",
+ "pr_update_path",
+ "user_question",
+ "missing_authority_scopes",
+ "source_event",
+}
+TERMINAL_PR_STATES = {"MERGED", "CLOSED"}
# Set to "1" to abort between the accepted ledger entry write and its Todo
@@ -34,6 +109,731 @@
}
+def _upper_label(value: Any, default: str = "UNKNOWN") -> str:
+ text = str(value or "").strip().upper()
+ return text or default
+
+
+def _timestamp(value: Any) -> datetime | None:
+ text = str(value or "").strip()
+ if not text:
+ return None
+ try:
+ parsed = datetime.fromisoformat(text.replace("Z", "+00:00"))
+ except ValueError:
+ return None
+ return parsed if parsed.tzinfo is not None else None
+
+
+def build_issue_fix_transition_preview(
+ *,
+ decision: str,
+ action_kind: str,
+ priority: str,
+ reason: str,
+ depends_on: Sequence[str] = ("issue_fix_pr_lifecycle_monitor",),
+ role: str = "agent",
+ task_class: str = "continuous_monitor",
+ text: str | None = None,
+ terminal_state_precedence: bool = False,
+ material_change: bool = False,
+ **extra: Any,
+) -> dict[str, Any]:
+ """Return a non-mutating PR lifecycle transition preview payload."""
+
+ if text is None:
+ text = f"[{priority}] {reason}"
+ return {
+ "schema_version": "issue_fix_pr_lifecycle_transition_v0",
+ "decision": decision,
+ "command_preview": "loopx todo transition",
+ "role": role,
+ "priority": priority,
+ "task_class": task_class,
+ "action_kind": action_kind,
+ "reason": reason,
+ "text": text,
+ "depends_on": list(depends_on),
+ "would_write": False,
+ "requires_execute_flag": True,
+ "terminal_state_precedence": terminal_state_precedence,
+ "material_change": material_change,
+ **extra,
+ }
+
+
+def _compact_public_text(value: Any, *, field: str, limit: int) -> str:
+ raw = " ".join(str(value or "").strip().split())
+ if len(raw) > limit:
+ raise ValueError(f"maintainer correction {field} exceeds the compact limit")
+ if "" in raw or "\x00" in raw:
+ raise ValueError(
+ f"maintainer correction {field} must not contain control-plane markup"
+ )
+ text = public_safe_compact_text(value, limit=limit)
+ if not text:
+ raise ValueError(
+ f"maintainer correction {field} must be compact and public-safe"
+ )
+ return " ".join(text.split())
+
+
+def _public_source_reference(value: Any) -> str:
+ reference = _compact_public_text(value, field="source_ref", limit=300)
+ if "://" in reference:
+ parsed = urlsplit(reference)
+ if (
+ parsed.scheme != "https"
+ or not parsed.netloc
+ or parsed.username
+ or parsed.password
+ or parsed.query
+ ):
+ raise ValueError(
+ "maintainer correction source_ref URL must be public https without user info or query"
+ )
+ hostname = (parsed.hostname or "").lower()
+ if hostname == "localhost" or hostname.endswith((".localhost", ".local")):
+ raise ValueError(
+ "maintainer correction source_ref must not target a local host"
+ )
+ try:
+ address = ipaddress.ip_address(hostname)
+ except ValueError:
+ address = None
+ if address and (
+ address.is_private
+ or address.is_loopback
+ or address.is_link_local
+ or address.is_reserved
+ ):
+ raise ValueError(
+ "maintainer correction source_ref must target a public host"
+ )
+ return reference
+ if reference.startswith(("/", "~")) or re.match(r"^[A-Za-z]:[\\/]", reference):
+ raise ValueError("maintainer correction source_ref must not be a local path")
+ path = PurePosixPath(reference.replace("\\", "/"))
+ if path.as_posix() == "." or ".." in path.parts:
+ raise ValueError("maintainer correction source_ref must be repo-relative")
+ return path.as_posix()
+
+
+def _normalise_provider_event_id(value: Any) -> str:
+ if isinstance(value, bool):
+ raise ValueError("maintainer correction source_event id must be a provider id")
+ text = str(value if value is not None else "").strip()
+ if not _PROVIDER_EVENT_ID_PATTERN.fullmatch(text):
+ raise ValueError("maintainer correction source_event id must be a numeric provider id")
+ return text
+
+
+def _normalise_author_login(value: Any, *, field: str) -> str:
+ text = str(value or "").strip()
+ if not _GITHUB_LOGIN_PATTERN.fullmatch(text):
+ raise ValueError(f"maintainer correction {field} must be a public provider login")
+ return text
+
+
+def _normalise_event_timestamp(value: Any, *, field: str) -> str:
+ text = str(value or "").strip()
+ parsed = _timestamp(text)
+ if parsed is None:
+ raise ValueError(
+ f"maintainer correction {field} must be a timezone-aware ISO timestamp"
+ )
+ return parsed.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
+
+
+def normalise_issue_fix_maintainer_correction_source_event(
+ value: Any,
+) -> dict[str, Any]:
+ """Normalise the provider event that carried a maintainer correction.
+
+ Only identity and revision metadata survive; no body field is accepted.
+ """
+
+ if not isinstance(value, Mapping):
+ raise ValueError("maintainer correction source_event must be an object")
+ unknown_fields = set(value) - MAINTAINER_CORRECTION_SOURCE_EVENT_FIELDS
+ if unknown_fields:
+ raise ValueError(
+ "maintainer correction source_event contains unsupported fields: "
+ + ", ".join(sorted(str(field) for field in unknown_fields))
+ )
+ kind = str(value.get("kind") or "").strip()
+ if kind not in MAINTAINER_CORRECTION_SOURCE_EVENT_KINDS:
+ raise ValueError(
+ "maintainer correction source_event kind must be issue_comment, review, or review_comment"
+ )
+ html_url = _public_source_reference(value.get("html_url"))
+ if "://" not in html_url:
+ raise ValueError("maintainer correction source_event html_url must be a public https URL")
+ return {
+ "kind": kind,
+ "id": _normalise_provider_event_id(value.get("id")),
+ "html_url": html_url,
+ "author_login": _normalise_author_login(
+ value.get("author_login"), field="source_event author_login"
+ ),
+ "author_association": _upper_label(value.get("author_association"), "NONE"),
+ "created_at": _normalise_event_timestamp(
+ value.get("created_at"), field="source_event created_at"
+ ),
+ "updated_at": _normalise_event_timestamp(
+ value.get("updated_at"), field="source_event updated_at"
+ ),
+ "body_captured": False,
+ }
+
+
+def normalise_issue_fix_maintainer_correction_input(
+ value: Mapping[str, Any],
+) -> dict[str, Any]:
+ unknown_fields = set(value) - MAINTAINER_CORRECTION_INPUT_FIELDS
+ if unknown_fields:
+ raise ValueError(
+ "maintainer correction input contains unsupported fields: "
+ + ", ".join(sorted(str(field) for field in unknown_fields))
+ )
+ schema_version = value.get("schema_version")
+ if schema_version not in MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSIONS:
+ raise ValueError(
+ "maintainer correction schema_version must be "
+ "issue_fix_maintainer_correction_input_v0 or issue_fix_maintainer_correction_input_v1"
+ )
+ provider_verified = (
+ schema_version == ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION_V1
+ )
+ if provider_verified:
+ if "source_event" not in value:
+ raise ValueError(
+ "issue_fix_maintainer_correction_input_v1 requires source_event"
+ )
+ source_event: dict[str, Any] | None = (
+ normalise_issue_fix_maintainer_correction_source_event(
+ value.get("source_event")
+ )
+ )
+ else:
+ if "source_event" in value:
+ raise ValueError(
+ "source_event requires schema_version issue_fix_maintainer_correction_input_v1"
+ )
+ source_event = None
+ correction_kind = str(value.get("correction_kind") or "").strip()
+ if correction_kind not in MAINTAINER_CORRECTION_KINDS:
+ raise ValueError(
+ "maintainer correction correction_kind must be actionable_patch, semantic_ambiguity, missing_authority, or unchanged"
+ )
+ source_kind = str(value.get("source_kind") or "").strip()
+ if source_kind not in MAINTAINER_CORRECTION_SOURCE_KINDS:
+ raise ValueError(
+ "maintainer correction source_kind must be review or maintainer_comment"
+ )
+ source_ref = _public_source_reference(value.get("source_ref"))
+ summary = _compact_public_text(value.get("summary"), field="summary", limit=400)
+ verification_plan = ""
+ pr_update_path = ""
+ user_question = ""
+ missing_authority_scopes: list[str] = []
+ if correction_kind == "actionable_patch":
+ verification_plan = _compact_public_text(
+ value.get("verification_plan"), field="verification_plan", limit=300
+ )
+ pr_update_path = _compact_public_text(
+ value.get("pr_update_path"), field="pr_update_path", limit=240
+ )
+ elif correction_kind == "semantic_ambiguity":
+ user_question = _compact_public_text(
+ value.get("user_question"), field="user_question", limit=300
+ )
+ elif correction_kind == "missing_authority":
+ raw_scopes = value.get("missing_authority_scopes")
+ if not isinstance(raw_scopes, Sequence) or isinstance(raw_scopes, (str, bytes)):
+ raise ValueError("missing_authority requires missing_authority_scopes")
+ for raw_scope in raw_scopes:
+ scope = str(raw_scope or "").strip()
+ if scope not in WRITE_SCOPES:
+ raise ValueError("maintainer correction authority scope is unsupported")
+ if scope not in missing_authority_scopes:
+ missing_authority_scopes.append(scope)
+ if not missing_authority_scopes:
+ raise ValueError("missing_authority requires at least one authority scope")
+ # The fingerprint covers the compacted correction content under the v0
+ # field set, so existing v0 ledger rows keep their recorded fingerprints and
+ # a v1 event carrying identical content hashes the same; event identity
+ # lives in the operation id, not here.
+ content = {
+ "schema_version": ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION,
+ "correction_kind": correction_kind,
+ "source_kind": source_kind,
+ "source_ref": source_ref,
+ "summary": summary,
+ "verification_plan": verification_plan or None,
+ "pr_update_path": pr_update_path or None,
+ "user_question": user_question or None,
+ "missing_authority_scopes": missing_authority_scopes,
+ "raw_body_captured": False,
+ "raw_comment_captured": False,
+ "raw_provider_payload_captured": False,
+ "local_paths_captured": False,
+ }
+ fingerprint = hashlib.sha256(
+ json.dumps(content, sort_keys=True, separators=(",", ":")).encode("utf-8")
+ ).hexdigest()[:16]
+ normalized = {
+ **content,
+ "schema_version": str(schema_version),
+ "source_event": source_event,
+ "admission_basis": "provider_verified" if provider_verified else "host_asserted",
+ }
+ normalized["correction_fingerprint"] = fingerprint
+ return normalized
+
+
+def issue_fix_maintainer_correction_operation_id(
+ correction: Mapping[str, Any], *, repo: str, pr_ref: str
+) -> str:
+ """Return the stable Todo operation id for one correction revision."""
+
+ source_event = correction.get("source_event")
+ if isinstance(source_event, Mapping):
+ identity = [
+ repo,
+ pr_ref,
+ str(source_event.get("kind")),
+ str(source_event.get("id")),
+ str(source_event.get("updated_at")),
+ ]
+ else:
+ identity = [repo, pr_ref, str(correction.get("correction_fingerprint"))]
+ digest = hashlib.sha256(
+ json.dumps(identity, separators=(",", ":")).encode("utf-8")
+ ).hexdigest()[:24]
+ return f"{MAINTAINER_CORRECTION_OPERATION_ID_PREFIX}{digest}"
+
+
+def _provider_event_entries(value: Any) -> list[Mapping[str, Any]]:
+ if isinstance(value, Sequence) and not isinstance(value, (str, bytes)):
+ return [item for item in value if isinstance(item, Mapping)]
+ return []
+
+
+def normalise_issue_fix_pr_source_events(
+ provider_payload: Mapping[str, Any] | None,
+) -> dict[str, Any]:
+ """Project body-free PR comment/review metadata into verifiable events.
+
+ Reviews expose ``submitted_at`` instead of ``updated_at``; that timestamp
+ stands in as the review revision because GitHub does not re-time edits of a
+ review summary through the list endpoint.
+ """
+
+ payload = provider_payload if isinstance(provider_payload, Mapping) else {}
+ groups = (
+ ("issue_comment", payload.get("comments") or payload.get("issueComments")),
+ ("review", payload.get("reviews")),
+ ("review_comment", payload.get("reviewComments") or payload.get("review_comments")),
+ )
+ available = any(
+ isinstance(raw, Sequence) and not isinstance(raw, (str, bytes))
+ for _kind, raw in groups
+ )
+ events: list[dict[str, Any]] = []
+ for kind, raw in groups:
+ for item in _provider_event_entries(raw):
+ raw_id = item.get("id")
+ if isinstance(raw_id, bool) or raw_id is None:
+ continue
+ event_id = str(raw_id).strip()
+ if not _PROVIDER_EVENT_ID_PATTERN.fullmatch(event_id):
+ continue
+ author = item.get("author") or item.get("user")
+ if isinstance(author, Mapping):
+ author = author.get("login")
+ updated_at = (
+ item.get("updated_at")
+ or item.get("updatedAt")
+ or item.get("submitted_at")
+ or item.get("submittedAt")
+ )
+ created_at = item.get("created_at") or item.get("createdAt")
+ events.append(
+ {
+ "kind": kind,
+ "id": event_id,
+ "author_login": str(author or "").strip()[:60] or None,
+ "author_association": _upper_label(
+ item.get("author_association")
+ or item.get("authorAssociation"),
+ "NONE",
+ ),
+ "created_at": (
+ _normalise_event_timestamp(created_at, field="created_at")
+ if _timestamp(created_at) else None
+ ),
+ "updated_at": (
+ _normalise_event_timestamp(updated_at, field="updated_at")
+ if _timestamp(updated_at) else None
+ ),
+ "body_captured": False,
+ }
+ )
+ return {
+ "status": "available" if available else "unavailable",
+ "events": events,
+ "body_captured": False,
+ }
+
+
+def _same_instant(left: Any, right: Any) -> bool:
+ left_time = _timestamp(left)
+ right_time = _timestamp(right)
+ return left_time is not None and right_time is not None and left_time == right_time
+
+
+def _is_bot_or_self_login(login: Any) -> bool:
+ text = str(login or "").strip().lower()
+ return text.endswith(BOT_LOGIN_SUFFIX)
+
+
+def _observed_source_events(
+ source_events: Mapping[str, Any] | None, source_event: Mapping[str, Any]
+) -> list[Mapping[str, Any]]:
+ raw_events = source_events.get("events") if isinstance(source_events, Mapping) else []
+ if not isinstance(raw_events, Sequence) or isinstance(raw_events, (str, bytes)):
+ return []
+ return [
+ event
+ for event in raw_events
+ if isinstance(event, Mapping)
+ and event.get("kind") == source_event.get("kind")
+ and str(event.get("id")) == str(source_event.get("id"))
+ ]
+
+
+def _latest_accepted_revision(
+ prior_entries: Sequence[Mapping[str, Any]], *, source_event: Mapping[str, Any]
+) -> dict[str, Any] | None:
+ latest: dict[str, Any] | None = None
+ for entry in prior_entries:
+ event = entry.get("source_event")
+ if not isinstance(event, Mapping):
+ continue
+ if event.get("kind") != source_event.get("kind") or str(
+ event.get("id")
+ ) != str(source_event.get("id")):
+ continue
+ if entry.get("disposition") not in {"accepted", "superseded"}:
+ continue
+ candidate_time = _timestamp(event.get("updated_at"))
+ if candidate_time is None:
+ continue
+ latest_time = (
+ _timestamp(latest["source_event"].get("updated_at")) if latest else None
+ )
+ if latest_time is None or candidate_time > latest_time:
+ latest = dict(entry)
+ return latest
+
+
+def evaluate_issue_fix_maintainer_correction_admission(
+ correction: Mapping[str, Any],
+ *,
+ observation: Mapping[str, Any],
+ source_events: Mapping[str, Any] | None,
+ prior_entries: Sequence[Mapping[str, Any]] | None,
+ prior_ledger_read: bool,
+ recorded_at: str | None,
+) -> dict[str, Any]:
+ """Run the deterministic admission gates for one maintainer correction.
+
+ Returns the admission record plus the ledger entries this run touches:
+ the entry for this correction and any earlier revision it supersedes.
+ """
+
+ repo = str(observation.get("repo") or "")
+ pr_ref = str(observation.get("pr_ref") or "")
+ operation_id = issue_fix_maintainer_correction_operation_id(
+ correction, repo=repo, pr_ref=pr_ref
+ )
+ source_event = correction.get("source_event")
+ admission_basis = str(correction.get("admission_basis") or "host_asserted")
+ prior = issue_fix_maintainer_correction_entries(
+ {"maintainer_corrections": prior_entries}
+ )
+ existing_entry = next(
+ (entry for entry in prior if entry.get("operation_id") == operation_id), None
+ )
+ gates: list[dict[str, Any]] = []
+ reason: str | None = None
+ verified_event: dict[str, Any] | None = None
+ provider_events_status = (
+ str(source_events.get("status") or "unavailable")
+ if isinstance(source_events, Mapping)
+ else "unavailable"
+ )
+
+ def record(name: str, passed: bool, failure_reason: str | None = None) -> None:
+ nonlocal reason
+ gates.append({"gate": name, "passed": passed})
+ if not passed and reason is None:
+ reason = failure_reason
+
+ if isinstance(source_event, Mapping):
+ observed = _observed_source_events(source_events, source_event)
+ if not observed:
+ record("source_event_observed", False, "source_event_not_observed")
+ elif not any(
+ _same_instant(event.get("updated_at"), source_event.get("updated_at"))
+ for event in observed
+ ):
+ record("source_event_observed", False, "source_event_revision_mismatch")
+ else:
+ record("source_event_observed", True)
+ provider_event = observed[0]
+ verified_event = {
+ **dict(source_event),
+ "author_login": str(
+ provider_event.get("author_login") or source_event.get("author_login")
+ ),
+ "author_association": _upper_label(
+ provider_event.get("author_association"),
+ str(source_event.get("author_association") or "NONE"),
+ ),
+ "created_at": _normalise_event_timestamp(
+ provider_event.get("created_at") or source_event.get("created_at"),
+ field="source_event created_at",
+ ),
+ }
+ effective_event = verified_event or dict(source_event)
+ latest_prior = _latest_accepted_revision(prior, source_event=source_event)
+ prior_time = (
+ _timestamp(latest_prior["source_event"].get("updated_at"))
+ if latest_prior
+ else None
+ )
+ incoming_time = _timestamp(source_event.get("updated_at"))
+ revision_relation = "first"
+ if prior_time is not None and incoming_time is not None:
+ if incoming_time < prior_time:
+ revision_relation = "stale"
+ elif incoming_time == prior_time:
+ revision_relation = "replay"
+ else:
+ revision_relation = "newer"
+ record("revision_not_stale", revision_relation != "stale", "stale_revision")
+ record(
+ "not_self_or_bot_author",
+ not _is_bot_or_self_login(effective_event.get("author_login")),
+ "self_or_bot_author",
+ )
+ record(
+ "author_is_maintainer",
+ _upper_label(effective_event.get("author_association"), "NONE")
+ in MAINTAINER_ASSOCIATIONS,
+ "author_not_maintainer",
+ )
+ else:
+ effective_event = None
+ latest_prior = None
+ revision_relation = "replay" if existing_entry else "first"
+ record(
+ "pr_state_not_terminal",
+ str(observation.get("state") or "") not in TERMINAL_PR_STATES,
+ "terminal_pr_state",
+ )
+
+ accepted = reason is None
+ replay = existing_entry is not None and (
+ not accepted or existing_entry.get("disposition") in {"accepted", "superseded"}
+ )
+ if replay:
+ entry = dict(existing_entry)
+ else:
+ entry = {
+ "schema_version": ISSUE_FIX_MAINTAINER_CORRECTION_LEDGER_ENTRY_SCHEMA_VERSION,
+ "operation_id": operation_id,
+ "source_event": effective_event,
+ "correction_fingerprint": correction.get("correction_fingerprint"),
+ "correction_kind": correction.get("correction_kind"),
+ "admission_basis": admission_basis,
+ "disposition": "accepted" if accepted else "context_only",
+ "reason": reason,
+ "todo_id": None,
+ "accepted_head_sha": observation.get("head_commit_ref") if accepted else None,
+ "recorded_at": recorded_at,
+ "superseded_by": None,
+ "body_captured": False,
+ }
+ touched_entries = [entry]
+ superseded_operation_id = None
+ if (
+ accepted
+ and revision_relation == "newer"
+ and latest_prior is not None
+ and latest_prior.get("operation_id") != operation_id
+ ):
+ superseded_operation_id = str(latest_prior.get("operation_id"))
+ touched_entries.append(
+ {
+ **latest_prior,
+ "disposition": "superseded",
+ "superseded_by": operation_id,
+ }
+ )
+ return {
+ "schema_version": ISSUE_FIX_MAINTAINER_CORRECTION_ADMISSION_SCHEMA_VERSION,
+ "operation_id": operation_id,
+ "admission_basis": admission_basis,
+ "disposition": "accepted" if accepted else "context_only",
+ "reason": reason,
+ "host_summary_mismatch": bool(
+ existing_entry is not None
+ and correction.get("correction_fingerprint")
+ != existing_entry.get("correction_fingerprint")
+ ),
+ "gates": gates,
+ "revision_relation": revision_relation,
+ "replay": replay,
+ "supersedes_operation_id": superseded_operation_id,
+ "provider_events_status": provider_events_status,
+ "prior_ledger_read": bool(prior_ledger_read),
+ "source_event": effective_event,
+ "entries": touched_entries,
+ "body_captured": False,
+ }
+
+
+def recheck_issue_fix_maintainer_correction_effect_gates(
+ entry: Mapping[str, Any],
+ *,
+ observation: Mapping[str, Any],
+ ledger_entries: Sequence[Mapping[str, Any]] | None,
+ goal_stopped: bool,
+ source_events: Mapping[str, Any] | None = None,
+) -> str | None:
+ """Re-run the effect-time gates for an accepted entry just before its Todo write."""
+
+ source_event = entry.get("source_event")
+ if isinstance(source_event, Mapping):
+ observed = _observed_source_events(source_events, source_event)
+ if not observed:
+ return "source_event_not_observed"
+ current_event = next(
+ (
+ event
+ for event in observed
+ if _same_instant(event.get("updated_at"), source_event.get("updated_at"))
+ ),
+ None,
+ )
+ if current_event is None:
+ return "source_event_revision_mismatch"
+ latest = _latest_accepted_revision(
+ issue_fix_maintainer_correction_entries(
+ {"maintainer_corrections": ledger_entries}
+ ),
+ source_event=source_event,
+ )
+ if latest is not None and latest.get("operation_id") != entry.get("operation_id"):
+ latest_time = _timestamp(latest["source_event"].get("updated_at"))
+ own_time = _timestamp(source_event.get("updated_at"))
+ if latest_time is not None and own_time is not None and own_time < latest_time:
+ return "stale_revision"
+ if _is_bot_or_self_login(current_event.get("author_login")):
+ return "self_or_bot_author"
+ if (
+ _upper_label(current_event.get("author_association"), "NONE")
+ not in MAINTAINER_ASSOCIATIONS
+ ):
+ return "author_not_maintainer"
+ if str(observation.get("state") or "") in TERMINAL_PR_STATES:
+ return "terminal_pr_state"
+ if goal_stopped:
+ return "goal_not_active"
+ return None
+
+
+def maintainer_correction_transition(
+ correction: Mapping[str, Any],
+ *,
+ pr_permalink: str,
+) -> dict[str, Any]:
+ """Build the transition preview dict for one admitted maintainer correction."""
+
+ correction_kind = str(correction.get("correction_kind"))
+ source_ref = str(correction.get("source_ref"))
+ summary = str(correction.get("summary"))
+ source_event = correction.get("source_event")
+ # Todo creation dedupes identical active text, so distinct provider events
+ # or revisions with the same wording must name the event they came from.
+ event_suffix = (
+ f" Source event: {source_event.get('kind')} {source_event.get('id')}"
+ f" updated {source_event.get('updated_at')}."
+ if isinstance(source_event, Mapping)
+ else ""
+ )
+ if correction_kind == "actionable_patch":
+ verification_plan = str(correction.get("verification_plan"))
+ pr_update_path = str(correction.get("pr_update_path"))
+ return build_issue_fix_transition_preview(
+ decision="runnable_successor",
+ action_kind="issue_fix_maintainer_correction_patch",
+ priority="P0",
+ task_class="advancement_task",
+ reason="Maintainer correction is bounded and actionable; create one claimed patch successor.",
+ text=(
+ f"[P0] Apply maintainer correction for {pr_permalink} from {source_ref}: "
+ f"{summary} Verify: {verification_plan} Update PR via: {pr_update_path}"
+ f"{event_suffix}"
+ ),
+ material_change=True,
+ required_write_scopes=["write", "publish"],
+ )
+ if correction_kind == "semantic_ambiguity":
+ return build_issue_fix_transition_preview(
+ decision="user_gate",
+ action_kind="clarify_issue_fix_maintainer_correction",
+ priority="P0",
+ role="user",
+ task_class="user_gate",
+ reason="Maintainer correction changes or leaves the intended behavior ambiguous.",
+ text=(
+ f"[P0] Clarify maintainer correction for {pr_permalink} from {source_ref}: "
+ f"{correction.get('user_question')} Context: {summary}{event_suffix}"
+ ),
+ material_change=True,
+ )
+ if correction_kind == "missing_authority":
+ scopes = ", ".join(
+ str(value) for value in correction.get("missing_authority_scopes") or []
+ )
+ return build_issue_fix_transition_preview(
+ decision="user_gate",
+ action_kind="grant_issue_fix_maintainer_correction_authority",
+ priority="P0",
+ role="user",
+ task_class="user_gate",
+ reason="The correction is understood but the required write authority is not active.",
+ text=(
+ f"[P0] Grant or decline [{scopes}] authority for maintainer correction "
+ f"{source_ref} on {pr_permalink}: {summary}{event_suffix}"
+ ),
+ material_change=True,
+ missing_authority_scopes=list(
+ correction.get("missing_authority_scopes") or []
+ ),
+ )
+ return build_issue_fix_transition_preview(
+ decision="monitor_continuation",
+ action_kind="issue_fix_maintainer_correction_unchanged_monitor",
+ priority="P2",
+ reason="Maintainer correction is unchanged; keep the monitor quiet and create no successor.",
+ )
+
+
def _require_origin_registry(registry_path: Path) -> None:
"""Check the origin profile before looking up the request in its store."""
try:
@@ -47,6 +847,9 @@ def _require_origin_registry(registry_path: Path) -> None:
def _goal_stopped_for_correction(registry_path: Path | None, goal_id: str | None) -> bool:
if registry_path is None or not goal_id:
return False
+ from ...agent_registry import load_goal_from_registry
+ from ...control_plane.goals.activation import goal_is_stopped
+
try:
goal = load_goal_from_registry(registry_path, goal_id)
if goal is None:
@@ -80,6 +883,8 @@ def _write_maintainer_correction_todo(
operation_id: str,
intent: _CorrectionTodoIntent,
) -> dict[str, object]:
+ from ...control_plane.todos.mutation_api import add_goal_todo
+
role = str(intent.get("role") or "agent")
return add_goal_todo(
registry_path=registry_path,
diff --git a/loopx/capabilities/issue_fix/pr_lifecycle.py b/loopx/capabilities/issue_fix/pr_lifecycle.py
index febfca6428..4dc025b2da 100644
--- a/loopx/capabilities/issue_fix/pr_lifecycle.py
+++ b/loopx/capabilities/issue_fix/pr_lifecycle.py
@@ -1,21 +1,30 @@
from __future__ import annotations
import hashlib
-import ipaddress
import json
-import re
import subprocess
from collections.abc import Mapping, Sequence
-from datetime import datetime, timezone
-from pathlib import PurePosixPath
from typing import Any
-from urllib.parse import quote, urlsplit
+from urllib.parse import quote
-from ...control_plane.runtime.public_safety import public_safe_compact_text
from ...domain_packs.issue_fix import issue_fix_maintainer_correction_entries
+from .correction_settlement import (
+ MAINTAINER_CORRECTION_ADMISSION_BASES,
+ MAINTAINER_CORRECTION_DISPOSITIONS,
+ MAINTAINER_CORRECTION_OPERATION_ID_PREFIX,
+ MAINTAINER_CORRECTION_REJECTION_REASONS,
+ TERMINAL_PR_STATES as TERMINAL_PR_STATES,
+ _timestamp,
+ _upper_label,
+ build_issue_fix_transition_preview as _transition_preview,
+ evaluate_issue_fix_maintainer_correction_admission,
+ maintainer_correction_transition as _maintainer_correction_transition,
+ normalise_issue_fix_maintainer_correction_input as normalise_issue_fix_maintainer_correction_input,
+ normalise_issue_fix_pr_source_events,
+)
from .github_public import (
- MAINTAINER_ASSOCIATIONS,
+ MAINTAINER_ASSOCIATIONS as MAINTAINER_ASSOCIATIONS,
fetch_github_pull_request_event_metadata,
)
from .metadata_preview import (
@@ -28,76 +37,6 @@
ISSUE_FIX_PR_GROUPED_MONITOR_PROJECTION_SCHEMA_VERSION = (
"issue_fix_pr_grouped_monitor_projection_v1"
)
-ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION = (
- "issue_fix_maintainer_correction_input_v0"
-)
-ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION_V1 = (
- "issue_fix_maintainer_correction_input_v1"
-)
-MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSIONS = {
- ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION,
- ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION_V1,
-}
-ISSUE_FIX_MAINTAINER_CORRECTION_ADMISSION_SCHEMA_VERSION = (
- "issue_fix_maintainer_correction_admission_v0"
-)
-ISSUE_FIX_MAINTAINER_CORRECTION_LEDGER_ENTRY_SCHEMA_VERSION = (
- "issue_fix_maintainer_correction_ledger_entry_v0"
-)
-MAINTAINER_CORRECTION_OPERATION_ID_PREFIX = "issue-fix-correction:"
-MAINTAINER_CORRECTION_KINDS = {
- "actionable_patch",
- "semantic_ambiguity",
- "missing_authority",
- "unchanged",
-}
-MAINTAINER_CORRECTION_SOURCE_KINDS = {"review", "maintainer_comment"}
-MAINTAINER_CORRECTION_SOURCE_EVENT_KINDS = {
- "issue_comment",
- "review",
- "review_comment",
-}
-MAINTAINER_CORRECTION_SOURCE_EVENT_FIELDS = {
- "kind",
- "id",
- "html_url",
- "author_login",
- "author_association",
- "created_at",
- "updated_at",
-}
-MAINTAINER_CORRECTION_ADMISSION_BASES = {"host_asserted", "provider_verified"}
-MAINTAINER_CORRECTION_DISPOSITIONS = {"accepted", "context_only", "superseded"}
-MAINTAINER_CORRECTION_REJECTION_REASONS = {
- "source_event_not_observed",
- "source_event_revision_mismatch",
- "stale_revision",
- "self_or_bot_author",
- "author_not_maintainer",
- "terminal_pr_state",
- "goal_not_active",
- "origin_request_cancelled",
- "origin_request_unreadable",
-}
-# GitHub Apps post as `[bot]`; without a configured LoopX actor login this
-# suffix is the only self/bot signal available from body-free event metadata.
-BOT_LOGIN_SUFFIX = "[bot]"
-_GITHUB_LOGIN_PATTERN = re.compile(r"[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})(?:\[bot\])?")
-_PROVIDER_EVENT_ID_PATTERN = re.compile(r"[0-9]{1,20}")
-WRITE_SCOPES = {"write", "publish", "external_review_request"}
-MAINTAINER_CORRECTION_INPUT_FIELDS = {
- "schema_version",
- "correction_kind",
- "source_kind",
- "source_ref",
- "summary",
- "verification_plan",
- "pr_update_path",
- "user_question",
- "missing_authority_scopes",
- "source_event",
-}
-TERMINAL_PR_STATES = {"MERGED", "CLOSED"}
FAILING_CHECK_STATES = {
"ACTION_REQUIRED",
"CANCELLED",
@@ -140,18 +79,6 @@
""".strip()
-def _upper_label(value: Any, default: str = "UNKNOWN") -> str:
- text = str(value or "").strip().upper()
- return text or default
-
-
-def _compact_label(value: Any, default: str) -> str:
- text = str(value or "").strip()
- if not text:
- return default
- return "_".join(text.replace("/", "_").replace("-", "_").split()).lower()
-
-
def _safe_bool(value: Any) -> bool:
return bool(value) if isinstance(value, bool) else False
@@ -166,17 +93,6 @@ def _safe_count(value: Any) -> int | None:
return number if number >= 0 else None
-def _timestamp(value: Any) -> datetime | None:
- text = str(value or "").strip()
- if not text:
- return None
- try:
- parsed = datetime.fromisoformat(text.replace("Z", "+00:00"))
- except ValueError:
- return None
- return parsed if parsed.tzinfo is not None else None
-
-
def _review_response_observation(
provider_payload: Mapping[str, Any], *, review_decision: str
) -> dict[str, Any]:
@@ -224,680 +140,6 @@ def _review_response_observation(
}
-def _compact_public_text(value: Any, *, field: str, limit: int) -> str:
- raw = " ".join(str(value or "").strip().split())
- if len(raw) > limit:
- raise ValueError(f"maintainer correction {field} exceeds the compact limit")
- if "" in raw or "\x00" in raw:
- raise ValueError(
- f"maintainer correction {field} must not contain control-plane markup"
- )
- text = public_safe_compact_text(value, limit=limit)
- if not text:
- raise ValueError(
- f"maintainer correction {field} must be compact and public-safe"
- )
- return " ".join(text.split())
-
-
-def _public_source_reference(value: Any) -> str:
- reference = _compact_public_text(value, field="source_ref", limit=300)
- if "://" in reference:
- parsed = urlsplit(reference)
- if (
- parsed.scheme != "https"
- or not parsed.netloc
- or parsed.username
- or parsed.password
- or parsed.query
- ):
- raise ValueError(
- "maintainer correction source_ref URL must be public https without user info or query"
- )
- hostname = (parsed.hostname or "").lower()
- if hostname == "localhost" or hostname.endswith((".localhost", ".local")):
- raise ValueError(
- "maintainer correction source_ref must not target a local host"
- )
- try:
- address = ipaddress.ip_address(hostname)
- except ValueError:
- address = None
- if address and (
- address.is_private
- or address.is_loopback
- or address.is_link_local
- or address.is_reserved
- ):
- raise ValueError(
- "maintainer correction source_ref must target a public host"
- )
- return reference
- if reference.startswith(("/", "~")) or re.match(r"^[A-Za-z]:[\\/]", reference):
- raise ValueError("maintainer correction source_ref must not be a local path")
- path = PurePosixPath(reference.replace("\\", "/"))
- if path.as_posix() == "." or ".." in path.parts:
- raise ValueError("maintainer correction source_ref must be repo-relative")
- return path.as_posix()
-
-
-def _normalise_provider_event_id(value: Any) -> str:
- if isinstance(value, bool):
- raise ValueError("maintainer correction source_event id must be a provider id")
- text = str(value if value is not None else "").strip()
- if not _PROVIDER_EVENT_ID_PATTERN.fullmatch(text):
- raise ValueError("maintainer correction source_event id must be a numeric provider id")
- return text
-
-
-def _normalise_author_login(value: Any, *, field: str) -> str:
- text = str(value or "").strip()
- if not _GITHUB_LOGIN_PATTERN.fullmatch(text):
- raise ValueError(f"maintainer correction {field} must be a public provider login")
- return text
-
-
-def _normalise_event_timestamp(value: Any, *, field: str) -> str:
- text = str(value or "").strip()
- parsed = _timestamp(text)
- if parsed is None:
- raise ValueError(
- f"maintainer correction {field} must be a timezone-aware ISO timestamp"
- )
- return parsed.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
-
-
-def normalise_issue_fix_maintainer_correction_source_event(
- value: Any,
-) -> dict[str, Any]:
- """Normalise the provider event that carried a maintainer correction.
-
- Only identity and revision metadata survive; no body field is accepted.
- """
-
- if not isinstance(value, Mapping):
- raise ValueError("maintainer correction source_event must be an object")
- unknown_fields = set(value) - MAINTAINER_CORRECTION_SOURCE_EVENT_FIELDS
- if unknown_fields:
- raise ValueError(
- "maintainer correction source_event contains unsupported fields: "
- + ", ".join(sorted(str(field) for field in unknown_fields))
- )
- kind = str(value.get("kind") or "").strip()
- if kind not in MAINTAINER_CORRECTION_SOURCE_EVENT_KINDS:
- raise ValueError(
- "maintainer correction source_event kind must be issue_comment, review, or review_comment"
- )
- html_url = _public_source_reference(value.get("html_url"))
- if "://" not in html_url:
- raise ValueError("maintainer correction source_event html_url must be a public https URL")
- return {
- "kind": kind,
- "id": _normalise_provider_event_id(value.get("id")),
- "html_url": html_url,
- "author_login": _normalise_author_login(
- value.get("author_login"), field="source_event author_login"
- ),
- "author_association": _upper_label(value.get("author_association"), "NONE"),
- "created_at": _normalise_event_timestamp(
- value.get("created_at"), field="source_event created_at"
- ),
- "updated_at": _normalise_event_timestamp(
- value.get("updated_at"), field="source_event updated_at"
- ),
- "body_captured": False,
- }
-
-
-def normalise_issue_fix_maintainer_correction_input(
- value: Mapping[str, Any],
-) -> dict[str, Any]:
- unknown_fields = set(value) - MAINTAINER_CORRECTION_INPUT_FIELDS
- if unknown_fields:
- raise ValueError(
- "maintainer correction input contains unsupported fields: "
- + ", ".join(sorted(str(field) for field in unknown_fields))
- )
- schema_version = value.get("schema_version")
- if schema_version not in MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSIONS:
- raise ValueError(
- "maintainer correction schema_version must be "
- "issue_fix_maintainer_correction_input_v0 or issue_fix_maintainer_correction_input_v1"
- )
- provider_verified = (
- schema_version == ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION_V1
- )
- if provider_verified:
- if "source_event" not in value:
- raise ValueError(
- "issue_fix_maintainer_correction_input_v1 requires source_event"
- )
- source_event: dict[str, Any] | None = (
- normalise_issue_fix_maintainer_correction_source_event(
- value.get("source_event")
- )
- )
- else:
- if "source_event" in value:
- raise ValueError(
- "source_event requires schema_version issue_fix_maintainer_correction_input_v1"
- )
- source_event = None
- correction_kind = str(value.get("correction_kind") or "").strip()
- if correction_kind not in MAINTAINER_CORRECTION_KINDS:
- raise ValueError(
- "maintainer correction correction_kind must be actionable_patch, semantic_ambiguity, missing_authority, or unchanged"
- )
- source_kind = str(value.get("source_kind") or "").strip()
- if source_kind not in MAINTAINER_CORRECTION_SOURCE_KINDS:
- raise ValueError(
- "maintainer correction source_kind must be review or maintainer_comment"
- )
- source_ref = _public_source_reference(value.get("source_ref"))
- summary = _compact_public_text(value.get("summary"), field="summary", limit=400)
- verification_plan = ""
- pr_update_path = ""
- user_question = ""
- missing_authority_scopes: list[str] = []
- if correction_kind == "actionable_patch":
- verification_plan = _compact_public_text(
- value.get("verification_plan"), field="verification_plan", limit=300
- )
- pr_update_path = _compact_public_text(
- value.get("pr_update_path"), field="pr_update_path", limit=240
- )
- elif correction_kind == "semantic_ambiguity":
- user_question = _compact_public_text(
- value.get("user_question"), field="user_question", limit=300
- )
- elif correction_kind == "missing_authority":
- raw_scopes = value.get("missing_authority_scopes")
- if not isinstance(raw_scopes, Sequence) or isinstance(raw_scopes, (str, bytes)):
- raise ValueError("missing_authority requires missing_authority_scopes")
- for raw_scope in raw_scopes:
- scope = str(raw_scope or "").strip()
- if scope not in WRITE_SCOPES:
- raise ValueError("maintainer correction authority scope is unsupported")
- if scope not in missing_authority_scopes:
- missing_authority_scopes.append(scope)
- if not missing_authority_scopes:
- raise ValueError("missing_authority requires at least one authority scope")
- # The fingerprint covers the compacted correction content under the v0
- # field set, so existing v0 ledger rows keep their recorded fingerprints and
- # a v1 event carrying identical content hashes the same; event identity
- # lives in the operation id, not here.
- content = {
- "schema_version": ISSUE_FIX_MAINTAINER_CORRECTION_INPUT_SCHEMA_VERSION,
- "correction_kind": correction_kind,
- "source_kind": source_kind,
- "source_ref": source_ref,
- "summary": summary,
- "verification_plan": verification_plan or None,
- "pr_update_path": pr_update_path or None,
- "user_question": user_question or None,
- "missing_authority_scopes": missing_authority_scopes,
- "raw_body_captured": False,
- "raw_comment_captured": False,
- "raw_provider_payload_captured": False,
- "local_paths_captured": False,
- }
- fingerprint = hashlib.sha256(
- json.dumps(content, sort_keys=True, separators=(",", ":")).encode("utf-8")
- ).hexdigest()[:16]
- normalized = {
- **content,
- "schema_version": str(schema_version),
- "source_event": source_event,
- "admission_basis": "provider_verified" if provider_verified else "host_asserted",
- }
- normalized["correction_fingerprint"] = fingerprint
- return normalized
-
-
-def issue_fix_maintainer_correction_operation_id(
- correction: Mapping[str, Any], *, repo: str, pr_ref: str
-) -> str:
- """Return the stable Todo operation id for one correction revision."""
-
- source_event = correction.get("source_event")
- if isinstance(source_event, Mapping):
- identity = [
- repo,
- pr_ref,
- str(source_event.get("kind")),
- str(source_event.get("id")),
- str(source_event.get("updated_at")),
- ]
- else:
- identity = [repo, pr_ref, str(correction.get("correction_fingerprint"))]
- digest = hashlib.sha256(
- json.dumps(identity, separators=(",", ":")).encode("utf-8")
- ).hexdigest()[:24]
- return f"{MAINTAINER_CORRECTION_OPERATION_ID_PREFIX}{digest}"
-
-
-def _provider_event_entries(value: Any) -> list[Mapping[str, Any]]:
- if isinstance(value, Sequence) and not isinstance(value, (str, bytes)):
- return [item for item in value if isinstance(item, Mapping)]
- return []
-
-
-def normalise_issue_fix_pr_source_events(
- provider_payload: Mapping[str, Any] | None,
-) -> dict[str, Any]:
- """Project body-free PR comment/review metadata into verifiable events.
-
- Reviews expose ``submitted_at`` instead of ``updated_at``; that timestamp
- stands in as the review revision because GitHub does not re-time edits of a
- review summary through the list endpoint.
- """
-
- payload = provider_payload if isinstance(provider_payload, Mapping) else {}
- groups = (
- ("issue_comment", payload.get("comments") or payload.get("issueComments")),
- ("review", payload.get("reviews")),
- ("review_comment", payload.get("reviewComments") or payload.get("review_comments")),
- )
- available = any(
- isinstance(raw, Sequence) and not isinstance(raw, (str, bytes))
- for _kind, raw in groups
- )
- events: list[dict[str, Any]] = []
- for kind, raw in groups:
- for item in _provider_event_entries(raw):
- raw_id = item.get("id")
- if isinstance(raw_id, bool) or raw_id is None:
- continue
- event_id = str(raw_id).strip()
- if not _PROVIDER_EVENT_ID_PATTERN.fullmatch(event_id):
- continue
- author = item.get("author") or item.get("user")
- if isinstance(author, Mapping):
- author = author.get("login")
- updated_at = (
- item.get("updated_at")
- or item.get("updatedAt")
- or item.get("submitted_at")
- or item.get("submittedAt")
- )
- created_at = item.get("created_at") or item.get("createdAt")
- events.append(
- {
- "kind": kind,
- "id": event_id,
- "author_login": str(author or "").strip()[:60] or None,
- "author_association": _upper_label(
- item.get("author_association")
- or item.get("authorAssociation"),
- "NONE",
- ),
- "created_at": (
- _normalise_event_timestamp(created_at, field="created_at")
- if _timestamp(created_at) else None
- ),
- "updated_at": (
- _normalise_event_timestamp(updated_at, field="updated_at")
- if _timestamp(updated_at) else None
- ),
- "body_captured": False,
- }
- )
- return {
- "status": "available" if available else "unavailable",
- "events": events,
- "body_captured": False,
- }
-
-
-def _same_instant(left: Any, right: Any) -> bool:
- left_time = _timestamp(left)
- right_time = _timestamp(right)
- return left_time is not None and right_time is not None and left_time == right_time
-
-
-def _is_bot_or_self_login(login: Any) -> bool:
- text = str(login or "").strip().lower()
- return text.endswith(BOT_LOGIN_SUFFIX)
-
-
-def _latest_accepted_revision(
- prior_entries: Sequence[Mapping[str, Any]], *, source_event: Mapping[str, Any]
-) -> dict[str, Any] | None:
- latest: dict[str, Any] | None = None
- for entry in prior_entries:
- event = entry.get("source_event")
- if not isinstance(event, Mapping):
- continue
- if event.get("kind") != source_event.get("kind") or str(
- event.get("id")
- ) != str(source_event.get("id")):
- continue
- if entry.get("disposition") not in {"accepted", "superseded"}:
- continue
- candidate_time = _timestamp(event.get("updated_at"))
- if candidate_time is None:
- continue
- latest_time = (
- _timestamp(latest["source_event"].get("updated_at")) if latest else None
- )
- if latest_time is None or candidate_time > latest_time:
- latest = dict(entry)
- return latest
-
-
-def evaluate_issue_fix_maintainer_correction_admission(
- correction: Mapping[str, Any],
- *,
- observation: Mapping[str, Any],
- source_events: Mapping[str, Any] | None,
- prior_entries: Sequence[Mapping[str, Any]] | None,
- prior_ledger_read: bool,
- recorded_at: str | None,
-) -> dict[str, Any]:
- """Run the deterministic admission gates for one maintainer correction.
-
- Returns the admission record plus the ledger entries this run touches:
- the entry for this correction and any earlier revision it supersedes.
- """
-
- repo = str(observation.get("repo") or "")
- pr_ref = str(observation.get("pr_ref") or "")
- operation_id = issue_fix_maintainer_correction_operation_id(
- correction, repo=repo, pr_ref=pr_ref
- )
- source_event = correction.get("source_event")
- admission_basis = str(correction.get("admission_basis") or "host_asserted")
- prior = issue_fix_maintainer_correction_entries(
- {"maintainer_corrections": prior_entries}
- )
- existing_entry = next(
- (entry for entry in prior if entry.get("operation_id") == operation_id), None
- )
- gates: list[dict[str, Any]] = []
- reason: str | None = None
- verified_event: dict[str, Any] | None = None
- provider_events_status = (
- str(source_events.get("status") or "unavailable")
- if isinstance(source_events, Mapping)
- else "unavailable"
- )
-
- def record(name: str, passed: bool, failure_reason: str | None = None) -> None:
- nonlocal reason
- gates.append({"gate": name, "passed": passed})
- if not passed and reason is None:
- reason = failure_reason
-
- if isinstance(source_event, Mapping):
- observed = [
- event
- for event in (
- source_events.get("events") if isinstance(source_events, Mapping) else []
- )
- if isinstance(event, Mapping)
- and event.get("kind") == source_event.get("kind")
- and str(event.get("id")) == str(source_event.get("id"))
- ]
- if not observed:
- record("source_event_observed", False, "source_event_not_observed")
- elif not any(
- _same_instant(event.get("updated_at"), source_event.get("updated_at"))
- for event in observed
- ):
- record("source_event_observed", False, "source_event_revision_mismatch")
- else:
- record("source_event_observed", True)
- provider_event = observed[0]
- verified_event = {
- **dict(source_event),
- "author_login": str(
- provider_event.get("author_login") or source_event.get("author_login")
- ),
- "author_association": _upper_label(
- provider_event.get("author_association"),
- str(source_event.get("author_association") or "NONE"),
- ),
- "created_at": _normalise_event_timestamp(
- provider_event.get("created_at") or source_event.get("created_at"),
- field="source_event created_at",
- ),
- }
- effective_event = verified_event or dict(source_event)
- latest_prior = _latest_accepted_revision(prior, source_event=source_event)
- prior_time = (
- _timestamp(latest_prior["source_event"].get("updated_at"))
- if latest_prior
- else None
- )
- incoming_time = _timestamp(source_event.get("updated_at"))
- revision_relation = "first"
- if prior_time is not None and incoming_time is not None:
- if incoming_time < prior_time:
- revision_relation = "stale"
- elif incoming_time == prior_time:
- revision_relation = "replay"
- else:
- revision_relation = "newer"
- record("revision_not_stale", revision_relation != "stale", "stale_revision")
- record(
- "not_self_or_bot_author",
- not _is_bot_or_self_login(effective_event.get("author_login")),
- "self_or_bot_author",
- )
- record(
- "author_is_maintainer",
- _upper_label(effective_event.get("author_association"), "NONE")
- in MAINTAINER_ASSOCIATIONS,
- "author_not_maintainer",
- )
- else:
- effective_event = None
- latest_prior = None
- revision_relation = "replay" if existing_entry else "first"
- record(
- "pr_state_not_terminal",
- str(observation.get("state") or "") not in TERMINAL_PR_STATES,
- "terminal_pr_state",
- )
-
- accepted = reason is None
- replay = existing_entry is not None and (
- not accepted or existing_entry.get("disposition") in {"accepted", "superseded"}
- )
- if replay:
- entry = dict(existing_entry)
- else:
- entry = {
- "schema_version": ISSUE_FIX_MAINTAINER_CORRECTION_LEDGER_ENTRY_SCHEMA_VERSION,
- "operation_id": operation_id,
- "source_event": effective_event,
- "correction_fingerprint": correction.get("correction_fingerprint"),
- "correction_kind": correction.get("correction_kind"),
- "admission_basis": admission_basis,
- "disposition": "accepted" if accepted else "context_only",
- "reason": reason,
- "todo_id": None,
- "accepted_head_sha": observation.get("head_commit_ref") if accepted else None,
- "recorded_at": recorded_at,
- "superseded_by": None,
- "body_captured": False,
- }
- touched_entries = [entry]
- superseded_operation_id = None
- if (
- accepted
- and revision_relation == "newer"
- and latest_prior is not None
- and latest_prior.get("operation_id") != operation_id
- ):
- superseded_operation_id = str(latest_prior.get("operation_id"))
- touched_entries.append(
- {
- **latest_prior,
- "disposition": "superseded",
- "superseded_by": operation_id,
- }
- )
- return {
- "schema_version": ISSUE_FIX_MAINTAINER_CORRECTION_ADMISSION_SCHEMA_VERSION,
- "operation_id": operation_id,
- "admission_basis": admission_basis,
- "disposition": "accepted" if accepted else "context_only",
- "reason": reason,
- "host_summary_mismatch": bool(
- existing_entry is not None
- and correction.get("correction_fingerprint")
- != existing_entry.get("correction_fingerprint")
- ),
- "gates": gates,
- "revision_relation": revision_relation,
- "replay": replay,
- "supersedes_operation_id": superseded_operation_id,
- "provider_events_status": provider_events_status,
- "prior_ledger_read": bool(prior_ledger_read),
- "source_event": effective_event,
- "entries": touched_entries,
- "body_captured": False,
- }
-
-
-def recheck_issue_fix_maintainer_correction_effect_gates(
- entry: Mapping[str, Any],
- *,
- observation: Mapping[str, Any],
- ledger_entries: Sequence[Mapping[str, Any]] | None,
- goal_stopped: bool,
- source_events: Mapping[str, Any] | None = None,
-) -> str | None:
- """Re-run the effect-time gates for an accepted entry just before its Todo write."""
-
- source_event = entry.get("source_event")
- if isinstance(source_event, Mapping):
- observed = [
- event
- for event in (
- source_events.get("events") if isinstance(source_events, Mapping) else []
- )
- if isinstance(event, Mapping)
- and event.get("kind") == source_event.get("kind")
- and str(event.get("id")) == str(source_event.get("id"))
- ]
- if not observed:
- return "source_event_not_observed"
- current_event = next(
- (event for event in observed
- if _same_instant(event.get("updated_at"), source_event.get("updated_at"))),
- None,
- )
- if current_event is None:
- return "source_event_revision_mismatch"
- latest = _latest_accepted_revision(
- issue_fix_maintainer_correction_entries(
- {"maintainer_corrections": ledger_entries}
- ),
- source_event=source_event,
- )
- if latest is not None and latest.get("operation_id") != entry.get("operation_id"):
- latest_time = _timestamp(latest["source_event"].get("updated_at"))
- own_time = _timestamp(source_event.get("updated_at"))
- if latest_time is not None and own_time is not None and own_time < latest_time:
- return "stale_revision"
- if _is_bot_or_self_login(current_event.get("author_login")):
- return "self_or_bot_author"
- if (
- _upper_label(current_event.get("author_association"), "NONE")
- not in MAINTAINER_ASSOCIATIONS
- ):
- return "author_not_maintainer"
- if str(observation.get("state") or "") in TERMINAL_PR_STATES:
- return "terminal_pr_state"
- if goal_stopped:
- return "goal_not_active"
- return None
-
-def _maintainer_correction_transition(
- correction: Mapping[str, Any],
- *,
- pr_permalink: str,
-) -> dict[str, Any]:
- correction_kind = str(correction.get("correction_kind"))
- source_ref = str(correction.get("source_ref"))
- summary = str(correction.get("summary"))
- source_event = correction.get("source_event")
- # Todo creation dedupes identical active text, so distinct provider events
- # or revisions with the same wording must name the event they came from.
- event_suffix = (
- f" Source event: {source_event.get('kind')} {source_event.get('id')}"
- f" updated {source_event.get('updated_at')}."
- if isinstance(source_event, Mapping)
- else ""
- )
- if correction_kind == "actionable_patch":
- verification_plan = str(correction.get("verification_plan"))
- pr_update_path = str(correction.get("pr_update_path"))
- return _transition_preview(
- decision="runnable_successor",
- action_kind="issue_fix_maintainer_correction_patch",
- priority="P0",
- task_class="advancement_task",
- reason="Maintainer correction is bounded and actionable; create one claimed patch successor.",
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- text=(
- f"[P0] Apply maintainer correction for {pr_permalink} from {source_ref}: "
- f"{summary} Verify: {verification_plan} Update PR via: {pr_update_path}"
- f"{event_suffix}"
- ),
- ) | {
- "terminal_state_precedence": False,
- "material_change": True,
- "required_write_scopes": ["write", "publish"],
- }
- if correction_kind == "semantic_ambiguity":
- return _transition_preview(
- decision="user_gate",
- action_kind="clarify_issue_fix_maintainer_correction",
- priority="P0",
- role="user",
- task_class="user_gate",
- reason="Maintainer correction changes or leaves the intended behavior ambiguous.",
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- text=(
- f"[P0] Clarify maintainer correction for {pr_permalink} from {source_ref}: "
- f"{correction.get('user_question')} Context: {summary}{event_suffix}"
- ),
- ) | {"terminal_state_precedence": False, "material_change": True}
- if correction_kind == "missing_authority":
- scopes = ", ".join(
- str(value) for value in correction.get("missing_authority_scopes") or []
- )
- return _transition_preview(
- decision="user_gate",
- action_kind="grant_issue_fix_maintainer_correction_authority",
- priority="P0",
- role="user",
- task_class="user_gate",
- reason="The correction is understood but the required write authority is not active.",
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- text=(
- f"[P0] Grant or decline [{scopes}] authority for maintainer correction "
- f"{source_ref} on {pr_permalink}: {summary}{event_suffix}"
- ),
- ) | {
- "terminal_state_precedence": False,
- "material_change": True,
- "missing_authority_scopes": list(
- correction.get("missing_authority_scopes") or []
- ),
- }
- return _transition_preview(
- decision="monitor_continuation",
- action_kind="issue_fix_maintainer_correction_unchanged_monitor",
- priority="P2",
- reason="Maintainer correction is unchanged; keep the monitor quiet and create no successor.",
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- ) | {"terminal_state_precedence": False, "material_change": False}
-
-
def _normalise_check_item(item: Mapping[str, Any]) -> str:
state = (
item.get("conclusion")
@@ -966,35 +208,6 @@ def _check_rollup(payload: Mapping[str, Any]) -> dict[str, Any]:
}
-def _transition_preview(
- *,
- decision: str,
- action_kind: str,
- priority: str,
- reason: str,
- depends_on: Sequence[str],
- role: str = "agent",
- task_class: str = "continuous_monitor",
- text: str | None = None,
-) -> dict[str, Any]:
- if text is None:
- text = f"[{priority}] {reason}"
- return {
- "schema_version": "issue_fix_pr_lifecycle_transition_v0",
- "decision": decision,
- "command_preview": "loopx todo transition",
- "role": role,
- "priority": priority,
- "task_class": task_class,
- "action_kind": action_kind,
- "reason": reason,
- "text": text,
- "depends_on": list(depends_on),
- "would_write": False,
- "requires_execute_flag": True,
- }
-
-
def _build_observation(
*,
repo: str,
@@ -1191,11 +404,9 @@ def _decide_transition(observation: Mapping[str, Any]) -> dict[str, Any]:
"PR is merged; close the monitor with no follow-up even if stale "
"review metadata still says review is required."
),
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- ) | {
- "terminal_state_precedence": True,
- "material_change": True,
- }
+ terminal_state_precedence=True,
+ material_change=True,
+ )
if state == "CLOSED":
return _transition_preview(
decision="no_followup",
@@ -1203,11 +414,9 @@ def _decide_transition(observation: Mapping[str, Any]) -> dict[str, Any]:
priority="P1",
task_class="terminal_transition",
reason="PR is closed without an open continuation; close the monitor.",
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- ) | {
- "terminal_state_precedence": True,
- "material_change": True,
- }
+ terminal_state_precedence=True,
+ material_change=True,
+ )
if failing_checks:
return _transition_preview(
decision="runnable_successor",
@@ -1215,11 +424,8 @@ def _decide_transition(observation: Mapping[str, Any]) -> dict[str, Any]:
priority="P0",
task_class="advancement_task",
reason="PR checks are failing; inspect public check summary and plan a fix.",
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- ) | {
- "terminal_state_precedence": False,
- "material_change": True,
- }
+ material_change=True,
+ )
if review_decision == "CHANGES_REQUESTED":
if review_response_status == "changes_addressed_awaiting_rereview":
return _transition_preview(
@@ -1230,22 +436,15 @@ def _decide_transition(observation: Mapping[str, Any]) -> dict[str, Any]:
"Review changes have a newer repair commit and every fetched "
"review thread is resolved; wait for reviewer re-approval."
),
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- ) | {
- "terminal_state_precedence": False,
- "material_change": False,
- }
+ )
return _transition_preview(
decision="runnable_successor",
action_kind="issue_fix_review_changes_replan",
priority="P0",
task_class="advancement_task",
reason="Maintainer review requested changes; derive a follow-up patch or blocker.",
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- ) | {
- "terminal_state_precedence": False,
- "material_change": True,
- }
+ material_change=True,
+ )
if merge_state in BRANCH_REPLAN_MERGE_STATES:
return _transition_preview(
decision="runnable_successor",
@@ -1253,11 +452,8 @@ def _decide_transition(observation: Mapping[str, Any]) -> dict[str, Any]:
priority="P1",
task_class="advancement_task",
reason="PR branch is stale or conflicted; decide rebase, blocker note, or no-follow-up.",
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- ) | {
- "terminal_state_precedence": False,
- "material_change": True,
- }
+ material_change=True,
+ )
if is_draft:
return _transition_preview(
decision="user_gate",
@@ -1266,32 +462,21 @@ def _decide_transition(observation: Mapping[str, Any]) -> dict[str, Any]:
role="user",
task_class="user_gate",
reason="PR is still draft; owner must approve marking ready or keep monitoring.",
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- ) | {
- "terminal_state_precedence": False,
- "material_change": True,
- }
+ material_change=True,
+ )
if pending_checks:
return _transition_preview(
decision="monitor_continuation",
action_kind="issue_fix_pr_checks_pending_monitor",
priority="P2",
reason="PR checks are still pending; continue monitor without spawning work.",
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- ) | {
- "terminal_state_precedence": False,
- "material_change": False,
- }
+ )
return _transition_preview(
decision="monitor_continuation",
action_kind="issue_fix_pr_wait_for_review_or_merge_monitor",
priority="P2",
reason="No actionable PR state change; keep watching for CI, review, merge, or maintainer signal.",
- depends_on=["issue_fix_pr_lifecycle_monitor"],
- ) | {
- "terminal_state_precedence": False,
- "material_change": False,
- }
+ )
def _fetch_github_review_response_metadata(
@@ -1729,69 +914,59 @@ def build_issue_fix_pr_lifecycle_monitor_packet(
return packet
+def _require_false_flags(
+ mapping: Mapping[str, Any], prefix: str, keys: Sequence[str], errors: list[str]
+) -> None:
+ for key in keys:
+ if mapping.get(key) is not False:
+ errors.append(f"{prefix} {key} must be false")
+
+
def validate_issue_fix_pr_lifecycle_monitor_packet(
packet: Mapping[str, Any],
) -> dict[str, Any]:
errors: list[str] = []
if packet.get("schema_version") != ISSUE_FIX_PR_LIFECYCLE_MONITOR_SCHEMA_VERSION:
errors.append("packet schema_version must be issue_fix_pr_lifecycle_monitor_v0")
- for key in (
- "external_writes_performed",
- "issue_body_captured",
- "comment_bodies_captured",
- "maintainer_correction_body_captured",
- "response_payloads_captured",
- "raw_check_logs_captured",
- "local_paths_captured",
- "private_repo_state_read",
- "destructive_git_used",
- ):
- if packet.get(key) is not False:
- errors.append(f"packet {key} must be false")
+ _require_false_flags(
+ packet,
+ "packet",
+ (
+ "external_writes_performed", "issue_body_captured", "comment_bodies_captured",
+ "maintainer_correction_body_captured", "response_payloads_captured",
+ "raw_check_logs_captured", "local_paths_captured", "private_repo_state_read",
+ "destructive_git_used",
+ ),
+ errors,
+ )
observation = packet.get("observation")
if not isinstance(observation, Mapping):
errors.append("observation is required")
observation = {}
if observation.get("kind") != "pull_request":
errors.append("observation kind must be pull_request")
- for key in (
- "body_captured",
- "comment_bodies_captured",
- "timeline_captured",
- "log_output_captured",
- "response_payload_captured",
- ):
- if observation.get(key) is not False:
- errors.append(f"observation {key} must be false")
+ _require_false_flags(
+ observation,
+ "observation",
+ ("body_captured", "comment_bodies_captured", "timeline_captured", "log_output_captured", "response_payload_captured"),
+ errors,
+ )
checks = observation.get("checks")
if isinstance(checks, Mapping):
- if checks.get("raw_status_captured") is not False:
- errors.append("checks raw_status_captured must be false")
- if checks.get("log_output_captured") is not False:
- errors.append("checks log_output_captured must be false")
+ _require_false_flags(checks, "checks", ("raw_status_captured", "log_output_captured"), errors)
else:
errors.append("observation checks are required")
review_response = observation.get("review_response")
- if not isinstance(review_response, Mapping):
- errors.append("observation review_response is required")
+ if isinstance(review_response, Mapping):
+ _require_false_flags(review_response, "review_response", ("raw_reviews_captured", "raw_thread_comments_captured"), errors)
else:
- if review_response.get("raw_reviews_captured") is not False:
- errors.append("review_response raw_reviews_captured must be false")
- if review_response.get("raw_thread_comments_captured") is not False:
- errors.append(
- "review_response raw_thread_comments_captured must be false"
- )
+ errors.append("observation review_response is required")
transition = packet.get("transition")
if not isinstance(transition, Mapping):
errors.append("transition is required")
transition = {}
decision = transition.get("decision")
- if decision not in {
- "runnable_successor",
- "monitor_continuation",
- "user_gate",
- "no_followup",
- }:
+ if decision not in {"runnable_successor", "monitor_continuation", "user_gate", "no_followup"}:
errors.append("transition decision is invalid")
if transition.get("would_write") is not False:
errors.append("transition would_write must be false")
@@ -1800,10 +975,7 @@ def validate_issue_fix_pr_lifecycle_monitor_packet(
state = observation.get("state")
if state in TERMINAL_PR_STATES and decision != "no_followup":
errors.append("terminal PR state must choose no_followup")
- if (
- state in TERMINAL_PR_STATES
- and transition.get("terminal_state_precedence") is not True
- ):
+ if state in TERMINAL_PR_STATES and transition.get("terminal_state_precedence") is not True:
errors.append("terminal PR state must record terminal_state_precedence")
if transition.get("material_change") is True and decision == "monitor_continuation":
errors.append("material PR change must not choose monitor_continuation")
@@ -1812,14 +984,12 @@ def validate_issue_fix_pr_lifecycle_monitor_packet(
if not isinstance(correction, Mapping):
errors.append("maintainer_correction must be an object")
else:
- for key in (
- "raw_body_captured",
- "raw_comment_captured",
- "raw_provider_payload_captured",
- "local_paths_captured",
- ):
- if correction.get(key) is not False:
- errors.append(f"maintainer_correction {key} must be false")
+ _require_false_flags(
+ correction,
+ "maintainer_correction",
+ ("raw_body_captured", "raw_comment_captured", "raw_provider_payload_captured", "local_paths_captured"),
+ errors,
+ )
if packet.get("maintainer_correction_fingerprint") != correction.get(
"correction_fingerprint"
):
@@ -1935,122 +1105,56 @@ def validate_issue_fix_pr_lifecycle_monitor_packet(
}
+def _kv_bullets(mapping: Mapping[str, Any], keys: Sequence[str]) -> list[str]:
+ return [f"- {key}: `{mapping.get(key)}`" for key in keys]
+
+
+def _append_section(lines: list[str], title: str, rows: Sequence[str]) -> None:
+ lines.extend(["", f"## {title}", "", *rows])
+
+
def render_issue_fix_pr_lifecycle_monitor_markdown(payload: dict[str, Any]) -> str:
lines = [
"# LoopX Issue Fix PR Lifecycle",
"",
- f"- ok: `{payload.get('ok')}`",
- f"- schema_version: `{payload.get('schema_version')}`",
- f"- external_reads_performed: `{payload.get('external_reads_performed')}`",
- f"- external_writes_performed: `{payload.get('external_writes_performed')}`",
- f"- todo_write_performed: `{payload.get('todo_write_performed')}`",
- f"- observation_fingerprint: `{payload.get('observation_fingerprint')}`",
+ *_kv_bullets(
+ payload,
+ (
+ "ok", "schema_version", "external_reads_performed",
+ "external_writes_performed", "todo_write_performed", "observation_fingerprint",
+ ),
+ ),
]
observation = payload.get("observation")
if isinstance(observation, Mapping):
- checks = (
- observation.get("checks")
- if isinstance(observation.get("checks"), Mapping)
- else {}
- )
- lines.extend(
- [
- "",
- "## Observation",
- "",
- f"- repo: `{observation.get('repo')}`",
- f"- pr_ref: `{observation.get('pr_ref')}`",
- f"- state: `{observation.get('state')}`",
- f"- review_decision: `{observation.get('review_decision')}`",
- f"- merge_state_status: `{observation.get('merge_state_status')}`",
- f"- checks: `{checks.get('aggregate')}`",
- ]
+ raw_checks = observation.get("checks")
+ checks: Mapping[str, Any] = raw_checks if isinstance(raw_checks, Mapping) else {}
+ _append_section(
+ lines,
+ "Observation",
+ [*_kv_bullets(observation, ("repo", "pr_ref", "state", "review_decision", "merge_state_status")), f"- checks: `{checks.get('aggregate')}`"],
)
transition = payload.get("transition")
if isinstance(transition, Mapping):
- lines.extend(
- [
- "",
- "## Transition",
- "",
- f"- decision: `{transition.get('decision')}`",
- f"- action_kind: `{transition.get('action_kind')}`",
- f"- material_change: `{transition.get('material_change')}`",
- f"- terminal_state_precedence: `{transition.get('terminal_state_precedence')}`",
- f"- reason: {transition.get('reason')}",
- ]
- )
- grouped_monitor = payload.get("grouped_monitor_projection")
- if isinstance(grouped_monitor, Mapping):
- lines.extend(
- [
- "",
- "## Grouped Monitor Projection",
- "",
- f"- state_bucket: `{grouped_monitor.get('state_bucket')}`",
- f"- target_key: `{grouped_monitor.get('target_key')}`",
- f"- member_operation: `{grouped_monitor.get('member_operation')}`",
- f"- creates_per_pr_continuous_monitor_todo: "
- f"`{grouped_monitor.get('creates_per_pr_continuous_monitor_todo')}`",
- f"- per_pr_material_action: "
- f"`{grouped_monitor.get('per_pr_material_action')}`",
- ]
- )
- correction = payload.get("maintainer_correction")
- if isinstance(correction, Mapping):
- lines.extend(
- [
- "",
- "## Maintainer Correction",
- "",
- f"- correction_kind: `{correction.get('correction_kind')}`",
- f"- source_kind: `{correction.get('source_kind')}`",
- f"- source_ref: `{correction.get('source_ref')}`",
- f"- correction_fingerprint: `{correction.get('correction_fingerprint')}`",
- ]
- )
- admission = payload.get("maintainer_correction_admission")
- if isinstance(admission, Mapping):
- lines.extend(
- [
- "",
- "## Maintainer Correction Admission",
- "",
- f"- admission_basis: `{admission.get('admission_basis')}`",
- f"- disposition: `{admission.get('disposition')}`",
- f"- reason: `{admission.get('reason')}`",
- f"- operation_id: `{admission.get('operation_id')}`",
- ]
- )
- domain_state = payload.get("domain_state_projection")
- if isinstance(domain_state, Mapping):
- lines.extend(
- [
- "",
- "## Domain State Projection",
- "",
- f"- domain_pack: `{domain_state.get('domain_pack')}`",
- f"- default_filename: `{domain_state.get('default_filename')}`",
- f"- write_performed: `{domain_state.get('write_performed')}`",
- f"- path_recorded: `{domain_state.get('path_recorded')}`",
- ]
+ _append_section(
+ lines,
+ "Transition",
+ [*_kv_bullets(transition, ("decision", "action_kind", "material_change", "terminal_state_precedence")), f"- reason: {transition.get('reason')}"],
)
+ for key, title, fields in (
+ ("grouped_monitor_projection", "Grouped Monitor Projection", ("state_bucket", "target_key", "member_operation", "creates_per_pr_continuous_monitor_todo", "per_pr_material_action")),
+ ("maintainer_correction", "Maintainer Correction", ("correction_kind", "source_kind", "source_ref", "correction_fingerprint")),
+ ("maintainer_correction_admission", "Maintainer Correction Admission", ("admission_basis", "disposition", "reason", "operation_id")),
+ ("domain_state_projection", "Domain State Projection", ("domain_pack", "default_filename", "write_performed", "path_recorded")),
+ ):
+ section = payload.get(key)
+ if isinstance(section, Mapping):
+ _append_section(lines, title, _kv_bullets(section, fields))
validation = payload.get("validation")
if isinstance(validation, Mapping):
- errors = (
- validation.get("errors")
- if isinstance(validation.get("errors"), list)
- else []
- )
- lines.extend(
- [
- "",
- "## Validation",
- "",
- f"- validation_ok: `{validation.get('ok')}`",
- f"- error_count: `{len(errors)}`",
- ]
- )
+ raw_errors = validation.get("errors")
+ errors = raw_errors if isinstance(raw_errors, list) else []
+ _append_section(lines, "Validation", (f"- validation_ok: `{validation.get('ok')}`", f"- error_count: `{len(errors)}`"))
if payload.get("error"):
- lines.extend(["", "## Error", "", str(payload.get("error"))])
+ _append_section(lines, "Error", (str(payload.get("error")),))
return "\n".join(lines) + "\n"
diff --git a/loopx/capabilities/native_chat/antigravity_adapter.py b/loopx/capabilities/native_chat/antigravity_adapter.py
index e97bae2a91..4311d965ca 100644
--- a/loopx/capabilities/native_chat/antigravity_adapter.py
+++ b/loopx/capabilities/native_chat/antigravity_adapter.py
@@ -18,6 +18,7 @@
import urllib.request
from urllib.parse import urlsplit
import uuid
+from contextlib import AbstractContextManager, nullcontext
from datetime import datetime
from ...chat import (
@@ -39,6 +40,9 @@
"gemini-4-argon-high": "MODEL_PLACEHOLDER_M417",
"gemini-4-argon-medium": "MODEL_PLACEHOLDER_M418",
"gemini-4-argon-low": "MODEL_PLACEHOLDER_M419",
+ "gemini-4-carbon-high": "MODEL_PLACEHOLDER_M427",
+ "gemini-4-carbon-medium": "MODEL_PLACEHOLDER_M437",
+ "gemini-4-carbon-low": "MODEL_PLACEHOLDER_M438",
}
_SETTLED_STEP_STATUSES = {
@@ -64,12 +68,13 @@ def _step_allows_completed_turn(step: dict[str, Any], *, final: dict[str, Any])
ANTIGRAVITY_MODEL_OPTIONS: tuple[dict[str, Any], ...] = tuple(
{
- "id": f"gemini-4-argon-{effort}",
- "display_name": f"Gemini 4 Argon ({effort.title()})",
- "is_default": effort == "high",
+ "id": f"gemini-4-{family}-{effort}",
+ "display_name": f"Gemini 4 {family.title()} ({effort.title()})",
+ "is_default": family == "argon" and effort == "high",
"default_reasoning_effort": effort,
"supported_reasoning_efforts": [{"reasoning_effort": effort}],
}
+ for family in ("argon", "carbon")
for effort in ("high", "medium", "low")
)
@@ -251,7 +256,7 @@ def _rpc_post(
def discover_antigravity_language_server(
env: Mapping[str, str] | None = None,
- *, deadline: float | None = None,
+ *, deadline: float | None = None, include_model_config: bool = False,
) -> dict[str, Any] | None:
"""Discover a running local endpoint without reusing an availability verdict.
@@ -269,7 +274,7 @@ def discover_antigravity_language_server(
timeout = 2.5 if deadline is None else min(2.5, deadline - time.monotonic())
if timeout <= 0:
return None
- _rpc_post(
+ model_data = _rpc_post(
explicit_url,
explicit_token,
"GetCascadeModelConfigData",
@@ -281,6 +286,7 @@ def discover_antigravity_language_server(
"csrf_token": explicit_token,
"pid": None,
"source": "environment",
+ **({"model_config_data": model_data} if include_model_config else {}),
}
except (CodexChatAgentError, ValueError):
return None
@@ -341,12 +347,13 @@ def discover_antigravity_language_server(
break
url = f"http://127.0.0.1:{port}"
try:
- _rpc_post(url, token, "GetCascadeModelConfigData", {}, timeout=min(1.5, remaining))
+ model_data = _rpc_post(url, token, "GetCascadeModelConfigData", {}, timeout=min(1.5, remaining))
return {
"url": url,
"csrf_token": token,
"pid": pid,
"source": "process_discovery",
+ **({"model_config_data": model_data} if include_model_config else {}),
}
except CodexChatAgentError:
continue
@@ -395,24 +402,111 @@ def resolve_antigravity_model_placeholder(
gate=None, error_code="unsupported_configuration")
if not raw_model or raw_model in {"argon", "gemini-4-argon"}:
canonical = f"gemini-4-argon-{effort or 'high'}"
+ elif raw_model in {"carbon", "gemini-4-carbon"}:
+ canonical = f"gemini-4-carbon-{effort or 'high'}"
else:
canonical = raw_model
if (canonical not in ANTIGRAVITY_MODEL_PLACEHOLDERS
- or effort and canonical != f"gemini-4-argon-{effort}"):
- raise CodexChatAgentError("Choose a supported Argon model and its matching reasoning effort.",
+ or effort and canonical.rsplit("-", 1)[-1] != effort):
+ raise CodexChatAgentError("Choose a supported Antigravity model and its matching reasoning effort.",
gate=None, error_code="unsupported_configuration")
placeholder = ANTIGRAVITY_MODEL_PLACEHOLDERS[canonical]
if dynamic_configs is not None:
match = next((row for row in dynamic_configs
- if row.get("modelId") == canonical
- or (row.get("modelOrAlias") or {}).get("model") == placeholder), None)
+ if isinstance(row.get("modelOrAlias") or {}, dict)
+ and (row.get("modelId") == canonical
+ or (not row.get("modelId")
+ and (row.get("modelOrAlias") or {}).get("model") == placeholder))), None)
if match is None:
- raise CodexChatAgentError("The local Antigravity account did not offer this Argon model.",
+ raise CodexChatAgentError("The local Antigravity account did not offer this model.",
gate=None, error_code="unsupported_configuration")
placeholder = str((match.get("modelOrAlias") or {}).get("model") or placeholder)
return canonical, placeholder
+def _discovered_model_configs(discovered: Mapping[str, Any]) -> list[dict[str, Any]]:
+ """Reuse verified metadata, or read it for an endpoint-only record."""
+ data = discovered.get("model_config_data")
+ if not isinstance(data, dict):
+ data = _rpc_post(discovered["url"], discovered["csrf_token"],
+ "GetCascadeModelConfigData", {}, timeout=3.0)
+ raw = data.get("clientModelConfigs")
+ return [row for row in raw if isinstance(row, dict)] if isinstance(raw, list) else []
+
+
+def offered_antigravity_model_options(
+ *, discovered: Mapping[str, Any] | None = None,
+) -> list[dict[str, Any]]:
+ """Read registered choices from the local account without opening a cascade."""
+ if discovered is None:
+ discovered = discover_antigravity_language_server(include_model_config=True)
+ if discovered is None:
+ raise CodexChatAgentError("Antigravity local language_server is not running or reachable",
+ gate=None, error_code="antigravity_unavailable")
+ configs = _discovered_model_configs(discovered)
+ options = []
+ for item in ANTIGRAVITY_MODEL_OPTIONS:
+ try:
+ resolve_antigravity_model_placeholder(item["id"], dynamic_configs=configs)
+ except CodexChatAgentError:
+ continue
+ options.append(item)
+ if not options:
+ raise CodexChatAgentError("The local Antigravity account did not offer a supported model. Retry model discovery.",
+ gate=None, error_code="unsupported_configuration")
+ return options
+
+
+def _matching_antigravity_model_config(rows: list[Any], model_id: str) -> list[dict[str, Any]]:
+ """Match clientModelConfigs entries by modelId or placeholder when LS omits modelId."""
+ expected_placeholder = ANTIGRAVITY_MODEL_PLACEHOLDERS.get(model_id)
+ return [
+ row for row in rows
+ if isinstance(row, dict) and (
+ row.get("modelId") == model_id
+ or (
+ not row.get("modelId")
+ and expected_placeholder is not None
+ and isinstance(row.get("modelOrAlias"), dict)
+ and row["modelOrAlias"].get("model") == expected_placeholder
+ )
+ )
+ ]
+
+
+def _extract_user_model_quota(
+ user: Any,
+ model_id: str,
+ *,
+ fallback_rows: list[Any] | None = None,
+) -> tuple[str, float, str] | None:
+ """Validate and extract `(account_ref, remaining_fraction, reset_time)` for one canonical model."""
+ if not isinstance(user, Mapping):
+ return None
+ account = user.get("email")
+ if not isinstance(account, str) or not account.strip():
+ return None
+ rows = (user.get("cascadeModelConfigData") or {}).get("clientModelConfigs")
+ if not isinstance(rows, list):
+ if fallback_rows is None:
+ return None
+ rows = fallback_rows
+ matching = _matching_antigravity_model_config(rows, model_id)
+ if len(matching) != 1:
+ return None
+ quota = matching[0]["quotaInfo"]
+ fraction, reset = quota.get("remainingFraction", 0), quota.get("resetTime")
+ if (type(fraction) not in (int, float) or not math.isfinite(fraction)
+ or not 0 <= fraction <= 1 or not isinstance(reset, str)
+ or datetime.fromisoformat(reset).tzinfo is None):
+ return None
+ return (
+ hashlib.sha256(account.strip().casefold().encode()).hexdigest(),
+ fraction,
+ reset,
+ )
+
+
def _native_user_input_text(step: dict[str, Any]) -> str | None:
"""Read the one native text item; any redundant response must agree."""
content = step.get("userInput")
@@ -622,6 +716,7 @@ def metadata_review_capacity(*, cascade_id: str, model_id: str) -> dict[str, Any
native failure alongside the exact model's quota epoch.
"""
try:
+ canonical_model_id, _ = resolve_antigravity_model_placeholder(model_id)
found = discover_antigravity_language_server()
if found is None:
return None
@@ -656,31 +751,62 @@ def read(method: str, payload: dict[str, Any]) -> dict[str, Any]:
if not normal and not exhausted:
return None # An unknown native failure is not capacity evidence.
status = read("GetUserStatus", {})
- user = status["userStatus"]
- account = user.get("email")
- if not isinstance(account, str) or not account.strip():
- return None
- rows = user["cascadeModelConfigData"]["clientModelConfigs"]
- matching = [row for row in rows if row.get("modelId") == model_id]
- if len(matching) != 1:
- return None
- quota = matching[0]["quotaInfo"]
- fraction, reset = quota.get("remainingFraction", 0), quota.get("resetTime")
- if (type(fraction) not in (int, float) or not math.isfinite(fraction)
- or not 0 <= fraction <= 1 or not isinstance(reset, str)
- or datetime.fromisoformat(reset).tzinfo is None):
+ extracted = _extract_user_model_quota(status.get("userStatus"), canonical_model_id)
+ if extracted is None:
return None
+ account_ref, fraction, reset = extracted
after = read("GetCascadeTrajectory", {"cascadeId": cascade_id})
if (after.get("status") != before["status"] or after.get("numTotalSteps") != total
or (after.get("trajectory") or {}).get("cascadeId") != cascade_id):
return None
- return {"model_id": model_id, "account_ref": hashlib.sha256(account.strip().casefold().encode()).hexdigest(),
+ return {"model_id": canonical_model_id, "account_ref": account_ref,
"remaining_fraction": fraction, "reset_time": reset,
"execution_id": (latest[0].get("metadata") or {}).get("executionId"),
"observed_at": time.time(), "quota_exhausted": exhausted, "normal_completion": normal}
except (KeyError, TypeError, ValueError, AttributeError, CodexChatAgentError):
return None
+ @staticmethod
+ def endpoint_model_capacity(
+ *,
+ model_id: str,
+ reasoning_effort: str | None = None,
+ env: Mapping[str, str] | None = None,
+ discovered: Mapping[str, Any] | None = None,
+ ) -> dict[str, Any] | None:
+ """Observe the live LS account's quota for one offered model without opening a cascade."""
+ try:
+ canonical_model_id, _ = resolve_antigravity_model_placeholder(
+ model_id,
+ reasoning_effort,
+ )
+ found = discovered or discover_antigravity_language_server(env=env, include_model_config=True)
+ if found is None:
+ return None
+ status = _rpc_post(str(found["url"]), str(found["csrf_token"]), "GetUserStatus", {}, timeout=5.0)
+ fallback_rows = (found.get("model_config_data") or {}).get("clientModelConfigs") or []
+ extracted = _extract_user_model_quota(
+ status.get("userStatus"),
+ canonical_model_id,
+ fallback_rows=fallback_rows if isinstance(fallback_rows, list) else [],
+ )
+ if extracted is None:
+ return None
+ account_ref, fraction, reset = extracted
+ exhausted = fraction <= 0
+ return {
+ "model_id": canonical_model_id,
+ "account_ref": account_ref,
+ "remaining_fraction": fraction,
+ "reset_time": reset,
+ "execution_id": f"endpoint-probe:{uuid.uuid4().hex}",
+ "observed_at": time.time(),
+ "quota_exhausted": exhausted,
+ "normal_completion": not exhausted,
+ }
+ except (KeyError, TypeError, ValueError, AttributeError, CodexChatAgentError):
+ return None
+
@staticmethod
def completed_result_observation(*, cascade_id: str, request_ref: str, message: str,
started_at: str, completed_at: str,
@@ -829,6 +955,8 @@ def __init__(
self._send_lock = threading.Lock()
self._active_turn_id: str | None = None
self._cancel_acknowledged = False
+ self._launch_guard: Callable[[], AbstractContextManager[None]] | None = None
+ self._steward_report_reader: Callable[[str, Any], dict[str, Any]] | None = None
@property
def current_model_id(self) -> str:
@@ -861,7 +989,7 @@ def start(
# Validate user intent before creating an upstream cascade.
requested = dict(executor_model or {})
resolve_antigravity_model_placeholder(requested.get("model"), requested.get("reasoning_effort"))
- discovered = discover_antigravity_language_server(env=env)
+ discovered = discover_antigravity_language_server(env=env, include_model_config=True)
if discovered is None:
raise CodexChatAgentError(
"Antigravity local language_server is not running or reachable",
@@ -871,9 +999,7 @@ def start(
ls_url = str(discovered["url"])
csrf_token = str(discovered["csrf_token"])
- model_data = _rpc_post(ls_url, csrf_token, "GetCascadeModelConfigData", {}, timeout=3.0)
- raw_configs = model_data.get("clientModelConfigs")
- dynamic_configs = [item for item in raw_configs if isinstance(item, dict)] if isinstance(raw_configs, list) else []
+ dynamic_configs = _discovered_model_configs(discovered)
requested_model = (
str(executor_model.get("model") or "").strip()
@@ -1073,15 +1199,16 @@ def _run_turn(
**({"media": media} if media else {}),
}
with self._send_lock:
- self._check_interrupted()
- self._active_turn_id = turn_id
- if event_sink is not None:
- event_sink("turn.started", {"upstream_turn_id": turn_id, "threadId": self.cascade_id,
- "native_input": {"thread_id": self.cascade_id, "start_step_index": start_step_index,
- "input_sha256": hashlib.sha256(full_prompt.encode()).hexdigest()}})
- _rpc_post(self.ls_url, self.csrf_token, "SendUserCascadeMessage", send_payload, timeout=15.0)
- self._system_primed = True
- self._pending_history.clear()
+ with (self._launch_guard() if self._launch_guard is not None else nullcontext()):
+ self._check_interrupted()
+ self._active_turn_id = turn_id
+ if event_sink is not None:
+ event_sink("turn.started", {"upstream_turn_id": turn_id, "threadId": self.cascade_id,
+ "native_input": {"thread_id": self.cascade_id, "start_step_index": start_step_index,
+ "input_sha256": hashlib.sha256(full_prompt.encode()).hexdigest()}})
+ _rpc_post(self.ls_url, self.csrf_token, "SendUserCascadeMessage", send_payload, timeout=15.0)
+ self._system_primed = True
+ self._pending_history.clear()
stream_filter = VisibleResponseStreamFilter()
emitted_visible_delta = False
diff --git a/loopx/capabilities/native_chat/codex_adapter.py b/loopx/capabilities/native_chat/codex_adapter.py
index 3cf75faa2a..1a02aeae17 100644
--- a/loopx/capabilities/native_chat/codex_adapter.py
+++ b/loopx/capabilities/native_chat/codex_adapter.py
@@ -18,6 +18,7 @@
import threading
import time
from typing import Any, Callable, Mapping, TYPE_CHECKING
+from urllib.parse import urlsplit
from ...chat_agent import (
CodexChatAgentError, CodexChatAgentSession, CodexChatTimeoutError,
@@ -446,6 +447,63 @@ def compact_native_history(
return import_headroom
+def _loopback_api_fallback_auth_failure(error: Any) -> dict[str, Any] | None:
+ """Return the typed error payload only for local OpenAI relay 401 failures."""
+ info = _retry_error_details(error).get("retry", {}).get("codex_error_info")
+ if not isinstance(info, dict) or len(info) != 1:
+ return None
+ variant, detail = next(iter(info.items()))
+ if (variant not in {"httpConnectionFailed", "responseStreamConnectionFailed"}
+ or not isinstance(detail, dict) or type(detail.get("httpStatusCode")) is not int
+ or detail.get("httpStatusCode") != 401):
+ return None
+ message = error.get("message") if isinstance(error, dict) else None
+ if not isinstance(message, str) or "OpenAI API fallback request could not complete., url: " not in message:
+ return None
+ _, url_part = message.rsplit(", url: ", 1)
+ url_token = url_part.split()[0] if url_part.split() else ""
+ parsed = urlsplit(url_token)
+ if (parsed.scheme != "http" or parsed.hostname != "127.0.0.1"
+ or parsed.port is None or not 1024 <= parsed.port <= 65535
+ or parsed.path not in {"/v1/responses", "/steward/v1/responses"}):
+ return None
+ return info
+
+
+def _loopback_relay_transport_ready(runtime_root: Path, codex_home: Path, consumer: str) -> tuple[bool, float]:
+ """Verify current native-account or relay health for recovering a relay-401 Turn."""
+ from loopx_codex_provider_routing import openai_relay
+ from loopx_codex_provider_routing.native import NativeAccounts
+ from loopx_codex_provider_routing.native_host import HostError
+
+ owner = NativeAccounts(runtime_root)
+ now = float(int(time.time()))
+ try:
+ state = owner._load()
+ adapter = state.get("adapter")
+ profile_home = adapter.get("profile", {}).get("home") if isinstance(adapter, dict) else None
+ if not isinstance(profile_home, str) or not profile_home:
+ return False, now
+ if Path(profile_home).expanduser().resolve() != codex_home.expanduser().resolve():
+ return False, now
+ transport = owner.model_transport(codex_home=codex_home, consumer=consumer)
+ if transport is not None:
+ relay = openai_relay.route(owner)
+ return bool(openai_relay.healthy(relay, consumer=consumer) and owner.api_keys().candidates("")), now
+ status = owner.status()
+ if status.get("available") is not True or status.get("recovery_required") is not False or status.get("login_matches") is not True:
+ return False, now
+ if status.get("api_active") is True:
+ relay = openai_relay.route(owner)
+ return bool(openai_relay.healthy(relay, consumer=consumer) and owner.api_keys().candidates("")), now
+ active_slot = status.get("active_slot")
+ accounts = {row.get("slot"): row for row in (status.get("accounts") or []) if isinstance(row, dict)}
+ return (isinstance(active_slot, str) and bool(active_slot)
+ and accounts.get(active_slot, {}).get("availability") == "available"), now
+ except (HostError, ImportError, KeyError, OSError, RuntimeError, ValueError, subprocess.SubprocessError):
+ return False, now
+
+
def failed_request_readback(session: CodexChatAgentSession, turn_id: str) -> dict[str, Any] | None:
"""Observe a historical pre-execution failure without resuming it.
@@ -483,6 +541,10 @@ def terminal() -> dict[str, Any] | None:
# can use the compaction exception to that inventory.
return {"codex_error_info": "other", "pre_execution_routing_timeout": True,
"latest_routing_failure": ids[0] == turn_id}
+ loopback_auth_info = _loopback_api_fallback_auth_failure(selected.get("error"))
+ if loopback_auth_info is not None:
+ return {"codex_error_info": loopback_auth_info, "loopback_relay_auth_failure": True,
+ "latest_routing_failure": ids[0] == turn_id}
info = _retry_error_details(selected.get("error")).get("retry", {}).get("codex_error_info")
# `other` is evidence of a failed native request, not permission to
# retry it. The typed admission owner separately decides whether
@@ -503,7 +565,8 @@ def terminal() -> dict[str, Any] | None:
if proof is None:
return None
items = list(session._control_items("thread/items/list", {"turnId": turn_id}, deadline))
- routing = proof.get("pre_execution_routing_timeout") is True
+ routing = (proof.get("pre_execution_routing_timeout") is True
+ or proof.get("loopback_relay_auth_failure") is True)
allowed = ({"userMessage", "contextCompaction"}
if routing and proof["latest_routing_failure"] else {"userMessage"})
ids: set[str] = set()
@@ -801,7 +864,9 @@ def failed_request_observation(*, codex_bin: str, codex_home: Path,
allow_untyped_result_review: bool = False,
known_turn_ids: set[str] | None = None,
work_dir: Path | None = None,
- model_auth_home: Path | None = None) -> dict[str, Any] | None:
+ model_auth_home: Path | None = None,
+ runtime_root: Path | None = None,
+ consumer: str = "worker") -> dict[str, Any] | None:
"""Read persisted native metadata/items; create no native conversation.
The initialized stdio client is not a listener or a model runner. It
@@ -858,6 +923,14 @@ def failed_request_observation(*, codex_bin: str, codex_home: Path,
quota_observed_at=now)
except (ImportError, KeyError, OSError, RuntimeError, ValueError, subprocess.SubprocessError):
proof["quota_available"] = False
+ elif proof.get("loopback_relay_auth_failure") is True:
+ proof["relay_transport_ready"] = False
+ if runtime_root is not None:
+ try:
+ ready, observed_at = _loopback_relay_transport_ready(runtime_root, codex_home, consumer)
+ proof.update(relay_transport_ready=ready, relay_observed_at=observed_at)
+ except (ImportError, KeyError, OSError, RuntimeError, ValueError, subprocess.SubprocessError):
+ proof["relay_transport_ready"] = False
return proof
if time.monotonic() >= deadline:
break
diff --git a/loopx/capabilities/native_chat/external_conversations.py b/loopx/capabilities/native_chat/external_conversations.py
index 4cb2488e9a..ba050d2a6f 100644
--- a/loopx/capabilities/native_chat/external_conversations.py
+++ b/loopx/capabilities/native_chat/external_conversations.py
@@ -10,13 +10,15 @@
from copy import deepcopy
from functools import cache
from pathlib import Path
-import hashlib
import json
import re
import threading
+import time
from datetime import datetime as datetime, timezone as timezone
from typing import Any
+from ...chat_attachments import normalize_chat_image_attachments
+
from ...chat_ingress import (
STEWARD_BINDING_ADOPTION_PENDING as STEWARD_BINDING_ADOPTION_PENDING,
ensure_external_admission_scope,
@@ -28,7 +30,6 @@
_read_json as _read_json,
RESUMABLE_SESSION_STATES,
)
-from ...chat_attachments import normalize_chat_image_attachments
from ...control_plane import effect_runtime
from ...control_plane.quota.automation_cadence_readback import (
automation_cadence_readback,
@@ -40,6 +41,11 @@
from .file_discovery import (
FileChatDiscovery,
QuotaBacklogObservationCache,
+ _DeferredBacklogRequest,
+ _RecoveryDiscoveryReads,
+ _preparation_image_identity,
+ _project_source_request_digest,
+ _project_user_message_hint,
_same_store_method,
external_source_route_path,
file_change_fingerprint,
@@ -58,94 +64,6 @@
_QUOTA_BACKLOG_BATCH_BYTES = 1024 * 1024
-class _DeferredBacklogRequest(dict[str, Any]):
- """Bounded backlog request view carrying its observed file fingerprint."""
-
- __slots__ = ("req_fp", "needs_full_read")
-
- def __init__(
- self,
- data: Mapping[str, Any],
- *,
- req_fp: tuple[int, ...] | None,
- needs_full_read: bool,
- ) -> None:
- super().__init__(data)
- self.req_fp = req_fp
- self.needs_full_read = needs_full_read
-
-
-def _preparation_image_identity(value: Any) -> dict[str, Any] | None:
- """Fingerprint already normalized Core images without exporting their bytes."""
- if not isinstance(value, list) or not value:
- return None
- try:
- images = normalize_chat_image_attachments(value)
- if images != value:
- return None
- encoded = json.dumps(images, sort_keys=True, separators=(",", ":")).encode()
- return {"count": len(images), "sha256": hashlib.sha256(encoded).hexdigest()}
- except (ValueError, TypeError, OverflowError):
- return None
-
-
-def _project_source_request_digest(_path: Path, item: dict[str, Any]) -> dict[str, Any]:
- canonical = json.dumps(item, sort_keys=True, ensure_ascii=False, separators=(",", ":"))
- return {
- "request_ref": item.get("request_ref"),
- "binding_id": item.get("binding_id"),
- "source": item.get("source"),
- "sha256": hashlib.sha256(canonical.encode("utf-8")).hexdigest(),
- }
-
-
-def _project_user_message_hint(message: Mapping[str, Any]) -> dict[str, Any]:
- return {
- key: message[key]
- for key in ("role", "turn_id", "text", "origin", "created_at")
- if key in message
- }
-
-
-class _RecoveryDiscoveryReads:
- """Lazy facts for one queue-discovery pass, never recovery admission.
-
- Historical requests share Session history. Retain queue/control/history
- hints, never provider identity or a recovery decision. A new
- pass builds new observations; normal admission never receives this object.
- """
-
- def __init__(self, core: ChatExternalConversations) -> None:
- self.core = core
- self.queues: dict[str, bool] = {}
- self.histories: dict[str, list[dict[str, Any]]] = {}
- self.message_indexes: dict[str, dict[str, list[dict[str, Any]]]] = {}
- self.controls: list[dict[str, Any]] | None = None
-
- def queued(self, session_id: str) -> bool:
- if session_id not in self.queues:
- self.queues[session_id] = bool(self.core.controller.store.queued_turns(session_id))
- return self.queues[session_id]
-
- def user_messages(self, session_id: str) -> list[dict[str, Any]]:
- if session_id not in self.histories:
- self.histories[session_id] = list(self.core._cached_user_messages(session_id))
- return self.histories[session_id]
-
- def source_controls(self) -> list[dict[str, Any]]:
- if self.controls is None:
- self.controls = self.core.source_controls()
- return self.controls
-
- def matching_user_messages(self, session_id: str, text: str) -> list[dict[str, Any]]:
- if session_id not in self.message_indexes:
- index: dict[str, list[dict[str, Any]]] = {}
- for row in self.user_messages(session_id):
- index.setdefault(row.get("text", ""), []).append(row)
- self.message_indexes[session_id] = index
- return self.message_indexes[session_id].get(text, [])
-
-
class ChatExternalConversations(ExternalStewardCommissionMixin):
def __init__(self, controller: Any) -> None:
self.controller = controller
@@ -192,6 +110,7 @@ def _context_recovery_plan(self, row: dict[str, Any], *,
discovery_only: bool = False,
preparation_scope_available: bool = False,
metadata_capacity: dict[str, Any] | None = None,
+ failure_notice_receipt_unknown: bool = False,
discovery_reads: _RecoveryDiscoveryReads | None = None) -> dict[str, Any]:
from ...chat_manager import MANAGER_CONTEXT_VERSION
@@ -225,7 +144,8 @@ def _context_recovery_plan(self, row: dict[str, Any], *,
and (retained_review is None or reservation.get("work_attempt") == 1
or reservation.get("legacy_result_review")) else None)
image_failure = turn.get("error_code") == "runtime_error" and bool(row.get("attachments"))
- runtime_failure = turn.get("error_code") in {"model_auth_unavailable", "resume_failed"}
+ runtime_failure = turn.get("error_code") in {
+ "model_auth_unavailable", "resume_failed", "execution_worker_exited_before_dispatch"}
needs_full_events = (not discovery_only or retained_review is not None
or turn.get("error_code") == "manager_authorization_unavailable"
or image_failure or runtime_failure)
@@ -319,6 +239,7 @@ def _context_recovery_plan(self, row: dict[str, Any], *,
"preparation_observation": preparation,
"later_equivalent_input_present": later_equivalent,
"preparation_scope_available": preparation_scope_available,
+ "failure_notice_receipt_unknown": bool(failure_notice_receipt_unknown),
"later_source_input_present": any(item.get("binding_id") == row["binding_id"]
and item.get("source") == row["source"] and item.get("command") is None
and datetime.fromisoformat(item["created_at"]) > datetime.fromisoformat(row["created_at"])
@@ -529,13 +450,15 @@ def quota_backlog_pending(self, row: dict[str, Any], *,
"""Retain quota and declared unfinished work without admitting recovery."""
return self.quota_backlog_observer(source_controls=source_controls)([row])[0]
- def context_recovery_observer(self) -> Callable[[str], bool]:
+ def context_recovery_observer(self) -> Callable[..., bool]:
"""Reuse scan-local hints; reread each request, Turn and binding."""
reads = _RecoveryDiscoveryReads(self)
- return lambda ref: self.context_recovery_candidate(ref, discovery_reads=reads)
+ return lambda ref, *, failure_notice_receipt_unknown=False: self.context_recovery_candidate(
+ ref, discovery_reads=reads, failure_notice_receipt_unknown=failure_notice_receipt_unknown)
def context_recovery_candidate(self, request_ref: str, *,
- discovery_reads: _RecoveryDiscoveryReads | None = None) -> bool:
+ discovery_reads: _RecoveryDiscoveryReads | None = None,
+ failure_notice_receipt_unknown: bool = False) -> bool:
"""Discovery only; every authority and native fact is re-read at admission."""
try:
row = self.read_request(request_ref)
@@ -550,6 +473,7 @@ def context_recovery_candidate(self, request_ref: str, *,
return True
if not row.get("delivery_verified"):
return self._context_recovery_plan(row, discovery_only=True,
+ failure_notice_receipt_unknown=failure_notice_receipt_unknown,
discovery_reads=discovery_reads).get("recovery_candidate") is True
turn = self.controller.store.load_turn(row["session_id"], row["turn_id"])
if not turn or turn.get("status") not in {"failed", "completed"}:
@@ -562,12 +486,14 @@ def context_recovery_candidate(self, request_ref: str, *,
and result_review_evidence.collect_related_result_review(self, row, turn, discovery_reads=discovery_reads) is None):
return False # A lexical probe hint, never permission to adopt or send.
return self._context_recovery_plan(row, discovery_only=True,
+ failure_notice_receipt_unknown=failure_notice_receipt_unknown,
discovery_reads=discovery_reads).get("recovery_candidate") is True
except (KeyError, ValueError, RuntimeError, OSError):
return False
def recover_failed_request(self, request_ref: str, *,
- source_observation: Callable[[dict[str, Any]], dict[str, Any] | None]) -> dict[str, Any]:
+ source_observation: Callable[[dict[str, Any]], dict[str, Any] | None],
+ failure_notice_receipt_unknown: bool = False) -> dict[str, Any]:
"""Reserve/admit one same-source continuation through the existing queue.
The failed Turn and its original notice remain immutable. The new Turn
@@ -581,7 +507,7 @@ def recover_failed_request(self, request_ref: str, *,
path = self.root / f"{request_ref}.json"
with exclusive_file_lock(path, operation="admit_external_chat_request"):
row = self.read_request(request_ref)
- plan = self._context_recovery_plan(row)
+ plan = self._context_recovery_plan(row, failure_notice_receipt_unknown=failure_notice_receipt_unknown)
if not plan.get("recovery_candidate"):
return row
session = self.controller.store.load_session(row["session_id"])
@@ -659,6 +585,8 @@ def recover_failed_request(self, request_ref: str, *,
proof = CodexAppServerAdapter.failed_request_observation(
codex_bin=self.controller.codex_bin, codex_home=Path(session["codex_home"]),
model_auth_home=model_auth_home,
+ runtime_root=self.controller.coordination_runtime_root,
+ consumer="steward" if session.get("steward_context") else "worker",
read_only_review=bool(plan.get("read_only_review")) and not quota_answer,
thread_ids=plan["native_thread_ids"], turn_id=turn["upstream_turn_id"])
if (proof is not None and proof.get("codex_error_info") == "usageLimitExceeded"
@@ -706,7 +634,7 @@ def recover_failed_request(self, request_ref: str, *,
return row
plan = self._context_recovery_plan(row, observation=proof, source_verified=source is not None,
selected_binding=selected["binding"], preparation_scope_available=scope_available,
- metadata_capacity=capacity)
+ metadata_capacity=capacity, failure_notice_receipt_unknown=failure_notice_receipt_unknown)
if plan.get("retain_metadata_result") and facts and not self.controller.store.native_result(row["session_id"], turn["turn_id"]):
try:
self.controller.store.retain_native_result(row["session_id"], turn["turn_id"],
@@ -717,7 +645,8 @@ def recover_failed_request(self, request_ref: str, *,
return row # Preserve the delivered answer and unspent review.
proof = self._original_native_result(row, session, turn, context, include_raw_response=True)
plan = self._context_recovery_plan(row, observation=proof, source_verified=source is not None,
- selected_binding=selected["binding"], metadata_capacity=capacity)
+ selected_binding=selected["binding"], metadata_capacity=capacity,
+ failure_notice_receipt_unknown=failure_notice_receipt_unknown)
if not plan.get("recovery_eligible"):
if plan.get("metadata_quota_wait") is not None and row.get("metadata_quota_wait") != plan["metadata_quota_wait"]:
row["metadata_quota_wait"] = plan["metadata_quota_wait"]
@@ -730,13 +659,13 @@ def recover_failed_request(self, request_ref: str, *,
recovery = {"version": MANAGER_CONTEXT_VERSION, "failed_turn_id": turn["turn_id"],
"native_observation": proof, "source_observation": source,
"client_turn_id": plan["client_turn_id"],
- "failure_notice_delivery_verified": row["delivery_verified"]}
+ "failure_notice_delivery_verified": row.get("delivery_verified") is True}
if plan.get("read_only_review"):
recovery.update(read_only_review=True, presentation_key=f"{MANAGER_CONTEXT_VERSION}.review.1")
if plan.get("unfinished_work") or plan.get("legacy_result_review"):
recovery.pop("failure_notice_delivery_verified", None)
recovery.update(original_turn=deepcopy(turn), binding_snapshot=selected["binding"],
- prior_result_delivery_verified=row["delivery_verified"])
+ prior_result_delivery_verified=row.get("delivery_verified") is True)
if plan.get("unfinished_work"):
recovery["unfinished_work"] = deepcopy(plan["unfinished_work"])
else:
@@ -758,7 +687,7 @@ def recover_failed_request(self, request_ref: str, *,
if plan.get("work_attempt"):
recovery.pop("failure_notice_delivery_verified", None)
recovery.update(work_attempt=1, unfinished_work=plan["unfinished_work"],
- prior_result_delivery_verified=row["delivery_verified"],
+ prior_result_delivery_verified=row.get("delivery_verified") is True,
presentation_key=f"{MANAGER_CONTEXT_VERSION}.work.1", not_before=plan["not_before"])
if plan.get("completed_review_turn_id"):
recovery.update(completed_review_turn_id=plan["completed_review_turn_id"],
@@ -800,11 +729,17 @@ def _preparation_scope_available(self, session: dict[str, Any], *, runtime_prepa
if runtime_preparation:
# Preparation never starts a model request. Check the same
# optional transport owner before reserving a bounded retry.
- from .codex_auth import native_api_transport
- native_api_transport(self.controller.coordination_runtime_root,
- checked_session_home(self.controller.codex_home, session),
- consumer="worker" if session.get("project_context") is not None else "steward",
- model=session.get("requested_agent_model_id") or session.get("agent_model_id"))
+ if session.get("agent_id") == "antigravity":
+ from .antigravity_adapter import discover_antigravity_language_server
+
+ if discover_antigravity_language_server(deadline=time.monotonic() + 3.0) is None:
+ return False
+ else:
+ from .codex_auth import native_api_transport
+ native_api_transport(self.controller.coordination_runtime_root,
+ checked_session_home(self.controller.codex_home, session),
+ consumer="worker" if session.get("project_context") is not None else "steward",
+ model=session.get("requested_agent_model_id") or session.get("agent_model_id"))
if session.get("project_context") is not None:
# Resolve the existing project grant again; a project assistant
# has no manager portfolio scope to borrow.
diff --git a/loopx/capabilities/native_chat/file_discovery.py b/loopx/capabilities/native_chat/file_discovery.py
index 39bc49ae3f..84e0e79ffc 100644
--- a/loopx/capabilities/native_chat/file_discovery.py
+++ b/loopx/capabilities/native_chat/file_discovery.py
@@ -6,6 +6,7 @@
from datetime import datetime
import base64
import hashlib
+import json
from pathlib import Path
import re
import sys
@@ -563,3 +564,93 @@ def capture_reply_image_plan(workspace: Path, turn_id: str, targets: list[str])
except (OSError, ValueError):
result.append(None)
return result
+
+
+class _DeferredBacklogRequest(dict[str, Any]):
+ """Bounded backlog request view carrying its observed file fingerprint."""
+
+ __slots__ = ("req_fp", "needs_full_read")
+
+ def __init__(
+ self,
+ data: Mapping[str, Any],
+ *,
+ req_fp: tuple[int, ...] | None,
+ needs_full_read: bool,
+ ) -> None:
+ super().__init__(data)
+ self.req_fp = req_fp
+ self.needs_full_read = needs_full_read
+
+
+def _preparation_image_identity(value: Any) -> dict[str, Any] | None:
+ """Fingerprint already normalized Core images without exporting their bytes."""
+ from ...chat_attachments import normalize_chat_image_attachments
+
+ if not isinstance(value, list) or not value:
+ return None
+ try:
+ images = normalize_chat_image_attachments(value)
+ if images != value:
+ return None
+ encoded = json.dumps(images, sort_keys=True, separators=(",", ":")).encode()
+ return {"count": len(images), "sha256": hashlib.sha256(encoded).hexdigest()}
+ except (ValueError, TypeError, OverflowError):
+ return None
+
+
+def _project_source_request_digest(_path: Path, item: dict[str, Any]) -> dict[str, Any]:
+ canonical = json.dumps(item, sort_keys=True, ensure_ascii=False, separators=(",", ":"))
+ return {
+ "request_ref": item.get("request_ref"),
+ "binding_id": item.get("binding_id"),
+ "source": item.get("source"),
+ "sha256": hashlib.sha256(canonical.encode("utf-8")).hexdigest(),
+ }
+
+
+def _project_user_message_hint(message: Mapping[str, Any]) -> dict[str, Any]:
+ return {
+ key: message[key]
+ for key in ("role", "turn_id", "text", "origin", "created_at")
+ if key in message
+ }
+
+
+class _RecoveryDiscoveryReads:
+ """Lazy facts for one queue-discovery pass, never recovery admission.
+
+ Historical requests share Session history. Retain queue/control/history
+ hints, never provider identity or a recovery decision. A new
+ pass builds new observations; normal admission never receives this object.
+ """
+
+ def __init__(self, core: Any) -> None:
+ self.core = core
+ self.queues: dict[str, bool] = {}
+ self.histories: dict[str, list[dict[str, Any]]] = {}
+ self.message_indexes: dict[str, dict[str, list[dict[str, Any]]]] = {}
+ self.controls: list[dict[str, Any]] | None = None
+
+ def queued(self, session_id: str) -> bool:
+ if session_id not in self.queues:
+ self.queues[session_id] = bool(self.core.controller.store.queued_turns(session_id))
+ return self.queues[session_id]
+
+ def user_messages(self, session_id: str) -> list[dict[str, Any]]:
+ if session_id not in self.histories:
+ self.histories[session_id] = list(self.core._cached_user_messages(session_id))
+ return self.histories[session_id]
+
+ def source_controls(self) -> list[dict[str, Any]]:
+ if self.controls is None:
+ self.controls = self.core.source_controls()
+ return self.controls
+
+ def matching_user_messages(self, session_id: str, text: str) -> list[dict[str, Any]]:
+ if session_id not in self.message_indexes:
+ index: dict[str, list[dict[str, Any]]] = {}
+ for row in self.user_messages(session_id):
+ index.setdefault(row.get("text", ""), []).append(row)
+ self.message_indexes[session_id] = index
+ return self.message_indexes[session_id].get(text, [])
diff --git a/loopx/capabilities/native_chat/github_pr_evidence.py b/loopx/capabilities/native_chat/github_pr_evidence.py
index a718608889..94834bfdc2 100644
--- a/loopx/capabilities/native_chat/github_pr_evidence.py
+++ b/loopx/capabilities/native_chat/github_pr_evidence.py
@@ -9,6 +9,7 @@
import subprocess
from typing import Any, Callable, Mapping, Sequence
+from ..issue_fix.repository_snapshot import _REPO_PATTERN as _REPO_SLUG_RE
from ..pr_review_queue.review_body import (
author_owned_review_title_matches,
check_review_body,
@@ -25,17 +26,16 @@
)
GITHUB_PR_VIEW_FIELDS = GITHUB_PR_LIST_FIELDS + ",state,mergedAt,mergeCommit"
-_REPO_SLUG_RE = re.compile(r"^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$")
_GITHUB_URL_TARGET_RE = re.compile(
- r"^https://github\.com/([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)/pull/([1-9][0-9]*)(?:[/?#].*)?$"
+ r"^https://github\.com/([^/]+/[^/]+)/pull/([1-9][0-9]*)(?:[/?#].*)?$"
)
_REPO_NUM_TARGET_RE = re.compile(
- r"^(?:git:github\.com/)?([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)#([1-9][0-9]*)$"
+ r"^(?:git:github\.com/)?([^/#]+/[^/#]+)#([1-9][0-9]*)$"
)
_BARE_NUM_TARGET_RE = re.compile(r"^#?([1-9][0-9]*)$")
_REMOTE_GITHUB_RE = re.compile(
r"(?:^git:github\.com/|^git@github\.com:|^ssh://git@github\.com/|^https?://github\.com/)"
- r"([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?)(?:\.git)?/?$"
+ r"([^/]+/[^/]+?)(?:\.git)?/?$"
)
_CHECK_FAIL_CONCLUSIONS = frozenset(
{"FAILURE", "TIMED_OUT", "CANCELLED", "ACTION_REQUIRED", "STARTUP_FAILURE"}
@@ -51,7 +51,9 @@ def normalize_github_repo_slug(value: Any) -> str | None:
if _REPO_SLUG_RE.fullmatch(raw):
return raw.removesuffix(".git")
match = _REMOTE_GITHUB_RE.match(raw)
- return match.group(1) if match else None
+ if match and _REPO_SLUG_RE.fullmatch(match.group(1)):
+ return match.group(1)
+ return None
def parse_github_pr_target(
@@ -62,10 +64,10 @@ def parse_github_pr_target(
raise ValueError("protected merge target must be a string")
raw = target.strip()
url_match = _GITHUB_URL_TARGET_RE.fullmatch(raw)
- if url_match:
+ if url_match and _REPO_SLUG_RE.fullmatch(url_match.group(1)):
return url_match.group(1), int(url_match.group(2))
repo_match = _REPO_NUM_TARGET_RE.fullmatch(raw)
- if repo_match:
+ if repo_match and _REPO_SLUG_RE.fullmatch(repo_match.group(1)):
return repo_match.group(1), int(repo_match.group(2))
bare_match = _BARE_NUM_TARGET_RE.fullmatch(raw)
if bare_match and default_repository and _REPO_SLUG_RE.fullmatch(default_repository):
@@ -112,7 +114,7 @@ def _git_origin_slug(repo_path: Path) -> str | None:
try:
completed = subprocess.run(
["git", "-C", str(repo_path), "config", "--get", "remote.origin.url"],
- capture_output=True, text=True, check=False, timeout=3,
+ capture_output=True, text=True, encoding="utf-8", check=False, timeout=3,
)
if completed.returncode == 0:
return normalize_github_repo_slug(completed.stdout.strip())
diff --git a/loopx/capabilities/native_chat/model_settings.py b/loopx/capabilities/native_chat/model_settings.py
index 0e9306126b..127b6aecbd 100644
--- a/loopx/capabilities/native_chat/model_settings.py
+++ b/loopx/capabilities/native_chat/model_settings.py
@@ -133,14 +133,16 @@ def inspect_agent_model_options(controller: ChatRuntimeController, agent_id: str
},
}
if agent_id == "antigravity":
- from .antigravity_adapter import ANTIGRAVITY_DEFAULT_MODEL, ANTIGRAVITY_MODEL_OPTIONS
+ from .antigravity_adapter import ANTIGRAVITY_DEFAULT_MODEL, offered_antigravity_model_options
+ options = offered_antigravity_model_options()
return {
"agent_id": agent_id,
"supported": True,
"model": {
"id": "model",
"label": "Model",
- "selected": ANTIGRAVITY_DEFAULT_MODEL,
+ "selected": (ANTIGRAVITY_DEFAULT_MODEL
+ if any(item["id"] == ANTIGRAVITY_DEFAULT_MODEL for item in options) else None),
"options": [
{
"value": str(item["id"]),
@@ -154,7 +156,7 @@ def inspect_agent_model_options(controller: ChatRuntimeController, agent_id: str
if isinstance(effort, dict) and effort.get("reasoning_effort")
],
}
- for item in ANTIGRAVITY_MODEL_OPTIONS
+ for item in options
],
},
}
diff --git a/loopx/capabilities/native_chat/session_queue.py b/loopx/capabilities/native_chat/session_queue.py
index 217e882154..fdbb0a1fda 100644
--- a/loopx/capabilities/native_chat/session_queue.py
+++ b/loopx/capabilities/native_chat/session_queue.py
@@ -6,6 +6,14 @@
from ...chat_store import CHAT_SESSION_MODE_ATTACHED, TERMINAL_TURN_STATES
+def _is_observation_only_monitor_turn(turn: dict[str, Any] | None) -> bool:
+ """Return True only for native no-model monitor turns."""
+ if not isinstance(turn, dict) or turn.get("loopx_execution") is not True:
+ return False
+ request = turn.get("loopx_request")
+ return isinstance(request, dict) and request.get("work_kind") == "monitor"
+
+
def drain_session_queue(controller: Any, *, session_id: str, work_dir: Path, objective: str) -> None:
wakeup = threading.Event()
with controller.turn_release_wakeup(wakeup):
@@ -76,19 +84,20 @@ def _drain_session_queue_events(controller: Any, *, session_id: str, work_dir: P
# normal preparation and execution fences below.
turn = active
else:
- with controller.lock:
- attached = controller.adapters.get(session_id)
- if attached is None or not attached.healthcheck():
- try:
- controller._ensure_adapter(
- session,
- work_dir=work_dir,
- objective=objective,
- )
- except Exception as exc:
- controller._fail_queue_preparation(session_id, active_turn_id, exc)
- else:
- continue
+ if not _is_observation_only_monitor_turn(active):
+ with controller.lock:
+ attached = controller.adapters.get(session_id)
+ if attached is None or not attached.healthcheck():
+ try:
+ controller._ensure_adapter(
+ session,
+ work_dir=work_dir,
+ objective=objective,
+ )
+ except Exception as exc:
+ controller._fail_queue_preparation(session_id, active_turn_id, exc)
+ else:
+ continue
active = controller.store.load_turn(session_id, active_turn_id)
if active and active.get("status") not in TERMINAL_TURN_STATES:
controller.closed.wait(0.05)
@@ -132,11 +141,16 @@ def _drain_session_queue_events(controller: Any, *, session_id: str, work_dir: P
current = controller.store.load_session(session_id)
if current is None or current.get("active_turn_id") != turn_id:
return
- adapter = controller._ensure_adapter(
- current,
- work_dir=work_dir,
- objective=objective,
- )
+ if _is_observation_only_monitor_turn(turn):
+ from .steward_monitor import StewardObservationAdapter
+
+ adapter: Any = StewardObservationAdapter(current.get("upstream_thread_id"))
+ else:
+ adapter = controller._ensure_adapter(
+ current,
+ work_dir=work_dir,
+ objective=objective,
+ )
if (controller.store.load_session(session_id) or {}).get("active_turn_id") != turn_id:
return
except Exception as exc:
diff --git a/loopx/capabilities/native_chat/steward_managed_mode.py b/loopx/capabilities/native_chat/steward_managed_mode.py
index 83c3366b05..56f6af14e9 100644
--- a/loopx/capabilities/native_chat/steward_managed_mode.py
+++ b/loopx/capabilities/native_chat/steward_managed_mode.py
@@ -167,7 +167,7 @@ def health(self, binding_id: str) -> dict[str, Any]:
return {"available": True, "prepared": True,
**(self.bindings._core("collaboration.steward.managed_mode.executor", {
"binding": binding, "session": session})
- if binding["executor_endpoint_id"] != "codex" else {}),
+ if binding["executor_endpoint_id"] != "codex" or session.get("agent_id") != "codex" else {}),
"scheduling_enabled": bool(active and monitor and monitor.get("status") == "open"),
"goal_active": active, "report": StewardReports(self.controller).health(binding_id),
"monitor": ({"todo_id": monitor["todo_id"], "status": monitor["status"],
@@ -267,7 +267,8 @@ def _request(self, session_id: str, agent_id: str, operation_id: str,
def admit(self, request: dict[str, Any], *, turn: dict[str, Any] | None = None,
settlement_recovery: dict[str, Any] | None = None,
- selection_recovery: dict[str, Any] | None = None) -> dict[str, Any]:
+ selection_recovery: dict[str, Any] | None = None,
+ frozen_contract_recovery: dict[str, Any] | None = None) -> dict[str, Any]:
from .conversation_bindings import ChatConversationBindings
from .steward_turn import StewardTurnHost
if self.controller.closed.is_set():
@@ -310,6 +311,7 @@ def admit(self, request: dict[str, Any], *, turn: dict[str, Any] | None = None,
"goal_active": bool(goal and goal.get("status") == "active" and not goal_is_stopped(goal)),
"monitor_todo": monitor, **report, **creation,
**({"report_settlement_recovery": settlement_recovery} if settlement_recovery is not None else {}),
+ **({"report_frozen_contract_recovery": frozen_contract_recovery} if frozen_contract_recovery is not None else {}),
**({"report_selection_recovery": selection_recovery} if selection_recovery is not None else {}),
**StewardTurnHost(self.controller).report_quota_basis(request),
**StewardTurnHost(self.controller).monitor_restart_basis(request),
diff --git a/loopx/capabilities/native_chat/steward_monitor.py b/loopx/capabilities/native_chat/steward_monitor.py
index 4bfb11b612..4788daeb3b 100644
--- a/loopx/capabilities/native_chat/steward_monitor.py
+++ b/loopx/capabilities/native_chat/steward_monitor.py
@@ -12,7 +12,6 @@
import json
import logging
from pathlib import Path
-import re
import subprocess
import time
from typing import Any
@@ -24,6 +23,7 @@
from ...control_plane.runtime.time import now_utc
from ...control_plane.goals.activation import goal_is_stopped
from ...file_lock import exclusive_file_lock
+from ..issue_fix.repository_snapshot import _REPO_PATTERN
from .github_pr_evidence import observe_github_repository_prs
from .steward_commands import native_command
from .steward_managed_mode import StewardManagedMode
@@ -31,7 +31,6 @@
TARGET = "steward-portfolio-watch"
FOLLOWUP_TARGET_PREFIX = "steward-monitor-followup:"
_LOG = logging.getLogger(__name__)
-_GITHUB_REPO_URL = re.compile(r"^git:github\.com/([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+)$")
class StewardObservationAdapter:
@@ -71,8 +70,11 @@ def advancement_turn_id(todo: dict[str, Any]) -> str:
def _github_repo_slug(task_repository: Any) -> str | None:
if not isinstance(task_repository, str):
return None
- match = _GITHUB_REPO_URL.fullmatch(task_repository.strip())
- return match.group(1) if match else None
+ raw = task_repository.strip()
+ if not raw.startswith("git:github.com/"):
+ return None
+ slug = raw.removeprefix("git:github.com/")
+ return slug if _REPO_PATTERN.fullmatch(slug) else None
def _observe_github_repository_prs(repo_slug: str) -> dict[str, Any]:
diff --git a/loopx/capabilities/native_chat/steward_reports.py b/loopx/capabilities/native_chat/steward_reports.py
index da82967192..44ee3e3d2d 100644
--- a/loopx/capabilities/native_chat/steward_reports.py
+++ b/loopx/capabilities/native_chat/steward_reports.py
@@ -426,7 +426,8 @@ def _frontier(self, owner_key: str, actor: dict[str, Any]) -> tuple[dict[str, An
rows = [row for row in StewardMonitor(self.controller).todos(actor) if row.get("target_key") == window["window_id"]]
folder = attempt_directory(self.root, window)
if len(rows) == 1 and (rows[0]["status"] == "done"
- or rows[0]["status"] == "open" and (folder / "private-generation.json").exists()):
+ or rows[0]["status"] == "open" and ((folder / "private-generation.json").exists()
+ or StewardTurnHost(self.controller).has_recoverable_stopped_report(actor, window, rows[0]))):
StewardTurnHost(self.controller).recover_report_settlement(actor, window, rows[0])
return None, False
if len(rows) != 1 or rows[0]["status"] != "open":
@@ -438,7 +439,8 @@ def prepare(self, request: dict[str, Any], adapter: Any, *, authorization_scope_
_, window = self.current_window(request["actor"]["goal_id"].removeprefix("steward-agent-"))
if not window or window["window_id"] != request["report_window_id"]:
raise ValueError("the admitted original report window is unavailable")
- reader = getattr(adapter.session, "read_tool_handler", None)
+ reader = (getattr(adapter.session, "read_tool_handler", None)
+ if hasattr(adapter, "session") else getattr(adapter, "_steward_report_reader", None))
if reader is None or not authorization_scope_id:
raise ValueError("the original scoped report reader is unavailable")
session = self.controller.store.load_session(request["actor"]["session_id"])
@@ -580,10 +582,11 @@ def frontier(self, session_id: str) -> tuple[dict[str, Any] | None, bool]:
subscription, window = self.current_window(session_id)
if subscription is None:
return None, False
- if window and (attempt_directory(self.root, window) / "private-generation.json").exists():
+ if window:
todo = next((row for row in StewardMonitor(self.controller).todos(actor)
if row.get("target_key") == window["window_id"]), None)
- if todo:
+ if todo and ((attempt_directory(self.root, window) / "private-generation.json").exists()
+ or StewardTurnHost(self.controller).has_recoverable_stopped_report(actor, window, todo)):
StewardTurnHost(self.controller).recover_report_settlement(actor, window, todo)
self.publish(session_id, window)
return self._frontier(session_id, actor)
diff --git a/loopx/capabilities/native_chat/steward_turn.py b/loopx/capabilities/native_chat/steward_turn.py
index d430286b47..51fbffbed1 100644
--- a/loopx/capabilities/native_chat/steward_turn.py
+++ b/loopx/capabilities/native_chat/steward_turn.py
@@ -9,10 +9,12 @@
from collections.abc import Mapping, Sequence
from contextlib import contextmanager, ExitStack, nullcontext
+from datetime import datetime, timezone
import hashlib
import json
from pathlib import Path
import re
+import time
from typing import Any
from ...control_plane.goals.activation import goal_is_stopped
@@ -20,6 +22,16 @@
from ...file_lock import exclusive_file_lock
from .steward_managed_mode import ManagedSessionTurn, ManagedTaskValidationUnavailable, StewardManagedMode
+MONITOR_RECOVERY_RETRY_COOLDOWN_SECONDS = 300
+_WORKER_EXITED_ERROR = (
+ "The LoopX Chat execution worker exited before this turn finished. "
+ "Your conversation is saved; execution may have occurred. This turn was not retried automatically."
+)
+_POST_CONTEXT_WATCH_ERRORS = frozenset({
+ "steward monitor external signal source temporarily unavailable",
+ "disk I/O error",
+ _WORKER_EXITED_ERROR,
+})
_USER_ACTION_DEDUPE_KEY = re.compile(r"^[a-z0-9][a-z0-9._:@/-]{0,119}$")
_REPORT_VALIDATOR_PREFIX = (
"from pathlib import Path; from loopx.capabilities.periodic_report.private_generation import validate_private_generation; "
@@ -27,6 +39,18 @@
)
+def _iso_timestamp_seconds(value: Any) -> float | None:
+ if not isinstance(value, str) or not value.strip():
+ return None
+ try:
+ parsed = datetime.fromisoformat(value.strip().replace("Z", "+00:00"))
+ except ValueError:
+ return None
+ if parsed.tzinfo is None:
+ parsed = parsed.replace(tzinfo=timezone.utc)
+ return parsed.timestamp()
+
+
def _same_report_validator_command(frozen_cmd: Any, request_cmd: Any) -> bool:
"""Allow report validation commands whose only drift is the host Python path."""
return (isinstance(frozen_cmd, list) and isinstance(request_cmd, list)
@@ -58,6 +82,18 @@ def report_turn(store: Any, actor: Mapping[str, Any], window: Mapping[str, Any])
return original
+def _normalize_report_kernel_candidate(candidate: Mapping[str, Any]) -> dict[str, Any]:
+ """Normalize a verified report candidate to canonical Turn settlement fields."""
+ normalized = dict(candidate)
+ normalized.pop("editorial_response", None)
+ # This finite task's postcondition is the complete validated generation, not
+ # the model's choice of progress wording or profile-minimum shorthand.
+ normalized["result_kind"] = "validated_completion"
+ if normalized.get("delivery_batch_scale") == "multi_surface_or_implementation":
+ normalized["delivery_batch_scale"] = "multi_surface"
+ return normalized
+
+
class StewardTurnHost:
def __init__(self, controller: Any) -> None:
self.controller = controller
@@ -94,6 +130,7 @@ def _observe(self, actor: Mapping[str, Any]) -> dict[str, Any] | None:
def _admit(self, request: dict[str, Any], *, turn: dict[str, Any] | None = None,
observed: dict[str, Any] | None = None,
prepare_context: bool = False, settlement_recovery: dict[str, Any] | None = None,
+ frozen_contract_recovery: dict[str, Any] | None = None,
launch_recovery: dict[str, Any] | None = None,
selection_recovery: dict[str, Any] | None = None) -> dict[str, Any]:
if self.controller.closed.is_set():
@@ -101,7 +138,9 @@ def _admit(self, request: dict[str, Any], *, turn: dict[str, Any] | None = None,
actor = request["actor"]
if actor.get("schema_version") == "loopx_managed_session_actor_v0":
return ManagedSessionTurn(self.controller).admit(request, turn=turn,
- settlement_recovery=settlement_recovery, selection_recovery=selection_recovery)
+ settlement_recovery=settlement_recovery,
+ frozen_contract_recovery=frozen_contract_recovery,
+ selection_recovery=selection_recovery)
if self.bindings is None:
raise ValueError("the original steward binding is unavailable")
observed = observed if observed is not None else self._observe(actor)
@@ -151,6 +190,7 @@ def scope_id(ids):
"goal": self.managed._goal(actor["goal_id"]), "goal_active": bool(goal and goal.get("status") == "active" and not goal_is_stopped(goal)),
"request": request, "turn": turn, "scope": scope, "monitor_todo": monitor,
**({"report_settlement_recovery": settlement_recovery} if settlement_recovery is not None else {}),
+ **({"report_frozen_contract_recovery": frozen_contract_recovery} if frozen_contract_recovery is not None else {}),
**({"report_launch_recovery": launch_recovery} if launch_recovery is not None else {}),
**({"report_selection_recovery": selection_recovery} if selection_recovery is not None else {}),
**self.report_quota_basis(request),
@@ -209,17 +249,27 @@ def monitor_restart_basis(self, request: Mapping[str, Any]) -> dict[str, Any]:
"event_kinds": [row["kind"] for row in events], "poll_absent": not present,
"contexts": [row["payload"] for row in events if row["kind"] == "manager.context"],
"phases": [row["payload"] for row in events if row["kind"] == "agent.phase"],
- "old_worker_excluded": True, "lease_available": available}}
+ "old_worker_excluded": True, "lease_available": available,
+ "observed_at": time.time()}}
@staticmethod
- def monitor_restart_request(original: Mapping[str, Any]) -> dict[str, Any] | None:
+ def monitor_restart_request(original: Mapping[str, Any], *, now: float | None = None) -> dict[str, Any] | None:
"""Prepare correlation only; current typed admission decides recovery."""
prior = original.get("loopx_request") or {}
error = original.get("error_code")
- if (original.get("status") != "failed" or error not in {"server_restarted", "runtime_error"}
- or prior.get("work_kind") != "monitor"
- or (prior.get("monitor_restart_recovery") is not None and error != "server_restarted")):
+ if (original.get("status") != "failed"
+ or error not in {"server_restarted", "runtime_error", "runtime_unavailable"}
+ or prior.get("work_kind") != "monitor"):
return None
+ if prior.get("monitor_restart_recovery") is not None and error != "server_restarted":
+ post_context_error = error == "runtime_unavailable" or (
+ error == "runtime_error" and original.get("error") in _POST_CONTEXT_WATCH_ERRORS
+ )
+ completed_ts = _iso_timestamp_seconds(original.get("completed_at"))
+ current_ts = time.time() if now is None else now
+ if (not post_context_error or completed_ts is None
+ or current_ts - completed_ts < MONITOR_RECOVERY_RETRY_COOLDOWN_SECONDS):
+ return None
return {**prior, "monitor_restart_recovery": {"original_turn_id": original["turn_id"]}}
def monitor_restart_carrier(self, original: dict[str, Any], client_id: str) -> dict[str, Any] | None:
@@ -247,7 +297,6 @@ def report_quota_basis(self, request: Mapping[str, Any]) -> dict[str, Any]:
from ...control_plane.turn_driver import inspect_loopx_turn_journal
from ..periodic_report.private_generation import attempt_directory
from .steward_reports import report_service
- import time
actor = request["actor"]
original = self.store.load_turn(actor["session_id"], recovery["original_turn_id"])
result = ((original or {}).get("response") or {}).get("native_loopx") or {}
@@ -335,15 +384,52 @@ def report_selection_recovery(self, original: dict[str, Any], *,
from ...control_plane.effect_runtime import source_number_tokens
journal = load_turn_journal(turn_journal_path(reports.root, goal_id=actor["goal_id"], turn_key=key))
+ current_guard = None
+ if (journal.get("status") == "failed"
+ and journal.get("result_kind") == "validation_failed"
+ and journal.get("validation_stage") == "host_result_contract"):
+ current_guard = self._cleared_replan_guard(actor)
+ if current_guard is None:
+ return None
# Transport canonical observations to the shared typed admission
# owner.
proof["probe"] = {"turn": probe_turn,
"inspection": inspect_loopx_turn_journal(reports.root, goal_id=actor["goal_id"],
agent_id=actor["agent_id"], turn_key=key),
"journal": journal,
- "source_number_tokens": source_number_tokens((journal.get("plan") or {}).get("work_contract"))}
+ "source_number_tokens": source_number_tokens((journal.get("plan") or {}).get("work_contract")),
+ **({"current_guard": current_guard} if current_guard is not None else {})}
return proof
+ def _cleared_replan_guard(self, actor: Mapping[str, Any]) -> dict[str, Any] | None:
+ """Read-only guard check proving an earlier autonomous_replan obligation was cleared."""
+ from ...control_plane.quota.effective_action import EffectiveAction
+ from .steward_commands import NativeStewardCommandError, native_command
+ try:
+ raw = native_command(self.controller, [
+ "quota", "should-run", "--goal-id", actor["goal_id"], "--agent-id", actor["agent_id"], "--dry-run",
+ ])
+ except (NativeStewardCommandError, OSError, ValueError, RuntimeError):
+ return None
+ payload: Mapping[str, Any] = raw
+ if payload.get("effective_action") is None and isinstance(payload.get("reference_file"), str):
+ try:
+ ref = json.loads(Path(payload["reference_file"]).read_text(encoding="utf-8"))
+ target = Path(str(ref.get("path") or payload["reference_file"]))
+ loaded = json.loads(target.read_text(encoding="utf-8"))
+ except (OSError, ValueError):
+ return None
+ if isinstance(loaded, Mapping):
+ payload = loaded
+ current = {
+ "effective_action": payload.get("effective_action"),
+ "replan_action_packet": payload.get("replan_action_packet"),
+ }
+ if (current["effective_action"] != EffectiveAction.NORMAL_RUN.value
+ or current["replan_action_packet"] is not None):
+ return None
+ return current
+
def _report_launch_recovery(self, original: dict[str, Any]) -> dict[str, Any] | None:
"""Read the original typed launch refusal and its canonical transaction.
@@ -463,6 +549,260 @@ def _task_execution_lease(self, request: Mapping[str, Any], turn_id: str, *, rec
self.store.append_event(actor["session_id"], turn_id, kind="native_loopx.task_lease_release_unconfirmed",
payload={"todo_id": request["todo_id"], "lease_epoch": lease["lease_epoch"]})
+ def _frozen_report_contract_recovery(
+ self,
+ actor: Mapping[str, Any],
+ window: Mapping[str, Any],
+ original: Mapping[str, Any],
+ *,
+ key: str,
+ generation: Mapping[str, Any],
+ root: Path,
+ ) -> tuple[dict[str, Any], dict[str, Any]] | None:
+ """Recover only a already-frozen report whose host candidate hit host_result_contract."""
+ from ...control_plane.turn_driver.host_candidate import parse_model_json
+ from ...control_plane.turn_driver.journal_store import load_turn_journal, turn_journal_path
+
+ journal = load_turn_journal(turn_journal_path(root, goal_id=actor["goal_id"], turn_key=key))
+ if (
+ not isinstance(journal, Mapping)
+ or journal.get("status") != "failed"
+ or journal.get("result_kind") != "validation_failed"
+ or journal.get("validation_stage") != "host_result_contract"
+ or journal.get("host_attempt_count") != 1
+ ):
+ return None
+ resumed = self.store.turn_for_client(
+ actor["session_id"],
+ native_steward_client_id(actor, "report." + window["window_id"] + ".admission-resume"),
+ )
+ retry = self.store.turn_for_client(
+ actor["session_id"],
+ native_steward_client_id(actor, "report." + window["window_id"] + ".admission-retry"),
+ )
+ seen_turn_ids: set[str] = set()
+ candidates: list[Mapping[str, Any]] = []
+ for candidate_turn in (resumed, retry, original):
+ if isinstance(candidate_turn, Mapping):
+ tid = candidate_turn.get("turn_id")
+ if isinstance(tid, str) and tid not in seen_turn_ids:
+ seen_turn_ids.add(tid)
+ candidates.append(candidate_turn)
+ for candidate_turn in candidates:
+ events = self.store.events_after(actor["session_id"], candidate_turn["turn_id"], None)
+ native_failed = any(
+ row.get("kind") == "native_loopx.result"
+ and isinstance(row.get("payload"), Mapping)
+ and row["payload"].get("resume_turn_key") == key
+ and row["payload"].get("status") == "failed"
+ and row["payload"].get("result_kind") == "validation_failed"
+ and row["payload"].get("validation_stage") == "host_result_contract"
+ for row in events
+ )
+ if not native_failed:
+ continue
+ for row in reversed(events):
+ if row.get("kind") != "answer.final":
+ continue
+ message = (((row.get("payload") or {}).get("response") or {}).get("message"))
+ parsed = parse_model_json(str(message or ""))
+ if not isinstance(parsed, dict):
+ continue
+ if parsed.get("result_kind") not in {"validated_progress", "validated_completion"}:
+ continue
+ editorial = parsed.get("editorial_response")
+ if not isinstance(editorial, Mapping) or editorial.get("request_digest") != generation.get("request_digest"):
+ continue
+ proof = {
+ "turn_key": key,
+ "result_kind": "validation_failed",
+ "validation_stage": "host_result_contract",
+ "host_attempt_count": 1,
+ "generation_verified": True,
+ "saved_candidate_verified": True,
+ }
+ return proof, _normalize_report_kernel_candidate(parsed)
+ return None
+
+ def _stopped_report_candidate_recovery(
+ self,
+ actor: Mapping[str, Any],
+ window: Mapping[str, Any],
+ original: Mapping[str, Any],
+ *,
+ key: str,
+ folder: Path,
+ root: Path,
+ ) -> tuple[dict[str, Any], dict[str, Any]] | None:
+ """Recover a stopped report Turn only when its saved final answer already holds a valid candidate."""
+ from ...control_plane.turn_driver.host_candidate import parse_model_json
+ from ...control_plane.turn_driver.journal_store import load_turn_journal, turn_journal_path
+
+ req_path = folder / "editorial-request.json"
+ if not req_path.is_file() or not (folder / "source.json").is_file():
+ return None
+ if (folder / "editorial-response.json").exists() and not (folder / "private-generation.json").exists():
+ return None
+ try:
+ request_digest = json.loads(req_path.read_text(encoding="utf-8")).get("request_digest")
+ except (OSError, ValueError):
+ return None
+ if not isinstance(request_digest, str) or not request_digest:
+ return None
+ journal = load_turn_journal(turn_journal_path(root, goal_id=actor["goal_id"], turn_key=key))
+ if (
+ not isinstance(journal, Mapping)
+ or journal.get("status") != "stopped"
+ or journal.get("result_kind") != "wait"
+ or journal.get("host_attempt_count") != 1
+ or (journal.get("host_result") or {}).get("classification") != "no_typed_host_result"
+ ):
+ return None
+ events = self.store.events_after(actor["session_id"], original["turn_id"], None)
+ stopped_event = any(
+ row.get("kind") == "native_loopx.result"
+ and isinstance(row.get("payload"), Mapping)
+ and row["payload"].get("resume_turn_key") == key
+ and row["payload"].get("status") == "stopped"
+ and row["payload"].get("result_kind") == "wait"
+ for row in events
+ )
+ if not stopped_event:
+ return None
+ for row in reversed(events):
+ if row.get("kind") != "answer.final":
+ continue
+ message = ((row.get("payload") or {}).get("response") or {}).get("message")
+ parsed = parse_model_json(str(message or ""))
+ if not isinstance(parsed, dict) or parsed.get("result_kind") not in {"validated_progress", "validated_completion"}:
+ continue
+ editorial = parsed.get("editorial_response")
+ if not isinstance(editorial, Mapping) or editorial.get("request_digest") != request_digest:
+ continue
+ proof = {
+ "turn_key": key,
+ "result_kind": "wait",
+ "classification": "no_typed_host_result",
+ "host_attempt_count": 1,
+ "generation_verified": True,
+ "saved_candidate_verified": True,
+ }
+ return proof, dict(parsed)
+ return None
+
+ def has_recoverable_stopped_report(
+ self,
+ actor: Mapping[str, Any],
+ window: Mapping[str, Any],
+ todo: Mapping[str, Any],
+ ) -> bool:
+ """Read-only gate check before acquiring the recovery fence."""
+ if todo.get("status") != "open":
+ return False
+ original = report_turn(self.store, actor, window)
+ if not original:
+ return False
+ if any(row.get("kind") == "native_loopx.result" and (row.get("payload") or {}).get("status") == "committed"
+ for row in self.store.events_after(actor["session_id"], original["turn_id"], None)):
+ return False
+ request = original.get("loopx_request") or {}
+ turn_instance_id = request.get("turn_instance_id")
+ if not isinstance(turn_instance_id, str) or not turn_instance_id:
+ return False
+ from ...control_plane.turn_driver.journal_store import find_loopx_turn_key_by_settlement_identity
+ from ..periodic_report.private_generation import attempt_directory
+ from .steward_reports import report_service
+ reports, _ = report_service(self.controller, dict(actor))
+ folder = attempt_directory(reports.root, dict(window))
+ saved_key = find_loopx_turn_key_by_settlement_identity(
+ reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"],
+ todo_id=todo["todo_id"], turn_instance_id=turn_instance_id + ".saved-candidate",
+ )
+ if saved_key is not None and (folder / "private-generation.json").exists():
+ return True
+ key = find_loopx_turn_key_by_settlement_identity(
+ reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"],
+ todo_id=todo["todo_id"], turn_instance_id=turn_instance_id,
+ )
+ return bool(key is not None and self._stopped_report_candidate_recovery(
+ actor, window, original, key=key, folder=folder, root=reports.root,
+ ) is not None)
+
+ @staticmethod
+ def _build_replay_runner(normalized_candidate: Mapping[str, Any]):
+ from ...control_plane.turn_driver.host_candidate import build_result
+
+ replayed = False
+
+ def replay_frozen_candidate(native_request: Mapping[str, Any], _launch_scope: Any) -> dict[str, Any]:
+ nonlocal replayed
+ if replayed:
+ raise ValueError("frozen report candidate replay may execute at most once")
+ replayed = True
+ return build_result(native_request, dict(normalized_candidate), host_name="Codex app-server", allow_completion=True)
+
+ return replay_frozen_candidate
+
+ def _settle_stopped_report_candidate(
+ self,
+ *,
+ actor: Mapping[str, Any],
+ window: Mapping[str, Any],
+ todo: Mapping[str, Any],
+ original: Mapping[str, Any],
+ request: dict[str, Any],
+ observed: dict[str, Any] | None,
+ reports: Any,
+ owner_key: str,
+ folder: Path,
+ original_key: str,
+ stopped_proof: dict[str, Any],
+ raw_candidate: dict[str, Any],
+ ) -> dict[str, Any]:
+ from ...cli import build_parser
+ from ...cli_commands.turn import handle_turn_command
+ from ...control_plane.turn_driver import inspect_loopx_turn_journal
+ from ..periodic_report.private_generation import validate_private_generation
+
+ inspection = inspect_loopx_turn_journal(
+ reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"],
+ turn_key=original_key, retry_failed=True,
+ )
+ admitted = self._admit(
+ request, observed=observed, settlement_recovery=inspection,
+ frozen_contract_recovery=stopped_proof,
+ )
+ if not (folder / "private-generation.json").exists():
+ reports.freeze(request, dict(raw_candidate))
+ generation = validate_private_generation(folder)
+ if raw_candidate["editorial_response"]["request_digest"] != generation["request_digest"]:
+ raise ValueError("the recovered candidate digest does not match the frozen report")
+ if not reports.delivery_current(owner_key, window):
+ raise ValueError("the original report authorization changed before settlement recovery")
+ saved_instance_id = request["turn_instance_id"] + ".saved-candidate"
+ argv = ["turn", "run-once", "--goal-id", actor["goal_id"], "--agent-id", actor["agent_id"],
+ "--project", admitted["workspace_path"], "--host", "generic-cli",
+ "--turn-instance-id", saved_instance_id, "--todo-id", todo["todo_id"], "--execute"]
+ if request.get("validation_command") is not None:
+ argv += ["--validation-command-json", json.dumps(request["validation_command"])]
+ args = build_parser().parse_args(argv)
+ args.host, args.session_scope = "codex-app-server", "agent"
+ result: dict[str, Any] = {}
+ host_runner = self._build_replay_runner(_normalize_report_kernel_candidate(raw_candidate))
+ with self._task_execution_lease(request, original["turn_id"], recovery=True) as lease:
+ handle_turn_command(
+ args, registry_path=self.controller.registry_path,
+ runtime_root_arg=str(reports.root), output_format=lambda *_a, **_kw: "json",
+ print_payload=lambda value, *_a: result.update(value), native_host_runner=host_runner,
+ native_session_binding_resolver=lambda _envelope: {
+ "schema_version": "loopx_turn_session_binding_v0",
+ "goal_id": actor["goal_id"], "agent_id": actor["agent_id"],
+ },
+ native_task_validator=self._report_validator(request),
+ native_completion_lease_resolver=lambda: self._task_lease_proof(request, lease),
+ )
+ return result
+
def recover_report_settlement(self, actor: dict[str, Any], window: dict[str, Any], todo: dict[str, Any]) -> None:
"""Resume only cached, validated native effects of the original report.
@@ -489,18 +829,54 @@ def recover_report_settlement(self, actor: dict[str, Any], window: dict[str, Any
for row in self.store.events_after(actor["session_id"], original["turn_id"], None)):
return
reports, owner_key = report_service(self.controller, actor)
- validate_private_generation(attempt_directory(reports.root, window))
- if not reports.delivery_current(owner_key, window):
- raise ValueError("the original report authorization changed before settlement recovery")
- key = find_loopx_turn_key_by_settlement_identity(reports.root, goal_id=actor["goal_id"],
- agent_id=actor["agent_id"], todo_id=todo["todo_id"], turn_instance_id=request["turn_instance_id"])
+ folder = attempt_directory(reports.root, window)
+ saved_key = find_loopx_turn_key_by_settlement_identity(
+ reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"],
+ todo_id=todo["todo_id"], turn_instance_id=request["turn_instance_id"] + ".saved-candidate",
+ )
+ key = saved_key or find_loopx_turn_key_by_settlement_identity(
+ reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"],
+ todo_id=todo["todo_id"], turn_instance_id=request["turn_instance_id"],
+ )
if key is None:
return
+ if saved_key is None:
+ stopped_recovery = self._stopped_report_candidate_recovery(
+ actor, window, original, key=key, folder=folder, root=reports.root,
+ )
+ if stopped_recovery is not None:
+ result = self._settle_stopped_report_candidate(
+ actor=actor, window=window, todo=todo, original=original, request=request,
+ observed=observed, reports=reports, owner_key=owner_key, folder=folder,
+ original_key=key, stopped_proof=stopped_recovery[0], raw_candidate=stopped_recovery[1],
+ )
+ self.store.append_event(actor["session_id"], original["turn_id"], kind="native_loopx.result", payload=result)
+ return
+ generation = validate_private_generation(folder)
+ if not reports.delivery_current(owner_key, window):
+ raise ValueError("the original report authorization changed before settlement recovery")
inspection = inspect_loopx_turn_journal(reports.root, goal_id=actor["goal_id"],
agent_id=actor["agent_id"], turn_key=key, retry_failed=True)
- admitted = self._admit(request, observed=observed, settlement_recovery=inspection)
+ contract_recovery = (
+ self._frozen_report_contract_recovery(
+ actor, window, original, key=key, generation=generation, root=reports.root,
+ )
+ if (inspection.get("recovery_decision") or {}).get("reinvoke_host") is True
+ else None
+ )
+ admitted = self._admit(
+ request,
+ observed=observed,
+ settlement_recovery=inspection,
+ frozen_contract_recovery=contract_recovery[0] if contract_recovery else None,
+ )
def no_model(*_args):
raise ValueError("report settlement recovery must never invoke a model")
+ host_runner = (
+ self._build_replay_runner(contract_recovery[1])
+ if contract_recovery is not None
+ else no_model
+ )
args = build_parser().parse_args(["turn", "run-once", "--goal-id", actor["goal_id"],
"--agent-id", actor["agent_id"], "--project", admitted["workspace_path"],
"--host", "generic-cli", "--resume-turn-key", key, "--retry-failed-turn", "--execute"])
@@ -510,7 +886,7 @@ def no_model(*_args):
if todo["status"] == "open" else nullcontext(None)) as lease:
handle_turn_command(args, registry_path=self.controller.registry_path,
runtime_root_arg=str(reports.root), output_format=lambda *_a, **_kw: "json",
- print_payload=lambda value, *_a: result.update(value), native_host_runner=no_model,
+ print_payload=lambda value, *_a: result.update(value), native_host_runner=host_runner,
native_session_binding_resolver=lambda _envelope: {"schema_version": "loopx_turn_session_binding_v0",
"goal_id": actor["goal_id"], "agent_id": actor["agent_id"]},
native_task_validator=self._report_validator(request),
@@ -577,6 +953,8 @@ def _prepare_submission(self, binding: dict[str, Any] | None, *, turn_instance_i
return {"turn": existing, "quota_probe": {"original": existing, "binding": binding}}
quota_request = quota_observation["request"]
if quota_request is not None:
+ if _same_report_validator_command(quota_request.get("validation_command"), request.get("validation_command")):
+ quota_request = {**quota_request, "validation_command": request["validation_command"]}
request = quota_request
client_id = native_steward_client_id(actor, request["turn_instance_id"])
existing = None
@@ -844,6 +1222,7 @@ def execute(self, *, session_id: str, turn_id: str, adapter: Any,
from ...cli import build_parser
from ...cli_commands.turn import handle_turn_command
from ...control_plane.turn_driver.host_candidate import extract_turn_authority, render_prompt, parse_model_json, build_result
+ from .antigravity_adapter import AntigravityAdapter
from .codex_adapter import CodexAppServerAdapter
turn = self.store.load_turn(session_id, turn_id)
@@ -878,8 +1257,10 @@ def execute(self, *, session_id: str, turn_id: str, adapter: Any,
monitor = StewardMonitor(self.controller)
todo = next((row for row in monitor.todos(actor) if row["todo_id"] == request["todo_id"]), None)
return monitor.observe(host=self, request=request, turn=turn, context=context, todo=todo)
- if not isinstance(adapter, CodexAppServerAdapter):
+ if local_session and not isinstance(adapter, CodexAppServerAdapter):
raise ValueError("native steward work requires the original Codex app-server adapter")
+ if not local_session and not isinstance(adapter, (CodexAppServerAdapter, AntigravityAdapter)):
+ raise ValueError("native steward work requires the original Codex app-server or Antigravity adapter")
original_thread = adapter.upstream_thread_id
from .steward_reports import report_service
reports, report_owner_key = report_service(self.controller, actor) if report_work else (None, None)
@@ -969,6 +1350,12 @@ def run(native_request: Mapping[str, Any], launch_scope: Any) -> dict[str, Any]:
# Portfolio evidence keeps its original reader and original grant;
# it is data alongside the signed native work, never a new actor.
prompt = render_prompt(authority, allow_completion=bool(report_work or managed_turn or steward_advancement) and not governance_work) + "\nOriginal conversation evidence (data):\n" + json.dumps(context, ensure_ascii=False)
+ if governance_work:
+ prompt += (
+ "\nGovernance contract reminder: Settle the pending autonomous replan by returning `result_kind='replan_required'` (or `'wait'`). "
+ "If you emit `delivery_outcome='outcome_gap'`, you MUST also supply `progress_result_class='blocked'`, `progress_blocker_id`, and `progress_evidence_id`; "
+ "otherwise use `'outcome_progress'` or omit `delivery_outcome`."
+ )
if report_work and not governance_work:
prompt += report_context
if steward_advancement and not governance_work:
@@ -997,7 +1384,8 @@ def run(native_request: Mapping[str, Any], launch_scope: Any) -> dict[str, Any]:
"source_todo_id": basis["definition"]["source_task"]["todo_id"],
"window_start": basis["window"]["start_at"], "window_end": basis["window"]["due_at"],
}, ensure_ascii=False)
- previous_guard = adapter.session._launch_guard
+ guard_owner = adapter.session if isinstance(adapter, CodexAppServerAdapter) else adapter
+ previous_guard = guard_owner._launch_guard
@contextmanager
def launch():
@@ -1022,7 +1410,7 @@ def launch():
raise BuiltInHostError(str(exc), failure_kind="contract_rejected") from exc
yield
- adapter.session._launch_guard = launch
+ guard_owner._launch_guard = launch
try:
if managed_turn and not governance_work:
from ...control_plane.effect_runtime import effect_runtime_result
@@ -1033,7 +1421,7 @@ def launch():
else:
response = adapter.start_turn(prompt, event_sink)
finally:
- adapter.session._launch_guard = previous_guard
+ guard_owner._launch_guard = previous_guard
current() # Revocation/stop/retarget after a long model call wins.
candidate = parse_model_json(str(response.get("message") or ""))
kernel_candidate = dict(candidate) if candidate is not None else None
@@ -1053,8 +1441,9 @@ def launch():
# generation, not the model's choice of progress wording.
# The kernel still runs its independent validator and lease
# fence before closing only this window's generation Todo.
- kernel_candidate["result_kind"] = "validated_completion"
- built = build_result(native_request, kernel_candidate, host_name="Codex app-server",
+ kernel_candidate = _normalize_report_kernel_candidate(kernel_candidate)
+ built = build_result(native_request, kernel_candidate,
+ host_name="Antigravity" if isinstance(adapter, AntigravityAdapter) else "Codex app-server",
**({"allow_completion": True} if (report_work or managed_turn or steward_advancement) and not governance_work else {}))
managed_answer = str(built.get("summary") or "") if (managed_turn or steward_advancement) else ""
return built
diff --git a/loopx/capabilities/steward_executor/__init__.py b/loopx/capabilities/steward_executor/__init__.py
index 5c045c47f4..d1ba32a149 100644
--- a/loopx/capabilities/steward_executor/__init__.py
+++ b/loopx/capabilities/steward_executor/__init__.py
@@ -14,6 +14,7 @@
)
from .machine_defaults import (
ELIGIBLE_ENDPOINTS_FIELD,
+ ENDPOINT_MODELS_FIELD,
FLEXIBLE_SELECTION_POLICY,
PINNED_SELECTION_POLICY,
PREFERRED_SELECTION_POLICY,
@@ -33,6 +34,7 @@
__all__ = [
"ELIGIBLE_ENDPOINTS_FIELD",
+ "ENDPOINT_MODELS_FIELD",
"FLEXIBLE_SELECTION_POLICY",
"MANAGER_ALLOCATION_REASON_CONFIGURED_PREFERENCE",
"MANAGER_ALLOCATION_REASON_FLEXIBLE_FALLBACK",
diff --git a/loopx/capabilities/steward_executor/machine_defaults.py b/loopx/capabilities/steward_executor/machine_defaults.py
index 9d97981709..efa385e67a 100644
--- a/loopx/capabilities/steward_executor/machine_defaults.py
+++ b/loopx/capabilities/steward_executor/machine_defaults.py
@@ -75,6 +75,7 @@
REASONING_EFFORT_FIELD = "executor_reasoning_effort"
SELECTION_POLICY_FIELD = "selection_policy"
ELIGIBLE_ENDPOINTS_FIELD = "eligible_endpoints"
+ENDPOINT_MODELS_FIELD = "endpoint_models"
PREFERRED_SELECTION_POLICY = "preferred"
PINNED_SELECTION_POLICY = "pinned"
FLEXIBLE_SELECTION_POLICY = "flexible"
@@ -132,7 +133,7 @@ def normalize_steward_executor_machine_defaults(
"steward_executor must use "
f"{STEWARD_EXECUTOR_SCHEMA_V0} or {STEWARD_EXECUTOR_SCHEMA}"
)
- v1_fields = {SELECTION_POLICY_FIELD, ELIGIBLE_ENDPOINTS_FIELD}
+ v1_fields = {SELECTION_POLICY_FIELD, ELIGIBLE_ENDPOINTS_FIELD, ENDPOINT_MODELS_FIELD}
unknown = sorted(
set(raw)
- {
@@ -205,11 +206,22 @@ def normalize_steward_executor_machine_defaults(
raise ValueError(
"steward_executor.eligible_endpoints is only valid for flexible selection"
)
- return {
- **normalized,
- SELECTION_POLICY_FIELD: selection_policy,
- ELIGIBLE_ENDPOINTS_FIELD: eligible_endpoints,
- }
+ raw_ep = raw.get(ENDPOINT_MODELS_FIELD)
+ if raw_ep is not None and not isinstance(raw_ep, Mapping):
+ raise TypeError("steward_executor.endpoint_models must be an object")
+ ep_models: dict[str, dict[str, str | None]] = {}
+ for ep_key, ep_val in (raw_ep or {}).items():
+ ep = str(ep_key or "").strip()
+ if ep not in supported or not isinstance(ep_val, Mapping) or set(ep_val) - {"model", "reasoning_effort"}:
+ raise ValueError(f"steward_executor.endpoint_models.{ep or ep_key} is invalid")
+ ep_m = _optional_text(ep_val.get("model"), field=f"endpoint_models.{ep}.model")
+ ep_e = _optional_text(ep_val.get("reasoning_effort"), field=f"endpoint_models.{ep}.reasoning_effort")
+ if ep_e is not None and ep_e not in supported_efforts:
+ raise ValueError("steward_executor.endpoint_models reasoning_effort must be one of " + ", ".join(supported_efforts))
+ if ep_m is not None or ep_e is not None:
+ ep_models[ep] = {"model": ep_m, "reasoning_effort": ep_e}
+ return {**normalized, SELECTION_POLICY_FIELD: selection_policy, ELIGIBLE_ENDPOINTS_FIELD: eligible_endpoints,
+ **({ENDPOINT_MODELS_FIELD: ep_models} if ep_models else {})}
def steward_executor_machine_configuration_namespace() -> (
@@ -260,6 +272,7 @@ def _projection(
reasoning_effort: str | None = None,
selection_policy: str = PREFERRED_SELECTION_POLICY,
eligible_endpoints: list[str] | None = None,
+ endpoint_models: dict[str, dict[str, str | None]] | None = None,
repair: str = "",
) -> Payload:
return {
@@ -272,6 +285,7 @@ def _projection(
REASONING_EFFORT_FIELD: reasoning_effort,
SELECTION_POLICY_FIELD: selection_policy,
ELIGIBLE_ENDPOINTS_FIELD: list(eligible_endpoints or []),
+ **({ENDPOINT_MODELS_FIELD: dict(endpoint_models)} if endpoint_models else {}),
**({"repair": repair} if repair else {}),
}
@@ -313,22 +327,16 @@ def effective_steward_executor_defaults(
"machine_configuration.namespaces.steward_executor must be an object"
)
normalized = normalize_steward_executor_machine_defaults(raw)
- raw_model = normalized[MODEL_FIELD]
- raw_effort = normalized[REASONING_EFFORT_FIELD]
- raw_eligible = normalized.get(ELIGIBLE_ENDPOINTS_FIELD)
+ raw_model, raw_effort = normalized[MODEL_FIELD], normalized[REASONING_EFFORT_FIELD]
+ raw_eligible, raw_endpoint_models = normalized.get(ELIGIBLE_ENDPOINTS_FIELD), normalized.get(ENDPOINT_MODELS_FIELD)
return _projection(
- status="ready",
- source="machine_configuration",
- revision=machine_configuration_revision(normalized),
+ status="ready", source="machine_configuration", revision=machine_configuration_revision(normalized),
executor_endpoint=str(normalized[EXECUTOR_ENDPOINT_FIELD]),
model=raw_model if isinstance(raw_model, str) else None,
reasoning_effort=raw_effort if isinstance(raw_effort, str) else None,
- selection_policy=str(
- normalized.get(SELECTION_POLICY_FIELD) or PREFERRED_SELECTION_POLICY
- ),
- eligible_endpoints=[str(item) for item in raw_eligible]
- if isinstance(raw_eligible, list)
- else [],
+ selection_policy=str(normalized.get(SELECTION_POLICY_FIELD) or PREFERRED_SELECTION_POLICY),
+ eligible_endpoints=[str(item) for item in raw_eligible] if isinstance(raw_eligible, list) else [],
+ endpoint_models=dict(raw_endpoint_models) if isinstance(raw_endpoint_models, dict) and raw_endpoint_models else None,
)
diff --git a/loopx/chat/coordination.py b/loopx/chat/coordination.py
index ec58cd8fa3..55b79e6e77 100644
--- a/loopx/chat/coordination.py
+++ b/loopx/chat/coordination.py
@@ -400,7 +400,15 @@ def prepare_turn_context(controller, adapter, session, turn_id, event_sink, *, s
)
if execution_catalog["bindings"] or not execution_catalog["available"]:
context["context_execution"] = execution_catalog
- if isinstance(adapter, CodexAppServerAdapter):
+ from ..capabilities.native_chat.antigravity_adapter import AntigravityAdapter
+ current_turn = controller.store.load_turn(session_id, turn_id) or {}
+ antigravity_report = (
+ not isinstance(adapter, CodexAppServerAdapter)
+ and isinstance(adapter, AntigravityAdapter)
+ and (current_turn.get("loopx_request") or {}).get("operation") == "native_turn"
+ and (current_turn.get("loopx_request") or {}).get("work_kind") == "report"
+ )
+ if isinstance(adapter, CodexAppServerAdapter) or antigravity_report:
from ..capabilities.manager_context.inspection import ManagerInspection, manager_index
from ..chat_manager_context import manager_authorization_scope_is_current
expected_scope_id = context.get("authorization_scope_id")
@@ -438,19 +446,22 @@ def scope_valid() -> bool:
session_id, turn_id, kind="manager.evidence_read", payload=result,
),
)
- adapter.session.read_tool_handler = inspection.read
- adapter._managed_read_turn_guard = None
- if (session.get("managed_agent_id") and turn.get("origin") == "web"
- and not turn.get("loopx_request") and not turn.get("loopx_execution")
- and not session.get("loopx_tools") and not (session.get("loopx_mode") or {}).get("enabled")
- and not adapter.session.execution_mode and not adapter.session.read_only_review
- and getattr(adapter, "goal_driver", None) is None
- and scope["kind"] == "owner_goal" and session.get("upstream_mode") == "chat"):
- adapter.session.read_tool_handler = _GoalSessionCheckPreparation(
- controller, adapter, session, turn_id, inspection.read)
- adapter._managed_read_turn_guard = adapter.session.read_tool_handler._current
- context["evidence_sources"] = inspection.sources()
- context = manager_index(context, status_on_demand=status_on_demand)
+ if isinstance(adapter, CodexAppServerAdapter):
+ adapter.session.read_tool_handler = inspection.read
+ adapter._managed_read_turn_guard = None
+ if (session.get("managed_agent_id") and turn.get("origin") == "web"
+ and not turn.get("loopx_request") and not turn.get("loopx_execution")
+ and not session.get("loopx_tools") and not (session.get("loopx_mode") or {}).get("enabled")
+ and not adapter.session.execution_mode and not adapter.session.read_only_review
+ and getattr(adapter, "goal_driver", None) is None
+ and scope["kind"] == "owner_goal" and session.get("upstream_mode") == "chat"):
+ adapter.session.read_tool_handler = _GoalSessionCheckPreparation(
+ controller, adapter, session, turn_id, inspection.read)
+ adapter._managed_read_turn_guard = adapter.session.read_tool_handler._current
+ context["evidence_sources"] = inspection.sources()
+ context = manager_index(context, status_on_demand=status_on_demand)
+ else:
+ adapter._steward_report_reader = inspection.read
from ..capabilities.native_chat.external_conversations import ChatExternalConversations
recovery = ChatExternalConversations(controller).recovery_context(
session, controller.store.load_turn(session_id, turn_id) or {}) if scope.get("bound_steward") else None
diff --git a/loopx/chat/session_open.py b/loopx/chat/session_open.py
index 7185aedd8d..615baf76d7 100644
--- a/loopx/chat/session_open.py
+++ b/loopx/chat/session_open.py
@@ -853,7 +853,7 @@ def check_route_configuration() -> None:
if (
defaults.get("status") != "ready"
or defaults.get("selection_policy") != "flexible"
- or defaults.get("executor_endpoint") != "antigravity"
+ or defaults.get("executor_endpoint") not in {"antigravity", "codex"}
or set(defaults.get("eligible_endpoints") or []) != {"antigravity", "codex"}
or defaults.get("configuration_revision") != allocation.get("configuration_revision")
):
diff --git a/loopx/chat_action_normalization.py b/loopx/chat_action_normalization.py
index c1cea4a7c1..8746b34478 100644
--- a/loopx/chat_action_normalization.py
+++ b/loopx/chat_action_normalization.py
@@ -7,6 +7,7 @@
from typing import Any, Mapping
from .agent_registry import registered_agent_ids_for_goal
+from .capabilities.issue_fix.repository_snapshot import _REPO_PATTERN
from .control_plane.content_digest import BARE_SHA256_PATTERN
from .control_plane.runtime.time import now_utc, parse_timestamp, utc_isoformat
from .control_plane.todos.contract import require_supported_todo_resume_when
@@ -786,7 +787,7 @@ def _normalize(
}
if values.get("task_repository"):
raw_repo = _text(values["task_repository"], field="task_repository", limit=200)
- if re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", raw_repo):
+ if _REPO_PATTERN.fullmatch(raw_repo):
raw_repo = f"git:github.com/{raw_repo}"
result["task_repository"] = raw_repo
stop_cond_raw = values.get("stop_condition")
@@ -843,7 +844,7 @@ def _normalize(
result["target_key"] = _opaque(values["target_key"], field="target_key")
if values.get("task_repository"):
raw_repo = _text(values["task_repository"], field="task_repository", limit=200)
- if re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", raw_repo):
+ if _REPO_PATTERN.fullmatch(raw_repo):
raw_repo = f"git:github.com/{raw_repo}"
result["task_repository"] = raw_repo
if values.get("cadence"):
@@ -881,7 +882,7 @@ def _normalize(
},
)
repository = str(values.get("repository") or "").strip()
- if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", repository):
+ if not _REPO_PATTERN.fullmatch(repository):
raise ValueError("pr.merge repository must be /")
raw_num = values.get("pr_number")
if isinstance(raw_num, bool) or not isinstance(raw_num, int) or raw_num < 1:
diff --git a/loopx/chat_manager.py b/loopx/chat_manager.py
index eb25e82423..0d877ef9d2 100644
--- a/loopx/chat_manager.py
+++ b/loopx/chat_manager.py
@@ -364,6 +364,32 @@ def _machine_default_text(
return str(machine_defaults.get(field) or "").strip()
+def _machine_default_endpoint_text(
+ machine_defaults: Mapping[str, Any] | None,
+ endpoint: str | None,
+ field: str,
+) -> str:
+ """Return one machine-configured model or reasoning_effort for an endpoint."""
+
+ if not isinstance(machine_defaults, Mapping):
+ return ""
+ configured_endpoint = _machine_default_text(machine_defaults, "executor_endpoint")
+ if not endpoint or not configured_endpoint or endpoint == configured_endpoint:
+ primary_value = _machine_default_text(machine_defaults, f"executor_{field}")
+ if primary_value:
+ return primary_value
+ target = str(endpoint or configured_endpoint or "").strip()
+ if not target:
+ return ""
+ per_endpoint = machine_defaults.get("endpoint_models")
+ if not isinstance(per_endpoint, Mapping):
+ return ""
+ entry = per_endpoint.get(target)
+ if not isinstance(entry, Mapping):
+ return ""
+ return str(entry.get(field) or "").strip()
+
+
def _resolve_manager_endpoint(
environ: dict[str, str] | None = None,
*,
@@ -530,7 +556,6 @@ def manager_executor_allocation(
environ, endpoint=endpoint, machine_defaults=defaults
)
configured_endpoint = _machine_default_text(defaults, "executor_endpoint")
- defaults_apply = not configured_endpoint or endpoint == configured_endpoint
return normalize_manager_executor_allocation({
"schema_version": MANAGER_EXECUTOR_ALLOCATION_SCHEMA_VERSION,
"selection_policy": policy,
@@ -550,14 +575,10 @@ def manager_executor_allocation(
"model_source": model_source,
"reasoning_effort": model_config["reasoning_effort"],
"configured_model": (
- _machine_default_text(defaults, "executor_model") or None
- if defaults_apply
- else None
+ _machine_default_endpoint_text(defaults, endpoint, "model") or None
),
"configured_reasoning_effort": (
- _machine_default_text(defaults, "executor_reasoning_effort") or None
- if defaults_apply
- else None
+ _machine_default_endpoint_text(defaults, endpoint, "reasoning_effort") or None
),
})
@@ -866,18 +887,17 @@ def manager_model_resolution(
vendor default. A credential never picks a model.
"""
- configured = _machine_default_text(machine_defaults, "executor_model")
- configured_endpoint = _machine_default_text(
- machine_defaults, "executor_endpoint"
+ resolved_endpoint = (
+ endpoint or _resolve_manager_endpoint(environ, machine_defaults=machine_defaults)[0]
)
- if configured and (not endpoint or not configured_endpoint or endpoint == configured_endpoint):
+ configured = _machine_default_endpoint_text(
+ machine_defaults, resolved_endpoint, "model"
+ )
+ if configured:
return configured, MANAGER_MODEL_SOURCE_MACHINE_CONFIGURATION
override = env_text(MANAGER_MODEL_ENV_VAR, environ)
if override:
return override, MANAGER_MODEL_SOURCE_ENV_OVERRIDE
- resolved_endpoint = (
- endpoint or _resolve_manager_endpoint(environ, machine_defaults=machine_defaults)[0]
- )
if MANAGER_ENDPOINT_KINDS.get(resolved_endpoint) == MANAGER_EXECUTOR_KIND_MANAGED:
profile = managed_execution_profile(environ)
return str(profile["model"]), MANAGER_MODEL_SOURCE_MANAGED_PROFILE
@@ -949,9 +969,10 @@ def manager_session_quota_route(controller: Any, session: Mapping[str, Any]) ->
"""
defaults = steward_machine_defaults(controller)
previous = session.get("manager_executor_allocation") or {}
+ primary = _machine_default_text(defaults, "executor_endpoint")
if (not defaults or defaults.get("status") != "ready"
or _manager_selection_policy(defaults) != FLEXIBLE_SELECTION_POLICY
- or _machine_default_text(defaults, "executor_endpoint") != "antigravity"
+ or primary not in {"antigravity", "codex"}
or set(_manager_eligible_endpoints(defaults)) != {"antigravity", "codex"}
or previous.get("allocation_reason") == MANAGER_ALLOCATION_REASON_USER_EXPLICIT
or session.get("requested_agent_model_id") or session.get("requested_agent_reasoning_effort")
@@ -960,37 +981,59 @@ def manager_session_quota_route(controller: Any, session: Mapping[str, Any]) ->
native = session if session.get("agent_id") == "antigravity" else next((row for row in
reversed(session.get("upstream_context_history") or []) if row.get("agent_id") == "antigravity"), None)
try:
- from .capabilities.native_chat.antigravity_adapter import AntigravityAdapter, ANTIGRAVITY_DEFAULT_MODEL
+ from .capabilities.native_chat.antigravity_adapter import (
+ ANTIGRAVITY_DEFAULT_MODEL,
+ AntigravityAdapter,
+ )
from loopx_codex_provider_routing.native import NativeAccounts
environment = dict(operator_credential_resolution(controller)["environ"])
- codex_environment = {key: value for key, value in environment.items()
+ clean_environment = {key: value for key, value in environment.items()
if key not in (MANAGER_MODEL_ENV_VAR, MANAGER_REASONING_EFFORT_ENV_VAR)}
accounts = NativeAccounts(controller.coordination_runtime_root)
- route_inputs = dict(codex_home=controller.codex_home, primary="antigravity",
+ route_inputs = dict(codex_home=controller.codex_home, primary=primary,
pool=_manager_eligible_endpoints(defaults), current=str(session["agent_id"]),
blocked_primary=session.get("manager_quota_block"))
if callable(getattr(accounts, "model_transport", None)):
- route_inputs["model"] = manager_model_resolution(codex_environment, endpoint="codex", machine_defaults=defaults)[0]
+ route_inputs["model"] = manager_model_resolution(clean_environment, endpoint="codex", machine_defaults=defaults)[0]
capacity = None
- # Explicit API primary does not depend on an unrelated subscription
- # transport being up. The same owner holds when that role is absent.
- route = accounts.steward_route(**route_inputs, primary_capacity=None)
- if route.get("endpoint") is None and native:
- capacity = AntigravityAdapter.metadata_review_capacity(
- cascade_id=str(native.get("upstream_thread_id") or native.get("thread_id")),
- model_id=str(native.get("agent_model_id") or ANTIGRAVITY_DEFAULT_MODEL))
- route = accounts.steward_route(**route_inputs, primary_capacity=capacity)
+ if primary == "antigravity":
+ # Explicit API primary does not depend on an unrelated subscription
+ # transport being up. The same owner holds when that role is absent.
+ route = accounts.steward_route(**route_inputs, primary_capacity=None)
+ if route.get("endpoint") is None and native:
+ capacity = AntigravityAdapter.metadata_review_capacity(
+ cascade_id=str(native.get("upstream_thread_id") or native.get("thread_id")),
+ model_id=str(native.get("agent_model_id") or ANTIGRAVITY_DEFAULT_MODEL))
+ route = accounts.steward_route(**route_inputs, primary_capacity=capacity)
+ else:
+ route = accounts.steward_route(**route_inputs, primary_capacity=None, fallback_capacity=None)
+ if route.get("endpoint") is None:
+ target_ag_config = manager_model_config(
+ clean_environment, endpoint="antigravity", machine_defaults=defaults,
+ )
+ capacity = AntigravityAdapter.endpoint_model_capacity(
+ model_id=target_ag_config["model"],
+ reasoning_effort=target_ag_config["reasoning_effort"],
+ )
+ if capacity is not None:
+ route = accounts.steward_route(
+ **route_inputs, primary_capacity=None, fallback_capacity=capacity,
+ )
endpoint = route.get("endpoint")
if endpoint is None:
return None
capabilities = controller.capabilities()
if not any(row.get("agent_id") == endpoint and row.get("available") is True for row in capabilities):
return None
- if endpoint != "antigravity":
- environment.pop("LOOPX_MANAGER_MODEL", None)
- environment.pop("LOOPX_MANAGER_REASONING_EFFORT", None)
- allocation = manager_executor_allocation(controller, machine_defaults=defaults, environ=environment,
- endpoint_capabilities=[{**row, "available": row.get("agent_id") == endpoint} for row in capabilities])
+ if endpoint != primary:
+ environment.pop(MANAGER_MODEL_ENV_VAR, None)
+ environment.pop(MANAGER_REASONING_EFFORT_ENV_VAR, None)
+ allocation = manager_executor_allocation(
+ controller,
+ machine_defaults=defaults,
+ environ=environment,
+ endpoint_capabilities=[{**row, "available": row.get("agent_id") == endpoint} for row in capabilities],
+ )
return {"allocation": allocation, "blocked_primary": route.get("blocked_primary"),
"observed_at": capacity["observed_at"] if capacity else time.time()}
except Exception as exc:
@@ -1018,8 +1061,8 @@ def manager_session_model_allocation(
if (session.get("goal_id") != MANAGER_AGENT_GOAL_ID or defaults is None
or defaults.get("status") != "ready"
or _machine_default_text(defaults, "executor_endpoint") != endpoint
- or not any(_machine_default_text(defaults, name) for name in
- ("executor_model", "executor_reasoning_effort"))):
+ or not any(_machine_default_endpoint_text(defaults, str(endpoint), name) for name in
+ ("model", "reasoning_effort"))):
return None
proposed = manager_executor_allocation(controller, endpoint, machine_defaults=defaults,
environ=operator_credential_resolution(controller)["environ"])
@@ -1153,18 +1196,18 @@ def manager_model_config(
environ, machine_defaults=machine_defaults
)[0]
model, _source = manager_model_resolution(
- environ, endpoint=endpoint, machine_defaults=machine_defaults
- )
- configured_endpoint = _machine_default_text(
- machine_defaults, "executor_endpoint"
+ environ,
+ endpoint=endpoint,
+ machine_defaults=machine_defaults,
)
- effort = (
- _machine_default_text(machine_defaults, "executor_reasoning_effort")
- if not configured_endpoint or endpoint == configured_endpoint
- else ""
+ effort = _machine_default_endpoint_text(
+ machine_defaults, endpoint, "reasoning_effort"
)
if not effort:
effort = env_text(MANAGER_REASONING_EFFORT_ENV_VAR, environ)
+ if not effort and endpoint == "antigravity":
+ tier = model.rsplit("-", 1)[-1]
+ effort = tier if tier in {"low", "medium", "high"} else "high"
if not effort and MANAGER_ENDPOINT_KINDS.get(endpoint) == MANAGER_EXECUTOR_KIND_MANAGED:
effort = str(managed_execution_profile(environ)["reasoning_effort"])
effort = effort or MANAGER_REASONING_EFFORT_DEFAULT
diff --git a/loopx/chat_runtime.py b/loopx/chat_runtime.py
index 29e1ce9f2f..acfa28907a 100644
--- a/loopx/chat_runtime.py
+++ b/loopx/chat_runtime.py
@@ -100,6 +100,9 @@
"Your conversation is saved; this turn was not retried automatically. "
"You can continue with a follow-up in this session."
)
+WORKER_EXITED_CODE = "execution_worker_exited"
+BEFORE_DISPATCH_CODE = "execution_worker_exited_before_dispatch"
+BEFORE_DISPATCH_MESSAGE = "The LoopX Chat worker exited before the original request was dispatched."
WORKER_EXITED_MESSAGE = (
"The LoopX Chat execution worker exited before this turn finished. "
"Your conversation is saved; execution may have occurred. "
@@ -473,20 +476,13 @@ def submit_turn(
self.resume_session(session_id=session_id, work_dir=work_dir, objective=objective)
return self.store.load_turn(session_id, str(accepted.turn["turn_id"])) or accepted.turn, accepted.created
if accepted.dispatch_required:
- try:
- with commit():
- self._start_accepted_turn_worker(
- session_id=session_id, turn_id=str(accepted.turn["turn_id"]), message=message,
- attachments=attachments or [], adapter=adapter, loopx_execution=loopx_execution)
- except Exception:
- with self.lock:
- has_live_worker = any(
- sid == session_id and not done.is_set()
- for (sid, _), done in self.turn_done_events.items()
- )
- if not has_live_worker:
- self._drop_owned_adapter(session_id, adapter)
- raise
+ # The session owns the prepared adapter. A retry can select it
+ # before registering its worker, so a failed dispatch attempt only
+ # releases its own worker slot.
+ with commit():
+ self._start_accepted_turn_worker(
+ session_id=session_id, turn_id=str(accepted.turn["turn_id"]), message=message,
+ attachments=attachments or [], adapter=adapter, loopx_execution=loopx_execution)
return accepted.turn, accepted.created
def _start_accepted_turn_worker(
@@ -764,16 +760,23 @@ def recover_exited_turn(self, session_id: str, turn_id: str | None = None) -> No
# Stop deliberately retires the execution worker before its
# caller commits `interrupted`; a free fence is expected here.
return
- if managed_turn_recovery_allowed(
- session, turn, managed=bool(session and session.get("session_mode") == CHAT_SESSION_MODE_MANAGED),
- owner_dead=True,
- ):
+ managed = bool(session and session.get("session_mode") == CHAT_SESSION_MODE_MANAGED)
+ if (managed and turn and turn.get("status") == "completing"
+ and not turn.get("completion_id")):
+ # All response effects preceded the durable reservation. Finish its
+ # idempotent transcript/events under the original execution fence;
+ # the running service can recover it without a restart or replay.
+ self.store.finalize_managed_turn_completion(session_id, turn_id)
+ return
+ if managed_turn_recovery_allowed(session, turn, managed=managed, owner_dead=True):
code, message = (("server_restarted", SERVER_RESTARTED_MESSAGE)
- if state == LOCK_HOLDER_DEAD else ("runtime_error", WORKER_EXITED_MESSAGE))
- self._fail_turn(
- session_id, turn_id, code, message,
- status="failed", expected_statuses={"starting", "running", "interrupting"},
- )
+ if state == LOCK_HOLDER_DEAD else (WORKER_EXITED_CODE, WORKER_EXITED_MESSAGE))
+ statuses = {"starting", "running", "interrupting"}
+ if (session and session.get("steward_context") and turn and turn.get("status") == "starting"
+ and turn.get("execution_dispatch") == "pending"):
+ code, message = BEFORE_DISPATCH_CODE, BEFORE_DISPATCH_MESSAGE
+ statuses = {"starting"} # A concurrent stop must win over replay.
+ self._fail_turn(session_id, turn_id, code, message, status="failed", expected_statuses=statuses)
else:
# An earlier recovery may have persisted the exact failed result
# and then lost its event, message or claim write. The typed
@@ -782,7 +785,9 @@ def recover_exited_turn(self, session_id: str, turn_id: str | None = None) -> No
self.store.complete_recovered_terminalization(
session_id, turn_id, session, turn,
recovery_messages={"server_restarted": SERVER_RESTARTED_MESSAGE,
- "runtime_error": WORKER_EXITED_MESSAGE},
+ WORKER_EXITED_CODE: WORKER_EXITED_MESSAGE,
+ BEFORE_DISPATCH_CODE: BEFORE_DISPATCH_MESSAGE,
+ "runtime_error": WORKER_EXITED_MESSAGE}, # Legacy exact record.
)
def _run_turn(
@@ -808,6 +813,7 @@ def _run_turn(
# Turn was never dispatched, so the single-flight guard must be given
# back here rather than left holding the key, which would make every
# later dispatch of this same Turn a silent no-op.
+ phase = "start"
try:
with try_exclusive_file_lock(
self.store.turn_execution_lock_path(session_id, turn_id),
@@ -821,6 +827,7 @@ def _run_turn(
expected_statuses={"queued"},
status="starting",
started_at=utc_now(),
+ **({"execution_dispatch": "pending"} if isinstance(adapter, CodexAppServerAdapter) else {}),
)
if started_turn is None:
with self.lock:
@@ -831,6 +838,7 @@ def _run_turn(
# Reuse one code revision across this worker's typed decisions.
# This caches no Session, Turn or authority facts: each checkpoint
# still reads current state, including after a long provider wait.
+ phase = "execute_or_finalize"
with effect_runtime_request_scope():
retry_binding: dict[str, str] = {}
while self._run_started_turn(
@@ -839,6 +847,12 @@ def _run_turn(
loopx_execution=loopx_execution, retry_binding=retry_binding,
):
pass
+ except OSError as exc:
+ # The queue will reconcile this Turn. Keep a correlated diagnostic
+ # without logging exception text (which may contain private paths).
+ logging.getLogger(__name__).warning(
+ "Chat worker I/O failure: turn=%s phase=%s errno=%s", turn_id, phase, exc.errno)
+ raise
finally:
self._release_done_event(key, done_event)
@@ -911,6 +925,7 @@ def event_sink(kind: str, payload: dict[str, Any]) -> None:
if kind == "native_goal.status":
self.store.update_session(session_id, native_goal=payload)
+ completion_reserved = False
try:
if execution_ended():
return
@@ -952,6 +967,10 @@ def event_sink(kind: str, payload: dict[str, Any]) -> None:
if conversation_scope(session)["kind"] == "owner_goal"
else None
)
+ else:
+ from .capabilities.native_chat.antigravity_adapter import AntigravityAdapter
+ if isinstance(adapter, AntigravityAdapter):
+ adapter._steward_report_reader = None
native_command = None if current_turn.get("origin") == "peer" else parse_native_goal_command(message)
if native_command is not None:
validate_goal_chat(session, attachments)
@@ -977,6 +996,12 @@ def event_sink(kind: str, payload: dict[str, Any]) -> None:
adapter.team_plan_context = team_plan_context
if execution_ended():
return
+ # Seal against replay before invoking anything that can execute the
+ # original input, even if it never reports an upstream Turn ID.
+ if current_turn.get("execution_dispatch") == "pending" and self.store.update_turn(
+ session_id, turn_id, expected_statuses={"starting", "running"},
+ execution_dispatch="committed") is None:
+ return
if native_loopx:
from .capabilities.native_chat.steward_turn import StewardTurnHost
response = StewardTurnHost(self).execute(session_id=session_id, turn_id=turn_id,
@@ -1123,6 +1148,7 @@ def record_owner_resume() -> None:
)
if completed_turn is None:
return
+ completion_reserved = True
if adapter.upstream_thread_id != str((self.store.load_session(session_id) or {}).get("upstream_thread_id") or ""):
self.store.update_session(session_id, upstream_thread_id=adapter.upstream_thread_id)
if not native_loopx and scope.get("bound_steward") is True:
@@ -1166,6 +1192,16 @@ def record_owner_resume() -> None:
last_error_code="transport_disconnected",
)
except Exception as exc: # noqa: BLE001 - preserve compact runtime failure.
+ if isinstance(exc, OSError) and (self.store.load_turn(session_id, turn_id) or {}).get("execution_dispatch") == "pending":
+ # The exact execution-fence owner will reconcile this I/O loss
+ # with the durable dispatch marker instead of a generic failure.
+ raise
+ if completion_reserved and isinstance(exc, OSError):
+ # The answer already exists. Its lifecycle owner can finish
+ # publication without turning this into an execution failure.
+ logging.getLogger(__name__).warning(
+ "Chat worker I/O failure: turn=%s phase=publish_saved_answer errno=%s", turn_id, exc.errno)
+ return
event_buffer.close()
self._fail_turn(session_id, turn_id, "runtime_error", str(exc), status="failed")
finally:
diff --git a/loopx/chat_store.py b/loopx/chat_store.py
index 64fff16914..2dc43e81d3 100644
--- a/loopx/chat_store.py
+++ b/loopx/chat_store.py
@@ -523,7 +523,7 @@ def update_session(self, session_id: str, *, expected_statuses: set[str] | None
and str(active.get("client_turn_id")).endswith(
"-metadata-1" if metadata_review else "-review-1" if answer_review_admission else "-quota-1")))
or allocation["selection_policy"] != "flexible"
- or allocation["configured_endpoint"] != "antigravity"
+ or allocation["configured_endpoint"] not in {"antigravity", "codex"}
or set(allocation["eligible_endpoints"]) != {"antigravity", "codex"}
or endpoint not in {"antigravity", "codex"} or endpoint == payload.get("agent_id")
or changes.get("executor_endpoint_id") != endpoint or allocation["executor_endpoint"] != endpoint
@@ -1313,13 +1313,25 @@ def update_turn(
"delta_count", "sse_reconnect_count", "expires_at", "host_claim_id",
"completion_id", "loopx_execution", "loopx_request",
"collaboration_answer",
- "native_context_preparation",
+ "native_context_preparation", "execution_dispatch",
}
unknown = set(changes) - allowed
if unknown:
raise ValueError(f"unsupported chat turn fields: {sorted(unknown)}")
if expected_statuses is not None and payload.get("status") not in expected_statuses:
return None
+ if "execution_dispatch" in changes:
+ previous = payload.get("execution_dispatch")
+ dispatch = changes["execution_dispatch"]
+ if (dispatch == "pending" and previous is None and payload.get("status") == "queued"
+ and changes.get("status") == "starting"):
+ # This is established in the same write that starts the worker.
+ # Legacy records never acquire evidence about whether they dispatched.
+ if payload.get("origin") != "lark" or not str(payload.get("client_turn_id") or "").startswith("external-"):
+ changes.pop("execution_dispatch")
+ elif (dispatch != "committed" or previous not in {"pending", "committed"}
+ or payload.get("status") not in {"starting", "running"} or "status" in changes):
+ raise ValueError("invalid chat execution dispatch transition")
if "native_context_preparation" in changes:
observation = changes["native_context_preparation"]
flags = {"native_checkpoint_requested", "working_checkpoint_available", "manager_context_migration",
diff --git a/loopx/cli_commands/quota.py b/loopx/cli_commands/quota.py
index 22985998b1..5f6acd8a2c 100644
--- a/loopx/cli_commands/quota.py
+++ b/loopx/cli_commands/quota.py
@@ -259,6 +259,7 @@ def _prepare_quota_result_context(
*,
registry_path: Path,
heartbeat_turn_id: str | None,
+ attach_recall: bool = True,
) -> tuple[dict[str, object], dict[str, Any]]:
from .quota_result_delivery import prepare_quota_result_context
@@ -267,6 +268,7 @@ def _prepare_quota_result_context(
args,
registry_path=registry_path,
heartbeat_turn_id=heartbeat_turn_id,
+ attach_recall=attach_recall,
)
diff --git a/loopx/cli_commands/quota_capture.py b/loopx/cli_commands/quota_capture.py
index ef2c636b09..a33c355c74 100644
--- a/loopx/cli_commands/quota_capture.py
+++ b/loopx/cli_commands/quota_capture.py
@@ -22,11 +22,13 @@
context_index_fields as _context_index_fields,
write_initial_context_read_reference,
validate_handoff_socket, context_capture_read_command,
- write_context_read_reference, private_capture_uid, open_regular_capture,
+ write_context_read_reference,
+ private_capture_uid as private_capture_uid,
+ open_regular_capture as open_regular_capture,
)
from ..control_plane.turn_driver.native_context_profile import (
NATIVE_CONTEXT_FIRST_READ_FALLBACK_BYTES, NATIVE_CONTEXT_PAGE_BYTES,
- native_context_page_budget,
+ native_context_page_budget as native_context_page_budget,
)
from .quota_context_reader import (
budgeted_ready_opening,
@@ -234,10 +236,60 @@ def quota_context_surface(payload: dict[str, Any]) -> str:
return "turn_envelope" if version == TURN_ENVELOPE_SCHEMA_VERSION else "quota_packet"
+def _saved_decision_reference(
+ directory: Path | None, *, expected_sha256: str | None = None,
+) -> dict[str, str] | None:
+ """Verify the just-captured private decision file before staging replan views."""
+ if directory is None:
+ return None
+ candidate = directory / "decision.json"
+ if not candidate.is_file():
+ return None
+ import hashlib
+ import os
+ from ..control_plane.content_digest import BARE_SHA256_PATTERN
+ from ..control_plane.effect_runtime import MAX_REQUEST_BYTES
+ from ..control_plane.turn_driver.decision_capture import (
+ open_regular_capture,
+ private_capture_supported,
+ private_capture_uid,
+ )
+
+ if not private_capture_supported():
+ return None
+ if expected_sha256 is not None and BARE_SHA256_PATTERN.fullmatch(expected_sha256) is None:
+ return None
+ try:
+ uid = private_capture_uid()
+ resolved = candidate.expanduser().absolute()
+ resolved = resolved.parent.resolve(strict=True) / resolved.name
+ if resolved.is_symlink():
+ return None
+ descriptor = open_regular_capture(resolved)
+ with os.fdopen(descriptor, "rb") as stream:
+ metadata = os.fstat(stream.fileno())
+ if (
+ metadata.st_uid != uid
+ or metadata.st_mode & 0o077
+ or metadata.st_size > MAX_REQUEST_BYTES
+ ):
+ return None
+ raw = stream.read(metadata.st_size + 1)
+ except (OSError, ValueError):
+ return None
+ if len(raw) > MAX_REQUEST_BYTES:
+ return None
+ digest = hashlib.sha256(raw).hexdigest()
+ if expected_sha256 is not None and digest != expected_sha256:
+ return None
+ return {"path": str(resolved), "sha256": digest}
+
+
def project_native_quota_context(payload: dict[str, Any], args: argparse.Namespace, *,
capture_directory: Path, capture_id: str, decision_identity: tuple[str, str],
progressive_enabled: bool, reader_argv: list[str] | None) -> dict[str, Any]:
"""Prepare one already-decided native view; callers retain receipt failures."""
+ from .quota_request import quota_detail_sections_from_args
from ..control_plane.turn_driver.codex_context import (
NativeContextSessionUnavailable, codex_context_directory, prepare_codex_context,
)
@@ -246,11 +298,19 @@ def project_native_quota_context(payload: dict[str, Any], args: argparse.Namespa
root=getattr(args, "codex_context_root", None), create=True)
except NativeContextSessionUnavailable:
directory = None # Current full input, no fabricated native journal.
+ decision_ref = (
+ _saved_decision_reference(capture_directory, expected_sha256=capture_id)
+ if progressive_enabled
+ and (directory is not None or not bool(getattr(args, "begin_turn", False)))
+ and "vision" not in quota_detail_sections_from_args(args)
+ else None
+ )
prepared = prepare_codex_context(payload, directory=directory,
capture_directory=capture_directory, goal_id=decision_identity[0], agent_id=decision_identity[1],
surface=quota_context_surface(payload),
capture_id=capture_id, continuity_generation=getattr(args, "_context_delivery_generation", None),
- progressive_enabled=progressive_enabled)
+ progressive_enabled=progressive_enabled,
+ captured_decision=decision_ref)
recovery = prepared.delivery.get("recovery")
view = native_context_capture_view(prepared.delivery, capture_directory, context_directory=directory,
progressive_enabled=progressive_enabled, initial_read_plan=prepared.initial_read_plan,
@@ -410,14 +470,15 @@ def capture_decision(directory: Path | None, payload: dict[str, object], *,
args: argparse.Namespace | None = None, native_context: bool = False,
record_capture: Callable[[dict[str, object], str], None] | None = None) -> str | None:
if directory is None:
- return
+ return None
from ..control_plane.turn_driver.decision_capture import write_decision_capture
try:
digest = write_decision_capture(directory / "decision.json", payload, publish=os.replace)
if record_capture is not None:
record_capture(payload, digest)
- if isinstance(payload.get("detail_ref"), dict) and "full_decision" in payload["detail_ref"]:
+ detail_ref = payload.get("detail_ref")
+ if isinstance(detail_ref, dict) and "full_decision" in detail_ref:
from ..command_invocation import current_loopx_cli_argv
write_context_read_reference(directory / "decision.json", digest,
diff --git a/loopx/cli_commands/quota_context.py b/loopx/cli_commands/quota_context.py
index 7c2fde92cd..57630dc136 100644
--- a/loopx/cli_commands/quota_context.py
+++ b/loopx/cli_commands/quota_context.py
@@ -68,10 +68,14 @@ class QuotaCommandContext:
def _scheduler_execution_context_from_args(
args: argparse.Namespace,
) -> Mapping[str, object] | SchedulerExecutionContextResolution | None:
+ host_surface = getattr(args, "host_surface", None)
+ scheduler_owner = getattr(args, "scheduler_owner", None)
+ execution_mode = getattr(args, "execution_mode", None)
+ runtime_profile = getattr(args, "runtime_profile", None)
explicit_scheduler_fields = (
- args.host_surface,
- args.scheduler_owner,
- args.execution_mode,
+ host_surface,
+ scheduler_owner,
+ execution_mode,
)
codex_app = bool(getattr(args, "codex_app", False))
trae_app = bool(getattr(args, "trae_app", False))
@@ -81,32 +85,34 @@ def _scheduler_execution_context_from_args(
raise QuotaCommandValidationError(
"--codex-app, --trae_app, and --antigravity_app are mutually exclusive"
)
- if app_alias_count and (args.runtime_profile or any(explicit_scheduler_fields)):
+ if app_alias_count and (runtime_profile or any(explicit_scheduler_fields)):
raise QuotaCommandValidationError(
"app runtime aliases cannot be combined with --runtime-profile, "
"--host-surface, --scheduler-owner, or --execution-mode"
)
- if args.runtime_profile and any(explicit_scheduler_fields):
+ if runtime_profile and any(explicit_scheduler_fields):
raise QuotaCommandValidationError(
"--runtime-profile cannot be combined with --host-surface, "
"--scheduler-owner, or --execution-mode"
)
- runtime_profile = (
+ effective_runtime_profile = (
SchedulerRuntimeProfile.CODEX_APP_HEARTBEAT.value
if codex_app
else SchedulerRuntimeProfile.TRAE_APP.value
if trae_app
else SchedulerRuntimeProfile.ANTIGRAVITY_APP.value
if antigravity_app
- else args.runtime_profile
+ else runtime_profile
)
- if runtime_profile:
- return scheduler_execution_context_for_runtime_profile(runtime_profile)
+ if effective_runtime_profile:
+ return scheduler_execution_context_for_runtime_profile(
+ effective_runtime_profile
+ )
if any(explicit_scheduler_fields):
return {
- "host_surface": args.host_surface,
- "scheduler_owner": args.scheduler_owner,
- "execution_mode": args.execution_mode,
+ "host_surface": host_surface,
+ "scheduler_owner": scheduler_owner,
+ "execution_mode": execution_mode,
"source": "quota_cli_invocation",
}
return None
@@ -180,7 +186,7 @@ def validate_quota_command_context_request(
raise QuotaCommandValidationError(
"--replan-obligation-id cannot be combined with --todo-id"
)
- if heartbeat_turn_id and not args.agent_id:
+ if heartbeat_turn_id and not getattr(args, "agent_id", None):
raise QuotaCommandValidationError(
"turn-scoped quota settlement requires --agent-id"
)
@@ -266,7 +272,7 @@ def validate_quota_command_context_request(
if (
(heartbeat_turn_id or begin_turn)
and command == "should-run"
- and bool(args.dry_run)
+ and bool(getattr(args, "dry_run", False))
):
raise QuotaCommandValidationError(
"turn-scoped `quota should-run` cannot use --dry-run"
diff --git a/loopx/cli_commands/quota_native_context.py b/loopx/cli_commands/quota_native_context.py
index 6fc591b321..328c1ef710 100644
--- a/loopx/cli_commands/quota_native_context.py
+++ b/loopx/cli_commands/quota_native_context.py
@@ -280,6 +280,7 @@ def authority_refusal(error: Exception) -> dict[str, Any]:
continuity_generation=getattr(args, "_context_delivery_generation", None),
progressive_enabled=activation["progressive_enabled"], selection_command_args_template=transport,
captured_decision=captured_decision if activation["progressive_enabled"]
+ and not args.select and not getattr(args, "list_fields", False)
and (args.initial_read or "sha256" in captured_decision) else None,
hook_session=hook_session, saved_capture=True, publication_guard=publication_guard)
projected = prepared.delivery
@@ -423,6 +424,8 @@ def _handle_context_hook(args: argparse.Namespace, *, activation: dict[str, Any]
and event.get("hook_event_name") in CONTEXT_LIFECYCLE_HOOKS):
directory = codex_context_directory(directory=args.codex_context_dir,
root=getattr(args, "codex_context_root", None), session=event.get("session_id"), create=True)
+ if directory is None:
+ raise ValueError("context directory unavailable")
scenario = activation["scenario"]
progressive_enabled = activation["progressive_enabled"]
payload = codex_context_hook(directory, event)
@@ -485,9 +488,10 @@ def _handle_context_hook(args: argparse.Namespace, *, activation: dict[str, Any]
preparation_context = str(error)
except NativePreparationTurnUnavailable as error:
preparation_context = str(error)
- record_native_execution_preparation(
- directory, event, source_sha256=request["source_sha256"], receipt=None,
- )
+ if request is not None:
+ record_native_execution_preparation(
+ directory, event, source_sha256=request["source_sha256"], receipt=None,
+ )
except (OSError, RuntimeError, subprocess.SubprocessError, ValueError, TypeError, RecursionError):
# Reconcile only the attempted source. A concurrent guard
# can replace recovery, so it must never be reserved here.
diff --git a/loopx/cli_commands/quota_presentation.py b/loopx/cli_commands/quota_presentation.py
index 966a3145d6..c6537b0339 100644
--- a/loopx/cli_commands/quota_presentation.py
+++ b/loopx/cli_commands/quota_presentation.py
@@ -9,6 +9,32 @@
from ..control_plane.scheduler.execution_context import SchedulerExecutionContextResolution
+UNDECLARED_JSON_COMPLETE_TURN_OPTIONS = (
+ "complete_turn",
+ "turn_envelope",
+ "codex_context_dir",
+ "codex_context_root",
+ "prepared_capture_reference",
+ "defer_context_presentation",
+ "decision_handoff_socket",
+)
+
+
+def is_undeclared_json_quota_caller(
+ args: argparse.Namespace,
+ scheduler_context: Mapping[str, object] | SchedulerExecutionContextResolution | None,
+) -> bool:
+ """Return True when a JSON caller uses the currently qualified public quota packet."""
+ return (
+ getattr(args, "format", None) == "json"
+ and scheduler_context is None
+ and not any(
+ getattr(args, option, None)
+ for option in UNDECLARED_JSON_COMPLETE_TURN_OPTIONS
+ )
+ )
+
+
def compose_quota_work_contract(payload: dict[str, object], args: argparse.Namespace, *,
scheduler_context: Mapping[str, object] | SchedulerExecutionContextResolution | None,
capture_directory: Path | None,
@@ -16,14 +42,7 @@ def compose_quota_work_contract(payload: dict[str, object], args: argparse.Names
"""Compose Complete Turn sources before capture and display."""
if args.quota_command != "should-run" or not isinstance(payload.get("interaction_contract"), Mapping):
return payload
- if (
- args.format == "json"
- and scheduler_context is None
- and not any(getattr(args, option, None) for option in (
- "complete_turn", "turn_envelope", "codex_context_dir", "codex_context_root",
- "prepared_capture_reference", "defer_context_presentation", "decision_handoff_socket",
- ))
- ):
+ if is_undeclared_json_quota_caller(args, scheduler_context):
# Undeclared JSON callers use the currently qualified public quota packet.
return payload
from ..control_plane.effect_runtime import EffectRuntimeRemoteError, EffectRuntimeStartupError
diff --git a/loopx/cli_commands/quota_request.py b/loopx/cli_commands/quota_request.py
index bab2f66cb4..fef28d6103 100644
--- a/loopx/cli_commands/quota_request.py
+++ b/loopx/cli_commands/quota_request.py
@@ -126,7 +126,7 @@ def register_quota_monitor_poll_request_arguments(
def validate_quota_command_request(args: argparse.Namespace) -> None:
command = args.quota_command
if getattr(args, "material_change_scope", None) and (
- command != "monitor-poll" or not args.material_change
+ command != "monitor-poll" or not bool(getattr(args, "material_change", False))
):
raise QuotaCommandValidationError(
"--material-change-scope requires quota monitor-poll --material-change"
@@ -139,9 +139,13 @@ def validate_quota_command_request(args: argparse.Namespace) -> None:
lease_version = getattr(args, "task_lease_expected_version", None)
use_current_lease = bool(getattr(args, "use_current_task_lease", False))
if use_current_lease:
- if command != "monitor-poll" or not args.execute:
+ if command != "monitor-poll" or not bool(getattr(args, "execute", False)):
raise QuotaCommandValidationError("--use-current-task-lease requires executing quota monitor-poll")
- if not args.todo_id or not args.turn_instance_id or not args.agent_id:
+ if (
+ not getattr(args, "todo_id", None)
+ or not getattr(args, "turn_instance_id", None)
+ or not getattr(args, "agent_id", None)
+ ):
raise QuotaCommandValidationError(
"--use-current-task-lease requires --todo-id, --turn-instance-id, and --agent-id"
)
@@ -154,14 +158,14 @@ def validate_quota_command_request(args: argparse.Namespace) -> None:
raise QuotaCommandValidationError("task lease proof is only valid with quota monitor-poll")
if not lease_key or lease_version is None or lease_version < 1 or lease_version > 9007199254740991:
raise QuotaCommandValidationError("Monitor lease proof requires an execution key and positive safe-integer version")
- if not (args.todo_id or args.target_key):
+ if not (getattr(args, "todo_id", None) or getattr(args, "target_key", None)):
raise QuotaCommandValidationError("Monitor lease proof requires --todo-id or --target-key")
if getattr(args, "delivery_workspace_path", None) is not None and command != "spend-slot":
raise QuotaCommandValidationError(
"--delivery-workspace-path is only valid with `loopx quota spend-slot`"
)
begin_turn = bool(getattr(args, "begin_turn", False))
- if command not in {"status", "plan"} and not args.goal_id:
+ if command not in {"status", "plan"} and not getattr(args, "goal_id", None):
raise QuotaCommandValidationError(
f"`loopx quota {command}` requires --goal-id"
)
@@ -170,15 +174,19 @@ def validate_quota_command_request(args: argparse.Namespace) -> None:
"scheduler-ack-current",
"scheduler-fail-current",
}
- if command in scheduler_commands and not args.agent_id:
+ if command in scheduler_commands and not getattr(args, "agent_id", None):
raise QuotaCommandValidationError(
f"`loopx quota {command}` requires --agent-id"
)
- if command == "void-slot" and not args.void_generated_at:
+ if command == "void-slot" and not getattr(args, "void_generated_at", None):
raise QuotaCommandValidationError(
"`loopx quota void-slot` requires --void-generated-at"
)
- if command == "should-run" and args.todo_id and not (args.turn_instance_id or begin_turn):
+ if (
+ command == "should-run"
+ and getattr(args, "todo_id", None)
+ and not (getattr(args, "turn_instance_id", None) or begin_turn)
+ ):
raise QuotaCommandValidationError(
"`loopx quota should-run --todo-id` requires --turn-instance-id or --begin-turn"
)
@@ -186,18 +194,18 @@ def validate_quota_command_request(args: argparse.Namespace) -> None:
raise QuotaCommandValidationError(
"--begin-turn is only valid with `loopx quota should-run`"
)
- if begin_turn and args.turn_instance_id:
+ if begin_turn and getattr(args, "turn_instance_id", None):
raise QuotaCommandValidationError(
"--begin-turn cannot be combined with --turn-instance-id"
)
- if begin_turn and not args.agent_id:
+ if begin_turn and not getattr(args, "agent_id", None):
raise QuotaCommandValidationError(
"--begin-turn requires exact --agent-id identity"
)
if (
command not in {"status", "plan", "should-run"}
- and args.dry_run
- and args.execute
+ and bool(getattr(args, "dry_run", False))
+ and bool(getattr(args, "execute", False))
):
raise QuotaCommandValidationError(
f"`loopx quota {command}` accepts only one of --dry-run or --execute"
diff --git a/loopx/cli_commands/quota_result_delivery.py b/loopx/cli_commands/quota_result_delivery.py
index ccad2d22f9..c0ba899765 100644
--- a/loopx/cli_commands/quota_result_delivery.py
+++ b/loopx/cli_commands/quota_result_delivery.py
@@ -81,8 +81,9 @@ def prepare_quota_result_context(
*,
registry_path: Path,
heartbeat_turn_id: str | None,
+ attach_recall: bool = True,
) -> tuple[dict[str, object], dict[str, Any]]:
- """Resolve presentation and attach recall before composing the work contract."""
+ """Resolve presentation and optionally attach recall before composition."""
try:
activation = native_context_activation(args)
except QuotaCommandValidationError:
@@ -90,7 +91,7 @@ def prepare_quota_result_context(
raise
# Keep the original diagnostic on a refused guard.
activation = {"progressive_enabled": False, "enabled": False}
- if args.quota_command == "should-run":
+ if attach_recall and args.quota_command == "should-run":
attach_reward_memory_recall_after_should_run(
payload,
quota_command=args.quota_command,
@@ -174,10 +175,11 @@ def emit_quota_result(
capture_sha256: str | None = None
saved_decision = payload
- def record_capture(decision: dict[str, object], digest: str) -> None:
+ def record_capture(decision: dict[str, object], digest: str | None) -> None:
nonlocal saved_decision, capture_sha256
saved_decision = decision
- capture_sha256 = digest
+ if digest is not None:
+ capture_sha256 = digest
try:
return _publish_quota_result(
@@ -224,7 +226,7 @@ def _publish_quota_result(
capture_directory: Path | None,
detail_sections: frozenset[str],
print_payload: PrintPayload,
- record_capture: Callable[[dict[str, object], str], None],
+ record_capture: Callable[[dict[str, object], str | None], None],
) -> int:
from ..command_invocation import current_loopx_cli_argv
@@ -253,6 +255,7 @@ def _publish_quota_result(
args,
registry_path=registry_path,
heartbeat_turn_id=heartbeat_turn_id,
+ attach_recall=False,
)
# Compose optional capability guidance after recovery has chosen the plan,
# before capture/compaction. The quota decision owner stays capability-neutral.
@@ -303,6 +306,7 @@ def _publish_quota_result(
quota_module._export("attach_host_poll_receipt")(
context.status_payload, args, payload, registry_path=registry_path,
)
+ record_capture(payload, None)
native_context = context_delivery_enabled and bool(
getattr(args, "codex_context_dir", None)
or getattr(args, "codex_context_root", None)
@@ -318,6 +322,7 @@ def _publish_quota_result(
"full_decision": context_capture_read_command(source, "",
reference_file=source.with_suffix(".reader.json"), reader_argv=reader_argv),
}}
+ pre_contract_decision = payload
payload = compose_quota_work_contract(
payload,
args,
@@ -325,6 +330,40 @@ def _publish_quota_result(
capture_directory=capture_directory,
record_capture=record_capture,
)
+ if args.quota_command == "should-run":
+ from ..control_plane.quota.turn_envelope import (
+ COMPLETE_TURN_DECISION_VERSION,
+ attach_complete_turn_context,
+ )
+
+ if payload.get("schema_version") == COMPLETE_TURN_DECISION_VERSION:
+ hook_target = dict(payload)
+ attach_reward_memory_recall_after_should_run(
+ hook_target,
+ quota_command=args.quota_command,
+ heartbeat_turn_id=heartbeat_turn_id,
+ registry_path=registry_path,
+ goal_id=args.goal_id,
+ agent_id=args.agent_id,
+ decision=pre_contract_decision,
+ )
+ additions = {
+ key: value
+ for key, value in hook_target.items()
+ if key not in payload
+ }
+ if additions:
+ payload = attach_complete_turn_context(payload, additions)
+ else:
+ attach_reward_memory_recall_after_should_run(
+ payload,
+ quota_command=args.quota_command,
+ heartbeat_turn_id=heartbeat_turn_id,
+ registry_path=registry_path,
+ goal_id=args.goal_id,
+ agent_id=args.agent_id,
+ decision=pre_contract_decision,
+ )
captured_digest = capture_decision(
capture_directory,
payload,
diff --git a/loopx/cli_commands/quota_reward_memory.py b/loopx/cli_commands/quota_reward_memory.py
index 17580d090b..68b33e910d 100644
--- a/loopx/cli_commands/quota_reward_memory.py
+++ b/loopx/cli_commands/quota_reward_memory.py
@@ -110,6 +110,7 @@ def attach_reward_memory_recall_after_should_run(
registry_path: Path,
goal_id: str,
agent_id: str,
+ decision: Mapping[str, Any] | None = None,
) -> None:
heartbeat_receipt = payload.get("heartbeat_receipt")
if (
@@ -128,6 +129,6 @@ def attach_reward_memory_recall_after_should_run(
registry_path=registry_path,
goal_id=goal_id,
agent_id=agent_id,
- decision=payload,
+ decision=payload if decision is None else decision,
turn_instance_id=heartbeat_turn_id,
)
diff --git a/loopx/cli_commands/quota_should_run.py b/loopx/cli_commands/quota_should_run.py
index e65679bdc8..9b4bb4eeb9 100644
--- a/loopx/cli_commands/quota_should_run.py
+++ b/loopx/cli_commands/quota_should_run.py
@@ -21,6 +21,7 @@
)
from ..file_lock import LockAcquireTimeoutError
from ..rollout_event_log import RolloutAppendPreconditionChanged
+from .quota_presentation import is_undeclared_json_quota_caller
if TYPE_CHECKING:
from .quota_action_selection import RequestedQuotaActionSelection
@@ -53,7 +54,9 @@ def dispatch_quota_turn_start_hooks(
runtime_root_arg: str | None,
lark_turn_start_hook_dispatcher: Callable[..., dict[str, object]] | None = None,
) -> tuple[dict[str, object] | None, bool]:
- quota_module._export("validate_quota_command_context_request")(args)
+ _, scheduler_context, _ = quota_module._export(
+ "validate_quota_command_context_request"
+ )(args)
if args.quota_command != "should-run":
return None, False
preview = bool(getattr(args, "dry_run", False))
@@ -96,7 +99,10 @@ def dispatch_quota_turn_start_hooks(
)
# A preview still needs the current local Inbox and preferences. These
# hooks observe existing context without recording reads or delivery.
- model_read = not getattr(args, "scheduler_observation", False)
+ model_read = (
+ not getattr(args, "scheduler_observation", False)
+ and not is_undeclared_json_quota_caller(args, scheduler_context)
+ )
context_dispatch = dispatch_turn_start_hooks(
(
turn_start_hook(
@@ -367,7 +373,10 @@ def execute_quota_should_run(
agent_id=args.agent_id,
todo_id=args.todo_id,
)
- complete_context = not getattr(args, "scheduler_observation", False)
+ complete_context = (
+ not getattr(args, "scheduler_observation", False)
+ and not is_undeclared_json_quota_caller(args, scheduler_context)
+ )
payload = export("build_live_quota_should_run_decision")(
status_payload,
goal_id=args.goal_id,
diff --git a/loopx/cli_commands/support_control_upgrade.py b/loopx/cli_commands/support_control_upgrade.py
index 273f0119b3..40b453c021 100644
--- a/loopx/cli_commands/support_control_upgrade.py
+++ b/loopx/cli_commands/support_control_upgrade.py
@@ -219,7 +219,7 @@ def _upgrade_desktop(plan: dict[str, Any], timeout_seconds: int) -> dict[str, An
[str(app / "Contents/MacOS" / executable),
"--loopx-upgrade-prepare", f"--loopx-upgrade-request={request_id}",
f"--loopx-upgrade-channel={plan['channel']}"],
- capture_output=True, text=True, timeout=timeout_seconds,
+ capture_output=True, text=True, encoding="utf-8", timeout=timeout_seconds,
)
result = json.loads(prepared.stdout)
target = result.get("target") if isinstance(result, dict) else None
diff --git a/loopx/cli_commands/todo.py b/loopx/cli_commands/todo.py
index ea55679ebc..dd46614ffb 100644
--- a/loopx/cli_commands/todo.py
+++ b/loopx/cli_commands/todo.py
@@ -7,6 +7,7 @@
from operator import itemgetter
from pathlib import Path
+from ..cli_runtime import report_json_text
from ..command_invocation import cli_option_args, loopx_cli_argv
from ..control_plane.agents.workspace_guard import capture_delivery_workspace
from ..control_plane.capability_hooks import PostWritebackHookRegistration
@@ -151,6 +152,67 @@ def _thin_claim_projection(payload: dict[str, object], route: Sequence[str]) ->
}
+def _thin_claim_overlap_advisories(payload: dict[str, object], route: Sequence[str]) -> None:
+ """Link long peer scope lists to the immutable claim receipt."""
+ advisories = payload.get("integration_overlap_advisories")
+ goal_id = payload.get("goal_id")
+ operation_id = payload.get("claim_operation_id")
+ if (
+ not isinstance(goal_id, str) or not goal_id
+ or not isinstance(operation_id, str) or not operation_id
+ or not isinstance(advisories, list) or not advisories
+ or any(not isinstance(row, dict) or not isinstance(row.get("write_scopes"), list)
+ for row in advisories)
+ ):
+ return
+ compact = {
+ "integration_overlap_advisories": [
+ {**{key: value for key, value in row.items() if key != "write_scopes"},
+ "write_scope_count": len(row["write_scopes"])}
+ for row in advisories
+ ],
+ "integration_overlap_detail_command": shlex.join([
+ *route, "todo", "receipt", "--goal-id", goal_id, "--operation-id", operation_id, "--thin",
+ ]),
+ }
+ original = {"integration_overlap_advisories": advisories}
+ if len(report_json_text(compact).encode("utf-8")) < len(report_json_text(original).encode("utf-8")):
+ payload.update(compact)
+
+
+def _thin_claim_receipt(payload: dict[str, object], goal_id: str, operation_id: str) -> None:
+ """Project historical overlap advice from the exact canonical claim receipt."""
+ if payload.get("status") == "found":
+ records = payload.get("receipts")
+ receipt = records[0] if isinstance(records, list) and len(records) == 1 else None
+ result = receipt.get("result") if isinstance(receipt, dict) else None
+ if (
+ not isinstance(receipt, dict)
+ or receipt.get("schema_version") != "loopx_coordination_todo_claim_receipt_v0"
+ or receipt.get("goal_id") != goal_id
+ or receipt.get("operation_id") != operation_id
+ or not isinstance(result, dict)
+ or result.get("claim_operation_id") != operation_id
+ or result.get("execution_managed") is not True
+ or not isinstance(result.get("todo_id"), str)
+ or not result["todo_id"]
+ or not isinstance(result.get("integration_overlap_advisories"), list)
+ ):
+ raise ValueError(
+ "todo receipt --thin requires an exact managed claim receipt with overlap advice. "
+ "Omit --thin for the full receipt."
+ )
+ payload["receipts"] = [{
+ **receipt,
+ "result": {
+ "claim_operation_id": operation_id,
+ "todo_id": result["todo_id"],
+ "integration_overlap_advisories": result["integration_overlap_advisories"],
+ },
+ }]
+ payload["thin"] = True
+
+
def handle_todo_command(
args: argparse.Namespace,
*,
@@ -174,6 +236,7 @@ def handle_todo_command(
format_name=format_name,
list_reader=list_goal_todos,
)
+ output_format = format_name or str(getattr(args, "format", None) or "markdown")
renderer = render_todo_markdown
if args.todo_command == "plan":
renderer = render_task_planning_packet
@@ -219,6 +282,8 @@ def handle_todo_command(
)
elif args.todo_command == "receipt":
validate_todo_receipt_options(args)
+ if args.todo_thin and output_format != "json":
+ raise ValueError("todo receipt --thin requires --format json")
runtime_root = resolve_runtime_root(
load_registry(registry_path), runtime_root_arg
)
@@ -245,6 +310,8 @@ def handle_todo_command(
"command": "receipt",
**result,
}
+ if args.todo_thin:
+ _thin_claim_receipt(payload, args.goal_id, args.operation_id)
elif args.todo_command == "result-read":
validate_todo_result_read_options(args)
registry = load_registry(registry_path)
@@ -506,6 +573,8 @@ def handle_todo_command(
payload.pop(internal, None)
if args.todo_thin:
_thin_claim_projection(payload, route)
+ if output_format == "json":
+ _thin_claim_overlap_advisories(payload, route)
elif args.todo_command == "update":
validate_todo_update_options(args)
update_operation = partial(
@@ -872,7 +941,6 @@ def handle_todo_command(
post_writeback_projection_builder=post_writeback_projection_builder,
admission_factory=FirstPartyHostGoalAdmission,
)
- output_format = format_name or str(getattr(args, "format", None) or "markdown")
print_payload(
payload,
output_format,
diff --git a/loopx/cli_commands/todo_argument_validation.py b/loopx/cli_commands/todo_argument_validation.py
index 89a5bd766f..469b20f582 100644
--- a/loopx/cli_commands/todo_argument_validation.py
+++ b/loopx/cli_commands/todo_argument_validation.py
@@ -148,7 +148,7 @@
# field owner; other lifecycle commands retain full help until centralized.
TODO_ACTION_FIELDS = {
"list": frozenset({"role", "todo_id", "status", "agent_id", "todo_limit", "todo_thin", "todo_summary", "include_archived", "state_file", "project", "dry_run"}),
- "receipt": frozenset({"operation_id"}),
+ "receipt": frozenset({"operation_id", "todo_thin"}),
"result-read": frozenset({"todo_id"}),
"plan": frozenset({"text", "agent_id", "project"}),
"project-markdown": frozenset({"state_file", "execute", "provider_revision", "project"}),
@@ -324,7 +324,7 @@ def validate_todo_list_options(args: argparse.Namespace) -> None:
def validate_todo_receipt_options(args: argparse.Namespace) -> None:
_validate_todo_option_subset(
args, TODO_ACTION_FIELDS["receipt"],
- "todo receipt only accepts --goal-id, --operation-id, and --format; unsupported: ",
+ "todo receipt only accepts --goal-id, --operation-id, --thin, and --format; unsupported: ",
)
if not args.operation_id:
raise ValueError("todo receipt requires --operation-id")
@@ -766,8 +766,11 @@ def validate_shared_todo_options(args: argparse.Namespace) -> None:
raise ValueError(
"--limit is supported only by todo list"
)
- if args.todo_thin and args.todo_command not in {"list", "claim"}:
- raise ValueError("--thin is supported only by todo list and todo claim")
+ if args.todo_thin and "todo_thin" not in TODO_ACTION_FIELDS.get(args.todo_command, ()):
+ supported = ", ".join(
+ f"todo {action}" for action, fields in TODO_ACTION_FIELDS.items() if "todo_thin" in fields
+ )
+ raise ValueError(f"--thin is supported only by {supported}")
if getattr(args, "todo_summary", False):
if args.todo_command != "list":
raise ValueError("--summary is supported only by todo list")
diff --git a/loopx/cli_commands/turn_decision.py b/loopx/cli_commands/turn_decision.py
index a1e1de678f..bf54f53947 100644
--- a/loopx/cli_commands/turn_decision.py
+++ b/loopx/cli_commands/turn_decision.py
@@ -417,9 +417,20 @@ def current_turn_host_launch_decision(
)
# Reuse the existing side-effect-free Host route projection, including
# delivery, user-gate and protocol checks. No plan/session is persisted.
+ host = getattr(args, "host", "generic-cli")
+ session_scope = (
+ getattr(args, "session_scope", None)
+ if host in {"codex-cli", "codex-app-server"}
+ and not getattr(args, "codex_operation_tools", False)
+ else "todo"
+ )
+ if host in {"codex-cli", "codex-app-server"} and identity.request_subject is not None:
+ session_scope = "agent"
plan = build_loopx_turn_plan(
current_contract, host=args.host, execution_mode=args.execution_mode,
scheduler_owner=args.scheduler_owner, turn_instance_id=identity.turn_instance_id,
+ iteration_context_policy=getattr(args, "iteration_context", "resume"),
+ session_scope=session_scope,
goal_ref=goal_ref, include_selected_work_basis=planned_selected_work_basis is not None,
)
route = plan["route"]
diff --git a/loopx/cli_commands/turn_rendering.py b/loopx/cli_commands/turn_rendering.py
index f298def76c..cda0d23560 100644
--- a/loopx/cli_commands/turn_rendering.py
+++ b/loopx/cli_commands/turn_rendering.py
@@ -6,6 +6,7 @@
from pathlib import Path
from typing import Any
+from ..control_plane.effect_runtime import EffectRuntimeRemoteError
from ..control_plane.turn_driver import (
LOOPX_TURN_EXECUTION_SCHEMA_VERSION,
TurnRecoveryBlockedError,
@@ -72,6 +73,8 @@ def build_turn_error_payload(
turn_key = str(transaction.get("turn_key") or "")
run_once = turn_command == "run-once"
error_code = getattr(exc, "code", None)
+ if not isinstance(error_code, str) and isinstance(exc, EffectRuntimeRemoteError):
+ error_code = exc.diagnostic_code
error_payload = getattr(exc, "payload", None)
planned_effects = planned.get("effects")
planned_effects = planned_effects if isinstance(planned_effects, Mapping) else {}
diff --git a/loopx/cli_commands/turn_run_once.py b/loopx/cli_commands/turn_run_once.py
index f43ddb5062..cb1e0d23ca 100644
--- a/loopx/cli_commands/turn_run_once.py
+++ b/loopx/cli_commands/turn_run_once.py
@@ -18,6 +18,7 @@
from ..control_plane.goals.first_party_host_admission import (
FirstPartyHostGoalAdmission,
)
+from ..control_plane.quota.execution_preparation import read_active_task_lease_epoch
from ..control_plane.quota.heartbeat_receipt import (
ensure_turn_heartbeat_settlement_receipt,
)
@@ -117,6 +118,29 @@ def execute_turn_run_once(
selected_todo = execution_context["selected_todo"]
expected_todo_text_sha256 = execution_context["sha256"]
task_lease_proof = execution_context.get("task_lease_proof") or None
+ task_lease_epoch = execution_context.get("task_lease_epoch")
+ rebound_lease_active = False
+ if (
+ args.execute
+ and getattr(args, "resume_turn_key", None)
+ and native_completion_lease_resolver is not None
+ and selected_todo.get("todo_id")
+ ):
+ resolved_lease = dict(native_completion_lease_resolver())
+ if resolved_lease.get("task_lease_idempotency_key") is not None:
+ rebound_proof = {
+ "idempotency_key": resolved_lease["task_lease_idempotency_key"],
+ "expected_version": resolved_lease["task_lease_expected_version"],
+ }
+ task_lease_epoch = read_active_task_lease_epoch(
+ runtime_root=runtime_root,
+ goal_id=args.goal_id,
+ todo_id=str(selected_todo["todo_id"]),
+ agent_id=args.agent_id,
+ requested_proof=rebound_proof,
+ )
+ task_lease_proof = rebound_proof
+ rebound_lease_active = True
transaction = (
payload.get("transaction")
if isinstance(payload.get("transaction"), Mapping)
@@ -254,6 +278,20 @@ def refresh_candidate(
# Do not overwrite task context before refresh can reject a result.
progress_observation = result.get("progress_observation")
vision, explore_result = split_candidate_vision(result)
+ recovery_turn_instance_id = (
+ settlement_identity.turn_instance_id
+ if (
+ bool(args.retry_failed_turn)
+ and not dry_run
+ and completion_turn_key == settlement_identity.turn_instance_id
+ )
+ else None
+ )
+ refresh_task_lease_proof = (
+ None
+ if completion_todo_id is not None and (recovery_turn_instance_id is None or rebound_lease_active)
+ else task_lease_proof
+ )
return refresh_state_run(
registry_path=registry_path,
runtime_root_override=runtime_root_arg,
@@ -289,7 +327,9 @@ def refresh_candidate(
dry_run=dry_run,
sync_global=not bool(args.no_global_sync),
goal_ref=goal_ref,
- task_lease_proof=task_lease_proof,
+ task_lease_proof=refresh_task_lease_proof,
+ allow_same_generation_lease_renewal=refresh_task_lease_proof is not None,
+ validation_recovery_turn_instance_id=recovery_turn_instance_id,
)
# A Todo-less native governance Turn has no business artifact/argv.
# Its independent validator is the canonical semantic writeback preview,
@@ -312,6 +352,8 @@ def writeback(
effect_ref: str,
) -> dict[str, object]:
require_effect_ref(effect_ref, SettlementStepKind.DURABLE_WRITEBACK)
+ if args.execute and rebound_lease_active:
+ bind_heartbeat_settlement_receipt()
refresh = refresh_candidate(result, completion_todo_id=completion_todo_id,
completion_turn_key=completion_turn_key)
if refresh.get("ok") and (
@@ -451,6 +493,8 @@ def completion_writeback(
effect_ref: str,
) -> dict[str, object]:
require_effect_ref(effect_ref, SettlementStepKind.DURABLE_WRITEBACK)
+ if args.execute and rebound_lease_active:
+ bind_heartbeat_settlement_receipt()
prior = completion_readback()
if prior is None:
# A rejected attachment must not leave the Todo completed.
@@ -742,10 +786,7 @@ def codex_settlement_admission():
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,
- registry_path=registry_path, runtime_root=runtime_root, workspace=project)
+ def bind_heartbeat_settlement_receipt() -> None:
replan_packet = execution_context["replan_action_packet"]
capability_guard = (
replan_packet.get("capability_guard")
@@ -765,9 +806,15 @@ def on_managed_start_admitted() -> None:
),
goal_ref=goal_ref,
task_lease_proof=task_lease_proof,
- task_lease_epoch=execution_context.get("task_lease_epoch"),
+ task_lease_epoch=task_lease_epoch,
)
+ def on_managed_start_admitted() -> None:
+ if settlement_identity.request_subject is not None:
+ require_current_request_operation(request_locator, settlement_identity,
+ registry_path=registry_path, runtime_root=runtime_root, workspace=project)
+ bind_heartbeat_settlement_receipt()
+
managed_cadence = managed_cadence_start(
runtime_root=runtime_root,
goal_id=args.goal_id,
diff --git a/loopx/cli_runtime.py b/loopx/cli_runtime.py
index dd605032e1..e70f2fb937 100644
--- a/loopx/cli_runtime.py
+++ b/loopx/cli_runtime.py
@@ -965,9 +965,15 @@ def _dispatch_selected(args: argparse.Namespace, raw_argv: list[str]) -> int:
def _standalone_core() -> bool:
- from importlib.util import find_spec
+ if sys.modules.get("loopx.cli") is not None:
+ return False
+ from importlib.machinery import PathFinder
- if find_spec("loopx.cli") is not None:
+ package = sys.modules.get(__package__ or "loopx")
+ search_path = list(getattr(package, "__path__", ())) or [
+ str(Path(__file__).resolve().parent)
+ ]
+ if PathFinder.find_spec("loopx.cli", search_path) is not None:
return False
from importlib.metadata import PackageNotFoundError, distribution
from .python_install_owner import installed_loopx_distribution
diff --git a/loopx/control_plane/collaboration/conversation_binding.ts b/loopx/control_plane/collaboration/conversation_binding.ts
index aa7b0f4baf..9af05bca17 100644
--- a/loopx/control_plane/collaboration/conversation_binding.ts
+++ b/loopx/control_plane/collaboration/conversation_binding.ts
@@ -631,7 +631,8 @@ export function planBoundConversationRequest(params: JsonObject): JsonObject {
|| turn?.status === "failed"
&& ["usage_limit_exceeded", "response_timeout", "upstream_transport_failure", "context_window_exceeded",
"antigravity_rpc_error", "antigravity_transport_error", "antigravity_step_error",
- "manager_authorization_unavailable", "runtime_error", "server_restarted"].includes(String(turn.error_code)))
+ "manager_authorization_unavailable", "runtime_error", "execution_worker_exited",
+ "execution_worker_exited_before_dispatch", "server_restarted"].includes(String(turn.error_code)))
&& !(current?.session_id === row.session_id && current?.status === "closed")
&& observation.stop_observed !== true};
}
@@ -764,11 +765,15 @@ function planContextRecovery(params: JsonObject, row: JsonObject, current: JsonO
const no = {operation: "admit_turn", recovery_candidate: false, recovery_eligible: false};
const pendingTurn = recovery.failed_turn as JsonObject | undefined;
const pendingResponse = pendingTurn?.response as JsonObject | undefined;
- // A committed empty answer has no delivered failure to acknowledge first.
- // It may only adopt its verified native result, never dispatch recovery work.
+ // A committed empty answer or a pre-dispatch preparation failure whose
+ // notice receipt is ambiguous has no delivered failure to acknowledge first.
const pendingEmpty = current?.agent_id === "antigravity" && pendingTurn?.status === "completed"
&& pendingResponse?.message === "" && pureResultText(pendingResponse, true);
- if (!current || row.command !== null || row.status !== "accepted" || row.delivery_verified !== true && !pendingEmpty
+ const pendingPreparationNotice = recovery.failure_notice_receipt_unknown === true
+ && pendingTurn?.status === "failed" && pendingTurn?.upstream_turn_id === null
+ && pendingTurn?.delta_count === 0 && pendingTurn?.response === null;
+ if (!current || row.command !== null || row.status !== "accepted"
+ || (row.delivery_verified !== true && !pendingEmpty && !pendingPreparationNotice)
|| row.agent_target != null || current.session_id !== row.session_id
|| !["codex", "antigravity"].includes(String(current.agent_id))
|| current.upstream_mode !== (current.agent_id === "antigravity" ? "default" : "chat") || current.status === "closed"
@@ -798,6 +803,8 @@ function planContextRecovery(params: JsonObject, row: JsonObject, current: JsonO
if (runtimePreparation !== null) return runtimePreparation;
const preparation = steward ? planPreparationRecovery(recovery, row, current, failed, version) : null;
if (preparation !== null) return preparation;
+ // Post-dispatch and read-only recovery families always require a verified prior delivery.
+ if (row.delivery_verified !== true && !pendingEmpty) return no;
// Once preparation has dispatched, a different terminal category must not
// switch recovery families and start a fresh business retry budget.
if ((row.context_recovery != null
@@ -1641,25 +1648,40 @@ function legacyImagePreparation(recovery: JsonObject, failed: JsonObject): boole
*/
function planRuntimePreparationRecovery(recovery: JsonObject, row: JsonObject, current: JsonObject,
failed: JsonObject, version: number): JsonObject | null {
- if (!["model_auth_unavailable", "resume_failed"].includes(String(failed.error_code))) return null;
+ const expectedError = new Map([
+ ["model_auth_unavailable", "Native account API transport is unavailable."],
+ ["resume_failed", "The previous Agent conversation could not be restored."],
+ ["execution_worker_exited_before_dispatch", "The LoopX Chat worker exited before the original request was dispatched."],
+ ]).get(String(failed.error_code));
+ if (expectedError === undefined) return null;
const no = {operation: "admit_turn", recovery_candidate: false, recovery_eligible: false};
- const expectedError = failed.error_code === "model_auth_unavailable"
- ? "Native account API transport is unavailable." : "The previous Agent conversation could not be restored.";
- if (current.agent_id !== "codex" || failed.session_id !== current.session_id || failed.turn_id !== row.turn_id
- || failed.status !== "failed" || failed.error !== expectedError || failed.started_at !== null
- || failed.first_event_at !== null || failed.response !== null || failed.upstream_turn_id !== null
+ const lostWorker = failed.error_code === "execution_worker_exited_before_dispatch";
+ const beforeDispatch = lostWorker
+ ? row.origin === "lark" && current.steward_context != null && failed.execution_dispatch === "pending"
+ && failed.loopx_request == null && !failed.loopx_execution
+ && (failed.attachments === undefined || Array.isArray(failed.attachments) && failed.attachments.length === 0)
+ && typeof failed.started_at === "string" && Number.isFinite(Date.parse(failed.started_at))
+ : failed.started_at === null && failed.first_event_at === null;
+ if (!["codex", "antigravity"].includes(String(current.agent_id))
+ || failed.session_id !== current.session_id || failed.turn_id !== row.turn_id
+ || failed.status !== "failed" || failed.error !== expectedError || !beforeDispatch
+ || failed.response !== null || failed.upstream_turn_id !== null
|| failed.delta_count !== 0 || failed.message !== row.message || failed.origin !== row.origin
|| recovery.attachments_present !== false || recovery.steering_observed !== false
|| recovery.native_observation != null || recovery.native_result_present !== false) return no;
const observed = recovery.preparation_observation == null ? null
: requireJsonObject(recovery.preparation_observation, "runtime preparation readback");
if (observed?.kind !== "core_runtime_preparation_failure" || observed.session_id !== failed.session_id
- || observed.turn_id !== failed.turn_id || !Array.isArray(observed.events) || observed.events.length !== 2) return no;
+ || observed.turn_id !== failed.turn_id || !Array.isArray(observed.events)
+ || observed.events.length < 2 || (!lostWorker && observed.events.length !== 2)) return no;
const events = observed.events.map(value => requireJsonObject(value, "runtime preparation event"));
+ const failure = events.at(-1)!;
if (!events.every(item => typeof item.event_id === "string" && item.event_id.length > 0)
- || events[0].event_id === events[1].event_id || events[0].kind !== "turn.queued"
- || events[0].delivery_mode !== "session_queue" || events[1].kind !== "turn.failed"
- || events[1].error_code !== failed.error_code || events[1].message !== expectedError) return no;
+ || new Set(events.map(item => item.event_id)).size !== events.length || events[0].kind !== "turn.queued"
+ || events[0].delivery_mode !== "session_queue" || failure.kind !== "turn.failed"
+ || failure.error_code !== failed.error_code || failure.message !== expectedError
+ || events.slice(1, -1).some(item => item.kind !== "manager.context"
+ && !(item.kind === "agent.phase" && item.phase === "manager_context"))) return no;
const original = `external-${ref(row.request_ref, "original request")}`;
const previous = row.context_recovery == null ? null : requireJsonObject(row.context_recovery, "runtime preparation reservation");
if (row.context_recovery_history != null && !Array.isArray(row.context_recovery_history)) return no;
@@ -1773,7 +1795,16 @@ function planRoutingRecovery(recovery: JsonObject, row: JsonObject, current: Jso
const observed = raw == null ? null : requireJsonObject(raw, "pre-execution failure readback");
const interrupted = observed?.interrupted_before_execution === true
&& ["host_gate", "upstream_transport_failure"].includes(String(failed.error_code));
- if (observed && !quota && !preparation && observed.pre_execution_routing_timeout !== true && !interrupted) return null;
+ const info = observed?.codex_error_info;
+ const infoEntries = info && typeof info === "object" && !Array.isArray(info) ? Object.entries(info) : [];
+ const infoDetail = infoEntries.length === 1
+ && ["httpConnectionFailed", "responseStreamConnectionFailed"].includes(infoEntries[0][0])
+ && infoEntries[0][1] !== null && typeof infoEntries[0][1] === "object" && !Array.isArray(infoEntries[0][1])
+ ? infoEntries[0][1] as JsonObject : null;
+ const loopbackRelayAuth = observed?.loopback_relay_auth_failure === true
+ && failed.error_code === "upstream_transport_failure"
+ && infoDetail?.httpStatusCode === 401;
+ if (observed && !quota && !preparation && observed.pre_execution_routing_timeout !== true && !interrupted && !loopbackRelayAuth) return null;
const no = {operation: "admit_turn", recovery_candidate: false, recovery_eligible: false};
const reserved = previous?.failed_turn_id === failed.turn_id;
const priorAttempt = previous?.[attemptKey] ?? 0;
@@ -1822,6 +1853,10 @@ function planRoutingRecovery(recovery: JsonObject, row: JsonObject, current: Jso
&& observed.quota_available === true && typeof observed.quota_observed_at === "number"
&& now / 1000 >= observed.quota_observed_at && now / 1000 - observed.quota_observed_at <= 120
: interrupted ? recovery.stop_observed === false && observed.only_original_user_item === true
+ : loopbackRelayAuth ? observed.only_pre_execution_items === true
+ && observed.relay_transport_ready === true
+ && typeof observed.relay_observed_at === "number" && Number.isFinite(observed.relay_observed_at)
+ && now / 1000 >= observed.relay_observed_at && now / 1000 - observed.relay_observed_at <= 120
: observed.codex_error_info === "other" && observed.only_pre_execution_items === true)};
}
diff --git a/loopx/control_plane/collaboration/conversation_scope.ts b/loopx/control_plane/collaboration/conversation_scope.ts
index eb2718afa3..553cf75fcd 100644
--- a/loopx/control_plane/collaboration/conversation_scope.ts
+++ b/loopx/control_plane/collaboration/conversation_scope.ts
@@ -220,15 +220,21 @@ export function resolveConversationScope(input: Record): Conver
export function boundConversationExecutorMatches(row: Record, session: Record): boolean {
if (session.agent_id === row.executor_endpoint_id && session.executor_endpoint_id === row.executor_endpoint_id) return true;
const allocation = session.manager_executor_allocation as Record | undefined;
+ const configured = row.executor_endpoint_id;
+ if (configured !== "antigravity" && configured !== "codex") return false;
+ const fallback = configured === "antigravity" ? "codex" : "antigravity";
+ const flexiblePoolMatch =
+ (allocation?.configured_endpoint === configured && allocation.allocation_reason === "flexible_availability_fallback")
+ || (allocation?.configured_endpoint === fallback && allocation.allocation_reason === "flexible_primary_available");
return row.context_kind === "steward" && row.audience !== "group"
- && row.executor_endpoint_id === "antigravity" && session.agent_id === "codex"
- && session.executor_endpoint_id === "codex" && allocation?.schema_version === "manager_executor_allocation_v0"
- && allocation.selection_policy === "flexible" && allocation.configured_endpoint === "antigravity"
- && allocation.executor_endpoint === "codex" && allocation.allocation_reason === "flexible_availability_fallback"
+ && session.agent_id === fallback && session.executor_endpoint_id === fallback
+ && allocation?.schema_version === "manager_executor_allocation_v0"
+ && allocation.selection_policy === "flexible"
+ && allocation.executor_endpoint === fallback && flexiblePoolMatch
&& typeof allocation.configuration_revision === "string" && allocation.configuration_revision.length > 0
&& Array.isArray(allocation.eligible_endpoints) && allocation.eligible_endpoints.length === 2
&& allocation.eligible_endpoints.includes("antigravity") && allocation.eligible_endpoints.includes("codex")
&& Array.isArray(session.upstream_context_history) && session.upstream_context_history.some(value =>
- value && typeof value === "object" && !Array.isArray(value) && (value as Record).agent_id === "antigravity"
+ value && typeof value === "object" && !Array.isArray(value) && (value as Record).agent_id === configured
&& typeof (value as Record).thread_id === "string");
}
diff --git a/loopx/control_plane/collaboration/delegation.ts b/loopx/control_plane/collaboration/delegation.ts
index 58814c3867..a116c10919 100644
--- a/loopx/control_plane/collaboration/delegation.ts
+++ b/loopx/control_plane/collaboration/delegation.ts
@@ -236,6 +236,7 @@ export function delegationRequestDecision(params: JsonObject): JsonObject {
cli.spend_policy = "spend once after independent request validation and durable writeback";
const contract = requireJsonObject(packet.interaction_contract, "interaction contract");
const agent = requireJsonObject(contract.agent_channel, "agent channel");
+ for (const key of ["selection_required", "action_portfolio_ref", "suggested_action_count"]) delete agent[key];
const work = requireJsonObject(agent.work_context, "work context");
if (Array.isArray(work.sources)) work.sources = work.sources.filter(raw =>
requireJsonObject(raw, "work source").source !== "selected_todo");
diff --git a/loopx/control_plane/collaboration/steward_managed_mode.ts b/loopx/control_plane/collaboration/steward_managed_mode.ts
index 05ad062b08..db7b409348 100644
--- a/loopx/control_plane/collaboration/steward_managed_mode.ts
+++ b/loopx/control_plane/collaboration/steward_managed_mode.ts
@@ -18,9 +18,56 @@ import {projectTurnBoundary, turnPlanWorkSource} from "../quota/turn_envelope.ts
import {projectGoalCreationExecutionRequest} from "../goals/creation_execution.ts";
/** A stopped observation may get a new Chat carrier for the same native
- * identity. After a recovery starts, only another proven process restart can
- * continue it. The host reads the stopped worker lock, journals and native poll
- * receipts. Normal admission still checks ownership, deadline, permissions and user work. */
+ * identity. After a recovery starts, only another proven process restart or a
+ * cooldown-bounded pre-poll transient failure can continue it. The host reads
+ * the stopped worker lock, journals and native poll receipts. Normal admission
+ * still checks ownership, deadline, permissions and user work. */
+const WORKER_EXITED_ERROR =
+ "The LoopX Chat execution worker exited before this turn finished. Your conversation is saved; execution may have occurred. This turn was not retried automatically.";
+const MONITOR_RECOVERY_RETRY_COOLDOWN_MS = 300_000;
+const REPORT_VALIDATOR_PREFIX =
+ "from pathlib import Path; from loopx.capabilities.periodic_report.private_generation import validate_private_generation; "
+ + "validate_private_generation(Path(";
+
+function sameReportValidatorCommand(frozenCmd: unknown, requestCmd: unknown): boolean {
+ return Array.isArray(frozenCmd) && Array.isArray(requestCmd)
+ && frozenCmd.length === 3 && requestCmd.length === 3
+ && frozenCmd.every(part => typeof part === "string" && part.length > 0 && !part.includes("\0"))
+ && requestCmd.every(part => typeof part === "string" && part.length > 0 && !part.includes("\0"))
+ && frozenCmd[1] === "-c" && requestCmd[1] === "-c"
+ && frozenCmd[2] === requestCmd[2]
+ && frozenCmd[2].startsWith(REPORT_VALIDATOR_PREFIX)
+ && frozenCmd[2].endsWith("))");
+}
+
+function sameReportRequestField(key: string, priorValue: unknown, requestValue: unknown): boolean {
+ if (key === "validation_command" && sameReportValidatorCommand(priorValue, requestValue)) {
+ return true;
+ }
+ return JSON.stringify(priorValue) === JSON.stringify(requestValue);
+}
+
+function sameReportRequest(priorValue: unknown, request: JsonObject): boolean {
+ const prior = jsonObject(priorValue);
+ if (!prior) return false;
+ const keys = Object.keys(request);
+ return Object.keys(prior).length === keys.length
+ && keys.every(key => Object.hasOwn(prior, key) && sameReportRequestField(key, prior[key], request[key]));
+}
+
+function hasSingleManagerContextEvidence(basis: JsonObject, prior: JsonObject, events: unknown): boolean {
+ if (requireJsonObject(prior.actor, "original watch actor").schema_version !== "loopx_steward_managed_actor_v0"
+ || !Array.isArray(basis.contexts) || basis.contexts.length !== 1
+ || !Array.isArray(basis.phases) || basis.phases.length !== 1
+ || requireJsonObject(basis.phases[0], "original watch phase").phase !== "manager_context"
+ || !Array.isArray(events)
+ || events.filter(kind => kind === "agent.phase").length !== 1
+ || events.filter(kind => kind === "manager.context").length !== 1) {
+ return false;
+ }
+ return requireJsonObject(basis.contexts[0], "watch context").schema_version === "manager_turn_context_v1";
+}
+
function admitMonitorRestartRecovery(params: JsonObject, request: JsonObject, session: JsonObject): void {
if (request.monitor_restart_recovery === undefined) {
if (params.monitor_restart_basis != null) throw new EffectRuntimeRequestError("watch recovery lacks its original request");
@@ -62,30 +109,34 @@ function admitMonitorRestartRecovery(params: JsonObject, request: JsonObject, se
&& Array.isArray(basis.phases) && basis.phases.length === 1
&& requireJsonObject(basis.phases[0], "original watch phase").phase === "manager_context"
&& Array.isArray(events) && events.filter(kind => kind === "agent.phase").length === 1;
- // A custom signal monitor may fail inside the read-only external signal
- // check after manager_context completes and before any poll receipt is written.
- // Allow at most one bounded recovery when zero-effect proofs hold.
- let signalUnavailable = false;
- if (original.error_code === "runtime_error"
- && original.error === "steward monitor external signal source temporarily unavailable"
- && requireJsonObject(prior.actor, "original watch actor").schema_version === "loopx_steward_managed_actor_v0"
- && Array.isArray(basis.contexts) && basis.contexts.length === 1
- && Array.isArray(basis.phases) && basis.phases.length === 1
- && requireJsonObject(basis.phases[0], "original watch phase").phase === "manager_context"
- && Array.isArray(events) && events.filter(kind => kind === "agent.phase").length === 1
- && Array.isArray(events) && events.filter(kind => kind === "manager.context").length === 1) {
- const context = requireJsonObject(basis.contexts[0], "signal unavailable watch context");
- signalUnavailable = context.schema_version === "manager_turn_context_v1";
- }
+ // Post-context monitor failures (external signal outage, daemon worker exit,
+ // storage I/O error, or runtime unavailability) before any poll receipt or
+ // upstream model turn may recover once immediately and retry only after a
+ // bounded cooldown when strict single-context and zero-effect journal proofs
+ // hold; pre-context unknown worker launches remain closed.
+ const postContextWatchRecoverable = hasSingleManagerContextEvidence(basis, prior, events) && (
+ original.error_code === "runtime_unavailable"
+ || original.error_code === "runtime_error" && (
+ original.error === "steward monitor external signal source temporarily unavailable"
+ || original.error === "disk I/O error"
+ || original.error === WORKER_EXITED_ERROR
+ )
+ );
+ const completedAtMs = typeof original.completed_at === "string" ? Date.parse(original.completed_at) : NaN;
+ const observedAtMs = typeof basis.observed_at === "number" && Number.isFinite(basis.observed_at)
+ ? basis.observed_at * 1000 : NaN;
+ const chainedPostContextWatchRecoverable = postContextWatchRecoverable
+ && Number.isFinite(completedAtMs) && Number.isFinite(observedAtMs)
+ && observedAtMs - completedAtMs >= MONITOR_RECOVERY_RETRY_COOLDOWN_MS;
if (Object.keys(recovery).join() !== "original_turn_id"
|| recovery.original_turn_id !== original.turn_id || typeof original.turn_id !== "string"
|| !/^[a-f0-9]{32}$/.test(original.turn_id) || original.session_id !== session.session_id
|| original.status !== "failed"
- || !(original.error_code === "server_restarted" || scopeRefused || identityRefused || signalUnavailable)
+ || !(original.error_code === "server_restarted" || scopeRefused || identityRefused || postContextWatchRecoverable)
|| original.origin !== "native_loopx" || original.loopx_execution !== true
|| original.response !== null || original.upstream_turn_id !== null || original.delta_count !== 0
|| prior.work_kind !== "monitor" || request.work_kind !== "monitor" || request.validation_command !== null
- || (predecessor !== null && (original.error_code !== "server_restarted"
+ || (predecessor !== null && (!(original.error_code === "server_restarted" || chainedPostContextWatchRecoverable)
|| Object.keys(predecessor).join() !== "original_turn_id"
|| typeof predecessor.original_turn_id !== "string" || !/^[a-f0-9]{32}$/.test(predecessor.original_turn_id)
|| predecessor.original_turn_id === original.turn_id))
@@ -141,9 +192,9 @@ function admitReportQuotaRecovery(params: JsonObject, request: JsonObject, sessi
|| original.origin !== "native_loopx" || original.loopx_execution !== true || original.status !== "completed"
|| original.delta_count !== 0 || typeof original.upstream_turn_id !== "string" || !original.upstream_turn_id
|| Object.keys(prior).length !== keys.length
- || keys.some(key => key === "turn_instance_id"
+ || keys.some(key => !Object.hasOwn(prior, key) || (key === "turn_instance_id"
? request[key] !== String(prior[key]) + ".quota-recovery"
- : JSON.stringify(request[key]) !== JSON.stringify(prior[key]))
+ : !sameReportRequestField(key, prior[key], request[key])))
|| result.schema_version !== "loopx_turn_execution_v0" || result.ok !== false || result.mode !== "run_once"
|| result.status !== "failed" || result.result_kind !== "host_failure"
|| failure.schema_version !== "loopx_turn_host_failure_v0" || failure.attempt !== 1
@@ -181,8 +232,7 @@ function admitReportSelectionRecovery(params: JsonObject, request: JsonObject, s
if (original.session_id !== sessionId || original.origin !== "native_loopx"
|| original.loopx_execution !== true || original.status !== "completed"
|| original.delta_count !== 0 || original.upstream_turn_id !== null
- || Object.keys(prior).length !== Object.keys(request).length
- || Object.keys(request).some(key => JSON.stringify(prior[key]) !== JSON.stringify(request[key]))
+ || !sameReportRequest(prior, request)
|| result.schema_version !== "loopx_turn_execution_v0" || result.ok !== false
|| result.mode !== "run_once" || result.effects_scope !== "current_invocation"
|| result.error_code !== "turn_todo_selection_deferred" || result.resume_turn_key != null
@@ -196,8 +246,6 @@ function admitReportSelectionRecovery(params: JsonObject, request: JsonObject, s
throw new EffectRuntimeRequestError("report selection recovery requires the original proven pre-execution replan refusal");
}
if (proof.probe == null) return;
- // The original host returned before calling its model when a fresh guard
- // no longer owed governance. Absence of output alone cannot prove that.
const probe = requireJsonObject(proof.probe, "original host admission probe");
const observed = requireJsonObject(probe.turn, "original probe Chat Turn");
const journal = requireJsonObject(probe.journal, "canonical probe journal");
@@ -207,41 +255,71 @@ function admitReportSelectionRecovery(params: JsonObject, request: JsonObject, s
const plan = requireJsonObject(journal.plan, "probe plan");
const route = requireJsonObject(plan.route, "probe route");
const envelope = turnPlanWorkSource(plan, probe.source_number_tokens).work;
- const action = requireJsonObject(envelope.action, "probe action");
- const selected = requireJsonObject(action.selected_todo, "probe selected task");
const identity = requireJsonObject(requireJsonObject(requireJsonObject(plan.transaction, "probe transaction")
.settlement_plan, "probe settlement").identity, "probe identity");
const host = requireJsonObject(journal.host, "probe host");
- const stopped = requireJsonObject(journal.host_result, "probe host result");
+ const actor = requireJsonObject(request.actor, "report actor");
const runningHere = params.turn != null && requireJsonObject(params.turn, "current Chat Turn").turn_id === observed.turn_id
&& ["starting", "running"].includes(String(observed.status));
- if (observed.session_id !== sessionId || typeof observed.turn_id !== "string" || !/^[a-f0-9]{32}$/.test(observed.turn_id)
- || observed.turn_id === original.turn_id
- || observed.origin !== "native_loopx" || observed.loopx_execution !== true
- || observed.delta_count !== 0 || observed.upstream_turn_id !== null || observed.error_code != null || observed.error != null
- || JSON.stringify(observed.loopx_request) !== JSON.stringify(request)
- || !(runningHere || observed.status === "completed"
- && JSON.stringify(requireJsonObject(observed.response, "probe response").native_loopx) === JSON.stringify(result))
- || journal.schema_version !== "loopx_turn_journal_v0" || !ENVELOPED_SHA256_PATTERN.test(String(journal.turn_key))
- || journal.status !== "stopped" || journal.goal_id !== envelope.goal_id
- || journal.host_attempt_count !== 1 || journal.result_kind !== "wait"
+ const commonInvalid = observed.session_id !== sessionId || typeof observed.turn_id !== "string"
+ || !/^[a-f0-9]{32}$/.test(observed.turn_id) || observed.turn_id === original.turn_id
+ || observed.origin !== "native_loopx" || observed.loopx_execution !== true
+ || observed.error_code != null || observed.error != null
+ || !sameReportRequest(observed.loopx_request, request)
+ || !(runningHere || observed.status === "completed"
+ && JSON.stringify(requireJsonObject(observed.response, "probe response").native_loopx) === JSON.stringify(result))
+ || journal.schema_version !== "loopx_turn_journal_v0" || !ENVELOPED_SHA256_PATTERN.test(String(journal.turn_key))
+ || journal.goal_id !== envelope.goal_id || journal.host_attempt_count !== 1
+ || host.kind !== "codex-app-server" || host.executable !== "built-in"
+ || inspection.schema_version !== "loopx_turn_journal_inspection_v1" || inspection.ok !== true
+ || inspection.goal_matches !== true || inspection.owner_matches !== true || inspection.turn_key_matches !== true
+ || inspection.journal_consistent !== true
+ || recovery.action !== "return_existing" || recovery.reinvoke_host !== false
+ || recorded.host_invoked !== true
+ || ["state_written", "quota_spent", "scheduler_acknowledged"].some(key => recorded[key] !== false)
+ || identity.goal_id !== envelope.goal_id || identity.agent_id !== envelope.agent_id
+ || identity.goal_id !== actor.goal_id || identity.agent_id !== actor.agent_id
+ || identity.turn_instance_id !== request.turn_instance_id + ".replan";
+ if (commonInvalid) {
+ throw new EffectRuntimeRequestError("report continuation requires its canonical host-only admission stop; retain all model or unknown work");
+ }
+ if (journal.status === "failed" && journal.result_kind === "validation_failed"
+ && journal.validation_stage === "host_result_contract") {
+ // Only once the blocking autonomous replan obligation is cleared elsewhere
+ // may a report whose prior governance probe failed host contract validation
+ // resume cleanly via one bounded .admission-resume carrier.
+ const receipt = requireJsonObject(journal.receipt, "probe receipt");
+ const eligibility = requireJsonObject(receipt.commit_eligibility, "probe commit eligibility");
+ const guard = requireJsonObject(probe.current_guard, "current guard observation");
+ if (inspection.journal_status !== "failed"
+ || JSON.stringify(journal.completed_phases) !== JSON.stringify(["host_execute", "typed_result"])
+ || receipt.ok !== true || receipt.failed_phase !== "validation" || receipt.status !== "failed"
+ || ["writeback", "quota_spend", "scheduler_ack"].some(key => eligibility[key] !== false)
+ || route.kind !== "replan_required"
+ || route.effective_action !== EffectiveAction.AUTONOMOUS_REPLAN_REQUIRED
+ || envelope.effective_action !== EffectiveAction.AUTONOMOUS_REPLAN_REQUIRED
+ || identity.binding_kind !== "autonomous_replan"
+ || guard.effective_action !== EffectiveAction.NORMAL_RUN
+ || guard.replan_action_packet != null) {
+ throw new EffectRuntimeRequestError("report continuation after validation failure requires a zero-write governance refusal and cleared replan");
+ }
+ return;
+ }
+ // The original host returned before calling its model when a fresh guard
+ // no longer owed governance. Absence of output alone cannot prove that.
+ const action = requireJsonObject(envelope.action, "probe action");
+ const selected = requireJsonObject(action.selected_todo, "probe selected task");
+ const stopped = requireJsonObject(journal.host_result, "probe host result");
+ if (observed.delta_count !== 0 || observed.upstream_turn_id !== null
+ || journal.status !== "stopped" || journal.result_kind !== "wait"
|| JSON.stringify(journal.completed_phases) !== JSON.stringify(["host_execute", "typed_result", "validation"])
- || host.kind !== "codex-app-server" || host.executable !== "built-in"
|| stopped.schema_version !== "loopx_turn_result_v0" || stopped.turn_key !== journal.turn_key
|| stopped.result_kind !== "wait" || stopped.classification !== "report_replan_not_admitted"
- || inspection.schema_version !== "loopx_turn_journal_inspection_v1" || inspection.ok !== true
- || inspection.goal_matches !== true || inspection.owner_matches !== true || inspection.turn_key_matches !== true
- || inspection.journal_consistent !== true || inspection.journal_status !== "stopped"
- || recovery.action !== "return_existing" || recovery.reinvoke_host !== false
- || recorded.host_invoked !== true || ["state_written", "quota_spent", "scheduler_acknowledged"].some(key => recorded[key] !== false)
+ || inspection.journal_status !== "stopped"
|| route.kind !== "ready_for_host" || route.effective_action !== EffectiveAction.NORMAL_RUN
|| envelope.effective_action !== EffectiveAction.NORMAL_RUN || envelope.replan_action_packet != null
|| action.must_attempt !== true || action.delivery_allowed !== true || selected.todo_id !== identity.todo_id
- || identity.goal_id !== envelope.goal_id || identity.agent_id !== envelope.agent_id
- || identity.goal_id !== requireJsonObject(request.actor, "report actor").goal_id
- || identity.agent_id !== requireJsonObject(request.actor, "report actor").agent_id
- || typeof identity.todo_id !== "string" || !/^todo_[A-Za-z0-9_-]{1,100}$/.test(identity.todo_id)
- || identity.turn_instance_id !== request.turn_instance_id + ".replan") {
+ || typeof identity.todo_id !== "string" || !/^todo_[A-Za-z0-9_-]{1,100}$/.test(identity.todo_id)) {
throw new EffectRuntimeRequestError("report continuation requires its canonical host-only admission stop; retain all model or unknown work");
}
}
@@ -356,7 +434,7 @@ export function admitManagedSessionTurn(params: JsonObject): JsonObject {
|| todo.action_kind !== "generate_periodic_report" || !(todo.status === "open" && !recovery || settlementOnly)) {
throw new EffectRuntimeRequestError("report execution requires the original Session subscription, window and task");
}
- } else if (params.report_settlement_recovery != null) {
+ } else if (params.report_settlement_recovery != null || params.report_frozen_contract_recovery != null) {
throw new EffectRuntimeRequestError("report recovery belongs only to its original calendar report");
}
if (scheduledWork) {
@@ -413,14 +491,48 @@ export function admitManagedSessionTurn(params: JsonObject): JsonObject {
/** Both original conversation hosts use the native journal's no-model recovery. */
function reportSettlementOnly(params: JsonObject, request: JsonObject, todo: JsonObject): boolean {
- if (params.report_settlement_recovery == null) return false;
+ if (params.report_settlement_recovery == null) {
+ if (params.report_frozen_contract_recovery != null) {
+ throw new EffectRuntimeRequestError("original report recovery requires validated remaining effects without a model");
+ }
+ return false;
+ }
const recovery = requireJsonObject(params.report_settlement_recovery, "native original report recovery");
const decision = requireJsonObject(recovery.recovery_decision, "native journal recovery decision");
+ if (params.report_frozen_contract_recovery != null) {
+ const frozen = requireJsonObject(params.report_frozen_contract_recovery, "frozen report contract recovery");
+ const effects = requireJsonObject(recovery.recorded_effects, "native recorded effects");
+ const commonProof = recovery.schema_version === "loopx_turn_journal_inspection_v1" && recovery.ok === true
+ && recovery.goal_matches === true && recovery.owner_matches === true && recovery.turn_key_matches === true
+ && recovery.journal_consistent === true && todo.status === "open"
+ && effects.host_invoked === true && effects.state_written === false
+ && effects.quota_spent === false && effects.scheduler_acknowledged === false
+ && typeof frozen.turn_key === "string" && ENVELOPED_SHA256_PATTERN.test(frozen.turn_key)
+ && frozen.host_attempt_count === 1
+ && frozen.generation_verified === true && frozen.saved_candidate_verified === true;
+ const acceptedFrozen = commonProof && recovery.journal_status === "failed"
+ && Array.isArray(recovery.completed_phases) && recovery.completed_phases.length === 2
+ && recovery.completed_phases[0] === "host_execute" && recovery.completed_phases[1] === "typed_result"
+ && decision.action === "continue" && decision.resume_from === "host_execute" && decision.reinvoke_host === true
+ && frozen.result_kind === "validation_failed" && frozen.validation_stage === "host_result_contract";
+ const acceptedStoppedCandidate = commonProof && recovery.journal_status === "stopped"
+ && Array.isArray(recovery.completed_phases) && recovery.completed_phases.length === 3
+ && recovery.completed_phases[0] === "host_execute" && recovery.completed_phases[1] === "typed_result"
+ && recovery.completed_phases[2] === "validation"
+ && decision.action === "return_existing" && decision.resume_from === null && decision.reinvoke_host === false
+ && frozen.result_kind === "wait" && frozen.classification === "no_typed_host_result"
+ && !Object.hasOwn(frozen, "validation_stage");
+ if ((!acceptedFrozen && !acceptedStoppedCandidate) || params.preparing_model_context === true) {
+ throw new EffectRuntimeRequestError("original report recovery requires validated remaining effects without a model");
+ }
+ return true;
+ }
const cachedGeneration = todo.status === "open" && recovery.journal_status === "failed"
&& decision.action === "continue" && decision.resume_from === "validation"
&& Array.isArray(recovery.completed_phases) && recovery.completed_phases.length === 2
&& recovery.completed_phases[0] === "host_execute" && recovery.completed_phases[1] === "typed_result";
- const completedGeneration = todo.status === "done" && todo.completion_turn_key === request.turn_instance_id
+ const completedGeneration = todo.status === "done"
+ && [request.turn_instance_id, `${String(request.turn_instance_id)}.saved-candidate`].includes(String(todo.completion_turn_key))
&& (decision.action === "continue" && ["durable_writeback", "quota_spend", "scheduler_apply", "scheduler_ack"].includes(String(decision.resume_from))
|| decision.action === "return_existing" && recovery.journal_status === "committed");
const accepted = recovery.schema_version === "loopx_turn_journal_inspection_v1" && recovery.ok === true
@@ -464,8 +576,10 @@ export function normalizeStewardManagedActor(value: unknown): JsonObject {
export function observeStewardNativeExecutor(params: JsonObject): JsonObject {
const binding = requireJsonObject(params.binding, "steward binding");
const session = requireJsonObject(params.session, "original steward Session");
- const supported = session.agent_id === "codex" && (binding.executor_endpoint_id === "codex"
- || boundConversationExecutorMatches(binding, session));
+ const supported = (session.agent_id === "codex" && (binding.executor_endpoint_id === "codex"
+ || boundConversationExecutorMatches(binding, session)))
+ || (binding.executor_endpoint_id === "codex" && session.agent_id === "antigravity"
+ && boundConversationExecutorMatches(binding, session));
return {execution_available: supported, ...(!supported ? {execution_error_code: "unsupported_executor"} : {})};
}
@@ -595,7 +709,7 @@ export function admitStewardManagedTurn(params: JsonObject): JsonObject {
|| original.loopx_execution !== true || original.status !== "completed"
|| original.delta_count !== 0 || original.upstream_turn_id !== null
|| Object.keys(prior).length !== keys.length
- || keys.some(key => JSON.stringify(prior[key]) !== JSON.stringify(request[key]))
+ || keys.some(key => !Object.hasOwn(prior, key) || !sameReportRequestField(key, prior[key], request[key]))
|| result.schema_version !== "loopx_turn_execution_v0" || result.ok !== false
|| result.mode !== "run_once" || result.status !== "failed" || result.result_kind !== "host_failure"
|| result.reason !== "first-party Host runtime rejected: stale_goal_authority"
@@ -625,7 +739,8 @@ export function admitStewardManagedTurn(params: JsonObject): JsonObject {
throw new EffectRuntimeRequestError("launch recovery must retain the original native Turn key");
}
} else if (request.report_window_id !== undefined || request.report_subscription_revision !== undefined
- || request.resume_turn_key !== undefined || params.report_settlement_recovery != null || params.report_launch_recovery != null) {
+ || request.resume_turn_key !== undefined || params.report_settlement_recovery != null
+ || params.report_frozen_contract_recovery != null || params.report_launch_recovery != null) {
throw new EffectRuntimeRequestError("report fields belong only to an original calendar report");
}
let refreshScope = false;
diff --git a/loopx/control_plane/coordination/remote_authority_gateway.py b/loopx/control_plane/coordination/remote_authority_gateway.py
index bc15e6e7b6..a2c16ce0c0 100644
--- a/loopx/control_plane/coordination/remote_authority_gateway.py
+++ b/loopx/control_plane/coordination/remote_authority_gateway.py
@@ -66,6 +66,7 @@ def check_remote_authority_gateway_status(
env={key: os.environ[key] for key in ("PATH", "SystemRoot", "WINDIR", "TMPDIR", "TEMP")
if key in os.environ},
text=True,
+ encoding="utf-8",
capture_output=True,
check=True,
timeout=5,
diff --git a/loopx/control_plane/goals/goal_frontier/__init__.py b/loopx/control_plane/goals/goal_frontier/__init__.py
index a399b8d130..0ba133919f 100644
--- a/loopx/control_plane/goals/goal_frontier/__init__.py
+++ b/loopx/control_plane/goals/goal_frontier/__init__.py
@@ -80,7 +80,7 @@
declared_fallback_gap_from_agent_vision,
parse_fallback_declarations, # noqa: F401
)
-from .long_todo_chain import LONG_TODO_CHAIN_TRIGGER, evaluate_long_todo_chain
+from .long_todo_chain import LONG_TODO_CHAIN_TRIGGER, LongTodoChainAckDecision, evaluate_long_todo_chain # noqa: E501
from .replan_rules import (
GoalFrontierReplanFacts,
GoalFrontierReplanRule,
@@ -90,6 +90,7 @@
_latest_runs_for_goal,
latest_agent_vision_from_runs as latest_agent_vision_from_runs,
latest_agent_vision_from_status_payload,
+ latest_autonomous_replan_ack_from_status_payload,
receipt_bound_replan_ack_history,
latest_missing_vision_checkpoint_from_status_payload,
latest_replan_ack_feedback_from_status_payload,
@@ -900,6 +901,7 @@ def derive_goal_frontier_replan_obligation_from_summaries(
existing_replan_obligation: dict[str, Any] | None,
agent_todo_source_items: list[dict[str, Any]] | None = None,
latest_replan_ack: dict[str, Any] | None = None,
+ prior_rearm_ack: dict[str, Any] | None = None,
current_transition_replan_ack: dict[str, Any] | None = None,
acceptance_gaps: list[dict[str, Any]] | None = None,
monitor_lane_semantically_valid: bool = True,
@@ -972,6 +974,16 @@ def derive_goal_frontier_replan_obligation_from_summaries(
agent_todo_source_items=agent_todo_source_items,
latest_replan_ack=current_transition_replan_ack or latest_replan_ack,
)
+ if (long_chain_ack_decision and not long_chain_ack_decision.acknowledged
+ and long_chain_ack_decision.rearmed_after_obligation_id is None and prior_rearm_ack is not None):
+ _, prior_decision = evaluate_long_todo_chain(
+ agent_todo_summary=agent_todo_summary, agent_counts=agent_counts, frontier_counts=frontier_counts,
+ agent_id=agent_id, agent_todo_source_items=agent_todo_source_items, latest_replan_ack=prior_rearm_ack,
+ )
+ if prior_decision and not prior_decision.acknowledged and prior_decision.rearmed_after_obligation_id is not None:
+ long_chain_ack_decision = LongTodoChainAckDecision(
+ acknowledged=False, rearmed_after_obligation_id=prior_decision.rearmed_after_obligation_id,
+ )
replan_rule = select_goal_frontier_replan_rule(
GoalFrontierReplanFacts(
existing_replan_required=autonomous_replan_is_required(
@@ -1126,7 +1138,7 @@ def derive_goal_frontier_replan_obligation_from_summaries(
if outcome_gap is not None and outcome_gap not in trigger_gaps:
trigger_gaps.append(outcome_gap)
rearmed_after_obligation_id = _acknowledged_replan_obligation_id(
- latest_replan_ack
+ latest_replan_ack or prior_rearm_ack
)
return build_autonomous_replan_obligation_payload(
schema_version=AUTONOMOUS_REPLAN_OBLIGATION_SCHEMA_VERSION,
@@ -1634,6 +1646,14 @@ def build_goal_frontier_projection_context_from_status(
agent_id=agent_id,
registered_agent_ids=registered_agent_ids,
)
+ prior_rearm_ack = None
+ if receipt_bound_replan_obligation_id and latest_agent_replan_ack is None:
+ candidate_prior_ack = latest_autonomous_replan_ack_from_status_payload(
+ status_payload, goal_id=goal_id, agent_id=agent_id,
+ neutral_classifications=neutral_replan_ack_classifications,
+ )
+ if _acknowledged_replan_obligation_id(candidate_prior_ack) != receipt_bound_replan_obligation_id:
+ prior_rearm_ack = candidate_prior_ack
frontier_replan_obligation = derive_goal_frontier_replan_obligation_from_summaries(
user_todo_summary=user_todo_summary,
agent_todo_summary=agent_todo_summary,
@@ -1642,6 +1662,7 @@ def build_goal_frontier_projection_context_from_status(
existing_replan_obligation=replan_obligation,
agent_todo_source_items=agent_todo_source_items,
latest_replan_ack=effective_replan_ack,
+ prior_rearm_ack=prior_rearm_ack,
current_transition_replan_ack=replan_transition_ack,
_frontier_counts=frontier_counts,
acceptance_gaps=acceptance_gaps,
diff --git a/loopx/control_plane/projects/registry_codec.py b/loopx/control_plane/projects/registry_codec.py
index f4a86be3af..15442a1d10 100644
--- a/loopx/control_plane/projects/registry_codec.py
+++ b/loopx/control_plane/projects/registry_codec.py
@@ -14,7 +14,6 @@
from ..content_digest import ENVELOPED_SHA256_PATTERN
from ..goals.activation import require_current_registry_goal_activation
-from ..runtime.document_io import fsync_state_directory
from ..runtime.private_permissions import protect_private_path, require_private_path
from ...file_lock import exclusive_cross_runtime_file_lock
from ...paths import GLOBAL_REGISTRY_FILENAME
@@ -437,6 +436,8 @@ def backup_preimage(self) -> Path:
protect_private_path(directory, directory=True)
target = directory / f"{self._path.name}.{hashlib.sha256(raw).hexdigest()}.bak"
if not target.exists():
+ from ..runtime.document_io import fsync_state_directory
+
with target.open("xb") as stream:
protect_private_path(target)
stream.write(raw)
diff --git a/loopx/control_plane/quota/accounting_artifact_transaction.ts b/loopx/control_plane/quota/accounting_artifact_transaction.ts
index e70a5c7bb9..51a8f7b914 100644
--- a/loopx/control_plane/quota/accounting_artifact_transaction.ts
+++ b/loopx/control_plane/quota/accounting_artifact_transaction.ts
@@ -538,16 +538,41 @@ export async function readQuotaSpendEventFromRun(
return inline;
}
if (typeof run.json_path !== "string" || !run.json_path.trim()) return null;
+ const runsDirReal = await realpath(runsDir);
+ const rawPath = run.json_path.trim();
+ const relocatedCandidate = (() => {
+ if (!isAbsolute(rawPath)) return null;
+ const normalized = resolve(rawPath);
+ const fileName = basename(normalized);
+ const runsParent = dirname(normalized);
+ const goalDir = dirname(runsParent);
+ const goalsDir = dirname(goalDir);
+ if (
+ !fileName || fileName === "." || fileName === ".." || extname(fileName) !== ".json" ||
+ basename(runsParent) !== "runs" || basename(goalDir) !== goalId || basename(goalsDir) !== "goals"
+ ) {
+ return null;
+ }
+ return join(runsDirReal, fileName);
+ })();
+ const initialRelative = relative(runsDirReal, resolve(rawPath));
+ const staysInsideInitial = Boolean(
+ initialRelative && initialRelative !== ".." &&
+ !initialRelative.startsWith(`..${sep}`) && !isAbsolute(initialRelative)
+ );
+ const candidatePath = staysInsideInitial || relocatedCandidate === null
+ ? resolve(rawPath)
+ : relocatedCandidate;
let targetPath: string;
try {
- targetPath = await realpath(resolve(run.json_path.trim()));
+ targetPath = await realpath(candidatePath);
} catch (error) {
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
return null;
}
throw error;
}
- const relativePath = relative(await realpath(runsDir), targetPath);
+ const relativePath = relative(runsDirReal, targetPath);
if (
!relativePath ||
relativePath === ".." ||
diff --git a/loopx/control_plane/quota/execution_preparation.py b/loopx/control_plane/quota/execution_preparation.py
index 43e4cea981..1f694ed755 100644
--- a/loopx/control_plane/quota/execution_preparation.py
+++ b/loopx/control_plane/quota/execution_preparation.py
@@ -144,6 +144,27 @@ class RefreshTaskLeaseUnavailable(ValueError):
code = "refresh_task_lease_unavailable"
+def read_active_task_lease_epoch(
+ *,
+ runtime_root: Path,
+ goal_id: str,
+ todo_id: str,
+ agent_id: str,
+ requested_proof: Mapping[str, Any],
+) -> int | None:
+ """Return the verified current lease epoch for an active resumed execution lease."""
+ snapshot = _read_execution_snapshot(runtime_root, goal_id, todo_id)
+ if snapshot is None:
+ raise RefreshTaskLeaseUnavailable("The selected Todo's execution authority could not be read.")
+ if snapshot.get("handoff_mode") != "hard_lease":
+ return None
+ current = _project_execution_facts(_execution_facts(snapshot, goal_id, todo_id, agent_id))
+ if current.get("state") != "ready" or current.get("lease_proof") != dict(requested_proof):
+ raise RefreshTaskLeaseUnavailable("The resumed Turn's active task lease could not be verified.")
+ epoch = current.get("lease_epoch")
+ return int(epoch) if epoch is not None else None
+
+
def prepared_lease_receipt_fields(
proof: Mapping[str, Any], *, lease_epoch: int | None = None,
) -> dict[str, Any]:
@@ -175,12 +196,194 @@ def quota_prepared_lease_receipt_fields(
return fields
+def _clamp_admitted_lease_version(
+ fields: dict[str, Any], *, prepared_lease: Mapping[str, Any],
+) -> None:
+ """Allow same-generation version advances while keeping epoch-pinned fields comparable."""
+ admitted_epoch = prepared_lease.get("prepared_lease_epoch")
+ admitted_version = prepared_lease.get("prepared_lease_version")
+ if (
+ isinstance(admitted_epoch, int)
+ and not isinstance(admitted_epoch, bool)
+ and isinstance(admitted_version, int)
+ and not isinstance(admitted_version, bool)
+ and fields["prepared_lease_version"] > admitted_version
+ ):
+ fields["prepared_lease_version"] = admitted_version
+
+
+def _recovery_lease_bound(
+ *,
+ prepared_lease: Mapping[str, Any] | None,
+ requested_proof: Mapping[str, Any] | None,
+ current_proof: Mapping[str, Any] | None,
+ settlement: QuotaSettlementReadback,
+ todo_id: str,
+ agent_id: str,
+ validation_recovery_turn_instance_id: str | None,
+) -> bool:
+ """Verify retired `.validation-recovery.` or `.admission-retry` lease ownership for a resumed completion."""
+ if (
+ not validation_recovery_turn_instance_id
+ or prepared_lease is None
+ or requested_proof is None
+ or current_proof is None
+ or not isinstance(settlement.completion_event, Mapping)
+ ):
+ return False
+ requested_key = str(requested_proof.get("idempotency_key") or "")
+ current_key = str(current_proof.get("idempotency_key") or "")
+ completion = settlement.completion_event
+ if (
+ completion.get("event_kind") != "todo_complete"
+ or completion.get("todo_id") != todo_id
+ or completion.get("agent_id") != agent_id
+ or completion.get("run_id") != validation_recovery_turn_instance_id
+ ):
+ return False
+ recovery_bases = tuple(
+ dict.fromkeys(base for base in (validation_recovery_turn_instance_id, requested_key) if base)
+ )
+ if not any(
+ current_key == f"{base}.admission-retry"
+ or current_key.startswith((f"{base}.validation-recovery.", f"{base}.admission-retry."))
+ for base in recovery_bases
+ ):
+ return False
+ admitted_epoch = prepared_lease.get("prepared_lease_epoch")
+ admitted_fields = prepared_lease_receipt_fields(
+ requested_proof,
+ lease_epoch=(
+ admitted_epoch
+ if isinstance(admitted_epoch, int) and not isinstance(admitted_epoch, bool)
+ else None
+ ),
+ )
+ _clamp_admitted_lease_version(admitted_fields, prepared_lease=prepared_lease)
+ return prepared_lease == admitted_fields
+
+
+def _autonomous_replan_guard_bound(
+ *,
+ settlement: QuotaSettlementReadback,
+ current: Mapping[str, Any],
+ current_proof: Mapping[str, Any] | None,
+ requested_proof: Mapping[str, Any] | None,
+) -> bool:
+ """Allow semantic writeback for a todo-bound autonomous replan guard without execution leases."""
+ guard = settlement.semantic_replan_guard
+ receipt = settlement.heartbeat_receipt
+ details = receipt.get("details") if isinstance(receipt, Mapping) else None
+ return (
+ isinstance(guard, Mapping)
+ and guard.get("scope") == "turn_guard"
+ and isinstance(guard.get("selected_obligation_id"), str)
+ and bool(guard.get("selected_obligation_id"))
+ and isinstance(receipt, Mapping)
+ and receipt.get("status") == "autonomous_replan_required"
+ and isinstance(details, Mapping)
+ and details.get("delivery_allowed") is not True
+ and current.get("state") in {"ready", "claim_required"}
+ and (requested_proof is None or (current_proof is not None and requested_proof == current_proof))
+ )
+
+
+def _hard_lease_execution_bound(
+ *,
+ snapshot: Mapping[str, Any],
+ goal_id: str,
+ todo_id: str,
+ agent_id: str,
+ requested_proof: dict[str, Any] | None,
+ settlement: QuotaSettlementReadback,
+ allow_same_generation_lease_renewal: bool,
+ validation_recovery_turn_instance_id: str | None,
+) -> bool:
+ """Check whether hard-lease execution or recovery authority binds this writeback."""
+ prepared_lease = settlement.prepared_lease
+ prepared_required = settlement.prepared_lease_required or prepared_lease is not None
+ todo_open = snapshot["todo"].get("status") == "open"
+ facts = _execution_facts(dict(snapshot), goal_id, todo_id, agent_id)
+ current = _project_execution_facts({**facts, "include_retired_lease_for_writeback": not todo_open})
+ current_proof = (
+ current.get("lease_proof") if current.get("state") == "ready" else current.get("retired_lease_proof")
+ )
+ if not (todo_open or prepared_required):
+ # Older admitted closeouts may predate persisted lease readiness.
+ # Their retired holder still has to match the original binding.
+ admission = settlement.native_child_admission or {}
+ return (
+ current_proof is not None
+ and admission.get("report_permission") in {"new_operation", "existing_operation_only"}
+ and (requested_proof is None or requested_proof == current_proof)
+ )
+ # Legacy prepared receipts can prove their exact version, but cannot
+ # advance to a later version without a recorded generation.
+ lease_epoch = (
+ current.get("lease_epoch") if prepared_lease and "prepared_lease_epoch" in prepared_lease else None
+ )
+ # Recovery can read the retired proof from the same locked
+ # snapshot, including a renewal within the admitted generation.
+ proof = current_proof if not todo_open and requested_proof is None else requested_proof
+ if (
+ allow_same_generation_lease_renewal
+ and lease_epoch is not None
+ and requested_proof is not None
+ and current_proof is not None
+ and current_proof.get("idempotency_key") == requested_proof.get("idempotency_key")
+ and isinstance(current_proof.get("expected_version"), int)
+ and not isinstance(current_proof.get("expected_version"), bool)
+ and isinstance(requested_proof.get("expected_version"), int)
+ and not isinstance(requested_proof.get("expected_version"), bool)
+ and current_proof["expected_version"] >= requested_proof["expected_version"]
+ ):
+ proof = current_proof
+ if prepared_lease is not None and proof is not None and proof == current_proof:
+ observed = prepared_lease_receipt_fields(proof, lease_epoch=lease_epoch)
+ _clamp_admitted_lease_version(observed, prepared_lease=prepared_lease)
+ if prepared_lease == observed:
+ return True
+ if not todo_open and _recovery_lease_bound(
+ prepared_lease=prepared_lease,
+ requested_proof=requested_proof,
+ current_proof=current_proof,
+ settlement=settlement,
+ todo_id=todo_id,
+ agent_id=agent_id,
+ validation_recovery_turn_instance_id=validation_recovery_turn_instance_id,
+ ):
+ return True
+ return not prepared_required and prepared_lease is None and _autonomous_replan_guard_bound(
+ settlement=settlement, current=current, current_proof=current_proof, requested_proof=requested_proof,
+ )
+
+
+def _todo_superseded_for_turn(
+ settlement: QuotaSettlementReadback, *, todo_id: str, agent_id: str,
+) -> bool:
+ """Return True when the exact Turn's todo_supersede receipt already closed the Todo."""
+ supersede = settlement.supersede_event
+ settlement_turn_id = (
+ settlement.identity.value.turn_instance_id if settlement.identity.value is not None else None
+ )
+ return (
+ isinstance(supersede, Mapping)
+ and supersede.get("event_kind") == "todo_supersede"
+ and supersede.get("status") == "done"
+ and supersede.get("todo_id") == todo_id
+ and supersede.get("agent_id") == agent_id
+ and (settlement_turn_id is None or supersede.get("run_id") == settlement_turn_id)
+ )
+
+
@contextmanager
def guard_first_writeback_execution(
*, runtime_root: Path, registry_path: Path, goal_id: str,
todo_id: str, agent_id: str, requested_proof: dict[str, Any] | None,
settlement: QuotaSettlementReadback,
locked_goal: dict[str, Any] | None,
+ allow_same_generation_lease_renewal: bool = False,
+ validation_recovery_turn_instance_id: str | None = None,
) -> Iterator[None]:
prepared_lease = settlement.prepared_lease
prepared_required = settlement.prepared_lease_required or prepared_lease is not None
@@ -204,42 +407,26 @@ def guard_first_writeback_execution(
if prepared_required and snapshot.get("handoff_mode") != "hard_lease":
raise RefreshTaskLeaseUnavailable("The Todo's original hard-lease authority is unavailable.")
if snapshot.get("handoff_mode") == "hard_lease":
- todo_open = snapshot["todo"].get("status") == "open"
- facts = _execution_facts(snapshot, goal_id, todo_id, agent_id)
- current = _project_execution_facts({**facts, "include_retired_lease_for_writeback": not todo_open})
- current_proof = (current.get("lease_proof") if current.get("state") == "ready"
- else current.get("retired_lease_proof"))
- if todo_open or prepared_required:
- # Legacy prepared receipts can prove their exact version, but cannot
- # advance to a later version without a recorded generation.
- lease_epoch = (current.get("lease_epoch")
- if prepared_lease and "prepared_lease_epoch" in prepared_lease else None)
- # Recovery can read the retired proof from the same locked
- # snapshot, including a renewal within the admitted generation.
- proof = current_proof if not todo_open and requested_proof is None else requested_proof
- execution_bound = False
- if prepared_lease is not None and proof is not None and proof == current_proof:
- observed = prepared_lease_receipt_fields(proof, lease_epoch=lease_epoch)
- admitted_version = prepared_lease["prepared_lease_version"]
- if (
- not todo_open and lease_epoch is not None
- and observed["prepared_lease_version"] > admitted_version
- ):
- observed["prepared_lease_version"] = admitted_version
- execution_bound = prepared_lease == observed
- else:
- # Older admitted closeouts may predate persisted lease readiness.
- # Their retired holder still has to match the original binding.
- admission = settlement.native_child_admission or {}
- execution_bound = (current_proof is not None
- and admission.get("report_permission") in {"new_operation", "existing_operation_only"}
- and (requested_proof is None or requested_proof == current_proof))
- if not execution_bound:
+ if not _hard_lease_execution_bound(
+ snapshot=snapshot,
+ goal_id=goal_id,
+ todo_id=todo_id,
+ agent_id=agent_id,
+ requested_proof=requested_proof,
+ settlement=settlement,
+ allow_same_generation_lease_renewal=allow_same_generation_lease_renewal,
+ validation_recovery_turn_instance_id=validation_recovery_turn_instance_id,
+ ):
raise RefreshTaskLeaseUnavailable(
"The original writeback needs a current task lease recorded for this Turn. "
"Read the same Todo and Turn through its current guard before retrying."
)
- if snapshot["todo"].get("status") == "done" and prepared_required and settlement.completion_event is None:
+ if (
+ snapshot["todo"].get("status") == "done"
+ and prepared_required
+ and settlement.completion_event is None
+ and not _todo_superseded_for_turn(settlement, todo_id=todo_id, agent_id=agent_id)
+ ):
raise RefreshTaskLeaseUnavailable("The completed Todo is missing this Turn's completion receipt.")
goal = locked_goal
if goal is None:
@@ -268,12 +455,21 @@ def apply_selected_todo_execution_preparation(
receipt_bound: bool = False,
) -> None:
selected = payload.get("selected_todo")
+ scoped_fallback_admitted = (
+ isinstance(payload.get("scoped_user_gate_fallback"), Mapping)
+ and obligation is not None
+ and obligation.get("delivery_allowed") is True
+ )
if (
not isinstance(selected, dict)
or payload.get("should_run") is not True
# Planning and recovery can retain a Todo without authorizing its
- # business execution. Preserve their existing typed route.
- or payload.get("normal_delivery_allowed") is not True
+ # business execution. Preserve their existing typed route, while still
+ # preparing scoped user-gate fallback work whose delivery is admitted.
+ or (
+ payload.get("normal_delivery_allowed") is not True
+ and not scoped_fallback_admitted
+ )
or action_portfolio_requires_explicit_selection(payload)
):
return
diff --git a/loopx/control_plane/quota/heartbeat_receipt.py b/loopx/control_plane/quota/heartbeat_receipt.py
index 3874436caa..364a5ac6d6 100644
--- a/loopx/control_plane/quota/heartbeat_receipt.py
+++ b/loopx/control_plane/quota/heartbeat_receipt.py
@@ -269,6 +269,15 @@ def ensure_turn_heartbeat_settlement_receipt(
# identity before transport-specific start admission. Its owner may
# refuse admission without writing a receipt or claiming work.
before_bind()
+ if task_lease_proof is not None and identity.todo_id is not None:
+ from .execution_preparation import prepared_lease_receipt_fields
+
+ rebound_lease_fields = prepared_lease_receipt_fields(
+ task_lease_proof, lease_epoch=task_lease_epoch,
+ )
+ else:
+ rebound_lease_fields = None
+ preserved_details: dict[str, object] = {}
if effective is not None:
if observed is not None:
effective_details = effective.get("details")
@@ -277,9 +286,15 @@ def ensure_turn_heartbeat_settlement_receipt(
if isinstance(effective_details, Mapping)
else {}
)
+ preserved_details = dict(effective_details)
+ lease_unchanged = rebound_lease_fields is None or (
+ all(effective_details.get(k) == v for k, v in rebound_lease_fields.items())
+ and (task_lease_epoch is not None or "prepared_lease_epoch" not in effective_details)
+ )
if not semantic_replan_guard_scoped:
- return effective
- if "semantic_replan_obligation_id" in effective_details:
+ if lease_unchanged:
+ return effective
+ elif "semantic_replan_obligation_id" in effective_details:
raw_existing_guard = effective_details.get(
"semantic_replan_obligation_id"
)
@@ -308,9 +323,11 @@ def ensure_turn_heartbeat_settlement_receipt(
raise HeartbeatReceiptIdentityConflictError(
"Turn heartbeat receipt belongs to another capability guard"
)
- return effective
+ if lease_unchanged:
+ return effective
details = {
+ **preserved_details,
"turn_instance_id": identity.turn_instance_id,
"todo_id": str(identity.todo_id or ""),
"replan_obligation_id": str(identity.replan_obligation_id or ""),
@@ -321,10 +338,10 @@ def ensure_turn_heartbeat_settlement_receipt(
}
if identity.todo_id is not None and (prepared_lease_required or task_lease_proof is not None):
details["prepared_lease_required"] = True
- if task_lease_proof is not None and identity.todo_id is not None:
- from .execution_preparation import prepared_lease_receipt_fields
-
- details.update(prepared_lease_receipt_fields(task_lease_proof, lease_epoch=task_lease_epoch))
+ if rebound_lease_fields is not None:
+ details.update(rebound_lease_fields)
+ if task_lease_epoch is None:
+ details.pop("prepared_lease_epoch", None)
if semantic_replan_guard_scoped:
details["semantic_replan_obligation_id"] = (
normalized_semantic_replan_obligation_id or ""
diff --git a/loopx/control_plane/quota/monitor_poll.py b/loopx/control_plane/quota/monitor_poll.py
index 60858e69bf..daeb9e406d 100644
--- a/loopx/control_plane/quota/monitor_poll.py
+++ b/loopx/control_plane/quota/monitor_poll.py
@@ -50,10 +50,6 @@ def __init__(self, message: str, *, diagnostic_code: str) -> None:
self.diagnostic_code = diagnostic_code
-def _now_local() -> str:
- return now_local_iso()
-
-
def _mapping(value: object) -> Payload:
return {str(key): entry for key, entry in value.items()} if isinstance(value, Mapping) else {}
@@ -254,6 +250,7 @@ def _decision_packet(
registry_path: Path | None = None,
runtime_root: Path | None = None,
authorized_due_monitor_poll: bool | None = None,
+ todo_id_explicit: bool = False,
) -> Payload:
before_dict = _mapping(before)
agent_id = quota_decision_agent_id(before_dict)
@@ -266,6 +263,8 @@ def _decision_packet(
todo_id=todo_id,
target_key=target_key,
)
+ if registry_due:
+ registry_due["todo_id_explicit"] = todo_id_explicit and todo_id is not None
if authorized_due_monitor_poll is False:
due_candidates = []
registry_due = {}
@@ -477,7 +476,7 @@ def build_quota_monitor_poll_event(
runtime_root=None,
goal_id=goal_id,
source=source,
- generated_at=generated_at or _now_local(),
+ generated_at=generated_at or now_local_iso(),
execute=False,
expected_index_digest=None,
turn_instance_id=normalized_turn_id,
@@ -804,7 +803,7 @@ def record_quota_monitor_poll_for_decision(
*,
goal_id: str,
after_decision: Callable[[Payload], PayloadView],
- render_markdown: Callable[[Payload], str],
+ render_markdown: Callable[[Payload], str] | None = None, # Retained for existing callers.
registry_path: Path | None = None,
execute: bool = False,
source: str = DEFAULT_SLOT_SPEND_SOURCE,
@@ -838,8 +837,8 @@ def record_quota_monitor_poll_for_decision(
status_reloader: Callable[[], PayloadView] | None = None,
goal_ref: PayloadView | None = None,
recovery_effect_id: str | None = None,
+ todo_id_explicit: bool = True,
) -> Payload:
- del render_markdown
safe_todo_id, safe_target_key, safe_result_hash, normalized_turn_id = _normalized_poll_targets(
todo_id=todo_id, target_key=target_key, result_hash=result_hash,
turn_instance_id=turn_instance_id,
@@ -896,6 +895,7 @@ def record_quota_monitor_poll_for_decision(
target_key=safe_target_key,
registry_path=registry_path,
runtime_root=runtime_root,
+ todo_id_explicit=todo_id_explicit,
)
if auxiliary_settlement_todo is not None:
decision["auxiliary_settlement_todo"] = {
@@ -928,7 +928,7 @@ def record_quota_monitor_poll_for_decision(
task_lease_idempotency_key=task_lease_idempotency_key,
task_lease_expected_version=task_lease_expected_version,
)
- generated_at = _now_local()
+ generated_at = now_local_iso()
def failure(reason: str, *, include_capability_retry: bool = False) -> Payload:
payload = _monitor_poll_failure(
diff --git a/loopx/control_plane/quota/monitor_poll_admission.ts b/loopx/control_plane/quota/monitor_poll_admission.ts
index 7ce319ae3f..9635baf565 100644
--- a/loopx/control_plane/quota/monitor_poll_admission.ts
+++ b/loopx/control_plane/quota/monitor_poll_admission.ts
@@ -66,6 +66,7 @@ function dueMonitorAllowed(
decision: MonitorDecision,
observation: MonitorObservation,
facts: MonitorLaneFacts,
+ historicalAdmission: boolean,
): boolean {
if (!facts.must_attempt_work) return false;
const candidate = decision.due_monitor_candidates.find((item) => candidateMatches(item, observation));
@@ -73,7 +74,11 @@ function dueMonitorAllowed(
const registryAllowed = registry.due === true &&
registry.claimed_by === decision.agent_id && candidateMatches(registry, observation);
if (facts.obligation === "attempt_due_monitor") {
- return Boolean(candidate) || (!decision.requires_user_action && registryAllowed);
+ // A key lookup establishes observation identity. Only an explicit Todo can
+ // select another due Monitor. Older frozen admissions keep their basis.
+ const explicitRegistryTarget = (observation.todo_id !== null && registry.todo_id_explicit === true) ||
+ (historicalAdmission && registry.todo_id_explicit === undefined);
+ return Boolean(candidate) || (!decision.requires_user_action && registryAllowed && explicitRegistryTarget);
}
if (facts.reason_codes.has("due_monitor_context") && candidate) return true;
return facts.reason_codes.has("due_monitor_context") && registryAllowed;
@@ -157,7 +162,7 @@ async function auxiliaryMonitorAllowed(
// Retain ordinary quota/due-work admission and capability/gate projections;
// lifecycle lookup must not become a second should-run bypass.
return monitorGateAllowsObservation(decision) &&
- dueMonitorAllowed(decision, observation, monitorLaneFacts(decision)) &&
+ dueMonitorAllowed(decision, observation, monitorLaneFacts(decision), historicalAdmission) &&
monitor.due === true && candidateMatches(monitor, observation) &&
(monitor.claimed_by == null || monitor.claimed_by === decision.agent_id);
}
@@ -188,7 +193,7 @@ export async function admission(
const blocked = blockedSuccessorAllowed(request.decision);
const external = externalMonitorAllowed(request.decision, laneFacts);
const auxiliary = await auxiliaryMonitorAllowed(request, historicalAdmission);
- const due = auxiliary ?? dueMonitorAllowed(request.decision, request.observation, laneFacts);
+ const due = auxiliary ?? dueMonitorAllowed(request.decision, observation, laneFacts, historicalAdmission);
if (auxiliary === false) {
throw new EffectRuntimeRequestError(
"auxiliary monitor-poll requires its own due Monitor target", "monitor_poll_admission_rejected",
diff --git a/loopx/control_plane/quota/progressive_context.ts b/loopx/control_plane/quota/progressive_context.ts
index 7ade3bce89..0672dbded0 100644
--- a/loopx/control_plane/quota/progressive_context.ts
+++ b/loopx/control_plane/quota/progressive_context.ts
@@ -763,8 +763,10 @@ export function prepareProgressiveContext(request: JsonObject, plan?: JsonObject
return contextProjectionChanges(source, {context: required.context, projected: required.projected}, values, undefined, true);
}
+type InitialOpeningSplit = {controls: Map; details: string[]; linkedDetail: string};
+
function prepareInitialOpening(context: JsonObject, groups: ReturnType,
- scope?: Path): {boundary: string; detail: string; controls: string[]; checkpointEntries?: string} | undefined {
+ scope?: Path): InitialOpeningSplit | undefined {
const cli = jsonObject(jsonObject(context.interaction_contract)?.cli_channel);
const candidate = "/selected_todo";
if (cli?.selection_required === true && groups.opening.includes(candidate)) {
@@ -777,31 +779,50 @@ function prepareInitialOpening(context: JsonObject, groups: ReturnType= 0) groups.opening.push(...groups.remaining.splice(index, 1));
}
- return {boundary: candidate, detail: candidate, controls: []};
+ return {controls: new Map([[candidate, []]]), details: [candidate], linkedDetail: candidate};
}
if (!scope) return undefined;
const parent = scope.slice(0, -1), detail = scope.at(-1)!;
const body = jsonObject(at(context, parent)), boundary = pointer(parent);
- if (!groups.opening.includes(boundary) || !body
- || !Array.isArray(body[detail]) || !body[detail].length) return undefined;
+ const controls = new Map();
+ const details: string[] = [];
+ let linkedDetail: string | undefined;
+ if (groups.opening.includes(boundary) && body && Array.isArray(body[detail]) && body[detail].length) {
+ const boundaryControls = Object.keys(body).filter(key => key !== detail).map(key => pointer([...parent, key]));
+ linkedDetail = pointer(scope);
+ details.push(linkedDetail);
+ controls.set(boundary, boundaryControls);
+ const checkpointPath = [...parent, "checkpointed_boundary_authority"];
+ const checkpoint = jsonObject(at(context, checkpointPath));
+ if (checkpoint?.schema_version === "checkpointed_boundary_authority_v0"
+ && Array.isArray(checkpoint.active_write_scope) && Number.isSafeInteger(checkpoint.active_count)
+ && Number.isSafeInteger(checkpoint.inactive_count) && Array.isArray(checkpoint.entries)
+ && checkpoint.entries.length) {
+ // Keep the current approval summary in the opening. Read the exact
+ // entries and write scope from the same capture before work.
+ const checkpointIndex = boundaryControls.indexOf(pointer(checkpointPath));
+ boundaryControls.splice(checkpointIndex, 1, ...Object.keys(checkpoint).filter(key => key !== "entries")
+ .map(key => pointer([...checkpointPath, key])));
+ details.push(pointer([...checkpointPath, "entries"]));
+ }
+ }
+ const selected = jsonObject(context.selected_todo), requested = "required_write_scopes";
+ const scopes = selected?.[requested];
+ if (groups.opening.includes(candidate) && selected?.schema_version === "quota_selected_todo_v0"
+ && Array.isArray(scopes) && scopes.length && scopes.every(value => typeof value === "string" && value)) {
+ controls.set(candidate, Object.keys(selected).filter(key => key !== requested)
+ .map(key => pointer(["selected_todo", key])));
+ // Current requested scopes need this read even when the Goal's exact
+ // write scope can carry forward from an earlier observation.
+ linkedDetail = pointer(["selected_todo", requested]);
+ details.push(linkedDetail);
+ }
+ if (!linkedDetail) return undefined;
// Current readiness belongs with the decision. Reorder only the new pinned
// reader, so an explicit historical initial-read cursor still replays.
const preparation = groups.remaining.indexOf("/interaction_contract/cli_channel/execution_preparation");
if (preparation >= 0) groups.opening.push(...groups.remaining.splice(preparation, 1));
- const controls = Object.keys(body).filter(key => key !== detail).map(key => pointer([...parent, key]));
- const checkpointPath = [...parent, "checkpointed_boundary_authority"];
- const checkpoint = jsonObject(at(context, checkpointPath));
- const opening = {boundary, detail: pointer(scope), controls};
- if (checkpoint?.schema_version !== "checkpointed_boundary_authority_v0"
- || !Array.isArray(checkpoint.active_write_scope) || !Number.isSafeInteger(checkpoint.active_count)
- || !Number.isSafeInteger(checkpoint.inactive_count) || !Array.isArray(checkpoint.entries)
- || !checkpoint.entries.length) return opening;
- // Keep the current approval summary in the opening. The exact entries and
- // write scope are both required from the same capture before work.
- const checkpointIndex = controls.indexOf(pointer(checkpointPath));
- controls.splice(checkpointIndex, 1, ...Object.keys(checkpoint).filter(key => key !== "entries")
- .map(key => pointer([...checkpointPath, key])));
- return {...opening, checkpointEntries: pointer([...checkpointPath, "entries"])};
+ return {controls, details, linkedDetail};
}
/** Invocation-local navigation, not captured input or a read/retention receipt.
@@ -818,8 +839,8 @@ function nativeContextReadPlan(context: JsonObject, pointers: string[],
let {opening, remaining} = groups;
if (openingSplit) {
// The reader chooses the split using the actual serialized opening.
- opening = opening.flatMap(field => field === openingSplit.boundary ? openingSplit.controls : [field]);
- remaining = [openingSplit.detail, ...(openingSplit.checkpointEntries ? [openingSplit.checkpointEntries] : []), ...remaining];
+ opening = opening.flatMap(field => openingSplit.controls.get(field) ?? [field]);
+ remaining = [...openingSplit.details, ...remaining];
}
const ordinaryRows = openingSplit ? (plan.selections as ReadSelection[]).reduce((end, row, index) =>
groups.opening.some(field => field === row.pointer || field.startsWith(row.pointer + "/"))
@@ -827,7 +848,7 @@ function nativeContextReadPlan(context: JsonObject, pointers: string[],
// Keep the ordinary fallback in the same order as the observed selection.
return {initial: true, plan, ...(opening.length && remaining.length
? {opening: select(opening), remaining: select(remaining)} : {}),
- ...(openingSplit ? {linked_opening_detail: openingSplit.detail, ordinary_opening_rows: ordinaryRows} : {})};
+ ...(openingSplit ? {linked_opening_detail: openingSplit.linkedDetail, ordinary_opening_rows: ordinaryRows} : {})};
}
export function initialContextReadPointers(context: JsonObject, decisionFields: readonly string[]): string[] {
diff --git a/loopx/control_plane/quota/settlement.py b/loopx/control_plane/quota/settlement.py
index 99747d82bf..d3a37e0fc5 100644
--- a/loopx/control_plane/quota/settlement.py
+++ b/loopx/control_plane/quota/settlement.py
@@ -175,6 +175,7 @@ class QuotaSettlementReadback:
completion_event: dict[str, Any] | None
monitor_phase: ReceiptBoundMonitorPhase | None
replay_phase: ReceiptBoundReplayPhase | None
+ supersede_event: dict[str, Any] | None = None
refresh_recovery: dict[str, Any] | None = None
external_delivery: dict[str, Any] | None = None
progress: dict[str, Any] | None = None
@@ -575,6 +576,7 @@ def read_heartbeat_settlement(
writeback_event=_optional_readback_record(payload.get("writeback_event")),
spend_event=_optional_readback_record(payload.get("spend_event")),
completion_event=_optional_readback_record(payload.get("completion_event")),
+ supersede_event=_optional_readback_record(payload.get("supersede_event")),
monitor_phase=(
ReceiptBoundMonitorPhase(str(monitor_phase))
if monitor_phase is not None
diff --git a/loopx/control_plane/quota/settlement_cli.py b/loopx/control_plane/quota/settlement_cli.py
index c73eb4b760..39adb33660 100644
--- a/loopx/control_plane/quota/settlement_cli.py
+++ b/loopx/control_plane/quota/settlement_cli.py
@@ -551,7 +551,7 @@ def quota_rollout_details(
todo_id=todo_id,
selected_todo_id=details["selected_todo_id"],
agent_id=getattr(args, "agent_id", None),
- delivery_admitted=closeout_required and payload.get("normal_delivery_allowed") is True,
+ delivery_admitted=closeout_required,
))
return details
diff --git a/loopx/control_plane/quota/settlement_readback.ts b/loopx/control_plane/quota/settlement_readback.ts
index 9f046b5009..ee4da148d5 100644
--- a/loopx/control_plane/quota/settlement_readback.ts
+++ b/loopx/control_plane/quota/settlement_readback.ts
@@ -1083,23 +1083,26 @@ function resolveIdentity(
details(guard).pending_action_selection_todo_id,
);
if (!originalTodoId && !originalReplanId && !requestSubject) {
- return failedIdentity("the original Turn guard has no settlement binding", "receipt_unbound");
- }
- 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(
- "settlement binding does not match: requested binding differs from the original Turn guard",
- "identity_mismatch",
- );
+ if (todoId === null && replanObligationId === null) {
+ return failedIdentity("the original Turn guard has no settlement binding", "receipt_unbound");
+ }
+ } else {
+ 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(
+ "settlement binding does not match: requested binding differs from the original Turn guard",
+ "identity_mismatch",
+ );
+ }
+ todoId = originalTodoId;
+ replanObligationId = originalReplanId;
}
- todoId = originalTodoId;
- replanObligationId = originalReplanId;
}
if (!agentId || !turnInstanceId || [todoId, replanObligationId, requestSubject].filter(Boolean).length !== 1) {
return failedIdentity(
@@ -1352,7 +1355,7 @@ function readPendingCloseoutFromRequest(
// immutable snapshot and admitted owner. Maintenance Run classifications
// cannot conceal the guard's debt or select another Agent's work.
const result = readQuotaSettlementFromRequest({...request, turn_instance_id: turn,
- pending_closeout: false, resolve_original_binding: true}, snapshot, false);
+ pending_closeout: false, resolve_original_binding: true}, snapshot, request.turn_instance_id !== null);
const identity = jsonObject(jsonObject(jsonObject(result.identity)?.result)?.value);
const admission = jsonObject(result.native_child_admission);
const originalReceipt = jsonObject(result.heartbeat_receipt);
@@ -1620,6 +1623,7 @@ function readQuotaSettlementFromRequest(
writeback_event: includeRecords ? writebackEvent : null,
spend_event: includeRecords ? spendEvent : null,
completion_event: includeRecords ? completionEvent : null,
+ ...(includeRecords && supersedeEvent ? {supersede_event: supersedeEvent} : {}),
monitor_phase: monitorPhase,
replay_phase: replayPhase,
replan_execution_pending: replayPhase === "open" &&
diff --git a/loopx/control_plane/quota/turn_envelope.py b/loopx/control_plane/quota/turn_envelope.py
index 23cf9f685f..ab55982167 100644
--- a/loopx/control_plane/quota/turn_envelope.py
+++ b/loopx/control_plane/quota/turn_envelope.py
@@ -110,6 +110,10 @@ def _turn_envelope_result(
tokens = source_number_tokens(params)
if tokens:
request["source_number_tokens"] = tokens
+ if operation == "attach_complete_turn_context" and isinstance(params.get("context_additions"), Mapping):
+ addition_tokens = source_number_tokens(params["context_additions"])
+ if addition_tokens:
+ request["context_addition_tokens"] = addition_tokens
if operation == "capability_automation_source" and set(params) == {"payload"} and isinstance(payload, Mapping):
transport = pack_turn_source(payload)
if transport is not None:
@@ -331,6 +335,22 @@ def compose_turn_decision(
return _apply_context_projection_changes(source, result)
+def attach_complete_turn_context(
+ payload: Mapping[str, Any],
+ context_additions: Mapping[str, Any],
+) -> dict[str, Any]:
+ """Seal validated turn-context fields onto an already-composed decision."""
+ source = dict(payload)
+ additions = dict(context_additions)
+ result = _turn_envelope_result(
+ "attach_complete_turn_context",
+ payload=source,
+ context_additions=additions,
+ source_edits=True,
+ )
+ return _apply_context_projection_changes({**source, **additions}, result)
+
+
def is_legacy_saved_quota_decision(payload: Mapping[str, Any]) -> bool:
"""Recognize saved raw/v0 carriers that predate complete-source checking."""
return (
diff --git a/loopx/control_plane/quota/turn_envelope.ts b/loopx/control_plane/quota/turn_envelope.ts
index a39e9e5ba9..46f2c1d3db 100644
--- a/loopx/control_plane/quota/turn_envelope.ts
+++ b/loopx/control_plane/quota/turn_envelope.ts
@@ -2046,6 +2046,20 @@ function selectedWorkLaunchBasis(decision: JsonObject, verifiedTextSha256?: stri
required_capabilities: selected.required_capabilities ?? []});
}
+function sealCompleteTurnDecision(
+ source: JsonObject,
+ schedulerExecutionArgs: string,
+ identity: ReturnType,
+): JsonObject {
+ requireCompleteDecisionSource(source);
+ const result: JsonObject = {...source, schema_version: COMPLETE_TURN_DECISION_VERSION,
+ contract_binding: {scheduler_execution_args: schedulerExecutionArgs,
+ hash_codec: COMPLETE_TURN_HASH_CODEC,
+ source_decision_hash: completeSourceHash(source, identity)}};
+ result.contract_binding = {...object(result.contract_binding), content_hash: completeSourceHash(result, identity)};
+ return result;
+}
+
export function composeCompleteTurnDecision(value: unknown): JsonObject {
const request = requireJsonObject(value, "complete Turn decision request");
const payload = requireJsonObject(request.payload, "complete Turn decision source");
@@ -2114,13 +2128,38 @@ export function composeCompleteTurnDecision(value: unknown): JsonObject {
source = referenceCurrentTaskTitle(source);
source = referenceVisionContinuation(source, sourceIdentity);
source = referenceQuotaSummaryLanes(source, sourceIdentity);
- requireCompleteDecisionSource(source);
- const result: JsonObject = {...source, schema_version: COMPLETE_TURN_DECISION_VERSION,
- contract_binding: {scheduler_execution_args: request.scheduler_execution_args,
- hash_codec: COMPLETE_TURN_HASH_CODEC,
- source_decision_hash: completeSourceHash(source, sourceIdentity)}};
- result.contract_binding = {...object(result.contract_binding), content_hash: completeSourceHash(result, sourceIdentity)};
- return result;
+ return sealCompleteTurnDecision(source, request.scheduler_execution_args, sourceIdentity);
+}
+
+const CONTEXT_ADDITION_FIELD_PATTERN = /^(?:turn_context_contributions|turn_context_hook_failures|[a-z][a-z0-9_]{2,95})$/;
+
+export function attachCompleteTurnContext(value: unknown): JsonObject {
+ const request = requireJsonObject(value, "complete Turn context request");
+ const decision = requireCompleteTurnDecision(request.payload, request.source_number_tokens);
+ const schedulerExecutionArgs = object(decision.contract_binding).scheduler_execution_args;
+ if (typeof schedulerExecutionArgs !== "string") {
+ throw new EffectRuntimeRequestError("Turn scheduler execution binding must be a string");
+ }
+ const additions = requireJsonObject(request.context_additions, "complete Turn context additions");
+ const additionTokens = Array.isArray(request.context_addition_tokens)
+ ? request.context_addition_tokens : [];
+ const source: JsonObject = {...decision};
+ delete source.schema_version;
+ delete source.contract_binding;
+ for (const [key, item] of Object.entries(additions)) {
+ if (key === "schema_version" || key === "contract_binding"
+ || !CONTEXT_ADDITION_FIELD_PATTERN.test(key)
+ || Object.hasOwn(source, key)) {
+ throw new EffectRuntimeRequestError(`complete Turn context cannot replace or add invalid field: ${key}`);
+ }
+ source[key] = item;
+ }
+ const combinedTokens = [
+ ...(Array.isArray(request.source_number_tokens) ? request.source_number_tokens : []),
+ ...additionTokens,
+ ];
+ const sourceIdentity = contextSourceIdentity(source, combinedTokens);
+ return sealCompleteTurnDecision(source, schedulerExecutionArgs, sourceIdentity);
}
export function buildTurnEnvelope(value: unknown): JsonObject {
@@ -2510,9 +2549,9 @@ function nativeReplanPresentation(request: JsonObject): JsonObject | undefined {
if (projection.sha256 === undefined || request.surface !== "complete_turn" &&
(request.surface !== "quota_packet" || Object.hasOwn(source, "schema_version") ||
Object.hasOwn(source, "contract_binding"))) return undefined;
- // Preserve a bound reversal in the opening, before candidate choice.
- if (scopedReplanYardstickReversal(source)) return undefined;
}
+ // Preserve a bound reversal in the opening, before candidate choice.
+ if (scopedReplanYardstickReversal(source)) return undefined;
const packet = object(source.replan_action_packet);
const review = object(object(source.autonomous_replan_obligation).replan_context);
// Existing staged files are presentation inputs. Do not derive a new source
@@ -2628,6 +2667,13 @@ export function evaluateTurnEnvelope(value: unknown): JsonObject {
return request.source_edits === true
? contextProjectionChanges(requireJsonObject(request.payload, "complete Turn source"), result) : result;
}
+ if (operation === "attach_complete_turn_context") {
+ const result = attachCompleteTurnContext(request);
+ if (request.source_edits !== true) return result;
+ const payload = requireJsonObject(request.payload, "complete Turn decision");
+ const additions = requireJsonObject(request.context_additions, "complete Turn context additions");
+ return contextProjectionChanges({...payload, ...additions}, result);
+ }
if (operation === "complete_consumer") {
requireCompleteTurnDecision(request.payload, request.source_number_tokens);
return {ok: true};
diff --git a/loopx/control_plane/quota/unsettled_host_turn.py b/loopx/control_plane/quota/unsettled_host_turn.py
index 60cff9bc65..b4efce2ac0 100644
--- a/loopx/control_plane/quota/unsettled_host_turn.py
+++ b/loopx/control_plane/quota/unsettled_host_turn.py
@@ -129,10 +129,10 @@ def restore_receipt_bound_todo_source(
# owner still validates the original source identity and timestamps.
from ..todos.list_readback import list_goal_todos
- source = list_goal_todos(
+ goal_todos = list_goal_todos(
registry_path=registry_path, runtime_root_arg=str(runtime_root), goal_id=goal_id,
)
- todo_fields = {key: source[key] for key in ("user_todos", "agent_todos")}
+ todo_fields = {key: goal_todos[key] for key in ("user_todos", "agent_todos")}
queue = payload.get("attention_queue") or {}
return {**payload, "attention_queue": {**queue, "items": [
{**row, **todo_fields,
@@ -598,6 +598,38 @@ def _apply_recovery_projection(
)
if attention:
payload["effective_action"] = EffectiveAction.BLOCKED_WAIT.value
+ recovery_lease_proof: dict[str, Any] | None = None
+ if not attention and recovery.get("repair") == "monitor_poll":
+ from .execution_preparation import project_selected_todo_execution_preparation
+
+ agent_identity = payload.get("agent_identity")
+ agent_id = (
+ agent_identity.get("agent_id")
+ if isinstance(agent_identity, Mapping)
+ else payload.get("agent_id")
+ )
+ prior_todo_id = recovery.get("binding_id")
+ if (
+ isinstance(prior_todo_id, str)
+ and prior_todo_id.strip()
+ and isinstance(payload.get("goal_id"), str)
+ and payload["goal_id"].strip()
+ and isinstance(agent_id, str)
+ and agent_id.strip()
+ ):
+ preparation = project_selected_todo_execution_preparation(
+ runtime_root=runtime_root,
+ goal_id=payload["goal_id"],
+ selected_todo={"todo_id": prior_todo_id},
+ agent_id=agent_id,
+ )
+ if (
+ isinstance(preparation, Mapping)
+ and preparation.get("handoff_mode") == "hard_lease"
+ and preparation.get("state") == "ready"
+ and isinstance(preparation.get("lease_proof"), Mapping)
+ ):
+ recovery_lease_proof = dict(preparation["lease_proof"])
interaction_contract, arbitration = _build_interaction_contract_projection(
payload,
agent_scope_frontier_actions=[action.value for action in AgentScopeFrontierAction],
@@ -606,6 +638,7 @@ def _apply_recovery_projection(
turn_instance_id=current_turn_instance_id,
runtime_root=str(runtime_root),
registry_path=str(registry_path),
+ recovery_lease_proof=recovery_lease_proof,
)
agent_channel = interaction_contract.get("agent_channel")
if isinstance(agent_channel, dict):
diff --git a/loopx/control_plane/status/goal_source.py b/loopx/control_plane/status/goal_source.py
index 01aadf8d7c..25746ea128 100644
--- a/loopx/control_plane/status/goal_source.py
+++ b/loopx/control_plane/status/goal_source.py
@@ -386,7 +386,7 @@ def _read_page(self, goal_id, cursor, *, scope, load):
raise
with self._lock:
if pages is not self._pages:
- raise SourceScopeError("The original Goal source became unavailable during the read")
+ raise ValueError("The original Goal source became unavailable during the read")
return {**page, "source": self.source.describe(), "goal_id": goal_id}
def history(self, goal_id, cursor=""):
diff --git a/loopx/control_plane/testing/doubao_model_behavior_actor.py b/loopx/control_plane/testing/doubao_model_behavior_actor.py
index 0466cf5ebe..2d0fe51c91 100644
--- a/loopx/control_plane/testing/doubao_model_behavior_actor.py
+++ b/loopx/control_plane/testing/doubao_model_behavior_actor.py
@@ -506,9 +506,12 @@ def _onboarding_decision_instruction(phase: str) -> str:
infer this boolean from activation_allowed or the selected route.
- host_loop_activation_after_todo_write: copy
activation_required_after_todo_write from that activation object, else false.
-- requested_host_surface: copy the packet's top-level host_surface, including
- null. Copy host_surface and activation_method from the activation object,
- including null.
+- requested_host_surface: copy ONLY the packet's top-level host_surface
+ (including null).
+- host_surface and activation_method: copy ONLY from that activation object
+ (top-level host_loop_activation or command_pack.host_loop_activation),
+ never from the packet's top-level host_surface; return null when omitted or
+ null.
- visible_goal_command_available: true exactly when
activation.host_mutation.host_command is `/goal `.
- writes_now and spends_quota_now: copy the corresponding booleans from
diff --git a/loopx/control_plane/turn_driver/codex_context.py b/loopx/control_plane/turn_driver/codex_context.py
index c1b5a0ca99..ffabdca8f5 100644
--- a/loopx/control_plane/turn_driver/codex_context.py
+++ b/loopx/control_plane/turn_driver/codex_context.py
@@ -419,7 +419,7 @@ def codex_context_hook(directory: str | None, event: dict[str, Any]) -> dict[str
previous = updated.get("host_execution_preparation")
ready = previous.get("ready_capture") if isinstance(previous, dict) else None
ready_digest = ready.get("sha256") if isinstance(ready, dict) else None
- if isinstance(ready_digest, str) and not previous.get("retired_context_readback") and any(
+ if isinstance(previous, dict) and isinstance(ready_digest, str) and not previous.get("retired_context_readback") and any(
page.get("ok") is True
and ready_digest in (page.get("sha256"), page.get("source_sha256"))
for page in pages
@@ -741,7 +741,7 @@ def _native_context_request(state: dict[str, Any], scope: dict[str, Any], captur
if isinstance(target, dict) and target.get("capture_id") == capture_id:
# A retry may use
# the original predecessor, but never this offer as its own baseline.
- previous = previous.get("comparison_base")
+ previous = previous.get("comparison_base") if isinstance(previous, dict) else None
if isinstance(previous, dict) and isinstance(previous.get("target"), dict):
try:
path, raw, payload = read_context_capture_file(previous["path"], previous["sha256"], private=True)
@@ -903,7 +903,7 @@ def _record_native_delivery(root: Path, state: dict[str, Any], result: dict[str,
if pinned is not None:
reference, opening = pinned
state["pending"]["observed_first_read"] = reference
- if opening is not None:
+ if opening is not None and read_plan is not None:
read_plan = {**read_plan, "observed_opening": opening}
_save(root, state)
if read_plan is None:
@@ -941,7 +941,12 @@ def prepare_codex_context(payload: dict[str, Any], *, directory: str | None, cap
from .decision_capture import capture_progressive_context
return NativeContextPreparation(capture_progressive_context(
- payload, directory=capture_directory, surface=surface)[0])
+ payload,
+ directory=capture_directory,
+ surface=surface,
+ selection_command_args_template=selection_command_args_template,
+ captured_decision=captured_decision,
+ )[0])
try:
with _locked(directory) as root:
recovery_committed = False
diff --git a/loopx/control_plane/turn_driver/host_candidate.py b/loopx/control_plane/turn_driver/host_candidate.py
index 829f5b49df..dccadcb2cb 100644
--- a/loopx/control_plane/turn_driver/host_candidate.py
+++ b/loopx/control_plane/turn_driver/host_candidate.py
@@ -470,6 +470,69 @@ def render_prompt(authority: Mapping[str, Any], *, allow_completion: bool = Fals
)
+def _repair_minor_json_delimiters(raw: str) -> str | None:
+ """Repair only unquoted trailing commas and <=2 extra terminal closers."""
+ out: list[str] = []
+ stack: list[str] = []
+ in_str = False
+ escaped = False
+ n = len(raw)
+ i = 0
+ while i < n:
+ ch = raw[i]
+ if in_str:
+ out.append(ch)
+ if escaped:
+ escaped = False
+ elif ch == "\\":
+ escaped = True
+ elif ch == '"':
+ in_str = False
+ i += 1
+ continue
+ if ch == '"':
+ in_str = True
+ out.append(ch)
+ elif ch == ",":
+ j = i + 1
+ while j < n and raw[j] in " \t\r\n":
+ j += 1
+ if j >= n or raw[j] not in "}]":
+ out.append(ch)
+ elif ch in "{[":
+ stack.append(ch)
+ out.append(ch)
+ elif ch in "}]":
+ expected = ("}" if stack[-1] == "{" else "]") if stack else None
+ if expected == ch:
+ stack.pop()
+ out.append(ch)
+ else:
+ tail = raw[i:]
+ if not all(c in "}] \t\r\n" for c in tail):
+ return None
+ tail_closers = [c for c in tail if c in "}]"]
+ expected_closers = ["}" if o == "{" else "]" for o in reversed(stack)]
+ if not (0 <= len(tail_closers) - len(expected_closers) <= 2):
+ return None
+ pos = 0
+ for ec in expected_closers:
+ while pos < len(tail_closers) and tail_closers[pos] != ec:
+ pos += 1
+ if pos >= len(tail_closers):
+ return None
+ pos += 1
+ out.extend(expected_closers)
+ stack.clear()
+ break
+ else:
+ out.append(ch)
+ i += 1
+ if in_str or stack:
+ return None
+ return "".join(out)
+
+
def parse_model_json(text: str) -> dict[str, Any] | None:
"""Parse the host final assistant message as one JSON object.
@@ -499,10 +562,17 @@ def parse_model_json(text: str) -> dict[str, Any] | None:
end = value.rfind("}")
if start == -1 or end == -1 or end <= start:
return None
+ candidate = value[start : end + 1]
try:
- parsed = json.loads(value[start : end + 1])
+ parsed = json.loads(candidate)
except json.JSONDecodeError:
- return None
+ repaired = _repair_minor_json_delimiters(candidate)
+ if repaired is None:
+ return None
+ try:
+ parsed = json.loads(repaired)
+ except json.JSONDecodeError:
+ return None
return parsed if isinstance(parsed, dict) else None
diff --git a/loopx/control_plane/turn_driver/host_process_transport.py b/loopx/control_plane/turn_driver/host_process_transport.py
index 07a1da28e0..0b1e79869d 100644
--- a/loopx/control_plane/turn_driver/host_process_transport.py
+++ b/loopx/control_plane/turn_driver/host_process_transport.py
@@ -422,7 +422,10 @@ def run_host_process(
raise RuntimeError("Invalid managed Host process observation")
finally:
try:
- if guarded is not None and (launch_deadline is not None or sys.exc_info()[0] is not None):
+ if guarded is not None and (
+ launch_deadline is not None
+ or (supervision == "direct" and sys.exc_info()[0] is not None)
+ ):
# A failed handoff still owns the Goal fence. The TS bridge must
# report no launch or finish its Host cleanup before it releases.
_cancel_guarded_host(
diff --git a/loopx/control_plane/work_items/interaction_contract.py b/loopx/control_plane/work_items/interaction_contract.py
index 74ec33dba7..1af3bfea28 100644
--- a/loopx/control_plane/work_items/interaction_contract.py
+++ b/loopx/control_plane/work_items/interaction_contract.py
@@ -717,6 +717,7 @@ def interaction_next_cli_actions(
turn_instance_id: str | None = None,
runtime_root: str | None = None,
registry_path: str | None = None,
+ recovery_lease_proof: Mapping[str, object] | None = None,
) -> list[str]:
if unadmitted_action_selection(payload):
return [_selection_recovery_command(
@@ -730,6 +731,7 @@ def interaction_next_cli_actions(
scheduler_execution_context=scheduler_execution_context, capability_reentry=capability_reentry,
capability_reentry_resolved=capability_reentry_resolved, settlement_plan=settlement_plan,
turn_instance_id=turn_instance_id, runtime_root=runtime_root, registry_path=registry_path,
+ recovery_lease_proof=recovery_lease_proof,
)
@@ -840,6 +842,7 @@ def _admitted_interaction_next_cli_actions(
turn_instance_id: str | None = None,
runtime_root: str | None = None,
registry_path: str | None = None,
+ recovery_lease_proof: Mapping[str, object] | None = None,
) -> list[str]:
goal_id = str(payload.get("goal_id") or "")
command_prefix = selection.render_cli_command_prefix(runtime_root=runtime_root, registry_path=registry_path)
@@ -1072,6 +1075,7 @@ def _admitted_interaction_next_cli_actions(
lifecycle_actor_args=lifecycle_actor_args,
typed_quota_guard=typed_quota_guard,
turn_instance_id=turn_instance_id,
+ lease_proof=recovery_lease_proof,
)
if mode == "autonomous_replan":
return build_autonomous_replan_cli_actions(
@@ -1445,6 +1449,7 @@ def _build_interaction_cli_channel(
turn_instance_id: str | None = None,
runtime_root: str | None = None,
registry_path: str | None = None,
+ recovery_lease_proof: Mapping[str, object] | None = None,
) -> dict[str, Any]:
if selection_recovery_required:
return selection.action_selection_recovery_cli_channel(_selection_recovery_command(
@@ -1479,6 +1484,7 @@ def _build_interaction_cli_channel(
turn_instance_id=turn_instance_id,
runtime_root=runtime_root,
registry_path=registry_path,
+ recovery_lease_proof=recovery_lease_proof,
),
"spend_allowed_now": False,
"spend_after_validation": spend_after_selection,
@@ -1774,6 +1780,7 @@ def _build_interaction_contract_projection(
turn_instance_id: str | None = None,
runtime_root: str | None = None,
registry_path: str | None = None,
+ recovery_lease_proof: Mapping[str, object] | None = None,
) -> tuple[InteractionContractPacket, SchedulerArbitration]:
execution_obligation = payload.get("execution_obligation")
if not isinstance(execution_obligation, dict):
@@ -1875,6 +1882,7 @@ def _build_interaction_contract_projection(
turn_instance_id=turn_instance_id,
runtime_root=runtime_root,
registry_path=registry_path,
+ recovery_lease_proof=recovery_lease_proof,
),
}
if turn_binding_required:
diff --git a/loopx/control_plane/work_items/semantic_replan_writeback.py b/loopx/control_plane/work_items/semantic_replan_writeback.py
index b6e3c72192..38ff46c6b3 100644
--- a/loopx/control_plane/work_items/semantic_replan_writeback.py
+++ b/loopx/control_plane/work_items/semantic_replan_writeback.py
@@ -14,6 +14,9 @@
from ..goals.goal_frontier import (
build_goal_frontier_projection_context_from_status,
)
+from ..goals.goal_frontier.semantic_history import (
+ latest_autonomous_replan_ack_from_status_payload,
+)
from ..status.autonomous_replan_projection import (
AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
autonomous_replan_obligation_from_runs,
@@ -212,6 +215,93 @@ def _obligation_was_created_by_current_completion(
)
+def _build_writeback_projection_context(
+ *,
+ goal_id: str,
+ safe_agent_id: str,
+ owned_history: list[dict[str, Any]],
+ registry_goal: dict[str, Any] | None,
+ replan_source_route: Mapping[str, Any] | None,
+ guard_scoped: bool,
+ guard_semantic_replan_obligation_id: str | None,
+ receipt_bound_guard_recorded_at: str | None,
+ receipt_bound_guard_turn_instance_id: str | None,
+ goal_ref: Mapping[str, Any] | None,
+ admission_event_id: str | None,
+ capability_evidence: CapabilityReplanEvidence | None,
+ replan_explore_reader: Callable[[str, str | None], Mapping[str, Any] | None] | None,
+ run_obligation: Mapping[str, Any] | None,
+ agent_identity: Mapping[str, object] | None,
+ user_todos: dict[str, object] | None,
+ agent_todos: dict[str, object] | None,
+ agent_todo_source_items: list[dict[str, object]],
+) -> tuple[dict[str, Any], dict[str, Any]]:
+ """Build status payload and goal-frontier context for replan writeback."""
+
+ status_payload: dict[str, Any] = {
+ **dict(replan_source_route or {}),
+ "run_history": {
+ "goals": [
+ {
+ **(registry_goal or {}),
+ "id": goal_id,
+ "latest_runs": owned_history,
+ }
+ ]
+ },
+ }
+ projected_ack = (
+ latest_autonomous_replan_ack_from_status_payload(
+ status_payload,
+ goal_id=goal_id,
+ agent_id=safe_agent_id,
+ neutral_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
+ )
+ if guard_scoped and guard_semantic_replan_obligation_id
+ else None
+ )
+ status_payload["receipt_bound_replan_ack_candidates"] = historical_ack_candidates_with_writeback_order(
+ owned_history, goal_id=goal_id, agent_id=safe_agent_id,
+ obligation_id=guard_semantic_replan_obligation_id, guard_scoped=guard_scoped,
+ source_route=replan_source_route,
+ guard_recorded_at=receipt_bound_guard_recorded_at,
+ guard_turn_instance_id=receipt_bound_guard_turn_instance_id,
+ goal_ref=goal_ref, admission_event_id=admission_event_id,
+ )
+ if capability_evidence is not None:
+ status_payload["bounded_research_frontier"] = capability_evidence.frontier
+ context = build_goal_frontier_projection_context_from_status(
+ replan_explore_reader=replan_explore_reader,
+ goal_id=goal_id,
+ agent_id=safe_agent_id,
+ status_payload=status_payload,
+ item={
+ **({"autonomous_replan_obligation": run_obligation} if run_obligation else {}),
+ **({"autonomous_replan_ack": projected_ack} if projected_ack is not None else {}),
+ },
+ project_asset={"execution_profile": (registry_goal or {}).get("execution_profile")},
+ user_todo_summary=user_todos,
+ agent_todo_summary=agent_todos,
+ agent_todo_source_items=agent_todo_source_items,
+ fallback_todo_source_items=agent_todo_source_items,
+ work_lane_contract=build_work_lane_context_contract(
+ {"progress_scope": "primary_goal"},
+ agent_todo_summary=agent_todos,
+ ),
+ neutral_replan_ack_classifications=AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS,
+ registered_agent_ids=list(cast(dict[str, Any], agent_identity)["registered_agents"]),
+ goal_status=str((registry_goal or {}).get("status") or "active"),
+ receipt_bound_replan_obligation_id=(guard_semantic_replan_obligation_id if guard_scoped else None),
+ receipt_bound_guard_recorded_at=receipt_bound_guard_recorded_at,
+ agent_profile=(
+ cast(dict[str, Any], cast(dict[str, Any], agent_identity).get("agent_profile"))
+ if isinstance(cast(dict[str, Any], agent_identity).get("agent_profile"), dict)
+ else None
+ ),
+ )
+ return status_payload, context
+
+
@effect_runtime_request_scope()
def qualify_replan_writeback(
*,
@@ -288,61 +378,25 @@ def qualify_replan_writeback(
external_progress_review=external_progress_review,
effective_turn_cadence=effective_turn_cadence,
)
- status_payload = {
- **dict(replan_source_route or {}),
- "run_history": {
- "goals": [
- {
- **(registry_goal or {}),
- "id": goal_id,
- "latest_runs": owned_history,
- }
- ]
- }
- }
- status_payload["receipt_bound_replan_ack_candidates"] = historical_ack_candidates_with_writeback_order(
- owned_history, goal_id=goal_id, agent_id=safe_agent_id,
- obligation_id=guard_semantic_replan_obligation_id, guard_scoped=guard_scoped,
- source_route=replan_source_route,
- guard_recorded_at=receipt_bound_guard_recorded_at,
- guard_turn_instance_id=receipt_bound_guard_turn_instance_id,
- goal_ref=goal_ref, admission_event_id=admission_event_id,
- )
- if capability_evidence is not None:
- status_payload["bounded_research_frontier"] = capability_evidence.frontier
- context = build_goal_frontier_projection_context_from_status(
- replan_explore_reader=replan_explore_reader,
+ status_payload, context = _build_writeback_projection_context(
goal_id=goal_id,
- agent_id=safe_agent_id,
- status_payload=status_payload,
- item={
- **(
- {"autonomous_replan_obligation": run_obligation}
- if run_obligation
- else {}
- )
- },
- project_asset={"execution_profile": (registry_goal or {}).get("execution_profile")},
- user_todo_summary=user_todos,
- agent_todo_summary=agent_todos,
- agent_todo_source_items=agent_todo_source_items,
- fallback_todo_source_items=agent_todo_source_items,
- work_lane_contract=build_work_lane_context_contract(
- {"progress_scope": "primary_goal"},
- agent_todo_summary=agent_todos,
- ),
- neutral_replan_ack_classifications=(
- AUTONOMOUS_RUN_HISTORY_NEUTRAL_CLASSIFICATIONS
- ),
- registered_agent_ids=list(cast(dict[str, Any], agent_identity)["registered_agents"]),
- goal_status=str((registry_goal or {}).get("status") or "active"),
- receipt_bound_replan_obligation_id=(guard_semantic_replan_obligation_id if guard_scoped else None),
+ safe_agent_id=safe_agent_id,
+ owned_history=owned_history,
+ registry_goal=registry_goal,
+ replan_source_route=replan_source_route,
+ guard_scoped=guard_scoped,
+ guard_semantic_replan_obligation_id=guard_semantic_replan_obligation_id,
receipt_bound_guard_recorded_at=receipt_bound_guard_recorded_at,
- agent_profile=(
- cast(dict[str, Any], cast(dict[str, Any], agent_identity).get("agent_profile"))
- if isinstance(cast(dict[str, Any], agent_identity).get("agent_profile"), dict)
- else None
- ),
+ receipt_bound_guard_turn_instance_id=receipt_bound_guard_turn_instance_id,
+ goal_ref=goal_ref,
+ admission_event_id=admission_event_id,
+ capability_evidence=capability_evidence,
+ replan_explore_reader=replan_explore_reader,
+ run_obligation=run_obligation,
+ agent_identity=agent_identity,
+ user_todos=user_todos,
+ agent_todos=agent_todos,
+ agent_todo_source_items=agent_todo_source_items,
)
obligation = context.get("replan_obligation")
selected_capability = guard_capability if guard_scoped else (obligation or {}).get("capability_guard")
diff --git a/loopx/control_plane/work_items/unsettled_host_turn_contract.py b/loopx/control_plane/work_items/unsettled_host_turn_contract.py
index 1a9ef5238a..a57a024381 100644
--- a/loopx/control_plane/work_items/unsettled_host_turn_contract.py
+++ b/loopx/control_plane/work_items/unsettled_host_turn_contract.py
@@ -13,6 +13,7 @@ def recovery_cli_actions(
lifecycle_actor_args: str,
typed_quota_guard: str,
turn_instance_id: str | None,
+ lease_proof: Mapping[str, Any] | None = None,
) -> list[str]:
"""Render the bounded typed closeout repair outside the main dispatcher."""
@@ -95,6 +96,17 @@ def recovery_cli_actions(
)
cadence = str(recovery.get("binding_cadence") or "").strip()
cadence_arg = f" --cadence {shlex.quote(cadence)}" if cadence else ""
+ lease_args = ""
+ if (
+ isinstance(lease_proof, Mapping)
+ and isinstance(lease_proof.get("idempotency_key"), str)
+ and lease_proof["idempotency_key"].strip()
+ and isinstance(lease_proof.get("expected_version"), int)
+ ):
+ lease_args = (
+ f" --task-lease-idempotency-key {shlex.quote(lease_proof['idempotency_key'].strip())}"
+ f" --task-lease-expected-version {lease_proof['expected_version']}"
+ )
return [
(
"If a verified observation was obtained under the original Turn, "
@@ -106,8 +118,8 @@ def recovery_cli_actions(
f"{command_prefix} quota monitor-poll --goal-id {goal_id}"
f"{lifecycle_actor_args} --todo-id {shlex.quote(prior_todo_id)}"
f"{target_arg} --result-hash ''"
- f"{cadence_arg} --turn-instance-id {shlex.quote(prior_turn_id)} "
- "--execute"
+ f"{cadence_arg} --turn-instance-id {shlex.quote(prior_turn_id)}"
+ f"{lease_args} --execute"
),
(
"If observation is unavailable, instead close only the original "
@@ -120,6 +132,7 @@ def recovery_cli_actions(
f"{command_prefix} refresh-state --goal-id {goal_id}{lifecycle_actor_args}"
f" --todo-id {shlex.quote(prior_todo_id)}"
f" --turn-instance-id {shlex.quote(prior_turn_id)}"
+ f"{lease_args}"
" --classification monitor_observation_unavailable"
" --delivery-batch-scale single_surface --delivery-outcome outcome_gap"
" --progress-result-class blocked --progress-blocker-id ''"
diff --git a/loopx/extensions/lark/cli_resolution.py b/loopx/extensions/lark/cli_resolution.py
index 981798caf7..f20a8ac4dd 100644
--- a/loopx/extensions/lark/cli_resolution.py
+++ b/loopx/extensions/lark/cli_resolution.py
@@ -278,7 +278,7 @@ def run(
if dispatch_guard is not None:
with dispatch_guard():
process = subprocess.Popen(
- args, cwd=str(cwd) if cwd else None, env=env, text=True,
+ args, cwd=str(cwd) if cwd else None, env=env, text=True, encoding="utf-8",
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
)
with process:
diff --git a/loopx/extensions/lark/goal_channel_operation.py b/loopx/extensions/lark/goal_channel_operation.py
index e26c19f739..cb6f7fd650 100644
--- a/loopx/extensions/lark/goal_channel_operation.py
+++ b/loopx/extensions/lark/goal_channel_operation.py
@@ -78,9 +78,7 @@ def _digest(value: object) -> str:
"#": "#",
"_": "_",
}
-_CARD_TEXT_ESCAPE_PATTERN = re.compile(
- "[" + re.escape("".join(_CARD_TEXT_ENTITIES)) + "]"
-)
+_CARD_TEXT_ESCAPE_PATTERN = re.compile(r"[&<>\"'*~\[\]()#_]")
def _card_text(value: object) -> str:
diff --git a/loopx/extensions/lark/manager_context.py b/loopx/extensions/lark/manager_context.py
index a2fea7936d..acf6222412 100644
--- a/loopx/extensions/lark/manager_context.py
+++ b/loopx/extensions/lark/manager_context.py
@@ -58,6 +58,8 @@ def manager_failure_reply(
"host_gate": "执行器未能完成本次调用,请检查管家执行器的具体错误",
"upstream_invalid_request": "上游拒绝了请求参数,请检查所选模型与 Codex CLI、账户的兼容性",
"server_restarted": "管家服务在处理过程中重启",
+ "execution_worker_exited": "本轮执行任务提前退出",
+ "execution_worker_exited_before_dispatch": "管家在请求交给执行器前中断",
"runtime_unavailable": "本地运行环境初始化失败,请检查 LoopX 安装与运行配置,修复后重试",
"resume_failed": "原 Agent 会话恢复失败,请检查执行器状态,修复后重试",
"model_auth_unavailable": "账户模型连接暂时不可用",
@@ -98,9 +100,15 @@ def manager_failure_reply(
if code in {"model_auth_unavailable", "resume_failed"} and preparation_recovery:
return code, ("已收到你的消息,但本轮准备执行器连接时失败。请求已保留;只有核验为模型调用前失败、"
"连接恢复且没有后续重交时,原队列才会按剩余次数接续。可用 /status 查看状态。")
+ if code == "execution_worker_exited_before_dispatch" and preparation_recovery:
+ return code, ("管家在请求交给执行器前中断,原请求已保留。系统会重新核验原消息和当前权限;"
+ "请求未被停止或替换时,原队列会限次接续。可用 /status 查看状态。")
if code == "server_restarted":
return code, ("处理被管家服务重启中断,原请求和会话已保留。系统会先核对原执行记录,"
"符合条件的由原队列恢复结果回复;执行效果不明的操作不会直接重做。")
+ if code == "execution_worker_exited":
+ return code, ("本轮执行任务在完整答复保存前退出,原请求和会话已保留。"
+ "已有操作可能已生效,系统不会自动重跑这条请求。可用 /status 查看状态。")
# Old hosts can terminate with `other` after typed disconnect retries.
# Describe that observed history without changing the terminal category,
# inferring an authorization failure, or granting permission to replay.
diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py
index 44100987f4..15f8945434 100644
--- a/loopx/extensions/lark/private_conversations.py
+++ b/loopx/extensions/lark/private_conversations.py
@@ -27,19 +27,18 @@
STEWARD_BINDING_ADOPTION_PENDING,
)
from ...capabilities.native_chat.file_discovery import (
- REPLY_IMAGE_NAME_PATTERN, FileChatDiscovery, optional_file_fingerprint, reply_image_workspace,
+ FileChatDiscovery, optional_file_fingerprint, reply_image_workspace,
)
from ...chat_attachments import CHAT_IMAGE_MAX_COUNT
from ...chat_store import TERMINAL_TURN_STATES, _atomic_write_json, _read_json
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 .conversation_identity import identity_ref, lark_private_source
from .event_inbox import acknowledge_lark_event_inbox, ingest_lark_event_inbox
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 .identity_shapes import LARK_APP_ID_PATTERN as APP_ID_PATTERN
from .inbox_reply import (
SourceAuthorityVerifier, _message, reply_lark_event_inbox, update_lark_inbox_reply, verify_lark_inbox_reply,
)
@@ -48,8 +47,11 @@
mark_lark_event_inbox_processing, mark_lark_event_inbox_received)
from .outbound import normalize_lark_outbound_text, present_lark_conversation_text as _presentation_text
from .private_images import private_message_caption, private_message_images
-from .private_delivery_discovery import (PrivateDeliveryDiscovery, answer_image_source_matches, answer_media_dispatch,
- historical_image_reply_matches, prepared_steward_binding_waits, private_answer_images, resumable_answer_image_session)
+from .private_delivery_discovery import (
+ PrivateDeliveryDiscovery, answer_image_retry_waits, answer_image_source_matches, answer_media_dispatch,
+ failure_notice_receipt_unknown, historical_image_plan, historical_image_reply_matches, historical_image_source,
+ is_answer_image_followup, prepared_steward_binding_waits, private_answer_images, resumable_answer_image_session,
+)
from .private_ingress import PrivateSourceUnavailable, private_source_text, recovery_source_matches, steward_source_preflight
from .private_ingress import project_private_reply_history as project_private_reply_history
from . import private_steward_triage as triage
@@ -94,21 +96,6 @@ def _lark_payload(runner: Any, cli_bin: str, profile: str, tail: list[str]) -> M
return payload
-def _is_answer_image_followup(record: Mapping[str, Any]) -> bool:
- if record.get("status") != "accepted" or not isinstance(record.get("answer_images"), dict):
- return False
- terminal = record.get("deliveries", {}).get(presentation_phase(record, "terminal"), {})
- return terminal.get("verified") is True
-
-
-def _answer_image_retry_waits(record: Mapping[str, Any], now: float) -> bool:
- if not _is_answer_image_followup(record):
- return False
- state = record["answer_images"]
- retry_at = state.get("next_check_at")
- return state.get("status") != "delivered" and type(retry_at) in (int, float) and retry_at > now
-
-
class _ControlReadFallback(Exception):
"""A valid source needs ordinary rendering, before any Core write."""
@@ -1213,8 +1200,9 @@ def before_send(_intent: str) -> dict[str, bool]:
before_send=before_send)
elif phase_state.get("started"):
# A crash between provider write and its receipt is ambiguous. Keep
- # the original source recoverable; never resend blindly.
- save_phase(blocker="provider_delivery_receipt_unknown")
+ # the original source recoverable; never resend blindly or churn disk.
+ if phase_state.get("blocker") != "provider_delivery_receipt_unknown":
+ save_phase(blocker="provider_delivery_receipt_unknown")
return False
else:
def attempt(value: Any) -> None:
@@ -1324,7 +1312,7 @@ def authorized() -> bool:
if state.get("status") == "delivered" and record["deliveries"].get(media_phase, {}).get("images_verified") is True:
return True
now = datetime.now(timezone.utc).timestamp()
- if _answer_image_retry_waits(record, now):
+ if answer_image_retry_waits(record, now):
return False
state.update(status="pending", next_check_at=now + ANSWER_IMAGE_RETRY_SECONDS)
_atomic_write_json(path, record)
@@ -1370,63 +1358,6 @@ def plan_scope() -> tuple[Path, str] | None:
inbox=inbox, authorization_check=authorized, finalize_source=False)
return False
- def _historical_image_source(self, record: Mapping[str, Any], plan: Mapping[str, Any]) -> dict:
- event = record["event"]
- terminal = record["deliveries"]["terminal"]
- if (record["status"] != "delivered" or record.get("context_recovery")
- or event.get("chat_type") != "p2p" or event.get("message_type") != "text"
- or record["source"].get("private_human_message") is not True
- or record["binding_id"] != plan["binding_id"] or record["turn_id"] != plan["turn_id"]
- or terminal.get("verified") is not True or terminal.get("image_keys")
- or terminal.get("attempt", {}).get("message_ref") != plan["message_id"]):
- raise ValueError("original text-only reply unavailable")
- native = self.core.read_request(record["request_ref"])
- if (native.get("status") != "accepted" or native.get("origin") != "lark"
- or native.get("command") is not None or native.get("agent_target")
- or native.get("delivery_verified") is not True
- or not _same_fields(native, record, "request_ref", "binding_id", "source", "session_id", "turn_id")):
- raise ValueError("original request no longer matches")
- turn = self.core.controller.store.load_turn(record["session_id"], record["turn_id"]) or {}
- response = turn.get("response") or {}
- if (not isinstance(answer := response.get("message"), str)
- or hashlib.sha256(answer.encode()).hexdigest() != plan["answer_sha256"]
- or terminal.get("text") != _presentation_text(answer)
- or (session := resumable_answer_image_session(self.core, native, turn)) is None):
- raise ValueError("original completed answer or its authority changed")
- image_targets = private_answer_images(answer)[1]
- if "captured_images" in plan:
- if image_targets or response.get("answer_image_plan") or reply_image_workspace(session, turn) is None:
- raise ValueError("original reply is not a qualified legacy project answer")
- elif not image_targets:
- raise ValueError("original remote image reply unavailable")
- return session
-
- def _historical_image_plan(self, record: Mapping[str, Any], plan: Mapping[str, Any]) -> dict:
- from ..public_source_reader import public_url
-
- captured = "captured_images" in plan
- image_field, reference_field = ("captured_images", "name") if captured else ("images", "url")
- if set(plan) != {"binding_id", "turn_id", "message_id", "answer_sha256", image_field}:
- raise ValueError("invalid original image repair fields")
- if (not isinstance(plan["message_id"], str) or not LARK_MESSAGE_ID_PATTERN.fullmatch(plan["message_id"])
- or not isinstance(plan["answer_sha256"], str)
- or not BARE_SHA256_PATTERN.fullmatch(plan["answer_sha256"])
- or not isinstance(plan[image_field], list) or not 1 <= len(plan[image_field]) <= CHAT_IMAGE_MAX_COUNT):
- raise ValueError("invalid original image repair target")
- images = []
- for image in plan[image_field]:
- if (not isinstance(image, dict) or set(image) != {reference_field, "sha256"}
- or not isinstance(reference := image[reference_field], str)
- or not isinstance(image["sha256"], str) or not BARE_SHA256_PATTERN.fullmatch(image["sha256"])):
- raise ValueError("each original image requires a reference and its SHA-256")
- if captured and not REPLY_IMAGE_NAME_PATTERN.fullmatch(reference):
- raise ValueError("invalid original image snapshot name")
- images.append({reference_field: reference if captured else public_url(reference), "sha256": image["sha256"]})
- if len({image[reference_field] for image in images}) != len(images):
- raise ValueError("duplicate original image reference")
- self._historical_image_source(record, plan)
- return {**plan, image_field: images}
-
def _load_historical_image_plan(self, path: Path, record: dict[str, Any]) -> dict | None:
if record.get("historical_answer_images") is not None:
return record["historical_answer_images"]
@@ -1444,7 +1375,7 @@ def _load_historical_image_plan(self, path: Path, record: dict[str, Any]) -> dic
plan = json.loads(raw)
if not isinstance(plan, dict) or path.stem != record.get("request_ref"):
raise ValueError("invalid original image repair descriptor")
- plan = self._historical_image_plan(record, plan)
+ plan = historical_image_plan(self.core, record, plan)
state = record["historical_answer_images"] = {"plan": plan, "status": "pending"}
_atomic_write_json(path, record)
return state
@@ -1463,7 +1394,7 @@ def _reconcile_historical_answer_images(self, path: Path) -> int:
_atomic_write_json(path, record)
plan = state["plan"]
try:
- session = self._historical_image_source(record, plan)
+ session = historical_image_source(self.core, record, plan)
selected = self.bindings.resolve(binding_id=plan["binding_id"], session=session,
session_context=session.get("project_context") or session.get("steward_context"),
include_observation=True, **record["source"])
@@ -1504,7 +1435,7 @@ def original_pair_matches() -> bool:
def authorized() -> bool:
try:
return (selected["binding"] in self.bindings.read()["bindings"]
- and bool(self._historical_image_source(record, plan)))
+ and bool(historical_image_source(self.core, record, plan)))
except (KeyError, OSError, ValueError):
return False
@@ -1552,7 +1483,8 @@ def _planned_historical_repairs(self) -> frozenset[str]:
def pending_body_paths(self) -> list[Path]:
"""Find unsent bodies without waiting for historical recovery probes."""
paths = [path for path, row, _ in self._body_discovery.rows()
- if row["status"] != "delivered" and triage.pending(row) and not _is_answer_image_followup(row)]
+ if row["status"] != "delivered" and triage.pending(row)
+ and not is_answer_image_followup(row) and not failure_notice_receipt_unknown(row)]
return [*paths, *self._pending_web_projection_paths()]
def _noop_backoff_cache(self) -> dict[Path, tuple[tuple[tuple[int, ...], ...], int]]:
@@ -1606,8 +1538,9 @@ def pending_delivery_paths(self) -> list[Path]:
planned_repairs = self._planned_historical_repairs()
noop_backoff = self._noop_backoff_cache()
for path, row, repairs in self._delivery_discovery.rows():
- if row["status"] != "delivered":
- if triage.pending(row) and not _answer_image_retry_waits(row, now):
+ receipt_unknown = failure_notice_receipt_unknown(row)
+ if row["status"] != "delivered" and not receipt_unknown:
+ if triage.pending(row) and not answer_image_retry_waits(row, now):
paths.append(path)
elif row.get("historical_images_due_at") is not None and row["historical_images_due_at"] <= now:
paths.append(path)
@@ -1630,7 +1563,11 @@ def pending_delivery_paths(self) -> list[Path]:
now_mono = time.monotonic()
stagger = zlib.crc32(path.name.encode("utf-8")) % 60
self._context_recovery_probe_at[path] = now_mono + 60 + stagger
- if repairs or recovery_candidate(row["request_ref"]):
+ candidate = (
+ recovery_candidate(row["request_ref"], failure_notice_receipt_unknown=True)
+ if receipt_unknown else recovery_candidate(row["request_ref"])
+ )
+ if repairs or candidate:
self._context_recovery_probe_at[path] = now_mono + 60
paths.append(path)
return [*paths, *self._web_projection_paths()]
@@ -1642,7 +1579,8 @@ def is_recovery_path(self, path: Path) -> bool:
row = _read_json(path)
card = row.get("triage") or {}
return (
- row.get("status") == "delivered" or _is_answer_image_followup(row)
+ row.get("status") == "delivered" or is_answer_image_followup(row)
+ or failure_notice_receipt_unknown(row)
or bool(card.get("send_started_at") or card.get("message_id"))
)
@@ -1745,7 +1683,7 @@ def reconcile_request(self, path: Path) -> int:
if historical_images is None:
self._historical_image_probe_at[path] = time.monotonic() + 60
return self._reconcile_historical_answer_images(path)
- if _answer_image_retry_waits(row, datetime.now(timezone.utc).timestamp()):
+ if answer_image_retry_waits(row, datetime.now(timezone.utc).timestamp()):
return 0
record_event_timing(row["profile"], row["event"]["message_id"], "reconciliation_started")
if row["status"] == "delivered" and row.get("session_id") and row.get("turn_id"):
@@ -1760,13 +1698,21 @@ def reconcile_request(self, path: Path) -> int:
self._repair_recovery_presentation(path)
if not self.core.context_recovery_candidate(row["request_ref"]):
return 0
+ receipt_unknown = failure_notice_receipt_unknown(row)
if (row.get("session_id") and row.get("turn_id") and (row["status"] == "delivered"
- or row["status"] == "accepted" and self.core.context_recovery_candidate(row["request_ref"]))):
+ or (row["status"] == "accepted" and (
+ self.core.context_recovery_candidate(row["request_ref"], failure_notice_receipt_unknown=True)
+ if receipt_unknown else self.core.context_recovery_candidate(row["request_ref"]))))):
# Legacy terminal delivery may be only a verified failure notice.
# Core decides whether a migration continuation is safe; preserve
# the old Turn and every frozen/ambiguous provider attempt.
- recovered = self.core.recover_failed_request(row["request_ref"],
- source_observation=lambda selected: self._recovery_source_observation(row, selected))
+ if receipt_unknown:
+ recovered = self.core.recover_failed_request(row["request_ref"],
+ source_observation=lambda selected: self._recovery_source_observation(row, selected),
+ failure_notice_receipt_unknown=True)
+ else:
+ recovered = self.core.recover_failed_request(row["request_ref"],
+ source_observation=lambda selected: self._recovery_source_observation(row, selected))
if (row["status"] == "delivered" and recovered.get("native_result_recovery")
and recovered["native_result_recovery"]["turn_id"] == recovered.get("turn_id")):
delivered_native = self._reconcile_native_result(path, recovered)
@@ -1785,8 +1731,14 @@ def reconcile_request(self, path: Path) -> int:
row.update(status="accepted", turn_id=recovered["turn_id"],
context_recovery=recovered["context_recovery"])
_atomic_write_json(path, row)
+ elif receipt_unknown:
+ self._record_context_recovery_noop_backoff(path, row)
+ return 0
elif row["status"] == "delivered":
self._record_context_recovery_noop_backoff(path, row)
+ elif receipt_unknown:
+ self._record_context_recovery_noop_backoff(path, row)
+ return 0
if row["status"] == "captured":
# Intake may still own this capture. Wait on its existing fence and
# read its result before retrying admission from a stale snapshot.
@@ -1866,24 +1818,19 @@ def inbox() -> Path:
# A name supplies presentation metadata only; outbound
# provenance and Core recovery/admission keep their own
# fresh checks, including on the completed-stop path.
- config = self._inbox(record, observation=selected["observation"] if selected else None,
- reuse_display_name=control_receipt_first and selected is None)
+ config = self._inbox(record, observation=selected["observation"] if selected else None, reuse_display_name=control_receipt_first and selected is None)
return config
try:
if record["status"] in {"command_queued", "command_completed", "commission_running"}:
native = self.core.read_request(record["request_ref"])
if native["status"] == "command_queued":
- selected = self.bindings.resolve(binding_id=record["binding_id"],
- include_observation=True, **record["source"])
+ selected = self.bindings.resolve(binding_id=record["binding_id"], include_observation=True, **record["source"])
self._feedback(path, record, inbox=inbox)
self._deliver(path, record, "admission", native["response"], inbox=inbox)
return 0
- record.update(status=native["status"], response=native.get("response"),
- commission_resources=native.get("commission_resources"),
- status_snapshot=native.get("status_snapshot"))
- control_receipt_first = (native["status"] == "command_completed"
- and native.get("command") in CHAT_CONTROL_COMMANDS.values())
+ record.update(status=native["status"], response=native.get("response"), commission_resources=native.get("commission_resources"), status_snapshot=native.get("status_snapshot"))
+ control_receipt_first = (native["status"] == "command_completed" and native.get("command") in CHAT_CONTROL_COMMANDS.values())
# A completed group stop has no Core work left. Its existing
# Inbox name is presentation only; sending still runs both
# fresh binding/source observations in _source_verified. Do
@@ -1893,8 +1840,7 @@ def inbox() -> Path:
and record["source"].get("group_human_message") is True
and not record["deliveries"].get("terminal", {}).get("verified")
and _same_fields(native, record, "request_ref", "binding_id", "source")):
- selected = self.bindings.resolve(binding_id=record["binding_id"],
- include_observation=True, **record["source"])
+ selected = self.bindings.resolve(binding_id=record["binding_id"], include_observation=True, **record["source"])
if record["status"] == "accepted":
native = self.core.read_request(record["request_ref"])
if native.get("agent_target"):
@@ -1925,6 +1871,10 @@ def inbox() -> Path:
turn = self.core.controller.store.load_turn(record["session_id"], record["turn_id"])
if turn and turn["status"] in {"starting", "running"}:
self._progress(path, record, inbox=inbox)
+ if turn and turn["status"] == "completing":
+ # Finish an orphaned completing turn's saved answer under the original request without replaying.
+ self.core.controller.recover_exited_turn(record["session_id"], record["turn_id"])
+ turn = self.core.controller.store.load_turn(record["session_id"], record["turn_id"])
if not turn or turn["status"] not in TERMINAL_TURN_STATES:
return 0
if turn["status"] == "failed":
@@ -1977,11 +1927,9 @@ def inbox() -> Path:
if not self._deliver(path, record, "commission_result", result_text, inbox=inbox):
return 0
config = inbox()
- acknowledge_lark_event_inbox(project=self.runtime_root, config_path=config,
- message_ids=[record["event"]["message_id"]], execute=True)
+ acknowledge_lark_event_inbox(project=self.runtime_root, config_path=config, message_ids=[record["event"]["message_id"]], execute=True)
if native_path.exists():
- self.core.record_delivery(record["request_ref"], session_id=record.get("session_id"),
- turn_id=record.get("turn_id"), message_text=response)
+ self.core.record_delivery(record["request_ref"], session_id=record.get("session_id"), turn_id=record.get("turn_id"), message_text=response)
elif record["status"] != "rejected":
raise ValueError("delivery request journal unavailable")
record["status"] = "delivered"
diff --git a/loopx/extensions/lark/private_delivery_discovery.py b/loopx/extensions/lark/private_delivery_discovery.py
index 5a42030c70..0e94714222 100644
--- a/loopx/extensions/lark/private_delivery_discovery.py
+++ b/loopx/extensions/lark/private_delivery_discovery.py
@@ -1,6 +1,7 @@
"""Read-only source and scheduling checks for private-message delivery."""
from __future__ import annotations
+import hashlib
import json
import re
from collections.abc import Callable, Iterator, Mapping
@@ -8,12 +9,23 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, cast
+from ...chat_attachments import CHAT_IMAGE_MAX_COUNT
from ...chat_store import RESUMABLE_SESSION_STATES, ChatSessionStore, _read_json
-from ...capabilities.native_chat.file_discovery import FileChatDiscovery, external_source_route_path, file_change_fingerprint
+from ...control_plane.content_digest import BARE_SHA256_PATTERN
+from ...capabilities.native_chat.file_discovery import (
+ REPLY_IMAGE_NAME_PATTERN,
+ FileChatDiscovery,
+ external_source_route_path,
+ file_change_fingerprint,
+ reply_image_workspace,
+)
from ...file_lock import try_exclusive_file_lock
+from .identity_shapes import LARK_MESSAGE_ID_PATTERN
+from .outbound import present_lark_conversation_text as _presentation_text
from .presentation.markdown_post import lark_markdown_readback_matches
from .private_ingress import private_source_text
-from .private_progress import legacy_recovery_repairs
+from .private_progress import legacy_recovery_repairs, presentation_phase
+from .private_web_projection import _same_fields
if TYPE_CHECKING:
from ...capabilities.native_chat.external_conversations import ChatExternalConversations
@@ -251,3 +263,91 @@ def _observe(self, path: Path) -> tuple[dict[str, Any], bool]:
if before is not None and file_change_fingerprint(path) == before:
self._completed[path] = (before, view, repairs)
return view, repairs
+
+
+def failure_notice_receipt_unknown(record: Mapping[str, Any]) -> bool:
+ """Return True when the current terminal failure notice lost its provider receipt."""
+ if record.get("status") != "accepted" or "triage" in record:
+ return False
+ phase = presentation_phase(record, "terminal")
+ terminal = (record.get("deliveries") or {}).get(phase) or {}
+ return (
+ terminal.get("started") is True
+ and not terminal.get("attempt")
+ and terminal.get("verified") is not True
+ and terminal.get("blocker") == "provider_delivery_receipt_unknown"
+ )
+
+
+def is_answer_image_followup(record: Mapping[str, Any]) -> bool:
+ if record.get("status") != "accepted" or not isinstance(record.get("answer_images"), dict):
+ return False
+ terminal = record.get("deliveries", {}).get(presentation_phase(record, "terminal"), {})
+ return terminal.get("verified") is True
+
+
+def answer_image_retry_waits(record: Mapping[str, Any], now: float) -> bool:
+ if not is_answer_image_followup(record):
+ return False
+ state = record["answer_images"]
+ retry_at = state.get("next_check_at")
+ return state.get("status") != "delivered" and type(retry_at) in (int, float) and retry_at > now
+
+
+def historical_image_source(core: ChatExternalConversations, record: Mapping[str, Any], plan: Mapping[str, Any]) -> dict:
+ event = record["event"]
+ terminal = record["deliveries"]["terminal"]
+ if (record["status"] != "delivered" or record.get("context_recovery")
+ or event.get("chat_type") != "p2p" or event.get("message_type") != "text"
+ or record["source"].get("private_human_message") is not True
+ or record["binding_id"] != plan["binding_id"] or record["turn_id"] != plan["turn_id"]
+ or terminal.get("verified") is not True or terminal.get("image_keys")
+ or terminal.get("attempt", {}).get("message_ref") != plan["message_id"]):
+ raise ValueError("original text-only reply unavailable")
+ native = core.read_request(record["request_ref"])
+ if (native.get("status") != "accepted" or native.get("origin") != "lark"
+ or native.get("command") is not None or native.get("agent_target")
+ or native.get("delivery_verified") is not True
+ or not _same_fields(native, record, "request_ref", "binding_id", "source", "session_id", "turn_id")):
+ raise ValueError("original request no longer matches")
+ turn = core.controller.store.load_turn(record["session_id"], record["turn_id"]) or {}
+ response = turn.get("response") or {}
+ if (not isinstance(answer := response.get("message"), str)
+ or hashlib.sha256(answer.encode()).hexdigest() != plan["answer_sha256"]
+ or terminal.get("text") != _presentation_text(answer)
+ or (session := resumable_answer_image_session(core, native, turn)) is None):
+ raise ValueError("original completed answer or its authority changed")
+ image_targets = private_answer_images(answer)[1]
+ if "captured_images" in plan:
+ if image_targets or response.get("answer_image_plan") or reply_image_workspace(session, turn) is None:
+ raise ValueError("original reply is not a qualified legacy project answer")
+ elif not image_targets:
+ raise ValueError("original remote image reply unavailable")
+ return session
+
+
+def historical_image_plan(core: ChatExternalConversations, record: Mapping[str, Any], plan: Mapping[str, Any]) -> dict:
+ from ..public_source_reader import public_url
+
+ captured = "captured_images" in plan
+ image_field, reference_field = ("captured_images", "name") if captured else ("images", "url")
+ if set(plan) != {"binding_id", "turn_id", "message_id", "answer_sha256", image_field}:
+ raise ValueError("invalid original image repair fields")
+ if (not isinstance(plan["message_id"], str) or not LARK_MESSAGE_ID_PATTERN.fullmatch(plan["message_id"])
+ or not isinstance(plan["answer_sha256"], str)
+ or not BARE_SHA256_PATTERN.fullmatch(plan["answer_sha256"])
+ or not isinstance(plan[image_field], list) or not 1 <= len(plan[image_field]) <= CHAT_IMAGE_MAX_COUNT):
+ raise ValueError("invalid original image repair target")
+ images = []
+ for image in plan[image_field]:
+ if (not isinstance(image, dict) or set(image) != {reference_field, "sha256"}
+ or not isinstance(reference := image[reference_field], str)
+ or not isinstance(image["sha256"], str) or not BARE_SHA256_PATTERN.fullmatch(image["sha256"])):
+ raise ValueError("each original image requires a reference and its SHA-256")
+ if captured and not REPLY_IMAGE_NAME_PATTERN.fullmatch(reference):
+ raise ValueError("invalid original image snapshot name")
+ images.append({reference_field: reference if captured else public_url(reference), "sha256": image["sha256"]})
+ if len({image[reference_field] for image in images}) != len(images):
+ raise ValueError("duplicate original image reference")
+ historical_image_source(core, record, plan)
+ return {**plan, image_field: images}
diff --git a/loopx/presentation/goal_storage_api.py b/loopx/presentation/goal_storage_api.py
index 77017193fa..eb7bbffdf4 100644
--- a/loopx/presentation/goal_storage_api.py
+++ b/loopx/presentation/goal_storage_api.py
@@ -147,11 +147,10 @@ def _storage_cold_snapshot(
) -> 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
+ from ..registry import find_registry_goal, load_registry
if source_registry != Path(self.server.registry_path).resolve():
- registry = load_project_registry(source_registry)
+ registry = load_registry(source_registry)
selected_goal = find_registry_goal(registry, goal_id)
if selected_goal is None:
raise ValueError("registered Goal source changed")
diff --git a/loopx/quota.py b/loopx/quota.py
index b59adbeb1e..46af12acd9 100644
--- a/loopx/quota.py
+++ b/loopx/quota.py
@@ -73,7 +73,6 @@
)
from .execution_profile import execution_profile_outcome_floor, outcome_floor_threshold
from .presentation.renderers.quota_event_markdown import (
- render_quota_monitor_poll_markdown as _render_quota_monitor_poll_markdown,
render_quota_slot_preview_markdown as render_quota_slot_preview_markdown,
)
from .presentation.renderers.quota_markdown import (
@@ -1030,7 +1029,7 @@ def _attach_auxiliary_monitor_settlement_resume(
goal_id=identity.goal_id, agent_id=identity.agent_id, todo_id=identity.todo_id,
turn_instance_id=identity.turn_instance_id, replan_obligation_id=identity.replan_obligation_id,
command_prefix=prefix, scoped_cli_args=scoped_args, lifecycle_actor_args="",
- quota_spend_source=str(progress["quota_spend_source"]), goal_ref=goal_ref,
+ quota_spend_source=str(progress["quota_spend_source"]), goal_ref=goal_ref, composed_settlement=True,
)
ordered_steps = _dict_items(plan.as_dict().get("ordered_steps"))
result["settlement_resume"] = {
@@ -1151,7 +1150,7 @@ def should_run(current_status: dict[str, object]) -> dict[str, object]:
).get("todo")
effective_todo_id = normalized_observation_todo_id or (normalized_receipt_todo_id if not target_key else None)
result: dict[str, object] = record_quota_monitor_poll_for_decision(
- before, status_payload, goal_id=safe_goal_id, render_markdown=_render_quota_monitor_poll_markdown,
+ before, status_payload, goal_id=safe_goal_id,
after_decision=should_run, registry_path=registry_path, execute=execute, source=source,
reason_summary=reason_summary, agent_id=agent_id, settlement_todo_id=normalized_receipt_todo_id,
auxiliary_settlement_todo=auxiliary_settlement_todo, todo_id=effective_todo_id,
@@ -1166,6 +1165,7 @@ def should_run(current_status: dict[str, object]) -> dict[str, object]:
task_lease_expected_version=task_lease_expected_version, use_current_task_lease=use_current_task_lease,
turn_instance_id=turn_instance_id, status_reloader=status_reloader, goal_ref=goal_ref,
recovery_effect_id=recovery_effect_id,
+ todo_id_explicit=normalized_requested_todo_id is not None,
)
continuation = result.get("turn_continuation")
if (
diff --git a/loopx/self_update.py b/loopx/self_update.py
index f6653c7720..396430b063 100644
--- a/loopx/self_update.py
+++ b/loopx/self_update.py
@@ -333,10 +333,14 @@ def _release_root_from_doctor(doctor_payload: dict[str, Any]) -> str | None:
def _user_loopx_bin(home: Path | None = None) -> Path:
+ if home is None and (bin_dir := os.environ.get("LOOPX_BIN_DIR")):
+ return Path(bin_dir) / "loopx"
return (home or Path.home()) / ".local" / "bin" / "loopx"
def _user_releases_dir(home: Path | None = None) -> Path:
+ if home is None and (releases_dir := os.environ.get("LOOPX_RELEASES_DIR")):
+ return Path(releases_dir)
return (home or Path.home()) / ".local" / "share" / "loopx" / "releases"
@@ -1508,7 +1512,7 @@ def execute_rollback_plan(
# Share the archive installer's exact guard through checks and recovery.
guard_path = release_root.parent / ".install-guard"
guard_path.parent.mkdir(parents=True, exist_ok=True)
- promotion_guard = os.fdopen(_open_lock_descriptor(guard_path, flags=os.O_CREAT | os.O_RDWR), "r+")
+ promotion_guard = os.fdopen(_open_lock_descriptor(guard_path, flags=os.O_CREAT | os.O_RDWR), "r+", encoding="utf-8")
deadline = time.monotonic() + timeout_seconds
while not _try_acquire_kernel_lock(promotion_guard):
remaining = deadline - time.monotonic()
diff --git a/loopx/semantics/goal_instance_binding_inventory_v1.json b/loopx/semantics/goal_instance_binding_inventory_v1.json
index 23720079be..bcc5108083 100644
--- a/loopx/semantics/goal_instance_binding_inventory_v1.json
+++ b/loopx/semantics/goal_instance_binding_inventory_v1.json
@@ -26,7 +26,7 @@
"consumer_sites": [
"loopx/attached_session.py::claim_attached_agent_turn",
"loopx/attached_session.py::complete_attached_agent_turn",
- "loopx/chat_runtime.py::ChatRuntimeController.resume_session"
+ "loopx/chat/session_open.py::ChatSessionLifecycleMixin.resume_session"
],
"effect_boundary": "loopx/control_plane/goals/chat_session_lifecycle.ts::decideChatSessionLifecycle",
"authority_role": "runtime_session_binding",
diff --git a/loopx/state_refresh.py b/loopx/state_refresh.py
index 28d6e10900..d63eac6af6 100644
--- a/loopx/state_refresh.py
+++ b/loopx/state_refresh.py
@@ -759,6 +759,8 @@ def refresh_state_run(
goal_ref: dict[str, str] | None = None,
goal_acceptance_preview: dict[str, Any] | None = None,
task_lease_proof: dict[str, Any] | None = None,
+ allow_same_generation_lease_renewal: bool = False,
+ validation_recovery_turn_instance_id: str | None = None,
) -> dict[str, Any]:
from .control_plane.todos.provider_projection import recover_refresh_todo_projection
@@ -1614,6 +1616,8 @@ def read_explore_context(read_goal_id: str, read_agent_id: str | None):
requested_proof=task_lease_proof,
settlement=settlement_readback,
locked_goal=current_goal if normalized_next_action else None,
+ allow_same_generation_lease_renewal=allow_same_generation_lease_renewal,
+ validation_recovery_turn_instance_id=validation_recovery_turn_instance_id,
))
if settlement_readback is not None:
persist_external_delivery_pause(
diff --git a/packages/dsh-loopx-plugin/README.md b/packages/dsh-loopx-plugin/README.md
index c57b99046c..4c30fa7443 100644
--- a/packages/dsh-loopx-plugin/README.md
+++ b/packages/dsh-loopx-plugin/README.md
@@ -61,9 +61,10 @@ copy under `$DSH_AGENTS_HOME/runtime/dsh-loopx-plugin` (default
`~/.agents/runtime/dsh-loopx-plugin`) and never mutates the system Python
environment. This works with externally managed Python distributions that
enforce PEP 668; the plugin does not use `--break-system-packages`.
-The next plugin release requires LoopX 2.4.0 or newer for the original-Turn
-`quota receipt` reader. Publish the matching Core release before distributing
-the plugin; published LoopX 2.3.0 does not provide that command.
+The 0.1.1-beta.7 release requires LoopX 2.4.0 or newer (`loopx>=2.4.0`) for
+the original-Turn `quota receipt` reader; LoopX 2.3.0 does not provide that
+command. Publish or install the matching 2.4.0 Core release alongside the
+plugin.
Bootstrap, Driver and GoalBar use the same
version floor; an older global or managed CLI is upgraded or rejected before
business commands run. An explicit outdated `LOOPX_BIN` must be upgraded by its
@@ -71,22 +72,20 @@ owner. Older plugin releases retain their original CLI requirements.
The continuation driver follows LoopX's context delivery setting.
When the guard returns a capture reference, the driver reads the
admission fields from that same capture.
-Install the prebuilt release into the web profile:
+Install the prebuilt `0.1.1-beta.7` release into the web profile:
```bash
dsh plugin --profile web add \
- "https://github.com/loopx-project/loopx/releases/download/dsh-loopx-plugin-v0.1.1-beta.6/dsh-loopx-plugin-0.1.1-beta.6.tgz"
+ "https://github.com/loopx-project/loopx/releases/download/dsh-loopx-plugin-v0.1.1-beta.7/dsh-loopx-plugin-0.1.1-beta.7.tgz"
```
-The beta.6 package targets the qualified DSH 0.2.0-rc.2 API and retains the
-qualified frozen 0.1.5 and 0.1.7-rc.2 compatibility ranges. The exported legacy
-RPC registration remains available to explicit callers, but plugin startup
-always uses the shared API.
-
-The beta.7 source candidate adds DSH 0.2.2-rc.1 and 0.2.2-rc.3 qualification,
-with Cordis 4.1.0 as the pinned development host. The earlier 0.2.1-rc.2
-combination remains covered. Its frozen source host uses MCP client 2.3.0 and
-SDK 1.32.0 to address
+The `0.1.1-beta.7` package requires `loopx>=2.4.0`, adds DSH `0.2.2-rc.1`
+through `0.2.2-rc.3` qualification (specifically `0.2.2-rc.1` and `0.2.2-rc.3`,
+with Cordis 4.1.0 as the pinned development host), and retains the qualified
+0.1.5, 0.1.7-rc.2, 0.2.0-rc.2, 0.2.1-rc.1, and 0.2.1-rc.2 compatibility
+ranges. The exported legacy RPC registration remains available to explicit
+callers, but plugin startup always uses the shared API. Its frozen development
+host uses MCP client 2.3.0 and SDK 1.32.0 to address
[GHSA-6prh-2h8m-c8cw](https://github.com/advisories/GHSA-6prh-2h8m-c8cw).
These dependency overrides do not patch an independently installed DSH binary;
that host's owner must update its affected MCP dependencies separately.
@@ -101,9 +100,10 @@ cd packages/dsh-loopx-plugin
Start DSH on loopback (port `0` asks the OS for a free port) and open the
printed URL. The plugin finishes its idempotent LoopX CLI and skill bootstrap
before DSH publishes the Web URL through its native Loader readiness boundary.
-The source candidate gates only LoopX's Host and Driver on `loopxBootstrap`,
-so disabling or uninstalling it keeps the shared Web server and runtime alive.
-The published beta.6 limitation is described under automatic initialization:
+Release `0.1.1-beta.7` gates only LoopX's Host and Driver on `loopxBootstrap`,
+so disabling or uninstalling it keeps the shared Web server and runtime alive
+(see the earlier published `0.1.1-beta.6` limitation under automatic
+initialization):
```bash
dsh --profile web --port 0
@@ -485,9 +485,9 @@ rows unavailable while DSH remains usable. Removing the package's
rows releases its routes without stopping the shared Web server or runtime.
Automatic startup does not create Agent followups or model calls.
-The host-isolation repair in this source is pending a new plugin release.
-Published beta.6 still adds the shared Web dependency and can disconnect DSH
-during hot uninstall, including with Hub 1.6.4. For that published package,
+Release `0.1.1-beta.7` ships this host-isolation repair. Earlier published
+`0.1.1-beta.6` still added the shared Web dependency and could disconnect DSH
+during hot uninstall, including with Hub 1.6.4. When removing `0.1.1-beta.6`,
close DSH, remove it with the native plugin CLI, then restart DSH.
The repair command has no arguments. Extra input returns a usage error before any
@@ -680,10 +680,11 @@ qualified 0.1.7-rc.2, 0.2.0-rc.2, 0.2.1-rc.1, 0.2.1-rc.2, 0.2.2-rc.1 and
0.2.2-rc.3 candidates. It excludes earlier 0.2 candidates, stable 0.2.1 and
0.2.2, and other unqualified prerelease
tuples. A stable-version range alone does not admit every prerelease.
-The immutable beta.6 download above is the published package. Building this
-checkout produces the separate beta.7 candidate; it never replaces the
-immutable beta.6 asset. Candidate qualification is separate from publishing
-and public npm/Hub adoption.
+Building this checkout produces the `0.1.1-beta.7` package (requiring
+`loopx>=2.4.0` and supporting DSH `0.2.2-rc.1..rc.3`) as its own immutable
+tag and asset alongside LoopX `2.4.0`; it never replaces the earlier immutable
+`0.1.1-beta.6` asset. Candidate qualification is separate from publishing and
+public npm/Hub adoption.
If a marketplace reports `loopx-repository: entry file missing: index.js`, it
selected the monorepo root instead of this plugin package. Installing
@@ -691,9 +692,10 @@ selected the monorepo root instead of this plugin package. Installing
plugin. The marketplace's npm route requires a published `dsh-loopx-plugin`
whose repository points here and whose keywords include `dsh-plugin`; otherwise
older Hubs fall back to that invalid Git target.
-The [source catalog entry](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin/pull/6633)
-points to the published beta.6 release tarball, though the live [marketplace listing](https://dsh-plugin.org/plugins/loopx-project/loopx?ref=hub)
-may still show the cached beta.5 install command until the site redeploys.
+Earlier catalog updates (such as [PR #6633](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin/pull/6633)
+for `beta.6`) pointed to prior tarballs; once `dsh-loopx-plugin-v0.1.1-beta.7`
+is published and the [marketplace listing](https://dsh-plugin.org/plugins/loopx-project/loopx?ref=hub)
+updates, install via Hub or the pinned `0.1.1-beta.7` release URL below.
[DSH Plugin Hub v1.6.0](https://github.com/dshplugin/dsh-plugin-hub/releases/tag/v1.6.0)
or later provides profile-specific install guidance and includes the earlier
fixes for release-package readback, stale npm targets, and desktop-profile
@@ -704,11 +706,12 @@ dsh plugin --profile web add dsh-plugin
```
Replace `web` with `desktop` when using the desktop app. LoopX is not currently
-published to npm as `dsh-loopx-plugin`. While the web catalog entry redeploys its
-command, install the verified beta.6 release package directly from its immutable asset:
+published to npm as `dsh-loopx-plugin`. Until the web catalog entry updates to
+`0.1.1-beta.7`, install the `0.1.1-beta.7` release package directly from its
+immutable asset once published:
```bash
-dsh plugin --profile web add https://github.com/loopx-project/loopx/releases/download/dsh-loopx-plugin-v0.1.1-beta.6/dsh-loopx-plugin-0.1.1-beta.6.tgz
+dsh plugin --profile web add https://github.com/loopx-project/loopx/releases/download/dsh-loopx-plugin-v0.1.1-beta.7/dsh-loopx-plugin-0.1.1-beta.7.tgz
```
To build from source instead, use the package installer with
diff --git a/packages/dsh-loopx-plugin/tests/admission-closeout.integration.spec.ts b/packages/dsh-loopx-plugin/tests/admission-closeout.integration.spec.ts
index cd178b7c02..c999adf9de 100644
--- a/packages/dsh-loopx-plugin/tests/admission-closeout.integration.spec.ts
+++ b/packages/dsh-loopx-plugin/tests/admission-closeout.integration.spec.ts
@@ -283,12 +283,13 @@ function receiptEffectIds(payload: Record): string[] {
function executablePlanArgs(
steps: readonly Record[],
- kind: 'durable_writeback' | 'quota_spend',
+ kind: 'durable_writeback',
replacements: Readonly> = {},
): string[] {
const command = String(
steps.find(step => step.kind === kind)?.command_template ?? '',
)
+ expect(command).not.toBe('')
const tokens = command.match(/'[^']*'|"[^"]*"|\S+/gu) ?? []
expect(tokens.shift()).toBe('loopx')
const args = tokens.map(token => replacements[token] ?? token.replace(/^(['"])(.*)\1$/u, '$2'))
@@ -522,6 +523,7 @@ describe('DSH admission-to-closeout integration', () => {
expect(heartbeatReceipt.turn_instance_id).toBe(turnInstanceId)
const cliChannel = record(record(guard.payload.interaction_contract).cli_channel)
const rawPlan = record(cliChannel.settlement_plan)
+ expect(rawPlan.schema_version).toBe('quota_settlement_plan_v2')
const identity = record(rawPlan.identity)
const replayChannel = record(record(guardReplay.payload.interaction_contract).cli_channel)
expect(record(record(replayChannel.settlement_plan).identity)).toEqual(identity)
@@ -530,16 +532,15 @@ describe('DSH admission-to-closeout integration', () => {
expect(identity.todo_id).toBe(todoId)
expect(JSON.stringify(rawPlan)).not.toContain('${LOOPX_TURN:?}')
const steps = array(rawPlan.ordered_steps).map(record)
- const writebackTemplate = String(
- steps.find(step => step.kind === 'durable_writeback')?.command_template,
- )
- const spendTemplate = String(
- steps.find(step => step.kind === 'quota_spend')?.command_template,
- )
- for (const command of [writebackTemplate, spendTemplate]) {
- expect(command).toContain(`--turn-instance-id ${turnInstanceId}`)
- expect(command).toContain(`--todo-id ${todoId}`)
- }
+ const writebackStep = steps.find(step => step.kind === 'durable_writeback')
+ const spendStep = steps.find(step => step.kind === 'quota_spend')
+ const writebackTemplate = String(writebackStep?.command_template)
+ expect(writebackStep?.owner).toBe('agent')
+ expect(writebackTemplate).toContain(`--turn-instance-id ${turnInstanceId}`)
+ expect(writebackTemplate).toContain(`--todo-id ${todoId}`)
+ expect(writebackTemplate).toContain('--settle-turn')
+ expect(spendStep?.owner).toBe('runtime')
+ expect(spendStep?.command_template).toBeUndefined()
const writeback = await runCli(testFixture, [
...executablePlanArgs(steps, 'durable_writeback', {
@@ -552,15 +553,25 @@ describe('DSH admission-to-closeout integration', () => {
])
expect(writeback.exitCode, JSON.stringify(writeback.payload)).toBe(0)
expect(new Set(receiptEffectIds(writeback.payload))).toEqual(new Set([effectId]))
+ expect(
+ array(record(writeback.payload.settlement_result).receipts)
+ .map(receipt => String(record(receipt).step_kind)),
+ ).toEqual(['validation', 'durable_writeback', 'quota_spend'])
- const spendArgs = executablePlanArgs(steps, 'quota_spend')
- const spend = await runCli(testFixture, spendArgs)
- const spendReplay = await runCli(testFixture, spendArgs)
- expect(spend.exitCode).toBe(0)
- expect(spendReplay.exitCode).toBe(0)
+ const spendReplay = await runCli(testFixture, [
+ 'quota', 'spend-slot',
+ '--goal-id', goalId,
+ '--slots', '1',
+ '--source', 'heartbeat',
+ '--execute',
+ '--todo-id', todoId,
+ '--agent-id', agentId,
+ '--turn-instance-id', turnInstanceId,
+ ])
+ expect(spendReplay.exitCode, JSON.stringify(spendReplay.payload)).toBe(0)
expect(spendReplay.payload.idempotent_replay).toBe(true)
expect(spendReplay.payload.appended).toBe(false)
- expect(new Set(receiptEffectIds(spend.payload))).toEqual(new Set([effectId]))
+ expect(new Set(receiptEffectIds(spendReplay.payload))).toEqual(new Set([effectId]))
const closeout = await runCli(testFixture, [
'todo', 'complete',
diff --git a/packages/loopx-codex-provider-routing/NATIVE.md b/packages/loopx-codex-provider-routing/NATIVE.md
index 10fc6aba04..e370acd0fe 100644
--- a/packages/loopx-codex-provider-routing/NATIVE.md
+++ b/packages/loopx-codex-provider-routing/NATIVE.md
@@ -91,8 +91,11 @@ configured steward route as well as the worker, without creating credentials.
The model transport's ordinary `ensure` also performs reconciliation for its
own route. An authenticated
new-protocol daemon with a different loaded build stops new admissions, drains
-already accepted requests and releases its existing owner lock before one new
-daemon starts. The model, route credentials, key policy and message queue stay
+already admitted model requests, and releases its existing owner lock before one
+new daemon starts. It closes connections still sending headers or request bodies.
+An admitted response can also finish draining when its client stops reading and
+the local socket write times out.
+The model, route credentials, key policy and message queue stay
with their existing owners. A bounded timeout returns `api_relay_drain_pending`;
read status later instead of forcing exit or replaying a Turn. Old `/health`
response bytes remain compatible, but do not qualify version adoption.
@@ -165,15 +168,28 @@ is presented to OpenAI and rejected with `401 invalid_api_key`. Installers that
add chat transports should write the relay endpoint into each provider
definition and verify it on readback.
-A local authenticated transport sends the original selected model and Responses
-body to OpenAI. An explicit upstream quota rejection, 401, 403 or temporary 429 can
+A local authenticated transport sends the original selected model and complete
+Responses body to OpenAI. Model requests require one valid Content-Length and
+are eligible for dispatch after every declared byte has arrived. Loopback
+socket reads and writes have a ten-second timeout per operation. A timed-out
+response write closes the relay's upstream connection and leaves the HTTP
+transfer incomplete. Upstream effects remain unknown. Use the existing Turn
+recovery path to determine any further action. Waiting for the upstream response
+uses the upstream timeout.
+An explicit upstream quota rejection, 401, 403 or temporary 429 can
try the next configured key before output. Each key is attempted at most once per
request, with typed cooldowns; model permission failures are scoped to that model.
Unknown network effects, ordinary request/server failures and any started stream
are not retried on another key. No job replay, model substitution or synthetic
-completion is added. There are no background paid probes. The transport survives
-the Chat/App restart used for native handoff; disabling stops new admissions and
-drains already accepted requests before its process exits. With the policy off,
+completion is added. For HTTP/1.1 clients, the relay streams bytes as they arrive
+and leaves the response incomplete when an upstream transfer is truncated. A
+chunked upstream transfer requires CRLF after each data chunk and a final blank
+line after the trailer fields. A complete HTTP transfer alone does not prove a
+completed model response. A truncated rejection body or one larger than the
+64 KiB inspection limit cannot trigger another key. There are no background paid
+probes. The transport survives the Chat/App restart used for native handoff.
+Disabling stops new admissions and
+drains admitted model requests before its process exits. With the policy off,
an existing subscription is never routed through this transport.
Version 0.15.2 recognizes the official `credit_balance_exhausted` error in both
diff --git a/packages/loopx-codex-provider-routing/pyproject.toml b/packages/loopx-codex-provider-routing/pyproject.toml
index 6f6a7fbcdd..fda176a2ba 100644
--- a/packages/loopx-codex-provider-routing/pyproject.toml
+++ b/packages/loopx-codex-provider-routing/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "loopx-codex-provider-routing"
-version = "0.15.8"
+version = "0.15.9"
description = "Native Codex account slots and exhausted-account handoff for LoopX"
readme = "README.md"
requires-python = ">=3.11"
diff --git a/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/native.py b/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/native.py
index 44355291c4..942178cfdb 100644
--- a/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/native.py
+++ b/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/native.py
@@ -246,25 +246,31 @@ def conversation_epoch(self, *, codex_home: Path, consumer: str = "worker", mode
return (revision, slot) if isinstance(revision, str) and revision and isinstance(slot, str) and slot else None
def steward_route(self, *, codex_home: Path, primary: str, pool: list[str], current: str,
- primary_capacity: dict | None, blocked_primary: dict | None = None, model: str | None = None) -> dict:
+ primary_capacity: dict | None, fallback_capacity: dict | None = None,
+ blocked_primary: dict | None = None, model: str | None = None) -> dict:
"""Observe the existing account owner; never start a second watcher."""
state = self._load()
adapter = state.get("adapter")
if adapter and Path(adapter.get("profile", {}).get("home", "")).resolve() != codex_home.resolve():
return {"endpoint": None, "blocked_primary": blocked_primary}
- api_ready = bool(self.api_keys().candidates(model or "")) if state["policy"].get("steward_api_role") == "primary" else False
- if api_ready:
+ policy_cfg = state.get("policy") or {}
+ role = policy_cfg.get("steward_api_role")
+ api_enabled = role == "primary" or (role != "disabled" and policy_cfg.get("api_fallback") is True)
+ api_ready = bool(self.api_keys().candidates(model or "")) if api_enabled else False
+ if role == "primary" and api_ready:
return policy({"operation": "steward-route", "now": self.clock(), "primary": primary,
"pool": pool, "current": current, "native": self._projection(state), "api_ready": True,
- "primary_capacity": primary_capacity, "blocked_primary": blocked_primary})
- if primary_capacity is None:
+ "primary_capacity": primary_capacity, "fallback_capacity": fallback_capacity,
+ "blocked_primary": blocked_primary})
+ if primary == "antigravity" and primary_capacity is None:
return {"endpoint": None, "blocked_primary": blocked_primary}
- if state["policy"].get("enabled") is not True or not adapter:
+ if policy_cfg.get("enabled") is not True or not adapter:
return {"endpoint": None, "blocked_primary": blocked_primary}
native = self.transport(adapter, "status")
return policy({"operation": "steward-route", "now": self.clock(),
"primary": primary, "pool": pool, "current": current,
- "native": self._projection(state, native), "primary_capacity": primary_capacity,
+ "native": self._projection(state, native), "api_ready": api_ready,
+ "primary_capacity": primary_capacity, "fallback_capacity": fallback_capacity,
"blocked_primary": blocked_primary})
def model_transport(self, *, codex_home: Path, consumer: str, model: str | None = None) -> dict | None:
diff --git a/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/native_policy.ts b/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/native_policy.ts
index b15a8bbdd3..1460f60cb9 100644
--- a/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/native_policy.ts
+++ b/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/native_policy.ts
@@ -48,12 +48,13 @@ export function modelRoute(input: {consumer: string; native: Record
export function stewardRoute(input: {
now: number; primary: string; pool: string[]; current: string;
native: Record; primary_capacity: Record | null;
+ fallback_capacity?: Record | null;
blocked_primary?: Record | null;
api_ready?: boolean;
}) {
const no = {endpoint: null, blocked_primary: input.blocked_primary ?? null};
if (!Number.isFinite(input.now) || !["antigravity", "codex"].includes(input.current)
- || input.primary !== "antigravity" || !Array.isArray(input.pool)
+ || !["antigravity", "codex"].includes(input.primary) || !Array.isArray(input.pool)
|| input.pool.length !== 2 || new Set(input.pool).size !== 2
|| !input.pool.includes("antigravity") || !input.pool.includes("codex")) return no;
const validCapacity = (row: Record | null | undefined) => row
@@ -65,32 +66,64 @@ export function stewardRoute(input: {
&& typeof row.quota_exhausted === "boolean" && typeof row.normal_completion === "boolean"
&& row.quota_exhausted !== row.normal_completion;
if (input.blocked_primary != null && !validCapacity(input.blocked_primary)) return no;
- if (modelRoute({consumer: "steward", native: input.native, api_ready: input.api_ready === true}).api
- && apiRole(input.native.policy, "steward") === "primary") return {endpoint: "codex", blocked_primary: input.blocked_primary ?? null};
- const capacity = input.primary_capacity;
- if (!validCapacity(capacity) || !capacity || input.now < capacity.observed_at
- || input.now - capacity.observed_at > 120) return no;
- let blocked = input.blocked_primary ?? null;
- if (capacity.quota_exhausted && (!blocked || blocked.execution_id !== capacity.execution_id)) blocked = capacity;
- const restored = blocked && (blocked.account_ref !== capacity.account_ref
- || blocked.model_id !== capacity.model_id || blocked.reset_time !== capacity.reset_time
- || input.now >= Date.parse(blocked.reset_time) / 1000);
- if (capacity.remaining_fraction > 0 && (!blocked || restored) && (!capacity.quota_exhausted || restored)) {
- return {endpoint: "antigravity", blocked_primary: blocked};
- }
const native = input.native;
- // API connection/model-list checks do not prove generation allowance. An
- // already active, explicitly authorized API route remains the native owner's
- // selection; its pre-output key failover retains that owner's own guards.
+ const role = apiRole(native.policy, "steward");
+ const codexApiPrimary = modelRoute({consumer: "steward", native, api_ready: input.api_ready === true}).api && role === "primary";
const current = Array.isArray(native.accounts)
? native.accounts.find((a: any) => a.slot === native.active_slot) : null;
const fresh = current && Number.isFinite(current.observed_at)
&& current.observed_at <= input.now && input.now - current.observed_at <= 120;
- const usable = native.policy?.enabled === true && native.login_matches === true
+ // API connection/model-list checks do not prove generation allowance. An
+ // already active, explicitly authorized API route remains the native owner's
+ // selection; its pre-output key failover retains that owner's own guards.
+ const codexUsable = codexApiPrimary || (native.policy?.enabled === true && native.login_matches === true
&& native.recovery_required === false && (native.api_active === true
? modelRoute({consumer: "steward", native, api_ready: input.api_ready === true}).api
- : native.action === "ready" && fresh && current.availability === "available");
- return {endpoint: usable ? "codex" : null, blocked_primary: blocked};
+ : native.action === "ready" && fresh && current.availability === "available"));
+
+ if (input.primary === "antigravity") {
+ if (codexApiPrimary) return {endpoint: "codex", blocked_primary: input.blocked_primary ?? null};
+ const capacity = input.primary_capacity;
+ if (!validCapacity(capacity) || !capacity || input.now < capacity.observed_at
+ || input.now - capacity.observed_at > 120) return no;
+ let blocked = input.blocked_primary ?? null;
+ if (capacity.quota_exhausted && (!blocked || blocked.execution_id !== capacity.execution_id)) blocked = capacity;
+ const restored = blocked && (blocked.account_ref !== capacity.account_ref
+ || blocked.model_id !== capacity.model_id || blocked.reset_time !== capacity.reset_time
+ || input.now >= Date.parse(blocked.reset_time) / 1000);
+ if (capacity.remaining_fraction > 0 && (!blocked || restored) && (!capacity.quota_exhausted || restored)) {
+ return {endpoint: "antigravity", blocked_primary: blocked};
+ }
+ return {endpoint: codexUsable ? "codex" : null, blocked_primary: blocked};
+ }
+
+ if (codexUsable) return {endpoint: "codex", blocked_primary: input.blocked_primary ?? null};
+ const slots: unknown[] = Array.isArray(native.policy?.slots) ? native.policy.slots : [];
+ const accountsBySlot = new Map(
+ Array.isArray(native.accounts) ? native.accounts.map((a: any) => [a?.slot, a]) : [],
+ );
+ const allSlotsExhausted = slots.length > 0 && native.policy?.enabled === true
+ && native.login_matches === true && native.recovery_required === false
+ && slots.every(slot => {
+ const a = accountsBySlot.get(slot);
+ return a && a.availability === "exhausted"
+ && ((Number.isFinite(a.observed_at) && a.observed_at <= input.now && input.now - a.observed_at <= 120)
+ || (Number.isFinite(a.reset_at) && a.reset_at > input.now));
+ });
+ const apiFallback = role === "fallback" || native.policy?.api_fallback === true;
+ if (!allSlotsExhausted || (apiFallback && input.api_ready === true)) return no;
+ const fallback = input.fallback_capacity;
+ if (!validCapacity(fallback) || !fallback || fallback.observed_at > input.now
+ || input.now - fallback.observed_at > 120) return no;
+ const blocked = input.blocked_primary ?? null;
+ const unblockedFallback = !blocked || blocked.account_ref !== fallback.account_ref
+ || blocked.model_id !== fallback.model_id || blocked.reset_time !== fallback.reset_time
+ || input.now >= Date.parse(blocked.reset_time) / 1000;
+ if (fallback.remaining_fraction > 0 && !fallback.quota_exhausted
+ && fallback.normal_completion === true && unblockedFallback) {
+ return {endpoint: "antigravity", blocked_primary: blocked};
+ }
+ return no;
}
/** Diagnostic liveness only; this never grants switching or process authority. */
diff --git a/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/openai_relay.py b/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/openai_relay.py
index 0ff0c41e98..dbab25786f 100644
--- a/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/openai_relay.py
+++ b/packages/loopx-codex-provider-routing/src/loopx_codex_provider_routing/openai_relay.py
@@ -8,6 +8,7 @@
import argparse
import hashlib
import hmac
+from http.client import HTTPException, HTTPResponse, HTTPSConnection, IncompleteRead, LineTooLong, _MAXHEADERS, _MAXLINE
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import json
import os
@@ -28,6 +29,8 @@
SCHEMA = "codex_openai_api_relay_v1"
API_SLOT = "openai-api"
RUNTIME_SCHEMA = "codex_openai_api_relay_runtime_v1"
+CLIENT_IO_TIMEOUT = 10
+ERROR_BODY_LIMIT = 65536
# Loaded once. Replacing an installed wheel cannot make its old daemon claim
# the newly installed implementation. No credential or request data is hashed.
BUILD = hashlib.sha256(b"".join(Path(__file__).with_name(name).read_bytes() for name in (
@@ -36,6 +39,38 @@
"openai_policy.ts", "openai_policy_bridge.ts", "native-extension.toml"))).hexdigest()
+class _RelayHTTPResponse(HTTPResponse):
+ def _get_chunk_left(self):
+ if self.chunk_left == 0:
+ # The standard parser discards the delimiter without validating it.
+ if self._safe_read(2) != b"\r\n":
+ raise IncompleteRead(b"")
+ # The delimiter is consumed. Let the standard parser advance.
+ self.chunk_left = None
+ return super()._get_chunk_left()
+
+ def _read_and_discard_trailer(self):
+ # Require the final blank line while retaining the standard trailer bounds.
+ for _ in range(_MAXHEADERS + 1):
+ line = self.fp.readline(_MAXLINE + 1)
+ if len(line) > _MAXLINE:
+ raise LineTooLong("trailer line")
+ if not line:
+ raise IncompleteRead(b"")
+ if line in (b"\r\n", b"\n"):
+ return
+ raise HTTPException(f"got more than {_MAXHEADERS} trailers")
+
+
+class _RelayHTTPSConnection(HTTPSConnection):
+ response_class = _RelayHTTPResponse
+
+
+class _RelayHTTPSHandler(urllib.request.HTTPSHandler):
+ def https_open(self, request):
+ return self.do_open(_RelayHTTPSConnection, request, context=self._context)
+
+
class _StreamOutcome:
"""Bounded, transient SSE framing; the existing TS owner classifies errors."""
def __init__(self):
@@ -130,13 +165,36 @@ class RelayServer(ThreadingHTTPServer):
def __init__(self, owner: NativeAccounts, value: dict, *, opener=None):
self.owner, self.value = owner, value
- self.opener = opener or urllib.request.build_opener(NoRedirect())
+ self.opener = opener or urllib.request.build_opener(NoRedirect(), _RelayHTTPSHandler())
self.instance_id = secrets.token_hex(16)
self._admission_lock = threading.Lock()
+ self._receiving: set[socket.socket] = set()
self.draining, self.active_requests = False, 0
self.started = time.monotonic()
super().__init__(("127.0.0.1", value["port"]), RelayHandler)
+ def process_request(self, request, client_address):
+ with self._admission_lock:
+ self._receiving.add(request)
+ super().process_request(request, client_address)
+
+ def shutdown_request(self, request):
+ with self._admission_lock:
+ self._receiving.discard(request)
+ super().shutdown_request(request)
+
+ def server_close(self):
+ with self._admission_lock:
+ self.draining = True
+ # Stop reads on connections still outside model admission. Keep their
+ # write half open for the control acknowledgement and error responses.
+ for request in self._receiving:
+ try:
+ request.shutdown(socket.SHUT_RD)
+ except OSError:
+ pass
+ super().server_close()
+
def runtime(self):
with self._admission_lock:
return {"schema_version": RUNTIME_SCHEMA, "build": BUILD,
@@ -163,6 +221,9 @@ def service_actions(self):
class RelayHandler(BaseHTTPRequestHandler):
server: RelayServer
+ # This bounds I/O on the loopback client socket. The upstream uses its
+ # own timeout and may produce a response long after the request arrives.
+ timeout = CLIENT_IO_TIMEOUT
def log_message(self, *_args):
pass
@@ -237,19 +298,10 @@ def do_POST(self):
self._json(401, "relay_auth_required")
return
with self.server._admission_lock:
- admitted = not self.server.draining
- if admitted:
- self.server.active_requests += 1
- if not admitted:
+ draining = self.server.draining
+ if draining:
self._json(503, "api_relay_draining")
return
- try:
- self._model_request()
- finally:
- with self.server._admission_lock:
- self.server.active_requests -= 1
-
- def _model_request(self):
upstream_path = self.path.removeprefix("/steward") if self.consumer == "steward" else self.path
if upstream_path not in ("/v1/responses", "/v1/responses/compact"):
self._json(404, "unsupported_route")
@@ -258,10 +310,18 @@ def _model_request(self):
self._json(503, "api_fallback_disabled")
return
try:
- length = int(self.headers.get("Content-Length", "0"))
+ lengths = self.headers.get_all("Content-Length", [])
+ if len(lengths) != 1 or "Transfer-Encoding" in self.headers:
+ raise ValueError()
+ declared = lengths[0].strip(" \t")
+ if not re.fullmatch(r"[0-9]+", declared):
+ raise ValueError()
+ length = int(declared)
if not 1 <= length <= 32 * 1024 * 1024:
raise ValueError()
body = self.rfile.read(length)
+ if len(body) != length:
+ raise ValueError()
value = json.loads(body)
model = value["model"]
if not isinstance(model, str) or not re.fullmatch(r"[A-Za-z0-9_.:-]{1,160}", model) or model.startswith("sk-"):
@@ -269,6 +329,21 @@ def _model_request(self):
except (TypeError, ValueError, KeyError):
self._json(400, "invalid_request")
return
+ with self.server._admission_lock:
+ admitted = not self.server.draining
+ if admitted:
+ self.server._receiving.discard(self.connection)
+ self.server.active_requests += 1
+ if not admitted:
+ self._json(503, "api_relay_draining")
+ return
+ try:
+ self._forward(upstream_path, body, model)
+ finally:
+ with self.server._admission_lock:
+ self.server.active_requests -= 1
+
+ def _forward(self, upstream_path, body, model):
keys = self.server.owner.api_keys()
candidates = keys.candidates(model)
last_status, last_code = 429, "api_keys_unavailable"
@@ -288,48 +363,84 @@ def _model_request(self):
try:
response = self.server.opener.open(request, timeout=300)
except urllib.error.HTTPError as error:
- encoded = error.read(65537)
+ with error:
+ try:
+ encoded = error.read(ERROR_BODY_LIMIT + 1)
+ except (OSError, HTTPException):
+ encoded = None
+ remaining = getattr(error.fp, "length", None)
+ # A bounded read can silently reach early EOF, and a
+ # body larger than our limit cannot be verified in full.
+ if encoded is None or len(encoded) > ERROR_BODY_LIMIT or (remaining or 0) > 0:
+ self._json(502, "upstream_unavailable")
+ return
try:
- code = json.loads(encoded).get("error", {}).get("code") if len(encoded) <= 65536 else None
+ code = json.loads(encoded).get("error", {}).get("code")
code = code if isinstance(code, str) and code.isascii() and code.replace("_", "").isalnum() else None
except (TypeError, ValueError, AttributeError):
code = None
headers = {k.lower(): v for k, v in error.headers.items()}
status = error.code
- error.close()
outcome = keys.record(row["id"], status=status, code=code, model=model, headers=headers)
last_status, last_code = status, code or "upstream_rejected"
if outcome["retry_next_key"]:
continue
self._json(status, last_code)
return
- except (OSError, HostError):
+ except (OSError, HTTPException, HostError):
# Unknown upstream effects never authorize trying another key.
self._json(502, "upstream_unavailable")
return
with response:
headers = {k.lower(): v for k, v in response.headers.items()}
- keys.record(row["id"], status=response.status, code=None, model=model, headers=headers)
+
+ def observe(status: int, code: str | None = None):
+ try:
+ keys.record(row["id"], status=status, code=code, model=model, headers=headers)
+ except (OSError, ValueError, HostError, subprocess.SubprocessError):
+ # Once admitted, an observation failure must not
+ # replace or discard the upstream's response.
+ pass
+
+ observe(response.status)
+ # urllib removes the upstream chunk framing. Use the decoded
+ # length when known; otherwise frame the HTTP/1.1 response
+ # so a read failure cannot look like clean EOF.
+ can_have_body = response.status not in (204, 304)
+ length = getattr(response, "length", None)
+ chunked = can_have_body and length is None and self.request_version == "HTTP/1.1"
+ if chunked:
+ self.protocol_version = "HTTP/1.1"
self.send_response(response.status)
self.send_header("Content-Type", headers.get("content-type", "text/event-stream"))
self.send_header("Cache-Control", "no-store")
+ if chunked:
+ self.send_header("Transfer-Encoding", "chunked")
+ self.send_header("Connection", "close")
+ elif can_have_body and length is not None:
+ self.send_header("Content-Length", str(length))
self.end_headers()
+ if not can_have_body:
+ return
# Once headers/output are admitted, disconnects and SSE errors
# end this request. No key rotation, synthetic completion or job replay.
- observer = _StreamOutcome() if headers.get("content-type", "").split(";", 1)[0].strip().lower() == "text/event-stream" else None
+ content_type = headers.get("content-type", "").split(";", 1)[0].strip().lower()
+ observer = _StreamOutcome() if content_type == "text/event-stream" else None
try:
read = getattr(response, "read1", response.read)
while chunk := read(8192):
outcome = observer.feed(chunk) if observer is not None else None
if outcome is not None:
- try:
- keys.record(row["id"], status=outcome["status"], code=outcome["code"],
- model=model, headers=headers)
- except (OSError, ValueError, HostError, subprocess.SubprocessError):
- pass
- self.wfile.write(chunk)
+ observe(outcome["status"], outcome["code"])
+ if chunked:
+ self.wfile.write(f"{len(chunk):x}\r\n".encode() + chunk + b"\r\n")
+ else:
+ self.wfile.write(chunk)
+ self.wfile.flush()
+ if chunked:
+ self.wfile.write(b"0\r\n\r\n")
self.wfile.flush()
- except (OSError, TimeoutError):
+ except (OSError, HTTPException):
self.close_connection = True
return
self._json(last_status, last_code)
diff --git a/packages/loopx-codex-provider-routing/tests/steward_route.test.ts b/packages/loopx-codex-provider-routing/tests/steward_route.test.ts
index 87d9d53e6a..58e6830557 100644
--- a/packages/loopx-codex-provider-routing/tests/steward_route.test.ts
+++ b/packages/loopx-codex-provider-routing/tests/steward_route.test.ts
@@ -56,3 +56,35 @@ test("invalid retained epochs and ambiguous observations stay unselected", () =>
assert.equal(stewardRoute({...input, native: {...input.native, api_active: true,
action: "api_active", policy: {...input.native.policy, api_fallback: false}}}).endpoint, null);
});
+
+test("codex primary fails over to antigravity only when all codex slots are exhausted and restores when ready", () => {
+ const futureReset = now + 3600;
+ const agCapacity = {observed_at: now, account_ref: "b".repeat(64), model_id: "gemini-4-carbon-high",
+ execution_id: "endpoint-probe:ok", remaining_fraction: 0.9, reset_time: "2026-01-02T00:00:00Z",
+ quota_exhausted: false, normal_completion: true};
+ const exhausted = {
+ now, primary: "codex", pool: ["codex", "antigravity"], current: "codex",
+ primary_capacity: null, fallback_capacity: agCapacity, api_ready: false,
+ native: {
+ policy: {enabled: true, slots: ["slot-a", "slot-b"], api_fallback: true},
+ active_slot: "slot-a", action: "selection_changed", login_matches: true, recovery_required: false,
+ accounts: [
+ {slot: "slot-a", observed_at: now - 600, availability: "exhausted", reset_at: futureReset},
+ {slot: "slot-b", observed_at: now - 600, availability: "exhausted", reset_at: futureReset},
+ ],
+ },
+ };
+ assert.equal(stewardRoute(exhausted).endpoint, "antigravity");
+ assert.equal(stewardRoute({...exhausted, api_ready: true}).endpoint, null);
+ assert.equal(stewardRoute({...exhausted, native: {...exhausted.native, recovery_required: true}}).endpoint, null);
+ assert.equal(stewardRoute({...exhausted, fallback_capacity: {...agCapacity, quota_exhausted: true, normal_completion: false, remaining_fraction: 0}}).endpoint, null);
+ assert.equal(stewardRoute({
+ ...exhausted,
+ current: "antigravity",
+ native: {
+ ...exhausted.native,
+ action: "ready",
+ accounts: [{slot: "slot-a", observed_at: now, availability: "available"}, exhausted.native.accounts[1]],
+ },
+ }).endpoint, "codex");
+});
diff --git a/packages/loopx-codex-provider-routing/tests/test_provider_lifecycle.py b/packages/loopx-codex-provider-routing/tests/test_provider_lifecycle.py
index 406b402db0..4f56d1e37a 100644
--- a/packages/loopx-codex-provider-routing/tests/test_provider_lifecycle.py
+++ b/packages/loopx-codex-provider-routing/tests/test_provider_lifecycle.py
@@ -1,8 +1,10 @@
"""Real loopback admission/drain and real detached provider replacement."""
import importlib
+from http.client import HTTPResponse, IncompleteRead
import io
import json
from pathlib import Path
+import socket
import subprocess
import sys
import threading
@@ -90,11 +92,199 @@ def open(self, call, timeout):
client.join(5)
+def _send_partial_request(server, request):
+ connection = socket.create_connection(("127.0.0.1", server.server_port), timeout=3)
+ connection.sendall(request)
+ return connection
+
+
+@pytest.mark.parametrize("framing", ["short", "ambiguous", "transfer_encoded"])
+def test_invalid_model_request_framing_never_reaches_upstream(owner, framing):
+ relay = importlib.import_module("loopx_codex_provider_routing.openai_relay")
+ body = b'{"model":"selected-model","input":"fixture"}'
+ calls = []
+
+ class Upstream:
+ def open(self, call, timeout):
+ calls.append(call.data)
+ response = io.BytesIO(b'{"ok":true}')
+ response.status, response.headers = 200, {"Content-Type": "application/json"}
+ return response
+
+ length = len(body) + 20 if framing == "short" else len(body)
+ extra_header = {
+ "short": b"",
+ "ambiguous": b"Content-Length: 1\r\n",
+ "transfer_encoded": b"Transfer-Encoding: chunked\r\n",
+ }[framing]
+ headers = (b"POST /v1/responses HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer worker\r\n"
+ + f"Content-Length: {length}\r\n".encode() + extra_header + b"\r\n")
+ server = relay.RelayServer(owner, {"port": 0, "token": "worker"}, opener=Upstream())
+ thread = threading.Thread(target=server.serve_forever)
+ thread.start()
+ try:
+ with _send_partial_request(server, headers + body) as client, HTTPResponse(client) as result:
+ client.shutdown(socket.SHUT_WR)
+ result.begin()
+ assert result.status == 400
+ assert json.loads(result.read())["error"]["code"] == "invalid_request"
+ assert calls == []
+ assert request(server, "/v1/responses", "worker", body=body) == (200, b'{"ok":true}')
+ assert calls == [body]
+ finally:
+ server.shutdown()
+ server.server_close()
+ thread.join(5)
+
+
+def test_drain_releases_stalled_request_headers_and_body_without_waiting_for_clients(owner, monkeypatch):
+ relay = importlib.import_module("loopx_codex_provider_routing.openai_relay")
+ accepted_headers, accepted_body = threading.Event(), threading.Event()
+ original_parse, original_admitted = relay.RelayHandler.parse_request, relay.RelayHandler._admitted
+
+ def parse_request(handler):
+ if handler.raw_requestline.startswith(b"GET /half"):
+ accepted_headers.set()
+ return original_parse(handler)
+
+ def admitted(handler):
+ if handler.path == "/v1/responses":
+ accepted_body.set()
+ return original_admitted(handler)
+
+ monkeypatch.setattr(relay.RelayHandler, "parse_request", parse_request)
+ monkeypatch.setattr(relay.RelayHandler, "_admitted", admitted)
+ calls = []
+
+ class Upstream:
+ def open(self, call, timeout):
+ calls.append(call.data)
+ raise AssertionError("Draining incomplete request reached the upstream")
+
+ server = relay.RelayServer(owner, {"port": 0, "token": "worker", "control_token": "control"}, opener=Upstream())
+ thread = threading.Thread(target=server.serve_forever)
+ closed = threading.Event()
+ def close_after_drain():
+ thread.join()
+ server.server_close()
+ closed.set()
+
+ closer = threading.Thread(target=close_after_drain)
+ thread.start()
+ closer.start()
+ try:
+ with (
+ _send_partial_request(server, b"GET /half HTTP/1.1\r\nHost: lo"),
+ _send_partial_request(server, b"POST /v1/responses HTTP/1.1\r\nHost: localhost\r\n"
+ b"Authorization: Bearer worker\r\nContent-Length: 40\r\n\r\n{"),
+ ):
+ assert accepted_headers.wait(3) and accepted_body.wait(3)
+ live = json.loads(request(server, "/runtime", "worker")[1])
+ assert request(server, "/control/drain", "control", body=b"", instance=live["instance_id"])[0] == 202
+ assert closed.wait(3)
+ assert server.runtime()["active_requests"] == 0
+ assert live["active_requests"] == 0 and calls == []
+ finally:
+ server.shutdown()
+ closer.join(5)
+
+
+def test_idle_body_read_closes_before_model_admission(owner, monkeypatch):
+ relay = importlib.import_module("loopx_codex_provider_routing.openai_relay")
+ monkeypatch.setattr(relay.RelayHandler, "timeout", 0.2)
+ calls = []
+
+ class Upstream:
+ def open(self, call, timeout):
+ calls.append(call.data)
+ raise AssertionError("Incomplete request reached the upstream")
+
+ server = relay.RelayServer(owner, {"port": 0, "token": "worker"}, opener=Upstream())
+ thread = threading.Thread(target=server.serve_forever)
+ thread.start()
+ try:
+ incomplete = (b"POST /v1/responses HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer worker\r\n"
+ b"Content-Length: 40\r\n\r\n{")
+ with _send_partial_request(server, incomplete) as client:
+ assert client.recv(1) == b""
+ assert calls == [] and server.runtime()["active_requests"] == 0
+ finally:
+ server.shutdown()
+ server.server_close()
+ thread.join(5)
+
+
+def test_drain_releases_a_response_with_a_stalled_client(owner, monkeypatch):
+ relay = importlib.import_module("loopx_codex_provider_routing.openai_relay")
+ monkeypatch.setattr(relay.RelayHandler, "timeout", 0.25)
+ owner.api_keys().add(expected_revision=1, label="Spare", api_key="sk-" + "synthetic-spare-" * 4)
+ release, stopped = threading.Event(), threading.Event()
+ calls = []
+
+ class StreamingResponse(io.BytesIO):
+ status, headers = 200, {"Content-Type": "application/octet-stream"}
+
+ def read1(self, size):
+ assert release.wait(5)
+ return b"x" * size
+
+ stream = StreamingResponse()
+
+ class Upstream:
+ def open(self, call, timeout):
+ calls.append(call.data)
+ return stream
+
+ class SmallSendBufferRelay(relay.RelayServer):
+ def get_request(self):
+ connection, address = super().get_request()
+ connection.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 4096)
+ return connection, address
+
+ server = SmallSendBufferRelay(owner, {"port": 0, "token": "worker", "control_token": "control"},
+ opener=Upstream())
+
+ def serve_and_close():
+ try:
+ server.serve_forever()
+ finally:
+ server.server_close()
+ stopped.set()
+
+ thread = threading.Thread(target=serve_and_close)
+ thread.start()
+ body = b'{"model":"selected-model","stream":true}'
+ headers = (b"POST /v1/responses HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer worker\r\n"
+ + f"Content-Length: {len(body)}\r\n\r\n".encode())
+ try:
+ with socket.socket() as client, HTTPResponse(client) as response:
+ client.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 4096)
+ client.settimeout(5)
+ client.connect(("127.0.0.1", server.server_port))
+ client.sendall(headers + body)
+ response.begin()
+ assert response.status == 200 and response.chunked
+ live = json.loads(request(server, "/runtime", "worker")[1])
+ assert live["active_requests"] == 1
+ assert request(server, "/control/drain", "control", body=b"", instance=live["instance_id"])[0] == 202
+ release.set()
+ # Keep the socket open without reading any body bytes until the
+ # listener and all its admitted request threads have drained.
+ assert stopped.wait(3), "A stalled client kept the relay from closing"
+ assert stream.closed and server.runtime()["active_requests"] == 0
+ with pytest.raises(IncompleteRead):
+ response.read()
+ assert calls == [body]
+ finally:
+ release.set()
+ server.shutdown()
+ thread.join(5)
+
+
def test_real_detached_old_build_replaced_once_with_same_routes_and_private_state(owner, monkeypatch):
relay = importlib.import_module("loopx_codex_provider_routing.openai_relay")
life = importlib.import_module("loopx_codex_provider_routing.relay_lifecycle")
native = importlib.import_module("loopx_codex_provider_routing.native")
- import socket
with socket.socket() as sock:
sock.bind(("127.0.0.1", 0))
port = sock.getsockname()[1]
diff --git a/packages/loopx-codex-provider-routing/tests/test_steward_native_owner.py b/packages/loopx-codex-provider-routing/tests/test_steward_native_owner.py
index e7d1a269c7..e8448247b3 100644
--- a/packages/loopx-codex-provider-routing/tests/test_steward_native_owner.py
+++ b/packages/loopx-codex-provider-routing/tests/test_steward_native_owner.py
@@ -45,3 +45,65 @@ def transport(adapter, operation):
state["action"] = "handoff_interrupted"
atomic(owner.path, state)
assert owner.conversation_epoch(codex_home=home) is None
+
+
+def test_codex_primary_steward_route_switches_to_antigravity_fallback_when_exhausted(tmp_path):
+ now = 1767225600
+ home = tmp_path / "codex-home"
+ native = {
+ "active_slot": "slot-a",
+ "selection_revision": "revision-a",
+ "current_login_matches": True,
+ "recovery_required": False,
+ "action": "selection_changed",
+ }
+ owner = NativeAccounts(
+ tmp_path / "runtime",
+ transport=lambda _adapter, _op: native,
+ clock=lambda: now,
+ )
+ owner.root.mkdir(parents=True)
+ state = owner._load()
+ state.update(
+ policy={"enabled": True, "slots": ["slot-a", "slot-b"], "api_fallback": False},
+ adapter={"profile": {"home": str(home)}},
+ action="selection_changed",
+ accounts=[
+ {"slot": "slot-a", "availability": "exhausted", "observed_at": now - 600, "reset_at": now + 3600},
+ {"slot": "slot-b", "availability": "exhausted", "observed_at": now - 600, "reset_at": now + 3600},
+ ],
+ )
+ atomic(owner.path, state)
+ fallback = {
+ "observed_at": now,
+ "account_ref": "b" * 64,
+ "model_id": "gemini-4-carbon-high",
+ "execution_id": "endpoint-probe:ok",
+ "remaining_fraction": 0.99,
+ "reset_time": "2026-01-02T00:00:00Z",
+ "quota_exhausted": False,
+ "normal_completion": True,
+ }
+ assert owner.steward_route(
+ codex_home=home,
+ primary="codex",
+ pool=["codex", "antigravity"],
+ current="codex",
+ primary_capacity=None,
+ fallback_capacity=fallback,
+ model="gpt-6.2-sol",
+ )["endpoint"] == "antigravity"
+ state.update(action="ready", accounts=[
+ {"slot": "slot-a", "availability": "available", "observed_at": now},
+ state["accounts"][1],
+ ])
+ atomic(owner.path, state)
+ assert owner.steward_route(
+ codex_home=home,
+ primary="codex",
+ pool=["codex", "antigravity"],
+ current="antigravity",
+ primary_capacity=None,
+ fallback_capacity=fallback,
+ model="gpt-6.2-sol",
+ )["endpoint"] == "codex"
diff --git a/scripts/chat_bundle.py b/scripts/chat_bundle.py
index bb8d881f5d..acb7def125 100644
--- a/scripts/chat_bundle.py
+++ b/scripts/chat_bundle.py
@@ -14,6 +14,8 @@
import subprocess
import sys
import tempfile
+import time
+from typing import Callable, TypeVar
import zipfile
ROOT = Path(__file__).resolve().parents[1]
@@ -224,19 +226,52 @@ def _ensure(previous: Path | None) -> None:
)
+_T = TypeVar("_T")
+
+
+def _retry_gh(
+ operation: Callable[[], _T],
+ *,
+ label: str,
+ attempts: int = 4,
+ base_delay: float = 1.0,
+ before_attempt: Callable[[], None] | None = None,
+) -> _T:
+ """Retry transient GitHub CLI failures with bounded exponential backoff."""
+ for attempt in range(1, attempts + 1):
+ if before_attempt is not None:
+ before_attempt()
+ try:
+ return operation()
+ except subprocess.CalledProcessError as exc:
+ if attempt >= attempts:
+ raise
+ delay = base_delay * (2 ** (attempt - 1))
+ print(
+ f"Retrying {label} after gh exit {exc.returncode} "
+ f"(attempt {attempt}/{attempts}, sleep {delay:.1f}s)",
+ file=sys.stderr,
+ )
+ time.sleep(delay)
+ raise AssertionError("unreachable")
+
+
def release_build(tag: str, repository: str) -> None:
# Use the preceding published version, never the latest desktop prerelease
# or a future release when rebuilding an older tag.
pages = json.loads(
- subprocess.check_output(
- [
- "gh",
- "api",
- "--paginate",
- "--slurp",
- f"repos/{repository}/releases?per_page=100",
- ],
- text=True,
+ _retry_gh(
+ lambda: subprocess.check_output(
+ [
+ "gh",
+ "api",
+ "--paginate",
+ "--slurp",
+ f"repos/{repository}/releases?per_page=100",
+ ],
+ text=True,
+ ),
+ label=f"gh release listing for {repository}",
)
)
versions = [
@@ -278,23 +313,33 @@ def number(value: str) -> tuple[int, ...]:
if len(wheels) != 1:
raise RuntimeError("preceding release must contain exactly one LoopX wheel")
with tempfile.TemporaryDirectory(prefix="loopx-previous-release-") as temporary:
- directory = Path(temporary)
- subprocess.run(
- [
- "gh",
- "release",
- "download",
- previous["tag_name"],
- "--repo",
- repository,
- "--pattern",
- wheels[0],
- "--pattern",
- "SHA256SUMS",
- "--dir",
- str(directory),
- ],
- check=True,
+ root = Path(temporary)
+ directory = root / "download"
+
+ def reset_download_dir() -> None:
+ shutil.rmtree(directory, ignore_errors=True)
+ directory.mkdir(parents=True, exist_ok=True)
+
+ _retry_gh(
+ lambda: subprocess.run(
+ [
+ "gh",
+ "release",
+ "download",
+ previous["tag_name"],
+ "--repo",
+ repository,
+ "--pattern",
+ wheels[0],
+ "--pattern",
+ "SHA256SUMS",
+ "--dir",
+ str(directory),
+ ],
+ check=True,
+ ),
+ label=f"gh release download {previous['tag_name']}",
+ before_attempt=reset_download_dir,
)
checksums = (directory / "SHA256SUMS").read_text().splitlines()
matches = [
@@ -305,7 +350,7 @@ def number(value: str) -> tuple[int, ...]:
]
if matches != [contract.digest(directory / wheels[0])]:
raise RuntimeError("previous release wheel checksum mismatch")
- old_bundle = directory / "chat"
+ old_bundle = root / "chat"
extract_wheel(directory / wheels[0], old_bundle)
build(old_bundle, install=True)
diff --git a/scripts/semantic_incident_retrodiction.py b/scripts/semantic_incident_retrodiction.py
index 17e910300f..e187128465 100644
--- a/scripts/semantic_incident_retrodiction.py
+++ b/scripts/semantic_incident_retrodiction.py
@@ -254,14 +254,28 @@ def probe_definitions_ratcheted(guard: Guard) -> tuple[bool, str]:
def probe_commit_time_selection(guard: Guard) -> tuple[bool, str]:
"""E07: the guard must be selected before merge, not discovered by the fleet."""
+ from loopx.canary.premerge import build_premerge_validation_gate
+ from loopx.canary.runner import MAINTAINABILITY_RATCHET_SCRIPT
+
planner = (REPO_ROOT / "loopx" / "canary" / "planner.py").read_text(encoding="utf-8")
- premerge = (REPO_ROOT / "loopx" / "canary" / "premerge.py").read_text(encoding="utf-8")
command = "examples/semantic-vocabulary-drift-smoke.py"
planned = command in planner
- inherited = "INHERITED_BASELINE_COMMANDS" in premerge and "planner" in premerge
+ gate = build_premerge_validation_gate(
+ changed_files=["loopx/_probe_semantic_guard.py"],
+ execute=False,
+ repo_root=REPO_ROOT,
+ )
+ selected_commands = [
+ str(check.get("command") or "")
+ for check in (gate.get("catalog_run") or {}).get("selected_checks") or []
+ if isinstance(check, dict)
+ ]
+ selected_by_premerge = any(command in item for item in selected_commands)
+ not_advisory_baseline = command != MAINTAINABILITY_RATCHET_SCRIPT
guarded_roots = [hint for hint in ("loopx/", "scripts/", "examples/") if f'"{hint}"' in planner]
- return planned and inherited, (
- f"named_in_planner={planned} premerge_consumes_planner={inherited} planner_trigger_hints={guarded_roots}"
+ return planned and selected_by_premerge and not_advisory_baseline, (
+ f"named_in_planner={planned} selected_by_premerge={selected_by_premerge} "
+ f"not_advisory_baseline={not_advisory_baseline} planner_trigger_hints={guarded_roots}"
)
diff --git a/skills/loopx-self-repair/references/repair-patterns.md b/skills/loopx-self-repair/references/repair-patterns.md
index 90e88999a0..a38db8cc6d 100644
--- a/skills/loopx-self-repair/references/repair-patterns.md
+++ b/skills/loopx-self-repair/references/repair-patterns.md
@@ -167,6 +167,7 @@ teaches a reusable control-plane lesson.
| `machine_cli_capture_projection_gap` | A native hook or non-model JSON consumer treats context navigation as the effective decision and denies ready work because a top-level admission field is absent. | Original child argv/runtime profile; outer schema versus the same immutable captured Core decision; parent/child context settings; real Core ready, user-gate and zero-quota cases. | Model-facing context transport wrapped the formerly inline machine payload. A locator is not a policy decision; a locale/codec repair alone cannot restore this path. | Use the existing machine-output contract only for the non-model child process, retaining Core gates, identity, UTF-8 and environment isolation. Reuse the established per-child `LOOPX_CONTEXT_DELIVERY=off`; do not disable model context globally or interpret navigation `ok` as admission. Actual model consumers must follow the original readers and retain current bytes/digests. Validate ready, user-gate and zero-quota behavior with parent context on/off unchanged; distinguish source, installed-host and live-model qualification. |
| `dsh_quota_capture_read_transport_loss` | DSH abandons a continuation after LoopX has saved its admission and a quota reader subprocess fails to return a page. | Compare the original Turn receipt, recorded reader arguments and page cursor with the subsequent reservation and settlement records. Separate lost transport from a typed denial or changed digest. | Guard transport retry did not apply to the separate read of its saved capture. | Retry the same verified page with the existing bounded CLI retry. Preserve the reference, offset, selection digest, cancellation, identity revalidation and queue ownership. Verify with a lost response on a later page, an unrecoverable read and the complete original-Turn settlement. |
| `cli_descendant_holds_timeout_pipe` | A CLI deadline has elapsed and the leader exited, but the host still waits for output held open by a descendant. | Compare the leader and owned group state, stdout/stderr closure, and the retry or mutation readback. Reproduce in an isolated process group. | Killing the direct child does not retire its inherited pipe writers. A closed pipe is not proof that a writer stopped. | Confirm cleanup of the command's owned group and drain its pipes before retrying a safe read. An escaped writer or unverifiable cleanup must not authorize replay or a fallback install. A mutation keeps its unknown-result and authoritative readback path; do not kill unrelated process trees. Keep POSIX group, detached-session and Windows evidence separate. |
+| `relay_truncation_looks_like_clean_eof` | A Responses client receives an output or tool-item prefix followed by apparently clean HTTP EOF after the upstream body was truncated. A client that stops reading can also keep the relay from draining. | Observe upstream and downstream framing, exact prefix bytes, client status, and actual request count; compare a short declared length, missing final HTTP chunk, and a healthy completion. For drain, check lock release with an open stalled client and an independent healthy reader. | The relay replaced a framed upstream response with close-delimited output, which erases the transport's incomplete-body signal. A syntactically complete error JSON can also be carried in an incomplete HTTP body. Clearing the local socket timeout at model admission leaves a response write waiting indefinitely. | Preserve a verified response length or terminate downstream chunked framing only after clean transfer. Do not manufacture a model terminal event or retry a request with unknown effects. Verify full and truncated paths over real loopback HTTP, keep already received bytes, and separately qualify explicit same-session recovery. Bound loopback writes independently of the upstream timeout. Verify that a healthy reader receives its original response before the instance releases its lock. |
| `native_policy_test_consumer_navigation` | A source-owned policy smoke intentionally exercises native output but raises `KeyError` on an admission field, or mistakes navigation `ok` for decision readiness. | Original CLI result and the same current immutable recovery, unchanged semantic assertions, and full connection/own-Todo readback. | The test helper decoded only outer JSON and treated the model-facing navigation envelope as the policy. This differs from the machine-output host adapter in `machine_cli_capture_projection_gap`. | Reuse the existing test-only `read_cli_policy_payload` oracle to verify the current complete recovery; preserve inline results, nonzero exits, physical-file/digest checks and all original semantic assertions. Keep native output enabled for this qualification. Test generic/domain connection and the caller's own first Todo through the real CLI, plus failed/inline/native and damaged-reference cases. Isolate temporary runtimes and telemetry before child invocation and retire only authenticated test-owned runtimes before removing their directory. The oracle proves test observations, not model retention, execution authority or whole-Goal benefit. |
| `replan_settlement_binding_projection_gap` | A replan packet and selected Todo are both visible, so an agent combines `--todo-id` with `--replan-obligation-id`, alternates between them after rejection, or performs the Todo-bound refresh but omits the matching spend. | Original quota receipt identity, selected Todo, semantic obligation id, interaction settlement plan, projected refresh/spend commands, and accepted semantic ACK. | The semantic obligation target and causal settlement identity were adjacent but unlabeled, while one boolean conflated direct obligation binding with whether a full settlement chain was required. | Keep exactly one causal settlement binding. For a turn-scoped executable plan, project a domain-local typed replan settlement contract that labels the selected Todo or Todo-less obligation as the binding and keeps the replan id as a separate semantic target when necessary. Todo-bound and directly bound replan writes both project material refresh followed by spend; never combine their CLI flags. Keep unscoped diagnostic reads compact and non-executable. Cover TS projection, hot-path envelope, real Todo-bound/Todo-less CLI chains, output budget, and in-flight delivery continuity. |
| `native_goal_todoless_replan_reentry_gap` | A native visible Goal completes all advancement Todos, semantic closeout derives a Todo-less autonomous replan obligation, and the next unbound quota read projects a bare refresh with no Turn identity or spend chain. | Native Goal runtime profile, selected Todo absence, replan obligation id, interaction CLI actions before and after Turn creation, and settlement identity. | Native-Goal Turn re-entry recognized only selected Todos as actionable settlement bindings and ignored a valid Todo-less replan obligation. | Treat either a selected Todo or a valid replan obligation as a native-Goal settlement binding. When no Turn or settlement plan exists, project only the exact `quota should-run ... --begin-turn` re-entry. After execution, require the obligation-bound refresh and spend chain; keep generic unscoped diagnostic behavior unchanged. |
diff --git a/tests/capabilities/test_capability_configuration_ui.py b/tests/capabilities/test_capability_configuration_ui.py
index ab045c6bd1..3b4dac0d53 100644
--- a/tests/capabilities/test_capability_configuration_ui.py
+++ b/tests/capabilities/test_capability_configuration_ui.py
@@ -313,6 +313,58 @@ def test_steward_executor_editor_is_machine_only_and_typed() -> None:
)
+def test_steward_executor_editor_documents_json_endpoint_models_and_preserves_catalog_values() -> None:
+ editor = capability_configuration_editor("steward_executor")
+ fields = {field["key"]: field for field in editor["fields"]}
+ assert "endpoint_models" not in fields
+ assert {field["input_kind"] for field in editor["fields"]} <= {
+ "boolean",
+ "number",
+ "select",
+ "string_list",
+ "text",
+ "periodic_report_schedule",
+ }
+ assert "endpoint_models" in fields["eligible_endpoints"]["description"]
+ assert "JSON mode" in fields["eligible_endpoints"]["description"]
+ assert "endpoint_models" in fields["executor_model"]["description"]
+ assert "JSON mode" in fields["executor_model"]["description"]
+
+ configured = {
+ "schema_version": "steward_executor_machine_defaults_v1",
+ "selection_policy": "flexible",
+ "executor_endpoint": "codex",
+ "eligible_endpoints": ["codex", "antigravity"],
+ "executor_model": "gpt-6.2-sol",
+ "executor_reasoning_effort": "xhigh",
+ "endpoint_models": {
+ "antigravity": {"model": "carbon", "reasoning_effort": "medium"},
+ },
+ }
+ catalog = build_capability_configuration_catalog(
+ machine_namespaces=[
+ {
+ "namespace": "steward_executor",
+ "title": "Steward executor",
+ "current": configured,
+ "configuration_template": {
+ "schema_version": "steward_executor_machine_defaults_v1",
+ "selection_policy": "preferred",
+ "executor_endpoint": "codex",
+ "eligible_endpoints": [],
+ "executor_model": None,
+ "executor_reasoning_effort": None,
+ },
+ }
+ ]
+ )
+ capability = catalog["capabilities"][0]
+ assert capability["machine_current"]["endpoint_models"] == {
+ "antigravity": {"model": "carbon", "reasoning_effort": "medium"},
+ }
+ assert capability["effective_configuration"]["configuration"] == configured
+
+
def test_catalog_merges_machine_and_goal_descriptors_without_losing_scope() -> None:
catalog = build_capability_configuration_catalog(
machine_namespaces=[
diff --git a/tests/capabilities/test_issue_fix_maintainer_correction_admission.py b/tests/capabilities/test_issue_fix_maintainer_correction_admission.py
index a8fa7106f7..aa35f0cec0 100644
--- a/tests/capabilities/test_issue_fix_maintainer_correction_admission.py
+++ b/tests/capabilities/test_issue_fix_maintainer_correction_admission.py
@@ -15,6 +15,7 @@
import pytest
+from loopx.control_plane.goals.activation import build_goal_activation
from loopx.control_plane.testing.canary_harness import import_fixture_authority
from loopx.domain_packs.issue_fix import (
default_issue_fix_domain_state_ledger_path,
@@ -400,7 +401,17 @@ def test_t05_missing_event_metadata_is_context_only(harness: Harness) -> None:
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_t06_stopped_goal_refuses_at_effect_time(tmp_path: Path, provider: str) -> None:
- h = Harness(tmp_path, provider, goal_overrides={"activation_state": "stopped"})
+ h = Harness(
+ tmp_path,
+ provider,
+ goal_overrides={
+ "activation": build_goal_activation(
+ state="stopped",
+ updated_at="2026-06-20T09:00:00Z",
+ reason="Owner stopped goal before correction settlement.",
+ )
+ },
+ )
event = _event("6001")
payload = h.run_json(metadata=_metadata([event]), correction=_correction_v1(event))
admission = payload["maintainer_correction_admission"]
diff --git a/tests/capabilities/test_periodic_report_native_successor.py b/tests/capabilities/test_periodic_report_native_successor.py
index 7cf667a509..7adaa59c0b 100644
--- a/tests/capabilities/test_periodic_report_native_successor.py
+++ b/tests/capabilities/test_periodic_report_native_successor.py
@@ -78,13 +78,19 @@ def _native_successor(
selected = first_guard["selected_todo"]["todo_id"]
bound_guard = _run_cli([*guard, "--todo-id", selected])
assert bound_guard["heartbeat_receipt"]["settlement_identity"]["todo_id"] == selected
- review = json.loads(subprocess.check_output(
- shlex.split(bound_guard["replan_action_packet"]["review"]), text=True,
- ))
- writeback = json.loads(subprocess.check_output(
- shlex.split(review["next"]["writeback"]), text=True,
- ))
- assert writeback["obligation_id"] == bound_guard["replan_action_packet"]["obligation_id"]
+ packet = bound_guard["replan_action_packet"]
+ if "review" in packet:
+ review = json.loads(subprocess.check_output(
+ shlex.split(packet["review"]), text=True,
+ ))
+ writeback = json.loads(subprocess.check_output(
+ shlex.split(review["next"]["writeback"]), text=True,
+ ))
+ assert writeback["obligation_id"] == packet["obligation_id"]
+ stride_decision = writeback["stride_control"]["minimal_example"]
+ else:
+ stride_decision = packet["stride_control"]["minimal_example"]
+ assert stride_decision["reference_obligation_id"] == packet["obligation_id"]
successor_vision = {
"schema_version": "goal_vision_replan_contract_v0",
@@ -101,7 +107,7 @@ def _native_successor(
"evidence_refs": ["fixture:accepted-stage-boundary"],
"changed": ["Advance the successor fixture check."],
},
- "stride_decision": writeback["stride_control"]["minimal_example"],
+ "stride_decision": stride_decision,
}
vision_path = project / "successor-vision.json"
vision_path.write_text(json.dumps(successor_vision), encoding="utf-8")
diff --git a/tests/capabilities/test_periodic_report_pending_intent.py b/tests/capabilities/test_periodic_report_pending_intent.py
index ebb89132b2..7c2f0aaba4 100644
--- a/tests/capabilities/test_periodic_report_pending_intent.py
+++ b/tests/capabilities/test_periodic_report_pending_intent.py
@@ -236,7 +236,7 @@ def test_calendar_reporter_replacement_uses_admitted_window_state(tmp_path, prep
@pytest.mark.parametrize(
- "change", ["reporter", "schedule", "disabled", "stopped", "activation_stopped"]
+ "change", ["reporter", "schedule", "disabled", "archived", "activation_stopped"]
)
def test_calendar_hook_rechecks_live_authority_after_construction(tmp_path, change):
from datetime import datetime
@@ -256,8 +256,8 @@ def test_calendar_hook_rechecks_live_authority_after_construction(tmp_path, chan
goal["control_plane"]["periodic_report"]["schedule"]["rrule"] = "FREQ=WEEKLY;BYDAY=FR;BYHOUR=17"
elif change == "disabled":
goal["control_plane"]["periodic_report"]["enabled"] = False
- elif change == "stopped":
- goal["status"] = "stopped"
+ elif change == "archived":
+ goal["status"] = "archived"
else:
goal["activation"] = {
"schema_version": "loopx_goal_activation_v1",
@@ -303,6 +303,8 @@ def test_inactive_goal_does_not_reactivate_an_admitted_report(tmp_path, field, v
periodic_report_cadence_hooks,
)
from loopx.capabilities.periodic_report.cadence_journal import read_cadence_journal
+ from loopx.control_plane.goals.activation_service import upgrade_goal_activation_registries
+ from loopx.control_plane.projects.registry_codec import ProjectRegistryProtocolError
registry, runtime = _calendar_fixture(tmp_path)
kwargs = dict(registry_path=registry, runtime_root=runtime, goal_id=GOAL_ID, agent_id=AGENT_ID)
@@ -311,6 +313,13 @@ def test_inactive_goal_does_not_reactivate_an_admitted_report(tmp_path, field, v
config = json.loads(registry.read_text())
config["goals"][0][field] = value
registry.write_text(json.dumps(config))
+ if field == "status" and value in ("stopped", "paused"):
+ with pytest.raises(ProjectRegistryProtocolError):
+ periodic_report_cadence_hooks(**kwargs)
+ with pytest.raises(ProjectRegistryProtocolError):
+ pending_periodic_report_intents(**kwargs)
+ assert read_cadence_journal(runtime_root=runtime, goal_id=GOAL_ID) == original
+ upgrade_goal_activation_registries([registry], execute=True)
assert periodic_report_cadence_hooks(**kwargs) == ()
assert pending_periodic_report_intents(**kwargs) == []
assert consume_pending_periodic_report_intent(**kwargs, execute=True)["status"] == "no_pending_intent"
diff --git a/tests/capabilities/test_periodic_report_turn_replan.py b/tests/capabilities/test_periodic_report_turn_replan.py
index 3f84402014..08e8fd477a 100644
--- a/tests/capabilities/test_periodic_report_turn_replan.py
+++ b/tests/capabilities/test_periodic_report_turn_replan.py
@@ -119,8 +119,11 @@ def test_turn_milestone_intent_is_replay_stable(
author_candidate = (
"import json, shlex, subprocess\n"
f"candidate = {candidate!r}\n"
+ f"expected_schema = {'loopx_turn_model_context_v0' if host_kind == 'dsh' else 'loopx_turn_host_request_v1'!r}\n"
"def authored_candidate(request):\n"
- " assert request['schema_version'] == 'loopx_turn_host_request_v1'\n"
+ " assert request['schema_version'] == expected_schema\n"
+ " if expected_schema == 'loopx_turn_model_context_v0':\n"
+ " assert request['source_schema_version'] == 'loopx_turn_host_request_v1'\n"
" packet = request['detail_ref']['replan_action_packet']\n"
" review = json.loads(subprocess.check_output(\n"
" shlex.split(packet['review']), text=True))\n"
diff --git a/tests/capabilities/test_periodic_report_turn_writeback.py b/tests/capabilities/test_periodic_report_turn_writeback.py
index d7192adab9..cde8507cb6 100644
--- a/tests/capabilities/test_periodic_report_turn_writeback.py
+++ b/tests/capabilities/test_periodic_report_turn_writeback.py
@@ -94,13 +94,19 @@ def _accepted_closed_vision_and_successor(
assert guard["decision"] == "autonomous_replan_required"
todo_id = guard["selected_todo"]["todo_id"]
bound_guard = _run_cli([*guard_args, "--todo-id", todo_id])
- review = json.loads(subprocess.check_output(
- shlex.split(bound_guard["replan_action_packet"]["review"]), text=True,
- ))
- writeback = json.loads(subprocess.check_output(
- shlex.split(review["next"]["writeback"]), text=True,
- ))
- assert writeback["obligation_id"] == bound_guard["replan_action_packet"]["obligation_id"]
+ packet = bound_guard["replan_action_packet"]
+ if "review" in packet:
+ review = json.loads(subprocess.check_output(
+ shlex.split(packet["review"]), text=True,
+ ))
+ writeback = json.loads(subprocess.check_output(
+ shlex.split(review["next"]["writeback"]), text=True,
+ ))
+ assert writeback["obligation_id"] == packet["obligation_id"]
+ stride_decision = writeback["stride_control"]["minimal_example"]
+ else:
+ stride_decision = packet["stride_control"]["minimal_example"]
+ assert stride_decision["reference_obligation_id"] == packet["obligation_id"]
successor_vision = {
"schema_version": "goal_vision_replan_contract_v0",
@@ -117,7 +123,7 @@ def _accepted_closed_vision_and_successor(
"evidence_refs": ["fixture:accepted-closed-vision"],
"changed": ["Finish the remaining fixture Todo."],
},
- "stride_decision": writeback["stride_control"]["minimal_example"],
+ "stride_decision": stride_decision,
}
vision_path = project / "successor-vision.json"
vision_path.write_text(json.dumps(successor_vision), encoding="utf-8")
@@ -131,6 +137,13 @@ def _accepted_closed_vision_and_successor(
assert ack["recorded"] is True
assert ack["semantic_delta"]["accepted"] is True
assert "fresh_vision_path_outcome" in ack["semantic_delta"]["outcomes"]
+ spent = _run_cli([
+ "--registry", str(registry), "--runtime-root", str(runtime), "--format", "json",
+ "quota", "spend-slot", "--goal-id", GOAL_ID,
+ "--agent-id", AGENT_ID, "--todo-id", todo_id, "--turn-instance-id", turn_id,
+ "--slots", "1", "--source", "heartbeat", "--execute",
+ ])
+ assert spent["appended"] is True
def _enable_weekly_report(registry: Path) -> None:
@@ -225,7 +238,7 @@ def count_optional_source_read(*args: object, **kwargs: object) -> object:
).read_text(encoding="utf-8").splitlines()
]
assert sum(row.get("classification") == "fixture_completion" for row in rows) == 1
- assert sum(row.get("classification") == "quota_slot_spent" for row in rows) == 1
+ assert sum(row.get("classification") == "quota_slot_spent" for row in rows) == 2
if enabled:
assert replay["terminal_post_writeback_hooks"]["intents"] == []
assert {
@@ -302,7 +315,7 @@ def fail_one_terminal_response(**kwargs: object) -> dict[str, object]:
).read_text(encoding="utf-8").splitlines()
]
assert sum(row.get("classification") == "fixture_completion" for row in rows) == 1
- assert sum(row.get("classification") == "quota_slot_spent" for row in rows) == 1
+ assert sum(row.get("classification") == "quota_slot_spent" for row in rows) == 2
@pytest.mark.parametrize("provider", ["file", "sqlite"])
@@ -420,7 +433,7 @@ def malformed_optional_readback(*args: object, **kwargs: object) -> object:
).read_text(encoding="utf-8").splitlines()
]
assert sum(row.get("classification") == "fixture_completion" for row in rows) == 1
- assert sum(row.get("classification") == "quota_slot_spent" for row in rows) == 1
+ assert sum(row.get("classification") == "quota_slot_spent" for row in rows) == 2
def _public_closed_current_vision_args(
diff --git a/tests/capabilities/test_steward_executor_machine_defaults.py b/tests/capabilities/test_steward_executor_machine_defaults.py
index 2c12817d8d..ca01868214 100644
--- a/tests/capabilities/test_steward_executor_machine_defaults.py
+++ b/tests/capabilities/test_steward_executor_machine_defaults.py
@@ -351,3 +351,41 @@ def test_restored_executor_model_ignores_partial_pre_v0_and_validates_v0() -> No
}
) == {"model": "legacy-model"}
assert restored_executor_model({"loopx_tools": False, "loopx_executor": {"model": "x"}}) is None
+
+
+def test_flexible_endpoint_models_normalize_validate_and_project() -> None:
+ base = {
+ "schema_version": "steward_executor_machine_defaults_v1",
+ "selection_policy": "flexible",
+ "executor_endpoint": "codex",
+ "eligible_endpoints": ["codex", "antigravity"],
+ "executor_model": "gpt-6.2-sol",
+ "executor_reasoning_effort": "xhigh",
+ }
+ normalized = normalize_steward_executor_machine_defaults({
+ **base,
+ "endpoint_models": {
+ "antigravity": {"model": "gemini-4-carbon-high", "reasoning_effort": "high"},
+ "codex": {"model": None, "reasoning_effort": None},
+ },
+ })
+ assert normalized["endpoint_models"] == {
+ "antigravity": {"model": "gemini-4-carbon-high", "reasoning_effort": "high"},
+ }
+ effective = effective_steward_executor_defaults(_document({"steward_executor": normalized}))
+ assert effective["endpoint_models"] == {
+ "antigravity": {"model": "gemini-4-carbon-high", "reasoning_effort": "high"},
+ }
+ without = effective_steward_executor_defaults(_document({"steward_executor": {
+ **base,
+ "endpoint_models": {"antigravity": {"model": "", "reasoning_effort": None}},
+ }}))
+ assert "endpoint_models" not in without
+ for bad in [
+ {"endpoint_models": "bad"},
+ {"endpoint_models": {"dhs": {"model": "x"}}},
+ {"endpoint_models": {"antigravity": {"unknown": "x"}}},
+ {"endpoint_models": {"antigravity": {"reasoning_effort": "maximum"}}},
+ ]:
+ with pytest.raises((TypeError, ValueError)):
+ normalize_steward_executor_machine_defaults({**base, **bad})
diff --git a/tests/cli_commands/test_quota_turn_envelope_validation_failure.py b/tests/cli_commands/test_quota_turn_envelope_validation_failure.py
index bd667aa8fd..4adab21f65 100644
--- a/tests/cli_commands/test_quota_turn_envelope_validation_failure.py
+++ b/tests/cli_commands/test_quota_turn_envelope_validation_failure.py
@@ -68,6 +68,8 @@ def _fail_context(*_args, **_kwargs):
assert payload.get("decision") == "skip"
assert payload.get("error_code")
if turn_envelope:
- # The envelope is additive; on a failure payload it degrades to the
- # typed diagnostic instead of masking it with a renderer crash.
- assert payload.get("turn_envelope_skipped") or payload.get("view") == "turn_envelope"
+ # Collection/validation failures without an interaction contract
+ # retain the exact typed diagnostic without crashing or reentering.
+ assert payload.get("error_code") == "QUOTA_VALIDATION_FAILED"
+ assert "interaction_contract" not in payload
+ assert "turn_envelope_skipped" not in payload
diff --git a/tests/control_plane/replan_packet_fixture.py b/tests/control_plane/replan_packet_fixture.py
index 2f2f5bbdf9..23d9aec6bd 100644
--- a/tests/control_plane/replan_packet_fixture.py
+++ b/tests/control_plane/replan_packet_fixture.py
@@ -24,7 +24,17 @@ def populated_replan_source(root, *, acceptance=False, explore_context=None):
def scoped(value):
if isinstance(value, dict):
- return {key: scoped(item) for key, item in value.items()}
+ mapped = {key: scoped(item) for key, item in value.items()}
+ if (
+ mapped.get("schema_version") == "quota_settlement_identity_v0"
+ and mapped.get("todo_id")
+ and mapped.get("turn_instance_id")
+ ):
+ mapped["effect_id"] = (
+ f"{mapped['goal_id']}:{mapped['agent_id']}:"
+ f"{mapped['todo_id']}:{mapped['turn_instance_id']}"
+ )
+ return mapped
if isinstance(value, list):
return [scoped(item) for item in value]
if value == "evidence-goal":
@@ -60,6 +70,10 @@ def scoped(value):
row["generated_at"] = (start + timedelta(minutes=index)).isoformat()
row["turn_instance_id"] = f"turn-synthetic-{index}"
row["settlement_identity"]["turn_instance_id"] = row["turn_instance_id"]
+ row["settlement_identity"]["effect_id"] = (
+ f"{row['settlement_identity']['goal_id']}:{row['settlement_identity']['agent_id']}:"
+ f"{row['settlement_identity']['todo_id']}:{row['turn_instance_id']}"
+ )
rows.append(row)
contract = None
if acceptance:
diff --git a/tests/control_plane/test_acceptance_restoration.py b/tests/control_plane/test_acceptance_restoration.py
index 092bb81a64..782cc92f1b 100644
--- a/tests/control_plane/test_acceptance_restoration.py
+++ b/tests/control_plane/test_acceptance_restoration.py
@@ -76,8 +76,6 @@ def inspect():
assert first["acquired"]
turn_binding = ["--agent-id", "agent-a", "--todo-id", target,
"--turn-instance-id", "turn-restore-acceptance"]
- guard = cli("quota", "should-run", "--codex-app", *turn_binding, )
- assert guard["heartbeat_receipt"]["settlement_identity"]["todo_id"] == target
cli("todo", "update", "--todo-id", target, "--agent-id", "agent-a", "--clear-resume-when",
"--task-lease-idempotency-key", "execution-one", "--task-lease-expected-version", "1")
assert inspect()["goal_acceptance_contract"]["tasks"][0]["state"] == "stale"
@@ -112,6 +110,8 @@ def turn_holds():
next_lease = cli("task-lease", "acquire", "--todo-id", target, "--owner", "agent-a",
"--idempotency-key", "execution-two", "--expected-version", "1", "--ttl-seconds", "600")
assert next_lease["acquired"] and next_lease["lease"]["version"] == 2
+ guard = cli("quota", "should-run", "--codex-app", *turn_binding, )
+ assert guard["heartbeat_receipt"]["settlement_identity"]["todo_id"] == target
# Recovery replay remains historical after a fresh execution starts; it
# cannot release or acquire another generation, complete work or spend quota.
after_acquire = inspect()
diff --git a/tests/control_plane/test_actual_default_model_behavior_portfolio.py b/tests/control_plane/test_actual_default_model_behavior_portfolio.py
index 7ca3c6f1ba..f82a4a839d 100644
--- a/tests/control_plane/test_actual_default_model_behavior_portfolio.py
+++ b/tests/control_plane/test_actual_default_model_behavior_portfolio.py
@@ -722,6 +722,8 @@ def test_live_packet_builder_preserves_gate_and_vision_contracts(tmp_path: Path)
"activation_state": "goal_selection_required",
"activation_allowed": False,
"activation_required_after_todo_write": False,
+ "host_surface": None,
+ "activation_method": None,
}
assert (
onboarding_entry_semantic_contract(packets["onboarding_goal_selection_gate"])[
diff --git a/tests/control_plane/test_agent_preferences.py b/tests/control_plane/test_agent_preferences.py
index fb69b6de9a..30b7cef208 100644
--- a/tests/control_plane/test_agent_preferences.py
+++ b/tests/control_plane/test_agent_preferences.py
@@ -212,12 +212,13 @@ def call(action, *args):
assert authority["interaction_contract"]["agent_channel"]["required_reads"] == []
prompt = render_prompt(authority)
rendered = json.loads(prompt.split("Turn request:\n", 1)[1].split("\n\n", 1)[0])
- assert rendered["work_contract"]["interaction_contract"]["agent_channel"]["work_context"]["unavailable_context"] == unavailable
+ assert rendered["schema_version"] == "loopx_turn_model_context_v0"
+ assert rendered["task_context"]["interaction_contract"]["agent_channel"]["work_context"]["unavailable_context"] == unavailable
assert "work_context" in prompt and "independent work retains existing authority" in prompt
assert "Use a reviewer." not in prompt and "{broken" not in prompt
rc, quota_envelope = _run_cli(registry, runtime, "quota", "should-run", *scope, "--turn-envelope")
assert rc == 0, quota_envelope
- assert quota_envelope["contract_capsule"]["unavailable_context"] == unavailable
+ assert quota_envelope["interaction_contract"]["agent_channel"]["work_context"]["unavailable_context"] == unavailable
changed = deepcopy(failed_plan)
del changed["work_contract"]["interaction_contract"]["agent_channel"]["work_context"]["unavailable_context"]
with pytest.raises(ValueError, match="complete Turn contract rejected"):
diff --git a/tests/control_plane/test_bound_owner_web_input.py b/tests/control_plane/test_bound_owner_web_input.py
index 4088685c98..3dcea654b5 100644
--- a/tests/control_plane/test_bound_owner_web_input.py
+++ b/tests/control_plane/test_bound_owner_web_input.py
@@ -185,9 +185,9 @@ def observe(_):
def test_http_binding_discovery_preserves_duplicate_observations_for_the_typed_owner(bound_chat, monkeypatch):
store, _, _, _, post, open_bound, _, _ = bound_chat
binding, _ = open_bound()
- observed = store.list_sessions()
+ observed = list(store.iter_session_metadata())
# Inject an ambiguous storage read, never duplicate/corrupt a live store.
- monkeypatch.setattr(store, "list_sessions", lambda **_: observed + observed)
+ monkeypatch.setattr(store, "iter_session_metadata", lambda **_: iter(observed + observed))
status, denied = _bound_sessions(post, "conversation_binding_id=" + binding["binding_id"])
assert status == 409 and "sessions" not in denied
diff --git a/tests/control_plane/test_canonical_todo_writer_isolation.py b/tests/control_plane/test_canonical_todo_writer_isolation.py
index 1c6051c268..04350273fa 100644
--- a/tests/control_plane/test_canonical_todo_writer_isolation.py
+++ b/tests/control_plane/test_canonical_todo_writer_isolation.py
@@ -348,6 +348,7 @@ def guard(turn):
offline.rename(backend)
refreshed = run("refresh-state", *identity, "--turn-instance-id", settlement.TURN_ID,
+ *proof,
"--classification", "validated_progress", "--delivery-batch-scale", "implementation",
"--delivery-outcome", "outcome_progress", "--no-global-sync", "--suppress-external-sinks",
"--vision-state", "vision_on_track", "--vision-summary", "Continue canonical delivery.",
diff --git a/tests/control_plane/test_capability_monitor_repair_tool_behavior.py b/tests/control_plane/test_capability_monitor_repair_tool_behavior.py
index 9d7110336b..ee21888e65 100644
--- a/tests/control_plane/test_capability_monitor_repair_tool_behavior.py
+++ b/tests/control_plane/test_capability_monitor_repair_tool_behavior.py
@@ -138,9 +138,19 @@ def transport(**kwargs: Any) -> Mapping[str, Any]:
assert CAPABILITY_REPAIR_TARGET_CAPABILITY in quota_result[
"capability_gate"
]["repair_missing"]
- assert quota_result["agent_todo_summary"]["monitor_schedule_gap_items"][0][
- "todo_id"
- ] == CAPABILITY_REPAIR_MONITOR_TODO_ID
+ from loopx.control_plane.todos.todo_semantics import (
+ todo_summary_monitor_schedule_gap_items,
+ )
+
+ summary = quota_result["agent_todo_summary"]
+ gap_items = summary.get("monitor_schedule_gap_items")
+ if gap_items is None:
+ assert (
+ summary["monitor_schedule_gap_items_ref"]
+ == "$.agent_todo_summary.monitor_open_items"
+ )
+ gap_items = todo_summary_monitor_schedule_gap_items(summary)
+ assert gap_items[0]["todo_id"] == CAPABILITY_REPAIR_MONITOR_TODO_ID
def test_actor_rejects_capability_repair_before_quota(tmp_path: Path) -> None:
diff --git a/tests/control_plane/test_checkpoint_restore_capture.py b/tests/control_plane/test_checkpoint_restore_capture.py
index 4c08087b34..32efab1b21 100644
--- a/tests/control_plane/test_checkpoint_restore_capture.py
+++ b/tests/control_plane/test_checkpoint_restore_capture.py
@@ -169,7 +169,11 @@ def test_sealed_pending_basis_reuses_original_guard_and_rejects_replacements(tmp
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}})
+ complete_source = {
+ key: value for key, value in source.items()
+ if key not in {"schema_version", "contract_binding"}
+ }
+ complete = compose_turn_decision({**complete_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)}]
@@ -265,11 +269,16 @@ def test_successor_review_needs_checkpoint_linked_settlement(tmp_path, monkeypat
from loopx.control_plane.quota.settlement import read_heartbeat_settlement
call, runtime, _ = _fixture(tmp_path, monkeypatch, provider)
+ from loopx.control_plane.quota.turn_envelope import project_captured_replan_cli_view
original = call("quota", "should-run", "--codex-app", "--goal-id", GOAL,
"--agent-id", AGENT, "--turn-instance-id", TURN,
"--codex-app-current-rrule", "FREQ=MINUTELY;INTERVAL=3",
"--decision-output-dir", str(tmp_path / "decision"))
- read = subprocess.run(shlex.split(original["replan_action_packet"]["writeback"]),
+ packet = original["replan_action_packet"]
+ writeback_cmd = packet.get("writeback") or project_captured_replan_cli_view(
+ original, captured_decision_path=str(tmp_path / "decision" / "decision.json"),
+ )["replan_action_packet"]["writeback"]
+ read = subprocess.run(shlex.split(writeback_cmd),
capture_output=True, text=True, check=True)
writer = json.loads(read.stdout)
successor_binding = shlex.split(writer["writeback_contract"]["successor_binding_args"])
@@ -289,12 +298,15 @@ def test_successor_review_needs_checkpoint_linked_settlement(tmp_path, monkeypat
assert successor["settlement_identity"] == original["heartbeat_receipt"]["settlement_identity"]
# Follow only the returned receipt; no fresh guard or successor admission.
steps = successor["settlement_plan"]["ordered_steps"]
- actions = [step["command_template"] for step in steps if step["kind"] in {"durable_writeback", "quota_spend"}]
+ actions = [
+ step["command_template"] for step in steps
+ if step["kind"] in {"durable_writeback", "quota_spend"} and "command_template" in step
+ ]
from loopx.control_plane.todos.markdown import render_todo_markdown
rendered = render_todo_markdown(successor)
assert successor["instruction"] in rendered
assert all(command in rendered for command in actions)
- actions[0] = actions[0].replace("", "source_review").replace("", "single_surface").replace("", "outcome_progress")
+ writeback_action = actions[0].replace("", "source_review").replace("", "single_surface").replace("", "outcome_progress")
sha = "a" * 64
def capture():
@@ -317,18 +329,29 @@ def capture():
"evidence_refs": ["checkpoint:" + sha if cited else "evidence:source-review"]}}
vision = tmp_path / "checkpoint-review.json"
vision.write_text(json.dumps(packet))
- refresh_args = (*shlex.split(actions[0])[1:], "--agent-vision-json", str(vision),
- "--no-global-sync", "--suppress-external-sinks")
if cited:
- settled = call(*refresh_args, "--settle-turn")
+ settled = call(
+ *shlex.split(writeback_action)[1:],
+ "--agent-vision-json", str(vision), "--no-global-sync", "--suppress-external-sinks",
+ )
assert settled["settlement_progress"]["state"] == "settled"
else:
- call(*refresh_args)
+ written = call(
+ *shlex.split(writeback_action.replace(" --settle-turn --settlement-receipt", ""))[1:],
+ "--agent-vision-json", str(vision), "--no-global-sync", "--suppress-external-sinks",
+ )
assert capture()["capture_allowed"] is False # Writeback alone is not settled.
- call(*shlex.split(actions[1])[1:])
+ spend_cmd = shlex.split(written["settlement_owed"]["command"])[1:]
+ call(*spend_cmd)
assert capture()["capture_allowed"] is cited
# Retrying the original debit repairs/replays its receipt, never starts the successor.
- call(*shlex.split(actions[1])[1:])
+ if cited:
+ call(
+ *shlex.split(writeback_action)[1:],
+ "--agent-vision-json", str(vision), "--no-global-sync", "--suppress-external-sinks",
+ )
+ else:
+ call(*spend_cmd)
assert capture()["capture_allowed"] is cited
assert call("todo", "list", "--goal-id", GOAL,
"--todo-id", successor["todo_id"])["todo"]["status"] == "open"
diff --git a/tests/control_plane/test_cli_output_budget.py b/tests/control_plane/test_cli_output_budget.py
index 312c8dead6..37cad32ed2 100644
--- a/tests/control_plane/test_cli_output_budget.py
+++ b/tests/control_plane/test_cli_output_budget.py
@@ -547,6 +547,7 @@ def _mode_variant_commands(
registry_path: Path,
state_file: Path,
output_format: str,
+ complete_quota: bool = True,
) -> dict[str, list[str]]:
common = [
"--registry",
@@ -574,6 +575,7 @@ def _mode_variant_commands(
quota = _surface_commands(
project=project, runtime=runtime, registry_path=registry_path,
state_file=state_file, output_format=output_format,
+ complete_quota=complete_quota,
)["quota_should_run"]
return {
"start_goal_guided_command_pack_detail": [
@@ -923,8 +925,8 @@ def test_turn_envelope_references_selected_todo_without_duplicate_context(
assert exit_code == 0, text
payload = _json_payload(text)
- assert payload["action"]["selected_todo"]["text_ref"] == "action.recommended_action"
- work_context = payload["work_context"]
+ assert payload["selected_todo"]["text"]
+ work_context = payload["interaction_contract"]["agent_channel"]["work_context"]
assert work_context["selected_todo_ref"] == "selected_todo"
source = next(row for row in work_context["sources"] if row["source"] == "selected_todo")
authority = source["content"]["authority_read"]
@@ -983,11 +985,7 @@ def test_compact_selected_todo_markdown_preserves_work_context_instruction(
assert json_exit_code == 0, json_text
assert markdown_exit_code == 0, markdown_text
payload = _json_payload(json_text)
- work_context = (
- payload["interaction_contract"]["agent_channel"]["work_context"]
- if surface_id == "quota_should_run"
- else payload["work_context"]
- )
+ work_context = payload["interaction_contract"]["agent_channel"]["work_context"]
instruction = work_context["instruction"]
assert instruction
assert markdown_text.count(instruction) == 1
@@ -1042,7 +1040,7 @@ def test_quota_should_run_no_format_uses_machine_contract_json(
assert no_format_exit_code == 0, no_format_text
assert _json_payload(no_format_text)["goal_id"] == GOAL_ID
assert markdown_exit_code == 0, markdown_text
- assert markdown_text.startswith("# LoopX Quota Should Run")
+ assert markdown_text.startswith("# LoopX Turn Envelope")
assert not markdown_text.lstrip().startswith("{")
@@ -1089,6 +1087,7 @@ def test_quota_cli_keeps_full_agent_todo_diagnostics_on_explicit_cold_path(
registry_path=registry_path,
state_file=state_file,
output_format="json",
+ complete_quota=False,
)["quota_should_run"]
detail_command = _mode_variant_commands(
project=project,
@@ -1096,6 +1095,7 @@ def test_quota_cli_keeps_full_agent_todo_diagnostics_on_explicit_cold_path(
registry_path=registry_path,
state_file=state_file,
output_format="json",
+ complete_quota=False,
)["quota_should_run_todo_summary_detail"]
default_exit_code, default_text = _invoke_cli(default_command, read_context=True)
@@ -1193,6 +1193,7 @@ def test_quota_cli_keeps_complete_vision_rules_with_reachable_duplicate_refs(
registry_path=registry_path,
state_file=state_file,
output_format="json",
+ complete_quota=False,
)["quota_should_run"]
detail_command = _mode_variant_commands(
project=project,
@@ -1200,6 +1201,7 @@ def test_quota_cli_keeps_complete_vision_rules_with_reachable_duplicate_refs(
registry_path=registry_path,
state_file=state_file,
output_format="json",
+ complete_quota=False,
)["quota_should_run_vision_detail"]
default_exit_code, default_text = _invoke_cli(default_command, read_context=True)
@@ -1474,6 +1476,7 @@ def test_quota_cli_keeps_full_user_todo_diagnostics_on_explicit_cold_path(
registry_path=registry_path,
state_file=state_file,
output_format="json",
+ complete_quota=False,
)["quota_should_run"]
detail_command = _mode_variant_commands(
project=project,
@@ -1481,6 +1484,7 @@ def test_quota_cli_keeps_full_user_todo_diagnostics_on_explicit_cold_path(
registry_path=registry_path,
state_file=state_file,
output_format="json",
+ complete_quota=False,
)["quota_should_run_user_todo_summary_detail"]
default_exit_code, default_text = _invoke_cli(default_command, read_context=True)
@@ -1530,6 +1534,7 @@ def test_quota_cli_keeps_goal_boundary_authority_on_explicit_cold_path(
registry_path=registry_path,
state_file=state_file,
output_format="json",
+ complete_quota=False,
)["quota_should_run"]
detail_command = _mode_variant_commands(
project=project,
@@ -1537,6 +1542,7 @@ def test_quota_cli_keeps_goal_boundary_authority_on_explicit_cold_path(
registry_path=registry_path,
state_file=state_file,
output_format="json",
+ complete_quota=False,
)["quota_should_run_goal_boundary_detail"]
default_exit_code, default_text = _invoke_cli(default_command, read_context=True)
@@ -1596,6 +1602,7 @@ def test_quota_cli_deprecated_scheduler_detail_flag_matches_canonical_selector(
registry_path=registry_path,
state_file=state_file,
output_format="json",
+ complete_quota=False,
)["quota_should_run"]
canonical_exit_code, canonical_text = _invoke_cli(
default_command + ["--include-detail", "scheduler"],
@@ -1636,6 +1643,7 @@ def test_quota_cli_all_detail_matches_repeated_canonical_selectors(
registry_path=registry_path,
state_file=state_file,
output_format="json",
+ complete_quota=False,
)["quota_should_run"]
repeated_command = list(default_command)
for section in ("scheduler", "agent-todos", "user-todos", "goal-boundary"):
@@ -2262,16 +2270,21 @@ def test_turn_envelope_cli_preserves_codex_app_scheduler_binding(
assert exit_code == 0, text
payload = _json_payload(text)
- full_decision = shlex.split(payload["detail_ref"]["full_decision"])
- # A cold read must preserve the complete originating source and host profile.
- assert full_decision == [
- "loopx", "--registry", str(registry_path), "--runtime-root", str(runtime),
- "--format", "json", "quota", "should-run", "--goal-id", GOAL_ID,
- "--agent-id", AGENT_IDS[0], "--codex-app",
- ]
- read_rc, read_text = _invoke_cli(full_decision[1:], read_context=True)
+ assert shlex.split(payload["contract_binding"]["scheduler_execution_args"]) == ["--codex-app"]
+ assert payload["scheduler_hint"]["app_automation"]["host_surface"] == "codex_app"
+ # A fresh codex-app read without --turn-envelope preserves the same host profile.
+ read_rc, read_text = _invoke_cli(
+ [
+ "--registry", str(registry_path), "--runtime-root", str(runtime),
+ "--format", "json", "quota", "should-run", "--goal-id", GOAL_ID,
+ "--agent-id", AGENT_IDS[0], "--codex-app",
+ ],
+ read_context=True,
+ )
assert read_rc == 0, read_text
- assert _json_payload(read_text)["goal_id"] == GOAL_ID
+ read_payload = _json_payload(read_text)
+ assert read_payload["goal_id"] == GOAL_ID
+ assert shlex.split(read_payload["contract_binding"]["scheduler_execution_args"]) == ["--codex-app"]
def test_quota_should_run_cli_actions_keep_explicit_runtime_root(
diff --git a/tests/control_plane/test_cli_output_differential.py b/tests/control_plane/test_cli_output_differential.py
index d5cff98a04..8be8bb6f35 100644
--- a/tests/control_plane/test_cli_output_differential.py
+++ b/tests/control_plane/test_cli_output_differential.py
@@ -320,15 +320,14 @@ def test_over_budget_base_is_reported_before_candidate_failure_without_waiver(
row_id="surface/quota_should_run/crowded/json", surface_id="quota_should_run",
scenario="crowded", chars=chars, lines=1,
)
- probes, git_commands, workloads = [], [], []
+ probes, materialized, workloads = [], [], []
fixture_bytes = namespace["PROBE_TEST"].read_bytes()
- def git_transport(command, **_):
- git_commands.append(command)
- if "add" in command:
- base_test = Path(command[-2]) / namespace["PROBE_TEST"].relative_to(namespace["REPO_ROOT"])
- base_test.parent.mkdir(parents=True)
- base_test.write_text("# Synthetic historical fixture\n")
+ def materialize(base_ref, base_root):
+ materialized.append((base_ref, base_root))
+ base_test = base_root / namespace["PROBE_TEST"].relative_to(namespace["REPO_ROOT"])
+ base_test.parent.mkdir(parents=True)
+ base_test.write_text("# Synthetic historical fixture\n")
def probe_transport(**kwargs):
probes.append(kwargs["enforce_budget"])
@@ -343,7 +342,7 @@ def probe_transport(**kwargs):
measurement={"chars": chars, "lines": 1},
)
- monkeypatch.setitem(namespace, "_run", git_transport)
+ monkeypatch.setitem(namespace, "_materialize_base_source", materialize)
monkeypatch.setitem(namespace, "_run_probe", probe_transport)
monkeypatch.setitem(namespace, "_is_ancestor", lambda *_: True)
monkeypatch.setitem(namespace, "_pin_probe_base_ref", lambda ref, **_: ref)
@@ -354,7 +353,8 @@ def probe_transport(**kwargs):
assert workloads[0][0] == workloads[1][0]
assert all(body == fixture_bytes for _, body, _ in workloads)
assert workloads[0][2] != workloads[1][2]
- assert "remove" in git_commands[-1]
+ assert materialized == [("synthetic-base", workloads[0][2])]
+ assert not workloads[0][2].exists()
output = capsys.readouterr().out
prefix = "cli-output-base-current-ceilings "
assert output.startswith(prefix)
@@ -709,12 +709,18 @@ def git(*args):
probe_test = repo / "tests" / "probe.py"
probe_test.parent.mkdir()
probe_test.write_text("# Public synthetic probe\n", encoding="utf-8")
- git("add", "packet.json", "tests/probe.py")
+ base_package = repo / "loopx" / "__init__.py"
+ base_package.parent.mkdir()
+ base_package.write_bytes(b"# Fixture package\n")
+ attributes = repo / ".gitattributes"
+ attributes.write_bytes(b"*.py text eol=lf\n")
+ git("add", ".gitattributes", "packet.json", "tests/probe.py", "loopx/__init__.py")
git("commit", "-m", "common baseline")
common = git("rev-parse", "HEAD")
git("checkout", "-b", "candidate")
+ attributes.write_bytes(b"*.py text eol=crlf\n")
packet.write_text(json.dumps({**common_payload, "note": "x" * (4000 + growth)}), encoding="utf-8")
- git("add", "packet.json")
+ git("add", ".gitattributes", "packet.json")
git("commit", "--allow-empty", "-m", "candidate")
candidate = git("rev-parse", "HEAD")
git("checkout", "main")
@@ -725,19 +731,28 @@ def git(*args):
upstream = git("rev-parse", "HEAD")
git("update-ref", "refs/remotes/origin/main", upstream)
git("checkout", "candidate")
+ (repo / "staged.txt").write_text("Uncommitted fixture change\n", encoding="utf-8")
+ git("add", "staged.txt")
+ original_index = git("ls-files", "--stage")
probes = []
pinned = []
- run = namespace["_run"]
+ materialize = namespace["_materialize_base_source"]
- def git_transport(command, **kwargs):
- if "worktree" in command and "add" in command:
- pinned.append(command[-1])
- # Advance the ref again after selection but before checkout.
- git("update-ref", "refs/remotes/origin/main", candidate)
- run(command, **kwargs)
+ def observe_materialization(base_ref, base_root):
+ pinned.append(base_ref)
+ # Advance the ref again after selection but before materialization.
+ git("update-ref", "refs/remotes/origin/main", candidate)
+ materialize(base_ref, base_root)
def probe_transport(**kwargs):
- text = (kwargs["source_root"] / "packet.json").read_text(encoding="utf-8")
+ source_root = kwargs["source_root"]
+ if not kwargs["enforce_budget"]:
+ assert (source_root / "loopx" / "__init__.py").read_bytes() == b"# Fixture package\n"
+ assert not (source_root / "tests").exists()
+ assert not (source_root / "staged.txt").exists()
+ assert not (source_root / ".git").exists()
+ assert git("worktree", "list", "--porcelain").count("worktree ") == 1
+ text = (source_root / "packet.json").read_text(encoding="utf-8")
measured = measure_cli_output(text, output_format="json")
row = _row(
row_id="surface/quota_should_run/crowded/json",
@@ -756,7 +771,7 @@ def probe_transport(**kwargs):
monkeypatch.setitem(namespace, "REPO_ROOT", repo)
monkeypatch.setitem(namespace, "PROBE_TEST", probe_test)
- monkeypatch.setitem(namespace, "_run", git_transport)
+ monkeypatch.setitem(namespace, "_materialize_base_source", observe_materialization)
monkeypatch.setitem(namespace, "_run_probe", probe_transport)
# An explicit historical commit remains authoritative even off this branch.
args = ["probe", "--base-ref", upstream] if explicit_base else ["probe"]
@@ -772,6 +787,7 @@ def probe_transport(**kwargs):
assert pinned == [upstream if explicit_base else common]
assert git("worktree", "list", "--porcelain").count("worktree ") == 1
assert git("rev-parse", "HEAD") == candidate
+ assert git("ls-files", "--stage") == original_index
assert git("show", f"{common}:packet.json") == json.dumps(common_payload)
@@ -791,6 +807,30 @@ def test_probe_rejects_an_unresolvable_base(tmp_path, monkeypatch, base_ref):
pin(base_ref, main_ref="origin/main")
+def test_probe_rejects_a_commit_without_its_own_cli_source(tmp_path, monkeypatch):
+ import os
+ import runpy
+ import subprocess
+ from pathlib import Path
+
+ materialize = runpy.run_path(str(
+ Path(__file__).resolve().parents[2]
+ / "examples/control_plane/cli-output-base-head-differential-smoke.py"
+ ))["_materialize_base_source"]
+ repo = tmp_path / "repo"
+ subprocess.run(["git", "init", str(repo)], check=True, capture_output=True)
+ subprocess.run([
+ "git", "-c", "user.name=Fixture", "-c", "user.email=fixture@example.invalid",
+ "-c", f"core.hooksPath={os.devnull}", "-c", "commit.gpgsign=false",
+ "commit", "--allow-empty", "-m", "Commit before the CLI existed",
+ ], cwd=repo, check=True, capture_output=True)
+ monkeypatch.setitem(materialize.__globals__, "REPO_ROOT", repo)
+ source_root = tmp_path / "base"
+ with pytest.raises(ValueError, match="has no LoopX Python source"):
+ materialize("HEAD", source_root)
+ assert not source_root.exists()
+
+
@pytest.mark.parametrize("row_kind", ["surface", "variant"])
@pytest.mark.parametrize("mode", ["thin", "brief", "compact"])
def test_host_safety_restoration_budget_is_one_time_bounded_and_prompt_only(
diff --git a/tests/control_plane/test_cli_source_activation_readback.py b/tests/control_plane/test_cli_source_activation_readback.py
index 8ba75f1650..0e487b2ccd 100644
--- a/tests/control_plane/test_cli_source_activation_readback.py
+++ b/tests/control_plane/test_cli_source_activation_readback.py
@@ -49,7 +49,7 @@ def decision():
assert decision()["source_change_allowed"] is False
assert _spend_run_count(runtime) == 0
plan = packet["interaction_contract"]["cli_channel"]["settlement_plan"]
- writeback = plan["ordered_steps"][1]["command_template"]
+ writeback = plan["ordered_steps"][1]["command_template"].replace(" --settle-turn --settlement-receipt", "")
for old, new in (("", "verified_exclusion"), ("", "single_surface"),
("", "outcome_progress")):
writeback = writeback.replace(old, new)
diff --git a/tests/control_plane/test_context_capture_registry_independence.py b/tests/control_plane/test_context_capture_registry_independence.py
index afd04c2d54..e516ae47f3 100644
--- a/tests/control_plane/test_context_capture_registry_independence.py
+++ b/tests/control_plane/test_context_capture_registry_independence.py
@@ -209,6 +209,9 @@ def test_replan_stages_read_original_authority_capture_without_registry(
assert page["next_offset"] == len(observed)
command = page["next_command"]
guard = json.loads(observed)
+ from loopx.control_plane.quota.turn_envelope import _turn_envelope_result
+ assert "review" not in guard["replan_action_packet"]
+ staged = _turn_envelope_result("replan_cli_view", payload=source)["replan_action_packet"]
missing = tmp_path / "unrelated-missing-registry.json"
def snapshot():
@@ -228,7 +231,7 @@ def read(command):
assert value["obligation_id"] == source["replan_action_packet"]["obligation_id"]
return value
- review = read(guard["replan_action_packet"]["review"])
+ review = read(staged["review"])
writer = read(review["next"]["writeback"])
assert writer["context_id"] == review["context_id"]
assert writer["source_hash"] == review["source_hash"]
@@ -237,7 +240,7 @@ def read(command):
assert len(index["outcomes"]) == 1
acceptance = read(index["read_action_template"].replace("{outcome_ref}", index["outcomes"][0]["outcome_ref"]))
assert acceptance["outcome"]["requirements"][0]["description"] == "Stop before publication."
- explore = read(guard["replan_action_packet"]["review"].replace("--replan-stage review", "--replan-stage explore"))
+ explore = read(staged["review"].replace("--replan-stage review", "--replan-stage explore"))
assert explore["explore_basis"] == source["autonomous_replan_obligation"]["replan_context"].get("explore_basis")
assert snapshot() == before, "captured readers cannot admit, settle, repair routes or rewrite authority"
assert not missing.exists()
diff --git a/tests/control_plane/test_context_delivery_cli.py b/tests/control_plane/test_context_delivery_cli.py
index 9ec8a50ee9..3f0b48f21e 100644
--- a/tests/control_plane/test_context_delivery_cli.py
+++ b/tests/control_plane/test_context_delivery_cli.py
@@ -239,14 +239,24 @@ def test_resumed_task_keeps_its_condition_without_expanding_diagnostic_siblings(
"--goal-id", GOAL_ID, "--agent-id", AGENT_ID, "--todo-id", TODO_ID,
"--turn-instance-id", "mixed-diagnostics", cwd=project)
assert code == 0 and guard["should_run"], guard
+ assert guard["schema_version"] == "loopx_turn_envelope_v1"
summary = guard["agent_todo_summary"]
- assert [item["todo_id"] for item in summary["backlog_items"]] == ["todo_work_1"]
- condition = summary["backlog_items"][0]["resume_condition"]
+ # The complete writer retains the original bounded source rows. It no
+ # longer emits the legacy display projection as its canonical decision.
+ assert [item["todo_id"] for item in summary["backlog_items"]] == [TODO_ID, "todo_work_1"]
+ condition = summary["backlog_items"][1]["resume_condition"]
assert condition["satisfied"] and condition["target_todo_id"] == "todo_dependency"
assert len(summary["first_executable_items"]) == 3
candidates = guard["capability_gate"]["runnable_candidates"]
assert {row["todo_id"] for row in candidates} == {row["todo_id"] for row in todos[:8]}
assert all(row["title"].endswith("Final condition: stop before publication.") for row in candidates)
+ # The shared historical display owner still isolates the exceptional row;
+ # no resumed row expands known diagnostic siblings in that projection.
+ from loopx.control_plane.quota.cli_projection import compact_quota_should_run_cli_payload
+ displayed = compact_quota_should_run_cli_payload(deepcopy(guard))["agent_todo_summary"]
+ assert [item["todo_id"] for item in displayed["backlog_items"]] == ["todo_work_1"]
+ assert displayed["backlog_items"][0]["resume_condition"] == condition
+ assert len(displayed["first_executable_items"]) == 3
assert state.read_bytes() == before and _spend_run_count(runtime) == 0
code, detailed = _run_cli(registry, runtime, "todo", "list", "--goal-id", GOAL_ID,
"--todo-id", "todo_work_7", cwd=project)
@@ -648,16 +658,16 @@ def test_native_projection_never_layers_host_projection(tmp_path, monkeypatch, n
from hashlib import sha256
from loopx.cli import build_parser
- from loopx.cli_commands import quota, quota_context_reader
+ from loopx.cli_commands import quota, quota_context_reader, quota_result_delivery
from loopx.control_plane.turn_driver import codex_context
args = build_parser().parse_args(["--format", "json", "quota", "should-run",
"--goal-id", "fixture", "--agent-id", "worker", "--context-delivery", "on", "--codex-app", "--codex-context-dir", str(tmp_path)])
original = {"ok": True, "goal_id": "fixture", "agent_identity": {"agent_id": "worker"},
"heartbeat_receipt": {"turn_instance_id": "original-turn", "status": "committed"}}
- monkeypatch.setattr(quota, "handoff_decision",
+ monkeypatch.setattr(quota_result_delivery, "handoff_decision",
lambda *args, capture_sha256: ({"scope": "host-request"}, capture_sha256))
- monkeypatch.setattr(quota, "_project_quota_cli_payload", lambda payload, *args, **kwargs: payload)
+ monkeypatch.setattr(quota_result_delivery, "_project_quota_cli_payload", lambda payload, *args, **kwargs: payload)
def competing(*args, **kwargs):
pytest.fail("explicit native observation must not compose two projection owners")
@@ -676,7 +686,7 @@ def unavailable_directory(**kwargs):
def unavailable_reference(*args, **kwargs):
raise OSError("synthetic reader metadata unavailable")
- monkeypatch.setattr(quota, "project_captured_context", competing)
+ monkeypatch.setattr(quota_result_delivery, "project_captured_context", competing)
monkeypatch.setattr(codex_context, "prepare_codex_context", native)
if native_failure == "directory":
monkeypatch.setattr(codex_context, "codex_context_directory", unavailable_directory)
diff --git a/tests/control_plane/test_context_first_read.py b/tests/control_plane/test_context_first_read.py
index 77c90bee54..5bc6202bc9 100644
--- a/tests/control_plane/test_context_first_read.py
+++ b/tests/control_plane/test_context_first_read.py
@@ -661,11 +661,13 @@ def test_cli_current_goal_and_action_precede_complete_scoped_preferences(tmp_pat
@pytest.mark.parametrize("warm", [False, True])
@pytest.mark.parametrize("provider", ["file", "sqlite"])
-@pytest.mark.parametrize(("scope_count", "page_budget", "checkpoint_count"), [
- (165, 12000, 0), (290, 12000, 0), (165, 16000, 0), (290, 12000, 30),
+@pytest.mark.parametrize(("scope_count", "page_budget", "checkpoint_count", "requested_count"), [
+ (165, 12000, 0, 0), (290, 12000, 0, 0), (165, 16000, 0, 0), (290, 12000, 30, 0),
+ pytest.param(290, 12000, 0, 355, id="large-goal-and-todo-scopes"),
+ pytest.param(290, 12000, 30, 355, id="large-scopes-with-approvals"),
])
def test_ready_opening_keeps_controls_readable_with_a_large_write_scope(
- tmp_path, monkeypatch, provider, warm, scope_count, page_budget, checkpoint_count,
+ tmp_path, monkeypatch, provider, warm, scope_count, page_budget, checkpoint_count, requested_count,
):
from test_codex_context_lifecycle import deliver, setup
from test_quota_settlement_cli import (
@@ -683,6 +685,14 @@ def test_ready_opening_keeps_controls_readable_with_a_large_write_scope(
state.write_text(state.read_text().replace("## Next Action", narrative + "\n\n## Next Action", 1))
configuration = json.loads(registry.read_text())
scopes = [f"docs/areas/{index:03d}/fixture-entrypoints/public/**" for index in range(scope_count)]
+ requested_scopes = [
+ f"{scopes[index % scope_count][:-2]}section-{index:03d}/**" for index in range(requested_count)
+ ]
+ if requested_scopes:
+ state.write_text(state.read_text().replace(
+ "required_capabilities=shell%2Cfilesystem_read -->",
+ f"required_capabilities=shell%2Cfilesystem_read required_write_scopes={','.join(requested_scopes)} -->",
+ ))
if scope_count == 165:
assert len(json.dumps(scopes, separators=(",", ":")).encode()) < 8192
coordination = configuration["goals"][0]["coordination"]
@@ -748,26 +758,38 @@ def read(command):
if warm:
prior = capture("wide-prior")
_, prior_ref = read_context_read_reference(prior["recovery"]["reference_file"])
- rows = json.loads(Path(prior_ref["path"]).read_bytes())["interaction_contract"]["agent_channel"]["work_context"]["sources"]
+ prior_decision = json.loads(Path(prior_ref["path"]).read_bytes())
+ rows = prior_decision["interaction_contract"]["agent_channel"]["work_context"]["sources"]
index = next(i for i, source in enumerate(rows) if source["source"] == "goal_state")
body_pointer = f"/interaction_contract/agent_channel/work_context/sources/{index}/content/text"
- prior_pages, prior_facts = read(prior["recovery"]["command"] + " --select " + body_pointer)
+ prior_read = prior["recovery"]["command"] + " --select " + body_pointer
+ if requested_scopes:
+ prior_read += " --select /goal_boundary/write_scope"
+ prior_pages, prior_facts = read(prior_read)
assert narrative in prior_facts[body_pointer]
+ if requested_scopes:
+ assert prior_facts["/goal_boundary/write_scope"] == scopes
deliver(journal, event, prior_pages)
+ proof = prior_decision["execution_preparation"]["lease_proof"]
cli("refresh-state", *binding, "--turn-instance-id", "wide-prior", "--classification", "fixture_verified",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
+ "--task-lease-idempotency-key", proof["idempotency_key"],
+ "--task-lease-expected-version", str(proof["expected_version"]),
"--delivery-boundary", "in_flight_continuation", "--no-global-sync", "--suppress-external-sinks")
cli("quota", "spend-slot", *binding, "--turn-instance-id", "wide-prior",
"--slots", "1", "--source", "heartbeat", "--execute")
- scopes = [*scopes[:-1], "docs/areas/current-fixture-scope/**"]
- configuration["goals"][0]["coordination"]["write_scope"] = scopes
- registry.write_text(json.dumps(configuration))
+ if not requested_scopes:
+ scopes = [*scopes[:-1], "docs/areas/current-fixture-scope/**"]
+ configuration["goals"][0]["coordination"]["write_scope"] = scopes
+ registry.write_text(json.dumps(configuration))
view = capture("wide-ready")
_, source_ref = read_context_read_reference(view["recovery"]["reference_file"])
path = Path(source_ref["path"])
raw = path.read_bytes()
source = json.loads(raw)
+ if requested_scopes:
+ assert source["selected_todo"]["required_write_scopes"] == requested_scopes
pages, opening = read(view["first_read_command"])
if page_budget == 16000:
# A paged body is not itself a reason to add a second reader. The
@@ -793,7 +815,12 @@ def read(command):
assert opening["/interaction_contract/cli_channel/execution_preparation/state"] == "ready"
assert opening["/goal_boundary/requires_parent_approval"] == ["publish"]
assert opening["/goal_boundary/stop_condition"] == source["goal_boundary"]["stop_condition"]
+ required_reads = "/interaction_contract/agent_channel/required_reads"
+ assert opening[required_reads] == source["interaction_contract"]["agent_channel"]["required_reads"]
assert "/goal_boundary/write_scope" not in opening
+ if requested_scopes:
+ assert "/selected_todo/required_write_scopes" not in opening
+ assert opening["/selected_todo/text"] == source["selected_todo"]["text"]
authority = "/goal_boundary/checkpointed_boundary_authority"
if checkpoint_count:
assert opening[authority + "/active_count"] == checkpoint_count // 2
@@ -804,7 +831,11 @@ def read(command):
deliver(journal, event, pages)
pages, remaining = read(view["remaining_read_command"])
- assert len(pages) > 1 and remaining["/goal_boundary/write_scope"] == scopes
+ assert len(pages) > 1
+ if requested_scopes:
+ assert remaining["/selected_todo/required_write_scopes"] == requested_scopes
+ if not (warm and requested_scopes):
+ assert remaining["/goal_boundary/write_scope"] == scopes
if checkpoint_count:
entries = source["goal_boundary"]["checkpointed_boundary_authority"]["entries"]
assert len(entries) == checkpoint_count
@@ -818,9 +849,14 @@ def leaves(values):
if not isinstance(value, (dict, list)) or not value}
assert not opening.keys() & remaining.keys()
if warm:
- assert body_pointer in pages[0]["previously_delivered_fields"] and body_pointer not in remaining
- assert "/goal_boundary/write_scope" not in pages[0]["previously_delivered_fields"]
- assert leaves(opening | remaining) == {key: value for key, value in leaves(ordinary).items() if key != body_pointer}
+ expected_reuse = {body_pointer}
+ if requested_scopes:
+ expected_reuse.add("/goal_boundary/write_scope")
+ assert set(pages[0]["previously_delivered_fields"]) == expected_reuse
+ assert leaves(opening | remaining) == {
+ key: value for key, value in leaves(ordinary).items()
+ if not any(key == reused or key.startswith(reused + "/") for reused in expected_reuse)
+ }
deliver(journal, event, pages)
assert "/goal_boundary/write_scope" in codex_context._state(journal)["consumed_pointers"]
codex_context.codex_context_hook(str(journal), {**event, "hook_event_name": "PostCompact"})
@@ -828,6 +864,7 @@ def leaves(values):
assert "previously_delivered_fields" not in recovery_pages[0]
assert not opening.keys() & restored.keys()
assert leaves(opening | restored) == leaves(ordinary)
+ assert restored["/goal_boundary/write_scope"] == scopes
else:
assert leaves(opening | remaining) == leaves(ordinary)
assert path.read_bytes() == raw and hashlib.sha256(raw).hexdigest() == source_ref["sha256"]
@@ -2064,9 +2101,13 @@ def capture(turn, registry_path=registry):
registry_path=registry_path)
def settle(turn, registry_path=registry):
+ lease = cli("task-lease", "inspect", "--goal-id", GOAL_ID, "--todo-id", TODO_ID,
+ registry_path=registry_path)["lease"]
cli("refresh-state", *binding(turn), "--classification", "fixture_validated",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
"--delivery-boundary", "in_flight_continuation", "--delivery-workspace-path", str(project),
+ "--task-lease-idempotency-key", lease["idempotency_key"],
+ "--task-lease-expected-version", str(lease["version"]),
"--no-global-sync", "--suppress-external-sinks", registry_path=registry_path)
spent = cli("quota", "spend-slot", *binding(turn), "--slots", "1", "--source", "heartbeat", "--execute",
registry_path=registry_path)
@@ -2228,9 +2269,12 @@ def capture(turn, todo_id=TODO_ID):
"--decision-output-root", str(captures), "--codex-context-dir", str(journal))
def settle(turn):
+ lease = cli("task-lease", "inspect", "--goal-id", GOAL_ID, "--todo-id", TODO_ID)["lease"]
cli("refresh-state", *binding(turn), "--classification", "fixture_validated",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
"--delivery-boundary", "in_flight_continuation", "--delivery-workspace-path", str(project),
+ "--task-lease-idempotency-key", lease["idempotency_key"],
+ "--task-lease-expected-version", str(lease["version"]),
"--no-global-sync", "--suppress-external-sinks")
spent = cli("quota", "spend-slot", *binding(turn), "--slots", "1", "--source", "heartbeat", "--execute")
assert spent["appended"]
diff --git a/tests/control_plane/test_declared_terminal_settlement_cli.py b/tests/control_plane/test_declared_terminal_settlement_cli.py
index f262d12ffa..5341282176 100644
--- a/tests/control_plane/test_declared_terminal_settlement_cli.py
+++ b/tests/control_plane/test_declared_terminal_settlement_cli.py
@@ -130,7 +130,9 @@ def test_declared_leased_completion_writeback_spend_terminal_and_replay(
completion_command = recovery["ordered_steps"][0]["command_template"]
assert "--project" in recovery["ordered_steps"][1]["command_template"]
assert "--state-file" in recovery["ordered_steps"][1]["command_template"]
- assert "--project" not in recovery["ordered_steps"][2]["command_template"]
+ assert "--settle-turn" in recovery["ordered_steps"][1]["command_template"]
+ assert recovery["ordered_steps"][2]["owner"] == "runtime"
+ assert "command_template" not in recovery["ordered_steps"][2]
assert "--no-follow-up" not in shlex.split(completion_command)
assert completion_command in render_todo_markdown(premature)
completion_command = completion_command.replace("", complete_args[-1])
diff --git a/tests/control_plane/test_deferred_resume_generated_command.py b/tests/control_plane/test_deferred_resume_generated_command.py
index f90dd19195..9dc94651e7 100644
--- a/tests/control_plane/test_deferred_resume_generated_command.py
+++ b/tests/control_plane/test_deferred_resume_generated_command.py
@@ -275,6 +275,7 @@ def cli(*args: str) -> tuple[int, dict]:
rc, reentry = cli(*reentry_args)
assert rc == 0 and reentry["normal_delivery_allowed"], reentry
+ lease_proof = reentry["execution_preparation"]["lease_proof"]
assert (
reentry["heartbeat_receipt"]["settlement_identity"]
== guard["heartbeat_receipt"]["settlement_identity"]
@@ -328,6 +329,8 @@ def cli(*args: str) -> tuple[int, dict]:
"--todo-id", TODO_ID, "--turn-instance-id", "resume-command-turn",
"--classification", "resumed_work_verified", "--delivery-batch-scale", "single_surface",
"--delivery-outcome", "outcome_progress", "--agent-vision-json", str(vision),
+ "--task-lease-idempotency-key", lease_proof["idempotency_key"],
+ "--task-lease-expected-version", str(lease_proof["expected_version"]),
"--no-global-sync", "--suppress-external-sinks", "--settle-turn")
for _ in range(2):
rc, settled = cli(*settlement)
diff --git a/tests/control_plane/test_dependency_dispatch_cli.py b/tests/control_plane/test_dependency_dispatch_cli.py
index 5c69ccfc91..3080170341 100644
--- a/tests/control_plane/test_dependency_dispatch_cli.py
+++ b/tests/control_plane/test_dependency_dispatch_cli.py
@@ -152,7 +152,9 @@ def test_local_wait_and_user_gates_preserve_only_verified_independent_work(publi
host.run()
assert not host.counts.exists()
- update(goal, gate, "--unblocks-todo-id", waiting)
+ second_gated = add(goal, "Validate another gated artifact after owner approval.",
+ "--task-class", "advancement_task", "--action-kind", "validate", "--status", "blocked")
+ update(goal, gate, "--unblocks-todo-id", second_gated)
fresh = guard(goal)
assert fresh["should_run"] is True and fresh["requires_user_action"] is True
assert fresh["selected_todo"]["todo_id"] == independent
diff --git a/tests/control_plane/test_doubao_onboarding_model_behavior_actor.py b/tests/control_plane/test_doubao_onboarding_model_behavior_actor.py
index b838d5c5bd..340da5268a 100644
--- a/tests/control_plane/test_doubao_onboarding_model_behavior_actor.py
+++ b/tests/control_plane/test_doubao_onboarding_model_behavior_actor.py
@@ -134,6 +134,13 @@ def test_onboarding_reason_code_instruction_matches_the_strict_result_boundary(p
assert "including when the observation is healthy" in instruction
+def test_onboarding_entry_instruction_separates_requested_and_activation_host_surface():
+ instruction = _onboarding_decision_instruction("entry")
+ assert "copy ONLY the packet's top-level host_surface" in instruction
+ assert "never from the packet's top-level host_surface" in instruction
+ assert "return null when omitted or" in instruction
+
+
@pytest.mark.parametrize(
"reasons", [[], ["not a token"], [f"reason_{index}" for index in range(13)]],
)
diff --git a/tests/control_plane/test_effect_runtime_host_permission.py b/tests/control_plane/test_effect_runtime_host_permission.py
index 26c333d4c4..c9ca7090a8 100644
--- a/tests/control_plane/test_effect_runtime_host_permission.py
+++ b/tests/control_plane/test_effect_runtime_host_permission.py
@@ -184,7 +184,10 @@ def denied(*_args, **_kwargs):
assert payload["ok"] is False and payload["should_run"] is False
assert payload["error_code"] == "quota_runtime_permission_denied"
else:
- assert "- ok: `False`" in output
+ assert "# LoopX Turn Envelope" in output
+ assert '"ok":false' in output
+ assert '"should_run":false' in output
+ assert '"error_code":"quota_runtime_permission_denied"' in output
assert registry.read_text() == "{}"
diff --git a/tests/control_plane/test_effect_runtime_request_scope.py b/tests/control_plane/test_effect_runtime_request_scope.py
index c31f4f3edd..ced59136f4 100644
--- a/tests/control_plane/test_effect_runtime_request_scope.py
+++ b/tests/control_plane/test_effect_runtime_request_scope.py
@@ -232,6 +232,7 @@ def run_command(
*,
registry_path: Path,
allow_missing_registry: bool,
+ registry_was_configured: bool | None = None,
) -> int:
assert registry_path == Path("registry.json")
assert allow_missing_registry is False
diff --git a/tests/control_plane/test_effective_turn_replan.py b/tests/control_plane/test_effective_turn_replan.py
index 03fba2571b..4422758afc 100644
--- a/tests/control_plane/test_effective_turn_replan.py
+++ b/tests/control_plane/test_effective_turn_replan.py
@@ -339,8 +339,9 @@ def periodic():
)
assert rc == 0, envelope
assert envelope["replan_action_packet"]["planning_guidance"] == selected["replan_action_packet"]["planning_guidance"]
- assert envelope["replan_action_packet"]["writeback_contract"]["vision_authoring"] == authoring
- assert envelope["writeback"]["settlement_plan"] == plan
+ env_contract = envelope["replan_action_packet"]["writeback_contract"]
+ assert env_contract["vision_authoring_ref"] == contract["vision_authoring_ref"]
+ assert envelope["interaction_contract"]["cli_channel"]["settlement_plan"] == plan
vision_path = tmp_path / "periodic-vision.json"
vision = json.loads(json.dumps(authoring["minimal_example"]))
vision["vision_patch"] = {"acceptance_summary": "Original acceptance remains open."}
diff --git a/tests/control_plane/test_explore_capture_identity.py b/tests/control_plane/test_explore_capture_identity.py
index 3371f1c5d5..a2a88c6a02 100644
--- a/tests/control_plane/test_explore_capture_identity.py
+++ b/tests/control_plane/test_explore_capture_identity.py
@@ -35,6 +35,7 @@ def test_first_capture_allocates_identity_and_recovers_without_rewriting_scope(
rc, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app",
*binding, cwd=project)
assert rc == 0, guard
+ proof = guard["execution_preparation"]["lease_proof"]
attachment = deepcopy(ATTACHMENT)
del attachment["node_id"]
packet = {"schema_version": "goal_vision_replan_contract_v0",
@@ -52,6 +53,8 @@ def test_first_capture_allocates_identity_and_recovers_without_rewriting_scope(
vision = tmp_path / "vision.json"
args = ("refresh-state", *binding, "--classification", "validated_change",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
+ "--task-lease-idempotency-key", proof["idempotency_key"],
+ "--task-lease-expected-version", str(proof["expected_version"]),
"--no-global-sync", "--suppress-external-sinks", "--agent-vision-json", str(vision))
log = explore_result_log_path(runtime, GOAL_ID)
if conflict:
diff --git a/tests/control_plane/test_explore_durable_links.py b/tests/control_plane/test_explore_durable_links.py
index 2a32c18a3b..17ee50f6c7 100644
--- a/tests/control_plane/test_explore_durable_links.py
+++ b/tests/control_plane/test_explore_durable_links.py
@@ -51,12 +51,15 @@ def test_ninth_capture_keeps_history_and_survives_real_cli_replay(tmp_path, prov
rc, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app",
*binding, cwd=project)
assert rc == 0, guard
+ proof = guard["execution_preparation"]["lease_proof"]
vision = tmp_path / "vision.json"
vision.write_text(json.dumps({"schema_version": "goal_vision_replan_contract_v0",
"vision_patch": {"acceptance_summary": "Require a uniform tail bound."},
"explore_result": deepcopy(ATTACHMENT)}))
command = ("refresh-state", *binding, "--classification", "validated_change",
"--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
+ "--task-lease-idempotency-key", proof["idempotency_key"],
+ "--task-lease-expected-version", str(proof["expected_version"]),
"--no-global-sync", "--suppress-external-sinks", "--agent-vision-json", str(vision))
rc, captured = _run_cli(registry, runtime, *command, cwd=project)
assert rc == 0 and captured["explore_result_delivery"]["ok"], captured
diff --git a/tests/control_plane/test_explore_result_writeback.py b/tests/control_plane/test_explore_result_writeback.py
index 7670e037d8..aa32ac690a 100644
--- a/tests/control_plane/test_explore_result_writeback.py
+++ b/tests/control_plane/test_explore_result_writeback.py
@@ -374,8 +374,6 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r
cwd=project,
)
assert rc == 0, claimed
- if terminal:
- from loopx.todos import complete_goal_todo
binding = (
"--goal-id",
@@ -398,14 +396,21 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r
)
assert rc == 0, guard
if terminal:
- complete_goal_todo(
- registry_path=path,
- goal_id=GOAL_ID,
- todo_id=TODO_ID,
- agent_id=AGENT_ID,
- evidence="Synthetic validation passed",
- **terminal_proof,
+ rc, completed = _run_cli(
+ path,
+ runtime,
+ "todo",
+ "complete",
+ *binding,
+ "--evidence",
+ "Synthetic validation passed",
+ "--task-lease-idempotency-key",
+ terminal_proof["task_lease_idempotency_key"],
+ "--task-lease-expected-version",
+ str(terminal_proof["task_lease_expected_version"]),
+ cwd=project,
)
+ assert rc == 0, completed
source = tmp_path / "result.json"
plan = guard["interaction_contract"]["cli_channel"]["settlement_plan"]
template = plan["ordered_steps"][1]["optional_attachments"][0]["attachment_template"]
@@ -452,6 +457,12 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r
attachment_args += ("--explore-result-json", str(source))
if source_mode in {"vision", "both", "path_delta", "wide_path_delta", "linked_path_delta"}:
attachment_args += ("--agent-vision-json", str(vision))
+ proof = guard.get("execution_preparation", {}).get("lease_proof")
+ lease_args = (
+ ("--task-lease-idempotency-key", proof["idempotency_key"],
+ "--task-lease-expected-version", str(proof["expected_version"]))
+ if proof else ()
+ )
args = (
"refresh-state",
*binding,
@@ -461,6 +472,7 @@ def test_real_cli_normal_writeback_persists_attachment_and_rejects_conflicting_r
"implementation",
"--delivery-outcome",
"outcome_progress",
+ *lease_args,
"--no-global-sync",
"--suppress-external-sinks",
*attachment_args,
@@ -884,10 +896,30 @@ def run(*command):
assert not log.exists()
assert list_goal_todos(registry_path=path, goal_id=GOAL_ID)["todos"] == todos
- # Supported recovery: fresh admission and an active lease, then the same capture.
- run(*guard_command)
- current = run("task-lease", "acquire", *lease_binding, "--idempotency-key", "recovery")["lease"]
- captured = run(*refresh)
+ # Supported recovery after retiring the original generation: supersede onto a
+ # fresh Todo/Turn with an active lease, then commit and test replay after release.
+ reclaimed = run("todo", "claim", "--goal-id", GOAL_ID, "--todo-id", TODO_ID,
+ "--agent-id", AGENT_ID, "--claimed-by", AGENT_ID)
+ superseded = run("todo", "supersede", "--goal-id", GOAL_ID, "--todo-id", TODO_ID,
+ "--agent-id", AGENT_ID, "--claim-operation-id", reclaimed["claim_operation_id"],
+ "--reason", "Original lease was released before writeback",
+ "--next-agent-todo", "[P1] Continue tail bound check",
+ "--next-claimed-by", AGENT_ID)
+ recovery_todo_id = superseded["successor_todo_ids"][0]
+ recovery_lease_binding = ["--goal-id", GOAL_ID, "--todo-id", recovery_todo_id, "--owner", AGENT_ID]
+ current = run("task-lease", "acquire", *recovery_lease_binding, "--idempotency-key", "recovery")["lease"]
+ recovery_binding = ["--goal-id", GOAL_ID, "--agent-id", AGENT_ID,
+ "--todo-id", recovery_todo_id, "--turn-instance-id", f"{TURN_ID}-recovery"]
+ recovery_guard = run("quota", "should-run", "--codex-app", *recovery_binding)
+ proof = recovery_guard["execution_preparation"]["lease_proof"]
+ recovery_refresh = [
+ "refresh-state", *recovery_binding, "--classification", "validated_change",
+ "--delivery-batch-scale", "implementation", "--delivery-outcome", "outcome_progress",
+ "--task-lease-idempotency-key", proof["idempotency_key"],
+ "--task-lease-expected-version", str(proof["expected_version"]),
+ "--agent-vision-json", str(vision), "--no-global-sync", "--suppress-external-sinks",
+ ]
+ captured = run(*recovery_refresh)
assert captured["appended"] and captured["explore_result_delivery"]["ok"]
display_input = captured["explore_result_delivery"]["hook"]["intents"][0]["payload"]
assert display_input == {"attachment_ref": "$.explore_result"}
@@ -895,15 +927,15 @@ def run(*command):
assert len(load_explore_result_events(log)) == 2
committed_index, committed_log = index.read_bytes(), log.read_bytes()
rows = list_goal_todos(registry_path=path, goal_id=GOAL_ID)["todos"]
- target = next(row for row in rows if row["todo_id"] == TODO_ID)
+ target = next(row for row in rows if row["todo_id"] == recovery_todo_id)
assert target["status"] == "open" and target["claimed_by"] == AGENT_ID
assert target["explore_result_node_refs"] == [ATTACHMENT["node_id"]]
- run("task-lease", "release", *lease_binding, "--idempotency-key", "recovery",
+ run("task-lease", "release", *recovery_lease_binding, "--idempotency-key", "recovery",
"--expected-version", str(current["version"]))
leases = read_canonical_todos_if_promoted(
runtime_root=runtime, goal_id=GOAL_ID, include_leases=True)["leases"]
- assert leases[0]["status"] == "released"
- replay = run(*refresh)
+ assert all(lease["status"] == "released" for lease in leases), leases
+ replay = run(*recovery_refresh)
assert replay["idempotent_replay"] and replay["explore_result_delivery"]["idempotent_replay"]
assert replay["explore_result"] == captured["explore_result"]
assert replay["explore_result_delivery"]["hook"]["intents"][0]["payload"] == display_input
diff --git a/tests/control_plane/test_explore_settlement_attachment.py b/tests/control_plane/test_explore_settlement_attachment.py
index 026b44b37c..e75bda46b0 100644
--- a/tests/control_plane/test_explore_settlement_attachment.py
+++ b/tests/control_plane/test_explore_settlement_attachment.py
@@ -72,7 +72,7 @@ def test_real_cli_settlement_exposes_optional_attachment(tmp_path, monkeypatch,
assert "--explore-result-json" not in writeback["command_template"]
rc, compact = _run_cli(path, runtime, *command, "--turn-envelope", cwd=project)
assert rc == 0, compact
- assert compact["writeback"]["settlement_plan"] == plan
+ assert compact["interaction_contract"]["cli_channel"]["settlement_plan"] == plan
rc, completed = _run_cli(path, runtime, "todo", "complete", *binding,
"--evidence", "Synthetic deliverable validated", "--no-follow-up", cwd=project)
assert rc != 0 and completed["settlement_blocked_completion"], completed
@@ -80,7 +80,7 @@ def test_real_cli_settlement_exposes_optional_attachment(tmp_path, monkeypatch,
# affordance; evidence attachment never bypasses the ordinary receipt gate.
def plans(value):
if isinstance(value, dict):
- if value.get("schema_version") == "quota_settlement_plan_v1":
+ if value.get("schema_version") in {"quota_settlement_plan_v1", "quota_settlement_plan_v2"}:
yield value
for child in value.values():
yield from plans(child)
@@ -483,14 +483,17 @@ def test_ordinary_cli_settlement_without_evidence_does_not_create_graph(tmp_path
assert rc == 0, guard
steps = guard["interaction_contract"]["cli_channel"]["settlement_plan"]["ordered_steps"]
assert steps[1]["optional_attachments"][0]["required"] is False
+ assert steps[2]["owner"] == "runtime" and "command_template" not in steps[2]
command = steps[1]["command_template"].replace("", "validated_change").replace(
"", "single_surface").replace("", "outcome_progress")
- rc, refreshed = _run_generated_cli(command + " --progress-result-class advanced"
+ refresh_cmd = (command + " --progress-result-class advanced"
" --progress-evidence-id validation:ordinary-result --delivery-boundary in_flight_continuation"
- " --no-global-sync --suppress-external-sinks", registry_path=path)
- assert rc == 0, refreshed
- rc, spent = _run_generated_cli(steps[2]["command_template"], registry_path=path)
- assert rc == 0, spent
+ " --no-global-sync --suppress-external-sinks")
+ rc, refreshed = _run_generated_cli(refresh_cmd, registry_path=path)
+ assert rc == 0 and refreshed["settlement_progress"]["state"] == "settled", refreshed
+ assert refreshed["settlement_result"]["ok"] is True
+ rc, replayed = _run_generated_cli(refresh_cmd, registry_path=path)
+ assert rc == 0 and replayed["idempotent_replay"] is True, replayed
assert _spend_run_count(runtime) == 1
assert not explore_result_log_path(runtime, GOAL_ID).exists()
diff --git a/tests/control_plane/test_frontier_deadline.py b/tests/control_plane/test_frontier_deadline.py
index b7caca89ce..1703f01fd2 100644
--- a/tests/control_plane/test_frontier_deadline.py
+++ b/tests/control_plane/test_frontier_deadline.py
@@ -1,3 +1,4 @@
+import copy
import json
import subprocess
import sys
@@ -200,7 +201,10 @@ def test_installed_cli_keeps_full_source_deadline_after_compaction(tmp_path, mon
assert summary["frontier_deadline"]["identity"] == "todo_monitor_23"
assert summary["frontier_deadline"]["next_due_at"] == "2099-01-01T00:01:00+00:00"
if role == "agent":
- assert "monitor_open_items" not in summary # CLI omits this diagnostic lane entirely.
+ from loopx.control_plane.quota.cli_projection import compact_quota_should_run_cli_payload
+ assert len(summary["monitor_open_items"]) >= 1
+ displayed = compact_quota_should_run_cli_payload(copy.deepcopy(packet))["agent_todo_summary"]
+ assert "monitor_open_items" not in displayed # CLI display omits this diagnostic lane.
else:
assert all(row["todo_id"] != "todo_monitor_23" for row in summary["gate_open_items"])
assert packet["scheduler_hint"]["cold_path_detail"]["frontier_recheck"][
diff --git a/tests/control_plane/test_full_loopx_checkpoint_restore.py b/tests/control_plane/test_full_loopx_checkpoint_restore.py
index 6c8b8d6d9f..24e1f75489 100644
--- a/tests/control_plane/test_full_loopx_checkpoint_restore.py
+++ b/tests/control_plane/test_full_loopx_checkpoint_restore.py
@@ -102,6 +102,8 @@ def cli(*args):
"retained": ["Complete experiments and original acceptance"], "evidence_refs": ["validation:fixture"]}}))
cli("refresh-state", *actor, "--classification", "validated_experiment", "--delivery-outcome", "outcome_progress",
"--delivery-batch-scale", "multi_surface", "--agent-vision-json", str(vision),
+ "--task-lease-idempotency-key", lease["idempotency_key"],
+ "--task-lease-expected-version", str(lease["version"]),
"--suppress-external-sinks", "--no-global-sync")
cli("quota", "spend-slot", *actor, "--source", "heartbeat", "--execute")
settled = read_heartbeat_settlement(runtime, goal_id="research", agent_id="researcher", todo_id=todo,
diff --git a/tests/control_plane/test_goal_create_completion.py b/tests/control_plane/test_goal_create_completion.py
index d105a7dbe8..2b2d6bbcf5 100644
--- a/tests/control_plane/test_goal_create_completion.py
+++ b/tests/control_plane/test_goal_create_completion.py
@@ -379,6 +379,7 @@ def lose_claim_response(*args, **kwargs):
@pytest.mark.parametrize("completion_surface", ["native-cli", "external-worker"])
def test_ordinary_goal_replans_after_first_todo_and_delivers_the_missing_section(environment, app, monkeypatch, provider, completion_surface):
"""Real creation, native CLI and authority; only the work decision is scripted."""
+ monkeypatch.delenv("LOOPX_CONTEXT_DELIVERY", raising=False)
configure, _, _, project, runtime = environment
_, request, _ = app
configure(provider, mode="soft_claim")
diff --git a/tests/control_plane/test_goal_outcome_diagnostics.py b/tests/control_plane/test_goal_outcome_diagnostics.py
index a4d1833760..59ebd19bb1 100644
--- a/tests/control_plane/test_goal_outcome_diagnostics.py
+++ b/tests/control_plane/test_goal_outcome_diagnostics.py
@@ -134,7 +134,8 @@ def test_review_capacity_diagnosis_and_lossless_review_recovery(acceptance_goal,
assert "outcome review capacity exceeded" in detail
assert "declaration and review budgets are independent" in detail
assert "all outcomes in the Goal, not the source count" in detail
- assert "report the capacity blocker" in detail
+ assert "storage_format: records" in detail
+ assert "recover ambiguous operations unchanged" in detail
assert declaration["source"]["summary"] not in json.dumps(rejected)
assert "保留证据" not in json.dumps(rejected, ensure_ascii=False)
assert cli("inspect")[1] == before
diff --git a/tests/control_plane/test_goal_state_context_transport.py b/tests/control_plane/test_goal_state_context_transport.py
index 2e07d7d2a0..a03f32d497 100644
--- a/tests/control_plane/test_goal_state_context_transport.py
+++ b/tests/control_plane/test_goal_state_context_transport.py
@@ -1,4 +1,4 @@
-"""Progressive quota and complete host delivery retain their distinct IO contracts."""
+"""Full and paged quota delivery preserve the same registered Goal obligations."""
from __future__ import annotations
import contextlib
@@ -82,7 +82,7 @@ def test_goal_availability_transport_preserves_full_read_and_recovery(tmp_path,
state_path=state, provider=provider)
source_args = dict(registry_path=registry, runtime_root=runtime, goal_id=goal, todo_id=todo_id)
original_effect = context_readback.effect_runtime_result
- requests, packets = [], []
+ requests = []
omit_delivery_profile = [False]
def observe(operation, request, **kwargs):
@@ -95,19 +95,26 @@ def observe(operation, request, **kwargs):
monkeypatch.setattr(context_readback, "effect_runtime_result", observe)
- def guard():
+ def run_full_guard():
output = io.StringIO()
with contextlib.redirect_stdout(output):
code = main(["--registry", str(registry), "--runtime-root", str(runtime), "--format", "json",
"quota", "should-run", "--goal-id", goal, "--agent-id", "agent-a",
"--runtime-profile", "generic_cli", "--context-delivery", "off"])
- packet = json.loads(output.getvalue())
- assert code == 0 and packet["should_run"] is True, packet.get("reason")
- packets.append(deepcopy(packet))
- return packet["interaction_contract"]["agent_channel"]
+ return code, json.loads(output.getvalue())
- small_channel = guard()
+ def assert_complete_goal(channel, expected_bytes):
+ goals = [row["content"] for row in channel["work_context"]["sources"] if row["source"] == "goal_state"]
+ assert goals == [{"state_file": str(state), "revision": "sha256:" + sha256(expected_bytes).hexdigest(),
+ "text": expected_bytes.decode(), "full_source": True}]
+ assert not any(row["source"] == "goal_state" for row in channel["required_reads"])
+
+ code, small = run_full_guard()
+ assert code == 0 and small["should_run"] is True, small.get("reason")
+ assert small["schema_version"] == "loopx_turn_envelope_v1"
small_request = requests[-1]
+ small_channel = small["interaction_contract"]["agent_channel"]
+ assert_complete_goal(small_channel, initial)
from loopx.cli import build_parser
from loopx.cli_commands.quota_capture import read_context_capture
from loopx.control_plane.turn_driver import codex_context
@@ -145,32 +152,29 @@ def guard():
page = read_context_capture(build_parser().parse_args(shlex.split(page["next_command"])[1:]))
assert sha256(raw).hexdigest() == digest
native_channel = json.loads(raw)["interaction_contract"]["agent_channel"]
- delivered = [row["content"] for row in native_channel["work_context"]["sources"]
- if row.get("source") == "goal_state"]
- assert len(delivered) == 1, "Native delivery must include the complete registered Goal"
- assert delivered[0] == {"state_file": str(state), "revision": "sha256:" + sha256(initial).hexdigest(),
- "text": initial.decode(), "full_source": True}
- assert not any(row.get("source") == "goal_state" for row in native_channel["required_reads"])
+ assert_complete_goal(native_channel, initial)
assert state.read_bytes() == initial
large = initial + b"\n## Evidence\n" + b"Prior evidence remains available. " * 100_000
large += "\n## Stop Conditions\n未经授权不得发布。\n".encode()
assert len(large) > 2 * 1024 * 1024 # Preserve the original source regression.
state.write_bytes(large)
- large_channel = guard()
+ code, large_packet = run_full_guard()
+ assert code == 0 and large_packet["should_run"] is True, large_packet.get("reason")
+ assert large_packet["schema_version"] == "loopx_turn_envelope_v1"
large_request = requests[-1]
- # Exercise the public decision, including its work-read obligations. The
- # internal planner input deliberately carries only selection metadata.
- captured_plan = packets[-1]
+ large_channel = large_packet["interaction_contract"]["agent_channel"]
assert state.read_bytes() == large
- # Source growth changes neither the complete Agent contract nor transport
- # size. The full Goal read remains pending; availability is not consumption.
- assert small_channel == large_channel
+ # Explicit Off returns the complete source, including a large Goal.
+ assert_complete_goal(large_channel, large)
assert large_channel["delivery_allowed"] is True
assert large_channel["work_context"]["complete"] is True
assert large_channel["work_context"]["selected_todo_text_verified"] is True
- assert any(row.get("source") == "goal_state" for row in large_channel["required_reads"])
- assert not any(row.get("source") == "goal_state" for row in large_channel["work_context"]["sources"])
- assert len(json.dumps(small_request).encode()) == len(json.dumps(large_request).encode())
+ assert small_channel["required_reads"] == large_channel["required_reads"]
+
+ goal_read = next(row for row in large_request["required_reads"] if row["source"] == "goal_state")
+ goal_read_probe = {"goal_id": goal, "interaction_contract": {
+ "agent_channel": {"required_reads": [goal_read]},
+ }}
content = context_readback._source_content({"source": "goal_state"}, **source_args)
assert content == {"state_file": str(state), "revision": "sha256:" + sha256(large).hexdigest()}
full = context_readback._source_content(
@@ -179,18 +183,29 @@ def guard():
omit_delivery_profile[0] = True
try:
with pytest.raises(RuntimeError, match="unsupported by this runtime"):
- context_readback.attach_work_context(deepcopy(captured_plan), registry_path=registry,
+ context_readback.attach_work_context(deepcopy(goal_read_probe), registry_path=registry,
runtime_root=runtime, hook_dispatch=None, inline_goal_source=True)
finally:
omit_delivery_profile[0] = False
- # Independently compare the current typed projector with the former full
- # source input. Removing the transport must not weaken its obligations.
- legacy_request = deepcopy(large_request)
- source = next(row for row in legacy_request["source_results"]
- if row.get("content", {}).get("state_file") == str(state))
- source["content"]["text"] = large.decode()
- assert original_effect("work_item.context.project", legacy_request, large_local_snapshot=True) == \
- original_effect("work_item.context.project", large_request, large_local_snapshot=True)
+
+ def metadata_projection(request):
+ projected = deepcopy(request)
+ projected.pop("require_full_goal_source")
+ result = next(row for row in projected["source_results"] if row["command"] == goal_read["command"])
+ result["content"].pop("text")
+ result["content"].pop("full_source")
+ return projected, result
+
+ # The metadata-only projector still preserves a pending read at constant
+ # transport size. A body without the complete-source marker cannot fulfill it.
+ small_meta, _ = metadata_projection(small_request)
+ large_meta, legacy_goal = metadata_projection(large_request)
+ assert len(json.dumps(small_meta).encode()) == len(json.dumps(large_meta).encode())
+ pending = original_effect("work_item.context.project", large_meta, large_local_snapshot=True)
+ assert goal_read in pending["required_reads"]
+ assert not any(row["source"] == "goal_state" for row in pending["work_context"].get("sources", []))
+ legacy_goal["content"]["text"] = large.decode()
+ assert original_effect("work_item.context.project", large_meta, large_local_snapshot=True) == pending
changed = large + b"\nAdditional stop at the final byte.\n"
state.write_bytes(changed)
@@ -200,7 +215,7 @@ def guard():
# Fault the disposable source after admission: exercise actual file reads
# and the real TS context projection, not a mocked failure result. Then
- # recover through the complete native CLI guard.
+ # recover through the public full-source CLI guard.
for fault in ("invalid_utf8_tail", "missing"):
if fault == "missing":
state.unlink()
@@ -210,7 +225,7 @@ def guard():
error = UnicodeDecodeError
with pytest.raises(error):
context_readback._source_content({"source": "goal_state"}, **source_args)
- failed = deepcopy(captured_plan)
+ failed = deepcopy(goal_read_probe)
context_readback.attach_work_context(failed, registry_path=registry,
runtime_root=runtime, hook_dispatch=None)
channel = failed["interaction_contract"]["agent_channel"]
@@ -218,7 +233,11 @@ def guard():
assert channel["work_context"]["complete"] is False
assert any(row.get("source") == "goal_state" for row in channel["required_reads"])
with pytest.raises(RuntimeError, match="Goal source"):
- context_readback.attach_work_context(deepcopy(captured_plan), registry_path=registry,
+ context_readback.attach_work_context(deepcopy(goal_read_probe), registry_path=registry,
runtime_root=runtime, hook_dispatch=None, inline_goal_source=True)
+ _, refused = run_full_guard()
+ assert refused.get("normal_delivery_allowed") is not True
state.write_bytes(large)
- assert guard() == large_channel
+ code, recovered = run_full_guard()
+ assert code == 0 and recovered["normal_delivery_allowed"] is True
+ assert_complete_goal(recovered["interaction_contract"]["agent_channel"], large)
diff --git a/tests/control_plane/test_heartbeat_receipt.py b/tests/control_plane/test_heartbeat_receipt.py
index 5a7e6f04ba..ecb8312e8c 100644
--- a/tests/control_plane/test_heartbeat_receipt.py
+++ b/tests/control_plane/test_heartbeat_receipt.py
@@ -374,22 +374,22 @@ def rows():
# Both routes still owe the same result, writeback and exactly one debit.
(project / "validated-result.txt").write_text("Synthetic result validated before closeout.\n")
plan = reentry["interaction_contract"]["cli_channel"]["settlement_plan"]
+ assert plan["schema_version"] == "quota_settlement_plan_v2"
command = next(s["command_template"] for s in plan["ordered_steps"] if s["kind"] == "durable_writeback")
command = command.replace("", "admission_timer_validated").replace(
"", "single_surface").replace("", "outcome_progress")
command += (" --delivery-boundary in_flight_continuation --progress-result-class advanced"
f" --progress-work-item-id {work} --progress-evidence-id evidence:synthetic-result"
" --no-global-sync --suppress-external-sinks")
- rc, written = _run_generated_cli(command, registry_path=registry, cwd=project)
- assert rc == 0, written
- spend = written["settlement_owed"]["command"]
for attempt in range(2):
- rc, settled = _run_generated_cli(spend, registry_path=registry, cwd=project)
- assert rc == 0, settled
- assert settled["settlement_result"]["ok"] is True
- if attempt:
- assert settled["idempotent_replay"] is True
- assert settled["appended"] is False
+ rc, written = _run_generated_cli(command, registry_path=registry, cwd=project)
+ assert rc == 0, written
+ assert written["settlement_result"]["ok"] is True
+ assert written["settlement_progress"]["state"] == "settled"
+ assert [row["step_kind"] for row in written["settlement_result"]["receipts"]] == [
+ "validation", "durable_writeback", "quota_spend",
+ ]
+ assert written.get("idempotent_replay", False) is bool(attempt)
assert _spend_run_count(runtime) == 1
rc, replay = _run_cli(registry, runtime, *guard)
assert rc == 0, replay
diff --git a/tests/control_plane/test_heartbeat_registry_route.py b/tests/control_plane/test_heartbeat_registry_route.py
index ecec55ac74..9207731093 100644
--- a/tests/control_plane/test_heartbeat_registry_route.py
+++ b/tests/control_plane/test_heartbeat_registry_route.py
@@ -240,6 +240,7 @@ def test_cli_generated_guard_selection_and_settlement_ignore_conflicting_cwd_reg
_route(step["command_template"], registry, runtime)
assert cli._spend_run_count(runtime) == 0
+ assert plan["schema_version"] == "quota_settlement_plan_v2"
refresh = next(c for c in channel["next_cli_actions"] if "refresh-state" in c)
replacements = {
"": "validated_progress", "": "implementation",
@@ -247,19 +248,20 @@ def test_cli_generated_guard_selection_and_settlement_ignore_conflicting_cwd_reg
}
for placeholder, value in replacements.items():
refresh = refresh.replace(placeholder, value)
- code, refreshed = _execute(shlex.split(refresh)[1:] + [
+ refresh_args = shlex.split(refresh)[1:] + [
"--vision-state", "vision_on_track",
"--vision-summary", "Keep validating the selected work.",
"--vision-acceptance", "The same Turn settles once against the selected authority.",
"--no-global-sync", "--suppress-external-sinks",
- ], project)
- assert code == 0 and refreshed["settlement_result"]["ok"], refreshed
- spend = next(c for c in channel["next_cli_actions"] if "spend-slot" in c)
+ ]
for replay in (False, True):
- code, spent = _execute(shlex.split(spend)[1:], project)
- assert code == 0 and spent["settlement_result"]["ok"], spent
- if replay:
- assert spent["idempotent_replay"] and not spent["appended"]
+ code, refreshed = _execute(refresh_args, project)
+ assert code == 0 and refreshed["settlement_result"]["ok"], refreshed
+ assert refreshed["settlement_progress"]["state"] == "settled"
+ assert [row["step_kind"] for row in refreshed["settlement_result"]["receipts"]] == [
+ "validation", "durable_writeback", "quota_spend",
+ ]
+ assert refreshed.get("idempotent_replay", False) is replay
assert cli._spend_run_count(runtime) == 1
code, settled = _execute(guard_argv, project)
assert code == 0 and settled["effective_action"] == "heartbeat_settled_skip", settled
diff --git a/tests/control_plane/test_issue_fix_private_evidence_settlement.py b/tests/control_plane/test_issue_fix_private_evidence_settlement.py
index f1c493a968..6ea5a4793c 100644
--- a/tests/control_plane/test_issue_fix_private_evidence_settlement.py
+++ b/tests/control_plane/test_issue_fix_private_evidence_settlement.py
@@ -99,6 +99,10 @@ def run(*command: str) -> dict:
refresh = (
"refresh-state",
*binding,
+ "--task-lease-idempotency-key",
+ "private-evidence-fixture",
+ "--task-lease-expected-version",
+ "1",
"--classification",
"validated_change",
"--delivery-batch-scale",
diff --git a/tests/control_plane/test_lifecycle_guard_reentry.py b/tests/control_plane/test_lifecycle_guard_reentry.py
index 14659dfe5e..aa9a5f7d0d 100644
--- a/tests/control_plane/test_lifecycle_guard_reentry.py
+++ b/tests/control_plane/test_lifecycle_guard_reentry.py
@@ -82,7 +82,28 @@ def add_work():
*(["--replan-obligation-id", obligation["obligation_id"]]
if new_work == "bound_before_closeout" else []))
later = None
- if new_work != "after_closeout":
+ if new_work == "bound_before_closeout":
+ later = add_work()
+ assert later["replan_transition"]["recorded"] is True
+ assert later["replan_transition"]["outcome"] == "new_runnable_successor"
+ pending = _guard(call, turn)
+ assert pending["heartbeat_receipt"]["settlement_identity"] == identity
+ assert pending.get("selected_todo") is None
+ assert pending["autonomous_replan_obligation"]["resolution_mode"] == "receipt_bound_replan_settlement"
+ assert pending["replan_action_packet"]["decision"] == "successor_transition_recorded"
+ assert pending["replan_action_packet"]["successor_todo_id"] == later["todo_id"]
+ denied = call("quota", "spend-slot", "--goal-id", GOAL, "--agent-id", AGENT,
+ "--codex-app", "--turn-instance-id", turn, "--replan-obligation-id",
+ obligation["obligation_id"], "--source", "heartbeat", "--execute", expected_code=1)
+ assert denied.get("appended") is not True
+ actions = pending["interaction_contract"]["cli_channel"]["next_cli_actions"]
+ refreshed = call(*shlex.split(actions[0])[1:], "--no-global-sync", "--suppress-external-sinks")
+ assert refreshed["settlement_progress"]["state"] == "spend_required"
+ assert call(*shlex.split(actions[1])[1:])["settlement_progress"]["state"] == "settled"
+ assert call(*shlex.split(actions[1])[1:])["appended"] is False
+ assert call("todo", "list", "--goal-id", GOAL, "--todo-id", later["todo_id"])["todo"]["status"] == "open"
+ return
+ if new_work == "before_closeout":
later = add_work()
for _ in range(2):
pending = _guard(call, turn)
@@ -177,14 +198,22 @@ def test_native_cli_context_exposes_the_same_bound_lifecycle_reentry(
entry = call("quota", "should-run", "--runtime-profile", "generic_cli", "--goal-id", GOAL,
"--agent-id", AGENT, "--turn-instance-id", turn, "--context-delivery", delivery,
"--decision-output-dir", str(tmp_path / "capture"))
+ from loopx.control_plane.quota.turn_envelope import build_turn_envelope
+
if delivery == "auto":
assert entry["schema_version"] == "context_capture_navigation_v1"
selected = call("quota", "context-read", "--reference-file", entry["recovery"]["reference_file"],
- "--select", "/replan_action_packet/writeback", "--page-bytes", "8000")
+ "--select", "/interaction_contract/cli_channel/next_cli_actions", "--page-bytes", "8000")
assert selected["eof"] is True
- writer_command = json.loads(selected["content"])["selections"][0]["value"]
+ selected_actions = json.loads(selected["content"])["selections"][0]["value"]
+ full_page = call("quota", "context-read", "--reference-file", entry["recovery"]["reference_file"],
+ "--page-bytes", "64000", "--max-page-output-bytes", "64000")
+ assert full_page["eof"] is True
+ projected = json.loads(full_page["content"])
+ assert projected["interaction_contract"]["cli_channel"]["next_cli_actions"] == selected_actions
else:
- writer_command = entry["replan_action_packet"]["writeback"]
+ projected = entry
+ writer_command = build_turn_envelope(projected)["replan_action_packet"]["writeback"]
result = subprocess.run(shlex.split(writer_command), capture_output=True, text=True, check=True)
writer = json.loads(result.stdout)
actions = writer["writeback"]["next_cli_actions"]
diff --git a/tests/control_plane/test_local_peer_delivery_journey.py b/tests/control_plane/test_local_peer_delivery_journey.py
index 4289539ee1..3d4cba95c2 100644
--- a/tests/control_plane/test_local_peer_delivery_journey.py
+++ b/tests/control_plane/test_local_peer_delivery_journey.py
@@ -12,7 +12,7 @@
from canonical_authority_fixture import isolate_sqlite_runtime
-@pytest.mark.parametrize("provider", ["legacy", "file", "sqlite"])
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_bound_repository_is_checked_from_the_complete_todo_source(tmp_path, monkeypatch, provider):
isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry, _, _ = journey._source(
@@ -84,6 +84,8 @@ def run(*args, cwd=project):
"--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID,
"--task-lease-idempotency-key", key)
assert code == 0 and claimed["ok"], claimed
+ code, guard = journey._guard(project, runtime, registry)
+ assert code == 0 and guard["decision"] == "run", guard
proof = ("--task-lease-idempotency-key", key,
"--task-lease-expected-version", str(claimed["lease"]["version"]))
# A successful caller preflight cannot substitute for controller validation.
@@ -125,7 +127,7 @@ def run(*args, cwd=project):
"--todo-id", cli.TODO_ID, "--turn-instance-id", cli.TURN_ID,
"--delivery-outcome", "outcome_progress", "--no-global-sync", cwd=outside)
assert not wrong_workspace["ok"] and not wrong_workspace["appended"], wrong_workspace
- code, refreshed = journey._refresh(project, runtime, registry)
+ code, refreshed = journey._refresh(project, runtime, registry, *proof)
assert code == 0 and refreshed["ok"], json.dumps(refreshed)
assert refreshed["delivery_workspace"]["identity_kind"] == "local_goal"
assert refreshed["delivery_workspace"]["workspace_identity"] == f"loopx:{cli.GOAL_ID}"
@@ -156,22 +158,27 @@ def test_local_in_flight_progress_keeps_completion_independent(tmp_path, monkeyp
"--agent-id", cli.AGENT_ID, "--claimed-by", cli.AGENT_ID,
"--task-lease-idempotency-key", "local-in-flight", cwd=project)
assert code == 0 and claimed["ok"], claimed
- if release_execution_lease:
- code, released = cli._run_cli(
- registry, runtime, "task-lease", "release", "--goal-id", cli.GOAL_ID,
- "--todo-id", cli.TODO_ID, "--owner", cli.AGENT_ID,
- "--idempotency-key", "local-in-flight",
- "--expected-version", str(claimed["lease"]["version"]), cwd=project,
- )
- assert code == 0 and released["ok"], released
+ code, guard = journey._guard(project, runtime, registry)
+ assert code == 0 and guard["decision"] == "run", guard
+ proof = ("--task-lease-idempotency-key", "local-in-flight",
+ "--task-lease-expected-version", str(claimed["lease"]["version"]))
code, refreshed = cli._run_cli(
registry, runtime, "refresh-state", "--goal-id", cli.GOAL_ID,
"--agent-id", cli.AGENT_ID, "--todo-id", cli.TODO_ID,
"--turn-instance-id", cli.TURN_ID, "--delivery-boundary", "in_flight_continuation",
"--delivery-outcome", "outcome_progress", "--delivery-batch-scale", "multi_surface",
+ *proof,
"--no-global-sync", "--suppress-external-sinks", cwd=project,
)
assert code == 0 and refreshed["ok"], json.dumps(refreshed)
+ if release_execution_lease:
+ code, released = cli._run_cli(
+ registry, runtime, "task-lease", "release", "--goal-id", cli.GOAL_ID,
+ "--todo-id", cli.TODO_ID, "--owner", cli.AGENT_ID,
+ "--idempotency-key", "local-in-flight",
+ "--expected-version", str(claimed["lease"]["version"]), cwd=project,
+ )
+ assert code == 0 and released["ok"], released
assert refreshed["delivery_workspace"]["workspace_kind"] == "local_goal_workspace"
code, spent = journey._execute(refreshed["settlement_owed"]["command"], project, runtime, registry)
assert code == 0 and spent["settlement_progress"]["state"] == "settled", spent
@@ -254,7 +261,7 @@ def run(*args):
assert cli._spend_run_count(runtime) == 0
-@pytest.mark.parametrize("provider", ["legacy", "file", "sqlite"])
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
@pytest.mark.parametrize("explicit_repository", [False, True])
def test_originless_local_git_settles_without_fabricating_repository(tmp_path, monkeypatch, provider, explicit_repository):
isolate_sqlite_runtime(tmp_path, monkeypatch)
@@ -285,7 +292,7 @@ def test_originless_local_git_settles_without_fabricating_repository(tmp_path, m
assert cli._spend_run_count(runtime) == 1
-@pytest.mark.parametrize("provider", ["legacy", "file", "sqlite"])
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_nested_originless_delivery_recovery_names_registered_workspace(tmp_path, monkeypatch, provider):
isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry, _, _ = journey._source(
diff --git a/tests/control_plane/test_material_feedback_checkpoint.py b/tests/control_plane/test_material_feedback_checkpoint.py
index 087027a989..e887530916 100644
--- a/tests/control_plane/test_material_feedback_checkpoint.py
+++ b/tests/control_plane/test_material_feedback_checkpoint.py
@@ -149,14 +149,26 @@ def read_stage(command):
assert fixture._spend_run_count(runtime) == 0
refreshed = execute(result_command, validated_progress="route_feedback_verified",
scale="implementation", outcome="outcome_progress", file=vision)
- execute(result_command, validated_progress="route_feedback_verified",
+ replayed = execute(result_command, validated_progress="route_feedback_verified",
scale="implementation", outcome="outcome_progress", file=vision)
assert fixture._classification_count(runtime, "route_feedback_verified") == 1
- persisted = json.loads(Path(refreshed["json_path"]).read_text())
+ assert refreshed["settlement_progress"]["state"] == "settled"
+ assert refreshed["settlement_result"]["ok"] is True
+ assert replayed["idempotent_replay"] is True
+ if "json_path" in refreshed:
+ persisted_path = refreshed["json_path"]
+ else:
+ selected_result = subprocess.run(
+ shlex.split(refreshed["result_details"]["command"] + " --select /json_path"),
+ cwd=project, capture_output=True, text=True, check=True,
+ )
+ selected_page = json.loads(selected_result.stdout)
+ persisted_path = json.loads(selected_page["content"])["selections"][0]["value"]
+ persisted = json.loads(Path(persisted_path).read_text())
assert persisted["vision_checkpoint"]["delivery_boundary"] == "semantic_closeout"
assert persisted["agent_vision"]["path_delta"]["evidence_refs"] == [evidence]
- execute(plan["ordered_steps"][2]["command_template"])
- execute(plan["ordered_steps"][2]["command_template"])
+ assert plan["ordered_steps"][2]["owner"] == "runtime"
+ assert "command_template" not in plan["ordered_steps"][2]
assert fixture._spend_run_count(runtime) == 1
retained_todo = cli("todo", "list", "--goal-id", fixture.GOAL_ID, "--todo-id", fixture.TODO_ID)["todo"]
assert retained_todo["status"] == "open" and retained_todo["completion_validation_required"]
@@ -214,7 +226,10 @@ def read_stage(command):
+ " --agent-vision-json " + shlex.quote(str(vision)) + " --no-global-sync --suppress-external-sinks",
validated_progress="route_comparison_decided", scale="implementation", outcome="outcome_progress")
assert acknowledged["autonomous_replan_ack"]["semantic_delta"]["accepted"]
- execute(review_plan["ordered_steps"][2]["command_template"])
+ if "command_template" in review_plan["ordered_steps"][2]:
+ execute(review_plan["ordered_steps"][2]["command_template"])
+ else:
+ assert acknowledged["settlement_progress"]["state"] == "settled"
next_guard = cli("quota", "should-run", *binding, "--runtime-profile", "generic_cli",
"--todo-id", fixture.TODO_ID, "--turn-instance-id", "route-feedback-2")
assert next_guard["should_run"] and next_guard["selected_todo"]["todo_id"] == fixture.TODO_ID
diff --git a/tests/control_plane/test_model_behavior_qualification.py b/tests/control_plane/test_model_behavior_qualification.py
index 7df96eff00..64876a2216 100644
--- a/tests/control_plane/test_model_behavior_qualification.py
+++ b/tests/control_plane/test_model_behavior_qualification.py
@@ -1032,6 +1032,9 @@ def test_complete_candidate_scans_actual_scheduler_source_paths(operation: str,
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_real_cli_private_source_is_refused_before_provider_io(tmp_path, monkeypatch, provider) -> None:
+ from loopx.control_plane.scheduler.execution_context import (
+ scheduler_execution_context_for_runtime_profile,
+ )
from loopx.control_plane.testing.model_behavior_qualification import ModelBehaviorPairValidationError
from tests.control_plane.canonical_authority_fixture import isolate_sqlite_runtime
from tests.control_plane.test_quota_settlement_cli import (
@@ -1044,7 +1047,10 @@ def test_real_cli_private_source_is_refused_before_provider_io(tmp_path, monkeyp
"--agent-id", AGENT_ID, "--codex-app", "--context-delivery", "off",
"--turn-instance-id", "qualification-private-source", cwd=project)
assert rc == 0 and source["ok"] is True
- candidate = compose_turn_decision(source)
+ candidate = compose_turn_decision(
+ source,
+ scheduler_execution_context=scheduler_execution_context_for_runtime_profile("codex_app_heartbeat"),
+ )
before = json.dumps(candidate, sort_keys=True)
calls = []
# Real CLI context includes private runtime routing. Complete-source
diff --git a/tests/control_plane/test_monitor_observation_admission.py b/tests/control_plane/test_monitor_observation_admission.py
index 1c088d8f40..eeea31d36e 100644
--- a/tests/control_plane/test_monitor_observation_admission.py
+++ b/tests/control_plane/test_monitor_observation_admission.py
@@ -310,9 +310,10 @@ def test_completed_advancement_retains_auxiliary_monitor_admission(
AGENT_ID,
"--claimed-by",
AGENT_ID,
+ "--turn-instance-id",
+ turn,
"--evidence",
"read-only fixture delivery validated",
- "--no-follow-up",
"--task-lease-idempotency-key",
"primary-execution",
"--task-lease-expected-version",
@@ -333,6 +334,10 @@ def test_completed_advancement_retains_auxiliary_monitor_admission(
*binding,
"--todo-id",
TODO_ID,
+ "--task-lease-idempotency-key",
+ "primary-execution",
+ "--task-lease-expected-version",
+ "3",
"--classification",
"fixture_delivery_validated",
"--delivery-batch-scale",
@@ -449,26 +454,35 @@ def parsed_closeout(step):
}.get(token, token)
for token in _projected_cli_args(writeback_command, turn_instance_id=turn)
)
- rc, refresh = _run_cli(
- registry_path,
- runtime,
- *args,
- "--delivery-workspace-path",
- str(project),
- "--no-global-sync",
- "--suppress-external-sinks",
- )
- assert rc == 0, refresh
- spend_command = refresh["settlement_owed"]["command"]
+ for replay_refresh in (False, True):
+ rc, refresh = _run_cli(
+ registry_path,
+ runtime,
+ *args,
+ "--delivery-workspace-path",
+ str(project),
+ "--no-global-sync",
+ "--suppress-external-sinks",
+ )
+ assert rc == 0, refresh
+ assert "settlement_owed" not in refresh
+ assert refresh["settlement_progress"]["state"] == "settled"
+ assert [row["step_kind"] for row in refresh["settlement_result"]["receipts"]] == [
+ "validation",
+ "durable_writeback",
+ "quota_spend",
+ ]
+ assert refresh.get("idempotent_replay", False) is replay_refresh
+ assert _spend_run_count(runtime) == 1
else:
assert resume["next_step"]["kind"] == "quota_spend"
spend_command = poll["settlement_owed"]["command"]
- spend_args = _projected_cli_args(spend_command, turn_instance_id=turn)
- for _ in range(2):
- rc, spend = _run_cli(registry_path, runtime, *spend_args)
- assert rc == 0, spend
- assert spend["settlement_identity"]["todo_id"] == TODO_ID
- assert _spend_run_count(runtime) == 1
+ spend_args = _projected_cli_args(spend_command, turn_instance_id=turn)
+ for _ in range(2):
+ rc, spend = _run_cli(registry_path, runtime, *spend_args)
+ assert rc == 0, spend
+ assert spend["settlement_identity"]["todo_id"] == TODO_ID
+ assert _spend_run_count(runtime) == 1
rc, replay = _run_cli(registry_path, runtime, *poll_args)
assert rc == 0, replay
assert replay["replayed"] is True
diff --git a/tests/control_plane/test_native_monitor_poll.py b/tests/control_plane/test_native_monitor_poll.py
index d1f30f791c..ea25438183 100644
--- a/tests/control_plane/test_native_monitor_poll.py
+++ b/tests/control_plane/test_native_monitor_poll.py
@@ -1,16 +1,17 @@
"""Actual CLI -> quota -> canonical transaction -> projection -> quota receipt."""
from __future__ import annotations
-import pytest
import hashlib
import json
+import pytest
+
from canonical_authority_fixture import initialize_canonical_authority, isolate_sqlite_runtime
-from test_monitor_followthrough_contract import _write_fixture, GOAL_ID, AGENT_ID
+from test_monitor_followthrough_contract import _add_monitor, _write_fixture, GOAL_ID, AGENT_ID
from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection
from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted
from loopx.control_plane.scheduler.monitor_poll_writeback import write_monitor_poll_todo_state
-from loopx.control_plane.testing.canary_harness import run_json_cli
+from loopx.control_plane.testing.canary_harness import run_json_cli, run_json_cli_result
def _canonical(
@@ -84,6 +85,47 @@ def test_public_cli_native_monitor_settles_with_independent_successor(tmp_path,
assert state.exists()
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_due_monitor_key_cannot_replace_selected_todo(tmp_path, monkeypatch, provider):
+ isolate_sqlite_runtime(tmp_path, monkeypatch)
+ registry, runtime, _state, first = _canonical(tmp_path, native=True, provider=provider)
+ second_key = "secondary-watch"
+ second = _add_monitor(
+ registry, text="Observe a second public target", target_key=second_key,
+ next_due_at="2001-01-01T00:00:00Z",
+ )
+ route = ("--goal-id", GOAL_ID, "--agent-id", AGENT_ID, "--runtime-profile", "generic_cli")
+ lookup = {"registry_path": registry, "runtime_root": runtime}
+ guard = run_json_cli("quota", "should-run", *route, **lookup)
+ assert guard["work_lane_contract"]["obligation"] == "attempt_due_monitor"
+ targets = {first["todo_id"]: first["target_key"], second["todo_id"]: second_key}
+ selected_id = guard["selected_todo"]["todo_id"]
+ selected_key = targets.pop(selected_id)
+ alternate_id, alternate_key = next(iter(targets.items()))
+
+ def snapshot():
+ return read_canonical_todos_if_promoted(runtime_root=runtime, goal_id=GOAL_ID)
+
+ before = snapshot()
+ poll = ("quota", "monitor-poll", *route, "--result-hash", "observed-revision", "--execute")
+ code, rejected = run_json_cli_result(*poll, "--target-key", alternate_key, **lookup)
+ assert code != 0 and not rejected["ok"], rejected
+ assert "monitor-poll requires" in rejected["reason"]
+ assert snapshot() == before
+
+ admitted = run_json_cli(
+ *poll, "--todo-id", alternate_id, "--target-key", alternate_key, **lookup,
+ )
+ assert admitted["todo_writeback"]["todo_id"] == alternate_id
+ after = snapshot()
+ assert int(after["cursor"]) == int(before["cursor"]) + 1
+ prior_selected = next(row for row in before["todos"] if row["todo_id"] == selected_id)
+ assert next(row for row in after["todos"] if row["todo_id"] == selected_id) == prior_selected
+
+ matching_key = run_json_cli(*poll, "--target-key", selected_key, **lookup)
+ assert matching_key["todo_writeback"]["todo_id"] == selected_id
+
+
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_native_monitor_retry_and_projection_failure_do_not_repeat_business(tmp_path, monkeypatch, provider):
isolate_sqlite_runtime(tmp_path, monkeypatch)
diff --git a/tests/control_plane/test_open_outcome_replan.py b/tests/control_plane/test_open_outcome_replan.py
index 3eccd94f11..4a7b2ec5ba 100644
--- a/tests/control_plane/test_open_outcome_replan.py
+++ b/tests/control_plane/test_open_outcome_replan.py
@@ -90,7 +90,11 @@ def guard(turn):
envelope = call("quota", "should-run", "--codex-app", "--turn-envelope", "--goal-id", GOAL,
"--agent-id", AGENT, "--todo-id", TODO, "--turn-instance-id", TURN,
)
- delivered = next(source for source in envelope["work_context"]["sources"] if source["source"] == "goal_acceptance")
+ delivered = next(
+ source
+ for source in envelope["interaction_contract"]["agent_channel"]["work_context"]["sources"]
+ if source["source"] == "goal_acceptance"
+ )
assert acceptance == delivered["content"]["goal_acceptance_contract"]
assert inspect() == before, "review admission does not rewrite acceptance"
diff --git a/tests/control_plane/test_operation_requests.py b/tests/control_plane/test_operation_requests.py
index 55c0c64436..8d184b29da 100644
--- a/tests/control_plane/test_operation_requests.py
+++ b/tests/control_plane/test_operation_requests.py
@@ -220,10 +220,13 @@ def test_current_goal_lifetime_is_checked_before_frozen_request_io(tmp_path, dri
session_id="owned-dsh", profile_digest="d" * 64, model="fixture-model",
reasoning_effort="high", host="dsh", frozen_request=bound, goal_admission=admission)
assert handler("loopx_operation", {"action": "context"}, native)["ok"]
+ from loopx.control_plane.goals.activation import build_goal_activation
with source_session_registry_transaction(registry, operation="fixture_revoke") as txn:
value = txn.payload_copy()
if drift == "stopped":
- value["goals"][0]["status"] = "stopped"
+ value["goals"][0]["activation"] = build_goal_activation(
+ state="stopped", updated_at="2026-01-01T00:00:01Z", reason="fixture stopped",
+ )
else:
value["goals"][0]["goal_instance_id"] = INSTANCE_B
txn.commit(value)
diff --git a/tests/control_plane/test_pr_wait_consumer_adoption.py b/tests/control_plane/test_pr_wait_consumer_adoption.py
index 1b924d909c..4e7a131ffa 100644
--- a/tests/control_plane/test_pr_wait_consumer_adoption.py
+++ b/tests/control_plane/test_pr_wait_consumer_adoption.py
@@ -80,13 +80,13 @@ def fetch(reference, **kwargs):
assert observation["checks"]["pr_dependencies"]["external_read_count"] == (2 if number == 0 else 0)
captured = []
- project_envelope = turn.build_turn_envelope
+ project_decision = turn.compose_turn_decision
def capture(decision, **kwargs):
captured.append(copy.deepcopy(decision))
- return project_envelope(decision, **kwargs)
+ return project_decision(decision, **kwargs)
- monkeypatch.setattr(turn, "build_turn_envelope", capture)
+ monkeypatch.setattr(turn, "compose_turn_decision", capture)
runner = tmp_path / "consumer.py"
runner.write_text(
"import json\nfrom pathlib import Path\n"
@@ -95,7 +95,7 @@ def capture(decision, **kwargs):
" prompt = kwargs['prompt']\n"
" (root / 'consumer-input.txt').write_text(prompt)\n"
" request = json.JSONDecoder().raw_decode(prompt.split('Turn request:\\n', 1)[1])[0]\n"
- " authority = request['work_contract']\n"
+ " authority = request.get('work_contract') or request['task_context']\n"
" selected = authority['selected_todo']\n"
f" assert selected['todo_id'] == {todo_id!r}\n"
" assert selected['resume_when'] == 'pr_merged:owner/repo#42'\n"
@@ -139,7 +139,8 @@ def capture(decision, **kwargs):
assert expected["resume_condition"]["matched_event_at"] == "2026-10-01T00:00:00Z"
assert envelope["action_signature"]["matches"] is True
prompt = (project / "consumer-input.txt").read_text()
- consumed = json.JSONDecoder().raw_decode(prompt.split("Turn request:\n", 1)[1])[0]["work_contract"]
+ raw_request = json.JSONDecoder().raw_decode(prompt.split("Turn request:\n", 1)[1])[0]
+ consumed = raw_request.get("work_contract") or raw_request["task_context"]
assert consumed["selected_todo"]["resume_condition"] == expected["resume_condition"]
assert "Require the terminal independent artifact marker." in prompt
diff --git a/tests/control_plane/test_progress_observation.py b/tests/control_plane/test_progress_observation.py
index f1833a5d8e..44dbdce333 100644
--- a/tests/control_plane/test_progress_observation.py
+++ b/tests/control_plane/test_progress_observation.py
@@ -314,7 +314,14 @@ def test_host_projects_evidence_context_and_minimal_action_packet() -> None:
isinstance(instruction, str) and instruction
for instruction in packet["planning_guidance"]
)
- assert packet["writeback_contract"] == {}
+ no_followup = packet["writeback_contract"]["no_followup_requirements"]
+ assert set(packet["writeback_contract"]) == {"no_followup_requirements"}
+ assert "--progress-result-class" in no_followup["cli_args"]
+ assert no_followup["vision_fields"] == {
+ "state": "no_followup",
+ "path_delta.outcome": "stop",
+ }
+ assert no_followup["preserve"] == ["vision_patch"]
assert packet["allowed_terminal"] == [
"exploration_exhausted",
"blocked",
diff --git a/tests/control_plane/test_quota_acceptance_references.py b/tests/control_plane/test_quota_acceptance_references.py
index 2a7592d8e1..38e861e5bd 100644
--- a/tests/control_plane/test_quota_acceptance_references.py
+++ b/tests/control_plane/test_quota_acceptance_references.py
@@ -298,10 +298,12 @@ def test_inline_cli_delivers_complete_acceptance_for_selection_and_replan(tmp_pa
guard = call("quota", "should-run", "--codex-app", "--context-delivery", "off", "--goal-id", GOAL, "--agent-id", AGENT,
"--turn-instance-id", "turn-acceptance-sharing")
assert guard["effective_action"] == "autonomous_replan_required"
- ref = "$.interaction_contract.agent_channel.work_context.sources[0].content.goal_acceptance_contract"
+ guard_sources = guard["interaction_contract"]["agent_channel"]["work_context"]["sources"]
+ guard_index = next(i for i, row in enumerate(guard_sources) if row["source"] == "goal_acceptance")
+ ref = f"$.interaction_contract.agent_channel.work_context.sources[{guard_index}].content.goal_acceptance_contract"
assert guard["autonomous_replan_obligation"]["replan_context"]["core_goal"]["acceptance_contract_ref"] == ref
assert guard["agent_todo_summary"]["goal_acceptance_contract_ref"] == ref
- assert guard["interaction_contract"]["agent_channel"]["work_context"]["sources"][0]["content"]["goal_acceptance_contract"] == expected
+ assert guard_sources[guard_index]["content"]["goal_acceptance_contract"] == expected
assert build_turn_envelope(guard)["action_signature"]["matches"] is True
# Normal selected delivery must use the exact inline acceptance read and
diff --git a/tests/control_plane/test_quota_execution_preparation.py b/tests/control_plane/test_quota_execution_preparation.py
index 58d7ccc214..8072d2b5da 100644
--- a/tests/control_plane/test_quota_execution_preparation.py
+++ b/tests/control_plane/test_quota_execution_preparation.py
@@ -17,7 +17,10 @@
from loopx.control_plane.runtime.time import now_utc, utc_isoformat
from loopx.control_plane.testing.canary_harness import write_fixture_registry, run_json_cli_result
from loopx.control_plane.quota.execution_preparation import (
+ RefreshTaskLeaseUnavailable,
apply_selected_todo_execution_preparation,
+ guard_first_writeback_execution,
+ prepared_lease_receipt_fields,
)
@@ -576,9 +579,13 @@ def hook(name):
claim = invoke(*first["execution_preparation"]["claim"]["cli_args"], "--thin")
ready = invoke("quota", "should-run", *scope, "--runtime-profile", scenario, "--context-delivery", "off")
assert ready["execution_preparation"]["state"] == "ready"
+ proof = ready["execution_preparation"]["lease_proof"]
identity = ready["heartbeat_receipt"]["settlement_identity"]
assert identity == first["heartbeat_receipt"]["settlement_identity"]
- refresh = ["refresh-state", *scope, "--classification", "fixture_manual_result",
+ refresh = ["refresh-state", *scope,
+ "--task-lease-idempotency-key", proof["idempotency_key"],
+ "--task-lease-expected-version", str(proof["expected_version"]),
+ "--classification", "fixture_manual_result",
"--delivery-batch-scale", "single_surface", "--delivery-outcome", "outcome_progress",
"--delivery-boundary", "in_flight_continuation", "--progress-result-class", "advanced",
"--progress-work-item-id", TODO, "--progress-evidence-id", "fixture:manual-result",
@@ -1306,3 +1313,340 @@ def test_codex_lifecycle_hook_uses_captured_authority_from_another_project(
timeout=15,
)
assert repeated.returncode == 0 and json.loads(repeated.stdout) == {}
+
+
+def _writeback_guard_fixture(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ *,
+ todo_status: str = "open",
+ leases: list[dict[str, object]] | None = None,
+):
+ from types import SimpleNamespace
+
+ isolate_sqlite_runtime(tmp_path, monkeypatch)
+ project = tmp_path / "project"
+ project.mkdir(parents=True, exist_ok=True)
+ state_path = project / "ACTIVE_GOAL_STATE.md"
+ state_path.write_text("# Fixture\n", encoding="utf-8")
+ runtime = tmp_path / "runtime"
+ registry = tmp_path / "registry.json"
+ write_fixture_registry(
+ project=project,
+ runtime_root=runtime,
+ registry_path=registry,
+ goal_id=GOAL,
+ domain="fixture",
+ adapter_kind="read_only_project_map_v0",
+ state_file="ACTIVE_GOAL_STATE.md",
+ registered_agents=(AGENT,),
+ )
+ todo = {**_todo(), "status": todo_status, "done": todo_status == "done"}
+ initialize_canonical_authority(
+ runtime,
+ GOAL,
+ build_todo_runtime_shadow_projection(
+ goal_id=GOAL,
+ handoff_mode="hard_lease",
+ todos=[todo],
+ leases=list(leases or ()),
+ ),
+ state_path=state_path,
+ provider="sqlite",
+ )
+
+ def make_settlement(**overrides):
+ defaults = {
+ "prepared_lease": None,
+ "prepared_lease_required": True,
+ "completion_event": None,
+ "native_child_admission": None,
+ "semantic_replan_guard": None,
+ "heartbeat_receipt": None,
+ "supersede_event": None,
+ "identity": SimpleNamespace(
+ value=SimpleNamespace(turn_instance_id="turn-1"),
+ ),
+ }
+ defaults.update(overrides)
+ return SimpleNamespace(**defaults)
+
+ def run_guard(**kwargs):
+ with guard_first_writeback_execution(
+ runtime_root=runtime,
+ registry_path=registry,
+ goal_id=GOAL,
+ todo_id=TODO,
+ agent_id=AGENT,
+ locked_goal=None,
+ **kwargs,
+ ):
+ return True
+
+ return make_settlement, run_guard
+
+
+def test_same_generation_lease_renewal_admits_open_todo_writeback_and_rejects_mismatches(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Same-epoch lease renewals pass when explicitly enabled, never across epochs or keys."""
+ current = {**_lease(), "idempotency_key": f"{AGENT}-execution", "version": 3, "lease_epoch": 1}
+ make_settlement, run_guard = _writeback_guard_fixture(
+ tmp_path, monkeypatch, todo_status="open", leases=[current],
+ )
+ prepared = prepared_lease_receipt_fields(
+ {"idempotency_key": f"{AGENT}-execution", "expected_version": 1},
+ lease_epoch=1,
+ )
+ settlement = make_settlement(prepared_lease=prepared, prepared_lease_required=True)
+ stale_proof = {"idempotency_key": f"{AGENT}-execution", "expected_version": 1}
+ renewed_proof = {"idempotency_key": f"{AGENT}-execution", "expected_version": 3}
+
+ assert run_guard(
+ requested_proof=stale_proof,
+ settlement=settlement,
+ allow_same_generation_lease_renewal=True,
+ )
+ assert run_guard(
+ requested_proof=renewed_proof,
+ settlement=settlement,
+ allow_same_generation_lease_renewal=False,
+ )
+ with pytest.raises(RefreshTaskLeaseUnavailable):
+ run_guard(
+ requested_proof=stale_proof,
+ settlement=settlement,
+ allow_same_generation_lease_renewal=False,
+ )
+
+ for bad_settlement, bad_proof in (
+ # Cross-epoch renewal must fail closed even for the same key.
+ (
+ make_settlement(
+ prepared_lease={**prepared, "prepared_lease_epoch": 2},
+ prepared_lease_required=True,
+ ),
+ renewed_proof,
+ ),
+ # Cross-key proof must fail closed.
+ (
+ make_settlement(
+ prepared_lease={**prepared, "prepared_lease_idempotency_key": "other-key"},
+ prepared_lease_required=True,
+ ),
+ renewed_proof,
+ ),
+ # Legacy prepared receipt without lease_epoch must not advance versions.
+ (
+ make_settlement(
+ prepared_lease={"prepared_lease_idempotency_key": f"{AGENT}-execution", "prepared_lease_version": 1},
+ prepared_lease_required=True,
+ ),
+ renewed_proof,
+ ),
+ # Version regression in requested_proof must fail closed.
+ (
+ settlement,
+ {"idempotency_key": f"{AGENT}-execution", "expected_version": 4},
+ ),
+ ):
+ with pytest.raises(RefreshTaskLeaseUnavailable):
+ run_guard(
+ requested_proof=bad_proof,
+ settlement=bad_settlement,
+ allow_same_generation_lease_renewal=True,
+ )
+
+
+def test_validation_recovery_turn_accepts_only_matching_retired_recovery_lease(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Resumed recovery matches exact `.validation-recovery.` or `.admission-retry` retired leases."""
+ for index, suffix in enumerate((".validation-recovery.1", ".admission-retry", ".admission-retry.turn-2")):
+ retired_recovery = {
+ **_lease(status="released"),
+ "idempotency_key": f"turn-1{suffix}",
+ "version": 1,
+ "lease_epoch": 2,
+ }
+ make_settlement, run_guard = _writeback_guard_fixture(
+ tmp_path / f"recovery-{index}", monkeypatch, todo_status="done", leases=[retired_recovery],
+ )
+ prepared = prepared_lease_receipt_fields(
+ {"idempotency_key": "turn-1", "expected_version": 1},
+ lease_epoch=1,
+ )
+ completion = {
+ "event_kind": "todo_complete",
+ "todo_id": TODO,
+ "agent_id": AGENT,
+ "run_id": "turn-1",
+ }
+ valid_proof = {"idempotency_key": "turn-1", "expected_version": 1}
+ assert run_guard(
+ requested_proof=valid_proof,
+ settlement=make_settlement(
+ prepared_lease=prepared,
+ prepared_lease_required=True,
+ completion_event=completion,
+ ),
+ validation_recovery_turn_instance_id="turn-1",
+ )
+ for bad_completion, bad_recovery_turn in (
+ (None, "turn-1"),
+ ({**completion, "run_id": "other-turn"}, "turn-1"),
+ ({**completion, "agent_id": "other-agent"}, "turn-1"),
+ (completion, None),
+ (completion, "other-turn"),
+ ):
+ with pytest.raises(RefreshTaskLeaseUnavailable):
+ run_guard(
+ requested_proof=valid_proof,
+ settlement=make_settlement(
+ prepared_lease=prepared,
+ prepared_lease_required=True,
+ completion_event=bad_completion,
+ ),
+ validation_recovery_turn_instance_id=bad_recovery_turn,
+ )
+
+
+def test_validation_recovery_rejects_unrelated_retired_key(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """An unrelated retired lease without recovery/retry suffix never satisfies recovery."""
+ for index, bad_key in enumerate(("unrelated-key", "turn-1.admission-retry2")):
+ foreign_retired = {
+ **_lease(status="released"),
+ "idempotency_key": bad_key,
+ "version": 1,
+ "lease_epoch": 2,
+ }
+ make_settlement, run_guard = _writeback_guard_fixture(
+ tmp_path / f"reject-{index}", monkeypatch, todo_status="done", leases=[foreign_retired],
+ )
+ prepared = prepared_lease_receipt_fields(
+ {"idempotency_key": "turn-1", "expected_version": 1},
+ lease_epoch=1,
+ )
+ with pytest.raises(RefreshTaskLeaseUnavailable):
+ run_guard(
+ requested_proof={"idempotency_key": "turn-1", "expected_version": 1},
+ settlement=make_settlement(
+ prepared_lease=prepared,
+ prepared_lease_required=True,
+ completion_event={
+ "event_kind": "todo_complete",
+ "todo_id": TODO,
+ "agent_id": AGENT,
+ "run_id": "turn-1",
+ },
+ ),
+ validation_recovery_turn_instance_id="turn-1",
+ )
+
+
+def test_supersede_event_admits_done_todo_only_for_exact_turn(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Superseded done Todos satisfy completion receipt checks only for their exact Turn."""
+ retired = {**_lease(status="released"), "idempotency_key": "turn-1", "version": 1, "lease_epoch": 1}
+ make_settlement, run_guard = _writeback_guard_fixture(
+ tmp_path, monkeypatch, todo_status="done", leases=[retired],
+ )
+ prepared = prepared_lease_receipt_fields(
+ {"idempotency_key": "turn-1", "expected_version": 1},
+ lease_epoch=1,
+ )
+ supersede = {
+ "event_kind": "todo_supersede",
+ "status": "done",
+ "todo_id": TODO,
+ "agent_id": AGENT,
+ "run_id": "turn-1",
+ }
+ assert run_guard(
+ requested_proof={"idempotency_key": "turn-1", "expected_version": 1},
+ settlement=make_settlement(
+ prepared_lease=prepared,
+ prepared_lease_required=True,
+ supersede_event=supersede,
+ ),
+ )
+ for bad_supersede in (
+ None,
+ {**supersede, "run_id": "foreign-turn"},
+ {**supersede, "todo_id": "other-todo"},
+ {**supersede, "status": "open"},
+ ):
+ with pytest.raises(RefreshTaskLeaseUnavailable):
+ run_guard(
+ requested_proof={"idempotency_key": "turn-1", "expected_version": 1},
+ settlement=make_settlement(
+ prepared_lease=prepared,
+ prepared_lease_required=True,
+ supersede_event=bad_supersede,
+ ),
+ )
+
+
+def test_todo_bound_autonomous_replan_admits_without_prepared_lease_only_on_replan_guard(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Todo-bound autonomous replan Turns pass without prepared_lease but stay lease-fenced."""
+ active = {**_lease(), "idempotency_key": f"{AGENT}-execution", "version": 1, "lease_epoch": 1}
+ make_settlement, run_guard = _writeback_guard_fixture(
+ tmp_path, monkeypatch, todo_status="open", leases=[active],
+ )
+ valid_proof = {"idempotency_key": f"{AGENT}-execution", "expected_version": 1}
+ replan_settlement = make_settlement(
+ prepared_lease=None,
+ prepared_lease_required=False,
+ semantic_replan_guard={"scope": "turn_guard", "selected_obligation_id": "replan-1"},
+ heartbeat_receipt={"status": "autonomous_replan_required", "details": {"delivery_allowed": False}},
+ )
+ assert run_guard(requested_proof=valid_proof, settlement=replan_settlement)
+ assert run_guard(requested_proof=None, settlement=replan_settlement)
+
+ for bad_settlement, proof in (
+ (replan_settlement, {"idempotency_key": f"{AGENT}-execution", "expected_version": 99}),
+ (
+ make_settlement(
+ prepared_lease=None,
+ prepared_lease_required=False,
+ semantic_replan_guard={"scope": "turn_guard", "selected_obligation_id": "replan-1"},
+ heartbeat_receipt={"status": "normal_run", "details": {"delivery_allowed": True}},
+ ),
+ valid_proof,
+ ),
+ (
+ make_settlement(
+ prepared_lease=None,
+ prepared_lease_required=False,
+ semantic_replan_guard={"scope": "turn_guard", "selected_obligation_id": ""},
+ heartbeat_receipt={"status": "autonomous_replan_required", "details": {"delivery_allowed": False}},
+ ),
+ valid_proof,
+ ),
+ ):
+ with pytest.raises(RefreshTaskLeaseUnavailable):
+ run_guard(requested_proof=proof, settlement=bad_settlement)
+
+ # Foreign active lease must reject even on a todo-bound replan guard.
+ foreign_dir = tmp_path / "foreign"
+ make_foreign, run_foreign = _writeback_guard_fixture(
+ foreign_dir,
+ monkeypatch,
+ todo_status="open",
+ leases=[{**_lease(owner="other-agent"), "idempotency_key": "other-exec", "version": 1}],
+ )
+ with pytest.raises(RefreshTaskLeaseUnavailable):
+ run_foreign(
+ requested_proof=None,
+ settlement=make_foreign(
+ prepared_lease=None,
+ prepared_lease_required=False,
+ semantic_replan_guard={"scope": "turn_guard", "selected_obligation_id": "replan-1"},
+ heartbeat_receipt={"status": "autonomous_replan_required", "details": {"delivery_allowed": False}},
+ ),
+ )
diff --git a/tests/control_plane/test_quota_memory_context.py b/tests/control_plane/test_quota_memory_context.py
index fdb8570b41..7212cb032d 100644
--- a/tests/control_plane/test_quota_memory_context.py
+++ b/tests/control_plane/test_quota_memory_context.py
@@ -339,3 +339,60 @@ def test_real_guard_complete_memory_rejects_changed_source_without_provider_repl
quota_capability_automation_source(damaged)
assert memory.calls == 1
assert json.dumps(source, ensure_ascii=False, sort_keys=True) == before
+
+
+def test_attach_complete_turn_context_preserves_tokenless_and_numeric_roundtrip() -> None:
+ from loopx.control_plane.effect_runtime import source_number_tokens
+ from loopx.control_plane.quota.turn_envelope import (
+ assert_complete_turn_decision,
+ attach_complete_turn_context,
+ compose_turn_decision,
+ )
+
+ guard, _ = quota_context()
+ assert not source_number_tokens(guard)
+ base = compose_turn_decision(guard)
+ assert not source_number_tokens(base)
+
+ tokenless_addition = {"reward_memory_recall": {"status": "empty"}}
+ assert not source_number_tokens(tokenless_addition)
+ tokenless_attached = attach_complete_turn_context(base, tokenless_addition)
+ assert tokenless_attached["reward_memory_recall"] == {"status": "empty"}
+ assert_complete_turn_decision(tokenless_attached)
+
+ big_int = 2**80
+ numeric_attached = attach_complete_turn_context(
+ tokenless_attached,
+ {"post_attach_recall": {"val": 1.0, "big": big_int}},
+ )
+ post_attach = numeric_attached["post_attach_recall"]
+ assert isinstance(post_attach["val"], float)
+ assert post_attach["val"] == 1.0
+ assert type(post_attach["big"]) is int
+ assert post_attach["big"] == big_int
+ assert_complete_turn_decision(numeric_attached)
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_real_guard_complete_memory_accepts_auto_context_delivery(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, provider: str,
+) -> None:
+ registry, route = quota_fixture(tmp_path, monkeypatch, provider)
+ enable_live_memory(registry, recall=True, ingest=True)
+ _, config = resolve_reward_memory_experiment(
+ registry_path=registry, goal_id=GOAL_ID, agent_id=AGENT_ID)
+ memory = RecallProvider(active_record(config, expires_at="2099-01-01T00:00:00+00:00"))
+ monkeypatch.setattr(
+ "loopx.capabilities.agent_turn_recall.run_configured_agent_turn_recall_fail_open",
+ lambda **kwargs: run_configured_agent_turn_recall_fail_open(**kwargs, provider=memory))
+ guard = run_cli(
+ route + [
+ "quota", "should-run", "--goal-id", GOAL_ID, "--agent-id", AGENT_ID,
+ "--turn-instance-id", "auto-delivery-memory-check",
+ "--context-delivery", "auto", "--available-capability", "filesystem_read",
+ ],
+ )
+ assert guard["should_run"] is True
+ assert guard["reward_memory_recall"]["status"] == "applied"
+ assert memory.calls == 1
+ assert project_turn_context(guard)["reward_memory_recall"]["context"]["guidance"]
diff --git a/tests/control_plane/test_quota_selection.py b/tests/control_plane/test_quota_selection.py
index 2a9c068d82..3bea2ffd03 100644
--- a/tests/control_plane/test_quota_selection.py
+++ b/tests/control_plane/test_quota_selection.py
@@ -6,6 +6,7 @@
from canonical_authority_fixture import initialize_canonical_authority, isolate_sqlite_runtime, empty_todo_display
from loopx.control_plane.coordination.local_authority import read_canonical_todos_if_promoted
+from loopx.control_plane.quota.turn_envelope import assert_complete_turn_decision
from loopx.control_plane.todos.machine_section_projection import render_canonical_todo_sections
from loopx.control_plane.testing.canary_harness import write_fixture_registry, run_json_cli_result
from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection
@@ -333,7 +334,12 @@ def test_quota_uses_complete_owned_commitment_count_before_display_cap(reverse):
@pytest.mark.parametrize("display", ["current", "empty", "stale"])
@pytest.mark.parametrize("scope", ["global_gate=true", "blocks_agent=agent-b"])
-def test_public_quota_keeps_gate_from_real_canonical_provider(tmp_path: Path, display: str, scope: str):
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+@pytest.mark.parametrize("complete_turn", [False, True], ids=["ordinary", "complete-turn"])
+def test_public_quota_keeps_gate_from_real_canonical_provider(
+ tmp_path: Path, monkeypatch, display: str, scope: str, provider: str, complete_turn: bool
+):
+ isolate_sqlite_runtime(tmp_path, monkeypatch)
runtime, registry, state = tmp_path / "runtime", tmp_path / "registry.json", tmp_path / "state.md"
history = "\n".join(f"- [x] [P2] Completed synthetic work {i}.\n"
f" " for i in range(12))
@@ -350,17 +356,32 @@ def test_public_quota_keeps_gate_from_real_canonical_provider(tmp_path: Path, di
fields = parse_active_state_todos(state.read_text(), goal=goal, item_limit=None)
projection = build_todo_runtime_shadow_projection(goal_id="goal-scope",
todos=fields["agent_todos"]["items"] + fields["user_todos"]["items"], handoff_mode="soft_claim")
- initialize_canonical_authority(runtime, "goal-scope", projection, state_path=state)
+ initialize_canonical_authority(runtime, "goal-scope", projection, state_path=state, provider=provider)
if display == "empty":
empty_todo_display(state)
elif display == "stale":
state.write_text("# Stale projection\n## User Todo\n- [x] Old approval.\n")
before = state.read_bytes() if state.exists() else None
- code, packet = run_json_cli_result("quota", "should-run", "--goal-id", "goal-scope",
- "--agent-id", "agent-b", "--context-delivery", "off", registry_path=registry, runtime_root=runtime)
+ view_args = ("--complete-turn",) if complete_turn else ()
+ code, packet = run_json_cli_result(
+ "quota", "should-run", "--goal-id", "goal-scope", "--agent-id", "agent-b",
+ "--context-delivery", "off", *view_args, registry_path=registry, runtime_root=runtime,
+ )
assert code == 0, packet
assert packet["requires_user_action"] is True
- gates = packet["user_todo_summary"]["gate_open_items"]
+ if complete_turn:
+ assert_complete_turn_decision(packet)
+ summary = packet["user_todo_summary"]
+ if "gate_open_items_ref" in summary:
+ assert summary["gate_open_items_ref"] == "$.user_todo_summary.first_open_items"
+ gates = summary["first_open_items"]
+ else:
+ gates = summary["gate_open_items"]
assert [row["todo_id"] for row in gates] == ["todo_gate"]
- assert not packet["user_todo_summary"].get("claim_scope")
+ assert gates[0]["task_class"] == "user_gate"
+ if scope == "global_gate=true":
+ assert gates[0]["global_gate"] is True
+ else:
+ assert gates[0]["blocks_agent"] == "agent-b"
+ assert not summary.get("claim_scope")
assert (state.read_bytes() if state.exists() else None) == before
diff --git a/tests/control_plane/test_quota_vision_audit_references.py b/tests/control_plane/test_quota_vision_audit_references.py
index 1e617aa306..b51d982e9c 100644
--- a/tests/control_plane/test_quota_vision_audit_references.py
+++ b/tests/control_plane/test_quota_vision_audit_references.py
@@ -11,6 +11,7 @@
from loopx.control_plane.quota.turn_envelope import (
build_turn_envelope,
)
+from test_complete_turn_decision import _resign_contract, _source_digest
from test_replan_host_context_projection import _quota_payload
@@ -239,6 +240,7 @@ def test_real_guard_capture_and_recovery_preserve_vision_before_effects(
invalid["goal_frontier_projection"]["vision_continuation_audit"][
"required"
] = not audit["required"]
+ invalid, _ = _resign_contract(invalid)
invalid_path = tmp_path / "invalid.json"
invalid_path.write_text(json.dumps(invalid))
rc, refused = _run_cli(
@@ -249,7 +251,27 @@ def test_real_guard_capture_and_recovery_preserve_vision_before_effects(
"--decision-file",
str(invalid_path),
"--source-hash",
- build_turn_envelope(invalid)["action_signature"]["source_decision_hash"],
+ _source_digest(invalid),
+ "--replan-stage",
+ "review",
+ cwd=project,
+ )
+ assert rc != 0 and "Vision audit reference" in json.dumps(refused)
+ historical = {
+ key: value
+ for key, value in invalid.items()
+ if key not in {"schema_version", "contract_binding"}
+ }
+ invalid_path.write_text(json.dumps(historical))
+ rc, refused = _run_cli(
+ registry,
+ runtime,
+ "quota",
+ "replan-view",
+ "--decision-file",
+ str(invalid_path),
+ "--source-hash",
+ _source_digest(historical),
"--replan-stage",
"review",
cwd=project,
diff --git a/tests/control_plane/test_recovery_due_successor_identity.py b/tests/control_plane/test_recovery_due_successor_identity.py
index dcbf8f4987..4db317e572 100644
--- a/tests/control_plane/test_recovery_due_successor_identity.py
+++ b/tests/control_plane/test_recovery_due_successor_identity.py
@@ -229,11 +229,16 @@ def cli(*args: str) -> tuple[int, dict]:
assert _spend_run_count(runtime) == 0
before = state.read_bytes()
+ lease_proof = reentry["execution_preparation"]["lease_proof"]
refresh_args = (
"refresh-state",
"--goal-id",
GOAL_ID,
*old_binding,
+ "--task-lease-idempotency-key",
+ lease_proof["idempotency_key"],
+ "--task-lease-expected-version",
+ str(lease_proof["expected_version"]),
"--classification",
"local_stop_closeout",
"--delivery-batch-scale",
diff --git a/tests/control_plane/test_replan_authoring_owner.py b/tests/control_plane/test_replan_authoring_owner.py
index 748453b4b2..2a6dc9613a 100644
--- a/tests/control_plane/test_replan_authoring_owner.py
+++ b/tests/control_plane/test_replan_authoring_owner.py
@@ -201,11 +201,18 @@ def test_real_capture_authoring_read_does_not_repeat_work_or_spend(tmp_path, mon
"--decision-output-dir", str(capture), cwd=project)
assert code == 0, result
source = json.loads((capture / "decision.json").read_text())
- authoring = deepcopy(source["replan_action_packet"]["writeback_contract"]["vision_authoring"])
+ assert source["replan_action_packet"]["writeback_contract"]["vision_authoring_ref"] == (
+ "$.interaction_contract.cli_channel.settlement_plan.ordered_steps[1].vision_authoring"
+ )
+ authoring = deepcopy(source["interaction_contract"]["cli_channel"]["settlement_plan"]["ordered_steps"][1]["vision_authoring"])
authoring.pop("minimal_example", None)
shared = shared_packet(source)
before = state.read_bytes()
- run = cli_writer(project, shared, source_digest(shared))
+ run = cli_writer(
+ project,
+ shared,
+ build_turn_envelope(shared)["action_signature"]["source_decision_hash"],
+ )
assert run.returncode == 0, run.stdout + run.stderr
assert json.loads(run.stdout)["writeback_contract"]["vision_authoring"] == authoring
assert state.read_bytes() == before
diff --git a/tests/control_plane/test_replan_progress_native_cli.py b/tests/control_plane/test_replan_progress_native_cli.py
index 9ea2edb670..f7d0cdc046 100644
--- a/tests/control_plane/test_replan_progress_native_cli.py
+++ b/tests/control_plane/test_replan_progress_native_cli.py
@@ -208,13 +208,39 @@ def test_captured_replan_preserves_scoped_memory_without_trusting_reader_locatio
source = json.loads(captured)
assert source["replan_action_packet"]["decision"] == "replan_required"
assert source["reward_memory_recall"]["status"] == "applied" and memory.calls == 1
+ from loopx.capabilities.turn_context import attach_turn_context
+ from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+
projected = project_turn_context(source)
assert projected["reward_memory_recall"]["context"]["guidance"]
- relocated = {**source, "replan_read_path": str(tmp_path / "other-location.json")}
- assert project_turn_context(relocated) == projected
+ # Signed complete captures reject direct top-level edits, while Host plan
+ # carrier detail_ref relocation preserves exact capability binding.
+ assert project_turn_context({**source, "replan_read_path": str(tmp_path / "other-location.json")}) == {}
+ plan = build_loopx_turn_plan(source, host="codex-cli", execution_mode="isolated-headless")
+ attach_turn_context(
+ plan,
+ registry_path=registry,
+ goal_id=GOAL,
+ agent_id=AGENT,
+ decision=source,
+ turn_instance_id=TURN,
+ )
+ plan_projected = project_turn_context(plan)
+ assert plan_projected["reward_memory_recall"]["context"]["guidance"]
+ relocated = deepcopy(plan)
+ relocated["detail_ref"] = {
+ "captured_decision": {
+ "path": str(tmp_path / "other-location.json"),
+ "turn_key": plan["transaction"]["turn_key"],
+ "goal_id": GOAL,
+ "agent_id": AGENT,
+ "source_hash_ref": "$.source_decision_hash",
+ }
+ }
+ assert project_turn_context(relocated) == plan_projected
changed = deepcopy(relocated)
- plan = changed["interaction_contract"]["cli_channel"]["settlement_plan"]
- plan["ordered_steps"][1]["command_template"] += " --evidence substituted"
+ settlement = changed["work_contract"]["interaction_contract"]["cli_channel"]["settlement_plan"]
+ settlement["ordered_steps"][1]["command_template"] += " --evidence substituted"
assert project_turn_context(changed) == {}
finally:
restart_effect_runtime(wait_for_exit=True)
diff --git a/tests/control_plane/test_replan_saved_context.py b/tests/control_plane/test_replan_saved_context.py
index 7b73e58e57..00f68afaa3 100644
--- a/tests/control_plane/test_replan_saved_context.py
+++ b/tests/control_plane/test_replan_saved_context.py
@@ -73,12 +73,17 @@ def test_saved_replan_reads_keep_original_authority(tmp_path, monkeypatch, provi
"source": "goal_acceptance", "content": {"status": "loaded", "goal_acceptance_contract": distinct},
})
source["precision_fixture"] = {"exact_integer": 2**53 + 1, "exact_float": 1.0}
+ source.pop("schema_version", None)
+ source.pop("contract_binding", None)
capture = tmp_path / "precision-source.json"
reader_path = capture
write_decision_capture(capture, source)
if entry == "staged":
capture = tmp_path / "already-staged.json"
- write_decision_capture(capture, _turn_envelope_result("replan_cli_view", payload=source))
+ staged_view = _turn_envelope_result("replan_cli_view", payload=source)
+ staged_view.pop("schema_version", None)
+ staged_view.pop("contract_binding", None)
+ write_decision_capture(capture, staged_view)
selected_raw = capture.read_bytes()
def snapshot():
@@ -105,7 +110,7 @@ def snapshot():
from loopx.entrypoint import main
original = turn_envelope._turn_envelope_result
def fail(operation, **kwargs):
- if operation == 'context_delivery' and (kwargs.get('native_projection') or {}).get('captured_decision_path'):
+ if operation == 'context_delivery' and (kwargs.get('native_projection') or {}).get('captured_decision'):
marker.write_text('context_delivery')
raise EffectRuntimeStartupError('synthetic presentation outage', diagnostic_code='synthetic_fixture')
return original(operation, **kwargs)
diff --git a/tests/control_plane/test_replan_staged_read.py b/tests/control_plane/test_replan_staged_read.py
index 27e18eb74d..1a2851ef01 100644
--- a/tests/control_plane/test_replan_staged_read.py
+++ b/tests/control_plane/test_replan_staged_read.py
@@ -123,8 +123,10 @@ def test_pure_replan_does_not_advertise_unavailable_explore_attachment(tmp_path,
source = json.loads((capture / "decision.json").read_bytes())
original = explore_source(source)["content"]["graph"]
graph = explore_source(entry)["content"]["graph"]
+ raw_graph = call("explore", "turn-context", "--goal-id", goal, "--agent-id", agent)["graph"]
assert entry["heartbeat_receipt"]["settlement_identity"]["binding_kind"] == "autonomous_replan"
assert not entry.get("selected_todo")
+ assert graph == original
assert "result_attachment_template" not in graph and "result_writeback_read" not in graph
assert "unavailable for this pure replan" in graph["result_writeback_guidance"]
assert "Read measurement_feedback before repeating a measured route" in graph["result_writeback_guidance"]
@@ -132,16 +134,17 @@ def test_pure_replan_does_not_advertise_unavailable_explore_attachment(tmp_path,
assert "Capture is optional, not a settlement obligation." in graph["result_writeback_guidance"]
for key in ("writeback_results", "measurement_feedback", "result_page", "recent_findings",
"record_node_template", "record_finding_template", "guidance"):
- assert graph.get(key) == original.get(key)
- assert json_bytes(original) - json_bytes(graph) > 2000
- writer_run = subprocess.run(shlex.split(entry["replan_action_packet"]["writeback"]),
+ assert graph.get(key) == raw_graph.get(key)
+ assert json_bytes(raw_graph) - json_bytes(graph) > 2000
+ writer_run = subprocess.run(
+ shlex.split(_turn_envelope_result("replan_cli_view", payload=entry)["replan_action_packet"]["writeback"]),
capture_output=True, text=True, check=True)
writer = json.loads(writer_run.stdout)
assert all(not row.get("optional_attachments")
for row in writer["writeback"]["settlement_plan"]["ordered_steps"])
# Prove the real attachment owner rejects this binding; projection must
# not suggest a new way around it or change its primary commit behavior.
- attachment = {**original["result_attachment_template"], "question": "Is the source valid?",
+ attachment = {**raw_graph["result_attachment_template"], "question": "Is the source valid?",
"applicability": "Synthetic source", "input_revision": "source-v1", "observation": "Reviewed",
"interpretation": "Unresolved", "status": "tentative", "evidence_refs": ["evidence:source-review"]}
with pytest.raises(ValueError, match="requires agent, Todo and Turn identity"):
@@ -151,6 +154,7 @@ def test_pure_replan_does_not_advertise_unavailable_explore_attachment(tmp_path,
# identity keep the old payload rather than guessing applicability.
for mutation in ("capture", "custom", "identity", "attachment"):
changed = copy.deepcopy(source)
+ explore_source(changed)["content"]["graph"] = copy.deepcopy(raw_graph)
expected = explore_source(changed)["content"]
if mutation == "capture":
changed.pop("replan_read_path")
@@ -188,9 +192,23 @@ def assert_explore_entry_and_writer(source, entry, writer):
assert graph.get(key) == original.get(key)
step = next(row for row in writer["writeback"]["settlement_plan"]["ordered_steps"] if row["kind"] == "durable_writeback")
attachment = next(row for row in step["optional_attachments"] if row["capability_id"] == "explore")
- assert attachment["guidance"] == original["result_writeback_guidance"]
+ original_guidance = original.get("result_writeback_guidance")
+ if original_guidance is None and isinstance(original.get("result_writeback_guidance_ref"), str):
+ node = source
+ for part in original["result_writeback_guidance_ref"].removeprefix("$.").split("."):
+ if "[" in part and part.endswith("]"):
+ key, index = part[:-1].split("[", 1)
+ node = node[key][int(index)]
+ else:
+ node = node[part]
+ original_guidance = node
+ assert attachment["guidance"] == original_guidance
assert attachment["attachment_template"] == original["result_attachment_template"]
- assert json_bytes(original) - json_bytes(graph) > 2000
+ resolved_original = dict(original)
+ if original_guidance is not None and "result_writeback_guidance" not in resolved_original:
+ resolved_original.pop("result_writeback_guidance_ref", None)
+ resolved_original["result_writeback_guidance"] = original_guidance
+ assert json_bytes(resolved_original) - json_bytes(graph) > 2000
@pytest.mark.parametrize("provider", ["file", "sqlite"])
@@ -307,8 +325,9 @@ def observe(command, **kwargs):
assert "replan_action_packet" not in view
return
assert source["autonomous_replan_obligation"]["replan_context"]["context_id"]
- assert "replan_context" not in view["autonomous_replan_obligation"]
- packet = view["replan_action_packet"]
+ staged_view = view if context_delivery else _turn_envelope_result("replan_cli_view", payload=view)
+ assert "replan_context" not in staged_view["autonomous_replan_obligation"]
+ packet = staged_view["replan_action_packet"]
assert "planning_guidance" not in packet
capture = Path(source["replan_read_path"])
assert capture.read_bytes() == source_path.read_bytes()
@@ -320,7 +339,7 @@ def observe(command, **kwargs):
assert read["obligation_id"] == selector["obligation_id"]
assert read["stage"] == stage
if stage == "writeback":
- assert_explore_entry_and_writer(source, view, read)
+ assert_explore_entry_and_writer(source, staged_view, read)
assert {key: fixture[key].read_bytes() for key in before} == before
assert capture.read_bytes() == source_path.read_bytes()
assert len(commands) == 1 # Reads did not re-enter checkpoint preflight.
@@ -357,7 +376,9 @@ def invoke(argv):
"--context-delivery", mode, "--delta-delivery", "auto" if mode == "on" else "off",
"--decision-output-root", str(captures),
"--codex-context-dir", str(native), *(["--defer-context-presentation"] if mode == "on" else []))
- review_argv = shlex.split(entry["replan_action_packet"]["review"])
+ assert entry["interaction_contract"]["cli_channel"]["selection_required"] is True
+ assert not entry["interaction_contract"]["cli_channel"].get("settlement_plan")
+ review_argv = shlex.split(build_turn_envelope(entry)["replan_action_packet"]["review"])
review = json.loads(invoke(review_argv).stdout)
assert review["next"]["selection_required"] is True and "writeback" not in review["next"]
selection = entry
@@ -444,6 +465,9 @@ def test_real_guard_capture_read_and_writeback_without_another_guard(tmp_path, m
step = next(row for row in changed["interaction_contract"]["cli_channel"]["settlement_plan"]["ordered_steps"]
if row["kind"] == "durable_writeback")
attachment = next(row for row in step["optional_attachments"] if row["capability_id"] == "explore")
+ if "result_writeback_guidance_ref" in graph and mutation != "capture":
+ graph.pop("result_writeback_guidance_ref")
+ graph["result_writeback_guidance"] = attachment["guidance"]
if mutation == "custom":
graph["result_writeback_guidance"] += " Stop before a new operation."
attachment["guidance"] = graph["result_writeback_guidance"]
@@ -457,7 +481,7 @@ def test_real_guard_capture_read_and_writeback_without_another_guard(tmp_path, m
changed.pop("replan_read_path")
projected = _turn_envelope_result("replan_cli_view", payload=changed)
assert explore_source(projected)["content"]["graph"] == graph
- entry_writeback = guard["writeback"] if envelope else guard["interaction_contract"]["cli_channel"]
+ entry_writeback = guard["interaction_contract"]["cli_channel"]
assert "settlement_plan" not in entry_writeback and "next_cli_actions" not in entry_writeback
assert entry_writeback["settlement_identity"] == source["heartbeat_receipt"]["settlement_identity"]
original_files = {key: fixture[key].read_bytes() for key in ("registry", "state", "index")}
@@ -477,12 +501,7 @@ def read(stage, expected_code=0):
assert result.returncode == expected_code, (result.stdout, result.stderr)
return json.loads(result.stdout)
review, writer = read("review"), read("writeback")
- if not envelope:
- assert_explore_entry_and_writer(source, guard, writer)
- else:
- assert_explore_entry_and_writer(source, {
- "interaction_contract": {"agent_channel": {"work_context": guard["work_context"]}},
- "replan_action_packet": packet}, writer)
+ assert_explore_entry_and_writer(source, guard, writer)
assert writer["writeback"]["settlement_plan"] == source["interaction_contract"]["cli_channel"]["settlement_plan"]
assert writer["settlement_command"].endswith(" --settle-turn")
# Real CLI/default-off and native readers both deliver the original
@@ -495,7 +514,7 @@ def read(stage, expected_code=0):
assert guard.get("action_portfolio") == source.get("action_portfolio")
assert review["context_id"] == writer["context_id"]
assert review["obligation_id"] == source["replan_action_packet"]["obligation_id"]
- assert {k: v for k, v in writer["writeback_contract"].items() if k not in {"successor_binding_args", "successor_settlement"}} == without_example(source["replan_action_packet"]["writeback_contract"])
+ assert writer["writeback_contract"] == without_example(source["replan_action_packet"]["writeback_contract"])
assert (capture / "decision.json").read_bytes() == saved
assert {key: fixture[key].read_bytes() for key in original_files} == original_files
assert not reader_fixture["registry"].exists()
@@ -514,7 +533,7 @@ def read(stage, expected_code=0):
target[field] = value
(capture / "decision.json").write_text(json.dumps(source))
rejected = read("review", expected_code=1)
- assert "source hash" in rejected["reason"]
+ assert rejected["reason"] == "capture digest mismatch; restore the original capture, do not rerun admission"
(capture / "decision.json").write_bytes(saved)
# A valid next operation still reaches the existing writer and semantic gate.
result = call_progress_cli(fixture, *fixture["refresh"])
diff --git a/tests/control_plane/test_replan_successor_guard_reentry.py b/tests/control_plane/test_replan_successor_guard_reentry.py
index 48508cd15a..6922bccd5f 100644
--- a/tests/control_plane/test_replan_successor_guard_reentry.py
+++ b/tests/control_plane/test_replan_successor_guard_reentry.py
@@ -438,8 +438,9 @@ def test_successor_guard_returns_original_settlement_not_repeated_planning(
envelope = call("quota", "should-run", "--codex-app", "--goal-id", GOAL,
"--agent-id", AGENT, "--turn-instance-id", TURN, "--turn-envelope")
assert envelope["replan_action_packet"]["settlement_only"] is True
- assert envelope["writeback"]["next_cli_actions"] == actions
- assert envelope["action_signature"]["matches"] is True
+ assert envelope["interaction_contract"]["cli_channel"]["next_cli_actions"] == actions
+ assert envelope["contract_binding"]["source_decision_hash"]
+ assert envelope["contract_binding"]["content_hash"]
premature_spend = call(*shlex.split(actions[1])[1:], expected_code=1)
assert premature_spend.get("appended") is not True
assert not any(json.loads(line).get("classification") == "quota_slot_spent"
@@ -464,9 +465,10 @@ def test_successor_guard_returns_original_settlement_not_repeated_planning(
assert pending_plan["ordered_steps"] == [cli["settlement_plan"]["ordered_steps"][-1]]
pending_envelope = call("quota", "should-run", "--codex-app", "--goal-id", GOAL,
"--agent-id", AGENT, "--turn-instance-id", TURN, "--turn-envelope")
- assert pending_envelope["writeback"]["next_cli_actions"] == spend_actions
- assert pending_envelope["writeback"]["settlement_plan"] == pending_plan
- assert pending_envelope["action_signature"]["matches"] is True
+ assert pending_envelope["interaction_contract"]["cli_channel"]["next_cli_actions"] == spend_actions
+ assert pending_envelope["interaction_contract"]["cli_channel"]["settlement_plan"] == pending_plan
+ assert pending_envelope["contract_binding"]["source_decision_hash"]
+ assert pending_envelope["contract_binding"]["content_hash"]
spent = call(*shlex.split(spend_actions[0])[1:])
assert spent["settlement_progress"]["state"] == "settled"
assert spent["appended"] is True
diff --git a/tests/control_plane/test_research_checkpoint_capture.py b/tests/control_plane/test_research_checkpoint_capture.py
index fa7bc154fe..4aace9fa22 100644
--- a/tests/control_plane/test_research_checkpoint_capture.py
+++ b/tests/control_plane/test_research_checkpoint_capture.py
@@ -307,6 +307,9 @@ def test_real_cli_settlement_capture_verification_and_inert_materialization(
tmp_path, monkeypatch, opportunity, corrupt, collisions):
isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry = cli._write_fixture(tmp_path, provider="sqlite")
+ config = json.loads(registry.read_text())
+ config["goals"][0]["quota"]["allowed_slots"] = 4
+ registry.write_text(json.dumps(config))
binding = ("--agent-id", cli.AGENT_ID, "--todo-id", cli.TODO_ID, "--turn-instance-id", cli.TURN_ID)
def run(*args):
code, result = cli._run_cli(registry, runtime, *args)
@@ -439,6 +442,9 @@ def test_real_native_cursor_must_match_sealed_store_basis(tmp_path, monkeypatch,
isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry = cli._write_fixture(tmp_path, provider="sqlite")
+ config = json.loads(registry.read_text())
+ config["goals"][0]["quota"]["allowed_slots"] = 4
+ registry.write_text(json.dumps(config))
binding = ("--agent-id", cli.AGENT_ID, "--todo-id", cli.TODO_ID, "--turn-instance-id", cli.TURN_ID)
def run(*args):
code, result = cli._run_cli(registry, runtime, *args, cwd=project)
@@ -591,6 +597,9 @@ def test_real_pre_replan_capture_is_before_decision_and_same_node_is_not_recaptu
isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry = cli._write_fixture(tmp_path, provider="sqlite")
+ config = json.loads(registry.read_text())
+ config["goals"][0]["quota"]["allowed_slots"] = 4
+ registry.write_text(json.dumps(config))
source_turn_id = "wake-100-prior"
binding = ("--agent-id", cli.AGENT_ID, "--todo-id", cli.TODO_ID, "--turn-instance-id", source_turn_id)
@@ -741,7 +750,10 @@ def run(*args):
'observed_reality':'Isolated source work was verified.', 'retained':['Participant capture'],
'evidence_refs':['evidence:controller-fixture']}}))
run('refresh-state', '--goal-id', cli.GOAL_ID, *binding, '--classification','controller_fixture_verified',
- '--delivery-batch-scale','multi_surface','--delivery-outcome','outcome_progress','--agent-vision-json',str(vision))
+ '--delivery-batch-scale','multi_surface','--delivery-outcome','outcome_progress',
+ '--task-lease-idempotency-key', lease['lease']['idempotency_key'],
+ '--task-lease-expected-version', str(lease['lease']['version']),
+ '--agent-vision-json',str(vision))
run('quota','spend-slot','--goal-id',cli.GOAL_ID,*binding,'--source','heartbeat','--execute')
before = run('todo','list','--goal-id',cli.GOAL_ID,'--todo-id',cli.TODO_ID)
from loopx.capabilities.explore.result_log import (
@@ -904,6 +916,9 @@ def test_failed_replan_index_cannot_relabel_later_work_after_permissions_recover
pytest.skip("Real POSIX permission denial requires an unprivileged test user")
isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry = cli._write_fixture(tmp_path, provider="sqlite")
+ config = json.loads(registry.read_text())
+ config["goals"][0]["quota"]["allowed_slots"] = 4
+ registry.write_text(json.dumps(config))
skills = tmp_path / "skills"
skills.mkdir()
binary = tmp_path / "codex"
diff --git a/tests/control_plane/test_research_checkpoint_cli.py b/tests/control_plane/test_research_checkpoint_cli.py
index cc0cefae20..21a51cd346 100644
--- a/tests/control_plane/test_research_checkpoint_cli.py
+++ b/tests/control_plane/test_research_checkpoint_cli.py
@@ -41,8 +41,8 @@ def cli(*args, ok=True):
todo = cli('todo', 'add', '--goal-id', 'source', '--role', 'agent', '--claimed-by', 'researcher',
'--task-class', 'advancement_task', '--text', 'Retain rejected candidate evidence.')['todo_id']
actor = ['--goal-id', 'source', '--agent-id', 'researcher', '--todo-id', todo, '--turn-instance-id', 'captured-work']
+ lease = cli('task-lease', 'acquire', '--goal-id', 'source', '--todo-id', todo, '--owner', 'researcher', '--idempotency-key', 'source-work')['lease']
cli('quota', 'should-run', *actor, '--runtime-profile', 'generic_cli', '--available-capability', 'shell')
- cli('task-lease', 'acquire', '--goal-id', 'source', '--todo-id', todo, '--owner', 'researcher', '--idempotency-key', 'source-work')
code = workspace / 'candidate.txt'
code.write_text('7')
from loopx.capabilities.explore.result_log import (
@@ -58,6 +58,7 @@ def cli(*args, ok=True):
vision.write_text(json.dumps({'schema_version': 'goal_vision_replan_contract_v0', 'state': 'vision_patch_proposed',
'vision_patch': {'acceptance_summary': 'Preserve rejected candidates and continue complete experiments.'}}))
cli('refresh-state', *actor, '--classification', 'captured_research', '--delivery-outcome', 'outcome_progress',
+ '--task-lease-idempotency-key', lease['idempotency_key'], '--task-lease-expected-version', str(lease['version']),
'--agent-vision-json', str(vision), '--no-global-sync', '--suppress-external-sinks')
cli('quota', 'spend-slot', *actor, '--slots', '1', '--source', 'heartbeat', '--execute')
pending = {'command': ''}
diff --git a/tests/control_plane/test_research_context_adoption.py b/tests/control_plane/test_research_context_adoption.py
index 0e94e254a1..d6f1382c3d 100644
--- a/tests/control_plane/test_research_context_adoption.py
+++ b/tests/control_plane/test_research_context_adoption.py
@@ -268,13 +268,15 @@ def _ordinary_review_and_work(run, workspace, tmp_path, todo, digest, monkeypatc
run('configure-goal', '--goal-id', 'branch', '--execution-replan-after-turns', '1',
'--execution-stride-strategy', 'evidence_conditioned_v1', '--execute')
actor = ['--goal-id', 'branch', '--agent-id', 'branch-actor']
- def settle(turn, vision, *, review=False):
+ def settle(turn, vision, *, review=False, lease_proof=None):
path = tmp_path / (turn + '.json')
path.write_text(json.dumps(vision))
+ proof_args = (['--task-lease-idempotency-key', lease_proof['idempotency_key'],
+ '--task-lease-expected-version', str(lease_proof['expected_version'])] if lease_proof else [])
written = run('refresh-state', *actor, '--todo-id', todo, '--turn-instance-id', turn,
'--classification', 'ordinary_checkpoint_research', '--delivery-batch-scale', 'single_surface',
'--delivery-outcome', 'outcome_progress', '--delivery-boundary', 'semantic_closeout' if review else 'in_flight_continuation',
- '--agent-vision-json', str(path), '--no-global-sync', '--suppress-external-sinks')
+ '--agent-vision-json', str(path), *proof_args, '--no-global-sync', '--suppress-external-sinks')
run('quota', 'spend-slot', *actor, '--todo-id', todo, '--turn-instance-id', turn,
'--slots', '1', '--source', 'heartbeat', '--execute')
return written
@@ -286,7 +288,8 @@ def settle(turn, vision, *, review=False):
initial = run('quota', 'should-run', *actor, '--todo-id', todo, '--turn-instance-id', 'ordinary-preparation',
'--runtime-profile', 'generic_cli', '--context-delivery', 'off', '--available-capability', 'shell')
assert initial['interaction_contract']['agent_channel']['delivery_allowed']
- settle('ordinary-preparation', vision)
+ initial_proof = initial['execution_preparation']['lease_proof']
+ settle('ordinary-preparation', vision, lease_proof=initial_proof)
review = run('quota', 'should-run', *actor, '--turn-instance-id', 'ordinary-review',
'--runtime-profile', 'generic_cli', '--context-delivery', 'off', '--available-capability', 'shell')
assert review['interaction_contract']['mode'] == 'autonomous_replan'
@@ -312,7 +315,7 @@ def settle(turn, vision, *, review=False):
revised['stride_decision'] = decision
revised['path_delta'].update(outcome='replan', observed_reality='Current review read the source-bound refuted finding.',
changed=['Complete candidates 8 and 9 together, keeping the rejected route excluded.'])
- reviewed = settle('ordinary-review', revised, review=True)
+ reviewed = settle('ordinary-review', revised, review=True, lease_proof=(review.get('execution_preparation') or {}).get('lease_proof') or initial_proof)
from loopx.history import load_index_snapshot
indexed = load_index_snapshot(Path(reviewed['index_path']), include_artifact_status=False).records
accepted = next(row for row in indexed if row.get('turn_instance_id') == 'ordinary-review')
@@ -334,7 +337,7 @@ def settle(turn, vision, *, review=False):
# Settlement proves actual current work can continue under the original lease.
revised.pop('stride_decision')
revised['path_delta'].update(outcome='continue', observed_reality='Candidates 8 and 9 were completed in one ordinary work unit.')
- settle('ordinary-experiment', revised)
+ settle('ordinary-experiment', revised, lease_proof=following['execution_preparation']['lease_proof'])
def test_research_history_is_complete_and_bound_to_original_source(tmp_path, monkeypatch, capsys):
diff --git a/tests/control_plane/test_scheduler_interaction_arbitration.py b/tests/control_plane/test_scheduler_interaction_arbitration.py
index 0f2e944eb3..2151cc04a5 100644
--- a/tests/control_plane/test_scheduler_interaction_arbitration.py
+++ b/tests/control_plane/test_scheduler_interaction_arbitration.py
@@ -659,6 +659,7 @@ def test_primary_instruction_adapter_preserves_pinned_python_views():
if name in {f"precedence-{i}" for i in range(8)} else
[{"goal_id": "other", "label": "Unscoped backlog"}] if name == "candidate-edge-9" else []
)
+ expected.setdefault("workspace_repair_action", None)
assert primary_action_instruction_facts(case["payload"]) == expected, name
diff --git a/tests/control_plane/test_selected_work_requirements.py b/tests/control_plane/test_selected_work_requirements.py
index bdaa1551ea..3c73c83527 100644
--- a/tests/control_plane/test_selected_work_requirements.py
+++ b/tests/control_plane/test_selected_work_requirements.py
@@ -147,7 +147,7 @@ def old_delivery(options):
assert packet["scoped_user_gate_fallback"]["selected_executable"]["todo_id"] == "todo_inspect"
assert channel["work_context"]["complete"] is True
envelope = guard("--turn-envelope")
- assert envelope["work_context"]["user_todos"] == channel["work_context"]["user_todos"]
+ assert envelope["interaction_contract"]["agent_channel"]["work_context"]["user_todos"] == channel["work_context"]["user_todos"]
assert state.read_text() == state_text
# Canonical tasks still exist: the full Goal source must fail closed,
# rather than silently substituting a Todo summary for missing intent.
@@ -231,8 +231,7 @@ def guard(*extra: str) -> dict:
full = json.loads(result.stdout)
assert full["should_run"] is True, full.get("reason")
channel = full["interaction_contract"]["agent_channel"]
- assert [read["source"] for read in channel["required_reads"]] == ["goal_state"]
- goal_read = channel["required_reads"][0]
+ assert channel["required_reads"] == []
assert "required_reads" not in full["interaction_contract"]["cli_channel"]
context = channel["work_context"]
assert context["complete"] and not context.get("failures", [])
@@ -240,8 +239,10 @@ def guard(*extra: str) -> dict:
assert "unknown, not unchanged" in context["instruction"]
assert "summaries cannot replace full requirements" in context["instruction"]
reads = context["sources"]
- assert [read["source"] for read in reads] == ["selected_todo"]
- assert not any(read["source"] == "goal_state" for read in reads)
+ assert [read["source"] for read in reads] == ["goal_state", "selected_todo"]
+ goal_read = next(read for read in reads if read["source"] == "goal_state")
+ assert goal_requirements in goal_read["content"]["text"]
+ assert "Stop before an unauthorized deployment." in goal_read["content"]["text"]
goal_result = subprocess.run(shlex.split(goal_read["command"]), capture_output=True, text=True, check=True)
assert goal_requirements in goal_result.stdout
assert "Stop before an unauthorized deployment." in goal_result.stdout
@@ -250,15 +251,11 @@ def guard(*extra: str) -> dict:
assert "_context_text_sha256" not in full["selected_todo"]
assert reads[-1]["content"]["todo"]["text"] == expected
assert json.dumps(context).count(expected) == 1
- assert envelope["required_reads"] == channel["required_reads"]
+ env_channel = envelope["interaction_contract"]["agent_channel"]
+ assert env_channel["required_reads"] == channel["required_reads"]
assert context["selected_todo_text_verified"] is True
- assert envelope["work_context"] == {
- key: value
- for key, value in context.items()
- if key != "selected_todo_text_verified"
- }
- assert envelope["action"]["selected_todo"]["text_source_verified"] is True
- selected = envelope["action"]["selected_todo"]
+ assert env_channel["work_context"] == context
+ selected = envelope["selected_todo"]
assert selected["todo_id"] == "todo_cursor_work"
read = reads[-1]
tokens = shlex.split(read["command"])
@@ -278,7 +275,8 @@ def read_detail() -> dict:
assert not {"todos", "agent_todos", "user_todos"}.intersection(detail)
assert json.dumps(detail).count(expected) == 1
assert detail["todo"]["claimed_by"] == "agent-a"
- assert envelope["action_signature"]["matches"] is True
+ assert envelope["contract_binding"]["source_decision_hash"]
+ assert envelope["contract_binding"]["content_hash"]
assert state.read_bytes() == before
plan = build_loopx_turn_plan(envelope, host="codex-cli", execution_mode="isolated-headless",
@@ -287,10 +285,11 @@ def read_detail() -> dict:
assert read["command"] in _prompt(request)
assert "work_context" in _prompt(request)
authority = extract_turn_authority(request)
+ auth_channel = authority["interaction_contract"]["agent_channel"]
assert authority["selected_todo"]["todo_id"] == "todo_cursor_work"
- assert authority["required_reads"] == channel["required_reads"]
- assert authority["work_context"] == envelope["work_context"]
- assert authority["selected_todo"]["text_source_verified"] is True
+ assert auth_channel["required_reads"] == channel["required_reads"]
+ assert auth_channel["work_context"] == env_channel["work_context"]
+ assert auth_channel["work_context"]["selected_todo_text_verified"] is True
assert read["command"] in render_prompt(authority)
# A fresh CLI process must recover current requirements from the same owner,
@@ -461,7 +460,8 @@ def test_short_selected_todo_deduplicates_body_but_keeps_canonical_note(
context = channel["work_context"]
assert context["complete"] is True
assert context["selected_todo_ref"] == "selected_todo"
- assert [read["source"] for read in channel["required_reads"]] == ["goal_state"]
+ assert channel["required_reads"] == []
+ assert [read["source"] for read in context["sources"]] == ["goal_state", "selected_todo"]
selected_source = next(source for source in context["sources"] if source["source"] == "selected_todo")
source_todo = selected_source["content"]["todo"]
assert source_todo.get("text") is None
@@ -473,7 +473,7 @@ def test_short_selected_todo_deduplicates_body_but_keeps_canonical_note(
"--agent-id", "agent-a", "--turn-envelope",
registry_path=registry, runtime_root=runtime)
assert code == 0 and turn["ok"], turn
- turn_context = turn["work_context"]
+ turn_context = turn["interaction_contract"]["agent_channel"]["work_context"]
assert turn_context["complete"] is True
assert turn_context["selected_todo_ref"] == "selected_todo"
turn_source = next(source for source in turn_context["sources"] if source["source"] == "selected_todo")
diff --git a/tests/control_plane/test_settlement_input_discovery_cli.py b/tests/control_plane/test_settlement_input_discovery_cli.py
index c2c2565dfe..1dd816819e 100644
--- a/tests/control_plane/test_settlement_input_discovery_cli.py
+++ b/tests/control_plane/test_settlement_input_discovery_cli.py
@@ -97,7 +97,9 @@ def run(*args):
assert build_turn_envelope(guard)["writeback"]["settlement_plan"] == plan
rc, premature = run("checkpoint-context", *binding)
assert rc != 0 and "original committed Turn writeback" in premature["error"]
- rc, no_writeback = _run_generated_cli(plan["ordered_steps"][2]["command_template"], registry_path=registry)
+ assert plan["ordered_steps"][2]["owner"] == "runtime"
+ assert "command_template" not in plan["ordered_steps"][2]
+ rc, no_writeback = run("quota", "spend-slot", *binding, "--slots", "1", "--source", "heartbeat", "--execute")
assert rc != 0 and _spend_run_count(runtime) == 0, no_writeback
if not in_flight:
@@ -124,12 +126,14 @@ def run(*args):
rc, rejected = _run_generated_cli(refresh, registry_path=registry)
assert rc != 0 and _spend_run_count(runtime) == 0, rejected
path.write_text(json.dumps(vision))
- rc, written = _run_generated_cli(refresh, registry_path=registry)
- assert rc == 0 and written["vision_checkpoint"]["satisfied"], written
- assert written["settlement_identity"] == plan["identity"]
for replay in (False, True):
- rc, spent = _run_generated_cli(written["settlement_owed"]["command"], registry_path=registry)
- assert rc == 0 and spent["appended"] is not replay, spent
+ rc, written = _run_generated_cli(refresh, registry_path=registry)
+ assert rc == 0 and written["settlement_progress"]["state"] == "settled", written
+ assert written["settlement_identity"] == plan["identity"]
+ assert written["settlement_result"]["ok"] is True
+ assert written.get("idempotent_replay", False) is replay
+ if not replay:
+ assert written["vision_checkpoint"]["satisfied"], written
assert _spend_run_count(runtime) == 1
rc, final_todo = _run_generated_cli(readback, registry_path=registry)
assert rc == 0 and final_todo["todo"]["status"] == ("open" if in_flight else "done")
@@ -211,6 +215,7 @@ def run(*args):
@pytest.mark.parametrize("provider", ["file", "sqlite"])
def test_replan_authoring_example_links_actual_subsequent_work(tmp_path, monkeypatch, provider):
"""A caller following the delivered example must retain a real work link."""
+ monkeypatch.delenv("LOOPX_CONTEXT_DELIVERY", raising=False)
isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry = _write_fixture(tmp_path)
# Fund all three admitted Turns before execution; the shared fixture's
@@ -274,8 +279,11 @@ def run(*args):
args += ["--no-global-sync", "--suppress-external-sinks"]
rc, written = run(*args)
assert rc == 0, written
- rc, spent = _run_generated_cli(written["settlement_owed"]["command"], registry_path=registry)
- assert rc == 0, spent
+ if written.get("settlement_progress", {}).get("state") == "settled":
+ assert written["settlement_result"]["ok"] is True
+ else:
+ rc, spent = _run_generated_cli(written["settlement_owed"]["command"], registry_path=registry)
+ assert rc == 0, spent
# Read the real immutable CLI history, not a hand-authored decision fixture.
status = {"registry": str(registry), "runtime_root": str(runtime), "run_history": {"runs": []}}
diff --git a/tests/control_plane/test_sqlite_archive_and_registry_health.py b/tests/control_plane/test_sqlite_archive_and_registry_health.py
index 0acea3f6b4..5f9c162ab3 100644
--- a/tests/control_plane/test_sqlite_archive_and_registry_health.py
+++ b/tests/control_plane/test_sqlite_archive_and_registry_health.py
@@ -482,8 +482,14 @@ def test_multi_goal_source_registry_sync_and_health_prevent_sibling_false_positi
)
# Modify goal-synced and goal-drifted in source_registry, then sync ONLY goal-synced
+ from loopx.control_plane.goals.activation import build_goal_activation
goals_in_source[0] = {**goals_in_source[0], "domain": "updated-domain"}
- goals_in_source[2] = {**goals_in_source[2], "status": "paused"}
+ goals_in_source[2] = {
+ **goals_in_source[2],
+ "activation": build_goal_activation(
+ state="stopped", updated_at="2026-01-01T00:00:01+00:00", reason="fixture drifted",
+ ),
+ }
source_registry = {
"schema_version": 1,
"common_runtime_root": str(runtime),
diff --git a/tests/control_plane/test_sqlite_authority_cli.py b/tests/control_plane/test_sqlite_authority_cli.py
index 230d97a0ae..65036d20f0 100644
--- a/tests/control_plane/test_sqlite_authority_cli.py
+++ b/tests/control_plane/test_sqlite_authority_cli.py
@@ -216,7 +216,7 @@ def todos():
"--update-operation-id", "trial-update", *proof]
assert command(*update)["source_authority"] == "sqlite_v0"
refresh_args = ["refresh-state", "--goal-id", goal, "--agent-id", agent,
- "--todo-id", todo, "--turn-instance-id", turn,
+ "--todo-id", todo, "--turn-instance-id", turn, *proof,
"--classification", "validated_progress", "--delivery-batch-scale", "test_only",
"--delivery-outcome", "outcome_progress", "--no-global-sync", "--suppress-external-sinks",
"--vision-state", "vision_on_track", "--vision-summary", "Continue reversible qualification.",
@@ -355,12 +355,19 @@ def listed(*archived_ids):
guard = ["quota", "should-run", "--context-delivery", "off", "--goal-id", goal, "--agent-id", agent,
"--todo-id", todo, "--codex-app", "--turn-instance-id", turn,
"--available-capability", "filesystem_read", ]
- assert command(*guard)["normal_delivery_allowed"] is True
+ guarded = command(*guard)
+ assert guarded["normal_delivery_allowed"] is True
+ lease_proof = guarded["execution_preparation"]["lease_proof"]
+ lease_args = [
+ "--task-lease-idempotency-key", lease_proof["idempotency_key"],
+ "--task-lease-expected-version", str(lease_proof["expected_version"]),
+ ]
update = ["todo", "update", "--goal-id", goal, "--todo-id", todo, "--agent-id", agent,
"--note", "Keep the latest note 完整 🧭", "--update-operation-id", "ordinary-update", *proof]
command(*update)
refreshed = command("refresh-state", "--goal-id", goal, "--agent-id", agent,
- "--todo-id", todo, "--turn-instance-id", turn, "--classification", "validated_progress",
+ "--todo-id", todo, "--turn-instance-id", turn, *lease_args,
+ "--classification", "validated_progress",
"--delivery-batch-scale", "test_only", "--delivery-outcome", "outcome_progress",
"--no-global-sync", "--suppress-external-sinks", "--vision-state", "vision_on_track",
"--vision-summary", "Continue the ordinary lifecycle.",
diff --git a/tests/control_plane/test_start_goal_compact_projection.py b/tests/control_plane/test_start_goal_compact_projection.py
index 972aed762d..4059b4563f 100644
--- a/tests/control_plane/test_start_goal_compact_projection.py
+++ b/tests/control_plane/test_start_goal_compact_projection.py
@@ -2067,6 +2067,13 @@ def test_projection_preserves_multi_goal_selection_actions(tmp_path: Path) -> No
"inspect_connection",
"select_goal",
]
+ assert compact["command_pack"]["host_loop_activation"] == {
+ "activation_state": "goal_selection_required",
+ "activation_allowed": False,
+ "activation_required_after_todo_write": False,
+ "host_surface": None,
+ "activation_method": None,
+ }
assert _host_shadow_document(compact) == _host_shadow_document(detailed)
for choice in compact["goal_selection_gate"]["choices"]:
assert "--capability-route issue-fix" in choice["rerun_command"]
diff --git a/tests/control_plane/test_todo_list_thin.py b/tests/control_plane/test_todo_list_thin.py
index 22e04616f6..1a99bce1e2 100644
--- a/tests/control_plane/test_todo_list_thin.py
+++ b/tests/control_plane/test_todo_list_thin.py
@@ -588,4 +588,4 @@ def test_cli_rejects_thin_outside_todo_list(tmp_path: Path) -> None:
assert result.returncode == 1, result.stdout
payload = json.loads(result.stdout)
- assert payload["error"] == "--thin is supported only by todo list"
+ assert payload["error"] == "--thin is supported only by todo list, todo receipt, todo claim"
diff --git a/tests/control_plane/test_turn_capability_lifecycle.py b/tests/control_plane/test_turn_capability_lifecycle.py
index 41f599c997..94fdad3cbf 100644
--- a/tests/control_plane/test_turn_capability_lifecycle.py
+++ b/tests/control_plane/test_turn_capability_lifecycle.py
@@ -228,7 +228,7 @@ def test_optional_recall_size_never_disables_enabled_ingest(
== 2400
)
assert any(
- "Set reward_memory_reflection_json" in item
+ "reward_memory_reflection_json" in item
for item in result["instructions"]
)
delivered = result["context_fields"]["reward_memory_recall"]
diff --git a/tests/control_plane/test_turn_host_context_delivery.py b/tests/control_plane/test_turn_host_context_delivery.py
index c71b6a9299..9670975ea1 100644
--- a/tests/control_plane/test_turn_host_context_delivery.py
+++ b/tests/control_plane/test_turn_host_context_delivery.py
@@ -14,7 +14,7 @@
import loopx
from loopx.control_plane.quota.turn_envelope import (
- build_turn_envelope, compose_turn_decision, turn_envelope_action_signature_document,
+ build_turn_envelope, turn_envelope_action_signature_document,
)
from loopx.control_plane.turn_driver.decision_capture import prepare_turn_decision_capture
from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
@@ -22,6 +22,7 @@
from loopx.control_plane.turn_driver.host_candidate import _canonical_hash
from loopx.control_plane.turn_driver import codex_context
from tests.control_plane.canonical_authority_fixture import isolate_sqlite_runtime
+from tests.control_plane.host_process_fixture import assert_host_task_context
from tests.control_plane.test_agent_context import POLICY
from tests.control_plane.test_quota_settlement_cli import (
AGENT_ID, GOAL_ID, TODO_ID, SELECTED_REPLAN_TODO_ID,
@@ -76,20 +77,26 @@ def test_captured_context_reaches_actual_host_without_readmission(tmp_path, monk
assert produced.returncode == 0, produced.stdout + produced.stderr
original = (capture / "decision.json").read_bytes()
source = json.loads(original)
- if mode == "legacy_reference":
- assert json.loads(produced.stdout)["schema_version"] == "loopx_turn_envelope_v0"
- envelope = build_turn_envelope(source, captured_decision_path=str(capture / "decision.json"))
+ assert source["schema_version"] == "loopx_turn_envelope_v1"
context = source["interaction_contract"].get("agent_context")
assert bool(context) is (mode != "off")
- if not context:
- assert "agent_context" not in envelope
if mode == "legacy_reference":
+ assert json.loads(produced.stdout) == source
+ # The historical decoder receives an explicit adapter over the original
+ # source and its recorded host binding.
+ envelope = build_turn_envelope(source, captured_decision_path=str(capture / "decision.json"))
envelope["agent_context"] = {
**{k: context[k] for k in ("schema_version", "phase", "scope", "target", "authority", "delivery")},
"content_hash": _canonical_hash(context),
"detail_ref": "full_decision.interaction_contract.agent_context",
"instruction": "Read original capability context before planning.",
}
+ goal_source = next(row for row in source["interaction_contract"]["agent_channel"]["work_context"]["sources"]
+ if row["source"] == "goal_state")
+ envelope["required_reads"] = [{
+ **{key: goal_source[key] for key in ("source", "ordering", "command")},
+ "reason": "Read the original Goal file before work.",
+ }]
digest = _canonical_hash(turn_envelope_action_signature_document(envelope))
envelope["action_signature"].update(source_hash=digest, envelope_hash=digest)
@@ -102,12 +109,10 @@ def test_captured_context_reaches_actual_host_without_readmission(tmp_path, monk
)
command = [sys.executable, "-m", "loopx.dsh_goal_mode", "--workspace", str(project),
"--dsh-home", str(tmp_path / "host-home"), "--dsh-runner", str(runner)]
- # Ordinary quota still emits v0 in this extraction. Its recorded envelope
- # must not be paired with a newly normalized source. Current Turn planning
- # accepts that complete source directly and publishes its own capture.
- plan = build_loopx_turn_plan(envelope if mode == "legacy_reference" else compose_turn_decision(source),
+ plan = build_loopx_turn_plan(envelope if mode == "legacy_reference" else source,
host="dsh", execution_mode="isolated-headless", turn_instance_id="host-context-turn")
if mode != "legacy_reference":
+ assert plan["work_contract"] == source
plan = prepare_turn_decision_capture(plan, journal_path=tmp_path / "current-turn" / "journal.json",
decision=plan["work_contract"], existing_journal=False)
request = build_loopx_turn_host_request(plan)
@@ -124,6 +129,10 @@ def test_captured_context_reaches_actual_host_without_readmission(tmp_path, monk
assert authority["action_signature"] == envelope["action_signature"]
assert authority["required_reads"] == envelope["required_reads"]
assert authority["required_reads"]
+ if os.name != "nt":
+ read_goal = subprocess.run(shlex.split(authority["required_reads"][0]["command"]),
+ capture_output=True, check=True)
+ assert read_goal.stdout == state.read_bytes()
assert authority["detail_ref"] == envelope["detail_ref"]
detail = authority["detail_ref"]
read = subprocess.run(shlex.split(detail["full_decision"]), capture_output=True, check=True).stdout
@@ -147,10 +156,9 @@ def test_captured_context_reaches_actual_host_without_readmission(tmp_path, monk
detail = actual["detail_ref"]["captured_decision"]
delivered_work = json.loads(Path(detail["path"]).read_text())
assert delivered_work == plan["work_contract"]
+ model_context = assert_host_task_context(actual, delivered_work)
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
+ assert "Validate the complete acceptance: " + "preserve original evidence; " * 180 in json.dumps(model_context)
delivered_context = delivered_work["interaction_contract"].get("agent_context")
assert delivered_context == context
if context:
@@ -197,19 +205,19 @@ def test_bound_replan_reaches_actual_host_with_original_evidence(tmp_path, monke
assert code == 0 and initial["decision"] == "autonomous_replan_required", initial
_bind_selected_replan_guard(registry, runtime, project, turn)
capture = tmp_path / "capture"
- code, envelope = _run_cli(registry, runtime, "quota", "should-run",
+ code, emitted = _run_cli(registry, runtime, "quota", "should-run",
"--goal-id", GOAL_ID, "--agent-id", AGENT_ID,
"--todo-id", SELECTED_REPLAN_TODO_ID, "--turn-instance-id", turn,
"--codex-app", "--turn-envelope", "--decision-output-dir", str(capture), cwd=project)
- assert code == 0, envelope
+ assert code == 0, emitted
original = (capture / "decision.json").read_bytes()
source = json.loads(original)
- assert envelope["schema_version"] == "loopx_turn_envelope_v0"
+ assert emitted == source and source["schema_version"] == "loopx_turn_envelope_v1"
agent_channel = source["interaction_contract"]["agent_channel"]
assert agent_channel["delivery_allowed"] is False
assert agent_channel["must_attempt"] is True
- plan = build_loopx_turn_plan(compose_turn_decision(source), host="dsh", execution_mode="isolated-headless",
- turn_instance_id=turn)
+ plan = build_loopx_turn_plan(source, host="dsh", execution_mode="isolated-headless", turn_instance_id=turn)
+ assert plan["work_contract"] == source
plan = prepare_turn_decision_capture(plan, journal_path=tmp_path / "turn" / "journal.json",
decision=plan["work_contract"], existing_journal=False)
assert plan["route"]["kind"] == "replan_required"
@@ -236,11 +244,18 @@ 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["schema_version"] == "loopx_turn_model_context_v0"
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
+ assert not {"work_contract", "turn_envelope", "turn_authority"}.intersection(actual)
+ model_context = actual["task_context"]
+ model_replan = model_context["replan_action_packet"]
+ original_replan = plan["work_contract"]["replan_action_packet"]
+ for key in ("obligation_id", "planning_guidance", "uncovered_frontier"):
+ assert model_replan[key] == original_replan[key]
+ original_plan = source["interaction_contract"]["cli_channel"]["settlement_plan"]
+ authoring = next(step["vision_authoring"] for step in original_plan["ordered_steps"]
+ if step["kind"] == "durable_writeback")
+ assert model_replan["writeback_contract"]["vision_authoring"] == authoring
# Execute the original observation readers actually delivered to the provider.
detail = actual["detail_ref"]
packet = detail["replan_action_packet"]
@@ -250,12 +265,16 @@ def test_bound_replan_reaches_actual_host_with_original_evidence(tmp_path, monke
captured = detail["captured_decision"]
assert (captured["goal_id"], captured["agent_id"], captured["turn_instance_id"]) == (GOAL_ID, AGENT_ID, turn)
read = subprocess.run(shlex.split(detail["full_decision"]), capture_output=True, check=True).stdout
- assert json.loads(read) == plan["work_contract"]
- assert build_turn_envelope(json.loads(read))["action_signature"]["source_decision_hash"] == actual["source_decision_hash"]
+ recovered = json.loads(read)
+ assert recovered == plan["work_contract"]
+ assert recovered["contract_binding"]["content_hash"] == actual["source_decision_hash"]
assert context["obligation_id"] == source["replan_action_packet"]["obligation_id"]
- goal_read = next(r for r in agent_channel["required_reads"] if r["source"] == "goal_state")
- delivered_goal = subprocess.run(shlex.split(goal_read["command"]), capture_output=True, check=True).stdout
- assert delivered_goal == goal_bytes and stop.encode() in delivered_goal
+ delivered = model_context["interaction_contract"]["agent_channel"]
+ goal_source = next(row["content"] for row in delivered["work_context"]["sources"]
+ if row["source"] == "goal_state")
+ assert goal_source["full_source"] is True
+ assert goal_source["text"].encode() == goal_bytes and stop in goal_source["text"]
+ assert not any(row["source"] == "goal_state" for row in delivered["required_reads"])
assert "Read detail_ref.replan_action_packet.review" in prompt
assert "before reading detail_ref.replan_action_packet.writeback" in prompt
assert "Never re-enter quota admission" in prompt
diff --git a/tests/control_plane/test_turn_todoless_settlement.py b/tests/control_plane/test_turn_todoless_settlement.py
index 44e37c8e3e..5a873b3f78 100644
--- a/tests/control_plane/test_turn_todoless_settlement.py
+++ b/tests/control_plane/test_turn_todoless_settlement.py
@@ -23,7 +23,7 @@
)
-def _fixture(tmp_path, monkeypatch, provider, *, replan=True):
+def _fixture(tmp_path, monkeypatch, provider, *, replan=True, capture_dir=None):
isolate_sqlite_runtime(tmp_path, monkeypatch)
project, runtime, registry = _write_fixture(tmp_path)
if replan:
@@ -47,9 +47,10 @@ def _fixture(tmp_path, monkeypatch, provider, *, replan=True):
assert code == 0, listed
original = listed["todos"]
turn = "empty-frontier-planning-turn"
+ capture_args = ("--decision-output-dir", str(capture_dir)) if capture_dir is not None else ()
code, guard = _run_cli(registry, runtime, "quota", "should-run", "--codex-app",
"--goal-id", GOAL_ID, "--agent-id", AGENT_ID, "--turn-instance-id", turn,
- cwd=project)
+ *capture_args, cwd=project)
assert code == 0 and guard["decision"] == ("autonomous_replan_required" if replan else "run"), guard
assert guard.get("selected_todo") is None
return project, runtime, registry, turn, guard, original
@@ -183,9 +184,21 @@ def test_bound_todoless_planning_executes_and_settles_once(tmp_path, monkeypatch
@pytest.mark.parametrize("provider", ["file", "sqlite"])
@pytest.mark.parametrize("captured", [False, True])
def test_no_todo_requires_the_original_signed_scoped_binding(tmp_path, monkeypatch, provider, captured):
- _project, runtime, _registry, turn, guard, _original = _fixture(tmp_path, monkeypatch, provider)
- if captured:
- guard["replan_read_path"] = str(tmp_path / "original-decision.json")
+ capture_dir = tmp_path / "decision-capture" if captured else None
+ _project, runtime, _registry, turn, guard, _original = _fixture(
+ tmp_path, monkeypatch, provider, capture_dir=capture_dir,
+ )
+ if capture_dir is not None:
+ capture_path = capture_dir / "decision.json"
+ assert guard["replan_read_path"] == str(capture_path)
+ assert json.loads(capture_path.read_bytes()) == guard
+
+ plan = build_loopx_turn_plan(guard, host="codex-cli", execution_mode="isolated-headless")
+ assert plan["route"]["would_invoke_host"] is True
+ assert plan["work_contract"] == guard
+ assert plan["transaction"]["settlement_plan"]["identity"] == guard["heartbeat_receipt"]["settlement_identity"]
+
+ # The historical adapter must preserve the same binding and reject mismatches.
envelope = build_turn_envelope(guard)
binding = (lambda value: value["writeback"]["settlement_identity"]) if captured else (
lambda value: value["writeback"]["settlement_plan"]["identity"])
diff --git a/tests/control_plane/test_vision_budget_cli.py b/tests/control_plane/test_vision_budget_cli.py
index c203b018a5..62af1b9f47 100644
--- a/tests/control_plane/test_vision_budget_cli.py
+++ b/tests/control_plane/test_vision_budget_cli.py
@@ -312,7 +312,7 @@ def test_authoring_example_material_closeout_has_no_missing_path_replan(tmp_path
registry, runtime, vision_path=path, check=True,
dry_run=False, autonomous_replan_recorded=False,
))
- assert result["vision_checkpoint"]["satisfied"] is True
+ assert result["vision_checkpoint"]["satisfied"] is (omitted != "wait")
quota = fixture.run_quota(registry, runtime)
gaps = quota["goal_frontier_projection"]["acceptance_gaps"]
outcome_gaps = [g for g in gaps if g["kind"] == "vision_outcome_checkpoint_required"]
diff --git a/tests/control_plane/test_work_acceptance_context.py b/tests/control_plane/test_work_acceptance_context.py
index 6de0c9e551..6eb658a732 100644
--- a/tests/control_plane/test_work_acceptance_context.py
+++ b/tests/control_plane/test_work_acceptance_context.py
@@ -252,19 +252,26 @@ def configure(*arguments):
first = read_cli_policy_payload(json.dumps(first), returncode=0)
saved = (capture / "decision.json").read_bytes()
full = json.loads(saved)
- basis = next(row["content"] for row in first["work_context"]["sources"]
+ from loopx.control_plane.quota.turn_envelope import build_turn_envelope
+ first_envelope = build_turn_envelope(first, captured_decision_path=str(capture / "decision.json"))
+ assert first["schema_version"] == "loopx_turn_envelope_v1"
+ assert first == full
+ basis = next(row["content"] for row in first_envelope["work_context"]["sources"]
if row.get("source") == "goal_acceptance")
- assert first["action"]["delivery_allowed"] is True
+ assert first["interaction_contract"]["agent_channel"]["delivery_allowed"] is True
assert basis["tasks"][0]["state"] == "ready"
assert basis["goal_acceptance_contract"]["outcome_requirements"] == original
- request = build_loopx_turn_host_request(build_loopx_turn_plan(first,
+ request = build_loopx_turn_host_request(build_loopx_turn_plan(first_envelope, work_contract=first,
host="codex-cli", execution_mode="isolated-headless", turn_instance_id="original-delivery"))
result = subprocess.run(_recording_host(project), input=json.dumps(request),
capture_output=True, text=True, timeout=30)
assert result.returncode == 0, result.stdout + result.stderr
prompt = (project / "received.txt").read_text()
- authority = json.loads(prompt.split("Turn authority JSON:\n", 1)[1].split("\n\nWhen finished", 1)[0])
- assert authority["work_context"] == first["work_context"]
+ authority = json.loads(prompt.split("Turn request:\n", 1)[1].split("\n\nWhen finished", 1)[0])
+ assert authority["schema_version"] == "loopx_turn_model_context_v0"
+ assert authority["source_schema_version"] == "loopx_turn_host_request_v1"
+ assert (authority["task_context"]["interaction_contract"]["agent_channel"]["work_context"]
+ == first["interaction_contract"]["agent_channel"]["work_context"])
# Finish the original synthetic work before attempting another managed
# Turn. An unsettled receipt is its own legitimate recovery boundary.
@@ -343,7 +350,7 @@ def configure(*arguments):
if source.get("source") == "goal_acceptance":
assert source["content"]["goal_acceptance_contract"] == cold["goal_acceptance_contract"]
before_read = {p: p.read_bytes() for p in capture.rglob("*") if p.is_file()}
- detail = subprocess.run(shlex.split(first["detail_ref"]["full_decision"]),
+ detail = subprocess.run(shlex.split(first_envelope["detail_ref"]["full_decision"]),
capture_output=True, check=True).stdout
assert detail == saved and json.loads(detail) == full
assert {p: p.read_bytes() for p in capture.rglob("*") if p.is_file()} == before_read
diff --git a/tests/control_plane/test_work_context_host_delivery.py b/tests/control_plane/test_work_context_host_delivery.py
index 6c0b2ed5b4..76d80952fe 100644
--- a/tests/control_plane/test_work_context_host_delivery.py
+++ b/tests/control_plane/test_work_context_host_delivery.py
@@ -1,18 +1,22 @@
"""Authored requirements must reach the Host, not just agree after projection."""
import copy
import json
-import os
import shlex
-import subprocess
from urllib.parse import quote
import pytest
from loopx.control_plane.coordination.runtime_shadow import build_todo_runtime_shadow_projection
+from loopx.control_plane.effect_runtime import EffectRuntimeRejected
+from loopx.control_plane.quota.turn_envelope import build_turn_envelope
+from loopx.control_plane.turn_driver.codex_cli import _prompt
+from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
+from loopx.control_plane.turn_driver.executor import build_loopx_turn_host_request
from loopx.control_plane.turn_driver.host_candidate import extract_turn_authority, render_prompt
from tests.control_plane.canonical_authority_fixture import (
initialize_canonical_authority, isolate_sqlite_runtime, read_fixture_todos,
)
+from tests.control_plane.host_process_fixture import assert_host_task_context
from tests.control_plane.test_quota_settlement_cli import (
AGENT_ID, GOAL_ID, TODO_ID, TURN_ID, _configure_read_only_todo, _configure_selectable_alternative,
_heartbeat_receipt_count, _run_cli, _spend_run_count, _write_fixture,
@@ -101,15 +105,33 @@ def test_authored_requirements_survive_capture_projection_and_host(tmp_path, mon
'--todo-id', TODO_ID, '--turn-instance-id', TURN_ID, '--codex-app',
'--available-capability', 'shell', '--available-capability', 'filesystem_read']
capture = tmp_path / 'original-context'
- code, envelope = _run_cli(registry, runtime, *args, '--turn-envelope',
- '--decision-output-dir', str(capture))
- assert code == 0, envelope
+ code, emitted = _run_cli(registry, runtime, *args, '--turn-envelope',
+ '--decision-output-dir', str(capture))
+ assert code == 0, emitted
saved = capture / 'decision.json'
original_bytes = saved.read_bytes()
full = json.loads(original_bytes)
+ assert emitted == full
source_context = full['interaction_contract']['agent_channel']['work_context']
assert source_context['complete'] is True
assert source_context['instruction'] # Non-vacuous instruction transport.
+
+ plan = build_loopx_turn_plan(full, host='codex-cli', execution_mode='isolated-headless')
+ request = build_loopx_turn_host_request(plan)
+ model = json.loads(_prompt(request).split('Turn request:\n', 1)[1])
+ model_context = assert_host_task_context(model, full)
+ model_channel = model_context['interaction_contract']['agent_channel']
+ model_task = next(source['content']['todo'] for source in model_channel['work_context']['sources']
+ if source['source'] == 'selected_todo')
+ assert model_task.get('text', model_context['selected_todo']['text']) == source_task['text']
+ assert model_task['note'] == note
+ model_goal = next(source['content'] for source in model_channel['work_context']['sources']
+ if source['source'] == 'goal_state')
+ assert model_goal['full_source'] is True and goal in model_goal['text']
+ assert all(item['source'] != 'goal_state' for item in model_channel['required_reads'])
+
+ # Keep the historical reader exercised with an explicit legacy projection.
+ envelope = build_turn_envelope(full, captured_decision_path=str(saved))
authority = extract_turn_authority({'turn_envelope': envelope})
# The raw heartbeat and the structured Host have different effect owners,
# but ordinary execution must not lose notification, bypass or scheduler
@@ -139,16 +161,8 @@ def test_authored_requirements_survive_capture_projection_and_host(tmp_path, mon
assert task_detail['note'] == note
delivered_task = task_detail.get('text', authority['selected_todo']['text'])
assert delivered_task == source_task['text']
- # This fixture has an unregistered Goal narrative: pending read must remain
- # explicit even when all available inline sources were loaded successfully.
- goal_read = next(item for item in authority['required_reads'] if item['source'] == 'goal_state')
- assert goal_read['ordering'] == 'before_work'
- if os.name != 'nt':
- readback = subprocess.run(shlex.split(goal_read['command']), capture_output=True,
- text=True, check=True)
- assert goal in readback.stdout
- else:
- assert goal in state.read_text(encoding='utf-8')
+ assert goal in source_by_kind['goal_state']['text']
+ assert all(item['source'] != 'goal_state' for item in authority['required_reads'])
# The actual Host prompt must contain the independent source requirements,
# not merely match an action signature computed by the same projector.
prompt = render_prompt(authority)
@@ -173,9 +187,6 @@ def test_authored_requirements_survive_capture_projection_and_host(tmp_path, mon
extract_turn_authority({'turn_envelope': altered})
# A matching v0 digest does not authorize interpreting a future wire version.
# Version declarations are outside the historical action digest document.
- from loopx.control_plane.effect_runtime import EffectRuntimeRejected
- from loopx.control_plane.turn_driver.codex_cli import _prompt
- from loopx.control_plane.turn_driver.driver import build_loopx_turn_plan
for path in (('schema_version',), ('action_signature', 'schema_version'),
('action_signature', 'coverage')):
for unsupported in ('future_required_semantics', None):
diff --git a/tests/control_plane/test_work_yardstick_cli.py b/tests/control_plane/test_work_yardstick_cli.py
index 524ace8aa9..00eb5fe4f2 100644
--- a/tests/control_plane/test_work_yardstick_cli.py
+++ b/tests/control_plane/test_work_yardstick_cli.py
@@ -7,6 +7,7 @@
from datetime import datetime, timezone
from itertools import groupby
import json
+from pathlib import Path
import shlex
import pytest
@@ -584,17 +585,18 @@ def test_yardstick_opening_keeps_the_current_cue_with_a_multi_page_write_scope(t
assert guidance == graph["work_yardstick_capture"]["guidance"]
assert cue == graph["work_yardsticks"][0] and cue["todo_id"] == TODO_ID
- _, reference = read_context_read_reference(opening["reference_file"])
- assert reference["sha256"] == hashlib.sha256(original).hexdigest()
+ expected_sha256 = hashlib.sha256(original).hexdigest()
+ assert opening["sha256"] == opening["navigation"]["recovery"]["sha256"] == expected_sha256
+ assert Path(opening["navigation"]["recovery"]["path"]) == saved
chunks = [content]
offset = len(content.encode())
page = opening
while not page["eof"]:
assert page["next_offset"] == offset
words = shlex.split(page["next_command"])
+ assert words[words.index("--capture-sha256") + 1] == expected_sha256
page = run(project, runtime, path, *words[words.index("quota"):])
- _, follow = read_context_read_reference(page["reference_file"])
- assert follow["path"] == reference["path"] and follow["sha256"] == reference["sha256"]
+ assert page["sha256"] == expected_sha256
assert page["offset"] == offset
chunks.append(page["content"])
offset += len(page["content"].encode())
diff --git a/tests/control_plane/test_worker_observation_recovery_cli.py b/tests/control_plane/test_worker_observation_recovery_cli.py
index 79124b97b5..774aeb25c9 100644
--- a/tests/control_plane/test_worker_observation_recovery_cli.py
+++ b/tests/control_plane/test_worker_observation_recovery_cli.py
@@ -120,10 +120,14 @@ def test_original_capture_retains_the_emitted_poll_observation(
elif wire == "envelope":
from loopx.control_plane.quota.turn_envelope import build_turn_envelope
- # Explicitly disabled context delivery keeps the bounded v0 wire view;
- # the immutable capture may carry the complete v1 source instead.
- assert emitted["schema_version"] == "loopx_turn_envelope_v0"
- assert emitted != source
+ # Complete v1 turn envelope carriers preserve the full decision while
+ # build_turn_envelope projects the bounded v0 view with action signatures.
+ assert emitted["schema_version"] in {"loopx_turn_envelope_v0", "loopx_turn_envelope_v1"}
+ envelope = (
+ build_turn_envelope(emitted)
+ if emitted.get("schema_version") == "loopx_turn_envelope_v1"
+ else emitted
+ )
# Replan readers bind the full observation, including this receipt.
# Their hash changes without changing the selected work or debit.
without_poll = deepcopy(source)
@@ -140,24 +144,47 @@ def test_original_capture_retains_the_emitted_poll_observation(
without_poll, _ = _resign_contract(without_poll)
assert_complete_turn_decision(without_poll)
prior = build_turn_envelope(without_poll)
- actual = emitted["action_signature"]
+ actual = envelope["action_signature"]
for key in ("decision", "should_run", "effective_action"):
- assert emitted[key] == prior[key]
- assert emitted["writeback"]["settlement_identity"] == prior["writeback"]["settlement_identity"]
+ assert envelope[key] == prior[key]
+ assert envelope["writeback"]["settlement_identity"] == prior["writeback"]["settlement_identity"]
assert actual["source_decision_hash"] != prior["action_signature"]["source_decision_hash"]
assert actual == build_turn_envelope(source)["action_signature"]
- review = subprocess.run(shlex.split(emitted["replan_action_packet"]["review"]),
+ review = subprocess.run(shlex.split(envelope["replan_action_packet"]["review"]),
check=True, capture_output=True, text=True, timeout=90)
assert json.loads(review.stdout)["source_hash"] == actual["source_decision_hash"]
- reader = shlex.split(emitted["detail_ref"]["full_decision"])
+ reader = shlex.split((emitted.get("detail_ref") or envelope["detail_ref"])["full_decision"])
else:
reader = [sys.executable, "-m", "loopx.cli", "--format", "json", "quota", "context-read",
"--capture-file", str(capture / "decision.json"),
"--capture-sha256", hashlib.sha256(original).hexdigest()]
if wire == "envelope":
- recovered = json.loads(subprocess.run(
+ def read_paged_json(page: dict) -> dict:
+ digest = page["sha256"]
+ raw = bytearray()
+ while True:
+ assert page["ok"] and page["sha256"] == digest and page["offset"] == len(raw)
+ raw.extend(page["content"].encode())
+ if page["eof"]:
+ assert len(raw) == page["total_bytes"]
+ return json.loads(raw)
+ page = json.loads(subprocess.run(
+ shlex.split(page["next_command"]),
+ check=True, capture_output=True, text=True, timeout=90,
+ ).stdout)
+
+ first = json.loads(subprocess.run(
reader, check=True, capture_output=True, text=True, timeout=90,
).stdout)
+ if first.get("schema_version") == "context_capture_page_v1":
+ recovered = read_paged_json(first)
+ elif first.get("schema_version") == "context_capture_navigation_v1":
+ recovered = read_paged_json(json.loads(subprocess.run(
+ shlex.split(first["recovery"]["command"]),
+ check=True, capture_output=True, text=True, timeout=90,
+ ).stdout))
+ else:
+ recovered = first
assert recovered == source, "The emitted reader must recover the complete original source"
if source.get("schema_version") == "loopx_turn_envelope_v1":
assert_complete_turn_decision(recovered)
@@ -185,17 +212,26 @@ def test_failed_capture_discloses_the_already_recorded_poll(
from unittest.mock import patch
from loopx.cli import main
- from loopx.cli_commands import quota, quota_capture
+ from loopx.cli_commands import quota_capture, quota_result_delivery
runtime, registry, count, _ = _worker_fixture(tmp_path, monkeypatch, provider)
original_capture = quota_capture.capture_decision
- def failed_publication(directory, payload, *, args, native_context):
- # Only the capture publisher fails; admission and receipt IO are real.
- with patch.object(quota_capture.os, "replace", side_effect=OSError("synthetic capture outage")):
- original_capture(directory, payload, args=args, native_context=native_context)
+ replace_calls = 0
- monkeypatch.setattr(quota, "capture_decision", failed_publication)
+ def failed_publication(directory, payload, *, args, native_context, record_capture=None):
+ nonlocal replace_calls
+ # Only the capture publisher fails; admission and receipt IO are real.
+ with patch.object(quota_capture.os, "replace", side_effect=OSError("synthetic capture outage")) as mocked_replace:
+ try:
+ return original_capture(
+ directory, payload, args=args, native_context=native_context, record_capture=record_capture,
+ )
+ finally:
+ replace_calls += mocked_replace.call_count
+
+ monkeypatch.setattr(quota_capture, "capture_decision", failed_publication)
+ monkeypatch.setattr(quota_result_delivery, "capture_decision", failed_publication)
monkeypatch.chdir(tmp_path / "project")
capture = tmp_path / "failed-capture"
code = main(["--registry", str(registry), "--runtime-root", str(runtime), "--format", "json",
@@ -204,7 +240,9 @@ def failed_publication(directory, payload, *, args, native_context):
"--record-host-poll", "--decision-output-dir", str(capture),
"--context-delivery", "off"])
failed = json.loads(capsys.readouterr().out)
+ assert replace_calls == 1
assert code == 1 and failed["status"] == "decision_transport_failed"
+ assert "error_class" not in failed and "Decision capture failed" in failed["reason"]
assert failed["should_run"] is False and "captured_decision" not in failed
assert failed["heartbeat_receipt"]["turn_instance_id"] == "capture-failure"
receipt = receipt_path_for_goal({"id": GOAL}, tmp_path / "project" / "ACTIVE_GOAL_STATE.md")
diff --git a/tests/control_plane_ts/conversation_binding.test.ts b/tests/control_plane_ts/conversation_binding.test.ts
index 2c7a27c3e8..54fa12b6a9 100644
--- a/tests/control_plane_ts/conversation_binding.test.ts
+++ b/tests/control_plane_ts/conversation_binding.test.ts
@@ -1968,7 +1968,8 @@ test("quota backlog projection retires explicit lifecycle stops, not unavailable
});
for (const errorCode of ["response_timeout", "upstream_transport_failure", "context_window_exceeded",
- "antigravity_rpc_error", "antigravity_transport_error", "antigravity_step_error", "runtime_error", "server_restarted"]) {
+ "antigravity_rpc_error", "antigravity_transport_error", "antigravity_step_error", "runtime_error",
+ "execution_worker_exited", "execution_worker_exited_before_dispatch", "server_restarted"]) {
test(`delivered ${errorCode} notices retain the exact failed request, including other executors`, () => {
const request = {request_ref: "f".repeat(24), command: null, status: "accepted",
session_id: "original", turn_id: "timed-out", delivery_verified: true};
@@ -2054,3 +2055,129 @@ test("a steward quota endpoint change preserves its exact bound audience", () =>
}
assert.throws(() => resolveBoundConversation({...packet, sender_ref: "f".repeat(24), session, session_context: context}));
});
+
+
+test("a steward session migrated via flexible_primary_available preserves web and bound intake audience", () => {
+ const codexBinding = {...row, context_kind: "steward", grant: "portfolio_read", goal_ids: [], executor_endpoint_id: "codex"};
+ const configured = planConversationBinding({...request, binding: codexBinding}).state;
+ const source = {source_ref: "e".repeat(24), sender_ref: row.operator_ref, private_human_message: true};
+ const packet = {current: configured, binding_id: row.binding_id, observation, available_projects: [project], ...source};
+ const context = resolveBoundConversation(packet).context;
+ const primaryAllocation = {schema_version: "manager_executor_allocation_v0", selection_policy: "flexible",
+ configured_endpoint: "antigravity", executor_endpoint: "antigravity", configuration_revision: "revision-2",
+ allocation_reason: "flexible_primary_available", eligible_endpoints: ["antigravity", "codex"]};
+ const migratedSession = {session_id: "original", goal_id: "loopx-manager", agent_id: "antigravity", executor_endpoint_id: "antigravity",
+ session_mode: "managed_runtime", status: "ready", upstream_thread_id: "antigravity-native",
+ channel_id: `manager.external.native.${row.binding_id}.${source.source_ref}`, steward_context: context,
+ manager_executor_allocation: primaryAllocation, upstream_context_history: [{agent_id: "codex", thread_id: "original-codex"}]};
+ const webPacket = {...packet, origin: "web", private_human_message: false, session: migratedSession, session_context: context};
+ assert.deepEqual(resolveBoundConversation(webPacket).context, context);
+ for (const invalid of [
+ {upstream_context_history: []},
+ {manager_executor_allocation: {...primaryAllocation, allocation_reason: "flexible_availability_fallback"}},
+ {manager_executor_allocation: {...primaryAllocation, configured_endpoint: "codex"}},
+ ]) {
+ assert.throws(() => resolveBoundConversation({...webPacket, session: {...migratedSession, ...invalid}}));
+ }
+});
+
+
+test("antigravity and flexible-pool steward sessions recover pre-dispatch resume_failed even when failure notice receipt is unknown", () => {
+ const codexBinding = {...row, context_kind: "steward", grant: "portfolio_read", goal_ids: [], executor_endpoint_id: "codex"};
+ const configured = planConversationBinding({...request, binding: codexBinding}).state;
+ const source = {source_ref: "e".repeat(24), sender_ref: row.operator_ref, private_human_message: true};
+ const context = resolveBoundConversation({current: configured, binding_id: row.binding_id,
+ observation, available_projects: [project], ...source}).context;
+ const primaryAllocation = {schema_version: "manager_executor_allocation_v0", selection_policy: "flexible",
+ configured_endpoint: "antigravity", executor_endpoint: "antigravity", configuration_revision: "revision-2",
+ allocation_reason: "flexible_primary_available", eligible_endpoints: ["antigravity", "codex"]};
+ const session = {session_id: "original", goal_id: "loopx-manager", agent_id: "antigravity",
+ executor_endpoint_id: "antigravity", upstream_mode: "default", status: "ready",
+ upstream_thread_id: "antigravity-native", active_turn_id: null,
+ channel_id: `manager.external.native.${row.binding_id}.${source.source_ref}`, steward_context: context,
+ manager_executor_allocation: primaryAllocation, upstream_context_history: [{agent_id: "codex", thread_id: "original-codex"}]};
+ const ref = "f".repeat(24);
+ const failed = {session_id: "original", turn_id: "prep-failed", client_turn_id: `external-${ref}`,
+ message: "Compare repository execution value", origin: "lark", status: "failed", delta_count: 0, response: null,
+ upstream_turn_id: null, started_at: null, first_event_at: null, error_code: "resume_failed",
+ error: "The previous Agent conversation could not be restored.", completed_at: "2026-01-01T00:00:00Z"};
+ const proof = {kind: "core_runtime_preparation_failure", session_id: "original", turn_id: failed.turn_id,
+ events: [{event_id: "queued", kind: "turn.queued", delivery_mode: "session_queue"},
+ {event_id: "failed", kind: "turn.failed", error_code: failed.error_code, message: failed.error}]};
+ const packet = {
+ request: {request_ref: ref, command: null, status: "accepted", delivery_verified: false,
+ message: failed.message, origin: "lark", binding_id: row.binding_id, session_id: "original",
+ turn_id: failed.turn_id, source},
+ binding: codexBinding,
+ current_session: session,
+ context_recovery: {version: 23, failed_turn: failed, observed_at: "2026-01-01T00:02:00Z",
+ source_verified: true, steering_observed: false, stop_observed: false, attachments_present: false,
+ native_result_present: false, queued_input_present: false, later_equivalent_input_present: false,
+ preparation_scope_available: true, preparation_observation: proof, failure_notice_receipt_unknown: true},
+ };
+ const plan = planBoundConversationRequest(packet);
+ assert.equal(plan.recovery_candidate, true);
+ assert.equal(plan.recovery_eligible, true);
+ assert.equal(plan.preparation_attempt, 1);
+ assert.equal(plan.client_turn_id, `external-${ref}-recovery-23-preparation-1`);
+ // Receipt-unknown tolerance stays strictly pre-dispatch; post-dispatch or non-preparation failures remain rejected.
+ assert.equal(planBoundConversationRequest({...packet,
+ context_recovery: {...packet.context_recovery, failure_notice_receipt_unknown: false}}).recovery_candidate, false);
+ for (const postDispatch of [
+ {started_at: "2026-01-01T00:00:01Z"},
+ {upstream_turn_id: "native-1"},
+ {delta_count: 1},
+ {error_code: "response_timeout", started_at: "2026-01-01T00:00:01Z", upstream_turn_id: "native-1"},
+ ]) {
+ assert.equal(planBoundConversationRequest({...packet,
+ context_recovery: {...packet.context_recovery, failed_turn: {...failed, ...postDispatch}}}).recovery_candidate, false);
+ }
+});
+
+for (const [kind, grant] of [["project", "workspace_write"], ["steward", "portfolio_read"]] as const) {
+ test(`${kind} loopback relay 401 failure (${grant}) recovers only while fresh relay readiness holds`, () => {
+ const selected = kind === "steward"
+ ? {...row, context_kind: "steward", grant, goal_ids: []}
+ : {...row, context_kind: "project", grant};
+ const availableProjects = [kind === "steward" ? project : {...project, grant}];
+ const configured = planConversationBinding({...request, binding: selected, available_projects: availableProjects}).state;
+ const source = {source_ref: "e".repeat(24), sender_ref: row.operator_ref, private_human_message: true};
+ const context = resolveBoundConversation({current: configured, binding_id: row.binding_id,
+ observation, available_projects: availableProjects, ...source}).context;
+ const session = {session_id: "original", goal_id: kind === "steward" ? "loopx-manager" : null,
+ agent_id: "codex", upstream_mode: "chat", status: "ready",
+ upstream_thread_id: "native", active_turn_id: null, manager_context_version: 23,
+ channel_id: `${kind === "steward" ? "manager.external.native" : "project.external"}.${row.binding_id}.${source.source_ref}`,
+ ...(kind === "steward" ? {steward_context: context} : {project_context: context})};
+ const failed = {session_id: "original", turn_id: "failed", status: "failed", delta_count: 0,
+ response: null, error_code: "upstream_transport_failure", upstream_turn_id: "native-failed",
+ completed_at: "2026-01-01T00:00:00Z"};
+ const observedAt = Date.parse("2026-01-01T00:10:00Z") / 1000;
+ const observed = {thread_id: "native", turn_id: "native-failed",
+ codex_error_info: {httpConnectionFailed: {httpStatusCode: 401}},
+ loopback_relay_auth_failure: true, only_pre_execution_items: true, only_original_user_item: true,
+ relay_transport_ready: true, relay_observed_at: observedAt};
+ const packet = {request: {request_ref: "f".repeat(24), command: null, status: "accepted", delivery_verified: true,
+ message: "Original synthetic request", origin: "lark",
+ binding_id: row.binding_id, session_id: "original", turn_id: "failed", source},
+ binding: selected, current_session: session, context_recovery: {version: 23, failed_turn: failed,
+ observed_at: "2026-01-01T00:10:00Z", steering_observed: false, source_verified: true, native_observation: observed}};
+ const plan = planBoundConversationRequest(packet);
+ assert.equal(plan.recovery_eligible, true);
+ assert.equal(plan.routing_attempt, 1);
+ assert.equal(plan.client_turn_id, `external-${"f".repeat(24)}-recovery-23-routing-1`);
+ for (const change of [{source_verified: false}, {stop_observed: true}, {steering_observed: true},
+ {later_equivalent_input_present: true},
+ {native_observation: {...observed, relay_transport_ready: false}},
+ {native_observation: {...observed, relay_observed_at: observedAt - 121}},
+ {native_observation: {...observed, relay_observed_at: observedAt + 1}},
+ {native_observation: {...observed, loopback_relay_auth_failure: false}},
+ {native_observation: {...observed, only_pre_execution_items: false}},
+ {native_observation: {...observed, codex_error_info: {httpConnectionFailed: {httpStatusCode: 403}}}},
+ {native_observation: {...observed, codex_error_info: {responseStreamDisconnected: {httpStatusCode: 401}}}},
+ {failed_turn: {...failed, error_code: "host_gate"}}]) {
+ assert.notEqual(planBoundConversationRequest({...packet,
+ context_recovery: {...packet.context_recovery, ...change}}).recovery_eligible, true);
+ }
+ });
+}
diff --git a/tests/control_plane_ts/conversation_scope.test.ts b/tests/control_plane_ts/conversation_scope.test.ts
index 3683040fe6..f2f312e9c8 100644
--- a/tests/control_plane_ts/conversation_scope.test.ts
+++ b/tests/control_plane_ts/conversation_scope.test.ts
@@ -1,6 +1,6 @@
import assert from "node:assert/strict";
import test from "node:test";
-import {resolveConversationScope, projectConversationIdentity, stewardConversationIdentity} from "../../loopx/control_plane/collaboration/conversation_scope.ts";
+import {boundConversationExecutorMatches, resolveConversationScope, projectConversationIdentity, stewardConversationIdentity} from "../../loopx/control_plane/collaboration/conversation_scope.ts";
test("result review narrows runtime effects without rewriting the original grant or native store", () => {
const context = {kind: "project_workspace", project_ref: "a".repeat(24), workspace_path: "/synthetic/project",
@@ -98,3 +98,64 @@ test("steward metadata formatting keeps the original audience with no execution
assert.equal(review.host_config.permissions.loopx_workspace_only_read.network.enabled, false);
assert.throws(() => stewardConversationIdentity({context: {...context, audience: "bound_group"}, metadata_review: true}));
});
+
+test("boundConversationExecutorMatches validates bidirectional flexible fallback", () => {
+ const row = {context_kind: "steward", audience: "owner", executor_endpoint_id: "codex"};
+ const allocation = {
+ schema_version: "manager_executor_allocation_v0",
+ selection_policy: "flexible",
+ configured_endpoint: "codex",
+ executor_endpoint: "antigravity",
+ allocation_reason: "flexible_availability_fallback",
+ configuration_revision: "rev-1",
+ eligible_endpoints: ["codex", "antigravity"],
+ };
+ const valid = {
+ agent_id: "antigravity",
+ executor_endpoint_id: "antigravity",
+ manager_executor_allocation: allocation,
+ upstream_context_history: [{agent_id: "codex", thread_id: "orig-codex-thread"}],
+ };
+ assert.equal(boundConversationExecutorMatches(row, valid), true);
+ assert.equal(boundConversationExecutorMatches(row, {...valid, upstream_context_history: []}), false);
+ assert.equal(boundConversationExecutorMatches(row, {
+ ...valid,
+ manager_executor_allocation: {...allocation, allocation_reason: "configured_preference"},
+ }), false);
+ assert.equal(boundConversationExecutorMatches({...row, audience: "group"}, valid), false);
+ assert.equal(boundConversationExecutorMatches(row, {...valid, executor_endpoint_id: "codex"}), false);
+
+ for (const [configured, migrated] of [["codex", "antigravity"], ["antigravity", "codex"]] as const) {
+ const primaryRow = {context_kind: "steward", audience: "owner", executor_endpoint_id: configured};
+ const primaryAllocation = {
+ schema_version: "manager_executor_allocation_v0",
+ selection_policy: "flexible",
+ configured_endpoint: migrated,
+ executor_endpoint: migrated,
+ allocation_reason: "flexible_primary_available",
+ configuration_revision: "rev-2",
+ eligible_endpoints: [migrated, configured],
+ };
+ const primaryMigrated = {
+ agent_id: migrated,
+ executor_endpoint_id: migrated,
+ manager_executor_allocation: primaryAllocation,
+ upstream_context_history: [{agent_id: configured, thread_id: `orig-${configured}-thread`}],
+ };
+ assert.equal(boundConversationExecutorMatches(primaryRow, primaryMigrated), true);
+ assert.equal(boundConversationExecutorMatches(primaryRow, {
+ ...primaryMigrated,
+ manager_executor_allocation: {...primaryAllocation, configured_endpoint: configured},
+ }), false);
+ assert.equal(boundConversationExecutorMatches(primaryRow, {
+ ...primaryMigrated,
+ manager_executor_allocation: {...primaryAllocation, allocation_reason: "flexible_availability_fallback"},
+ }), false);
+ assert.equal(boundConversationExecutorMatches(primaryRow, {...primaryMigrated, upstream_context_history: []}), false);
+ assert.equal(boundConversationExecutorMatches({...primaryRow, audience: "group"}, primaryMigrated), false);
+ assert.equal(boundConversationExecutorMatches(primaryRow, {
+ ...primaryMigrated,
+ manager_executor_allocation: {...primaryAllocation, selection_policy: "preferred"},
+ }), false);
+ }
+});
diff --git a/tests/control_plane_ts/quota_monitor_poll_commit.test.ts b/tests/control_plane_ts/quota_monitor_poll_commit.test.ts
index 62e85380e7..69407b7b11 100644
--- a/tests/control_plane_ts/quota_monitor_poll_commit.test.ts
+++ b/tests/control_plane_ts/quota_monitor_poll_commit.test.ts
@@ -291,13 +291,14 @@ test("admission revalidates due, external, and exact blocked-wait modes", async
);
});
-test("attempt_due_monitor admits secondary claimed due monitors via registry_due_monitor unless user gate applies", async () => {
+test("attempt_due_monitor admits an explicit secondary due Todo unless a user gate applies", async t => {
const secondaryCandidate = {
todo_id: "todo_secondary_monitor",
target_key: "secondary-watch",
task_class: "continuous_monitor",
claimed_by: "codex-main-control",
due: true,
+ todo_id_explicit: true,
};
const baseDecision = (overrides: Record = {}) => decision({
should_run: true,
@@ -333,6 +334,8 @@ test("attempt_due_monitor admits secondary claimed due monitors via registry_due
{ requires_user_action: true },
{ registry_due_monitor: { ...secondaryCandidate, due: false } },
{ registry_due_monitor: { ...secondaryCandidate, claimed_by: "other-agent" } },
+ { registry_due_monitor: { ...secondaryCandidate, todo_id_explicit: false } },
+ { registry_due_monitor: { ...secondaryCandidate, todo_id_explicit: undefined } },
]) {
await assert.rejects(
() => evaluateQuotaMonitorPollCommit(request({
@@ -350,6 +353,30 @@ test("attempt_due_monitor admits secondary claimed due monitors via registry_due
},
);
}
+
+ const runtime = await tempRuntime(t);
+ const effect = "previously-admitted-secondary";
+ const params = request({
+ phase: "preflight", runtime_root: runtime, execute: true, effect_id: effect,
+ decision: baseDecision(),
+ observation: observation({
+ todo_id: "todo_secondary_monitor",
+ target_key: "secondary-watch",
+ result_hash: "unchanged-secondary",
+ }),
+ });
+ const initial = await evaluateQuotaMonitorPollCommit(params);
+ assert.equal(initial.status, "provider_required");
+ const token = createHash("sha256").update(effect).digest("hex").slice(0, 24);
+ const receiptPath = join(runtime, "goals", goalId, "runs", ".transactions", "quota-monitor-poll", `${token}.json`);
+ const historical = JSON.parse(await readFile(receiptPath, "utf8"));
+ delete historical.admitted_decision.registry_due_monitor.todo_id_explicit;
+ await writeFile(receiptPath, JSON.stringify(historical));
+ const recovered = await evaluateQuotaMonitorPollCommit({
+ ...params,
+ decision: baseDecision({registry_due_monitor: null}),
+ });
+ assert.deepEqual(recovered.provider_plan, initial.provider_plan);
});
test("Todo preflight rejects invalid admission before journaling provider intent", async (t) => {
diff --git a/tests/control_plane_ts/quota_settlement_readback.test.ts b/tests/control_plane_ts/quota_settlement_readback.test.ts
index da5f0aae0f..ced46eb8a6 100644
--- a/tests/control_plane_ts/quota_settlement_readback.test.ts
+++ b/tests/control_plane_ts/quota_settlement_readback.test.ts
@@ -2639,3 +2639,66 @@ test("history retry qualification preserves its original aware clock and exact e
}
await assert.rejects(read([run], 123), /observation clock/);
});
+
+
+test("pending closeout with explicit turn and todo still names an owed prior Turn", async () => {
+ const runtimeRoot = await owedPriorTurnFixture();
+ try {
+ const pending = await readQuotaSettlement(
+ request(runtimeRoot, {pending_closeout: true, turn_instance_id: turnId, todo_id: todoId}),
+ );
+ const failure = (pending.identity as any).result.failure;
+ assert.equal(failure.kind, "receipt_unbound");
+ assert.match(failure.reason, /carries no settlement binding yet/);
+ assert.match(
+ failure.reason,
+ new RegExp(`earlier Turn owes its closeout \\(turn_instance_id ${priorTurnId}, todo ${todoId}`),
+ );
+ assert.doesNotMatch(failure.reason, new RegExp(`--turn-instance-id ${turnId} --todo-id`));
+
+ const originalOnly = await readQuotaSettlement(
+ request(runtimeRoot, {resolve_original_binding: true, todo_id: null}),
+ );
+ assert.equal(
+ (originalOnly.identity as any).result.failure.reason,
+ "the original Turn guard has no settlement binding",
+ );
+ } finally {
+ await rm(runtimeRoot, {recursive: true, force: true});
+ }
+});
+
+test("readback exposes matching supersede_event when records are included", async () => {
+ const runtimeRoot = await fixture();
+ try {
+ const eventsPath = join(runtimeRoot, "goals", goalId, "rollout-event-log.jsonl");
+ const existing = await readFile(eventsPath, "utf8");
+ const supersede = {
+ schema_version: "loopx_rollout_event_v0",
+ event_id: "event-supersede-1",
+ goal_id: goalId,
+ agent_id: agentId,
+ todo_id: todoId,
+ run_id: turnId,
+ event_kind: "todo_supersede",
+ status: "done",
+ recorded_at: "2026-06-01T00:05:00.000Z",
+ details: {
+ goal_id: goalId,
+ agent_id: agentId,
+ todo_id: todoId,
+ turn_instance_id: turnId,
+ settlement_effect_id: settlementIdentityPayload(identity).effect_id,
+ },
+ };
+ await writeFile(eventsPath, existing + JSON.stringify(supersede) + "\n", "utf8");
+ const readback = await readQuotaSettlement(
+ request(runtimeRoot, {include_record_bodies: true}),
+ );
+ assert.equal((readback.supersede_event as any)?.event_kind, "todo_supersede");
+ assert.equal((readback.supersede_event as any)?.run_id, turnId);
+ assert.equal((readback.supersede_event as any)?.todo_id, todoId);
+ } finally {
+ await rm(runtimeRoot, {recursive: true, force: true});
+ }
+});
diff --git a/tests/control_plane_ts/quota_spend_commit.test.ts b/tests/control_plane_ts/quota_spend_commit.test.ts
index dac67a0493..acf9f4d2f6 100644
--- a/tests/control_plane_ts/quota_spend_commit.test.ts
+++ b/tests/control_plane_ts/quota_spend_commit.test.ts
@@ -33,6 +33,9 @@ import {
quotaSpendIndexDigest,
QUOTA_SPEND_COMMIT_REQUEST_SCHEMA,
} from "../../loopx/control_plane/quota/spend_commit.ts";
+import {
+ readQuotaSpendEventFromRun,
+} from "../../loopx/control_plane/quota/accounting_artifact_transaction.ts";
const goalId = "quota-spend-transaction";
const instanceA = "ginst_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
@@ -1148,3 +1151,65 @@ test("concurrent exact-effect retries serialize to one spend", async (t) => {
const indexPath = String(results[0].payload.index_path);
assert.equal((await readFile(indexPath, "utf8")).trim().split("\n").length, 1);
});
+
+test("readQuotaSpendEventFromRun resolves checkpoint-relocated run json_path and rejects arbitrary outside paths", async (t) => {
+ const originalRoot = await tempRuntime(t);
+ const relocatedRoot = await tempRuntime(t);
+ const originalRunsDir = join(originalRoot, "goals", goalId, "runs");
+ const relocatedRunsDir = join(relocatedRoot, "goals", goalId, "runs");
+ await mkdir(originalRunsDir, { recursive: true });
+ await mkdir(relocatedRunsDir, { recursive: true });
+
+ const fileName = "2026-08-25T12-00-00Z-quota-spend.json";
+ const originalJson = join(originalRunsDir, fileName);
+ const relocatedJson = join(relocatedRunsDir, fileName);
+ await writeFile(
+ originalJson,
+ JSON.stringify({
+ classification: "quota_slot_spent",
+ goal_id: goalId,
+ effect_ref: "stale-original",
+ quota_event: { schema_version: "quota_event_v0", effect_id: "stale-original" },
+ }),
+ "utf8",
+ );
+ await writeFile(
+ relocatedJson,
+ JSON.stringify({
+ classification: "quota_slot_spent",
+ goal_id: goalId,
+ effect_ref: "quota-spend-effect-1",
+ quota_event: { schema_version: "quota_event_v0", effect_id: "quota-spend-effect-1" },
+ }),
+ "utf8",
+ );
+
+ const event = await readQuotaSpendEventFromRun(
+ relocatedRunsDir,
+ { effect_ref: "quota-spend-effect-1", json_path: originalJson },
+ goalId,
+ );
+ assert.equal(event?.effect_id, "quota-spend-effect-1");
+
+ const otherGoalRuns = join(originalRoot, "goals", "other-goal", "runs");
+ await mkdir(otherGoalRuns, { recursive: true });
+ const otherGoalJson = join(otherGoalRuns, fileName);
+ const outsideJson = join(originalRoot, "outside.json");
+ await writeFile(
+ otherGoalJson,
+ JSON.stringify({ classification: "quota_slot_spent", goal_id: goalId, effect_ref: "quota-spend-effect-1" }),
+ "utf8",
+ );
+ await writeFile(
+ outsideJson,
+ JSON.stringify({ classification: "quota_slot_spent", goal_id: goalId, effect_ref: "quota-spend-effect-1" }),
+ "utf8",
+ );
+
+ for (const unsafePath of [otherGoalJson, outsideJson]) {
+ await assert.rejects(
+ () => readQuotaSpendEventFromRun(relocatedRunsDir, { effect_ref: "quota-spend-effect-1", json_path: unsafePath }, goalId),
+ /quota spend json_path must stay inside the goal runs directory/,
+ );
+ }
+});
diff --git a/tests/control_plane_ts/steward_managed_mode.test.ts b/tests/control_plane_ts/steward_managed_mode.test.ts
index 2ee64fe2a8..20ef5b8c32 100644
--- a/tests/control_plane_ts/steward_managed_mode.test.ts
+++ b/tests/control_plane_ts/steward_managed_mode.test.ts
@@ -1,6 +1,6 @@
import assert from "node:assert/strict";
import test from "node:test";
-import {admitStewardManagedTurn, bindStewardManagedActor, planStewardManagedMode,
+import {admitStewardManagedTurn, bindStewardManagedActor, observeStewardNativeExecutor, planStewardManagedMode,
planStewardMonitor, observeStewardPortfolio,
observeStewardSignalMonitor} from "../../loopx/control_plane/collaboration/steward_managed_mode.ts";
import {planConversationBinding} from "../../loopx/control_plane/collaboration/conversation_binding.ts";
@@ -325,3 +325,152 @@ test("custom steward signal monitors admit model-free probes while excluding loc
assert.equal(unavailable.material_change, false);
assert.equal(unavailable.result_hash, first.result_hash);
});
+
+test("observeStewardNativeExecutor admits qualified codex-to-antigravity fallback while rejecting unallocated antigravity", () => {
+ const {params} = fixture();
+ assert.equal(observeStewardNativeExecutor(params).execution_available, true);
+ const pureAntigravity = {
+ binding: {...params.binding, executor_endpoint_id: "antigravity"},
+ session: {...params.session, agent_id: "antigravity", executor_endpoint_id: "antigravity"},
+ };
+ assert.deepEqual(observeStewardNativeExecutor(pureAntigravity), {
+ execution_available: false,
+ execution_error_code: "unsupported_executor",
+ });
+ const qualifiedFallback = {
+ binding: params.binding,
+ session: {
+ ...params.session,
+ agent_id: "antigravity",
+ executor_endpoint_id: "antigravity",
+ upstream_context_history: [{agent_id: "codex", thread_id: params.session.upstream_thread_id}],
+ manager_executor_allocation: {
+ schema_version: "manager_executor_allocation_v0",
+ selection_policy: "flexible",
+ configured_endpoint: "codex",
+ executor_endpoint: "antigravity",
+ allocation_reason: "flexible_availability_fallback",
+ configuration_revision: "rev-1",
+ eligible_endpoints: ["codex", "antigravity"],
+ },
+ },
+ };
+ assert.equal(observeStewardNativeExecutor(qualifiedFallback).execution_available, true);
+ assert.equal(observeStewardNativeExecutor({
+ ...qualifiedFallback,
+ session: {...qualifiedFallback.session, upstream_context_history: []},
+ }).execution_available, false);
+ const flexiblePrimaryMigrated = {
+ binding: params.binding,
+ session: {
+ ...qualifiedFallback.session,
+ manager_executor_allocation: {
+ schema_version: "manager_executor_allocation_v0",
+ selection_policy: "flexible",
+ configured_endpoint: "antigravity",
+ executor_endpoint: "antigravity",
+ allocation_reason: "flexible_primary_available",
+ configuration_revision: "rev-2",
+ eligible_endpoints: ["antigravity", "codex"],
+ },
+ },
+ };
+ assert.equal(observeStewardNativeExecutor(flexiblePrimaryMigrated).execution_available, true);
+});
+
+test("zero-effect worker exit, storage I/O, and runtime unavailability monitors admit one bounded recovery", () => {
+ const {params, goal} = fixture();
+ const actor = bindStewardManagedActor({...params, goal});
+ const prior = {operation: "native_turn", host: "codex-app-server", actor,
+ work_kind: "monitor", turn_instance_id: "monitor.transient", todo_id: "todo_transient_watch", validation_command: null,
+ monitor_deadline: "2026-11-20T07:44:39Z", monitor_lease_version: 0};
+ const monitor = {todo_id: "todo_transient_watch", task_class: "continuous_monitor",
+ target_key: "steward-portfolio-watch", watch_only: true, claimed_by: "steward", status: "open",
+ next_due_at: prior.monitor_deadline};
+ const workerExitedError =
+ "The LoopX Chat execution worker exited before this turn finished. Your conversation is saved; execution may have occurred. This turn was not retried automatically.";
+ for (const scenario of [
+ {
+ error_code: "runtime_error",
+ error: workerExitedError,
+ contexts: [{schema_version: "manager_turn_context_v1", goals: [goal]}],
+ event_kinds: ["turn.queued", "agent.phase", "manager.context", "turn.failed"],
+ },
+ {
+ error_code: "runtime_error",
+ error: "disk I/O error",
+ contexts: [{schema_version: "manager_turn_context_v1", goals: [goal]}],
+ event_kinds: ["turn.queued", "agent.phase", "manager.context", "turn.failed"],
+ },
+ {
+ error_code: "runtime_unavailable",
+ error: "The Agent runtime could not be prepared.",
+ contexts: [{schema_version: "manager_turn_context_v1", goals: [goal]}],
+ event_kinds: ["turn.queued", "turn.failed", "agent.phase", "manager.context"],
+ },
+ ]) {
+ const original = {turn_id: "4".repeat(32), session_id: params.session.session_id,
+ status: "failed", error_code: scenario.error_code, error: scenario.error,
+ origin: "native_loopx", loopx_execution: true, upstream_turn_id: null, response: null,
+ delta_count: 0, loopx_request: prior};
+ const basis = {original, contexts: scenario.contexts, phases: [{phase: "manager_context"}],
+ event_kinds: scenario.event_kinds, poll_absent: true, old_worker_excluded: true, lease_available: true};
+ const input = {...params, goal, goal_active: true, binding: {...params.binding, managed_actor: actor},
+ request: {...prior, monitor_restart_recovery: {original_turn_id: original.turn_id}},
+ monitor_todo: monitor, monitor_restart_basis: basis};
+ assert.equal(admitStewardManagedTurn(input).observation_only, true);
+ for (const bad of [
+ {contexts: []},
+ {phases: []},
+ {contexts: [{schema_version: "wrong"}]},
+ {contexts: [{schema_version: "manager_turn_context_v1"}, {schema_version: "manager_turn_context_v1"}]},
+ {poll_absent: false},
+ {old_worker_excluded: false},
+ {lease_available: false},
+ {original: {...original, upstream_turn_id: "started-upstream"}},
+ {original: {...original, loopx_request: {...prior, monitor_restart_recovery: {original_turn_id: "5".repeat(32)}}}},
+ ]) {
+ assert.throws(() => admitStewardManagedTurn({...input, monitor_restart_basis: {...basis, ...bad}}));
+ }
+ }
+});
+
+test("chained post-context transient watch carriers recover only after cooldown while scope and identity refusals remain one-shot", () => {
+ const {params, goal} = fixture();
+ const actor = bindStewardManagedActor({...params, goal});
+ const baseRequest = {operation: "native_turn", host: "codex-app-server", actor,
+ work_kind: "monitor", turn_instance_id: "monitor.chained", todo_id: "todo_chained_watch", validation_command: null,
+ monitor_deadline: "2026-11-20T07:45:00Z", monitor_lease_version: 0};
+ const completedAt = "2026-11-20T07:45:00Z";
+ const completedSec = Date.parse(completedAt) / 1000;
+ const monitor = {todo_id: "todo_chained_watch", task_class: "continuous_monitor",
+ target_key: "steward-portfolio-watch", watch_only: true, claimed_by: "steward", status: "open",
+ next_due_at: baseRequest.monitor_deadline};
+ const carrier = {turn_id: "6".repeat(32), session_id: params.session.session_id,
+ status: "failed", error_code: "runtime_unavailable",
+ error: "The Agent runtime could not be prepared.",
+ origin: "native_loopx", loopx_execution: true, upstream_turn_id: null, response: null,
+ delta_count: 0, completed_at: completedAt,
+ loopx_request: {...baseRequest, monitor_restart_recovery: {original_turn_id: "5".repeat(32)}}};
+ const basis = {original: carrier, contexts: [{schema_version: "manager_turn_context_v1", goals: [goal]}],
+ phases: [{phase: "manager_context"}],
+ event_kinds: ["turn.queued", "agent.phase", "manager.context", "turn.failed"],
+ poll_absent: true, old_worker_excluded: true, lease_available: true, observed_at: completedSec + 300};
+ const input = {...params, goal, goal_active: true, binding: {...params.binding, managed_actor: actor},
+ request: {...baseRequest, monitor_restart_recovery: {original_turn_id: carrier.turn_id}},
+ monitor_todo: monitor, monitor_restart_basis: basis};
+ assert.equal(admitStewardManagedTurn(input).observation_only, true);
+
+ for (const badBasis of [
+ {observed_at: completedSec + 299},
+ {observed_at: undefined},
+ {original: {...carrier, completed_at: undefined}},
+ {original: {...carrier, error_code: "runtime_error", error: "the portfolio observation omitted authorized Goals",
+ contexts: [{schema_version: "manager_turn_context_v1", goals: [], warnings: ["external_authorization_changed"],
+ coverage: {discovered: null, verified: 0, complete: false}}]}, observed_at: completedSec + 600},
+ {original: {...carrier, error_code: "runtime_error", error: "the selected App identity could not be verified"},
+ contexts: [], event_kinds: ["turn.queued", "agent.phase", "turn.failed"], observed_at: completedSec + 600},
+ ]) {
+ assert.throws(() => admitStewardManagedTurn({...input, monitor_restart_basis: {...basis, ...badBasis}}));
+ }
+});
diff --git a/tests/control_plane_ts/steward_reports.test.ts b/tests/control_plane_ts/steward_reports.test.ts
index c938d3314e..cb762d67c3 100644
--- a/tests/control_plane_ts/steward_reports.test.ts
+++ b/tests/control_plane_ts/steward_reports.test.ts
@@ -106,6 +106,49 @@ test("a local calendar report retains its exact original Session, subscription,
{recovery_decision: {...inspection.recovery_decision, resume_from: "host_execute"}}]) {
assert.throws(() => admitManagedSessionTurn({...input, report_settlement_recovery: {...inspection, ...changed}}));
}
+ const frozenInspection = {schema_version: "loopx_turn_journal_inspection_v1", ok: true,
+ goal_matches: true, owner_matches: true, turn_key_matches: true, journal_consistent: true,
+ journal_status: "failed", completed_phases: ["host_execute", "typed_result"],
+ recorded_effects: {host_invoked: true, state_written: false, quota_spent: false, scheduler_acknowledged: false},
+ recovery_decision: {action: "continue", resume_from: "host_execute", reinvoke_host: true}};
+ const frozenProof = {turn_key: "sha256:" + "d".repeat(64), result_kind: "validation_failed",
+ validation_stage: "host_result_contract", host_attempt_count: 1,
+ generation_verified: true, saved_candidate_verified: true};
+ assert.equal(admitManagedSessionTurn({
+ ...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: frozenProof,
+ }).observation_only, false);
+ const stoppedInspection = {schema_version: "loopx_turn_journal_inspection_v1", ok: true,
+ goal_matches: true, owner_matches: true, turn_key_matches: true, journal_consistent: true,
+ journal_status: "stopped", completed_phases: ["host_execute", "typed_result", "validation"],
+ recorded_effects: {host_invoked: true, state_written: false, quota_spent: false, scheduler_acknowledged: false},
+ recovery_decision: {action: "return_existing", resume_from: null, reinvoke_host: false}};
+ const stoppedProof = {turn_key: "sha256:" + "d".repeat(64), result_kind: "wait",
+ classification: "no_typed_host_result", host_attempt_count: 1,
+ generation_verified: true, saved_candidate_verified: true};
+ assert.equal(admitManagedSessionTurn({
+ ...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof,
+ }).observation_only, false);
+ assert.equal(admitManagedSessionTurn({
+ ...input, report_todo: {...todo, status: "done", completion_turn_key: request.turn_instance_id + ".saved-candidate"},
+ report_settlement_recovery: {schema_version: "loopx_turn_journal_inspection_v1", ok: true,
+ goal_matches: true, owner_matches: true, turn_key_matches: true, journal_consistent: true,
+ journal_status: "failed", recovery_decision: {action: "continue", reinvoke_host: false, resume_from: "quota_spend"}},
+ }).observation_only, false);
+ for (const invalid of [
+ {...input, report_frozen_contract_recovery: frozenProof},
+ {...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: {...frozenProof, saved_candidate_verified: false}},
+ {...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: {...frozenProof, validation_stage: "task_postcondition"}},
+ {...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: {...frozenProof, host_attempt_count: 2}},
+ {...input, report_settlement_recovery: {...frozenInspection, recorded_effects: {...frozenInspection.recorded_effects, state_written: true}}, report_frozen_contract_recovery: frozenProof},
+ {...input, report_todo: {...todo, status: "done"}, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: frozenProof},
+ {...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: {...stoppedProof, classification: "other"}},
+ {...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: {...stoppedProof, host_attempt_count: 2}},
+ {...input, report_settlement_recovery: {...stoppedInspection, recorded_effects: {...stoppedInspection.recorded_effects, state_written: true}}, report_frozen_contract_recovery: stoppedProof},
+ {...input, report_todo: {...todo, status: "done"}, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof},
+ {...input, preparing_model_context: true, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof},
+ ]) {
+ assert.throws(() => admitManagedSessionTurn(invalid));
+ }
});
function original() {
@@ -271,6 +314,45 @@ test("a model report requires the exact live calendar window and original task,
{...recovery, report_settlement_recovery: {...recovery.report_settlement_recovery, recovery_decision:
{action: "continue", reinvoke_host: true, resume_from: "quota_spend"}}},
]) assert.throws(() => admitStewardManagedTurn(invalid));
+ const frozenInspection = {schema_version: "loopx_turn_journal_inspection_v1", ok: true,
+ goal_matches: true, owner_matches: true, turn_key_matches: true,
+ journal_consistent: true, journal_status: "failed", completed_phases: ["host_execute", "typed_result"],
+ recorded_effects: {host_invoked: true, state_written: false, quota_spent: false, scheduler_acknowledged: false},
+ recovery_decision: {action: "continue", reinvoke_host: true, resume_from: "host_execute"}};
+ const frozenProof = {turn_key: "sha256:" + "e".repeat(64), result_kind: "validation_failed",
+ validation_stage: "host_result_contract", host_attempt_count: 1,
+ generation_verified: true, saved_candidate_verified: true};
+ assert.equal(admitStewardManagedTurn({
+ ...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: frozenProof,
+ }).observation_only, false);
+ const stoppedInspection = {schema_version: "loopx_turn_journal_inspection_v1", ok: true,
+ goal_matches: true, owner_matches: true, turn_key_matches: true,
+ journal_consistent: true, journal_status: "stopped", completed_phases: ["host_execute", "typed_result", "validation"],
+ recorded_effects: {host_invoked: true, state_written: false, quota_spent: false, scheduler_acknowledged: false},
+ recovery_decision: {action: "return_existing", reinvoke_host: false, resume_from: null}};
+ const stoppedProof = {turn_key: "sha256:" + "e".repeat(64), result_kind: "wait",
+ classification: "no_typed_host_result", host_attempt_count: 1,
+ generation_verified: true, saved_candidate_verified: true};
+ assert.equal(admitStewardManagedTurn({
+ ...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof,
+ }).observation_only, false);
+ assert.equal(admitStewardManagedTurn({
+ ...input, report_todo: {...todo, status: "done", completion_turn_key: request.turn_instance_id + ".saved-candidate"},
+ report_settlement_recovery: recovery.report_settlement_recovery,
+ }).observation_only, false);
+ for (const invalid of [
+ {...input, report_frozen_contract_recovery: frozenProof},
+ {...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: {...frozenProof, saved_candidate_verified: false}},
+ {...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: {...frozenProof, validation_stage: "task_postcondition"}},
+ {...input, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: {...frozenProof, host_attempt_count: 2}},
+ {...input, report_settlement_recovery: {...frozenInspection, recorded_effects: {...frozenInspection.recorded_effects, state_written: true}}, report_frozen_contract_recovery: frozenProof},
+ {...input, report_todo: {...todo, status: "done"}, report_settlement_recovery: frozenInspection, report_frozen_contract_recovery: frozenProof},
+ {...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: {...stoppedProof, classification: "other"}},
+ {...input, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: {...stoppedProof, host_attempt_count: 2}},
+ {...input, report_settlement_recovery: {...stoppedInspection, recorded_effects: {...stoppedInspection.recorded_effects, state_written: true}}, report_frozen_contract_recovery: stoppedProof},
+ {...input, report_todo: {...todo, status: "done"}, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof},
+ {...input, preparing_model_context: true, report_settlement_recovery: stoppedInspection, report_frozen_contract_recovery: stoppedProof},
+ ]) assert.throws(() => admitStewardManagedTurn(invalid));
const changed = {...input, binding: {...p.binding, goal_scope: "all_registered"},
session: {...p.session, active_turn_id: "running", manager_authorization_scope_id: "old_scope",
steward_context: {...p.session.steward_context, goal_scope: "all_registered"}},
@@ -510,4 +592,143 @@ test("only a proven original pre-execution replan refusal admits one correlated
({...probe, inspection: {...inspection, recorded_effects: {...inspection.recorded_effects, [key]: value}}}))),
{...probe, inspection: {...inspection, recovery_decision: {...inspection.recovery_decision, reinvoke_host: true}}},
]) assert.throws(() => admitStewardManagedTurn({...input, report_selection_recovery: {...proof, probe: changed}}));
+
+ const validationTurnKey = "sha256:" + "d".repeat(64);
+ const validationJournal = {schema_version: "loopx_turn_journal_v0", goal_id: p.actor.goal_id,
+ turn_key: validationTurnKey, status: "failed", validation_stage: "host_result_contract",
+ host: {kind: "codex-app-server", executable: "built-in"},
+ host_attempt_count: 1, result_kind: "validation_failed", completed_phases: ["host_execute", "typed_result"],
+ receipt: {ok: true, failed_phase: "validation", status: "failed",
+ commit_eligibility: {writeback: false, quota_spend: false, scheduler_ack: false}},
+ plan: {route: {kind: "replan_required", effective_action: "autonomous_replan_required"},
+ turn_envelope: {goal_id: p.actor.goal_id, agent_id: p.actor.agent_id, effective_action: "autonomous_replan_required"},
+ transaction: {settlement_plan: {identity: {goal_id: p.actor.goal_id, agent_id: p.actor.agent_id,
+ binding_kind: "autonomous_replan", turn_instance_id: request.turn_instance_id + ".replan"}}}}};
+ const validationInspection = {...inspection, journal_status: "failed"};
+ const clearedGuard = {effective_action: "normal_run", replan_action_packet: null};
+ const clearedProbe = {turn: {...source, turn_id: "d".repeat(32), delta_count: 18, upstream_turn_id: "model-turn"},
+ journal: validationJournal, inspection: validationInspection, current_guard: clearedGuard};
+ assert.equal(admitStewardManagedTurn({...input, report_selection_recovery: {...proof, probe: clearedProbe}}).observation_only, false);
+ for (const badProbe of [
+ {...clearedProbe, current_guard: {effective_action: "autonomous_replan_required", replan_action_packet: {obligation_id: "replan-1"}}},
+ {...clearedProbe, current_guard: {effective_action: "normal_run", replan_action_packet: {obligation_id: "replan-1"}}},
+ {...clearedProbe, journal: {...validationJournal, validation_stage: "task_postcondition"}},
+ {...clearedProbe, journal: {...validationJournal, receipt: {...validationJournal.receipt,
+ commit_eligibility: {writeback: true, quota_spend: false, scheduler_ack: false}}}},
+ {...clearedProbe, inspection: {...validationInspection, recorded_effects: {...inspection.recorded_effects, state_written: true}}},
+ {...clearedProbe, journal: {...validationJournal, plan: {...validationJournal.plan, transaction: {settlement_plan: {
+ identity: {...validationJournal.plan.transaction.settlement_plan.identity, turn_instance_id: "other.replan"}}}}}},
+ ]) {
+ assert.throws(() => admitStewardManagedTurn({...input, report_selection_recovery: {...proof, probe: badProbe}}));
+ }
+});
+
+test("report recovery paths tolerate host Python executable drift in validation_command and reject target drift", () => {
+ const p = original();
+ const folder = "/tmp/example-goal/periodic_reports/win-1";
+ const snippet = "from pathlib import Path; from loopx.capabilities.periodic_report.private_generation "
+ + `import validate_private_generation; validate_private_generation(Path('${folder}'))`;
+ const oldCmd = ["/opt/release-1/bin/python", "-c", snippet];
+ const newCmd = ["/opt/release-2/bin/python3", "-c", snippet];
+ const badTargetCmd = ["/opt/release-2/bin/python3", "-c", snippet.replace(folder, "/tmp/other/win-2")];
+ const badFlagCmd = ["/opt/release-2/bin/python3", "-m", snippet];
+
+ const basePrior = {operation: "native_turn", host: "codex-app-server", actor: p.actor,
+ turn_instance_id: "report.cadence_original", todo_id: "todo_report", validation_command: oldCmd,
+ work_kind: "report", report_window_id: "cadence_original", report_subscription_revision: "sha256:original"};
+ const reportMeta = {
+ report_window: {window_id: basePrior.report_window_id, subscription_revision: basePrior.report_subscription_revision,
+ goal_id: p.actor.goal_id, agent_id: p.actor.agent_id},
+ report_subscription: {enabled: true, profile_preset: "portfolio-progress",
+ route_ref: `steward-private:${p.binding.binding_id}:${p.source}`, effective_revision: basePrior.report_subscription_revision},
+ report_todo: {todo_id: basePrior.todo_id, target_key: basePrior.report_window_id,
+ claimed_by: "steward", action_kind: "generate_periodic_report", status: "open"},
+ };
+
+ // 1. report_selection_recovery with cleared validation_failed probe from an older Python release.
+ const effects = {host_invoked: false, state_written: false, quota_spent: false, scheduler_acknowledged: false};
+ const refusal = {schema_version: "loopx_turn_selection_rejection_v0", source: "quota.should-run",
+ requested_todo_id: basePrior.todo_id, state: "deferred", reason_code: "autonomous_replan",
+ recovery_action: "reenter_guard_without_selection", delivery_preemptions: ["autonomous_replan", "delivery_not_allowed"],
+ status_health_ok: true, contract_error_count: 0};
+ const selResult = {schema_version: "loopx_turn_execution_v0", mode: "run_once", ok: false,
+ error_code: "turn_todo_selection_deferred", effects_scope: "current_invocation", effects, selection_rejection: refusal};
+ const selOriginal = {session_id: p.session.session_id, status: "completed", origin: "native_loopx", loopx_execution: true,
+ delta_count: 0, upstream_turn_id: null, loopx_request: basePrior, response: {native_loopx: selResult}};
+ const probeKey = "sha256:" + "e".repeat(64);
+ const selProbe = {
+ turn: {...selOriginal, turn_id: "e".repeat(32), delta_count: 8, upstream_turn_id: "probe-model-turn"},
+ journal: {schema_version: "loopx_turn_journal_v0", goal_id: p.actor.goal_id, turn_key: probeKey,
+ status: "failed", validation_stage: "host_result_contract",
+ host: {kind: "codex-app-server", executable: "built-in"}, host_attempt_count: 1,
+ result_kind: "validation_failed", completed_phases: ["host_execute", "typed_result"],
+ receipt: {ok: true, failed_phase: "validation", status: "failed",
+ commit_eligibility: {writeback: false, quota_spend: false, scheduler_ack: false}},
+ plan: {route: {kind: "replan_required", effective_action: "autonomous_replan_required"},
+ turn_envelope: {goal_id: p.actor.goal_id, agent_id: p.actor.agent_id, effective_action: "autonomous_replan_required"},
+ transaction: {settlement_plan: {identity: {goal_id: p.actor.goal_id, agent_id: p.actor.agent_id,
+ binding_kind: "autonomous_replan", turn_instance_id: basePrior.turn_instance_id + ".replan"}}}}},
+ inspection: {schema_version: "loopx_turn_journal_inspection_v1", ok: true,
+ goal_matches: true, owner_matches: true, turn_key_matches: true, journal_consistent: true,
+ journal_status: "failed", recorded_effects: {host_invoked: true, state_written: false, quota_spent: false, scheduler_acknowledged: false},
+ recovery_decision: {action: "return_existing", reinvoke_host: false}},
+ current_guard: {effective_action: "normal_run", replan_action_packet: null},
+ };
+ const selInput = {...p, ...reportMeta, request: {...basePrior, validation_command: newCmd},
+ report_selection_recovery: {original: selOriginal, generation_present: false, probe: selProbe}};
+ assert.equal(admitStewardManagedTurn(selInput).observation_only, false);
+ for (const badCmd of [badTargetCmd, badFlagCmd]) {
+ assert.throws(() => admitStewardManagedTurn({...selInput, request: {...selInput.request, validation_command: badCmd}}));
+ }
+
+ // 2. report_quota_recovery across host Python upgrade.
+ const quotaResult = {schema_version: "loopx_turn_execution_v0", ok: false, mode: "run_once",
+ status: "failed", result_kind: "host_failure", host_failure: {schema_version: "loopx_turn_host_failure_v0", attempt: 1},
+ effects: {host_invoked: true, state_written: false, quota_spent: false, scheduler_acknowledged: false},
+ receipt: {ok: true, failed_phase: "host_execute", status: "failed", completed_phases: [],
+ lineage: {goal_id: p.actor.goal_id, agent_id: p.actor.agent_id, todo_id: basePrior.todo_id}}};
+ const quotaSource = {turn_id: "1".repeat(32), session_id: p.session.session_id, status: "completed",
+ origin: "native_loopx", loopx_execution: true, delta_count: 0, upstream_turn_id: "quota-native-turn",
+ loopx_request: basePrior, response: {native_loopx: quotaResult}};
+ const quotaObs = {thread_id: "original-native-thread", turn_id: quotaSource.upstream_turn_id,
+ only_original_user_item: true, codex_error_info: "usageLimitExceeded", quota_available: true, quota_observed_at: 100};
+ const quotaInput = {...p, ...reportMeta, session: {...p.session, upstream_thread_id: quotaObs.thread_id},
+ request: {...basePrior, validation_command: newCmd, turn_instance_id: basePrior.turn_instance_id + ".quota-recovery",
+ report_quota_recovery: {original_turn_id: quotaSource.turn_id, native_observation: quotaObs}},
+ report_quota_basis: {original: quotaSource, generation_present: false, observed_at: 101,
+ inspection: {schema_version: "loopx_turn_journal_inspection_v1", ok: true,
+ goal_matches: true, owner_matches: true, turn_key_matches: true, journal_consistent: true,
+ journal_status: "failed", completed_phases: [],
+ recorded_effects: {host_invoked: null, state_written: false, quota_spent: false, scheduler_acknowledged: false}}}};
+ assert.equal(admitStewardManagedTurn(quotaInput).observation_only, false);
+ assert.throws(() => admitStewardManagedTurn({
+ ...quotaInput, request: {...quotaInput.request, validation_command: badTargetCmd},
+ }));
+
+ // 3. report_launch_recovery across host Python upgrade.
+ const launchKey = "sha256:" + "f".repeat(64);
+ const launchResult = {schema_version: "loopx_turn_execution_v0", mode: "run_once", ok: false,
+ status: "failed", result_kind: "host_failure", reason: "first-party Host runtime rejected: stale_goal_authority",
+ host_failure: {schema_version: "loopx_turn_host_failure_v0", kind: "contract_rejected", attempt: 1, retryable: false},
+ effects: {host_invoked: true, state_written: false, quota_spent: false, scheduler_acknowledged: false},
+ resume_turn_key: launchKey};
+ const launchSource = {session_id: p.session.session_id, status: "completed", origin: "native_loopx", loopx_execution: true,
+ delta_count: 0, upstream_turn_id: null, loopx_request: basePrior, response: {native_loopx: launchResult}};
+ const launchInput = {...p, ...reportMeta,
+ request: {...basePrior, validation_command: newCmd, resume_turn_key: launchKey},
+ report_launch_recovery: {
+ original: launchSource, turn_key: launchKey, host_attempt_count: 1, generation_present: false,
+ inspection: {schema_version: "loopx_turn_journal_inspection_v1", ok: true,
+ goal_matches: true, owner_matches: true, turn_key_matches: true, journal_consistent: true,
+ journal_status: "failed", completed_phases: [],
+ recorded_effects: {host_invoked: null, state_written: false, quota_spent: false, scheduler_acknowledged: false},
+ recovery_decision: {action: "continue", reinvoke_host: true, resume_from: "host_execute"}},
+ journal: {status: "failed", result_kind: "host_failure", reason: launchResult.reason, turn_key: launchKey,
+ host_attempt_count: 1, completed_phases: [], host_failure: launchResult.host_failure,
+ receipt: {failed_phase: "host_execute"}},
+ }};
+ assert.equal(admitStewardManagedTurn(launchInput).observation_only, false);
+ assert.throws(() => admitStewardManagedTurn({
+ ...launchInput, request: {...launchInput.request, validation_command: badTargetCmd},
+ }));
});
diff --git a/tests/control_plane_ts/turn_envelope.test.ts b/tests/control_plane_ts/turn_envelope.test.ts
index 42e8a4e754..c4ca48e24a 100644
--- a/tests/control_plane_ts/turn_envelope.test.ts
+++ b/tests/control_plane_ts/turn_envelope.test.ts
@@ -477,6 +477,42 @@ test("both replan readers surface an opted-in reversal for the bound task only",
Object.keys(explore.work_yardstick_reversal as JsonObject).map(field => prefix + field));
const detail = evaluateTurnEnvelope({operation: "replan_view", payload: captured, source_hash, stage: "explore"});
assert.deepEqual((detail.explore_basis as JsonObject).work_yardsticks, cues);
+ const nativeInput: JsonObject = {
+ operation: "context_delivery",
+ surface: "complete_turn",
+ scope: {goal_id: String(source.goal_id), agent_id: String(source.agent_id),
+ session_id: "session", context_epoch: "epoch"},
+ capture_id: "c".repeat(64),
+ context: {...captured, replan_read_path: "/capture/original.json"},
+ native_projection: {
+ path: "/capture/recovery.json",
+ sha256: "a".repeat(64),
+ source_path: "/capture/recovery.json",
+ command_template: "loopx --format json quota context-read --select ''",
+ captured_decision: {path: "/capture/original.json", sha256: "c".repeat(64)},
+ },
+ consumed_pointers: {},
+ include_initial_read_plan: true,
+ observe_first_read: true,
+ };
+ const nativeResult = structuredClone(nativeInput);
+ const resolvePath = (root: unknown, path: (string | number)[]) => path.reduce(
+ (row, key) => (row as Record)[key], root);
+ for (const change of evaluateTurnEnvelope(nativeInput).changes as JsonObject[]) {
+ const path = change.path as (string | number)[], key = path.at(-1)!;
+ const parent = resolvePath(nativeResult, path.slice(0, -1)) as Record;
+ if (change.remove) delete parent[key];
+ else parent[key] = structuredClone(
+ change.from ? resolvePath(nativeInput, change.from as (string | number)[]) : change.value);
+ }
+ const planSelections = (((nativeResult.initial_read_plan as JsonObject)
+ .plan as JsonObject).selections as {pointer: string}[]);
+ assert.ok(planSelections.some(row => row.pointer === `${prefix}proxy_order_reversed`));
+ assert.equal(
+ ((nativeResult.context as JsonObject).replan_action_packet as JsonObject).review,
+ undefined,
+ );
+ assert.equal(nativeResult.projected, false);
} else {
assert.ok(!Object.hasOwn(explore, "work_yardstick_reversal"));
assert.equal(cuesInOpening.length, 0);
@@ -1930,3 +1966,50 @@ test("complete execution keeps the adaptive primary and unbound selection distin
assert.equal(result.sha256, null);
}
});
+
+test("attachCompleteTurnContext preserves tokenless identity and avoids rounding additions on source_edits", () => {
+ const source = payload();
+ const complete = evaluateTurnEnvelope({
+ operation: "compose_complete",
+ payload: source,
+ scheduler_execution_args: "",
+ });
+ const tokenlessAttached = evaluateTurnEnvelope({
+ operation: "attach_complete_turn_context",
+ payload: complete,
+ context_additions: {reward_memory_recall: {status: "empty", guidance: []}},
+ });
+ assert.deepEqual(
+ evaluateTurnEnvelope({operation: "complete_consumer", payload: tokenlessAttached}),
+ {ok: true},
+ );
+ const editResult = evaluateTurnEnvelope({
+ operation: "attach_complete_turn_context",
+ payload: complete,
+ context_additions: {post_attach_recall: {val: 1, big: 2 ** 80}},
+ context_addition_tokens: [
+ [["post_attach_recall", "val"], "1.0"],
+ [["post_attach_recall", "big"], "1208925819614629174706176"],
+ ],
+ source_edits: true,
+ });
+ const changes = editResult.changes as {path: (string | number)[]}[];
+ assert.ok(changes.length > 0);
+ assert.ok(changes.every(change => change.path[0] === "contract_binding"));
+ assert.throws(
+ () => evaluateTurnEnvelope({
+ operation: "attach_complete_turn_context",
+ payload: complete,
+ context_additions: {contract_binding: {}},
+ }),
+ EffectRuntimeRequestError,
+ );
+ assert.throws(
+ () => evaluateTurnEnvelope({
+ operation: "attach_complete_turn_context",
+ payload: complete,
+ context_additions: {goal_id: "overwrite"},
+ }),
+ EffectRuntimeRequestError,
+ );
+});
diff --git a/tests/extensions/test_periodic_report_goal_channel_delivery.py b/tests/extensions/test_periodic_report_goal_channel_delivery.py
index 6566498b65..bfd797ef81 100644
--- a/tests/extensions/test_periodic_report_goal_channel_delivery.py
+++ b/tests/extensions/test_periodic_report_goal_channel_delivery.py
@@ -12,6 +12,7 @@
build_periodic_report_generation_bundle,
build_periodic_report_source_result,
)
+from loopx.control_plane.goals.activation import build_goal_activation
from loopx.extensions.lark.goal_channel_contracts import (
GOAL_CHANNEL_BINDING_SCHEMA_VERSION,
write_goal_channel_binding,
@@ -873,7 +874,11 @@ def test_goal_channel_delivery_rejects_canonical_stop_before_send(
) -> None:
registry_path = tmp_path / ".loopx" / "registry.json"
stopped = _goal()
- stopped["activation_state"] = "stopped"
+ stopped["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-08-30T09:00:00Z",
+ reason="fixture stop",
+ )
_write_registry(registry_path, goal=stopped)
_write_binding(registry_path)
calls: list[list[str]] = []
@@ -981,7 +986,11 @@ def runner(
if "+messages-send" in args and not stopped:
stopped = True
stopped_goal = _goal()
- stopped_goal["activation_state"] = "stopped"
+ stopped_goal["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-08-30T09:00:00Z",
+ reason="fixture stop",
+ )
_write_registry(registry_path, goal=stopped_goal)
return result
diff --git a/tests/presentation/test_background_services_api.py b/tests/presentation/test_background_services_api.py
index 623b9f18d8..9e12c810e1 100644
--- a/tests/presentation/test_background_services_api.py
+++ b/tests/presentation/test_background_services_api.py
@@ -33,6 +33,7 @@ def transport(_adapter, operation, **parameters):
server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler)
server.verbose = False
server.codex_account_service = CodexAccountService(tmp_path, start_observer=False)
+ (tmp_path / "registry.json").write_text('{"schema_version": 1, "goals": []}', encoding="utf-8")
server.manager_return_service = ReturnService(tmp_path / "runtime", tmp_path / "registry.json",
ChatSessionStore(tmp_path / "runtime"), lambda *_: pytest.fail("No send is authorized"))
server.lark_goal_topic_runtime = LarkGoalTopicRuntimeService(
diff --git a/tests/presentation/test_chat_bundle.py b/tests/presentation/test_chat_bundle.py
index e1df5858e0..d4b5f0a2b3 100644
--- a/tests/presentation/test_chat_bundle.py
+++ b/tests/presentation/test_chat_bundle.py
@@ -456,3 +456,92 @@ def test_runtime_source_root_fails_closed_without_valid_release_manifest(tmp_pat
(tmp_path / "release.json").write_text(release_json, encoding="utf-8")
assert runtime_source_root(tmp_path) == tmp_path.resolve()
+
+
+def test_release_build_retries_transient_gh_errors_with_clean_download_dir(monkeypatch):
+ releases = [[{
+ "tag_name": "v2.3.0",
+ "draft": False,
+ "prerelease": False,
+ "assets": [{"name": "loopx-2.3.0-py3-none-any.whl"}],
+ }]]
+ sleeps = []
+ monkeypatch.setattr(builder.time, "sleep", sleeps.append)
+
+ api_attempts = 0
+
+ def flaky_check_output(command, **kwargs):
+ nonlocal api_attempts
+ api_attempts += 1
+ if api_attempts < 2:
+ raise subprocess.CalledProcessError(1, command, stderr="HTTP 500")
+ return json.dumps(releases)
+
+ monkeypatch.setattr(builder.subprocess, "check_output", flaky_check_output)
+
+ download_attempts = 0
+ saw_partial_marker = []
+
+ def flaky_download(command, **kwargs):
+ nonlocal download_attempts
+ download_attempts += 1
+ directory = Path(command[command.index("--dir") + 1])
+ saw_partial_marker.append((directory / "partial.tmp").exists())
+ if download_attempts < 4:
+ (directory / "partial.tmp").write_bytes(b"corrupt partial download")
+ raise subprocess.CalledProcessError(1, command)
+ wheel = directory / "loopx-2.3.0-py3-none-any.whl"
+ with zipfile.ZipFile(wheel, "w") as archive:
+ archive.writestr(
+ "loopx/web/chat/index.html",
+ '',
+ )
+ archive.writestr("loopx/web/chat/assets/prev.js", "prev code")
+ (directory / "SHA256SUMS").write_text(
+ f"{builder.contract.digest(wheel)} {wheel.name}\n"
+ )
+
+ monkeypatch.setattr(builder.subprocess, "run", flaky_download)
+ builds = []
+ monkeypatch.setattr(
+ builder,
+ "build",
+ lambda previous, **kwargs: builds.append((previous / "assets/prev.js").read_text()),
+ )
+
+ builder.release_build("v2.4.0", "fixture/repository")
+
+ assert api_attempts == 2
+ assert download_attempts == 4
+ assert saw_partial_marker == [False, False, False, False]
+ assert sleeps == [1.0, 1.0, 2.0, 4.0]
+ assert builds == ["prev code"]
+
+
+def test_release_build_exhausts_bounded_gh_download_retries(monkeypatch):
+ releases = [[{
+ "tag_name": "v2.3.0",
+ "draft": False,
+ "prerelease": False,
+ "assets": [{"name": "loopx-2.3.0-py3-none-any.whl"}],
+ }]]
+ sleeps = []
+ monkeypatch.setattr(builder.time, "sleep", sleeps.append)
+ monkeypatch.setattr(
+ builder.subprocess, "check_output", lambda *a, **k: json.dumps(releases)
+ )
+
+ download_attempts = 0
+
+ def always_fail(command, **kwargs):
+ nonlocal download_attempts
+ download_attempts += 1
+ raise subprocess.CalledProcessError(1, command)
+
+ monkeypatch.setattr(builder.subprocess, "run", always_fail)
+
+ with pytest.raises(subprocess.CalledProcessError):
+ builder.release_build("v2.4.0", "fixture/repository")
+
+ assert download_attempts == 4
+ assert sleeps == [1.0, 2.0, 4.0]
diff --git a/tests/presentation/test_openai_relay.py b/tests/presentation/test_openai_relay.py
index 2b993d31f9..f3f9acc02c 100644
--- a/tests/presentation/test_openai_relay.py
+++ b/tests/presentation/test_openai_relay.py
@@ -1,9 +1,11 @@
"""Real loopback HTTP and private store, with synthetic upstream transport."""
import importlib
+import http.client
import io
import json
import os
from pathlib import Path
+import socketserver
import threading
import urllib.error
import urllib.request
@@ -45,13 +47,16 @@ def open(self, request, timeout):
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
body = {"model": "unchanged-selected-model", "input": "synthetic prompt", "stream": True, "tools": []}
- def post(token="example-loopback-token", origin=None):
+ def post(token="example-loopback-token", origin=None, *, return_response=False):
headers = {"Authorization": "Bearer " + token, "Content-Type": "application/json"}
if origin:
headers["Origin"] = origin
request = urllib.request.Request(f"http://127.0.0.1:{server.server_port}/v1/responses", data=json.dumps(body).encode(), headers=headers)
try:
- with urllib.request.urlopen(request, timeout=10) as response:
+ response = urllib.request.urlopen(request, timeout=10)
+ if return_response:
+ return response
+ with response:
return response.status, response.read()
except urllib.error.HTTPError as error:
return error.code, error.read()
@@ -98,9 +103,218 @@ def read1(self, _size):
return self.read()
partial = b'data: {"type":"response.created"}\n\n'
outcomes.append(Interrupted(partial))
- status, result = post()
- assert status == 200 and result == partial and len(calls) == 1
- assert b"response.completed" not in result
+ with pytest.raises(http.client.IncompleteRead) as caught:
+ post()
+ assert caught.value.partial == partial and len(calls) == 1
+
+
+def test_bad_upstream_headers_return_one_unknown_failure(relay):
+ _, keys, calls, outcomes, post, _ = relay
+ outcomes.append(http.client.LineTooLong("upstream header"))
+ status, response = post()
+ assert status == 502 and json.loads(response)["error"]["code"] == "upstream_unavailable"
+ assert len(calls) == 1
+ assert all(row["runtime"] is None for row in keys.status()["keys"])
+
+
+@pytest.mark.parametrize("oversized", [False, True])
+def test_unverified_rejection_body_cannot_trigger_another_key(relay, oversized):
+ _, keys, calls, outcomes, post, _ = relay
+
+ class TruncatedRejection(io.BytesIO):
+ def read(self, *_args):
+ raise http.client.IncompleteRead(b'{"error":')
+
+ outcomes.extend([
+ urllib.error.HTTPError(
+ "https://api.openai.com/v1/responses", 429, "fixture", {},
+ io.BytesIO(b"x" * 65537) if oversized else TruncatedRejection(),
+ ),
+ Response(b"must-not-be-used"),
+ ])
+ status, response = post()
+ assert status == 502 and json.loads(response)["error"]["code"] == "upstream_unavailable"
+ assert len(calls) == 1
+ assert all(row["runtime"] is None for row in keys.status()["keys"])
+
+
+def test_unavailable_key_observation_does_not_discard_admitted_response(relay, monkeypatch):
+ _, keys, calls, outcomes, post, _ = relay
+
+ def unavailable(*_args, **_kwargs):
+ raise BlockingIOError("fixture observation lock")
+
+ monkeypatch.setattr(type(keys), "record", unavailable)
+ stream = b'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
+ outcomes.append(Response(stream))
+ assert post() == (200, stream) and len(calls) == 1
+ assert all(row["runtime"] is None for row in keys.status()["keys"])
+
+
+@pytest.mark.parametrize(
+ ("framing", "complete"), [
+ ("length", False), ("chunked", False), ("length", True), ("chunked", True),
+ ],
+)
+def test_loopback_truncation_framing_and_exact_tool_prefix(relay, framing, complete):
+ _, _, calls, outcomes, post, body = relay
+ prefix = b'data: {"type":"response.output_item.added","item":{"type":"function_call","call_id":"fixture"}}\n\n'
+ terminal = b'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
+ upstream_calls = []
+ upstream_finished = threading.Event()
+ downstream_saw_prefix = threading.Event()
+
+ class Upstream(socketserver.StreamRequestHandler):
+ def handle(self):
+ self.rfile.readline(4097)
+ headers = http.client.parse_headers(self.rfile)
+ upstream_calls.append(self.rfile.read(int(headers["Content-Length"])))
+ self.wfile.write(b"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\n")
+ if framing == "length":
+ self.wfile.write(f"Content-Length: {len(prefix + terminal)}\r\n\r\n".encode())
+ self.wfile.write(prefix)
+ else:
+ self.wfile.write(b"Transfer-Encoding: chunked\r\n\r\n")
+ self.wfile.write(f"{len(prefix):x}\r\n".encode() + prefix + b"\r\n")
+ self.wfile.flush()
+ try:
+ if complete:
+ # The relay must send the first tool item before the
+ # upstream sends a terminal item or closes the stream.
+ if not downstream_saw_prefix.wait(timeout=5):
+ return
+ if framing == "length":
+ self.wfile.write(terminal)
+ else:
+ self.wfile.write(f"{len(terminal):x}\r\n".encode() + terminal + b"\r\n0\r\n\r\n")
+ finally:
+ upstream_finished.set()
+
+ with socketserver.TCPServer(("127.0.0.1", 0), Upstream) as server:
+ serve = threading.Thread(target=server.serve_forever, daemon=True)
+ serve.start()
+ try:
+ connection = http.client.HTTPConnection(*server.server_address, timeout=5)
+ try:
+ connection.request("POST", "/v1/responses", json.dumps(body).encode())
+ outcomes.append(connection.getresponse())
+ if complete:
+ with post(return_response=True) as response:
+ assert response.status == 200
+ assert response.read(len(prefix)) == prefix
+ assert not upstream_finished.is_set()
+ downstream_saw_prefix.set()
+ assert response.read() == terminal
+ else:
+ with pytest.raises(http.client.IncompleteRead) as caught:
+ post()
+ assert caught.value.partial == prefix
+ assert len(calls) == len(upstream_calls) == 1
+ assert calls[0].data == upstream_calls[0] == json.dumps(body).encode()
+ finally:
+ downstream_saw_prefix.set()
+ connection.close()
+ finally:
+ server.shutdown()
+ serve.join(timeout=5)
+
+
+def test_valid_quota_json_in_short_content_length_cannot_rotate(relay):
+ _, keys, calls, outcomes, post, _ = relay
+ payload = json.dumps({"error": {"code": "insufficient_quota"}}).encode()
+ with socketserver.TCPServer(("127.0.0.1", 0), WireResponse) as server:
+ server.wire_response = (
+ f"HTTP/1.1 429 Too Many Requests\r\nContent-Length: {len(payload) + 1}\r\n"
+ "Content-Type: application/json\r\nConnection: close\r\n\r\n"
+ ).encode() + payload
+ serve = threading.Thread(target=server.serve_forever, daemon=True)
+ serve.start()
+ try:
+ try:
+ urllib.request.urlopen(f"http://127.0.0.1:{server.server_address[1]}/", timeout=5)
+ except urllib.error.HTTPError as rejection:
+ outcomes.extend([rejection, Response(b"must-not-be-used")])
+ else:
+ pytest.fail("fixture did not return HTTP 429")
+ status, response = post()
+ assert status == 502 and json.loads(response)["error"]["code"] == "upstream_unavailable"
+ assert len(calls) == 1
+ assert all(row["runtime"] is None for row in keys.status()["keys"])
+ finally:
+ server.shutdown()
+ serve.join(timeout=5)
+
+
+class WireResponse(socketserver.StreamRequestHandler):
+ def handle(self):
+ self.rfile.readline(4097)
+ http.client.parse_headers(self.rfile)
+ self.wfile.write(self.server.wire_response)
+
+
+@pytest.mark.parametrize("status", [200, 429])
+@pytest.mark.parametrize(("delimiter", "trailer", "complete"), [
+ pytest.param(b"\r\n", b"0\r\n", False, id="missing-final-line"),
+ pytest.param(b"\r\n", b"0\r\nX-Fixture: retained\r\n", False, id="truncated-trailer"),
+ pytest.param(b"\r\n", b"0\r\n\r\n", True, id="no-trailer-fields"),
+ pytest.param(b"\r\n", b"0\r\nX-Fixture: retained\r\n\r\n", True, id="complete-trailer"),
+ pytest.param(b" ", b"0\r\n\r\n", False, id="malformed-data-delimiter"),
+])
+def test_chunked_upstream_requires_complete_framing(relay, monkeypatch, status, delimiter, trailer, complete):
+ owner, keys, calls, outcomes, post, _ = relay
+ module = importlib.import_module("loopx_codex_provider_routing.openai_relay")
+ # Keep the production HTTPS response parser while serving a raw local
+ # socket fixture. These cases exercise framing above the TLS transport.
+ monkeypatch.setattr(http.client.HTTPSConnection, "connect", http.client.HTTPConnection.connect)
+ monkeypatch.setenv("no_proxy", "127.0.0.1")
+ prefix = b'data: {"type":"response.created"}\n\n'
+ error = b'{"error":{"code":"insufficient_quota"}}'
+ payload = prefix if status == 200 else error
+ status_line = "200 OK" if status == 200 else "429 Too Many Requests"
+ content_type = "text/event-stream" if status == 200 else "application/json"
+ midpoint = len(payload) // 2
+ first, second = payload[:midpoint], payload[midpoint:]
+ chunks = f"{len(first):x}\r\n".encode() + first + b"\r\n"
+ chunks += f"{len(second):x}\r\n".encode() + second + delimiter
+ with module.RelayServer(owner, {"port": 0}) as relay_server:
+ origin_opener = relay_server.opener
+ with socketserver.TCPServer(("127.0.0.1", 0), WireResponse) as server:
+ server.wire_response = (
+ f"HTTP/1.1 {status_line}\r\nTransfer-Encoding: chunked\r\n"
+ f"Content-Type: {content_type}\r\nConnection: close\r\n\r\n"
+ ).encode() + chunks + trailer
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+ try:
+ url = f"https://127.0.0.1:{server.server_address[1]}/"
+ if status == 200:
+ outcomes.append(origin_opener.open(url, timeout=5))
+ if complete:
+ assert post() == (200, prefix)
+ else:
+ with pytest.raises(http.client.IncompleteRead) as caught:
+ post()
+ assert caught.value.partial == prefix
+ assert len(calls) == 1
+ else:
+ with pytest.raises(urllib.error.HTTPError) as rejected:
+ origin_opener.open(url, timeout=5)
+ fallback = b'{"accepted":true}'
+ outcomes.extend([rejected.value, Response(fallback)])
+ if complete:
+ assert post() == (200, fallback)
+ assert len(calls) == 2
+ assert calls[0].data == calls[1].data
+ assert calls[0].get_header("Authorization") != calls[1].get_header("Authorization")
+ else:
+ response_status, response = post()
+ assert response_status == 502
+ assert json.loads(response)["error"]["code"] == "upstream_unavailable"
+ assert len(calls) == 1
+ assert all(row["runtime"] is None for row in keys.status()["keys"])
+ finally:
+ server.shutdown()
+ thread.join(timeout=5)
def test_pool_exhaustion_is_bounded_and_does_not_probe_in_background(relay):
diff --git a/tests/test_antigravity_steward_adapter.py b/tests/test_antigravity_steward_adapter.py
index ffb9c81a8e..8e56ccad56 100644
--- a/tests/test_antigravity_steward_adapter.py
+++ b/tests/test_antigravity_steward_adapter.py
@@ -1,4 +1,4 @@
-"""Focused tests for Antigravity (Gemini 4 Argon) Steward & Chat adapter."""
+"""Focused tests for the Antigravity Steward & Chat adapter."""
from __future__ import annotations
@@ -122,14 +122,15 @@ def do_POST(self):
self.end_headers()
return
if method == "GetCascadeModelConfigData":
- response = {"clientModelConfigs": [
+ response = {"clientModelConfigs": state.get("model_configs", [
{"modelId": f"gemini-4-argon-{tier}",
"modelOrAlias": {"model": f"MODEL_PLACEHOLDER_M{number}"}}
- for tier, number in (("high", 417), ("medium", 418), ("low", 419))]}
+ for tier, number in (("high", 417), ("medium", 418), ("low", 419))])}
elif method == "GetUserStatus":
from datetime import timedelta
reset = state.setdefault("quota_reset", (datetime.now(timezone.utc) + timedelta(days=7)).isoformat())
- response = {"userStatus": {"email": state.get("quota_account", "synthetic@invalid.example"),
+ response = state["user_status_response"] if "user_status_response" in state else {
+ "userStatus": {"email": state.get("quota_account", "synthetic@invalid.example"),
"cascadeModelConfigData": {"clientModelConfigs": [{
"modelId": "gemini-4-argon-high", "quotaInfo": {
"remainingFraction": state.get("quota_fraction", 1), "resetTime": reset}}]}}}
@@ -779,6 +780,115 @@ def test_canonical_model_and_effort_readback_agree(tmp_path, language_server):
assert observed_model_settings(adapter, "low", "argon")["agent_reasoning_effort"] == "low"
+@pytest.mark.parametrize("effort,placeholder", [
+ ("high", "MODEL_PLACEHOLDER_M427"),
+ ("medium", "MODEL_PLACEHOLDER_M437"),
+ ("low", "MODEL_PLACEHOLDER_M438"),
+])
+def test_carbon_native_request_and_same_conversation_resume(tmp_path, language_server, effort, placeholder):
+ from loopx.capabilities.native_chat.model_settings import observed_model_settings
+
+ model = f"gemini-4-carbon-{effort}"
+ language_server["model_configs"] = [{"modelId": model, "modelOrAlias": {"model": placeholder}}]
+ adapter = adapter_for(tmp_path, executor_model={"model": "carbon", "reasoning_effort": effort})
+ assert adapter.current_model_id == model
+ assert observed_model_settings(adapter, effort, "carbon")["agent_reasoning_effort"] == effort
+ adapter.start_turn("Synthetic Carbon request")
+ adapter.close_session()
+ resumed = adapter_for(tmp_path, resume_thread_id="synthetic-cascade",
+ executor_model={"model": model, "reasoning_effort": effort})
+ resumed.start_turn("Continue the same synthetic conversation")
+ sends = [body for method, body in language_server["calls"] if method == "SendUserCascadeMessage"]
+ assert len(sends) == 2
+ for body in sends:
+ assert body["cascadeId"] == "synthetic-cascade"
+ planner = body["cascadeConfig"]["plannerConfig"]
+ assert planner["planModel"] == placeholder
+ assert planner["requestedModel"] == {"model": placeholder}
+ assert planner["toolConfig"]["forceDisableToolCalling"] is True
+ assert sum(method == "StartCascade" for method, _ in language_server["calls"]) == 1
+
+
+@pytest.mark.parametrize("configs", [[], [
+ {"modelId": "another-model", "modelOrAlias": {"model": "MODEL_PLACEHOLDER_M427"}},
+], [{"modelId": "gemini-4-carbon-high", "modelOrAlias": "malformed"}]])
+def test_carbon_not_offered_by_account_cannot_create_or_resume(tmp_path, language_server, configs):
+ from loopx.chat_agent import CodexChatAgentError
+
+ language_server["model_configs"] = configs
+ with pytest.raises(CodexChatAgentError, match="did not offer"):
+ adapter_for(tmp_path, resume_thread_id="original",
+ executor_model={"model": "gemini-4-carbon-high", "reasoning_effort": "high"})
+ assert {method for method, _ in language_server["calls"]} == {"GetCascadeModelConfigData"}
+
+
+@pytest.mark.parametrize("model,effort", [("carbon", "xhigh"), ("gemini-4-carbon-high", "low"),
+ ("gemini-4-carbon-unknown", None)])
+def test_invalid_carbon_selection_is_rejected_before_native_reads(tmp_path, language_server, model, effort):
+ from loopx.chat_agent import CodexChatAgentError
+
+ with pytest.raises(CodexChatAgentError) as error:
+ adapter_for(tmp_path, executor_model={"model": model, "reasoning_effort": effort})
+ assert error.value.error_code == "unsupported_configuration"
+ assert language_server["calls"] == []
+
+
+def test_model_discovery_http_reads_account_choices_without_execution_and_can_recover(
+ tmp_path, language_server, monkeypatch,
+):
+ import http.client
+ from loopx.chat_runtime import ChatRuntimeController
+ from loopx.chat_server import ChatHTTPServer, ChatRequestHandler
+ from loopx.chat_store import ChatSessionStore
+
+ store = ChatSessionStore(tmp_path / "runtime")
+ runtime = ChatRuntimeController(store=store, codex_bin="unused")
+ monkeypatch.setattr(runtime, "capabilities", lambda: [{"agent_id": "antigravity", "available": True}])
+ server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler)
+ server.verbose = False
+ server.chat_store, server.runtime_controller = store, runtime
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+
+ def read():
+ connection = http.client.HTTPConnection("127.0.0.1", server.server_port, timeout=5)
+ try:
+ connection.request("POST", "/api/chat/agent-model-options", body=json.dumps({"agent_id": "antigravity"}),
+ headers={"Content-Type": "application/json",
+ "Origin": f"http://127.0.0.1:{server.server_port}"})
+ response = connection.getresponse()
+ return response.status, json.loads(response.read())
+ finally:
+ connection.close()
+
+ try:
+ status, argon = read()
+ assert status == 200, argon
+ assert [row["value"] for row in argon["model"]["options"]] == [
+ "gemini-4-argon-high", "gemini-4-argon-medium", "gemini-4-argon-low"]
+ assert argon["model"]["selected"] == "gemini-4-argon-high"
+ language_server["model_configs"] = []
+ status, failed = read()
+ assert status == 424 and failed["error_code"] == "unsupported_configuration"
+ language_server["model_configs"] = [
+ {"modelId": "gemini-4-carbon-medium", "modelOrAlias": {"model": "MODEL_PLACEHOLDER_M437"}},
+ {"modelId": "unknown-model", "modelOrAlias": {"model": "MODEL_PLACEHOLDER_M999"}},
+ ]
+ status, recovered = read()
+ assert status == 200, recovered
+ assert recovered["model"]["selected"] is None
+ assert recovered["model"]["options"] == [{"value": "gemini-4-carbon-medium",
+ "label": "Gemini 4 Carbon (Medium)", "description": "", "is_default": False,
+ "default_reasoning_effort": "medium", "reasoning_efforts": ["medium"]}]
+ assert [method for method, _ in language_server["calls"]] == ["GetCascadeModelConfigData"] * 3
+ assert store.list_sessions() == []
+ finally:
+ server.shutdown()
+ thread.join(timeout=5)
+ server.server_close()
+ runtime.close()
+
+
def test_failed_resume_does_not_create_a_hidden_new_conversation(tmp_path, language_server):
from loopx.chat_agent import CodexChatAgentError
language_server["missing_resume"] = True
@@ -1512,3 +1622,194 @@ def test_antigravity_attachment_turn_sends_inline_media(tmp_path, language_serve
"inlineData": base64.b64encode(png_bytes).decode("ascii"),
"description": "screen.png",
}]
+
+
+def test_carbon_and_argon_offered_together_with_argon_product_default(tmp_path, language_server, monkeypatch):
+ import http.client
+ from loopx.capabilities.native_chat.antigravity_adapter import (
+ ANTIGRAVITY_MODEL_OPTIONS,
+ ANTIGRAVITY_MODEL_PLACEHOLDERS,
+ )
+ from loopx.chat_runtime import ChatRuntimeController
+ from loopx.chat_server import ChatHTTPServer, ChatRequestHandler
+ from loopx.chat_store import ChatSessionStore
+
+ ordered_ids = [item["id"] for item in ANTIGRAVITY_MODEL_OPTIONS]
+ assert ordered_ids[:3] == [
+ "gemini-4-argon-high",
+ "gemini-4-argon-medium",
+ "gemini-4-argon-low",
+ ]
+ assert ANTIGRAVITY_MODEL_PLACEHOLDERS["gemini-4-carbon-high"] == "MODEL_PLACEHOLDER_M427"
+
+ language_server["model_configs"] = [
+ {"modelOrAlias": {"model": "MODEL_PLACEHOLDER_M417"}},
+ {"modelOrAlias": {"model": "MODEL_PLACEHOLDER_M427"}},
+ ]
+ store = ChatSessionStore(tmp_path / "runtime")
+ runtime = ChatRuntimeController(store=store, codex_bin="unused")
+ monkeypatch.setattr(runtime, "capabilities", lambda: [{"agent_id": "antigravity", "available": True}])
+ server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler)
+ server.verbose = False
+ server.chat_store, server.runtime_controller = store, runtime
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+ try:
+ connection = http.client.HTTPConnection("127.0.0.1", server.server_port, timeout=5)
+ try:
+ connection.request(
+ "POST",
+ "/api/chat/agent-model-options",
+ body=json.dumps({"agent_id": "antigravity"}),
+ headers={"Content-Type": "application/json", "Origin": f"http://127.0.0.1:{server.server_port}"},
+ )
+ response = connection.getresponse()
+ assert response.status == 200
+ payload = json.loads(response.read())
+ finally:
+ connection.close()
+ assert [row["value"] for row in payload["model"]["options"]] == [
+ "gemini-4-argon-high",
+ "gemini-4-carbon-high",
+ ]
+ assert payload["model"]["selected"] == "gemini-4-argon-high"
+ assert payload["model"]["options"][0]["is_default"] is True
+ assert payload["model"]["options"][1]["is_default"] is False
+ finally:
+ server.shutdown()
+ thread.join(timeout=5)
+ server.server_close()
+ runtime.close()
+
+
+def test_capacity_observation_matches_model_or_alias_without_model_id(tmp_path, language_server):
+ from datetime import timedelta
+ from loopx.capabilities.native_chat.antigravity_adapter import AntigravityAdapter
+
+ language_server["model_configs"] = [
+ {
+ "label": "Gemini 4 Carbon (High)",
+ "modelOrAlias": {"model": "MODEL_PLACEHOLDER_M427"},
+ }
+ ]
+ language_server["steps"] = [
+ {
+ "type": "CORTEX_STEP_TYPE_USER_INPUT",
+ "status": "CORTEX_STEP_STATUS_DONE",
+ "metadata": {"sourceTrajectoryStepInfo": {"stepIndex": 0}, "executionId": "exec-carbon"},
+ "userInput": {"items": [{"text": "probe"}]},
+ },
+ {
+ "type": "CORTEX_STEP_TYPE_PLANNER_RESPONSE",
+ "status": "CORTEX_STEP_STATUS_DONE",
+ "metadata": {"sourceTrajectoryStepInfo": {"stepIndex": 1}, "executionId": "exec-carbon"},
+ "plannerResponse": {"response": "Carbon answer", "stopReason": "STOP_REASON_STOP_PATTERN"},
+ },
+ ]
+ reset = (datetime.now(timezone.utc) + timedelta(days=3)).isoformat()
+ language_server["user_status_response"] = {
+ "userStatus": {
+ "email": "synthetic@invalid.example",
+ "cascadeModelConfigData": {
+ "clientModelConfigs": [
+ {
+ "label": "Gemini 4 Carbon (High)",
+ "modelOrAlias": {"model": "MODEL_PLACEHOLDER_M427"},
+ "quotaInfo": {"remainingFraction": 0.99, "resetTime": reset},
+ }
+ ]
+ },
+ }
+ }
+ adapter = adapter_for(
+ tmp_path,
+ executor_model={"model": "gemini-4-carbon-high", "reasoning_effort": "high"},
+ )
+ adapter.start_turn("probe")
+ reviewed = AntigravityAdapter.metadata_review_capacity(
+ cascade_id="synthetic-cascade",
+ model_id="gemini-4-carbon-high",
+ )
+ assert reviewed is not None and reviewed["model_id"] == "gemini-4-carbon-high"
+ assert reviewed["remaining_fraction"] == 0.99
+ endpoint_cap = AntigravityAdapter.endpoint_model_capacity(model_id="gemini-4-carbon-high")
+ assert endpoint_cap is not None and endpoint_cap["model_id"] == "gemini-4-carbon-high"
+ assert endpoint_cap["remaining_fraction"] == 0.99 and endpoint_cap["normal_completion"] is True
+
+
+def test_antigravity_launch_guard_runs_pre_publication_once(tmp_path, language_server):
+ from contextlib import contextmanager
+
+ adapter = adapter_for(tmp_path)
+ events = []
+
+ @contextmanager
+ def guard():
+ events.append(("guard_enter", len([m for m, _ in language_server["calls"] if m == "SendUserCascadeMessage"])))
+ yield
+ events.append(("guard_exit", len([m for m, _ in language_server["calls"] if m == "SendUserCascadeMessage"])))
+
+ adapter._launch_guard = guard
+ result = adapter.start_turn("guarded turn")
+ assert result["message"] == "Argon answer 1"
+ assert events == [("guard_enter", 0), ("guard_exit", 1)]
+
+
+def test_antigravity_endpoint_model_capacity_resolves_short_aliases_and_rejects_mismatches(
+ language_server,
+) -> None:
+ from datetime import timedelta
+
+ reset = (datetime.now(timezone.utc) + timedelta(days=7)).isoformat()
+ language_server["user_status_response"] = {
+ "userStatus": {
+ "email": "synthetic@invalid.example",
+ "cascadeModelConfigData": {
+ "clientModelConfigs": [
+ {
+ "label": "Gemini 4 Argon (High)",
+ "modelOrAlias": {"model": "MODEL_PLACEHOLDER_M417"},
+ "quotaInfo": {"remainingFraction": 0.42, "resetTime": reset},
+ },
+ {
+ "label": "Gemini 4 Carbon (High)",
+ "modelOrAlias": {"model": "MODEL_PLACEHOLDER_M427"},
+ "quotaInfo": {"remainingFraction": 0.85, "resetTime": reset},
+ },
+ {
+ "label": "Gemini 4 Carbon (Medium)",
+ "modelOrAlias": {"model": "MODEL_PLACEHOLDER_M437"},
+ "quotaInfo": {"remainingFraction": 0.0, "resetTime": reset},
+ },
+ ]
+ },
+ }
+ }
+
+ for alias, effort, expected_canonical, expected_fraction, expected_exhausted in [
+ ("carbon", None, "gemini-4-carbon-high", 0.85, False),
+ ("CARBON", None, "gemini-4-carbon-high", 0.85, False),
+ ("gemini-4-carbon", None, "gemini-4-carbon-high", 0.85, False),
+ ("argon", None, "gemini-4-argon-high", 0.42, False),
+ ("gemini-4-argon", None, "gemini-4-argon-high", 0.42, False),
+ ("carbon", "medium", "gemini-4-carbon-medium", 0.0, True),
+ ]:
+ observed = AntigravityAdapter.endpoint_model_capacity(
+ model_id=alias,
+ reasoning_effort=effort,
+ )
+ assert observed is not None, (alias, effort)
+ assert observed["model_id"] == expected_canonical
+ assert observed["remaining_fraction"] == expected_fraction
+ assert observed["quota_exhausted"] is expected_exhausted
+ assert observed["normal_completion"] is (not expected_exhausted)
+
+ calls_before = len(language_server["calls"])
+ assert AntigravityAdapter.endpoint_model_capacity(model_id="unknown-model") is None
+ assert AntigravityAdapter.endpoint_model_capacity(
+ model_id="gemini-4-carbon-high", reasoning_effort="medium",
+ ) is None
+ assert AntigravityAdapter.endpoint_model_capacity(
+ model_id="carbon", reasoning_effort="xhigh",
+ ) is None
+ assert len(language_server["calls"]) == calls_before
diff --git a/tests/test_chat_agent.py b/tests/test_chat_agent.py
index 749ada7483..a620eb8e9b 100644
--- a/tests/test_chat_agent.py
+++ b/tests/test_chat_agent.py
@@ -1140,6 +1140,45 @@ def write(packet):
assert proof is None
+@pytest.mark.parametrize("compact", [False, True])
+@pytest.mark.parametrize("fault", [None, "external-url", "status-403", "disconnect-variant", "tool", "answer", "older-compaction"])
+def test_loopback_relay_auth_readback_is_strictly_bounded(tmp_path, monkeypatch, compact, fault):
+ session = chat_agent.CodexChatAgentSession(process=SimpleNamespace(poll=lambda: None),
+ messages=queue.Queue(), thread_id="relay-thread", work_dir=tmp_path, response_timeout_sec=.2)
+ url = "https://api.openai.com/v1/responses" if fault == "external-url" else "http://127.0.0.1:57500/v1/responses"
+ status = 403 if fault == "status-403" else 401
+ variant = "responseStreamDisconnected" if fault == "disconnect-variant" else "httpConnectionFailed"
+ prefix = "Error running remote compact task: " if compact else ""
+ message = f"{prefix}unexpected status {status}: OpenAI API fallback request could not complete., url: {url}"
+
+ def write(packet):
+ method = packet["method"]
+ if method == "thread/turns/list":
+ rows = [{"id": "failed", "status": "failed", "error": {
+ "codexErrorInfo": {variant: {"httpStatusCode": status}}, "message": message}}]
+ if fault == "older-compaction":
+ rows.insert(0, {"id": "newer", "status": "completed"})
+ result = {"data": rows}
+ else:
+ items = [{"turnId": "failed", "item": {"id": "user", "type": "userMessage"}}]
+ if compact:
+ items.append({"turnId": "failed", "item": {"id": "compact", "type": "contextCompaction"}})
+ if fault in {"tool", "answer"}:
+ items.append({"turnId": "failed", "item": {"id": "extra",
+ "type": "commandExecution" if fault == "tool" else "agentMessage"}})
+ result = {"data": items}
+ session.messages.put({"id": packet["id"], "result": result})
+
+ monkeypatch.setattr(session, "_write", write)
+ proof = failed_request_readback(session, "failed")
+ if fault is None or (fault == "older-compaction" and not compact):
+ assert proof == {"thread_id": "relay-thread", "turn_id": "failed",
+ "only_original_user_item": not compact, "only_pre_execution_items": True,
+ "codex_error_info": {variant: {"httpStatusCode": 401}}, "loopback_relay_auth_failure": True}
+ else:
+ assert proof is None
+
+
@pytest.mark.parametrize("message", [None, {}, [], "workspace routing discovery timed out: extra",
"workspace routing discovery failed: extra", "Please retry: workspace routing discovery failed", "unknown"])
def test_routing_diagnostic_is_an_exact_provider_diagnostic(message):
@@ -2730,3 +2769,91 @@ def test_native_child_callback_is_scoped_to_owned_thread_and_turn(monkeypatch, t
observed = []
session.send("Reply briefly.", on_native_item=observed.append)
assert observed == [item]
+
+
+def test_turn_retry_keeps_the_adapter_selected_before_failed_start(tmp_path, monkeypatch):
+ import runpy
+ from concurrent.futures import ThreadPoolExecutor
+
+ from loopx.chat_store import ChatSessionStore
+
+ native_calls = tmp_path / "native-calls.jsonl"
+ fixture = Path(__file__).resolve().parents[1] / "examples" / "loopx-chat-runtime-smoke.py"
+ source = runpy.run_path(str(fixture))["FAKE_CODEX"]
+ source = source.replace(
+ ' method = request.get("method")',
+ ' method = request.get("method")\n'
+ f" with open({str(native_calls)!r}, 'a') as log:\n"
+ ' print(json.dumps(method), file=log)\n',
+ 1,
+ )
+ executable = tmp_path / "codex"
+ executable.write_text(source, encoding="utf-8")
+ executable.chmod(0o700)
+ store = ChatSessionStore(tmp_path / "runtime")
+ runtime = chat_runtime.ChatRuntimeController(store=store, codex_bin=str(executable))
+ home = tmp_path / "codex-home"
+ home.mkdir()
+ runtime.codex_home = home
+ first_starting = threading.Event()
+ retry_selected_adapter = threading.Event()
+ first_returned = threading.Event()
+ try:
+ session, _ = runtime.open_session(
+ goal_id="fixture", agent_id="codex", work_dir=tmp_path,
+ objective="Keep this native conversation.", mode="new",
+ )
+ session_id = session["session_id"]
+ adapter = runtime.adapters[session_id]
+ start_worker = runtime._start_accepted_turn_worker
+ start_thread = threading.Thread.start
+
+ def hold_retry_before_worker_registration(**kwargs):
+ if first_starting.is_set():
+ assert kwargs["adapter"] is adapter
+ retry_selected_adapter.set()
+ assert first_returned.wait(timeout=5)
+ return start_worker(**kwargs)
+
+ def fail_first_worker(thread):
+ if getattr(thread, "_target", None) == runtime._run_turn and not first_starting.is_set():
+ first_starting.set()
+ assert retry_selected_adapter.wait(timeout=5)
+ raise RuntimeError("can't start new thread")
+ start_thread(thread)
+
+ monkeypatch.setattr(runtime, "_start_accepted_turn_worker", hold_retry_before_worker_registration)
+ monkeypatch.setattr(threading.Thread, "start", fail_first_worker)
+
+ def submit():
+ return runtime.submit_turn(
+ session_id=session_id, client_turn_id="same-turn", message="complete normally",
+ work_dir=tmp_path, objective="Keep this native conversation.",
+ )
+
+ def submit_first():
+ try:
+ return submit()
+ finally:
+ first_returned.set()
+
+ with ThreadPoolExecutor(max_workers=2) as executor:
+ first = executor.submit(submit_first)
+ assert first_starting.wait(timeout=5)
+ retry = executor.submit(submit)
+ with pytest.raises(RuntimeError, match="can't start new thread"):
+ first.result(timeout=7)
+ replay, created = retry.result(timeout=7)
+
+ assert created is False
+ completed = runtime.wait_for_turn(session_id=session_id, turn_id=replay["turn_id"], timeout_sec=8)
+ assert completed["status"] == "completed", completed.get("error")
+ assert completed["upstream_turn_id"] == "durable-turn-1"
+ assert runtime.adapters[session_id] is adapter
+ assert adapter.healthcheck()
+ assert store.load_session(session_id)["upstream_thread_id"] == "durable-thread"
+ assert [json.loads(line) for line in native_calls.read_text().splitlines()].count("turn/start") == 1
+ assert runtime.close_session(session_id) is True
+ assert not adapter.healthcheck()
+ finally:
+ runtime.close()
diff --git a/tests/test_chat_machine_configuration_api.py b/tests/test_chat_machine_configuration_api.py
index 6b903e1ced..3380e5a689 100644
--- a/tests/test_chat_machine_configuration_api.py
+++ b/tests/test_chat_machine_configuration_api.py
@@ -975,3 +975,68 @@ def request(method, path, body=None):
server.shutdown()
thread.join(timeout=5)
server.server_close()
+
+
+def test_steward_executor_endpoint_models_round_trip_through_machine_api(tmp_path: Path) -> None:
+ from loopx.chat_server import ChatHTTPServer, ChatRequestHandler
+
+ server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler)
+ server.runtime_root = tmp_path
+ server.verbose = False
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+ connection = http.client.HTTPConnection(*server.server_address, timeout=10)
+
+ def request(method: str, path: str, body: dict[str, Any] | None = None) -> tuple[int, dict[str, Any]]:
+ connection.request(
+ method,
+ path,
+ body=json.dumps(body) if body is not None else None,
+ headers={"Content-Type": "application/json"},
+ )
+ response = connection.getresponse()
+ return response.status, json.loads(response.read())
+
+ try:
+ desired = {
+ "namespace": "steward_executor",
+ "namespace_configuration": {
+ "schema_version": "steward_executor_machine_defaults_v1",
+ "selection_policy": "flexible",
+ "executor_endpoint": "codex",
+ "eligible_endpoints": ["codex", "antigravity"],
+ "executor_model": "gpt-6.2-sol",
+ "executor_reasoning_effort": "xhigh",
+ "endpoint_models": {
+ "antigravity": {"model": "carbon", "reasoning_effort": "medium"},
+ },
+ },
+ }
+ status, preview = request("POST", CHAT_MACHINE_CONFIGURATION_PREVIEW_PATH, desired)
+ assert status == 201, preview
+ status, applied = request(
+ "POST",
+ CHAT_MACHINE_CONFIGURATION_APPLY_PATH,
+ {**desired, "expected_plan_revision": preview["plan_revision"]},
+ )
+ assert status == 200 and applied["ok"] is True, applied
+ status, inspection = request("GET", CHAT_MACHINE_CONFIGURATION_PATH)
+ assert status == 200
+ saved = inspection["machine_configuration"]["namespaces"]["steward_executor"]
+ assert saved["endpoint_models"] == {
+ "antigravity": {"model": "carbon", "reasoning_effort": "medium"},
+ }
+ steward_cap = next(
+ item
+ for item in inspection["capability_catalog"]["capabilities"]
+ if item["capability_id"] == "steward_executor"
+ )
+ assert steward_cap["configuration_editor"]["editable"] is True
+ fields = {field["key"]: field for field in steward_cap["configuration_editor"]["fields"]}
+ assert "endpoint_models" in fields["executor_model"]["description"]
+ assert applied["machine_configuration"]["namespaces"]["steward_executor"]["endpoint_models"] == saved["endpoint_models"]
+ finally:
+ connection.close()
+ server.shutdown()
+ thread.join(timeout=5)
+ server.server_close()
diff --git a/tests/test_chat_manager_model_adoption.py b/tests/test_chat_manager_model_adoption.py
index 3e5ea47b18..003be4f7c1 100644
--- a/tests/test_chat_manager_model_adoption.py
+++ b/tests/test_chat_manager_model_adoption.py
@@ -30,7 +30,7 @@
from loopx.extensions.lark.conversation_identity import identity_ref
-def configure(root, *, model, effort, endpoint="codex", policy="preferred"):
+def configure(root, *, model, effort, endpoint="codex", policy="preferred", endpoint_models=None):
document = {
"schema_version": "loopx_machine_configuration_v0",
"namespaces": {"steward_executor": {
@@ -40,6 +40,7 @@ def configure(root, *, model, effort, endpoint="codex", policy="preferred"):
"executor_endpoint": endpoint,
"executor_model": model,
"executor_reasoning_effort": effort,
+ **({"endpoint_models": endpoint_models} if endpoint_models is not None else {}),
}},
}
registry = build_builtin_machine_configuration_registry()
@@ -748,3 +749,249 @@ def test_manager_session_model_allocation_ignores_partial_pre_v0_record(conversa
partial_session = {**session, "manager_executor_allocation": partial_allocation}
assert manager_session_model_allocation(runtime, partial_session) is None
assert partial_session["manager_executor_allocation"] == partial_allocation
+
+
+def test_codex_primary_flexible_quota_route_selects_carbon_and_restores_codex(conversation, monkeypatch):
+ import sys
+ from datetime import datetime, timedelta, timezone
+ from types import ModuleType
+ from loopx.capabilities.native_chat.antigravity_adapter import AntigravityAdapter
+ from loopx.chat_manager import manager_session_quota_route
+
+ root, store, runtime, session, _starts = conversation
+ configure(root, model="gpt-6.2-sol", effort="xhigh", endpoint="codex", policy="flexible")
+ store.update_session(session["session_id"], upstream_thread_id="codex-thread-1",
+ agent_model_id="gpt-6.2-sol", agent_reasoning_effort="xhigh")
+
+ parent = ModuleType("loopx_codex_provider_routing")
+ module = ModuleType("loopx_codex_provider_routing.native")
+
+ class Accounts:
+ def __init__(self, path):
+ assert path == runtime.coordination_runtime_root
+
+ def model_transport(self, **_parameters):
+ return None
+
+ def steward_route(self, *, codex_home, primary, pool, current, primary_capacity,
+ fallback_capacity=None, blocked_primary=None, model=None):
+ assert primary == "codex" and set(pool) == {"codex", "antigravity"}
+ if current == "antigravity":
+ return {"endpoint": "codex", "blocked_primary": blocked_primary}
+ if fallback_capacity is None:
+ return {"endpoint": None, "blocked_primary": blocked_primary}
+ return {"endpoint": "antigravity", "blocked_primary": blocked_primary}
+
+ module.NativeAccounts = Accounts
+ monkeypatch.setitem(sys.modules, parent.__name__, parent)
+ monkeypatch.setitem(sys.modules, module.__name__, module)
+ monkeypatch.setattr(runtime, "capabilities", lambda: [
+ {"agent_id": "codex", "available": True},
+ {"agent_id": "antigravity", "available": True},
+ ])
+ monkeypatch.setattr(
+ AntigravityAdapter,
+ "endpoint_model_capacity",
+ staticmethod(lambda *, model_id, **_kw: {
+ "observed_at": 1767225600.0,
+ "account_ref": "b" * 64,
+ "model_id": model_id,
+ "execution_id": "probe-1",
+ "remaining_fraction": 0.99,
+ "reset_time": (datetime.now(timezone.utc) + timedelta(days=1)).isoformat(),
+ "quota_exhausted": False,
+ "normal_completion": True,
+ }),
+ )
+
+ loaded = store.load_session(session["session_id"])
+ neutral_route = manager_session_quota_route(runtime, loaded)
+ assert neutral_route is not None
+ neutral_alloc = neutral_route["allocation"]
+ assert neutral_alloc["executor_endpoint"] == "antigravity"
+ assert neutral_alloc["model"] == "gemini-4-argon-high"
+ assert neutral_alloc["model_source"] == "vendor_default"
+ assert neutral_alloc["reasoning_effort"] == "high"
+ assert neutral_alloc["configured_model"] is None
+
+ configure(
+ root,
+ model="gpt-6.2-sol",
+ effort="xhigh",
+ endpoint="codex",
+ policy="flexible",
+ endpoint_models={"antigravity": {"model": "gemini-4-carbon-high", "reasoning_effort": "high"}},
+ )
+ routed_to_ag = manager_session_quota_route(runtime, loaded)
+ assert routed_to_ag is not None
+ alloc_ag = routed_to_ag["allocation"]
+ assert alloc_ag["executor_endpoint"] == "antigravity"
+ assert alloc_ag["allocation_reason"] == "flexible_availability_fallback"
+ assert alloc_ag["model"] == "gemini-4-carbon-high"
+ assert alloc_ag["model_source"] == "machine_configuration"
+ assert alloc_ag["reasoning_effort"] == "high"
+ assert alloc_ag["configured_model"] == "gemini-4-carbon-high"
+ assert alloc_ag["configured_reasoning_effort"] == "high"
+
+ # Store transition preserves Codex provenance and allows subsequent recovery back to Codex.
+ turn1, _ = store.create_turn(session["session_id"], client_turn_id="switch-ag", message="Switch to Carbon")
+ store.update_session(
+ session["session_id"],
+ status="ready",
+ active_turn_id=turn1["turn_id"],
+ expected_upstream_thread_id="codex-thread-1",
+ agent_id="antigravity",
+ executor_endpoint_id="antigravity",
+ adapter_kind="antigravity_ls",
+ upstream_mode="default",
+ upstream_thread_id="ag-cascade-1",
+ agent_model_id=alloc_ag["model"],
+ agent_reasoning_effort=alloc_ag["reasoning_effort"],
+ manager_executor_allocation=alloc_ag,
+ upstream_context_history=[{
+ "thread_id": "codex-thread-1",
+ "manager_context_version": loaded.get("manager_context_version"),
+ "agent_id": "codex",
+ "agent_model_id": "gpt-6.2-sol",
+ }],
+ )
+ store.update_turn(session["session_id"], turn1["turn_id"], status="completed")
+ store.update_session(session["session_id"], active_turn_id=None)
+
+ on_ag = store.load_session(session["session_id"])
+ routed_back = manager_session_quota_route(runtime, on_ag)
+ assert routed_back is not None
+ alloc_codex = routed_back["allocation"]
+ assert alloc_codex["executor_endpoint"] == "codex"
+ assert alloc_codex["model"] == "gpt-6.2-sol"
+ assert alloc_codex["reasoning_effort"] == "xhigh"
+
+ turn2, _ = store.create_turn(session["session_id"], client_turn_id="restore-codex", message="Back to Codex")
+ store.update_session(
+ session["session_id"],
+ status="ready",
+ active_turn_id=turn2["turn_id"],
+ expected_upstream_thread_id="ag-cascade-1",
+ agent_id="codex",
+ executor_endpoint_id="codex",
+ adapter_kind="codex_app_server",
+ upstream_mode="chat",
+ upstream_thread_id="codex-thread-2",
+ agent_model_id=alloc_codex["model"],
+ agent_reasoning_effort=alloc_codex["reasoning_effort"],
+ manager_executor_allocation=alloc_codex,
+ upstream_context_history=[
+ *on_ag["upstream_context_history"],
+ {
+ "thread_id": "ag-cascade-1",
+ "manager_context_version": on_ag.get("manager_context_version"),
+ "agent_id": "antigravity",
+ "agent_model_id": "gemini-4-carbon-high",
+ },
+ ],
+ )
+ assert store.load_session(session["session_id"])["agent_id"] == "codex"
+
+
+def test_codex_to_antigravity_failover_resolves_short_alias_endpoint_model(conversation, monkeypatch):
+ import sys
+ from datetime import datetime, timedelta, timezone
+ from types import ModuleType
+ from loopx.capabilities.native_chat import antigravity_adapter
+ from loopx.chat_manager import manager_session_quota_route
+
+ root, store, runtime, session, _starts = conversation
+ store.update_session(
+ session["session_id"],
+ upstream_thread_id="codex-thread-1",
+ agent_model_id="gpt-6.2-sol",
+ agent_reasoning_effort="xhigh",
+ )
+
+ parent = ModuleType("loopx_codex_provider_routing")
+ module = ModuleType("loopx_codex_provider_routing.native")
+
+ class Accounts:
+ def __init__(self, path):
+ assert path == runtime.coordination_runtime_root
+
+ def model_transport(self, **_parameters):
+ return None
+
+ def steward_route(self, *, codex_home, primary, pool, current, primary_capacity,
+ fallback_capacity=None, blocked_primary=None, model=None):
+ if fallback_capacity is None:
+ return {"endpoint": None, "blocked_primary": blocked_primary}
+ return {"endpoint": "antigravity", "blocked_primary": blocked_primary}
+
+ module.NativeAccounts = Accounts
+ monkeypatch.setitem(sys.modules, parent.__name__, parent)
+ monkeypatch.setitem(sys.modules, module.__name__, module)
+ monkeypatch.setattr(runtime, "capabilities", lambda: [
+ {"agent_id": "codex", "available": True},
+ {"agent_id": "antigravity", "available": True},
+ ])
+ reset = (datetime.now(timezone.utc) + timedelta(days=1)).isoformat()
+ monkeypatch.setattr(
+ antigravity_adapter,
+ "discover_antigravity_language_server",
+ lambda **_kw: {"url": "http://127.0.0.1:4321", "csrf_token": "tok"},
+ )
+ monkeypatch.setattr(
+ antigravity_adapter,
+ "_rpc_post",
+ lambda _url, _tok, method, _payload, **_kw: {
+ "userStatus": {
+ "email": "synthetic@invalid.example",
+ "cascadeModelConfigData": {
+ "clientModelConfigs": [
+ {
+ "label": "Gemini 4 Carbon (High)",
+ "modelOrAlias": {"model": "MODEL_PLACEHOLDER_M427"},
+ "quotaInfo": {"remainingFraction": 0.91, "resetTime": reset},
+ },
+ {
+ "label": "Gemini 4 Carbon (Medium)",
+ "modelOrAlias": {"model": "MODEL_PLACEHOLDER_M437"},
+ "quotaInfo": {"remainingFraction": 0.77, "resetTime": reset},
+ },
+ ]
+ },
+ }
+ } if method == "GetUserStatus" else {},
+ )
+
+ loaded = store.load_session(session["session_id"])
+
+ # Alias with explicit effort resolves to gemini-4-carbon-medium and passes quota route.
+ configure(
+ root,
+ model="gpt-6.2-sol",
+ effort="xhigh",
+ endpoint="codex",
+ policy="flexible",
+ endpoint_models={"antigravity": {"model": "carbon", "reasoning_effort": "medium"}},
+ )
+ routed_medium = manager_session_quota_route(runtime, loaded)
+ assert routed_medium is not None
+ alloc_medium = routed_medium["allocation"]
+ assert alloc_medium["executor_endpoint"] == "antigravity"
+ assert alloc_medium["allocation_reason"] == "flexible_availability_fallback"
+ assert alloc_medium["model"] == "carbon"
+ assert alloc_medium["reasoning_effort"] == "medium"
+
+ # Alias without explicit effort defaults to high (gemini-4-carbon-high).
+ configure(
+ root,
+ model="gpt-6.2-sol",
+ effort="xhigh",
+ endpoint="codex",
+ policy="flexible",
+ endpoint_models={"antigravity": {"model": "carbon"}},
+ )
+ routed_high = manager_session_quota_route(runtime, loaded)
+ assert routed_high is not None
+ alloc_high = routed_high["allocation"]
+ assert alloc_high["executor_endpoint"] == "antigravity"
+ assert alloc_high["model"] == "carbon"
+ assert alloc_high["reasoning_effort"] == "high"
diff --git a/tests/test_chat_operation_actions.py b/tests/test_chat_operation_actions.py
index 42395cd287..b35478125c 100644
--- a/tests/test_chat_operation_actions.py
+++ b/tests/test_chat_operation_actions.py
@@ -14,6 +14,7 @@
from loopx.chat_actions import ChatActionService, ProtectedActionGate
from loopx.control_plane.collaboration.operation_handoff import agent_operation_action
from loopx.control_plane.collaboration.inbox import pending
+from loopx.control_plane.goals.activation import build_goal_activation
from loopx.capabilities.manager_context import turn_start_hook
from examples.operation_action_fixtures import (
GOAL_ID,
@@ -286,7 +287,11 @@ def test_managed_pending_reuses_registered_agent_and_goal_instance_scope(
)
registry = json.loads(service.registry_path.read_text())
- registry["goals"][0]["activation_state"] = "stopped"
+ registry["goals"][0]["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
service.registry_path.write_text(json.dumps(registry))
# Stopping execution does not erase the original evidence-reconciliation locator.
historical = handler("loopx_operation", args, native)
@@ -682,7 +687,11 @@ def test_agent_handoff_fails_closed_on_expiry_binding_activation_or_terms_drift(
elif change == "archived":
registry["goals"][0]["status"] = "archived"
else:
- registry["goals"][0]["activation_state"] = "stopped"
+ registry["goals"][0]["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
service.registry_path.write_text(json.dumps(registry))
else:
data = json.loads(store.path.read_text())
@@ -849,7 +858,11 @@ def test_unknown_result_never_grants_resubmission_and_late_evidence_does_not_req
**args,
)
registry = json.loads(service.registry_path.read_text())
- registry["goals"][0]["activation_state"] = "stopped"
+ registry["goals"][0]["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
registry["goals"][0]["coordination"]["thread_agent_bindings"][0]["thread_id"] = (
"thread-replacement"
)
@@ -1022,7 +1035,11 @@ def test_replacement_session_reconciles_under_its_current_binding_without_recons
)
if historical:
registry = json.loads(service.registry_path.read_text())
- registry["goals"][0]["activation_state"] = "stopped"
+ registry["goals"][0]["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
service.registry_path.write_text(json.dumps(registry))
after_expiry = (datetime.now(timezone.utc) + timedelta(hours=2)).isoformat()
monkeypatch.setattr("loopx.chat_action_store._utc_now", lambda: after_expiry)
diff --git a/tests/test_chat_queue_preparation.py b/tests/test_chat_queue_preparation.py
index 3d8ba2ec60..c02d652163 100644
--- a/tests/test_chat_queue_preparation.py
+++ b/tests/test_chat_queue_preparation.py
@@ -81,6 +81,56 @@ def write(packet):
assert store.messages(sid)[0]["attachments"] == attachments
+def test_live_queue_recovers_reserved_answer_then_runs_followup(tmp_path, monkeypatch, caplog):
+ import errno
+ import queue
+ from types import SimpleNamespace
+ from loopx.chat_agent import CodexChatAgentSession
+
+ store, sid, runtime = session_runtime(tmp_path)
+ original, _ = store.create_queued_turn(sid, client_turn_id="first", message="First request.")
+ followup, _ = store.create_queued_turn(sid, client_turn_id="second", message="Follow-up request.")
+ native = CodexChatAgentSession(process=SimpleNamespace(poll=lambda: None),
+ messages=queue.Queue(), thread_id="public-thread", work_dir=tmp_path)
+ sent = []
+
+ def write(packet):
+ assert packet["method"] == "turn/start"
+ sent.append(packet["params"]["input"][0]["text"])
+ text = f"Answer {len(sent)}."
+ native.messages.put({"id": packet["id"], "result": {"turn": {"id": f"native-{len(sent)}"}}})
+ native.messages.put({"method": "item/completed", "params": {"item": {
+ "type": "agentMessage", "phase": "final_answer", "text": text}}})
+ native.messages.put({"method": "turn/completed", "params": {"turn": {"status": "completed"}}})
+
+ publish = store.append_completed_response_events
+ failures = []
+
+ def fail_first_publication(session_id, turn_id, **kwargs):
+ if turn_id == original["turn_id"] and not failures:
+ failures.append(turn_id)
+ raise OSError(errno.EIO, "PRIVATE_ERROR_DETAIL")
+ return publish(session_id, turn_id, **kwargs)
+
+ monkeypatch.setattr(native, "_write", write)
+ monkeypatch.setattr(runtime, "_ensure_adapter", lambda *a, **kw: CodexAppServerAdapter(native))
+ monkeypatch.setattr(store, "append_completed_response_events", fail_first_publication)
+ runtime.resume_session_queue(session_id=sid, work_dir=tmp_path, objective="Answer requests")
+ try:
+ assert runtime.wait_for_turn(session_id=sid, turn_id=followup["turn_id"], timeout_sec=10)["status"] == "completed"
+ first = store.load_turn(sid, original["turn_id"])
+ assert first["status"] == "completed" and first["response"]["message"] == "Answer 1."
+ assert failures == [original["turn_id"]]
+ assert len(sent) == 2 and "First request." in sent[0] and "Follow-up request." in sent[1]
+ assert [m["text"] for m in store.messages(sid) if m["role"] == "agent"] == ["Answer 1.", "Answer 2."]
+ events = store.events_after(sid, original["turn_id"], None)
+ assert [e["kind"] for e in events if e["kind"] in {"turn.completed", "turn.failed"}] == ["turn.completed"]
+ assert f"turn={original['turn_id']} phase=publish_saved_answer errno={errno.EIO}" in caplog.text
+ assert "PRIVATE_ERROR_DETAIL" not in caplog.text
+ finally:
+ runtime.close()
+
+
@pytest.mark.parametrize("failure", ["before_message", "after_message", "before_event"])
@pytest.mark.parametrize("display", ["Execution request", "Original visible request 原文"])
def test_queue_replay_leaves_prepared_managed_input_with_its_owner(tmp_path, monkeypatch, failure, display):
diff --git a/tests/test_chat_runtime_activation.py b/tests/test_chat_runtime_activation.py
index fb8d33de14..a0bd8738ab 100644
--- a/tests/test_chat_runtime_activation.py
+++ b/tests/test_chat_runtime_activation.py
@@ -103,7 +103,7 @@ def test_same_process_worker_exit_is_recovered_without_replay(tmp_path, released
with mock.patch.object(runtime, "_start_adapter", side_effect=AssertionError("no provider")):
runtime.recover_exited_turn(sid, tid)
failed = store.load_turn(sid, tid)
- assert failed["status"] == "failed" and failed["error_code"] == "runtime_error"
+ assert failed["status"] == "failed" and failed["error_code"] == "execution_worker_exited"
assert "execution worker exited" in failed["error"]
assert "execution may have occurred" in failed["error"]
assert "not retried automatically" in failed["error"]
@@ -111,7 +111,8 @@ def test_same_process_worker_exit_is_recovered_without_replay(tmp_path, released
assert [m["text"] for m in store.messages(sid) if m["role"] == "user"] == ["Keep the original request."]
events = store.events_after(sid, tid, None)
assert any(e["kind"] == "answer.delta" for e in events)
- assert sum(e["kind"] == "turn.failed" for e in events) == 1
+ failure, = [e for e in events if e["kind"] == "turn.failed"]
+ assert failure["payload"]["error_code"] == failed["error_code"]
runtime.recover_exited_turn(sid, tid)
assert store.events_after(sid, tid, None) == events
assert lock_holder_path(path).read_bytes() == record
@@ -136,7 +137,7 @@ def test_recovery_preserves_stop_after_the_live_service_worker_retires(tmp_path,
holder["pid"] = process.pid
lock_holder_path(path).write_text(json.dumps(holder))
runtime.recover_exited_turn(sid, tid)
- assert store.load_turn(sid, tid)["error_code"] == ("runtime_error" if released else "server_restarted")
+ assert store.load_turn(sid, tid)["error_code"] == ("execution_worker_exited" if released else "server_restarted")
assert store.load_session(sid)["active_turn_id"] is None
@@ -184,6 +185,21 @@ def test_activation_recovers_only_unowned_execution_and_preserves_queued_input(s
"Keep the original request.", "Original queued follow-up."]
+def test_completion_recovery_does_not_publish_under_a_live_worker(tmp_path):
+ store = ChatSessionStore(tmp_path)
+ sid = session(store)
+ turn, _ = store.create_turn(sid, client_turn_id="live", message="Still finalizing.")
+ tid = turn["turn_id"]
+ runtime = ChatRuntimeController(store=store, codex_bin="must-not-execute")
+ with try_exclusive_file_lock(store.turn_execution_lock_path(sid, tid),
+ agent_id="loopx-chat", operation="execute_chat_turn"):
+ store.update_turn(sid, tid, status="completing", response={"message": "Pending answer."})
+ runtime.recover_exited_turn(sid, tid)
+ assert store.load_turn(sid, tid)["status"] == "completing"
+ assert store.load_session(sid)["active_turn_id"] == tid
+ assert not any(m["role"] == "agent" for m in store.messages(sid))
+
+
def test_activation_does_not_recover_a_live_execution_fence(services):
root, _agents, calls = services
store = ChatSessionStore(root)
diff --git a/tests/test_chat_status_owner.py b/tests/test_chat_status_owner.py
index aa7563d9b8..edbaa3ab0c 100644
--- a/tests/test_chat_status_owner.py
+++ b/tests/test_chat_status_owner.py
@@ -19,7 +19,7 @@ def test_status_owner_imports_canonically_and_has_no_legacy_module():
code = """
import importlib.util
from loopx.chat.status_api import ChatStatusRequestMixin, goal_read_runtime_root
-from loopx.chat.todo_detail import goal_read_runtime_root as todo_runtime_root
+from loopx.chat.todo_source import goal_read_runtime_root as todo_runtime_root
from loopx.chat_server import ChatRequestHandler
assert issubclass(ChatRequestHandler, ChatStatusRequestMixin)
@@ -44,7 +44,7 @@ def test_status_owner_imports_canonically_and_has_no_legacy_module():
def test_registry_patch_reaches_already_imported_todo_consumer(
monkeypatch, tmp_path, source_bound, controller_root, expected,
):
- from loopx.chat.todo_detail import goal_read_runtime_root
+ from loopx.chat.todo_source import goal_read_runtime_root
registry_path = tmp_path / "registry.json"
calls = []
diff --git a/tests/test_contract_git_probe_budget.py b/tests/test_contract_git_probe_budget.py
index 1fcbc72056..c3d5f3d7f4 100644
--- a/tests/test_contract_git_probe_budget.py
+++ b/tests/test_contract_git_probe_budget.py
@@ -181,7 +181,7 @@ def test_a_timed_out_probe_takes_the_helpers_it_started(
subprocess.run(
["git", "-C", str(repo), "config", "core.fsmonitor", str(hook)], check=True
)
- monkeypatch.setattr(contract, "GIT_PROBE_TIMEOUT_SECONDS", 0.8)
+ monkeypatch.setattr(contract, "GIT_PROBE_TIMEOUT_SECONDS", 1.5)
started = time.monotonic()
with pytest.raises(contract.GitProbeUnavailable):
diff --git a/tests/test_doctor_git_probe_budget.py b/tests/test_doctor_git_probe_budget.py
index 1a15a065b2..8efec24ee4 100644
--- a/tests/test_doctor_git_probe_budget.py
+++ b/tests/test_doctor_git_probe_budget.py
@@ -127,7 +127,7 @@ def test_a_blocked_query_takes_the_helpers_it_started(
["git", "-C", str(repo), "config", "core.fsmonitor", str(hook)], check=True
)
- monkeypatch.setattr(doctor_git, "GIT_PROBE_TIMEOUT_SECONDS", 0.8)
+ monkeypatch.setattr(doctor_git, "GIT_PROBE_TIMEOUT_SECONDS", 1.5)
cleanup_calls: list[float] = []
original_terminate = doctor_git.terminate_process_tree
diff --git a/tests/test_dsh_goal_mode.py b/tests/test_dsh_goal_mode.py
index d36ccf6ef1..bdb93f6c7e 100644
--- a/tests/test_dsh_goal_mode.py
+++ b/tests/test_dsh_goal_mode.py
@@ -408,6 +408,43 @@ def test_parse_model_json_tolerates_prose_and_fences() -> None:
assert turn_host_adapter.parse_model_json("no json at all") is None
+def test_parse_model_json_repairs_minor_delimiters_and_preserves_strings() -> None:
+ complex_payload = {
+ "result_kind": "validated_completion",
+ "details": [
+ {"label": "literal", "text": "Keep ,}, and }]}}]}} intact inside string literals."},
+ {"label": "second", "text": "Trailing text"},
+ ],
+ }
+ raw_trailing_comma = (
+ '{"result_kind": "validated_completion", "details": ['
+ '{"label": "literal", "text": "Keep ,}, and }]}}]}} intact inside string literals.",}, '
+ '{"label": "second", "text": "Trailing text",},'
+ '],}'
+ )
+ assert turn_host_adapter.parse_model_json(raw_trailing_comma) == complex_payload
+ # Extra } inside terminal closer run when outer expectation is ] followed by }
+ raw_extra_before_array_close = (
+ '{"result_kind": "validated_completion", "nested": {"sections": ['
+ '{"items": [{"summary": "ok",}]}]}]}}'
+ )
+ assert turn_host_adapter.parse_model_json(raw_extra_before_array_close) == {
+ "result_kind": "validated_completion",
+ "nested": {"sections": [{"items": [{"summary": "ok"}]}]},
+ }
+ raw_extra_outer_brace = '{"result_kind": "wait", "nested": {"a": [1,]}}}'
+ assert turn_host_adapter.parse_model_json(raw_extra_outer_brace) == {
+ "result_kind": "wait",
+ "nested": {"a": [1]},
+ }
+ # Truncated, unclosed string, mid-object mismatch, or >2 stray closers must fail closed.
+ assert turn_host_adapter.parse_model_json('{"result_kind": "wait", "items": [{"a": 1}') is None
+ assert turn_host_adapter.parse_model_json('{"result_kind": "wait", "items": [{"a": 1}}') is None
+ assert turn_host_adapter.parse_model_json('{"result_kind": "wait", "text": "unterminated}') is None
+ assert turn_host_adapter.parse_model_json('{"a": [1}, "b": 2}') is None
+ assert turn_host_adapter.parse_model_json('{"result_kind": "wait"}}}}') is None
+
+
def test_build_result_fails_closed_without_a_typed_candidate() -> None:
request = _signed_request()
result = turn_host_adapter.build_result(request, None)
diff --git a/tests/test_global_todo_readiness_characterization.py b/tests/test_global_todo_readiness_characterization.py
index 931c841b5d..66688a7902 100644
--- a/tests/test_global_todo_readiness_characterization.py
+++ b/tests/test_global_todo_readiness_characterization.py
@@ -14,7 +14,7 @@
FIXTURE = Path(__file__).parent / "fixtures/global_todo_readiness.json"
NOW = "2026-11-02T00:00:00+00:00"
-BASE_REVISION = "d4aaaaefbd37a83a30f10681de839b45d631eeb9"
+BASE_REVISION = "d210e4cc1d002f92498b2811c565b9430e79f4c1"
def observe(case, monkeypatch, tmp_path):
diff --git a/tests/test_kunluncode_goal_mode.py b/tests/test_kunluncode_goal_mode.py
index bd9fe3f927..afac67688a 100644
--- a/tests/test_kunluncode_goal_mode.py
+++ b/tests/test_kunluncode_goal_mode.py
@@ -179,6 +179,12 @@ def capture(command: list[str], **_kwargs) -> subprocess.CompletedProcess[str]:
),
"settlement_identity": identity.as_dict(),
"settlement_result": {"failure": None},
+ "settlement_progress": {
+ "schema_version": "quota_settlement_progress_v0",
+ "state": "settled",
+ "next_step": None,
+ "quota_spend_source": "heartbeat",
+ },
}
)
else:
@@ -188,6 +194,12 @@ def capture(command: list[str], **_kwargs) -> subprocess.CompletedProcess[str]:
"appended": True,
"settlement_identity": identity.as_dict(),
"settlement_result": {"failure": None},
+ "settlement_progress": {
+ "schema_version": "quota_settlement_progress_v0",
+ "state": "settled",
+ "next_step": None,
+ "quota_spend_source": "heartbeat",
+ },
}
)
return subprocess.CompletedProcess(command, 0, payload, "")
@@ -281,6 +293,12 @@ def capture(command: list[str], **_kwargs) -> subprocess.CompletedProcess[str]:
"completion_continuation": "active_goal",
"settlement_identity": identity.as_dict(),
"settlement_result": {"failure": None},
+ "settlement_progress": {
+ "schema_version": "quota_settlement_progress_v0",
+ "state": "settled",
+ "next_step": None,
+ "quota_spend_source": "heartbeat",
+ },
}
)
else:
@@ -290,6 +308,12 @@ def capture(command: list[str], **_kwargs) -> subprocess.CompletedProcess[str]:
"appended": True,
"settlement_identity": identity.as_dict(),
"settlement_result": {"failure": None},
+ "settlement_progress": {
+ "schema_version": "quota_settlement_progress_v0",
+ "state": "settled",
+ "next_step": None,
+ "quota_spend_source": "heartbeat",
+ },
}
)
return subprocess.CompletedProcess(command, 0, payload, "")
@@ -408,6 +432,12 @@ def capture(command: list[str], **_kwargs) -> subprocess.CompletedProcess[str]:
"status": "done",
"settlement_identity": identity.as_dict(),
"settlement_result": {"failure": None},
+ "settlement_progress": {
+ "schema_version": "quota_settlement_progress_v0",
+ "state": "settled",
+ "next_step": None,
+ "quota_spend_source": "heartbeat",
+ },
}
)
else:
@@ -417,6 +447,12 @@ def capture(command: list[str], **_kwargs) -> subprocess.CompletedProcess[str]:
"appended": True,
"settlement_identity": identity.as_dict(),
"settlement_result": {"failure": None},
+ "settlement_progress": {
+ "schema_version": "quota_settlement_progress_v0",
+ "state": "settled",
+ "next_step": None,
+ "quota_spend_source": "heartbeat",
+ },
}
)
return subprocess.CompletedProcess(command, 0, payload, "")
diff --git a/tests/test_lark_event_collector_git_budget.py b/tests/test_lark_event_collector_git_budget.py
index 7628b71d6d..04462acc96 100644
--- a/tests/test_lark_event_collector_git_budget.py
+++ b/tests/test_lark_event_collector_git_budget.py
@@ -82,7 +82,7 @@ def test_the_next_question_is_not_asked_after_one_goes_unanswered(
"exit 0\n",
monkeypatch,
)
- monkeypatch.setattr(event_collector, "GIT_PROBE_TIMEOUT_SECONDS", 0.5)
+ monkeypatch.setattr(event_collector, "GIT_PROBE_TIMEOUT_SECONDS", 1.5)
started = time.monotonic()
with pytest.raises(ValueError, match="needs a Git answer"):
@@ -141,7 +141,7 @@ def test_a_timed_out_probe_takes_the_helpers_it_started(
subprocess.run(
["git", "-C", str(project), "config", "core.fsmonitor", str(hook)], check=True
)
- monkeypatch.setattr(event_collector, "GIT_PROBE_TIMEOUT_SECONDS", 0.8)
+ monkeypatch.setattr(event_collector, "GIT_PROBE_TIMEOUT_SECONDS", 1.5)
started = time.monotonic()
with pytest.raises(ValueError, match="needs a Git answer"):
diff --git a/tests/test_lark_native_reply_context.py b/tests/test_lark_native_reply_context.py
index df2cdf0872..95b1eac41d 100644
--- a/tests/test_lark_native_reply_context.py
+++ b/tests/test_lark_native_reply_context.py
@@ -165,7 +165,10 @@ def test_unquoted_provider_root_keeps_plain_input_and_no_parent_read(ordinary):
assert native_request(transport, event)["message"] == event["content"]
calls = provider.calls[before:]
assert sum("+messages-mget" in args for args in calls) == 1
- assert not any("api" in args for args in calls)
+ api_calls = [args[args.index("api"):] for args in calls if "api" in args]
+ assert api_calls == [
+ ["api", "GET", f"/open-apis/im/v1/messages/{event['message_id']}", "--as", "bot"]
+ ]
finally:
runtime.close()
diff --git a/tests/test_lark_private_conversations.py b/tests/test_lark_private_conversations.py
index 52f9c22a9a..d7279ddd8f 100644
--- a/tests/test_lark_private_conversations.py
+++ b/tests/test_lark_private_conversations.py
@@ -5156,6 +5156,46 @@ def test_completed_answer_delivery_does_not_prepare_another_execution(ordinary,
runtime.close()
+def test_private_delivery_recovers_saved_answer_after_claim_release(ordinary, monkeypatch): # noqa: F811
+ import errno
+ from loopx import chat_store
+
+ store, runtime, provider, transport = connect(ordinary)
+ event = provider.event("notes-app", "saved-answer-original", "Complete the original request.")
+ write = chat_store._atomic_write_json
+ lost = threading.Event()
+
+ def fail_final_status(path, value, **kwargs):
+ if path.parent.name == "turns" and value.get("status") == "completed" and not lost.is_set():
+ lost.set()
+ raise OSError(errno.EIO, "Synthetic final status write failure")
+ return write(path, value, **kwargs)
+
+ monkeypatch.setattr(chat_store, "_atomic_write_json", fail_final_status)
+ try:
+ transport.admit("notes-app", event)
+ native, = transport.core.pending()
+ sid, tid = native["session_id"], native["turn_id"]
+ assert lost.wait(10)
+ worker = runtime.session_queue_threads.get(sid)
+ if worker is not None:
+ worker.join(5)
+ assert not worker.is_alive()
+ assert store.load_session(sid)["active_turn_id"] is None
+ assert store.load_turn(sid, tid)["status"] == "completing"
+ path = transport.root / f"{native['request_ref']}.json"
+ assert transport.reconcile_request(path) == 1
+ assert transport.reconcile_request(path) == 0
+ assert provider.writes == [("notes-app", "Runtime response.")]
+ assert store.load_turn(sid, tid)["status"] == "completed"
+ assert json.loads(path.read_text())["deliveries"]["terminal"]["verified"] is True
+ assert [m["text"] for m in store.messages(sid) if m["role"] == "agent"] == ["Runtime response."]
+ packets = [json.loads(line) for line in ordinary[4].read_text().splitlines()]
+ assert sum(row["method"] == "turn/start" for row in packets) == 1
+ finally:
+ runtime.close()
+
+
def test_unexecuted_request_still_uses_original_queue_recovery(ordinary, monkeypatch): # noqa: F811
store, runtime, provider, transport = connect(ordinary)
resumes = []
@@ -9096,3 +9136,102 @@ def reconcile_request(self, path):
release.set()
service.close()
service._delivery_thread.join(5)
+
+
+@pytest.mark.parametrize("relay_ready_initially", [False, True])
+def test_project_workspace_write_recovers_loopback_relay_auth_once_ready(
+ ordinary, monkeypatch, tmp_path, relay_ready_initially, # noqa: F811
+):
+ from datetime import timedelta, timezone
+ from pathlib import Path
+ from loopx.capabilities.native_chat import codex_adapter as adapter_module
+ from loopx.capabilities.native_chat import external_conversations
+
+ ordinary[2].workspace_grant = "workspace_write"
+ store, runtime, provider, transport = connect(ordinary)
+ try:
+ capture, fake = ordinary[-3:-1]
+ binding = next(row for row in transport.bindings.read()["bindings"] if row["transport_ref"] == "notes-app")
+ assert binding["grant"] == "workspace_write"
+ source = fake.read_text().replace(
+ '"codexErrorInfo": "cyberPolicy"',
+ '"codexErrorInfo": {"httpConnectionFailed": {"httpStatusCode": 401}}',
+ ).replace(
+ '"private-fixture-upstream-detail"',
+ '"unexpected status 401 Unauthorized: OpenAI API fallback request could not complete., url: http://127.0.0.1:57500/v1/responses"',
+ ).replace(
+ ' print(json.dumps({"method": "item/agentMessage/delta", "params": {"threadId": "durable-thread", "turnId": active_turn, "delta": "Partial answer."}}), flush=True)\n',
+ "",
+ )
+ fake.write_text(source)
+ event = provider.event("notes-app", "relay-401", "typed terminal failure")
+ transport.admit("notes-app", event)
+ ref = identity_ref(binding["provider_ref"], event["message_id"])
+ original = transport.core.read_request(ref)
+ failed = runtime.wait_for_turn(session_id=original["session_id"], turn_id=original["turn_id"], timeout_sec=10)
+ assert failed["status"] == "failed" and failed["error_code"] == "upstream_transport_failure" and failed["delta_count"] == 0
+ transport.reconcile()
+ delivery_path = transport.root / f"{ref}.json"
+ old_notice = json.loads(delivery_path.read_text())["deliveries"]["terminal"]
+ runtime.adapters.pop(original["session_id"]).close_session()
+ Path(store.load_session(original["session_id"])["codex_home"]).mkdir(parents=True, exist_ok=True)
+ inventory = tmp_path / "relay-inventory.json"
+ inventory.write_text(json.dumps({
+ "turns": [{"id": failed["upstream_turn_id"], "status": "failed", "error": {
+ "codexErrorInfo": {"httpConnectionFailed": {"httpStatusCode": 401}},
+ "message": "unexpected status 401 Unauthorized: OpenAI API fallback request could not complete., url: http://127.0.0.1:57500/v1/responses",
+ }}],
+ "items": [{"turnId": failed["upstream_turn_id"], "item": {"type": "userMessage", "id": "user-1"}}],
+ }))
+ begin = source.index(' elif method == "thread/turns/list":')
+ end = source.index(' else:\n result = {}', begin)
+ reader = f''' elif method in {{"thread/turns/list", "thread/items/list"}}:
+ with open({str(inventory)!r}) as observed:
+ snapshot = json.load(observed)
+ result = {{"data": snapshot["turns" if method == "thread/turns/list" else "items"]}}
+ elif method == "thread/backgroundTerminals/list":
+ result = {{"data": []}}
+'''
+ fake.write_text((source[:begin] + reader + source[end:]).replace(
+ 'if "typed terminal failure" in json.dumps(request.get("params") or {}):',
+ "if False:",
+ ))
+ clock = [datetime.now(timezone.utc) + timedelta(seconds=61)]
+
+ class AfterBackoff(datetime):
+ @classmethod
+ def now(cls, tz=None):
+ return clock[0]
+
+ ready_state = [relay_ready_initially]
+ monkeypatch.setattr(external_conversations, "datetime", AfterBackoff)
+ monkeypatch.setattr(
+ adapter_module,
+ "_loopback_relay_transport_ready",
+ lambda *_a, **_kw: (ready_state[0], int(clock[0].timestamp())),
+ )
+ from loopx.extensions.lark.private_progress import presentation_phase
+
+ worker = LarkPrivateConversations(controller=runtime, runtime_root=store.root.parent,
+ runner=provider, cli_bin="lark-cli")
+ worker.reconcile()
+ row = worker.core.read_request(ref)
+ if not relay_ready_initially:
+ assert row["turn_id"] == original["turn_id"] and not row.get("context_recovery")
+ ready_state[0] = True
+ clock[0] += timedelta(minutes=1)
+ worker = LarkPrivateConversations(controller=runtime, runtime_root=store.root.parent,
+ runner=provider, cli_bin="lark-cli")
+ worker.reconcile()
+ row = worker.core.read_request(ref)
+ assert row["turn_id"] != original["turn_id"]
+ assert row["context_recovery"]["routing_attempt"] == 1
+ assert runtime.wait_for_turn(session_id=row["session_id"], turn_id=row["turn_id"], timeout_sec=10)["status"] == "completed"
+ worker.reconcile()
+ delivered = json.loads(delivery_path.read_text())
+ assert delivered["deliveries"]["terminal"] == old_notice
+ assert delivered["deliveries"][presentation_phase(row, "terminal")]["verified"]
+ starts = [json.loads(line) for line in capture.read_text().splitlines() if json.loads(line).get("method") == "turn/start"]
+ assert len(starts) == 2
+ finally:
+ runtime.close()
diff --git a/tests/test_lark_private_ingress.py b/tests/test_lark_private_ingress.py
index 4041f3d23b..97b7ba7cca 100644
--- a/tests/test_lark_private_ingress.py
+++ b/tests/test_lark_private_ingress.py
@@ -437,7 +437,7 @@ def answer(*, route, **_kwargs):
message_id = route["message_id"]
if message_id == group_events[0]["message_id"]:
in_group_answer.set()
- assert release.wait(8)
+ assert release.wait(15)
answered.append(message_id)
return "Group answer"
@@ -452,7 +452,7 @@ def admit(profile_name, event, **kwargs):
def first_connection():
yield group_events[0]
- assert in_group_answer.wait(8)
+ assert in_group_answer.wait(15)
yield group_events[1]
queued.set()
@@ -523,7 +523,7 @@ def change_private_binding(method, body):
try:
service.refresh()
- assert queued.wait(8), service.health_snapshot()
+ assert queued.wait(15), service.health_snapshot()
if retargeted or retarget_on_retry:
provider.profile_apps[profile] = "cli_retargeted_app"
if unreadable:
@@ -547,7 +547,7 @@ def read_profile(args, cwd=None, timeout=None):
rows = json.loads(response["stdout"])
response["stdout"] = json.dumps([row for row in rows if row["name"] != profile])
inventory_captured.set()
- assert return_inventory.wait(8)
+ assert return_inventory.wait(15)
return response
if binding_change == "stale_inventory_after_error" and not return_inventory.is_set():
return unreadable_inventory
@@ -575,7 +575,7 @@ def read_profile(args, cwd=None, timeout=None):
assert replacement["provider_ref"] == identity_ref(provider.profile_apps[profile])
if delayed_inventory:
retry = service._refresh_retry
- assert retry is not None and inventory_captured.wait(8)
+ assert retry is not None and inventory_captured.wait(15)
if inventory_during_queue_recovery:
resume_session_queues = service._resume_session_queues
@@ -583,7 +583,7 @@ def resume_after_retry(snapshot):
# The foreground has the current inventory but has yet
# to update the listener. Finish the older read here.
return_inventory.set()
- retry.join(8)
+ retry.join(15)
assert not retry.is_alive()
assert not original_worker[0].is_set()
resume_session_queues(snapshot)
@@ -596,27 +596,27 @@ def resume_after_retry(snapshot):
assert service._workers[profile] == original_worker
if closing:
release.set()
- assert second_replied.wait(8), service.health_snapshot()
+ assert second_replied.wait(15), service.health_snapshot()
service.close()
return_inventory.set()
- retry.join(8)
+ retry.join(15)
assert not retry.is_alive()
if removed_inventory:
assert original_worker[0].is_set()
- assert consumer_processes[0].exited.wait(8)
+ assert consumer_processes[0].exited.wait(15)
release.set()
- original_worker[1].join(8)
+ original_worker[1].join(15)
assert not original_worker[1].is_alive()
expected = group_events[:1]
else:
if stale_inventory:
assert not original_worker[0].is_set()
release.set()
- assert second_replied.wait(8), service.health_snapshot()
+ assert second_replied.wait(15), service.health_snapshot()
with service._lock:
recheck = service._refresh_retry
assert recheck is not None and recheck is not retry
- recheck.join(8)
+ recheck.join(15)
assert not recheck.is_alive()
assert timer_reads == [retry, recheck]
assert service._workers[profile] == original_worker
@@ -629,16 +629,16 @@ def resume_after_retry(snapshot):
expected = group_events[:2]
elif unreadable and not retarget_on_retry:
release.set()
- assert second_replied.wait(8), service.health_snapshot()
- assert inventory_retried.wait(8)
+ assert second_replied.wait(15), service.health_snapshot()
+ assert inventory_retried.wait(15)
assert service._workers[profile] == original_worker
assert len(consumer_processes) == 1
expected = group_events[:2]
else:
- assert consumer_processes[0].exited.wait(8)
+ assert consumer_processes[0].exited.wait(15)
assert len(consumer_processes) == 1
release.set()
- assert next_stream_ready.wait(8), service.health_snapshot()
+ assert next_stream_ready.wait(15), service.health_snapshot()
expected = group_events[:1] if different_transport or retarget_on_retry else group_events[:2]
if removing or unreadable:
expected += group_events[2:]
diff --git a/tests/test_loopx_turn_error_readback.py b/tests/test_loopx_turn_error_readback.py
index db01c19f65..f3b95f94fb 100644
--- a/tests/test_loopx_turn_error_readback.py
+++ b/tests/test_loopx_turn_error_readback.py
@@ -87,3 +87,80 @@ def test_unreadable_journal_does_not_replace_original_error_with_false_effects()
assert result["effects"]["host_invoked"] is None
assert result["effects"]["quota_spent"] is None
assert result["journal_observation"] == {"scope": "original_turn", "status": "unavailable"}
+
+
+@pytest.mark.parametrize("command", ["plan", "run-once"])
+def test_runtime_conflict_preserves_machine_diagnostic_without_parsing_message(command):
+ from loopx.control_plane.effect_runtime import EffectRuntimeConflict
+
+ result = build_turn_error_payload(
+ {"transaction": {"turn_key": "sha256:" + "b" * 64}},
+ EffectRuntimeConflict("The operation was refused", diagnostic_code="goal_stopped"),
+ turn_command=command, execution_started=True,
+ )
+ assert result["error_code"] == "goal_stopped"
+ assert result["error"] == "The operation was refused"
+ assert all(value is None for value in result["effects"].values())
+ if command == "run-once":
+ assert result["resume_turn_key"] == "sha256:" + "b" * 64
+
+
+def test_generic_error_does_not_invent_a_machine_diagnostic():
+ result = build_turn_error_payload({}, ValueError("goal_stopped"), turn_command="run-once")
+ assert "error_code" not in result
+
+
+def test_ordinary_turn_cli_keeps_runtime_conflict_after_host_result(tmp_path, monkeypatch, capsys):
+ import json
+ import sys
+
+ from loopx.cli import main
+ from loopx.control_plane.effect_runtime import EffectRuntimeConflict
+ from loopx.control_plane.turn_driver import executor
+ from tests.test_loopx_turn_codex_cli import _fake_codex
+ from tests.test_loopx_turn_driver import _write_live_fixture
+
+ project, runtime, registry = _write_live_fixture(tmp_path)
+ executable, log = _fake_codex(tmp_path)
+ monkeypatch.setenv("FAKE_CODEX_LOG", str(log))
+ original_write = executor._write_journal
+ rejected = []
+
+ def reject_result(path, journal, **kwargs):
+ if "host_result" in journal:
+ rejected.append(journal["turn_key"])
+ raise EffectRuntimeConflict("Result no longer accepted", diagnostic_code="goal_stopped")
+ return original_write(path, journal, **kwargs)
+
+ monkeypatch.setattr(executor, "_write_journal", reject_result)
+ code = main([
+ "--registry", str(registry), "--runtime-root", str(runtime), "--format", "json",
+ "turn", "run-once", "--goal-id", "loopx-turn-fixture", "--agent-id", "codex-fixture",
+ "--host", "codex-cli", "--project", str(project), "--codex-bin", str(executable),
+ "--codex-sandbox", "read-only", "--no-global-sync", "--iteration-context", "fresh",
+ "--turn-instance-id", "runtime-conflict-after-host-result",
+ "--validation-command-json", json.dumps([sys.executable, "-c", "pass"]), "--execute",
+ ])
+ payload = json.loads(capsys.readouterr().out)
+ assert code == 1 and rejected and log.exists(), payload
+ assert payload["error_code"] == "goal_stopped"
+ assert payload["resume_turn_key"] == rejected[0]
+ assert payload["effects"]["quota_spent"] is None
+ observation = payload["journal_observation"]
+ assert observation["status"] == "observed"
+ assert observation["completed_phases"] == []
+ assert observation["recorded_effects"]["quota_spent"] is False
+
+
+def test_existing_exception_code_and_payload_keep_precedence():
+ from loopx.control_plane.effect_runtime import EffectRuntimeConflict
+
+ class CodedConflict(EffectRuntimeConflict):
+ code = "existing_boundary_code"
+ payload = {"reason": "existing_reason"}
+
+ result = build_turn_error_payload(
+ {}, CodedConflict("refused", diagnostic_code="goal_stopped"), turn_command="run-once",
+ )
+ assert result["error_code"] == "existing_boundary_code"
+ assert result["reason"] == "existing_reason"
diff --git a/tests/test_manager_context_handoff.py b/tests/test_manager_context_handoff.py
index 210ae8830e..2be00b9e78 100644
--- a/tests/test_manager_context_handoff.py
+++ b/tests/test_manager_context_handoff.py
@@ -20,6 +20,7 @@
)
from loopx.capabilities.manager_context.tracking import query
from loopx.control_plane.capability_hooks import dispatch_turn_start_hooks
+from loopx.control_plane.goals.activation import build_goal_activation
@pytest.fixture
@@ -198,8 +199,8 @@ def reject_enumeration(_registry):
{},
{"goal_instance_id": "invalid"},
{"goal_instance_id": "ginst_" + "b" * 32, "id": "unsafe/alias"},
- {"goal_instance_id": "ginst_" + "b" * 32, "activation_state": "stopped"},
- {"goal_instance_id": "ginst_" + "b" * 32, "activation_state": "invalid"},
+ {"goal_instance_id": "ginst_" + "b" * 32, "activation": build_goal_activation(state="stopped", updated_at="2026-01-01T00:00:00Z", reason="fixture stop")},
+ {"goal_instance_id": "ginst_" + "b" * 32, "activation": {"schema_version": "loopx_goal_activation_v1", "state": "invalid"}},
])
def test_source_session_catalog_keeps_only_current_instantiated_recipients(
fixture, strict_envelope, other_goal
@@ -240,7 +241,11 @@ def test_stopped_goal_is_not_a_context_recipient_and_revokes_replay(fixture):
first = deliver(root, registry, session=session, turn=turn, request=request)
data = json.loads(registry.read_text())
- data["goals"][0]["activation_state"] = "stopped"
+ data["goals"][0]["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
registry.write_text(json.dumps(data))
assert authority(root, registry, session, turn)["targets"] == [
{"goal_id": "other", "agent_id": "peer"}
@@ -251,7 +256,11 @@ def test_stopped_goal_is_not_a_context_recipient_and_revokes_replay(fixture):
assert before == {path: path.read_bytes() for path in _root(root).rglob("*.json")}
assert len(pending(root, "research", "worker")["items"]) == 1
- data["goals"][0]["activation_state"] = "active"
+ data["goals"][0]["activation"] = build_goal_activation(
+ state="active",
+ updated_at="2026-01-01T00:01:00Z",
+ reason="fixture resume",
+ )
registry.write_text(json.dumps(data))
assert deliver(root, registry, session=session, turn=turn, request=request) == {
**first, "replayed": True
@@ -489,7 +498,11 @@ def test_operator_target_grant_fails_closed_without_audited_source_or_agent(fixt
source["evidence_goal_ids"] = ["research"]
_write(policy_path, {"schema_version": POLICY_SCHEMA, "sources": {channel: source}})
data = json.loads(registry.read_text())
- data["goals"][0]["activation_state"] = "stopped"
+ data["goals"][0]["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
registry.write_text(json.dumps(data))
with pytest.raises(ValueError, match="active Goal"):
configure_delivery_target(root, registry, channel=channel, **request, grant=True, execute=True)
@@ -558,7 +571,11 @@ def test_goal_delivery_grant_inherits_agents_and_rechecks_specific_revocation(fi
assert deliver(root, registry, session=session, turn=turn, request=future)["request_id"] == receipt["request_id"]
with pytest.raises(ValueError, match="not authorized"):
deliver(root, registry, session=session, turn=turn, request={"goal_id": "other", "agent_id": "peer"})
- data["goals"][0]["activation_state"] = "stopped"
+ data["goals"][0]["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
registry.write_text(json.dumps(data))
assert authority(root, registry, session, turn)["targets"] == []
diff --git a/tests/test_manager_preparation_recovery.py b/tests/test_manager_preparation_recovery.py
index 5f1743eb57..00bcf38dcb 100644
--- a/tests/test_manager_preparation_recovery.py
+++ b/tests/test_manager_preparation_recovery.py
@@ -142,3 +142,174 @@ def test_preparation_notice_is_conditional_and_does_not_promise_unknown_replays(
assert "不会自动重放" in manager_failure_reply(failure)[1]
assert "核验为模型调用前失败" in manager_failure_reply(failure, preparation_recovery=True)[1]
assert "不会自动重放" in manager_failure_reply({"error_code": "host_gate"}, preparation_recovery=True)[1]
+
+
+@pytest.mark.parametrize("case", ["before-dispatch", "after-dispatch", "stop", "revoked", "source-changed"])
+def test_steward_worker_loss_only_continues_verified_undispatched_input(steward, monkeypatch, case): # noqa: F811
+ import errno
+ import threading
+ from loopx.capabilities.native_chat.external_conversations import ChatExternalConversations
+ from loopx.chat import coordination
+
+ store, runtime, provider, transport, binding, capture, _ = steward
+ original_context = coordination.prepare_turn_context
+ original_execute = ChatExternalConversations.execute_turn
+ failed_writer = runtime._fail_turn
+ run_turn = runtime._run_turn
+ worker_lost = threading.Event()
+
+ def observe_injected_exit(**kwargs):
+ try:
+ run_turn(**kwargs)
+ except OSError as exc:
+ assert exc.errno == errno.ENOSPC
+ worker_lost.set()
+
+ monkeypatch.setattr(runtime, "_run_turn", observe_injected_exit)
+
+ def unavailable(*_args, **_kwargs):
+ raise OSError(errno.ENOSPC, "synthetic private path")
+
+ def prepare_then_lose(*args, **kwargs):
+ # Retain the real preparation events; they are not provider dispatch.
+ original_context(*args, **kwargs)
+ unavailable()
+
+ if case == "after-dispatch":
+ # Simulate a lost worker at the dispatch boundary. There is no native
+ # receipt and no delta, but they cannot prove that the request did not run.
+ monkeypatch.setattr(ChatExternalConversations, "execute_turn", staticmethod(unavailable))
+ monkeypatch.setattr(runtime, "_fail_turn", unavailable)
+ else:
+ monkeypatch.setattr(coordination, "prepare_turn_context", prepare_then_lose)
+
+ event = provider.event("steward-app", "original-worker-loss", "Original queued work")
+ assert transport.admit("steward-app", event)["status"] == "durably_accepted"
+ row = transport.core.pending()[0]
+ key = row["session_id"], row["turn_id"]
+ # Wait for the worker's kernel fence to be released, before recovery.
+ assert worker_lost.wait(timeout=12)
+ monkeypatch.setattr(runtime, "_run_turn", run_turn)
+ if case == "after-dispatch":
+ monkeypatch.setattr(runtime, "_fail_turn", failed_writer)
+ monkeypatch.setattr(ChatExternalConversations, "execute_turn", staticmethod(original_execute))
+ else:
+ monkeypatch.setattr(coordination, "prepare_turn_context", original_context)
+ runtime.recover_exited_turn(*key)
+ failed = finish(runtime, row)
+ assert failed["status"] == "failed" and failed["upstream_turn_id"] is None
+ expected = "execution_worker_exited" if case == "after-dispatch" else "execution_worker_exited_before_dispatch"
+ assert failed["error_code"] == expected
+ transport.reconcile()
+ path = transport.root / f"{row['request_ref']}.json"
+ notice = _read_json(path)["deliveries"]["terminal"]["text"]
+ assert "synthetic private path" not in notice
+ assert ("原队列会限次接续" in notice) is (case != "after-dispatch")
+ assert not any(json.loads(line)["method"] == "turn/start" for line in capture.read_text().splitlines())
+ advance_recovery_clock(monkeypatch)
+ if case == "stop":
+ assert transport.admit("steward-app", provider.event("steward-app", "stop-worker-loss", "/stop"))["status"] == "command_recorded"
+ elif case == "revoked":
+ transport.bindings.disconnect(binding["binding_id"], expected_revision=transport.bindings.read()["revision"])
+ elif case == "source-changed":
+ provider.messages[event["message_id"]]["content"] = "Edited input"
+ if case == "revoked":
+ with pytest.raises(ValueError, match="no longer authorized"):
+ transport.reconcile_request(path)
+ elif case == "before-dispatch":
+ # Exercise the whole pending-delivery scan, not a direct invocation of
+ # the recovery function that would also work if the scheduler missed it.
+ transport._context_recovery_probe_at.clear()
+ transport.reconcile()
+ else:
+ transport.reconcile_request(path)
+ resumed = transport.core.read_request(row["request_ref"])
+ if case == "before-dispatch":
+ assert resumed["session_id"] == key[0] and resumed["turn_id"] != key[1]
+ assert resumed["context_recovery"]["preparation_attempt"] == 1
+ assert finish(runtime, resumed)["status"] == "completed"
+ transport.reconcile_request(path)
+ transport.reconcile_request(path)
+ deliveries = _read_json(path)["deliveries"]
+ assert sum(key.startswith("terminal.recovery.") and value.get("verified") is True
+ for key, value in deliveries.items()) == 1
+ assert sum(json.loads(line)["method"] == "turn/start" for line in capture.read_text().splitlines()) == 1
+ else:
+ assert resumed["turn_id"] == key[1] and not resumed.get("context_recovery")
+ assert not any(json.loads(line)["method"] == "turn/start" for line in capture.read_text().splitlines())
+ assert store.load_turn(*key) == failed
+
+
+def test_antigravity_steward_recovers_pre_dispatch_resume_failed_after_lost_notice_receipt_without_disk_churn(
+ steward, language_server, monkeypatch # noqa: F811
+):
+ from loopx.capabilities.steward_executor import STEWARD_EXECUTOR_SCHEMA, effective_steward_executor_defaults
+ from loopx.chat_agent import CodexChatAgentError
+
+ store, runtime, provider, transport, binding, capture, _ = steward
+ defaults = effective_steward_executor_defaults({"schema_version": "loopx_machine_configuration_v0",
+ "namespaces": {"steward_executor": {"schema_version": STEWARD_EXECUTOR_SCHEMA, "selection_policy": "preferred",
+ "executor_endpoint": "antigravity", "eligible_endpoints": [],
+ "executor_model": "gemini-4-argon-high", "executor_reasoning_effort": "high"}}})
+ monkeypatch.setattr(runtime, "steward_executor_defaults", lambda: defaults)
+ transport.bindings.configure(transport_ref="steward-app", project_ref=binding["project_ref"],
+ executor_endpoint_id="antigravity", context_kind="steward")
+
+ ensure = runtime._ensure_adapter
+
+ def resume_unavailable(*_args, **_kwargs):
+ raise CodexChatAgentError("The previous Agent conversation could not be restored.",
+ error_code="resume_failed", gate=None)
+
+ monkeypatch.setattr(runtime, "_ensure_adapter", resume_unavailable)
+ assert transport.admit("steward-app", provider.event("steward-app", "antigravity-resume-loss", "Analyze repo execution value"))["status"] == "durably_accepted"
+ row = transport.core.pending()[0]
+ failed = finish(runtime, row)
+ assert failed["status"] == "failed" and failed["error_code"] == "resume_failed"
+ assert failed["started_at"] is None and failed["upstream_turn_id"] is None
+
+ provider.lose_next_reply = True
+ transport.reconcile()
+ path = transport.root / f"{row['request_ref']}.json"
+ record = _read_json(path)
+ assert record["status"] == "accepted"
+ assert record["deliveries"]["terminal"]["started"] is True
+ assert not record["deliveries"]["terminal"].get("attempt")
+ assert not transport.core.read_request(row["request_ref"]).get("delivery_verified")
+
+ # Second reconcile records provider_delivery_receipt_unknown without resending;
+ # subsequent ticks before cooldown neither queue it as an unsent body nor rewrite disk.
+ writes_before = list(provider.writes)
+ transport.reconcile()
+ assert provider.writes == writes_before
+ record = _read_json(path)
+ assert record["deliveries"]["terminal"]["blocker"] == "provider_delivery_receipt_unknown"
+ bytes_before = path.read_bytes()
+ mtime_before = path.stat().st_mtime_ns
+ assert transport.pending_body_paths() == []
+ transport._context_recovery_probe_at.clear()
+ transport.reconcile()
+ assert provider.writes == writes_before
+ assert path.read_bytes() == bytes_before
+ assert path.stat().st_mtime_ns == mtime_before
+
+ monkeypatch.setattr(runtime, "_ensure_adapter", ensure)
+ advance_recovery_clock(monkeypatch)
+ transport._context_recovery_probe_at.clear()
+ transport.reconcile()
+
+ resumed = transport.core.read_request(row["request_ref"])
+ assert resumed["turn_id"] != failed["turn_id"]
+ assert resumed["context_recovery"]["preparation_attempt"] == 1
+ assert resumed["context_recovery"]["failure_notice_delivery_verified"] is False
+ continued = finish(runtime, resumed)
+ assert continued["status"] == "completed"
+ transport.reconcile()
+
+ final_record = _read_json(path)
+ assert final_record["status"] == "delivered"
+ assert final_record["deliveries"]["terminal"] == record["deliveries"]["terminal"]
+ assert any(key.startswith("terminal.recovery.") and key.endswith(".preparation.1") and value.get("verified") is True
+ for key, value in final_record["deliveries"].items())
+ assert language_server["sent_count"] == 1
+ assert store.load_turn(row["session_id"], failed["turn_id"]) == failed
diff --git a/tests/test_native_steward_monitor.py b/tests/test_native_steward_monitor.py
index ba51e97a76..9fa118d700 100644
--- a/tests/test_native_steward_monitor.py
+++ b/tests/test_native_steward_monitor.py
@@ -1372,3 +1372,208 @@ def flaky_repo_observe(repo_slug, **_kwargs):
recovered_done = finish(runtime, recovered_try[0])
assert recovered_done["status"] == "completed", recovered_done
assert local_watch_row(runtime, actor, pr_todo_id)["next_due_at"] != third_due
+
+
+@pytest.mark.parametrize(
+ ("error_code", "error_message", "fail_before_context"),
+ [
+ (
+ "runtime_error",
+ "The LoopX Chat execution worker exited before this turn finished. Your conversation is saved; execution may have occurred. This turn was not retried automatically.",
+ False,
+ ),
+ ("runtime_error", "disk I/O error", False),
+ (
+ "runtime_unavailable",
+ "The Agent runtime could not be prepared. Check the runtime installation and configuration, then retry.",
+ True,
+ ),
+ ],
+)
+def test_transient_zero_effect_monitor_failures_recover_once(
+ steward, tmp_path, monkeypatch, error_code, error_message, fail_before_context,
+):
+ from loopx.capabilities.native_chat.steward_turn import StewardTurnHost
+
+ store, runtime, *_ = steward
+ sid, actor, monitor = prepared(steward, tmp_path, monkeypatch, "sqlite")
+ default_watch = monitor.current(actor)
+ todo_id = default_watch["todo_id"]
+ due = (datetime.now(timezone.utc) - timedelta(seconds=2)).isoformat()
+ edit_local_watch(runtime, actor, todo_id, "--next-due-at", due)
+ with monkeypatch.context() as paused:
+ paused.setattr(runtime, "_start_accepted_turn_worker", lambda **_kw: True)
+ runtime._steward_monitor_next_check.clear()
+ first = pump_steward_monitors(runtime)
+ assert len(first) == 1
+ failed_turn = first[0]
+ turn_id = failed_turn["turn_id"]
+ if fail_before_context:
+ runtime._fail_turn(
+ sid, turn_id, error_code, error_message,
+ status="failed", expected_statuses={"queued", "starting", "running"},
+ )
+ store.append_event(
+ sid, turn_id, kind="agent.phase",
+ payload={"phase": "manager_context", "label": "Reading authorized Goal state"},
+ )
+ store.append_event(
+ sid, turn_id, kind="manager.context",
+ payload={"schema_version": "manager_turn_context_v1", "goals": []},
+ )
+ if not fail_before_context:
+ runtime._fail_turn(
+ sid, turn_id, error_code, error_message,
+ status="failed", expected_statuses={"queued", "starting", "running"},
+ )
+ request = StewardTurnHost.monitor_restart_request(store.load_turn(sid, turn_id))
+ assert request is not None and request["monitor_restart_recovery"]["original_turn_id"] == turn_id
+
+ runtime._steward_monitor_next_check.clear()
+ recovered = pump_steward_monitors(runtime)
+ assert len(recovered) == 1
+ assert recovered[0]["loopx_request"]["monitor_restart_recovery"]["original_turn_id"] == turn_id
+ done = finish(runtime, recovered[0])
+ assert done["status"] == "completed", done
+ assert local_watch_row(runtime, actor, todo_id)["next_due_at"] != due
+
+ # Even if a restart carrier itself fails without polling, it must not spawn a second retry.
+ store.update_turn(sid, done["turn_id"], status="failed", error_code=error_code, error=error_message)
+ carrier = store.load_turn(sid, done["turn_id"])
+ assert carrier is not None and carrier["client_turn_id"].endswith(".restart-recovery")
+ assert StewardTurnHost.monitor_restart_request(carrier) is None
+
+
+def test_chained_transient_monitor_recovery_waits_for_cooldown_then_recovers(
+ steward, tmp_path, monkeypatch,
+):
+ from loopx.capabilities.native_chat.steward_turn import MONITOR_RECOVERY_RETRY_COOLDOWN_SECONDS, StewardTurnHost
+
+ store, runtime, *_ = steward
+ sid, actor, monitor = prepared(steward, tmp_path, monkeypatch, "sqlite")
+ default_watch = monitor.current(actor)
+ todo_id = default_watch["todo_id"]
+ due = (datetime.now(timezone.utc) - timedelta(seconds=2)).isoformat()
+ edit_local_watch(runtime, actor, todo_id, "--next-due-at", due)
+
+ with monkeypatch.context() as paused:
+ paused.setattr(runtime, "_start_accepted_turn_worker", lambda **_kw: True)
+ runtime._steward_monitor_next_check.clear()
+ initial = pump_steward_monitors(runtime)[0]
+ store.append_event(sid, initial["turn_id"], kind="agent.phase", payload={"phase": "manager_context"})
+ store.append_event(sid, initial["turn_id"], kind="manager.context",
+ payload={"schema_version": "manager_turn_context_v1", "goals": []})
+ runtime._fail_turn(sid, initial["turn_id"], "runtime_error", "disk I/O error",
+ status="failed", expected_statuses={"queued", "starting", "running"})
+
+ with monkeypatch.context() as paused:
+ paused.setattr(runtime, "_start_accepted_turn_worker", lambda **_kw: True)
+ runtime._steward_monitor_next_check.clear()
+ carrier = pump_steward_monitors(runtime)[0]
+ store.append_event(sid, carrier["turn_id"], kind="agent.phase", payload={"phase": "manager_context"})
+ store.append_event(sid, carrier["turn_id"], kind="manager.context",
+ payload={"schema_version": "manager_turn_context_v1", "goals": []})
+ runtime._fail_turn(sid, carrier["turn_id"], "runtime_unavailable",
+ "The Agent runtime could not be prepared.",
+ status="failed", expected_statuses={"queued", "starting", "running"})
+
+ # Immediately after carrier failure (< cooldown), pumping stays idle instead of spinning.
+ runtime._steward_monitor_next_check.clear()
+ assert pump_steward_monitors(runtime) == []
+
+ # After cooldown elapses, a bounded .restart-recovery. carrier recovers and polls.
+ aged_completed = (
+ datetime.now(timezone.utc) - timedelta(seconds=MONITOR_RECOVERY_RETRY_COOLDOWN_SECONDS + 5)
+ ).isoformat().replace("+00:00", "Z")
+ store.update_turn(sid, carrier["turn_id"], completed_at=aged_completed)
+ req = StewardTurnHost.monitor_restart_request(store.load_turn(sid, carrier["turn_id"]))
+ assert req is not None and req["monitor_restart_recovery"]["original_turn_id"] == carrier["turn_id"]
+
+ runtime._steward_monitor_next_check.clear()
+ chained = pump_steward_monitors(runtime)
+ assert len(chained) == 1
+ assert chained[0]["client_turn_id"].endswith(".restart-recovery." + carrier["turn_id"])
+ assert chained[0]["loopx_request"]["monitor_restart_recovery"]["original_turn_id"] == carrier["turn_id"]
+ done = finish(runtime, chained[0])
+ assert done["status"] == "completed", done
+ assert local_watch_row(runtime, actor, todo_id)["next_due_at"] != due
+
+
+def test_session_queue_drain_preserves_active_and_queued_observation_only_monitors(
+ steward, tmp_path, monkeypatch,
+):
+ from loopx.chat_agent import CodexChatAgentError
+ import json
+ import threading
+
+ store, runtime, *_ = steward
+ sid, actor, monitor = prepared(steward, tmp_path, monkeypatch, "sqlite")
+ default_watch = monitor.current(actor)
+ todo_id = default_watch["todo_id"]
+ due = (datetime.now(timezone.utc) - timedelta(seconds=2)).isoformat()
+ edit_local_watch(runtime, actor, todo_id, "--next-due-at", due)
+
+ # Create a crash-stranded queued observation-only monitor turn (no _acceptance, status=queued).
+ with monkeypatch.context() as paused:
+ paused.setattr(runtime, "_start_accepted_turn_worker", lambda **_kw: True)
+ runtime._steward_monitor_next_check.clear()
+ queued_watch = pump_steward_monitors(runtime)[0]
+ turn_id = queued_watch["turn_id"]
+ store.update_session(sid, active_turn_id=None)
+ queued_doc = store.load_turn(sid, turn_id)
+ assert queued_doc is not None
+ queued_doc.pop("_acceptance", None)
+ queued_doc["status"] = "queued"
+ queued_doc["started_at"] = None
+ store._turn_path(sid, turn_id).write_text(json.dumps(queued_doc))
+
+ adapter_calls = 0
+ fail_calls = 0
+
+ def fail_ensure_adapter(*_args, **_kwargs):
+ nonlocal adapter_calls
+ adapter_calls += 1
+ raise CodexChatAgentError("Native account API transport is unavailable.")
+
+ orig_fail = runtime._fail_queue_preparation
+
+ def spy_fail(*args, **kwargs):
+ nonlocal fail_calls
+ fail_calls += 1
+ return orig_fail(*args, **kwargs)
+
+ # First verify draining while monitor is already running does not invoke _ensure_adapter or fail it,
+ # and second verify draining a crash-stranded queued monitor uses StewardObservationAdapter cleanly.
+ store.update_session(sid, active_turn_id=turn_id)
+ store.update_turn(sid, turn_id, status="running", started_at=queued_doc["created_at"])
+ monkeypatch.setattr(runtime, "_ensure_adapter", fail_ensure_adapter)
+ monkeypatch.setattr(runtime, "_fail_queue_preparation", spy_fail)
+
+ def complete_soon():
+ time.sleep(0.12)
+ store.update_turn(sid, turn_id, status="completed", response={"message": "ok", "proposals": []})
+
+ t = threading.Thread(target=complete_soon, daemon=True)
+ t.start()
+ runtime._drain_session_queue(session_id=sid, work_dir=tmp_path, objective="Monitor")
+ t.join(timeout=2)
+ assert adapter_calls == 0
+ assert fail_calls == 0
+ assert store.load_turn(sid, turn_id)["status"] == "completed"
+
+ # Now verify crash-stranded queued monitor branch invokes _start_accepted_turn_worker with StewardObservationAdapter.
+ dispatched: list[object] = []
+ store.update_session(sid, active_turn_id=None)
+ queued_doc["status"] = "queued"
+ queued_doc["started_at"] = None
+ store._turn_path(sid, turn_id).write_text(json.dumps(queued_doc))
+
+ def fake_run_turn(*, adapter, turn_id: str, **_kw):
+ dispatched.append(adapter)
+ store.update_turn(sid, turn_id, status="completed", response={"message": "ok", "proposals": []})
+ store.update_session(sid, active_turn_id=None)
+
+ monkeypatch.setattr(runtime, "_run_turn", fake_run_turn)
+ runtime._drain_session_queue(session_id=sid, work_dir=tmp_path, objective="Monitor")
+ assert adapter_calls == 0 and fail_calls == 0
+ assert len(dispatched) == 1 and getattr(dispatched[0], "observation_only", False) is True
diff --git a/tests/test_native_steward_reports.py b/tests/test_native_steward_reports.py
index 64e6cfb099..70af53558a 100644
--- a/tests/test_native_steward_reports.py
+++ b/tests/test_native_steward_reports.py
@@ -1034,6 +1034,113 @@ def legacy_selection(host, original, **kwargs):
assert (capture.read_bytes(), bot.writes) == after
+def test_report_recovers_via_admission_resume_once_blocking_replan_clears_after_contract_failure(steward, tmp_path, monkeypatch):
+ from datetime import timezone
+ from loopx.capabilities.periodic_report import cadence_runtime
+ from loopx.capabilities.native_chat.steward_monitor import StewardMonitor
+ from loopx.configure_goal import configure_goal
+ import loopx.capabilities.native_chat.steward_reports as report_host
+
+ store, runtime, bot, transport, binding, capture, _ = steward
+ point = datetime.now(timezone.utc).replace(hour=9, minute=1, second=0, microsecond=0) - timedelta(days=2)
+
+ class CalendarClock(datetime):
+ @classmethod
+ def now(cls, tz=None):
+ return point.astimezone(tz) if tz else point.replace(tzinfo=None)
+
+ monkeypatch.setattr(report_host, "datetime", CalendarClock)
+ monkeypatch.setattr(cadence_runtime, "datetime", CalendarClock)
+ sid, actor, reports, _ = configured(steward, tmp_path, monkeypatch, "sqlite")
+ owner = binding["binding_id"]
+ assert configure_goal(
+ registry_path=runtime.registry_path, goal_id=actor["goal_id"],
+ execution_replan_after_turns=1, execute=True,
+ )["ok"]
+ install_model(steward, tmp_path, sid)
+ assert finish(runtime, pump_steward_monitors(runtime)[0])["response"]["native_loopx"]["status"] == "committed"
+ assert transport.steward_reports.reconcile() == 1
+
+ point += timedelta(days=1)
+ runtime._steward_monitor_next_check.clear()
+ rejected = finish(runtime, pump_steward_monitors(runtime)[0])
+ assert rejected["response"]["native_loopx"]["error_code"] == "turn_todo_selection_deferred"
+ _, second_window = reports.current_window(owner)
+
+ prompts_seen: list[str] = []
+ # Return invalid host result contract (`replan_required` + `outcome_gap` without typed blocked fields).
+ bad_vision = {
+ "schema_version": "goal_vision_replan_contract_v0",
+ "state": "active",
+ "vision_patch": {"vision_summary": "Keep reporting.", "acceptance_summary": "Validate full report."},
+ "path_delta": {
+ "outcome": "replan",
+ "prior_assumption": "Old assumption",
+ "observed_reality": "Reality",
+ "retained": ["Same report route"],
+ "changed": ["Updated baseline"],
+ "evidence_refs": ["fixture:evidence"],
+ },
+ }
+ bad_outcome = {
+ "result_kind": "replan_required",
+ "classification": "outcome_gap_without_blocked_fields",
+ "delivery_batch_scale": "implementation",
+ "delivery_outcome": "outcome_gap",
+ "work_evidence": ["Observed gap"],
+ "validation_evidence": ["Validated check"],
+ "path_delta_mode": "material_replan",
+ "agent_vision_json": json.dumps(bad_vision),
+ "summary": "Missing blocked fields trigger host_result_contract validation failure.",
+ "next_action": "Clear replan before resuming report.",
+ }
+ helper = tmp_path / "report_model.py"
+ helper.write_text(
+ _REPORT_MODEL_SOURCE.replace("def fake_report_model(", "def report_only(")
+ + "\ndef fake_report_model(params):\n"
+ + " import json, pathlib\n"
+ + " prompt = '\\n'.join(row.get('text', '') for row in params.get('input', []) if isinstance(row, dict))\n"
+ + f" pathlib.Path({str(tmp_path / 'prompts.jsonl')!r}).open('a').write(json.dumps(prompt) + '\\n')\n"
+ + " request = json.JSONDecoder().raw_decode(prompt.split('Turn request:', 1)[1].lstrip())[0]\n"
+ + " if request.get('detail_ref', {}).get('replan_action_packet'):\n"
+ + " return " + repr(json.dumps(bad_outcome)) + "\n"
+ + " return report_only(params)\n"
+ )
+ runtime._steward_monitor_next_check.clear()
+ retry_turn = finish(runtime, pump_steward_monitors(runtime)[0])
+ prompts_seen = [json.loads(line) for line in (tmp_path / "prompts.jsonl").read_text().splitlines() if line.strip()]
+ assert any("Governance contract reminder:" in text for text in prompts_seen)
+ replan_events = [row["payload"] for row in store.events_after(sid, retry_turn["turn_id"], None)
+ if row["kind"] == "native_loopx.replan_result"]
+ assert len(replan_events) == 1
+ assert replan_events[0]["status"] == "failed" and replan_events[0]["validation_stage"] == "host_result_contract"
+
+ # While the autonomous replan obligation stays active, pumping must NOT spin or resume.
+ runtime._steward_monitor_next_check.clear()
+ assert not pump_steward_monitors(runtime)
+
+ # Once the blocking replan is cleared elsewhere, one .admission-resume carrier completes the original report cleanly.
+ assert configure_goal(
+ registry_path=runtime.registry_path, goal_id=actor["goal_id"],
+ execution_replan_after_turns=6, execute=True,
+ )["ok"]
+ calls = capture.read_bytes()
+ runtime._steward_monitor_next_check.clear()
+ resumed = pump_steward_monitors(runtime)
+ assert len(resumed) == 1
+ done = finish(runtime, resumed[0])
+ assert done["response"]["native_loopx"]["status"] == "committed", done
+ runtime._steward_monitor_next_check.clear()
+ assert not pump_steward_monitors(runtime)
+ assert transport.steward_reports.reconcile() == 1
+ assert reports.health(owner)["publication_verified"]
+ task = next(row for row in StewardMonitor(runtime).todos(actor) if row["todo_id"] == done["loopx_request"]["todo_id"])
+ assert task["status"] == "done"
+ tail = [json.loads(line) for line in capture.read_bytes()[len(calls):].splitlines()]
+ assert sum(row.get("method") == "turn/start" for row in tail) == 1
+ assert transport.steward_reports.reconcile() == 0
+
+
def embedded_report_python(tmp_path, monkeypatch):
"""A real child interpreter without the embedding host's LoopX bootstrap."""
import subprocess
@@ -2005,3 +2112,102 @@ def interrupted_owner(*args, **kwargs):
assert body == (folder / "report.md").read_bytes()
assert read_periodic_report_publication_cursor(runtime_root=reports.root,
goal_id=actor["goal_id"], agent_id=actor["agent_id"])["covered_until"] == window["next_due_at"]
+
+
+@pytest.mark.parametrize("provider", ["file", "sqlite"])
+def test_stopped_no_typed_result_report_recovers_saved_editorial_candidate_without_model_replay(steward, tmp_path, monkeypatch, provider):
+ from loopx.capabilities.native_chat.steward_monitor import StewardMonitor
+ from loopx.control_plane.turn_driver import host_candidate
+ from loopx.control_plane.turn_driver.journal_store import find_loopx_turn_key_by_settlement_identity, load_turn_journal, turn_journal_path
+
+ store, runtime, bot, transport, binding, capture, _ = steward
+ sid, actor, reports, _ = configured(steward, tmp_path, monkeypatch, provider)
+ install_model(steward, tmp_path, sid)
+
+ # Simulate an older parser build failing on one trailing comma + stray outer
+ # closer, leaving the Turn journal stopped with `no_typed_host_result` while
+ # `answer.final` preserved the complete `editorial_response` text.
+ helper = tmp_path / "report_model.py"
+ raw_source = helper.read_text()
+ helper.write_text(
+ raw_source.replace(
+ ' return json.dumps({"result_kind":',
+ ' encoded = json.dumps({"result_kind":',
+ ).replace(
+ '"editorial_response": response}, ensure_ascii=False)',
+ '"editorial_response": response}, ensure_ascii=False)\n'
+ ' assert encoded.endswith("}]}}")\n'
+ ' return encoded[:-3].replace(\'"tone": "neutral"}\', \'"tone": "neutral",}\', 1) + "}]}}"',
+ )
+ )
+
+ real_parse = host_candidate.parse_model_json
+ with monkeypatch.context() as old_parser:
+ old_parser.setattr(host_candidate, "parse_model_json", lambda _text: None)
+ first = finish(runtime, pump_steward_monitors(runtime)[0])
+
+ native = first["response"]["native_loopx"]
+ assert native["status"] == "stopped" and native["result_kind"] == "wait"
+ orig_journal = load_turn_journal(turn_journal_path(reports.root, goal_id=actor["goal_id"], turn_key=native["resume_turn_key"]))
+ assert orig_journal["host_result"]["classification"] == "no_typed_host_result"
+ window = read_cadence_journal(runtime_root=reports.root, goal_id=actor["goal_id"])["window"]
+ folder = attempt_directory(reports.root, window)
+ assert not (folder / "private-generation.json").exists()
+ assert not (folder / "editorial-response.json").exists()
+ todo = next(row for row in StewardMonitor(runtime).todos(actor) if row["todo_id"] == first["loopx_request"]["todo_id"])
+ assert todo["status"] == "open"
+ assert transport.steward_reports.reconcile() == 0
+
+ calls, writes = capture.read_bytes(), list(bot.writes)
+ runtime._steward_monitor_next_check.clear()
+ assert real_parse('{"items": [{"ok": true,}]}}') == {"items": [{"ok": True}]}
+ assert not pump_steward_monitors(runtime)
+ assert capture.read_bytes() == calls
+ assert bot.writes == writes
+
+ assert validate_private_generation(folder)
+ assert (folder / "report.md").is_file()
+ todo_after = next(row for row in StewardMonitor(runtime).todos(actor) if row["todo_id"] == todo["todo_id"])
+ assert todo_after["status"] == "done"
+ assert todo_after["completion_turn_key"] == first["loopx_request"]["turn_instance_id"] + ".saved-candidate"
+ saved_key = find_loopx_turn_key_by_settlement_identity(
+ reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"],
+ todo_id=todo["todo_id"], turn_instance_id=todo_after["completion_turn_key"],
+ )
+ assert saved_key and load_turn_journal(turn_journal_path(reports.root, goal_id=actor["goal_id"], turn_key=saved_key))["status"] == "committed"
+
+ assert transport.steward_reports.reconcile() == 1
+ assert len(bot.writes) > len(writes)
+ cursor = read_periodic_report_publication_cursor(runtime_root=reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"])
+ assert cursor and cursor["covered_until"] == window["due_at"]
+ runtime._steward_monitor_next_check.clear()
+ assert not pump_steward_monitors(runtime)
+ assert transport.steward_reports.reconcile() == 0
+ assert capture.read_bytes() == calls
+
+
+def test_stopped_report_without_valid_saved_editorial_candidate_stays_closed(steward, tmp_path, monkeypatch):
+ from loopx.capabilities.native_chat.steward_monitor import StewardMonitor
+ from loopx.control_plane.turn_driver import host_candidate
+
+ store, runtime, bot, transport, binding, capture, _ = steward
+ sid, actor, reports, _ = configured(steward, tmp_path, monkeypatch, "sqlite")
+ install_model(steward, tmp_path, sid)
+ helper = tmp_path / "report_model.py"
+ helper.write_text('def fake_report_model(_params):\n return "{\\"result_kind\\": \\"validated_completion\\", \\"truncated\\": "\n')
+
+ with monkeypatch.context() as old_parser:
+ old_parser.setattr(host_candidate, "parse_model_json", lambda _text: None)
+ first = finish(runtime, pump_steward_monitors(runtime)[0])
+
+ assert first["response"]["native_loopx"]["status"] == "stopped"
+ window = read_cadence_journal(runtime_root=reports.root, goal_id=actor["goal_id"])["window"]
+ folder = attempt_directory(reports.root, window)
+ calls, writes = capture.read_bytes(), list(bot.writes)
+ runtime._steward_monitor_next_check.clear()
+ assert not pump_steward_monitors(runtime)
+ assert transport.steward_reports.reconcile() == 0
+ assert capture.read_bytes() == calls and bot.writes == writes
+ assert not (folder / "private-generation.json").exists()
+ todo = next(row for row in StewardMonitor(runtime).todos(actor) if row["todo_id"] == first["loopx_request"]["todo_id"])
+ assert todo["status"] == "open"
diff --git a/tests/test_native_steward_turn_host.py b/tests/test_native_steward_turn_host.py
index 3c11caab85..5e8e6ca99f 100644
--- a/tests/test_native_steward_turn_host.py
+++ b/tests/test_native_steward_turn_host.py
@@ -542,16 +542,16 @@ def test_managed_work_never_commits_without_current_independent_postcondition(st
observation.write_text("not ready")
if fault == "stopped_after_model":
from loopx.capabilities.native_chat.codex_adapter import CodexAppServerAdapter
- original_start = CodexAppServerAdapter.start_turn
+ original_start = CodexAppServerAdapter.start_native_task_turn
- def stopped(adapter, message, sink):
- response = original_start(adapter, message, sink)
+ def stopped(adapter, message, sink, policy):
+ response = original_start(adapter, message, sink, policy)
registry = json.loads(runtime.registry_path.read_text())
registry["goals"][0]["activation"] = dict(STOPPED_ACTIVATION)
runtime.registry_path.write_text(json.dumps(registry))
return response
- monkeypatch.setattr(CodexAppServerAdapter, "start_turn", stopped)
+ monkeypatch.setattr(CodexAppServerAdapter, "start_native_task_turn", stopped)
calls = capture.read_bytes()
accepted, _ = service.submit(session["session_id"], agent_id=actor["agent_id"], operation_id="unqualified", todo_id=todo_id)
completed = finish(runtime, accepted)
@@ -1120,3 +1120,154 @@ def test_managed_host_does_not_borrow_another_execution_lease(steward, tmp_path,
retained = cli(runtime, "task-lease", "inspect", "--goal-id", actor["goal_id"], "--todo-id", todo_id)
assert retained["active"] is True and retained["lease"] == original
assert store.load_session(session["session_id"])["upstream_thread_id"] == session["upstream_thread_id"]
+
+
+def test_native_steward_advancement_and_report_run_on_qualified_antigravity_fallback(steward, tmp_path, monkeypatch):
+ import re
+ from test_chat_manager_model_adoption import configure
+ from test_native_steward_reports import configured, fake_report_model
+ from loopx.capabilities.native_chat.antigravity_adapter import AntigravityAdapter
+ from loopx.capabilities.native_chat.steward_monitor import pump_steward_monitors
+ from loopx.chat_manager import steward_machine_defaults
+
+ sid, actor, reports, _ = configured(steward, tmp_path, monkeypatch, "sqlite")
+ store, runtime, _, transport, binding, capture, workspace = steward
+ configure(runtime.coordination_runtime_root, model="gpt-6.2-sol", effort="xhigh", endpoint="codex", policy="flexible")
+ defaults = steward_machine_defaults(runtime)
+ added = cli(
+ runtime,
+ "todo",
+ "add",
+ "--goal-id",
+ actor["goal_id"],
+ "--role",
+ "agent",
+ "--text",
+ "Advance native steward triage on Carbon fallback.",
+ "--claimed-by",
+ "steward",
+ "--action-kind",
+ "inspect",
+ "--continuation-policy",
+ "same_agent_non_delivery",
+ "--execute",
+ )
+ session = store.load_session(sid)
+ orig_thread = session["upstream_thread_id"]
+ turn_switch, _ = store.create_turn(sid, client_turn_id="fallback-switch", message="Failover to Antigravity")
+ store.update_turn(sid, turn_switch["turn_id"], status="queued")
+ store.update_session(sid, status="ready", active_turn_id=turn_switch["turn_id"], agent_model_id="gpt-6.2-sol")
+ session = store.load_session(sid)
+ allocation = {
+ "schema_version": "manager_executor_allocation_v0",
+ "selection_policy": "flexible",
+ "allocation_reason": "flexible_availability_fallback",
+ "executor_endpoint": "antigravity",
+ "executor_endpoint_source": "flexible_pool",
+ "executor_endpoint_default_reason": "",
+ "configured_endpoint": "codex",
+ "eligible_endpoints": ["codex", "antigravity"],
+ "configuration_revision": defaults["configuration_revision"],
+ "available": True,
+ "model": "gemini-4-carbon-high",
+ "model_source": "vendor_default",
+ "reasoning_effort": "high",
+ "configured_model": "gpt-6.2-sol",
+ "configured_reasoning_effort": "xhigh",
+ }
+ store.update_session(
+ sid,
+ status="ready",
+ active_turn_id=turn_switch["turn_id"],
+ expected_upstream_thread_id=orig_thread,
+ agent_id="antigravity",
+ executor_endpoint_id="antigravity",
+ adapter_kind="antigravity_ls",
+ upstream_mode="default",
+ upstream_thread_id="ag-cascade-1",
+ agent_model_id="gemini-4-carbon-high",
+ agent_reasoning_effort="high",
+ manager_executor_allocation=allocation,
+ upstream_context_history=[
+ *(session.get("upstream_context_history") or []),
+ {
+ "thread_id": orig_thread,
+ "manager_context_version": session.get("manager_context_version"),
+ "agent_id": "codex",
+ "agent_model_id": "gpt-6.2-sol",
+ },
+ ],
+ )
+ store.update_turn(sid, turn_switch["turn_id"], status="completed")
+ store.update_session(sid, active_turn_id=None)
+ if sid in runtime.adapters:
+ runtime.adapters.pop(sid).close_session()
+
+ guards = []
+
+ def fake_start(cls, *, work_dir, objective, resume_thread_id=None, history=None,
+ runtime_profile="restricted", team_plan_context=None, executor_model=None,
+ env=None, hard_timeout_sec=None):
+ return cls(
+ work_dir=work_dir,
+ objective=objective,
+ cascade_id=resume_thread_id or "ag-cascade-1",
+ ls_url="http://127.0.0.1:1",
+ csrf_token="tok",
+ model_id=(executor_model or {}).get("model") or "gemini-4-carbon-high",
+ model_placeholder="MODEL_PLACEHOLDER_M427",
+ reasoning_effort="high",
+ runtime_profile=runtime_profile,
+ )
+
+ def fake_turn(self, message, event_sink=None, **_kwargs):
+ with (self._launch_guard() if self._launch_guard is not None else contextlib.nullcontext()):
+ guards.append((type(self).__name__, self._steward_report_reader is not None))
+ match = re.search(r'"editorial_request_path":\s*"([^"\n]+)"', message)
+ if match:
+ body = fake_report_model({"input": [{"text": message}]})
+ else:
+ body = json.dumps({
+ "result_kind": "validated_completion",
+ "classification": "steward_triage",
+ "summary": "Steward triage completed on Antigravity Carbon.",
+ "next_action": "Continue monitoring.",
+ "delivery_batch_scale": "single_surface",
+ "delivery_outcome": "outcome_progress",
+ "vision_unchanged_reason": "Current steward route holds.",
+ "work_evidence": ["Observed Goal state on Antigravity fallback"],
+ "validation_evidence": ["Scoped readback verified"],
+ "steward_user_actions": [{
+ "dedupe_key": "ag-fallback-digest",
+ "priority": "P1",
+ "text": "Review batched Carbon digest.",
+ }],
+ }, ensure_ascii=False)
+ return {"message": body}
+
+ monkeypatch.setattr(AntigravityAdapter, "start", classmethod(fake_start))
+ monkeypatch.setattr(AntigravityAdapter, "start_turn", fake_turn)
+
+ before_codex = capture.read_bytes()
+ assert transport.managed_steward.health(binding["binding_id"])["execution_available"] is True
+
+ # 1. Native report window runs and publishes via AntigravityAdapter + _steward_report_reader.
+ report_turn = pump_steward_monitors(runtime)[0]
+ report_done = finish(runtime, report_turn)
+ assert report_done["status"] == "completed", report_done
+ assert report_done["response"]["native_loopx"].get("status") == "committed", report_done
+ assert transport.steward_reports.reconcile() == 1
+ assert reports.health(binding["binding_id"])["publication_verified"] is True
+
+ # 2. Native steward advancement runs via AntigravityAdapter without CodexAppServerAdapter error.
+ adv_turn, _ = StewardTurnHost(runtime).submit(
+ binding["binding_id"],
+ turn_instance_id="advancement-on-carbon",
+ todo_id=added["todo_id"],
+ )
+ adv_done = finish(runtime, adv_turn)
+ assert adv_done["status"] == "completed", adv_done
+ assert adv_done["response"]["native_loopx"].get("status") == "committed", adv_done
+ assert adv_done["response"].get("steward_user_action_receipts")
+ assert guards == [("AntigravityAdapter", True), ("AntigravityAdapter", False)]
+ assert capture.read_bytes() == before_codex
diff --git a/tests/test_private_chat_backlog_projection.py b/tests/test_private_chat_backlog_projection.py
index 0049f86284..050102f250 100644
--- a/tests/test_private_chat_backlog_projection.py
+++ b/tests/test_private_chat_backlog_projection.py
@@ -98,7 +98,7 @@ def test_unavailable_or_misaligned_batch_result_is_never_empty_work(tmp_path, mo
core.quota_backlog_observer()(rows)
-@pytest.mark.parametrize("code", ["runtime_error", "server_restarted"])
+@pytest.mark.parametrize("code", ["runtime_error", "execution_worker_exited", "execution_worker_exited_before_dispatch", "server_restarted"])
def test_worker_or_preparation_failure_notice_does_not_answer_the_original_question(tmp_path, code):
store, _, transport, rows = fixture_history(tmp_path, count=1)
store.update_turn(rows[0]["session_id"], rows[0]["turn_id"], error_code=code)
diff --git a/tests/test_self_update_runtime_activation.py b/tests/test_self_update_runtime_activation.py
index b1647c2e97..177860b8c6 100644
--- a/tests/test_self_update_runtime_activation.py
+++ b/tests/test_self_update_runtime_activation.py
@@ -1060,8 +1060,9 @@ def run(command, **kwargs):
"target_read", ["compatible", "rejected", "other_revision", "legacy_fallback", "launcher_changed"],
)
@pytest.mark.parametrize("select_source_registry", [False, True])
+@pytest.mark.parametrize("archive_layout", ["default", "configured"])
def test_rollback_checks_registered_todo_read_before_switching(
- tmp_path, monkeypatch, capsys, target_read, select_source_registry,
+ tmp_path, monkeypatch, capsys, target_read, select_source_registry, archive_layout,
):
from loopx.cli import main
@@ -1082,21 +1083,32 @@ def test_rollback_checks_registered_todo_read_before_switching(
],
}))
source_runtime = project / "state"
- releases = tmp_path / "releases"
+ user_home = tmp_path / "user"
+ monkeypatch.setattr(Path, "home", lambda: user_home)
+ default_launcher = user_home / ".local/bin/loopx"
+ if archive_layout == "configured":
+ releases = tmp_path / "configured releases"
+ launcher = tmp_path / "configured bin/loopx"
+ monkeypatch.setenv("LOOPX_RELEASES_DIR", str(releases))
+ monkeypatch.setenv("LOOPX_BIN_DIR", str(launcher.parent))
+ default_launcher.parent.mkdir(parents=True)
+ default_launcher.write_text("Unrelated launcher")
+ else:
+ releases = user_home / ".local/share/loopx/releases"
+ launcher = default_launcher
+ monkeypatch.setenv("LOOPX_RELEASES_DIR", "")
+ monkeypatch.setenv("LOOPX_BIN_DIR", "")
current, target, concurrent = (
releases / label / "scripts/loopx" for label in ("current", "target", "concurrent")
)
for command in (current, target, concurrent):
command.parent.mkdir(parents=True)
command.touch()
- launcher = tmp_path / "bin/loopx"
- launcher.parent.mkdir()
+ launcher.parent.mkdir(parents=True)
launcher.symlink_to(current)
original_inode = launcher.lstat().st_ino
snapshot = doctor_payload()
snapshot["package"]["release_root"] = str(current.parent.parent)
- monkeypatch.setattr("loopx.self_update._user_loopx_bin", lambda *_: launcher)
- monkeypatch.setattr("loopx.self_update._user_releases_dir", lambda *_: releases)
monkeypatch.setattr("loopx.self_update.collect_doctor", lambda **_: snapshot)
authority = "loopx.control_plane.coordination.local_authority"
promoted = []
@@ -1147,6 +1159,8 @@ def run(argv, **kwargs):
"--format", "json", "update", "--rollback", "target",
])
result = json.loads(capsys.readouterr().out)
+ if archive_layout == "configured":
+ assert default_launcher.read_text() == "Unrelated launcher"
assert (common_runtime, "legacy-only") in promoted
assert current_reads == [(source_runtime, goal_id)]
if target_read == "compatible":
diff --git a/tests/test_steward_interrupted_answer.py b/tests/test_steward_interrupted_answer.py
index 3a4283c4c2..50ab5d9492 100644
--- a/tests/test_steward_interrupted_answer.py
+++ b/tests/test_steward_interrupted_answer.py
@@ -463,6 +463,17 @@ def test_timeout_presentation_does_not_trust_unknown_diagnostic_text(info):
assert "NATIVE_PRIVATE_SENTINEL" not in text and "期间原生助手记录了调用错误" not in text
+def test_worker_exit_reply_distinguishes_lost_execution_from_unknown_errors():
+ from loopx.extensions.lark.manager_context import manager_failure_reply
+
+ code, text = manager_failure_reply({"error_code": "execution_worker_exited", "error": "NATIVE_PRIVATE_SENTINEL"})
+ assert code == "execution_worker_exited"
+ assert "执行任务" in text and "原请求和会话已保留" in text and "不会自动重跑" in text
+ unknown, generic = manager_failure_reply({"error_code": "runtime_error", "error": "execution worker exited"})
+ assert unknown == "processing_failed" and "执行任务" not in generic
+ assert "NATIVE_PRIVATE_SENTINEL" not in text
+
+
@pytest.mark.parametrize("source_code", ["response_timeout", "antigravity_step_error", "unknown"])
def test_native_error_labels_preserve_shared_admission_failure_category(source_code):
from loopx.extensions.lark.manager_context import manager_failure_reply
@@ -1210,6 +1221,10 @@ def test_original_worker_answers_interrupted_steward_question_once(tmp_path, lan
monkeypatch.setenv("LOOPX_MANAGER_MODEL", "gemini-4-argon-high")
monkeypatch.setenv("LOOPX_MANAGER_REASONING_EFFORT", "high")
+ codex_home = tmp_path / "codex-home"
+ codex_home.mkdir(parents=True, exist_ok=True)
+ monkeypatch.setenv("LOOPX_CHAT_CODEX_HOME", str(codex_home))
+ monkeypatch.setenv("CODEX_HOME", str(codex_home))
fixture = steward.__wrapped__(tmp_path)
store, runtime, provider, transport, binding, capture, workspace = next(fixture)
try:
diff --git a/tests/test_steward_report_recovery_and_validator.py b/tests/test_steward_report_recovery_and_validator.py
index d602e38bb9..ae879b9e0a 100644
--- a/tests/test_steward_report_recovery_and_validator.py
+++ b/tests/test_steward_report_recovery_and_validator.py
@@ -6,7 +6,11 @@
import pytest
from loopx.capabilities.native_chat.steward_monitor import pump_steward_monitors
-from loopx.capabilities.native_chat.steward_turn import StewardTurnHost, _same_report_validator_command
+from loopx.capabilities.native_chat.steward_turn import (
+ StewardTurnHost,
+ _same_report_validator_command,
+ native_steward_client_id,
+)
from loopx.capabilities.periodic_report import cadence_runtime
from loopx.capabilities.periodic_report.cadence_journal import read_cadence_journal
from loopx.capabilities.periodic_report.incremental import read_periodic_report_publication_cursor
@@ -171,3 +175,202 @@ def mid_turn_failure(adapter, _prompt, event_sink):
assert transport.steward_reports.reconcile() == 1
cursor = read_periodic_report_publication_cursor(runtime_root=reports.root, goal_id=actor["goal_id"], agent_id=actor["agent_id"])
assert cursor["covered_until"] == old["window"]["next_due_at"]
+
+
+def test_selection_recovery_admission_resume_tolerates_validator_python_upgrade(
+ steward, tmp_path, monkeypatch, caplog,
+):
+ from loopx.capabilities.native_chat.steward_monitor import StewardMonitor
+ from loopx.configure_goal import configure_goal
+ from test_native_steward_reports import _REPORT_MODEL_SOURCE
+
+ store, runtime, _, transport, binding, _, _ = steward
+ point = datetime.now(timezone.utc).replace(hour=9, minute=1, second=0, microsecond=0) - timedelta(days=2)
+
+ class CalendarClock(datetime):
+ @classmethod
+ def now(cls, tz=None):
+ return point.astimezone(tz) if tz else point.replace(tzinfo=None)
+
+ monkeypatch.setattr(report_host, "datetime", CalendarClock)
+ monkeypatch.setattr(cadence_runtime, "datetime", CalendarClock)
+ sid, actor, reports, _ = configured(steward, tmp_path, monkeypatch, "sqlite")
+ owner = binding["binding_id"]
+ assert configure_goal(
+ registry_path=runtime.registry_path, goal_id=actor["goal_id"],
+ execution_replan_after_turns=1, execute=True,
+ )["ok"]
+ install_model(steward, tmp_path, sid)
+ assert finish(runtime, pump_steward_monitors(runtime)[0])["response"]["native_loopx"]["status"] == "committed"
+ assert transport.steward_reports.reconcile() == 1
+
+ point += timedelta(days=1)
+ runtime._steward_monitor_next_check.clear()
+ rejected = finish(runtime, pump_steward_monitors(runtime)[0])
+ assert rejected["response"]["native_loopx"]["error_code"] == "turn_todo_selection_deferred"
+
+ bad_vision = {
+ "schema_version": "goal_vision_replan_contract_v0",
+ "state": "active",
+ "vision_patch": {"vision_summary": "Keep reporting.", "acceptance_summary": "Validate full report."},
+ "path_delta": {
+ "outcome": "replan",
+ "prior_assumption": "Old assumption",
+ "observed_reality": "Reality",
+ "retained": ["Same report route"],
+ "changed": ["Updated baseline"],
+ "evidence_refs": ["fixture:evidence"],
+ },
+ }
+ bad_outcome = {
+ "result_kind": "replan_required",
+ "classification": "outcome_gap_without_blocked_fields",
+ "delivery_batch_scale": "implementation",
+ "delivery_outcome": "outcome_gap",
+ "work_evidence": ["Observed gap"],
+ "validation_evidence": ["Validated check"],
+ "path_delta_mode": "material_replan",
+ "agent_vision_json": json.dumps(bad_vision),
+ "summary": "Missing blocked fields trigger host_result_contract validation failure.",
+ "next_action": "Clear replan before resuming report.",
+ }
+ helper = tmp_path / "report_model.py"
+ helper.write_text(
+ _REPORT_MODEL_SOURCE.replace("def fake_report_model(", "def report_only(")
+ + "\ndef fake_report_model(params):\n"
+ + " import json\n"
+ + " prompt = '\\n'.join(row.get('text', '') for row in params.get('input', []) if isinstance(row, dict))\n"
+ + " request = json.JSONDecoder().raw_decode(prompt.split('Turn request:', 1)[1].lstrip())[0]\n"
+ + " if request.get('detail_ref', {}).get('replan_action_packet'):\n"
+ + " return " + repr(json.dumps(bad_outcome)) + "\n"
+ + " return report_only(params)\n"
+ )
+ runtime._steward_monitor_next_check.clear()
+ probe = finish(runtime, pump_steward_monitors(runtime)[0])
+
+ # Simulate host Python interpreter path upgrade on both the original refused turn and probe turn.
+ for row_id in (rejected["turn_id"], probe["turn_id"]):
+ row = store.load_turn(sid, row_id)
+ assert row is not None
+ req = json.loads(json.dumps(row["loopx_request"]))
+ req["validation_command"][0] = "/tmp/previous-release/bin/python"
+ row["loopx_request"] = req
+ store._turn_path(sid, row_id).write_text(json.dumps(row))
+
+ assert configure_goal(
+ registry_path=runtime.registry_path, goal_id=actor["goal_id"],
+ execution_replan_after_turns=6, execute=True,
+ )["ok"]
+ runtime._steward_monitor_next_check.clear()
+ with caplog.at_level(logging.WARNING):
+ resumed = pump_steward_monitors(runtime)
+ assert "An original steward report is unavailable" not in caplog.text
+ assert len(resumed) == 1
+ expected_client_id = native_steward_client_id(
+ actor,
+ rejected["loopx_request"]["turn_instance_id"] + ".admission-resume",
+ )
+ assert resumed[0]["client_turn_id"] == expected_client_id
+ done = finish(runtime, resumed[0])
+ assert done["response"]["native_loopx"]["status"] == "committed", done
+ runtime._steward_monitor_next_check.clear()
+ assert not pump_steward_monitors(runtime)
+ assert transport.steward_reports.reconcile() == 1
+ assert reports.health(owner)["publication_verified"]
+ task = next(row for row in StewardMonitor(runtime).todos(actor) if row["todo_id"] == done["loopx_request"]["todo_id"])
+ assert task["status"] == "done"
+
+
+def test_steward_report_normalizes_profile_minimum_scale_shorthand(steward, tmp_path, monkeypatch):
+ _, runtime, _, transport, binding, _, _ = steward
+ sid, _, reports, _ = configured(steward, tmp_path, monkeypatch, "sqlite")
+ install_model(steward, tmp_path, sid)
+ helper = tmp_path / "report_model.py"
+ helper.write_text(
+ helper.read_text(encoding="utf-8").replace(
+ '"delivery_batch_scale": "single_surface"',
+ '"delivery_batch_scale": "multi_surface_or_implementation"',
+ ),
+ encoding="utf-8",
+ )
+
+ done = finish(runtime, pump_steward_monitors(runtime)[0])
+ assert done["response"]["native_loopx"]["status"] == "committed", done
+ assert transport.steward_reports.reconcile() == 1
+ assert reports.health(binding["binding_id"])["publication_verified"]
+
+
+def test_frozen_report_recovers_host_result_contract_failure_without_model_call(
+ steward, tmp_path, monkeypatch, caplog,
+):
+ from loopx.capabilities.native_chat.steward_monitor import StewardMonitor
+ import loopx.capabilities.native_chat.steward_turn as steward_turn_mod
+ from loopx.configure_goal import configure_goal
+
+ _, runtime, _, transport, binding, _, _ = steward
+ point = datetime.now(timezone.utc).replace(hour=9, minute=1, second=0, microsecond=0) - timedelta(days=2)
+
+ class CalendarClock(datetime):
+ @classmethod
+ def now(cls, tz=None):
+ return point.astimezone(tz) if tz else point.replace(tzinfo=None)
+
+ monkeypatch.setattr(report_host, "datetime", CalendarClock)
+ monkeypatch.setattr(cadence_runtime, "datetime", CalendarClock)
+ sid, actor, reports, _ = configured(steward, tmp_path, monkeypatch, "sqlite")
+ owner = binding["binding_id"]
+ assert configure_goal(
+ registry_path=runtime.registry_path, goal_id=actor["goal_id"],
+ execution_replan_after_turns=1, execute=True,
+ )["ok"]
+ install_model(steward, tmp_path, sid)
+ assert finish(runtime, pump_steward_monitors(runtime)[0])["response"]["native_loopx"]["status"] == "committed"
+ assert transport.steward_reports.reconcile() == 1
+
+ point += timedelta(days=1)
+ runtime._steward_monitor_next_check.clear()
+ rejected = finish(runtime, pump_steward_monitors(runtime)[0])
+ assert rejected["response"]["native_loopx"]["error_code"] == "turn_todo_selection_deferred"
+
+ helper = tmp_path / "report_model.py"
+ helper.write_text(
+ helper.read_text(encoding="utf-8").replace(
+ '"delivery_batch_scale": "single_surface"',
+ '"delivery_batch_scale": "multi_surface_or_implementation"',
+ ),
+ encoding="utf-8",
+ )
+ assert configure_goal(
+ registry_path=runtime.registry_path, goal_id=actor["goal_id"],
+ execution_replan_after_turns=6, execute=True,
+ )["ok"]
+ runtime._steward_monitor_next_check.clear()
+ with monkeypatch.context() as unnormalized:
+ unnormalized.setattr(
+ steward_turn_mod,
+ "_normalize_report_kernel_candidate",
+ lambda candidate: {k: v for k, v in candidate.items() if k != "editorial_response"} | {
+ "result_kind": "validated_completion",
+ },
+ )
+ resumed = finish(runtime, pump_steward_monitors(runtime)[0])
+ native = resumed["response"]["native_loopx"]
+ assert native["status"] == "failed"
+ assert native["result_kind"] == "validation_failed"
+ assert native["validation_stage"] == "host_result_contract"
+ window = read_cadence_journal(runtime_root=reports.root, goal_id=actor["goal_id"])["window"]
+ assert (attempt_directory(reports.root, window) / "private-generation.json").is_file()
+
+ model_calls = steward[5].read_bytes()
+ runtime._steward_monitor_next_check.clear()
+ with caplog.at_level(logging.WARNING):
+ assert not pump_steward_monitors(runtime)
+ assert "An original steward report is unavailable" not in caplog.text
+ assert steward[5].read_bytes() == model_calls
+ assert transport.steward_reports.reconcile() == 1
+ assert reports.health(owner)["publication_verified"]
+ task = next(
+ row for row in StewardMonitor(runtime).todos(actor)
+ if row["todo_id"] == resumed["loopx_request"]["todo_id"]
+ )
+ assert task["status"] == "done"
diff --git a/tests/test_task_lease_worktree.py b/tests/test_task_lease_worktree.py
index 1c081219c5..4a9dfec8d9 100644
--- a/tests/test_task_lease_worktree.py
+++ b/tests/test_task_lease_worktree.py
@@ -1,5 +1,6 @@
"""Real sibling Git worktrees and the public lease CLI on both canonical providers."""
import json
+import shlex
import subprocess
import sys
from pathlib import Path
@@ -121,9 +122,13 @@ def acquire(key, path, expected=0, scope="src/**"):
@pytest.mark.skipif(sys.platform not in {"darwin", "linux"}, reason="worktree fixture requires symlinks")
@pytest.mark.parametrize("provider", ["file", "sqlite"])
-def test_ordinary_claim_uses_its_independent_worktree(tmp_path, monkeypatch, capsys, provider):
+@pytest.mark.parametrize("large_peer_scope", [False, True], ids=["small-scope", "large-scope"])
+def test_ordinary_claim_uses_its_independent_worktree(tmp_path, monkeypatch, capsys, provider, large_peer_scope):
"""Normal claims admit sibling edits without weakening physical collisions."""
+ import os
+
isolate_sqlite_runtime(tmp_path, monkeypatch)
+ monkeypatch.setenv("PATH", f"{Path(sys.executable).parent}{os.pathsep}{os.environ.get('PATH', '')}")
project = tmp_path / "repo"
project.mkdir()
@@ -145,11 +150,14 @@ def git(*args):
"id": goal, "repo": str(project), "state_file": str(state),
"coordination": {"registered_agents": ["agent-a", "agent-b"]},
}]}))
+ peer_scopes = ["src/**"]
+ if large_peer_scope:
+ peer_scopes.extend(f"tests/fixtures/peer-scope/module_{i:03}.py" for i in range(92))
projection = build_todo_runtime_shadow_projection(goal_id=goal, handoff_mode="hard_lease", leases=[], todos=[{
"schema_version": "todo_item_v0", "todo_id": f"todo_ordinary_{key}", "role": "agent", "status": "open", "done": False,
"text": "Edit isolated source", "archive_state": "active", "source_section": "Agent Todo", "index": i,
"task_class": "advancement_task", "task_domain": "code", "task_repository": "git:github.com/example/project",
- "required_write_scopes": ["src/**"],
+ "required_write_scopes": peer_scopes if key == "a" else ["src/**"],
} for i, key in enumerate(("a", "b"), 1)])
initialize_canonical_authority(runtime, goal, projection, state_path=state, provider=provider)
@@ -160,9 +168,9 @@ def cli(command, key, *args, expected=0):
assert rc == expected, output
return json.loads(output)
- def claim(key, operation, expected=0):
+ def claim(key, operation, *options, expected=0):
return cli(["todo", "claim"], key, "--agent-id", f"agent-{key}", "--claimed-by", f"agent-{key}",
- "--claim-operation-id", operation, expected=expected)
+ "--claim-operation-id", operation, *options, expected=expected)
monkeypatch.chdir(a)
assert claim("a", "claim-a")["execution_managed"]
@@ -179,12 +187,41 @@ def claim(key, operation, expected=0):
git("remote", "set-url", "origin", "https://github.com/example/project.git")
(b / "nested").mkdir()
monkeypatch.chdir(b / "nested")
- accepted = claim("b", "claim-b")
- assert accepted["execution_managed"]
- assert accepted["integration_overlap_advisories"] == [{
- "todo_id": "todo_ordinary_a", "owner": "agent-a", "write_scopes": ["src/**"],
+ overlap = {
+ "todo_id": "todo_ordinary_a", "owner": "agent-a", "write_scopes": peer_scopes,
"reason": "independent_worktree_integration_overlap",
- }]
+ }
+ if large_peer_scope:
+ preview = claim("b", "preview-b", "--thin", "--dry-run")
+ assert preview["integration_overlap_advisories"] == [overlap]
+ assert "integration_overlap_detail_command" not in preview
+ accepted = claim("b", "claim-b", "--thin")
+ assert accepted["execution_managed"]
+ if large_peer_scope:
+ assert accepted["integration_overlap_advisories"] == [{
+ "todo_id": "todo_ordinary_a", "owner": "agent-a", "write_scope_count": len(peer_scopes),
+ "reason": "independent_worktree_integration_overlap",
+ }]
+ command = shlex.split(accepted["integration_overlap_detail_command"])
+ assert command[-7:] == ["todo", "receipt", "--goal-id", goal, "--operation-id", "claim-b", "--thin"]
+ observed = subprocess.run(command, capture_output=True, text=True, timeout=30, check=True)
+ detail = json.loads(observed.stdout)
+ assert detail["ok"] and detail["status"] == "found"
+ assert detail["thin"] is True
+ assert detail["provider_revision"] == accepted["provider_revision"]
+ original = subprocess.run(command[:-1], capture_output=True, text=True, timeout=30, check=True)
+ full_receipt = json.loads(original.stdout)["receipts"][0]
+ detail_receipt = detail["receipts"][0]
+ assert detail_receipt == {**full_receipt, "result": {
+ "claim_operation_id": "claim-b",
+ "todo_id": "todo_ordinary_b",
+ "integration_overlap_advisories": [overlap],
+ }}
+ assert full_receipt["result"]["lease"]["owner"] == "agent-b"
+ assert len(observed.stdout.encode("utf-8")) < len(original.stdout.encode("utf-8"))
+ else:
+ assert accepted["integration_overlap_advisories"] == [overlap]
+ assert "integration_overlap_detail_command" not in accepted
second = cli(["task-lease", "inspect"], "b")["lease"]
assert "write_workspace" in first
assert first["write_workspace"] != second["write_workspace"]
@@ -192,9 +229,35 @@ def claim(key, operation, expected=0):
assert cli(["todo", "list"], "b")["todo"]["claimed_by"] == "agent-b"
replay = claim("b", "claim-b")
assert replay["status"] == "replayed"
+ assert replay["integration_overlap_advisories"] == [overlap]
assert cli(["task-lease", "inspect"], "b")["lease"] == second
- cli(["task-lease", "release"], "b", "--owner", "agent-b", "--idempotency-key", second["idempotency_key"],
- "--expected-version", str(second["version"]))
+ released = cli(["task-lease", "release"], "b", "--owner", "agent-b",
+ "--idempotency-key", second["idempotency_key"], "--expected-version", str(second["version"]))
+ assert released["released"]
+ if large_peer_scope:
+ observed = subprocess.run(command, capture_output=True, text=True, timeout=30, check=True)
+ late = json.loads(observed.stdout)
+ assert released["provider_revision"] != detail["provider_revision"]
+ assert late == detail
+ original = subprocess.run(command[:-1], capture_output=True, text=True, timeout=30, check=True)
+ assert json.loads(original.stdout)["receipts"][0] == full_receipt
+ missing = subprocess.run(
+ [*command[:-2], "missing-claim", "--thin"], capture_output=True, text=True, timeout=30, check=True,
+ )
+ assert json.loads(missing.stdout)["status"] == "missing"
+ subprocess.run([
+ *command[:-7], "todo", "add", "--goal-id", goal, "--role", "agent",
+ "--text", "A different operation", "--operation-id", "add-task",
+ ], capture_output=True, text=True, timeout=30, check=True)
+ original_add = subprocess.run(
+ [*command[:-2], "add-task"], capture_output=True, text=True, timeout=30, check=True,
+ )
+ assert json.loads(original_add.stdout)["status"] == "found"
+ rejected = subprocess.run(
+ [*command[:-2], "add-task", "--thin"], capture_output=True, text=True, timeout=30,
+ )
+ assert rejected.returncode == 1
+ assert "requires an exact managed claim receipt" in json.loads(rejected.stdout)["error"]
assert claim("b", "claim-b", expected=1)["ok"] is False
assert not cli(["task-lease", "inspect"], "b")["active"]
diff --git a/tests/test_turn_machine_credential.py b/tests/test_turn_machine_credential.py
index 31a0dc4887..80a4b16785 100644
--- a/tests/test_turn_machine_credential.py
+++ b/tests/test_turn_machine_credential.py
@@ -64,13 +64,6 @@ def sdk_runner(**kwargs):
monkeypatch.setattr(turn_host_adapter, "run_dsh_turn", sdk_runner)
for name in ("first", "second"):
project, runtime, registry = _write_live_fixture(tmp_path / name)
- expected.append({
- "DEEPSEEK_API_KEY": KEY,
- "LOOPX_TURN_GOAL_ID": "loopx-turn-fixture",
- "LOOPX_TURN_AGENT_ID": "codex-fixture",
- "LOOPX_TURN_TODO_ID": "todo_fixture0001",
- "LOOPX_TURN_WORKSPACE": str(project.resolve()),
- })
# A conflicting Goal-local store cannot redirect machine authentication.
provider.write_operator_provider(runtime_root=runtime, api_key=GOAL_KEY)
code, plan = turn(project, runtime, registry)
@@ -83,6 +76,14 @@ def sdk_runner(**kwargs):
assert code == 0, result
assert result["result_kind"] == "iteration_failed"
assert result["effects"]["quota_spent"] is False
+ expected.append({
+ "DEEPSEEK_API_KEY": KEY,
+ "LOOPX_TURN_KEY": result["receipt"]["turn_key"],
+ "LOOPX_TURN_GOAL_ID": "loopx-turn-fixture",
+ "LOOPX_TURN_AGENT_ID": "codex-fixture",
+ "LOOPX_TURN_TODO_ID": "todo_fixture0001",
+ "LOOPX_TURN_WORKSPACE": str(project.resolve()),
+ })
assert received == expected
diff --git a/tests/test_zcode_goal_bridge.py b/tests/test_zcode_goal_bridge.py
index 631915e2ed..fe32b0a7d3 100644
--- a/tests/test_zcode_goal_bridge.py
+++ b/tests/test_zcode_goal_bridge.py
@@ -8,6 +8,7 @@
import pytest
+from loopx.control_plane.goals.activation import build_goal_activation
from loopx.zcode_goal_mode import bridge
from loopx.zcode_goal_mode.api import ZCodeGoalRequestMixin, public_zcode_goal_readback
@@ -55,7 +56,11 @@ def test_invalid_authority_cannot_launch_provider(authority, monkeypatch, mutati
elif mutation == "invalid_instance":
goal["goal_instance_id"] = 123
elif mutation == "stopped":
- goal["activation_state"] = "stopped"
+ goal["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
elif mutation == "archived":
goal["status"] = "archived"
else:
@@ -490,7 +495,11 @@ def test_real_provider_status_is_read_only_with_exact_identity(authority):
@pytest.mark.parametrize("action", ["status", "pause", "stop"])
def test_same_exact_inactive_binding_can_be_read_or_cleaned(authority, monkeypatch, action):
registry, _, payload = authority
- payload["goals"][0]["activation_state"] = "stopped"
+ payload["goals"][0]["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
registry.write_text(json.dumps(payload), encoding="utf-8")
requests = _stub_operation(monkeypatch)
result = bridge.zcode_goal_operation(action=action, registry_path=registry, goal_id="delivery", agent_id="zcode-worker")
@@ -503,7 +512,11 @@ def test_same_exact_inactive_binding_can_be_read_or_cleaned(authority, monkeypat
@pytest.mark.parametrize("action", ["bind", "start", "resume"])
def test_inactive_binding_cannot_admit_execution(authority, monkeypatch, action):
registry, _, payload = authority
- payload["goals"][0]["activation_state"] = "stopped"
+ payload["goals"][0]["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
registry.write_text(json.dumps(payload), encoding="utf-8")
monkeypatch.setattr(bridge, "_node_command", lambda: pytest.fail("inactive Goal reached provider"))
with pytest.raises(bridge.ZCodeGoalBridgeError, match="no longer active"):
@@ -512,7 +525,11 @@ def test_inactive_binding_cannot_admit_execution(authority, monkeypatch, action)
def test_inactive_cleanup_still_rejects_changed_instance_or_unregistered_agent(authority, monkeypatch):
registry, _, payload = authority
- payload["goals"][0]["activation_state"] = "stopped"
+ payload["goals"][0]["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
registry.write_text(json.dumps(payload), encoding="utf-8")
def replace(request):
payload["goals"][0]["goal_instance_id"] = INSTANCE_B
@@ -598,7 +615,11 @@ def test_model_selection_forwards_existing_model_only(authority, monkeypatch):
def test_internal_guard_requires_known_action_and_validates_inactive_cleanup(authority):
registry, project, payload = authority
- payload["goals"][0]["activation_state"] = "stopped"
+ payload["goals"][0]["activation"] = build_goal_activation(
+ state="stopped",
+ updated_at="2026-01-01T00:00:00Z",
+ reason="fixture stop",
+ )
registry.write_text(json.dumps(payload), encoding="utf-8")
request = {"registry": str(registry), "project": str(project), "goal_id": "delivery", "agent_id": "zcode-worker", "goal_ref": {"goal_id": "delivery", "goal_instance_id": INSTANCE_A}}
command = [sys.executable, "-X", "utf8", "-B", "-m", "loopx.zcode_goal_mode.bridge", "--validate-binding"]