diff --git a/benchmark/edgebench/run.py b/benchmark/edgebench/run.py index e679d04b05..50efb3b7d3 100644 --- a/benchmark/edgebench/run.py +++ b/benchmark/edgebench/run.py @@ -131,6 +131,14 @@ def main(argv=None): help="Explicit complete v1 Turn contract with shared exact-Turn settlement; heartbeat workers only") parser.add_argument("--context-delivery", choices=("off", "on"), help="Native Codex context delivery: on by default for heartbeat workers; pass off to disable") + parser.add_argument("--compact-direct-read", action="store_true", + help="Opt-in Phase-2 lock-free compact hook direct-read (--compact-direct-read in hooks.json)") + parser.add_argument("--quiet-turn", action="store_true", + help="Opt-in Quiet-Turn skip metadata evaluation for supported heartbeat/turn workers") + parser.add_argument("--epoch-prefetch-json", type=json.loads, default=None, + help="Opt-in Stage-then-Swap EpochReset JSON configuration for supported heartbeat/turn workers") + parser.add_argument("--execution-yardstick-enabled", action=argparse.BooleanOptionalAction, default=None, + help="Opt-in Yardstick measurement review toggle for configure-goal (including WarmStart)") parser.add_argument("--execution-observation", choices=("local_probe",), help="Optional local execution probe for heartbeat workers") parser.add_argument("--native-result-comparison", action="store_true", @@ -216,6 +224,9 @@ def main(argv=None): context_delivery=args.context_delivery, turn_envelope=args.turn_envelope, complete_turn=args.complete_turn, validation_command=args.validation_command_json, native_child_limit=args.native_child_limit, + compact_direct_read=args.compact_direct_read, + quiet_turn=args.quiet_turn, + epoch_prefetch_json=args.epoch_prefetch_json, ) except ValueError as error: parser.error(str(error)) @@ -275,6 +286,8 @@ def main(argv=None): parser.error("--stride-strategy requires a heartbeat worker or turn-resume") if args.planning_horizon_goal is not None and not execution.uses_loopx: parser.error("--planning-horizon-goal requires a heartbeat worker or turn-resume") + if args.execution_yardstick_enabled is not None and not execution.uses_loopx: + parser.error("--execution-yardstick-enabled requires a heartbeat worker or turn-resume") checkpoint_request = None if args.checkpoint_request is not None: if not args.worker.startswith("heartbeat-"): @@ -464,6 +477,12 @@ def main(argv=None): validation_command=execution.validation_command, turn_envelope=args.turn_envelope, **({"complete_turn": True} if args.complete_turn else {}), + **({"compact_direct_read": True} if args.compact_direct_read else {}), + **({"quiet_turn": True} if args.quiet_turn else {}), + **({"epoch_prefetch_json": execution.epoch_prefetch_json} + if execution.epoch_prefetch_json is not None else {}), + **({"execution_yardstick_enabled": args.execution_yardstick_enabled} + if args.execution_yardstick_enabled is not None else {}), context_delivery=args.context_delivery, **({"execution_observation": args.execution_observation} if args.execution_observation is not None else {}), diff --git a/benchmark/runtime/codex.py b/benchmark/runtime/codex.py index 8766af77fb..5d35af4703 100644 --- a/benchmark/runtime/codex.py +++ b/benchmark/runtime/codex.py @@ -6,8 +6,10 @@ import json import math import os +from collections.abc import Mapping from dataclasses import dataclass from pathlib import Path +from typing import Any # New benchmark runs and the product both default to six settled work Turns. @@ -20,6 +22,34 @@ SANDBOXES = ("read-only", "workspace-write", "danger-full-access") +def normalize_epoch_prefetch_json(value: Any) -> str | None: + """Normalize optional Stage-then-Swap EpochReset config to canonical compact JSON.""" + if value is None or value == "": + return None + if isinstance(value, Mapping): + parsed = dict(value) + elif isinstance(value, str): + try: + decoded = json.loads(value) + except json.JSONDecodeError as exc: + raise ValueError("epoch_prefetch_json must be valid JSON") from exc + if not isinstance(decoded, dict): + raise ValueError("epoch_prefetch_json must decode to a JSON object") + parsed = decoded + else: + raise ValueError("epoch_prefetch_json must be a JSON string or mapping") + return json.dumps(parsed, ensure_ascii=False, sort_keys=True, separators=(",", ":")) + + +def epoch_prefetch_enabled(value: Any) -> bool: + """Return True iff normalized epoch_prefetch JSON explicitly enables the treatment.""" + normalized = normalize_epoch_prefetch_json(value) + if normalized is None: + return False + decoded = json.loads(normalized) + return isinstance(decoded, dict) and decoded.get("enabled") is True + + @dataclass(frozen=True) class Execution: mode: str = "heartbeat" @@ -32,6 +62,9 @@ class Execution: context_delivery: str | None = None native_child_limit: int | None = None complete_turn: bool = False + compact_direct_read: bool = False + quiet_turn: bool = False + epoch_prefetch_json: str | None = None def __post_init__(self) -> None: if self.native_child_limit is not None and ( @@ -48,6 +81,22 @@ def __post_init__(self) -> None: raise ValueError("turn_envelope requires heartbeat execution and a boolean opt-in") if type(self.complete_turn) is not bool or (self.complete_turn and (self.mode != "heartbeat" or self.turn_envelope)): raise ValueError("complete_turn requires heartbeat execution without legacy turn_envelope") + if type(self.compact_direct_read) is not bool or ( + self.compact_direct_read and (self.mode != "heartbeat" or self.context_delivery != "on") + ): + raise ValueError("compact_direct_read requires heartbeat execution with context_delivery=on") + if type(self.quiet_turn) is not bool or ( + self.quiet_turn and self.mode not in {"heartbeat", "turn"} + ): + raise ValueError("quiet_turn requires heartbeat or turn execution and a boolean opt-in") + if self.quiet_turn and self.mode == "heartbeat" and self.context_delivery != "on": + raise ValueError("quiet_turn in heartbeat mode requires context_delivery=on") + normalized_prefetch = normalize_epoch_prefetch_json(self.epoch_prefetch_json) + object.__setattr__(self, "epoch_prefetch_json", normalized_prefetch) + if normalized_prefetch is not None and self.mode not in {"heartbeat", "turn"}: + raise ValueError("epoch_prefetch_json requires heartbeat or turn execution") + if normalized_prefetch is not None and self.mode == "heartbeat" and self.context_delivery != "on": + raise ValueError("epoch_prefetch_json in heartbeat mode requires context_delivery=on") if self.mode not in MODES or self.context not in CONTEXTS: raise ValueError("unsupported execution mode or iteration context") if self.task_entry is None: @@ -89,22 +138,40 @@ def native_goal(self) -> bool: return self.mode in {"native-goal", "loopx-goal"} -def turn_envelope_receipt(*, explicit: bool, complete: bool = False) -> dict: +def turn_envelope_receipt(*, explicit: bool, complete: bool = False) -> dict[str, Any]: """Record only the explicit display opt-in; checkpoint capture is independent.""" return {**({"turn_envelope": True, "turn_envelope_source": "explicit"} if explicit else {}), **({"complete_turn": True, "complete_turn_source": "explicit"} if complete else {})} -def explicit_turn_display(env: dict[str, str]) -> Execution: +def explicit_turn_display(env: Mapping[str, str]) -> Execution: """Decode the shared explicit display request through its Execution owner.""" - selected = {} + selected: dict[str, bool] = {} for variable, field in (("LOOPX_TURN_ENVELOPE", "turn_envelope"), ("LOOPX_COMPLETE_TURN", "complete_turn")): value = env.get(variable, "0") if value not in {"0", "1"}: raise ValueError(f"{variable} must be 0 or 1") selected[field] = value == "1" - return Execution(**selected) + return Execution( + turn_envelope=selected["turn_envelope"], + complete_turn=selected["complete_turn"], + ) + + +def explicit_context_opt_ins(env: Mapping[str, str]) -> dict[str, Any]: + """Decode strictly default-off hook/Turn optimization opt-ins from worker env.""" + parsed: dict[str, Any] = {} + for variable, field in (("LOOPX_COMPACT_DIRECT_READ", "compact_direct_read"), + ("LOOPX_QUIET_TURN", "quiet_turn")): + value = env.get(variable, "0") + if value not in {"0", "1"}: + raise ValueError(f"{variable} must be 0 or 1") + parsed[field] = value == "1" + parsed["epoch_prefetch_json"] = normalize_epoch_prefetch_json( + env.get("LOOPX_EPOCH_PREFETCH_JSON") + ) + return parsed def native_child_instructions(limit: int | None) -> str: @@ -126,7 +193,7 @@ def native_child_instructions(limit: int | None) -> str: "and close child work before the parent finishes.\n" ) -def validate_model_catalog(model_catalog: Path, model: str) -> dict: +def validate_model_catalog(model_catalog: Path, model: str) -> dict[str, Any]: """Admit only catalog entries that keep Codex's function-tool surface. Real catalog entries are not capability-only. ``tool_mode`` selects how diff --git a/benchmark/runtime/context_delivery.py b/benchmark/runtime/context_delivery.py index f05a6e1051..e980f04292 100644 --- a/benchmark/runtime/context_delivery.py +++ b/benchmark/runtime/context_delivery.py @@ -5,11 +5,19 @@ """ from __future__ import annotations +import inspect import json import os import stat import shlex from pathlib import Path +from typing import Any + +from .codex import explicit_context_opt_ins + + +class ContextOptInConfigurationError(ValueError): + """Explicit opt-in hook parameter failed validation or product compatibility.""" def preflight_context_instruction(env: dict[str, str], path: Path, digest: str, turn_id: str, *, @@ -52,8 +60,33 @@ def prepare_context_delivery(env: dict[str, str], home: Path) -> None: if not stat.S_ISDIR(info.st_mode) or info.st_uid != os.getuid() or info.st_mode & 0o077: raise ValueError("context delivery requires a private, physical trial directory") env["LOOPX_CODEX_CONTEXT_DIR"] = str(root) + try: + opt_ins = explicit_context_opt_ins(env) + except ValueError as exc: + raise ContextOptInConfigurationError(str(exc)) from exc + params = inspect.signature(codex_context_configuration).parameters + extra_config: dict[str, Any] = {} + if opt_ins["compact_direct_read"]: + if "compact_direct_read" not in params: + raise ContextOptInConfigurationError( + "compact_direct_read requires a LoopX product release supporting --compact-direct-read" + ) + extra_config["compact_direct_read"] = True + if opt_ins["quiet_turn"]: + if "quiet_turn" not in params: + raise ContextOptInConfigurationError( + "quiet_turn requires a LoopX product release supporting --quiet-turn on context-config/hook" + ) + extra_config["quiet_turn"] = True + if opt_ins["epoch_prefetch_json"] is not None: + if "epoch_prefetch" not in params: + raise ContextOptInConfigurationError( + "epoch_prefetch_json requires a LoopX product release supporting --epoch-prefetch-json on context-config/hook" + ) + extra_config["epoch_prefetch"] = json.loads(opt_ins["epoch_prefetch_json"]) configuration = codex_context_configuration(str(root), goal_id=env["LOOPX_GOAL_ID"], - agent_id=env["LOOPX_AGENT_ID"], scenario="codex_benchmark_heartbeat", cli_argv=[env["LOOPX_CLI"], "--cli-bin", env["LOOPX_CLI"]]) + agent_id=env["LOOPX_AGENT_ID"], scenario="codex_benchmark_heartbeat", cli_argv=[env["LOOPX_CLI"], "--cli-bin", env["LOOPX_CLI"]], + **extra_config) content = json.dumps({"hooks": configuration["hooks"]}) path = home / "hooks.json" if path.is_symlink() or (path.exists() and path.read_text() != content): diff --git a/benchmark/runtime/harbor.py b/benchmark/runtime/harbor.py index b90d81b660..b45274c9cd 100644 --- a/benchmark/runtime/harbor.py +++ b/benchmark/runtime/harbor.py @@ -21,7 +21,13 @@ from loopx.control_plane.goals.goal_vision_policy import DEFAULT_PLANNING_HORIZON_GOAL from .codex_offline import MODEL_CATALOG_REMOTE, CodexOffline, stage_portable_python -from .codex import DEFAULT_REPLAN_AFTER_TURNS, Execution, turn_envelope_receipt +from .codex import ( + DEFAULT_REPLAN_AFTER_TURNS, + Execution, + epoch_prefetch_enabled, + normalize_epoch_prefetch_json, + turn_envelope_receipt, +) from .layout import _ROOT, _SRC, _CONTROL, _CODEX_HOME, _REGISTRY, _LOOPX_RUNTIME from .scheduler import phase_work_reserve @@ -84,6 +90,10 @@ def __init__( agent_id=_AGENT_ID, defer_codex_home=False, complete_turn=False, + compact_direct_read=False, + quiet_turn=False, + epoch_prefetch_json=None, + execution_yardstick_enabled=None, **kwargs, ): if "research_loop" in kwargs: @@ -105,6 +115,9 @@ def __init__( context_delivery, native_child_limit, complete_turn=complete_turn, + compact_direct_read=compact_direct_read, + quiet_turn=quiet_turn, + epoch_prefetch_json=normalize_epoch_prefetch_json(epoch_prefetch_json), ) if type(provider_request_admission) is not bool: raise ValueError("provider_request_admission must be a boolean opt-in") @@ -149,6 +162,12 @@ def __init__( DEFAULT_PLANNING_HORIZON_GOAL if planning_horizon_goal is None and self.execution.uses_loopx else planning_horizon_goal ) + if execution_yardstick_enabled is not None: + if type(execution_yardstick_enabled) is not bool: + raise ValueError("execution_yardstick_enabled must be a boolean") + if not self.execution.uses_loopx: + raise ValueError("execution_yardstick_enabled requires a LoopX execution mode") + self.execution_yardstick_enabled = execution_yardstick_enabled from .research_probe import resolve_execution_observation self.execution_observation = resolve_execution_observation(execution_observation) @@ -197,7 +216,13 @@ def _turn_envelope_receipt(self) -> dict: return {**turn_envelope_receipt(explicit=self.execution.turn_envelope, complete=self.execution.complete_turn), **({"native_child_limit": self.execution.native_child_limit} if self.execution.native_child_limit is not None else {}), - **({"context_delivery": "on"} if self.execution.context_delivery != "off" else {})} + **({"context_delivery": "on"} if self.execution.context_delivery != "off" else {}), + **({"compact_direct_read": True} if self.execution.compact_direct_read else {}), + **({"quiet_turn": True} if self.execution.quiet_turn else {}), + **({"epoch_prefetch": True, "epoch_prefetch_configured": True} + if epoch_prefetch_enabled(self.execution.epoch_prefetch_json) + else ({"epoch_prefetch_configured": False} + if self.execution.epoch_prefetch_json is not None else {})),} def _planning_receipt(self) -> dict: return {"native_goal_continue": True} if self.native_goal_continue else {} @@ -210,12 +235,33 @@ def _replan_configuration(self) -> tuple[str, str, int]: return ("replan_after_completed_todos", "--execution-replan-after-todos", self.replan_after_todos) - def _replan_receipt(self) -> dict[str, int | str]: + def _replan_receipt(self) -> dict[str, int | str | bool]: key, _, value = self._replan_configuration() return {key: value, **({"stride_strategy": self.stride_strategy} if self.stride_strategy is not None else {}), **({"planning_horizon_goal": self.planning_horizon_goal} - if self.planning_horizon_goal is not None else {})} + if self.planning_horizon_goal is not None else {}), + **({"yardstick_enabled": self.execution_yardstick_enabled} + if self.execution_yardstick_enabled is not None else {})} + + def _yardstick_args(self) -> list[str]: + if self.execution_yardstick_enabled is None: + return [] + return [ + "--execution-yardstick-enabled" + if self.execution_yardstick_enabled + else "--no-execution-yardstick-enabled" + ] + + async def _apply_warm_start_yardstick(self, environment: BaseEnvironment, *, cwd: str) -> None: + args = self._yardstick_args() + if not args: + return + await self._loopx( + environment, + ["configure-goal", "--goal-id", self.goal_id, *args, "--execute"], + cwd=cwd, + ) @staticmethod def name() -> str: @@ -496,7 +542,7 @@ async def _registry_exists(self, environment: BaseEnvironment) -> bool: return result.return_code == 0 async def _prepare_phase( - self, environment: BaseEnvironment, instruction: str, *, cwd: str + self, environment: BaseEnvironment, instruction: str, *, cwd: str, verify: bool = True, ) -> None: key, option, expected = self._replan_configuration() stride_args = (["--execution-stride-strategy", self.stride_strategy] @@ -504,6 +550,7 @@ async def _prepare_phase( if self.planning_horizon_goal is not None: stride_args.append("--execution-planning-horizon-goal" if self.planning_horizon_goal else "--no-execution-planning-horizon-goal") + stride_args.extend(self._yardstick_args()) pending = await environment.exec( command=f"test -e {_LOOPX_RUNTIME}/benchmark-pending-turn.json" ) @@ -588,7 +635,8 @@ async def _prepare_phase( if self.execution.task_entry == "seeded-todo": await self._seed_phase(environment, cwd=cwd) - await self._verify_phase_configuration(environment, cwd=cwd) + if verify: + await self._verify_phase_configuration(environment, cwd=cwd) async def _verify_phase_configuration(self, environment: BaseEnvironment, *, cwd: str) -> None: """Read the treatment without reconfiguring a restored Goal or its queue.""" @@ -611,6 +659,26 @@ async def _verify_phase_configuration(self, environment: BaseEnvironment, *, cwd raise RuntimeError( f"stride strategy readback mismatch: expected {self.stride_strategy or 'off'}, got {strategy!r}" ) + if self.execution_yardstick_enabled is not None: + actual_yardstick = configured_state.get("execution_profile", {}).get("yardstick_enabled") + if type(actual_yardstick) is not bool or actual_yardstick != self.execution_yardstick_enabled: + raise RuntimeError("execution_yardstick_enabled readback mismatch") + if self.execution_yardstick_enabled is True: + turn_context = await self._loopx( + environment, + ["explore", "turn-context", "--goal-id", self.goal_id, "--agent-id", self.agent_id], + cwd=cwd, + ) + graph_obj = turn_context.get("graph") if isinstance(turn_context.get("graph"), dict) else None + if ( + turn_context.get("graph_enabled") is not True + or graph_obj is None + or not isinstance(graph_obj.get("work_yardstick_capture"), dict) + ): + raise RuntimeError( + "execution_yardstick_enabled requires an active Explore evidence graph " + "with work_yardstick_capture available" + ) def _verify_native_child_configuration(self, configured_state: dict) -> None: if self.execution.native_child_limit is None: @@ -695,6 +763,9 @@ def _worker_env(self, *, cwd: str) -> dict[str, str]: **({"LOOPX_TURN_ENVELOPE": "1"} if self.execution.turn_envelope else {}), "LOOPX_COMPLETE_TURN": "1" if self.execution.complete_turn else "0", "LOOPX_CONTEXT_DELIVERY": self.execution.context_delivery, + "LOOPX_COMPACT_DIRECT_READ": "1" if self.execution.compact_direct_read else "0", + "LOOPX_QUIET_TURN": "1" if self.execution.quiet_turn else "0", + "LOOPX_EPOCH_PREFETCH_JSON": self.execution.epoch_prefetch_json or "", # A trial's explicit None must not inherit a container opt-in. "LOOPX_NATIVE_CHILD_LIMIT": ( str(self.execution.native_child_limit) diff --git a/benchmark/runtime/sforge.py b/benchmark/runtime/sforge.py index 6887532256..94ffc0f9a9 100644 --- a/benchmark/runtime/sforge.py +++ b/benchmark/runtime/sforge.py @@ -20,7 +20,14 @@ from benchmark.edgebench.feedback import FEEDBACK_MODES from benchmark.edgebench.feedback_hook import FEEDBACK_PAYLOAD -from .codex import DEFAULT_REPLAN_AFTER_TURNS, Execution, codex_settings, native_child_instructions, prepare_codex_home +from .codex import ( + DEFAULT_REPLAN_AFTER_TURNS, + Execution, + codex_settings, + native_child_instructions, + normalize_epoch_prefetch_json, + prepare_codex_home, +) from .codex_offline import CodexOffline, MODEL_CATALOG_REMOTE, stage_portable_python from .codex_readiness import native_readiness_budget from .harbor import ( @@ -46,13 +53,17 @@ def profile_execution(profile, *, task_entry=None, context_delivery=None, turn_envelope=False, complete_turn=False, - validation_command=None, native_child_limit=None): + validation_command=None, native_child_limit=None, + compact_direct_read=False, quiet_turn=False, + epoch_prefetch_json=None): """Resolve the native experiment arm through the shared runtime contract.""" if profile not in PROFILE_MODES: raise ValueError("Unknown benchmark worker profile") return Execution( mode=PROFILE_MODES[profile], task_entry=task_entry, context_delivery=context_delivery, turn_envelope=turn_envelope, complete_turn=complete_turn, native_child_limit=native_child_limit, + compact_direct_read=compact_direct_read, quiet_turn=quiet_turn, + epoch_prefetch_json=normalize_epoch_prefetch_json(epoch_prefetch_json), validation_command=() if validation_command is None else validation_command, ) @@ -130,13 +141,19 @@ def __init__(self, config, *, profile: str, cwd: str, checkpoint_request_path: str | None = None, checkpoint_request: bytes | None = None, warm_start=None, research_sidecar: bool = False, - native_result_scope: dict | None = None, complete_turn: bool = False): + native_result_scope: dict | None = None, complete_turn: bool = False, + compact_direct_read: bool = False, quiet_turn: bool = False, + epoch_prefetch_json: str | dict | None = None, + execution_yardstick_enabled: bool | None = None): super().__init__(config) self.execution = profile_execution( profile, task_entry=task_entry, context_delivery=context_delivery, turn_envelope=turn_envelope, complete_turn=complete_turn, validation_command=validation_command, native_child_limit=native_child_limit, + compact_direct_read=compact_direct_read, + quiet_turn=quiet_turn, + epoch_prefetch_json=epoch_prefetch_json, ) self.turn_envelope = self.execution.turn_envelope self.context_delivery = self.execution.context_delivery @@ -207,6 +224,12 @@ def __init__(self, config, *, profile: str, cwd: str, DEFAULT_PLANNING_HORIZON_GOAL if planning_horizon_goal is None and self.execution.uses_loopx else planning_horizon_goal ) + if execution_yardstick_enabled is not None: + if type(execution_yardstick_enabled) is not bool: + raise ValueError("execution_yardstick_enabled must be a boolean") + if not self.execution.uses_loopx: + raise ValueError("execution_yardstick_enabled requires a heartbeat profile or turn-resume") + self.execution_yardstick_enabled = execution_yardstick_enabled self.stride_strategy = stride_strategy self.checkpoint_request_path = None self.checkpoint_request = checkpoint_request @@ -423,6 +446,12 @@ def _install_worker(self, backend, handle, log_dir, logger): task_entry=self.task_entry, turn_envelope=self.turn_envelope, **({"complete_turn": True} if self.execution.complete_turn else {}), + **({"compact_direct_read": True} if self.execution.compact_direct_read else {}), + **({"quiet_turn": True} if self.execution.quiet_turn else {}), + **({"epoch_prefetch_json": self.execution.epoch_prefetch_json} + if self.execution.epoch_prefetch_json is not None else {}), + **({"execution_yardstick_enabled": self.execution_yardstick_enabled} + if self.execution_yardstick_enabled is not None else {}), context_delivery=self.context_delivery, **({"execution_observation": self.execution_observation} if self.execution_observation is not None else {}), @@ -590,20 +619,36 @@ def format_run_cmd(self, prompt_path, *, model=None, cwd="", internet=True, resu # The sealed Goal, task graph, review counters and strategy # are the continuing state. Do not bootstrap, seed another # task, or reset an existing configuration during restore. - if self.native_child_limit is not None: - asyncio.run(self.runtime._verify_phase_configuration(self.environment, cwd=self.cwd)) - asyncio.run(self.runtime._write_task_document(self.environment, result.stdout)) - if self.native_child_limit is None: + if self.execution_yardstick_enabled is not None: + if self.native_child_limit is not None: + retained = asyncio.run(self.runtime._loopx( + self.environment, ["configure-goal", "--goal-id", self.runtime.goal_id], cwd=self.cwd, + )) + self.runtime._verify_native_child_configuration( + retained.get("after") or retained.get("before") or {} + ) + asyncio.run(self.runtime._apply_warm_start_yardstick(self.environment, cwd=self.cwd)) + asyncio.run(self.runtime._write_task_document(self.environment, result.stdout)) asyncio.run(self.runtime._verify_phase_configuration(self.environment, cwd=self.cwd)) + else: + if self.native_child_limit is not None: + asyncio.run(self.runtime._verify_phase_configuration(self.environment, cwd=self.cwd)) + asyncio.run(self.runtime._write_task_document(self.environment, result.stdout)) + if self.native_child_limit is None: + asyncio.run(self.runtime._verify_phase_configuration(self.environment, cwd=self.cwd)) else: - asyncio.run(self.runtime._prepare_phase(self.environment, result.stdout, cwd=self.cwd)) - if self.profile == "heartbeat-explore" and self.warm_start is None: - observed = asyncio.run(self.runtime._loopx(self.environment, [ - "configure-goal", "--goal-id", self.runtime.goal_id, - "--explore-graph-enabled", "--explore-harness-enabled", - "--explore-harness-profile", "adaptive-resilient", "--execute", - ], cwd=self.cwd)) - (self.log_dir / "explore-activation.json").write_text(json.dumps(observed)) + is_explore_cold = self.profile == "heartbeat-explore" + asyncio.run(self.runtime._prepare_phase( + self.environment, result.stdout, cwd=self.cwd, verify=not is_explore_cold, + )) + if is_explore_cold: + observed = asyncio.run(self.runtime._loopx(self.environment, [ + "configure-goal", "--goal-id", self.runtime.goal_id, + "--explore-graph-enabled", "--explore-harness-enabled", + "--explore-harness-profile", "adaptive-resilient", "--execute", + ], cwd=self.cwd)) + (self.log_dir / "explore-activation.json").write_text(json.dumps(observed)) + asyncio.run(self.runtime._verify_phase_configuration(self.environment, cwd=self.cwd)) else: asyncio.run(self.runtime._write_task_document(self.environment, result.stdout)) if self.warm_start is not None: diff --git a/benchmark/runtime/worker.py b/benchmark/runtime/worker.py index 8b234e193b..4b4b046d27 100644 --- a/benchmark/runtime/worker.py +++ b/benchmark/runtime/worker.py @@ -16,6 +16,8 @@ from benchmark.runtime.codex import ( Execution, + epoch_prefetch_enabled, + explicit_context_opt_ins, explicit_turn_display, prepare_codex_home, process_environment, @@ -352,6 +354,9 @@ def turn_command( if execution.timeout_seconds is not None else []), "--validation-command-json", json.dumps(execution.validation_command), + *(["--epoch-prefetch-json", execution.epoch_prefetch_json] + if execution.epoch_prefetch_json is not None else []), + *(["--quiet-turn"] if execution.quiet_turn else []), "--available-capability", "shell", "--available-capability", @@ -585,6 +590,7 @@ def run_once(env: dict[str, str]) -> dict: if env.get("LOOPX_CONTEXT_DELIVERY", "off") not in {"off", "on"}: raise ValueError("LOOPX_CONTEXT_DELIVERY supports only off or on") display = explicit_turn_display(env) + context_opt_ins = explicit_context_opt_ins(env) execution = Execution( mode=env.get("LOOPX_EXECUTION_MODE", "heartbeat"), context=env.get("LOOPX_ITERATION_CONTEXT"), @@ -598,6 +604,7 @@ def run_once(env: dict[str, str]) -> dict: native_child_limit=(int(env["LOOPX_NATIVE_CHILD_LIMIT"]) if env.get("LOOPX_NATIVE_CHILD_LIMIT") else None), complete_turn=display.complete_turn, + **context_opt_ins, ) request_admission = False if env.get("LOOPX_PROVIDER_REQUEST_ADMISSION", "off") != "off": @@ -645,6 +652,12 @@ def run_once(env: dict[str, str]) -> dict: if observation_producer is not None else {}), **turn_envelope_receipt(explicit=False, complete=execution.complete_turn), **({"context_delivery": "on"} if execution.context_delivery != "off" else {}), + **({"compact_direct_read": True} if execution.compact_direct_read else {}), + **({"quiet_turn": True} if execution.quiet_turn else {}), + **({"epoch_prefetch": True, "epoch_prefetch_configured": True} + if epoch_prefetch_enabled(execution.epoch_prefetch_json) + else ({"epoch_prefetch_configured": False} + if execution.epoch_prefetch_json is not None else {})), **({"native_child_limit": execution.native_child_limit} if execution.native_child_limit is not None else {}), **({"provider_request_admission": "on"} if request_admission else {}), @@ -710,14 +723,23 @@ def run_once(env: dict[str, str]) -> dict: else: checkpoint_wake = _InertCheckpointWake() if execution.context_delivery != "off": - from benchmark.runtime.context_delivery import prepare_context_delivery + from benchmark.runtime.context_delivery import ContextOptInConfigurationError, prepare_context_delivery + explicit_h1_opt_in = ( + execution.compact_direct_read + or execution.quiet_turn + or execution.epoch_prefetch_json is not None + ) try: context_env = dict(env) prepare_context_delivery(context_env, home) env.update(context_env) receipt["context_transport"] = {"available": True} + except ContextOptInConfigurationError: + raise except Exception as exc: + if explicit_h1_opt_in: + raise # Optional presentation cannot block an otherwise admitted # trial. Unavailable native hooks keep ordinary full IO. env.pop("LOOPX_CODEX_CONTEXT_DIR", None) diff --git a/benchmark/tests/test_shared_codex_runtime.py b/benchmark/tests/test_shared_codex_runtime.py index cdca15e337..75a78a734f 100644 --- a/benchmark/tests/test_shared_codex_runtime.py +++ b/benchmark/tests/test_shared_codex_runtime.py @@ -1572,3 +1572,403 @@ def request(method, params): def test_context_benchmark_setting_rejects_broad_or_other_host_activation(value): with pytest.raises(ValueError, match="context_delivery"): Execution(context_delivery=value) + + +def test_h1_opt_in_execution_guardrails_and_codex_settings_parity(tmp_path): + from benchmark.runtime.codex import codex_settings + + default_exec = Execution() + assert default_exec.compact_direct_read is False + assert default_exec.quiet_turn is False + assert default_exec.epoch_prefetch_json is None + + opt_in_exec = Execution( + mode="heartbeat", + context_delivery="on", + compact_direct_read=True, + quiet_turn=True, + epoch_prefetch_json='{"b": 2, "a": 1}', + ) + assert opt_in_exec.epoch_prefetch_json == '{"a":1,"b":2}' + base_cfg = codex_settings(execution=default_exec, workspace=tmp_path, model="fixture", effort="low", + base_url="", wire_api="responses") + opt_cfg = codex_settings(execution=opt_in_exec, workspace=tmp_path, model="fixture", effort="low", + base_url="", wire_api="responses") + # WarmStart seals restore-config.toml digest; opt-ins never mutate config.toml. + assert base_cfg == opt_cfg + + for kwargs in ( + {"mode": "plain", "compact_direct_read": True}, + {"mode": "heartbeat", "context_delivery": "off", "compact_direct_read": True}, + {"mode": "heartbeat", "context_delivery": "off", "quiet_turn": True}, + {"mode": "heartbeat", "context_delivery": "off", "epoch_prefetch_json": '{"enabled":true}'}, + {"mode": "native-goal", "quiet_turn": True}, + {"mode": "loopx-goal", "epoch_prefetch_json": '{"enabled":true}'}, + {"mode": "turn", "validation_command": ("true",), "epoch_prefetch_json": "[1,2]"}, + {"mode": "heartbeat", "compact_direct_read": "1"}, + {"mode": "heartbeat", "quiet_turn": 1}, + ): + with pytest.raises(ValueError): + Execution(**kwargs) + + +def test_prepare_context_delivery_opt_ins_default_off_split_pin_and_hooks_json(tmp_path, monkeypatch): + from benchmark.runtime.context_delivery import ContextOptInConfigurationError, prepare_context_delivery + from loopx.control_plane.turn_driver import codex_context + from loopx.control_plane.turn_driver.codex_stdio import StdioNativeGoalTransport + + captured_calls = [] + real_config = codex_context.codex_context_configuration + + @contextmanager + def fake_discovery(argv, **kwargs): + def request(method, params): + if method == "initialize": + return {} + assert method == "hooks/list" + path = Path(kwargs["env"]["LOOPX_CODEX_HOME"]) / "hooks.json" if "LOOPX_CODEX_HOME" in kwargs.get("env", {}) else home_ref["path"] + configured = json.loads(path.read_text())["hooks"] + return {"data": [{"hooks": [ + {"sourcePath": str(path.resolve()), "eventName": event[0].lower() + event[1:], + "handlerType": h["type"], "command": h["command"], "timeoutSec": h["timeout"], + "matcher": g.get("matcher"), "async": False, "enabled": True, + "key": f"{event}-{i}", "currentHash": f"h-{event}-{i}"} + for event, groups in configured.items() for g in groups for i, h in enumerate(g["hooks"]) + ]}]} + yield SimpleNamespace(request=request, notify=lambda *args: None) + + monkeypatch.setattr(StdioNativeGoalTransport, "spawn", fake_discovery) + + # Simulate split-pinned origin/main codex_context_configuration lacking new kwargs: + # default-off must succeed without passing any new kwargs. + def legacy_config(directory, *, goal_id, agent_id, cli_argv, scenario, session_root=False, + inherit_machine=False, progressive_enabled=True): + captured_calls.append("legacy") + return real_config(directory, goal_id=goal_id, agent_id=agent_id, cli_argv=cli_argv, + scenario=scenario, session_root=session_root, + inherit_machine=inherit_machine, progressive_enabled=progressive_enabled) + + monkeypatch.setattr(codex_context, "codex_context_configuration", legacy_config) + home_off = tmp_path / "home-off" + home_off.mkdir() + home_ref = {"path": home_off / "hooks.json"} + env_off = {"LOOPX_CLI": "loopx", "LOOPX_GOAL_ID": "g", "LOOPX_AGENT_ID": "a", + "CODEX_BIN": "codex", "LOOPX_PROJECT": str(tmp_path)} + prepare_context_delivery(env_off, home_off) + assert captured_calls == ["legacy"] + off_hooks = (home_off / "hooks.json").read_text() + assert "--compact-direct-read" not in off_hooks + assert "--quiet-turn" not in off_hooks + assert "--epoch-prefetch-json" not in off_hooks + + # Explicit H1 opt-in fails closed against legacy signature lacking support. + home_fail = tmp_path / "home-fail" + home_fail.mkdir() + with pytest.raises(ContextOptInConfigurationError, match="compact_direct_read"): + prepare_context_delivery({**env_off, "LOOPX_COMPACT_DIRECT_READ": "1"}, home_fail) + + # Restore real codex_context_configuration -> verifies all 3 opt-in flags reach hooks.json. + monkeypatch.setattr(codex_context, "codex_context_configuration", real_config) + home_on = tmp_path / "home-on" + home_on.mkdir() + home_ref["path"] = home_on / "hooks.json" + env_on = { + **env_off, + "LOOPX_COMPACT_DIRECT_READ": "1", + "LOOPX_QUIET_TURN": "1", + "LOOPX_EPOCH_PREFETCH_JSON": '{"prefetch_command": ["git", "status", "--short"], "enabled": true}', + } + prepare_context_delivery(env_on, home_on) + hooks_json = json.loads((home_on / "hooks.json").read_text())["hooks"] + start_cmd = hooks_json["SessionStart"][0]["hooks"][0]["command"] + assert "--compact-direct-read" in start_cmd + assert "--quiet-turn" in start_cmd + assert "--epoch-prefetch-json" in start_cmd + assert '{"enabled":true,"prefetch_command":["git","status","--short"]}' in start_cmd + + +def test_worker_turn_command_and_run_once_forward_h1_opt_ins_default_off(tmp_path, monkeypatch): + from benchmark.tests.test_resume_sessions import resume_env + from loopx.control_plane.turn_driver.codex_stdio import StdioNativeGoalTransport + + env_turn = {"LOOPX_CLI": "loopx", "LOOPX_REGISTRY": "/r", "LOOPX_RUNTIME_ROOT": "/rt", + "LOOPX_PROJECT": "/p", "LOOPX_GOAL_ID": "g", "LOOPX_AGENT_ID": "a", + "REASONING_EFFORT": "low", "CODEX_BIN": "codex", "MODEL_NAME": "m"} + cmd_off = turn_command(env_turn, Execution(mode="turn", validation_command=("true",)), "w1") + assert "--quiet-turn" not in cmd_off + assert "--epoch-prefetch-json" not in cmd_off + + cmd_on = turn_command( + env_turn, + Execution(mode="turn", validation_command=("true",), quiet_turn=True, + epoch_prefetch_json={"enabled": True, "timeout_ms": 500}), + "w2", + ) + assert "--quiet-turn" in cmd_on + assert cmd_on[cmd_on.index("--epoch-prefetch-json") + 1] == '{"enabled":true,"timeout_ms":500}' + + # Real run_once(heartbeat) end-to-end verification (default-off vs opt-in hooks.json + receipt). + @contextmanager + def fake_spawn(argv, **kwargs): + home_p = Path(kwargs["env"]["LOOPX_CODEX_HOME"]) + def req(method, params): + if method == "initialize": + return {} + path = home_p / "hooks.json" + configured = json.loads(path.read_text())["hooks"] + return {"data": [{"hooks": [ + {"sourcePath": str(path.resolve()), "eventName": e[0].lower() + e[1:], + "handlerType": h["type"], "command": h["command"], "timeoutSec": h["timeout"], + "matcher": g.get("matcher"), "async": False, "enabled": True, + "key": f"{e}-{i}", "currentHash": f"h-{e}-{i}"} + for e, groups in configured.items() for g in groups for i, h in enumerate(g["hooks"]) + ]}]} + yield SimpleNamespace(request=req, notify=lambda *args: None) + + monkeypatch.setattr(StdioNativeGoalTransport, "spawn", fake_spawn) + monkeypatch.setattr("benchmark.runtime.worker.heartbeat_body", lambda *a, **kw: "Task body.") + + off_tmp = tmp_path / "off" + off_tmp.mkdir() + e_off = resume_env(off_tmp, monkeypatch) + e_off["LOOPX_CONTEXT_DELIVERY"] = "on" + res_off = run_once(e_off) + assert res_off["ok"] + assert "compact_direct_read" not in res_off + assert "quiet_turn" not in res_off + assert "epoch_prefetch" not in res_off + assert "--compact-direct-read" not in (Path(e_off["LOOPX_CODEX_HOME"]) / "hooks.json").read_text() + + on_tmp = tmp_path / "on" + on_tmp.mkdir() + e_on = resume_env(on_tmp, monkeypatch) + e_on.update({ + "LOOPX_CONTEXT_DELIVERY": "on", + "LOOPX_COMPACT_DIRECT_READ": "1", + "LOOPX_QUIET_TURN": "1", + "LOOPX_EPOCH_PREFETCH_JSON": '{"enabled": true}', + }) + res_on = run_once(e_on) + assert res_on["ok"] + assert res_on["compact_direct_read"] is True + assert res_on["quiet_turn"] is True + assert res_on["epoch_prefetch"] is True + assert res_on["epoch_prefetch_configured"] is True + hooks_text = (Path(e_on["LOOPX_CODEX_HOME"]) / "hooks.json").read_text() + assert "--compact-direct-read" in hooks_text + assert "--quiet-turn" in hooks_text + assert "--epoch-prefetch-json" in hooks_text + + # Explicitly disabled epoch_prefetch_json must NOT set epoch_prefetch=True in receipt. + dis_tmp = tmp_path / "dis" + dis_tmp.mkdir() + e_dis = resume_env(dis_tmp, monkeypatch) + e_dis.update({"LOOPX_CONTEXT_DELIVERY": "on", "LOOPX_EPOCH_PREFETCH_JSON": '{"enabled": false}'}) + res_dis = run_once(e_dis) + assert res_dis["ok"] + assert "epoch_prefetch" not in res_dis + assert res_dis["epoch_prefetch_configured"] is False + + +def test_harbor_h1_h2_opt_in_worker_env_and_warm_start_yardstick(tmp_path, monkeypatch): + pytest.importorskip("harbor") + from benchmark.runtime.harbor import BenchmarkCodex + from loopx.cli import main + + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("LOOPX_COMPACT_DIRECT_READ", "1") + monkeypatch.setenv("LOOPX_QUIET_TURN", "1") + monkeypatch.setenv("LOOPX_EPOCH_PREFETCH_JSON", '{"enabled":true}') + implicit = BenchmarkCodex(logs_dir=tmp_path / "imp", model_name="openai/fixture") + explicit = BenchmarkCodex(logs_dir=tmp_path / "exp", model_name="openai/fixture", + compact_direct_read=False, quiet_turn=False, + epoch_prefetch_json=None, execution_yardstick_enabled=None) + assert implicit._worker_env(cwd="/task") == explicit._worker_env(cwd="/task") + assert implicit._worker_env(cwd="/task")["LOOPX_COMPACT_DIRECT_READ"] == "0" + assert implicit._worker_env(cwd="/task")["LOOPX_QUIET_TURN"] == "0" + assert implicit._worker_env(cwd="/task")["LOOPX_EPOCH_PREFETCH_JSON"] == "" + assert "yardstick_enabled" not in implicit._replan_receipt() + + # Invalid execution_yardstick_enabled rejections. + for bad in ({"execution_yardstick_enabled": "yes"}, + {"execution_mode": "plain", "execution_yardstick_enabled": True}): + with pytest.raises(ValueError, match="execution_yardstick_enabled"): + BenchmarkCodex(logs_dir=tmp_path / "bad", model_name="openai/fixture", **bad) + + # Cold Harbor H2 without active Explore Graph must fail closed instead of accepting inert yardstick=True. + reg = tmp_path / "reg.json" + rt = tmp_path / "rt" + agent = BenchmarkCodex( + logs_dir=tmp_path / "h2", model_name="openai/fixture", + task_entry="seeded-todo", compact_direct_read=True, quiet_turn=True, + epoch_prefetch_json={"enabled": True}, execution_yardstick_enabled=True, + ) + assert agent._replan_receipt()["yardstick_enabled"] is True + assert agent._turn_envelope_receipt()["compact_direct_read"] is True + assert agent._turn_envelope_receipt()["quiet_turn"] is True + assert agent._turn_envelope_receipt()["epoch_prefetch"] is True + + calls = [] + async def cli(environment, args, **kwargs): + import contextlib + import io + calls.append(args) + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + code = main(["--format", "json", "--registry", str(reg), "--runtime-root", str(rt), *args]) + assert code == 0, buf.getvalue() + return json.loads(buf.getvalue()) + + async def no_pending(**kwargs): + return SimpleNamespace(return_code=1) + + async def noop(*a, **kw): + return None + + async def reg_exists(*a, **kw): + return reg.is_file() and reg.stat().st_size > 0 + + monkeypatch.setattr(agent, "_loopx", cli) + monkeypatch.setattr(agent, "_write_task_document", noop) + monkeypatch.setattr(agent, "_registry_exists", reg_exists) + with pytest.raises(RuntimeError, match="active Explore evidence graph"): + asyncio.run(agent._prepare_phase(SimpleNamespace(exec=no_pending), "Task 1", cwd=str(tmp_path))) + + # Enabling Explore Graph on the Goal allows cold/existing-registry and WarmStart H2 readback to pass, + # verifying real `explore turn-context` exposes `graph_enabled` and `work_yardstick_capture`. + asyncio.run(cli(None, ["configure-goal", "--goal-id", agent.goal_id, "--explore-graph-enabled", "--execute"])) + asyncio.run(agent._prepare_phase(SimpleNamespace(exec=no_pending), "Task 1", cwd=str(tmp_path))) + turn_ctx = asyncio.run(cli(None, ["explore", "turn-context", "--goal-id", agent.goal_id, "--agent-id", agent.agent_id])) + assert turn_ctx["graph_enabled"] is True + assert isinstance(turn_ctx["graph"]["work_yardstick_capture"], dict) + + # Reset yardstick externally, then simulate WarmStart / existing registry Phase 2 -> must re-apply! + asyncio.run(cli(None, ["configure-goal", "--goal-id", agent.goal_id, "--no-execution-yardstick-enabled", "--execute"])) + asyncio.run(agent._prepare_phase(SimpleNamespace(exec=no_pending), "Task 2", cwd=str(tmp_path))) + readback = asyncio.run(cli(None, ["configure-goal", "--goal-id", agent.goal_id])) + assert readback["after"]["execution_profile"]["yardstick_enabled"] is True + assert any("--execution-yardstick-enabled" in c for c in calls) + + # Explicit execution_yardstick_enabled=False persists cleanly without requiring Explore. + agent_off = BenchmarkCodex( + logs_dir=tmp_path / "h2-off", model_name="openai/fixture", + task_entry="seeded-todo", execution_yardstick_enabled=False, + ) + monkeypatch.setattr(agent_off, "_loopx", cli) + monkeypatch.setattr(agent_off, "_write_task_document", noop) + monkeypatch.setattr(agent_off, "_registry_exists", reg_exists) + asyncio.run(agent_off._prepare_phase(SimpleNamespace(exec=no_pending), "Task off", cwd=str(tmp_path))) + off_readback = asyncio.run(cli(None, ["configure-goal", "--goal-id", agent_off.goal_id])) + assert off_readback["after"]["execution_profile"]["yardstick_enabled"] is False + + # Readback mismatch must fail closed. + async def stale_cli(environment, args, **kwargs): + return {"after": {"execution_profile": { + "replan_after_effective_turns": 6, "planning_horizon_goal": True, + "stride_strategy": "off", "yardstick_enabled": False, + }}} + monkeypatch.setattr(agent, "_loopx", stale_cli) + with pytest.raises(RuntimeError, match="execution_yardstick_enabled readback mismatch"): + asyncio.run(agent._verify_phase_configuration(SimpleNamespace(), cwd=str(tmp_path))) + + +def test_sforge_worker_h2_warm_start_and_cold_explore_yardstick(tmp_path, monkeypatch): + pytest.importorskip("sforge") + pytest.importorskip("harbor") + from sforge.harness.config import SForgeConfig + from benchmark.runtime.sforge import SForgeWorker + + monkeypatch.chdir(tmp_path) + auth_file = tmp_path / "auth.json" + auth_file.write_text("{}", encoding="utf-8") + monkeypatch.setenv("CODEX_AUTH_JSON_PATH", str(auth_file)) + monkeypatch.setattr("benchmark.runtime.harbor.BenchmarkCodex.install", lambda self, env: asyncio.sleep(0)) + monkeypatch.setattr(SForgeWorker, "_warm_command", lambda *a, **kw: None) + + worker = SForgeWorker( + SForgeConfig(agent_model="fixture", agent_effort="xhigh"), + profile="heartbeat-explore", + cwd=str(tmp_path), + execution_yardstick_enabled=True, + ) + reg = tmp_path / "sforge-reg.json" + rt = tmp_path / "sforge-rt" + calls = [] + + async def cli(environment, args, **kwargs): + import contextlib + import io + from loopx.cli import main + calls.append(args) + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + code = main(["--format", "json", "--registry", str(reg), "--runtime-root", str(rt), *args]) + assert code == 0, buf.getvalue() + return json.loads(buf.getvalue()) + + async def noop(*a, **kw): + return None + + async def reg_exists(*a, **kw): + return reg.is_file() and reg.stat().st_size > 0 + + class DummyEnv: + async def exec(self, command, **_kw): + if command.startswith("cat "): + return SimpleNamespace(return_code=0, stdout="Solve the task.\n") + return SimpleNamespace(return_code=1, stdout="") + + async def upload_file(self, *a, **kw): + return None + + worker.backend = SimpleNamespace( + exec_run=lambda *a, **kw: SimpleNamespace(exit_code=0, output=""), + exec_run_with_exit_code=lambda *a, **kw: SimpleNamespace(timed_out=False, exit_code=0, output=""), + copy_to_container=lambda *a: None, + ) + logs = tmp_path / "logs" + logs.mkdir() + worker.install_stop_hook(worker.backend, None, logs, None) + worker.runtime.environment = DummyEnv() + worker.environment = DummyEnv() + monkeypatch.setattr(worker.runtime, "_loopx", cli) + monkeypatch.setattr(worker.runtime, "_write_task_document", noop) + monkeypatch.setattr(worker.runtime, "_registry_exists", reg_exists) + worker.format_run_cmd(str(tmp_path / "prompt.md"), model="fixture", cwd=str(tmp_path)) + assert (logs / "explore-activation.json").is_file() + readback = asyncio.run(cli(None, ["configure-goal", "--goal-id", worker.runtime.goal_id])) + assert readback["after"]["execution_profile"]["yardstick_enabled"] is True + + # Simulate warm_start forwarding --execution-yardstick-enabled on restore. + asyncio.run(cli(None, ["configure-goal", "--goal-id", worker.runtime.goal_id, "--no-execution-yardstick-enabled", "--execute"])) + warm = SimpleNamespace( + goal_id=worker.runtime.goal_id, + agent_id=worker.runtime.agent_id, + stage=lambda *a, **kw: asyncio.sleep(0, result=()), + research_sidecar=False, + native_child_limit=None, + deadline_epoch=time.time() + 600, + verify=lambda _cwd: None, + source_context_dir=lambda: tmp_path / "src-ctx", + ) + (tmp_path / "src-ctx").mkdir() + worker_warm = SForgeWorker( + SForgeConfig(agent_model="fixture", agent_effort="xhigh"), + profile="heartbeat-explore", + cwd=str(tmp_path), + task_entry="loopx-planned", + warm_start=warm, + execution_yardstick_enabled=True, + ) + worker_warm.backend = worker.backend + logs_warm = tmp_path / "logs-warm" + logs_warm.mkdir() + worker_warm.install_stop_hook(worker_warm.backend, None, logs_warm, None) + worker_warm.environment = DummyEnv() + worker_warm.runtime.environment = DummyEnv() + monkeypatch.setattr(worker_warm.runtime, "_loopx", cli) + monkeypatch.setattr(worker_warm.runtime, "_write_task_document", noop) + monkeypatch.setattr(worker_warm.runtime, "_registry_exists", reg_exists) + worker_warm.format_run_cmd(str(tmp_path / "prompt.md"), model="fixture", cwd=str(tmp_path)) + readback2 = asyncio.run(cli(None, ["configure-goal", "--goal-id", worker_warm.runtime.goal_id])) + assert readback2["after"]["execution_profile"]["yardstick_enabled"] is True diff --git a/loopx/cli_commands/quota_native_context.py b/loopx/cli_commands/quota_native_context.py index 94a2a4f568..5445a4a293 100644 --- a/loopx/cli_commands/quota_native_context.py +++ b/loopx/cli_commands/quota_native_context.py @@ -7,7 +7,7 @@ import stat import subprocess import tempfile -from collections.abc import Callable +from collections.abc import Callable, Mapping from contextlib import AbstractContextManager, nullcontext from functools import partial from pathlib import Path @@ -373,7 +373,9 @@ def _handle_context_config( config = codex_context_configuration(directory, goal_id=args.goal_id, agent_id=args.agent_id, cli_argv=cli_argv, scenario=activation["scenario"], session_root=bool(getattr(args, "codex_context_root", None)), inherit_machine=bool(machine_runtime), progressive_enabled=activation["progressive_enabled"], - compact_direct_read=bool(getattr(args, "compact_direct_read", False))) + compact_direct_read=bool(getattr(args, "compact_direct_read", False)), + quiet_turn=bool(getattr(args, "quiet_turn", False)), + epoch_prefetch=getattr(args, "epoch_prefetch_json", None)) explicit_options = "--context-delivery on" if activation["progressive_enabled"] else "--context-delivery off --delta-delivery auto" payload = {"ok": True, "context_delivery": activation, "native_config": config, "activation": "Apply config_overrides, merge hooks and append developer_instructions to the existing native configuration; " @@ -429,8 +431,7 @@ def _handle_context_hook(args: argparse.Namespace, *, activation: dict[str, Any] raise ValueError("context directory unavailable") scenario = activation["scenario"] progressive_enabled = activation["progressive_enabled"] - if getattr(args, "compact_direct_read", False) and "compact_direct_read" not in event: - event = {**event, "compact_direct_read": True} + event = _inject_context_hook_opt_ins(args, event) payload = codex_context_hook(directory, event) request = None preparation_context = "" @@ -528,6 +529,43 @@ def _handle_context_hook(args: argparse.Namespace, *, activation: dict[str, Any] return 0 +def _inject_context_hook_opt_ins(args: argparse.Namespace, event: dict[str, Any]) -> dict[str, Any]: + """Attach explicit default-off CLI hook flags absent from stdin event JSON.""" + updated = dict(event) + if getattr(args, "compact_direct_read", False) and "compact_direct_read" not in updated: + updated["compact_direct_read"] = True + if getattr(args, "quiet_turn", False) and "quiet_turn" not in updated: + updated["quiet_turn"] = True + epoch_prefetch = getattr(args, "epoch_prefetch_json", None) + if isinstance(epoch_prefetch, Mapping) and "epoch_prefetch" not in updated: + updated["epoch_prefetch"] = dict(epoch_prefetch) + return updated + + +def _validate_context_hook_opt_ins(args: argparse.Namespace, print_payload: Callable[..., None]) -> int | None: + """Enforce closed subcommand & shape validation for explicit context hook opt-ins.""" + hook_cmd = args.quota_command in ("context-config", "context-hook") + for flag, attr in ( + ("--compact-direct-read", "compact_direct_read"), + ("--quiet-turn", "quiet_turn"), + ): + if getattr(args, attr, False) and not hook_cmd: + print_payload({"ok": False, "reason": f"{flag} is only supported by context-config or context-hook"}, + args.format, str) + return 1 + epoch_prefetch = getattr(args, "epoch_prefetch_json", None) + if epoch_prefetch is not None: + if not hook_cmd: + print_payload({"ok": False, "reason": "--epoch-prefetch-json is only supported by context-config or context-hook"}, + args.format, str) + return 1 + if not isinstance(epoch_prefetch, dict): + print_payload({"ok": False, "reason": "--epoch-prefetch-json must decode to a JSON object"}, + args.format, str) + return 1 + return None + + def handle_native_context_command(args: argparse.Namespace, *, print_payload: Callable[..., None], registry_path: Path | None = None, runtime_root_arg: str | None = None) -> int | None: """Dispatch captured reads and native lifecycle IO before quota authority access.""" @@ -537,10 +575,9 @@ def handle_native_context_command(args: argparse.Namespace, *, print_payload: Ca print_payload({"ok": False, "reason": "--prepared-claim-operation-id requires quota context-project"}, args.format, str) return 1 - if getattr(args, "compact_direct_read", False) and args.quota_command not in ("context-config", "context-hook"): - print_payload({"ok": False, "reason": "--compact-direct-read is only supported by context-config or context-hook"}, - args.format, str) - return 1 + opt_in_rc = _validate_context_hook_opt_ins(args, print_payload) + if opt_in_rc is not None: + return opt_in_rc if getattr(args, "complete_turn", False) and (args.quota_command != "should-run" or args.format != "json"): print_payload({"ok": False, "reason": "--complete-turn requires JSON quota should-run"}, args.format, str) return 1 diff --git a/loopx/cli_commands/quota_registration.py b/loopx/cli_commands/quota_registration.py index b821cd1c60..232465bc83 100644 --- a/loopx/cli_commands/quota_registration.py +++ b/loopx/cli_commands/quota_registration.py @@ -1,6 +1,7 @@ from __future__ import annotations import argparse +import json from ..control_plane.quota.spend_sources import ( DEFAULT_SLOT_SPEND_SOURCE, @@ -275,6 +276,10 @@ def register_quota_command( "context-config renders native configuration for the exact Goal/Agent; no host configuration, trust, guard or spend are changed.") quota_parser.add_argument("--compact-direct-read", action="store_true", help="Opt-in Phase-2 lock-free in-process JSON pointer read, wake deduplication, and automatic failure/obligation extraction for native compact hooks.") + quota_parser.add_argument("--quiet-turn", action="store_true", + help="Opt-in Quiet-Turn zero-tax skip metadata evaluation for native context-config/context-hook (default-off).") + quota_parser.add_argument("--epoch-prefetch-json", type=json.loads, default=None, + help="Opt-in Stage-then-Swap EpochReset prefetch configuration JSON for native context-config/context-hook (default-off).") quota_parser.add_argument("--capture-sha256", help="For context-read, context-project or replan-view: exact SHA-256 supplied with the capture reference.") quota_parser.add_argument("--initial-read", action="store_true", diff --git a/loopx/control_plane/turn_driver/codex_context.py b/loopx/control_plane/turn_driver/codex_context.py index 5def9deb34..e4ad73d6d7 100644 --- a/loopx/control_plane/turn_driver/codex_context.py +++ b/loopx/control_plane/turn_driver/codex_context.py @@ -327,7 +327,9 @@ def codex_context_recovery_instructions(directory: str, *, goal_id: str, agent_i def codex_context_configuration(directory: str, *, goal_id: str, agent_id: str, cli_argv: list[str], scenario: str, session_root: bool = False, inherit_machine: bool = False, progressive_enabled: bool = True, - compact_direct_read: bool = False) -> dict[str, Any]: + compact_direct_read: bool = False, + quiet_turn: bool = False, + epoch_prefetch: Mapping[str, Any] | None = None) -> dict[str, Any]: """Native configuration data shared by the worker and CLI setup journey. This renders no trust hashes and writes no host configuration. The native @@ -340,6 +342,15 @@ def codex_context_configuration(directory: str, *, goal_id: str, agent_id: str, raise ValueError("unsupported native context configuration scenario") if not goal_id or not agent_id: raise ValueError("context configuration requires exact goal-id and agent-id") + if type(compact_direct_read) is not bool or type(quiet_turn) is not bool: + raise ValueError("compact_direct_read and quiet_turn require explicit booleans") + canonical_epoch_prefetch: str | None = None + if epoch_prefetch is not None: + if not isinstance(epoch_prefetch, Mapping): + raise ValueError("epoch_prefetch must be a JSON object") + canonical_epoch_prefetch = json.dumps( + dict(epoch_prefetch), ensure_ascii=False, sort_keys=True, separators=(",", ":"), + ) directory = str(_private_root(directory)) option = "--codex-context-root" if session_root else "--codex-context-dir" activation_args = ["--context-delivery", "on"] if progressive_enabled else [ @@ -348,6 +359,9 @@ def codex_context_configuration(directory: str, *, goal_id: str, agent_id: str, *cli_argv, "--format", "json", "quota", "context-hook", *([] if inherit_machine else activation_args), *(["--compact-direct-read"] if compact_direct_read else []), + *(["--quiet-turn"] if quiet_turn else []), + *(["--epoch-prefetch-json", canonical_epoch_prefetch] + if canonical_epoch_prefetch is not None else []), *(["--codex-app"] if scenario == "codex_app_heartbeat" else ["--runtime-profile", "codex_app_ssh_goal" if scenario == "codex_app_ssh_goal" else "generic_cli"]), option, directory]), "timeout": 10} @@ -432,6 +446,79 @@ def _reduce_hook_lifecycle( raise +def _epoch_prefetch_is_configured(state: Mapping[str, Any], event: Mapping[str, Any]) -> bool: + """Return True only when Stage-then-Swap EpochReset prefetch is explicitly enabled.""" + cfg = ( + event.get("epoch_prefetch") + if isinstance(event.get("epoch_prefetch"), Mapping) + else state.get("epoch_prefetch_policy") + if isinstance(state.get("epoch_prefetch_policy"), Mapping) + else None + ) + return isinstance(cfg, Mapping) and cfg.get("enabled") is True + + +def _compose_direct_read_and_epoch_prefetch( + direct_reply: dict[str, Any] | None, + prefetch_hook: dict[str, Any] | None, + *, + hook_name: Any, + epoch_prefetch_configured: bool, + recovery_needed: bool, + has_recovery_command: bool, +) -> dict[str, Any] | None: + """Keep compact direct-read and EpochReset prefetch composition explicit in hook receipts.""" + if direct_reply is None: + return prefetch_hook + direct_out = dict(direct_reply.get("hookSpecificOutput") or {}) + if prefetch_hook is None: + if epoch_prefetch_configured: + reason = ( + "post_compact_defers_to_session_start" + if hook_name != "SessionStart" + else "recovery_not_needed" + if not recovery_needed + else "missing_recovery_command" + if not has_recovery_command + else "no_prefetch_receipt" + ) + direct_out["epochPrefetch"] = { + "schema_version": "loopx_epoch_prefetch_receipt_v0", + "configured": True, + "executed": False, + "replayed": False, + "superseded": hook_name == "SessionStart", + "superseded_by": "compact_direct_read" if hook_name == "SessionStart" else None, + "operator": "KeepRaw", + "status": "superseded_by_compact_direct_read" + if hook_name == "SessionStart" + else "deferred_until_session_start", + "reason": reason, + } + return {"hookSpecificOutput": direct_out} + prefetch_out = dict(prefetch_hook.get("hookSpecificOutput") or {}) + parts = [ + part + for part in (direct_out.get("additionalContext"), prefetch_out.get("additionalContext")) + if isinstance(part, str) and part.strip() + ] + composed: dict[str, Any] = { + "hookEventName": hook_name, + **({"additionalContext": "\n\n".join(parts)} if parts else {}), + "compactDirectRead": { + **dict(direct_out.get("compactDirectRead") or {}), + "composed_with_epoch_prefetch": True, + }, + "epochPrefetch": { + **dict(prefetch_out.get("epochPrefetch") or {}), + "configured": True, + "superseded": False, + "composed_with_compact_direct_read": True, + }, + } + return {"hookSpecificOutput": composed} + + def _maybe_resolve_compact_direct_read( root: Path, updated: dict[str, Any], @@ -583,8 +670,8 @@ def codex_context_hook(directory: str | None, event: dict[str, Any]) -> dict[str raise except (OSError, ValueError, TypeError, RecursionError, EffectRuntimeRemoteError, EffectRuntimeStartupError): recovery_needed = True - if direct_reply is not None: - return direct_reply + epoch_prefetch_configured = _epoch_prefetch_is_configured(state, event) + prefetch_hook: dict[str, Any] | None = None if (name == "SessionStart" and recovery_needed and isinstance(recovery, dict) and recovery.get("command")): from .epoch_prefetch import resolve_codex_context_epoch_prefetch_hook @@ -606,6 +693,17 @@ def codex_context_hook(directory: str | None, event: dict[str, Any]) -> dict[str + CONTEXT_CAPTURE_PAGE_GUIDANCE ), ) + if direct_reply is not None: + return _compose_direct_read_and_epoch_prefetch( + direct_reply, + prefetch_hook, + hook_name=name, + epoch_prefetch_configured=epoch_prefetch_configured, + recovery_needed=recovery_needed, + has_recovery_command=bool(isinstance(recovery, dict) and recovery.get("command")), + ) or {} + if (name == "SessionStart" and recovery_needed + and isinstance(recovery, dict) and recovery.get("command")): if prefetch_hook is not None: return prefetch_hook return {"hookSpecificOutput": {"hookEventName": name, "additionalContext": diff --git a/loopx/control_plane/turn_driver/epoch_prefetch.py b/loopx/control_plane/turn_driver/epoch_prefetch.py index 16f0f06ca9..ae74b6573d 100644 --- a/loopx/control_plane/turn_driver/epoch_prefetch.py +++ b/loopx/control_plane/turn_driver/epoch_prefetch.py @@ -1452,6 +1452,8 @@ def resolve_codex_context_epoch_prefetch_hook( return None epoch_meta = { "schema_version": receipt["schema_version"], + "configured": True, + "superseded": False, "operator": receipt["operator"], "status": receipt["status"], "fallback_reason": receipt.get("fallback_reason"), diff --git a/tests/control_plane/test_codex_hook_direct_read.py b/tests/control_plane/test_codex_hook_direct_read.py index 6eb249a36a..facc3461a2 100644 --- a/tests/control_plane/test_codex_hook_direct_read.py +++ b/tests/control_plane/test_codex_hook_direct_read.py @@ -475,3 +475,294 @@ def test_default_off_parity_and_zero_post_tool_friction_and_cli_opt_in( assert cli_out["compactDirectRead"]["source_sha256"] == digest assert "todo_off" in cli_out["additionalContext"] assert not registry.exists() and not runtime.exists() + + +def test_quota_context_config_and_hook_opt_ins_quiet_turn_and_epoch_prefetch( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + """Verify --quiet-turn & --epoch-prefetch-json stay off by default and activate cleanly.""" + import os + from loopx.cli import main + + journal_dir = tmp_path / "ctx" + journal_dir.mkdir(mode=0o700) + monkeypatch.setenv("HOME", str(tmp_path)) + monkeypatch.setenv("LOOPX_EXECUTION_MODE", "heartbeat") + + def _run_cli(argv: list[str]) -> tuple[int, dict]: + import contextlib + import io + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + code = main(["--format", "json", "--registry", str(tmp_path / "r.json"), "--runtime-root", str(tmp_path / "rt"), *argv]) + return code, json.loads(buf.getvalue()) + + c_default, p_default = _run_cli([ + "quota", "context-config", + "--goal-id", "g1", "--agent-id", "a1", + "--context-delivery", "on", "--codex-app", + "--codex-context-dir", str(journal_dir), + ]) + assert c_default == 0 and p_default["ok"] is True, p_default + default_cmd = p_default["native_config"]["hooks"]["SessionStart"][0]["hooks"][0]["command"] + assert "--compact-direct-read" not in default_cmd + assert "--quiet-turn" not in default_cmd + assert "--epoch-prefetch-json" not in default_cmd + + prefetch_cfg = {"enabled": True, "prefetch_command": ["git", "status", "--short"]} + c_opt, p_opt = _run_cli([ + "quota", "context-config", + "--goal-id", "g1", "--agent-id", "a1", + "--context-delivery", "on", "--codex-app", + "--compact-direct-read", "--quiet-turn", + "--epoch-prefetch-json", json.dumps(prefetch_cfg), + "--codex-context-dir", str(journal_dir), + ]) + assert c_opt == 0 and p_opt["ok"] is True, p_opt + opt_cmd = p_opt["native_config"]["hooks"]["SessionStart"][0]["hooks"][0]["command"] + assert "--compact-direct-read" in opt_cmd + assert "--quiet-turn" in opt_cmd + assert "--epoch-prefetch-json" in opt_cmd + + # Reject context-hook flags on unrelated quota subcommands cleanly. + c_bad, p_bad = _run_cli(["quota", "should-run", "--goal-id", "g1", "--quiet-turn"]) + assert c_bad == 1 and "context-config or context-hook" in p_bad["reason"] + c_bad2, p_bad2 = _run_cli(["quota", "should-run", "--goal-id", "g1", "--epoch-prefetch-json", '{"enabled":true}']) + assert c_bad2 == 1 and "context-config or context-hook" in p_bad2["reason"] + + # Real CLI subprocess quota context-hook with --quiet-turn on unchanged digest. + capture = tmp_path / "capture.json" + capture.write_bytes(b'{"goal_id":"g1","selected_todo":{"todo_id":"todo_1","status":"open"}}') + digest = hashlib.sha256(capture.read_bytes()).hexdigest() + from loopx.control_plane.turn_driver import codex_context as ctx + ctx._save(journal_dir, { + "session_id": "s-qt", + "cwd": str(tmp_path), + "context_epoch": "epoch-1", + "candidate": {"epoch": "epoch-1"}, + "recovery": {"path": str(capture), "sha256": digest, "command": "loopx quota context-read"}, + }) + worktree_root = str(Path(__file__).resolve().parents[2]) + env_clean = {k: v for k, v in os.environ.items() if k != "LOOPX_CODEX_HOOK_DIRECT_READ"} + env_clean["HOME"] = str(tmp_path) + env_clean["PYTHONPATH"] = worktree_root + hook_cmd = [ + sys.executable, "-m", "loopx.entrypoint", + "--registry", str(tmp_path / "absent.json"), + "--runtime-root", str(tmp_path / "absent-rt"), + "--format", "json", "quota", "context-hook", + "--context-delivery", "on", "--codex-app", "--quiet-turn", + "--codex-context-dir", str(journal_dir), + ] + evt_hook = {"session_id": "s-qt", "cwd": str(tmp_path), "hook_event_name": "PostCompact"} + subprocess.run(hook_cmd, input=json.dumps(evt_hook), capture_output=True, text=True, env=env_clean, check=True) + r2 = subprocess.run(hook_cmd, input=json.dumps(evt_hook), capture_output=True, text=True, env=env_clean, check=True) + second_out = json.loads(r2.stdout) + assert second_out.get("hookSpecificOutput", {}).get("quietTurn", {}).get("skipped") is True + + # Real CLI subprocess quota context-hook with --epoch-prefetch-json on compact recovery. + prefetch_repo = tmp_path / "prefetch-repo" + prefetch_repo.mkdir() + subprocess.run(["git", "init"], cwd=prefetch_repo, capture_output=True, check=True) + large_capture = tmp_path / "large-capture.json" + large_capture.write_text(json.dumps({ + "goal_id": "g1", + "selected_todo": {"todo_id": "todo_1", "status": "open"}, + "padding": "x" * 2048, + })) + large_digest = hashlib.sha256(large_capture.read_bytes()).hexdigest() + prefetch_dir = tmp_path / "ctx-prefetch" + prefetch_dir.mkdir(mode=0o700) + ctx._save(prefetch_dir, { + "session_id": "s-ep", + "cwd": str(prefetch_repo), + "context_epoch": "epoch-9", + "recovery": {"path": str(large_capture), "sha256": large_digest, "command": "loopx quota context-read"}, + }) + ep_cfg = { + "enabled": True, + "prefetch_command": ["git", "status", "--short"], + "baseline_bytes": large_capture.stat().st_size, + "min_net_savings_bytes": 1, + } + ep_cmd = [ + sys.executable, "-m", "loopx.entrypoint", + "--registry", str(tmp_path / "absent2.json"), + "--runtime-root", str(tmp_path / "absent2-rt"), + "--format", "json", "quota", "context-hook", + "--context-delivery", "on", "--codex-app", + "--epoch-prefetch-json", json.dumps(ep_cfg), + "--codex-context-dir", str(prefetch_dir), + ] + ep_evt = {"session_id": "s-ep", "cwd": str(prefetch_repo), "hook_event_name": "SessionStart", "source": "compact"} + r_ep = subprocess.run( + ep_cmd, input=json.dumps(ep_evt), capture_output=True, text=True, + env=env_clean, cwd=prefetch_repo, check=True, + ) + ep_out = json.loads(r_ep.stdout)["hookSpecificOutput"] + assert ep_out["epochPrefetch"]["operator"] == "EpochReset" + assert ep_out["epochPrefetch"]["bytes_saved"] > 0 + + +def test_combined_compact_direct_read_and_epoch_prefetch_generated_hook_lifecycle( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + """Generated combined H1 hook executes both direct read and EpochReset prefetch, never silently dropping either.""" + import os + import shlex + from loopx.cli import main + from loopx.control_plane.turn_driver import codex_context as ctx + + repo = tmp_path / "repo" + repo.mkdir() + subprocess.run(["git", "init"], cwd=repo, capture_output=True, check=True) + (repo / "tracked.txt").write_text("base\n", encoding="utf-8") + subprocess.run(["git", "add", "tracked.txt"], cwd=repo, capture_output=True, check=True) + subprocess.run( + ["git", "-c", "user.name=Test", "-c", "user.email=test@example.com", "commit", "-m", "init"], + cwd=repo, capture_output=True, check=True, + ) + (repo / "dirty.txt").write_text("dirty\n", encoding="utf-8") + + capture = tmp_path / "combo-capture.json" + capture.write_text(json.dumps({ + "goal_id": "g-combo", + "selected_todo": {"todo_id": "todo_combo", "title": "Preserve direct read and prefetch"}, + "padding": "y" * 2048, + }), encoding="utf-8") + capture.chmod(0o600) + digest = hashlib.sha256(capture.read_bytes()).hexdigest() + + journal_dir = tmp_path / "ctx-combo" + journal_dir.mkdir(mode=0o700) + ctx._save(journal_dir, { + "session_id": "s-combo", + "cwd": str(repo), + "context_epoch": "epoch-combo-1", + "goal_id": "g-combo", + "agent_id": "a1", + "recovery": {"path": str(capture), "sha256": digest, "command": f"loopx quota context-read --capture-file {capture}"}, + }) + + ep_cfg = { + "enabled": True, + "prefetch_command": ["git", "status", "--short"], + "baseline_bytes": capture.stat().st_size, + "min_net_savings_bytes": 1, + } + monkeypatch.setenv("HOME", str(tmp_path)) + monkeypatch.setenv("LOOPX_EXECUTION_MODE", "heartbeat") + + import contextlib + import io + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + rc = main([ + "--format", "json", + "--registry", str(tmp_path / "r.json"), + "--runtime-root", str(tmp_path / "rt"), + "quota", "context-config", + "--goal-id", "g-combo", "--agent-id", "a1", + "--context-delivery", "on", "--codex-app", + "--compact-direct-read", "--quiet-turn", + "--epoch-prefetch-json", json.dumps(ep_cfg), + "--codex-context-dir", str(journal_dir), + ]) + assert rc == 0 + hooks = json.loads(buf.getvalue())["native_config"]["hooks"] + cmd_tokens = shlex.split(hooks["SessionStart"][0]["hooks"][0]["command"]) + worktree_root = str(Path(__file__).resolve().parents[2]) + hook_cmd = [ + sys.executable, "-m", "loopx.entrypoint", + "--registry", str(tmp_path / "absent-combo.json"), + "--runtime-root", str(tmp_path / "absent-combo-rt"), + *cmd_tokens[cmd_tokens.index("--format"):], + ] + env_clean = {k: v for k, v in os.environ.items() if k != "LOOPX_CODEX_HOOK_DIRECT_READ"} + env_clean["HOME"] = str(tmp_path) + env_clean["PYTHONPATH"] = worktree_root + + # 1. PostCompact records compactDirectRead AND explicit deferral metadata for epochPrefetch. + r_post = subprocess.run( + hook_cmd, + input=json.dumps({"session_id": "s-combo", "cwd": str(repo), "hook_event_name": "PostCompact"}), + capture_output=True, text=True, env=env_clean, cwd=repo, check=True, + ) + post_out = json.loads(r_post.stdout)["hookSpecificOutput"] + assert post_out["compactDirectRead"]["operator"] == "EpochReset" + assert post_out["epochPrefetch"]["configured"] is True + assert post_out["epochPrefetch"]["executed"] is False + assert post_out["epochPrefetch"]["reason"] == "post_compact_defers_to_session_start" + + # 2. SessionStart(compact) runs BOTH: compactDirectRead (deduped) AND real EpochReset prefetch! + start_evt = {"session_id": "s-combo", "cwd": str(repo), "hook_event_name": "SessionStart", "source": "compact"} + r_start = subprocess.run( + hook_cmd, input=json.dumps(start_evt), capture_output=True, text=True, + env=env_clean, cwd=repo, check=True, + ) + start_out = json.loads(r_start.stdout)["hookSpecificOutput"] + assert start_out["compactDirectRead"]["composed_with_epoch_prefetch"] is True + assert start_out["epochPrefetch"]["configured"] is True + assert start_out["epochPrefetch"]["superseded"] is False + assert start_out["epochPrefetch"]["composed_with_compact_direct_read"] is True + assert start_out["epochPrefetch"]["operator"] == "EpochReset" + assert start_out["epochPrefetch"]["executed"] is True + assert start_out["epochPrefetch"]["replayed"] is False + assert start_out["epochPrefetch"]["bytes_saved"] > 0 + assert "dirty.txt" in start_out["additionalContext"] + + # 3. Replay on same epoch NEVER re-executes prefetch_command. + r_replay = subprocess.run( + hook_cmd, input=json.dumps(start_evt), capture_output=True, text=True, + env=env_clean, cwd=repo, check=True, + ) + replay_out = json.loads(r_replay.stdout)["hookSpecificOutput"] + assert replay_out["epochPrefetch"]["replayed"] is True + assert replay_out["epochPrefetch"]["executed"] is False + assert replay_out["epochPrefetch"]["superseded"] is False + + # 3b. Direct SessionStart(compact) without prior PostCompact includes BOTH pointer payload and EpochReset output. + ctx._save(journal_dir, { + "session_id": "s-combo", + "cwd": str(repo), + "context_epoch": "epoch-combo-direct-start", + "goal_id": "g-combo", + "agent_id": "a1", + "recovery": {"path": str(capture), "sha256": digest, "command": f"loopx quota context-read --capture-file {capture}"}, + }) + r_direct_start = subprocess.run( + hook_cmd, input=json.dumps(start_evt), capture_output=True, text=True, + env=env_clean, cwd=repo, check=True, + ) + direct_start_out = json.loads(r_direct_start.stdout)["hookSpecificOutput"] + assert direct_start_out["compactDirectRead"]["operator"] == "EpochReset" + assert direct_start_out["compactDirectRead"]["deduplicated"] is False + assert direct_start_out["epochPrefetch"]["operator"] == "EpochReset" + assert direct_start_out["epochPrefetch"]["executed"] is True + assert direct_start_out["epochPrefetch"]["superseded"] is False + assert "todo_combo" in direct_start_out["additionalContext"] + assert "dirty.txt" in direct_start_out["additionalContext"] + + # 4. Failing epoch prefetch falls back cleanly to KeepRaw while preserving direct-read output. + ctx._save(journal_dir, { + "session_id": "s-combo", + "cwd": str(repo), + "context_epoch": "epoch-combo-fail", + "goal_id": "g-combo", + "agent_id": "a1", + "recovery": {"path": str(capture), "sha256": digest, "command": f"loopx quota context-read --capture-file {capture}"}, + }) + fail_ep = {"enabled": True, "prefetch_command": ["cat", str(repo / "missing-file.txt")], "baseline_bytes": 4096} + fail_cmd = list(hook_cmd) + fail_cmd[fail_cmd.index("--epoch-prefetch-json") + 1] = json.dumps(fail_ep) + r_fail = subprocess.run( + fail_cmd, input=json.dumps(start_evt), capture_output=True, text=True, + env=env_clean, cwd=repo, check=True, + ) + fail_out = json.loads(r_fail.stdout)["hookSpecificOutput"] + assert fail_out["compactDirectRead"]["operator"] == "EpochReset" + assert fail_out["epochPrefetch"]["configured"] is True + assert fail_out["epochPrefetch"]["superseded"] is False + assert fail_out["epochPrefetch"]["operator"] == "KeepRaw" + assert "todo_combo" in fail_out["additionalContext"] + assert "Prefetch fallback notice (KeepRaw)" in fail_out["additionalContext"]