@librechat/agents 3.1.66-dev.0 → 3.1.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +24 -15
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/common/enum.cjs +0 -13
  4. package/dist/cjs/common/enum.cjs.map +1 -1
  5. package/dist/cjs/graphs/Graph.cjs +0 -3
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/main.cjs +0 -40
  8. package/dist/cjs/main.cjs.map +1 -1
  9. package/dist/cjs/messages/format.cjs +12 -74
  10. package/dist/cjs/messages/format.cjs.map +1 -1
  11. package/dist/cjs/run.cjs +0 -111
  12. package/dist/cjs/run.cjs.map +1 -1
  13. package/dist/cjs/tools/ToolNode.cjs +140 -304
  14. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  15. package/dist/esm/agents/AgentContext.mjs +24 -15
  16. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  17. package/dist/esm/common/enum.mjs +1 -12
  18. package/dist/esm/common/enum.mjs.map +1 -1
  19. package/dist/esm/graphs/Graph.mjs +0 -3
  20. package/dist/esm/graphs/Graph.mjs.map +1 -1
  21. package/dist/esm/main.mjs +1 -10
  22. package/dist/esm/main.mjs.map +1 -1
  23. package/dist/esm/messages/format.mjs +4 -66
  24. package/dist/esm/messages/format.mjs.map +1 -1
  25. package/dist/esm/run.mjs +0 -111
  26. package/dist/esm/run.mjs.map +1 -1
  27. package/dist/esm/tools/ToolNode.mjs +142 -306
  28. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  29. package/dist/types/agents/AgentContext.d.ts +6 -0
  30. package/dist/types/common/enum.d.ts +1 -7
  31. package/dist/types/graphs/Graph.d.ts +0 -2
  32. package/dist/types/index.d.ts +0 -6
  33. package/dist/types/messages/format.d.ts +1 -2
  34. package/dist/types/run.d.ts +0 -1
  35. package/dist/types/tools/ToolNode.d.ts +2 -24
  36. package/dist/types/types/index.d.ts +0 -1
  37. package/dist/types/types/llm.d.ts +14 -2
  38. package/dist/types/types/run.d.ts +0 -20
  39. package/dist/types/types/tools.d.ts +1 -38
  40. package/package.json +1 -1
  41. package/src/agents/AgentContext.ts +28 -15
  42. package/src/agents/__tests__/AgentContext.test.ts +110 -0
  43. package/src/common/enum.ts +0 -12
  44. package/src/graphs/Graph.ts +0 -4
  45. package/src/index.ts +0 -8
  46. package/src/messages/format.ts +4 -74
  47. package/src/run.ts +0 -126
  48. package/src/tools/ToolNode.ts +169 -391
  49. package/src/tools/__tests__/ToolNode.session.test.ts +12 -12
  50. package/src/types/index.ts +0 -1
  51. package/src/types/llm.ts +16 -2
  52. package/src/types/run.ts +0 -20
  53. package/src/types/tools.ts +1 -41
  54. package/dist/cjs/hooks/HookRegistry.cjs +0 -162
  55. package/dist/cjs/hooks/HookRegistry.cjs.map +0 -1
  56. package/dist/cjs/hooks/executeHooks.cjs +0 -276
  57. package/dist/cjs/hooks/executeHooks.cjs.map +0 -1
  58. package/dist/cjs/hooks/matchers.cjs +0 -256
  59. package/dist/cjs/hooks/matchers.cjs.map +0 -1
  60. package/dist/cjs/hooks/types.cjs +0 -27
  61. package/dist/cjs/hooks/types.cjs.map +0 -1
  62. package/dist/cjs/tools/BashExecutor.cjs +0 -175
  63. package/dist/cjs/tools/BashExecutor.cjs.map +0 -1
  64. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +0 -296
  65. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +0 -1
  66. package/dist/cjs/tools/ReadFile.cjs +0 -43
  67. package/dist/cjs/tools/ReadFile.cjs.map +0 -1
  68. package/dist/cjs/tools/SkillTool.cjs +0 -50
  69. package/dist/cjs/tools/SkillTool.cjs.map +0 -1
  70. package/dist/cjs/tools/skillCatalog.cjs +0 -84
  71. package/dist/cjs/tools/skillCatalog.cjs.map +0 -1
  72. package/dist/esm/hooks/HookRegistry.mjs +0 -160
  73. package/dist/esm/hooks/HookRegistry.mjs.map +0 -1
  74. package/dist/esm/hooks/executeHooks.mjs +0 -273
  75. package/dist/esm/hooks/executeHooks.mjs.map +0 -1
  76. package/dist/esm/hooks/matchers.mjs +0 -251
  77. package/dist/esm/hooks/matchers.mjs.map +0 -1
  78. package/dist/esm/hooks/types.mjs +0 -25
  79. package/dist/esm/hooks/types.mjs.map +0 -1
  80. package/dist/esm/tools/BashExecutor.mjs +0 -169
  81. package/dist/esm/tools/BashExecutor.mjs.map +0 -1
  82. package/dist/esm/tools/BashProgrammaticToolCalling.mjs +0 -287
  83. package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +0 -1
  84. package/dist/esm/tools/ReadFile.mjs +0 -38
  85. package/dist/esm/tools/ReadFile.mjs.map +0 -1
  86. package/dist/esm/tools/SkillTool.mjs +0 -45
  87. package/dist/esm/tools/SkillTool.mjs.map +0 -1
  88. package/dist/esm/tools/skillCatalog.mjs +0 -82
  89. package/dist/esm/tools/skillCatalog.mjs.map +0 -1
  90. package/dist/types/hooks/HookRegistry.d.ts +0 -56
  91. package/dist/types/hooks/executeHooks.d.ts +0 -79
  92. package/dist/types/hooks/index.d.ts +0 -6
  93. package/dist/types/hooks/matchers.d.ts +0 -95
  94. package/dist/types/hooks/types.d.ts +0 -309
  95. package/dist/types/tools/BashExecutor.d.ts +0 -45
  96. package/dist/types/tools/BashProgrammaticToolCalling.d.ts +0 -72
  97. package/dist/types/tools/ReadFile.d.ts +0 -28
  98. package/dist/types/tools/SkillTool.d.ts +0 -40
  99. package/dist/types/tools/skillCatalog.d.ts +0 -19
  100. package/dist/types/types/skill.d.ts +0 -9
  101. package/src/hooks/HookRegistry.ts +0 -208
  102. package/src/hooks/__tests__/HookRegistry.test.ts +0 -190
  103. package/src/hooks/__tests__/executeHooks.test.ts +0 -1013
  104. package/src/hooks/__tests__/integration.test.ts +0 -337
  105. package/src/hooks/__tests__/matchers.test.ts +0 -238
  106. package/src/hooks/__tests__/toolHooks.test.ts +0 -669
  107. package/src/hooks/executeHooks.ts +0 -375
  108. package/src/hooks/index.ts +0 -55
  109. package/src/hooks/matchers.ts +0 -280
  110. package/src/hooks/types.ts +0 -388
  111. package/src/messages/formatAgentMessages.skills.test.ts +0 -334
  112. package/src/tools/BashExecutor.ts +0 -205
  113. package/src/tools/BashProgrammaticToolCalling.ts +0 -397
  114. package/src/tools/ReadFile.ts +0 -39
  115. package/src/tools/SkillTool.ts +0 -46
  116. package/src/tools/__tests__/ReadFile.test.ts +0 -44
  117. package/src/tools/__tests__/SkillTool.test.ts +0 -442
  118. package/src/tools/__tests__/skillCatalog.test.ts +0 -161
  119. package/src/tools/skillCatalog.ts +0 -126
  120. package/src/types/skill.ts +0 -11
