sentinelayer-cli 0.6.2 → 0.8.1

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 (280) hide show
  1. package/README.md +1009 -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/ai-governance/index.js +12 -0
  7. package/src/agents/ai-governance/tools/base.js +171 -0
  8. package/src/agents/ai-governance/tools/eval-regression.js +47 -0
  9. package/src/agents/ai-governance/tools/hitl-audit.js +81 -0
  10. package/src/agents/ai-governance/tools/index.js +52 -0
  11. package/src/agents/ai-governance/tools/prompt-drift.js +42 -0
  12. package/src/agents/ai-governance/tools/provenance-check.js +69 -0
  13. package/src/agents/backend/index.js +12 -0
  14. package/src/agents/backend/tools/base.js +189 -0
  15. package/src/agents/backend/tools/circuit-breaker-check.js +123 -0
  16. package/src/agents/backend/tools/idempotency-audit.js +105 -0
  17. package/src/agents/backend/tools/index.js +87 -0
  18. package/src/agents/backend/tools/retry-audit.js +132 -0
  19. package/src/agents/backend/tools/timeout-audit.js +144 -0
  20. package/src/agents/code-quality/index.js +12 -0
  21. package/src/agents/code-quality/tools/base.js +159 -0
  22. package/src/agents/code-quality/tools/complexity-measure.js +197 -0
  23. package/src/agents/code-quality/tools/coupling-analysis.js +81 -0
  24. package/src/agents/code-quality/tools/cycle-detect.js +49 -0
  25. package/src/agents/code-quality/tools/dep-graph.js +196 -0
  26. package/src/agents/code-quality/tools/index.js +89 -0
  27. package/src/agents/data-layer/index.js +12 -0
  28. package/src/agents/data-layer/tools/base.js +181 -0
  29. package/src/agents/data-layer/tools/index-audit.js +165 -0
  30. package/src/agents/data-layer/tools/index.js +83 -0
  31. package/src/agents/data-layer/tools/migration-scan.js +135 -0
  32. package/src/agents/data-layer/tools/query-explain.js +120 -0
  33. package/src/agents/data-layer/tools/tenancy-scan.js +166 -0
  34. package/src/agents/documentation/index.js +12 -0
  35. package/src/agents/documentation/tools/api-diff.js +91 -0
  36. package/src/agents/documentation/tools/base.js +151 -0
  37. package/src/agents/documentation/tools/dead-link-check.js +58 -0
  38. package/src/agents/documentation/tools/docstring-coverage.js +78 -0
  39. package/src/agents/documentation/tools/index.js +52 -0
  40. package/src/agents/documentation/tools/readme-freshness.js +61 -0
  41. package/src/agents/envelope/fix-cycle.js +45 -0
  42. package/src/agents/envelope/index.js +31 -0
  43. package/src/agents/envelope/loop.js +150 -0
  44. package/src/agents/envelope/pulse.js +18 -0
  45. package/src/agents/envelope/stream.js +40 -0
  46. package/src/agents/infrastructure/index.js +12 -0
  47. package/src/agents/infrastructure/tools/base.js +171 -0
  48. package/src/agents/infrastructure/tools/checkov-run.js +32 -0
  49. package/src/agents/infrastructure/tools/drift-detect.js +59 -0
  50. package/src/agents/infrastructure/tools/iam-least-priv-check.js +78 -0
  51. package/src/agents/infrastructure/tools/index.js +52 -0
  52. package/src/agents/infrastructure/tools/tflint-run.js +31 -0
  53. package/src/agents/jules/config/definition.js +160 -160
  54. package/src/agents/jules/config/system-prompt.js +182 -182
  55. package/src/agents/jules/error-intake.js +51 -51
  56. package/src/agents/jules/fix-cycle.js +17 -17
  57. package/src/agents/jules/loop.js +460 -450
  58. package/src/agents/jules/pulse.js +10 -10
  59. package/src/agents/jules/stream.js +187 -186
  60. package/src/agents/jules/swarm/file-scanner.js +74 -74
  61. package/src/agents/jules/swarm/index.js +11 -11
  62. package/src/agents/jules/swarm/orchestrator.js +362 -362
  63. package/src/agents/jules/swarm/pattern-hunter.js +123 -123
  64. package/src/agents/jules/swarm/sub-agent.js +315 -309
  65. package/src/agents/jules/tools/aidenid-email.js +189 -189
  66. package/src/agents/jules/tools/auth-audit.js +1708 -1691
  67. package/src/agents/jules/tools/dispatch.js +340 -335
  68. package/src/agents/jules/tools/file-edit.js +2 -2
  69. package/src/agents/jules/tools/file-read.js +2 -2
  70. package/src/agents/jules/tools/frontend-analyze.js +570 -570
  71. package/src/agents/jules/tools/glob.js +2 -2
  72. package/src/agents/jules/tools/grep.js +2 -2
  73. package/src/agents/jules/tools/index.js +29 -29
  74. package/src/agents/jules/tools/path-guards.js +2 -2
  75. package/src/agents/jules/tools/runtime-audit.js +507 -507
  76. package/src/agents/jules/tools/shell.js +2 -2
  77. package/src/agents/jules/tools/url-policy.js +100 -100
  78. package/src/agents/mode.js +113 -0
  79. package/src/agents/observability/index.js +12 -0
  80. package/src/agents/observability/tools/alert-audit.js +39 -0
  81. package/src/agents/observability/tools/base.js +181 -0
  82. package/src/agents/observability/tools/dashboard-gap.js +42 -0
  83. package/src/agents/observability/tools/index.js +54 -0
  84. package/src/agents/observability/tools/log-schema-check.js +74 -0
  85. package/src/agents/observability/tools/span-coverage.js +74 -0
  86. package/src/agents/persona-visuals.js +102 -61
  87. package/src/agents/release/index.js +12 -0
  88. package/src/agents/release/tools/base.js +181 -0
  89. package/src/agents/release/tools/changelog-diff.js +86 -0
  90. package/src/agents/release/tools/feature-flag-audit.js +126 -0
  91. package/src/agents/release/tools/index.js +61 -0
  92. package/src/agents/release/tools/rollback-verify.js +129 -0
  93. package/src/agents/release/tools/semver-check.js +109 -0
  94. package/src/agents/reliability/index.js +12 -0
  95. package/src/agents/reliability/tools/backpressure-check.js +129 -0
  96. package/src/agents/reliability/tools/base.js +181 -0
  97. package/src/agents/reliability/tools/chaos-probe.js +109 -0
  98. package/src/agents/reliability/tools/graceful-degradation-check.js +114 -0
  99. package/src/agents/reliability/tools/health-check-audit.js +111 -0
  100. package/src/agents/reliability/tools/index.js +87 -0
  101. package/src/agents/run-persona.js +109 -0
  102. package/src/agents/security/index.js +12 -0
  103. package/src/agents/security/tools/authz-audit.js +134 -0
  104. package/src/agents/security/tools/base.js +190 -0
  105. package/src/agents/security/tools/crypto-review.js +175 -0
  106. package/src/agents/security/tools/index.js +97 -0
  107. package/src/agents/security/tools/sast-scan.js +175 -0
  108. package/src/agents/security/tools/secrets-scan.js +216 -0
  109. package/src/agents/shared-tools/dispatch-core.js +320 -315
  110. package/src/agents/shared-tools/file-edit.js +180 -180
  111. package/src/agents/shared-tools/file-read.js +100 -100
  112. package/src/agents/shared-tools/glob.js +168 -168
  113. package/src/agents/shared-tools/grep.js +228 -228
  114. package/src/agents/shared-tools/index.js +46 -46
  115. package/src/agents/shared-tools/path-guards.js +161 -161
  116. package/src/agents/shared-tools/shell.js +383 -383
  117. package/src/agents/supply-chain/index.js +12 -0
  118. package/src/agents/supply-chain/tools/attestation-check.js +42 -0
  119. package/src/agents/supply-chain/tools/base.js +151 -0
  120. package/src/agents/supply-chain/tools/index.js +52 -0
  121. package/src/agents/supply-chain/tools/lockfile-integrity.js +73 -0
  122. package/src/agents/supply-chain/tools/package-verify.js +56 -0
  123. package/src/agents/supply-chain/tools/sbom-diff.js +34 -0
  124. package/src/agents/testing/index.js +12 -0
  125. package/src/agents/testing/tools/base.js +202 -0
  126. package/src/agents/testing/tools/coverage-gap.js +144 -0
  127. package/src/agents/testing/tools/flake-detect.js +125 -0
  128. package/src/agents/testing/tools/index.js +85 -0
  129. package/src/agents/testing/tools/mutation-test.js +143 -0
  130. package/src/agents/testing/tools/snapshot-diff.js +103 -0
  131. package/src/ai/aidenid.js +1021 -1009
  132. package/src/ai/client.js +553 -553
  133. package/src/ai/domain-target-store.js +268 -268
  134. package/src/ai/identity-store.js +270 -270
  135. package/src/ai/proxy.js +137 -137
  136. package/src/ai/site-store.js +145 -145
  137. package/src/audit/agents/architecture.js +180 -180
  138. package/src/audit/agents/compliance.js +179 -179
  139. package/src/audit/agents/documentation.js +165 -165
  140. package/src/audit/agents/performance.js +145 -145
  141. package/src/audit/agents/security.js +215 -215
  142. package/src/audit/agents/testing.js +172 -172
  143. package/src/audit/orchestrator.js +557 -557
  144. package/src/audit/package.js +204 -204
  145. package/src/audit/registry.js +284 -284
  146. package/src/audit/replay.js +103 -103
  147. package/src/auth/gate.js +428 -371
  148. package/src/auth/http.js +681 -611
  149. package/src/auth/service.js +1106 -1106
  150. package/src/auth/session-store.js +813 -813
  151. package/src/cli.js +257 -252
  152. package/src/commands/ai/identity-lifecycle.js +1338 -1338
  153. package/src/commands/ai/provision-governance.js +1272 -1272
  154. package/src/commands/ai/shared.js +147 -147
  155. package/src/commands/ai.js +11 -11
  156. package/src/commands/apply.js +12 -12
  157. package/src/commands/audit.js +1171 -1166
  158. package/src/commands/auth.js +419 -419
  159. package/src/commands/chat.js +184 -191
  160. package/src/commands/config.js +184 -184
  161. package/src/commands/cost.js +311 -311
  162. package/src/commands/daemon/core.js +850 -850
  163. package/src/commands/daemon/extended.js +1048 -1048
  164. package/src/commands/daemon/shared.js +213 -213
  165. package/src/commands/daemon.js +11 -11
  166. package/src/commands/guide.js +174 -174
  167. package/src/commands/ingest.js +58 -58
  168. package/src/commands/init.js +55 -55
  169. package/src/commands/legacy-args.js +20 -10
  170. package/src/commands/mcp.js +461 -461
  171. package/src/commands/omargate.js +63 -29
  172. package/src/commands/persona.js +65 -20
  173. package/src/commands/plugin.js +260 -260
  174. package/src/commands/policy.js +132 -132
  175. package/src/commands/prompt.js +238 -238
  176. package/src/commands/review.js +704 -704
  177. package/src/commands/scan.js +865 -872
  178. package/src/commands/session.js +1238 -0
  179. package/src/commands/spec.js +771 -716
  180. package/src/commands/swarm.js +651 -651
  181. package/src/commands/telemetry.js +202 -202
  182. package/src/commands/watch.js +511 -511
  183. package/src/config/agent-dictionary.js +182 -182
  184. package/src/config/io.js +56 -56
  185. package/src/config/paths.js +18 -18
  186. package/src/config/schema.js +55 -55
  187. package/src/config/service.js +184 -184
  188. package/src/coord/events-log.js +141 -0
  189. package/src/coord/handshake.js +719 -0
  190. package/src/coord/index.js +35 -0
  191. package/src/coord/paths.js +84 -0
  192. package/src/coord/priority.js +62 -0
  193. package/src/coord/tarjan.js +157 -0
  194. package/src/cost/budget.js +235 -235
  195. package/src/cost/history.js +188 -188
  196. package/src/cost/tokenizer.js +160 -0
  197. package/src/cost/tracker.js +232 -171
  198. package/src/daemon/artifact-lineage.js +896 -534
  199. package/src/daemon/assignment-ledger.js +1083 -770
  200. package/src/daemon/ast-drift.js +496 -0
  201. package/src/daemon/ast-parser-layer.js +258 -258
  202. package/src/daemon/budget-governor.js +633 -633
  203. package/src/daemon/callgraph-overlay.js +646 -646
  204. package/src/daemon/error-worker.js +1209 -626
  205. package/src/daemon/fix-cycle.js +384 -377
  206. package/src/daemon/hybrid-mapper.js +929 -929
  207. package/src/daemon/ingest-refresh.js +79 -11
  208. package/src/daemon/jira-lifecycle.js +767 -632
  209. package/src/daemon/operator-control.js +657 -657
  210. package/src/daemon/pulse.js +327 -327
  211. package/src/daemon/reliability-lane.js +471 -471
  212. package/src/daemon/scope-engine.js +1068 -0
  213. package/src/daemon/watchdog.js +971 -971
  214. package/src/events/schema.js +190 -0
  215. package/src/guide/generator.js +316 -316
  216. package/src/ingest/engine.js +933 -918
  217. package/src/ingest/ownership.js +380 -0
  218. package/src/interactive/index.js +97 -97
  219. package/src/legacy-cli.js +3228 -2994
  220. package/src/mcp/registry.js +695 -695
  221. package/src/memory/blackboard.js +301 -301
  222. package/src/memory/retrieval.js +581 -581
  223. package/src/orchestrator/kai-chen.js +126 -0
  224. package/src/plugin/manifest.js +553 -553
  225. package/src/policy/packs.js +144 -144
  226. package/src/prompt/generator.js +136 -118
  227. package/src/review/ai-review.js +672 -679
  228. package/src/review/compliance-pack.js +389 -0
  229. package/src/review/investor-dd-config.js +54 -0
  230. package/src/review/investor-dd-file-loop.js +303 -0
  231. package/src/review/investor-dd-file-router.js +406 -0
  232. package/src/review/investor-dd-html-report.js +233 -0
  233. package/src/review/investor-dd-notification.js +120 -0
  234. package/src/review/investor-dd-orchestrator.js +405 -0
  235. package/src/review/investor-dd-persona-runner.js +275 -0
  236. package/src/review/live-validator.js +253 -0
  237. package/src/review/local-review.js +1351 -1305
  238. package/src/review/omargate-interactive.js +68 -68
  239. package/src/review/omargate-orchestrator.js +492 -300
  240. package/src/review/persona-prompts.js +484 -296
  241. package/src/review/reconciliation-rules.js +329 -0
  242. package/src/review/replay.js +235 -235
  243. package/src/review/report.js +664 -664
  244. package/src/review/reproducibility-chain.js +136 -0
  245. package/src/review/scan-modes.js +147 -42
  246. package/src/review/spec-binding.js +487 -487
  247. package/src/scaffold/generator.js +67 -67
  248. package/src/scaffold/templates.js +150 -150
  249. package/src/scan/generator.js +418 -418
  250. package/src/scan/gh-secrets.js +107 -107
  251. package/src/session/agent-registry.js +359 -0
  252. package/src/session/analytics.js +479 -0
  253. package/src/session/daemon.js +1396 -0
  254. package/src/session/file-locks.js +666 -0
  255. package/src/session/paths.js +37 -0
  256. package/src/session/recap.js +567 -0
  257. package/src/session/redact.js +82 -0
  258. package/src/session/runtime-bridge.js +762 -0
  259. package/src/session/scoring.js +406 -0
  260. package/src/session/setup-guides.js +304 -0
  261. package/src/session/store.js +704 -0
  262. package/src/session/stream.js +333 -0
  263. package/src/session/sync.js +753 -0
  264. package/src/session/tasks.js +1054 -0
  265. package/src/session/templates.js +188 -0
  266. package/src/spec/generator.js +619 -519
  267. package/src/spec/regenerate.js +237 -237
  268. package/src/spec/templates.js +91 -91
  269. package/src/swarm/dashboard.js +247 -247
  270. package/src/swarm/factory.js +363 -363
  271. package/src/swarm/pentest.js +934 -934
  272. package/src/swarm/registry.js +419 -419
  273. package/src/swarm/report.js +158 -158
  274. package/src/swarm/runtime.js +569 -576
  275. package/src/swarm/scenario-dsl.js +272 -272
  276. package/src/telemetry/ledger.js +302 -302
  277. package/src/telemetry/session-tracker.js +234 -234
  278. package/src/telemetry/sync.js +203 -203
  279. package/src/ui/command-hints.js +13 -13
  280. 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
+ }