Repository navigation
Expand file tree
/
Copy pathclaudecode.ts
More file actions
121 lines (110 loc) · 7.48 KB
/
Copy pathclaudecode.ts
File metadata and controls
121 lines (110 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/** Claude Code bridge. Claude Code fires hooks by running a command from `settings.json` for each event; that command receives the hook payload as JSON on stdin and must emit the response JSON on stdout. token-goat's installer normally wires this to the `token-goat hook <event>` subcommand directly. {@link CLAUDECODE_HOOK_SCRIPT} is the standalone Node shim form of that wiring — a small script that reads stdin, shells out to `token-goat hook <event>`, and relays the result. The wire format matters: Claude Code reads exactly what the shim prints to stdout, so the shim must pass the child's stdout through verbatim and emit `{}` (a no-op) on any failure rather than crashing the hook. */
/** Node source for the Claude Code hook shim. Behavior: 1. Validate `eventName` (argv[2]) against the closed set of known hook events; if it doesn't match, print `{}` immediately without ever building a shell command from it. 2. Read the full hook payload from stdin (JSON). 3. Spawn `token-goat hook <eventName>`, feeding it that payload on stdin. 4. Print the child's stdout verbatim to this process's stdout. 5. On any error (spawn failure, non-JSON, missing binary) print `{}` so the tool call proceeds unchanged instead of the hook hard-failing. `eventName` is taken from `process.argv[2]`, mirroring how the installed settings.json command appends the event name as the last argument. It is concatenated into a shell command string below (`shell: true` is required on Windows to resolve the token-goat `.cmd`/`.bat` shim), so it is validated against `VALID_HOOK_EVENTS` first — a closed set that must be kept in sync with `HOOK_EVENTS` in src/types.ts. */
import { shimFallbackTimeoutConst, SHIM_ASYNC_DETACH, SHIM_MAX_BUFFER_CONST, SHIM_OWN_COMMAND_BYPASS, SHIM_REQUIRES, SHIM_SPAWN_LADDER, SHIM_TRY_IN_PROCESS, SHIM_TRY_SERVER, SHIM_VALID_HOOK_EVENTS } from './shim_common.js'
// A second copy of this logic lives in src/hook_adapters.ts, which the resident hook server runs for a harness-aware (v2) client; tests/native_hook_adapter_equivalence.test.ts drives this installed shim and a server request with the same payloads and fails unless both print the same bytes and exit code.
export const CLAUDECODE_HOOK_SCRIPT = `#!/usr/bin/env node
// token-goat Claude Code hook shim. Reads the hook payload on stdin, forwards it to \`token-goat hook <event>\`, and relays the response on stdout.
'use strict'
// Cache V8's compiled bytecode for the 3.4 MB hook bundle this shim import()s below. Without it
// every hook invocation recompiles that bundle from source, which profiled at ~40ms of the ~148ms
// a single hook takes -- and a hook fires on every tool call. Worth ~23ms per invocation, measured
// twice on the installed shim by interleaving both variants: 148ms to 125ms, and later 186ms to
// 163ms on a busier machine. The floor moves with load; the 23ms it removes does not.
//
// It has to be here rather than inside the bundle: a module is compiled before any of its own code
// runs, so a bundle cannot enable the cache for itself. This shim is small enough to compile in
// well under a millisecond, and the dynamic import() happens after this line, so the bundle is
// compiled with the cache already active. Same reason the MCP SDK could not be deferred from
// inside mcp_server.ts -- what a module can affect starts after it is already compiled.
//
// enableCompileCache landed in Node 22.1 and package.json allows >=22.0.0, so a miss is possible;
// it is also best-effort by nature (read-only cache dir, full disk). Swallow everything: this shim
// must never fail a tool call to save itself 23ms.
try { require('node:module').enableCompileCache() } catch { /* older Node, or an unwritable cache dir: run uncached */ }
${SHIM_REQUIRES}
${SHIM_VALID_HOOK_EVENTS}
${SHIM_TRY_SERVER}
${SHIM_TRY_IN_PROCESS}
${SHIM_MAX_BUFFER_CONST}
${shimFallbackTimeoutConst('claudecode')}
${SHIM_ASYNC_DETACH}
${SHIM_OWN_COMMAND_BYPASS}
async function main() {
const eventName = process.argv[2] || ''
if (!VALID_HOOK_EVENTS.has(eventName)) {
process.stdout.write('{}')
return
}
let input = ''
try {
input = require('node:fs').readFileSync(0, 'utf8')
} catch {
process.stdout.write('{}')
return
}
// A pre_tool_use Bash call that is token-goat's own CLI has nothing for the pipeline below to
// say, so this skips the round trip entirely rather than backgrounding it -- see
// isOwnTokenGoatCommand's docstring in shim_common.ts for the measured emission rate this
// forfeits.
if (eventName === 'pre_tool_use') {
try {
const payload = JSON.parse(input)
if (payload['tool_name'] === 'Bash' && isOwnTokenGoatCommand(payload['tool_input'] && payload['tool_input']['command'])) {
process.stdout.write('{}')
return
}
} catch {
// Fall through to normal handling on unparseable stdin.
}
}
// Print this before the in-process/spawn round trip below, not after: the harness detaches on
// the first stdout line it sees, and the whole point is not waiting out that round trip for a
// call that was always going to answer '{}'. asyncDetachAtMs, taken at this exact print, is the
// last instant this process's elapsed time and the harness's own wait are the same number --
// record it now so the finally block in relayInProcess (relay.ts) can report what Claude Code
// actually waited on instead of this call's full lifetime.
let asyncDetachAtMs
if (isAsyncDetachEligible(eventName, input)) {
asyncDetachAtMs = performance.now()
process.stdout.write('{"async":true}\\n')
}
// process.argv[3], when present, is the absolute path to the running token-goat CLI
// entry (mirrors the same argv[3]-baked-entry convention used by the Codex/Copilot
// CLI shims). Try the in-process hook lib first (tryInProcess above), which avoids
// spawning a second node process altogether; fall back to the entry path directly via
// process.execPath, then finally to the old PATH-based shell:true invocation (an
// older cached hook config with no argv[3], or a config wired directly to
// \`token-goat hook <event>\` rather than through this shim). A
// SHIM_FALLBACK_TIMEOUT_MS timeout/killSignal on both spawnSync fallbacks means token-goat degrades to its own
// fail-open '{}' rather than being force-killed by Claude Code's own hook timeout (30 s on UserPromptSubmit).
const entryPath = process.argv[3]
let stdout = await tryInProcess(entryPath, eventName, input, asyncDetachAtMs)
if (stdout === undefined) {
// Reaching here with asyncDetachAtMs set means tryInProcess failed after
// isAsyncDetachEligible had already parsed this same input successfully, so the cause is an
// install missing its dist/token-goat-hook.mjs sibling or a broken import -- never a bad
// payload. asyncDetachAtMs is this process's own elapsed time and has no meaning in the
// spawned child's clock, so it is deliberately not threaded across that boundary; the child
// falls back to timing its own lifetime, the same already-approximate number every hook got
// on this fallback path before async-detach existed.
${SHIM_SPAWN_LADDER}
: spawnSync('token-goat hook ' + eventName, {
input,
encoding: 'utf8',
shell: true,
timeout: SHIM_FALLBACK_TIMEOUT_MS,
killSignal: 'SIGKILL',
maxBuffer: SHIM_MAX_BUFFER_BYTES,
})
if (res.status !== 0 || !res.stdout) {
process.stdout.write('{}')
return
}
stdout = res.stdout
}
process.stdout.write(stdout)
}
main().catch(() => {
process.stdout.write('{}')
})
`