Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions scripts/ai-accounts/ai-accounts
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,10 @@ def cmd_shim(name: str, args: list[str]) -> int:
dirs = os.environ.get("PATH", "").split(os.pathsep)
shim_dirs = [d for d in dirs if is_shim_dir(d)]
rest = [d for d in dirs if d not in shim_dirs]
if name == "loopzero":
# Codex's transient launcher shims are hidden in the check sandbox.
arg0 = Path(os.environ.get("CODEX_HOME") or Path.home() / ".codex") / "tmp/arg0-linux"
rest = [d for d in rest if Path(d).parent != arg0 or not Path(d).name.startswith("codex-arg0")]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Filter compares paths lexically, so a symlinked or non-normalized CODEX_HOME will not match

Path(d).parent != arg0 is a plain string-level comparison. Codex canonicalizes its home directory before it builds the tmp/arg0-linux/codex-arg0* PATH entry. If CODEX_HOME is set to a symlink, a relative path, or a path containing .., the PATH entry will be the canonical path, the comparison fails, and the transient directory stays in PATH. The receipt fingerprint then changes from session to session, which is the bug this change is meant to fix. Smallest fix: compare resolved paths, e.g. compute arg0 = (...).resolve() once and test Path(d).parent.resolve() == arg0, or use os.path.realpath on both sides. Resolving does not touch the filesystem in a way that matters here, and it keeps the filter narrow.

real = next((str(c) for d in rest if (c := Path(d or ".") / name).is_file()
and os.access(c, os.X_OK)), None)
if real is None:
Expand Down
7 changes: 5 additions & 2 deletions tests/test_ai_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,10 @@ def test_shim_prefers_verified_codex_only_for_loopzero(helper, tmp_path, monkeyp
codex = pin / 'codex'
codex.write_text('#!/bin/sh\nexit 0\n')
codex.chmod(0o755 if pin_state != 'nonexecutable' else 0o644)
monkeypatch.setenv('PATH', f'{shims}:{binaries}')
codex_home = tmp_path / 'codex-home'
arg0 = codex_home / 'tmp/arg0-linux/codex-arg0ABC123'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Test only covers the positive match; the narrowness of the filter is not pinned

The task says the filter matches only a named Codex launcher inside the active CODEX_HOME. The test adds one matching directory, but no test checks that near-misses survive: a codex-arg0* directory under a different home, or a directory under arg0-linux whose name does not start with codex-arg0. If someone loosens the predicate later (for example to a substring match on arg0), it could silently drop legitimate PATH entries and no test would fail. Add one or two decoy PATH entries to the loopzero case and assert they stay in env['PATH'].

monkeypatch.setenv('CODEX_HOME', str(codex_home))
monkeypatch.setenv('PATH', f'{shims}:{arg0}:{binaries}')
monkeypatch.setattr(helper.sys, 'argv', [str(shims / name)])
calls = []
monkeypatch.setattr(helper.os, 'execve', lambda *args: calls.append(args))
Expand All @@ -471,7 +474,7 @@ def test_shim_prefers_verified_codex_only_for_loopzero(helper, tmp_path, monkeyp
expected.insert(0, str(pin))
assert env['PATH'].split(helper.os.pathsep) == expected
else:
assert env['PATH'] == f'{shims}:{binaries}'
assert env['PATH'] == f'{shims}:{arg0}:{binaries}'


@pytest.mark.parametrize('provider,email,shown', [
Expand Down
Loading