diff --git a/loopx/cli_commands/turn_dsh_host.py b/loopx/cli_commands/turn_dsh_host.py index 3984b81ce0..233eb1a02d 100644 --- a/loopx/cli_commands/turn_dsh_host.py +++ b/loopx/cli_commands/turn_dsh_host.py @@ -10,6 +10,7 @@ from typing import Any from ..hosts.dsh.turn_host_adapter import DshHostConfig, DshSession, run_dsh_host +from ..hosts.dsh_capabilities import DSH_SDK_SESSION_FILE from ..control_plane.goals.first_party_host_admission import FirstPartyHostGoalAdmission from ..control_plane.turn_driver.codex_sessions import ( _lineage, @@ -100,7 +101,7 @@ def _prepare_dsh_session( if action == "resume": original = select_codex_cli_session( runtime_root, lineage=lineage, session_scope=session_scope, host="dsh", - goal_admission=goal_admission, ordinary_dsh=True, + goal_admission=goal_admission, session_file=DSH_SDK_SESSION_FILE, ) if original is None: if profile_digest is None: @@ -111,7 +112,9 @@ def _prepare_dsh_session( raise BuiltInHostError("dsh_session_binding_changed", failure_kind="contract_rejected") return DshSession(resume_existing=True) - binding_path = _session_path(runtime_root, lineage, session_scope=session_scope, ordinary_dsh=True) + binding_path = _session_path( + runtime_root, lineage, session_scope=session_scope, session_file=DSH_SDK_SESSION_FILE, + ) if binding_path.exists() or binding_path.is_symlink(): raise BuiltInHostError("dsh_session_binding_conflict", failure_kind="contract_rejected") @@ -120,7 +123,7 @@ def store() -> None: _store_codex_cli_session( runtime_root, lineage=lineage, session_scope=session_scope, host="dsh", session_id=native_id, goal_ref=request.get("goal_ref"), - session_profile_digest=profile_digest, ordinary_dsh=True, require_absent=True, + session_profile_digest=profile_digest, session_file=DSH_SDK_SESSION_FILE, require_absent=True, ) if goal_admission is None: diff --git a/loopx/control_plane/turn_driver/codex_sessions.py b/loopx/control_plane/turn_driver/codex_sessions.py index e4ff186528..84492d8a3f 100644 --- a/loopx/control_plane/turn_driver/codex_sessions.py +++ b/loopx/control_plane/turn_driver/codex_sessions.py @@ -13,6 +13,7 @@ from ...boundary_authority import normalize_checkpointed_boundary_authority_entries from ...file_lock import exclusive_file_lock +from ...hosts.capabilities import NativeSessionFile from ..runtime.private_permissions import protect_private_path from ...runtime import validate_goal_id_path_segment from ..content_digest import BARE_SHA256_PATTERN @@ -31,6 +32,9 @@ _ABSENT_EXECUTOR_ADAPTER = object() +DEFAULT_NATIVE_SESSION_FILE = NativeSessionFile(".json") + + def _host_session_schema(host: str) -> str: if host == "codex-cli": return CODEX_CLI_SESSION_SCHEMA_VERSION @@ -66,10 +70,8 @@ def _session_path( lineage: Mapping[str, str], session_scope: str = "todo", *, - ordinary_dsh: bool = False, + session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE, ) -> Path: - if ordinary_dsh and session_scope not in {"todo", "agent"}: - raise ValueError("ordinary DSH session binding requires Todo or Agent scope") fields = session_identity_fields(session_scope) identity = ( dict(lineage) @@ -88,8 +90,7 @@ def _session_path( ).encode("utf-8") ).hexdigest() goal_id: str = validate_goal_id_path_segment(lineage["goal_id"]) - suffix = ".dsh-sdk.json" if ordinary_dsh else ".json" - return runtime_root / "goals" / goal_id / "turn-sessions" / f"{digest}{suffix}" + return runtime_root / "goals" / goal_id / "turn-sessions" / f"{digest}{session_file.suffix}" def _valid_session_id(value: Any) -> str | None: @@ -166,9 +167,9 @@ def _read_codex_cli_session_document( *, lineage: Mapping[str, str], session_scope: str = "todo", - ordinary_dsh: bool = False, + session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE, ) -> dict[str, Any] | None: - path = _session_path(runtime_root, lineage, session_scope, ordinary_dsh=ordinary_dsh) + path = _session_path(runtime_root, lineage, session_scope, session_file=session_file) if not path.exists(): return None try: @@ -207,13 +208,18 @@ def codex_cli_session_binding( if work_contract is not _ABSENT_WORK_CONTRACT: request["work_contract"] = work_contract lineage = _lineage(request, session_scope=session_scope) + session_file = DEFAULT_NATIVE_SESSION_FILE + if ordinary_dsh: + from ...hosts.dsh_capabilities import DSH_SDK_SESSION_FILE + + session_file = DSH_SDK_SESSION_FILE session = select_codex_cli_session( runtime_root, lineage=lineage, session_scope=session_scope, goal_admission=goal_admission, host=host, - ordinary_dsh=ordinary_dsh, + session_file=session_file, ) if session is None: return None @@ -235,31 +241,32 @@ def _store_codex_cli_session( operation_profile_digest: str | None = None, operation_model: str | None = None, operation_reasoning_effort: str | None = None, - ordinary_dsh: bool = False, + session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE, require_absent: bool = False, checkpoint_artifact_sha256: str | None = None, checkpoint_artifact_root: str | None = None, checkpoint_model_transition: Mapping[str, str] | None = None, expected_document: object = _UNCHECKED_SESSION_DOCUMENT, ) -> None: - if ordinary_dsh and ( - host != "dsh" or operation_profile_digest is not None + session_file.require_host(host) + if session_file.profile_host is not None and ( + operation_profile_digest is not None or not BARE_SHA256_PATTERN.fullmatch(session_profile_digest or "") ): - raise ValueError("ordinary DSH binding requires its own session profile") + raise ValueError("native binding requires its own session profile") if checkpoint_artifact_sha256 is not None and not BARE_SHA256_PATTERN.fullmatch(checkpoint_artifact_sha256): raise ValueError("checkpoint session source digest is invalid") if checkpoint_artifact_root is not None and ( checkpoint_artifact_sha256 is None or not Path(checkpoint_artifact_root).is_absolute() or str(Path(checkpoint_artifact_root).resolve()) != checkpoint_artifact_root): raise ValueError("checkpoint session source root must be physical and digest-bound") - path = _session_path(runtime_root, lineage, session_scope, ordinary_dsh=ordinary_dsh) + path = _session_path(runtime_root, lineage, session_scope, session_file=session_file) with exclusive_file_lock(path): if require_absent and (path.exists() or path.is_symlink()): raise ValueError("session binding already exists; read back the original admission") _host_session_schema(host) current = _read_codex_cli_session_document( - runtime_root, lineage=lineage, session_scope=session_scope, ordinary_dsh=ordinary_dsh, + runtime_root, lineage=lineage, session_scope=session_scope, session_file=session_file, ) if expected_document is not _UNCHECKED_SESSION_DOCUMENT and current != expected_document: raise ValueError("session binding changed during native execution") @@ -301,7 +308,7 @@ def _store_codex_cli_session( operation_profile_digest=operation_profile_digest, operation_model=operation_model, operation_reasoning_effort=operation_reasoning_effort, - ordinary_dsh=ordinary_dsh, + session_file=session_file, checkpoint_artifact_sha256=checkpoint_artifact_sha256 or retained_source, checkpoint_artifact_root=checkpoint_artifact_root or retained_root, checkpoint_context_refresh=retained_refresh, @@ -436,7 +443,7 @@ def _write_codex_cli_session( operation_profile_digest: str | None = None, operation_model: str | None = None, operation_reasoning_effort: str | None = None, - ordinary_dsh: bool = False, + session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE, checkpoint_artifact_sha256: str | None = None, checkpoint_artifact_root: str | None = None, checkpoint_context_refresh: dict[str, Any] | None = None, @@ -451,7 +458,7 @@ def _write_codex_cli_session( normalized_session_id = _valid_session_id(session_id) if not normalized_session_id: raise ValueError("Codex CLI returned an invalid session id") - path = _session_path(runtime_root, lineage, session_scope, ordinary_dsh=ordinary_dsh) + path = _session_path(runtime_root, lineage, session_scope, session_file=session_file) path.parent.mkdir(parents=True, exist_ok=True) protect_private_path(path.parent, directory=True) descriptor, temporary_name = tempfile.mkstemp( @@ -532,7 +539,7 @@ def select_codex_cli_session( session_scope: str = "todo", host: str = "codex-cli", goal_admission: FirstPartyHostGoalAdmission | None = None, - ordinary_dsh: bool = False, + session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE, ) -> dict[str, Any] | None: """Select through the existing Goal lifetime fence, then validate identity. @@ -541,7 +548,7 @@ def select_codex_cli_session( """ _, binding = _select_codex_cli_session_snapshot( runtime_root, lineage=lineage, session_scope=session_scope, host=host, - goal_admission=goal_admission, ordinary_dsh=ordinary_dsh, + goal_admission=goal_admission, session_file=session_file, ) return binding @@ -553,23 +560,22 @@ def _select_codex_cli_session_snapshot( session_scope: str = "todo", host: str = "codex-cli", goal_admission: FirstPartyHostGoalAdmission | None = None, - ordinary_dsh: bool = False, + session_file: NativeSessionFile = DEFAULT_NATIVE_SESSION_FILE, fresh: bool = False, ) -> tuple[dict[str, Any] | None, dict[str, Any] | None]: """Return the document read at selection and the binding qualified for reuse.""" session_identity_fields(session_scope) _host_session_schema(host) - if ordinary_dsh and host != "dsh": - raise ValueError("ordinary DSH session binding requires the DSH host") + session_file.require_host(host) def read() -> dict[str, Any] | None: value = _read_codex_cli_session_document( runtime_root, lineage=lineage, session_scope=session_scope, - ordinary_dsh=ordinary_dsh, + session_file=session_file, ) - if ordinary_dsh and value is not None: + if session_file.profile_host is not None and value is not None: digest = value.get("session_profile_digest") if (not isinstance(digest, str) or not BARE_SHA256_PATTERN.fullmatch(digest) or "operation_transport" in value): diff --git a/loopx/hosts/capabilities.py b/loopx/hosts/capabilities.py index ef82305d2a..7cd04fc257 100644 --- a/loopx/hosts/capabilities.py +++ b/loopx/hosts/capabilities.py @@ -327,6 +327,18 @@ def _validate_descriptor_fields( ) +@dataclass(frozen=True) +class NativeSessionFile: + """Built-in adapter choice of filename and profile-bound host.""" + + suffix: str + profile_host: str | None = None + + def require_host(self, host: str) -> None: + if self.profile_host is not None and self.profile_host != host: + raise ValueError("native session file does not match the selected host") + + @dataclass(frozen=True) class HostHarnessCapabilities: """Canonical capability descriptor for a LoopX host harness adapter.""" diff --git a/loopx/hosts/dsh_capabilities.py b/loopx/hosts/dsh_capabilities.py index 586c817c94..e1b978f381 100644 --- a/loopx/hosts/dsh_capabilities.py +++ b/loopx/hosts/dsh_capabilities.py @@ -2,7 +2,7 @@ from __future__ import annotations -from .capabilities import HostHarnessCapabilities, _register_builtin_host_capabilities +from .capabilities import HostHarnessCapabilities, NativeSessionFile, _register_builtin_host_capabilities DSH_HOST_CAPABILITIES: HostHarnessCapabilities = _register_builtin_host_capabilities( HostHarnessCapabilities( @@ -21,3 +21,7 @@ ), ) ) + +# The ordinary SDK binding has always used this profiled file. Managed +# operation tools have a separate legacy record even for the same Todo. +DSH_SDK_SESSION_FILE = NativeSessionFile(".dsh-sdk.json", profile_host=DSH_HOST_CAPABILITIES.host_id) diff --git a/tests/test_loopx_turn_driver.py b/tests/test_loopx_turn_driver.py index b2a7b2e24a..32334b93d7 100644 --- a/tests/test_loopx_turn_driver.py +++ b/tests/test_loopx_turn_driver.py @@ -5420,3 +5420,42 @@ def test_uses_effect_bound_start_identity_requires_validator_for_generic_cli() - assert uses_effect_bound_start_identity( "generic-cli", COMPLETE_TURN_HOST_REQUEST_SCHEMA, '["python3", "-c", "pass"]', ) is True + + +@pytest.mark.parametrize("provider", ["file", "sqlite"]) +def test_ordinary_dsh_plan_rejects_operation_transport_in_sdk_file(tmp_path, monkeypatch, provider): + import hashlib + + project, runtime, registry = _write_live_fixture(tmp_path, provider=provider) + monkeypatch.chdir(project) + lineage = {"goal_id": "loopx-turn-fixture", "agent_id": "codex-fixture", "todo_id": "todo_fixture0001"} + basename = hashlib.sha256(json.dumps( + lineage, ensure_ascii=False, sort_keys=True, separators=(",", ":"), + ).encode("utf-8")).hexdigest() + path = runtime / "goals" / lineage["goal_id"] / "turn-sessions" / f"{basename}.dsh-sdk.json" + path.parent.mkdir(parents=True) + legacy_sdk = { + **lineage, + "schema_version": "loopx_managed_cli_session_v1", + "host": "dsh", + "session_id": "saved-sdk-session", + "session_profile_digest": "a" * 64, + } + + def plan_action(document): + path.write_text(json.dumps(document), encoding="utf-8") + before = path.read_bytes() + output = io.StringIO() + with contextlib.redirect_stdout(output): + assert cli_main([ + "--registry", str(registry), "--runtime-root", str(runtime), "--format", "json", + "turn", "plan", "--goal-id", lineage["goal_id"], "--agent-id", lineage["agent_id"], + "--host", "dsh", "--execution-mode", "isolated-headless", "--include-transaction-detail", + ]) == 0 + result = json.loads(output.getvalue()) + assert result["effects"]["host_invoked"] is False + assert path.read_bytes() == before + return result["session"]["action"] + + assert plan_action(legacy_sdk) == "resume" + assert plan_action({**legacy_sdk, "operation_transport": "dsh-native-operation-tools-v0"}) == "start_new"