Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Harness Team account: Codex on the relay keeps the owner's model list, and starts again

bitcoinbuilderaab3905dbb4e
diff --git a/cli/src/lib/subscriptionRelay.ts b/cli/src/lib/subscriptionRelay.ts
index 0d00efdc0..49a2e46e2 100644
--- a/cli/src/lib/subscriptionRelay.ts
+++ b/cli/src/lib/subscriptionRelay.ts
@@ -280,43 +280,11 @@ export function codexRelayEnv(token: string): Record<string, string> {
return { [RELAY_TOKEN_VAR]: token }
}
-/**
- * Codex on the relay as a ChatGPT sign-in, not an API key: the provider asks for the sign-in in `auth.json`, which holds
- * [codexRelayAuth] (the relay token, nothing of the owner's). Through an API-key provider Codex never reads the
- * account's model list, so its `/model` picker offered only its built-in models and the agent stayed on `gpt-5.5`,
- * which ChatGPT refuses for an account it does not offer to ("The 'gpt-5.5' model is not supported when using Codex
- * with a ChatGPT account": every Codex start in a Harness Team account, 2026-10-29). As a sign-in, Codex reads the
- * owner's list through the relay (`/models`, kept in `models_cache.json`), and the model a start asks for is there.
- */
-export function codexRelaySignInArgs(relayUrl: string): string[] {
- const p = OWNER_CODEX_PROVIDER
- return [
- '-c', `model_provider="${p}"`,
- '-c', `model_providers.${p}.name="Owner subscription"`,
- '-c', `model_providers.${p}.base_url="${relayUrl.replace(/\/$/, '')}/codex"`,
- '-c', `model_providers.${p}.requires_openai_auth=true`,
- '-c', `model_providers.${p}.wire_api="responses"`,
- '-c', `model_providers.${p}.supports_websockets=false`,
- ]
-}
-
-/** The account id a relay sign-in names: what tells it apart from a sign-in of the account's own (`isCodexRelayAuth`).
- * The relay puts the owner's own account id on every request in its place. */
+/** The account id CLI 0.5.1 wrote into a Harness Team account's Codex `auth.json` for the relay, a sign-in Codex's own
+ * screen could not start with (`workspace routing discovery unauthorized (401)`, 2026-10-29). Read only to remove it. */
export const CODEX_RELAY_ACCOUNT = 'harness-relay'
-/**
- * The `auth.json` Codex reads for [codexRelaySignInArgs]: the relay token as its access token, and an id token that is
- * no secret (unsigned, no expiry: Codex reads only its plan from it). `last_refresh` is now, and written again at each
- * launch, so Codex never asks OpenAI to refresh it: the owner's daemon refreshes the owner's own sign-in.
- */
-export function codexRelayAuth(token: string, now: number): string {
- const part = (value: unknown) => Buffer.from(JSON.stringify(value)).toString('base64url')
- const idToken = `${part({ alg: 'none', typ: 'JWT' })}.${part({ 'https://api.openai.com/auth': { chatgpt_plan_type: 'plus', chatgpt_account_id: CODEX_RELAY_ACCOUNT } })}.relay`
- return `${JSON.stringify({ OPENAI_API_KEY: null, tokens: { id_token: idToken, access_token: token, refresh_token: CODEX_RELAY_ACCOUNT,
- account_id: CODEX_RELAY_ACCOUNT }, last_refresh: new Date(now).toISOString() }, null, 2)}\n`
-}
-
-/** Whether a parsed `auth.json` is [codexRelayAuth]'s: the relay's, not a sign-in of the account's own. */
+/** Whether a parsed `auth.json` is that left-over sign-in: the relay's, not one of the account's own. */
export function isCodexRelayAuth(auth: unknown): boolean {
const tokens = auth && typeof auth === 'object' ? (auth as { tokens?: unknown }).tokens : null
return !!tokens && typeof tokens === 'object' && (tokens as { account_id?: unknown }).account_id === CODEX_RELAY_ACCOUNT
diff --git a/cli/src/team/relay.spec.ts b/cli/src/team/relay.spec.ts
index b4d116412..dfa7f8ef2 100644
--- a/cli/src/team/relay.spec.ts
+++ b/cli/src/team/relay.spec.ts
@@ -1,12 +1,12 @@
-import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it, onTestFinished, vi } from 'vitest'
-import { CODEX_RELAY_ACCOUNT, codexRelayAuth, isCodexRelayAuth, type SubscriptionRelay } from '../lib/subscriptionRelay.js'
+import { CODEX_RELAY_ACCOUNT, type SubscriptionRelay } from '../lib/subscriptionRelay.js'
import { engineAccount } from '../lib/engineAccount.js'
import type { TeamConfig } from './config.js'
import {
- publishTeamRelay, readTeamRelay, TEAM_RELAY_GUEST, teamRelayDir, teamRelayFile, teamRelayLaunch, teamRelayPublished, TeamRelayPublisher, writeCodexRelaySignIn,
+ publishTeamRelay, readTeamRelay, TEAM_RELAY_GUEST, teamRelayDir, teamRelayFile, teamRelayLaunch, teamRelayPublished, TeamRelayPublisher, prepareCodexHome, installedCodexVersion, CODEX_CLIENT_VERSION_FALLBACK,
type TeamRelayLaunchDeps, type TeamRelayPublisherDeps, type TeamRelayReadDeps,
} from './relay.js'
@@ -149,7 +149,7 @@ describe('the team daemon, reading its owner\'s relay', () => {
describe('a launch in the team account', () => {
const grant = { url: 'http://127.0.0.1:9', token: TOKEN }
const launchDeps = (over: Partial<TeamRelayLaunchDeps> = {}): TeamRelayLaunchDeps => ({
- team: () => true, config: () => config, signedIn: async () => false, read: () => grant, onboarded: vi.fn(), codexSignIn: vi.fn(() => true), ...over,
+ team: () => true, config: () => config, signedIn: async () => false, read: () => grant, onboarded: vi.fn(), codexHome: vi.fn(async () => {}), ...over,
})
it('spends the owner\'s subscription for an engine it has not signed in itself', async () => {
@@ -159,15 +159,12 @@ describe('a launch in the team account', () => {
const codex = await teamRelayLaunch('codex', deps)
expect(codex?.env).toEqual({ HARNESS_RELAY_TOKEN: TOKEN })
expect(codex?.args).toContain('model_provider="harness_owner"')
- // As a ChatGPT sign-in through the relay, so Codex has the owner's models (2026-10-29: every start stayed on gpt-5.5).
- expect(deps.codexSignIn).toHaveBeenCalledWith(TOKEN)
- expect(codex?.args).toContain('model_providers.harness_owner.requires_openai_auth=true')
- expect(codex?.args.join(' ')).not.toContain('env_key')
- // A sign-in it could not write leaves Codex on the relay as an API key, as before.
- for (const codexSignIn of [() => false, () => { throw new Error('read-only') }]) {
- const fallback = await teamRelayLaunch('codex', launchDeps({ codexSignIn }))
- expect(fallback?.args).toContain('model_providers.harness_owner.env_key="HARNESS_RELAY_TOKEN"')
- }
+ // Its home is made ready first (the owner's model list), and Codex stays an API-key provider of the relay.
+ expect(deps.codexHome).toHaveBeenCalledWith(grant)
+ expect(codex?.args).toContain('model_providers.harness_owner.env_key="HARNESS_RELAY_TOKEN"')
+ expect(codex?.args.join(' ')).not.toContain('requires_openai_auth')
+ // A home that could not be made ready still launches, as before.
+ expect((await teamRelayLaunch('codex', launchDeps({ codexHome: async () => { throw new Error('read-only') } })))?.args).toContain('model_provider="harness_owner"')
// A first-run screen that cannot be recorded is left for the engine to ask.
expect(await teamRelayLaunch('claude', launchDeps({ onboarded: () => { throw new Error('read-only') } }))).not.toBeNull()
expect(await teamRelayLaunch('codex', launchDeps({ signedIn: async () => { throw new Error('keychain') } }))).not.toBeNull()
@@ -182,32 +179,55 @@ describe('a launch in the team account', () => {
})
})
-describe('Codex\'s sign-in for the relay', () => {
+describe('Codex\'s home for the relay', () => {
const home = () => { const dir = mkdtempSync(join(tmpdir(), 'team-codex-')); onTestFinished(() => rmSync(dir, { recursive: true, force: true })); return dir }
-
- it('writes the relay token as a ChatGPT sign-in, private to the account, and again at each launch', () => {
- const dir = join(home(), '.codex')
- expect(writeCodexRelaySignIn(dir, TOKEN, Date.parse('2026-10-29T04:00:00Z'))).toBe(true)
- const auth = JSON.parse(readFileSync(join(dir, 'auth.json'), 'utf8'))
- expect(auth.tokens).toMatchObject({ access_token: TOKEN, account_id: CODEX_RELAY_ACCOUNT })
- expect(auth.last_refresh).toBe('2026-10-29T04:00:00.000Z')
- expect(isCodexRelayAuth(auth)).toBe(true)
- expect(statSync(join(dir, 'auth.json')).mode & 0o777).toBe(0o600)
- // Codex reads its plan from the id token: three parts, the claims base64url JSON.
- const claims = JSON.parse(Buffer.from(auth.tokens.id_token.split('.')[1], 'base64url').toString())
- expect(claims['https://api.openai.com/auth']).toMatchObject({ chatgpt_account_id: CODEX_RELAY_ACCOUNT })
- expect(writeCodexRelaySignIn(dir, `${TOKEN}x`, Date.parse('2026-10-30T04:00:00Z'))).toBe(true)
- expect(JSON.parse(readFileSync(join(dir, 'auth.json'), 'utf8')).tokens.access_token).toBe(`${TOKEN}x`)
+ const grant = { url: 'http://127.0.0.1:9', token: TOKEN }
+ const models = [{ slug: 'gpt-6.1-sol', visibility: 'list' }, { slug: 'gpt-6-astra', visibility: 'list' }]
+ const answer = (status = 200, body: unknown = { models }) => vi.fn(async () => new Response(JSON.stringify(body), { status }))
+
+ // 2026-10-29: Codex on the relay knew only its built-in models, so a start's gpt-6.1-sol could not be chosen and it ran
+ // gpt-5.5, which ChatGPT refuses for the owner's account.
+ it('keeps the owner\'s model list where Codex reads it, asked through the relay with the relay token', async () => {
+ const dir = join(home(), '.codex'), fetch = answer()
+ await prepareCodexHome(dir, grant, { fetch: fetch as never, version: () => '0.165.0', now: () => Date.parse('2026-10-29T05:00:00Z') })
+ expect(fetch).toHaveBeenCalledWith('http://127.0.0.1:9/codex/models?client_version=0.165.0', expect.objectContaining({
+ headers: { authorization: `Bearer ${TOKEN}`, accept: 'application/json' } }))
+ const cache = JSON.parse(readFileSync(join(dir, 'models_cache.json'), 'utf8'))
+ expect(cache).toEqual({ fetched_at: '2026-10-29T05:00:00.000Z', client_version: '0.165.0', models })
+ expect(statSync(join(dir, 'models_cache.json')).mode & 0o777).toBe(0o600)
+ // An answer that is not a list, a refusal or no answer leaves the list there was.
+ for (const fetch of [answer(503), answer(200, { models: [] }), answer(200, { nope: 1 }), vi.fn(async () => { throw new Error('down') })]) {
+ await prepareCodexHome(dir, grant, { fetch: fetch as never, version: () => null, now: () => 0 })
+ expect(JSON.parse(readFileSync(join(dir, 'models_cache.json'), 'utf8')).client_version).toBe('0.165.0')
+ }
})
- it('never replaces a sign-in of the account\'s own, which is read as signed in while the relay\'s is not', async () => {
- const root = home(), dir = join(root, '.codex')
+ it('removes the relay sign-in CLI 0.5.1 wrote, which Codex\'s screen could not start with, and never the account\'s own', async () => {
+ const dir = join(home(), '.codex')
mkdirSync(dir)
- writeFileSync(join(dir, 'auth.json'), JSON.stringify({ tokens: { access_token: 'own', refresh_token: 'own', account_id: 'acct-1' } }))
- expect(writeCodexRelaySignIn(dir, TOKEN, 0)).toBe(false)
+ writeFileSync(join(dir, 'auth.json'), JSON.stringify({ tokens: { access_token: TOKEN, account_id: CODEX_RELAY_ACCOUNT } }))
+ await prepareCodexHome(dir, grant, { fetch: answer(503) as never, version: () => null, now: () => 0 })
+ expect(existsSync(join(dir, 'auth.json'))).toBe(false)
+ writeFileSync(join(dir, 'auth.json'), JSON.stringify({ tokens: { access_token: 'own', account_id: 'acct-1' } }))
+ await prepareCodexHome(dir, grant, { fetch: answer(503) as never, version: () => null, now: () => 0 })
expect(JSON.parse(readFileSync(join(dir, 'auth.json'), 'utf8')).tokens.account_id).toBe('acct-1')
- expect((await engineAccount('codex', { home: root, env: {}, lastUsed: false })).signedIn).toBe(true)
- writeFileSync(join(dir, 'auth.json'), codexRelayAuth(TOKEN, 0))
+ })
+
+ it('asks for the version of the Codex the account installed, or a known one', async () => {
+ const root = home()
+ expect(installedCodexVersion(root)).toBeNull()
+ mkdirSync(join(root, '.local', 'lib', 'node_modules', '@openai', 'codex'), { recursive: true })
+ writeFileSync(join(root, '.local', 'lib', 'node_modules', '@openai', 'codex', 'package.json'), JSON.stringify({ version: '0.165.0' }))
+ expect(installedCodexVersion(root)).toBe('0.165.0')
+ const fetch = answer()
+ await prepareCodexHome(join(root, '.codex'), grant, { fetch: fetch as never, version: () => null, now: () => 0 })
+ expect(String((fetch.mock.calls[0] as unknown[])[0])).toContain(`client_version=${CODEX_CLIENT_VERSION_FALLBACK}`)
+ })
+
+ it('reads the relay\'s left-over sign-in as Codex not signed in', async () => {
+ const root = home()
+ mkdirSync(join(root, '.codex'))
+ writeFileSync(join(root, '.codex', 'auth.json'), JSON.stringify({ tokens: { access_token: TOKEN, account_id: CODEX_RELAY_ACCOUNT } }))
expect((await engineAccount('codex', { home: root, env: {}, lastUsed: false })).signedIn).toBe(false)
})
})
diff --git a/cli/src/team/relay.ts b/cli/src/team/relay.ts
index 76f052888..c84bd5971 100644
--- a/cli/src/team/relay.ts
+++ b/cli/src/team/relay.ts
@@ -25,7 +25,7 @@ import type { AgentEngine } from '../engines/types.js'
import { recordAcceptanceIn } from '../engines/kit/folderTrust.js'
import { readAuthSession } from '../lib/authSession.js'
import { engineAccount } from '../lib/engineAccount.js'
-import { claudeRelayEnv, codexRelayArgs, codexRelayEnv, codexRelayAuth, codexRelaySignInArgs, isCodexRelayAuth, type SubscriptionRelay } from '../lib/subscriptionRelay.js'
+import { claudeRelayEnv, codexRelayArgs, codexRelayEnv, isCodexRelayAuth, type SubscriptionRelay } from '../lib/subscriptionRelay.js'
import { readTeamConfig, type TeamConfig } from './config.js'
/** The owner's folder for the hand-off, per owner so two people's Macs never share one. */
@@ -219,9 +219,8 @@ export interface TeamRelayLaunchDeps {
/** Claude Code that never ran in the team account opens on its first-run screens, which nobody is there to answer:
* recorded as done, in the team account's own home, before the first launch on the relay. */
onboarded(engine: 'claude' | 'codex'): void
- /** Codex's sign-in for the relay (lib/subscriptionRelay.ts codexRelayAuth), in the team account's own Codex home;
- * false when it could not be written. */
- codexSignIn(token: string): boolean
+ /** Codex's home made ready for the relay before each launch ([prepareCodexHome]). Never throws. */
+ codexHome(grant: TeamRelayGrant): Promise<void>
}
/** In the team daemon, for an engine it has not signed in itself: the owner's relay, when there is one. */
@@ -235,24 +234,58 @@ export async function teamRelayLaunch(engine: AgentEngine, deps: TeamRelayLaunch
if (!grant) return null
try { deps.onboarded(engine) } catch { /* The engine asks on its screen, as it would have. */ }
if (engine === 'claude') return { env: claudeRelayEnv(grant.url, grant.token), args: [] }
- // As a ChatGPT sign-in, so Codex has the owner's models (codexRelaySignInArgs); as an API key, as before, when the
- // sign-in could not be written.
- let signIn = false
- try { signIn = deps.codexSignIn(grant.token) } catch { signIn = false }
- return { env: codexRelayEnv(grant.token), args: signIn ? codexRelaySignInArgs(grant.url) : codexRelayArgs(grant.url) }
+ await deps.codexHome(grant).catch(() => {})
+ return { env: codexRelayEnv(grant.token), args: codexRelayArgs(grant.url) }
}
-/** Writes the relay's sign-in to [dir]/auth.json, unless a sign-in of the account's own is there. Private to the account. */
-export function writeCodexRelaySignIn(dir: string, token: string, now: number): boolean {
- const file = join(dir, 'auth.json')
- let existing: unknown = null
- try { existing = JSON.parse(readFileSync(file, 'utf8')) } catch { existing = null }
- if (existing && !isCodexRelayAuth(existing)) return false
- mkdirSync(dir, { recursive: true, mode: 0o700 })
- const next = `${file}.harness-relay-${process.pid}`
- writeFileSync(next, codexRelayAuth(token, now), { mode: 0o600 })
- renameSync(next, file)
- return true
+/** The Codex version a model list is asked for when the account's own Codex cannot be read. */
+export const CODEX_CLIENT_VERSION_FALLBACK = '0.163.0'
+
+export interface CodexHomeDeps {
+ fetch: typeof fetch
+ /** The account's own Codex version (the npm package it installed), or null. */
+ version(): string | null
+ now(): number
+}
+
+/**
+ * The team account's Codex home made ready for the owner's relay, before each Codex launch.
+ *
+ * Codex reaches the relay as an API-key provider, and so never reads the account's model list: its /model picker offered
+ * only its built-in models, the model a Swarm start asked for (gpt-6.1-sol) could not be chosen, and the agent stayed on
+ * gpt-5.5, which ChatGPT does not offer the owner's account ("The 'gpt-5.5' model is not supported when using Codex with
+ * a ChatGPT account": every Codex start in a Harness Team account, 2026-10-29). Codex does read a `models_cache.json` in
+ * its home whatever the provider, so the owner's list is fetched through the relay and kept there: Codex then starts on
+ * the owner's default and its picker offers the owner's models. A list that cannot be fetched leaves the one before.
+ *
+ * CLI 0.5.1 instead signed Codex in to ChatGPT through the relay, which `codex exec` takes but Codex's own screen does not:
+ * it checks the workspace with ChatGPT before it opens and exited ("account/read failed during TUI bootstrap"). Its
+ * sign-in is removed here, so a team account it reached starts Codex again.
+ */
+export async function prepareCodexHome(dir: string, grant: TeamRelayGrant, deps: CodexHomeDeps): Promise<void> {
+ const auth = join(dir, 'auth.json')
+ try { if (isCodexRelayAuth(JSON.parse(readFileSync(auth, 'utf8')))) rmSync(auth, { force: true }) } catch { /* none, or the account's own */ }
+ const version = deps.version() ?? CODEX_CLIENT_VERSION_FALLBACK
+ try {
+ const answer = await deps.fetch(`${grant.url.replace(/\/$/, '')}/codex/models?client_version=${encodeURIComponent(version)}`, {
+ headers: { authorization: `Bearer ${grant.token}`, accept: 'application/json' }, signal: AbortSignal.timeout(10_000),
+ })
+ if (!answer.ok) return
+ const models = ((await answer.json()) as { models?: unknown }).models
+ if (!Array.isArray(models) || !models.length) return
+ mkdirSync(dir, { recursive: true, mode: 0o700 })
+ const file = join(dir, 'models_cache.json'), next = `${file}.harness-${process.pid}`
+ writeFileSync(next, JSON.stringify({ fetched_at: new Date(deps.now()).toISOString(), client_version: version, models }), { mode: 0o600 })
+ renameSync(next, file)
+ } catch { /* The relay or ChatGPT did not answer: Codex keeps the list it had. */ }
+}
+
+/** The version of the Codex the team account installed for itself (team/script.ts TEAM_ENGINES_INSTALL, in ~/.local). */
+export function installedCodexVersion(home: string): string | null {
+ try {
+ const version = (JSON.parse(readFileSync(join(home, '.local', 'lib', 'node_modules', '@openai', 'codex', 'package.json'), 'utf8')) as { version?: unknown }).version
+ return typeof version === 'string' && /^\d+\.\d+\.\d+/.test(version) ? version : null
+ } catch { return null }
}
/** The team daemon's own: its sign-in, the root-owned config, its engines' sign-ins in its own home. */
@@ -263,7 +296,8 @@ export const realTeamRelayLaunch = (engine: AgentEngine) => teamRelayLaunch(engi
signedIn: async (e) => (await engineAccount(e, { home: homedir(), env: process.env, lastUsed: false })).signedIn === true,
read: (config) => readTeamRelay(config),
onboarded: (e) => { if (e === 'claude') recordAcceptanceIn(join(homedir(), '.claude.json'), 'hasCompletedOnboarding') },
- codexSignIn: (token) => writeCodexRelaySignIn(process.env.CODEX_HOME || join(homedir(), '.codex'), token, Date.now()),
+ codexHome: (grant) => prepareCodexHome(process.env.CODEX_HOME || join(homedir(), '.codex'), grant,
+ { fetch, version: () => installedCodexVersion(homedir()), now: Date.now }),
})
/** In the team daemon: whether the owner's relay is there for its engines (superTabs/machines.ts readiness). */