Skip to content
Draft
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
45 changes: 41 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,17 @@ npx @expo/code-review-cli doctor
The scaffolded default is **Anthropic via the Claude Code CLI**: locally your
`claude` login is enough, and for CI it helps you mint a token with
`claude setup-token`. It also handles the alternatives (an OpenAI **API key**,
a **ChatGPT/Codex subscription** sign-in, or a Meta Model API key for **Muse
Spark**). `doctor` offers to run it whenever a credential is missing.
a **ChatGPT/Codex subscription** sign-in, a **SuperGrok subscription** sign-in,
or a Meta Model API key for **Muse Spark**). `doctor` offers to run it whenever a
credential is missing.

In CI, store the credential as the repo secret the scaffolded workflow forwards
(`CLAUDE_CODE_REVIEW_SHARED_API_TOKEN` by default — an `sk-ant-oat…` token from
`claude setup-token`, or an `sk-ant-api…` Console key; the CLI reads either).

Prefer **Muse Spark**, **OpenAI** (API key, or a ChatGPT/Codex subscription, or
both mixed), or another provider? See [Other providers & auth modes](#other-providers)
below.
both mixed), **Grok** (SuperGrok or an xAI API key), or another provider? See
[Other providers & auth modes](#other-providers) below.

### Reviewing (already configured)

Expand Down Expand Up @@ -997,6 +998,42 @@ fallback key; `ecr doctor` diagnoses setup.
`$0` cost — OpenCode can't price config-declared aliases; the OpenAI dashboard
is the source of truth for spend).

- **xAI / Grok.** Models stay `xai/<id>` (for example `xai/grok-4.7`) on the
OpenCode engine. A SuperGrok subscription and a console API key are different
credentials.

```jsonc
"auth": { "providers": {
"xai": { "mode": "oauth", "tokenEnv": "XAI_OAUTH_ACCESS_TOKEN" }
} }
```

```jsonc
"auth": { "providers": {
"xai": { "mode": "api-key", "tokenEnv": "XAI_API_KEY" }
} }
```

`ecr setup-auth` runs the bundled
`opencode auth login -p xai -m "SuperGrok Subscription"`. That prints a device
code for <https://accounts.x.ai/oauth2/device>. The command then exports the
**access** token from OpenCode's store. The refresh token stays there. Reviews
copy that access token into an isolated `auth.json` (empty `refresh`) and do
not set `XAI_API_KEY` from it. An API key is created at
<https://console.x.ai> and exported as `XAI_API_KEY`. `mode: "oauth"` caps
concurrency at 3. An API key stays at 6.

The access token lasts 6 hours, and a review cannot renew it. So the SuperGrok
mode suits local runs: a CI secret goes stale the same day unless a job
re-mints it.
`ecr doctor` and the review both warn when under two hours are left. The
review stops before any pass when 120 seconds or less are left. Run
`ecr setup-auth` again for a new token. It signs in again unless a review
would take the current token without a warning.

`XAI_OAUTH_ACCESS_TOKEN` needs `"mode": "oauth"`. An entry that names it
without that mode is refused.

- **Another provider** — `opencode auth login` once, then run with e.g.
`REVIEWER_MODEL=google/gemini-3-pro`. No `auth` block needed.

Expand Down
11 changes: 10 additions & 1 deletion llp/0001-trust-model.principles.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,16 @@ in the deny-list — an anthropic entry may legitimately name them — but a
cross-provider guard refuses a non-anthropic entry that names one. Without it,
`{provider:"openai", tokenEnv:"CLAUDE_CODE_OAUTH_TOKEN"}` would slip past (neither
a forbidden secret nor a provider key) and forward the Anthropic subscription
token to a foreign provider [observed] (`src/core/auth.ts:19-33`, commit 93aaf30).
token to a foreign provider [observed] (`src/core/auth.ts` `ANTHROPIC_TOKEN_ENVS`, commit 93aaf30).
`XAI_OAUTH_ACCESS_TOKEN` belongs to `xai` the same way (`XAI_TOKEN_ENVS`).
`XAI_API_KEY` belongs to `xai` through `PROVIDER_KEY_ENV`. A non-xai entry that
names either is refused, and `upstream: "xai"` does not make another provider id
an owner. `CODEX_OAUTH_ACCESS_TOKEN` and `CODEX_OAUTH_REFRESH_TOKEN` belong to
`openai` (`CODEX_OAUTH_TOKEN_ENVS`): a ChatGPT access token is a JWT, so the xAI
shape check alone would accept it. Only an api-key alias may borrow its
upstream's key. An oauth entry never does, because it is staged under its own
`provider` [observed] (`src/core/auth.ts` `checkAuthEntry`; tests in
`src/__tests__/auth.test.ts`).

