sentinelayer-cli 0.4.5 → 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,929 +1,929 @@
1
- import fsp from "node:fs/promises";
2
- import path from "node:path";
3
-
4
- import { parseAstModuleSpecifiers } from "./ast-parser-layer.js";
5
- import { buildCallgraphOverlay } from "./callgraph-overlay.js";
6
- import { collectCodebaseIngest } from "../ingest/engine.js";
7
- import { listErrorQueue, resolveErrorDaemonStorage } from "./error-worker.js";
8
-
9
- const HYBRID_MAP_SCHEMA_VERSION = "1.0.0";
10
- const HYBRID_HANDOFF_SCHEMA_VERSION = "1.0.0";
11
- const DEFAULT_MAX_SCOPE_FILES = 40;
12
- const DEFAULT_GRAPH_DEPTH = 2;
13
- const DEFAULT_HANDOFF_MAX_FILES = 24;
14
- const LANGUAGE_IMPORT_EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".py"];
15
-
16
- function normalizeString(value) {
17
- return String(value || "").trim();
18
- }
19
-
20
- function normalizeIsoTimestamp(value, fallbackIso = new Date().toISOString()) {
21
- const normalized = normalizeString(value);
22
- if (!normalized) {
23
- return fallbackIso;
24
- }
25
- const epoch = Date.parse(normalized);
26
- if (!Number.isFinite(epoch)) {
27
- return fallbackIso;
28
- }
29
- return new Date(epoch).toISOString();
30
- }
31
-
32
- function normalizePositiveInteger(value, fieldName, fallbackValue) {
33
- if (value === undefined || value === null || normalizeString(value) === "") {
34
- return fallbackValue;
35
- }
36
- const normalized = Number(value);
37
- if (!Number.isFinite(normalized) || normalized <= 0) {
38
- throw new Error(`${fieldName} must be a positive integer.`);
39
- }
40
- return Math.floor(normalized);
41
- }
42
-
43
- function toPosixPath(value = "") {
44
- return String(value || "").replace(/\\/g, "/");
45
- }
46
-
47
- function normalizeMapIndex(index = {}, nowIso = new Date().toISOString()) {
48
- return {
49
- schemaVersion: HYBRID_MAP_SCHEMA_VERSION,
50
- generatedAt: normalizeIsoTimestamp(index.generatedAt, nowIso),
51
- maps: Array.isArray(index.maps)
52
- ? index.maps
53
- .map((entry) => ({
54
- ...entry,
55
- workItemId: normalizeString(entry.workItemId),
56
- runId: normalizeString(entry.runId),
57
- generatedAt: normalizeIsoTimestamp(entry.generatedAt, nowIso),
58
- mapPath: normalizeString(entry.mapPath),
59
- }))
60
- .filter((entry) => entry.workItemId && entry.runId && entry.mapPath)
61
- : [],
62
- };
63
- }
64
-
65
- function createInitialMapIndex(nowIso = new Date().toISOString()) {
66
- return {
67
- schemaVersion: HYBRID_MAP_SCHEMA_VERSION,
68
- generatedAt: normalizeIsoTimestamp(nowIso, nowIso),
69
- maps: [],
70
- };
71
- }
72
-
73
- function normalizeHandoffIndex(index = {}, nowIso = new Date().toISOString()) {
74
- return {
75
- schemaVersion: HYBRID_HANDOFF_SCHEMA_VERSION,
76
- generatedAt: normalizeIsoTimestamp(index.generatedAt, nowIso),
77
- handoffs: Array.isArray(index.handoffs)
78
- ? index.handoffs
79
- .map((entry) => ({
80
- ...entry,
81
- workItemId: normalizeString(entry.workItemId),
82
- handoffRunId: normalizeString(entry.handoffRunId),
83
- mapRunId: normalizeString(entry.mapRunId),
84
- generatedAt: normalizeIsoTimestamp(entry.generatedAt, nowIso),
85
- handoffPath: normalizeString(entry.handoffPath),
86
- assignee: normalizeString(entry.assignee),
87
- }))
88
- .filter((entry) => entry.workItemId && entry.handoffRunId && entry.handoffPath)
89
- : [],
90
- };
91
- }
92
-
93
- function createInitialHandoffIndex(nowIso = new Date().toISOString()) {
94
- return {
95
- schemaVersion: HYBRID_HANDOFF_SCHEMA_VERSION,
96
- generatedAt: normalizeIsoTimestamp(nowIso, nowIso),
97
- handoffs: [],
98
- };
99
- }
100
-
101
- async function readJsonFile(filePath, defaultFactory) {
102
- try {
103
- const raw = await fsp.readFile(filePath, "utf-8");
104
- return JSON.parse(raw);
105
- } catch (error) {
106
- if (error && typeof error === "object" && error.code === "ENOENT") {
107
- return defaultFactory();
108
- }
109
- throw error;
110
- }
111
- }
112
-
113
- async function writeJsonFile(filePath, payload = {}) {
114
- await fsp.mkdir(path.dirname(filePath), { recursive: true });
115
- await fsp.writeFile(filePath, `${JSON.stringify(payload, null, 2)}\n`, "utf-8");
116
- }
117
-
118
- async function appendJsonLine(filePath, payload = {}) {
119
- await fsp.mkdir(path.dirname(filePath), { recursive: true });
120
- await fsp.appendFile(filePath, `${JSON.stringify(payload)}\n`, "utf-8");
121
- }
122
-
123
- function tokenizeWorkItem(queueItem = {}) {
124
- const parts = [
125
- normalizeString(queueItem.endpoint),
126
- normalizeString(queueItem.errorCode),
127
- normalizeString(queueItem.service),
128
- ]
129
- .join("/")
130
- .split(/[^a-zA-Z0-9]+/)
131
- .map((token) => token.toLowerCase())
132
- .filter(Boolean);
133
- const unique = new Set();
134
- for (const token of parts) {
135
- if (token.length < 3) {
136
- continue;
137
- }
138
- if (["api", "v1", "v2", "err", "error", "svc", "service"].includes(token)) {
139
- continue;
140
- }
141
- unique.add(token);
142
- }
143
- return [...unique].slice(0, 24);
144
- }
145
-
146
- function scoreDeterministicPath(pathText, tokens = []) {
147
- const normalizedPath = normalizeString(pathText).toLowerCase();
148
- let score = 0;
149
- for (const token of tokens) {
150
- if (normalizedPath.includes(token)) {
151
- score += 1;
152
- }
153
- }
154
- if (/route|router|controller|handler/.test(normalizedPath)) {
155
- score += 2;
156
- }
157
- if (/service|runtime|daemon|worker/.test(normalizedPath)) {
158
- score += 1;
159
- }
160
- return score;
161
- }
162
-
163
- function countTokenMatches(content, token) {
164
- if (!content || !token) {
165
- return 0;
166
- }
167
- const expression = new RegExp(`\\b${token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\b`, "gi");
168
- const matches = String(content).match(expression);
169
- return matches ? matches.length : 0;
170
- }
171
-
172
- function scoreSemanticContent({ content = "", endpoint = "", tokens = [] } = {}) {
173
- const normalizedContent = String(content || "");
174
- let score = 0;
175
- for (const token of tokens) {
176
- score += Math.min(4, countTokenMatches(normalizedContent, token));
177
- }
178
- if (endpoint && normalizedContent.includes(endpoint)) {
179
- score += 8;
180
- }
181
- if (/(router\.|app\.(get|post|put|patch|delete)|def\s+[a-zA-Z0-9_]+|class\s+[A-Z])/.test(normalizedContent)) {
182
- score += 2;
183
- }
184
- return score;
185
- }
186
-
187
- function resolveSpecifierToIndexedPath(fromPath, specifier, indexedPathsSet) {
188
- const normalizedSpecifier = normalizeString(specifier);
189
- if (!normalizedSpecifier) {
190
- return null;
191
- }
192
- const posixFromPath = toPosixPath(fromPath);
193
- if (normalizedSpecifier.startsWith(".")) {
194
- const fromDir = path.posix.dirname(posixFromPath);
195
- const baseCandidate = path.posix.normalize(path.posix.join(fromDir, normalizedSpecifier));
196
- const directCandidates = [baseCandidate];
197
- for (const extension of LANGUAGE_IMPORT_EXTENSIONS) {
198
- directCandidates.push(`${baseCandidate}${extension}`);
199
- }
200
- for (const extension of LANGUAGE_IMPORT_EXTENSIONS) {
201
- directCandidates.push(path.posix.join(baseCandidate, `index${extension}`));
202
- }
203
- for (const candidate of directCandidates) {
204
- if (indexedPathsSet.has(candidate)) {
205
- return candidate;
206
- }
207
- }
208
- return null;
209
- }
210
- if (/^[a-zA-Z0-9_\.]+$/.test(normalizedSpecifier)) {
211
- const dottedCandidate = normalizedSpecifier.replace(/\./g, "/");
212
- const pythonCandidates = [dottedCandidate, `${dottedCandidate}.py`, `${dottedCandidate}/__init__.py`];
213
- for (const candidate of pythonCandidates) {
214
- if (indexedPathsSet.has(candidate)) {
215
- return candidate;
216
- }
217
- }
218
- }
219
- return null;
220
- }
221
-
222
- async function buildImportGraph({ rootPath, indexedFilesByPath, seedPaths = [], maxDepth = 2 }) {
223
- const indexedPathsSet = new Set(indexedFilesByPath.keys());
224
- const importCache = new Map();
225
- const parserStats = {
226
- astParsedFileCount: 0,
227
- fallbackParsedFileCount: 0,
228
- parseErrorCount: 0,
229
- };
230
- const distances = new Map();
231
- const queue = [];
232
- for (const seed of seedPaths) {
233
- if (!indexedPathsSet.has(seed)) {
234
- continue;
235
- }
236
- if (!distances.has(seed)) {
237
- distances.set(seed, 0);
238
- queue.push(seed);
239
- }
240
- }
241
-
242
- async function getResolvedImports(filePath) {
243
- if (importCache.has(filePath)) {
244
- return importCache.get(filePath);
245
- }
246
- const metadata = indexedFilesByPath.get(filePath);
247
- if (!metadata) {
248
- importCache.set(filePath, []);
249
- return [];
250
- }
251
- const absolutePath = path.join(rootPath, filePath);
252
- let content = "";
253
- try {
254
- content = await fsp.readFile(absolutePath, "utf-8");
255
- } catch {
256
- importCache.set(filePath, []);
257
- return [];
258
- }
259
- const parsed = await parseAstModuleSpecifiers({
260
- absolutePath,
261
- content,
262
- language: metadata.language,
263
- });
264
- const specifiers = Array.isArray(parsed.specifiers) ? parsed.specifiers : [];
265
- if (parsed.parserMode === "babel_ast" || parsed.parserMode === "python_ast") {
266
- parserStats.astParsedFileCount += 1;
267
- } else {
268
- parserStats.fallbackParsedFileCount += 1;
269
- }
270
- if (normalizeString(parsed.parseError)) {
271
- parserStats.parseErrorCount += 1;
272
- }
273
- const resolved = specifiers
274
- .map((specifier) => resolveSpecifierToIndexedPath(filePath, specifier, indexedPathsSet))
275
- .filter(Boolean);
276
- importCache.set(filePath, resolved);
277
- return resolved;
278
- }
279
-
280
- while (queue.length > 0) {
281
- const current = queue.shift();
282
- const distance = distances.get(current) ?? maxDepth + 1;
283
- if (distance >= maxDepth) {
284
- continue;
285
- }
286
- const imports = await getResolvedImports(current);
287
- for (const importedPath of imports) {
288
- const existing = distances.get(importedPath);
289
- if (existing === undefined || distance + 1 < existing) {
290
- distances.set(importedPath, distance + 1);
291
- queue.push(importedPath);
292
- }
293
- }
294
- }
295
-
296
- const edges = [];
297
- for (const source of distances.keys()) {
298
- const imports = await getResolvedImports(source);
299
- for (const target of imports) {
300
- if (distances.has(target)) {
301
- edges.push({
302
- from: source,
303
- to: target,
304
- });
305
- }
306
- }
307
- }
308
-
309
- return {
310
- distances,
311
- edges,
312
- parserStats,
313
- };
314
- }
315
-
316
- function createHybridMapRunId(nowIso, workItemId) {
317
- const normalizedWorkItem = normalizeString(workItemId).replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 48);
318
- return `hybrid-map-${normalizedWorkItem}-${nowIso.replace(/[:.]/g, "-")}`;
319
- }
320
-
321
- function createHybridHandoffRunId(nowIso, workItemId) {
322
- const normalizedWorkItem = normalizeString(workItemId).replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 48);
323
- return `hybrid-handoff-${normalizedWorkItem}-${nowIso.replace(/[:.]/g, "-")}`;
324
- }
325
-
326
- function deriveHandoffBudgets(scopedFiles = []) {
327
- const fileCount = scopedFiles.length;
328
- const totalLoc = scopedFiles.reduce((sum, file) => sum + Math.max(0, Number(file.loc) || 0), 0);
329
- const estimatedInputTokens = Math.max(2000, Math.min(64000, totalLoc * 6 + fileCount * 240));
330
- const maxRuntimeMinutes = Math.max(10, Math.min(180, Math.ceil(totalLoc / 140 + fileCount)));
331
- const maxToolCalls = Math.max(20, Math.min(400, fileCount * 8));
332
- return {
333
- estimatedInputTokens,
334
- maxRuntimeMinutes,
335
- maxToolCalls,
336
- maxFiles: fileCount,
337
- };
338
- }
339
-
340
- function buildHandoffFiles(scopedFiles = [], maxFiles = DEFAULT_HANDOFF_MAX_FILES) {
341
- return scopedFiles.slice(0, maxFiles).map((file, index) => ({
342
- path: file.path,
343
- language: file.language,
344
- loc: file.loc,
345
- totalScore: file.totalScore,
346
- priority: index + 1,
347
- reasons: Array.isArray(file.reasons) ? file.reasons : [],
348
- constraints: {
349
- readOnly: true,
350
- },
351
- }));
352
- }
353
-
354
- export async function resolveHybridMappingStorage({
355
- targetPath = ".",
356
- outputDir = "",
357
- env,
358
- homeDir,
359
- } = {}) {
360
- const daemonStorage = await resolveErrorDaemonStorage({
361
- targetPath,
362
- outputDir,
363
- env,
364
- homeDir,
365
- });
366
- const mappingDir = path.join(daemonStorage.baseDir, "mapping");
367
- return {
368
- ...daemonStorage,
369
- mappingDir,
370
- mapIndexPath: path.join(mappingDir, "hybrid-map-index.json"),
371
- mapEventsPath: path.join(mappingDir, "hybrid-map-events.ndjson"),
372
- mapRunsDir: path.join(mappingDir, "runs"),
373
- handoffIndexPath: path.join(mappingDir, "hybrid-handoff-index.json"),
374
- handoffEventsPath: path.join(mappingDir, "hybrid-handoff-events.ndjson"),
375
- handoffRunsDir: path.join(mappingDir, "handoffs"),
376
- };
377
- }
378
-
379
- export async function buildHybridScopeMap({
380
- targetPath = ".",
381
- outputDir = "",
382
- workItemId,
383
- maxFiles = DEFAULT_MAX_SCOPE_FILES,
384
- graphDepth = DEFAULT_GRAPH_DEPTH,
385
- env,
386
- homeDir,
387
- nowIso = new Date().toISOString(),
388
- } = {}) {
389
- const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
390
- const normalizedWorkItemId = normalizeString(workItemId);
391
- if (!normalizedWorkItemId) {
392
- throw new Error("workItemId is required.");
393
- }
394
- const normalizedMaxFiles = normalizePositiveInteger(maxFiles, "maxFiles", DEFAULT_MAX_SCOPE_FILES);
395
- const normalizedGraphDepth = normalizePositiveInteger(
396
- graphDepth,
397
- "graphDepth",
398
- DEFAULT_GRAPH_DEPTH
399
- );
400
-
401
- const storage = await resolveHybridMappingStorage({
402
- targetPath,
403
- outputDir,
404
- env,
405
- homeDir,
406
- });
407
- const queue = await listErrorQueue({
408
- targetPath,
409
- outputDir,
410
- limit: 5000,
411
- env,
412
- homeDir,
413
- });
414
- const queueItem = queue.items.find((item) => normalizeString(item.workItemId) === normalizedWorkItemId);
415
- if (!queueItem) {
416
- throw new Error(`Work item '${normalizedWorkItemId}' was not found in daemon queue.`);
417
- }
418
-
419
- const ingest = await collectCodebaseIngest({ rootPath: targetPath });
420
- const indexedFiles = Array.isArray(ingest.indexedFiles?.files) ? ingest.indexedFiles.files : [];
421
- const indexedFilesByPath = new Map();
422
- for (const file of indexedFiles) {
423
- const filePath = toPosixPath(file.path);
424
- if (!filePath) {
425
- continue;
426
- }
427
- indexedFilesByPath.set(filePath, {
428
- ...file,
429
- path: filePath,
430
- });
431
- }
432
-
433
- const tokens = tokenizeWorkItem(queueItem);
434
- const deterministicCandidates = [];
435
- for (const file of indexedFilesByPath.values()) {
436
- const deterministicScore = scoreDeterministicPath(file.path, tokens);
437
- if (deterministicScore <= 0) {
438
- continue;
439
- }
440
- deterministicCandidates.push({
441
- path: file.path,
442
- deterministicScore,
443
- });
444
- }
445
- deterministicCandidates.sort((left, right) => {
446
- if (right.deterministicScore !== left.deterministicScore) {
447
- return right.deterministicScore - left.deterministicScore;
448
- }
449
- return left.path.localeCompare(right.path);
450
- });
451
-
452
- const seedPaths = deterministicCandidates.slice(0, 20).map((candidate) => candidate.path);
453
- const importGraph = await buildImportGraph({
454
- rootPath: targetPath,
455
- indexedFilesByPath,
456
- seedPaths,
457
- maxDepth: normalizedGraphDepth,
458
- });
459
-
460
- const deterministicScoreMap = new Map(
461
- deterministicCandidates.map((candidate) => [candidate.path, candidate.deterministicScore])
462
- );
463
- const selectedPaths = new Set([...importGraph.distances.keys(), ...seedPaths]);
464
- const scoredFiles = [];
465
- for (const filePath of selectedPaths) {
466
- const metadata = indexedFilesByPath.get(filePath);
467
- if (!metadata) {
468
- continue;
469
- }
470
- const absolutePath = path.join(targetPath, filePath);
471
- let content = "";
472
- try {
473
- content = await fsp.readFile(absolutePath, "utf-8");
474
- } catch {
475
- content = "";
476
- }
477
- const deterministicScore = deterministicScoreMap.get(filePath) || 0;
478
- const semanticScore = scoreSemanticContent({
479
- content,
480
- endpoint: normalizeString(queueItem.endpoint),
481
- tokens,
482
- });
483
- const graphDistance = importGraph.distances.get(filePath);
484
- const graphScore =
485
- graphDistance === undefined ? 0 : Math.max(1, normalizedGraphDepth - graphDistance + 1);
486
- const totalScore = deterministicScore * 3 + semanticScore + graphScore;
487
- const reasons = [];
488
- if (deterministicScore > 0) {
489
- reasons.push(`deterministic_path_match:${deterministicScore}`);
490
- }
491
- if (semanticScore > 0) {
492
- reasons.push(`semantic_content_match:${semanticScore}`);
493
- }
494
- if (graphDistance !== undefined) {
495
- reasons.push(`import_graph_distance:${graphDistance}`);
496
- }
497
- scoredFiles.push({
498
- path: filePath,
499
- language: metadata.language,
500
- loc: metadata.loc,
501
- sizeBytes: metadata.sizeBytes,
502
- deterministicScore,
503
- semanticScore,
504
- graphDistance: graphDistance === undefined ? null : graphDistance,
505
- graphScore,
506
- totalScore,
507
- reasons,
508
- });
509
- }
510
-
511
- scoredFiles.sort((left, right) => {
512
- if (right.totalScore !== left.totalScore) {
513
- return right.totalScore - left.totalScore;
514
- }
515
- return left.path.localeCompare(right.path);
516
- });
517
-
518
- const scopedFiles = scoredFiles.slice(0, normalizedMaxFiles);
519
- const scopedPathSet = new Set(scopedFiles.map((file) => file.path));
520
- const scopedEdges = importGraph.edges.filter(
521
- (edge) => scopedPathSet.has(edge.from) && scopedPathSet.has(edge.to)
522
- );
523
- const callGraphOverlay = await buildCallgraphOverlay({
524
- rootPath: targetPath,
525
- indexedFilesByPath,
526
- scopedPaths: [...scopedPathSet],
527
- });
528
-
529
- const runId = createHybridMapRunId(normalizedNow, normalizedWorkItemId);
530
- const runPath = path.join(storage.mapRunsDir, `${runId}.json`);
531
- const runPayload = {
532
- schemaVersion: HYBRID_MAP_SCHEMA_VERSION,
533
- generatedAt: normalizedNow,
534
- runId,
535
- workItem: {
536
- workItemId: queueItem.workItemId,
537
- severity: queueItem.severity,
538
- status: queueItem.status,
539
- service: queueItem.service,
540
- endpoint: queueItem.endpoint,
541
- errorCode: queueItem.errorCode,
542
- message: queueItem.message,
543
- },
544
- strategy: {
545
- mode: "hybrid_deterministic_ast_semantic_overlay",
546
- tokenizedSignals: tokens,
547
- deterministicSeeds: deterministicCandidates.slice(0, 20),
548
- graphDepth: normalizedGraphDepth,
549
- maxFiles: normalizedMaxFiles,
550
- astParser: importGraph.parserStats,
551
- callGraphOverlay: callGraphOverlay.summary,
552
- },
553
- summary: {
554
- indexedFileCount: indexedFilesByPath.size,
555
- deterministicCandidateCount: deterministicCandidates.length,
556
- graphNodeCount: importGraph.distances.size,
557
- graphEdgeCount: importGraph.edges.length,
558
- scopedFileCount: scopedFiles.length,
559
- astParsedFileCount: importGraph.parserStats.astParsedFileCount,
560
- fallbackParsedFileCount: importGraph.parserStats.fallbackParsedFileCount,
561
- astParseErrorCount: importGraph.parserStats.parseErrorCount,
562
- callGraphNodeCount: callGraphOverlay.summary.nodeCount,
563
- callGraphEdgeCount: callGraphOverlay.summary.edgeCount,
564
- callGraphParsedFileCount: callGraphOverlay.summary.parsedFileCount,
565
- callGraphFallbackParsedFileCount: callGraphOverlay.summary.fallbackParsedFileCount,
566
- callGraphParseErrorCount: callGraphOverlay.summary.parseErrorCount,
567
- },
568
- scopedFiles,
569
- importGraph: {
570
- nodes: [...scopedPathSet].sort((left, right) => left.localeCompare(right)),
571
- edges: scopedEdges,
572
- },
573
- callGraph: {
574
- nodes: callGraphOverlay.nodes,
575
- edges: callGraphOverlay.edges,
576
- parsedFiles: callGraphOverlay.parsedFiles,
577
- },
578
- };
579
-
580
- await fsp.mkdir(storage.mapRunsDir, { recursive: true });
581
- await writeJsonFile(runPath, runPayload);
582
-
583
- const rawIndex = await readJsonFile(storage.mapIndexPath, () => createInitialMapIndex(normalizedNow));
584
- const index = normalizeMapIndex(rawIndex, normalizedNow);
585
- index.generatedAt = normalizedNow;
586
- index.maps = [
587
- {
588
- workItemId: queueItem.workItemId,
589
- runId,
590
- generatedAt: normalizedNow,
591
- mapPath: toPosixPath(path.relative(storage.outputRoot, runPath)),
592
- service: queueItem.service,
593
- endpoint: queueItem.endpoint,
594
- errorCode: queueItem.errorCode,
595
- status: queueItem.status,
596
- tokenizedSignals: tokens,
597
- deterministicSeedCount: deterministicCandidates.slice(0, 20).length,
598
- scopedFileCount: scopedFiles.length,
599
- },
600
- ...index.maps.filter((entry) => entry.runId !== runId),
601
- ].slice(0, 2000);
602
- await Promise.all([
603
- writeJsonFile(storage.mapIndexPath, index),
604
- appendJsonLine(storage.mapEventsPath, {
605
- timestamp: normalizedNow,
606
- eventType: "hybrid_scope_map",
607
- runId,
608
- workItemId: queueItem.workItemId,
609
- deterministicSeedCount: deterministicCandidates.slice(0, 20).length,
610
- graphNodeCount: importGraph.distances.size,
611
- graphEdgeCount: importGraph.edges.length,
612
- scopedFileCount: scopedFiles.length,
613
- astParsedFileCount: importGraph.parserStats.astParsedFileCount,
614
- fallbackParsedFileCount: importGraph.parserStats.fallbackParsedFileCount,
615
- astParseErrorCount: importGraph.parserStats.parseErrorCount,
616
- callGraphNodeCount: callGraphOverlay.summary.nodeCount,
617
- callGraphEdgeCount: callGraphOverlay.summary.edgeCount,
618
- callGraphParsedFileCount: callGraphOverlay.summary.parsedFileCount,
619
- callGraphFallbackParsedFileCount: callGraphOverlay.summary.fallbackParsedFileCount,
620
- callGraphParseErrorCount: callGraphOverlay.summary.parseErrorCount,
621
- }),
622
- ]);
623
-
624
- return {
625
- ...storage,
626
- runId,
627
- runPath,
628
- summary: runPayload.summary,
629
- strategy: runPayload.strategy,
630
- scopedFiles: runPayload.scopedFiles,
631
- importGraph: runPayload.importGraph,
632
- callGraph: runPayload.callGraph,
633
- workItem: runPayload.workItem,
634
- };
635
- }
636
-
637
- export async function listHybridScopeMaps({
638
- targetPath = ".",
639
- outputDir = "",
640
- workItemId = "",
641
- limit = 50,
642
- env,
643
- homeDir,
644
- nowIso = new Date().toISOString(),
645
- } = {}) {
646
- const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
647
- const normalizedLimit = normalizePositiveInteger(limit, "limit", 50);
648
- const normalizedWorkItemId = normalizeString(workItemId);
649
- const storage = await resolveHybridMappingStorage({
650
- targetPath,
651
- outputDir,
652
- env,
653
- homeDir,
654
- });
655
- const rawIndex = await readJsonFile(storage.mapIndexPath, () => createInitialMapIndex(normalizedNow));
656
- const index = normalizeMapIndex(rawIndex, normalizedNow);
657
- const filtered = index.maps
658
- .filter((entry) => {
659
- if (normalizedWorkItemId && normalizeString(entry.workItemId) !== normalizedWorkItemId) {
660
- return false;
661
- }
662
- return true;
663
- })
664
- .sort((left, right) => {
665
- const leftEpoch = Date.parse(String(left.generatedAt || "")) || 0;
666
- const rightEpoch = Date.parse(String(right.generatedAt || "")) || 0;
667
- return rightEpoch - leftEpoch;
668
- });
669
- return {
670
- ...storage,
671
- generatedAt: index.generatedAt,
672
- totalCount: index.maps.length,
673
- visibleCount: filtered.length,
674
- maps: filtered.slice(0, normalizedLimit),
675
- };
676
- }
677
-
678
- export async function showHybridScopeMap({
679
- targetPath = ".",
680
- outputDir = "",
681
- workItemId = "",
682
- runId = "",
683
- env,
684
- homeDir,
685
- nowIso = new Date().toISOString(),
686
- } = {}) {
687
- const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
688
- const storage = await resolveHybridMappingStorage({
689
- targetPath,
690
- outputDir,
691
- env,
692
- homeDir,
693
- });
694
- const listed = await listHybridScopeMaps({
695
- targetPath,
696
- outputDir,
697
- workItemId,
698
- limit: 500,
699
- env,
700
- homeDir,
701
- nowIso: normalizedNow,
702
- });
703
- const normalizedRunId = normalizeString(runId);
704
- const selected = listed.maps.find((entry) => {
705
- if (normalizedRunId && normalizeString(entry.runId) !== normalizedRunId) {
706
- return false;
707
- }
708
- if (workItemId && normalizeString(entry.workItemId) !== normalizeString(workItemId)) {
709
- return false;
710
- }
711
- return true;
712
- });
713
- if (!selected) {
714
- throw new Error(
715
- `No hybrid scope map found for work item '${normalizeString(workItemId) || "n/a"}' and run '${normalizedRunId || "latest"}'.`
716
- );
717
- }
718
- const absoluteMapPath = path.join(storage.outputRoot, selected.mapPath);
719
- const payload = await readJsonFile(absoluteMapPath, () => null);
720
- if (!payload || typeof payload !== "object") {
721
- throw new Error(`Hybrid scope map artifact not found: ${absoluteMapPath}`);
722
- }
723
- return {
724
- ...storage,
725
- map: selected,
726
- mapPath: absoluteMapPath,
727
- payload,
728
- };
729
- }
730
-
731
- export async function buildHybridHandoffPackage({
732
- targetPath = ".",
733
- outputDir = "",
734
- workItemId,
735
- mapRunId = "",
736
- assignee = "omar",
737
- maxFiles = DEFAULT_HANDOFF_MAX_FILES,
738
- env,
739
- homeDir,
740
- nowIso = new Date().toISOString(),
741
- } = {}) {
742
- const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
743
- const normalizedWorkItemId = normalizeString(workItemId);
744
- if (!normalizedWorkItemId) {
745
- throw new Error("workItemId is required.");
746
- }
747
- const normalizedMaxFiles = normalizePositiveInteger(maxFiles, "maxFiles", DEFAULT_HANDOFF_MAX_FILES);
748
- const normalizedAssignee = normalizeString(assignee) || "omar";
749
- const storage = await resolveHybridMappingStorage({
750
- targetPath,
751
- outputDir,
752
- env,
753
- homeDir,
754
- });
755
- const shown = await showHybridScopeMap({
756
- targetPath,
757
- outputDir,
758
- workItemId: normalizedWorkItemId,
759
- runId: mapRunId,
760
- env,
761
- homeDir,
762
- nowIso: normalizedNow,
763
- });
764
- const mapPayload = shown.payload;
765
- const handoffRunId = createHybridHandoffRunId(normalizedNow, normalizedWorkItemId);
766
- const handoffPath = path.join(storage.handoffRunsDir, `${handoffRunId}.json`);
767
- const handoffFiles = buildHandoffFiles(
768
- Array.isArray(mapPayload.scopedFiles) ? mapPayload.scopedFiles : [],
769
- normalizedMaxFiles
770
- );
771
- const budgets = deriveHandoffBudgets(handoffFiles);
772
- const payload = {
773
- schemaVersion: HYBRID_HANDOFF_SCHEMA_VERSION,
774
- generatedAt: normalizedNow,
775
- handoffRunId,
776
- sourceMapRunId: normalizeString(mapPayload.runId),
777
- workItem: mapPayload.workItem,
778
- assignee: {
779
- primary: normalizedAssignee,
780
- },
781
- constraints: {
782
- allowedPaths: handoffFiles.map((file) => file.path),
783
- readOnly: true,
784
- },
785
- budgets,
786
- context: {
787
- strategy: mapPayload.strategy,
788
- summary: mapPayload.summary,
789
- importGraph: mapPayload.importGraph,
790
- callGraph: mapPayload.callGraph,
791
- },
792
- files: handoffFiles,
793
- };
794
- await fsp.mkdir(storage.handoffRunsDir, { recursive: true });
795
- await writeJsonFile(handoffPath, payload);
796
-
797
- const rawIndex = await readJsonFile(storage.handoffIndexPath, () => createInitialHandoffIndex(normalizedNow));
798
- const index = normalizeHandoffIndex(rawIndex, normalizedNow);
799
- index.generatedAt = normalizedNow;
800
- index.handoffs = [
801
- {
802
- workItemId: normalizedWorkItemId,
803
- handoffRunId,
804
- mapRunId: normalizeString(mapPayload.runId),
805
- generatedAt: normalizedNow,
806
- handoffPath: toPosixPath(path.relative(storage.outputRoot, handoffPath)),
807
- assignee: normalizedAssignee,
808
- scopedFileCount: handoffFiles.length,
809
- estimatedInputTokens: budgets.estimatedInputTokens,
810
- },
811
- ...index.handoffs.filter((entry) => normalizeString(entry.handoffRunId) !== handoffRunId),
812
- ].slice(0, 2000);
813
-
814
- await Promise.all([
815
- writeJsonFile(storage.handoffIndexPath, index),
816
- appendJsonLine(storage.handoffEventsPath, {
817
- timestamp: normalizedNow,
818
- eventType: "hybrid_handoff_package",
819
- handoffRunId,
820
- mapRunId: normalizeString(mapPayload.runId),
821
- workItemId: normalizedWorkItemId,
822
- assignee: normalizedAssignee,
823
- scopedFileCount: handoffFiles.length,
824
- estimatedInputTokens: budgets.estimatedInputTokens,
825
- }),
826
- ]);
827
-
828
- return {
829
- ...storage,
830
- handoffRunId,
831
- handoffPath,
832
- summary: {
833
- scopedFileCount: handoffFiles.length,
834
- estimatedInputTokens: budgets.estimatedInputTokens,
835
- maxRuntimeMinutes: budgets.maxRuntimeMinutes,
836
- maxToolCalls: budgets.maxToolCalls,
837
- },
838
- payload,
839
- };
840
- }
841
-
842
- export async function listHybridHandoffs({
843
- targetPath = ".",
844
- outputDir = "",
845
- workItemId = "",
846
- limit = 50,
847
- env,
848
- homeDir,
849
- nowIso = new Date().toISOString(),
850
- } = {}) {
851
- const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
852
- const normalizedLimit = normalizePositiveInteger(limit, "limit", 50);
853
- const normalizedWorkItemId = normalizeString(workItemId);
854
- const storage = await resolveHybridMappingStorage({
855
- targetPath,
856
- outputDir,
857
- env,
858
- homeDir,
859
- });
860
- const rawIndex = await readJsonFile(storage.handoffIndexPath, () => createInitialHandoffIndex(normalizedNow));
861
- const index = normalizeHandoffIndex(rawIndex, normalizedNow);
862
- const filtered = index.handoffs
863
- .filter((entry) => {
864
- if (normalizedWorkItemId && normalizeString(entry.workItemId) !== normalizedWorkItemId) {
865
- return false;
866
- }
867
- return true;
868
- })
869
- .sort((left, right) => {
870
- const leftEpoch = Date.parse(String(left.generatedAt || "")) || 0;
871
- const rightEpoch = Date.parse(String(right.generatedAt || "")) || 0;
872
- return rightEpoch - leftEpoch;
873
- });
874
- return {
875
- ...storage,
876
- generatedAt: index.generatedAt,
877
- totalCount: index.handoffs.length,
878
- visibleCount: filtered.length,
879
- handoffs: filtered.slice(0, normalizedLimit),
880
- };
881
- }
882
-
883
- export async function showHybridHandoff({
884
- targetPath = ".",
885
- outputDir = "",
886
- workItemId = "",
887
- handoffRunId = "",
888
- env,
889
- homeDir,
890
- nowIso = new Date().toISOString(),
891
- } = {}) {
892
- const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
893
- const normalizedWorkItemId = normalizeString(workItemId);
894
- if (!normalizedWorkItemId) {
895
- throw new Error("workItemId is required.");
896
- }
897
- const listed = await listHybridHandoffs({
898
- targetPath,
899
- outputDir,
900
- workItemId: normalizedWorkItemId,
901
- limit: 500,
902
- env,
903
- homeDir,
904
- nowIso: normalizedNow,
905
- });
906
- const normalizedRunId = normalizeString(handoffRunId);
907
- const selected = listed.handoffs.find((entry) => {
908
- if (normalizedRunId && normalizeString(entry.handoffRunId) !== normalizedRunId) {
909
- return false;
910
- }
911
- return true;
912
- });
913
- if (!selected) {
914
- throw new Error(
915
- `No hybrid handoff package found for work item '${normalizedWorkItemId}' and run '${normalizedRunId || "latest"}'.`
916
- );
917
- }
918
- const absoluteHandoffPath = path.join(listed.outputRoot, selected.handoffPath);
919
- const payload = await readJsonFile(absoluteHandoffPath, () => null);
920
- if (!payload || typeof payload !== "object") {
921
- throw new Error(`Hybrid handoff artifact not found: ${absoluteHandoffPath}`);
922
- }
923
- return {
924
- ...listed,
925
- handoff: selected,
926
- handoffPath: absoluteHandoffPath,
927
- payload,
928
- };
929
- }
1
+ import fsp from "node:fs/promises";
2
+ import path from "node:path";
3
+
4
+ import { parseAstModuleSpecifiers } from "./ast-parser-layer.js";
5
+ import { buildCallgraphOverlay } from "./callgraph-overlay.js";
6
+ import { collectCodebaseIngest } from "../ingest/engine.js";
7
+ import { listErrorQueue, resolveErrorDaemonStorage } from "./error-worker.js";
8
+
9
+ const HYBRID_MAP_SCHEMA_VERSION = "1.0.0";
10
+ const HYBRID_HANDOFF_SCHEMA_VERSION = "1.0.0";
11
+ const DEFAULT_MAX_SCOPE_FILES = 40;
12
+ const DEFAULT_GRAPH_DEPTH = 2;
13
+ const DEFAULT_HANDOFF_MAX_FILES = 24;
14
+ const LANGUAGE_IMPORT_EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".py"];
15
+
16
+ function normalizeString(value) {
17
+ return String(value || "").trim();
18
+ }
19
+
20
+ function normalizeIsoTimestamp(value, fallbackIso = new Date().toISOString()) {
21
+ const normalized = normalizeString(value);
22
+ if (!normalized) {
23
+ return fallbackIso;
24
+ }
25
+ const epoch = Date.parse(normalized);
26
+ if (!Number.isFinite(epoch)) {
27
+ return fallbackIso;
28
+ }
29
+ return new Date(epoch).toISOString();
30
+ }
31
+
32
+ function normalizePositiveInteger(value, fieldName, fallbackValue) {
33
+ if (value === undefined || value === null || normalizeString(value) === "") {
34
+ return fallbackValue;
35
+ }
36
+ const normalized = Number(value);
37
+ if (!Number.isFinite(normalized) || normalized <= 0) {
38
+ throw new Error(`${fieldName} must be a positive integer.`);
39
+ }
40
+ return Math.floor(normalized);
41
+ }
42
+
43
+ function toPosixPath(value = "") {
44
+ return String(value || "").replace(/\\/g, "/");
45
+ }
46
+
47
+ function normalizeMapIndex(index = {}, nowIso = new Date().toISOString()) {
48
+ return {
49
+ schemaVersion: HYBRID_MAP_SCHEMA_VERSION,
50
+ generatedAt: normalizeIsoTimestamp(index.generatedAt, nowIso),
51
+ maps: Array.isArray(index.maps)
52
+ ? index.maps
53
+ .map((entry) => ({
54
+ ...entry,
55
+ workItemId: normalizeString(entry.workItemId),
56
+ runId: normalizeString(entry.runId),
57
+ generatedAt: normalizeIsoTimestamp(entry.generatedAt, nowIso),
58
+ mapPath: normalizeString(entry.mapPath),
59
+ }))
60
+ .filter((entry) => entry.workItemId && entry.runId && entry.mapPath)
61
+ : [],
62
+ };
63
+ }
64
+
65
+ function createInitialMapIndex(nowIso = new Date().toISOString()) {
66
+ return {
67
+ schemaVersion: HYBRID_MAP_SCHEMA_VERSION,
68
+ generatedAt: normalizeIsoTimestamp(nowIso, nowIso),
69
+ maps: [],
70
+ };
71
+ }
72
+
73
+ function normalizeHandoffIndex(index = {}, nowIso = new Date().toISOString()) {
74
+ return {
75
+ schemaVersion: HYBRID_HANDOFF_SCHEMA_VERSION,
76
+ generatedAt: normalizeIsoTimestamp(index.generatedAt, nowIso),
77
+ handoffs: Array.isArray(index.handoffs)
78
+ ? index.handoffs
79
+ .map((entry) => ({
80
+ ...entry,
81
+ workItemId: normalizeString(entry.workItemId),
82
+ handoffRunId: normalizeString(entry.handoffRunId),
83
+ mapRunId: normalizeString(entry.mapRunId),
84
+ generatedAt: normalizeIsoTimestamp(entry.generatedAt, nowIso),
85
+ handoffPath: normalizeString(entry.handoffPath),
86
+ assignee: normalizeString(entry.assignee),
87
+ }))
88
+ .filter((entry) => entry.workItemId && entry.handoffRunId && entry.handoffPath)
89
+ : [],
90
+ };
91
+ }
92
+
93
+ function createInitialHandoffIndex(nowIso = new Date().toISOString()) {
94
+ return {
95
+ schemaVersion: HYBRID_HANDOFF_SCHEMA_VERSION,
96
+ generatedAt: normalizeIsoTimestamp(nowIso, nowIso),
97
+ handoffs: [],
98
+ };
99
+ }
100
+
101
+ async function readJsonFile(filePath, defaultFactory) {
102
+ try {
103
+ const raw = await fsp.readFile(filePath, "utf-8");
104
+ return JSON.parse(raw);
105
+ } catch (error) {
106
+ if (error && typeof error === "object" && error.code === "ENOENT") {
107
+ return defaultFactory();
108
+ }
109
+ throw error;
110
+ }
111
+ }
112
+
113
+ async function writeJsonFile(filePath, payload = {}) {
114
+ await fsp.mkdir(path.dirname(filePath), { recursive: true });
115
+ await fsp.writeFile(filePath, `${JSON.stringify(payload, null, 2)}\n`, "utf-8");
116
+ }
117
+
118
+ async function appendJsonLine(filePath, payload = {}) {
119
+ await fsp.mkdir(path.dirname(filePath), { recursive: true });
120
+ await fsp.appendFile(filePath, `${JSON.stringify(payload)}\n`, "utf-8");
121
+ }
122
+
123
+ function tokenizeWorkItem(queueItem = {}) {
124
+ const parts = [
125
+ normalizeString(queueItem.endpoint),
126
+ normalizeString(queueItem.errorCode),
127
+ normalizeString(queueItem.service),
128
+ ]
129
+ .join("/")
130
+ .split(/[^a-zA-Z0-9]+/)
131
+ .map((token) => token.toLowerCase())
132
+ .filter(Boolean);
133
+ const unique = new Set();
134
+ for (const token of parts) {
135
+ if (token.length < 3) {
136
+ continue;
137
+ }
138
+ if (["api", "v1", "v2", "err", "error", "svc", "service"].includes(token)) {
139
+ continue;
140
+ }
141
+ unique.add(token);
142
+ }
143
+ return [...unique].slice(0, 24);
144
+ }
145
+
146
+ function scoreDeterministicPath(pathText, tokens = []) {
147
+ const normalizedPath = normalizeString(pathText).toLowerCase();
148
+ let score = 0;
149
+ for (const token of tokens) {
150
+ if (normalizedPath.includes(token)) {
151
+ score += 1;
152
+ }
153
+ }
154
+ if (/route|router|controller|handler/.test(normalizedPath)) {
155
+ score += 2;
156
+ }
157
+ if (/service|runtime|daemon|worker/.test(normalizedPath)) {
158
+ score += 1;
159
+ }
160
+ return score;
161
+ }
162
+
163
+ function countTokenMatches(content, token) {
164
+ if (!content || !token) {
165
+ return 0;
166
+ }
167
+ const expression = new RegExp(`\\b${token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\b`, "gi");
168
+ const matches = String(content).match(expression);
169
+ return matches ? matches.length : 0;
170
+ }
171
+
172
+ function scoreSemanticContent({ content = "", endpoint = "", tokens = [] } = {}) {
173
+ const normalizedContent = String(content || "");
174
+ let score = 0;
175
+ for (const token of tokens) {
176
+ score += Math.min(4, countTokenMatches(normalizedContent, token));
177
+ }
178
+ if (endpoint && normalizedContent.includes(endpoint)) {
179
+ score += 8;
180
+ }
181
+ if (/(router\.|app\.(get|post|put|patch|delete)|def\s+[a-zA-Z0-9_]+|class\s+[A-Z])/.test(normalizedContent)) {
182
+ score += 2;
183
+ }
184
+ return score;
185
+ }
186
+
187
+ function resolveSpecifierToIndexedPath(fromPath, specifier, indexedPathsSet) {
188
+ const normalizedSpecifier = normalizeString(specifier);
189
+ if (!normalizedSpecifier) {
190
+ return null;
191
+ }
192
+ const posixFromPath = toPosixPath(fromPath);
193
+ if (normalizedSpecifier.startsWith(".")) {
194
+ const fromDir = path.posix.dirname(posixFromPath);
195
+ const baseCandidate = path.posix.normalize(path.posix.join(fromDir, normalizedSpecifier));
196
+ const directCandidates = [baseCandidate];
197
+ for (const extension of LANGUAGE_IMPORT_EXTENSIONS) {
198
+ directCandidates.push(`${baseCandidate}${extension}`);
199
+ }
200
+ for (const extension of LANGUAGE_IMPORT_EXTENSIONS) {
201
+ directCandidates.push(path.posix.join(baseCandidate, `index${extension}`));
202
+ }
203
+ for (const candidate of directCandidates) {
204
+ if (indexedPathsSet.has(candidate)) {
205
+ return candidate;
206
+ }
207
+ }
208
+ return null;
209
+ }
210
+ if (/^[a-zA-Z0-9_\.]+$/.test(normalizedSpecifier)) {
211
+ const dottedCandidate = normalizedSpecifier.replace(/\./g, "/");
212
+ const pythonCandidates = [dottedCandidate, `${dottedCandidate}.py`, `${dottedCandidate}/__init__.py`];
213
+ for (const candidate of pythonCandidates) {
214
+ if (indexedPathsSet.has(candidate)) {
215
+ return candidate;
216
+ }
217
+ }
218
+ }
219
+ return null;
220
+ }
221
+
222
+ async function buildImportGraph({ rootPath, indexedFilesByPath, seedPaths = [], maxDepth = 2 }) {
223
+ const indexedPathsSet = new Set(indexedFilesByPath.keys());
224
+ const importCache = new Map();
225
+ const parserStats = {
226
+ astParsedFileCount: 0,
227
+ fallbackParsedFileCount: 0,
228
+ parseErrorCount: 0,
229
+ };
230
+ const distances = new Map();
231
+ const queue = [];
232
+ for (const seed of seedPaths) {
233
+ if (!indexedPathsSet.has(seed)) {
234
+ continue;
235
+ }
236
+ if (!distances.has(seed)) {
237
+ distances.set(seed, 0);
238
+ queue.push(seed);
239
+ }
240
+ }
241
+
242
+ async function getResolvedImports(filePath) {
243
+ if (importCache.has(filePath)) {
244
+ return importCache.get(filePath);
245
+ }
246
+ const metadata = indexedFilesByPath.get(filePath);
247
+ if (!metadata) {
248
+ importCache.set(filePath, []);
249
+ return [];
250
+ }
251
+ const absolutePath = path.join(rootPath, filePath);
252
+ let content = "";
253
+ try {
254
+ content = await fsp.readFile(absolutePath, "utf-8");
255
+ } catch {
256
+ importCache.set(filePath, []);
257
+ return [];
258
+ }
259
+ const parsed = await parseAstModuleSpecifiers({
260
+ absolutePath,
261
+ content,
262
+ language: metadata.language,
263
+ });
264
+ const specifiers = Array.isArray(parsed.specifiers) ? parsed.specifiers : [];
265
+ if (parsed.parserMode === "babel_ast" || parsed.parserMode === "python_ast") {
266
+ parserStats.astParsedFileCount += 1;
267
+ } else {
268
+ parserStats.fallbackParsedFileCount += 1;
269
+ }
270
+ if (normalizeString(parsed.parseError)) {
271
+ parserStats.parseErrorCount += 1;
272
+ }
273
+ const resolved = specifiers
274
+ .map((specifier) => resolveSpecifierToIndexedPath(filePath, specifier, indexedPathsSet))
275
+ .filter(Boolean);
276
+ importCache.set(filePath, resolved);
277
+ return resolved;
278
+ }
279
+
280
+ while (queue.length > 0) {
281
+ const current = queue.shift();
282
+ const distance = distances.get(current) ?? maxDepth + 1;
283
+ if (distance >= maxDepth) {
284
+ continue;
285
+ }
286
+ const imports = await getResolvedImports(current);
287
+ for (const importedPath of imports) {
288
+ const existing = distances.get(importedPath);
289
+ if (existing === undefined || distance + 1 < existing) {
290
+ distances.set(importedPath, distance + 1);
291
+ queue.push(importedPath);
292
+ }
293
+ }
294
+ }
295
+
296
+ const edges = [];
297
+ for (const source of distances.keys()) {
298
+ const imports = await getResolvedImports(source);
299
+ for (const target of imports) {
300
+ if (distances.has(target)) {
301
+ edges.push({
302
+ from: source,
303
+ to: target,
304
+ });
305
+ }
306
+ }
307
+ }
308
+
309
+ return {
310
+ distances,
311
+ edges,
312
+ parserStats,
313
+ };
314
+ }
315
+
316
+ function createHybridMapRunId(nowIso, workItemId) {
317
+ const normalizedWorkItem = normalizeString(workItemId).replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 48);
318
+ return `hybrid-map-${normalizedWorkItem}-${nowIso.replace(/[:.]/g, "-")}`;
319
+ }
320
+
321
+ function createHybridHandoffRunId(nowIso, workItemId) {
322
+ const normalizedWorkItem = normalizeString(workItemId).replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 48);
323
+ return `hybrid-handoff-${normalizedWorkItem}-${nowIso.replace(/[:.]/g, "-")}`;
324
+ }
325
+
326
+ function deriveHandoffBudgets(scopedFiles = []) {
327
+ const fileCount = scopedFiles.length;
328
+ const totalLoc = scopedFiles.reduce((sum, file) => sum + Math.max(0, Number(file.loc) || 0), 0);
329
+ const estimatedInputTokens = Math.max(2000, Math.min(64000, totalLoc * 6 + fileCount * 240));
330
+ const maxRuntimeMinutes = Math.max(10, Math.min(180, Math.ceil(totalLoc / 140 + fileCount)));
331
+ const maxToolCalls = Math.max(20, Math.min(400, fileCount * 8));
332
+ return {
333
+ estimatedInputTokens,
334
+ maxRuntimeMinutes,
335
+ maxToolCalls,
336
+ maxFiles: fileCount,
337
+ };
338
+ }
339
+
340
+ function buildHandoffFiles(scopedFiles = [], maxFiles = DEFAULT_HANDOFF_MAX_FILES) {
341
+ return scopedFiles.slice(0, maxFiles).map((file, index) => ({
342
+ path: file.path,
343
+ language: file.language,
344
+ loc: file.loc,
345
+ totalScore: file.totalScore,
346
+ priority: index + 1,
347
+ reasons: Array.isArray(file.reasons) ? file.reasons : [],
348
+ constraints: {
349
+ readOnly: true,
350
+ },
351
+ }));
352
+ }
353
+
354
+ export async function resolveHybridMappingStorage({
355
+ targetPath = ".",
356
+ outputDir = "",
357
+ env,
358
+ homeDir,
359
+ } = {}) {
360
+ const daemonStorage = await resolveErrorDaemonStorage({
361
+ targetPath,
362
+ outputDir,
363
+ env,
364
+ homeDir,
365
+ });
366
+ const mappingDir = path.join(daemonStorage.baseDir, "mapping");
367
+ return {
368
+ ...daemonStorage,
369
+ mappingDir,
370
+ mapIndexPath: path.join(mappingDir, "hybrid-map-index.json"),
371
+ mapEventsPath: path.join(mappingDir, "hybrid-map-events.ndjson"),
372
+ mapRunsDir: path.join(mappingDir, "runs"),
373
+ handoffIndexPath: path.join(mappingDir, "hybrid-handoff-index.json"),
374
+ handoffEventsPath: path.join(mappingDir, "hybrid-handoff-events.ndjson"),
375
+ handoffRunsDir: path.join(mappingDir, "handoffs"),
376
+ };
377
+ }
378
+
379
+ export async function buildHybridScopeMap({
380
+ targetPath = ".",
381
+ outputDir = "",
382
+ workItemId,
383
+ maxFiles = DEFAULT_MAX_SCOPE_FILES,
384
+ graphDepth = DEFAULT_GRAPH_DEPTH,
385
+ env,
386
+ homeDir,
387
+ nowIso = new Date().toISOString(),
388
+ } = {}) {
389
+ const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
390
+ const normalizedWorkItemId = normalizeString(workItemId);
391
+ if (!normalizedWorkItemId) {
392
+ throw new Error("workItemId is required.");
393
+ }
394
+ const normalizedMaxFiles = normalizePositiveInteger(maxFiles, "maxFiles", DEFAULT_MAX_SCOPE_FILES);
395
+ const normalizedGraphDepth = normalizePositiveInteger(
396
+ graphDepth,
397
+ "graphDepth",
398
+ DEFAULT_GRAPH_DEPTH
399
+ );
400
+
401
+ const storage = await resolveHybridMappingStorage({
402
+ targetPath,
403
+ outputDir,
404
+ env,
405
+ homeDir,
406
+ });
407
+ const queue = await listErrorQueue({
408
+ targetPath,
409
+ outputDir,
410
+ limit: 5000,
411
+ env,
412
+ homeDir,
413
+ });
414
+ const queueItem = queue.items.find((item) => normalizeString(item.workItemId) === normalizedWorkItemId);
415
+ if (!queueItem) {
416
+ throw new Error(`Work item '${normalizedWorkItemId}' was not found in daemon queue.`);
417
+ }
418
+
419
+ const ingest = await collectCodebaseIngest({ rootPath: targetPath });
420
+ const indexedFiles = Array.isArray(ingest.indexedFiles?.files) ? ingest.indexedFiles.files : [];
421
+ const indexedFilesByPath = new Map();
422
+ for (const file of indexedFiles) {
423
+ const filePath = toPosixPath(file.path);
424
+ if (!filePath) {
425
+ continue;
426
+ }
427
+ indexedFilesByPath.set(filePath, {
428
+ ...file,
429
+ path: filePath,
430
+ });
431
+ }
432
+
433
+ const tokens = tokenizeWorkItem(queueItem);
434
+ const deterministicCandidates = [];
435
+ for (const file of indexedFilesByPath.values()) {
436
+ const deterministicScore = scoreDeterministicPath(file.path, tokens);
437
+ if (deterministicScore <= 0) {
438
+ continue;
439
+ }
440
+ deterministicCandidates.push({
441
+ path: file.path,
442
+ deterministicScore,
443
+ });
444
+ }
445
+ deterministicCandidates.sort((left, right) => {
446
+ if (right.deterministicScore !== left.deterministicScore) {
447
+ return right.deterministicScore - left.deterministicScore;
448
+ }
449
+ return left.path.localeCompare(right.path);
450
+ });
451
+
452
+ const seedPaths = deterministicCandidates.slice(0, 20).map((candidate) => candidate.path);
453
+ const importGraph = await buildImportGraph({
454
+ rootPath: targetPath,
455
+ indexedFilesByPath,
456
+ seedPaths,
457
+ maxDepth: normalizedGraphDepth,
458
+ });
459
+
460
+ const deterministicScoreMap = new Map(
461
+ deterministicCandidates.map((candidate) => [candidate.path, candidate.deterministicScore])
462
+ );
463
+ const selectedPaths = new Set([...importGraph.distances.keys(), ...seedPaths]);
464
+ const scoredFiles = [];
465
+ for (const filePath of selectedPaths) {
466
+ const metadata = indexedFilesByPath.get(filePath);
467
+ if (!metadata) {
468
+ continue;
469
+ }
470
+ const absolutePath = path.join(targetPath, filePath);
471
+ let content = "";
472
+ try {
473
+ content = await fsp.readFile(absolutePath, "utf-8");
474
+ } catch {
475
+ content = "";
476
+ }
477
+ const deterministicScore = deterministicScoreMap.get(filePath) || 0;
478
+ const semanticScore = scoreSemanticContent({
479
+ content,
480
+ endpoint: normalizeString(queueItem.endpoint),
481
+ tokens,
482
+ });
483
+ const graphDistance = importGraph.distances.get(filePath);
484
+ const graphScore =
485
+ graphDistance === undefined ? 0 : Math.max(1, normalizedGraphDepth - graphDistance + 1);
486
+ const totalScore = deterministicScore * 3 + semanticScore + graphScore;
487
+ const reasons = [];
488
+ if (deterministicScore > 0) {
489
+ reasons.push(`deterministic_path_match:${deterministicScore}`);
490
+ }
491
+ if (semanticScore > 0) {
492
+ reasons.push(`semantic_content_match:${semanticScore}`);
493
+ }
494
+ if (graphDistance !== undefined) {
495
+ reasons.push(`import_graph_distance:${graphDistance}`);
496
+ }
497
+ scoredFiles.push({
498
+ path: filePath,
499
+ language: metadata.language,
500
+ loc: metadata.loc,
501
+ sizeBytes: metadata.sizeBytes,
502
+ deterministicScore,
503
+ semanticScore,
504
+ graphDistance: graphDistance === undefined ? null : graphDistance,
505
+ graphScore,
506
+ totalScore,
507
+ reasons,
508
+ });
509
+ }
510
+
511
+ scoredFiles.sort((left, right) => {
512
+ if (right.totalScore !== left.totalScore) {
513
+ return right.totalScore - left.totalScore;
514
+ }
515
+ return left.path.localeCompare(right.path);
516
+ });
517
+
518
+ const scopedFiles = scoredFiles.slice(0, normalizedMaxFiles);
519
+ const scopedPathSet = new Set(scopedFiles.map((file) => file.path));
520
+ const scopedEdges = importGraph.edges.filter(
521
+ (edge) => scopedPathSet.has(edge.from) && scopedPathSet.has(edge.to)
522
+ );
523
+ const callGraphOverlay = await buildCallgraphOverlay({
524
+ rootPath: targetPath,
525
+ indexedFilesByPath,
526
+ scopedPaths: [...scopedPathSet],
527
+ });
528
+
529
+ const runId = createHybridMapRunId(normalizedNow, normalizedWorkItemId);
530
+ const runPath = path.join(storage.mapRunsDir, `${runId}.json`);
531
+ const runPayload = {
532
+ schemaVersion: HYBRID_MAP_SCHEMA_VERSION,
533
+ generatedAt: normalizedNow,
534
+ runId,
535
+ workItem: {
536
+ workItemId: queueItem.workItemId,
537
+ severity: queueItem.severity,
538
+ status: queueItem.status,
539
+ service: queueItem.service,
540
+ endpoint: queueItem.endpoint,
541
+ errorCode: queueItem.errorCode,
542
+ message: queueItem.message,
543
+ },
544
+ strategy: {
545
+ mode: "hybrid_deterministic_ast_semantic_overlay",
546
+ tokenizedSignals: tokens,
547
+ deterministicSeeds: deterministicCandidates.slice(0, 20),
548
+ graphDepth: normalizedGraphDepth,
549
+ maxFiles: normalizedMaxFiles,
550
+ astParser: importGraph.parserStats,
551
+ callGraphOverlay: callGraphOverlay.summary,
552
+ },
553
+ summary: {
554
+ indexedFileCount: indexedFilesByPath.size,
555
+ deterministicCandidateCount: deterministicCandidates.length,
556
+ graphNodeCount: importGraph.distances.size,
557
+ graphEdgeCount: importGraph.edges.length,
558
+ scopedFileCount: scopedFiles.length,
559
+ astParsedFileCount: importGraph.parserStats.astParsedFileCount,
560
+ fallbackParsedFileCount: importGraph.parserStats.fallbackParsedFileCount,
561
+ astParseErrorCount: importGraph.parserStats.parseErrorCount,
562
+ callGraphNodeCount: callGraphOverlay.summary.nodeCount,
563
+ callGraphEdgeCount: callGraphOverlay.summary.edgeCount,
564
+ callGraphParsedFileCount: callGraphOverlay.summary.parsedFileCount,
565
+ callGraphFallbackParsedFileCount: callGraphOverlay.summary.fallbackParsedFileCount,
566
+ callGraphParseErrorCount: callGraphOverlay.summary.parseErrorCount,
567
+ },
568
+ scopedFiles,
569
+ importGraph: {
570
+ nodes: [...scopedPathSet].sort((left, right) => left.localeCompare(right)),
571
+ edges: scopedEdges,
572
+ },
573
+ callGraph: {
574
+ nodes: callGraphOverlay.nodes,
575
+ edges: callGraphOverlay.edges,
576
+ parsedFiles: callGraphOverlay.parsedFiles,
577
+ },
578
+ };
579
+
580
+ await fsp.mkdir(storage.mapRunsDir, { recursive: true });
581
+ await writeJsonFile(runPath, runPayload);
582
+
583
+ const rawIndex = await readJsonFile(storage.mapIndexPath, () => createInitialMapIndex(normalizedNow));
584
+ const index = normalizeMapIndex(rawIndex, normalizedNow);
585
+ index.generatedAt = normalizedNow;
586
+ index.maps = [
587
+ {
588
+ workItemId: queueItem.workItemId,
589
+ runId,
590
+ generatedAt: normalizedNow,
591
+ mapPath: toPosixPath(path.relative(storage.outputRoot, runPath)),
592
+ service: queueItem.service,
593
+ endpoint: queueItem.endpoint,
594
+ errorCode: queueItem.errorCode,
595
+ status: queueItem.status,
596
+ tokenizedSignals: tokens,
597
+ deterministicSeedCount: deterministicCandidates.slice(0, 20).length,
598
+ scopedFileCount: scopedFiles.length,
599
+ },
600
+ ...index.maps.filter((entry) => entry.runId !== runId),
601
+ ].slice(0, 2000);
602
+ await Promise.all([
603
+ writeJsonFile(storage.mapIndexPath, index),
604
+ appendJsonLine(storage.mapEventsPath, {
605
+ timestamp: normalizedNow,
606
+ eventType: "hybrid_scope_map",
607
+ runId,
608
+ workItemId: queueItem.workItemId,
609
+ deterministicSeedCount: deterministicCandidates.slice(0, 20).length,
610
+ graphNodeCount: importGraph.distances.size,
611
+ graphEdgeCount: importGraph.edges.length,
612
+ scopedFileCount: scopedFiles.length,
613
+ astParsedFileCount: importGraph.parserStats.astParsedFileCount,
614
+ fallbackParsedFileCount: importGraph.parserStats.fallbackParsedFileCount,
615
+ astParseErrorCount: importGraph.parserStats.parseErrorCount,
616
+ callGraphNodeCount: callGraphOverlay.summary.nodeCount,
617
+ callGraphEdgeCount: callGraphOverlay.summary.edgeCount,
618
+ callGraphParsedFileCount: callGraphOverlay.summary.parsedFileCount,
619
+ callGraphFallbackParsedFileCount: callGraphOverlay.summary.fallbackParsedFileCount,
620
+ callGraphParseErrorCount: callGraphOverlay.summary.parseErrorCount,
621
+ }),
622
+ ]);
623
+
624
+ return {
625
+ ...storage,
626
+ runId,
627
+ runPath,
628
+ summary: runPayload.summary,
629
+ strategy: runPayload.strategy,
630
+ scopedFiles: runPayload.scopedFiles,
631
+ importGraph: runPayload.importGraph,
632
+ callGraph: runPayload.callGraph,
633
+ workItem: runPayload.workItem,
634
+ };
635
+ }
636
+
637
+ export async function listHybridScopeMaps({
638
+ targetPath = ".",
639
+ outputDir = "",
640
+ workItemId = "",
641
+ limit = 50,
642
+ env,
643
+ homeDir,
644
+ nowIso = new Date().toISOString(),
645
+ } = {}) {
646
+ const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
647
+ const normalizedLimit = normalizePositiveInteger(limit, "limit", 50);
648
+ const normalizedWorkItemId = normalizeString(workItemId);
649
+ const storage = await resolveHybridMappingStorage({
650
+ targetPath,
651
+ outputDir,
652
+ env,
653
+ homeDir,
654
+ });
655
+ const rawIndex = await readJsonFile(storage.mapIndexPath, () => createInitialMapIndex(normalizedNow));
656
+ const index = normalizeMapIndex(rawIndex, normalizedNow);
657
+ const filtered = index.maps
658
+ .filter((entry) => {
659
+ if (normalizedWorkItemId && normalizeString(entry.workItemId) !== normalizedWorkItemId) {
660
+ return false;
661
+ }
662
+ return true;
663
+ })
664
+ .sort((left, right) => {
665
+ const leftEpoch = Date.parse(String(left.generatedAt || "")) || 0;
666
+ const rightEpoch = Date.parse(String(right.generatedAt || "")) || 0;
667
+ return rightEpoch - leftEpoch;
668
+ });
669
+ return {
670
+ ...storage,
671
+ generatedAt: index.generatedAt,
672
+ totalCount: index.maps.length,
673
+ visibleCount: filtered.length,
674
+ maps: filtered.slice(0, normalizedLimit),
675
+ };
676
+ }
677
+
678
+ export async function showHybridScopeMap({
679
+ targetPath = ".",
680
+ outputDir = "",
681
+ workItemId = "",
682
+ runId = "",
683
+ env,
684
+ homeDir,
685
+ nowIso = new Date().toISOString(),
686
+ } = {}) {
687
+ const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
688
+ const storage = await resolveHybridMappingStorage({
689
+ targetPath,
690
+ outputDir,
691
+ env,
692
+ homeDir,
693
+ });
694
+ const listed = await listHybridScopeMaps({
695
+ targetPath,
696
+ outputDir,
697
+ workItemId,
698
+ limit: 500,
699
+ env,
700
+ homeDir,
701
+ nowIso: normalizedNow,
702
+ });
703
+ const normalizedRunId = normalizeString(runId);
704
+ const selected = listed.maps.find((entry) => {
705
+ if (normalizedRunId && normalizeString(entry.runId) !== normalizedRunId) {
706
+ return false;
707
+ }
708
+ if (workItemId && normalizeString(entry.workItemId) !== normalizeString(workItemId)) {
709
+ return false;
710
+ }
711
+ return true;
712
+ });
713
+ if (!selected) {
714
+ throw new Error(
715
+ `No hybrid scope map found for work item '${normalizeString(workItemId) || "n/a"}' and run '${normalizedRunId || "latest"}'.`
716
+ );
717
+ }
718
+ const absoluteMapPath = path.join(storage.outputRoot, selected.mapPath);
719
+ const payload = await readJsonFile(absoluteMapPath, () => null);
720
+ if (!payload || typeof payload !== "object") {
721
+ throw new Error(`Hybrid scope map artifact not found: ${absoluteMapPath}`);
722
+ }
723
+ return {
724
+ ...storage,
725
+ map: selected,
726
+ mapPath: absoluteMapPath,
727
+ payload,
728
+ };
729
+ }
730
+
731
+ export async function buildHybridHandoffPackage({
732
+ targetPath = ".",
733
+ outputDir = "",
734
+ workItemId,
735
+ mapRunId = "",
736
+ assignee = "omar",
737
+ maxFiles = DEFAULT_HANDOFF_MAX_FILES,
738
+ env,
739
+ homeDir,
740
+ nowIso = new Date().toISOString(),
741
+ } = {}) {
742
+ const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
743
+ const normalizedWorkItemId = normalizeString(workItemId);
744
+ if (!normalizedWorkItemId) {
745
+ throw new Error("workItemId is required.");
746
+ }
747
+ const normalizedMaxFiles = normalizePositiveInteger(maxFiles, "maxFiles", DEFAULT_HANDOFF_MAX_FILES);
748
+ const normalizedAssignee = normalizeString(assignee) || "omar";
749
+ const storage = await resolveHybridMappingStorage({
750
+ targetPath,
751
+ outputDir,
752
+ env,
753
+ homeDir,
754
+ });
755
+ const shown = await showHybridScopeMap({
756
+ targetPath,
757
+ outputDir,
758
+ workItemId: normalizedWorkItemId,
759
+ runId: mapRunId,
760
+ env,
761
+ homeDir,
762
+ nowIso: normalizedNow,
763
+ });
764
+ const mapPayload = shown.payload;
765
+ const handoffRunId = createHybridHandoffRunId(normalizedNow, normalizedWorkItemId);
766
+ const handoffPath = path.join(storage.handoffRunsDir, `${handoffRunId}.json`);
767
+ const handoffFiles = buildHandoffFiles(
768
+ Array.isArray(mapPayload.scopedFiles) ? mapPayload.scopedFiles : [],
769
+ normalizedMaxFiles
770
+ );
771
+ const budgets = deriveHandoffBudgets(handoffFiles);
772
+ const payload = {
773
+ schemaVersion: HYBRID_HANDOFF_SCHEMA_VERSION,
774
+ generatedAt: normalizedNow,
775
+ handoffRunId,
776
+ sourceMapRunId: normalizeString(mapPayload.runId),
777
+ workItem: mapPayload.workItem,
778
+ assignee: {
779
+ primary: normalizedAssignee,
780
+ },
781
+ constraints: {
782
+ allowedPaths: handoffFiles.map((file) => file.path),
783
+ readOnly: true,
784
+ },
785
+ budgets,
786
+ context: {
787
+ strategy: mapPayload.strategy,
788
+ summary: mapPayload.summary,
789
+ importGraph: mapPayload.importGraph,
790
+ callGraph: mapPayload.callGraph,
791
+ },
792
+ files: handoffFiles,
793
+ };
794
+ await fsp.mkdir(storage.handoffRunsDir, { recursive: true });
795
+ await writeJsonFile(handoffPath, payload);
796
+
797
+ const rawIndex = await readJsonFile(storage.handoffIndexPath, () => createInitialHandoffIndex(normalizedNow));
798
+ const index = normalizeHandoffIndex(rawIndex, normalizedNow);
799
+ index.generatedAt = normalizedNow;
800
+ index.handoffs = [
801
+ {
802
+ workItemId: normalizedWorkItemId,
803
+ handoffRunId,
804
+ mapRunId: normalizeString(mapPayload.runId),
805
+ generatedAt: normalizedNow,
806
+ handoffPath: toPosixPath(path.relative(storage.outputRoot, handoffPath)),
807
+ assignee: normalizedAssignee,
808
+ scopedFileCount: handoffFiles.length,
809
+ estimatedInputTokens: budgets.estimatedInputTokens,
810
+ },
811
+ ...index.handoffs.filter((entry) => normalizeString(entry.handoffRunId) !== handoffRunId),
812
+ ].slice(0, 2000);
813
+
814
+ await Promise.all([
815
+ writeJsonFile(storage.handoffIndexPath, index),
816
+ appendJsonLine(storage.handoffEventsPath, {
817
+ timestamp: normalizedNow,
818
+ eventType: "hybrid_handoff_package",
819
+ handoffRunId,
820
+ mapRunId: normalizeString(mapPayload.runId),
821
+ workItemId: normalizedWorkItemId,
822
+ assignee: normalizedAssignee,
823
+ scopedFileCount: handoffFiles.length,
824
+ estimatedInputTokens: budgets.estimatedInputTokens,
825
+ }),
826
+ ]);
827
+
828
+ return {
829
+ ...storage,
830
+ handoffRunId,
831
+ handoffPath,
832
+ summary: {
833
+ scopedFileCount: handoffFiles.length,
834
+ estimatedInputTokens: budgets.estimatedInputTokens,
835
+ maxRuntimeMinutes: budgets.maxRuntimeMinutes,
836
+ maxToolCalls: budgets.maxToolCalls,
837
+ },
838
+ payload,
839
+ };
840
+ }
841
+
842
+ export async function listHybridHandoffs({
843
+ targetPath = ".",
844
+ outputDir = "",
845
+ workItemId = "",
846
+ limit = 50,
847
+ env,
848
+ homeDir,
849
+ nowIso = new Date().toISOString(),
850
+ } = {}) {
851
+ const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
852
+ const normalizedLimit = normalizePositiveInteger(limit, "limit", 50);
853
+ const normalizedWorkItemId = normalizeString(workItemId);
854
+ const storage = await resolveHybridMappingStorage({
855
+ targetPath,
856
+ outputDir,
857
+ env,
858
+ homeDir,
859
+ });
860
+ const rawIndex = await readJsonFile(storage.handoffIndexPath, () => createInitialHandoffIndex(normalizedNow));
861
+ const index = normalizeHandoffIndex(rawIndex, normalizedNow);
862
+ const filtered = index.handoffs
863
+ .filter((entry) => {
864
+ if (normalizedWorkItemId && normalizeString(entry.workItemId) !== normalizedWorkItemId) {
865
+ return false;
866
+ }
867
+ return true;
868
+ })
869
+ .sort((left, right) => {
870
+ const leftEpoch = Date.parse(String(left.generatedAt || "")) || 0;
871
+ const rightEpoch = Date.parse(String(right.generatedAt || "")) || 0;
872
+ return rightEpoch - leftEpoch;
873
+ });
874
+ return {
875
+ ...storage,
876
+ generatedAt: index.generatedAt,
877
+ totalCount: index.handoffs.length,
878
+ visibleCount: filtered.length,
879
+ handoffs: filtered.slice(0, normalizedLimit),
880
+ };
881
+ }
882
+
883
+ export async function showHybridHandoff({
884
+ targetPath = ".",
885
+ outputDir = "",
886
+ workItemId = "",
887
+ handoffRunId = "",
888
+ env,
889
+ homeDir,
890
+ nowIso = new Date().toISOString(),
891
+ } = {}) {
892
+ const normalizedNow = normalizeIsoTimestamp(nowIso, new Date().toISOString());
893
+ const normalizedWorkItemId = normalizeString(workItemId);
894
+ if (!normalizedWorkItemId) {
895
+ throw new Error("workItemId is required.");
896
+ }
897
+ const listed = await listHybridHandoffs({
898
+ targetPath,
899
+ outputDir,
900
+ workItemId: normalizedWorkItemId,
901
+ limit: 500,
902
+ env,
903
+ homeDir,
904
+ nowIso: normalizedNow,
905
+ });
906
+ const normalizedRunId = normalizeString(handoffRunId);
907
+ const selected = listed.handoffs.find((entry) => {
908
+ if (normalizedRunId && normalizeString(entry.handoffRunId) !== normalizedRunId) {
909
+ return false;
910
+ }
911
+ return true;
912
+ });
913
+ if (!selected) {
914
+ throw new Error(
915
+ `No hybrid handoff package found for work item '${normalizedWorkItemId}' and run '${normalizedRunId || "latest"}'.`
916
+ );
917
+ }
918
+ const absoluteHandoffPath = path.join(listed.outputRoot, selected.handoffPath);
919
+ const payload = await readJsonFile(absoluteHandoffPath, () => null);
920
+ if (!payload || typeof payload !== "object") {
921
+ throw new Error(`Hybrid handoff artifact not found: ${absoluteHandoffPath}`);
922
+ }
923
+ return {
924
+ ...listed,
925
+ handoff: selected,
926
+ handoffPath: absoluteHandoffPath,
927
+ payload,
928
+ };
929
+ }