Repository navigation
Expand file tree
/
Copy pathdispatch.ts
More file actions
792 lines (722 loc) · 43.1 KB
/
Copy pathdispatch.ts
File metadata and controls
792 lines (722 loc) · 43.1 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// impl/09 §4 (handleHook), ADR-0009 §1, §6 and §8.2, ADR-0016; DEVIATIONS D-M3-arch-3 (case-B wrapper
// next to the hook bundle), D-M3-H2-1..7, D-M4-arch-5 and D-M4-H2-1..3 (compact "none" branches, decision rows).
// One path per event. Every failure fails open: allow, one stderr line. Never block the host.
// D-CI-PERF-7: node:fs through getBuiltinModule; a static ESM import of node:fs also loads fs/promises, readline and streams.
const { lstatSync, readFileSync } = process.getBuiltinModule("node:fs") as typeof import("node:fs");
import { dirname, join, relative, resolve, sep } from "node:path";
import { AGENT_IDS, type AgentId } from "../contracts/agents.ts";
import {
COMPACTED_META_PREFIX, COMPACT_POINTER_LINE, COMPACT_STATE_KEY, DECISION_OPTION_KEY, DECISION_SOURCE, DECISION_TOOL, EDIT_PATH_KEYS, EDIT_TOOLS, EXIT_CODE_LINE_PREFIX,
OWN_TOOL_PREFIX, prefixToolNames, REPLACE_CHUNK_BYTES, REPLACE_HANDLE_LINE, REPLACE_RETURN_BYTES, REPLACE_TRIGGER_BYTES, RANGE_READ_TOOLS, READ_PATH_KEYS, READ_RANGE_KEYS, SHAPE_RETURN_BYTES, PARSE_ERROR_DENY_LINE, PENDING_COMPACT_FLAG, PENDING_COMPACT_KEY,
PENDING_COMPACT_META_PREFIX, POLICY_UNREADABLE_LINE, RECALL_KEY, RECALL_MAX_ROWS, RECALL_SEEN_META_PREFIX, RECALL_UNIT_CANDIDATES, RECALL_UNITS_SEEN_META_PREFIX, RESUME_HINT, RESUME_HINT_KEY, RUN_WRAPPER_FILE,
RUN_WRAPPER_AGENT, RUN_WRAPPER_NODE, SEARCH_TWIN_LINE, SEARCH_WRAPPER_FILE, SUBAGENT_HANDLE_LINE, ECHO_MAX_BYTES, SHELL_TOOL, SOURCE_LABEL_MAX_CHARS, SUBAGENT_SOURCE_PREFIX, SYNC_INDEX_MAX_BYTES, TOOL_PREFIX_SLOT,
USER_DENY_REASON_PREFIX,
SUBAGENT_RUN_DENY, WORKSPACE_FOLDER_FLAG, PROJECTS_DIR_NAME, CLAUDE_TOOL_RESULTS_DIR_NAME, PERSISTED_OUTPUT_MAX_BYTES,
} from "../contracts/constants.ts";
import type {
AdapterRow, CanonicalHookEvent, Chunk, Decision, DbHandle, EventKind, HookOutput, ProjectContext, ToolCall, ToolResultInfo,
} from "../contracts/types.ts";
import { getAdapter, hasRecall, hasReplace } from "../../adapters/table.ts";
import { decide } from "../policy/decide.ts";
import { isOwnFile } from "../policy/own-file.ts";
import { isSubagentCall, pickSubagentCase, toolSetForAgentType, type ToolSetSources } from "../policy/subagent.ts";
import { readUserDeny } from "../policy/user-deny.ts";
import { resolveProject } from "../project/resolve.ts";
import { claudeDirOf, homeDirOf } from "../project/config-dir.ts";
import { isInside } from "../project/contain.ts";
import { metaGet, metaSet, metaTake, openDb } from "../store/db.ts";
import { appendEvent, canonicalKey } from "../journal/append.ts";
import { hasAgentRows, hasSessionRows, lastSessionSeq, readLastPrompt, readMaxSeq, readRecallRows, readSessionEvents } from "../journal/read.ts";
import { finishReserved, insertChunks, insertUnits, queryUnits, readMaxSourceId, recallQueryText, reserveSource, sourceHandle, type SourceDraft } from "../store/fts.ts";
import { buildUnits } from "../index/units.ts";
import { renderState } from "../compaction/render.ts";
import { pickRecall } from "../compaction/recall.ts";
import { chunkText } from "../index/chunk.ts";
import { moreLine, selectChunksInMemory } from "../index/select.ts";
import { shapeDigest } from "../index/shape.ts";
import { indexText } from "../index/text.ts";
import { appendDetached, detach, pendingJobFiles, releaseDrainLock, takeDrainLock, touchDrainLock, writeJournal, type DetachedJob } from "./detached.ts";
import { askedWords } from "./handle-query.ts";
import { answerDecisions, askOptionLabels, optionEchoDecision, type DecisionPart } from "./decision.ts";
import { shouldHint } from "./throttle.ts";
import { CtxError } from "../contracts/errors.ts";
import { allowOutput, failureOutput, fault, logHookError, nodeTooOld } from "./fallback.ts";
export { failureOutput, logHookError } from "./fallback.ts"; // D-HNE-GUARD-1: the entry's last-resort catch
export { readStdin, writeOut } from "./io.ts";
export { DETACHED_JOBS_ARG, deferDetachedTo, spawnDetached, type DetachedJob } from "./detached.ts"; // D-M7-HP-1: entry I/O without process.stdin/stdout streams
const ALLOW: Decision = { kind: "allow" };
const EMPTY: HookOutput = { stdout: "", exitCode: 0 };
const DIGIT_0 = 48;
function isAgentId(s: string): s is AgentId {
return (AGENT_IDS as readonly string[]).includes(s);
}
function message(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}
function str(v: unknown): string {
return typeof v === "string" ? v : "";
}
/** impl/09 §4.2. Never throws: decode, project and dispatch failures all end in this agent's allow shape. */
export async function handleHook(agent: AgentId, raw: unknown, env: NodeJS.ProcessEnv): Promise<HookOutput> {
if (!isAgentId(agent)) {
process.stderr.write("hooks: unknown agent " + JSON.stringify(agent) + "\n"); // step 1, A22, D-FU-PS-1
return EMPTY;
}
const row = getAdapter(agent);
// D-HNE-GUARD-1: every failure below ends in failureOutput (exit 0, this host's shape, tool table only) and a log line.
const report = (stage: string, err: unknown, line?: string): void => {
logHookError(env, agent, stage, err);
process.stderr.write(line ?? "hooks: " + stage + " failed: " + message(err) + "\n");
};
const fail = (stage: string, err: unknown, line?: string): HookOutput => {
report(stage, err, line);
return failureOutput(agent, raw, err);
};
if (nodeTooOld(process.version)) {
// D-1370-OLDNODE-ALLOW-1 (#1370): the MCP server runs on this same node and is degraded (PRD §13: ctx_doctor only), so a
// redirect to ctx_execute would block the host. Allow, and keep the stderr and log lines (D5). An in-process host
// (Pi, OMP, OpenCode) registers its ctx_* tools itself, so its redirect target exists and the D-HNE-GUARD-1 deny stays.
const err = new CtxError("NODE_TOO_OLD", { v: process.version });
if (row.installMode === "in-process") return fail("node", err);
report("node", err);
return allowOutput(agent);
}
let ev: CanonicalHookEvent;
try {
fault(env, "decode");
ev = row.decode(raw);
} catch (err) {
return fail("decode", err); // step 2
}
// D-1323-CODEX-STOP-1: Stop has no project or store work; it gets this agent's allow reply before the project step.
if (ev.event === "Stop") return row.encode(ev, ALLOW);
let project: ProjectContext;
try {
fault(env, "project");
// D-HA-PI-PROJECT-1: on pi the entry puts ctx.cwd into the payload, so ev.cwd is the pi:ctx.cwd source.
// D-HF-VSCODE-PROJECT-1: a VS Code hook has no launch args; its workspace folder is the payload cwd.
const argv = agent === "vscode" ? [WORKSPACE_FOLDER_FLAG, ev.cwd] : process.argv;
project = resolveProject(agent, { env, argv, cwd: ev.cwd, piCwd: getAdapter(agent).projectSource === "pi:ctx.cwd" ? ev.cwd : undefined }); // step 3 (pi, omp: ADR-0026)
} catch (err) {
// D-FU-PS-1 keeps its one stderr line (code first); the decision part is new.
return fail("project", err, "hooks: no project: " + String((err as { code?: unknown }).code ?? "ERROR") + " " + message(err) + "\n"); // D-HNE-GUARD-1 supersedes D-FU-PS-1: a flooding call is denied, the rest allowed
}
let decision: Decision;
const settle: Settle = {};
try {
fault(env, "policy");
decision = await dispatch(ev, row, project, env, settle); // step 4
} catch (err) {
return fail("store", err);
}
try {
fault(env, "encode");
const out = row.encode(ev, decision); // step 6
settle.run?.(true);
return out;
} catch (err) {
settle.run?.(false); // D-STATS-RET-3: the host gets the failure output, the whole tool output
return fail("encode", err);
}
}
/** D-STATS-RET-3: PostToolUse leaves its journal write here; handleHook runs it once the encode stage is known. */
interface Settle { run?: (encoded: boolean) => void }
function dispatch(ev: CanonicalHookEvent, row: AdapterRow, project: ProjectContext, env: NodeJS.ProcessEnv, settle: Settle): Promise<Decision> {
switch (ev.event) {
case "SessionStart":
return sessionStart(ev, row, project);
case "UserPromptSubmit":
return userPromptSubmit(ev, row, project);
case "PreToolUse":
return preToolUse(ev, row, project, env);
case "PostToolUse":
return postToolUse(ev, row, project, env, settle);
case "PreCompact":
return preCompact(ev, row, project);
case "Usage":
return usage(ev, project);
}
return Promise.resolve(ALLOW); // step 4 defensive default
}
function withPrefix(template: string, row: AdapterRow): string {
return template.split(TOOL_PREFIX_SLOT).join(row.toolNamePrefix);
}
// ---------- §4.3 SessionStart ----------
// D6: "host" sends the fixed pointer line only; "none" sends the full renderState block (§4.3 step 1.3,
// §4.4 step 2.4). Rows are this session's journal, read once per compact delivery.
function compactText(db: DbHandle, ev: CanonicalHookEvent, row: AdapterRow): string {
if (row.compactSummary === "host") return prefixToolNames(COMPACT_POINTER_LINE, row.toolNamePrefix);
return renderState(readSessionEvents(db, ev.sessionId), ev.turn ?? 0, undefined, row.toolNamePrefix);
}
async function sessionStart(ev: CanonicalHookEvent, row: AdapterRow, project: ProjectContext): Promise<Decision> {
if (ev.source === "compact" && hasRecall(row)) markCompacted(await openDb(project), ev.sessionId);
if (ev.source === "compact" && row.compactDelivery === "sessionstart") {
const db = await openDb(project);
// A23, #1396: the host sends one SessionStart(compact) per compaction with the same session id. The
// marker key carries the newest journal seq, so each compaction gets its state block and a duplicate
// delivery of the same compaction (no new row in between) stays silent.
const onceKey = COMPACT_STATE_KEY + ":" + lastSessionSeq(db, ev.sessionId);
if (!shouldHint(db, ev.sessionId, onceKey)) return ALLOW;
return { kind: "hint", text: compactText(db, ev, row), onceKey };
}
const db = await openDb(project);
// impl/12 §4.6 step 2, D-M5-P2-4: the OpenCode plugin's one `context` call per session reads and clears
// the pending-compact marker here (handleHook is the one DB path); its stdout is the cache-stable payload.
if (row.compactDelivery === "opencode-cache" && metaTake(db, PENDING_COMPACT_META_PREFIX + ev.sessionId) !== null) {
return { kind: "hint", text: compactText(db, ev, row), onceKey: PENDING_COMPACT_KEY };
}
if (!shouldHint(db, ev.sessionId, RESUME_HINT_KEY)) return ALLOW;
return { kind: "hint", text: withPrefix(RESUME_HINT, row), onceKey: RESUME_HINT_KEY }; // D-M3-H2-2
}
// ---------- §4.4 UserPromptSubmit ----------
async function userPromptSubmit(ev: CanonicalHookEvent, row: AdapterRow, project: ProjectContext): Promise<Decision> {
const body = ev.prompt ?? "";
// D-FU-GCH-2: decided before this turn's own prompt row is queued, so that row never counts as "something to resume".
const resume = await firstTurnResume(ev, row, project);
appendDetached(project, ev.sessionId, {
session_id: ev.sessionId, worktree: project.worktree, agent: ev.agent, kind: "prompt", tool: null, key: null,
path: null, ok: 1, exit_code: null, bytes: Buffer.byteLength(body), body, turn: ev.turn ?? 0,
sandbox_bytes: 0, returned_bytes: 0,
});
echoDetached(ev, project, body);
if (row.compactDelivery === "pending-flag") {
const db = await openDb(project);
// §4.4 step 2.2: DELETE ... RETURNING inside BEGIN IMMEDIATE, so the delivery fires once.
if (metaTake(db, PENDING_COMPACT_META_PREFIX + ev.sessionId) !== null) {
return { kind: "hint", text: compactText(db, ev, row), onceKey: PENDING_COMPACT_KEY };
}
}
if (resume !== null) return resume;
return recall(ev, row, project, body);
}
// D-FU-GCH-2: a row with no SessionStart hook (Gemini: hooks/hooks.json drops it, D-FU-GCH-1) gets the
// resume hint on the first UserPromptSubmit of a session instead. First turn = the payload session id has
// no journal row yet (a host-resumed session keeps its id and its rows, and the host replays that history
// itself). The marker is the SessionStart one (RESUME_HINT_KEY, same session id), so the hint fires at most
// once per session. It fires only when this worktree already holds a journal row of this agent.
async function firstTurnResume(ev: CanonicalHookEvent, row: AdapterRow, project: ProjectContext): Promise<Decision | null> {
if (row.eventMap.SessionStart !== undefined || ev.sessionId === "") return null;
const db = await openDb(project);
if (hasSessionRows(db, ev.sessionId)) return null;
if (!shouldHint(db, ev.sessionId, RESUME_HINT_KEY)) return null;
if (!hasAgentRows(db, project.worktree, ev.agent)) return null;
return { kind: "hint", text: withPrefix(RESUME_HINT, row), onceKey: RESUME_HINT_KEY };
}
// ---------- DECISIONS-r11 (B) turn-boundary recall ----------
// #1394: the value is the boundary of the latest compaction, {"seq": newest journal seq, "source": newest
// sources id}. Rows and outputs written after it are still in the model's live context, so recall skips them.
function markCompacted(db: DbHandle, sessionId: string): void {
if (sessionId === "") return;
metaSet(db, COMPACTED_META_PREFIX + sessionId, JSON.stringify({ seq: readMaxSeq(db), source: readMaxSourceId(db) }));
}
// A value from before #1394 ("1") carries no boundary: every row stays a candidate, as before.
function compactBoundary(value: string): { seq: number; source: number } {
const all = { seq: Number.MAX_SAFE_INTEGER, source: Number.MAX_SAFE_INTEGER };
try {
const v = JSON.parse(value) as unknown;
if (typeof v !== "object" || v === null) return all;
const { seq, source } = v as { seq?: unknown; source?: unknown };
return {
seq: typeof seq === "number" ? seq : all.seq,
source: typeof source === "number" ? source : all.source,
};
} catch {
return all;
}
}
function seenSeqs(value: string | null): number[] {
if (value === null) return [];
try {
const v = JSON.parse(value) as unknown;
return Array.isArray(v) ? v.filter((n): n is number => typeof n === "number") : [];
} catch {
return [];
}
}
function seenUnitKeys(value: string | null): string[] {
if (value === null) return [];
try {
const v = JSON.parse(value) as unknown;
return Array.isArray(v) ? v.filter((k): k is string => typeof k === "string") : [];
} catch {
return [];
}
}
// No compaction in this session (or a row without UserPromptSubmit additionalContext): ALLOW, zero added
// bytes. After one: BM25 of this session's rows and the same worktree's prior-session rows against the
// prompt; whole rows over RECALL_MIN_SCORE, within RECALL_BUDGET_BYTES; a row goes out once per session.
async function recall(ev: CanonicalHookEvent, row: AdapterRow, project: ProjectContext, prompt: string): Promise<Decision> {
if (!hasRecall(row) || ev.sessionId === "") return ALLOW;
const db = await openDb(project);
const compacted = metaGet(db, COMPACTED_META_PREFIX + ev.sessionId);
if (compacted === null) return ALLOW;
const boundary = compactBoundary(compacted);
const seenKey = RECALL_SEEN_META_PREFIX + ev.sessionId;
const seen = seenSeqs(metaGet(db, seenKey));
// r3-ledger: one ranked query over this session's line units, by the current prompt only (D-1330-RECALL-PROMPT-1).
const unitsKey = RECALL_UNITS_SEEN_META_PREFIX + ev.sessionId;
const seenUnits = seenUnitKeys(metaGet(db, unitsKey));
const units = queryUnits(db, ev.sessionId, recallQueryText(prompt), RECALL_UNIT_CANDIDATES, boundary.source);
const rows = readRecallRows(db, ev.sessionId, project.worktree, RECALL_MAX_ROWS, boundary.seq);
const picked = pickRecall(rows, prompt, new Set(seen), units, new Set(seenUnits));
if (picked.text === "") return ALLOW;
if (picked.seqs.length > 0) metaSet(db, seenKey, JSON.stringify([...seen, ...picked.seqs]));
if (picked.units !== undefined) metaSet(db, unitsKey, JSON.stringify([...seenUnits, ...picked.units]));
return { kind: "hint", text: picked.text, onceKey: RECALL_KEY };
}
// ---------- ADR-0009 §8.2 decision rows (D-M4-arch-5, D-M4-H2-2) ----------
// Meta key that holds the option labels of the last unanswered AskUserQuestion call for one session.
function optionsKey(sessionId: string): string {
return DECISION_SOURCE + ":" + DECISION_OPTION_KEY + ":" + sessionId;
}
interface PendingOptions { seq: number; labels: string[] }
function readOptions(value: string): PendingOptions | null {
try {
const v = JSON.parse(value) as unknown;
if (typeof v !== "object" || v === null) return null;
const { seq, labels } = v as Record<string, unknown>;
if (typeof seq !== "number" || !Array.isArray(labels)) return null;
return { seq, labels: labels.filter((l): l is string => typeof l === "string") };
} catch {
return null;
}
}
// Rule 2, detached like the prompt row (A21): the labels offered in the turn before this prompt apply to
// this prompt only (taken, then gone). The metaGet check keeps a prompt with nothing offered write-free.
function echoDetached(ev: CanonicalHookEvent, project: ProjectContext, prompt: string): void {
if (ev.sessionId === "") return;
detach({ kind: "echo", ev, project, prompt }, () => echoWrite(ev, project, prompt));
}
function echoWrite(ev: CanonicalHookEvent, project: ProjectContext, prompt: string): Promise<void> {
const key = optionsKey(ev.sessionId);
return openDb(project)
.then((db) => {
if (metaGet(db, key) === null) return;
const taken = metaTake(db, key);
const offered = taken === null ? null : readOptions(taken);
if (offered === null) return;
const part = optionEchoDecision(prompt, offered.labels, offered.seq);
if (part !== null) writeDecisions(db, ev, project, [part]);
})
.catch((err: unknown) => {
process.stderr.write("hooks: decision write failed: " + message(err) + "\n");
});
}
// One decision = one kind:"decision" journal row + its body as one chunk under DECISION_SOURCE (D-M4-arch-5).
function writeDecisions(db: DbHandle, ev: CanonicalHookEvent, project: ProjectContext, parts: readonly DecisionPart[]): void {
for (const part of parts) {
appendEvent(db, {
session_id: ev.sessionId, worktree: project.worktree, agent: ev.agent, kind: "decision", tool: DECISION_TOOL,
key: part.key, path: null, ok: 1, exit_code: null, bytes: Buffer.byteLength(part.body), body: part.body,
turn: ev.turn ?? 0, sandbox_bytes: 0, returned_bytes: 0,
});
indexText(db, project, ev.sessionId, DECISION_SOURCE, part.body, "text");
}
}
// Rule 1 at PostToolUse, detached like every hook journal write (A21). An answered call writes its rows;
// an unanswered one keeps its labels for the next prompt, with ref = seq of this turn's prompt row.
function askDetached(ev: CanonicalHookEvent, project: ProjectContext, tool: ToolCall, text: string): void {
const parts = answerDecisions(text);
const labels = parts.length === 0 ? askOptionLabels(tool.input) : [];
if (parts.length === 0 && labels.length === 0) return;
detach({ kind: "ask", ev, project, tool, text }, () => askWrite(ev, project, parts, labels));
}
function askWrite(ev: CanonicalHookEvent, project: ProjectContext, parts: DecisionPart[], labels: string[]): Promise<void> {
return openDb(project)
.then((db) => {
if (parts.length > 0) return writeDecisions(db, ev, project, parts);
const prompts = readSessionEvents(db, ev.sessionId, "prompt");
const last = prompts[prompts.length - 1];
if (last === undefined || ev.sessionId === "") return; // no turn to point at: no row (fail-safe)
metaSet(db, optionsKey(ev.sessionId), JSON.stringify({ seq: last.seq, labels }));
})
.catch((err: unknown) => {
process.stderr.write("hooks: decision write failed: " + message(err) + "\n");
});
}
// ADR-0016 l.23 (#1348): an in-process host hands the agent's tool set on the event; it is the manifest source.
function toolSetSources(ev: CanonicalHookEvent, project: ProjectContext, env: NodeJS.ProcessEnv): ToolSetSources {
const src: ToolSetSources = { projectDir: project.worktree, env };
if (ev.agentTools !== undefined) src.manifestTools = ev.agentTools;
return src;
}
// ---------- §4.5 PreToolUse ----------
async function preToolUse(ev: CanonicalHookEvent, row: AdapterRow, project: ProjectContext, env: NodeJS.ProcessEnv): Promise<Decision> {
const userDeny = readUserDeny(project.worktree, ev.agent);
const decision0 = decide(ev, row, userDeny, project);
let decision = decision0;
if (decision0.kind === "deny" && isSubagentCall(ev) && !isUserPolicyDeny(decision0.reason, row)) {
// Case A keeps decision0 (D-M3-H2-4); case B names the run.mjs wrapper for a shell call.
// D-SUB-NOCTX-1 (#1311): a deny names only tools the caller has. A caller without ctx_* tools runs the call:
// a known tool set without them, or a named agent type no source defines on a row that replaces afterwards
// (PostToolUse case C keeps a large output out). A call with no agent type inherits the session tools (case A).
const set = toolSetForAgentType(ev, toolSetSources(ev, project, env));
const subCase = pickSubagentCase(ev, set);
const tool = ev.tool;
const runs = set !== null || ((ev.agentType ?? "") !== "" && hasReplace(row));
if (subCase === "B" && tool !== undefined && isShell(tool, row)) decision = caseB(ev, row, decision0, userDeny);
else if (subCase !== "A" && runs) decision = ALLOW;
}
return decision; // no PreToolUse hint, so no throttle marker and no DB touch (D-M7-RR-2)
}
function isShell(tool: ToolCall, row: AdapterRow): boolean {
return tool.canonicalName === SHELL_TOOL || row.nativeTools.shell.includes(tool.name);
}
// The user's own rules stay as they are: the wrapper would refuse them too (D-M3-H2-5).
function isUserPolicyDeny(reason: string, row: AdapterRow): boolean {
return reason.startsWith(USER_DENY_REASON_PREFIX) || reason === prefixToolNames(POLICY_UNREADABLE_LINE, row.toolNamePrefix)
|| reason === prefixToolNames(PARSE_ERROR_DENY_LINE, row.toolNamePrefix);
}
function b64(s: string): string {
return Buffer.from(s, "utf8").toString("base64url");
}
// §4.5 step 3 case B, D-M3-arch-3: run.mjs sits next to the running hook bundle (impl/13 §2).
function caseB(ev: CanonicalHookEvent, row: AdapterRow, d0: Extract<Decision, { kind: "deny" }>, userDeny: string[] | null): Decision {
const tool = ev.tool;
if (tool === undefined || !isShell(tool, row) || isUserPolicyDeny(d0.reason, row)) return d0;
const command = tool.input.command;
if (typeof command !== "string") return d0;
const wrapper = '"' + join(dirname(process.argv[1] ?? ""), RUN_WRAPPER_FILE) + '"';
const rewritten = [RUN_WRAPPER_NODE, wrapper, b64(command), b64(JSON.stringify(userDeny ?? [])), b64(str(tool.input.description))].join(" ");
return { kind: "deny", reason: SUBAGENT_RUN_DENY, rewritten }; // D-M3-F-7: no ctx_* tool named
}
// ---------- §4.6 PostToolUse ----------
function journalKind(tool: ToolCall, row: AdapterRow, isError: boolean): EventKind | null {
if (isError) return "toolerr";
if ((EDIT_TOOLS as readonly string[]).includes(tool.canonicalName) || (row.nativeTools.edit ?? []).includes(tool.name)) return "edit";
if (isShell(tool, row)) return "cmd";
return null; // A25: a clean Read/Grep/WebFetch/other call has no journal kind
}
/** Sorted-key JSON, so one input always gives one key (ADR-0009 §1 "stable JSON of the input"). */
function stableJson(v: unknown): string {
if (Array.isArray(v)) return "[" + v.map(stableJson).join(",") + "]";
if (typeof v === "object" && v !== null) {
const o = v as Record<string, unknown>;
const keys = Object.keys(o).filter((k) => o[k] !== undefined).sort();
return "{" + keys.map((k) => JSON.stringify(k) + ":" + stableJson(o[k])).join(",") + "}";
}
return JSON.stringify(v) ?? "null";
}
/** ADR-0009 §1: first line starts with "Exit code ", then a digit loop. No such line -> null. */
function exitCodeOf(text: string): number | null {
const nl = text.indexOf("\n");
const first = nl < 0 ? text : text.slice(0, nl);
if (!first.startsWith(EXIT_CODE_LINE_PREFIX)) return null;
let n = 0;
let digits = 0;
for (const ch of first.slice(EXIT_CODE_LINE_PREFIX.length)) {
const d = ch.charCodeAt(0) - DIGIT_0;
if (d < 0 || d > 9) break;
n = n * 10 + d;
digits++;
}
return digits === 0 ? null : n;
}
function pathOf(input: Record<string, unknown>): string | null {
for (const k of EDIT_PATH_KEYS) if (typeof input[k] === "string") return input[k] as string;
return null;
}
async function postToolUse(ev: CanonicalHookEvent, row: AdapterRow, project: ProjectContext, env: NodeJS.ProcessEnv, settle: Settle): Promise<Decision> {
const tool = ev.tool;
if (tool === undefined || tool.canonicalName.startsWith(OWN_TOOL_PREFIX)) return ALLOW; // step 1 (ADR-0009 §0)
const result = ev.result ?? { isError: false, bytes: 0 };
const text = wholeOutput(result, ev.sessionId, env);
if (result.persistedPath !== undefined) result.bytes = Buffer.byteLength(text);
if (tool.canonicalName === DECISION_TOOL) askDetached(ev, project, tool, text); // ADR-0009 §8.2
const kind = journalKind(tool, row, result.isError); // step 2
// D-STATS-RET-1: the row is written once, after the decision, so returned_bytes is what the model got:
// the replace text when the hook replaced the output, result.bytes when it allowed it whole. Same detached write.
// D-STATS-RET-3: and after the encode stage, so a failed encode (the host gets the whole output) is not a saving.
const journal = (returned: number): void => {
if (kind === null) return;
const key = kind === "cmd" ? canonicalKey(tool.canonicalName, str(tool.input.command))
: kind === "toolerr" ? tool.canonicalName + ":" + stableJson(tool.input) : null; // step 3
appendDetached(project, ev.sessionId, { // step 4; body is the whole text (D-M3-H2-6)
session_id: ev.sessionId, worktree: project.worktree, agent: ev.agent, kind, tool: tool.canonicalName, key,
path: pathOf(tool.input), ok: result.isError ? 0 : 1, exit_code: exitCodeOf(text), bytes: result.bytes,
body: text, turn: ev.turn ?? 0, sandbox_bytes: result.bytes, returned_bytes: returned,
});
};
let decision: Decision;
try {
decision = await postReplace(ev, row, project, env, tool, result, text);
} catch (err) {
journal(result.bytes); // the call still gets its row; the caller turns the throw into the failure output
throw err;
}
const decided = decision;
settle.run = (encoded) => journal(encoded && decided.kind === "replace" ? Buffer.byteLength(decided.output) : result.bytes);
return decision;
}
/** D-R48-OWN-FILE-1 (#1326): a read-like tool whose target is one of the agent's own instruction or skill files. */
function ownRead(ev: CanonicalHookEvent, tool: ToolCall, env: NodeJS.ProcessEnv): boolean {
if (!RANGE_READ_TOOLS.includes(tool.canonicalName)) return false;
const p = READ_PATH_KEYS.map((k) => tool.input[k]).find((v): v is string => typeof v === "string" && v !== "");
return p !== undefined && isOwnFile(ev.agent, ev.cwd, p, env);
}
async function postReplace(ev: CanonicalHookEvent, row: AdapterRow, project: ProjectContext, env: NodeJS.ProcessEnv,
tool: ToolCall, result: ToolResultInfo, text: string): Promise<Decision> {
// step 5: replace, DECISIONS-r9 F1 split. Subagent case B/C (ADR-0016), and the main agent on a row
// with a replace channel ("run, then replace", D-M7-RR-1, DECISIONS-r10 (1)).
if (result.bytes <= REPLACE_TRIGGER_BYTES) return ALLOW; // D-M7-SV-1
if (boundedRead(tool)) return ALLOW; // D-R41-READ-OFFSET-1 (#1314): the user asked for these lines
if (ompRangedRead(ev, tool)) return ALLOW; // D-1392-OMP-RANGE-1 (#1392): the same window, written in the path
if (ownRead(ev, tool, env)) return ALLOW; // D-R48-OWN-FILE-1 (#1326): a skill or rule file reaches the model whole
if (result.keep === true) return ALLOW; // D-M2-MCP-1: an MCP result with a non-text block stays as the host made it
// ADR-0001: one path per row, no runtime fallback. A row with no replace channel gets no replace decision on
// any path, the subagent one too (D-STATS-RET-2): its encoder drops it, so the model gets the whole output.
if (!hasReplace(row)) return ALLOW;
// D-CC-POINTER-1 (#1342, ADR-0016 line 16): every caller keeps the same replace budget; case A is no passthrough.
// The pointer names only what the caller can call: ctx_search for the main session and case A, the plain
// SUBAGENT_HANDLE_LINE for case B and C. D-CC-POINTER-4: the subagent label carries the call id, as mainLabel does.
let label: string;
let caller: Caller = { ctx: true, shell: true, read: false };
if (isSubagentCall(ev)) {
// #1348: toolSetSources carries the OpenCode 2.x agentTools next to the project dir and env.
const set = toolSetForAgentType(ev, toolSetSources(ev, project, env));
const subCase = pickSubagentCase(ev, set);
const has = (names: readonly string[]): boolean => set !== null && names.some((n) => set.includes(n));
caller = { ctx: subCase === "A", shell: has(row.nativeTools.shell),
read: RANGE_READ_TOOLS.includes(tool.canonicalName) && has(row.nativeTools.read ?? []) };
// D-1382-NO-ROUTE-1 (#1382, ADR-0016 lines 11 and 16): a known tool set with no ctx tool, no shell and no ranged
// Read has no call that opens the handle, so the replace is a dead end: the output reaches it whole.
if (set !== null && !caller.ctx && !caller.shell && !caller.read) return ALLOW;
label = SUBAGENT_SOURCE_PREFIX + tool.canonicalName + ":" + (tool.callId ?? String(ev.turn ?? 0));
} else {
label = mainLabel(tool, ev);
}
// D-M7-RR-3: no description -> the command text, file path, pattern or URL, never an empty query.
// D-CC-POINTER-2 (#1358): an MCP input has none of them, so the tool name is the last field.
const intent = [tool.input.description, tool.input.command, tool.input.file_path, tool.input.pattern, tool.input.url, tool.name]
.map(str).find((v) => v.trim() !== "") ?? tool.canonicalName;
// One chunk pass for both forms (D-M7-FT-2): the stored chunks and the selected chunks are the same
// array, so the in-memory selection equals selectChunks over the stored source without reading it back.
const chunks = chunkText(text, "text", REPLACE_CHUNK_BYTES); // D-M7-SV-1: small chunks, line-aligned cuts
// D-CR3-HANDLE-1: the handle is the short id of the stored row. Up to SYNC_INDEX_MAX_BYTES the whole write
// is sync; above it (F1) only the source row is written now (one small row, D-CR3-HANDLE-2) and the
// chunks and line units follow detached under that id, so the handle never falls back to the long label.
const db = await openDb(project);
let sourceId: number;
if (result.bytes <= SYNC_INDEX_MAX_BYTES) {
sourceId = indexText(db, project, ev.sessionId, label, text, "text", chunks).sourceId;
insertUnits(db, sourceId, ev.sessionId, buildUnits(text)); // r3-ledger
} else {
sourceId = reserveSource(db, sourceRowOf(project, ev.sessionId, label), chunks);
indexDetached(project, ev.sessionId, sourceId, label, text, chunks);
}
const handle = sourceHandle(sourceId);
// D-M7-SV-5: rank by the intent and the session's last user prompt (any word), so a bare cat still
// returns the line the user asked for. The more line keeps the intent. No prompt -> intent alone.
const prompt = await lastPrompt(project, ev.sessionId);
const rankQuery = prompt.trim() === "" ? undefined : intent + " " + prompt;
// D-M2-SHAPE-1: a parser-found shape (JSON rows, a line log) gives its rare units the query names, whole,
// within the same budget, plus the handle. No shape or no named rare unit -> the BM25 pick below, unchanged.
// D-CR3-HANDLE-1: one handle line replaces the more line and counts inside the budget. The budget per
// replace is the size bound of D-CR3-HANDLE-1 (REPLACE_RETURN_BYTES of selection + the more line with the full
// label); the selection gets that budget minus the handle line, so the selection plus the handle line never grows.
// D-CC-POINTER-2: on claude-code the search.mjs twin comes on top of that bound (two absolute paths, about 120-250 B).
// D-HA-GEMINI-HINT-1: a second query of the prompt words the output holds, so the call returns the asked line.
const handleLine = caller.ctx
? REPLACE_HANDLE_LINE(intent, handle, result.bytes, row.toolNamePrefix, askedWords(intent, prompt, text))
: SUBAGENT_HANDLE_LINE(handle, result.bytes, caller.read);
const budget = replaceBudget(handleLine, moreLine(intent, label));
// D-CC-POINTER-2 (#1358): on the claude-code row a caller with a shell also gets the MCP-free search.mjs call,
// which works when the MCP server is not connected. It sits after the budget: the selection is unchanged.
const pointer = handleLine + (caller.shell && ev.agent === RUN_WRAPPER_AGENT ? searchTwin(handle, intent, project) : "");
const digest = shapeDigest(text, rankQuery ?? intent, SHAPE_RETURN_BYTES); // D-M2-JSON-1
if (digest !== null) return { kind: "replace", output: replaceOutput(digest, pointer) };
const selected = selectChunksInMemory(chunks, intent, budget, label, rankQuery);
return { kind: "replace", output: selected.more !== null ? replaceOutput(selected.text, pointer) : selected.text };
}
type Caller = { ctx: boolean; shell: boolean; read: boolean };
// search.mjs sits next to hook.mjs (D-M3-arch-3, as caseB's run.mjs). A long intent is not echoed (D-NO-LOSSY-CUT-2).
function searchTwin(handle: string, intent: string, project: ProjectContext): string {
const terms = Buffer.byteLength(intent, "utf8") > ECHO_MAX_BYTES ? "" : intent;
return SEARCH_TWIN_LINE(join(dirname(process.argv[1] ?? ""), SEARCH_WRAPPER_FILE), handle, terms, project.worktree);
}
// D-R41-READ-OFFSET-1 (#1314): a read-like tool called with offset, limit or view_range is a bounded window.
export function boundedRead(tool: ToolCall): boolean {
return RANGE_READ_TOOLS.includes(tool.canonicalName) && READ_RANGE_KEYS.some((k) => tool.input[k] !== undefined && tool.input[k] !== null);
}
// D-1392-OMP-RANGE-1 (#1392, ADR-0026): OMP's read has `path` only and takes the line range inline (OMP 18.21.0
// read-selector.ts parseSel: :N, :N-M, :N+K, :N-, :-N, comma lists, `raw` before or after the range). A path whose
// suffix is a range is a bounded window (D-R41-READ-OFFSET-1). A suffix with no range (none, :raw, :img, a drive
// letter, a colon inside a folder name) is a full read and stays replaced. A file whose literal name holds the suffix
// is read whole by OMP (path-utils.ts splitPathAndSelPreferringLiteral), so it stays replaced too.
function ompRangedRead(ev: CanonicalHookEvent, tool: ToolCall): boolean {
if (ev.agent !== "omp" || !RANGE_READ_TOOLS.includes(tool.canonicalName)) return false;
const p = READ_PATH_KEYS.map((k) => tool.input[k]).find((v): v is string => typeof v === "string" && v !== "");
if (p === undefined || !rangedPathSuffix(p)) return false;
try {
return lstatSync(resolve(ev.cwd, p), { throwIfNoEntry: false }) === undefined;
} catch {
return false; // an unclear probe keeps the full-read path: the replace stays
}
}
// D-1392-OMP-RANGE-1: plain string parse from the last colon, no regex. `<file>:<range>`, `<file>:<range>:raw` or
// `<file>:raw:<range>`. The file part is never empty and never one drive letter (`D:50` is a Windows path).
export function rangedPathSuffix(path: string): boolean {
let cut = path.lastIndexOf(":");
let sel = path.slice(cut + 1);
if (sel.toLowerCase() === "raw") {
cut = path.lastIndexOf(":", cut - 1);
sel = path.slice(cut + 1, path.length - 4);
}
if (cut <= 0 || !rangeList(sel)) return false;
let file = path.slice(0, cut);
if (file.toLowerCase().endsWith(":raw")) file = file.slice(0, -4);
return file.length > 1 || !asciiLetter(file);
}
function rangeList(sel: string): boolean {
return sel !== "" && sel.split(",").every(rangeItem);
}
// One list item: N, N-M, N+K, N- or -N (OMP parseLineRanges and parseTailCount).
function rangeItem(item: string): boolean {
if (item.startsWith("-")) return digitsOnly(item.slice(1));
let i = 0;
while (i < item.length && digit(item[i])) i++;
if (i === 0) return false;
if (i === item.length) return true;
const op = item[i];
const rest = item.slice(i + 1);
return (op === "-" && (rest === "" || digitsOnly(rest))) || (op === "+" && digitsOnly(rest));
}
function digitsOnly(s: string): boolean {
if (s === "") return false;
for (const c of s) if (!digit(c)) return false;
return true;
}
function digit(c: string): boolean {
return c >= "0" && c <= "9";
}
function asciiLetter(s: string): boolean {
const c = s.toLowerCase();
return c.length === 1 && c >= "a" && c <= "z";
}
// D-R4-BUDGET-1: the replace is the body, one newline, the handle line. An empty body (selection budget 0, no
// chunk fits) gives the handle line alone, with no leading newline.
export function replaceOutput(body: string, handleLine: string): string {
return body === "" ? handleLine : body + "\n" + handleLine;
}
// D-R4-BUDGET-1: the selection budget left after the handle line and the newline that joins it to the body.
// It never goes below 0 (a handle line longer than the more line plus REPLACE_RETURN_BYTES leaves no room for a body).
export function replaceBudget(handleLine: string, more: string): number {
return Math.max(0, REPLACE_RETURN_BYTES + Buffer.byteLength("\n" + more) - 1 - Buffer.byteLength(handleLine));
}
// D-SEC-PERSIST-1: a persisted output is read only at <claude dir>/projects/<project>/<this session>/tool-results/<file>:
// a plain file (not a link), inside that dir after realpath, at most PERSISTED_OUTPUT_MAX_BYTES.
function hostToolResult(file: string, sessionId: string, env: NodeJS.ProcessEnv): boolean {
if (sessionId === "" || sessionId.includes(sep) || sessionId.includes("/")) return false;
const projects = join(claudeDirOf(env, homeDirOf(env) ?? ""), PROJECTS_DIR_NAME);
const steps = relative(projects, resolve(file)).split(sep);
if (steps.length !== 4 || steps.includes("..") || steps.includes("")) return false;
if (steps[1] !== sessionId || steps[2] !== CLAUDE_TOOL_RESULTS_DIR_NAME) return false;
const st = lstatSync(file);
return st.isFile() && st.size <= PERSISTED_OUTPUT_MAX_BYTES && isInside(projects, file);
}
// D-M2-SHAPE-2: the whole output when the host persisted it to a file; an unreadable or refused file keeps the head.
function wholeOutput(result: ToolResultInfo, sessionId: string, env: NodeJS.ProcessEnv): string {
const head = result.text ?? "";
if (result.persistedPath === undefined) return head;
try {
if (!hostToolResult(result.persistedPath, sessionId, env)) {
// D-SEC-PERSIST-1: doctor shows this line under "Hook errors"; the model still gets the head.
logHookError(env, "claude-code", "persisted-output", new Error("persisted output path is outside this session's tool-results dir or too large; kept the head"));
return head;
}
return readFileSync(result.persistedPath, "utf8");
} catch {
return head;
}
}
// D-M7-SV-5: best effort; a store that cannot open gives "" and the selection uses the intent alone.
async function lastPrompt(project: ProjectContext, sessionId: string): Promise<string> {
try {
return readLastPrompt(await openDb(project), sessionId);
} catch {
return "";
}
}
// D-M7-RR-1: "<tool>:" + the first line of the command (or the tool name), at most SOURCE_LABEL_MAX_CHARS,
// + "#" + call id when the host gives one, so two runs of one command stay two sources.
function mainLabel(tool: ToolCall, ev: CanonicalHookEvent): string {
const cmd = str(tool.input.command) ?? str(tool.input.file_path) ?? "";
const nl = cmd.indexOf("\n");
const head = (tool.canonicalName + ":" + (nl < 0 ? cmd : cmd.slice(0, nl))).slice(0, SOURCE_LABEL_MAX_CHARS);
return head + "#" + (tool.callId ?? String(ev.turn ?? 0));
}
// F1: above SYNC_INDEX_MAX_BYTES the FTS5 chunk write never blocks the decision; same rules as appendDetached.
// The source row is already reserved (D-CR3-HANDLE-2); chunks and line units land under its id.
function indexDetached(project: ProjectContext, sessionId: string, sourceId: number, label: string, text: string, chunks: Chunk[]): void {
detach({ kind: "index", project, sessionId, sourceId, label, text, chunks }, () => indexWrite(project, sessionId, sourceId, label, text, chunks));
}
function indexWrite(project: ProjectContext, sessionId: string, sourceId: number, label: string, text: string, chunks: Chunk[]): Promise<void> {
return openDb(project)
.then((db) => {
finishReserved(db, sourceId, sourceRowOf(project, sessionId, label), chunks, buildUnits(text)); // r3-ledger, D-R4-RESERVE-1
})
.catch((err: unknown) => {
process.stderr.write("hooks: index write failed: " + message(err) + "\n");
});
}
// The source fields a hook output stores (kind "content"). reserveSource and finishReserved take these only;
// the byte and chunk counts come from the chunks they write (DEVIATIONS D-CR3-HANDLE-2).
export function sourceRowOf(project: ProjectContext, sessionId: string, label: string): SourceDraft {
return { sessionId, worktree: project.worktree, agent: project.agent, kind: "content", label, createdAt: Date.now() };
}
// ---------- §4.7 PreCompact ----------
async function preCompact(ev: CanonicalHookEvent, row: AdapterRow, project: ProjectContext): Promise<Decision> {
const db = await openDb(project);
metaSet(db, PENDING_COMPACT_META_PREFIX + ev.sessionId, PENDING_COMPACT_FLAG);
if (hasRecall(row)) markCompacted(db, ev.sessionId);
return ALLOW;
}
// ---------- Usage (D-M5-arch-2) ----------
// One kind:"cost" row per Usage event (OpenCode step-finish), detached like every hook journal write (A21).
// handleHook is the only writer; a repeat for the same (session_id, turn) is dropped by the store's
// partial unique index, so this path does no dedup of its own. Cost rows are not ctx_stats call rows
// (D-M4-S3-1): tool stays null and the byte columns stay 0.
function usage(ev: CanonicalHookEvent, project: ProjectContext): Promise<Decision> {
appendDetached(project, ev.sessionId, {
session_id: ev.sessionId, worktree: project.worktree, agent: ev.agent, kind: "cost", tool: null, key: null,
path: null, ok: 1, exit_code: null, bytes: 0, body: "", turn: ev.turn ?? 0, sandbox_bytes: 0, returned_bytes: 0,
});
return Promise.resolve(ALLOW);
}
export async function runDetachedJob(job: DetachedJob): Promise<void> {
switch (job.kind) {
case "append":
return writeJournal(job.project, job.row);
case "index":
return indexWrite(job.project, job.sessionId, job.sourceId, job.label, job.text, job.chunks);
case "echo":
return echoWrite(job.ev, job.project, job.prompt);
case "ask": {
const parts = answerDecisions(job.text);
return askWrite(job.ev, job.project, parts, parts.length === 0 ? askOptionLabels(job.tool.input) : []);
}
}
}
export async function drainDetached(projectDir: string): Promise<void> {
const { readFileSync, rmSync } = process.getBuiltinModule("node:fs") as typeof import("node:fs");
while (pendingJobFiles(projectDir).length > 0) {
if (!takeDrainLock(projectDir)) return;
try {
for (let files = pendingJobFiles(projectDir); files.length > 0; files = pendingJobFiles(projectDir)) {
for (const file of files) {
try {
const batch = JSON.parse(readFileSync(file, "utf8")) as DetachedJob[];
for (const job of batch) await runDetachedJob(job);
} catch (err) {
process.stderr.write("hooks: detached job file failed: " + message(err) + "\n");
} finally {
rmSync(file, { force: true });
}
touchDrainLock(projectDir);
}
}
} finally {
releaseDrainLock(projectDir);
}
}
}