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): stop opencode's grid answer depending on a live process's argv

bubu-dudubb90948950af
diff --git a/cli/src/lib/gridAssignment.spec.ts b/cli/src/lib/gridAssignment.spec.ts
index 87521fa79..be17d79bf 100644
--- a/cli/src/lib/gridAssignment.spec.ts
+++ b/cli/src/lib/gridAssignment.spec.ts
@@ -169,10 +169,12 @@ describe('readOpencodeGridAssignment', () => {
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.
+ // OpenCode's endpoint lives in the config file this daemon wrote, not in a variable. Two
+ // regressions are pinned here:
+ // * listing opencode under BASE_URL_VAR while its endpoint had moved made every opencode agent
+ // report "own login" while the grid answered correctly underneath;
+ // * reading the model out of argv made the app's pill flicker between the model and "own login"
+ // while the agent was answering, because a live process may rewrite its command line.
const RELAY = 'https://grid.autonomous.ai/grid-3378218621364f16/relay/v1'
const write = (body: unknown): string => {
const dir = mkdtempSync(join(tmpdir(), 'harness-opencode-cfg-'))
@@ -181,14 +183,20 @@ describe('readOpencodeGridAssignment', () => {
writeFileSync(path, JSON.stringify(body))
return path
}
- const config = (baseURL: string, provider = 'autonomous-ai') => ({
- provider: { [provider]: { npm: '@ai-sdk/openai-compatible', options: { baseURL } } },
+ const config = (baseURL: string, models: string[] = ['DeepSeek-V4-Flash-0731']) => ({
+ provider: {
+ 'autonomous-ai': {
+ npm: '@ai-sdk/openai-compatible',
+ options: { baseURL },
+ models: Object.fromEntries(models.map((m) => [m, { name: m }])),
+ },
+ },
})
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.
+ // The strongest form of this test: 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-'))
@@ -196,11 +204,9 @@ describe('readOpencodeGridAssignment', () => {
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(' ')}`,
- )
+ const assignment = await readOpencodeGridAssignment({
+ [built.launch.configDir!.envVar]: join(dir, built.launch.configDir!.pointAt!),
+ })
expect(assignment).not.toBeNull()
expect(assignment!.baseUrl).toContain(NETWORK_ID)
expect(assignment!.model).toBe(OVERRIDE.model)
@@ -208,47 +214,39 @@ describe('readOpencodeGridAssignment', () => {
expect(JSON.stringify(assignment)).not.toContain('gridkey-secret')
})
- it('reads the endpoint out of the provider the argv selected', async () => {
+ it('answers the same whatever the engine has done to its command line', async () => {
+ // The flicker: argv is a live process's to rewrite, so nothing here may depend on it.
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' })
+ const expected = { baseUrl: RELAY, model: 'DeepSeek-V4-Flash-0731' }
+ await expect(readOpencodeGridAssignment({ OPENCODE_CONFIG: path })).resolves.toEqual(expected)
})
- 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 an endpoint that is not a relay', async () => {
+ const path = write(config('https://api.openai.com/v1'))
+ await expect(readOpencodeGridAssignment({ OPENCODE_CONFIG: path })).resolves.toBeNull()
})
- 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 a file this launch did not write', async () => {
+ // A user's own config can carry several providers; guessing which one an engine would pick
+ // would report an agent as being on a grid it is not on.
+ const path = write({
+ provider: {
+ 'autonomous-ai': { options: { baseURL: RELAY }, models: { a: {} } },
+ other: { options: { baseURL: RELAY } },
+ },
+ })
+ await expect(readOpencodeGridAssignment({ OPENCODE_CONFIG: path })).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 when the provider names more than one model', async () => {
+ // Then the file cannot say which one is live, and a guess would be reported as a measurement.
+ const path = write(config(RELAY, ['DeepSeek-V4-Flash-0731', 'Auto']))
+ await expect(readOpencodeGridAssignment({ OPENCODE_CONFIG: path })).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'))
+ await expect(readOpencodeGridAssignment({ OPENCODE_CONFIG: '/nonexistent/opencode.json' }))
.resolves.toBeNull()
+ await expect(readOpencodeGridAssignment({})).resolves.toBeNull()
})
})
diff --git a/cli/src/lib/gridAssignment.ts b/cli/src/lib/gridAssignment.ts
index 193b8bca9..24102db3d 100644
--- a/cli/src/lib/gridAssignment.ts
+++ b/cli/src/lib/gridAssignment.ts
@@ -133,36 +133,45 @@ export async function readPiGridAssignment(
}
}
-/** OpenCode's config file, and the `-m <provider>/<model>` that selects out of it. */
+/** The config file OpenCode was pointed at. Fixed at exec; nothing the engine does can change 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.
+ * OpenCode's endpoint and model, 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.
*
+ * **It deliberately does not read argv.** The first version matched `-m <provider>/<model>` there,
+ * and the app's grid pill flickered between the model and "own login" while an agent was answering:
+ * every re-probe during a turn had to re-parse a live process's command line, and a command line is
+ * that process's to rewrite. The file is written once by the launch and is exactly one provider on
+ * one grid with one model, so those are read straight out of it — a fact that cannot move.
+ *
* 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 } }>
+ provider?: Record<string, { options?: { baseURL?: unknown }; models?: Record<string, unknown> }>
}
- const baseUrl = raw.provider?.[selected[1]]?.options?.baseURL
+ const providers = Object.values(raw.provider ?? {})
+ // More than one provider is not a file this launch wrote. Rather than guess which one the engine
+ // would pick, say nothing — the caller reads that as "not on the grid you picked", which is the
+ // safe direction to be wrong in.
+ if (providers.length !== 1) return null
+ const baseUrl = providers[0].options?.baseURL
if (typeof baseUrl !== 'string' || !isGridUrl(baseUrl)) return null
- return { baseUrl, model: selected[2] }
+ const models = Object.keys(providers[0].models ?? {})
+ if (models.length !== 1) return null
+ return { baseUrl, model: models[0] }
} catch {
return null
}
@@ -185,7 +194,7 @@ export async function probeGridAssignment(
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)
+ if (engine === 'opencode') return await readOpencodeGridAssignment(processEnv)
return classifyGridAssignment(engine, processEnv, args)
}
diff --git a/cli/src/lib/gridLaunch.spec.ts b/cli/src/lib/gridLaunch.spec.ts
index a59faf8a3..afae1fdd8 100644
--- a/cli/src/lib/gridLaunch.spec.ts
+++ b/cli/src/lib/gridLaunch.spec.ts
@@ -343,7 +343,14 @@ describe('opencode declares the grid as a provider', () => {
it("falls back to the grid's router when no model was chosen", () => {
expect(launch().args).toEqual(['-m', 'autonomous-ai/Auto'])
- // And the router is not listed twice when it IS the selection.
+ })
+
+ it('writes EXACTLY ONE model, which is what the probe reads back', () => {
+ // `readOpencodeGridAssignment` answers "which model is this agent on" from this file alone,
+ // precisely so it never has to parse a live process's argv — which flickered. A second entry
+ // here would leave that question unanswerable and the probe would go back to saying nothing.
+ expect(Object.keys(config('DeepSeek-V4-Flash-0731').provider['autonomous-ai'].models))
+ .toEqual(['DeepSeek-V4-Flash-0731'])
expect(Object.keys(config().provider['autonomous-ai'].models)).toEqual(['Auto'])
})
diff --git a/cli/src/lib/gridLaunch.ts b/cli/src/lib/gridLaunch.ts
index f23bff7e9..30a3b1605 100644
--- a/cli/src/lib/gridLaunch.ts
+++ b/cli/src/lib/gridLaunch.ts
@@ -206,13 +206,16 @@ function opencodeGridConfig(
override: GridLaunchOverride,
model: string,
): string {
+ // EXACTLY ONE model, and that is deliberate: it is what the agent was created with.
+ //
+ // Offering the router alongside it would let someone switch inside OpenCode, which reads as a
+ // kindness until you ask what the agent is running on. The probe that answers that
+ // (`readOpencodeGridAssignment`) reads this file, so a file naming two models can only say which
+ // one is live by parsing the engine's argv — and an argv is a live process's business, which it
+ // may rewrite. One model here makes the answer a fact about a file that cannot change under us.
+ //
+ // Choosing a different model is what every other engine here does too: per agent, at creation.
const models: Record<string, { name: string }> = { [model]: { name: model } }
- // The router is always offered, so the picker inside OpenCode can fall back to "let the grid
- // choose" without the agent being recreated. Skipped when it IS the selection, so one id never
- // appears twice.
- if (model !== GRID_ROUTER_MODEL) {
- models[GRID_ROUTER_MODEL] = { name: `${GRID_ROUTER_MODEL} (grid picks the model)` }
- }
return `${JSON.stringify({
$schema: 'https://opencode.ai/config.json',
provider: {