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): answer "which grid" during the install, and survive a partial env read

bubu-dudu3195e6f694ba
diff --git a/cli/src/cli.ts b/cli/src/cli.ts
index 8647002b3..f25287096 100644
--- a/cli/src/cli.ts
+++ b/cli/src/cli.ts
@@ -3169,15 +3169,25 @@ async function runForeground(session: AuthSession): Promise<void> {
// `routeAgent` branch in registry.ts, which matches on terminal route exactly while an agent has
// no engine session bound. Same agentId, same tile, no flicker.
const placeholder = await resolvePaneRootProcess(spawned.runtime.paneId)
+ // The grid, READ off the shell that is about to become the engine — not declared.
+ //
+ // That shell already carries everything the launch set, because `tmux new-session -e` put it
+ // there before anything ran: it is the same environment the engine will inherit, in a process
+ // that exists now. So the ordinary probe answers, and it answers truthfully.
+ //
+ // Skipping this was a real bug, not a purity win. An install runs for a minute, and for that
+ // whole minute the app said "own login" over an agent that had already been pointed at a grid
+ // — the one moment the user is watching, and the one answer they were owed.
+ const placeholderGrid = placeholder
+ ? await probeGridAssignment(placeholder, engine, argv.join(' '))
+ : undefined
const opened = placeholder
? registry.openProcessAgent({
engine,
runtimes: [spawned.runtime],
cwd,
- // Deliberately no `grid`: the registry re-derives that from the LIVE process on every
- // discovery, never from a declaration. Declaring it here would state, of a pane currently
- // running npm, which grid its engine is on.
processIdentity: placeholder,
+ ...(placeholderGrid !== undefined ? { grid: placeholderGrid } : {}),
})
: null
if (opened) {
diff --git a/cli/src/lib/processEnv.spec.ts b/cli/src/lib/processEnv.spec.ts
new file mode 100644
index 000000000..d58a71dfb
--- /dev/null
+++ b/cli/src/lib/processEnv.spec.ts
@@ -0,0 +1,22 @@
+import { describe, expect, it } from 'vitest'
+import { parsePsEnviron } from './processEnv.js'
+
+describe('parsePsEnviron', () => {
+ it('reads the variables out of a ps line that begins with argv', () => {
+ const env = parsePsEnviron('/usr/local/bin/opencode -m grid/x FOO=1 OPENCODE_CONFIG=/tmp/a.json')
+ expect(env.FOO).toBe('1')
+ expect(env.OPENCODE_CONFIG).toBe('/tmp/a.json')
+ })
+
+ it('shows what a PARTIAL ps answer looks like, which is why the cache merges', () => {
+ // macOS prints argv and the environment into one field, so a long argv can push the trailing
+ // variables out of it. `ps` still exits 0, so the loss is invisible: the caller gets a map that
+ // simply lacks a key, indistinguishable from a process that never had it. The merge in
+ // readProcessEnv is what makes that harmless, and this is the shape it is defending against.
+ const full = parsePsEnviron('opencode FOO=1 OPENCODE_CONFIG=/tmp/a.json')
+ const cut = parsePsEnviron('opencode FOO=1')
+ expect(full.OPENCODE_CONFIG).toBe('/tmp/a.json')
+ expect(cut.OPENCODE_CONFIG).toBeUndefined()
+ expect({ ...full, ...cut }.OPENCODE_CONFIG).toBe('/tmp/a.json')
+ })
+})
diff --git a/cli/src/lib/processEnv.ts b/cli/src/lib/processEnv.ts
index 7d00ef811..52ac713e5 100644
--- a/cli/src/lib/processEnv.ts
+++ b/cli/src/lib/processEnv.ts
@@ -87,7 +87,23 @@ export async function readProcessEnv(identity: ProcessIdentity): Promise<Record<
if (hit && Date.now() - hit.at < TTL_MS) return hit.env
const env = await read(identity.pid)
if (!env) return null
+ // A later read of the same process may see LESS than an earlier one — and when it does, the earlier
+ // one was right. That follows from the invariant above: the environment did not change, so a key
+ // that has gone missing was dropped by the READING, not by the process.
+ //
+ // Only macOS can lose one. `/proc/<pid>/environ` is all-or-nothing; `ps eww -o command=` prints
+ // argv and then the environment into one field, so a partial answer is a shape that exists there
+ // and nowhere else. It is also invisible: `ps` exits 0 and the caller gets a map that simply lacks
+ // a variable, which reads exactly like a process that never had it.
+ //
+ // That difference is not cosmetic here. `readOpencodeGridAssignment` treats a missing
+ // `OPENCODE_CONFIG` as "this agent is on no grid", so one partial read turned an agent's grid into
+ // "own login" in the app — briefly, then back, for the length of a turn.
+ //
+ // Merging restores only what a partial read lost, and never invents: the key includes the pid AND
+ // its start marker, so another process cannot reach this entry.
+ const merged = hit ? { ...hit.env, ...env } : env
if (cache.size >= CACHE_LIMIT) cache.clear()
- cache.set(key, { env, at: Date.now() })
- return env
+ cache.set(key, { env: merged, at: Date.now() })
+ return merged
}