sentinelayer-cli 0.6.2 → 0.8.0

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 (159) hide show
  1. package/README.md +996 -996
  2. package/bin/create-sentinelayer.js +5 -5
  3. package/bin/sentinelayer-cli.js +4 -4
  4. package/bin/sl.js +5 -5
  5. package/package.json +64 -63
  6. package/src/agents/jules/config/definition.js +160 -160
  7. package/src/agents/jules/config/system-prompt.js +182 -182
  8. package/src/agents/jules/error-intake.js +51 -51
  9. package/src/agents/jules/fix-cycle.js +17 -17
  10. package/src/agents/jules/loop.js +457 -450
  11. package/src/agents/jules/pulse.js +10 -10
  12. package/src/agents/jules/stream.js +187 -186
  13. package/src/agents/jules/swarm/file-scanner.js +74 -74
  14. package/src/agents/jules/swarm/index.js +11 -11
  15. package/src/agents/jules/swarm/orchestrator.js +362 -362
  16. package/src/agents/jules/swarm/pattern-hunter.js +123 -123
  17. package/src/agents/jules/swarm/sub-agent.js +311 -309
  18. package/src/agents/jules/tools/aidenid-email.js +189 -189
  19. package/src/agents/jules/tools/auth-audit.js +1699 -1691
  20. package/src/agents/jules/tools/dispatch.js +340 -335
  21. package/src/agents/jules/tools/file-edit.js +2 -2
  22. package/src/agents/jules/tools/file-read.js +2 -2
  23. package/src/agents/jules/tools/frontend-analyze.js +570 -570
  24. package/src/agents/jules/tools/glob.js +2 -2
  25. package/src/agents/jules/tools/grep.js +2 -2
  26. package/src/agents/jules/tools/index.js +29 -29
  27. package/src/agents/jules/tools/path-guards.js +2 -2
  28. package/src/agents/jules/tools/runtime-audit.js +507 -507
  29. package/src/agents/jules/tools/shell.js +2 -2
  30. package/src/agents/jules/tools/url-policy.js +100 -100
  31. package/src/agents/persona-visuals.js +64 -61
  32. package/src/agents/shared-tools/dispatch-core.js +320 -315
  33. package/src/agents/shared-tools/file-edit.js +180 -180
  34. package/src/agents/shared-tools/file-read.js +100 -100
  35. package/src/agents/shared-tools/glob.js +168 -168
  36. package/src/agents/shared-tools/grep.js +228 -228
  37. package/src/agents/shared-tools/index.js +46 -46
  38. package/src/agents/shared-tools/path-guards.js +161 -161
  39. package/src/agents/shared-tools/shell.js +383 -383
  40. package/src/ai/aidenid.js +1021 -1009
  41. package/src/ai/client.js +553 -553
  42. package/src/ai/domain-target-store.js +268 -268
  43. package/src/ai/identity-store.js +270 -270
  44. package/src/ai/proxy.js +137 -137
  45. package/src/ai/site-store.js +145 -145
  46. package/src/audit/agents/architecture.js +180 -180
  47. package/src/audit/agents/compliance.js +179 -179
  48. package/src/audit/agents/documentation.js +165 -165
  49. package/src/audit/agents/performance.js +145 -145
  50. package/src/audit/agents/security.js +215 -215
  51. package/src/audit/agents/testing.js +172 -172
  52. package/src/audit/orchestrator.js +557 -557
  53. package/src/audit/package.js +204 -204
  54. package/src/audit/registry.js +284 -284
  55. package/src/audit/replay.js +103 -103
  56. package/src/auth/gate.js +400 -371
  57. package/src/auth/http.js +681 -611
  58. package/src/auth/service.js +1106 -1106
  59. package/src/auth/session-store.js +813 -813
  60. package/src/cli.js +257 -252
  61. package/src/commands/ai/identity-lifecycle.js +1338 -1338
  62. package/src/commands/ai/provision-governance.js +1272 -1272
  63. package/src/commands/ai/shared.js +147 -147
  64. package/src/commands/ai.js +11 -11
  65. package/src/commands/apply.js +12 -12
  66. package/src/commands/audit.js +1171 -1166
  67. package/src/commands/auth.js +419 -419
  68. package/src/commands/chat.js +191 -191
  69. package/src/commands/config.js +184 -184
  70. package/src/commands/cost.js +311 -311
  71. package/src/commands/daemon/core.js +850 -850
  72. package/src/commands/daemon/extended.js +1048 -1048
  73. package/src/commands/daemon/shared.js +213 -213
  74. package/src/commands/daemon.js +11 -11
  75. package/src/commands/guide.js +174 -174
  76. package/src/commands/ingest.js +58 -58
  77. package/src/commands/init.js +55 -55
  78. package/src/commands/legacy-args.js +10 -10
  79. package/src/commands/mcp.js +461 -461
  80. package/src/commands/omargate.js +29 -29
  81. package/src/commands/persona.js +20 -20
  82. package/src/commands/plugin.js +260 -260
  83. package/src/commands/policy.js +132 -132
  84. package/src/commands/prompt.js +238 -238
  85. package/src/commands/review.js +704 -704
  86. package/src/commands/scan.js +872 -872
  87. package/src/commands/session.js +590 -0
  88. package/src/commands/spec.js +778 -716
  89. package/src/commands/swarm.js +651 -651
  90. package/src/commands/telemetry.js +202 -202
  91. package/src/commands/watch.js +511 -511
  92. package/src/config/agent-dictionary.js +182 -182
  93. package/src/config/io.js +56 -56
  94. package/src/config/paths.js +18 -18
  95. package/src/config/schema.js +55 -55
  96. package/src/config/service.js +184 -184
  97. package/src/cost/budget.js +235 -235
  98. package/src/cost/history.js +188 -188
  99. package/src/cost/tracker.js +171 -171
  100. package/src/daemon/artifact-lineage.js +534 -534
  101. package/src/daemon/assignment-ledger.js +966 -770
  102. package/src/daemon/ast-parser-layer.js +258 -258
  103. package/src/daemon/budget-governor.js +633 -633
  104. package/src/daemon/callgraph-overlay.js +646 -646
  105. package/src/daemon/error-worker.js +1209 -626
  106. package/src/daemon/fix-cycle.js +384 -377
  107. package/src/daemon/hybrid-mapper.js +929 -929
  108. package/src/daemon/ingest-refresh.js +10 -9
  109. package/src/daemon/jira-lifecycle.js +767 -632
  110. package/src/daemon/operator-control.js +657 -657
  111. package/src/daemon/pulse.js +327 -327
  112. package/src/daemon/reliability-lane.js +471 -471
  113. package/src/daemon/scope-engine.js +1068 -0
  114. package/src/daemon/watchdog.js +971 -971
  115. package/src/events/schema.js +190 -0
  116. package/src/guide/generator.js +316 -316
  117. package/src/ingest/engine.js +918 -918
  118. package/src/interactive/index.js +97 -97
  119. package/src/legacy-cli.js +3161 -2994
  120. package/src/mcp/registry.js +695 -695
  121. package/src/memory/blackboard.js +301 -301
  122. package/src/memory/retrieval.js +581 -581
  123. package/src/plugin/manifest.js +553 -553
  124. package/src/policy/packs.js +144 -144
  125. package/src/prompt/generator.js +136 -118
  126. package/src/review/ai-review.js +679 -679
  127. package/src/review/local-review.js +1351 -1305
  128. package/src/review/omargate-interactive.js +68 -68
  129. package/src/review/omargate-orchestrator.js +404 -300
  130. package/src/review/persona-prompts.js +296 -296
  131. package/src/review/replay.js +235 -235
  132. package/src/review/report.js +664 -664
  133. package/src/review/scan-modes.js +48 -42
  134. package/src/review/spec-binding.js +487 -487
  135. package/src/scaffold/generator.js +67 -67
  136. package/src/scaffold/templates.js +150 -150
  137. package/src/scan/generator.js +418 -418
  138. package/src/scan/gh-secrets.js +107 -107
  139. package/src/session/agent-registry.js +352 -0
  140. package/src/session/daemon.js +801 -0
  141. package/src/session/paths.js +33 -0
  142. package/src/session/runtime-bridge.js +739 -0
  143. package/src/session/store.js +388 -0
  144. package/src/session/stream.js +325 -0
  145. package/src/spec/generator.js +619 -519
  146. package/src/spec/regenerate.js +237 -237
  147. package/src/spec/templates.js +91 -91
  148. package/src/swarm/dashboard.js +247 -247
  149. package/src/swarm/factory.js +363 -363
  150. package/src/swarm/pentest.js +934 -934
  151. package/src/swarm/registry.js +419 -419
  152. package/src/swarm/report.js +158 -158
  153. package/src/swarm/runtime.js +576 -576
  154. package/src/swarm/scenario-dsl.js +272 -272
  155. package/src/telemetry/ledger.js +302 -302
  156. package/src/telemetry/session-tracker.js +234 -234
  157. package/src/telemetry/sync.js +203 -203
  158. package/src/ui/command-hints.js +13 -13
  159. package/src/ui/markdown.js +220 -220