The credential must never reach logs, error messages, or `.expo-code-review/.runs/`.
This is a named cross-cutting invariant, not an incidental property [observed]
Expand Down
57 changes: 49 additions & 8 deletions llp/0003-model-runtimes-and-credentials.explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,14 @@ auto-review path it can be PR-controlled.
ownership guard: a non-anthropic entry naming an Anthropic token env is refused.
Without that guard, `{provider:"openai", tokenEnv:"CLAUDE_CODE_OAUTH_TOKEN"}` passes
both other checks and forwards the Anthropic subscription token to a foreign provider
([observed] `auth.ts:20-34`; commit `93aaf30`).
([observed] `auth.ts` `ANTHROPIC_TOKEN_ENVS`; commit `93aaf30`). The same guard
covers `XAI_TOKEN_ENVS`: a non-xai entry that names `XAI_OAUTH_ACCESS_TOKEN` is
refused, and `XAI_API_KEY` is owned through `PROVIDER_KEY_ENV`.
`CODEX_OAUTH_TOKEN_ENVS` gives both Codex names to `openai`. `upstream` counts as
the owner only for an api-key alias. An oauth entry is staged under its own
`provider`, so `upstream` decides nothing there. An xAI credential never passes
through an alias: an `xai` alias is synthesized with the openai-compatible SDK and
no host ([observed] `auth.ts` `checkAuthEntry`; `opencode.ts` `buildOpencodeConfig`).

**Scoped to providers in use.** Auth checks (`checkProviderAuth`) and credential
forwarding are scoped to only the providers a resolved model actually routes to. A dead
Expand All @@ -366,12 +373,42 @@ secret-forwarding the guard prevents ([observed] `auth.ts:378-392`).
**Isolated staging, engines kept separate.** `prepareAuth` writes all OAuth credentials
into one **isolated** OpenCode `auth.json` under a temp dir pointed to by
`XDG_DATA_HOME`, so a run's injected credentials never touch the developer's real
`auth.json` ([observed] `auth.ts:557-560`). It must run and complete before the server
`auth.json` ([observed] `auth.ts` `prepareAuth`). It must run and complete before the server
starts, because it mutates env the spawned server reads — documented as a precondition,
not a convenience ([observed] `auth.ts:479-482`). Anthropic credentials **never** pass
not a convenience ([observed] `auth.ts` `prepareAuth`). Anthropic credentials **never** pass
through this path at all: anthropic is Claude-engine-only, so its credential is injected
per-invocation into the Claude CLI child env by `startClaudeCode`, keeping the two
engines' credential plumbing fully separate ([observed] `auth.ts:527-532`).
engines' credential plumbing fully separate ([observed] `auth.ts` `prepareAuth`;
`claude-code.ts` `claudeTokenCredential`).

