diff --git a/docs/reference/operator-model-credential.md b/docs/reference/operator-model-credential.md index 10b124c4e2..b602ff30ec 100644 --- a/docs/reference/operator-model-credential.md +++ b/docs/reference/operator-model-credential.md @@ -21,12 +21,14 @@ The credential is its own file, not a machine-configuration namespace: The canonical machine runtime is `~/.loopx` for new installations; a legacy-only machine stays on `~/.codex/loopx` until the [explicit migration](../product/migrations/local-state-path-migration.md). -This selection is independent of `CODEX_HOME` -and any Goal's `common_runtime_root` or Turn `--runtime-root`. Turn planning, -default host selection, dispatch and delegation readiness use this machine -store. A Goal-local credential file does not override it. Explicit machine -credential APIs retain their root parameter for operating an isolated machine -store; a Goal runtime override is not that parameter. +Turn planning, default host selection, dispatch and delegation readiness use +the machine store independently of `CODEX_HOME` and Goal-local credentials. +When both machine roots contain state, a managed Turn or confirmation callback +can resolve the credential only if its bound runtime selects one of those two +roots. An alias in a parent directory of a valid root resolves to the same +machine directory. For a standalone managed Turn, specify `--host dsh` and the +intended machine `--runtime-root`. Explicit machine credential APIs retain +their root parameter for operating an isolated machine store. This corrects the previous behavior where isolated Goal runtimes missed an already configured machine credential, and parser defaults consulted only the @@ -39,6 +41,10 @@ interpreter separately from credential availability. 隔离 Goal 的运行目录不再遮蔽已配置的机器凭证;未指定 Turn host 时, 有效的机器凭证会按既有规则选择 `dsh`。显式 host 选择保持优先, DSH SDK 是否安装仍按实际执行的 Python 环境检查。 +两个默认机器目录同时存在时,已绑定其中一个目录的托管 Turn 和卡片回调读取该目录 +的凭证。有效机器目录的上级路径存在别名时,仍按实际目录匹配。单独运行托管 Turn 时, +请同时指定 `--host dsh` 和对应机器目录的 `--runtime-root`。 +任意项目目录中的凭证文件仍不能覆盖机器凭证。 The directory is mode `0700` and the file is mode `0600`, written atomically. It is deliberately **not** part of `machine/configuration.json`: that document diff --git a/loopx/cli_commands/turn.py b/loopx/cli_commands/turn.py index 06ed815709..58e641bd54 100644 --- a/loopx/cli_commands/turn.py +++ b/loopx/cli_commands/turn.py @@ -270,7 +270,7 @@ def execution_preparer(decision: Mapping[str, Any]) -> str | None: operator_inbox_urgency_projector = build_lark_operator_inbox_urgency_projector( runtime_root_arg=runtime_root, ) - operator_environ = operator_provider_environ() + operator_environ = operator_provider_environ(bound_runtime_root=runtime_root) else: # Planning and dry-run execution inspect existing admitted intents. # Only an executing wake may sync inboxes or reserve a calendar window. @@ -413,7 +413,7 @@ def read_decision_owner(): # Turn keeps this locator and its original captured request. payload["delegation_operation_locator"] = json.loads(args.delegation_context_json) # Resolve machine authentication once for the readback and host launch. - operator_environ = operator_provider_environ() + operator_environ = operator_provider_environ(bound_runtime_root=runtime_root) payload["managed_executor"] = managed_executor_cli_binding(args, environ=operator_environ) if ( args.turn_command == "run-once" diff --git a/loopx/control_plane/collaboration/delegation_context.py b/loopx/control_plane/collaboration/delegation_context.py index 8e8d26e4a8..1674e60eb1 100644 --- a/loopx/control_plane/collaboration/delegation_context.py +++ b/loopx/control_plane/collaboration/delegation_context.py @@ -68,7 +68,7 @@ def read_delegation_facts( rows = [row for row in (config.get("bindings") or []) if isinstance(row, dict) and isinstance(row.get("requesters"), list) and agent_id in row["requesters"]] executors: dict[tuple[str, ...], Any] = {} - environ = operator_provider_environ() + environ = operator_provider_environ(bound_runtime_root=runtime_root) for host_args in (row.get("host_args") for row in rows[:MAX_PROBED_ROUTES]): if (isinstance(host_args, list) and all(isinstance(arg, str) for arg in host_args) and tuple(host_args) not in executors): diff --git a/loopx/control_plane/collaboration/operation_wake.py b/loopx/control_plane/collaboration/operation_wake.py index a5896f865d..7796f44335 100644 --- a/loopx/control_plane/collaboration/operation_wake.py +++ b/loopx/control_plane/collaboration/operation_wake.py @@ -72,7 +72,7 @@ def _original_operation_turn( "goal_id": parameters["goal_id"], "agent_id": parameters["agent_id"], "todo_id": executor["todo_id"], }, host=host) or {} - profile = managed_execution_profile(operator_provider_environ(), + profile = managed_execution_profile(operator_provider_environ(bound_runtime_root=runtime_root), provider=selected.dsh_provider, model=selected.dsh_model, reasoning_effort=selected.dsh_reasoning_effort) if host == "dsh" else { "model": selected.codex_model, "reasoning_effort": selected.codex_reasoning_effort} @@ -193,7 +193,7 @@ def dispatch_confirmed_operation_wake( "todo_id": binding["todo_id"], }, host=host) or {} profile = managed_execution_profile( - operator_provider_environ(), + operator_provider_environ(bound_runtime_root=service.root), provider=turn_host_arg_option(argv, "--dsh-provider"), model=turn_host_arg_option(argv, "--dsh-model"), reasoning_effort=turn_host_arg_option(argv, "--dsh-reasoning-effort"), diff --git a/loopx/control_plane/operator_provider.py b/loopx/control_plane/operator_provider.py index 6f310224b2..2ed460c9b5 100644 --- a/loopx/control_plane/operator_provider.py +++ b/loopx/control_plane/operator_provider.py @@ -46,7 +46,7 @@ from urllib.parse import urlsplit from ..file_lock import exclusive_file_lock -from ..paths import select_default_runtime_root +from ..paths import RuntimeRouteStatus, default_runtime_route, select_default_runtime_root from ..registry import atomic_write_json, read_json from .operator_credential import ( OPERATOR_CREDENTIAL_ENV_VARS, @@ -402,6 +402,7 @@ def operator_credential_source( def operator_provider_environ( runtime_root: Path | None = None, *, + bound_runtime_root: Path | None = None, environ: Mapping[str, str] | None = None, ) -> dict[str, str]: """Return an environment mapping with the stored credential resolved in. @@ -409,12 +410,24 @@ def operator_provider_environ( Every surface that resolves a credential already reads a mapping rather than ``os.environ`` directly, so resolving the machine store here is what lets the steward channel and the managed host pick up a key an operator stored - without editing a launch file or restarting a service. The input mapping is - never mutated. + without editing a launch file or restarting a service. A bound runtime may + disambiguate two default machine roots, but a Goal-local store remains + ineligible. The input mapping is never mutated. """ resolved = dict(_process_environ(environ)) try: + if runtime_root is None and bound_runtime_root is not None: + route = default_runtime_route() + if route["status"] == RuntimeRouteStatus.CONFLICT: + bound_root = bound_runtime_root.expanduser().resolve() + if bound_root in { + Path(str(route["target_runtime_root"])).resolve(), + Path(str(route["legacy_runtime_root"])).resolve(), + }: + runtime_root = bound_root + elif route["status"] != RuntimeRouteStatus.INVALID: + runtime_root = Path(str(route["selected_runtime_root"])) record = read_operator_provider(runtime_root) except (OSError, TypeError, ValueError): # A damaged record resolves to "no credential" rather than falling back diff --git a/tests/test_turn_machine_credential.py b/tests/test_turn_machine_credential.py index 80a4b16785..07907ddccc 100644 --- a/tests/test_turn_machine_credential.py +++ b/tests/test_turn_machine_credential.py @@ -18,6 +18,7 @@ from tests.test_loopx_turn_driver import _write_live_fixture KEY = "machine-credential-fixture" +LEGACY_KEY = "legacy-machine-credential-fixture" GOAL_KEY = "goal-must-not-select-this-credential" @@ -45,6 +46,7 @@ def turn(project, runtime, registry, *options, action="plan"): ]) payload = json.loads(output.getvalue()) assert KEY not in output.getvalue() + assert LEGACY_KEY not in output.getvalue() assert GOAL_KEY not in output.getvalue() return code, payload @@ -97,6 +99,55 @@ def test_invalid_machine_store_cannot_fall_back_to_goal_or_environment(tmp_path, assert plan["managed_executor"]["unavailable_reason"] == "operator_credential_unconfigured" +@pytest.mark.parametrize("alias_parent", [False, True], ids=["direct-parent", "symlink-parent"]) +def test_explicit_machine_binding_disambiguates_dual_default_roots( + tmp_path, machine, monkeypatch, alias_parent, +): + machine_home = tmp_path / "operator-home" + current = _write_live_fixture(machine_home / "current") + legacy = _write_live_fixture(machine_home / "legacy") + route_home = machine_home + if alias_parent: + route_home = tmp_path / "operator-home-link" + route_home.symlink_to(machine_home, target_is_directory=True) + monkeypatch.setattr(paths, "DEFAULT_RUNTIME_ROOT", route_home / "current" / "runtime") + monkeypatch.setattr(paths, "LEGACY_RUNTIME_ROOT", route_home / "legacy" / "runtime") + provider.write_operator_provider(runtime_root=current[1], api_key=KEY) + provider.write_operator_provider(runtime_root=legacy[1], api_key=LEGACY_KEY) + assert paths.default_runtime_route()["status"] == paths.RuntimeRouteStatus.CONFLICT + + host_options = ("--host", "dsh", "--execution-mode", "isolated-headless") + for (project, runtime, registry), configured_root, key in ( + (current, paths.DEFAULT_RUNTIME_ROOT, KEY), + (legacy, paths.LEGACY_RUNTIME_ROOT, LEGACY_KEY), + ): + code, plan = turn(project, runtime, registry, *host_options) + assert code == 0, plan.get("error") + assert plan["managed_executor"]["available"] is True, plan["managed_executor"] + credential_environ = provider.operator_provider_environ( + bound_runtime_root=configured_root, environ={}, + ) + assert credential_environ["DEEPSEEK_API_KEY"] == key + + other_project, other_runtime, other_registry = _write_live_fixture(tmp_path / "other") + provider.write_operator_provider(runtime_root=other_runtime, api_key=GOAL_KEY) + _, plan = turn(other_project, other_runtime, other_registry, *host_options) + assert plan["managed_executor"]["unavailable_reason"] == "operator_credential_unconfigured" + + +def test_explicit_binding_cannot_select_redirected_default_root(tmp_path, machine): + project, runtime, registry = _write_live_fixture(tmp_path / "legacy") + provider.write_operator_provider(runtime_root=runtime, api_key=LEGACY_KEY) + paths.LEGACY_RUNTIME_ROOT.symlink_to(runtime, target_is_directory=True) + assert paths.default_runtime_route()["status"] == paths.RuntimeRouteStatus.INVALID + + code, plan = turn( + project, runtime, registry, "--host", "dsh", "--execution-mode", "isolated-headless", + ) + assert code == 0, plan.get("error") + assert plan["managed_executor"]["unavailable_reason"] == "operator_credential_unconfigured" + + def test_explicit_codex_selection_survives_machine_credential(tmp_path, machine): project, runtime, registry = _write_live_fixture(tmp_path / "goal") _, plan = turn(project, runtime, registry, "--host", "codex-cli")