// src/utils/cursor-payload.ts // Grok Build (formerly Cursor) hook payloads, read in node9's own vocabulary. // // The desktop app sends its own dialect to every hook, whether the hook is in // its native ~/.cursor/hooks.json or imported from ~/.claude/settings.json // (its "Third-Party Imports" setting, on by default): camelCase event names // (`preToolUse`, `beforeSubmitPrompt`, `postToolUse`), the shell tool named // `Shell`, an empty `cwd`, and the workspace in `workspace_roots`. Verified // against captured payloads of Grok Build 3.32.24 on Linux (2026-12-01). // // Before this adapter a Cursor tool call read as Claude Code (it carries // `tool_use_id`) and a Cursor prompt as an unrecognised payload, which // `node9 check` blocked: every prompt in the app was refused. // // One copy at the boundary of `check` and `log`, so everything after it (the // gate, the rules, the audit) keeps speaking Claude's vocabulary. The payload // the agent sent is never changed. import path from 'path'; import { inspectableArgs } from './hook-payload'; /** The agent key in audit rows and on the dashboard. Shown to people as * "Grok Build (Cursor)"; the key stays so existing rows keep grouping. */ export const CURSOR_AGENT = 'Cursor'; /** * What node9 does with one Cursor hook event: * - `tool`: a call to check (preToolUse); * - `prompt`: a prompt to check (beforeSubmitPrompt); * - `after`: a call that ran, to record (postToolUse); * - `pass`: anything else, which node9 neither checks nor records. */ export type CursorKind = 'tool' | 'prompt' | 'after' | 'pass'; /** The Cursor events node9 reads, and the Claude event each one is. Claude's * own spelling is listed too: the app sends it nowhere today, but a payload * that carries it is the same call. */ // A Map, not an object: an event named `constructor` or `__proto__` must not // match an Object prototype member. const CURSOR_EVENT_TO_CLAUDE: ReadonlyMap = new Map([ ['preToolUse', { event: 'PreToolUse', kind: 'tool' }], ['PreToolUse', { event: 'PreToolUse', kind: 'tool' }], ['beforeSubmitPrompt', { event: 'UserPromptSubmit', kind: 'prompt' }], ['UserPromptSubmit', { event: 'UserPromptSubmit', kind: 'prompt' }], ['postToolUse', { event: 'PostToolUse', kind: 'after' }], ['PostToolUse', { event: 'PostToolUse', kind: 'after' }], ]); /** * The Grok Build events that carry no call for node9 to gate (Grok Build * hooks docs, and the live capture of 3.32.24). Every other event is treated * as a possible call: an event the app adds or renames later must never pass * unchecked just because node9 does not know its name. */ export const CURSOR_NON_CALL_EVENTS: ReadonlySet = new Set([ 'sessionStart', 'sessionEnd', 'stop', 'subagentStart', 'subagentStop', 'preCompact', 'beforeShellExecution', 'beforeMCPExecution', 'beforeReadFile', 'beforeTabFileRead', 'afterShellExecution', 'afterMCPExecution', 'afterFileEdit', 'afterAgentResponse', 'afterAgentThought', 'postToolUse', 'postToolUseFailure', ]); /** True when node9 knows this Cursor event name (mapped or known non-call). */ export function isKnownCursorEvent(event: string): boolean { return CURSOR_EVENT_TO_CLAUDE.has(event) || CURSOR_NON_CALL_EVENTS.has(event); } /** * The Cursor permission events: a hook on one of these that prints no valid * JSON blocks the action (Grok Build hooks docs). An allow there must say so. */ export const CURSOR_PERMISSION_EVENTS: ReadonlySet = new Set([ 'preToolUse', 'beforeShellExecution', 'beforeMCPExecution', 'beforeReadFile', 'beforeTabFileRead', 'subagentStart', ]); /** * True for a payload Grok Build wrote. Every captured event carries * `cursor_version`; the camelCase event names alone are not used, because * another agent's dialect could share one of them. */ export function isCursorPayload(payload: Record): boolean { return typeof payload.cursor_version === 'string' && payload.cursor_version.length > 0; } /** The workspace the agent works in: `workspace_roots[0]`, else the * `CURSOR_PROJECT_DIR` the app sets for every hook. Absolute paths only: the * value steers config resolution. Undefined when neither is usable. */ export function cursorWorkspaceDir( payload: Record, env: NodeJS.ProcessEnv = process.env ): string | undefined { const roots = payload.workspace_roots; if (Array.isArray(roots) && typeof roots[0] === 'string' && path.isAbsolute(roots[0])) { return roots[0]; } const fromEnv = env.CURSOR_PROJECT_DIR; if (typeof fromEnv === 'string' && path.isAbsolute(fromEnv)) return fromEnv; return undefined; } /** Cursor's Shell input `{command, cwd, timeout}`: `cwd` is often empty, and * an empty or relative one is dropped rather than read against node9's own * working directory (the hook runs in ~/.cursor). */ function shellInput(input: unknown): unknown { if (typeof input !== 'object' || input === null || Array.isArray(input)) return input; const { cwd, ...rest } = input as Record; if (typeof cwd === 'string' && path.isAbsolute(cwd)) return { ...rest, cwd }; return rest; } /** A server name that fits node9's `mcp____` form. */ const MCP_SERVER_NAME = /^[^_](?:[^_]|_(?!_))*$/; /** `tool_output` is a JSON string; read it as the object it holds, made safe * to walk like the rest of the payload. Text that is not JSON stays text. */ function toolResponse(output: string): unknown { try { return inspectableArgs(JSON.parse(output)); } catch { return output; } } /** * The Claude-shaped copy of a Cursor payload, the Cursor event it came from, * and what node9 does with it. Pure: reads `env` only for the workspace. */ export function normalizeCursorPayload( payload: Record, env: NodeJS.ProcessEnv = process.env, /** The server an `MCP:` call goes to (cursor-hooks.ts), if known. */ resolveMcpServer?: (tool: string) => string | undefined ): { payload: Record; event: string; kind: CursorKind } { const event = typeof payload.hook_event_name === 'string' ? payload.hook_event_name : ''; const out: Record = { ...payload }; // The payload's `cwd` is "" in every capture: the workspace stands in. if (!(typeof payload.cwd === 'string' && path.isAbsolute(payload.cwd))) { const dir = cursorWorkspaceDir(payload, env); if (dir) out.cwd = dir; else delete out.cwd; } if (typeof out.session_id !== 'string' && typeof payload.conversation_id === 'string') { out.session_id = payload.conversation_id; } let mapped: { event: string; kind: CursorKind } | undefined = CURSOR_EVENT_TO_CLAUDE.get(event); if (!mapped && !CURSOR_NON_CALL_EVENTS.has(event)) { // An event node9 does not know (a renamed or new one, or none at all): // what it carries decides. A tool name is a call to check, a prompt is a // prompt to check; only a payload with neither passes. if (typeof payload.tool_name === 'string' && payload.tool_name) { mapped = { event: 'PreToolUse', kind: 'tool' }; } else if (typeof payload.prompt === 'string') { mapped = { event: 'UserPromptSubmit', kind: 'prompt' }; } } if (!mapped) return { payload: out, event, kind: 'pass' }; out.hook_event_name = mapped.event; if (mapped.kind === 'tool' || mapped.kind === 'after') { // Shell is Cursor's name for the shell tool. Mapped here, not in // canonicalToolName, so no other agent's tool named Shell changes meaning. if (payload.tool_name === 'Shell') { out.tool_name = 'Bash'; out.tool_input = shellInput(payload.tool_input); } // MCP:: an MCP call with no server name. Named as node9 names MCP // tools, so the gate treats it as MCP (taint, the scan of what is sent, // the add-on rules); an unresolved server is `unknown`. if (typeof payload.tool_name === 'string' && payload.tool_name.startsWith('MCP:')) { const tool = payload.tool_name.slice(4); const server = tool ? resolveMcpServer?.(tool) : undefined; out.tool_name = `mcp__${server && MCP_SERVER_NAME.test(server) ? server : 'unknown'}__${tool}`; } if ( mapped.kind === 'after' && out.tool_response === undefined && typeof payload.tool_output === 'string' ) { out.tool_response = toolResponse(payload.tool_output); } } return { payload: out, event, kind: mapped.kind }; }