Skip to content

Commit 0f305a3

Browse files
authored
Merge pull request #9905 from loopx-project/codex/turn-host-input-owner-20261202
Measure Turn input through the selected host transport
2 parents 78fa4fb + b655f64 commit 0f305a3

6 files changed

Lines changed: 126 additions & 80 deletions

File tree

‎loopx/cli_commands/turn_run_once.py‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
run_codex_cli_host,
4848
run_loopx_turn_once,
4949
)
50+
from ..control_plane.turn_driver.host_input import resolve_host_input_renderer
5051
from ..control_plane.turn_driver.quiet_turn_store import build_cli_quiet_turn_option
5152
from ..control_plane.turn_driver.transaction import LoopXTurnResultKind
5253
from ..quota import spend_quota_slot
@@ -1058,15 +1059,6 @@ def admit_managed_start(identity: Mapping[str, Any]) -> dict[str, Any]:
10581059
)
10591060

10601061
execution_started = bool(args.execute)
1061-
unmeasurable_host_input = (
1062-
(args.host == "codex-cli" and bool(getattr(args, "codex_operation_tools", False)))
1063-
# The private page path is assigned after start admission. Until
1064-
# then the prepared renderer cannot measure the delivered prompt.
1065-
or (args.host == "dsh" and (
1066-
bool(getattr(args, "dsh_operation_module", None))
1067-
or bool(getattr(args, "dsh_context_pages", False))
1068-
))
1069-
)
10701062
result = run_loopx_turn_once(
10711063
payload,
10721064
decision_capture_payload=decision,
@@ -1113,7 +1105,7 @@ def admit_managed_start(identity: Mapping[str, Any]) -> dict[str, Any]:
11131105
epoch_prefetch=getattr(args, "epoch_prefetch_json", None),
11141106
quiet_turn=quiet_turn_option,
11151107
semantic_unit_fold=getattr(args, "semantic_unit_fold_json", None),
1116-
**({"host_input_renderer": None} if unmeasurable_host_input else {}),
1108+
host_input_renderer=resolve_host_input_renderer(args.host, options=vars(args)),
11171109
)
11181110
if args.execute and strategy_config.get("mode") == "auto":
11191111
restore_session_binding_on_unsettled_epoch_reset(

‎loopx/control_plane/turn_driver/epoch_prefetch.py‎

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import sys
2020
import tempfile
2121
import time
22-
from typing import Any, cast
22+
from typing import Any
2323

2424
from ...file_lock import exclusive_file_lock
2525
from ..content_digest import BARE_SHA256_PATTERN
@@ -29,6 +29,13 @@
2929
plan_stage_then_swap_prefetch,
3030
)
3131
from .decision_capture import open_regular_capture
32+
from .host_input import (
33+
HostInputRenderer,
34+
UNSET_HOST_INPUT_RENDERER,
35+
_safe_rendered_prompt,
36+
resolve_active_host_renderer as _resolve_active_host_renderer,
37+
resolve_host_input_renderer as resolve_host_input_renderer,
38+
)
3239
from .quiet_turn_store import (
3340
QUIET_TURN_SKIP_SCHEMA_VERSION as QUIET_TURN_SKIP_SCHEMA_VERSION,
3441
QUIET_TURN_STATE_SCHEMA_VERSION as QUIET_TURN_STATE_SCHEMA_VERSION,
@@ -852,54 +859,6 @@ def _turn_epoch_inputs(
852859
}
853860

854861

855-
HostInputRenderer = Callable[[Mapping[str, Any]], str | None]
856-
UNSET_HOST_INPUT_RENDERER: object = object()
857-
858-
859-
def _render_codex_cli_host_input(request: Mapping[str, Any]) -> str:
860-
from .codex_cli import _prompt
861-
862-
return _prompt(request)
863-
864-
865-
def _render_generic_cli_host_input(request: Mapping[str, Any]) -> str:
866-
from .host_process_transport import serialize_generic_cli_host_input
867-
868-
return serialize_generic_cli_host_input(request)
869-
870-
871-
def _render_dsh_host_input(request: Mapping[str, Any]) -> str | None:
872-
from .host_candidate import render_dsh_host_prompt
873-
874-
return render_dsh_host_prompt(request)
875-
876-
877-
def resolve_host_input_renderer(host: str | None) -> HostInputRenderer | None:
878-
"""Return the actual delivered host-input serializer for measurable hosts, or None."""
879-
normalized = (host or "").strip()
880-
if normalized in {"codex", "codex-cli"}:
881-
return _render_codex_cli_host_input
882-
if normalized == "generic-cli":
883-
return _render_generic_cli_host_input
884-
if normalized == "dsh":
885-
return _render_dsh_host_input
886-
return None
887-
888-
889-
def _safe_rendered_prompt(
890-
request: Mapping[str, Any],
891-
*,
892-
renderer: HostInputRenderer | None = _render_codex_cli_host_input,
893-
) -> str | None:
894-
"""Render the exact delivered host input when the host adapter is measurable."""
895-
if renderer is None:
896-
return None
897-
try:
898-
return renderer(request)
899-
except (ValueError, RuntimeError, TypeError, KeyError):
900-
return None
901-
902-
903862
_PROMPT_GATE_AUDIT_FIELDS = (
904863
"net_savings_tokens",
905864
"cold_prefill_amortization_tokens",
@@ -1154,18 +1113,6 @@ def _prompt_gate_policy(
11541113
return policy or None
11551114

11561115

1157-
def _resolve_active_host_renderer(
1158-
*,
1159-
host: str | None,
1160-
host_input_renderer: HostInputRenderer | None | object,
1161-
) -> HostInputRenderer | None:
1162-
if host_input_renderer is not UNSET_HOST_INPUT_RENDERER:
1163-
return cast(HostInputRenderer | None, host_input_renderer)
1164-
if host is not None:
1165-
return resolve_host_input_renderer(host)
1166-
return _render_codex_cli_host_input
1167-
1168-
11691116
def _adjudicate_candidate_prompt_gate(
11701117
*,
11711118
policy_cfg: Mapping[str, Any] | None,

‎loopx/control_plane/turn_driver/executor.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,14 @@
4545
)
4646
from .decision_capture import StagedTurnDecisionCapture, stage_turn_decision_capture
4747
from .epoch_prefetch import (
48-
HostInputRenderer,
49-
UNSET_HOST_INPUT_RENDERER,
5048
attach_epoch_metadata_to_journal,
5149
attach_turn_replay_epoch_metadata,
5250
finalize_turn_epoch_prefetch_request,
5351
prepare_turn_execution_epoch_context,
5452
project_epoch_prefetch_receipt,
5553
reset_failed_turn_journal_with_epoch_metadata,
5654
)
55+
from .host_input import HostInputRenderer, UNSET_HOST_INPUT_RENDERER
5756
from .quiet_turn_store import (
5857
assert_no_conflicting_quiet_turn_skip,
5958
commit_turn_quiet_state_after_success,
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""Render the input of measurable Turn host transports without loading unselected adapters."""
2+
3+
from __future__ import annotations
4+
5+
from collections.abc import Callable, Mapping
6+
from dataclasses import dataclass
7+
from typing import Any, cast
8+
9+
10+
HostInputRenderer = Callable[[Mapping[str, Any]], str | None]
11+
UNSET_HOST_INPUT_RENDERER: object = object()
12+
13+
14+
def _render_codex_cli_host_input(request: Mapping[str, Any]) -> str:
15+
from .codex_cli import _prompt
16+
17+
return _prompt(request)
18+
19+
20+
def _render_generic_cli_host_input(request: Mapping[str, Any]) -> str:
21+
from .host_process_transport import serialize_generic_cli_host_input
22+
23+
return serialize_generic_cli_host_input(request)
24+
25+
26+
def _render_dsh_host_input(request: Mapping[str, Any]) -> str | None:
27+
from .host_candidate import render_dsh_host_prompt
28+
29+
return render_dsh_host_prompt(request)
30+
31+
32+
@dataclass(frozen=True)
33+
class _HostInput:
34+
render: HostInputRenderer
35+
late_options: tuple[str, ...] = ()
36+
37+
38+
_CODEX_CLI_INPUT = _HostInput(_render_codex_cli_host_input, ("codex_operation_tools",))
39+
_HOST_INPUTS = {
40+
"codex": _CODEX_CLI_INPUT,
41+
"codex-cli": _CODEX_CLI_INPUT,
42+
"generic-cli": _HostInput(_render_generic_cli_host_input),
43+
"dsh": _HostInput(_render_dsh_host_input, ("dsh_operation_module", "dsh_context_pages")),
44+
}
45+
46+
47+
def resolve_host_input_renderer(
48+
host: str | None, *,
49+
options: Mapping[str, object] | None = None,
50+
) -> HostInputRenderer | None:
51+
"""Select the serializer for the bound transport's prelaunch input.
52+
53+
The ordinary CLI serializers above match their launchers. Operation tools
54+
assemble their native input later, and DSH native page locations are assigned
55+
after admission. Their input cannot be measured by an ordinary serializer.
56+
"""
57+
adapter = _HOST_INPUTS.get((host or "").strip())
58+
if adapter is None or (options is not None and any(options.get(name) for name in adapter.late_options)):
59+
return None
60+
return adapter.render
61+
62+
63+
def resolve_active_host_renderer(
64+
*,
65+
host: str | None,
66+
host_input_renderer: HostInputRenderer | None | object,
67+
) -> HostInputRenderer | None:
68+
if host_input_renderer is not UNSET_HOST_INPUT_RENDERER:
69+
return cast(HostInputRenderer | None, host_input_renderer)
70+
if host is not None:
71+
return resolve_host_input_renderer(host)
72+
return _render_codex_cli_host_input
73+
74+
75+
def _safe_rendered_prompt(
76+
request: Mapping[str, Any],
77+
*,
78+
renderer: HostInputRenderer | None = _render_codex_cli_host_input,
79+
) -> str | None:
80+
"""Render the exact delivered host input when the host adapter is measurable."""
81+
if renderer is None:
82+
return None
83+
try:
84+
return renderer(request)
85+
except (ValueError, RuntimeError, TypeError, KeyError):
86+
return None

‎loopx/control_plane/turn_driver/todo_unit_settlement.py‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
)
2525
from .epoch_prefetch import (
2626
DEFAULT_TOKENIZER_ENCODING,
27-
HostInputRenderer,
28-
UNSET_HOST_INPUT_RENDERER,
27+
_HOST_WIRE_EXCLUDED_PREFETCH_FIELDS,
2928
_adjudicate_candidate_prompt_gate,
3029
_apply_prompt_gate_audit_fields,
31-
_resolve_active_host_renderer,
30+
)
31+
from .host_input import (
32+
HostInputRenderer,
33+
UNSET_HOST_INPUT_RENDERER,
3234
_safe_rendered_prompt,
35+
resolve_active_host_renderer,
3336
)
3437
from .quiet_turn_store import init_turn_prefetch_sqlite_db
3538
from .semantic_unit_hook import _evaluate_semantic_unit_fold
@@ -445,10 +448,11 @@ def prepare_turn_semantic_unit_context(
445448
elif fold_cfg.get("quiescent_boundary") is False or fold_cfg.get("tool_in_flight") or fold_cfg.get("unpaired_tool_calls"):
446449
conflict_reason = "non_quiescent_boundary"
447450

448-
options_host = str(options.get("host") or _as_dict(plan.get("host")).get("kind") or "")
449-
renderer = _resolve_active_host_renderer(
450-
host=options_host,
451-
host_input_renderer=options.get("host_input_renderer", UNSET_HOST_INPUT_RENDERER),
451+
host = str(options.get("host") or _as_dict(plan.get("host")).get("kind") or "")
452+
host_renderer_opt = options.get("host_input_renderer", UNSET_HOST_INPUT_RENDERER)
453+
renderer = resolve_active_host_renderer(
454+
host=host,
455+
host_input_renderer=host_renderer_opt,
452456
)
453457
updated_request = dict(request)
454458
for field in ("parent_prefix", "exploratory_context"):

‎tests/control_plane/test_stage_then_swap_quiet_turn.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,6 +2878,24 @@ def _run_in_isolated_fixture(
28782878
assert count_verifiable_tokens(delivered_inline_stdin)[0] == inline_ep["rendered_prompt_delivered_tokens"]
28792879

28802880

2881+
@pytest.mark.parametrize(
2882+
("host", "option"),
2883+
[
2884+
("codex-cli", "codex_operation_tools"),
2885+
("dsh", "dsh_operation_module"),
2886+
("dsh", "dsh_context_pages"),
2887+
],
2888+
)
2889+
def test_late_input_options_only_disable_the_selected_host_renderer(host: str, option: str) -> None:
2890+
from loopx.control_plane.turn_driver.epoch_prefetch import resolve_host_input_renderer
2891+
2892+
ordinary = resolve_host_input_renderer(host)
2893+
assert ordinary is not None
2894+
assert resolve_host_input_renderer(host, options={option: False}) is ordinary
2895+
assert resolve_host_input_renderer(host, options={option: True}) is None
2896+
assert resolve_host_input_renderer("generic-cli", options={option: True}) is not None
2897+
2898+
28812899
def test_unmeasurable_and_unsupported_hosts_fail_closed_to_keep_raw(tmp_path: Path) -> None:
28822900
"""PR #9213: non-Codex hosts never silently fall back to Codex prompt formatting."""
28832901
from loopx.control_plane.turn_driver.epoch_prefetch import (

0 commit comments

Comments
 (0)