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.

fix(gemini): a node waiting for permission no longer shows RUNNING

enes0aae68740294
diff --git a/src/shared/agents/hook-events.ts b/src/shared/agents/hook-events.ts
index 8b6281919..14f21fcba 100644
--- a/src/shared/agents/hook-events.ts
+++ b/src/shared/agents/hook-events.ts
@@ -38,8 +38,33 @@ export const CLAUDE_HOOK_EVENTS = [
'PostToolUse'
] as const
-/** Gemini CLI hook events — its own names, NOT Claude's (see normalizeGemini). */
-export const GEMINI_HOOK_EVENTS = ['BeforeAgent', 'AfterAgent', 'AfterTool', 'BeforeTool'] as const
+/**
+ * Gemini CLI hook events (→ normalizeGemini) — its own names, NOT Claude's.
+ *
+ * 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
+ * `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.)
+ * - `BeforeToolSelection` (reference.md:204-219) and `PreCompress` (reference.md:287-297) have
+ * no state we render today.
+ *
+ * `Notification` is the important addition: it is gemini's ONLY signal that the user is being
+ * asked something (`notification_type: "ToolPermission"`, reference.md:272-285). Without it a
+ * gemini node sat on RUNNING while it waited for a permission answer, because the last event we
+ * heard was `BeforeTool`.
+ */
+export const GEMINI_HOOK_EVENTS = [
+ 'BeforeAgent',
+ 'AfterAgent',
+ 'AfterTool',
+ 'BeforeTool',
+ 'Notification',
+ 'SessionStart',
+ 'SessionEnd'
+] as const
/**
* Grok hook events (→ normalizeGrok). Grok's shipped 1.0.0 docs list fourteen
diff --git a/src/shared/agents/normalize.gemini.test.ts b/src/shared/agents/normalize.gemini.test.ts
new file mode 100644
index 000000000..9223069d7
--- /dev/null
+++ b/src/shared/agents/normalize.gemini.test.ts
@@ -0,0 +1,97 @@
+import { describe, expect, it } from 'vitest'
+import { normalizeGemini } from './normalize'
+import { GEMINI_HOOK_EVENTS } from './hook-events'
+import type { RawHookEnvelope } from './normalize'
+
+/**
+ * Every field here is measured against gemini 0.54.4's OWN bundled hook reference,
+ * `/usr/lib/node_modules/@google/gemini-cli/bundle/docs/hooks/reference.md` — cited per case.
+ * The envelope is snake_case and claude-shaped (reference.md:48-58).
+ */
+const env = (payload: Record<string, unknown>): RawHookEnvelope => ({
+ nodeId: 'n1',
+ agentId: 'gemini',
+ payload
+})
+
+describe('normalizeGemini — the permission prompt (reference.md:272-285)', () => {
+ it('maps a ToolPermission notification to blocked', () => {
+ const e = normalizeGemini(
+ env({
+ hook_event_name: 'Notification',
+ session_id: 's1',
+ notification_type: 'ToolPermission',
+ message: 'Allow run_shell_command?'
+ })
+ )
+ expect(e).toMatchObject({
+ kind: 'state',
+ state: 'blocked',
+ sessionId: 's1',
+ lastMessage: 'Allow run_shell_command?'
+ })
+ })
+
+ it('leaves an UNKNOWN notification type alone', () => {
+ // The docs name exactly one type. An unknown future one must not stick a badge on a node —
+ // the failure grok's substring match actually produced.
+ for (const t of ['Info', 'Warning', 'SomethingNew', undefined]) {
+ expect(normalizeGemini(env({ hook_event_name: 'Notification', notification_type: t })), String(t)).toBeNull()
+ }
+ // A CLOSED match, not a substring: a longer type that merely CONTAINS the documented word is
+ // still unknown. Without this line a widening to `includes('Permission')` passes every test.
+ expect(
+ normalizeGemini(env({ hook_event_name: 'Notification', notification_type: 'ToolPermissionGranted' }))
+ ).toBeNull()
+ })
+})
+
+describe('normalizeGemini — session lifecycle', () => {
+ it('maps SessionStart to the start phase for every documented source', () => {
+ for (const source of ['startup', 'resume', 'clear']) {
+ expect(normalizeGemini(env({ hook_event_name: 'SessionStart', session_id: 's1', source })), source).toMatchObject(
+ { kind: 'session', sessionPhase: 'start' }
+ )
+ }
+ })
+
+ it('maps SessionEnd to the end phase for every documented reason', () => {
+ for (const reason of ['exit', 'clear', 'logout', 'prompt_input_exit', 'other']) {
+ expect(normalizeGemini(env({ hook_event_name: 'SessionEnd', session_id: 's1', reason })), reason).toMatchObject(
+ { kind: 'session', sessionPhase: 'end' }
+ )
+ }
+ })
+})
+
+describe('normalizeGemini — what must not change', () => {
+ it('still maps its turn and tool events exactly as before', () => {
+ expect(normalizeGemini(env({ hook_event_name: 'BeforeAgent', session_id: 's1' }))).toMatchObject({
+ state: 'working',
+ newTurn: true
+ })
+ for (const ev of ['BeforeTool', 'AfterTool']) {
+ expect(normalizeGemini(env({ hook_event_name: ev })), ev).toMatchObject({ state: 'working' })
+ }
+ expect(normalizeGemini(env({ hook_event_name: 'AfterAgent' }))).toMatchObject({ state: 'done' })
+ })
+
+ it('reads the legacy key spellings the normalizer already accepted', () => {
+ expect(normalizeGemini(env({ hookEventName: 'AfterAgent' }))).toMatchObject({ state: 'done' })
+ expect(normalizeGemini(env({ event: 'AfterAgent' }))).toMatchObject({ state: 'done' })
+ })
+
+ 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).
+ expect(GEMINI_HOOK_EVENTS).not.toContain('AfterModel')
+ expect(GEMINI_HOOK_EVENTS).not.toContain('BeforeModel')
+ expect(GEMINI_HOOK_EVENTS).not.toContain('BeforeToolSelection')
+ })
+
+ it('subscribes to exactly the seven events we can act on', () => {
+ expect([...GEMINI_HOOK_EVENTS].sort()).toEqual(
+ ['AfterAgent', 'AfterTool', 'BeforeAgent', 'BeforeTool', 'Notification', 'SessionEnd', 'SessionStart'].sort()
+ )
+ })
+})
diff --git a/src/shared/agents/normalize.ts b/src/shared/agents/normalize.ts
index b918750c7..6a6a0fbf8 100644
--- a/src/shared/agents/normalize.ts
+++ b/src/shared/agents/normalize.ts
@@ -304,13 +304,19 @@ export function normalizeCodex(env: RawHookEnvelope): NormalizedAgentEvent | nul
return null
}
-// Gemini hook payload. Event name is read defensively (some builds use
-// `hook_event_name`/`hookEventName`); a session id may be present under `session_id`.
+// Gemini hook payload. Its envelope is snake_case and claude-shaped — every event carries
+// `session_id`, `transcript_path`, `cwd`, `hook_event_name` and `timestamp` (gemini 0.54.4's own
+// `docs/hooks/reference.md:48-58`). The event name is still read defensively (`hookEventName` /
+// `event`) because older builds spelled it differently and those payloads cost nothing to accept.
interface GeminiPayload {
hook_event_name?: string
hookEventName?: string
event?: string
session_id?: string
+ /** Notification only (reference.md:272-285). The documented type is `"ToolPermission"`. */
+ notification_type?: string
+ /** Notification only: the alert's summary — shown on the needs-you badge (reference.md:279). */
+ message?: string
}
export function normalizeGemini(env: RawHookEnvelope): NormalizedAgentEvent | null {
@@ -327,7 +333,35 @@ export function normalizeGemini(env: RawHookEnvelope): NormalizedAgentEvent | nu
return { ...base, kind: 'state', state: 'working' }
}
if (ev === 'AfterAgent') return { ...base, kind: 'state', state: 'done' }
- // Gemini has no waiting/blocked states.
+ // `source` is startup|resume|clear and `reason` is exit|clear|logout|prompt_input_exit|other
+ // (reference.md:250-251, 265-266). Neither changes what a session boundary MEANS to us, so both
+ // phases map unconditionally — a `/clear` really is one session ending and another beginning.
+ if (ev === 'SessionStart') return { ...base, kind: 'session', sessionPhase: 'start' }
+ if (ev === 'SessionEnd') return { ...base, kind: 'session', sessionPhase: 'end' }
+ if (ev === 'Notification') {
+ // Gemini's ONE ask-the-user signal (reference.md:272-285, "for example, Tool Permissions"), and
+ // the reason this event was worth subscribing to: without it a node waiting on a permission
+ // answer sat on RUNNING, because the last hook we heard was `BeforeTool`.
+ //
+ // `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.
+ //
+ // 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
+ // this codebase has shipped before. Grok is the cautionary tale: there
+ // `type.includes('permission')` turned a notification that fires before every tool call into a
+ // strobing NEEDS YOU. Widening this "to be safe" is the unsafe direction.
+ //
+ // Nothing here can be answered from our side: the hook is observability only and its
+ // flow-control fields are ignored (reference.md:284-285), so this reports state and no more —
+ // no `pendingId`, unlike claude's deterministic-approval path.
+ if (p.notification_type === 'ToolPermission') {
+ return { ...base, kind: 'state', state: 'blocked', lastMessage: p.message }
+ }
+ return null
+ }
return null
}