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.

fix(grid): read opencode's grid back from its config file, not from OPENAI_BASE_URL

bubu-duduffb38b7592e0
diff --git a/cli/src/lib/gridAssignment.spec.ts b/cli/src/lib/gridAssignment.spec.ts
index 709026a68..87521fa79 100644
--- a/cli/src/lib/gridAssignment.spec.ts
+++ b/cli/src/lib/gridAssignment.spec.ts
@@ -2,7 +2,7 @@ import { afterEach, describe, expect, it } from 'vitest'
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
-import { assignmentMatches, classifyGridAssignment, readPiGridAssignment } from './gridAssignment.js'
+import { assignmentMatches, classifyGridAssignment, readOpencodeGridAssignment, readPiGridAssignment } from './gridAssignment.js'
import { buildGridEngineLaunch, gridCapableEngines, type GridLaunchOverride } from './gridLaunch.js'
import { clearProcessEnvCache, parsePsEnviron } from './processEnv.js'
@@ -23,23 +23,22 @@ describe('classifyGridAssignment', () => {
// The point of the round trip: the probe and the launcher must use the SAME knob per engine, or
// an agent that IS on a grid reports as being on none and gets pointlessly restarted.
for (const engine of gridCapableEngines()) {
- // Pi's endpoint is in a file rather than the process, so it round-trips through the probe
- // below instead — this one only covers what a process carries.
- if (engine === 'pi') continue
+ // Pi and OpenCode keep their endpoint in a file rather than in the process, so they
+ // round-trip through their own probes below — this one only covers what a process carries.
+ if (engine === 'pi' || engine === 'opencode') continue
const built = buildGridEngineLaunch(engine, OVERRIDE)
expect(built.ok).toBe(true)
if (!built.ok) continue
const assignment = classifyGridAssignment(engine, built.launch.env, built.launch.args.join(' '))
expect(assignment, engine).not.toBeNull()
expect(assignment?.baseUrl, engine).toContain(NETWORK_ID)
- // opencode is the one engine with no verified model knob, so its model stays the app's own.
- expect(assignment?.model, engine).toBe(engine === 'opencode' ? null : 'GLM-4.7-Flash')
+ expect(assignment?.model, engine).toBe('GLM-4.7-Flash')
}
})
it('never carries the credential out of the process', () => {
for (const engine of gridCapableEngines()) {
- if (engine === 'pi') continue
+ if (engine === 'pi' || engine === 'opencode') continue
const built = buildGridEngineLaunch(engine, OVERRIDE)
if (!built.ok) continue
const assignment = classifyGridAssignment(engine, built.launch.env, built.launch.args.join(' '))
@@ -163,3 +162,93 @@ describe('assignmentMatches', () => {
expect(assignmentMatches(unpinned, NETWORK_ID, 'GLM-4.7-Flash')).toBe(false)
})
})
+
+describe('readOpencodeGridAssignment', () => {
+ const dirs: string[] = []
+ afterEach(() => {
+ for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true })
+ })
+
+ // OpenCode's endpoint lives in the config file this daemon wrote, not in a variable — so the probe
+ // reads that file. The regression this guards: while opencode was still listed under
+ // BASE_URL_VAR, moving it to a config file made every opencode agent report "own login" in the
+ // app, with the grid answering correctly underneath.
+ const RELAY = 'https://grid.autonomous.ai/grid-3378218621364f16/relay/v1'
+ const write = (body: unknown): string => {
+ const dir = mkdtempSync(join(tmpdir(), 'harness-opencode-cfg-'))
+ dirs.push(dir)
+ const path = join(dir, 'opencode.json')
+ writeFileSync(path, JSON.stringify(body))
+ return path
+ }
+ const config = (baseURL: string, provider = 'autonomous-ai') => ({
+ provider: { [provider]: { npm: '@ai-sdk/openai-compatible', options: { baseURL } } },
+ })
+
+ it('round-trips the config the LAUNCHER writes, not one written by hand', async () => {
+ // The strongest form of this test, and the one that would have caught the regression: build the
+ // real launch, write its real files where the probe will look, and read them back. A spec that
+ // hand-writes the config proves only that two hand-written shapes agree.
+ const built = buildGridEngineLaunch('opencode', OVERRIDE)
+ if (!built.ok) throw new Error(built.detail)
+ const dir = mkdtempSync(join(tmpdir(), 'opencode-grid-'))
+ dirs.push(dir)
+ for (const file of built.launch.configDir!.files) {
+ writeFileSync(join(dir, file.name), file.content)
+ }
+ const path = join(dir, built.launch.configDir!.pointAt!)
+ const assignment = await readOpencodeGridAssignment(
+ { [built.launch.configDir!.envVar]: path },
+ `opencode ${built.launch.args.join(' ')}`,
+ )
+ expect(assignment).not.toBeNull()
+ expect(assignment!.baseUrl).toContain(NETWORK_ID)
+ expect(assignment!.model).toBe(OVERRIDE.model)
+ // And the credential never leaves the process, exactly as for every other engine.
+ expect(JSON.stringify(assignment)).not.toContain('gridkey-secret')
+ })
+
+ it('reads the endpoint out of the provider the argv selected', async () => {
+ const path = write(config(RELAY))
+ await expect(readOpencodeGridAssignment(
+ { OPENCODE_CONFIG: path },
+ 'opencode -m autonomous-ai/DeepSeek-V4-Flash-0731',
+ )).resolves.toEqual({ baseUrl: RELAY, model: 'DeepSeek-V4-Flash-0731' })
+ })
+
+ it('reports the router as the model when that is what was selected', async () => {
+ const path = write(config(RELAY))
+ await expect(readOpencodeGridAssignment({ OPENCODE_CONFIG: path }, '-m autonomous-ai/Auto'))
+ .resolves.toEqual({ baseUrl: RELAY, model: 'Auto' })
+ })
+
+ it('answers null for a provider the argv did not name', async () => {
+ // Two grids can be configured side by side; reading the wrong block would report an agent as
+ // being on a grid it is not on.
+ const path = write(config(RELAY, 'other-grid'))
+ await expect(readOpencodeGridAssignment({ OPENCODE_CONFIG: path }, '-m autonomous-ai/Auto'))
+ .resolves.toBeNull()
+ })
+
+ it('answers null for an endpoint that is not a relay', async () => {
+ const path = write(config('https://api.openai.com/v1'))
+ await expect(readOpencodeGridAssignment(
+ { OPENCODE_CONFIG: path },
+ '-m autonomous-ai/gpt-4o',
+ )).resolves.toBeNull()
+ })
+
+ it('answers null rather than throwing when the file is gone or unreadable', async () => {
+ await expect(readOpencodeGridAssignment(
+ { OPENCODE_CONFIG: '/nonexistent/opencode.json' },
+ '-m autonomous-ai/Auto',
+ )).resolves.toBeNull()
+ await expect(readOpencodeGridAssignment({}, '-m autonomous-ai/Auto')).resolves.toBeNull()
+ })
+
+ it('answers null when no model was selected on argv', async () => {
+ const path = write(config(RELAY))
+ await expect(readOpencodeGridAssignment({ OPENCODE_CONFIG: path }, 'opencode'))
+ .resolves.toBeNull()
+ })
+})
diff --git a/cli/src/lib/gridAssignment.ts b/cli/src/lib/gridAssignment.ts
index 1827d16a7..193b8bca9 100644
--- a/cli/src/lib/gridAssignment.ts
+++ b/cli/src/lib/gridAssignment.ts
@@ -35,7 +35,9 @@ export interface GridAssignment {
/** The environment variable each engine's endpoint was written to, where it is one. */
const BASE_URL_VAR: Partial<Record<AgentEngine, string>> = {
claude: 'ANTHROPIC_BASE_URL',
- opencode: 'OPENAI_BASE_URL',
+ // opencode is deliberately absent: its endpoint moved into a config file (see
+ // `readOpencodeGridAssignment`), and leaving it here would read a stray OPENAI_BASE_URL from the
+ // user's own shell as proof this agent is on a grid.
hermes: 'OPENAI_BASE_URL',
grok: 'GROK_MODELS_BASE_URL',
copilot: 'COPILOT_PROVIDER_BASE_URL',
@@ -131,6 +133,41 @@ export async function readPiGridAssignment(
}
}
+/** OpenCode's config file, and the `-m <provider>/<model>` that selects out of it. */
+const OPENCODE_CONFIG_VAR = 'OPENCODE_CONFIG'
+const OPENCODE_ARGV_MODEL = /(?:^|\s)(?:-m|--model)[\s=]+([A-Za-z0-9._-]+)\/(\S+)/
+
+/**
+ * OpenCode's endpoint, read out of the config file its process was pointed at.
+ *
+ * Same shape as Pi's, and for the same reason: neither engine carries its endpoint in an environment
+ * variable, so the only honest answer comes from the file the process is actually reading. Reading
+ * our own record of what we launched would answer a different question — what we intended — and that
+ * is precisely the disagreement a probe exists to catch.
+ *
+ * Every failure lands on null. "We cannot tell" and "not on a grid" are treated alike by the caller;
+ * the opposite error, claiming a grid nobody verified, would leave an agent quietly somewhere else.
+ */
+export async function readOpencodeGridAssignment(
+ processEnv: Record<string, string>,
+ args: string,
+): Promise<GridAssignment | null> {
+ const configPath = processEnv[OPENCODE_CONFIG_VAR]?.trim()
+ if (!configPath) return null
+ const selected = OPENCODE_ARGV_MODEL.exec(args)
+ if (!selected) return null
+ try {
+ const raw = JSON.parse(await readFile(configPath, 'utf8')) as {
+ provider?: Record<string, { options?: { baseURL?: unknown } }>
+ }
+ const baseUrl = raw.provider?.[selected[1]]?.options?.baseURL
+ if (typeof baseUrl !== 'string' || !isGridUrl(baseUrl)) return null
+ return { baseUrl, model: selected[2] }
+ } catch {
+ return null
+ }
+}
+
/**
* Read one live engine process's grid.
*
@@ -146,7 +183,9 @@ export async function probeGridAssignment(
): Promise<GridAssignment | null> {
const processEnv = await readProcessEnv(identity)
if (!processEnv) return null
+ // The two engines whose provider lives in a file this daemon wrote, rather than in a variable.
if (engine === 'pi') return await readPiGridAssignment(processEnv, args)
+ if (engine === 'opencode') return await readOpencodeGridAssignment(processEnv, args)
return classifyGridAssignment(engine, processEnv, args)
}