Skip to content

Commit aa0647d

Browse files
authored
fix(autoreview): sync Codex isolation preflight (#131002)
1 parent 15ce9ab commit aa0647d

4 files changed

Lines changed: 403 additions & 55 deletions

File tree

‎.agents/skills/autoreview/SKILL.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ The helper:
317317
- scans safe Git patches in full, recognizes synthetic fixture values tied to their credential field, reviews them in one pass up to the aggregate prompt limit, and automatically uses complete bounded passes above it
318318
- should be left in `--mode auto` or forced to `--mode branch` for PR/branch work; do not force `--mode local` after committing
319319
- writes only to stdout unless `--output`, `--json-output`, or live streamed engine stderr is set
320-
- supports `--dry-run` (validates bundle construction and reviewer CLI binary resolution without contacting any engine; exits nonzero if either check fails), an opt-in per-reviewer wall-clock bound via `--engine-timeout-seconds`, `--prompt`, repo-relative `--prompt-file`, repo-relative `--dataset`, `--no-tools`, `--no-web-search`, repeatable Codex-only safe model/response tuning with `--codex-config key=value`, Codex-only `--codex-speed fast|flex|default`, and commit refs
320+
- supports `--dry-run` (validates bundle construction, reviewer CLI resolution, and local isolation startup with version/help probes without contacting a provider; exits nonzero if any check fails), an opt-in per-reviewer wall-clock bound via `--engine-timeout-seconds`, `--prompt`, repo-relative `--prompt-file`, repo-relative `--dataset`, `--no-tools`, `--no-web-search`, repeatable Codex-only safe model/response tuning with `--codex-config key=value`, Codex-only `--codex-speed fast|flex|default`, and commit refs
321321
- supports `--stream-engine-output` or `AUTOREVIEW_STREAM_ENGINE_OUTPUT=1` for live engine text while preserving structured validation; Codex and Claude hide tool/file event details, emit compact activity summaries, and report usage at turn completion
322322
- supports per-engine `--model`, `--thinking`, and Claude `--fallback-model`
323323
- uses built-in defaults `codex=gpt-5.6-sol` with `high` reasoning and an access-only `gpt-5.6-terra` retry, `claude=claude-fable-5`, and `amp=openai/gpt-5.6-sol` with `high` reasoning; honors `AUTOREVIEW_MODEL`, `AUTOREVIEW_THINKING`, `AUTOREVIEW_FALLBACK_MODEL`, and per-engine `AUTOREVIEW_<ENGINE>_MODEL` / `AUTOREVIEW_<ENGINE>_THINKING` environment overrides when CLI flags are omitted

‎.agents/skills/autoreview/scripts/autoreview‎

Lines changed: 120 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3368,30 +3368,37 @@ def codex_auth_config_flags(repo: Path, *, force_file: bool = False) -> list[str
33683368
return flags
33693369

33703370

3371-
def prepare_codex_runtime_auth(
3372-
repo: Path,
3373-
runtime_codex_home: Path,
3374-
) -> bool:
3371+
def codex_file_auth_source(repo: Path) -> Path | None:
33753372
source_home = codex_source_home(repo)
33763373
if source_home is None:
3377-
return False
3374+
return None
33783375
config = load_codex_auth_config(source_home / "config.toml")
33793376
credential_store = config.get("cli_auth_credentials_store")
33803377
if credential_store not in {None, "file"}:
3381-
return False
3378+
return None
33823379
source_auth = source_home / "auth.json"
33833380
try:
33843381
source_stat = source_auth.lstat()
33853382
except OSError:
3386-
return False
3383+
return None
33873384
if not stat.S_ISREG(source_stat.st_mode):
3388-
return False
3385+
return None
33893386
try:
33903387
data, truncated = read_prefix(source_auth, 1_000_000)
33913388
parsed = json.loads(data)
33923389
except (OSError, SystemExit, json.JSONDecodeError):
3393-
return False
3390+
return None
33943391
if truncated or not isinstance(parsed, dict):
3392+
return None
3393+
return source_auth
3394+
3395+
3396+
def prepare_codex_runtime_auth(
3397+
repo: Path,
3398+
runtime_codex_home: Path,
3399+
) -> bool:
3400+
source_auth = codex_file_auth_source(repo)
3401+
if source_auth is None:
33953402
return False
33963403
runtime_codex_home.mkdir(parents=True, exist_ok=True)
33973404
runtime_auth = runtime_codex_home / "auth.json"
@@ -3410,6 +3417,96 @@ def prepare_codex_runtime_auth(
34103417
return True
34113418

34123419

3420+
def codex_runtime_env(
3421+
repo: Path,
3422+
runtime_root: Path,
3423+
codex_bin: str,
3424+
*,
3425+
file_auth_linked: bool,
3426+
) -> dict[str, str]:
3427+
runtime_home = runtime_root / "home"
3428+
runtime_config = runtime_home / ".config"
3429+
runtime_data = runtime_home / ".local" / "share"
3430+
runtime_state = runtime_home / ".local" / "state"
3431+
runtime_cache = runtime_home / ".cache"
3432+
runtime_codex_home = runtime_root / "codex-home"
3433+
for path in (
3434+
runtime_home,
3435+
runtime_config,
3436+
runtime_data,
3437+
runtime_state,
3438+
runtime_cache,
3439+
runtime_codex_home,
3440+
):
3441+
path.mkdir(parents=True, exist_ok=True)
3442+
source_codex_home = codex_source_home(repo)
3443+
active_codex_home = (
3444+
runtime_codex_home
3445+
if file_auth_linked or source_codex_home is None
3446+
else source_codex_home
3447+
)
3448+
return safe_engine_env(
3449+
repo,
3450+
[Path(codex_bin).parent],
3451+
engine="codex",
3452+
extra={
3453+
"HOME": str(runtime_home),
3454+
"USERPROFILE": str(runtime_home),
3455+
"XDG_CACHE_HOME": str(runtime_cache),
3456+
"XDG_CONFIG_HOME": str(runtime_config),
3457+
"XDG_DATA_HOME": str(runtime_data),
3458+
"XDG_STATE_HOME": str(runtime_state),
3459+
# Keyring namespaces are derived from canonical CODEX_HOME.
3460+
# Linked file auth uses the isolated home; keyring/auto must
3461+
# retain the source namespace until Codex supports an auth split.
3462+
"CODEX_HOME": str(active_codex_home),
3463+
},
3464+
)
3465+
3466+
3467+
def ensure_codex_isolation_supported(
3468+
args: argparse.Namespace,
3469+
repo: Path,
3470+
) -> str:
3471+
selected_bin = args.codex_bin
3472+
codex_bin = resolve_command(selected_bin, repo)
3473+
temp_root = safe_temp_root(repo)
3474+
with tempfile.TemporaryDirectory(
3475+
prefix="autoreview-codex-probe-workspace.",
3476+
dir=temp_root,
3477+
) as workspace_dir, tempfile.TemporaryDirectory(
3478+
prefix="autoreview-codex-probe-runtime.",
3479+
dir=temp_root,
3480+
) as runtime_dir:
3481+
probe_env = codex_runtime_env(
3482+
repo,
3483+
Path(runtime_dir),
3484+
codex_bin,
3485+
file_auth_linked=codex_file_auth_source(repo) is not None,
3486+
)
3487+
result = run(
3488+
[codex_bin, "--version"],
3489+
Path(workspace_dir),
3490+
check=False,
3491+
env=probe_env,
3492+
)
3493+
if result.returncode != 0:
3494+
detail = display_escape(
3495+
(result.stderr or result.stdout).strip(),
3496+
500,
3497+
multiline=True,
3498+
)
3499+
suffix = f"\n{detail}" if detail else ""
3500+
raise SystemExit(
3501+
"Codex isolation preflight failed: selected binary "
3502+
f"{selected_bin!r} resolved to {codex_bin!r}, but --version exited "
3503+
f"{result.returncode} under the isolated runtime.{suffix}\n"
3504+
"Use an absolute --codex-bin path, set CODEX_BIN, or correct PATH. "
3505+
"CODEX_HOME-dependent launchers are incompatible with autoreview isolation."
3506+
)
3507+
return codex_bin
3508+
3509+
34133510
def codex_exec_isolation_flags() -> list[str]:
34143511
return ["--ignore-user-config", "--ignore-rules", "--skip-git-repo-check"]
34153512

@@ -3979,6 +4076,7 @@ def codex_command(
39794076
def run_codex(args: argparse.Namespace, repo: Path, prompt: str) -> str:
39804077
if not args.tools:
39814078
raise SystemExit("--no-tools is not supported by the Codex engine; use --engine claude --no-tools for a no-tools run")
4079+
ensure_codex_isolation_supported(args, repo)
39824080
temp_root = safe_temp_root(repo)
39834081
schema_path = write_json_temp(SCHEMA, temp_root)
39844082
with tempfile.NamedTemporaryFile(
@@ -4006,28 +4104,8 @@ def run_codex(args: argparse.Namespace, repo: Path, prompt: str) -> str:
40064104
) as runtime_dir:
40074105
review_root = Path(workspace_dir)
40084106
runtime_root = Path(runtime_dir)
4009-
runtime_home = runtime_root / "home"
4010-
runtime_config = runtime_home / ".config"
4011-
runtime_data = runtime_home / ".local" / "share"
4012-
runtime_state = runtime_home / ".local" / "state"
4013-
runtime_cache = runtime_home / ".cache"
40144107
runtime_codex_home = runtime_root / "codex-home"
4015-
for path in (
4016-
runtime_home,
4017-
runtime_config,
4018-
runtime_data,
4019-
runtime_state,
4020-
runtime_cache,
4021-
runtime_codex_home,
4022-
):
4023-
path.mkdir(parents=True, exist_ok=True)
40244108
file_auth_linked = prepare_codex_runtime_auth(repo, runtime_codex_home)
4025-
source_codex_home = codex_source_home(repo)
4026-
active_codex_home = (
4027-
runtime_codex_home
4028-
if file_auth_linked or source_codex_home is None
4029-
else source_codex_home
4030-
)
40314109
for index, model in enumerate(models):
40324110
output_path.write_text("")
40334111
cmd = codex_command(
@@ -4048,22 +4126,11 @@ def run_codex(args: argparse.Namespace, repo: Path, prompt: str) -> str:
40484126
max_runtime_seconds=getattr(args, "engine_timeout_seconds", None),
40494127
stream_output=args.stream_engine_output,
40504128
stream_display=CodexStreamDisplay() if args.stream_engine_output else None,
4051-
env=safe_engine_env(
4129+
env=codex_runtime_env(
40524130
repo,
4053-
[Path(cmd[0]).parent],
4054-
engine="codex",
4055-
extra={
4056-
"HOME": str(runtime_home),
4057-
"USERPROFILE": str(runtime_home),
4058-
"XDG_CACHE_HOME": str(runtime_cache),
4059-
"XDG_CONFIG_HOME": str(runtime_config),
4060-
"XDG_DATA_HOME": str(runtime_data),
4061-
"XDG_STATE_HOME": str(runtime_state),
4062-
# Keyring namespaces are derived from canonical CODEX_HOME.
4063-
# Linked file auth uses the isolated home; keyring/auto must
4064-
# retain the source namespace until Codex supports an auth split.
4065-
"CODEX_HOME": str(active_codex_home),
4066-
},
4131+
runtime_root,
4132+
cmd[0],
4133+
file_auth_linked=file_auth_linked,
40674134
),
40684135
)
40694136
output = output_path.read_text()
@@ -5523,6 +5590,7 @@ def reviewer_label(args: argparse.Namespace) -> str:
55235590

55245591

55255592
ENGINE_ISOLATION_PROBES: dict[str, Callable[[argparse.Namespace, Path], object]] = {
5593+
"codex": ensure_codex_isolation_supported,
55265594
"claude": ensure_claude_isolation_supported,
55275595
"amp": ensure_amp_isolation_supported,
55285596
"pi": ensure_pi_isolation_supported,
@@ -5538,15 +5606,15 @@ def resolve_engine_binary(reviewer: argparse.Namespace, repo: Path) -> tuple[boo
55385606
reported unavailable with that same reason, and the selected engine is
55395607
checked for a resolvable CLI binary on PATH.
55405608
5541-
Once a binary resolves, claude/pi/kimi are also put through the same
5609+
Once a binary resolves, codex/claude/pi/kimi are also put through the same
55425610
local version and required-flag probes their real runners perform
5543-
before contacting the engine (ensure_claude_isolation_supported,
5544-
ensure_pi_isolation_supported, ensure_kimi_isolation_supported), so a
5545-
dry run cannot report OK for a configuration a real run would reject
5546-
immediately (unsupported CLI version, missing required flag). Those
5547-
probes only invoke the resolved binary locally with --version/--help;
5548-
they never contact a provider. Codex has its own unconditional
5549-
--no-tools rejection above and no local version gate.
5611+
before contacting the engine (ensure_codex_isolation_supported,
5612+
ensure_claude_isolation_supported, ensure_pi_isolation_supported,
5613+
ensure_kimi_isolation_supported), so a dry run cannot report OK for a
5614+
configuration a real run would reject immediately (unsupported CLI
5615+
version, missing required flag, or a launcher that fails under the
5616+
isolated runtime). Those probes only invoke the resolved binary locally
5617+
with --version/--help; they never contact a provider.
55505618
55515619
Codex additionally validates --codex-config/--codex-speed (and their
55525620
AUTOREVIEW_CODEX_CONFIG/AUTOREVIEW_CODEX_SPEED env equivalents) via

‎.agents/skills/autoreview/scripts/autoreview_test.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,10 @@ def fake_run(command: list[str], *_args: object, **_kwargs: object) -> subproces
984984
AUTOREVIEW,
985985
"resolve_command",
986986
return_value="/usr/bin/codex",
987+
), mock.patch.object(
988+
AUTOREVIEW,
989+
"ensure_codex_isolation_supported",
990+
return_value="/usr/bin/codex",
987991
), mock.patch.object(AUTOREVIEW, "codex_auth_config_flags", return_value=[]), mock.patch.object(
988992
AUTOREVIEW,
989993
"prepare_codex_runtime_auth",
@@ -1038,6 +1042,10 @@ def fake_run(
10381042
AUTOREVIEW,
10391043
"resolve_command",
10401044
return_value="/usr/bin/codex",
1045+
), mock.patch.object(
1046+
AUTOREVIEW,
1047+
"ensure_codex_isolation_supported",
1048+
return_value="/usr/bin/codex",
10411049
), mock.patch.object(
10421050
AUTOREVIEW,
10431051
"codex_auth_config_flags",
@@ -1102,6 +1110,10 @@ def fake_run(command: list[str], *_args: object, **_kwargs: object) -> subproces
11021110
AUTOREVIEW,
11031111
"resolve_command",
11041112
return_value="/usr/bin/codex",
1113+
), mock.patch.object(
1114+
AUTOREVIEW,
1115+
"ensure_codex_isolation_supported",
1116+
return_value="/usr/bin/codex",
11051117
), mock.patch.object(AUTOREVIEW, "codex_auth_config_flags", return_value=[]), mock.patch.object(
11061118
AUTOREVIEW,
11071119
"prepare_codex_runtime_auth",
@@ -1143,6 +1155,10 @@ def fake_run(command: list[str], *_args: object, **_kwargs: object) -> subproces
11431155
AUTOREVIEW,
11441156
"resolve_command",
11451157
return_value="/usr/bin/codex",
1158+
), mock.patch.object(
1159+
AUTOREVIEW,
1160+
"ensure_codex_isolation_supported",
1161+
return_value="/usr/bin/codex",
11461162
), mock.patch.object(AUTOREVIEW, "codex_auth_config_flags", return_value=[]), mock.patch.object(
11471163
AUTOREVIEW,
11481164
"prepare_codex_runtime_auth",

0 commit comments

Comments
 (0)