Skip to content

Commit 21048d8

Browse files
authored
Select the ordinary DSH session file in the host adapter (#9902)
* Select the ordinary DSH session file in the host adapter Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> * Keep native session selection on the snapshotted owner Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --------- Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
1 parent 1a903a4 commit 21048d8

5 files changed

Lines changed: 92 additions & 28 deletions

File tree

‎loopx/cli_commands/turn_dsh_host.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from typing import Any
1111

1212
from ..hosts.dsh.turn_host_adapter import DshHostConfig, DshSession, run_dsh_host
13+
from ..hosts.dsh_capabilities import DSH_SDK_SESSION_FILE
1314
from ..control_plane.goals.first_party_host_admission import FirstPartyHostGoalAdmission
1415
from ..control_plane.turn_driver.codex_sessions import (
1516
_lineage,
@@ -100,7 +101,7 @@ def _prepare_dsh_session(
100101
if action == "resume":
101102
original = select_codex_cli_session(
102103
runtime_root, lineage=lineage, session_scope=session_scope, host="dsh",
103-
goal_admission=goal_admission, ordinary_dsh=True,
104+
goal_admission=goal_admission, session_file=DSH_SDK_SESSION_FILE,
104105
)
105106
if original is None:
106107
if profile_digest is None:
@@ -111,7 +112,9 @@ def _prepare_dsh_session(
111112
raise BuiltInHostError("dsh_session_binding_changed", failure_kind="contract_rejected")
112113
return DshSession(resume_existing=True)
113114

114-
binding_path = _session_path(runtime_root, lineage, session_scope=session_scope, ordinary_dsh=True)
115+
binding_path = _session_path(
116+
runtime_root, lineage, session_scope=session_scope, session_file=DSH_SDK_SESSION_FILE,
117+
)
115118
if binding_path.exists() or binding_path.is_symlink():
116119
raise BuiltInHostError("dsh_session_binding_conflict", failure_kind="contract_rejected")
117120

@@ -120,7 +123,7 @@ def store() -> None:
120123
_store_codex_cli_session(
121124
runtime_root, lineage=lineage, session_scope=session_scope, host="dsh",
122125
session_id=native_id, goal_ref=request.get("goal_ref"),
123-
session_profile_digest=profile_digest, ordinary_dsh=True, require_absent=True,
126+
session_profile_digest=profile_digest, session_file=DSH_SDK_SESSION_FILE, require_absent=True,
124127
)
125128

126129
if goal_admission is None:

‎loopx/control_plane/turn_driver/codex_sessions.py‎

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from ...boundary_authority import normalize_checkpointed_boundary_authority_entries
1515
from ...file_lock import exclusive_file_lock
16+
from ...hosts.capabilities import NativeSessionFile
1617
from ..runtime.private_permissions import protect_private_path
1718
from ...runtime import validate_goal_id_path_segment
1819
from ..content_digest import BARE_SHA256_PATTERN
@@ -31,6 +32,9 @@
3132
_ABSENT_EXECUTOR_ADAPTER = object()
3233

3334

35+
DEFAULT_NATIVE_SESSION_FILE = NativeSessionFile(".json")
36+
37+
3438
def _host_session_schema(host: str) -> str:
3539
if host == "codex-cli":
3640
return CODEX_CLI_SESSION_SCHEMA_VERSION
@@ -66,10 +70,8 @@ def _session_path(
6670
lineage: Mapping[str, str],
6771
session_scope: str = "todo",
6872
*,
69-
ordinary_dsh: bool = False,
73+
session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE,
7074
) -> Path:
71-
if ordinary_dsh and session_scope not in {"todo", "agent"}:
72-
raise ValueError("ordinary DSH session binding requires Todo or Agent scope")
7375
fields = session_identity_fields(session_scope)
7476
identity = (
7577
dict(lineage)
@@ -88,8 +90,7 @@ def _session_path(
8890
).encode("utf-8")
8991
).hexdigest()
9092
goal_id: str = validate_goal_id_path_segment(lineage["goal_id"])
91-
suffix = ".dsh-sdk.json" if ordinary_dsh else ".json"
92-
return runtime_root / "goals" / goal_id / "turn-sessions" / f"{digest}{suffix}"
93+
return runtime_root / "goals" / goal_id / "turn-sessions" / f"{digest}{session_file.suffix}"
9394

9495

9596
def _valid_session_id(value: Any) -> str | None:
@@ -166,9 +167,9 @@ def _read_codex_cli_session_document(
166167
*,
167168
lineage: Mapping[str, str],
168169
session_scope: str = "todo",
169-
ordinary_dsh: bool = False,
170+
session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE,
170171
) -> dict[str, Any] | None:
171-
path = _session_path(runtime_root, lineage, session_scope, ordinary_dsh=ordinary_dsh)
172+
path = _session_path(runtime_root, lineage, session_scope, session_file=session_file)
172173
if not path.exists():
173174
return None
174175
try:
@@ -207,13 +208,18 @@ def codex_cli_session_binding(
207208
if work_contract is not _ABSENT_WORK_CONTRACT:
208209
request["work_contract"] = work_contract
209210
lineage = _lineage(request, session_scope=session_scope)
211+
session_file = DEFAULT_NATIVE_SESSION_FILE
212+
if ordinary_dsh:
213+
from ...hosts.dsh_capabilities import DSH_SDK_SESSION_FILE
214+
215+
session_file = DSH_SDK_SESSION_FILE
210216
session = select_codex_cli_session(
211217
runtime_root,
212218
lineage=lineage,
213219
session_scope=session_scope,
214220
goal_admission=goal_admission,
215221
host=host,
216-
ordinary_dsh=ordinary_dsh,
222+
session_file=session_file,
217223
)
218224
if session is None:
219225
return None
@@ -235,31 +241,32 @@ def _store_codex_cli_session(
235241
operation_profile_digest: str | None = None,
236242
operation_model: str | None = None,
237243
operation_reasoning_effort: str | None = None,
238-
ordinary_dsh: bool = False,
244+
session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE,
239245
require_absent: bool = False,
240246
checkpoint_artifact_sha256: str | None = None,
241247
checkpoint_artifact_root: str | None = None,
242248
checkpoint_model_transition: Mapping[str, str] | None = None,
243249
expected_document: object = _UNCHECKED_SESSION_DOCUMENT,
244250
) -> None:
245-
if ordinary_dsh and (
246-
host != "dsh" or operation_profile_digest is not None
251+
session_file.require_host(host)
252+
if session_file.profile_host is not None and (
253+
operation_profile_digest is not None
247254
or not BARE_SHA256_PATTERN.fullmatch(session_profile_digest or "")
248255
):
249-
raise ValueError("ordinary DSH binding requires its own session profile")
256+
raise ValueError("native binding requires its own session profile")
250257
if checkpoint_artifact_sha256 is not None and not BARE_SHA256_PATTERN.fullmatch(checkpoint_artifact_sha256):
251258
raise ValueError("checkpoint session source digest is invalid")
252259
if checkpoint_artifact_root is not None and (
253260
checkpoint_artifact_sha256 is None or not Path(checkpoint_artifact_root).is_absolute() or
254261
str(Path(checkpoint_artifact_root).resolve()) != checkpoint_artifact_root):
255262
raise ValueError("checkpoint session source root must be physical and digest-bound")
256-
path = _session_path(runtime_root, lineage, session_scope, ordinary_dsh=ordinary_dsh)
263+
path = _session_path(runtime_root, lineage, session_scope, session_file=session_file)
257264
with exclusive_file_lock(path):
258265
if require_absent and (path.exists() or path.is_symlink()):
259266
raise ValueError("session binding already exists; read back the original admission")
260267
_host_session_schema(host)
261268
current = _read_codex_cli_session_document(
262-
runtime_root, lineage=lineage, session_scope=session_scope, ordinary_dsh=ordinary_dsh,
269+
runtime_root, lineage=lineage, session_scope=session_scope, session_file=session_file,
263270
)
264271
if expected_document is not _UNCHECKED_SESSION_DOCUMENT and current != expected_document:
265272
raise ValueError("session binding changed during native execution")
@@ -301,7 +308,7 @@ def _store_codex_cli_session(
301308
operation_profile_digest=operation_profile_digest,
302309
operation_model=operation_model,
303310
operation_reasoning_effort=operation_reasoning_effort,
304-
ordinary_dsh=ordinary_dsh,
311+
session_file=session_file,
305312
checkpoint_artifact_sha256=checkpoint_artifact_sha256 or retained_source,
306313
checkpoint_artifact_root=checkpoint_artifact_root or retained_root,
307314
checkpoint_context_refresh=retained_refresh,
@@ -436,7 +443,7 @@ def _write_codex_cli_session(
436443
operation_profile_digest: str | None = None,
437444
operation_model: str | None = None,
438445
operation_reasoning_effort: str | None = None,
439-
ordinary_dsh: bool = False,
446+
session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE,
440447
checkpoint_artifact_sha256: str | None = None,
441448
checkpoint_artifact_root: str | None = None,
442449
checkpoint_context_refresh: dict[str, Any] | None = None,
@@ -451,7 +458,7 @@ def _write_codex_cli_session(
451458
normalized_session_id = _valid_session_id(session_id)
452459
if not normalized_session_id:
453460
raise ValueError("Codex CLI returned an invalid session id")
454-
path = _session_path(runtime_root, lineage, session_scope, ordinary_dsh=ordinary_dsh)
461+
path = _session_path(runtime_root, lineage, session_scope, session_file=session_file)
455462
path.parent.mkdir(parents=True, exist_ok=True)
456463
protect_private_path(path.parent, directory=True)
457464
descriptor, temporary_name = tempfile.mkstemp(
@@ -532,7 +539,7 @@ def select_codex_cli_session(
532539
session_scope: str = "todo",
533540
host: str = "codex-cli",
534541
goal_admission: FirstPartyHostGoalAdmission | None = None,
535-
ordinary_dsh: bool = False,
542+
session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE,
536543
) -> dict[str, Any] | None:
537544
"""Select through the existing Goal lifetime fence, then validate identity.
538545
@@ -541,7 +548,7 @@ def select_codex_cli_session(
541548
"""
542549
_, binding = _select_codex_cli_session_snapshot(
543550
runtime_root, lineage=lineage, session_scope=session_scope, host=host,
544-
goal_admission=goal_admission, ordinary_dsh=ordinary_dsh,
551+
goal_admission=goal_admission, session_file=session_file,
545552
)
546553
return binding
547554

@@ -553,23 +560,22 @@ def _select_codex_cli_session_snapshot(
553560
session_scope: str = "todo",
554561
host: str = "codex-cli",
555562
goal_admission: FirstPartyHostGoalAdmission | None = None,
556-
ordinary_dsh: bool = False,
563+
session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE,
557564
fresh: bool = False,
558565
) -> tuple[dict[str, Any] | None, dict[str, Any] | None]:
559566
"""Return the document read at selection and the binding qualified for reuse."""
560567
session_identity_fields(session_scope)
561568
_host_session_schema(host)
562-
if ordinary_dsh and host != "dsh":
563-
raise ValueError("ordinary DSH session binding requires the DSH host")
569+
session_file.require_host(host)
564570

565571
def read() -> dict[str, Any] | None:
566572
value = _read_codex_cli_session_document(
567573
runtime_root,
568574
lineage=lineage,
569575
session_scope=session_scope,
570-
ordinary_dsh=ordinary_dsh,
576+
session_file=session_file,
571577
)
572-
if ordinary_dsh and value is not None:
578+
if session_file.profile_host is not None and value is not None:
573579
digest = value.get("session_profile_digest")
574580
if (not isinstance(digest, str) or not BARE_SHA256_PATTERN.fullmatch(digest)
575581
or "operation_transport" in value):

‎loopx/hosts/capabilities.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ def _validate_descriptor_fields(
327327
)
328328

329329

330+
@dataclass(frozen=True)
331+
class NativeSessionFile:
332+
"""Built-in adapter choice of filename and profile-bound host."""
333+
334+
suffix: str
335+
profile_host: str | None = None
336+
337+
def require_host(self, host: str) -> None:
338+
if self.profile_host is not None and self.profile_host != host:
339+
raise ValueError("native session file does not match the selected host")
340+
341+
330342
@dataclass(frozen=True)
331343
class HostHarnessCapabilities:
332344
"""Canonical capability descriptor for a LoopX host harness adapter."""

‎loopx/hosts/dsh_capabilities.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from .capabilities import HostHarnessCapabilities, _register_builtin_host_capabilities
5+
from .capabilities import HostHarnessCapabilities, NativeSessionFile, _register_builtin_host_capabilities
66

77
DSH_HOST_CAPABILITIES: HostHarnessCapabilities = _register_builtin_host_capabilities(
88
HostHarnessCapabilities(
@@ -21,3 +21,7 @@
2121
),
2222
)
2323
)
24+
25+
# The ordinary SDK binding has always used this profiled file. Managed
26+
# operation tools have a separate legacy record even for the same Todo.
27+
DSH_SDK_SESSION_FILE = NativeSessionFile(".dsh-sdk.json", profile_host=DSH_HOST_CAPABILITIES.host_id)

‎tests/test_loopx_turn_driver.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5420,3 +5420,42 @@ def test_uses_effect_bound_start_identity_requires_validator_for_generic_cli() -
54205420
assert uses_effect_bound_start_identity(
54215421
"generic-cli", COMPLETE_TURN_HOST_REQUEST_SCHEMA, '["python3", "-c", "pass"]',
54225422
) is True
5423+
5424+
5425+
@pytest.mark.parametrize("provider", ["file", "sqlite"])
5426+
def test_ordinary_dsh_plan_rejects_operation_transport_in_sdk_file(tmp_path, monkeypatch, provider):
5427+
import hashlib
5428+
5429+
project, runtime, registry = _write_live_fixture(tmp_path, provider=provider)
5430+
monkeypatch.chdir(project)
5431+
lineage = {"goal_id": "loopx-turn-fixture", "agent_id": "codex-fixture", "todo_id": "todo_fixture0001"}
5432+
basename = hashlib.sha256(json.dumps(
5433+
lineage, ensure_ascii=False, sort_keys=True, separators=(",", ":"),
5434+
).encode("utf-8")).hexdigest()
5435+
path = runtime / "goals" / lineage["goal_id"] / "turn-sessions" / f"{basename}.dsh-sdk.json"
5436+
path.parent.mkdir(parents=True)
5437+
legacy_sdk = {
5438+
**lineage,
5439+
"schema_version": "loopx_managed_cli_session_v1",
5440+
"host": "dsh",
5441+
"session_id": "saved-sdk-session",
5442+
"session_profile_digest": "a" * 64,
5443+
}
5444+
5445+
def plan_action(document):
5446+
path.write_text(json.dumps(document), encoding="utf-8")
5447+
before = path.read_bytes()
5448+
output = io.StringIO()
5449+
with contextlib.redirect_stdout(output):
5450+
assert cli_main([
5451+
"--registry", str(registry), "--runtime-root", str(runtime), "--format", "json",
5452+
"turn", "plan", "--goal-id", lineage["goal_id"], "--agent-id", lineage["agent_id"],
5453+
"--host", "dsh", "--execution-mode", "isolated-headless", "--include-transaction-detail",
5454+
]) == 0
5455+
result = json.loads(output.getvalue())
5456+
assert result["effects"]["host_invoked"] is False
5457+
assert path.read_bytes() == before
5458+
return result["session"]["action"]
5459+
5460+
assert plan_action(legacy_sdk) == "resume"
5461+
assert plan_action({**legacy_sdk, "operation_transport": "dsh-native-operation-tools-v0"}) == "start_new"

0 commit comments

Comments
 (0)