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
@@ -0,0 +1,1068 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import fsp from "node:fs/promises";
3
+ import path from "node:path";
4
+ import process from "node:process";
5
+
6
+ import { resolveOutputRoot } from "../config/service.js";
7
+ import { createAgentEvent } from "../events/schema.js";
8
+ import { collectCodebaseIngest } from "../ingest/engine.js";
9
+ import { appendToStream } from "../session/stream.js";
10
+ import { parseAstModuleSpecifiers } from "./ast-parser-layer.js";
11
+ import { buildCallgraphOverlay } from "./callgraph-overlay.js";
12
+
13
+ const SCOPE_ENGINE_AGENT_ID = "scope-engine";
14
+ const DEFAULT_MAX_CANDIDATE_FILES = 28;
15
+ const DEFAULT_MAX_SEMANTIC_FILES = 8;
16
+ const DEFAULT_SEMANTIC_SIGNAL_THRESHOLD = 10;
17
+ const DEFAULT_ALLOWED_TOOLS = Object.freeze(["file_read", "grep", "glob"]);
18
+ const DEFAULT_DENIED_PATTERNS = Object.freeze([
19
+ ".git/**",
20
+ "node_modules/**",
21
+ ".sentinelayer/**",
22
+ "dist/**",
23
+ "build/**",
24
+ "coverage/**",
25
+ ]);
26
+ const INTAKE_TOKEN_STOP_WORDS = new Set([
27
+ "api",
28
+ "v1",
29
+ "v2",
30
+ "v3",
31
+ "error",
32
+ "service",
33
+ "route",
34
+ "handler",
35
+ "request",
36
+ "response",
37
+ "timeout",
38
+ "failed",
39
+ "failure",
40
+ "unknown",
41
+ ]);
42
+ const SEVERITY_BUDGETS = Object.freeze({
43
+ P0: Object.freeze({
44
+ maxTokens: 9_000,
45
+ maxCostUsd: 2.5,
46
+ maxRuntimeMinutes: 25,
47
+ maxToolCalls: 90,
48
+ }),
49
+ P1: Object.freeze({
50
+ maxTokens: 7_500,
51
+ maxCostUsd: 2.0,
52
+ maxRuntimeMinutes: 20,
53
+ maxToolCalls: 75,
54
+ }),
55
+ P2: Object.freeze({
56
+ maxTokens: 6_000,
57
+ maxCostUsd: 1.5,
58
+ maxRuntimeMinutes: 15,
59
+ maxToolCalls: 60,
60
+ }),
61
+ P3: Object.freeze({
62
+ maxTokens: 4_000,
63
+ maxCostUsd: 1.0,
64
+ maxRuntimeMinutes: 12,
65
+ maxToolCalls: 45,
66
+ }),
67
+ UNKNOWN: Object.freeze({
68
+ maxTokens: 3_000,
69
+ maxCostUsd: 0.7,
70
+ maxRuntimeMinutes: 10,
71
+ maxToolCalls: 35,
72
+ }),
73
+ });
74
+ const ACTIVE_SCOPE_RUNS = new Map();
75
+
76
+ function isPlainObject(value) {
77
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
78
+ }
79
+
80
+ function normalizeString(value) {
81
+ return String(value || "").trim();
82
+ }
83
+
84
+ function normalizeIsoTimestamp(value, fallbackIso = new Date().toISOString()) {
85
+ const normalized = normalizeString(value);
86
+ if (!normalized) {
87
+ return fallbackIso;
88
+ }
89
+ const epoch = Date.parse(normalized);
90
+ if (!Number.isFinite(epoch)) {
91
+ return fallbackIso;
92
+ }
93
+ return new Date(epoch).toISOString();
94
+ }
95
+
96
+ function normalizePositiveInteger(value, fallbackValue) {
97
+ const normalized = Number(value);
98
+ if (!Number.isFinite(normalized) || normalized <= 0) {
99
+ return fallbackValue;
100
+ }
101
+ return Math.max(1, Math.floor(normalized));
102
+ }
103
+
104
+ function normalizeNonNegativeNumber(value, fallbackValue = 0) {
105
+ const normalized = Number(value);
106
+ if (!Number.isFinite(normalized) || normalized < 0) {
107
+ return fallbackValue;
108
+ }
109
+ return normalized;
110
+ }
111
+
112
+ function normalizeSeverity(value) {
113
+ const normalized = normalizeString(value).toUpperCase();
114
+ if (normalized === "P0" || normalized === "P1" || normalized === "P2" || normalized === "P3") {
115
+ return normalized;
116
+ }
117
+ return "UNKNOWN";
118
+ }
119
+
120
+ function toPosixPath(value = "") {
121
+ return String(value || "").replace(/\\/g, "/");
122
+ }
123
+
124
+ function normalizeStringArray(values = []) {
125
+ if (!Array.isArray(values)) {
126
+ return [];
127
+ }
128
+ const deduped = new Set();
129
+ for (const value of values) {
130
+ const normalized = normalizeString(value);
131
+ if (!normalized) {
132
+ continue;
133
+ }
134
+ deduped.add(toPosixPath(normalized));
135
+ }
136
+ return [...deduped];
137
+ }
138
+
139
+ function normalizeDomainAllowlist(values = []) {
140
+ if (!Array.isArray(values)) {
141
+ return [];
142
+ }
143
+ const deduped = new Set();
144
+ for (const value of values) {
145
+ const normalized = normalizeString(value).toLowerCase();
146
+ if (!normalized) {
147
+ continue;
148
+ }
149
+ deduped.add(normalized);
150
+ }
151
+ return [...deduped];
152
+ }
153
+
154
+ function deepFreeze(value) {
155
+ if (!value || typeof value !== "object") {
156
+ return value;
157
+ }
158
+ Object.freeze(value);
159
+ for (const child of Object.values(value)) {
160
+ if (child && typeof child === "object" && !Object.isFrozen(child)) {
161
+ deepFreeze(child);
162
+ }
163
+ }
164
+ return value;
165
+ }
166
+
167
+ export const ISSUE_SCOPE_ENVELOPE_VERSION = "scope-envelope/v1";
168
+ export const ISSUE_SCOPE_ENVELOPE_V1_SCHEMA = deepFreeze({
169
+ $id: "https://sentinelayer.com/schemas/issue-scope-envelope-v1.schema.json",
170
+ $schema: "https://json-schema.org/draft/2020-12/schema",
171
+ version: ISSUE_SCOPE_ENVELOPE_VERSION,
172
+ required: Object.freeze([
173
+ "workItemId",
174
+ "deterministicPack",
175
+ "candidateFiles",
176
+ "endpointMapping",
177
+ "budgetEnvelope",
178
+ "allowedTools",
179
+ "version",
180
+ ]),
181
+ });
182
+
183
+ function buildDayKey(nowIso = new Date().toISOString()) {
184
+ return normalizeIsoTimestamp(nowIso, new Date().toISOString()).slice(0, 10);
185
+ }
186
+
187
+ function deriveLocBucket(totalLoc = 0) {
188
+ const normalized = normalizeNonNegativeNumber(totalLoc, 0);
189
+ if (normalized < 2_500) return "small";
190
+ if (normalized < 10_000) return "medium";
191
+ if (normalized < 40_000) return "large";
192
+ return "xlarge";
193
+ }
194
+
195
+ function splitTokens(value) {
196
+ return normalizeString(value)
197
+ .toLowerCase()
198
+ .split(/[^a-z0-9]+/)
199
+ .map((token) => token.trim())
200
+ .filter(Boolean);
201
+ }
202
+
203
+ function buildIntakeTokens(intakeEvent = {}) {
204
+ const tokens = new Set();
205
+ for (const source of [
206
+ intakeEvent.service,
207
+ intakeEvent.endpoint,
208
+ intakeEvent.errorCode,
209
+ intakeEvent.message,
210
+ ]) {
211
+ for (const token of splitTokens(source)) {
212
+ if (token.length < 3) {
213
+ continue;
214
+ }
215
+ if (INTAKE_TOKEN_STOP_WORDS.has(token)) {
216
+ continue;
217
+ }
218
+ tokens.add(token);
219
+ }
220
+ }
221
+ return [...tokens];
222
+ }
223
+
224
+ function deriveEndpointTokens(endpoint = "") {
225
+ return splitTokens(endpoint)
226
+ .filter((token) => token.length >= 3 && !/^v[0-9]+$/.test(token))
227
+ .slice(0, 8);
228
+ }
229
+
230
+ function normalizeIntakeEvent(intakeEvent = {}, nowIso = new Date().toISOString()) {
231
+ const normalized = isPlainObject(intakeEvent) ? intakeEvent : {};
232
+ return {
233
+ workItemId: normalizeString(normalized.workItemId || normalized.work_item_id),
234
+ service: normalizeString(normalized.service) || "unknown-service",
235
+ endpoint: normalizeString(normalized.endpoint),
236
+ errorCode: normalizeString(normalized.errorCode || normalized.error_code) || "UNKNOWN_ERROR",
237
+ message: normalizeString(normalized.message),
238
+ severity: normalizeSeverity(normalized.severity),
239
+ metadata: isPlainObject(normalized.metadata) ? { ...normalized.metadata } : {},
240
+ occurredAt: normalizeIsoTimestamp(
241
+ normalized.occurredAt || normalized.occurred_at || normalized.first_seen_at,
242
+ nowIso
243
+ ),
244
+ };
245
+ }
246
+
247
+ function createAbortError(reason = "manual_stop") {
248
+ const normalizedReason = normalizeString(reason) || "manual_stop";
249
+ const error = new Error(`Scope engine run aborted: ${normalizedReason}.`);
250
+ error.name = "AbortError";
251
+ error.code = "ABORT_ERR";
252
+ error.reason = normalizedReason;
253
+ return error;
254
+ }
255
+
256
+ function throwIfAborted(runState) {
257
+ if (runState.controller.signal.aborted) {
258
+ throw createAbortError(runState.stopReason);
259
+ }
260
+ }
261
+
262
+ function buildRunKey(sessionId, workItemId, targetPath) {
263
+ return `${path.resolve(String(targetPath || "."))}::${normalizeString(sessionId)}::${normalizeString(workItemId)}`;
264
+ }
265
+
266
+ function toRunSnapshot(runState) {
267
+ return {
268
+ runKey: runState.runKey,
269
+ runId: runState.runId,
270
+ sessionId: runState.sessionId || null,
271
+ workItemId: runState.workItemId,
272
+ targetPath: runState.targetPath,
273
+ startedAt: runState.startedAt,
274
+ running: runState.running,
275
+ stopReason: runState.stopReason || null,
276
+ };
277
+ }
278
+
279
+ function scoreCandidatePath(filePath, intakeTokens = [], endpointTokens = [], service = "") {
280
+ const normalizedPath = toPosixPath(filePath).toLowerCase();
281
+ if (!normalizedPath) {
282
+ return { score: 0, reasons: [] };
283
+ }
284
+
285
+ let score = 0;
286
+ const reasons = [];
287
+ for (const token of intakeTokens) {
288
+ if (normalizedPath.includes(token)) {
289
+ score += 2;
290
+ if (reasons.length < 3) {
291
+ reasons.push(`path_token:${token}`);
292
+ }
293
+ }
294
+ }
295
+ for (const token of endpointTokens) {
296
+ if (normalizedPath.includes(token)) {
297
+ score += 3;
298
+ if (reasons.length < 3) {
299
+ reasons.push(`endpoint_token:${token}`);
300
+ }
301
+ }
302
+ }
303
+ const normalizedService = normalizeString(service).toLowerCase();
304
+ if (normalizedService && normalizedPath.includes(normalizedService.replace(/[^a-z0-9]/g, ""))) {
305
+ score += 2;
306
+ if (reasons.length < 3) {
307
+ reasons.push("service_hint");
308
+ }
309
+ }
310
+ if (score > 0 && /(api|route|router|controller|handler|service|daemon|worker)/.test(normalizedPath)) {
311
+ score += 1;
312
+ if (reasons.length < 3) {
313
+ reasons.push("runtime_surface");
314
+ }
315
+ }
316
+ if (/\.test\.|\.spec\.|fixtures?|docs\//.test(normalizedPath)) {
317
+ score = Math.max(0, score - 1);
318
+ }
319
+ return { score, reasons };
320
+ }
321
+
322
+ function createCandidateFiles({
323
+ indexedFiles = [],
324
+ intakeTokens = [],
325
+ endpointTokens = [],
326
+ intakeEvent = {},
327
+ maxCandidateFiles = DEFAULT_MAX_CANDIDATE_FILES,
328
+ } = {}) {
329
+ const scored = [];
330
+ for (const file of indexedFiles) {
331
+ const pathValue = toPosixPath(file.path);
332
+ if (!pathValue) {
333
+ continue;
334
+ }
335
+ const scoreResult = scoreCandidatePath(
336
+ pathValue,
337
+ intakeTokens,
338
+ endpointTokens,
339
+ intakeEvent.service
340
+ );
341
+ if (scoreResult.score <= 0) {
342
+ continue;
343
+ }
344
+ scored.push({
345
+ path: pathValue,
346
+ score: scoreResult.score,
347
+ reason: scoreResult.reasons.join(",") || "deterministic_path_match",
348
+ });
349
+ }
350
+
351
+ scored.sort((left, right) => {
352
+ if (right.score !== left.score) {
353
+ return right.score - left.score;
354
+ }
355
+ return left.path.localeCompare(right.path);
356
+ });
357
+ return scored.slice(0, normalizePositiveInteger(maxCandidateFiles, DEFAULT_MAX_CANDIDATE_FILES));
358
+ }
359
+
360
+ function createEndpointMapping(endpoint = "", candidateFiles = []) {
361
+ const normalizedEndpoint = normalizeString(endpoint);
362
+ if (!normalizedEndpoint) {
363
+ return [];
364
+ }
365
+ const endpointTokens = deriveEndpointTokens(normalizedEndpoint);
366
+ let mapped = candidateFiles.filter((candidate) => {
367
+ const normalizedPath = toPosixPath(candidate.path).toLowerCase();
368
+ return endpointTokens.some((token) => normalizedPath.includes(token));
369
+ });
370
+ if (mapped.length === 0) {
371
+ mapped = candidateFiles.slice(0, 3);
372
+ }
373
+ return [
374
+ {
375
+ endpoint: normalizedEndpoint,
376
+ files: mapped.map((candidate) => toPosixPath(candidate.path)),
377
+ },
378
+ ];
379
+ }
380
+
381
+ function deriveAllowedAndDeniedPaths(candidateFiles = [], ingest = {}) {
382
+ const allowedRoots = new Set();
383
+ const allowedExactFiles = new Set();
384
+ for (const candidate of candidateFiles.slice(0, 12)) {
385
+ const normalizedPath = toPosixPath(candidate.path);
386
+ if (!normalizedPath) {
387
+ continue;
388
+ }
389
+ const slashIndex = normalizedPath.indexOf("/");
390
+ if (slashIndex <= 0) {
391
+ allowedExactFiles.add(normalizedPath);
392
+ continue;
393
+ }
394
+ allowedRoots.add(normalizedPath.slice(0, slashIndex));
395
+ }
396
+
397
+ if (allowedRoots.size === 0 && allowedExactFiles.size === 0) {
398
+ const fallbackEntryPoints = Array.isArray(ingest.entryPoints) ? ingest.entryPoints : [];
399
+ for (const entryPoint of fallbackEntryPoints.slice(0, 3)) {
400
+ const normalized = toPosixPath(entryPoint);
401
+ const slashIndex = normalized.indexOf("/");
402
+ if (slashIndex <= 0) {
403
+ allowedExactFiles.add(normalized);
404
+ } else {
405
+ allowedRoots.add(normalized.slice(0, slashIndex));
406
+ }
407
+ }
408
+ }
409
+
410
+ const topLevelDirectories = normalizeStringArray(ingest?.topLevel?.directories || []);
411
+ if (allowedRoots.size === 0 && topLevelDirectories.length > 0) {
412
+ allowedRoots.add(topLevelDirectories[0]);
413
+ }
414
+
415
+ const allowedPaths = [
416
+ ...[...allowedRoots].sort((left, right) => left.localeCompare(right)).map((root) => `${root}/**`),
417
+ ...[...allowedExactFiles].sort((left, right) => left.localeCompare(right)),
418
+ ];
419
+ const deniedSet = new Set(DEFAULT_DENIED_PATTERNS);
420
+ for (const directory of topLevelDirectories) {
421
+ if (!allowedRoots.has(directory)) {
422
+ deniedSet.add(`${directory}/**`);
423
+ }
424
+ }
425
+
426
+ return {
427
+ allowedPaths,
428
+ deniedPaths: [...deniedSet].sort((left, right) => left.localeCompare(right)),
429
+ };
430
+ }
431
+
432
+ function deriveAllowedTools(intakeEvent = {}, ingest = {}) {
433
+ const metadata = intakeEvent.metadata || {};
434
+ const fromMetadata = normalizeStringArray(metadata.allowed_tools || metadata.allowedTools || []);
435
+ if (fromMetadata.length > 0) {
436
+ return fromMetadata;
437
+ }
438
+
439
+ const tools = new Set(DEFAULT_ALLOWED_TOOLS);
440
+ const riskSurfaces = Array.isArray(ingest.riskSurfaces) ? ingest.riskSurfaces : [];
441
+ if (riskSurfaces.includes("ci_cd_pipeline")) {
442
+ tools.add("file_read");
443
+ tools.add("grep");
444
+ }
445
+ return [...tools];
446
+ }
447
+
448
+ function deriveBudgetEnvelope({
449
+ intakeEvent = {},
450
+ candidateFiles = [],
451
+ allowedPaths = [],
452
+ deniedPaths = [],
453
+ } = {}) {
454
+ const severity = normalizeSeverity(intakeEvent.severity);
455
+ const baseBudget = SEVERITY_BUDGETS[severity] || SEVERITY_BUDGETS.UNKNOWN;
456
+ const candidateCount = Math.max(1, candidateFiles.length);
457
+ const tokenBoost = Math.min(4_000, candidateCount * 120);
458
+ const toolCallBoost = Math.min(40, Math.ceil(candidateCount / 2));
459
+ const metadata = intakeEvent.metadata || {};
460
+ return {
461
+ maxTokens: baseBudget.maxTokens + tokenBoost,
462
+ maxCostUsd: Number((baseBudget.maxCostUsd + Math.min(0.75, candidateCount * 0.01)).toFixed(3)),
463
+ maxRuntimeMinutes: baseBudget.maxRuntimeMinutes + Math.min(10, Math.ceil(candidateCount / 4)),
464
+ maxToolCalls: baseBudget.maxToolCalls + toolCallBoost,
465
+ networkDomainAllowlist: normalizeDomainAllowlist(
466
+ metadata.networkDomainAllowlist || metadata.network_domain_allowlist || []
467
+ ),
468
+ allowedPaths: normalizeStringArray(allowedPaths),
469
+ deniedPaths: normalizeStringArray(deniedPaths),
470
+ };
471
+ }
472
+
473
+ function computeSemanticSignalScore(candidateFiles = [], intakeEvent = {}) {
474
+ const topCandidates = candidateFiles.slice(0, 5);
475
+ const candidateScore = topCandidates.reduce(
476
+ (sum, candidate) => sum + normalizeNonNegativeNumber(candidate.score, 0),
477
+ 0
478
+ );
479
+ const endpointBoost = normalizeString(intakeEvent.endpoint) ? 2 : 0;
480
+ const serviceBoost = normalizeString(intakeEvent.service) ? 1 : 0;
481
+ return candidateScore + endpointBoost + serviceBoost;
482
+ }
483
+
484
+ function shouldAttachSemanticOverlay({
485
+ candidateFiles = [],
486
+ signalScore = 0,
487
+ semanticSignalThreshold = DEFAULT_SEMANTIC_SIGNAL_THRESHOLD,
488
+ maxSemanticFiles = DEFAULT_MAX_SEMANTIC_FILES,
489
+ } = {}) {
490
+ if (!Array.isArray(candidateFiles) || candidateFiles.length === 0) {
491
+ return false;
492
+ }
493
+ if (candidateFiles.length > normalizePositiveInteger(maxSemanticFiles, DEFAULT_MAX_SEMANTIC_FILES)) {
494
+ return false;
495
+ }
496
+ return signalScore >= normalizePositiveInteger(
497
+ semanticSignalThreshold,
498
+ DEFAULT_SEMANTIC_SIGNAL_THRESHOLD
499
+ );
500
+ }
501
+
502
+ async function collectSemanticOverlay({
503
+ rootPath,
504
+ indexedFilesByPath,
505
+ candidateFiles,
506
+ maxSemanticFiles = DEFAULT_MAX_SEMANTIC_FILES,
507
+ signalScore = 0,
508
+ semanticSignalThreshold = DEFAULT_SEMANTIC_SIGNAL_THRESHOLD,
509
+ runState,
510
+ } = {}) {
511
+ const scopedPaths = candidateFiles
512
+ .slice(0, normalizePositiveInteger(maxSemanticFiles, DEFAULT_MAX_SEMANTIC_FILES))
513
+ .map((candidate) => toPosixPath(candidate.path))
514
+ .filter(Boolean);
515
+ const astEvidence = [];
516
+
517
+ for (const scopedPath of scopedPaths) {
518
+ throwIfAborted(runState);
519
+ const metadata = indexedFilesByPath.get(scopedPath);
520
+ if (!metadata) {
521
+ continue;
522
+ }
523
+ const absolutePath = path.join(rootPath, scopedPath);
524
+ let content = "";
525
+ try {
526
+ content = await fsp.readFile(absolutePath, "utf-8");
527
+ } catch {
528
+ continue;
529
+ }
530
+ const parsed = await parseAstModuleSpecifiers({
531
+ absolutePath,
532
+ content,
533
+ language: metadata.language,
534
+ });
535
+ astEvidence.push({
536
+ path: scopedPath,
537
+ parserMode: normalizeString(parsed.parserMode) || "unknown",
538
+ parseError: normalizeString(parsed.parseError),
539
+ specifierCount: Array.isArray(parsed.specifiers) ? parsed.specifiers.length : 0,
540
+ specifiers: Array.isArray(parsed.specifiers) ? parsed.specifiers.slice(0, 8) : [],
541
+ });
542
+ }
543
+
544
+ throwIfAborted(runState);
545
+ const callgraph = await buildCallgraphOverlay({
546
+ rootPath,
547
+ indexedFilesByPath,
548
+ scopedPaths,
549
+ });
550
+ const symbols = [...new Set(callgraph.nodes.map((node) => normalizeString(node.symbol)).filter(Boolean))]
551
+ .sort((left, right) => left.localeCompare(right))
552
+ .slice(0, 180);
553
+ const callHierarchy = callgraph.edges.slice(0, 220).map((edge) => ({
554
+ from: normalizeString(edge.from),
555
+ to: normalizeString(edge.to),
556
+ callee: normalizeString(edge.callee),
557
+ }));
558
+
559
+ return {
560
+ mode: "on_demand_ast_callgraph",
561
+ signalScore,
562
+ signalThreshold: normalizePositiveInteger(
563
+ semanticSignalThreshold,
564
+ DEFAULT_SEMANTIC_SIGNAL_THRESHOLD
565
+ ),
566
+ symbols,
567
+ callHierarchy,
568
+ astEvidence,
569
+ callgraphSummary: callgraph.summary,
570
+ };
571
+ }
572
+
573
+ export function validateIssueScopeEnvelope(envelope, { throwOnError = false } = {}) {
574
+ const errors = [];
575
+ if (!isPlainObject(envelope)) {
576
+ errors.push("Envelope must be an object.");
577
+ }
578
+
579
+ const version = normalizeString(envelope?.version);
580
+ if (version !== ISSUE_SCOPE_ENVELOPE_VERSION) {
581
+ errors.push(`version must be '${ISSUE_SCOPE_ENVELOPE_VERSION}'.`);
582
+ }
583
+
584
+ if (!normalizeString(envelope?.workItemId)) {
585
+ errors.push("workItemId is required.");
586
+ }
587
+
588
+ const deterministicPack = envelope?.deterministicPack;
589
+ if (!isPlainObject(deterministicPack)) {
590
+ errors.push("deterministicPack must be an object.");
591
+ } else {
592
+ if (!Array.isArray(deterministicPack.frameworks)) {
593
+ errors.push("deterministicPack.frameworks must be an array.");
594
+ }
595
+ if (!Array.isArray(deterministicPack.riskSurfaces)) {
596
+ errors.push("deterministicPack.riskSurfaces must be an array.");
597
+ }
598
+ if (!normalizeString(deterministicPack.locBucket)) {
599
+ errors.push("deterministicPack.locBucket is required.");
600
+ }
601
+ }
602
+
603
+ if (!Array.isArray(envelope?.candidateFiles)) {
604
+ errors.push("candidateFiles must be an array.");
605
+ } else {
606
+ for (const candidate of envelope.candidateFiles) {
607
+ if (!normalizeString(candidate?.path)) {
608
+ errors.push("candidateFiles[*].path is required.");
609
+ break;
610
+ }
611
+ if (!Number.isFinite(Number(candidate?.score)) || Number(candidate.score) < 0) {
612
+ errors.push("candidateFiles[*].score must be a non-negative number.");
613
+ break;
614
+ }
615
+ if (!normalizeString(candidate?.reason)) {
616
+ errors.push("candidateFiles[*].reason is required.");
617
+ break;
618
+ }
619
+ }
620
+ }
621
+
622
+ if (!Array.isArray(envelope?.endpointMapping)) {
623
+ errors.push("endpointMapping must be an array.");
624
+ }
625
+
626
+ const budgetEnvelope = envelope?.budgetEnvelope;
627
+ if (!isPlainObject(budgetEnvelope)) {
628
+ errors.push("budgetEnvelope must be an object.");
629
+ } else {
630
+ if (!Number.isFinite(Number(budgetEnvelope.maxTokens)) || Number(budgetEnvelope.maxTokens) <= 0) {
631
+ errors.push("budgetEnvelope.maxTokens must be > 0.");
632
+ }
633
+ if (!Number.isFinite(Number(budgetEnvelope.maxCostUsd)) || Number(budgetEnvelope.maxCostUsd) <= 0) {
634
+ errors.push("budgetEnvelope.maxCostUsd must be > 0.");
635
+ }
636
+ if (
637
+ !Number.isFinite(Number(budgetEnvelope.maxRuntimeMinutes)) ||
638
+ Number(budgetEnvelope.maxRuntimeMinutes) <= 0
639
+ ) {
640
+ errors.push("budgetEnvelope.maxRuntimeMinutes must be > 0.");
641
+ }
642
+ if (!Number.isFinite(Number(budgetEnvelope.maxToolCalls)) || Number(budgetEnvelope.maxToolCalls) <= 0) {
643
+ errors.push("budgetEnvelope.maxToolCalls must be > 0.");
644
+ }
645
+ if (!Array.isArray(budgetEnvelope.allowedPaths)) {
646
+ errors.push("budgetEnvelope.allowedPaths must be an array.");
647
+ }
648
+ if (!Array.isArray(budgetEnvelope.deniedPaths)) {
649
+ errors.push("budgetEnvelope.deniedPaths must be an array.");
650
+ }
651
+ }
652
+
653
+ if (!Array.isArray(envelope?.allowedTools) || envelope.allowedTools.length === 0) {
654
+ errors.push("allowedTools must contain at least one tool.");
655
+ }
656
+
657
+ if (envelope?.semanticOverlay !== undefined && envelope?.semanticOverlay !== null) {
658
+ if (!isPlainObject(envelope.semanticOverlay)) {
659
+ errors.push("semanticOverlay must be an object when present.");
660
+ } else {
661
+ if (!Array.isArray(envelope.semanticOverlay.symbols)) {
662
+ errors.push("semanticOverlay.symbols must be an array.");
663
+ }
664
+ if (!Array.isArray(envelope.semanticOverlay.callHierarchy)) {
665
+ errors.push("semanticOverlay.callHierarchy must be an array.");
666
+ }
667
+ }
668
+ }
669
+
670
+ if (errors.length > 0 && throwOnError) {
671
+ throw new Error(`Invalid IssueScopeEnvelope: ${errors[0]}`);
672
+ }
673
+ return errors.length === 0;
674
+ }
675
+
676
+ async function emitScopeSessionEvent(
677
+ sessionId,
678
+ event,
679
+ payload = {},
680
+ {
681
+ targetPath = process.cwd(),
682
+ workItemId = "",
683
+ nowIso = new Date().toISOString(),
684
+ } = {}
685
+ ) {
686
+ const normalizedSessionId = normalizeString(sessionId);
687
+ if (!normalizedSessionId) {
688
+ return null;
689
+ }
690
+ const envelope = createAgentEvent({
691
+ event,
692
+ agentId: SCOPE_ENGINE_AGENT_ID,
693
+ sessionId: normalizedSessionId,
694
+ workItemId: normalizeString(workItemId) || undefined,
695
+ ts: normalizeIsoTimestamp(nowIso, new Date().toISOString()),
696
+ payload,
697
+ });
698
+ await appendToStream(normalizedSessionId, envelope, {
699
+ targetPath,
700
+ });
701
+ return envelope;
702
+ }
703
+
704
+ export async function resolveScopeEngineStorage({
705
+ targetPath = ".",
706
+ outputDir = "",
707
+ env,
708
+ homeDir,
709
+ } = {}) {
710
+ const outputRoot = await resolveOutputRoot({
711
+ cwd: path.resolve(String(targetPath || ".")),
712
+ outputDirOverride: outputDir,
713
+ env,
714
+ homeDir,
715
+ });
716
+ const observabilityRoot = path.join(outputRoot, "observability");
717
+ const baseDir = path.join(observabilityRoot, "scope-engine");
718
+ return {
719
+ outputRoot,
720
+ observabilityRoot,
721
+ baseDir,
722
+ runsDir: path.join(baseDir, "runs"),
723
+ };
724
+ }
725
+
726
+ async function writeScopeEnvelopeArtifact(storage, workItemId, envelope, nowIso) {
727
+ const artifactPath = path.join(
728
+ storage.observabilityRoot,
729
+ buildDayKey(nowIso),
730
+ normalizeString(workItemId),
731
+ "scope_envelope.json"
732
+ );
733
+ await fsp.mkdir(path.dirname(artifactPath), { recursive: true });
734
+ await fsp.writeFile(artifactPath, `${JSON.stringify(envelope, null, 2)}\n`, "utf-8");
735
+ return artifactPath;
736
+ }
737
+
738
+ function buildRunMetadataPath(storage, runId) {
739
+ return path.join(storage.runsDir, `${runId}.json`);
740
+ }
741
+
742
+ async function persistRunMetadata(storage, payload) {
743
+ const runPath = buildRunMetadataPath(storage, payload.runId);
744
+ await fsp.mkdir(path.dirname(runPath), { recursive: true });
745
+ await fsp.writeFile(runPath, `${JSON.stringify(payload, null, 2)}\n`, "utf-8");
746
+ return runPath;
747
+ }
748
+
749
+ export function getScopeEngineRun(
750
+ sessionId,
751
+ { workItemId = "", targetPath = process.cwd() } = {}
752
+ ) {
753
+ const normalizedSessionId = normalizeString(sessionId);
754
+ const normalizedWorkItemId = normalizeString(workItemId);
755
+ const resolvedTargetPath = path.resolve(String(targetPath || "."));
756
+
757
+ if (normalizedSessionId && normalizedWorkItemId) {
758
+ const runKey = buildRunKey(normalizedSessionId, normalizedWorkItemId, resolvedTargetPath);
759
+ const runState = ACTIVE_SCOPE_RUNS.get(runKey);
760
+ return runState ? toRunSnapshot(runState) : null;
761
+ }
762
+
763
+ for (const runState of ACTIVE_SCOPE_RUNS.values()) {
764
+ if (runState.targetPath !== resolvedTargetPath) {
765
+ continue;
766
+ }
767
+ if (normalizedSessionId && runState.sessionId !== normalizedSessionId) {
768
+ continue;
769
+ }
770
+ if (normalizedWorkItemId && runState.workItemId !== normalizedWorkItemId) {
771
+ continue;
772
+ }
773
+ return toRunSnapshot(runState);
774
+ }
775
+ return null;
776
+ }
777
+
778
+ export async function stopScopeEngine({
779
+ targetPath = ".",
780
+ sessionId = "",
781
+ workItemId = "",
782
+ reason = "manual_stop",
783
+ } = {}) {
784
+ const resolvedTargetPath = path.resolve(String(targetPath || "."));
785
+ const normalizedSessionId = normalizeString(sessionId);
786
+ const normalizedWorkItemId = normalizeString(workItemId);
787
+ const normalizedReason = normalizeString(reason) || "manual_stop";
788
+
789
+ const matchingRuns = [];
790
+ for (const [runKey, runState] of ACTIVE_SCOPE_RUNS.entries()) {
791
+ if (runState.targetPath !== resolvedTargetPath) {
792
+ continue;
793
+ }
794
+ if (normalizedSessionId && runState.sessionId !== normalizedSessionId) {
795
+ continue;
796
+ }
797
+ if (normalizedWorkItemId && runState.workItemId !== normalizedWorkItemId) {
798
+ continue;
799
+ }
800
+ matchingRuns.push([runKey, runState]);
801
+ }
802
+
803
+ if (matchingRuns.length === 0) {
804
+ return {
805
+ stopped: false,
806
+ count: 0,
807
+ targetPath: resolvedTargetPath,
808
+ sessionId: normalizedSessionId || null,
809
+ workItemId: normalizedWorkItemId || null,
810
+ reason: normalizedReason,
811
+ runs: [],
812
+ };
813
+ }
814
+
815
+ const stoppedRuns = [];
816
+ for (const [runKey, runState] of matchingRuns) {
817
+ runState.running = false;
818
+ runState.stopReason = normalizedReason;
819
+ ACTIVE_SCOPE_RUNS.delete(runKey);
820
+ runState.controller.abort(createAbortError(normalizedReason));
821
+ const event = await emitScopeSessionEvent(
822
+ runState.sessionId,
823
+ "agent_killed",
824
+ {
825
+ target: SCOPE_ENGINE_AGENT_ID,
826
+ reason: normalizedReason,
827
+ runId: runState.runId,
828
+ workItemId: runState.workItemId,
829
+ },
830
+ {
831
+ targetPath: runState.targetPath,
832
+ workItemId: runState.workItemId,
833
+ }
834
+ );
835
+ stoppedRuns.push({
836
+ runKey,
837
+ runId: runState.runId,
838
+ workItemId: runState.workItemId,
839
+ sessionId: runState.sessionId || null,
840
+ reason: normalizedReason,
841
+ event,
842
+ });
843
+ }
844
+
845
+ return {
846
+ stopped: true,
847
+ count: stoppedRuns.length,
848
+ targetPath: resolvedTargetPath,
849
+ sessionId: normalizedSessionId || null,
850
+ workItemId: normalizedWorkItemId || null,
851
+ reason: normalizedReason,
852
+ runs: stoppedRuns,
853
+ };
854
+ }
855
+
856
+ export async function buildIssueScopeEnvelope({
857
+ workItemId = "",
858
+ intakeEvent = {},
859
+ sessionId = "",
860
+ targetPath = ".",
861
+ outputDir = "",
862
+ maxCandidateFiles = DEFAULT_MAX_CANDIDATE_FILES,
863
+ maxSemanticFiles = DEFAULT_MAX_SEMANTIC_FILES,
864
+ semanticSignalThreshold = DEFAULT_SEMANTIC_SIGNAL_THRESHOLD,
865
+ signal,
866
+ env,
867
+ homeDir,
868
+ nowIso = new Date().toISOString(),
869
+ } = {}) {
870
+ const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
871
+ const normalizedSessionId = normalizeString(sessionId);
872
+ const resolvedTargetPath = path.resolve(String(targetPath || "."));
873
+ const normalizedIntake = normalizeIntakeEvent(intakeEvent, normalizedNow);
874
+ const normalizedWorkItemId = normalizeString(workItemId) || normalizedIntake.workItemId;
875
+ if (!normalizedWorkItemId) {
876
+ throw new Error("workItemId is required.");
877
+ }
878
+
879
+ const runId = `scope-${normalizedWorkItemId.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 48)}-${randomUUID().slice(0, 8)}`;
880
+ const runKey = buildRunKey(normalizedSessionId, normalizedWorkItemId, resolvedTargetPath);
881
+ if (ACTIVE_SCOPE_RUNS.has(runKey)) {
882
+ throw new Error(`Scope engine run already active for work item '${normalizedWorkItemId}'.`);
883
+ }
884
+
885
+ const controller = new AbortController();
886
+ const runState = {
887
+ runId,
888
+ runKey,
889
+ sessionId: normalizedSessionId,
890
+ workItemId: normalizedWorkItemId,
891
+ targetPath: resolvedTargetPath,
892
+ startedAt: normalizedNow,
893
+ running: true,
894
+ stopReason: "",
895
+ controller,
896
+ externalAbortListener: null,
897
+ };
898
+ if (signal && typeof signal.addEventListener === "function") {
899
+ if (signal.aborted) {
900
+ controller.abort(createAbortError("manual_stop"));
901
+ } else {
902
+ runState.externalAbortListener = () => {
903
+ runState.stopReason = "manual_stop";
904
+ controller.abort(createAbortError("manual_stop"));
905
+ };
906
+ signal.addEventListener("abort", runState.externalAbortListener, { once: true });
907
+ }
908
+ }
909
+ ACTIVE_SCOPE_RUNS.set(runKey, runState);
910
+
911
+ try {
912
+ throwIfAborted(runState);
913
+ const ingest = await collectCodebaseIngest({
914
+ rootPath: resolvedTargetPath,
915
+ });
916
+ throwIfAborted(runState);
917
+
918
+ const indexedFiles = Array.isArray(ingest?.indexedFiles?.files) ? ingest.indexedFiles.files : [];
919
+ const indexedFilesByPath = new Map();
920
+ for (const file of indexedFiles) {
921
+ const filePath = toPosixPath(file.path);
922
+ if (!filePath) {
923
+ continue;
924
+ }
925
+ indexedFilesByPath.set(filePath, {
926
+ ...file,
927
+ path: filePath,
928
+ });
929
+ }
930
+
931
+ const intakeTokens = buildIntakeTokens(normalizedIntake);
932
+ const endpointTokens = deriveEndpointTokens(normalizedIntake.endpoint);
933
+ const candidateFiles = createCandidateFiles({
934
+ indexedFiles: [...indexedFilesByPath.values()],
935
+ intakeTokens,
936
+ endpointTokens,
937
+ intakeEvent: normalizedIntake,
938
+ maxCandidateFiles,
939
+ });
940
+ const endpointMapping = createEndpointMapping(normalizedIntake.endpoint, candidateFiles);
941
+ const { allowedPaths, deniedPaths } = deriveAllowedAndDeniedPaths(candidateFiles, ingest);
942
+ const budgetEnvelope = deriveBudgetEnvelope({
943
+ intakeEvent: normalizedIntake,
944
+ candidateFiles,
945
+ allowedPaths,
946
+ deniedPaths,
947
+ });
948
+ const allowedTools = deriveAllowedTools(normalizedIntake, ingest);
949
+ const signalScore = computeSemanticSignalScore(candidateFiles, normalizedIntake);
950
+ let semanticOverlay;
951
+ if (
952
+ shouldAttachSemanticOverlay({
953
+ candidateFiles,
954
+ signalScore,
955
+ semanticSignalThreshold,
956
+ maxSemanticFiles,
957
+ })
958
+ ) {
959
+ semanticOverlay = await collectSemanticOverlay({
960
+ rootPath: resolvedTargetPath,
961
+ indexedFilesByPath,
962
+ candidateFiles,
963
+ maxSemanticFiles,
964
+ signalScore,
965
+ semanticSignalThreshold,
966
+ runState,
967
+ });
968
+ }
969
+ throwIfAborted(runState);
970
+
971
+ const envelope = {
972
+ workItemId: normalizedWorkItemId,
973
+ deterministicPack: {
974
+ riskSurfaces: Array.isArray(ingest.riskSurfaces) ? ingest.riskSurfaces : [],
975
+ frameworks: Array.isArray(ingest.frameworks) ? ingest.frameworks : [],
976
+ locBucket: deriveLocBucket(ingest?.summary?.totalLoc),
977
+ },
978
+ candidateFiles: candidateFiles.map((candidate) => ({
979
+ path: toPosixPath(candidate.path),
980
+ score: normalizeNonNegativeNumber(candidate.score, 0),
981
+ reason: normalizeString(candidate.reason) || "deterministic_path_match",
982
+ })),
983
+ endpointMapping,
984
+ semanticOverlay: semanticOverlay || undefined,
985
+ budgetEnvelope,
986
+ allowedTools,
987
+ version: ISSUE_SCOPE_ENVELOPE_VERSION,
988
+ generatedAt: normalizedNow,
989
+ intake: {
990
+ service: normalizedIntake.service,
991
+ endpoint: normalizedIntake.endpoint,
992
+ errorCode: normalizedIntake.errorCode,
993
+ severity: normalizedIntake.severity,
994
+ occurredAt: normalizedIntake.occurredAt,
995
+ },
996
+ };
997
+ validateIssueScopeEnvelope(envelope, { throwOnError: true });
998
+
999
+ const storage = await resolveScopeEngineStorage({
1000
+ targetPath: resolvedTargetPath,
1001
+ outputDir,
1002
+ env,
1003
+ homeDir,
1004
+ });
1005
+ const artifactPath = await writeScopeEnvelopeArtifact(
1006
+ storage,
1007
+ normalizedWorkItemId,
1008
+ envelope,
1009
+ normalizedNow
1010
+ );
1011
+ const runMetadataPath = await persistRunMetadata(storage, {
1012
+ schemaVersion: "1.0.0",
1013
+ generatedAt: normalizedNow,
1014
+ runId,
1015
+ runKey,
1016
+ sessionId: normalizedSessionId || null,
1017
+ workItemId: normalizedWorkItemId,
1018
+ targetPath: resolvedTargetPath,
1019
+ artifactPath: toPosixPath(path.relative(storage.outputRoot, artifactPath)),
1020
+ semanticOverlayAttached: Boolean(semanticOverlay),
1021
+ candidateFileCount: envelope.candidateFiles.length,
1022
+ allowedPathCount: budgetEnvelope.allowedPaths.length,
1023
+ deniedPathCount: budgetEnvelope.deniedPaths.length,
1024
+ signalScore,
1025
+ signalThreshold: normalizePositiveInteger(
1026
+ semanticSignalThreshold,
1027
+ DEFAULT_SEMANTIC_SIGNAL_THRESHOLD
1028
+ ),
1029
+ });
1030
+
1031
+ const event = await emitScopeSessionEvent(
1032
+ normalizedSessionId,
1033
+ "scope_envelope_built",
1034
+ {
1035
+ runId,
1036
+ workItemId: normalizedWorkItemId,
1037
+ version: envelope.version,
1038
+ artifactPath: toPosixPath(path.relative(storage.outputRoot, artifactPath)),
1039
+ candidateFileCount: envelope.candidateFiles.length,
1040
+ semanticOverlayAttached: Boolean(semanticOverlay),
1041
+ },
1042
+ {
1043
+ targetPath: resolvedTargetPath,
1044
+ workItemId: normalizedWorkItemId,
1045
+ nowIso: normalizedNow,
1046
+ }
1047
+ );
1048
+
1049
+ return {
1050
+ runId,
1051
+ runKey,
1052
+ workItemId: normalizedWorkItemId,
1053
+ envelope,
1054
+ artifactPath,
1055
+ runMetadataPath,
1056
+ outputRoot: storage.outputRoot,
1057
+ observabilityRoot: storage.observabilityRoot,
1058
+ semanticOverlayAttached: Boolean(semanticOverlay),
1059
+ event,
1060
+ };
1061
+ } finally {
1062
+ ACTIVE_SCOPE_RUNS.delete(runKey);
1063
+ runState.running = false;
1064
+ if (signal && runState.externalAbortListener && typeof signal.removeEventListener === "function") {
1065
+ signal.removeEventListener("abort", runState.externalAbortListener);
1066
+ }
1067
+ }
1068
+ }