Skip to content

Commit cfc58ad

Browse files
authored
Merge pull request #9903 from loopx-project/codex/turn-native-dispatch-lifecycle-20261202
Keep native dispatch admission in the selected Turn adapter
2 parents 8811c5c + e52ed82 commit cfc58ad

3 files changed

Lines changed: 34 additions & 13 deletions

File tree

‎loopx/cli_commands/turn_dsh_host.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from ..control_plane.turn_driver.execution_profile import managed_execution_profile
1919
from ..control_plane.turn_driver.host_binding import dsh_output_token_budget
2020
from ..control_plane.turn_driver.host_failure import BuiltInHostError
21+
from ..control_plane.effect_program import SettlementBindingKind
2122
from ..hosts.dsh.turn_host_adapter import resolve_dsh_home
2223
from ..control_plane.operator_credential import (
2324
OPERATOR_CREDENTIAL_ENV_VARS,
@@ -235,3 +236,14 @@ def run(request: Mapping[str, Any]) -> dict[str, Any]:
235236
return run_dsh_operation_host(request, **options)
236237

237238
return run
239+
240+
241+
def native_dispatch_operation(args: argparse.Namespace, binding_kind: SettlementBindingKind) -> str | None:
242+
"""Select the SDK launch guard that also binds the pre-prompt receipt."""
243+
if (
244+
getattr(args, "dsh_operation_module", None)
245+
or getattr(args, "dsh_runner", None)
246+
or binding_kind not in (SettlementBindingKind.TODO, SettlementBindingKind.AUTONOMOUS_REPLAN)
247+
):
248+
return None
249+
return "dsh_turn_launch"

‎loopx/cli_commands/turn_run_once.py‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from ..cli_rollout import append_cli_rollout_event
1717
from ..hosts.capabilities import builtin_native_session_file
1818
from ..control_plane.agents.workspace_guard import capture_delivery_workspace
19-
from ..control_plane.effect_program import SettlementBindingKind
2019
from ..control_plane.goals.first_party_host_admission import (
2120
FirstPartyHostGoalAdmission,
2221
FirstPartyHostRuntimeRejected,
@@ -793,14 +792,7 @@ def scheduler(_spend_payload: dict[str, object]) -> dict[str, object]:
793792

794793
host_runner: Callable[[Mapping[str, Any]], dict[str, Any]] | None = None
795794
session_binding_resolver = None
796-
native_dsh_dispatch = (
797-
args.host == "dsh"
798-
and not getattr(args, "dsh_operation_module", None)
799-
and not getattr(args, "dsh_runner", None)
800-
and settlement_identity.binding_kind in (
801-
SettlementBindingKind.TODO, SettlementBindingKind.AUTONOMOUS_REPLAN,
802-
)
803-
)
795+
dispatch_operation: str | None = None
804796

805797
def current_launch_decision(current_registry: Path) -> Mapping[str, Any]:
806798
nonlocal prior_launch_decision
@@ -814,9 +806,9 @@ def current_launch_decision(current_registry: Path) -> Mapping[str, Any]:
814806
scheduler_execution_context=payload.get("scheduler_execution_context") or {},
815807
operator_inbox_urgency_projector=operator_inbox_urgency_projector,
816808
turn_start_hook_dispatch=turn_start_hook_dispatch, goal_ref=goal_ref,
817-
fresh_dispatch=native_dsh_dispatch,
809+
fresh_dispatch=dispatch_operation is not None,
818810
)
819-
if native_dsh_dispatch:
811+
if dispatch_operation is not None:
820812
current = launch["current"]
821813
recovery = current.get("unsettled_host_turn_recovery")
822814
if (current.get("effective_action") == EffectiveAction.UNSETTLED_HOST_TURN_RECOVERY.value
@@ -932,6 +924,10 @@ def resolve_built_in_session_binding(
932924

933925
session_binding_resolver = resolve_built_in_session_binding
934926
elif args.host == "dsh":
927+
from .turn_dsh_host import native_dispatch_operation
928+
929+
dispatch_operation = native_dispatch_operation(args, settlement_identity.binding_kind)
930+
native_dsh_dispatch = dispatch_operation is not None
935931
dsh_operation = bool(getattr(args, "dsh_operation_module", None))
936932
if dsh_operation:
937933
host_runner = getattr(_sys.modules[__name__], "build_dsh_operation_host_runner")(
@@ -1014,15 +1010,15 @@ def on_managed_start_admitted() -> None:
10141010
)
10151011

10161012
def admit_managed_start(identity: Mapping[str, Any]) -> dict[str, Any]:
1017-
if native_dsh_dispatch and not getattr(args, "resume_turn_key", None):
1013+
if dispatch_operation is not None and not getattr(args, "resume_turn_key", None):
10181014
# The plan can predate another Turn's dispatch. Recheck inside
10191015
# the executing lane before reserving cadence or binding work.
10201016
admission = goal_admission or FirstPartyHostGoalAdmission.for_plan(
10211017
registry_path=registry_path, goal_id=args.goal_id, planned_goal_ref=goal_ref,
10221018
agent_id=args.agent_id,
10231019
)
10241020
with admission.with_launch_decision(current_launch_decision).launch_lifetime(
1025-
operation="dsh_turn_launch",
1021+
operation=dispatch_operation,
10261022
):
10271023
pass
10281024
return managed_cadence.admit(identity)

‎tests/test_loopx_turn_driver.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3417,6 +3417,8 @@ def intercept(request, *, config, prepare_session=None, before_prompt=None):
34173417
source = Path(capture["path"])
34183418
checked["source"] = source
34193419
checked["original"] = source.read_bytes()
3420+
checked["turn_key"] = capture["turn_key"]
3421+
checked["goal_ref"] = request.get("goal_ref")
34203422
if mode == "wrong_turn":
34213423
request = {**request, "detail_ref": {**request["detail_ref"],
34223424
"captured_decision": {**capture, "turn_key": "sha256:" + "0" * 64}}}
@@ -3434,7 +3436,18 @@ def intercept(request, *, config, prepare_session=None, before_prompt=None):
34343436
source.chmod(0o600)
34353437

34363438
def runner(*, prompt, before_prompt, **_options):
3439+
from loopx.control_plane.quota.heartbeat_receipt import find_heartbeat_receipt
3440+
3441+
def read_dispatch_key():
3442+
receipt = find_heartbeat_receipt(
3443+
runtime, goal_id="loopx-turn-fixture", agent_id="codex-fixture",
3444+
turn_instance_id="fixture-read-pages", goal_ref=checked["goal_ref"],
3445+
)
3446+
return (receipt or {}).get("details", {}).get("host_dispatch_turn_key")
3447+
3448+
assert read_dispatch_key() is None
34373449
before_prompt()
3450+
assert read_dispatch_key() == checked["turn_key"]
34383451
model_input, _ = json.JSONDecoder().raw_decode(prompt.split("\nTurn request:\n", 1)[1])
34393452
detail = model_input["detail_ref"]
34403453
assert model_input["task_context"]["selected_todo"]["todo_id"] == "todo_fixture0001"

0 commit comments

Comments
 (0)