sentinelayer-cli 0.4.4 → 0.6.2

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 (149) hide show
  1. package/README.md +996 -998
  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 +63 -63
  6. package/src/agents/jules/config/definition.js +160 -209
  7. package/src/agents/jules/config/system-prompt.js +182 -175
  8. package/src/agents/jules/error-intake.js +51 -51
  9. package/src/agents/jules/fix-cycle.js +17 -377
  10. package/src/agents/jules/loop.js +450 -367
  11. package/src/agents/jules/pulse.js +10 -327
  12. package/src/agents/jules/stream.js +186 -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 +309 -308
  18. package/src/agents/jules/tools/aidenid-email.js +189 -0
  19. package/src/agents/jules/tools/auth-audit.js +1691 -557
  20. package/src/agents/jules/tools/dispatch.js +335 -327
  21. package/src/agents/jules/tools/file-edit.js +2 -180
  22. package/src/agents/jules/tools/file-read.js +2 -100
  23. package/src/agents/jules/tools/frontend-analyze.js +570 -570
  24. package/src/agents/jules/tools/glob.js +2 -168
  25. package/src/agents/jules/tools/grep.js +2 -228
  26. package/src/agents/jules/tools/index.js +29 -29
  27. package/src/agents/jules/tools/path-guards.js +2 -161
  28. package/src/agents/jules/tools/runtime-audit.js +507 -503
  29. package/src/agents/jules/tools/shell.js +2 -383
  30. package/src/agents/jules/tools/url-policy.js +100 -100
  31. package/src/agents/persona-visuals.js +61 -0
  32. package/src/agents/shared-tools/dispatch-core.js +315 -0
  33. package/src/agents/shared-tools/file-edit.js +180 -0
  34. package/src/agents/shared-tools/file-read.js +100 -0
  35. package/src/agents/shared-tools/glob.js +168 -0
  36. package/src/agents/shared-tools/grep.js +228 -0
  37. package/src/agents/shared-tools/index.js +46 -0
  38. package/src/agents/shared-tools/path-guards.js +161 -0
  39. package/src/agents/shared-tools/shell.js +383 -0
  40. package/src/ai/aidenid.js +1009 -972
  41. package/src/ai/client.js +553 -508
  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 -0
  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 +371 -126
  57. package/src/auth/http.js +611 -270
  58. package/src/auth/service.js +1106 -891
  59. package/src/auth/session-store.js +813 -359
  60. package/src/cli.js +252 -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 +1166 -1166
  67. package/src/commands/auth.js +419 -375
  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 -21
  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 -866
  87. package/src/commands/spec.js +716 -716
  88. package/src/commands/swarm.js +651 -651
  89. package/src/commands/telemetry.js +202 -202
  90. package/src/commands/watch.js +511 -510
  91. package/src/config/agent-dictionary.js +182 -182
  92. package/src/config/io.js +56 -56
  93. package/src/config/paths.js +18 -18
  94. package/src/config/schema.js +55 -55
  95. package/src/config/service.js +184 -184
  96. package/src/cost/budget.js +235 -235
  97. package/src/cost/history.js +188 -188
  98. package/src/cost/tracker.js +171 -171
  99. package/src/daemon/artifact-lineage.js +534 -534
  100. package/src/daemon/assignment-ledger.js +770 -770
  101. package/src/daemon/ast-parser-layer.js +258 -258
  102. package/src/daemon/budget-governor.js +633 -633
  103. package/src/daemon/callgraph-overlay.js +646 -646
  104. package/src/daemon/error-worker.js +626 -626
  105. package/src/daemon/fix-cycle.js +377 -0
  106. package/src/daemon/hybrid-mapper.js +929 -929
  107. package/src/daemon/jira-lifecycle.js +632 -632
  108. package/src/daemon/operator-control.js +657 -657
  109. package/src/daemon/pulse.js +327 -0
  110. package/src/daemon/reliability-lane.js +471 -471
  111. package/src/daemon/watchdog.js +971 -971
  112. package/src/guide/generator.js +316 -316
  113. package/src/ingest/engine.js +918 -918
  114. package/src/interactive/index.js +97 -95
  115. package/src/legacy-cli.js +2994 -2592
  116. package/src/mcp/registry.js +695 -695
  117. package/src/memory/blackboard.js +301 -301
  118. package/src/memory/retrieval.js +581 -581
  119. package/src/plugin/manifest.js +553 -553
  120. package/src/policy/packs.js +144 -144
  121. package/src/prompt/generator.js +118 -118
  122. package/src/review/ai-review.js +679 -669
  123. package/src/review/local-review.js +1305 -1295
  124. package/src/review/omargate-interactive.js +68 -0
  125. package/src/review/omargate-orchestrator.js +300 -0
  126. package/src/review/persona-prompts.js +296 -0
  127. package/src/review/replay.js +235 -235
  128. package/src/review/report.js +664 -664
  129. package/src/review/scan-modes.js +42 -0
  130. package/src/review/spec-binding.js +487 -487
  131. package/src/scaffold/generator.js +67 -67
  132. package/src/scaffold/templates.js +150 -150
  133. package/src/scan/generator.js +418 -418
  134. package/src/scan/gh-secrets.js +107 -107
  135. package/src/spec/generator.js +519 -519
  136. package/src/spec/regenerate.js +237 -237
  137. package/src/spec/templates.js +91 -91
  138. package/src/swarm/dashboard.js +247 -247
  139. package/src/swarm/factory.js +363 -363
  140. package/src/swarm/pentest.js +934 -934
  141. package/src/swarm/registry.js +419 -419
  142. package/src/swarm/report.js +158 -158
  143. package/src/swarm/runtime.js +576 -576
  144. package/src/swarm/scenario-dsl.js +272 -272
  145. package/src/telemetry/ledger.js +302 -302
  146. package/src/telemetry/session-tracker.js +234 -118
  147. package/src/telemetry/sync.js +203 -199
  148. package/src/ui/command-hints.js +13 -0
  149. package/src/ui/markdown.js +220 -220