**xAI follows the Codex file, not the Claude child env.** A `xai` oauth review writes
the SuperGrok access token into that same isolated `auth.json`: `type` `oauth`, `access`
set, `refresh` empty, `expires` from the JWT `exp` claim. `prepareAuth` does not copy
that token into `XAI_API_KEY`. An api-key entry that names `XAI_OAUTH_ACCESS_TOKEN` is
refused: `api-key` is the schema default, and it would send the token as a key with no
expiry check. An opaque xAI value is refused before any pass, and the
message names `ecr setup-auth`. A JWT that is expired, or that has 120 seconds or less
left, fails the same way, because the isolated entry has no refresh token. 120 seconds
is the point where OpenCode would refresh on the first request. Under about
two hours left, `doctor` and the run both warn and continue: `prepareAuth` returns the
warnings of the tokens it staged, and `runReview` prints them. The hard failure stays
at 120 seconds on purpose. A token that lapses later still fails the passes that run
then, and the warning says so. The token is short-lived: an access token from the
pinned OpenCode's SuperGrok login had `exp − iat` = 21600 seconds, and OpenCode stored
the same deadline in `expires` ([observed] 2026-11-27). The pinned OpenCode sends the
staged token as a bearer to `https://api.x.ai/v1/responses`, because no `provider.xai`
block changes the SDK's default host ([observed] 2026-11-27 with a placeholder token,
which that host refused with 400). With a real token staged the same way, one direct
request and one local review on `xai/grok-4.7` completed ([observed] 2026-11-27; the
token's scope held `grok-cli:access` and `api:access`). The user's
`~/.local/share/opencode/auth.json` is written by OpenCode during
`opencode auth login` and is only read by setup-auth, which exports the access token.
API-key mode copies the configured env into `XAI_API_KEY` (`PROVIDER_KEY_ENV`) and
writes no oauth entry. An `xai` api-key entry with no `tokenEnv` and no `XAI_API_KEY`
still passes and relies on OpenCode's own login, as it did before `XAI_API_KEY` had an
owner. `XAI_OAUTH_ACCESS_TOKEN` belongs to `xai` the same way
`CLAUDE_CODE_OAUTH_TOKEN` belongs to `anthropic` ([observed] `auth.ts`
`checkOauthTokenShape`, `XAI_TOKEN_ENVS`, `oauthAuthJsonEntry`, `prepareAuth`).

**A deliberate re-check at the forwarding site.** The deny-list check (`checkAuthEntry`)
is re-run at the Claude CLI credential-forwarding site inside `startClaudeCode`, in
Expand All @@ -388,16 +425,20 @@ not a hole, because it requires an explicit env var the CI workflow never sets
credential by what the token **is**: a JWT access token (three base64url segments) is
used as-is with its own `exp` claim and never refreshed; an opaque OpenAI value is
stored with `expires: 0` so OpenCode's codex plugin mints access tokens via its refresh
flow. Mixing the two breaks because refresh tokens are **single-use** — the
flow. That opaque-as-refresh branch is **openai-only**. A JWT for any other provider,
including xAI, is staged as the access token with an empty `refresh`. Mixing the two
breaks because refresh tokens are **single-use** — the
refresh-token-as-static-secret strategy died in production (every pass "Token refresh
failed: 401"; the shared copy was spent by its first use). The opaque strategy is only
safe when the run is the token's **sole** consumer; a value shared across runs or repos
dies on first rotation ([observed] `auth.ts:452-477`; commit `188f852`, euxy#8). The
hard-fail bar for `checkOauthTokenShape` is deliberately narrow — "this cannot be a
dies on first rotation ([observed] `auth.ts` `oauthAuthJsonEntry`; commit `188f852`, euxy#8). The
hard-fail bar for `checkOauthTokenShape` is deliberately narrow for providers whose
format we do not know — "this cannot be a
valid credential" (whitespace, absurd length, an API key in OAuth mode, a
provably-mangled `sk-` prefix), not "this looks odd" — because a false rejection blocks a
working setup, which is judged worse than the failure being prevented ([observed]
`auth.ts:88-101`).
`auth.ts` `checkOauthTokenShape`). xAI is judged more strictly: the value must be a
JWT access token, because the isolated entry cannot refresh.

## Subprocess Spawning Rules

Expand Down
5 changes: 3 additions & 2 deletions llp/0007-cli-commands-and-ci.explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ doctor resolves auth the way real reviews do: a `routing.jsonc` `defaults.auth`
`ecr setup-auth` derives a plan from the repo's auth config via a pure `planFromAuth`, then guides local credential acquisition. Its decisions:

- It hands out only ACCESS tokens; the refresh token never leaves OpenCode's store, because OpenCode's refresh tokens are SINGLE-USE (rotation) and OpenCode is their sole legitimate consumer — a copy in a shell config or CI secret dies on the next rotation and can take the whole sign-in down with it [observed: `src/commands/setup-auth.ts:87-93`].
- The exported shell line single-quotes the token value so a token containing shell metacharacters (`$HOME`, `!`) is never expanded when sourced [observed: `src/commands/setup-auth.ts:127-131`; test in `src/__tests__/setup-auth.test.ts`].
- The exported shell line single-quotes the token value so a token containing shell metacharacters (`$HOME`, `!`) is never expanded when sourced [observed: `src/commands/setup-auth.ts:127-131`; test in `src/__tests__/setup-auth.test.ts`]. A single quote in the value is escaped. The env name is printed unquoted, and the config that names it comes from the repo, so the schema refuses an `auth` `tokenEnv` that is not an env var name. That check sits on the `auth` union, not on the field: a field error in the map shape would fall through to the legacy shape and load the default auth [observed: `src/commands/setup-auth.ts` `exportLine`; `src/config/schema.ts` `ENV_VAR_NAME`; test in `src/__tests__/config-load.test.ts`].
- `anthropic` auth is always satisfied by the Claude Code CLI subscription-login flow regardless of the declared `mode` (oauth vs api-key) — mode is irrelevant for that provider [observed: `src/commands/setup-auth.ts:50-59`].
- A `meta` API-key entry is a manual-key plan: setup-auth points to the Meta AI developer portal and prints the configured `META_API_KEY` export, while doctor uses the same generic provider readiness check as the real review [observed: `src/commands/setup-auth.ts` `planFromAuth` / manual-key guidance; `src/core/auth.ts` `PROVIDER_KEY_ENV`].
- The two interactive logins run via `spawnSync` with `stdio: "inherit"` and `cwd: os.tmpdir()` — the tmpdir cwd keeps a possibly-untrusted checkout from becoming the login subprocess's working directory, and the `inherit` stdio is why these two calls cannot be converted to `exec.ts`'s capturing `run()` [observed: `src/commands/setup-auth.ts:200-203,256-259`]. `ecr attach` is interactive too, but it stays inside `exec.ts` via `spawnInherited` because the opened session needs the terminal (see [Session archive](#session-archive)).
- A `xai` oauth entry is a SuperGrok login. setup-auth runs the bundled CLI as `opencode auth login -p xai -m "SuperGrok Subscription"` (`SUPERGROK_LOGIN_ARGS`), with cwd `os.tmpdir()`. OpenCode prints a device code for `https://accounts.x.ai/oauth2/device`. setup-auth then reads `xai.access` from OpenCode's own auth.json and exports that access token. The refresh token stays in the store. A stored token, or one already in the token env, is reused only when a review would take it with no error and no expiry warning. Otherwise setup-auth signs in again, because the review names this command as the fix for both. setup-auth cannot refresh: after the token lapses, it runs the device login again. An xAI api-key entry stays manual and points at `https://console.x.ai` [observed: `src/commands/setup-auth.ts` `planFromAuth`, `accessTokenFromStore`, `superGrokTokenComplaint`].
- The two interactive logins run via `spawnSync` with `stdio: "inherit"` and `cwd: os.tmpdir()` — the tmpdir cwd keeps a possibly-untrusted checkout from becoming the login subprocess's working directory, and the `inherit` stdio is why these two calls cannot be converted to `exec.ts`'s capturing `run()` [observed: `src/commands/setup-auth.ts:200-203,256-259`]. The temp dir alone is not enough for OpenCode, which also reads `opencode.json` and `.opencode/` from every parent of its cwd, and the temp dir is the shared `/tmp` on Linux. So the OpenCode logins set `OPENCODE_DISABLE_PROJECT_CONFIG=1` [observed: `src/commands/setup-auth.ts` `opencodeLoginEnv`; the pinned `opencode debug config` loads a parent directory's `opencode.json` without the flag and not with it]. `ecr attach` is interactive too, but it stays inside `exec.ts` via `spawnInherited` because the opened session needs the terminal (see [Session archive](#session-archive)).

## init and dismiss

Expand Down
Loading
Loading