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: 2 additions & 2 deletions examples/shared-goal-authority-e2e/mutants.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def remove_native_update_maintenance(source: str) -> 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, () =>",
Expand Down
71 changes: 66 additions & 5 deletions loopx/cli_commands/quota_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions loopx/cli_commands/quota_native_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
37 changes: 35 additions & 2 deletions loopx/control_plane/quota/unsettled_host_turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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],
Expand All @@ -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):
Expand Down
13 changes: 9 additions & 4 deletions loopx/control_plane/turn_driver/codex_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions loopx/control_plane/work_items/interaction_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
)


Expand Down Expand Up @@ -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 "<GOAL_ID>")
command_prefix = selection.render_cli_command_prefix(runtime_root=runtime_root, registry_path=registry_path)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
Loading
Loading