diff --git a/loopx/capabilities/configuration_ui.py b/loopx/capabilities/configuration_ui.py index 77ac8dac60..f40f8bbdd2 100644 --- a/loopx/capabilities/configuration_ui.py +++ b/loopx/capabilities/configuration_ui.py @@ -125,6 +125,14 @@ def capability_configuration_editor( _field("max_trials", "Maximum proposed trials", "number", minimum=0, maximum=2, required=True), ], }, + "goal_closeout": { + "supported_scopes": ["machine"], "writable_scopes": ["machine"], + "fields": [ + _field("timeout_seconds", "Closeout timeout (seconds)", "number", + minimum=1, maximum=600, required=True, + description="Bounds one terminal hook invocation. The command itself is local-private: configure it with the machine-config CLI, never through the Dashboard."), + ], + }, "goal_storage": { "supported_scopes": ["machine"], "writable_scopes": ["machine"], "fields": [_field("new_goal_provider", "New Goal storage provider", "select", diff --git a/loopx/capabilities/goal_closeout/__init__.py b/loopx/capabilities/goal_closeout/__init__.py new file mode 100644 index 0000000000..002d1dfa3f --- /dev/null +++ b/loopx/capabilities/goal_closeout/__init__.py @@ -0,0 +1 @@ +"""Terminal Goal closeout hooks owned by the machine configuration.""" diff --git a/loopx/capabilities/goal_closeout/machine_defaults.py b/loopx/capabilities/goal_closeout/machine_defaults.py new file mode 100644 index 0000000000..ae0a9eecbb --- /dev/null +++ b/loopx/capabilities/goal_closeout/machine_defaults.py @@ -0,0 +1,146 @@ +from __future__ import annotations + +from collections.abc import Mapping +from typing import Any + +from ..machine_configuration.contract import MachineConfigurationNamespace + +GOAL_CLOSEOUT_CONFIGURATION_SCHEMA = "loopx_goal_closeout_configuration_v0" +DEFAULT_CLOSEOUT_TIMEOUT_SECONDS = 30 +MAX_CLOSEOUT_TIMEOUT_SECONDS = 600 + + +def normalize_goal_closeout_configuration( + raw: Mapping[str, Any], +) -> dict[str, Any]: + unknown = sorted(set(raw) - {"schema_version", "command", "timeout_seconds"}) + if unknown: + raise ValueError( + "goal_closeout contains unsupported fields: " + ", ".join(unknown) + ) + if raw.get("schema_version") != GOAL_CLOSEOUT_CONFIGURATION_SCHEMA: + raise ValueError( + "goal_closeout must use " + GOAL_CLOSEOUT_CONFIGURATION_SCHEMA + ) + raw_command = raw.get("command", []) + if not isinstance(raw_command, (list, tuple)): + raise ValueError("goal_closeout command must be an array of strings") + command: list[str] = [] + for part in raw_command: + if not isinstance(part, str) or not part.strip(): + raise ValueError( + "goal_closeout command entries must be non-empty strings" + ) + command.append(part) + raw_timeout = raw.get("timeout_seconds", DEFAULT_CLOSEOUT_TIMEOUT_SECONDS) + if isinstance(raw_timeout, bool) or not isinstance(raw_timeout, int): + raise ValueError("goal_closeout timeout_seconds must be an integer") + if not 1 <= raw_timeout <= MAX_CLOSEOUT_TIMEOUT_SECONDS: + raise ValueError( + "goal_closeout timeout_seconds must be between 1 and " + f"{MAX_CLOSEOUT_TIMEOUT_SECONDS}" + ) + return { + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "command": command, + "timeout_seconds": raw_timeout, + } + + +def _public_goal_closeout_configuration( + value: Mapping[str, Any], +) -> dict[str, Any]: + """Publish whether a command exists, never the local program or its path.""" + + normalized = normalize_goal_closeout_configuration(value) + return { + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "configured": bool(normalized["command"]), + "timeout_seconds": normalized["timeout_seconds"], + } + + +def _apply_goal_closeout_public_update( + current: Mapping[str, Any] | None, + update: Mapping[str, Any], +) -> dict[str, Any]: + """Let the Dashboard change only the public timeout, not the command.""" + + base = ( + normalize_goal_closeout_configuration(current) + if current is not None + else { + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "command": [], + "timeout_seconds": DEFAULT_CLOSEOUT_TIMEOUT_SECONDS, + } + ) + unknown = sorted(set(update) - {"schema_version", "configured", "timeout_seconds"}) + if unknown: + raise ValueError( + "goal_closeout public update contains unsupported fields: " + + ", ".join(unknown) + ) + if "schema_version" in update and ( + update["schema_version"] != GOAL_CLOSEOUT_CONFIGURATION_SCHEMA + ): + raise ValueError( + "goal_closeout must use " + GOAL_CLOSEOUT_CONFIGURATION_SCHEMA + ) + if "configured" in update and bool(update["configured"]) != bool(base["command"]): + raise ValueError( + "goal_closeout configured is derived from the locally configured command" + ) + merged = dict(base) + if "timeout_seconds" in update: + merged["timeout_seconds"] = update["timeout_seconds"] + return normalize_goal_closeout_configuration(merged) + + +def _apply_goal_closeout_local_update( + current: Mapping[str, Any] | None, + update: Mapping[str, Any], +) -> dict[str, Any]: + """Let the local operator CLI write the command the Dashboard cannot.""" + + base = ( + normalize_goal_closeout_configuration(current) + if current is not None + else { + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "command": [], + "timeout_seconds": DEFAULT_CLOSEOUT_TIMEOUT_SECONDS, + } + ) + unknown = sorted(set(update) - {"schema_version", "command", "timeout_seconds"}) + if unknown: + raise ValueError( + "goal_closeout update contains unsupported fields: " + ", ".join(unknown) + ) + return normalize_goal_closeout_configuration({**base, **dict(update)}) + + +def goal_closeout_machine_configuration_namespace() -> MachineConfigurationNamespace: + return MachineConfigurationNamespace( + namespace="goal_closeout", + schema_versions=frozenset({GOAL_CLOSEOUT_CONFIGURATION_SCHEMA}), + normalize=normalize_goal_closeout_configuration, + project_public=_public_goal_closeout_configuration, + apply_public_update=_apply_goal_closeout_public_update, + local_only_fields=frozenset({"command"}), + apply_local_update=_apply_goal_closeout_local_update, + title="Terminal Goal closeout command", + description=( + "Optional machine-wide command run after a Goal reaches a terminal " + "transition (stop, retirement or runtime archive). The command is " + "best effort, receives one bounded JSON payload on stdin, and can " + "never block the LoopX command that triggered it. Keep the command " + "local-private: write it with the machine-config CLI; the Dashboard " + "only sees whether one is configured and may adjust the timeout." + ), + default_configuration={ + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "command": [], + "timeout_seconds": DEFAULT_CLOSEOUT_TIMEOUT_SECONDS, + }, + ) diff --git a/loopx/capabilities/machine_configuration/builtins.py b/loopx/capabilities/machine_configuration/builtins.py index 1805619573..f8bab4abc9 100644 --- a/loopx/capabilities/machine_configuration/builtins.py +++ b/loopx/capabilities/machine_configuration/builtins.py @@ -31,6 +31,11 @@ "loopx.capabilities.machine_configuration.goal_storage", "goal_storage_machine_configuration_namespace", ), + ( + "goal_closeout", + "loopx.capabilities.goal_closeout.machine_defaults", + "goal_closeout_machine_configuration_namespace", + ), ( "manager_runtime", "loopx.capabilities.manager_runtime.machine_profile", diff --git a/loopx/capabilities/machine_configuration/cli.py b/loopx/capabilities/machine_configuration/cli.py index fdcc0b7940..15f81d171b 100644 --- a/loopx/capabilities/machine_configuration/cli.py +++ b/loopx/capabilities/machine_configuration/cli.py @@ -276,6 +276,9 @@ def handle_machine_configuration_command( namespace=args.namespace, namespace_configuration=configuration, registry=registry, + # The local operator CLI is the trusted writer for fields + # the loopback Dashboard may only read. + allow_local_only=True, ) payload = configure_machine_configuration( runtime_root=runtime_root, @@ -290,6 +293,9 @@ def handle_machine_configuration_command( namespace=args.namespace, namespace_configuration=configuration, registry=registry, + # The local operator CLI is the trusted writer for fields + # the loopback Dashboard may only read. + allow_local_only=True, ) payload = configure_machine_configuration( runtime_root=runtime_root, diff --git a/loopx/capabilities/machine_configuration/contract.py b/loopx/capabilities/machine_configuration/contract.py index 7b6d8b6b85..77510220ae 100644 --- a/loopx/capabilities/machine_configuration/contract.py +++ b/loopx/capabilities/machine_configuration/contract.py @@ -33,6 +33,11 @@ class MachineConfigurationNamespace: description: str | None = None documentation: Mapping[str, str] | None = None default_configuration: Mapping[str, Any] | None = None + # Fields only the local operator CLI may write. The loopback Dashboard + # routes merge through this contract too, so without this split a public + # namespace update could persist an arbitrary executable path. + local_only_fields: frozenset[str] = frozenset() + apply_local_update: PublicUpdater | None = None def __post_init__(self) -> None: if not _NAMESPACE_RE.fullmatch(self.namespace): @@ -197,10 +202,18 @@ def merge_machine_configuration_namespace( namespace: str, namespace_configuration: Mapping[str, Any], registry: MachineConfigurationRegistry, + allow_local_only: bool = False, ) -> dict[str, Any]: """Build one whole-document update while preserving sibling namespaces.""" contract = registry.resolve(namespace) + if not allow_local_only: + blocked = sorted(set(namespace_configuration) & contract.local_only_fields) + if blocked: + raise ValueError( + "machine-configuration namespace only accepts these fields from " + f"the local CLI: {', '.join(blocked)}" + ) # Namespace updates are also the recovery path for an installed namespace # whose previously valid value no longer satisfies the current contract. # Keep the stored value opaque until the owning namespace has replaced it; @@ -228,8 +241,13 @@ def merge_machine_configuration_namespace( raise TypeError( f"machine_configuration.namespaces.{namespace} must be an object" ) + updater = ( + contract.apply_local_update + if allow_local_only and contract.apply_local_update is not None + else contract.apply_public_update + ) updated_namespace = _mapping( - contract.apply_public_update(current_namespace, namespace_configuration), + updater(current_namespace, namespace_configuration), f"machine_configuration.namespaces.{namespace}", ) return normalize_machine_configuration( diff --git a/loopx/control_plane/goals/activation_service.py b/loopx/control_plane/goals/activation_service.py index e4fec5e42e..cfb8afcdd8 100644 --- a/loopx/control_plane/goals/activation_service.py +++ b/loopx/control_plane/goals/activation_service.py @@ -18,6 +18,7 @@ from ...paths import ( GLOBAL_REGISTRY_FILENAME, global_registry_path, + goal_state_file_from_record, resolve_runtime_root, select_default_runtime_root, ) @@ -233,6 +234,73 @@ def _source_and_target( ) +def _project_committed_transition( + *, + source_registry: Path, + runtime_root_override: str | None, + goal_id: str, +) -> dict[str, Any]: + """Project a transition the source registry already committed. + + The source registry owns the lifecycle transition; the shared registry is + a repairable projection of it. A projection writer that raises is the same + fixable fault as a failure receipt, so report it here instead of letting it + escape and erase the committed transition receipt. + """ + + try: + return sync_project_registry_to_global( + registry_path=source_registry, + runtime_root_override=runtime_root_override, + goal_id=goal_id, + dry_run=False, + _global_registry_lock_held=True, + ) + except Exception as exc: # the committed source transition keeps its receipt + return { + "ok": False, + "dry_run": False, + "error_kind": "global_sync_failed", + "error": f"shared registry projection failed: {exc}", + "registry": str(source_registry), + "recommended_action": ( + f"rerun loopx sync-global --goal-id {goal_id} " + "after repairing the shared registry route" + ), + } + +def _readback_committed_transition( + *, + source_registry: Path, + target_registry: Path, + goal_id: str, + expected_state: GoalActivationState, + expected_owner_resume_id: str | None, +) -> dict[str, Any]: + """Read back a committed transition without discarding its receipt. + + A read fault after the source commit is reported as an unverified readback + so the caller still sees ``written`` and the terminal hook still runs. + """ + + try: + return _readback( + source_registry=source_registry, + target_registry=target_registry, + goal_id=goal_id, + expected_state=expected_state, + expected_owner_resume_id=expected_owner_resume_id, + ) + except Exception as exc: # the committed source transition keeps its receipt + return { + "schema_version": GOAL_ACTIVATION_READBACK_SCHEMA_VERSION, + "status": "readback_failed", + "verified": False, + "goal_id": goal_id, + "expected_state": expected_state.value, + "error": f"activation readback failed: {exc}", + } + def _readback( *, source_registry: Path, @@ -472,6 +540,7 @@ def set_goal_activation_state( ) return payload locked_goal = _goal(source_payload, normalized_goal_id) + closeout_goal = dict(locked_goal) locked_state = goal_activation_state(locked_goal) if locked_state is not before_state: payload.update( @@ -505,12 +574,10 @@ def set_goal_activation_state( payload["written"] = True if registries_are_distinct: - sync_payload = sync_project_registry_to_global( - registry_path=source_registry, + sync_payload = _project_committed_transition( + source_registry=source_registry, runtime_root_override=sync_runtime_root, goal_id=normalized_goal_id, - dry_run=False, - _global_registry_lock_held=True, ) payload["global_sync"] = sync_payload @@ -521,7 +588,7 @@ def set_goal_activation_state( "verified": False, } if sync_payload is not None and not sync_payload.get("ok") - else _readback( + else _readback_committed_transition( source_registry=source_registry, target_registry=target_registry, goal_id=normalized_goal_id, @@ -539,14 +606,93 @@ def set_goal_activation_state( and readback.get("verified") ) if not payload["ok"]: - payload["error"] = "goal activation shared registry readback did not verify" + payload["error"] = str( + readback.get("error") + or (sync_payload or {}).get("error") + or "goal activation shared registry readback did not verify" + ) payload["recommended_action"] = ( (sync_payload or {}).get("recommended_action") or f"rerun loopx sync-global --goal-id {normalized_goal_id} after repairing the shared registry route" ) + # The stop itself is committed in the source registry, which owns the + # transition. A failed shared projection is a separately reported, fixable + # fault, and a retry cannot re-enter this branch because the source is + # already stopped; the hook therefore runs once for the committed + # transition even when the projection has to be repaired afterwards. + if ( + execute + and target_state is GoalActivationState.STOPPED + and changed + and payload.get("written") + and closeout_goal is not None + ): + payload["closeout"] = _stop_closeout_payload( + source_registry=source_registry, + registry_snapshot=source_payload, + runtime_root_override=runtime_root_override, + goal_id=normalized_goal_id, + closeout_goal=closeout_goal, + ) return payload +def _stop_closeout_payload( + *, + source_registry: Path, + registry_snapshot: dict[str, Any], + runtime_root_override: str | None, + goal_id: str, + closeout_goal: dict[str, Any], +) -> dict[str, Any]: + """Run the terminal hook after a committed stop transition. + + The stop is already committed when this runs, so the whole boundary is + best-effort: argument preparation reads the confirmed registry snapshot + taken inside the transaction, and any preparation failure is reported in + the hook result instead of raising past the committed receipt. + """ + + from ...lifecycle.goal_closeout import run_goal_closeout + + try: + closeout_runtime_root = resolve_runtime_root( + registry_snapshot, + runtime_root_override, + registry_path=source_registry, + ) + declared = goal_state_file_from_record( + closeout_goal, + registry_path=source_registry, + ) + if declared is None: + declared = ( + source_registry.parent + / "goals" + / goal_id + / "ACTIVE_GOAL_STATE.md" + ) + state_file = declared + return run_goal_closeout( + runtime_root=closeout_runtime_root, + goal_id=goal_id, + repo=str(closeout_goal.get("repo")) if closeout_goal.get("repo") else None, + state_file=str(state_file), + terminal_reason="stopped", + trigger="goal-lifecycle-stop", + ) + except Exception as exc: # the committed stop must keep its own receipt + return { + "schema_version": "loopx_goal_closeout_invocation_v0", + "goal_id": goal_id, + "trigger": "goal-lifecycle-stop", + "terminal_reason": "stopped", + "status": "preparation_error", + "error": f"{type(exc).__name__}: {exc}", + "ledger_recorded": False, + } + + def render_goal_activation_markdown(payload: dict[str, Any]) -> str: lines = [ "# Goal Activation", diff --git a/loopx/extensions/process_runtime.py b/loopx/extensions/process_runtime.py index b649aa48e1..43c11cd9b1 100644 --- a/loopx/extensions/process_runtime.py +++ b/loopx/extensions/process_runtime.py @@ -89,6 +89,7 @@ class CappedProcessResult: returncode: int stdout: bytes failure_kind: str | None = None + stderr: bytes = b"" def _wait_for_process(process: subprocess.Popen[bytes] | subprocess.Popen[str], timeout: float) -> bool: @@ -713,4 +714,5 @@ def write_stdin() -> None: returncode=returncode, stdout=bytes(stdout), failure_kind="timeout" if timed_out else limit_kind, + stderr=bytes(stderr), ) diff --git a/loopx/global_registry.py b/loopx/global_registry.py index 9d0e676b06..01f26b2803 100644 --- a/loopx/global_registry.py +++ b/loopx/global_registry.py @@ -18,7 +18,12 @@ from .control_plane.projects.registry_codec import load_registry from .control_plane.runtime.time import now_local_iso from .file_lock import exclusive_cross_runtime_file_lock -from .paths import global_registry_path, resolve_runtime_root, select_default_runtime_root +from .paths import ( + global_registry_path, + resolve_runtime_root, + select_default_runtime_root, + validate_goal_id_path_segment, +) from .registry import read_json, registry_goals from .registry_writability import is_write_denied_error, probe_registry_write_path @@ -488,7 +493,12 @@ def _global_goal_path( value = goal.get(field) if not value: return None - path = Path(str(value)).expanduser() + try: + path = Path(str(value)).expanduser() + except (OSError, RuntimeError): + # An alias that no longer expands cannot name a live path; the record + # keeps the raw value so a later reader sees what was declared. + return None if path.is_absolute(): return path repo = goal.get("repo") @@ -497,6 +507,80 @@ def _global_goal_path( return global_path.parent / path +def _retire_closeout_payload( + *, + runtime_root: Path, + retired_goal: dict[str, Any], + global_path: Path, +) -> dict[str, Any]: + """Run the retire hook after the committed removal, best effort only. + + The Goal is already gone from the registry, so nothing here may raise past + the retirement receipt. With no command configured the record's declared + path is never resolved, and one unusable record cannot stop the others. + """ + + from .lifecycle.goal_closeout import ( + GOAL_CLOSEOUT_LEDGER_SCHEMA_VERSION, + goal_closeout_configuration, + run_goal_closeout, + ) + + goal_id = str(retired_goal.get("id") or "").strip() + unconfigured = { + "schema_version": GOAL_CLOSEOUT_LEDGER_SCHEMA_VERSION, + "goal_id": goal_id, + "trigger": "retire-global-goal", + "terminal_reason": "retired", + "status": "not_configured", + "ledger_recorded": False, + } + try: + try: + validate_goal_id_path_segment(goal_id) + except ValueError: + # Let the shared executor report the invalid id as it always has. + return run_goal_closeout( + runtime_root=runtime_root, + goal_id=goal_id, + repo=None, + state_file=None, + terminal_reason="retired", + trigger="retire-global-goal", + ) + configuration, configuration_error = goal_closeout_configuration(runtime_root) + if configuration_error is None and not (configuration or {}).get("command"): + return unconfigured + state_file = _global_goal_path( + retired_goal, + "state_file", + global_path=global_path, + ) + if state_file is None: + state_file = runtime_root / "goals" / goal_id / "ACTIVE_GOAL_STATE.md" + goal_runtime_dir = runtime_root / "goals" / goal_id + repo = retired_goal.get("repo") + return run_goal_closeout( + runtime_root=runtime_root, + goal_id=goal_id, + repo=str(repo) if repo else None, + state_file=str(state_file), + terminal_reason="retired", + trigger="retire-global-goal", + ledger_dir=( + goal_runtime_dir + if goal_runtime_dir.is_dir() + else runtime_root / "closeout" / goal_id + ), + ) + except Exception as exc: # the terminal command already finished + return { + **unconfigured, + "status": "preparation_error", + "error": f"{type(exc).__name__}: {exc}", + } + + def _retire_global_registry_reduction( current: dict[str, Any], *, @@ -571,6 +655,9 @@ def _retire_global_registry_reduction( "inspections": inspections, "global_goal_count_before": len(current_goals), "global_goal_count_after": len(retained_goals), + "retired_goals": [ + dict(matches_by_id[goal_id][0]) for goal_id in requested_ids + ], }, backup_label="retire-goal-backup", ) @@ -656,6 +743,21 @@ def retire_global_registry_goals( receipt = mutation["receipt"] backup_path = mutation["backup_path"] + closeouts: list[dict[str, Any]] = [] + if execute: + for retired_goal in receipt.get("retired_goals", []): + if not str(retired_goal.get("id") or "").strip(): + continue + # The removal is committed. Each record prepares its own inputs, so + # one unusable path stays a per-record diagnostic. + closeouts.append( + _retire_closeout_payload( + runtime_root=runtime_root, + retired_goal=retired_goal, + global_path=global_path, + ) + ) + return { "ok": True, "schema_version": "loopx_global_goal_retirement_v0", @@ -674,6 +776,7 @@ def retire_global_registry_goals( "wrote": execute, "updated_at": updated_at, "global_registry_writability": writability, + "closeouts": closeouts, } diff --git a/loopx/lifecycle/__init__.py b/loopx/lifecycle/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/loopx/lifecycle/goal_closeout.py b/loopx/lifecycle/goal_closeout.py new file mode 100644 index 0000000000..6453983f46 --- /dev/null +++ b/loopx/lifecycle/goal_closeout.py @@ -0,0 +1,234 @@ +from __future__ import annotations + +import json +import time +from pathlib import Path +from typing import Any + +from ..capabilities.goal_closeout.machine_defaults import ( + normalize_goal_closeout_configuration, +) +from ..capabilities.machine_configuration.builtins import ( + build_builtin_machine_configuration_registry, +) +from ..capabilities.machine_configuration.store import read_machine_configuration +from ..control_plane.runtime.time import now_local_iso +from ..extensions.process_runtime import run_capped_process +from ..file_lock import LockAcquisitionPolicy, exclusive_file_lock +from ..paths import validate_goal_id_path_segment + +GOAL_CLOSEOUT_PAYLOAD_SCHEMA_VERSION = "loopx_goal_closeout_v1" +GOAL_CLOSEOUT_LEDGER_SCHEMA_VERSION = "loopx_goal_closeout_invocation_v0" +GOAL_CLOSEOUT_LEDGER_FILENAME = "closeout-ledger.jsonl" +_MAX_CAPTURE_BYTES = 8192 + + +def goal_closeout_ledger_path(runtime_root: Path, goal_id: str) -> Path: + safe_goal_id = validate_goal_id_path_segment(goal_id) + return runtime_root / "goals" / safe_goal_id / GOAL_CLOSEOUT_LEDGER_FILENAME + + +def _read_configuration(runtime_root: Path) -> tuple[dict[str, Any] | None, str | None]: + try: + configuration = read_machine_configuration( + runtime_root, + registry=build_builtin_machine_configuration_registry(), + namespace_ids=("goal_closeout",), + ) + except Exception as exc: # semantically fail-open for the terminal command + return None, f"{type(exc).__name__}: {exc}" + if not configuration: + return None, None + raw = configuration.get("namespaces", {}).get("goal_closeout") + if raw is None: + return None, None + try: + return normalize_goal_closeout_configuration(raw), None + except Exception as exc: + return None, f"{type(exc).__name__}: {exc}" + + +def goal_closeout_configuration( + runtime_root: Path, +) -> tuple[dict[str, Any] | None, str | None]: + """Report the configured terminal hook without running it. + + Callers use this to skip hook-only argument preparation when no command is + configured, so an operator's stale path can never fail a committed + terminal transition. + """ + + return _read_configuration(runtime_root) + + +def _write_ledger( + ledger_dir: Path, + record: dict[str, Any], +) -> tuple[Path, bool, str | None]: + path = ledger_dir / GOAL_CLOSEOUT_LEDGER_FILENAME + try: + path.parent.mkdir(parents=True, exist_ok=True) + with exclusive_file_lock( + path, + policy=LockAcquisitionPolicy.MUTATION, + operation="goal_closeout_ledger_append", + ): + with path.open("a", encoding="utf-8") as handle: + handle.write(json.dumps(record, ensure_ascii=False) + "\n") + return path, True, None + except Exception as exc: # the terminal command already ran; never block it + return path, False, f"{type(exc).__name__}: {exc}" + + +def _decode_capture(value: bytes) -> str: + return value[:_MAX_CAPTURE_BYTES].decode("utf-8", errors="replace") + + +def run_goal_closeout( + *, + runtime_root: Path, + goal_id: str, + repo: str | None, + state_file: str | None, + terminal_reason: str, + trigger: str, + ledger_dir: Path | None = None, +) -> dict[str, Any]: + """Run the configured closeout command once and record the attempt. + + The caller's terminal transition has already committed. Every failure in + this function is returned, never raised, so the command that finished the + Goal keeps its own result. + """ + + result: dict[str, Any] = { + "schema_version": GOAL_CLOSEOUT_LEDGER_SCHEMA_VERSION, + "goal_id": goal_id, + "trigger": trigger, + "terminal_reason": terminal_reason, + "status": "not_configured", + "ledger_recorded": False, + } + try: + safe_goal_id = validate_goal_id_path_segment(goal_id) + except ValueError as exc: + result.update({"status": "invalid_goal_id", "error": str(exc)}) + return result + result["goal_id"] = safe_goal_id + selected_ledger_dir = ( + ledger_dir + if ledger_dir is not None + else goal_closeout_ledger_path(runtime_root, safe_goal_id).parent + ) + try: + configuration, configuration_error = _read_configuration(runtime_root) + if configuration_error is not None: + result.update( + { + "status": "configuration_error", + "error": configuration_error, + } + ) + return result + if configuration is None: + return result + command = list(configuration.get("command") or []) + result["command"] = command + if not command: + return result + + payload = { + "schema_version": GOAL_CLOSEOUT_PAYLOAD_SCHEMA_VERSION, + "goal_id": safe_goal_id, + "repo": repo or "", + "state_file": state_file or "", + "terminal_reason": terminal_reason, + "runtime_root": str(runtime_root), + "trigger": trigger, + } + started = time.monotonic() + exit_code: int | None = None + timed_out = False + failure_kind: str | None = None + stdout_text = "" + stderr_text = "" + launch_error: str | None = None + try: + completed = run_capped_process( + command, + stdin=json.dumps(payload, ensure_ascii=False).encode("utf-8"), + timeout_seconds=float(configuration["timeout_seconds"]), + output_limit_bytes=_MAX_CAPTURE_BYTES, + # Never hold a directory handle inside a worktree that the + # archive transition is deleting. + cwd=None, + ) + except OSError as exc: + launch_error = f"{type(exc).__name__}: {exc}" + except Exception as exc: + launch_error = f"{type(exc).__name__}: {exc}" + else: + exit_code = completed.returncode + failure_kind = completed.failure_kind + timed_out = failure_kind == "timeout" + stdout_text = _decode_capture(completed.stdout) + stderr_text = _decode_capture(completed.stderr) + duration_ms = int((time.monotonic() - started) * 1000) + if launch_error is not None: + status = "launch_error" + elif timed_out: + status = "timeout" + elif failure_kind is not None: + status = "output_limit" + elif exit_code == 0: + status = "success" + else: + status = "failed" + result.update( + { + "status": status, + "exit_code": exit_code, + "timed_out": timed_out, + "failure_kind": failure_kind, + "duration_ms": duration_ms, + "stdout": stdout_text, + "stderr": stderr_text, + } + ) + if launch_error is not None: + result["error"] = launch_error + record = { + "schema_version": GOAL_CLOSEOUT_LEDGER_SCHEMA_VERSION, + "recorded_at": now_local_iso(), + "goal_id": safe_goal_id, + "trigger": trigger, + "terminal_reason": terminal_reason, + "command": command, + "status": status, + "exit_code": exit_code, + "timed_out": timed_out, + "failure_kind": failure_kind, + "duration_ms": duration_ms, + "stdout": stdout_text, + "stderr": stderr_text, + **({"error": launch_error} if launch_error is not None else {}), + } + ledger_path, ledger_recorded, ledger_error = _write_ledger( + selected_ledger_dir, record + ) + result.update( + { + "ledger_path": str(ledger_path), + "ledger_recorded": ledger_recorded, + } + ) + if ledger_error is not None: + result["ledger_error"] = ledger_error + except Exception as exc: + result.update( + { + "status": "internal_error", + "error": f"{type(exc).__name__}: {exc}", + } + ) + return result diff --git a/loopx/paths.py b/loopx/paths.py index 420aed1588..77462e46d0 100644 --- a/loopx/paths.py +++ b/loopx/paths.py @@ -3,6 +3,7 @@ import ast import os import stat +from collections.abc import Mapping from enum import StrEnum from pathlib import Path @@ -331,6 +332,35 @@ def registry_project_root(registry_path: Path) -> Path: return parent.parent if parent.name == ".loopx" else parent +def goal_state_file_from_record( + goal: Mapping[str, object], + *, + registry_path: Path, +) -> Path | None: + """Resolve the state file a Goal record declares, for hook payloads. + + Absolute values win. A project registry already owns its project, so a + relative value resolves there as it always has. A global registry lives in + the runtime root instead, which would point the hook at a file the project + never wrote; there the Goal's own repo decides, with the registry directory + as the last fallback, which is the rule the retired-goal route already uses. + """ + + value = goal.get("state_file") + if not value: + return None + path = Path(str(value)).expanduser() + if path.is_absolute(): + return path + expanded = registry_path.expanduser() + if expanded.parent.name == ".loopx": + return registry_project_root(expanded) / path + repo = goal.get("repo") + if repo: + return Path(str(repo)).expanduser() / path + return expanded.parent / path + + def resolve_runtime_root( registry: dict[str, object], override: str | None = None, diff --git a/loopx/runtime.py b/loopx/runtime.py index 1ad1cc68bc..2ca7dedc6f 100644 --- a/loopx/runtime.py +++ b/loopx/runtime.py @@ -10,6 +10,7 @@ from .history import load_registry from .paths import ( + goal_state_file_from_record, resolve_runtime_root, validate_goal_id_path_segment as validate_goal_id_path_segment, ) @@ -98,6 +99,72 @@ def _archive_source_goal_refs( return tuple(refs) +def _archive_closeout_payload( + *, + runtime_root: Path, + goal_id: str, + goal_record: dict[str, Any], + registry_path: Path, + archive_path: Path, +) -> dict[str, Any]: + """Run the archive hook after the committed move, best effort only. + + Nothing here may fail the archive: the directory already moved. When no + hook is configured the record's state file is never resolved at all, so a + stale operator path cannot turn a finished archive into an error. + """ + + from .lifecycle.goal_closeout import ( + GOAL_CLOSEOUT_LEDGER_SCHEMA_VERSION, + goal_closeout_configuration, + run_goal_closeout, + ) + + unconfigured = { + "schema_version": GOAL_CLOSEOUT_LEDGER_SCHEMA_VERSION, + "goal_id": goal_id, + "trigger": "archive-runtime", + "terminal_reason": "archived", + "status": "not_configured", + "ledger_recorded": False, + } + try: + configuration, configuration_error = goal_closeout_configuration(runtime_root) + if configuration_error is None and not (configuration or {}).get("command"): + return unconfigured + state_file: str | None = None + archived_state_file = archive_path / "ACTIVE_GOAL_STATE.md" + try: + if archived_state_file.exists(): + # The archived copy wins so the consumer reads the file where + # the committed move put it. + state_file = str(archived_state_file) + else: + declared = goal_state_file_from_record( + goal_record, + registry_path=registry_path, + ) + state_file = str(declared) if declared is not None else None + except Exception: # an unusable declared path is not worth a failure + state_file = None + repo = goal_record.get("repo") + return run_goal_closeout( + runtime_root=runtime_root, + goal_id=goal_id, + repo=str(repo) if repo else None, + state_file=state_file, + terminal_reason="archived", + trigger="archive-runtime", + ledger_dir=archive_path, + ) + except Exception as exc: # the terminal command already finished + return { + **unconfigured, + "status": "preparation_error", + "error": f"{type(exc).__name__}: {exc}", + } + + def archive_runtime_goal( *, registry_path: Path, @@ -138,6 +205,8 @@ def archive_runtime_goal( destination = unique_archive_path(archive_base, safe_goal_id, timestamp) _validate_archive_destination(source, destination) + closeout: dict[str, Any] | None = None + closeout_facts: dict[str, Any] | None = None if execute: from .control_plane.coordination.shadow_management import ( runtime_artifact_lock_target, @@ -237,6 +306,29 @@ def archive_runtime_goal( _validate_archive_destination(source, destination) destination.parent.mkdir(parents=True, exist_ok=True) shutil.move(str(source), str(destination)) + # The move is irreversible. Only plain copies of already confirmed + # facts happen before the canonical locks release; every fallible + # hook-only step waits for the best-effort boundary below. + locked_archive_goal = next( + ( + goal + for goal in registry_goals(locked_registry) + if str(goal.get("id")) == safe_goal_id + ), + None, + ) + closeout_facts = { + "goal_record": dict(locked_archive_goal) if locked_archive_goal else {}, + "archive_path": destination, + } + if closeout_facts is not None: + closeout = _archive_closeout_payload( + runtime_root=runtime_root, + goal_id=safe_goal_id, + goal_record=closeout_facts["goal_record"], + registry_path=registry_path, + archive_path=closeout_facts["archive_path"], + ) return { "ok": True, @@ -250,6 +342,7 @@ def archive_runtime_goal( "dry_run": not execute, "archived": execute, "action": "moved" if execute else "would-move", + "closeout": closeout, "checks": [ "goal id is a single path segment", "source runtime goal directory exists", diff --git a/tests/capabilities/test_goal_closeout_hook.py b/tests/capabilities/test_goal_closeout_hook.py new file mode 100644 index 0000000000..f8eb955dd2 --- /dev/null +++ b/tests/capabilities/test_goal_closeout_hook.py @@ -0,0 +1,248 @@ +from __future__ import annotations + +import json +import sys +from pathlib import Path + +import pytest + +from loopx.capabilities.goal_closeout.machine_defaults import ( + GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, +) +from loopx.capabilities.machine_configuration.builtins import ( + build_builtin_machine_configuration_registry, +) +from loopx.capabilities.machine_configuration.contract import ( + merge_machine_configuration_namespace, + project_machine_configuration, +) +from loopx.capabilities.machine_configuration.store import ( + configure_machine_configuration, +) +from loopx.lifecycle.goal_closeout import ( + GOAL_CLOSEOUT_LEDGER_FILENAME, + run_goal_closeout, +) + + +def _configure(root: Path, *, command: list[str], timeout_seconds: int = 5) -> None: + configuration = { + "schema_version": "loopx_machine_configuration_v0", + "namespaces": { + "goal_closeout": { + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "command": command, + "timeout_seconds": timeout_seconds, + } + }, + } + registry = build_builtin_machine_configuration_registry() + preview = configure_machine_configuration( + runtime_root=root, + configuration=configuration, + registry=registry, + ) + payload = configure_machine_configuration( + runtime_root=root, + configuration=configuration, + registry=registry, + execute=True, + expected_plan_revision=preview["plan_revision"], + ) + assert payload["ok"], payload + + +def _ledger_rows(root: Path, goal_id: str) -> list[dict[str, object]]: + path = root / "goals" / goal_id / GOAL_CLOSEOUT_LEDGER_FILENAME + return [ + json.loads(line) + for line in path.read_text(encoding="utf-8").splitlines() + if line.strip() + ] + + +def test_unconfigured_closeout_runs_nothing_and_writes_no_ledger(tmp_path: Path) -> None: + result = run_goal_closeout( + runtime_root=tmp_path, + goal_id="fixture", + repo=None, + state_file=None, + terminal_reason="retired", + trigger="retire", + ) + assert result["status"] == "not_configured" + assert result["ledger_recorded"] is False + assert not (tmp_path / "goals" / "fixture" / GOAL_CLOSEOUT_LEDGER_FILENAME).exists() + + +def test_closeout_receives_bounded_payload_and_records_ledger(tmp_path: Path) -> None: + payload_path = tmp_path / "payload.json" + command = [sys.executable, "-c", "import sys; open(sys.argv[1], 'w').write(sys.stdin.read())", str(payload_path)] + _configure(tmp_path, command=command) + + result = run_goal_closeout( + runtime_root=tmp_path, + goal_id="fixture", + repo="/tmp/fixture-repo", + state_file="/tmp/fixture-state.md", + terminal_reason="retired", + trigger="retire", + ) + + assert result["status"] == "success", result + payload = json.loads(payload_path.read_text(encoding="utf-8")) + assert payload == { + "schema_version": "loopx_goal_closeout_v1", + "goal_id": "fixture", + "repo": "/tmp/fixture-repo", + "state_file": "/tmp/fixture-state.md", + "terminal_reason": "retired", + "runtime_root": str(tmp_path), + "trigger": "retire", + } + assert result["ledger_recorded"] is True + rows = _ledger_rows(tmp_path, "fixture") + assert len(rows) == 1 + assert rows[0]["status"] == "success" + assert rows[0]["exit_code"] == 0 + + +def test_closeout_timeout_is_bounded_and_never_raises(tmp_path: Path) -> None: + _configure( + tmp_path, + command=[sys.executable, "-c", "import time; time.sleep(30)"], + timeout_seconds=1, + ) + result = run_goal_closeout( + runtime_root=tmp_path, + goal_id="fixture", + repo=None, + state_file=None, + terminal_reason="stopped", + trigger="stop", + ) + assert result["status"] == "timeout" + assert result["timed_out"] is True + assert result["ledger_recorded"] is True + assert 900 <= result["duration_ms"] < 10000 + + +def test_closeout_command_failure_is_reported_without_blocking(tmp_path: Path) -> None: + _configure( + tmp_path, + command=[sys.executable, "-c", "import sys; print('bad', file=sys.stderr); sys.exit(7)"], + ) + result = run_goal_closeout( + runtime_root=tmp_path, + goal_id="fixture", + repo=None, + state_file=None, + terminal_reason="archived", + trigger="archive", + ) + assert result["status"] == "failed" + assert result["exit_code"] == 7 + assert "bad" in result["stderr"] + assert result["ledger_recorded"] is True + + +def test_invalid_goal_id_is_rejected_before_any_command_or_ledger( + tmp_path: Path, +) -> None: + marker = tmp_path / "ran.txt" + _configure( + tmp_path, + command=[sys.executable, "-c", "open(sys.argv[1], 'w').write('x')", str(marker)], + ) + + result = run_goal_closeout( + runtime_root=tmp_path, + goal_id="../escape", + repo=None, + state_file=None, + terminal_reason="retired", + trigger="retire", + ) + + assert result["status"] == "invalid_goal_id" + assert result["ledger_recorded"] is False + assert not marker.exists() + assert not (tmp_path / "goals").exists() + + +def test_missing_executable_is_recorded_as_a_launch_error(tmp_path: Path) -> None: + _configure(tmp_path, command=[str(tmp_path / "missing-closeout-binary")]) + + result = run_goal_closeout( + runtime_root=tmp_path, + goal_id="fixture", + repo=None, + state_file=None, + terminal_reason="retired", + trigger="retire", + ) + + assert result["status"] == "launch_error" + assert result["ledger_recorded"] is True + assert "FileNotFoundError" in result["error"] + rows = _ledger_rows(tmp_path, "fixture") + assert len(rows) == 1 + assert rows[0]["status"] == "launch_error" + assert "FileNotFoundError" in str(rows[0]["error"]) + + +def test_public_projection_hides_the_command_and_public_update_rejects_it( + tmp_path: Path, +) -> None: + command = [sys.executable, "-c", "raise SystemExit(0)"] + _configure(tmp_path, command=command) + registry = build_builtin_machine_configuration_registry() + stored = json.loads( + (tmp_path / "machine" / "configuration.json").read_text(encoding="utf-8") + ) + + projected = project_machine_configuration(stored, registry=registry) + public = projected["namespaces"]["goal_closeout"] + assert public == { + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "configured": True, + "timeout_seconds": 5, + } + assert str(tmp_path) not in json.dumps(projected) + assert sys.executable not in json.dumps(projected) + + with pytest.raises(ValueError, match="local CLI: command"): + merge_machine_configuration_namespace( + stored, + namespace="goal_closeout", + namespace_configuration={ + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "command": ["/bin/sh", "-c", "id > /tmp/pwned"], + }, + registry=registry, + ) + + merged = merge_machine_configuration_namespace( + stored, + namespace="goal_closeout", + namespace_configuration={ + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "timeout_seconds": 9, + }, + registry=registry, + ) + assert merged["namespaces"]["goal_closeout"]["command"] == command + assert merged["namespaces"]["goal_closeout"]["timeout_seconds"] == 9 + + local = merge_machine_configuration_namespace( + stored, + namespace="goal_closeout", + namespace_configuration={ + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "command": command, + "timeout_seconds": 9, + }, + registry=registry, + allow_local_only=True, + ) + assert local["namespaces"]["goal_closeout"]["command"] == command diff --git a/tests/capabilities/test_goal_closeout_terminal_hooks.py b/tests/capabilities/test_goal_closeout_terminal_hooks.py new file mode 100644 index 0000000000..69d984e44d --- /dev/null +++ b/tests/capabilities/test_goal_closeout_terminal_hooks.py @@ -0,0 +1,929 @@ +from __future__ import annotations + +import errno +import json +import sys +import pytest +from pathlib import Path + +from loopx.capabilities.goal_closeout.machine_defaults import ( + GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, +) +from loopx.capabilities.machine_configuration.builtins import ( + build_builtin_machine_configuration_registry, +) +from loopx.capabilities.machine_configuration.store import ( + configure_machine_configuration, +) +from loopx.control_plane.goals.activation_service import set_goal_activation_state +from loopx.global_registry import retire_global_registry_goals, sync_project_registry_to_global +from loopx.lifecycle.goal_closeout import GOAL_CLOSEOUT_LEDGER_FILENAME +from loopx.runtime import archive_runtime_goal + + +def _write_json(path: Path, payload: dict[str, object]) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8") + + +def _configure_closeout(root: Path, payload_path: Path) -> None: + configuration = { + "schema_version": "loopx_machine_configuration_v0", + "namespaces": { + "goal_closeout": { + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "command": [ + sys.executable, + "-c", + "import sys; open(sys.argv[1], 'a').write(sys.stdin.read())", + str(payload_path), + ], + "timeout_seconds": 5, + } + }, + } + registry = build_builtin_machine_configuration_registry() + preview = configure_machine_configuration( + runtime_root=root, + configuration=configuration, + registry=registry, + ) + payload = configure_machine_configuration( + runtime_root=root, + configuration=configuration, + registry=registry, + execute=True, + expected_plan_revision=preview["plan_revision"], + ) + assert payload["ok"], payload + +_LOCK_PROBE = r""" +import pathlib +import sys + +repo_root, runtime_root, goal_id, registry_path, output_path = sys.argv[1:6] +sys.path.insert(0, repo_root) + +from loopx.control_plane.coordination.shadow_management import ( + runtime_artifact_lock_target, +) +from loopx.file_lock import exclusive_cross_runtime_file_lock + + +def probe(target): + try: + with exclusive_cross_runtime_file_lock(target, timeout_seconds=0): + return "FREE" + except Exception: + return "HELD" + + +lines = [ + probe(runtime_artifact_lock_target(pathlib.Path(runtime_root), goal_id)), + probe(pathlib.Path(registry_path)), +] +pathlib.Path(output_path).write_text("\n".join(lines), encoding="utf-8") +""" + + +def _configure_lock_probe_closeout( + root: Path, + *, + output_path: Path, + goal_id: str, + registry_path: Path, +) -> None: + configuration = { + "schema_version": "loopx_machine_configuration_v0", + "namespaces": { + "goal_closeout": { + "schema_version": GOAL_CLOSEOUT_CONFIGURATION_SCHEMA, + "command": [ + sys.executable, + "-c", + _LOCK_PROBE, + str(Path(__file__).resolve().parents[2]), + str(root), + goal_id, + str(registry_path), + str(output_path), + ], + "timeout_seconds": 10, + } + }, + } + registry = build_builtin_machine_configuration_registry() + preview = configure_machine_configuration( + runtime_root=root, + configuration=configuration, + registry=registry, + ) + payload = configure_machine_configuration( + runtime_root=root, + configuration=configuration, + registry=registry, + execute=True, + expected_plan_revision=preview["plan_revision"], + ) + assert payload["ok"], payload + + +def _ledger(root: Path, goal_id: str) -> list[dict[str, object]]: + path = root / "goals" / goal_id / GOAL_CLOSEOUT_LEDGER_FILENAME + return [ + json.loads(line) + for line in path.read_text(encoding="utf-8").splitlines() + if line.strip() + ] + + +def test_stop_runs_closeout_once_and_dry_run_does_not(tmp_path: Path) -> None: + runtime_root = tmp_path / "runtime" + project = tmp_path / "project" + source_registry = project / ".loopx" / "registry.json" + _write_json( + source_registry, + { + "schema_version": "0.1", + "common_runtime_root": str(runtime_root), + "goals": [ + { + "id": "goal-one", + "display_name": "Closeout fixture", + "repo": str(project), + "quota": {"compute": 1, "allowed_slots": 4, "spent_slots": 0}, + } + ], + }, + ) + synced = sync_project_registry_to_global( + registry_path=source_registry, + runtime_root_override=str(runtime_root), + goal_id="goal-one", + dry_run=False, + ) + assert synced["ok"] is True + global_registry = runtime_root / "registry.global.json" + payload_path = tmp_path / "payloads.jsonl" + _configure_closeout(runtime_root, payload_path) + + preview = set_goal_activation_state( + registry_path=global_registry, + goal_id="goal-one", + state="stopped", + actor_kind="owner", + execute=False, + ) + assert "closeout" not in preview + + stopped = set_goal_activation_state( + registry_path=global_registry, + goal_id="goal-one", + state="stopped", + actor_kind="owner", + execute=True, + ) + assert stopped["ok"] is True, stopped + assert stopped["closeout"]["status"] == "success", stopped + payload = json.loads(payload_path.read_text(encoding="utf-8").splitlines()[0]) + assert payload["goal_id"] == "goal-one" + assert payload["terminal_reason"] == "stopped" + assert payload["repo"] == str(project) + rows = _ledger(runtime_root, "goal-one") + assert len(rows) == 1 + + repeated = set_goal_activation_state( + registry_path=global_registry, + goal_id="goal-one", + state="stopped", + actor_kind="owner", + execute=True, + ) + assert "closeout" not in repeated + assert len(_ledger(runtime_root, "goal-one")) == 1 + + +def test_retire_runs_closeout_for_each_retired_goal(tmp_path: Path) -> None: + runtime_root = tmp_path / "runtime" + global_registry = runtime_root / "registry.global.json" + project = tmp_path / "project" + _write_json( + global_registry, + { + "schema_version": "0.1", + "registry_role": "global-local", + "goals": [ + { + "id": "retired-goal", + "display_name": "Retired fixture", + "repo": str(project), + "source_registry": str(tmp_path / "missing" / "registry.json"), + "state_file": str(tmp_path / "missing" / "ACTIVE_GOAL_STATE.md"), + } + ], + }, + ) + payload_path = tmp_path / "payloads.jsonl" + _configure_closeout(runtime_root, payload_path) + + result = retire_global_registry_goals( + runtime_root_override=str(runtime_root), + goal_ids=["retired-goal"], + execute=True, + ) + assert result["ok"] is True, result + assert len(result["closeouts"]) == 1 + assert result["closeouts"][0]["status"] == "success", result + payload = json.loads(payload_path.read_text(encoding="utf-8").splitlines()[0]) + assert payload["goal_id"] == "retired-goal" + assert payload["terminal_reason"] == "retired" + assert payload["repo"] == str(project) + ledger_path = Path(result["closeouts"][0]["ledger_path"]) + assert ledger_path.is_file() + assert json.loads(ledger_path.read_text(encoding="utf-8").splitlines()[0])["status"] == "success" + + +def test_archive_runs_closeout_after_move_without_recreating_source(tmp_path: Path) -> None: + project = tmp_path / "project" + registry_path = project / ".loopx" / "registry.json" + runtime_root = project / ".loopx" / "runtime" + source = runtime_root / "goals" / "archived-goal" + source.mkdir(parents=True) + (source / "ACTIVE_GOAL_STATE.md").write_text("state\n", encoding="utf-8") + (source / "expected.txt").write_text("kept\n", encoding="utf-8") + _write_json( + registry_path, + { + "schema_version": 1, + "common_runtime_root": ".loopx/runtime", + "goals": [ + { + "id": "archived-goal", + "display_name": "Archive fixture", + "repo": str(project), + "state_file": ".loopx/runtime/goals/archived-goal/ACTIVE_GOAL_STATE.md", + } + ], + }, + ) + payload_path = tmp_path / "payloads.jsonl" + _configure_closeout(runtime_root, payload_path) + + result = archive_runtime_goal( + registry_path=registry_path, + runtime_root_override=None, + goal_id="archived-goal", + archive_root=None, + allow_registered=True, + execute=True, + ) + assert result["ok"] is True, result + assert result["closeout"]["status"] == "success", result + archive_path = Path(result["archive_path"]) + assert not source.exists() + assert (archive_path / "expected.txt").read_text(encoding="utf-8") == "kept\n" + assert (archive_path / GOAL_CLOSEOUT_LEDGER_FILENAME).is_file() + payload = json.loads(payload_path.read_text(encoding="utf-8").splitlines()[0]) + assert payload["goal_id"] == "archived-goal" + assert payload["terminal_reason"] == "archived" + assert payload["state_file"] == str(archive_path / "ACTIVE_GOAL_STATE.md") + + +def test_archive_runs_closeout_after_canonical_locks_are_released(tmp_path: Path) -> None: + project = tmp_path / "project" + registry_path = project / ".loopx" / "registry.json" + runtime_root = project / ".loopx" / "runtime" + source = runtime_root / "goals" / "locked-goal" + source.mkdir(parents=True) + (source / "ACTIVE_GOAL_STATE.md").write_text("state\n", encoding="utf-8") + _write_json( + registry_path, + { + "schema_version": 1, + "common_runtime_root": ".loopx/runtime", + "goals": [ + { + "id": "locked-goal", + "display_name": "Archive lock fixture", + "repo": str(project), + "state_file": ".loopx/runtime/goals/locked-goal/ACTIVE_GOAL_STATE.md", + } + ], + }, + ) + probe_path = tmp_path / "lock-probe.txt" + _configure_lock_probe_closeout( + runtime_root, + output_path=probe_path, + goal_id="locked-goal", + registry_path=registry_path, + ) + + result = archive_runtime_goal( + registry_path=registry_path, + runtime_root_override=None, + goal_id="locked-goal", + archive_root=None, + allow_registered=True, + execute=True, + ) + assert result["ok"] is True, result + assert result["closeout"]["status"] == "success", result + assert probe_path.read_text(encoding="utf-8").splitlines() == ["FREE", "FREE"] + + +def test_stop_resolves_relative_state_file_to_project_root(tmp_path: Path) -> None: + runtime_root = tmp_path / "runtime" + project = tmp_path / "project" + source_registry = project / ".loopx" / "registry.json" + _write_json( + source_registry, + { + "schema_version": "0.1", + "common_runtime_root": str(runtime_root), + "goals": [ + { + "id": "relative-stop", + "display_name": "Relative stop fixture", + "repo": str(project), + "state_file": "goals/relative-stop/ACTIVE_GOAL_STATE.md", + "quota": {"compute": 1, "allowed_slots": 4, "spent_slots": 0}, + } + ], + }, + ) + synced = sync_project_registry_to_global( + registry_path=source_registry, + runtime_root_override=str(runtime_root), + goal_id="relative-stop", + dry_run=False, + ) + assert synced["ok"] is True + global_registry = runtime_root / "registry.global.json" + global_payload = json.loads(global_registry.read_text(encoding="utf-8")) + global_payload["goals"][0]["state_file"] = ( + "goals/relative-stop/ACTIVE_GOAL_STATE.md" + ) + _write_json(global_registry, global_payload) + payload_path = tmp_path / "payloads.jsonl" + _configure_closeout(runtime_root, payload_path) + + stopped = set_goal_activation_state( + registry_path=global_registry, + goal_id="relative-stop", + state="stopped", + actor_kind="owner", + execute=True, + ) + assert stopped["ok"] is True, stopped + assert stopped["closeout"]["status"] == "success", stopped + payload = json.loads(payload_path.read_text(encoding="utf-8").splitlines()[0]) + assert payload["state_file"] == str( + project / "goals" / "relative-stop" / "ACTIVE_GOAL_STATE.md" + ) + + +def test_stop_keeps_committed_receipt_when_registry_read_fails_after_commit( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + from loopx.control_plane.goals import activation_service + + runtime_root = tmp_path / "runtime" + project = tmp_path / "project" + source_registry = project / ".loopx" / "registry.json" + _write_json( + source_registry, + { + "schema_version": "0.1", + "common_runtime_root": str(runtime_root), + "goals": [ + { + "id": "masked-stop", + "display_name": "Masked stop fixture", + "repo": str(project), + "quota": {"compute": 1, "allowed_slots": 4, "spent_slots": 0}, + } + ], + }, + ) + synced = sync_project_registry_to_global( + registry_path=source_registry, + runtime_root_override=str(runtime_root), + goal_id="masked-stop", + dry_run=False, + ) + assert synced["ok"] is True + global_registry = runtime_root / "registry.global.json" + payload_path = tmp_path / "payloads.jsonl" + _configure_closeout(runtime_root, payload_path) + + original_load_registry = activation_service.load_registry + + def _flaky_load_registry(path: Path) -> dict[str, object]: + snapshot = original_load_registry(path) + for goal in snapshot.get("goals", []): + if isinstance(goal, dict) and goal.get("state") == "stopped": + raise RuntimeError("registry re-read failed after commit") + return snapshot + + monkeypatch.setattr(activation_service, "load_registry", _flaky_load_registry) + + stopped = set_goal_activation_state( + registry_path=global_registry, + goal_id="masked-stop", + state="stopped", + actor_kind="owner", + execute=True, + ) + assert stopped["ok"] is True, stopped + assert stopped["written"] is True, stopped + assert stopped["after_state"] == "stopped", stopped + # The closeout reads the transaction's confirmed snapshot. A registry + # load that only fails once the stop is committed must never run, so the + # committed receipt keeps its successful hook result. + assert stopped["closeout"]["status"] == "success", stopped + ledger_path = runtime_root / "goals" / "masked-stop" / GOAL_CLOSEOUT_LEDGER_FILENAME + assert ledger_path.is_file() + + +def test_retire_resolves_relative_state_file_to_registry_root(tmp_path: Path) -> None: + runtime_root = tmp_path / "runtime" + global_registry = runtime_root / "registry.global.json" + project = tmp_path / "project" + _write_json( + global_registry, + { + "schema_version": "0.1", + "registry_role": "global-local", + "goals": [ + { + "id": "relative-retired", + "display_name": "Relative retire fixture", + "repo": str(project), + "source_registry": str(tmp_path / "missing" / "registry.json"), + "state_file": "goals/relative-retired/ACTIVE_GOAL_STATE.md", + } + ], + }, + ) + payload_path = tmp_path / "payloads.jsonl" + _configure_closeout(runtime_root, payload_path) + + result = retire_global_registry_goals( + runtime_root_override=str(runtime_root), + goal_ids=["relative-retired"], + execute=True, + ) + assert result["ok"] is True, result + assert result["closeouts"][0]["status"] == "success", result + payload = json.loads(payload_path.read_text(encoding="utf-8").splitlines()[0]) + assert payload["state_file"] == str( + project / "goals" / "relative-retired" / "ACTIVE_GOAL_STATE.md" + ) + + +def test_archive_resolves_global_relative_state_file_from_project_repo( + tmp_path: Path, +) -> None: + runtime_root = tmp_path / "runtime" + project = tmp_path / "project" + project.mkdir() + # The project owns the state file; the runtime holds only artifacts, so a + # runtime-relative resolution would hand the hook an unreadable path. + project_state = project / "ACTIVE_GOAL_STATE.md" + project_state.write_text("project state\n", encoding="utf-8") + source = runtime_root / "goals" / "global-archive" + source.mkdir(parents=True) + (source / "artifact.txt").write_text("kept\n", encoding="utf-8") + global_registry = runtime_root / "registry.global.json" + _write_json( + global_registry, + { + "schema_version": 0.1, + "registry_role": "global-local", + "goals": [ + { + "id": "global-archive", + "display_name": "Global archive fixture", + "repo": str(project), + "state_file": "ACTIVE_GOAL_STATE.md", + } + ], + }, + ) + payload_path = tmp_path / "payloads.jsonl" + _configure_closeout(runtime_root, payload_path) + + result = archive_runtime_goal( + registry_path=global_registry, + runtime_root_override=str(runtime_root), + goal_id="global-archive", + archive_root=None, + allow_registered=True, + execute=True, + ) + assert result["ok"] is True, result + assert result["closeout"]["status"] == "success", result + assert not source.exists() + payload = json.loads(payload_path.read_text(encoding="utf-8").splitlines()[0]) + assert payload["state_file"] == str(project_state) + assert Path(payload["state_file"]).read_text(encoding="utf-8") == "project state\n" + + +def test_orphaned_global_stop_resolves_relative_state_file_from_repo( + tmp_path: Path, +) -> None: + runtime_root = tmp_path / "runtime" + project = tmp_path / "project" + project.mkdir() + project_state = project / "ACTIVE_GOAL_STATE.md" + project_state.write_text("orphan state\n", encoding="utf-8") + global_registry = runtime_root / "registry.global.json" + _write_json( + global_registry, + { + "schema_version": 0.1, + "registry_role": "global-local", + "goals": [ + { + "id": "orphaned-stop", + "display_name": "Orphaned stop fixture", + "repo": str(project), + "source_registry": str(tmp_path / "missing" / "registry.json"), + "state_file": "ACTIVE_GOAL_STATE.md", + "quota": {"compute": 1, "allowed_slots": 4, "spent_slots": 0}, + } + ], + }, + ) + payload_path = tmp_path / "payloads.jsonl" + _configure_closeout(runtime_root, payload_path) + + stopped = set_goal_activation_state( + registry_path=global_registry, + goal_id="orphaned-stop", + state="stopped", + runtime_root_override=str(runtime_root), + actor_kind="owner", + execute=True, + ) + assert stopped["ok"] is True, stopped + assert stopped["closeout"]["status"] == "success", stopped + payload = json.loads(payload_path.read_text(encoding="utf-8").splitlines()[0]) + assert payload["state_file"] == str(project_state) + assert Path(payload["state_file"]).read_text(encoding="utf-8") == "orphan state\n" + + +def test_archive_without_hook_keeps_success_for_unusable_state_file( + tmp_path: Path, +) -> None: + project = tmp_path / "project" + registry_path = project / ".loopx" / "registry.json" + runtime_root = project / ".loopx" / "runtime" + source = runtime_root / "goals" / "stale-state-goal" + source.mkdir(parents=True) + (source / "artifact.txt").write_text("kept\n", encoding="utf-8") + _write_json( + registry_path, + { + "schema_version": 1, + "common_runtime_root": ".loopx/runtime", + "goals": [ + { + "id": "stale-state-goal", + "display_name": "Stale state fixture", + "repo": str(project), + # A stale user alias whose expansion raises; with no + # hook configured this must not be touched at all. + "state_file": "~loopx-retired-operator/goals/stale/ACTIVE_GOAL_STATE.md", + } + ], + }, + ) + + result = archive_runtime_goal( + registry_path=registry_path, + runtime_root_override=None, + goal_id="stale-state-goal", + archive_root=None, + allow_registered=True, + execute=True, + ) + assert result["ok"] is True, result + archive_path = Path(result["archive_path"]) + assert not source.exists() + assert (archive_path / "artifact.txt").read_text(encoding="utf-8") == "kept\n" + assert result["closeout"]["status"] == "not_configured", result + assert result["closeout"]["ledger_recorded"] is False + assert not (archive_path / GOAL_CLOSEOUT_LEDGER_FILENAME).exists() + + +def _two_goal_global_registry(tmp_path: Path) -> tuple[Path, Path]: + runtime_root = tmp_path / "runtime" + project = tmp_path / "project" + project.mkdir() + global_registry = runtime_root / "registry.global.json" + _write_json( + global_registry, + { + "schema_version": "0.1", + "registry_role": "global-local", + "goals": [ + { + "id": "retired-first", + "display_name": "First retired fixture", + "repo": str(project), + "source_registry": str(tmp_path / "missing" / "registry.json"), + "state_file": "ACTIVE_GOAL_STATE.md", + }, + { + "id": "retired-second", + "display_name": "Second retired fixture", + "repo": str(project), + "state_file": "ACTIVE_GOAL_STATE.md", + }, + ], + }, + ) + return runtime_root, global_registry + + +def test_retire_keeps_committed_receipt_when_hook_preparation_fails( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + from loopx.lifecycle import goal_closeout + + runtime_root, global_registry = _two_goal_global_registry(tmp_path) + + def _broken_configuration(root: Path): + raise RuntimeError("hook preparation failed after commit") + + monkeypatch.setattr( + goal_closeout, + "goal_closeout_configuration", + _broken_configuration, + ) + + result = retire_global_registry_goals( + runtime_root_override=str(runtime_root), + goal_ids=["retired-first", "retired-second"], + execute=True, + ) + assert result["ok"] is True, result + assert result["wrote"] is True, result + assert result["global_goal_count_after"] == 0 + assert not (runtime_root / "registry.global.json").read_text(encoding="utf-8").count( + '"retired' + ) + # Both removed records still report their own diagnostic; one unusable + # preparation does not hide the other or the retirement itself. + assert [item["status"] for item in result["closeouts"]] == [ + "preparation_error", + "preparation_error", + ], result + assert all(item["ledger_recorded"] is False for item in result["closeouts"]) + assert not (runtime_root / "closeout").exists() + + +def test_retire_without_hook_ignores_unusable_state_file(tmp_path: Path) -> None: + runtime_root, global_registry = _two_goal_global_registry(tmp_path) + payload = json.loads(global_registry.read_text(encoding="utf-8")) + for goal in payload["goals"]: + # A stale alias raises on expansion; with no hook configured nothing + # may resolve it, and the retirement receipt must stay successful. + goal["state_file"] = "~loopx-retired-operator/goals/stale/ACTIVE_GOAL_STATE.md" + _write_json(global_registry, payload) + + result = retire_global_registry_goals( + runtime_root_override=str(runtime_root), + goal_ids=["retired-first", "retired-second"], + execute=True, + ) + assert result["ok"] is True, result + assert result["wrote"] is True, result + assert result["global_goal_count_after"] == 0 + assert [item["status"] for item in result["closeouts"]] == [ + "not_configured", + "not_configured", + ], result + assert not (runtime_root / "closeout").exists() + + +def test_stop_runs_closeout_for_committed_transition_when_projection_fails( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + from loopx.control_plane.goals import activation_service + + runtime_root = tmp_path / "runtime" + project = tmp_path / "project" + source_registry = project / ".loopx" / "registry.json" + _write_json( + source_registry, + { + "schema_version": "0.1", + "common_runtime_root": str(runtime_root), + "goals": [ + { + "id": "projected-stop", + "display_name": "Projected stop fixture", + "repo": str(project), + "quota": {"compute": 1, "allowed_slots": 4, "spent_slots": 0}, + } + ], + }, + ) + synced = sync_project_registry_to_global( + registry_path=source_registry, + runtime_root_override=str(runtime_root), + goal_id="projected-stop", + dry_run=False, + ) + assert synced["ok"] is True + global_registry = runtime_root / "registry.global.json" + payload_path = tmp_path / "payloads.jsonl" + _configure_closeout(runtime_root, payload_path) + + real_sync = activation_service.sync_project_registry_to_global + failures = {"remaining": 1} + + def _flaky_sync(**kwargs): + if failures["remaining"]: + failures["remaining"] -= 1 + return { + "ok": False, + "error": "shared registry write is temporarily unavailable", + "recommended_action": "rerun loopx sync-global --goal-id projected-stop", + } + return real_sync(**kwargs) + + monkeypatch.setattr( + activation_service, "sync_project_registry_to_global", _flaky_sync + ) + + first = set_goal_activation_state( + registry_path=global_registry, + goal_id="projected-stop", + state="stopped", + actor_kind="owner", + execute=True, + ) + # The projection failed, but the source stop committed and the hook still + # ran once for that committed transition. + assert first["ok"] is False, first + assert first["partial_write"] is True, first + assert first["written"] is True, first + assert first["closeout"]["status"] == "success", first + assert len(_ledger(runtime_root, "projected-stop")) == 1 + + second = set_goal_activation_state( + registry_path=global_registry, + goal_id="projected-stop", + state="stopped", + actor_kind="owner", + execute=True, + ) + assert second["ok"] is True, second + assert second["projection_reconciled"] is True, second + assert "closeout" not in second + # The retry reconciles the projection without a second hook invocation. + assert len(_ledger(runtime_root, "projected-stop")) == 1 + assert len(payload_path.read_text(encoding="utf-8").splitlines()) == 1 + + + +def test_stop_runs_closeout_when_shared_registry_writer_raises_eio( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """An EIO from the shared-registry writer must not erase the stop.""" + + from loopx import global_registry as global_registry_module + + runtime_root = tmp_path / "runtime" + project = tmp_path / "project" + source_registry = project / ".loopx" / "registry.json" + _write_json( + source_registry, + { + "schema_version": "0.1", + "common_runtime_root": str(runtime_root), + "goals": [ + { + "id": "eio-stop", + "display_name": "EIO stop fixture", + "repo": str(project), + "quota": {"compute": 1, "allowed_slots": 4, "spent_slots": 0}, + } + ], + }, + ) + synced = sync_project_registry_to_global( + registry_path=source_registry, + runtime_root_override=str(runtime_root), + goal_id="eio-stop", + dry_run=False, + ) + assert synced["ok"] is True + global_registry = runtime_root / "registry.global.json" + payload_path = tmp_path / "payloads.jsonl" + _configure_closeout(runtime_root, payload_path) + + real_write = global_registry_module.write_json + failures = {"remaining": 1} + + target = global_registry.resolve() + + def _flaky_write(path, payload): + if failures["remaining"] and Path(path).resolve() == target: + failures["remaining"] -= 1 + raise OSError(errno.EIO, "Input/output error") + return real_write(path, payload) + + monkeypatch.setattr(global_registry_module, "write_json", _flaky_write) + + first = set_goal_activation_state( + registry_path=global_registry, + goal_id="eio-stop", + state="stopped", + actor_kind="owner", + execute=True, + ) + # The projection raised, but the committed source stop keeps its receipt + # and still dispatches the configured hook exactly once. + assert first["written"] is True, first + assert first["ok"] is False, first + assert first["partial_write"] is True, first + assert first["global_sync"]["error_kind"] == "global_sync_failed", first + assert first["closeout"]["status"] == "success", first + assert len(_ledger(runtime_root, "eio-stop")) == 1 + + second = set_goal_activation_state( + registry_path=global_registry, + goal_id="eio-stop", + state="stopped", + actor_kind="owner", + execute=True, + ) + assert second["ok"] is True, second + assert second["projection_reconciled"] is True, second + assert "closeout" not in second + assert len(_ledger(runtime_root, "eio-stop")) == 1 + assert len(payload_path.read_text(encoding="utf-8").splitlines()) == 1 + + +def test_stop_runs_closeout_when_readback_raises( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A read fault after the commit must not erase the committed stop.""" + + from loopx.control_plane.goals import activation_service + + runtime_root = tmp_path / "runtime" + project = tmp_path / "project" + source_registry = project / ".loopx" / "registry.json" + _write_json( + source_registry, + { + "schema_version": "0.1", + "common_runtime_root": str(runtime_root), + "goals": [ + { + "id": "readback-stop", + "display_name": "Readback stop fixture", + "repo": str(project), + "quota": {"compute": 1, "allowed_slots": 4, "spent_slots": 0}, + } + ], + }, + ) + synced = sync_project_registry_to_global( + registry_path=source_registry, + runtime_root_override=str(runtime_root), + goal_id="readback-stop", + dry_run=False, + ) + assert synced["ok"] is True + global_registry = runtime_root / "registry.global.json" + payload_path = tmp_path / "payloads.jsonl" + _configure_closeout(runtime_root, payload_path) + + def _raising_readback(**kwargs): + raise OSError(errno.EIO, "Input/output error") + + monkeypatch.setattr( + activation_service, "_readback", _raising_readback + ) + + first = set_goal_activation_state( + registry_path=global_registry, + goal_id="readback-stop", + state="stopped", + actor_kind="owner", + execute=True, + ) + assert first["written"] is True, first + assert first["ok"] is False, first + assert first["partial_write"] is True, first + assert first["readback"]["status"] == "readback_failed", first + assert "Input/output error" in first["readback"]["error"], first + assert first["closeout"]["status"] == "success", first + assert len(_ledger(runtime_root, "readback-stop")) == 1 diff --git a/tests/capabilities/test_machine_configuration_contract.py b/tests/capabilities/test_machine_configuration_contract.py index 8817473b7e..de0d4d68ae 100644 --- a/tests/capabilities/test_machine_configuration_contract.py +++ b/tests/capabilities/test_machine_configuration_contract.py @@ -352,6 +352,7 @@ def test_builtin_catalog_lists_every_owner_in_namespace_order() -> None: "codex_chat", "context_delivery", "explore_harness", + "goal_closeout", "goal_storage", "manager_runtime", "periodic_report", diff --git a/tests/test_chat_machine_configuration_api.py b/tests/test_chat_machine_configuration_api.py index 3380e5a689..fc19e80717 100644 --- a/tests/test_chat_machine_configuration_api.py +++ b/tests/test_chat_machine_configuration_api.py @@ -347,6 +347,86 @@ def test_real_chat_http_invalid_namespace_has_safe_repair_path( server.server_close() +def test_goal_closeout_http_update_cannot_write_a_command(tmp_path: Path) -> None: + from loopx.capabilities.machine_configuration.builtins import ( + build_builtin_machine_configuration_registry, + ) + + handler = _Handler( + tmp_path, + body={ + "namespace": "goal_closeout", + "namespace_configuration": { + "schema_version": "loopx_goal_closeout_configuration_v0", + "command": ["/bin/sh", "-c", "id > /tmp/pwned"], + }, + }, + ) + registry = build_builtin_machine_configuration_registry() + + with pytest.raises(ValueError, match="local CLI: command"): + handler._machine_configuration_namespace_update( + handler.body, registry=registry + ) + assert not (tmp_path / "machine" / "configuration.json").exists() + + timeout_only = _Handler( + tmp_path, + body={ + "namespace": "goal_closeout", + "namespace_configuration": { + "schema_version": "loopx_goal_closeout_configuration_v0", + "timeout_seconds": 12, + }, + }, + ) + configuration = timeout_only._machine_configuration_namespace_update( + timeout_only.body, registry=registry + ) + assert configuration["namespaces"]["goal_closeout"] == { + "schema_version": "loopx_goal_closeout_configuration_v0", + "command": [], + "timeout_seconds": 12, + } + + +def test_goal_closeout_real_http_rejects_a_command_update(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=5) + try: + connection.request( + "POST", + CHAT_MACHINE_CONFIGURATION_PREVIEW_PATH, + body=json.dumps( + { + "namespace": "goal_closeout", + "namespace_configuration": { + "schema_version": "loopx_goal_closeout_configuration_v0", + "command": ["/bin/sh", "-c", "id > /tmp/pwned"], + }, + } + ), + headers={"Content-Type": "application/json"}, + ) + response = connection.getresponse() + assert response.status == 400 + body = json.loads(response.read()) + assert body["ok"] is False + assert "local CLI: command" in body["error"] + assert not (tmp_path / "machine" / "configuration.json").exists() + finally: + connection.close() + server.shutdown() + thread.join(timeout=5) + server.server_close() + + def test_inspection_lists_registered_namespaces_without_local_refs( tmp_path: Path, ) -> None: @@ -361,6 +441,7 @@ def test_inspection_lists_registered_namespaces_without_local_refs( "codex_chat", "context_delivery", "explore_harness", + "goal_closeout", "goal_storage", "manager_runtime", "periodic_report", @@ -386,6 +467,14 @@ def test_inspection_lists_registered_namespaces_without_local_refs( "planning_horizon_goal": True, "yardstick_enabled": False, } + assert namespace_catalog["goal_closeout"]["configuration_template"] == { + "schema_version": "loopx_goal_closeout_configuration_v0", + "configured": False, + "timeout_seconds": 30, + } + assert namespace_catalog["goal_closeout"]["configuration_template"].get( + "command" + ) is None assert namespace_catalog["change_quality_qualification"][ "configuration_template" ] == { @@ -466,7 +555,16 @@ def test_machine_catalog_discovers_goal_features_without_granting_machine_writes ) # Machine-only capabilities are the ones a Goal cannot override: the # manager profile, native host defaults, steward executor and storage target. - assert set(machine) - {"manager_runtime", "steward_executor", "steward_memory", "goal_storage", "codex_chat", "context_delivery"} == { + machine_only = { + "manager_runtime", + "steward_executor", + "steward_memory", + "goal_storage", + "codex_chat", + "context_delivery", + "goal_closeout", + } + assert set(machine) - machine_only == { feature["feature_id"] for feature in goal["features"] } assert machine["pull_request_review"]["available_scopes"] == ["machine", "goal"] @@ -482,7 +580,7 @@ def test_machine_catalog_discovers_goal_features_without_granting_machine_writes for capability_id, item in machine.items(): assert "current" not in item assert "commands" not in item - if capability_id in {"manager_runtime", "steward_executor", "steward_memory", "goal_storage", "codex_chat", "context_delivery"}: + if capability_id in machine_only: assert item["available_scopes"] == ["machine"] assert item["machine_namespace"] == capability_id assert item["configuration_editor"]["writable_scopes"] == ["machine"]