Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(gemini): allow the in-place restart-and-resume for gemini nodes

enes26999a351b3c
diff --git a/src/renderer/terminal/agent-restart.test.ts b/src/renderer/terminal/agent-restart.test.ts
index 67ae4b188..6c07330ce 100644
--- a/src/renderer/terminal/agent-restart.test.ts
+++ b/src/renderer/terminal/agent-restart.test.ts
@@ -18,12 +18,17 @@ import {
} from './agent-restart'
describe('exitSequence', () => {
- it('knows claude, codex and grok, refuses others', () => {
+ it('knows claude, codex, grok and gemini, refuses others', () => {
expect(exitSequence('claude')).toBe('/exit')
expect(exitSequence('codex')).toBe('/quit')
// grok's documented primary is `/quit` (`/exit` is its alias).
expect(exitSequence('grok')).toBe('/quit')
- expect(exitSequence('gemini')).toBeNull()
+ // gemini's too (docs/reference/commands.md:325) — and BARE: `/quit --delete` would exit AND
+ // permanently delete the session history we are about to `--resume` into.
+ expect(exitSequence('gemini')).toBe('/quit')
+ expect(exitSequence('gemini')).not.toContain('--delete')
+ // opencode is resumable but we know no way to ask it to quit, so it is still not a target.
+ expect(exitSequence('opencode')).toBeNull()
expect(exitSequence('my-custom')).toBeNull()
})
})
@@ -53,7 +58,12 @@ describe('restartEligibility', () => {
ok: false,
reason: 'no-session'
})
- expect(restartEligibility('gemini', 'waiting', 'abc')).toEqual({
+ // opencode: resumable, but no exit sequence — so still never a target.
+ expect(restartEligibility('opencode', 'waiting', 'abc')).toEqual({
+ ok: false,
+ reason: 'not-resumable'
+ })
+ expect(restartEligibility('my-custom', 'waiting', 'abc')).toEqual({
ok: false,
reason: 'not-resumable'
})
@@ -285,9 +295,11 @@ describe('performRestartResume', () => {
it('refuses an agent without an exit sequence', async () => {
const { io } = fakeIo()
+ // opencode is in RESUMABLE_AGENTS but not in EXIT_SEQUENCES: the exit-line table is its own,
+ // independent half of the gate.
expect(
await performRestartResume({
- agentId: 'gemini',
+ agentId: 'opencode',
sessionId: 's',
io,
paneCommand: async () => 'zsh'
@@ -510,6 +522,83 @@ describe('restartEligibility — grok', () => {
})
})
+describe('performRestartResume — gemini', () => {
+ beforeEach(() => vi.useFakeTimers())
+ afterEach(() => vi.useRealTimers())
+
+ it('quits with /quit and resumes by session id', async () => {
+ const { written, io } = fakeIo()
+ let pane = 'gemini'
+ const p = performRestartResume({
+ agentId: 'gemini',
+ sessionId: 'abc-1',
+ io,
+ paneCommand: async () => pane,
+ timeoutMs: 6000,
+ pollMs: 100
+ })
+ await vi.advanceTimersByTimeAsync(250) // a few polls while the CLI is still up
+ pane = 'zsh'
+ await vi.advanceTimersByTimeAsync(5000)
+ expect(await p).toBe('restarted')
+ // `/quit` (alias `/exit`) measured in gemini's bundled docs/reference/commands.md:325.
+ // `\x15` is the kill-line that clears any half-typed prompt first.
+ expect(written.slice(0, 2)).toEqual(['\x15', '/quit\r'])
+ expect(written.join('')).toContain('gemini --resume abc-1')
+ })
+
+ it('never types the history-destroying --delete flag', async () => {
+ // `/quit --delete` exits AND permanently deletes the session's history and temporary files —
+ // the exact conversation the `--resume` on the next line is supposed to return to.
+ const { written, io } = fakeIo()
+ const p = performRestartResume({
+ agentId: 'gemini',
+ sessionId: 'abc-1',
+ io,
+ paneCommand: async () => 'zsh',
+ timeoutMs: 1000,
+ pollMs: 100
+ })
+ await vi.advanceTimersByTimeAsync(5000)
+ expect(await p).toBe('restarted')
+ expect(written.join('')).not.toContain('--delete')
+ })
+})
+
+describe('restartEligibility — gemini', () => {
+ it('is a target once it has a session id and is not busy', () => {
+ expect(restartEligibility('gemini', 'done', 'abc-1')).toEqual({ ok: true })
+ expect(restartEligibility('gemini', 'waiting', 'abc-1')).toEqual({ ok: true })
+ expect(restartEligibility('gemini', 'done', undefined)).toEqual({
+ ok: false,
+ reason: 'no-session'
+ })
+ })
+
+ it('is refused while the session is working or blocked', () => {
+ // A gemini node genuinely REACHES `blocked` now that its `Notification`/ToolPermission hook is
+ // subscribed, so this refusal is newly reachable rather than theoretical: `/quit` typed into a
+ // permission prompt would ANSWER the prompt instead of quitting the CLI. Before the
+ // EXIT_SEQUENCES entry landed, `not-resumable` short-circuited ahead of BUSY_STATES and this
+ // branch could never be observed for gemini.
+ for (const state of ['working', 'blocked'] as const)
+ expect(restartEligibility('gemini', state, 'abc-1'), state).toEqual({
+ ok: false,
+ reason: 'working'
+ })
+ })
+
+ it('keeps a busy gemini node out of the bulk run', () => {
+ const plan = planBulkRestart([
+ { id: 'idle', agentId: 'gemini', state: 'done', sessionId: 'sid-1', wired: true },
+ { id: 'busy', agentId: 'gemini', state: 'working', sessionId: 'sid-2', wired: true },
+ { id: 'prompt', agentId: 'gemini', state: 'blocked', sessionId: 'sid-3', wired: true }
+ ])
+ expect(plan.runnable).toEqual(['idle'])
+ expect(plan.skipped).toEqual({ working: 2, noSession: 0 })
+ })
+})
+
describe('performRestartResume — the delivery await is bounded', () => {
beforeEach(() => {
vi.resetModules()
@@ -679,7 +768,8 @@ describe('planBulkRestart', () => {
it('ignores nodes that were never restart targets, instead of counting them as skips', () => {
const plan = planBulkRestart([
cand({ id: 'shell', agentId: undefined }), // plain terminal
- cand({ id: 'gem', agentId: 'gemini' }), // no exit sequence / no --resume
+ cand({ id: 'oc', agentId: 'opencode' }), // resumable, but no exit sequence
+ cand({ id: 'custom', agentId: 'my-custom' }), // neither
cand({ id: 'a' })
])
expect(plan.runnable).toEqual(['a'])
diff --git a/src/renderer/terminal/agent-restart.ts b/src/renderer/terminal/agent-restart.ts
index 73a4b5b67..8131cf5eb 100644
--- a/src/renderer/terminal/agent-restart.ts
+++ b/src/renderer/terminal/agent-restart.ts
@@ -20,8 +20,20 @@ import {
* relaunch line comes from `resumeCommand` → `grok --resume <id>`, so one entry here turns on both
* surfaces at once: the single-node "Restart agent (resume)" row in the node context menu, and the
* bulk "restart idle agents" action (pane menu + command palette). There is no header button for
- * either — `HIDEABLE_HEADER_BUTTONS` is refresh / mic / ai-name / comments. */
-const EXIT_SEQUENCES: Record<string, string> = { claude: '/exit', codex: '/quit', grok: '/quit' }
+ * either — `HIDEABLE_HEADER_BUTTONS` is refresh / mic / ai-name / comments.
+ *
+ * gemini: `/quit` (alias `/exit`), measured in its own bundled
+ * `docs/reference/commands.md:325`, and relaunched as `gemini --resume <id>`. The command must be
+ * sent BARE: the same command also takes a `--delete` flag that exits AND *permanently deletes*
+ * the session's history and temporary files. A restart that ever typed `/quit --delete` would
+ * destroy the very conversation the `--resume` behind it is meant to return to, so nothing may
+ * append arguments to a value in this table. */
+const EXIT_SEQUENCES: Record<string, string> = {
+ claude: '/exit',
+ codex: '/quit',
+ grok: '/quit',
+ gemini: '/quit'
+}
export function exitSequence(agentId: string): string | null {
return EXIT_SEQUENCES[agentId] ?? null
@@ -41,7 +53,8 @@ export type IneligibleReason = 'working' | 'no-session' | 'not-resumable'
/** States in which the pane must be left alone. `blocked` is here for a sharper reason than
* politeness: it means a permission / question dialog owns the prompt (see normalize.ts —
- * Claude's PermissionRequest, codex's permission.asked / question.asked), so writing `/exit`
+ * Claude's PermissionRequest, codex's permission.asked / question.asked, gemini's
+ * Notification/ToolPermission), so writing `/exit`
* would be typed AS THE ANSWER to that dialog instead of quitting the CLI. Both states report
* the reason `'working'`: to the user they are the same "busy, try again in a moment". */
const BUSY_STATES = new Set(['working', 'blocked'])
diff --git a/src/shared/agents/hook-events.ts b/src/shared/agents/hook-events.ts
index 14f21fcba..163df4046 100644
--- a/src/shared/agents/hook-events.ts
+++ b/src/shared/agents/hook-events.ts
@@ -43,11 +43,16 @@ export const CLAUDE_HOOK_EVENTS = [
*
* Gemini publishes ELEVEN events; these are the seven we can act on. The four left out are
* deliberate, all per gemini 0.54.4's bundled `docs/hooks/reference.md`:
- * - `AfterModel` / `BeforeModel` fire for EVERY streamed chunk (reference.md:236-237), so
- * subscribing would spawn a hook process per chunk. `AfterModel` is the one that carries
+ * - `AfterModel` fires for EVERY streamed chunk (reference.md:236-237, "Fired for every chunk
+ * generated by the model"; its `fireAfterModelEvent(request, chunk)` runs inside the chunk
+ * loop), so subscribing would spawn a hook process per chunk. It is also the one that carries
* `llm_response.usageMetadata.totalTokenCount` — the one number worth having — which is why a
* gemini context meter must read the transcript instead of subscribing here. (Not built yet;
* that is a separate task. Do not "just add AfterModel" to get the tokens.)
+ * - `BeforeModel` is NOT per-chunk — it fires once per model request (reference.md:187-190,
+ * "Fires before sending a request to the LLM"). It is left out for the plain reason the two
+ * below are: it reports nothing we render. A turn's start is already `BeforeAgent`, and a
+ * second working→working event per request would only add hook processes.
* - `BeforeToolSelection` (reference.md:204-219) and `PreCompress` (reference.md:287-297) have
* no state we render today.
*
diff --git a/src/shared/agents/normalize.gemini.test.ts b/src/shared/agents/normalize.gemini.test.ts
index 9223069d7..2d7df1a31 100644
--- a/src/shared/agents/normalize.gemini.test.ts
+++ b/src/shared/agents/normalize.gemini.test.ts
@@ -83,10 +83,13 @@ describe('normalizeGemini — what must not change', () => {
it('does NOT subscribe to AfterModel — it fires per streamed chunk', () => {
// reference.md:236-237. One hook process per chunk is a performance defect; the token numbers
- // come off the transcript instead (Task 3).
+ // come off the transcript instead (Task 3). The other three are left out because they report
+ // no state we render — `BeforeModel` fires once per model request (reference.md:187-190), not
+ // per chunk. All four of gemini's unsubscribed events, spelled out.
expect(GEMINI_HOOK_EVENTS).not.toContain('AfterModel')
expect(GEMINI_HOOK_EVENTS).not.toContain('BeforeModel')
expect(GEMINI_HOOK_EVENTS).not.toContain('BeforeToolSelection')
+ expect(GEMINI_HOOK_EVENTS).not.toContain('PreCompress')
})
it('subscribes to exactly the seven events we can act on', () => {
diff --git a/src/shared/agents/normalize.ts b/src/shared/agents/normalize.ts
index 6a6a0fbf8..b3141d6e1 100644
--- a/src/shared/agents/normalize.ts
+++ b/src/shared/agents/normalize.ts
@@ -346,7 +346,10 @@ export function normalizeGemini(env: RawHookEnvelope): NormalizedAgentEvent | nu
// `blocked` rather than `waiting` for two reasons: normalizeClaude already uses it for a
// permission ask (every consumer treats the two alike), and BUSY_STATES then refuses an
// in-place restart on this node — correct, since `/quit` typed into a permission prompt would
- // ANSWER the prompt instead of quitting.
+ // ANSWER the prompt instead of quitting. That refusal only became REACHABLE for gemini once
+ // gemini joined `EXIT_SEQUENCES` (agent-restart.ts): `restartEligibility` returns
+ // `not-resumable` before it ever consults BUSY_STATES, so until then no gemini node could be
+ // restarted at all, blocked or otherwise.
//
// A CLOSED match, not a substring: the docs name exactly ONE type (reference.md:278), and an
// unknown future one must stay a no-op — a badge that sticks on a finished node is a failure