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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(grok): report unknown Stop reasons, and match the asking notifications exactly
enesb11257233a85
diff --git a/src/shared/agents/normalize.grok.test.ts b/src/shared/agents/normalize.grok.test.tsindex 70434fc82..02f8f5dfa 100644--- a/src/shared/agents/normalize.grok.test.ts+++ b/src/shared/agents/normalize.grok.test.ts@@ -1,5 +1,5 @@import { describe, expect, it } from 'vitest'-import { grokRawFields, normalizeClaude, normalizeGrok } from './normalize'+import { grokRawFields, normalizeClaude, normalizeGrok, type RawHookEnvelope } from './normalize'/*** Grok's hook envelope is its own dialect: camelCase keys whose `hookEventName` VALUE is@@ -7,7 +7,9 @@ import { grokRawFields, normalizeClaude, normalizeGrok } from './normalize'* Both spellings are therefore read, and the event name is canonicalized rather than matched* literally — measured against the shipped 1.0.0 docs, not inferred from claude's shape.*/-const env = (payload: Record<string, unknown>) => ({ nodeId: 'n1', agentId: 'grok', payload })+function env(payload: Record<string, unknown>): RawHookEnvelope {+ return { nodeId: 'n1', agentId: 'grok', payload }+}describe('normalizeGrok — lifecycle', () => {it('maps session_start / session_end to the session phases', () => {@@ -51,6 +53,19 @@ describe('normalizeGrok — Stop', () => {expect(normalizeGrok(env({ hookEventName: 'stop', sessionId: 's1' }))).toMatchObject({ state: 'done' })})+ /**+ * The two observe-only reasons are a DENYLIST, not 'end_turn' an allowlist: only grok's session+ * close is observe-only, so any turn-end reason grok labels later must still report its badge and+ * its completion alert. An allowlist would silently mark all of these `interrupted`.+ */+ it('an UNKNOWN reason is a real turn end too, alert and message intact', () => {+ for (const reason of ['max_tokens', 'refusal', 'something_new']) {+ const e = normalizeGrok(env({ hookEventName: 'stop', sessionId: 's1', reason, lastAssistantMessage: 'text' }))+ expect(e, reason).toMatchObject({ state: 'done', lastMessage: 'text' })+ expect(e?.interrupted, reason).toBeFalsy()+ }+ })+it('the observe-only session-close Stop is marked interrupted, so no completion alert fires', () => {for (const reason of ['channel_closed', 'shutdown']) {const e = normalizeGrok(env({ hookEventName: 'stop', sessionId: 's1', reason, lastAssistantMessage: 'x' }))@@ -88,6 +103,35 @@ describe('normalizeGrok — Notification', () => {expect(normalizeGrok(env({ hookEventName: 'notification', notificationType: 'auth_success' }))).toBeNull()expect(normalizeGrok(env({ hookEventName: 'notification' }))).toBeNull()})++ /**+ * The asking types are matched EXACTLY, not by substring: grok's vocabulary is claude-derived, and+ * claude's `elicitation_complete` / `elicitation_response` fire when an elicitation ENDS. A+ * substring test on 'elicit' would read those as a fresh ask and leave NEEDS YOU on a node that+ * just finished — the exact bug normalizeClaude's closed set exists to avoid+ * (normalize.test.ts, 'informational / unknown Notification types do not change state').+ */+ it('the elicitation END notifications are informational, NOT a new ask', () => {+ for (const type of ['elicitation_complete', 'elicitation_response', 'agent_completed']) {+ expect(normalizeGrok(env({ hookEventName: 'notification', notificationType: type })), type).toBeNull()+ }+ // The dialog OPENING is the one elicitation type that does ask.+ expect(+ normalizeGrok(env({ hookEventName: 'notification', notificationType: 'elicitation_dialog' }))+ ).toMatchObject({ state: 'waiting' })+ })++ it('reads the notification type in the SDK snake_case dialect too', () => {+ expect(+ normalizeGrok(env({ hook_event_name: 'notification', notification_type: 'permission_prompt' }))+ ).toMatchObject({ state: 'blocked' })+ expect(+ normalizeGrok(env({ hook_event_name: 'notification', notification_type: 'agent_needs_input' }))+ ).toMatchObject({ state: 'waiting' })+ expect(+ normalizeGrok(env({ hook_event_name: 'notification', notification_type: 'idle_prompt' }))+ ).toMatchObject({ state: 'done', idle: true, interrupted: true })+ })})describe('normalizeGrok — dialects', () => {@@ -114,14 +158,31 @@ describe('normalizeGrok — dialects', () => {* this is the test that keeps it a property instead of a coincidence.*/describe('the claude-compat cross-fire is inert', () => {- it('normalizeClaude returns null for every grok payload', () => {- for (const ev of ['session_start', 'user_prompt_submit', 'pre_tool_use', 'post_tool_use', 'stop', 'session_end']) {+ const GROK_EVENTS = ['session_start', 'user_prompt_submit', 'pre_tool_use', 'post_tool_use', 'stop', 'session_end']++ it('normalizeClaude returns null for every grok payload (camelCase dialect)', () => {+ for (const ev of GROK_EVENTS) {expect(normalizeClaude({ nodeId: 'n1', agentId: 'claude', payload: { hookEventName: ev, sessionId: 's1' } }),ev).toBeNull()}})++ /**+ * This is the leg that actually carries the property: in the SDK dialect grok writes the key+ * claude DOES read (`hook_event_name`), so inertness rests entirely on claude's compare being+ * case-sensitive and literal — 'stop' is not 'Stop'. Anyone who "helpfully" lowercases claude's+ * event name breaks this and nothing else.+ */+ it('normalizeClaude returns null for every grok payload (SDK snake_case dialect)', () => {+ for (const ev of GROK_EVENTS) {+ expect(+ normalizeClaude({ nodeId: 'n1', agentId: 'claude', payload: { hook_event_name: ev, session_id: 's1' } }),+ ev+ ).toBeNull()+ }+ })})describe('grokRawFields', () => {@@ -143,9 +204,24 @@ describe('grokRawFields', () => {toolUseId: 't1',toolInput: { subagent_type: 'explore' }})- expect(grokRawFields({ hook_event_name: 'session_end', session_id: 's2' })).toMatchObject({- event: 'sessionend',- sessionId: 's2'+ // Every field the function reads, in the SDK dialect — dual-dialect reading is its whole reason+ // to exist, so dropping any one `?? p.snake_case` fallback has to fail here.+ expect(+ grokRawFields({+ hook_event_name: 'post_tool_use',+ session_id: 's2',+ cwd: '/w2',+ tool_name: 'read_file',+ tool_use_id: 't2',+ tool_input: { path: '/w2/a.ts' }+ })+ ).toEqual({+ event: 'posttooluse',+ sessionId: 's2',+ cwd: '/w2',+ toolName: 'read_file',+ toolUseId: 't2',+ toolInput: { path: '/w2/a.ts' }})})})diff --git a/src/shared/agents/normalize.ts b/src/shared/agents/normalize.tsindex 47f9e9181..019ad4daa 100644--- a/src/shared/agents/normalize.ts+++ b/src/shared/agents/normalize.ts@@ -414,27 +414,37 @@ export function normalizeGrok(env: RawHookEnvelope): NormalizedAgentEvent | nullreturn { ...base, kind: 'state', state: 'working' }}if (ev === 'stop') {- // grok fires a SECOND, observe-only Stop when the session itself closes (reason- // 'channel_closed' / 'shutdown'). Reporting that as a finished turn would pop a "your agent is- // done" notification every time a session ends, so it is marked `interrupted` — the flag the- // renderer already reads as "skip the completion alert and the unread dot" — and its- // lastAssistantMessage (an earlier turn's text) is dropped. A MISSING reason is treated as a- // genuine turn end: this is the one event the RUNNING badge depends on ending, so an unknown- // dialect must fail towards reporting it, not towards swallowing it.- const genuineTurnEnd = p.reason === undefined || p.reason === 'end_turn'- return genuineTurnEnd+ // grok fires a SECOND, observe-only Stop when the session itself closes — and ONLY those two+ // reasons, 'channel_closed' and 'shutdown', are that fire. Reporting them as a finished turn+ // would pop a "your agent is done" notification every time a session ends, so they are marked+ // `interrupted` — the flag the renderer already reads as "skip the completion alert and the+ // unread dot" — and their lastAssistantMessage (an earlier turn's text) is dropped.+ //+ // The test is a DENYLIST of those two, not an allowlist of 'end_turn', so everything else —+ // an absent reason, or a genuine turn end grok labels later ('max_tokens', 'refusal', …) —+ // reports normally. Stop is the one event the RUNNING badge depends on ending, so an unknown+ // dialect must fail towards reporting the badge-clearing event, never towards swallowing it.+ const sessionClose = p.reason === 'channel_closed' || p.reason === 'shutdown'+ return !sessionClose? { ...base, kind: 'state', state: 'done', lastMessage }: { ...base, kind: 'state', state: 'done', interrupted: true }}// The turn died on an API error — grok skips Stop entirely here, exactly as Claude does.if (ev === 'stopfailure') return { ...base, kind: 'state', state: 'done', lastMessage }if (ev === 'notification') {- // grok's notification-type vocabulary is not documented as a closed set (its `matcher` tests- // the type as a regex), so we classify by substring and default to a NO-OP. Same discipline as- // normalizeClaude: an unrecognized future type must never stick a badge on a finished node.+ // Everything unrecognized is a deliberate NO-OP: same discipline as normalizeClaude, where an+ // unknown future type sticking a badge on a finished node is the failure that has actually+ // happened before.const type = (p.notificationType ?? p.notification_type ?? '').toLowerCase()+ // A permission ask is genuinely a FAMILY of names ('permission_prompt', 'permission_request',+ // …), and its worst case is a `blocked` badge the agent's next hook clears — so substring is+ // the right trade here.if (type.includes('permission')) return { ...base, kind: 'state', state: 'blocked', lastMessage }- if (type.includes('input') || type.includes('elicit')) {+ // The asking types are a CLOSED set, exactly as in normalizeClaude, and for its reason: grok's+ // vocabulary is claude-derived, and claude's `elicitation_complete` / `elicitation_response` are+ // informational — they fire when an elicitation ENDS. A substring test on 'elicit' would match+ // them and leave NEEDS YOU on a node that just finished, with no later hook to clear it.+ if (type === 'elicitation_dialog' || type === 'agent_needs_input') {return { ...base, kind: 'state', state: 'waiting', lastMessage }}// Idle at the prompt: the RESCUE signal for a node stuck on `working`. grok fires no hook at