Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/agent-hooks/installer-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,14 @@ export function wrapWindowsCmdHookCommand(scriptPath: string): string {
*/
export function buildWindowsAgentHookPostCommand(
source: AgentHookSource,
extraFormLines: readonly string[] = []
extraFormLines: readonly string[] = [],
maxTimeSeconds = 1.5
): string {
// Why: PowerShell startup makes inline per-turn Codex hooks visibly slow, so mirror the POSIX curl path.
// Why: fully-qualify curl so a repo-local curl.exe can't hijack hook payloads.
return [
`"%SystemRoot%\\System32\\curl.exe" -sS -X POST "http://127.0.0.1:%ORCA_AGENT_HOOK_PORT%/hook/${source}" ^`,
' --connect-timeout 0.5 --max-time 1.5 ^',
` --connect-timeout 0.5 --max-time ${maxTimeSeconds} ^`,
' -H "Content-Type: application/x-www-form-urlencoded" ^',
' -H "X-Orca-Agent-Hook-Token: %ORCA_AGENT_HOOK_TOKEN%" ^',
' --data-urlencode "paneKey=%ORCA_PANE_KEY%" ^',
Expand Down
2 changes: 2 additions & 0 deletions src/main/agent-hooks/remote-hook-service-installers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ describe('remote hook service installers', () => {
'UserPromptSubmit',
'Stop',
'StopFailure',
'StopCancelled',
'SessionEnd',
'PreToolUse',
'PostToolUse',
Expand All @@ -415,6 +416,7 @@ describe('remote hook service installers', () => {
expect(grokConfig.hooks.PreToolUse?.[0]?.matcher).toBe('.*')
expect(grokConfig.hooks.PostToolUse?.[0]?.matcher).toBe('.*')
expect(grokConfig.hooks.StopFailure?.[0]?.matcher).toBeUndefined()
expect(grokConfig.hooks.StopCancelled?.[0]?.matcher).toBeUndefined()

const devinText = devin.fs.files.get('/home/dev/.config/devin/config.json')!
// Why: Devin config.json is JSONC — parse it as such, and assert the user's comment
Expand Down
6 changes: 5 additions & 1 deletion src/main/grok/hook-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ describe('GrokHookService', () => {
'SessionEnd',
'SessionStart',
'Stop',
'StopCancelled',
'StopFailure',
'UserPromptSubmit'
].sort()
Expand All @@ -242,8 +243,9 @@ describe('GrokHookService', () => {
expect(config.hooks.PreToolUse[0].matcher).toBe('.*')
expect(config.hooks.PostToolUseFailure[0].matcher).toBe('.*')
expect(config.hooks.PostToolUse[0].matcher).toBe('.*')
// Why: StopFailure must not carry a tool matcher — lifecycle-only event.
// Why: StopFailure / StopCancelled must not carry a tool matcher — lifecycle-only events.
expect(config.hooks.StopFailure[0].matcher).toBeUndefined()
expect(config.hooks.StopCancelled[0].matcher).toBeUndefined()
expect(config.hooks.Notification[0].matcher).toBeUndefined()
// Why: assert the shipped helper still matches what install wrote (regression
// guard if GROK_TOOL_EVENT_MATCHER drifts from install).
Expand All @@ -265,6 +267,8 @@ describe('GrokHookService', () => {
'utf8'
)
expect(script).toContain('/hook/grok')
expect(script).toContain('--max-time 8')
expect(script).not.toContain('--max-time 1.5')
if (process.platform === 'win32') {
expect(script).toContain('%SystemRoot%\\System32\\curl.exe')
// Why: windows-grok-hook-script.test.ts pins the GROK_HOME guard shape itself,
Expand Down
9 changes: 7 additions & 2 deletions src/main/grok/hook-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
import { buildPosixHookPayloadCapture } from '../agent-hooks/hook-stdin-contract'
import {
buildWindowsGrokHookScript,
GROK_HOME_ENVELOPE_MAX_LENGTH
GROK_HOME_ENVELOPE_MAX_LENGTH,
GROK_HOOK_POST_MAX_TIME_SECONDS
} from './windows-grok-hook-script'

// Why: Grok's tool-event matcher is a real regex (see Grok hooks docs). Bare
Expand All @@ -39,6 +40,8 @@ const GROK_EVENTS = [
// Why: Grok can end a turn on API error without a normal Stop; without this
// the sidebar can stick on working (same rationale as Claude StopFailure).
{ eventName: 'StopFailure', definition: { hooks: [{ type: 'command', command: '' }] } },
// Why (#15225): Grok 1.0+ fires StopCancelled instead of Stop on Ctrl+C / Esc / max-turns.
{ eventName: 'StopCancelled', definition: { hooks: [{ type: 'command', command: '' }] } },
{ eventName: 'SessionEnd', definition: { hooks: [{ type: 'command', command: '' }] } },
{
eventName: 'PreToolUse',
Expand Down Expand Up @@ -130,7 +133,9 @@ function getManagedScript(target: 'local' | 'posix' = 'local'): string {
// `payload=$VALUE` arg, so tens-of-KB tool output stays off the curl
// command line (EDR command-line false positives). Wire body is identical.
'printf \'%s\' "$payload" | curl -sS -X POST "http://127.0.0.1:${ORCA_AGENT_HOOK_PORT}/hook/grok" \\',
' --connect-timeout 0.5 --max-time 1.5 \\',
// Why: Grok Stop stdin includes lastAssistantMessage (up to 32 KiB). The
// shared 1.5s bound drops those posts; TUI still shows `stop [hooks: 1]`.
` --connect-timeout 0.5 --max-time ${GROK_HOOK_POST_MAX_TIME_SECONDS} \\`,
' -H "Content-Type: application/x-www-form-urlencoded" \\',
' -H "X-Orca-Agent-Hook-Token: ${ORCA_AGENT_HOOK_TOKEN}" \\',
' --data-urlencode "paneKey=${ORCA_PANE_KEY}" \\',
Expand Down
13 changes: 12 additions & 1 deletion src/main/grok/windows-grok-hook-script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { buildWindowsGrokHookScript } from './windows-grok-hook-script'
import {
buildWindowsGrokHookScript,
GROK_HOOK_POST_MAX_TIME_SECONDS
} from './windows-grok-hook-script'

/**
* Conservative model of cmd's read-then-evaluate order: `%VAR:~n%` is expanded
Expand Down Expand Up @@ -59,6 +62,14 @@ describe('buildWindowsGrokHookScript', () => {
expect(lines.filter((line) => line.includes('!'))).toEqual([])
})

it('uses a longer curl deadline than the shared 1.5s hook bound', () => {
const script = buildWindowsGrokHookScript()

expect(GROK_HOOK_POST_MAX_TIME_SECONDS).toBeGreaterThan(1.5)
expect(script).toContain(`--max-time ${GROK_HOOK_POST_MAX_TIME_SECONDS}`)
expect(script).not.toContain('--max-time 1.5')
})

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
it('re-checks the envelope after appending the trailing-backslash sentinel', () => {
const lines = buildWindowsGrokHookScript().split('\r\n')
const appended = lines.indexOf(
Expand Down
15 changes: 11 additions & 4 deletions src/main/grok/windows-grok-hook-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ import {
/** Matches the envelope length cap used by the POSIX grok-hook branch. */
export const GROK_HOME_ENVELOPE_MAX_LENGTH = 4096

const WINDOWS_GROK_HOOK_POST_COMMAND = buildWindowsAgentHookPostCommand('grok', [
// Why: attach grokHome before payload@- without string-replacing the shared template.
' --data-urlencode "grokHome=%ORCA_GROK_HOME%" ^'
])
/** Curl deadline for Grok posts (#15225). Stop payloads carry lastAssistantMessage. */
export const GROK_HOOK_POST_MAX_TIME_SECONDS = 8

const WINDOWS_GROK_HOOK_POST_COMMAND = buildWindowsAgentHookPostCommand(
'grok',
[
// Why: attach grokHome before payload@- without string-replacing the shared template.
' --data-urlencode "grokHome=%ORCA_GROK_HOME%" ^'
],
GROK_HOOK_POST_MAX_TIME_SECONDS
)

/**
* Windows `grok-hook.cmd` body.
Expand Down
120 changes: 120 additions & 0 deletions src/shared/agent-hook-listener-grok.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,114 @@ describe('shared agent-hook-listener', () => {
expect(failed?.payload.toolInput).toBeUndefined()
})

it('maps Grok StopCancelled to done', () => {
normalizeHookPayload(
state,
'grok',
{
paneKey: PANE_KEY,
payload: { hookEventName: 'UserPromptSubmit', prompt: 'do work' }
},
'production'
)
const done = normalizeHookPayload(
state,
'grok',
{
paneKey: PANE_KEY,
payload: { hookEventName: 'StopCancelled', reason: 'user_interrupt' }
},
'production'
)
expect(done?.payload).toMatchObject({
agentType: 'grok',
state: 'done',
prompt: 'do work'
})
})

it('maps Grok idle_prompt and task_complete notifications to done', () => {
normalizeHookPayload(
state,
'grok',
{ paneKey: PANE_KEY, payload: { hookEventName: 'UserPromptSubmit', prompt: 'ship it' } },
'production'
)

const idle = normalizeHookPayload(
state,
'grok',
{
paneKey: PANE_KEY,
payload: {
hookEventName: 'Notification',
notificationType: 'idle_prompt',
message: 'Waiting for input'
}
},
'production'
)
expect(idle?.payload).toMatchObject({
state: 'done',
prompt: 'ship it',
agentType: 'grok'
})

normalizeHookPayload(
state,
'grok',
{
paneKey: PANE_KEY,
payload: {
hookEventName: 'PreToolUse',
toolName: 'read_file',
toolInput: { path: 'README.md' }
}
},
'production'
)
const complete = normalizeHookPayload(
state,
'grok',
{
paneKey: PANE_KEY,
payload: { hookEventName: 'Notification', notification_type: 'task_complete' }
},
'production'
)
expect(complete?.payload).toMatchObject({
state: 'done',
prompt: 'ship it',
agentType: 'grok'
})
})

it('maps legacy Grok idle notification copy to done', () => {
normalizeHookPayload(
state,
'grok',
{ paneKey: PANE_KEY, payload: { hookEventName: 'UserPromptSubmit', prompt: 'ship it' } },
'production'
)
const event = normalizeHookPayload(
state,
'grok',
{
paneKey: PANE_KEY,
payload: {
hookEventName: 'Notification',
message: 'Type your message · Enter send · Shift-Tab normal'
}
},
'production'
)
expect(event?.payload).toMatchObject({
state: 'done',
prompt: 'ship it',
agentType: 'grok'
})
})

it('maps Grok StopFailure to done', () => {
normalizeHookPayload(
state,
Expand Down Expand Up @@ -527,6 +635,18 @@ describe('shared agent-hook-listener', () => {
).toBe(true)
})

it('treats Grok StopCancelled chat history as pending result text', () => {
expect(
hasPendingAgentResultText('grok', {
payload: {
hookEventName: 'StopCancelled',
sessionId: '019e37f4-5135-7b63-a4ab-6d13aa6bf528',
cwd: '/tmp/workspace'
}
})
).toBe(true)
})

it('enriches a long-cwd Grok result after async discovery completes', async () => {
const tmpDir = mkdtempSync(join(tmpdir(), 'orca-grok-pending-home-'))
const hookGrokHome = join(tmpDir, 'hook-grok')
Expand Down
18 changes: 13 additions & 5 deletions src/shared/agent-hook-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ export function preparePendingGrokResultDiscovery(
envelope?.hookEventName ??
record.hook_event_name ??
record.hookEventName
if (!isGrokEvent(eventName, 'stop', 'session_end')) {
if (!isGrokEvent(eventName, 'stop', 'session_end', 'stop_cancelled')) {
return null
}
const metadata = readGrokSessionMetadata(
Expand Down Expand Up @@ -2285,7 +2285,7 @@ function extractGrokToolFields(
}
return update
}
if (isGrokEvent(eventName, 'stop', 'session_end', 'stop_failure')) {
if (isGrokEvent(eventName, 'stop', 'session_end', 'stop_failure', 'stop_cancelled')) {
const direct =
readString(hookPayload, 'lastAssistantMessage') ??
readString(hookPayload, 'last_assistant_message')
Expand Down Expand Up @@ -2407,7 +2407,15 @@ function isGrokRoutinePermissionPromptNotification(
)
}

function isGrokIdleNotification(message: string | undefined): boolean {
function isGrokIdleNotification(
notificationType: string | undefined,
message: string | undefined
): boolean {
// Why (#15225): Grok 1.0+ documents idle_prompt / task_complete as the official
// idle types. Message-text matching is a leftover from earlier Build TUI copy.
if (isGrokEvent(notificationType, 'idle_prompt', 'task_complete')) {
return true
}
if (!message) {
return false
}
Expand Down Expand Up @@ -4150,7 +4158,7 @@ function normalizeGrokEvent(
stateName = 'working'
} else if (isUserInputPreTool) {
stateName = 'waiting'
} else if (isGrokEvent(eventName, 'stop', 'session_end', 'stop_failure')) {
} else if (isGrokEvent(eventName, 'stop', 'session_end', 'stop_failure', 'stop_cancelled')) {
stateName = 'done'
} else if (
isGrokEvent(eventName, 'notification') &&
Expand All @@ -4168,7 +4176,7 @@ function normalizeGrokEvent(
stateName = 'waiting'
} else if (
isGrokEvent(eventName, 'notification') &&
isGrokIdleNotification(notificationMessage)
isGrokIdleNotification(notificationType, notificationMessage)
) {
stateName = 'done'
}
Expand Down
Loading