Skip to content

Commit e52ed82

Browse files
committed
Keep native dispatch admission in the selected Turn adapter
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
1 parent f2a9c7e commit e52ed82

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
@@ -20,6 +20,7 @@
2020
from ..control_plane.turn_driver.execution_profile import managed_execution_profile
2121
from ..control_plane.turn_driver.host_binding import dsh_output_token_budget
2222
from ..control_plane.turn_driver.host_failure import BuiltInHostError
23+
from ..control_plane.effect_program import SettlementBindingKind
2324
from ..hosts.dsh.turn_host_adapter import resolve_dsh_home
2425
from ..control_plane.operator_credential import (
2526
OPERATOR_CREDENTIAL_ENV_VARS,
@@ -242,3 +243,14 @@ def run(request: Mapping[str, Any]) -> dict[str, Any]:
242243
return run_dsh_operation_host(request, **options)
243244

244245
return run
246+
247+
248+
def native_dispatch_operation(args: argparse.Namespace, binding_kind: SettlementBindingKind) -> str | None:
249+
"""Select the SDK launch guard that also binds the pre-prompt receipt."""
250+
if (
251+
getattr(args, "dsh_operation_module", None)
252+
or getattr(args, "dsh_runner", None)
253+
or binding_kind not in (SettlementBindingKind.TODO, SettlementBindingKind.AUTONOMOUS_REPLAN)
254+
):
255+
return None
256+
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
@@ -15,7 +15,6 @@
1515
from ..capabilities.turn_context import configured_post_settlement
1616
from ..cli_rollout import append_cli_rollout_event
1717
from ..control_plane.agents.workspace_guard import capture_delivery_workspace
18-
from ..control_plane.effect_program import SettlementBindingKind
1918
from ..control_plane.goals.first_party_host_admission import (
2019
FirstPartyHostGoalAdmission,
2120
FirstPartyHostRuntimeRejected,
@@ -750,14 +749,7 @@ def scheduler(_spend_payload: dict[str, object]) -> dict[str, object]:
750749

751750
host_runner: Callable[[Mapping[str, Any]], dict[str, Any]] | None = None
752751
session_binding_resolver = None
753-
native_dsh_dispatch = (
754-
args.host == "dsh"
755-
and not getattr(args, "dsh_operation_module", None)
756-
and not getattr(args, "dsh_runner", None)
757-
and settlement_identity.binding_kind in (
758-
SettlementBindingKind.TODO, SettlementBindingKind.AUTONOMOUS_REPLAN,
759-
)
760-
)
752+
dispatch_operation: str | None = None
761753

762754
def current_launch_decision(current_registry: Path) -> Mapping[str, Any]:
763755
nonlocal prior_launch_decision
@@ -771,9 +763,9 @@ def current_launch_decision(current_registry: Path) -> Mapping[str, Any]:
771763
scheduler_execution_context=payload.get("scheduler_execution_context") or {},
772764
operator_inbox_urgency_projector=operator_inbox_urgency_projector,
773765
turn_start_hook_dispatch=turn_start_hook_dispatch, goal_ref=goal_ref,
774-
fresh_dispatch=native_dsh_dispatch,
766+
fresh_dispatch=dispatch_operation is not None,
775767
)
776-
if native_dsh_dispatch:
768+
if dispatch_operation is not None:
777769
current = launch["current"]
778770
recovery = current.get("unsettled_host_turn_recovery")
779771
if (current.get("effective_action") == EffectiveAction.UNSETTLED_HOST_TURN_RECOVERY.value
@@ -838,6 +830,10 @@ def resolve_built_in_session_binding(
838830

839831
session_binding_resolver = resolve_built_in_session_binding
840832
elif args.host == "dsh":
833+
from .turn_dsh_host import native_dispatch_operation
834+
835+
dispatch_operation = native_dispatch_operation(args, settlement_identity.binding_kind)
836+
native_dsh_dispatch = dispatch_operation is not None
841837
dsh_operation = bool(getattr(args, "dsh_operation_module", None))
842838
if dsh_operation:
843839
host_runner = getattr(_sys.modules[__name__], "build_dsh_operation_host_runner")(
@@ -933,15 +929,15 @@ def on_managed_start_admitted() -> None:
933929
)
934930

935931
def admit_managed_start(identity: Mapping[str, Any]) -> dict[str, Any]:
936-
if native_dsh_dispatch and not getattr(args, "resume_turn_key", None):
932+
if dispatch_operation is not None and not getattr(args, "resume_turn_key", None):
937933
# The plan can predate another Turn's dispatch. Recheck inside
938934
# the executing lane before reserving cadence or binding work.
939935
admission = goal_admission or FirstPartyHostGoalAdmission.for_plan(
940936
registry_path=registry_path, goal_id=args.goal_id, planned_goal_ref=goal_ref,
941937
agent_id=args.agent_id,
942938
)
943939
with admission.with_launch_decision(current_launch_decision).launch_lifetime(
944-
operation="dsh_turn_launch",
940+
operation=dispatch_operation,
945941
):
946942
pass
947943
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
@@ -3391,6 +3391,8 @@ def intercept(request, *, config, prepare_session=None, before_prompt=None):
33913391
source = Path(capture["path"])
33923392
checked["source"] = source
33933393
checked["original"] = source.read_bytes()
3394+
checked["turn_key"] = capture["turn_key"]
3395+
checked["goal_ref"] = request.get("goal_ref")
33943396
if mode == "wrong_turn":
33953397
request = {**request, "detail_ref": {**request["detail_ref"],
33963398
"captured_decision": {**capture, "turn_key": "sha256:" + "0" * 64}}}
@@ -3408,7 +3410,18 @@ def intercept(request, *, config, prepare_session=None, before_prompt=None):
34083410
source.chmod(0o600)
34093411

34103412
def runner(*, prompt, before_prompt, **_options):
3413+
from loopx.control_plane.quota.heartbeat_receipt import find_heartbeat_receipt
3414+
3415+
def read_dispatch_key():
3416+
receipt = find_heartbeat_receipt(
3417+
runtime, goal_id="loopx-turn-fixture", agent_id="codex-fixture",
3418+
turn_instance_id="fixture-read-pages", goal_ref=checked["goal_ref"],
3419+
)
3420+
return (receipt or {}).get("details", {}).get("host_dispatch_turn_key")
3421+
3422+
assert read_dispatch_key() is None
34113423
before_prompt()
3424+
assert read_dispatch_key() == checked["turn_key"]
34123425
model_input, _ = json.JSONDecoder().raw_decode(prompt.split("\nTurn request:\n", 1)[1])
34133426
detail = model_input["detail_ref"]
34143427
assert model_input["task_context"]["selected_todo"]["todo_id"] == "todo_fixture0001"

0 commit comments

Comments
 (0)