@@ -1,576 +1,576 @@
1
- import { randomUUID } from "node:crypto";
2
- import fsp from "node:fs/promises";
3
- import path from "node:path";
4
-
5
- import { resolveOutputRoot } from "../config/service.js";
6
- import { evaluateBudget } from "../cost/budget.js";
7
-
8
- function normalizeString(value) {
9
- return String(value || "").trim();
10
- }
11
-
12
- function formatTimestampToken() {
13
- const now = new Date();
14
- const pad = (value) => String(value).padStart(2, "0");
15
- return `${now.getUTCFullYear()}${pad(now.getUTCMonth() + 1)}${pad(now.getUTCDate())}-${pad(
16
- now.getUTCHours()
17
- )}${pad(now.getUTCMinutes())}${pad(now.getUTCSeconds())}`;
18
- }
19
-
20
- function createRuntimeRunId() {
21
- return `swarm-runtime-${formatTimestampToken()}-${randomUUID().slice(0, 8)}`;
22
- }
23
-
24
- function toPosixPath(value) {
25
- return String(value || "").replace(/\\/g, "/");
26
- }
27
-
28
- function estimateTokens(text) {
29
- const normalized = normalizeString(text);
30
- if (!normalized) {
31
- return 0;
32
- }
33
- return Math.max(1, Math.ceil(normalized.length / 4));
34
- }
35
-
36
- function normalizeEngine(value) {
37
- const normalized = normalizeString(value).toLowerCase();
38
- if (normalized === "playwright") {
39
- return "playwright";
40
- }
41
- return "mock";
42
- }
43
-
44
- function normalizeMaxSteps(value) {
45
- const normalized = Number(value || 0);
46
- if (!Number.isFinite(normalized) || normalized < 1) {
47
- throw new Error("maxSteps must be an integer >= 1.");
48
- }
49
- return Math.floor(normalized);
50
- }
51
-
52
- function normalizePlaybookAction(action = {}) {
53
- const type = normalizeString(action.type).toLowerCase();
54
- if (!type) {
55
- return null;
56
- }
57
- return {
58
- type,
59
- url: normalizeString(action.url),
60
- selector: normalizeString(action.selector),
61
- text: normalizeString(action.text),
62
- ms: Math.max(0, Math.floor(Number(action.ms || 0))),
63
- path: normalizeString(action.path),
64
- };
65
- }
66
-
67
- export async function loadSwarmPlaybook(playbookFile = "") {
68
- const normalizedPath = normalizeString(playbookFile);
69
- if (!normalizedPath) {
70
- return [];
71
- }
72
- const resolved = path.resolve(process.cwd(), normalizedPath);
73
- const raw = await fsp.readFile(resolved, "utf-8");
74
- const parsed = JSON.parse(raw);
75
- if (!parsed || typeof parsed !== "object" || !Array.isArray(parsed.actions)) {
76
- throw new Error("Invalid playbook file: expected { actions: [...] }.");
77
- }
78
- return parsed.actions.map((action) => normalizePlaybookAction(action)).filter(Boolean);
79
- }
80
-
81
- export async function loadSwarmPlanFile(planFile = "") {
82
- const normalizedPath = normalizeString(planFile);
83
- if (!normalizedPath) {
84
- throw new Error("planFile is required.");
85
- }
86
- const resolved = path.resolve(process.cwd(), normalizedPath);
87
- const raw = await fsp.readFile(resolved, "utf-8");
88
- const parsed = JSON.parse(raw);
89
- if (!parsed || typeof parsed !== "object") {
90
- throw new Error("Invalid plan file: expected object payload.");
91
- }
92
- if (!Array.isArray(parsed.assignments) || parsed.assignments.length === 0) {
93
- throw new Error("Invalid plan file: assignments are required.");
94
- }
95
- return parsed;
96
- }
97
-
98
- function createEvent({
99
- runId,
100
- step,
101
- eventType,
102
- agentId = "",
103
- message = "",
104
- metadata = {},
105
- usage = {},
106
- } = {}) {
107
- return {
108
- timestamp: new Date().toISOString(),
109
- runId,
110
- step,
111
- eventType,
112
- agentId: normalizeString(agentId).toLowerCase(),
113
- message: normalizeString(message),
114
- usage: {
115
- outputTokens: Number(usage.outputTokens || 0),
116
- toolCalls: Number(usage.toolCalls || 0),
117
- durationMs: Number(usage.durationMs || 0),
118
- costUsd: Number(usage.costUsd || 0),
119
- },
120
- metadata,
121
- };
122
- }
123
-
124
- async function executePlaywrightAction({ page, action, runDirectory, usage, runId, step }) {
125
- const type = action.type;
126
- const metadata = {
127
- action: type,
128
- };
129
- if (type === "goto") {
130
- const url = action.url || "about:blank";
131
- await page.goto(url, { waitUntil: "domcontentloaded" });
132
- metadata.url = url;
133
- usage.toolCalls += 1;
134
- usage.outputTokens += estimateTokens(`goto:${url}`);
135
- return createEvent({
136
- runId,
137
- step,
138
- eventType: "tool_call",
139
- agentId: "omar",
140
- message: `Playwright goto ${url}`,
141
- metadata,
142
- usage,
143
- });
144
- }
145
- if (type === "click") {
146
- if (!action.selector) {
147
- throw new Error("Playbook click action requires selector.");
148
- }
149
- await page.click(action.selector);
150
- metadata.selector = action.selector;
151
- usage.toolCalls += 1;
152
- usage.outputTokens += estimateTokens(`click:${action.selector}`);
153
- return createEvent({
154
- runId,
155
- step,
156
- eventType: "tool_call",
157
- agentId: "omar",
158
- message: `Playwright click ${action.selector}`,
159
- metadata,
160
- usage,
161
- });
162
- }
163
- if (type === "fill") {
164
- if (!action.selector) {
165
- throw new Error("Playbook fill action requires selector.");
166
- }
167
- await page.fill(action.selector, action.text || "");
168
- metadata.selector = action.selector;
169
- usage.toolCalls += 1;
170
- usage.outputTokens += estimateTokens(`fill:${action.selector}:${action.text || ""}`);
171
- return createEvent({
172
- runId,
173
- step,
174
- eventType: "tool_call",
175
- agentId: "omar",
176
- message: `Playwright fill ${action.selector}`,
177
- metadata,
178
- usage,
179
- });
180
- }
181
- if (type === "wait") {
182
- const waitMs = Math.max(0, Number(action.ms || 0));
183
- await page.waitForTimeout(waitMs);
184
- metadata.ms = waitMs;
185
- usage.toolCalls += 1;
186
- usage.outputTokens += estimateTokens(`wait:${waitMs}`);
187
- return createEvent({
188
- runId,
189
- step,
190
- eventType: "tool_call",
191
- agentId: "omar",
192
- message: `Playwright wait ${waitMs}ms`,
193
- metadata,
194
- usage,
195
- });
196
- }
197
- if (type === "screenshot") {
198
- const outputPath = action.path
199
- ? path.resolve(runDirectory, action.path)
200
- : path.join(runDirectory, "runtime", `step-${String(step).padStart(3, "0")}.png`);
201
- await fsp.mkdir(path.dirname(outputPath), { recursive: true });
202
- await page.screenshot({
203
- path: outputPath,
204
- fullPage: true,
205
- });
206
- metadata.path = toPosixPath(path.relative(runDirectory, outputPath));
207
- usage.toolCalls += 1;
208
- usage.outputTokens += estimateTokens(`screenshot:${metadata.path}`);
209
- return createEvent({
210
- runId,
211
- step,
212
- eventType: "tool_call",
213
- agentId: "omar",
214
- message: `Playwright screenshot ${metadata.path}`,
215
- metadata,
216
- usage,
217
- });
218
- }
219
-
220
- usage.toolCalls += 1;
221
- usage.outputTokens += estimateTokens(`unsupported:${type}`);
222
- return createEvent({
223
- runId,
224
- step,
225
- eventType: "tool_call",
226
- agentId: "omar",
227
- message: `Unsupported Playwright action skipped: ${type}`,
228
- metadata: {
229
- action: type,
230
- skipped: true,
231
- },
232
- usage,
233
- });
234
- }
235
-
236
- function buildRuntimeMarkdown(summary = {}) {
237
- return `# SWARM_RUNTIME
238
-
239
- Generated: ${summary.generatedAt}
240
- Run ID: ${summary.runId}
241
- Plan run ID: ${summary.planRunId}
242
- Target: ${summary.targetPath}
243
- Scenario: ${summary.scenario}
244
- Engine: ${summary.engine}
245
- Execute: ${summary.execute ? "yes" : "no"}
246
-
247
- Status:
248
- - completed: ${summary.completed ? "yes" : "no"}
249
- - stop_class: ${summary.stop?.stopClass || "NONE"}
250
- - stop_reason: ${summary.stop?.reason || "none"}
251
-
252
- Usage:
253
- - output_tokens: ${summary.usage.outputTokens}
254
- - tool_calls: ${summary.usage.toolCalls}
255
- - duration_ms: ${summary.usage.durationMs}
256
- - cost_usd: ${summary.usage.costUsd}
257
-
258
- Artifacts:
259
- - events: ${summary.runtimeEventsPath}
260
- - summary_json: ${summary.runtimeJsonPath}
261
- `;
262
- }
263
-
264
- async function writeRuntimeArtifacts({
265
- summary,
266
- events,
267
- outputDir = "",
268
- env,
269
- homeDir,
270
- } = {}) {
271
- const outputRoot = await resolveOutputRoot({
272
- cwd: path.resolve(String(summary.targetPath || ".")),
273
- outputDirOverride: outputDir,
274
- env,
275
- homeDir,
276
- });
277
- const runDirectory = path.join(outputRoot, "swarms", summary.runId);
278
- const runtimeDirectory = path.join(runDirectory, "runtime");
279
- const runtimeJsonPath = path.join(runtimeDirectory, "SWARM_RUNTIME.json");
280
- const runtimeMarkdownPath = path.join(runtimeDirectory, "SWARM_RUNTIME.md");
281
- const runtimeEventsPath = path.join(runtimeDirectory, "events.ndjson");
282
- await fsp.mkdir(runtimeDirectory, { recursive: true });
283
- await fsp.writeFile(runtimeEventsPath, `${events.map((event) => JSON.stringify(event)).join("\n")}\n`, "utf-8");
284
-
285
- const fullSummary = {
286
- ...summary,
287
- outputRoot,
288
- runDirectory,
289
- runtimeDirectory,
290
- runtimeJsonPath,
291
- runtimeMarkdownPath,
292
- runtimeEventsPath,
293
- };
294
-
295
- await fsp.writeFile(runtimeJsonPath, `${JSON.stringify(fullSummary, null, 2)}\n`, "utf-8");
296
- await fsp.writeFile(runtimeMarkdownPath, `${buildRuntimeMarkdown(fullSummary).trim()}\n`, "utf-8");
297
-
298
- return fullSummary;
299
- }
300
-
301
- export async function runSwarmRuntime({
302
- plan,
303
- targetPath,
304
- engine = "mock",
305
- execute = false,
306
- maxSteps = 20,
307
- startUrl = "about:blank",
308
- playbookActions = [],
309
- outputDir = "",
310
- env,
311
- } = {}) {
312
- if (!plan || typeof plan !== "object") {
313
- throw new Error("plan is required.");
314
- }
315
- if (!Array.isArray(plan.assignments) || plan.assignments.length === 0) {
316
- throw new Error("plan.assignments must include at least one assignment.");
317
- }
318
-
319
- const normalizedEngine = normalizeEngine(engine);
320
- const normalizedTargetPath = path.resolve(String(targetPath || plan.targetPath || "."));
321
- const normalizedMaxSteps = normalizeMaxSteps(maxSteps);
322
- const runId = createRuntimeRunId();
323
- const resolvedOutputRoot = await resolveOutputRoot({
324
- cwd: normalizedTargetPath,
325
- outputDirOverride: outputDir,
326
- env,
327
- });
328
- const runtimeRunDirectory = path.join(resolvedOutputRoot, "swarms", runId);
329
- const runStartedAt = Date.now();
330
- const events = [];
331
- let step = 0;
332
-
333
- const usage = {
334
- outputTokens: 0,
335
- toolCalls: 0,
336
- durationMs: 0,
337
- costUsd: 0,
338
- };
339
- let stop = {
340
- stopClass: "NONE",
341
- reason: "",
342
- blocking: false,
343
- };
344
-
345
- events.push(
346
- createEvent({
347
- runId,
348
- step,
349
- eventType: "run_start",
350
- agentId: "omar",
351
- message: `Swarm runtime started with engine=${normalizedEngine}, execute=${Boolean(execute)}`,
352
- metadata: {
353
- planRunId: normalizeString(plan.runId),
354
- scenario: normalizeString(plan.scenario),
355
- },
356
- usage,
357
- })
358
- );
359
-
360
- let browser = null;
361
- let page = null;
362
- let playwrightActions = playbookActions;
363
-
364
- try {
365
- if (normalizedEngine === "playwright" && execute) {
366
- const playwright = await import("playwright");
367
- browser = await playwright.chromium.launch({
368
- headless: true,
369
- });
370
- page = await browser.newPage();
371
- await page.goto(startUrl || "about:blank", { waitUntil: "domcontentloaded" });
372
- usage.toolCalls += 1;
373
- usage.outputTokens += estimateTokens(`goto:${startUrl || "about:blank"}`);
374
- step += 1;
375
- events.push(
376
- createEvent({
377
- runId,
378
- step,
379
- eventType: "tool_call",
380
- agentId: "omar",
381
- message: `Playwright runtime initialized at ${startUrl || "about:blank"}`,
382
- metadata: {
383
- action: "goto",
384
- url: startUrl || "about:blank",
385
- },
386
- usage,
387
- })
388
- );
389
- }
390
-
391
- for (const assignment of plan.assignments) {
392
- if (step >= normalizedMaxSteps) {
393
- stop = {
394
- stopClass: "MAX_STEPS_EXCEEDED",
395
- reason: `max-steps reached (${normalizedMaxSteps})`,
396
- blocking: true,
397
- };
398
- break;
399
- }
400
-
401
- step += 1;
402
- usage.outputTokens += estimateTokens(assignment.objective);
403
- events.push(
404
- createEvent({
405
- runId,
406
- step,
407
- eventType: "run_step",
408
- agentId: assignment.agentId,
409
- message: `Assignment started: ${assignment.objective}`,
410
- metadata: {
411
- assignmentId: assignment.assignmentId,
412
- role: assignment.role,
413
- domain: assignment.domain,
414
- },
415
- usage,
416
- })
417
- );
418
-
419
- if (normalizedEngine === "mock" || !execute) {
420
- usage.toolCalls += 1;
421
- usage.outputTokens += estimateTokens(`mock:${assignment.agentId}`);
422
- step += 1;
423
- events.push(
424
- createEvent({
425
- runId,
426
- step,
427
- eventType: "tool_call",
428
- agentId: assignment.agentId,
429
- message: `Mock runtime action completed for ${assignment.agentId}`,
430
- metadata: {
431
- engine: normalizedEngine,
432
- execute: Boolean(execute),
433
- },
434
- usage,
435
- })
436
- );
437
- } else if (normalizedEngine === "playwright" && execute && page) {
438
- let resolvedActions;
439
- if (Array.isArray(playwrightActions) && playwrightActions.length > 0) {
440
- resolvedActions = playwrightActions;
441
- } else {
442
- resolvedActions = [{ type: "wait", ms: 250 }, { type: "screenshot" }];
443
- }
444
-
445
- for (const action of resolvedActions) {
446
- if (step >= normalizedMaxSteps) {
447
- stop = {
448
- stopClass: "MAX_STEPS_EXCEEDED",
449
- reason: `max-steps reached (${normalizedMaxSteps})`,
450
- blocking: true,
451
- };
452
- break;
453
- }
454
- step += 1;
455
- const event = await executePlaywrightAction({
456
- page,
457
- action,
458
- runDirectory: runtimeRunDirectory,
459
- usage,
460
- runId,
461
- step,
462
- });
463
- events.push(event);
464
- }
465
- if (stop.blocking) {
466
- break;
467
- }
468
- }
469
-
470
- usage.durationMs = Date.now() - runStartedAt;
471
- usage.costUsd = Number((usage.outputTokens * 0.000003).toFixed(6));
472
- const budgetStatus = evaluateBudget({
473
- sessionSummary: {
474
- costUsd: usage.costUsd,
475
- outputTokens: usage.outputTokens,
476
- noProgressStreak: 0,
477
- durationMs: usage.durationMs,
478
- toolCalls: usage.toolCalls,
479
- },
480
- maxCostUsd: Number(plan.globalBudget?.maxCostUsd || 5),
481
- maxOutputTokens: Number(plan.globalBudget?.maxOutputTokens || 20000),
482
- maxNoProgress: Number.MAX_SAFE_INTEGER,
483
- maxRuntimeMs: Number(plan.globalBudget?.maxRuntimeMs || 3600000),
484
- maxToolCalls: Number(plan.globalBudget?.maxToolCalls || 500),
485
- warningThresholdPercent: Number(plan.globalBudget?.warningThresholdPercent || 80),
486
- });
487
- if (budgetStatus.blocking) {
488
- stop = {
489
- stopClass: String(budgetStatus.reasons[0]?.code || "BUDGET_EXCEEDED"),
490
- reason: String(budgetStatus.reasons[0]?.message || "Budget exceeded."),
491
- blocking: true,
492
- };
493
- step += 1;
494
- events.push(
495
- createEvent({
496
- runId,
497
- step,
498
- eventType: "budget_stop",
499
- agentId: "omar",
500
- message: stop.reason,
501
- metadata: {
502
- reasonCodes: budgetStatus.reasons.map((reason) => reason.code),
503
- },
504
- usage,
505
- })
506
- );
507
- break;
508
- }
509
- }
510
- } finally {
511
- if (browser) {
512
- await browser.close();
513
- }
514
- }
515
-
516
- usage.durationMs = Date.now() - runStartedAt;
517
- usage.costUsd = Number((usage.outputTokens * 0.000003).toFixed(6));
518
- const completed = !stop.blocking;
519
- if (completed) {
520
- step += 1;
521
- events.push(
522
- createEvent({
523
- runId,
524
- step,
525
- eventType: "run_stop",
526
- agentId: "omar",
527
- message: "Swarm runtime completed successfully.",
528
- metadata: {
529
- completed: true,
530
- },
531
- usage,
532
- })
533
- );
534
- } else {
535
- step += 1;
536
- events.push(
537
- createEvent({
538
- runId,
539
- step,
540
- eventType: "run_stop",
541
- agentId: "omar",
542
- message: stop.reason || "Swarm runtime stopped.",
543
- metadata: {
544
- completed: false,
545
- stopClass: stop.stopClass,
546
- },
547
- usage,
548
- })
549
- );
550
- }
551
-
552
- const summary = {
553
- schemaVersion: 1,
554
- generatedAt: new Date().toISOString(),
555
- runId,
556
- planRunId: normalizeString(plan.runId),
557
- targetPath: normalizedTargetPath,
558
- scenario: normalizeString(plan.scenario || "qa_audit"),
559
- engine: normalizedEngine,
560
- execute: Boolean(execute),
561
- startUrl: normalizeString(startUrl || "about:blank"),
562
- maxSteps: normalizedMaxSteps,
563
- completed,
564
- stop,
565
- usage,
566
- eventCount: events.length,
567
- selectedAgents: Array.isArray(plan.selectedAgents) ? [...plan.selectedAgents] : [],
568
- };
569
-
570
- return writeRuntimeArtifacts({
571
- summary,
572
- events,
573
- outputDir,
574
- env,
575
- });
576
- }
1
+ import { randomUUID } from "node:crypto";
2
+ import fsp from "node:fs/promises";
3
+ import path from "node:path";
4
+
5
+ import { resolveOutputRoot } from "../config/service.js";
6
+ import { evaluateBudget } from "../cost/budget.js";
7
+
8
+ function normalizeString(value) {
9
+ return String(value || "").trim();
10
+ }
11
+
12
+ function formatTimestampToken() {
13
+ const now = new Date();
14
+ const pad = (value) => String(value).padStart(2, "0");
15
+ return `${now.getUTCFullYear()}${pad(now.getUTCMonth() + 1)}${pad(now.getUTCDate())}-${pad(
16
+ now.getUTCHours()
17
+ )}${pad(now.getUTCMinutes())}${pad(now.getUTCSeconds())}`;
18
+ }
19
+
20
+ function createRuntimeRunId() {
21
+ return `swarm-runtime-${formatTimestampToken()}-${randomUUID().slice(0, 8)}`;
22
+ }
23
+
24
+ function toPosixPath(value) {
25
+ return String(value || "").replace(/\\/g, "/");
26
+ }
27
+
28
+ function estimateTokens(text) {
29
+ const normalized = normalizeString(text);
30
+ if (!normalized) {
31
+ return 0;
32
+ }
33
+ return Math.max(1, Math.ceil(normalized.length / 4));
34
+ }
35
+
36
+ function normalizeEngine(value) {
37
+ const normalized = normalizeString(value).toLowerCase();
38
+ if (normalized === "playwright") {
39
+ return "playwright";
40
+ }
41
+ return "mock";
42
+ }
43
+
44
+ function normalizeMaxSteps(value) {
45
+ const normalized = Number(value || 0);
46
+ if (!Number.isFinite(normalized) || normalized < 1) {
47
+ throw new Error("maxSteps must be an integer >= 1.");
48
+ }
49
+ return Math.floor(normalized);
50
+ }
51
+
52
+ function normalizePlaybookAction(action = {}) {
53
+ const type = normalizeString(action.type).toLowerCase();
54
+ if (!type) {
55
+ return null;
56
+ }
57
+ return {
58
+ type,
59
+ url: normalizeString(action.url),
60
+ selector: normalizeString(action.selector),
61
+ text: normalizeString(action.text),
62
+ ms: Math.max(0, Math.floor(Number(action.ms || 0))),
63
+ path: normalizeString(action.path),
64
+ };
65
+ }
66
+
67
+ export async function loadSwarmPlaybook(playbookFile = "") {
68
+ const normalizedPath = normalizeString(playbookFile);
69
+ if (!normalizedPath) {
70
+ return [];
71
+ }
72
+ const resolved = path.resolve(process.cwd(), normalizedPath);
73
+ const raw = await fsp.readFile(resolved, "utf-8");
74
+ const parsed = JSON.parse(raw);
75
+ if (!parsed || typeof parsed !== "object" || !Array.isArray(parsed.actions)) {
76
+ throw new Error("Invalid playbook file: expected { actions: [...] }.");
77
+ }
78
+ return parsed.actions.map((action) => normalizePlaybookAction(action)).filter(Boolean);
79
+ }
80
+
81
+ export async function loadSwarmPlanFile(planFile = "") {
82
+ const normalizedPath = normalizeString(planFile);
83
+ if (!normalizedPath) {
84
+ throw new Error("planFile is required.");
85
+ }
86
+ const resolved = path.resolve(process.cwd(), normalizedPath);
87
+ const raw = await fsp.readFile(resolved, "utf-8");
88
+ const parsed = JSON.parse(raw);
89
+ if (!parsed || typeof parsed !== "object") {
90
+ throw new Error("Invalid plan file: expected object payload.");
91
+ }
92
+ if (!Array.isArray(parsed.assignments) || parsed.assignments.length === 0) {
93
+ throw new Error("Invalid plan file: assignments are required.");
94
+ }
95
+ return parsed;
96
+ }
97
+
98
+ function createEvent({
99
+ runId,
100
+ step,
101
+ eventType,
102
+ agentId = "",
103
+ message = "",
104
+ metadata = {},
105
+ usage = {},
106
+ } = {}) {
107
+ return {
108
+ timestamp: new Date().toISOString(),
109
+ runId,
110
+ step,
111
+ eventType,
112
+ agentId: normalizeString(agentId).toLowerCase(),
113
+ message: normalizeString(message),
114
+ usage: {
115
+ outputTokens: Number(usage.outputTokens || 0),
116
+ toolCalls: Number(usage.toolCalls || 0),
117
+ durationMs: Number(usage.durationMs || 0),
118
+ costUsd: Number(usage.costUsd || 0),
119
+ },
120
+ metadata,
121
+ };
122
+ }
123
+
124
+ async function executePlaywrightAction({ page, action, runDirectory, usage, runId, step }) {
125
+ const type = action.type;
126
+ const metadata = {
127
+ action: type,
128
+ };
129
+ if (type === "goto") {
130
+ const url = action.url || "about:blank";
131
+ await page.goto(url, { waitUntil: "domcontentloaded" });
132
+ metadata.url = url;
133
+ usage.toolCalls += 1;
134
+ usage.outputTokens += estimateTokens(`goto:${url}`);
135
+ return createEvent({
136
+ runId,
137
+ step,
138
+ eventType: "tool_call",
139
+ agentId: "omar",
140
+ message: `Playwright goto ${url}`,
141
+ metadata,
142
+ usage,
143
+ });
144
+ }
145
+ if (type === "click") {
146
+ if (!action.selector) {
147
+ throw new Error("Playbook click action requires selector.");
148
+ }
149
+ await page.click(action.selector);
150
+ metadata.selector = action.selector;
151
+ usage.toolCalls += 1;
152
+ usage.outputTokens += estimateTokens(`click:${action.selector}`);
153
+ return createEvent({
154
+ runId,
155
+ step,
156
+ eventType: "tool_call",
157
+ agentId: "omar",
158
+ message: `Playwright click ${action.selector}`,
159
+ metadata,
160
+ usage,
161
+ });
162
+ }
163
+ if (type === "fill") {
164
+ if (!action.selector) {
165
+ throw new Error("Playbook fill action requires selector.");
166
+ }
167
+ await page.fill(action.selector, action.text || "");
168
+ metadata.selector = action.selector;
169
+ usage.toolCalls += 1;
170
+ usage.outputTokens += estimateTokens(`fill:${action.selector}:${action.text || ""}`);
171
+ return createEvent({
172
+ runId,
173
+ step,
174
+ eventType: "tool_call",
175
+ agentId: "omar",
176
+ message: `Playwright fill ${action.selector}`,
177
+ metadata,
178
+ usage,
179
+ });
180
+ }
181
+ if (type === "wait") {
182
+ const waitMs = Math.max(0, Number(action.ms || 0));
183
+ await page.waitForTimeout(waitMs);
184
+ metadata.ms = waitMs;
185
+ usage.toolCalls += 1;
186
+ usage.outputTokens += estimateTokens(`wait:${waitMs}`);
187
+ return createEvent({
188
+ runId,
189
+ step,
190
+ eventType: "tool_call",
191
+ agentId: "omar",
192
+ message: `Playwright wait ${waitMs}ms`,
193
+ metadata,
194
+ usage,
195
+ });
196
+ }
197
+ if (type === "screenshot") {
198
+ const outputPath = action.path
199
+ ? path.resolve(runDirectory, action.path)
200
+ : path.join(runDirectory, "runtime", `step-${String(step).padStart(3, "0")}.png`);
201
+ await fsp.mkdir(path.dirname(outputPath), { recursive: true });
202
+ await page.screenshot({
203
+ path: outputPath,
204
+ fullPage: true,
205
+ });
206
+ metadata.path = toPosixPath(path.relative(runDirectory, outputPath));
207
+ usage.toolCalls += 1;
208
+ usage.outputTokens += estimateTokens(`screenshot:${metadata.path}`);
209
+ return createEvent({
210
+ runId,
211
+ step,
212
+ eventType: "tool_call",
213
+ agentId: "omar",
214
+ message: `Playwright screenshot ${metadata.path}`,
215
+ metadata,
216
+ usage,
217
+ });
218
+ }
219
+
220
+ usage.toolCalls += 1;
221
+ usage.outputTokens += estimateTokens(`unsupported:${type}`);
222
+ return createEvent({
223
+ runId,
224
+ step,
225
+ eventType: "tool_call",
226
+ agentId: "omar",
227
+ message: `Unsupported Playwright action skipped: ${type}`,
228
+ metadata: {
229
+ action: type,
230
+ skipped: true,
231
+ },
232
+ usage,
233
+ });
234
+ }
235
+
236
+ function buildRuntimeMarkdown(summary = {}) {
237
+ return `# SWARM_RUNTIME
238
+
239
+ Generated: ${summary.generatedAt}
240
+ Run ID: ${summary.runId}
241
+ Plan run ID: ${summary.planRunId}
242
+ Target: ${summary.targetPath}
243
+ Scenario: ${summary.scenario}
244
+ Engine: ${summary.engine}
245
+ Execute: ${summary.execute ? "yes" : "no"}
246
+
247
+ Status:
248
+ - completed: ${summary.completed ? "yes" : "no"}
249
+ - stop_class: ${summary.stop?.stopClass || "NONE"}
250
+ - stop_reason: ${summary.stop?.reason || "none"}
251
+
252
+ Usage:
253
+ - output_tokens: ${summary.usage.outputTokens}
254
+ - tool_calls: ${summary.usage.toolCalls}
255
+ - duration_ms: ${summary.usage.durationMs}
256
+ - cost_usd: ${summary.usage.costUsd}
257
+
258
+ Artifacts:
259
+ - events: ${summary.runtimeEventsPath}
260
+ - summary_json: ${summary.runtimeJsonPath}
261
+ `;
262
+ }
263
+
264
+ async function writeRuntimeArtifacts({
265
+ summary,
266
+ events,
267
+ outputDir = "",
268
+ env,
269
+ homeDir,
270
+ } = {}) {
271
+ const outputRoot = await resolveOutputRoot({
272
+ cwd: path.resolve(String(summary.targetPath || ".")),
273
+ outputDirOverride: outputDir,
274
+ env,
275
+ homeDir,
276
+ });
277
+ const runDirectory = path.join(outputRoot, "swarms", summary.runId);
278
+ const runtimeDirectory = path.join(runDirectory, "runtime");
279
+ const runtimeJsonPath = path.join(runtimeDirectory, "SWARM_RUNTIME.json");
280
+ const runtimeMarkdownPath = path.join(runtimeDirectory, "SWARM_RUNTIME.md");
281
+ const runtimeEventsPath = path.join(runtimeDirectory, "events.ndjson");
282
+ await fsp.mkdir(runtimeDirectory, { recursive: true });
283
+ await fsp.writeFile(runtimeEventsPath, `${events.map((event) => JSON.stringify(event)).join("\n")}\n`, "utf-8");
284
+
285
+ const fullSummary = {
286
+ ...summary,
287
+ outputRoot,
288
+ runDirectory,
289
+ runtimeDirectory,
290
+ runtimeJsonPath,
291
+ runtimeMarkdownPath,
292
+ runtimeEventsPath,
293
+ };
294
+
295
+ await fsp.writeFile(runtimeJsonPath, `${JSON.stringify(fullSummary, null, 2)}\n`, "utf-8");
296
+ await fsp.writeFile(runtimeMarkdownPath, `${buildRuntimeMarkdown(fullSummary).trim()}\n`, "utf-8");
297
+
298
+ return fullSummary;
299
+ }
300
+
301
+ export async function runSwarmRuntime({
302
+ plan,
303
+ targetPath,
304
+ engine = "mock",
305
+ execute = false,
306
+ maxSteps = 20,
307
+ startUrl = "about:blank",
308
+ playbookActions = [],
309
+ outputDir = "",
310
+ env,
311
+ } = {}) {
312
+ if (!plan || typeof plan !== "object") {
313
+ throw new Error("plan is required.");
314
+ }
315
+ if (!Array.isArray(plan.assignments) || plan.assignments.length === 0) {
316
+ throw new Error("plan.assignments must include at least one assignment.");
317
+ }
318
+
319
+ const normalizedEngine = normalizeEngine(engine);
320
+ const normalizedTargetPath = path.resolve(String(targetPath || plan.targetPath || "."));
321
+ const normalizedMaxSteps = normalizeMaxSteps(maxSteps);
322
+ const runId = createRuntimeRunId();
323
+ const resolvedOutputRoot = await resolveOutputRoot({
324
+ cwd: normalizedTargetPath,
325
+ outputDirOverride: outputDir,
326
+ env,
327
+ });
328
+ const runtimeRunDirectory = path.join(resolvedOutputRoot, "swarms", runId);
329
+ const runStartedAt = Date.now();
330
+ const events = [];
331
+ let step = 0;
332
+
333
+ const usage = {
334
+ outputTokens: 0,
335
+ toolCalls: 0,
336
+ durationMs: 0,
337
+ costUsd: 0,
338
+ };
339
+ let stop = {
340
+ stopClass: "NONE",
341
+ reason: "",
342
+ blocking: false,
343
+ };
344
+
345
+ events.push(
346
+ createEvent({
347
+ runId,
348
+ step,
349
+ eventType: "run_start",
350
+ agentId: "omar",
351
+ message: `Swarm runtime started with engine=${normalizedEngine}, execute=${Boolean(execute)}`,
352
+ metadata: {
353
+ planRunId: normalizeString(plan.runId),
354
+ scenario: normalizeString(plan.scenario),
355
+ },
356
+ usage,
357
+ })
358
+ );
359
+
360
+ let browser = null;
361
+ let page = null;
362
+ let playwrightActions = playbookActions;
363
+
364
+ try {
365
+ if (normalizedEngine === "playwright" && execute) {
366
+ const playwright = await import("playwright");
367
+ browser = await playwright.chromium.launch({
368
+ headless: true,
369
+ });
370
+ page = await browser.newPage();
371
+ await page.goto(startUrl || "about:blank", { waitUntil: "domcontentloaded" });
372
+ usage.toolCalls += 1;
373
+ usage.outputTokens += estimateTokens(`goto:${startUrl || "about:blank"}`);
374
+ step += 1;
375
+ events.push(
376
+ createEvent({
377
+ runId,
378
+ step,
379
+ eventType: "tool_call",
380
+ agentId: "omar",
381
+ message: `Playwright runtime initialized at ${startUrl || "about:blank"}`,
382
+ metadata: {
383
+ action: "goto",
384
+ url: startUrl || "about:blank",
385
+ },
386
+ usage,
387
+ })
388
+ );
389
+ }
390
+
391
+ for (const assignment of plan.assignments) {
392
+ if (step >= normalizedMaxSteps) {
393
+ stop = {
394
+ stopClass: "MAX_STEPS_EXCEEDED",
395
+ reason: `max-steps reached (${normalizedMaxSteps})`,
396
+ blocking: true,
397
+ };
398
+ break;
399
+ }
400
+
401
+ step += 1;
402
+ usage.outputTokens += estimateTokens(assignment.objective);
403
+ events.push(
404
+ createEvent({
405
+ runId,
406
+ step,
407
+ eventType: "run_step",
408
+ agentId: assignment.agentId,
409
+ message: `Assignment started: ${assignment.objective}`,
410
+ metadata: {
411
+ assignmentId: assignment.assignmentId,
412
+ role: assignment.role,
413
+ domain: assignment.domain,
414
+ },
415
+ usage,
416
+ })
417
+ );
418
+
419
+ if (normalizedEngine === "mock" || !execute) {
420
+ usage.toolCalls += 1;
421
+ usage.outputTokens += estimateTokens(`mock:${assignment.agentId}`);
422
+ step += 1;
423
+ events.push(
424
+ createEvent({
425
+ runId,
426
+ step,
427
+ eventType: "tool_call",
428
+ agentId: assignment.agentId,
429
+ message: `Mock runtime action completed for ${assignment.agentId}`,
430
+ metadata: {
431
+ engine: normalizedEngine,
432
+ execute: Boolean(execute),
433
+ },
434
+ usage,
435
+ })
436
+ );
437
+ } else if (normalizedEngine === "playwright" && execute && page) {
438
+ let resolvedActions;
439
+ if (Array.isArray(playwrightActions) && playwrightActions.length > 0) {
440
+ resolvedActions = playwrightActions;
441
+ } else {
442
+ resolvedActions = [{ type: "wait", ms: 250 }, { type: "screenshot" }];
443
+ }
444
+
445
+ for (const action of resolvedActions) {
446
+ if (step >= normalizedMaxSteps) {
447
+ stop = {
448
+ stopClass: "MAX_STEPS_EXCEEDED",
449
+ reason: `max-steps reached (${normalizedMaxSteps})`,
450
+ blocking: true,
451
+ };
452
+ break;
453
+ }
454
+ step += 1;
455
+ const event = await executePlaywrightAction({
456
+ page,
457
+ action,
458
+ runDirectory: runtimeRunDirectory,
459
+ usage,
460
+ runId,
461
+ step,
462
+ });
463
+ events.push(event);
464
+ }
465
+ if (stop.blocking) {
466
+ break;
467
+ }
468
+ }
469
+
470
+ usage.durationMs = Date.now() - runStartedAt;
471
+ usage.costUsd = Number((usage.outputTokens * 0.000003).toFixed(6));
472
+ const budgetStatus = evaluateBudget({
473
+ sessionSummary: {
474
+ costUsd: usage.costUsd,
475
+ outputTokens: usage.outputTokens,
476
+ noProgressStreak: 0,
477
+ durationMs: usage.durationMs,
478
+ toolCalls: usage.toolCalls,
479
+ },
480
+ maxCostUsd: Number(plan.globalBudget?.maxCostUsd || 5),
481
+ maxOutputTokens: Number(plan.globalBudget?.maxOutputTokens || 20000),
482
+ maxNoProgress: Number.MAX_SAFE_INTEGER,
483
+ maxRuntimeMs: Number(plan.globalBudget?.maxRuntimeMs || 3600000),
484
+ maxToolCalls: Number(plan.globalBudget?.maxToolCalls || 500),
485
+ warningThresholdPercent: Number(plan.globalBudget?.warningThresholdPercent || 80),
486
+ });
487
+ if (budgetStatus.blocking) {
488
+ stop = {
489
+ stopClass: String(budgetStatus.reasons[0]?.code || "BUDGET_EXCEEDED"),
490
+ reason: String(budgetStatus.reasons[0]?.message || "Budget exceeded."),
491
+ blocking: true,
492
+ };
493
+ step += 1;
494
+ events.push(
495
+ createEvent({
496
+ runId,
497
+ step,
498
+ eventType: "budget_stop",
499
+ agentId: "omar",
500
+ message: stop.reason,
501
+ metadata: {
502
+ reasonCodes: budgetStatus.reasons.map((reason) => reason.code),
503
+ },
504
+ usage,
505
+ })
506
+ );
507
+ break;
508
+ }
509
+ }
510
+ } finally {
511
+ if (browser) {
512
+ await browser.close();
513
+ }
514
+ }
515
+
516
+ usage.durationMs = Date.now() - runStartedAt;
517
+ usage.costUsd = Number((usage.outputTokens * 0.000003).toFixed(6));
518
+ const completed = !stop.blocking;
519
+ if (completed) {
520
+ step += 1;
521
+ events.push(
522
+ createEvent({
523
+ runId,
524
+ step,
525
+ eventType: "run_stop",
526
+ agentId: "omar",
527
+ message: "Swarm runtime completed successfully.",
528
+ metadata: {
529
+ completed: true,
530
+ },
531
+ usage,
532
+ })
533
+ );
534
+ } else {
535
+ step += 1;
536
+ events.push(
537
+ createEvent({
538
+ runId,
539
+ step,
540
+ eventType: "run_stop",
541
+ agentId: "omar",
542
+ message: stop.reason || "Swarm runtime stopped.",
543
+ metadata: {
544
+ completed: false,
545
+ stopClass: stop.stopClass,
546
+ },
547
+ usage,
548
+ })
549
+ );
550
+ }
551
+
552
+ const summary = {
553
+ schemaVersion: 1,
554
+ generatedAt: new Date().toISOString(),
555
+ runId,
556
+ planRunId: normalizeString(plan.runId),
557
+ targetPath: normalizedTargetPath,
558
+ scenario: normalizeString(plan.scenario || "qa_audit"),
559
+ engine: normalizedEngine,
560
+ execute: Boolean(execute),
561
+ startUrl: normalizeString(startUrl || "about:blank"),
562
+ maxSteps: normalizedMaxSteps,
563
+ completed,
564
+ stop,
565
+ usage,
566
+ eventCount: events.length,
567
+ selectedAgents: Array.isArray(plan.selectedAgents) ? [...plan.selectedAgents] : [],
568
+ };
569
+
570
+ return writeRuntimeArtifacts({
571
+ summary,
572
+ events,
573
+ outputDir,
574
+ env,
575
+ });
576
+ }