@@ -1,84 +0,0 @@
1
- 'use strict';
2
-
3
- const HEADER = '## Available Skills';
4
- const DEFAULT_CONTEXT_WINDOW_TOKENS = 200_000;
5
- const DEFAULT_BUDGET_PERCENT = 0.01;
6
- const DEFAULT_MAX_ENTRY_CHARS = 250;
7
- const DEFAULT_MIN_DESC_LENGTH = 20;
8
- const DEFAULT_CHARS_PER_TOKEN = 4;
9
- /**
10
- * Formats a skill catalog for injection into agent context.
11
- * Uses a truncation ladder: full descriptions, proportional truncation, names-only.
12
- * Returns empty string for empty input.
13
- */
14
- function formatSkillCatalog(skills, opts) {
15
- if (skills.length === 0)
16
- return '';
17
- const contextWindowTokens = opts?.contextWindowTokens ?? DEFAULT_CONTEXT_WINDOW_TOKENS;
18
- const budgetPercent = opts?.budgetPercent ?? DEFAULT_BUDGET_PERCENT;
19
- const maxEntryChars = Math.max(1, opts?.maxEntryChars ?? DEFAULT_MAX_ENTRY_CHARS);
20
- const minDescLength = opts?.minDescLength ?? DEFAULT_MIN_DESC_LENGTH;
21
- const charsPerToken = opts?.charsPerToken ?? DEFAULT_CHARS_PER_TOKEN;
22
- const budgetChars = Math.floor(contextWindowTokens * budgetPercent * charsPerToken);
23
- const capped = skills.map((s) => ({
24
- name: s.name,
25
- description: s.description.length > maxEntryChars
26
- ? s.description.slice(0, maxEntryChars - 1) + '\u2026'
27
- : s.description,
28
- }));
29
- const fullOutput = formatEntries(capped);
30
- if (fullOutput.length <= budgetChars)
31
- return fullOutput;
32
- const headerLen = HEADER.length + 2;
33
- const newlineChars = capped.length > 1 ? capped.length - 1 : 0;
34
- const availableChars = budgetChars - headerLen - newlineChars;
35
- const perEntryOverhead = 4;
36
- const nameCharsTotal = capped.reduce((sum, s) => sum + s.name.length + perEntryOverhead, 0);
37
- const availableForDescs = availableChars - nameCharsTotal;
38
- if (availableForDescs <= 0) {
39
- return fitNamesOnly(capped, budgetChars);
40
- }
41
- const maxDescPerEntry = Math.floor(availableForDescs / capped.length);
42
- if (maxDescPerEntry < minDescLength) {
43
- return fitNamesOnly(capped, budgetChars);
44
- }
45
- const truncated = capped.map((s) => ({
46
- name: s.name,
47
- description: s.description.length > maxDescPerEntry
48
- ? s.description.slice(0, maxDescPerEntry - 1) + '\u2026'
49
- : s.description,
50
- }));
51
- const result = formatEntries(truncated);
52
- if (result.length <= budgetChars)
53
- return result;
54
- return fitNamesOnly(capped, budgetChars);
55
- }
56
- function formatEntries(entries) {
57
- const lines = entries.map((e) => e.description ? `- ${e.name}: ${e.description}` : `- ${e.name}`);
58
- return `${HEADER}\n\n${lines.join('\n')}`;
59
- }
60
- /** Names-only fallback that drops trailing entries if the list still exceeds budget. */
61
- function fitNamesOnly(entries, budgetChars) {
62
- // Format: "HEADER\n\n- name1\n- name2\n..."
63
- // Running sum avoids O(n²) repeated string construction.
64
- const prefix = HEADER.length + 2; // "HEADER\n\n"
65
- const entryOverhead = 2; // "- "
66
- let total = prefix;
67
- let fitCount = 0;
68
- for (let i = 0; i < entries.length; i++) {
69
- const added = (i > 0 ? 1 : 0) + entryOverhead + entries[i].name.length;
70
- if (total + added > budgetChars)
71
- break;
72
- total += added;
73
- fitCount = i + 1;
74
- }
75
- if (fitCount === 0)
76
- return '';
77
- const namesOnly = entries
78
- .slice(0, fitCount)
79
- .map((s) => ({ name: s.name, description: '' }));
80
- return formatEntries(namesOnly);
81
- }
82
-
83
- exports.formatSkillCatalog = formatSkillCatalog;
84
- //# sourceMappingURL=skillCatalog.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"skillCatalog.cjs","sources":["../../../src/tools/skillCatalog.ts"],"sourcesContent":["// src/tools/skillCatalog.ts\nimport type { SkillCatalogEntry } from '@/types';\n\nconst HEADER = '## Available Skills';\nconst DEFAULT_CONTEXT_WINDOW_TOKENS = 200_000;\nconst DEFAULT_BUDGET_PERCENT = 0.01;\nconst DEFAULT_MAX_ENTRY_CHARS = 250;\nconst DEFAULT_MIN_DESC_LENGTH = 20;\nconst DEFAULT_CHARS_PER_TOKEN = 4;\n\nexport type SkillCatalogOptions = {\n /** Total context window in tokens. Default: 200_000 */\n contextWindowTokens?: number;\n /** Fraction of context budget for catalog. Default: 0.01 (1%) */\n budgetPercent?: number;\n /** Max chars per entry description. Default: 250 */\n maxEntryChars?: number;\n /** Descriptions below this length trigger names-only fallback. Default: 20 */\n minDescLength?: number;\n /** Approximate chars per token for budget calculation. Default: 4 */\n charsPerToken?: number;\n};\n\n/**\n * Formats a skill catalog for injection into agent context.\n * Uses a truncation ladder: full descriptions, proportional truncation, names-only.\n * Returns empty string for empty input.\n */\nexport function formatSkillCatalog(\n skills: SkillCatalogEntry[],\n opts?: SkillCatalogOptions\n): string {\n if (skills.length === 0) return '';\n\n const contextWindowTokens =\n opts?.contextWindowTokens ?? DEFAULT_CONTEXT_WINDOW_TOKENS;\n const budgetPercent = opts?.budgetPercent ?? DEFAULT_BUDGET_PERCENT;\n const maxEntryChars = Math.max(\n 1,\n opts?.maxEntryChars ?? DEFAULT_MAX_ENTRY_CHARS\n );\n const minDescLength = opts?.minDescLength ?? DEFAULT_MIN_DESC_LENGTH;\n const charsPerToken = opts?.charsPerToken ?? DEFAULT_CHARS_PER_TOKEN;\n\n const budgetChars = Math.floor(\n contextWindowTokens * budgetPercent * charsPerToken\n );\n\n const capped = skills.map((s) => ({\n name: s.name,\n description:\n s.description.length > maxEntryChars\n ? s.description.slice(0, maxEntryChars - 1) + '\\u2026'\n : s.description,\n }));\n\n const fullOutput = formatEntries(capped);\n if (fullOutput.length <= budgetChars) return fullOutput;\n\n const headerLen = HEADER.length + 2;\n const newlineChars = capped.length > 1 ? capped.length - 1 : 0;\n const availableChars = budgetChars - headerLen - newlineChars;\n const perEntryOverhead = 4;\n const nameCharsTotal = capped.reduce(\n (sum, s) => sum + s.name.length + perEntryOverhead,\n 0\n );\n const availableForDescs = availableChars - nameCharsTotal;\n\n if (availableForDescs <= 0) {\n return fitNamesOnly(capped, budgetChars);\n }\n\n const maxDescPerEntry = Math.floor(availableForDescs / capped.length);\n\n if (maxDescPerEntry < minDescLength) {\n return fitNamesOnly(capped, budgetChars);\n }\n\n const truncated = capped.map((s) => ({\n name: s.name,\n description:\n s.description.length > maxDescPerEntry\n ? s.description.slice(0, maxDescPerEntry - 1) + '\\u2026'\n : s.description,\n }));\n\n const result = formatEntries(truncated);\n if (result.length <= budgetChars) return result;\n return fitNamesOnly(capped, budgetChars);\n}\n\nfunction formatEntries(\n entries: { name: string; description: string }[]\n): string {\n const lines = entries.map((e) =>\n e.description ? `- ${e.name}: ${e.description}` : `- ${e.name}`\n );\n return `${HEADER}\\n\\n${lines.join('\\n')}`;\n}\n\n/** Names-only fallback that drops trailing entries if the list still exceeds budget. */\nfunction fitNamesOnly(\n entries: { name: string }[],\n budgetChars: number\n): string {\n // Format: \"HEADER\\n\\n- name1\\n- name2\\n...\"\n // Running sum avoids O(n²) repeated string construction.\n const prefix = HEADER.length + 2; // \"HEADER\\n\\n\"\n const entryOverhead = 2; // \"- \"\n let total = prefix;\n let fitCount = 0;\n\n for (let i = 0; i < entries.length; i++) {\n const added = (i > 0 ? 1 : 0) + entryOverhead + entries[i].name.length;\n if (total + added > budgetChars) break;\n total += added;\n fitCount = i + 1;\n }\n\n if (fitCount === 0) return '';\n const namesOnly = entries\n .slice(0, f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C,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC,CAAC;AAClD,IAAA,OAAO,aAAa,CAAC,SAAS,CAAC;AACjC;;;;"}
@@ -1,160 +0,0 @@
1
- /**
2
- * Run-scoped storage for hook matchers with an additional layer for
3
- * session-scoped matchers that should be cleaned up between sessions.
4
- *
5
- * Hosts construct one registry per `Run` (mirroring how `HandlerRegistry` is
6
- * scoped) and register global matchers + per-session matchers against it.
7
- * Registration is strictly additive — nothing in this class mutates a
8
- * matcher's callbacks or flags after insertion.
9
- *
10
- * ## Why `Map<sessionId, MatcherBucket>` and not `Record`
11
- *
12
- * LibreChat runs thousands of parallel sessions in one Node process, and
13
- * hook registration happens inside hot paths (tool loading, agent spawning).
14
- * A `Record<sessionId, ...>` has to be spread on every insertion, which is
15
- * O(n) per call and O(n²) total for a batch of parallel registrations. A
16
- * Map mutates in place, keeping insertions O(1). This mirrors the reasoning
17
- * Claude Code documents at `utils/hooks/sessionHooks.ts:62`.
18
- */
19
- class HookRegistry {
20
- global = {};
21
- sessions = new Map();
22
- /**
23
- * Register a matcher for the lifetime of this registry (= one Run).
24
- * Returns an unregister function that removes the matcher by reference.
25
- */
26
- register(event, matcher) {
27
- const list = ensureList(this.global, event);
28
- list.push(widen(matcher));
29
- return () => {
30
- removeFromList(list, matcher);
31
- };
32
- }
33
- /**
34
- * Register a matcher for a specific session. Cleared automatically when
35
- * `clearSession(sessionId)` is called, or can be removed directly via the
36
- * returned unregister function.
37
- */
38
- registerSession(sessionId, event, matcher) {
39
- const bucket = this.ensureSessionBucket(sessionId);
40
- const list = ensureList(bucket, event);
41
- list.push(widen(matcher));
42
- return () => {
43
- removeFromList(list, matcher);
44
- };
45
- }
46
- /**
47
- * Returns all matchers registered for `event`, concatenating global first
48
- * and then session-specific (when `sessionId` is supplied). The caller
49
- * receives a fresh array, so iterating it is safe even if a matcher is
50
- * removed mid-iteration (e.g. via `once: true`).
51
- */
52
- getMatchers(event, sessionId) {
53
- const globalList = readList(this.global, event);
54
- if (sessionId === undefined) {
55
- return snapshot(globalList);
56
- }
57
- const bucket = this.sessions.get(sessionId);
58
- if (bucket === undefined) {
59
- return snapshot(globalList);
60
- }
61
- const sessionList = readList(bucket, event);
62
- if (globalList.length === 0) {
63
- return snapshot(sessionList);
64
- }
65
- if (sessionList.length === 0) {
66
- return snapshot(globalList);
67
- }
68
- return snapshot([...globalList, ...sessionList]);
69
- }
70
- /**
71
- * Removes `matcher` by reference from global storage first, falling back
72
- * to the session bucket when `sessionId` is supplied. Used by
73
- * `executeHooks` to drop `once: true` matchers after they fire.
74
- */
75
- removeMatcher(event, matcher, sessionId) {
76
- if (removeFromList(readList(this.global, event), matcher)) {
77
- return true;
78
- }
79
- if (sessionId === undefined) {
80
- return false;
81
- }
82
- const bucket = this.sessions.get(sessionId);
83
- if (bucket === undefined) {
84
- return false;
85
- }
86
- return removeFromList(readList(bucket, event), matcher);
87
- }
88
- /**
89
- * Drops every session-scoped matcher for `sessionId`. Call this in the
90
- * `finally` block around a Run so a `once: true` hook that never fired
91
- * cannot leak into the next session on the same registry.
92
- */
93
- clearSession(sessionId) {
94
- this.sessions.delete(sessionId);
95
- }
96
- /** True if at least one matcher exists for `event` (global + session). */
97
- hasHookFor(event, sessionId) {
98
- if (readList(this.global, event).length > 0) {
99
- return true;
100
- }
101
- if (sessionId === undefined) {
102
- return false;
103
- }
104
- const bucket = this.sessions.get(sessionId);
105
- if (bucket === undefined) {
106
- return false;
107
- }
108
- return readList(bucket, event).length > 0;
109
- }
110
- ensureSessionBucket(sessionId) {
111
- const existing = this.sessions.get(sessionId);
112
- if (existing !== undefined) {
113
- return existing;
114
- }
115
- const fresh = {};
116
- this.sessions.set(sessionId, fresh);
117
- return fresh;
118
- }
119
- }
120
- function ensureList(bucket, event) {
121
- const existing = bucket[event];
122
- if (existing !== undefined) {
123
- return existing;
124
- }
125
- const fresh = [];
126
- bucket[event] = fresh;
127
- return fresh;
128
- }
129
- function readList(bucket, event) {
130
- return bucket[event] ?? [];
131
- }
132
- function removeFromList(list, matcher) {
133
- const idx = list.indexOf(widen(matcher));
134
- if (idx < 0) {
135
- return false;
136
- }
137
- list.splice(idx, 1);
138
- return true;
139
- }
140
- /**
141
- * Widen a per-event matcher to the storage's uniform slot type. Unsound at
142
- * the type level (function parameters are contravariant) but safe by
143
- * construction: `HookRegistry.register<E>` only ever puts matchers into the
144
- * bucket slot for their own event, and reads go through `snapshot<E>`
145
- * which is only called with the same `E`.
146
- */
147
- function widen(matcher) {
148
- return matcher;
149
- }
150
- /**
151
- * Narrow a storage list back to a per-event matcher list on the way out.
152
- * Sound counterpart to `widen`: the list only contains matchers that were
153
- * registered against `E`, because the public API enforces it on insert.
154
- */
155
- function snapshot(list) {
156
- return list.slice();
157
- }
158
-
159
- export { HookRegistry };
160
- //# sourceMappingURL=HookRegistry.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"HookRegistry.mjs","sources":["../../../src/hooks/HookRegistry.ts"],"sourcesContent":["// src/hooks/HookRegistry.ts\nimport type { HookEvent, HookMatcher } from './types';\n\n/**\n * Internal matcher storage type.\n *\n * Matchers registered via the public `register<E>` API are strictly typed\n * to a single `E`, but the storage needs one uniform slot type per event.\n * We store them as `HookMatcher<HookEvent>` and cast once at the variance\n * boundary — see `ensureList` and `snapshot` below. The invariant (every\n * matcher in `bucket[event]` was registered with that exact event) is\n * enforced by the public API; breaking it requires bypassing the types.\n */\ntype MatcherBucket = Partial<Record<HookEvent, HookMatcher<HookEvent>[]>>;\n\n/**\n * Run-scoped storage for hook matchers with an additional layer for\n * session-scoped matchers that should be cleaned up between sessions.\n *\n * Hosts construct one registry per `Run` (mirroring how `HandlerRegistry` is\n * scoped) and register global matchers + per-session matchers against it.\n * Registration is strictly additive — nothing in this class mutates a\n * matcher's callbacks or flags after insertion.\n *\n * ## Why `Map<sessionId, MatcherBucket>` and not `Record`\n *\n * LibreChat runs thousands of parallel sessions in one Node process, and\n * hook registration happens inside hot paths (tool loading, agent spawning).\n * A `Record<sessionId, ...>` has to be spread on every insertion, which is\n * O(n) per call and O(n²) total for a batch of parallel registrations. A\n * Map mutates in place, keeping insertions O(1). This mirrors the reasoning\n * Claude Code documents at `utils/hooks/sessionHooks.ts:62`.\n */\nexport class HookRegistry {\n private readonly global: MatcherBucket = {};\n private readonly sessions: Map<string, MatcherBucket> = new Map();\n\n /**\n * Register a matcher for the lifetime of this registry (= one Run).\n * Returns an unregister function that removes the matcher by reference.\n */\n register<E extends HookEvent>(event: E, matcher: HookMatcher<E>): () => void {\n const list = ensureList(this.global, event);\n list.push(widen(matcher));\n return () => {\n removeFromList(list, matcher);\n };\n }\n\n /**\n * Register a matcher for a specific session. Cleared automatically when\n * `clearSession(sessionId)` is called, or can be removed directly via the\n * returned unregister function.\n */\n registerSession<E extends HookEvent>(\n sessionId: string,\n event: E,\n matcher: HookMatcher<E>\n ): () => void {\n const bucket = this.ensureSessionBucket(sessionId);\n const list = ensureList(bucket, event);\n list.push(widen(matcher));\n return () => {\n removeFromList(list, matcher);\n };\n }\n\n /**\n * Returns all matchers registered for `event`, concatenating global first\n * and then session-specific (when `sessionId` is supplied). The caller\n * receives a fresh array, so iterating it is safe even if a matcher is\n * removed mid-iteration (e.g. via `once: true`).\n */\n getMatchers<E extends HookEvent>(\n event: E,\n sessionId?: string\n ): HookMatcher<E>[] {\n const globalList = readList(this.global, event);\n if (sessionId === undefined) {\n return snapshot<E>(globalList);\n }\n const bucket = this.sessions.get(sessionId);\n if (bucket === undefined) {\n return snapshot<E>(globalList);\n }\n const sessionList = readList(bucket, event);\n if (globalList.length === 0) {\n return snapshot<E>(sessionList);\n }\n if (sessionList.length === 0) {\n return snapshot<E>(globalList);\n }\n return snapshot<E>([...globalList, ...sessionList]);\n }\n\n /**\n * Removes `matcher` by reference from global storage first, falling back\n * to the session bucket when `sessionId` is supplied. Used by\n * `executeHooks` to drop `once: true` matchers after they fire.\n */\n removeMatcher<E extends HookEvent>(\n event: E,\n matcher: HookMatcher<E>,\n sessionId?: string\n ): boolean {\n if (removeFromList(readList(this.global, event), matcher)) {\n return true;\n }\n if (sessionId === undefined) {\n return false;\n }\n const bucket = this.sessions.get(sessionId);\n if (bucket === undefined) {\n return false;\n }\n return removeFromList(readList(bucket, event), matcher);\n }\n\n /**\n * Drops every session-scoped matcher for `sessionId`. Call this in the\n * `finally` block around a Run so a `once: true` hook that never fired\n * cannot leak into the next session on the same registry.\n */\n clearSession(sessionId: string): void {\n this.sessions.delete(sessionId);\n }\n\n /** True if at least one matcher exists for `event` (global + session). */\n hasHookFor(event: HookEvent, sessionId?: string): boolean {\n if (readList(this.global, event).length > 0) {\n return true;\n }\n if (sessionId === undefined) {\n return false;\n }\n const bucket = this.sessions.get(sessionId);\n if (bucket === undefined) {\n return false;\n }\n return readList(bucket, event).length > 0;\n }\n\n private ensureSessionBucket(sessionId: string): MatcherBucket {\n const existing = this.sessions.get(sessionId);\n if (existing !== undefined) {\n return existing;\n }\n const fresh: MatcherBucket = {};\n this.sessions.set(sessionId, fresh);\n return fresh;\n }\n}\n\nfunction ensureList(\n bucket: MatcherBucket,\n event: HookEvent\n): HookMatcher<HookEvent>[] {\n const existing = bucket[event];\n if (existing !== undefined) {\n return existing;\n }\n const fresh: HookMatcher<HookEvent>[] = [];\n bucket[event] = fresh;\n return fresh;\n}\n\nfunction readList(\n bucket: MatcherBucket,\n event: HookEvent\n): HookMatcher<HookEvent>[] {\n return bucket[event] ?? [];\n}\n\nfunction removeFromList<E extends HookEvent>(\n list: HookMatcher<HookEvent>[],\n matcher: HookMatcher<E>\n): boolean {\n const idx = list.indexOf(widen(matcher));\n if (idx < 0) {\n return false;\n }\n list.splice(idx, 1);\n return true;\n}\n\n/**\n * Widen a per-event matcher to the storage's uniform slot type. Unsound at\n * the type level (function parameters are contravariant) but safe by\n * construction: `HookRegistry.register<E>` only ever puts matchers into the\n * bucket slot for their own event, and reads go through `snapshot<E>`\n * which is only called with the same `E`.\n */\nfunction widen<E extends HookEvent>(\n matcher: HookMatcher<E>\n): HookMatcher<HookEvent> {\n return matcher as unknown as HookMatcher<HookEvent>;\n}\n\n/**\n * Narrow a storage list back to a per-event matcher list on the way out.\n * Sound counterpart to `widen`: the list only contains matchers that were\n * registered against `E`, because the public API enforces it on insert.\n */\nfunction snapshot<E extends HookEvent>(\n list: readonly HookMatcher<HookEvent>[]\n): HookMatcher<E>[] {\n return list.slice() as unknown as HookMatcher<E>[];\n}\n"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;;;;AAiBG;MACU,YAAY,CAAA;IACN,MAAM,GAAkB,EAAE;AAC1B,IAAA,QAAQ,GAA+B,IAAI,GAAG,EAAE;AAEjE;;;AAGG;IACH,QAAQ,CAAsB,KAAQ,EAAE,OAAuB,EAAA;QAC7D,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC;QAC3C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AACzB,QAAA,OAAO,MAAK;AACV,YAAA,cAAc,CAAC,IAAI,EAAE,OAAO,CAAC;AAC/B,QAAA,CAAC;IACH;AAEA;;;;AAIG;AACH,IAAA,eAAe,CACb,SAAiB,EACjB,KAAQ,EACR,OAAuB,EAAA;QAEvB,MAAM,MAAM,GAAG,IAAI,CAAC,mBAAmB,CAAC,SAAS,CAAC;QAClD,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,EAAE,KAAK,CAAC;QACtC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AACzB,QAAA,OAAO,MAAK;AACV,YAAA,cAAc,CAAC,IAAI,EAAE,OAAO,CAAC;AAC/B,QAAA,CAAC;IACH;AAEA;;;;;AAKG;IACH,WAAW,CACT,KAAQ,EACR,SAAkB,EAAA;QAElB,MAAM,UAAU,GAAG,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC;AAC/C,QAAA,IAAI,SAAS,KAAK,SAAS,EAAE;AAC3B,YAAA,OAAO,QA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CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AACxC,IAAA,IAAI,GAAG,GAAG,CAAC,EAAE;AACX,QAAA,OAAO,KAAK;IACd;AACA,IAAA,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC;AACnB,IAAA,OAAO,IAAI;AACb;AAEA;;;;;;AAMG;AACH,SAAS,KAAK,CACZ,OAAuB,EAAA;AAEvB,IAAA,OAAO,OAA4C;AACrD;AAEA;;;;AAIG;AACH,SAAS,QAAQ,CACf,IAAuC,EAAA;AAEvC,IAAA,OAAO,IAAI,CAAC,KAAK,EAAiC;AACpD;;;;"}
@@ -1,273 +0,0 @@
1
- import { matchesQuery } from './matchers.mjs';
2
-
3
- /** Default per-hook timeout when a matcher doesn't set its own. */
4
- const DEFAULT_HOOK_TIMEOUT_MS = 30_000;
5
- function freshResult() {
6
- return {
7
- additionalContexts: [],
8
- errors: [],
9
- };
10
- }
11
- function combineSignals(parent, timeoutMs) {
12
- const timeoutSignal = AbortSignal.timeout(timeoutMs);
13
- if (parent === undefined) {
14
- return timeoutSignal;
15
- }
16
- return AbortSignal.any([parent, timeoutSignal]);
17
- }
18
- function isTimeout(err) {
19
- if (err instanceof Error) {
20
- return err.name === 'TimeoutError' || err.name === 'AbortError';
21
- }
22
- return false;
23
- }
24
- function describeError(err) {
25
- if (err instanceof Error) {
26
- return err.message !== '' ? err.message : err.name;
27
- }
28
- return String(err);
29
- }
30
- function makeAbortPromise(signal) {
31
- let onAbort;
32
- const promise = new Promise((_resolve, reject) => {
33
- if (signal.aborted) {
34
- reject(signal.reason instanceof Error ? signal.reason : new Error('aborted'));
35
- return;
36
- }
37
- onAbort = () => {
38
- reject(signal.reason instanceof Error ? signal.reason : new Error('aborted'));
39
- };
40
- signal.addEventListener('abort', onAbort, { once: true });
41
- });
42
- const cleanup = () => {
43
- if (onAbort !== undefined) {
44
- signal.removeEventListener('abort', onAbort);
45
- onAbort = undefined;
46
- }
47
- };
48
- return { promise, cleanup };
49
- }
50
- async function runHook(hook, input, signal, matcher) {
51
- const hookPromise = Promise.resolve().then(() => hook(input, signal));
52
- const { promise: abortPromise, cleanup } = makeAbortPromise(signal);
53
- try {
54
- const output = await Promise.race([hookPromise, abortPromise]);
55
- return { matcher, output, error: null, timedOut: false };
56
- }
57
- catch (err) {
58
- return {
59
- matcher,
60
- output: null,
61
- error: describeError(err),
62
- timedOut: isTimeout(err),
63
- };
64
- }
65
- finally {
66
- cleanup();
67
- }
68
- }
69
- function reportErrors(outcomes, event, logger) {
70
- for (const outcome of outcomes) {
71
- if (outcome.error === null) {
72
- continue;
73
- }
74
- if (outcome.matcher.internal === true) {
75
- continue;
76
- }
77
- const label = outcome.timedOut ? 'timed out' : 'threw an error';
78
- const message = `Hook for ${event} ${label}: ${outcome.error}`;
79
- if (logger !== undefined) {
80
- logger.warn(message);
81
- continue;
82
- }
83
- // eslint-disable-next-line no-console
84
- console.warn(message);
85
- }
86
- }
87
- function applyToolDecision(agg, decision, reason) {
88
- if (decision === 'deny') {
89
- if (agg.decision === 'deny') {
90
- return;
91
- }
92
- agg.decision = 'deny';
93
- agg.reason = reason;
94
- return;
95
- }
96
- if (decision === 'ask') {
97
- if (agg.decision === 'deny' || agg.decision === 'ask') {
98
- return;
99
- }
100
- agg.decision = 'ask';
101
- agg.reason = reason;
102
- return;
103
- }
104
- if (agg.decision === undefined) {
105
- agg.decision = 'allow';
106
- agg.reason = reason;
107
- }
108
- }
109
- function applyStopDecision(agg, decision, reason) {
110
- if (decision === 'block') {
111
- if (agg.stopDecision === 'block') {
112
- return;
113
- }
114
- agg.stopDecision = 'block';
115
- agg.reason = reason;
116
- return;
117
- }
118
- if (agg.stopDecision === undefined) {
119
- agg.stopDecision = 'continue';
120
- if (agg.reason === undefined) {
121
- agg.reason = reason;
122
- }
123
- }
124
- }
125
- function applyDecision(agg, output) {
126
- if (!('decision' in output) || output.decision === undefined) {
127
- return;
128
- }
129
- const decision = output.decision;
130
- const reason = 'reason' in output && typeof output.reason === 'string'
131
- ? output.reason
132
- : undefined;
133
- if (decision === 'deny' || decision === 'ask' || decision === 'allow') {
134
- applyToolDecision(agg, decision, reason);
135
- return;
136
- }
137
- applyStopDecision(agg, decision, reason);
138
- }
139
- function applyContext(agg, output) {
140
- if (typeof output.additionalContext === 'string' &&
141
- output.additionalContext.length > 0) {
142
- agg.additionalContexts.push(output.additionalContext);
143
- }
144
- }
145
- function applyStopFlag(agg, output) {
146
- if (output.preventContinuation !== true) {
147
- return;
148
- }
149
- agg.preventContinuation = true;
150
- if (typeof output.stopReason === 'string' && agg.stopReason === undefined) {
151
- agg.stopReason = output.stopReason;
152
- }
153
- }
154
- function applyUpdatedInput(agg, output) {
155
- if (!('updatedInput' in output) || output.updatedInput === undefined) {
156
- return;
157
- }
158
- agg.updatedInput = output.updatedInput;
159
- }
160
- function applyUpdatedOutput(agg, output) {
161
- if (!('updatedOutput' in output) || output.updatedOutput === undefined) {
162
- return;
163
- }
164
- agg.updatedOutput = output.updatedOutput;
165
- }
166
- function fold(outcomes) {
167
- const agg = freshResult();
168
- for (const outcome of outcomes) {
169
- if (outcome.error !== null) {
170
- if (outcome.matcher.internal !== true) {
171
- agg.errors.push(outcome.error);
172
- }
173
- continue;
174
- }
175
- const output = outcome.output;
176
- if (output === null) {
177
- continue;
178
- }
179
- applyContext(agg, output);
180
- applyStopFlag(agg, output);
181
- applyDecision(agg, output);
182
- applyUpdatedInput(agg, output);
183
- applyUpdatedOutput(agg, output);
184
- }
185
- return agg;
186
- }
187
- /**
188
- * Fires every matcher registered against `input.hook_event_name`, folding
189
- * their results per `deny > ask > allow` precedence and accumulating
190
- * context/errors.
191
- *
192
- * ## Parallelism and determinism
193
- *
194
- * All matching hooks fire simultaneously and are awaited via `Promise.all`,
195
- * which preserves input-array order in its returned results. The fold
196
- * therefore iterates outcomes in **registration order** — outer loop over
197
- * matchers as they sit in the registry (global first, then session), inner
198
- * loop over each matcher's `hooks` array. Last-writer-wins fields
199
- * (`updatedInput`, `updatedOutput`) are deterministic in that order, even
200
- * though hooks may complete in arbitrary wall-clock order.
201
- *
202
- * Consumers that need a single authoritative rewrite should still scope
203
- * `updatedInput`/`updatedOutput` to one hook per matcher to avoid subtle
204
- * precedence bugs when matchers are added in a different order than
205
- * expected.
206
- *
207
- * ## Timeouts and cancellation
208
- *
209
- * Each matcher receives **one shared `AbortSignal`** derived from the
210
- * caller's parent signal combined with `matcher.timeout` (falling back to
211
- * `opts.timeoutMs`, default {@link DEFAULT_HOOK_TIMEOUT_MS}). Sharing the
212
- * signal across hooks in a matcher collapses N timer allocations into
213
- * one, which matters on the PreToolUse hot path where a matcher with
214
- * several hooks fires on every tool call. Each hook call is raced
215
- * against the shared signal, so even a hook that ignores the signal is
216
- * force-unblocked when the timeout fires. Timeout/abort errors are
217
- * swallowed into the aggregated result's `errors` array (non-fatal by
218
- * default).
219
- *
220
- * ## Internal matchers
221
- *
222
- * A matcher with `internal: true` is excluded from both the `errors` array
223
- * and the logger output. Use it for infrastructure hooks whose failures
224
- * should not pollute user-visible diagnostics.
225
- *
226
- * ## Once semantics — atomic at-most-once
227
- *
228
- * A matcher with `once: true` is removed from the registry **before any
229
- * hook runs**, inside the synchronous prefix of `executeHooks` (between
230
- * `getMatchers` and the first `await`). Because Node's event loop serialises
231
- * sync work, two concurrent `executeHooks` calls can never both observe
232
- * and dispatch the same `once` matcher — whichever call runs its sync
233
- * prefix first consumes it, and the loser sees an empty bucket.
234
- *
235
- * Trade-off: if every hook in a `once` matcher throws, the matcher is
236
- * still gone. "Once" here means "at most one dispatch, ever", not "at
237
- * most one successful execution with retry on failure". Hosts that need
238
- * retry semantics should register a normal matcher and self-unregister
239
- * via the `unregister` callback returned from `registry.register`.
240
- */
241
- async function executeHooks(opts) {
242
- const { registry, input, sessionId, matchQuery, signal, timeoutMs = DEFAULT_HOOK_TIMEOUT_MS, logger, } = opts;
243
- const event = input.hook_event_name;
244
- const matchers = registry.getMatchers(event, sessionId);
245
- if (matchers.length === 0) {
246
- return freshResult();
247
- }
248
- // --- SYNC CRITICAL SECTION: once-matcher removal must complete before any await ---
249
- const tasks = [];
250
- for (const matcher of matchers) {
251
- if (!matchesQuery(matcher.pattern, matchQuery)) {
252
- continue;
253
- }
254
- if (matcher.once === true) {
255
- registry.removeMatcher(event, matcher, sessionId);
256
- }
257
- const perHookTimeout = matcher.timeout ?? timeoutMs;
258
- const matcherSignal = combineSignals(signal, perHookTimeout);
259
- for (const hook of matcher.hooks) {
260
- tasks.push(runHook(hook, input, matcherSignal, matcher));
261
- }
262
- }
263
- // --- END SYNC CRITICAL SECTION ---
264
- if (tasks.length === 0) {
265
- return freshResult();
266
- }
267
- const outcomes = await Promise.all(tasks);
268
- reportErrors(outcomes, event, logger);
269
- return fold(outcomes);
270
- }
271
-
272
- export { DEFAULT_HOOK_TIMEOUT_MS, executeHooks };
273
- //# sourceMappingURL=executeHooks.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"executeHooks.mjs","sources":["../../../src/hooks/executeHooks.ts"],"sourcesContent":["// src/hooks/executeHooks.ts\nimport type { Logger } from 'winston';\nimport type { HookRegistry } from './HookRegistry';\nimport type {\n HookInput,\n HookEvent,\n HookOutput,\n HookMatcher,\n ToolDecision,\n StopDecision,\n HookCallback,\n AggregatedHookResult,\n} from './types';\nimport { matchesQuery } from './matchers';\n\n/** Default per-hook timeout when a matcher doesn't set its own. */\nexport const DEFAULT_HOOK_TIMEOUT_MS = 30_000;\n\n/**\n * Options for a single `executeHooks` call. The `input` drives everything —\n * the event name is read from `input.hook_event_name`, matchers are looked\n * up against that event, and each hook receives `input` directly.\n */\nexport interface ExecuteHooksOptions {\n registry: HookRegistry;\n input: HookInput;\n /** Scope lookup to this session (in addition to global matchers). */\n sessionId?: string;\n /** Query string matched against each matcher's pattern (tool name, etc.). */\n matchQuery?: string;\n /** Parent AbortSignal — combined with per-hook timeout into the hook signal. */\n signal?: AbortSignal;\n /** Default per-hook timeout; overridden by `matcher.timeout` when present. */\n timeoutMs?: number;\n /** Optional winston logger for non-internal hook errors. */\n logger?: Logger;\n}\n\ntype WideMatcher = HookMatcher<HookEvent>;\ntype WideCallback = HookCallback<HookEvent>;\n\ninterface HookOutcome {\n matcher: WideMatcher;\n output: HookOutput | null;\n error: string | null;\n timedOut: boolean;\n}\n\nfunction freshResult(): AggregatedHookResult {\n return {\n additionalContexts: [],\n errors: [],\n };\n}\n\nfunction combineSignals(\n parent: AbortSignal | undefined,\n timeoutMs: number\n): AbortSignal {\n const timeoutSignal = AbortSignal.timeout(timeoutMs);\n if (parent === undefined) {\n return timeoutSignal;\n }\n return AbortSignal.any([parent, timeoutSignal]);\n}\n\nfunction isTimeout(err: unknown): boolean {\n if (err instanceof Error) {\n return err.name === 'TimeoutError' || err.name === 'AbortError';\n }\n return false;\n}\n\nfunction describeError(err: unknown): string {\n if (err instanceof Error) {\n return err.message !== '' ? err.message : err.name;\n }\n return String(err);\n}\n\nfunction makeAbortPromise(signal: AbortSignal): {\n promise: Promise<never>;\n cleanup: () => void;\n} {\n let onAbort: (() => void) | undefined;\n const promise = new Promise<never>((_resolve, reject) => {\n if (signal.aborted) {\n reject(\n signal.reason instanceof Error ? signal.reason : new Error('aborted')\n );\n return;\n }\n onAbort = (): void => {\n reject(\n signal.reason instanceof Error ? signal.reason : new Error('aborted')\n );\n };\n signal.addEventListener('abort', onAbort, { once: true });\n });\n const cleanup = (): void => {\n if (onAbort !== undefined) {\n signal.removeEventListener('abort', onAbort);\n onAbort = undefined;\n }\n };\n return { promise, cleanup };\n}\n\nasync function runHook(\n hook: WideCallback,\n input: HookInput,\n signal: AbortSignal,\n matcher: WideMatcher\n): Promise<HookOutcome> {\n const hookPromise = Promise.resolve().then(() => hook(input, signal));\n const { promise: abortPromise, cleanup } = makeAbortPromise(signal);\n try {\n const output = await Promise.race([hookPromise, abortPromise]);\n return { matcher, output, error: null, timedOut: false };\n } catch (err) {\n return {\n matcher,\n output: null,\n error: describeError(err),\n timedOut: isTimeout(err),\n };\n } finally {\n cleanup();\n }\n}\n\nfunction reportErrors(\n outcomes: readonly HookOutcome[],\n event: HookEvent,\n logger: Logger | undefined\n): void {\n for (const outcome of outcomes) {\n if (outcome.error === null) {\n continue;\n }\n if (outcome.matcher.internal === true) {\n continue;\n }\n const label = outcome.timedOut ? 'timed out' : 'threw an error';\n const message = `Hook for ${event} ${label}: ${outcome.error}`;\n if (logger !== undefined) {\n logger.warn(message);\n continue;\n }\n // eslint-disable-next-line no-console\n console.warn(message);\n }\n}\n\nfunction applyToolDecision(\n agg: AggregatedHookResult,\n decision: ToolDecision,\n reason: string | undefined\n): void {\n if (decision === 'deny') {\n if (agg.decision === 'deny') {\n return;\n }\n agg.decision = 'deny';\n agg.reason = reason;\n return;\n }\n if (decision === 'ask') {\n if (agg.decision === 'deny' || agg.decision === 'ask') {\n return;\n }\n agg.decision = 'ask';\n agg.reason = reason;\n return;\n }\n if (agg.decision === undefined) {\n agg.decision = 'allow';\n agg.reason = reason;\n }\n}\n\nfunction applyStopDecision(\n agg: AggregatedHookResult,\n decision: StopDecision,\n reason: string | undefined\n): void {\n if (decision === 'block') {\n if (agg.stopDecision === 'block') {\n return;\n }\n agg.stopDecision = 'block';\n agg.reason = reason;\n return;\n }\n if (agg.stopDecision === undefined) {\n agg.stopDecision = 'continue';\n if (agg.reason === undefined) {\n agg.reason = reason;\n }\n }\n}\n\nfunction applyDecision(agg: AggregatedHookResult, output: HookOutput): void {\n if (!('decision' in output) || output.decision === undefined) {\n return;\n }\n const decision = output.decision;\n const reason =\n 'reason' in output && typeof output.reason === 'string'\n ? output.reason\n : undefined;\n if (decision === 'deny' || decision === 'ask' || decision === 'allow') {\n applyToolDecision(agg, decision, reason);\n return;\n }\n applyStopDecision(agg, decision, reason);\n}\n\nfunction applyContext(agg: AggregatedHookResult, output: HookOutput): void {\n if (\n typeof output.additionalContext === 'string' &&\n output.additionalContext.length > 0\n ) {\n agg.additionalContexts.push(output.additionalContext);\n }\n}\n\nfunction applyStopFlag(agg: AggregatedHookResult, output: HookOutput): void {\n if (output.preventContinuation !== true) {\n return;\n }\n agg.preventContinuation = true;\n if (typeof output.stopReason === 'string' && agg.stopReason === undefined) {\n agg.stopReason = output.stopReason;\n }\n}\n\nfunction applyUpdatedInput(\n agg: AggregatedHookResult,\n output: HookOutput\n): void {\n if (!('updatedInput' in output) || output.updatedInput === undefined) {\n return;\n }\n agg.updatedInput = output.updatedInput;\n}\n\nfunction applyUpdatedOutput(\n agg: AggregatedHookResult,\n output: HookOutput\n): void {\n if (!('updatedOutput' in output) || output.updatedOutput === undefined) {\n return;\n }\n agg.updatedOutput = output.updatedOutput;\n}\n\nfunction fold(outcomes: readonly HookOutcome[]): AggregatedHookResult {\n const agg = freshResult();\n for (const outcome of outcomes) {\n if (outcome.error !== null) {\n if (outcome.matcher.internal !== true) {\n agg.errors.push(outcome.error);\n }\n continue;\n }\n const output = outcome.output;\n if (output === null) {\n continue;\n }\n applyContext(agg, output);\n applyStopFlag(agg, output);\n applyDecision(agg, output);\n applyUpdatedInput(agg, output);\n applyUpdatedOutput(agg, output);\n }\n return agg;\n}\n\n/**\n * Fires every matcher registered against `input.hook_event_name`, folding\n * their results per `deny > ask > allow` precedence and accumulating\n * context/errors.\n *\n * ## Parallelism and determinism\n *\n * All matching hooks fire simultaneously and are awaited via `Promise.all`,\n * which preserves input-array order in its returned results. The fold\n * therefore iterates outcomes in **registration order** — outer loop over\n * matchers as they sit in the registry (global first, then session), inner\n * loop over each matcher's `hooks` array. Last-writer-wins fields\n * (`updatedInput`, `updatedOutput`) are deterministic in that order, even\n * though hooks may complete in arbitrary wall-clock order.\n *\n * Consumers that need a single authoritative rewrite should still scope\n * `updatedInput`/`updatedOutput` to one hook per matcher to avoid subtle\n * precedence bugs when matchers are added in a different order than\n * expected.\n *\n * ## Timeouts and cancellation\n *\n * Each matcher receives **one shared `AbortSignal`** derived from the\n * caller's parent signal combined with `matcher.timeout` (falling back to\n * `opts.timeoutMs`, default {@link DEFAULT_HOOK_TIMEOUT_MS}). Sharing the\n * signal across hooks in a matcher collapses N timer allocations into\n * one, which matters on the PreToolUse hot path where a matcher with\n * several hooks fires on every tool call. Each hook call is raced\n * against the shared signal, so even a hook that ignores the signal is\n * force-unblocked when the timeout fires. Timeout/abort errors are\n * swallowed into the aggregated result's `errors` array (non-fatal by\n * default).\n *\n * ## Internal matchers\n *\n * A matcher with `internal: true` is excluded from both the `errors` array\n * and the logger output. Use it for infrastructure hooks whose failures\n * should not pollute user-visible diagnostics.\n *\n * ## Once semantics — atomic at-most-once\n *\n * A matcher with `once: true` is removed from the registry **before any\n * hook runs**, inside the synchronous prefix of `executeHooks` (between\n * `getMatchers` and the first `await`). Because Node's event loop serialises\n * sync work, two concurrent `executeHooks` calls can never both observe\n * and dispatch the same `once` matcher — whichever call runs its sync\n * prefix first consumes it, and the loser sees an empty bucket.\n *\n * Trade-off: if every hook in a `once` matcher throws, the matcher is\n * still gone. \"Once\" here means \"at most one dispatch, ever\", not \"at\n * most one successful execution with retry on failure\". Hosts that need\n * retry semantics should register a normal matcher and self-unregister\n * via the `unregister` callback returned from `registry.register`.\n */\nexport async function executeHooks(\n opts: ExecuteHooksOptions\n): Promise<AggregatedHookResult> {\n const {\n registry,\n input,\n sessionId,\n matchQuery,\n signal,\n timeoutMs = DEFAULT_HOOK_TIMEOUT_MS,\n logger,\n } = opts;\n const event = input.hook_event_name;\n const matchers = registry.getMatchers(event, sessionId);\n if (matchers.length === 0) {\n return freshResult();\n }\n\n // --- SYNC CRITICAL SECTION: once-matcher removal must complete before any await ---\n const tasks: Promise<HookOutcome>[] = [];\n for (const matcher of matchers) {\n if (!matchesQuery(matcher.pattern, matchQuery)) {\n continue;\n }\n if (matcher.once === true) {\n registry.removeMatcher(event, matcher, sessionId);\n }\n const perHookTimeout = matcher.timeout ?? timeoutMs;\n const matcherSignal = combineSignals(signal, perHookTimeout);\n for (const hook of matcher.hooks) {\n tasks.push(runHook(hook, input, matcherSignal, matcher));\n }\n }\n // --- END SYNC CRITICAL SECTION ---\n if (tasks.length === 0) {\n return freshResult();\n }\n\n const outcomes = await Promise.all(tasks);\n reportErrors(outcomes, event, logger);\n return fold(outcomes);\n}\n"],"names":[],"mappings":";;AAeA;AACO,MAAM,uBAAuB,GAAG;AAgCvC,SAAS,WAAW,GAAA;IAClB,OAAO;AACL,QAAA,kBAAkB,EAAE,EAAE;AACtB,QAAA,MAAM,EAAE,EAAE;KACX;AACH;AAEA,SAAS,cAAc,CACrB,MAA+B,EAC/B,SAAiB,EAAA;IAEjB,MAAM,aAAa,GAAG,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC;AACpD,IAAA,IAAI,MAAM,KAAK,SAAS,EAAE;AACxB,QAAA,OAAO,aAAa;IACtB;IACA,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;AACjD;AAEA,SAAS,SAAS,CAAC,GAAY,EAAA;AAC7B,IAAA,IAAI,GAAG,YAAY,KAAK,EAAE;QACxB,OAAO,GAAG,CAAC,IAAI,KAAK,cAAc,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY;IACjE;AACA,IAAA,OAAO,KAAK;AACd;AAEA,SAAS,aAAa,CAAC,GAAY,EAAA;AACjC,IAAA,IAAI,GAAG,YAAY,KAAK,EAAE;AACxB,QAAA,OAAO,GAAG,CAAC,OAAO,KAAK,EAAE,GAAG,GAAG,CAAC,OAAO,GAAG,GAAG,CAAC,IAAI;IACpD;AACA,IAAA,OAAO,MAAM,CAAC,GAAG,CAAC;AACpB;AAEA,SAAS,gBAAgB,CAAC,MAAmB,EAAA;AAI3C,IAAA,IAAI,OAAiC;IACrC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAQ,CAAC,QAAQ,EAAE,MAAM,KAAI;AACtD,QAAA,IAAI,MAAM,CAAC,OAAO,EAAE;YAClB,MAAM,CACJ,MAAM,CAAC,MAAM,YAAY,KAAK,GAAG,MAAM,CAAC,MAAM,GAAG,IAAI,KAAK,CAAC,SAAS,CAAC,CACtE;YACD;QACF;QACA,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