@@ -1,315 +1,320 @@
1
- import { randomUUID } from "node:crypto";
2
- import { evaluateBudget } from "../../cost/budget.js";
3
- import {
4
- normalizeRunEvent,
5
- appendRunEvent,
6
- } from "../../telemetry/ledger.js";
7
-
8
- /**
9
- * Shared tool dispatch infrastructure.
10
- *
11
- * Each persona builds its own TOOL_MAP (shared tools + domain tools)
12
- * and creates a dispatcher via createToolDispatcher(). This avoids
13
- * duplicating budget enforcement, telemetry, and result persistence.
14
- */
15
-
16
- const RESULT_PERSIST_THRESHOLD = 5000;
17
-
18
- /**
19
- * Create a tool dispatcher bound to a specific TOOL_MAP.
20
- *
21
- * @param {Record<string, Function>} toolMap - { ToolName: handler }
22
- * @param {Set<string>} [readOnlyTools] - tool names safe for concurrent use
23
- * @returns {{ dispatchTool, registerTool, isReadOnlyTool, listTools }}
24
- */
25
- export function createToolDispatcher(toolMap, readOnlyTools) {
26
- const TOOL_MAP = { ...toolMap };
27
- const READ_ONLY_TOOLS = new Set(readOnlyTools || []);
28
-
29
- async function dispatchTool(toolName, input, ctx) {
30
- const handler = TOOL_MAP[toolName];
31
- if (!handler) {
32
- throw new ToolDispatchError(`Unknown tool: ${toolName}`);
33
- }
34
-
35
- // 1. Pre-flight budget check
36
- const budgetCheck = evaluateBudget({
37
- maxCostUsd: ctx.budget.maxCostUsd,
38
- maxOutputTokens: ctx.budget.maxOutputTokens,
39
- maxRuntimeMs: ctx.budget.maxRuntimeMs,
40
- maxToolCalls: ctx.budget.maxToolCalls,
41
- warningThresholdPercent: ctx.budget.warningThresholdPercent ?? 70,
42
- maxNoProgress: 0,
43
- sessionSummary: {
44
- costUsd: ctx.usage.costUsd,
45
- outputTokens: ctx.usage.outputTokens,
46
- durationMs: Date.now() - ctx.startedAt,
47
- toolCalls: ctx.usage.toolCalls + 1,
48
- noProgressStreak: 0,
49
- },
50
- });
51
-
52
- if (budgetCheck.blocking) {
53
- const stopEvent = {
54
- eventType: "run_stop",
55
- sessionId: ctx.sessionId,
56
- runId: ctx.runId,
57
- stop: {
58
- stopClass: budgetCheck.reasons[0]?.code || "MAX_TOOL_CALLS_EXCEEDED",
59
- blocking: true,
60
- reasonCodes: budgetCheck.reasons.map((r) => r.code),
61
- },
62
- usage: snapshotUsage(ctx),
63
- metadata: { tool: toolName, phase: "pre_flight" },
64
- };
65
- await safeAppendEvent(ctx, stopEvent);
66
-
67
- if (ctx.onEvent) {
68
- ctx.onEvent({
69
- stream: "sl_event",
70
- event: "budget_stop",
71
- agent: ctx.agentIdentity,
72
- payload: {
73
- stopClass: stopEvent.stop.stopClass,
74
- reasons: budgetCheck.reasons,
75
- },
76
- usage: snapshotUsage(ctx),
77
- });
78
- }
79
-
80
- throw new BudgetExhaustedError(budgetCheck);
81
- }
82
-
83
- // Emit budget warnings
84
- if (budgetCheck.warnings.length > 0 && ctx.onEvent) {
85
- ctx.onEvent({
86
- stream: "sl_event",
87
- event: "budget_warning",
88
- agent: ctx.agentIdentity,
89
- payload: { warnings: budgetCheck.warnings },
90
- usage: snapshotUsage(ctx),
91
- });
92
- }
93
-
94
- // 2. Emit tool_call event
95
- const eventId = randomUUID();
96
- const callEvent = {
97
- eventType: "tool_call",
98
- sessionId: ctx.sessionId,
99
- runId: ctx.runId,
100
- metadata: {
101
- eventId,
102
- tool: toolName,
103
- input: sanitizeInput(toolName, input),
104
- agentId: ctx.agentIdentity?.id,
105
- persona: ctx.agentIdentity?.persona,
106
- },
107
- };
108
- await safeAppendEvent(ctx, callEvent);
109
-
110
- if (ctx.onEvent) {
111
- ctx.onEvent({
112
- stream: "sl_event",
113
- event: "tool_call",
114
- agent: ctx.agentIdentity,
115
- payload: { tool: toolName, input: sanitizeInput(toolName, input) },
116
- usage: snapshotUsage(ctx),
117
- });
118
- }
119
-
120
- // 3. Execute
121
- const startMs = Date.now();
122
- let result;
123
- let error;
124
- try {
125
- result = handler(input);
126
- } catch (err) {
127
- error = err;
128
- }
129
- const durationMs = Date.now() - startMs;
130
-
131
- // 4. Update accumulated usage
132
- ctx.usage.toolCalls++;
133
- ctx.usage.runtimeMs = Date.now() - ctx.startedAt;
134
- ctx.lastToolCallAt = Date.now();
135
- ctx.lastToolName = toolName;
136
-
137
- // Track confirmed file reads for coverage accounting
138
- if (!error && toolName === "FileRead") {
139
- const readPath = input?.file_path || input?.filePath || input?.path || "";
140
- if (readPath && ctx.usage.filesRead) ctx.usage.filesRead.add(readPath);
141
- }
142
-
143
- // 5. Emit tool_result event
144
- const resultEvent = {
145
- eventType: "tool_call",
146
- sessionId: ctx.sessionId,
147
- runId: ctx.runId,
148
- usage: {
149
- durationMs,
150
- toolCalls: 1,
151
- },
152
- metadata: {
153
- eventId,
154
- phase: "result",
155
- tool: toolName,
156
- success: !error,
157
- error: error?.message,
158
- agentId: ctx.agentIdentity?.id,
159
- },
160
- };
161
- await safeAppendEvent(ctx, resultEvent);
162
-
163
- if (ctx.onEvent) {
164
- ctx.onEvent({
165
- stream: "sl_event",
166
- event: "tool_result",
167
- agent: ctx.agentIdentity,
168
- payload: {
169
- tool: toolName,
170
- durationMs,
171
- success: !error,
172
- error: error?.message,
173
- },
174
- usage: snapshotUsage(ctx),
175
- });
176
- }
177
-
178
- if (error) throw error;
179
-
180
- // 6. Large result persistence
181
- const serialized = JSON.stringify(result);
182
- if (serialized.length > RESULT_PERSIST_THRESHOLD && ctx.artifactDir) {
183
- const refPath = `${ctx.artifactDir}/tool-results/${eventId}.json`;
184
- const fsp = await import("node:fs/promises");
185
- await fsp.mkdir(`${ctx.artifactDir}/tool-results`, { recursive: true });
186
- await fsp.writeFile(refPath, serialized, "utf-8");
187
- return {
188
- _persisted: true,
189
- _refPath: refPath,
190
- _summary: summarizeResult(toolName, result),
191
- };
192
- }
193
-
194
- return result;
195
- }
196
-
197
- function registerTool(name, handler, { readOnly = false } = {}) {
198
- TOOL_MAP[name] = handler;
199
- if (readOnly) READ_ONLY_TOOLS.add(name);
200
- }
201
-
202
- function isReadOnlyTool(toolName) {
203
- return READ_ONLY_TOOLS.has(toolName);
204
- }
205
-
206
- function listTools() {
207
- return Object.keys(TOOL_MAP);
208
- }
209
-
210
- return { dispatchTool, registerTool, isReadOnlyTool, listTools };
211
- }
212
-
213
- /**
214
- * Create an agent context for tool dispatch.
215
- */
216
- export function createAgentContext({
217
- agentIdentity,
218
- budget,
219
- sessionId,
220
- runId,
221
- artifactDir,
222
- onEvent,
223
- }) {
224
- return {
225
- agentIdentity,
226
- budget: {
227
- maxCostUsd: budget?.maxCostUsd ?? 5.0,
228
- maxOutputTokens: budget?.maxOutputTokens ?? 12000,
229
- maxRuntimeMs: budget?.maxRuntimeMs ?? 300000,
230
- maxToolCalls: budget?.maxToolCalls ?? 150,
231
- warningThresholdPercent: budget?.warningThresholdPercent ?? 70,
232
- },
233
- usage: {
234
- costUsd: 0,
235
- outputTokens: 0,
236
- toolCalls: 0,
237
- runtimeMs: 0,
238
- filesRead: new Set(),
239
- },
240
- sessionId: sessionId || randomUUID(),
241
- runId: runId || `agent-${Date.now()}-${randomUUID().slice(0, 8)}`,
242
- artifactDir,
243
- startedAt: Date.now(),
244
- lastToolCallAt: Date.now(),
245
- lastToolName: null,
246
- onEvent,
247
- };
248
- }
249
-
250
- function snapshotUsage(ctx) {
251
- return {
252
- costUsd: ctx.usage.costUsd,
253
- outputTokens: ctx.usage.outputTokens,
254
- toolCalls: ctx.usage.toolCalls,
255
- durationMs: Date.now() - ctx.startedAt,
256
- filesRead: [...(ctx.usage.filesRead || [])],
257
- };
258
- }
259
-
260
- function sanitizeInput(toolName, input) {
261
- const sanitized = { ...input };
262
- if (sanitized.content && sanitized.content.length > 200) {
263
- sanitized.content = `[${sanitized.content.length} chars]`;
264
- }
265
- return sanitized;
266
- }
267
-
268
- function summarizeResult(toolName, result) {
269
- if (toolName === "FileRead") {
270
- return `Read ${result.numLines} lines from ${result.filePath}`;
271
- }
272
- if (toolName === "Grep") {
273
- return `${result.numMatches} matches in ${result.numFiles} files`;
274
- }
275
- if (toolName === "Glob") {
276
- return `${result.numFiles} files matched`;
277
- }
278
- if (toolName === "Shell") {
279
- return `Exit ${result.exitCode} in ${result.durationMs}ms`;
280
- }
281
- return `${toolName} completed`;
282
- }
283
-
284
- async function safeAppendEvent(ctx, eventData) {
285
- try {
286
- const normalized = normalizeRunEvent({
287
- ...eventData,
288
- sessionId: ctx.sessionId,
289
- runId: ctx.runId,
290
- });
291
- if (ctx.artifactDir) {
292
- await appendRunEvent(
293
- { targetPath: ctx.artifactDir, outputDir: ctx.artifactDir },
294
- normalized,
295
- );
296
- }
297
- } catch {
298
- // Telemetry failures must not block tool execution
299
- }
300
- }
301
-
302
- export class ToolDispatchError extends Error {
303
- constructor(message) {
304
- super(message);
305
- this.name = "ToolDispatchError";
306
- }
307
- }
308
-
309
- export class BudgetExhaustedError extends Error {
310
- constructor(budgetCheck) {
311
- super(`Budget exhausted: ${budgetCheck.reasons.map((r) => r.code).join(", ")}`);
312
- this.name = "BudgetExhaustedError";
313
- this.budgetCheck = budgetCheck;
314
- }
315
- }
1
+ import { randomUUID } from "node:crypto";
2
+ import { evaluateBudget } from "../../cost/budget.js";
3
+ import {
4
+ normalizeRunEvent,
5
+ appendRunEvent,
6
+ } from "../../telemetry/ledger.js";
7
+ import { createAgentEvent } from "../../events/schema.js";
8
+
9
+ /**
10
+ * Shared tool dispatch infrastructure.
11
+ *
12
+ * Each persona builds its own TOOL_MAP (shared tools + domain tools)
13
+ * and creates a dispatcher via createToolDispatcher(). This avoids
14
+ * duplicating budget enforcement, telemetry, and result persistence.
15
+ */
16
+
17
+ const RESULT_PERSIST_THRESHOLD = 5000;
18
+
19
+ /**
20
+ * Create a tool dispatcher bound to a specific TOOL_MAP.
21
+ *
22
+ * @param {Record<string, Function>} toolMap - { ToolName: handler }
23
+ * @param {Set<string>} [readOnlyTools] - tool names safe for concurrent use
24
+ * @returns {{ dispatchTool, registerTool, isReadOnlyTool, listTools }}
25
+ */
26
+ export function createToolDispatcher(toolMap, readOnlyTools) {
27
+ const TOOL_MAP = { ...toolMap };
28
+ const READ_ONLY_TOOLS = new Set(readOnlyTools || []);
29
+
30
+ async function dispatchTool(toolName, input, ctx) {
31
+ const handler = TOOL_MAP[toolName];
32
+ if (!handler) {
33
+ throw new ToolDispatchError(`Unknown tool: ${toolName}`);
34
+ }
35
+
36
+ // 1. Pre-flight budget check
37
+ const budgetCheck = evaluateBudget({
38
+ maxCostUsd: ctx.budget.maxCostUsd,
39
+ maxOutputTokens: ctx.budget.maxOutputTokens,
40
+ maxRuntimeMs: ctx.budget.maxRuntimeMs,
41
+ maxToolCalls: ctx.budget.maxToolCalls,
42
+ warningThresholdPercent: ctx.budget.warningThresholdPercent ?? 70,
43
+ maxNoProgress: 0,
44
+ sessionSummary: {
45
+ costUsd: ctx.usage.costUsd,
46
+ outputTokens: ctx.usage.outputTokens,
47
+ durationMs: Date.now() - ctx.startedAt,
48
+ toolCalls: ctx.usage.toolCalls + 1,
49
+ noProgressStreak: 0,
50
+ },
51
+ });
52
+
53
+ if (budgetCheck.blocking) {
54
+ const stopEvent = {
55
+ eventType: "run_stop",
56
+ sessionId: ctx.sessionId,
57
+ runId: ctx.runId,
58
+ stop: {
59
+ stopClass: budgetCheck.reasons[0]?.code || "MAX_TOOL_CALLS_EXCEEDED",
60
+ blocking: true,
61
+ reasonCodes: budgetCheck.reasons.map((r) => r.code),
62
+ },
63
+ usage: snapshotUsage(ctx),
64
+ metadata: { tool: toolName, phase: "pre_flight" },
65
+ };
66
+ await safeAppendEvent(ctx, stopEvent);
67
+
68
+ if (ctx.onEvent) {
69
+ ctx.onEvent(createAgentEvent({
70
+ event: "budget_stop",
71
+ agent: ctx.agentIdentity,
72
+ payload: {
73
+ stopClass: stopEvent.stop.stopClass,
74
+ reasons: budgetCheck.reasons,
75
+ },
76
+ usage: snapshotUsage(ctx),
77
+ sessionId: ctx.sessionId,
78
+ runId: ctx.runId,
79
+ }));
80
+ }
81
+
82
+ throw new BudgetExhaustedError(budgetCheck);
83
+ }
84
+
85
+ // Emit budget warnings
86
+ if (budgetCheck.warnings.length > 0 && ctx.onEvent) {
87
+ ctx.onEvent(createAgentEvent({
88
+ event: "budget_warning",
89
+ agent: ctx.agentIdentity,
90
+ payload: { warnings: budgetCheck.warnings },
91
+ usage: snapshotUsage(ctx),
92
+ sessionId: ctx.sessionId,
93
+ runId: ctx.runId,
94
+ }));
95
+ }
96
+
97
+ // 2. Emit tool_call event
98
+ const eventId = randomUUID();
99
+ const callEvent = {
100
+ eventType: "tool_call",
101
+ sessionId: ctx.sessionId,
102
+ runId: ctx.runId,
103
+ metadata: {
104
+ eventId,
105
+ tool: toolName,
106
+ input: sanitizeInput(toolName, input),
107
+ agentId: ctx.agentIdentity?.id,
108
+ persona: ctx.agentIdentity?.persona,
109
+ },
110
+ };
111
+ await safeAppendEvent(ctx, callEvent);
112
+
113
+ if (ctx.onEvent) {
114
+ ctx.onEvent(createAgentEvent({
115
+ event: "tool_call",
116
+ agent: ctx.agentIdentity,
117
+ payload: { tool: toolName, input: sanitizeInput(toolName, input) },
118
+ usage: snapshotUsage(ctx),
119
+ sessionId: ctx.sessionId,
120
+ runId: ctx.runId,
121
+ }));
122
+ }
123
+
124
+ // 3. Execute
125
+ const startMs = Date.now();
126
+ let result;
127
+ let error;
128
+ try {
129
+ result = handler(input);
130
+ } catch (err) {
131
+ error = err;
132
+ }
133
+ const durationMs = Date.now() - startMs;
134
+
135
+ // 4. Update accumulated usage
136
+ ctx.usage.toolCalls++;
137
+ ctx.usage.runtimeMs = Date.now() - ctx.startedAt;
138
+ ctx.lastToolCallAt = Date.now();
139
+ ctx.lastToolName = toolName;
140
+
141
+ // Track confirmed file reads for coverage accounting
142
+ if (!error && toolName === "FileRead") {
143
+ const readPath = input?.file_path || input?.filePath || input?.path || "";
144
+ if (readPath && ctx.usage.filesRead) ctx.usage.filesRead.add(readPath);
145
+ }
146
+
147
+ // 5. Emit tool_result event
148
+ const resultEvent = {
149
+ eventType: "tool_call",
150
+ sessionId: ctx.sessionId,
151
+ runId: ctx.runId,
152
+ usage: {
153
+ durationMs,
154
+ toolCalls: 1,
155
+ },
156
+ metadata: {
157
+ eventId,
158
+ phase: "result",
159
+ tool: toolName,
160
+ success: !error,
161
+ error: error?.message,
162
+ agentId: ctx.agentIdentity?.id,
163
+ },
164
+ };
165
+ await safeAppendEvent(ctx, resultEvent);
166
+
167
+ if (ctx.onEvent) {
168
+ ctx.onEvent(createAgentEvent({
169
+ event: "tool_result",
170
+ agent: ctx.agentIdentity,
171
+ payload: {
172
+ tool: toolName,
173
+ durationMs,
174
+ success: !error,
175
+ error: error?.message,
176
+ },
177
+ usage: snapshotUsage(ctx),
178
+ sessionId: ctx.sessionId,
179
+ runId: ctx.runId,
180
+ }));
181
+ }
182
+
183
+ if (error) throw error;
184
+
185
+ // 6. Large result persistence
186
+ const serialized = JSON.stringify(result);
187
+ if (serialized.length > RESULT_PERSIST_THRESHOLD && ctx.artifactDir) {
188
+ const refPath = `${ctx.artifactDir}/tool-results/${eventId}.json`;
189
+ const fsp = await import("node:fs/promises");
190
+ await fsp.mkdir(`${ctx.artifactDir}/tool-results`, { recursive: true });
191
+ await fsp.writeFile(refPath, serialized, "utf-8");
192
+ return {
193
+ _persisted: true,
194
+ _refPath: refPath,
195
+ _summary: summarizeResult(toolName, result),
196
+ };
197
+ }
198
+
199
+ return result;
200
+ }
201
+
202
+ function registerTool(name, handler, { readOnly = false } = {}) {
203
+ TOOL_MAP[name] = handler;
204
+ if (readOnly) READ_ONLY_TOOLS.add(name);
205
+ }
206
+
207
+ function isReadOnlyTool(toolName) {
208
+ return READ_ONLY_TOOLS.has(toolName);
209
+ }
210
+
211
+ function listTools() {
212
+ return Object.keys(TOOL_MAP);
213
+ }
214
+
215
+ return { dispatchTool, registerTool, isReadOnlyTool, listTools };
216
+ }
217
+
218
+ /**
219
+ * Create an agent context for tool dispatch.
220
+ */
221
+ export function createAgentContext({
222
+ agentIdentity,
223
+ budget,
224
+ sessionId,
225
+ runId,
226
+ artifactDir,
227
+ onEvent,
228
+ }) {
229
+ return {
230
+ agentIdentity,
231
+ budget: {
232
+ maxCostUsd: budget?.maxCostUsd ?? 5.0,
233
+ maxOutputTokens: budget?.maxOutputTokens ?? 12000,
234
+ maxRuntimeMs: budget?.maxRuntimeMs ?? 300000,
235
+ maxToolCalls: budget?.maxToolCalls ?? 150,
236
+ warningThresholdPercent: budget?.warningThresholdPercent ?? 70,
237
+ },
238
+ usage: {
239
+ costUsd: 0,
240
+ outputTokens: 0,
241
+ toolCalls: 0,
242
+ runtimeMs: 0,
243
+ filesRead: new Set(),
244
+ },
245
+ sessionId: sessionId || randomUUID(),
246
+ runId: runId || `agent-${Date.now()}-${randomUUID().slice(0, 8)}`,
247
+ artifactDir,
248
+ startedAt: Date.now(),
249
+ lastToolCallAt: Date.now(),
250
+ lastToolName: null,
251
+ onEvent,
252
+ };
253
+ }
254
+
255
+ function snapshotUsage(ctx) {
256
+ return {
257
+ costUsd: ctx.usage.costUsd,
258
+ outputTokens: ctx.usage.outputTokens,
259
+ toolCalls: ctx.usage.toolCalls,
260
+ durationMs: Date.now() - ctx.startedAt,
261
+ filesRead: [...(ctx.usage.filesRead || [])],
262
+ };
263
+ }
264
+
265
+ function sanitizeInput(toolName, input) {
266
+ const sanitized = { ...input };
267
+ if (sanitized.content && sanitized.content.length > 200) {
268
+ sanitized.content = `[${sanitized.content.length} chars]`;
269
+ }
270
+ return sanitized;
271
+ }
272
+
273
+ function summarizeResult(toolName, result) {
274
+ if (toolName === "FileRead") {
275
+ return `Read ${result.numLines} lines from ${result.filePath}`;
276
+ }
277
+ if (toolName === "Grep") {
278
+ return `${result.numMatches} matches in ${result.numFiles} files`;
279
+ }
280
+ if (toolName === "Glob") {
281
+ return `${result.numFiles} files matched`;
282
+ }
283
+ if (toolName === "Shell") {
284
+ return `Exit ${result.exitCode} in ${result.durationMs}ms`;
285
+ }
286
+ return `${toolName} completed`;
287
+ }
288
+
289
+ async function safeAppendEvent(ctx, eventData) {
290
+ try {
291
+ const normalized = normalizeRunEvent({
292
+ ...eventData,
293
+ sessionId: ctx.sessionId,
294
+ runId: ctx.runId,
295
+ });
296
+ if (ctx.artifactDir) {
297
+ await appendRunEvent(
298
+ { targetPath: ctx.artifactDir, outputDir: ctx.artifactDir },
299
+ normalized,
300
+ );
301
+ }
302
+ } catch {
303
+ // Telemetry failures must not block tool execution
304
+ }
305
+ }
306
+
307
+ export class ToolDispatchError extends Error {
308
+ constructor(message) {
309
+ super(message);
310
+ this.name = "ToolDispatchError";
311
+ }
312
+ }
313
+
314
+ export class BudgetExhaustedError extends Error {
315
+ constructor(budgetCheck) {
316
+ super(`Budget exhausted: ${budgetCheck.reasons.map((r) => r.code).join(", ")}`);
317
+ this.name = "BudgetExhaustedError";
318
+ this.budgetCheck = budgetCheck;
319
+ }
320
+ }