Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion loopx/capabilities/native_chat/steward_managed_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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),
Expand Down
129 changes: 124 additions & 5 deletions loopx/capabilities/native_chat/steward_turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,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
Expand Down Expand Up @@ -118,14 +130,17 @@ 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():
raise ValueError("the original Chat host is stopped")
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)
Expand Down Expand Up @@ -175,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),
Expand Down Expand Up @@ -533,6 +549,81 @@ 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 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.

Expand All @@ -559,7 +650,7 @@ 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))
generation = 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"],
Expand All @@ -568,9 +659,37 @@ def recover_report_settlement(self, actor: dict[str, Any], window: dict[str, Any
return
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")
if contract_recovery is not None:
from ...control_plane.turn_driver.host_candidate import build_result

normalized_candidate = contract_recovery[1]
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, normalized_candidate, host_name="Codex app-server", allow_completion=True)

host_runner = replay_frozen_candidate
else:
host_runner = 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"])
Expand All @@ -580,7 +699,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),
Expand Down Expand Up @@ -1135,7 +1254,7 @@ 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"
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 {}))
Expand Down
43 changes: 37 additions & 6 deletions loopx/cli_commands/turn_run_once.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -289,7 +313,7 @@ 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=None if completion_todo_id is not None else task_lease_proof,
)
# A Todo-less native governance Turn has no business artifact/argv.
# Its independent validator is the canonical semantic writeback preview,
Expand All @@ -312,6 +336,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 (
Expand Down Expand Up @@ -451,6 +477,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.
Expand Down Expand Up @@ -742,10 +770,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")
Expand All @@ -765,9 +790,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,
Expand Down
Loading
Loading