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.

feat(tmux): remove variables from a pane's session environment

Dev jacob0765b7110723
diff --git a/cli/src/lib/tmuxBackend.spec.ts b/cli/src/lib/tmuxBackend.spec.ts
index e1b71bd33..8ce618d1e 100644
--- a/cli/src/lib/tmuxBackend.spec.ts
+++ b/cli/src/lib/tmuxBackend.spec.ts
@@ -2,7 +2,7 @@ import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'nod
import { tmpdir } from 'node:os'
import { delimiter, join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
-import { TmuxBackend } from './tmuxBackend.js'
+import { TmuxBackend, clearEnvArgs } from './tmuxBackend.js'
const originalPath = process.env.PATH
const dirs: string[] = []
@@ -221,3 +221,22 @@ exit 1
if (moved.state === 'unknown') expect(moved.reason).toContain('no such pane')
})
})
+
+// The command line is the whole contract here: `set-environment -u` REMOVES a variable, while the
+// `-e VAR=` form a respawn takes would set it to an empty string. An engine handed
+// ANTHROPIC_BASE_URL="" does not fall back to its own login; it tries to dial the empty string.
+describe('clearEnv command shape', () => {
+ it('unsets each name against the pane\'s session, never assigning an empty value', () => {
+ const args = clearEnvArgs('$3', ['ANTHROPIC_BASE_URL', 'ANTHROPIC_MODEL'])
+ expect(args).toEqual([
+ 'set-environment', '-t', '$3', '-u', 'ANTHROPIC_BASE_URL',
+ ';',
+ 'set-environment', '-t', '$3', '-u', 'ANTHROPIC_MODEL',
+ ])
+ expect(args.join(' ')).not.toContain('=')
+ })
+
+ it('asks for nothing when there is nothing to clear', () => {
+ expect(clearEnvArgs('$3', [])).toEqual([])
+ })
+})
diff --git a/cli/src/lib/tmuxBackend.ts b/cli/src/lib/tmuxBackend.ts
index 338a0f648..98348b28f 100644
--- a/cli/src/lib/tmuxBackend.ts
+++ b/cli/src/lib/tmuxBackend.ts
@@ -69,6 +69,22 @@ function legacyActionResult(ok: boolean, operation: string): TerminalActionResul
return ok ? TERMINAL_ACTION_SUCCEEDED : terminalActionPossiblyExecuted(`${operation} did not complete`)
}
+/**
+ * The argv for removing [names] from session [sessionId]'s environment.
+ *
+ * Exported so a spec can pin the exact command without a tmux server. `-u` REMOVES the variable;
+ * the `-e VAR=` form that `respawn-pane` accepts would set it to an empty string instead, and an
+ * engine handed an empty base URL does not fall back to its own login — it dials the empty string.
+ */
+export function clearEnvArgs(sessionId: string, names: readonly string[]): string[] {
+ const args: string[] = []
+ for (const name of names) {
+ if (args.length) args.push(';')
+ args.push('set-environment', '-t', sessionId, '-u', name)
+ }
+ return args
+}
+
export class TmuxBackend implements TerminalBackend<TmuxRuntimeRef> {
readonly name = 'tmux' as const
readonly instanceId = 'tmux:default'
@@ -161,6 +177,28 @@ export class TmuxBackend implements TerminalBackend<TmuxRuntimeRef> {
return legacyActionResult(ok, 'tmux session close')
}
+ /**
+ * Remove [names] from the environment of the session that owns [runtime].
+ *
+ * A pane created by `new-session -e` put those variables in the SESSION environment, and
+ * `respawn-pane` inherits it — so respawning with no `-e` flags would leave the old grid in place
+ * while reporting a clean swap. This is what makes "back to your own login" actually true.
+ */
+ async clearEnv(runtime: TmuxRuntimeRef, names: readonly string[]): Promise<TerminalActionResult> {
+ if (!names.length) return legacyActionResult(true, 'tmux clear environment')
+ const sessionId = await new Promise<string | null>((resolve) => {
+ execFile('tmux', ['display-message', '-p', '-t', runtime.paneId, '#{session_id}'], { timeout: 2_000 }, (error, stdout) => {
+ const value = stdout.trim()
+ resolve(!error && /^\$\d+$/.test(value) ? value : null)
+ })
+ })
+ if (!sessionId) return terminalActionNotStarted('tmux session could not be resolved from pane')
+ const ok = await new Promise<boolean>((resolve) => {
+ execFile('tmux', clearEnvArgs(sessionId, names), { timeout: 5_000 }, (error) => resolve(!error))
+ })
+ return legacyActionResult(ok, 'tmux clear environment')
+ }
+
/**
* Re-arm `remain-on-exit` on an already-live pane, mirroring what `create()` does at spawn time.
*