|
| 1 | +// `agentlock hook cursor <event>` — Cursor IDE's command-hook bridge to |
| 2 | +// the daemon. Cursor (≥1.7) spawns this binary, writes the hook payload |
| 3 | +// to stdin, and reads exit code + stdout JSON to decide whether the tool |
| 4 | +// call (or shell exec, or MCP call) proceeds. |
| 5 | +// |
| 6 | +// Wire shape (Cursor → daemon → Cursor): |
| 7 | +// stdin: Cursor's native event JSON (e.g. preToolUse with |
| 8 | +// conversation_id / generation_id / tool_use_id) |
| 9 | +// POST: /v1/hooks/cursor/<event> (raw payload forwarded verbatim) |
| 10 | +// reply: daemon emits the shared claudeHookOutput envelope |
| 11 | +// stdout: Cursor's expected {permission, agent_message?} shape |
| 12 | +// exit: 0 on allow, 2 on deny — Cursor reads either, both for safety |
| 13 | +// |
| 14 | +// Cursor exposes a dedicated `ask` permission, but ADR 0018 forbids |
| 15 | +// in-harness approval prompts. We map daemon `ask` → Cursor `deny` with |
| 16 | +// a fixed reason pointing at the dashboard. |
| 17 | +// |
| 18 | +// Failure modes are fail-open (exit 0). The daemon's ledger is the source |
| 19 | +// of truth; a missing daemon should not soft-brick the user's IDE. |
| 20 | +// Operators who want fail-closed semantics can install with |
| 21 | +// `failClosed: true` in the wired hook entries — Cursor will then treat |
| 22 | +// any exit-code error as a deny regardless of what we write to stdout. |
| 23 | + |
| 24 | +import { clearDaemonDownMarker, warnDaemonDownOnce } from "../util/daemon-warn.ts"; |
| 25 | + |
| 26 | +const ALLOWED_EVENTS = new Set([ |
| 27 | + "session-start", |
| 28 | + "pre-tool-use", |
| 29 | + "before-shell-execution", |
| 30 | + "before-mcp-execution", |
| 31 | + "after-mcp-execution", |
| 32 | + "post-tool-use", |
| 33 | + "stop", |
| 34 | +]); |
| 35 | + |
| 36 | +interface ClaudeHookSpecifics { |
| 37 | + hookEventName?: string; |
| 38 | + permissionDecision?: "allow" | "deny" | "ask"; |
| 39 | + permissionDecisionReason?: string; |
| 40 | +} |
| 41 | + |
| 42 | +interface DaemonResponse { |
| 43 | + continue?: boolean; |
| 44 | + stopReason?: string; |
| 45 | + hookSpecificOutput?: ClaudeHookSpecifics; |
| 46 | +} |
| 47 | + |
| 48 | +function defaultDaemonUrl(): string { |
| 49 | + return ( |
| 50 | + process.env.AGENTLOCK_DAEMON_URL ?? |
| 51 | + process.env.AGENTLOCK_CONTROL_PLANE_URL ?? |
| 52 | + "http://127.0.0.1:7878" |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +async function readStdin(): Promise<string> { |
| 57 | + const chunks: Buffer[] = []; |
| 58 | + for await (const chunk of process.stdin) { |
| 59 | + chunks.push(chunk as Buffer); |
| 60 | + } |
| 61 | + return Buffer.concat(chunks).toString("utf8"); |
| 62 | +} |
| 63 | + |
| 64 | +function emitAllow(): never { |
| 65 | + process.stdout.write(JSON.stringify({ permission: "allow" }) + "\n"); |
| 66 | + process.exit(0); |
| 67 | +} |
| 68 | + |
| 69 | +function emitDeny(reason: string): never { |
| 70 | + process.stderr.write(`${reason}\n`); |
| 71 | + process.stdout.write( |
| 72 | + JSON.stringify({ permission: "deny", agent_message: reason }) + "\n", |
| 73 | + ); |
| 74 | + process.exit(2); |
| 75 | +} |
| 76 | + |
| 77 | +export async function runHookCursor(argv: string[]): Promise<void> { |
| 78 | + const event = argv[0]; |
| 79 | + if (!event || !ALLOWED_EVENTS.has(event)) { |
| 80 | + process.stderr.write( |
| 81 | + `usage: agentlock hook cursor <session-start|pre-tool-use|before-shell-execution|before-mcp-execution|after-mcp-execution|post-tool-use|stop>\n`, |
| 82 | + ); |
| 83 | + process.exit(2); |
| 84 | + } |
| 85 | + |
| 86 | + const raw = await readStdin(); |
| 87 | + if (!raw.trim()) { |
| 88 | + // No payload — nothing to forward. Fail-open: let Cursor continue. |
| 89 | + process.exit(0); |
| 90 | + } |
| 91 | + |
| 92 | + // Validate JSON locally so a malformed body doesn't waste a daemon |
| 93 | + // round trip. Pass through verbatim if it parses. |
| 94 | + try { |
| 95 | + JSON.parse(raw); |
| 96 | + } catch (e) { |
| 97 | + process.stderr.write( |
| 98 | + `agentlock hook cursor ${event}: invalid JSON on stdin: ${(e as Error).message}\n`, |
| 99 | + ); |
| 100 | + // Fail-open: invalid payload is the harness's bug, not policy. |
| 101 | + process.exit(0); |
| 102 | + } |
| 103 | + |
| 104 | + const url = |
| 105 | + defaultDaemonUrl().replace(/\/+$/, "") + `/v1/hooks/cursor/${event}`; |
| 106 | + let res: Response; |
| 107 | + try { |
| 108 | + res = await fetch(url, { |
| 109 | + method: "POST", |
| 110 | + headers: { "Content-Type": "application/json" }, |
| 111 | + body: raw, |
| 112 | + }); |
| 113 | + } catch (e) { |
| 114 | + warnDaemonDownOnce("cursor", url, e as Error); |
| 115 | + process.exit(0); // fail-open |
| 116 | + } |
| 117 | + |
| 118 | + // Successful round-trip — re-arm the nudge for the next outage. |
| 119 | + clearDaemonDownMarker(); |
| 120 | + |
| 121 | + if (!res.ok) { |
| 122 | + process.stderr.write( |
| 123 | + `agentlock hook cursor ${event}: daemon returned ${res.status}\n`, |
| 124 | + ); |
| 125 | + process.exit(0); // fail-open |
| 126 | + } |
| 127 | + |
| 128 | + let parsed: DaemonResponse; |
| 129 | + try { |
| 130 | + parsed = (await res.json()) as DaemonResponse; |
| 131 | + } catch (e) { |
| 132 | + process.stderr.write( |
| 133 | + `agentlock hook cursor ${event}: malformed daemon response: ${(e as Error).message}\n`, |
| 134 | + ); |
| 135 | + process.exit(0); |
| 136 | + } |
| 137 | + |
| 138 | + // Only PreToolUse / shell / MCP gates can deny. Observability events |
| 139 | + // (session-start, post-tool-use, after-mcp-execution, stop) come back |
| 140 | + // with {continue: true} and no permissionDecision — exit 0 silently. |
| 141 | + const decision = parsed.hookSpecificOutput?.permissionDecision; |
| 142 | + if (decision === "deny") { |
| 143 | + const reason = |
| 144 | + parsed.hookSpecificOutput?.permissionDecisionReason ?? |
| 145 | + parsed.stopReason ?? |
| 146 | + "blocked by OpenAgentLock policy"; |
| 147 | + emitDeny(reason); |
| 148 | + } |
| 149 | + if (decision === "ask") { |
| 150 | + // ADR 0018: no in-harness approval prompts. Map to deny with a |
| 151 | + // fixed reason directing the user to the dashboard. |
| 152 | + emitDeny("approval pending — use dashboard"); |
| 153 | + } |
| 154 | + |
| 155 | + // Allow path. Some Cursor events don't carry hookSpecificOutput at |
| 156 | + // all (the daemon returns plain {continue: true}); treat that the |
| 157 | + // same as an explicit allow. |
| 158 | + if (decision === "allow") { |
| 159 | + emitAllow(); |
| 160 | + } |
| 161 | + process.exit(0); |
| 162 | +} |
0 commit comments