diff --git a/corp_codex_pool/mcodex.py b/corp_codex_pool/mcodex.py index 76539a1..1d26605 100644 --- a/corp_codex_pool/mcodex.py +++ b/corp_codex_pool/mcodex.py @@ -56,11 +56,18 @@ "MULTICA_WORKSPACE_ID", ) +VERSION_PROBE_ARGS = {"--version", "-V"} + class McodexError(RuntimeError): pass +def is_version_probe(args: list[str]) -> bool: + """Return true only for the side-effect-free daemon runtime probe.""" + return len(args) == 1 and args[0] in VERSION_PROBE_ARGS + + def mcodex_home() -> Path: """mcodex 自己的家目录。可用 MCODEX_HOME 覆盖。""" override = os.environ.get("MCODEX_HOME") @@ -327,8 +334,12 @@ def main(argv: list[str] | None = None) -> int: ) try: - updates = prepare_env(spec, settings.pool_session_url) real_codex = find_real_codex(mcodex_home()) + updates = ( + {} + if is_version_probe(args) + else prepare_env(spec, settings.pool_session_url) + ) except McodexError as exc: print(f"mcodex: {exc}", file=sys.stderr) return 1 diff --git a/pyproject.toml b/pyproject.toml index 24507af..6a00fc4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "corp-codex-pool" -version = "0.2.0" +version = "0.2.1" description = "Codex 号池与 multica 运行时的集成层:provider 注入、密钥下发、用量对账" requires-python = ">=3.11" dependencies = [ diff --git a/tests/test_mcodex.py b/tests/test_mcodex.py index 835ca18..fccc645 100644 --- a/tests/test_mcodex.py +++ b/tests/test_mcodex.py @@ -11,6 +11,7 @@ McodexError, find_real_codex, init_home, + is_version_probe, mcodex_home, prepare_env, read_key, @@ -247,6 +248,15 @@ def test_task_mode_rejects_standalone_use(self, tmp_path, monkeypatch, spec): prepare_env(spec, "https://pool.example/session-key") +class TestVersionProbe: + def test_only_exact_version_probe_bypasses_task_context(self): + assert is_version_probe(["--version"]) + assert is_version_probe(["-V"]) + assert not is_version_probe([]) + assert not is_version_probe(["--version", "app-server"]) + assert not is_version_probe(["app-server", "--version"]) + + class TestRequestSessionKey: def test_requires_complete_task_context(self): with pytest.raises(McodexError, match="MULTICA_TOKEN"):