sentinelayer-cli 0.6.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/README.md +996 -996
  2. package/bin/create-sentinelayer.js +5 -5
  3. package/bin/sentinelayer-cli.js +4 -4
  4. package/bin/sl.js +5 -5
  5. package/package.json +64 -63
  6. package/src/agents/jules/config/definition.js +160 -160
  7. package/src/agents/jules/config/system-prompt.js +182 -182
  8. package/src/agents/jules/error-intake.js +51 -51
  9. package/src/agents/jules/fix-cycle.js +17 -17
  10. package/src/agents/jules/loop.js +457 -450
  11. package/src/agents/jules/pulse.js +10 -10
  12. package/src/agents/jules/stream.js +187 -186
  13. package/src/agents/jules/swarm/file-scanner.js +74 -74
  14. package/src/agents/jules/swarm/index.js +11 -11
  15. package/src/agents/jules/swarm/orchestrator.js +362 -362
  16. package/src/agents/jules/swarm/pattern-hunter.js +123 -123
  17. package/src/agents/jules/swarm/sub-agent.js +311 -309
  18. package/src/agents/jules/tools/aidenid-email.js +189 -189
  19. package/src/agents/jules/tools/auth-audit.js +1699 -1691
  20. package/src/agents/jules/tools/dispatch.js +340 -335
  21. package/src/agents/jules/tools/file-edit.js +2 -2
  22. package/src/agents/jules/tools/file-read.js +2 -2
  23. package/src/agents/jules/tools/frontend-analyze.js +570 -570
  24. package/src/agents/jules/tools/glob.js +2 -2
  25. package/src/agents/jules/tools/grep.js +2 -2
  26. package/src/agents/jules/tools/index.js +29 -29
  27. package/src/agents/jules/tools/path-guards.js +2 -2
  28. package/src/agents/jules/tools/runtime-audit.js +507 -507
  29. package/src/agents/jules/tools/shell.js +2 -2
  30. package/src/agents/jules/tools/url-policy.js +100 -100
  31. package/src/agents/persona-visuals.js +64 -61
  32. package/src/agents/shared-tools/dispatch-core.js +320 -315
  33. package/src/agents/shared-tools/file-edit.js +180 -180
  34. package/src/agents/shared-tools/file-read.js +100 -100
  35. package/src/agents/shared-tools/glob.js +168 -168
  36. package/src/agents/shared-tools/grep.js +228 -228
  37. package/src/agents/shared-tools/index.js +46 -46
  38. package/src/agents/shared-tools/path-guards.js +161 -161
  39. package/src/agents/shared-tools/shell.js +383 -383
  40. package/src/ai/aidenid.js +1021 -1009
  41. package/src/ai/client.js +553 -553
  42. package/src/ai/domain-target-store.js +268 -268
  43. package/src/ai/identity-store.js +270 -270
  44. package/src/ai/proxy.js +137 -137
  45. package/src/ai/site-store.js +145 -145
  46. package/src/audit/agents/architecture.js +180 -180
  47. package/src/audit/agents/compliance.js +179 -179
  48. package/src/audit/agents/documentation.js +165 -165
  49. package/src/audit/agents/performance.js +145 -145
  50. package/src/audit/agents/security.js +215 -215
  51. package/src/audit/agents/testing.js +172 -172
  52. package/src/audit/orchestrator.js +557 -557
  53. package/src/audit/package.js +204 -204
  54. package/src/audit/registry.js +284 -284
  55. package/src/audit/replay.js +103 -103
  56. package/src/auth/gate.js +400 -371
  57. package/src/auth/http.js +681 -611
  58. package/src/auth/service.js +1106 -1106
  59. package/src/auth/session-store.js +813 -813
  60. package/src/cli.js +257 -252
  61. package/src/commands/ai/identity-lifecycle.js +1338 -1338
  62. package/src/commands/ai/provision-governance.js +1272 -1272
  63. package/src/commands/ai/shared.js +147 -147
  64. package/src/commands/ai.js +11 -11
  65. package/src/commands/apply.js +12 -12
  66. package/src/commands/audit.js +1171 -1166
  67. package/src/commands/auth.js +419 -419
  68. package/src/commands/chat.js +191 -191
  69. package/src/commands/config.js +184 -184
  70. package/src/commands/cost.js +311 -311
  71. package/src/commands/daemon/core.js +850 -850
  72. package/src/commands/daemon/extended.js +1048 -1048
  73. package/src/commands/daemon/shared.js +213 -213
  74. package/src/commands/daemon.js +11 -11
  75. package/src/commands/guide.js +174 -174
  76. package/src/commands/ingest.js +58 -58
  77. package/src/commands/init.js +55 -55
  78. package/src/commands/legacy-args.js +10 -10
  79. package/src/commands/mcp.js +461 -461
  80. package/src/commands/omargate.js +29 -29
  81. package/src/commands/persona.js +20 -20
  82. package/src/commands/plugin.js +260 -260
  83. package/src/commands/policy.js +132 -132
  84. package/src/commands/prompt.js +238 -238
  85. package/src/commands/review.js +704 -704
  86. package/src/commands/scan.js +872 -872
  87. package/src/commands/session.js +590 -0
  88. package/src/commands/spec.js +778 -716
  89. package/src/commands/swarm.js +651 -651
  90. package/src/commands/telemetry.js +202 -202
  91. package/src/commands/watch.js +511 -511
  92. package/src/config/agent-dictionary.js +182 -182
  93. package/src/config/io.js +56 -56
  94. package/src/config/paths.js +18 -18
  95. package/src/config/schema.js +55 -55
  96. package/src/config/service.js +184 -184
  97. package/src/cost/budget.js +235 -235
  98. package/src/cost/history.js +188 -188
  99. package/src/cost/tracker.js +171 -171
  100. package/src/daemon/artifact-lineage.js +534 -534
  101. package/src/daemon/assignment-ledger.js +966 -770
  102. package/src/daemon/ast-parser-layer.js +258 -258
  103. package/src/daemon/budget-governor.js +633 -633
  104. package/src/daemon/callgraph-overlay.js +646 -646
  105. package/src/daemon/error-worker.js +1209 -626
  106. package/src/daemon/fix-cycle.js +384 -377
  107. package/src/daemon/hybrid-mapper.js +929 -929
  108. package/src/daemon/ingest-refresh.js +10 -9
  109. package/src/daemon/jira-lifecycle.js +767 -632
  110. package/src/daemon/operator-control.js +657 -657
  111. package/src/daemon/pulse.js +327 -327
  112. package/src/daemon/reliability-lane.js +471 -471
  113. package/src/daemon/scope-engine.js +1068 -0
  114. package/src/daemon/watchdog.js +971 -971
  115. package/src/events/schema.js +190 -0
  116. package/src/guide/generator.js +316 -316
  117. package/src/ingest/engine.js +918 -918
  118. package/src/interactive/index.js +97 -97
  119. package/src/legacy-cli.js +3161 -2994
  120. package/src/mcp/registry.js +695 -695
  121. package/src/memory/blackboard.js +301 -301
  122. package/src/memory/retrieval.js +581 -581
  123. package/src/plugin/manifest.js +553 -553
  124. package/src/policy/packs.js +144 -144
  125. package/src/prompt/generator.js +136 -118
  126. package/src/review/ai-review.js +679 -679
  127. package/src/review/local-review.js +1351 -1305
  128. package/src/review/omargate-interactive.js +68 -68
  129. package/src/review/omargate-orchestrator.js +404 -300
  130. package/src/review/persona-prompts.js +296 -296
  131. package/src/review/replay.js +235 -235
  132. package/src/review/report.js +664 -664
  133. package/src/review/scan-modes.js +48 -42
  134. package/src/review/spec-binding.js +487 -487
  135. package/src/scaffold/generator.js +67 -67
  136. package/src/scaffold/templates.js +150 -150
  137. package/src/scan/generator.js +418 -418
  138. package/src/scan/gh-secrets.js +107 -107
  139. package/src/session/agent-registry.js +352 -0
  140. package/src/session/daemon.js +801 -0
  141. package/src/session/paths.js +33 -0
  142. package/src/session/runtime-bridge.js +739 -0
  143. package/src/session/store.js +388 -0
  144. package/src/session/stream.js +325 -0
  145. package/src/spec/generator.js +619 -519
  146. package/src/spec/regenerate.js +237 -237
  147. package/src/spec/templates.js +91 -91
  148. package/src/swarm/dashboard.js +247 -247
  149. package/src/swarm/factory.js +363 -363
  150. package/src/swarm/pentest.js +934 -934
  151. package/src/swarm/registry.js +419 -419
  152. package/src/swarm/report.js +158 -158
  153. package/src/swarm/runtime.js +576 -576
  154. package/src/swarm/scenario-dsl.js +272 -272
  155. package/src/telemetry/ledger.js +302 -302
  156. package/src/telemetry/session-tracker.js +234 -234
  157. package/src/telemetry/sync.js +203 -203
  158. package/src/ui/command-hints.js +13 -13
  159. package/src/ui/markdown.js +220 -220
@@ -1,581 +1,581 @@
1
- import fsp from "node:fs/promises";
2
- import path from "node:path";
3
-
4
- function normalizeString(value) {
5
- return String(value || "").trim();
6
- }
7
-
8
- function normalizeNumber(value, fallback = 0) {
9
- const parsed = Number(value);
10
- if (!Number.isFinite(parsed)) {
11
- return fallback;
12
- }
13
- return parsed;
14
- }
15
-
16
- function normalizeSeverity(value) {
17
- const severity = normalizeString(value).toUpperCase();
18
- if (severity === "P0" || severity === "P1" || severity === "P2" || severity === "P3") {
19
- return severity;
20
- }
21
- return "P3";
22
- }
23
-
24
- export function tokenize(value) {
25
- return Array.from(
26
- new Set(
27
- normalizeString(value)
28
- .toLowerCase()
29
- .split(/[^a-z0-9_]+/g)
30
- .map((token) => token.trim())
31
- .filter((token) => token.length >= 2)
32
- )
33
- );
34
- }
35
-
36
- function tokenizeWithFrequency(value) {
37
- const tokens = normalizeString(value)
38
- .toLowerCase()
39
- .split(/[^a-z0-9_]+/g)
40
- .map((token) => token.trim())
41
- .filter((token) => token.length >= 2);
42
- const frequency = new Map();
43
- for (const token of tokens) {
44
- frequency.set(token, (frequency.get(token) || 0) + 1);
45
- }
46
- return {
47
- tokens,
48
- uniqueTokens: Array.from(new Set(tokens)),
49
- frequency,
50
- };
51
- }
52
-
53
- function toPosixPath(value) {
54
- return normalizeString(value).replace(/\\/g, "/");
55
- }
56
-
57
- function clipSnippet(text, maxLength = 180) {
58
- const normalized = normalizeString(text).replace(/\s+/g, " ");
59
- if (normalized.length <= maxLength) {
60
- return normalized;
61
- }
62
- return `${normalized.slice(0, maxLength - 3)}...`;
63
- }
64
-
65
- const SEVERITY_SCORE = Object.freeze({
66
- P0: 1,
67
- P1: 0.9,
68
- P2: 0.65,
69
- P3: 0.4,
70
- });
71
-
72
- function parseTimestamp(value) {
73
- const millis = Date.parse(value);
74
- if (!Number.isFinite(millis)) {
75
- return 0;
76
- }
77
- return millis;
78
- }
79
-
80
- function buildDocumentVector({ frequency, tokenCount, idfByToken }) {
81
- const vector = new Map();
82
- const denominator = Math.max(1, tokenCount);
83
- for (const [token, count] of frequency.entries()) {
84
- const idf = idfByToken.get(token) || 0;
85
- const tf = count / denominator;
86
- const weight = tf * idf;
87
- if (weight > 0) {
88
- vector.set(token, weight);
89
- }
90
- }
91
- return vector;
92
- }
93
-
94
- function vectorMagnitude(vector) {
95
- let sum = 0;
96
- for (const value of vector.values()) {
97
- sum += value * value;
98
- }
99
- return Math.sqrt(sum);
100
- }
101
-
102
- function cosineSimilarity(leftVector, leftMagnitude, rightVector, rightMagnitude) {
103
- if (!leftVector || !rightVector || leftMagnitude <= 0 || rightMagnitude <= 0) {
104
- return 0;
105
- }
106
- const [smaller, larger] =
107
- leftVector.size <= rightVector.size ? [leftVector, rightVector] : [rightVector, leftVector];
108
- let dot = 0;
109
- for (const [token, value] of smaller.entries()) {
110
- const counterpart = larger.get(token);
111
- if (counterpart) {
112
- dot += value * counterpart;
113
- }
114
- }
115
- if (dot <= 0) {
116
- return 0;
117
- }
118
- return dot / (leftMagnitude * rightMagnitude);
119
- }
120
-
121
- function buildDeterministicScore({ document, queryTokens, cosine, recency }) {
122
- const tokenSet = document.tokenSet;
123
- const overlapCount = queryTokens.filter((token) => tokenSet.has(token)).length;
124
- const tokenOverlap = queryTokens.length > 0 ? overlapCount / queryTokens.length : 0;
125
- const queryText = normalizeString(document.queryTextForExact).toLowerCase();
126
- const exactMatch =
127
- queryTokens.length > 0 &&
128
- queryTokens.every((token) => queryText.includes(token))
129
- ? 1
130
- : 0;
131
- const severityScore = SEVERITY_SCORE[normalizeSeverity(document.severity)] || 0.35;
132
- return {
133
- exactMatch,
134
- tokenOverlap,
135
- cosine,
136
- recency,
137
- severityScore,
138
- score:
139
- exactMatch * 0.25 +
140
- tokenOverlap * 0.25 +
141
- cosine * 0.3 +
142
- recency * 0.1 +
143
- severityScore * 0.1,
144
- };
145
- }
146
-
147
- function resolveSpecCandidates({ targetPath, specFile }) {
148
- if (normalizeString(specFile)) {
149
- const absoluteCandidate = path.isAbsolute(specFile)
150
- ? specFile
151
- : path.join(targetPath, specFile);
152
- return [absoluteCandidate];
153
- }
154
- return [path.join(targetPath, "SPEC.md"), path.join(targetPath, "docs", "spec.md")];
155
- }
156
-
157
- async function loadSpecDocument({ targetPath, specFile = "" }) {
158
- const candidates = resolveSpecCandidates({ targetPath, specFile });
159
- for (const candidate of candidates) {
160
- try {
161
- const text = await fsp.readFile(candidate, "utf-8");
162
- const normalizedPath = toPosixPath(path.resolve(candidate));
163
- return {
164
- documentId: `spec:${normalizedPath}`,
165
- sourceType: "spec",
166
- sourcePath: normalizedPath,
167
- severity: "P3",
168
- updatedAt: new Date().toISOString(),
169
- text,
170
- metadata: {
171
- category: "spec",
172
- },
173
- };
174
- } catch {
175
- // Continue to next candidate.
176
- }
177
- }
178
- return null;
179
- }
180
-
181
- async function loadAuditHistoryDocuments({ outputRoot, excludeRunId = "", maxAuditRuns = 3 }) {
182
- const auditsDirectory = path.join(outputRoot, "audits");
183
- let runEntries = [];
184
- try {
185
- runEntries = await fsp.readdir(auditsDirectory, { withFileTypes: true });
186
- } catch {
187
- return [];
188
- }
189
- const runIds = runEntries
190
- .filter((entry) => entry.isDirectory() && entry.name.startsWith("audit-"))
191
- .map((entry) => entry.name)
192
- .filter((runId) => runId !== normalizeString(excludeRunId))
193
- .sort((left, right) => right.localeCompare(left))
194
- .slice(0, Math.max(0, Math.floor(Number(maxAuditRuns || 0))));
195
-
196
- const documents = [];
197
- for (const runId of runIds) {
198
- const reportPath = path.join(auditsDirectory, runId, "AUDIT_REPORT.json");
199
- try {
200
- const report = JSON.parse(await fsp.readFile(reportPath, "utf-8"));
201
- documents.push({
202
- documentId: `audit:${runId}:summary`,
203
- sourceType: "audit-history",
204
- sourcePath: toPosixPath(reportPath),
205
- severity: report.summary?.blocking ? "P1" : "P3",
206
- updatedAt: normalizeString(report.generatedAt) || new Date().toISOString(),
207
- text: [
208
- `run_id ${runId}`,
209
- `summary p0 ${report.summary?.P0 || 0}`,
210
- `p1 ${report.summary?.P1 || 0}`,
211
- `p2 ${report.summary?.P2 || 0}`,
212
- `p3 ${report.summary?.P3 || 0}`,
213
- `risk_surfaces ${(report.ingest?.riskSurfaces || []).join(" ")}`,
214
- ].join(" "),
215
- metadata: {
216
- category: "audit-summary",
217
- runId,
218
- },
219
- });
220
-
221
- let findingIndex = 0;
222
- for (const agent of report.agentResults || []) {
223
- for (const finding of agent.findings || []) {
224
- if (findingIndex >= 60) {
225
- break;
226
- }
227
- findingIndex += 1;
228
- documents.push({
229
- documentId: `audit:${runId}:finding:${findingIndex}`,
230
- sourceType: "audit-history",
231
- sourcePath: toPosixPath(reportPath),
232
- severity: normalizeSeverity(finding.severity),
233
- updatedAt: normalizeString(report.generatedAt) || new Date().toISOString(),
234
- text: [finding.file, finding.layer, finding.ruleId, finding.message].filter(Boolean).join(" "),
235
- metadata: {
236
- category: "audit-finding",
237
- runId,
238
- agentId: agent.agentId,
239
- file: toPosixPath(finding.file),
240
- line: normalizeNumber(finding.line, 0),
241
- },
242
- });
243
- }
244
- }
245
- } catch {
246
- // Ignore malformed historical run artifacts.
247
- }
248
- }
249
- return documents;
250
- }
251
-
252
- function buildIngestDocuments(ingest = {}) {
253
- const summary = ingest.summary || {};
254
- const frameworkList = Array.isArray(ingest.frameworks) ? ingest.frameworks : [];
255
- const riskSurfaces = Array.isArray(ingest.riskSurfaces) ? ingest.riskSurfaces : [];
256
- const documents = [
257
- {
258
- documentId: "ingest:summary",
259
- sourceType: "ingest",
260
- sourcePath: "",
261
- severity: "P3",
262
- updatedAt: new Date().toISOString(),
263
- text: [
264
- `files_scanned ${summary.filesScanned || 0}`,
265
- `total_loc ${summary.totalLoc || 0}`,
266
- `frameworks ${frameworkList.join(" ")}`,
267
- ].join(" "),
268
- metadata: {
269
- category: "ingest-summary",
270
- },
271
- },
272
- ];
273
- for (const [index, surface] of riskSurfaces.entries()) {
274
- const surfaceName = normalizeString(surface?.surface || surface);
275
- if (!surfaceName) {
276
- continue;
277
- }
278
- documents.push({
279
- documentId: `ingest:surface:${index + 1}`,
280
- sourceType: "ingest",
281
- sourcePath: "",
282
- severity: "P2",
283
- updatedAt: new Date().toISOString(),
284
- text: `risk_surface ${surfaceName}`,
285
- metadata: {
286
- category: "ingest-risk-surface",
287
- surface: surfaceName,
288
- },
289
- });
290
- }
291
- return documents;
292
- }
293
-
294
- function buildLocalDocument(raw = {}, index = 0) {
295
- const text = normalizeString(raw.text);
296
- const tokenized = tokenizeWithFrequency(text);
297
- const updatedAt = normalizeString(raw.updatedAt) || new Date().toISOString();
298
- const sequence = index + 1;
299
- return {
300
- documentId: normalizeString(raw.documentId) || `doc-${String(sequence).padStart(6, "0")}`,
301
- sourceType: normalizeString(raw.sourceType) || "memory",
302
- sourcePath: toPosixPath(raw.sourcePath),
303
- severity: normalizeSeverity(raw.severity),
304
- updatedAt,
305
- text,
306
- snippet: clipSnippet(text),
307
- metadata: raw.metadata && typeof raw.metadata === "object" ? raw.metadata : {},
308
- sequence,
309
- tokenSet: new Set(tokenized.uniqueTokens),
310
- tokenFrequency: tokenized.frequency,
311
- tokenCount: tokenized.tokens.length,
312
- queryTextForExact: text,
313
- timestampMs: parseTimestamp(updatedAt),
314
- };
315
- }
316
-
317
- export function buildDocumentsFromBlackboardEntries(entries = []) {
318
- return (entries || []).map((entry, index) => ({
319
- documentId: `blackboard:${normalizeString(entry.entryId) || index + 1}`,
320
- sourceType: "blackboard",
321
- sourcePath: "",
322
- severity: normalizeSeverity(entry.severity),
323
- updatedAt: normalizeString(entry.timestamp) || new Date().toISOString(),
324
- text: [
325
- entry.agentId,
326
- entry.source,
327
- entry.file,
328
- entry.layer,
329
- entry.ruleId,
330
- entry.message,
331
- entry.note,
332
- ]
333
- .filter(Boolean)
334
- .join(" "),
335
- metadata: {
336
- category: "blackboard-entry",
337
- entryId: entry.entryId || "",
338
- line: normalizeNumber(entry.line, 0),
339
- file: toPosixPath(entry.file),
340
- },
341
- }));
342
- }
343
-
344
- export async function buildSharedMemoryCorpus({
345
- outputRoot = "",
346
- targetPath = "",
347
- ingest = {},
348
- specFile = "",
349
- excludeRunId = "",
350
- maxAuditRuns = 3,
351
- } = {}) {
352
- const resolvedOutputRoot = path.resolve(String(outputRoot || "."));
353
- const resolvedTargetPath = path.resolve(String(targetPath || "."));
354
-
355
- const [historyDocuments, specDocument] = await Promise.all([
356
- loadAuditHistoryDocuments({
357
- outputRoot: resolvedOutputRoot,
358
- excludeRunId,
359
- maxAuditRuns,
360
- }),
361
- loadSpecDocument({
362
- targetPath: resolvedTargetPath,
363
- specFile,
364
- }),
365
- ]);
366
-
367
- const documents = [
368
- ...buildIngestDocuments(ingest),
369
- ...historyDocuments,
370
- ...(specDocument ? [specDocument] : []),
371
- ];
372
-
373
- const sourceCounts = documents.reduce(
374
- (counts, document) => {
375
- const key = normalizeString(document.sourceType) || "unknown";
376
- counts[key] = (counts[key] || 0) + 1;
377
- return counts;
378
- },
379
- {}
380
- );
381
-
382
- return {
383
- documents,
384
- sourceCounts,
385
- hasSpecDocument: Boolean(specDocument),
386
- historyRunDocumentCount: historyDocuments.length,
387
- };
388
- }
389
-
390
- export function buildLocalHybridIndex(documents = []) {
391
- const preparedDocuments = (documents || [])
392
- .map((document, index) => buildLocalDocument(document, index))
393
- .filter((document) => document.text.length > 0);
394
-
395
- const documentCount = preparedDocuments.length;
396
- const docFrequency = new Map();
397
- for (const document of preparedDocuments) {
398
- for (const token of document.tokenSet.values()) {
399
- docFrequency.set(token, (docFrequency.get(token) || 0) + 1);
400
- }
401
- }
402
-
403
- const idfByToken = new Map();
404
- for (const [token, frequency] of docFrequency.entries()) {
405
- const idf = Math.log((documentCount + 1) / (frequency + 1)) + 1;
406
- idfByToken.set(token, idf);
407
- }
408
-
409
- const timestampValues = preparedDocuments
410
- .map((document) => document.timestampMs)
411
- .filter((value) => Number.isFinite(value) && value > 0);
412
- const minTimestamp = timestampValues.length > 0 ? Math.min(...timestampValues) : 0;
413
- const maxTimestamp = timestampValues.length > 0 ? Math.max(...timestampValues) : 0;
414
-
415
- const entries = preparedDocuments.map((document) => {
416
- const vector = buildDocumentVector({
417
- frequency: document.tokenFrequency,
418
- tokenCount: document.tokenCount,
419
- idfByToken,
420
- });
421
- const magnitude = vectorMagnitude(vector);
422
- let recency = 0.5;
423
- if (maxTimestamp > minTimestamp && document.timestampMs > 0) {
424
- recency = (document.timestampMs - minTimestamp) / (maxTimestamp - minTimestamp);
425
- }
426
- return {
427
- ...document,
428
- vector,
429
- magnitude,
430
- recency,
431
- };
432
- });
433
-
434
- return {
435
- schemaVersion: "1.0.0",
436
- generatedAt: new Date().toISOString(),
437
- documentCount,
438
- vocabularySize: idfByToken.size,
439
- idfByToken,
440
- entries,
441
- };
442
- }
443
-
444
- export function queryLocalHybridIndex(index, { query = "", limit = 12, minScore = 0 } = {}) {
445
- const queryText = normalizeString(query);
446
- const queryTokenized = tokenizeWithFrequency(queryText);
447
- const queryTokens = queryTokenized.uniqueTokens;
448
- const queryVector = buildDocumentVector({
449
- frequency: queryTokenized.frequency,
450
- tokenCount: queryTokenized.tokens.length,
451
- idfByToken: index.idfByToken || new Map(),
452
- });
453
- const queryMagnitude = vectorMagnitude(queryVector);
454
-
455
- const normalizedLimit = Math.max(1, Math.floor(normalizeNumber(limit, 12)));
456
- const normalizedMinScore = Math.max(0, Math.min(1, normalizeNumber(minScore, 0)));
457
- const results = (index.entries || [])
458
- .map((document) => {
459
- const cosine = cosineSimilarity(queryVector, queryMagnitude, document.vector, document.magnitude);
460
- const signals = buildDeterministicScore({
461
- document,
462
- queryTokens,
463
- cosine,
464
- recency: document.recency,
465
- });
466
- return {
467
- documentId: document.documentId,
468
- sourceType: document.sourceType,
469
- sourcePath: document.sourcePath,
470
- severity: document.severity,
471
- updatedAt: document.updatedAt,
472
- snippet: document.snippet,
473
- metadata: document.metadata,
474
- score: Number(signals.score.toFixed(6)),
475
- scoreBreakdown: {
476
- exactMatch: Number(signals.exactMatch.toFixed(6)),
477
- tokenOverlap: Number(signals.tokenOverlap.toFixed(6)),
478
- cosine: Number(signals.cosine.toFixed(6)),
479
- recency: Number(signals.recency.toFixed(6)),
480
- severity: Number(signals.severityScore.toFixed(6)),
481
- },
482
- };
483
- })
484
- .filter((result) => result.score >= normalizedMinScore)
485
- .sort((left, right) => {
486
- if (right.score !== left.score) {
487
- return right.score - left.score;
488
- }
489
- return left.documentId.localeCompare(right.documentId);
490
- })
491
- .slice(0, normalizedLimit);
492
-
493
- return {
494
- query: queryText,
495
- limit: normalizedLimit,
496
- minScore: normalizedMinScore,
497
- queryTokenCount: queryTokens.length,
498
- results,
499
- };
500
- }
501
-
502
- export async function queryHybridRetriever({
503
- query = "",
504
- documents = [],
505
- limit = 12,
506
- provider = "local",
507
- apiEndpoint = "",
508
- apiKey = "",
509
- fetchImpl = globalThis.fetch,
510
- } = {}) {
511
- const normalizedProvider = normalizeString(provider).toLowerCase() || "local";
512
- const localIndex = buildLocalHybridIndex(documents);
513
- const localQuery = queryLocalHybridIndex(localIndex, {
514
- query,
515
- limit,
516
- });
517
-
518
- const canUseApi =
519
- (normalizedProvider === "api" || normalizedProvider === "auto") &&
520
- normalizeString(apiEndpoint) &&
521
- typeof fetchImpl === "function";
522
- if (!canUseApi) {
523
- return {
524
- providerRequested: normalizedProvider,
525
- providerUsed: "local",
526
- apiFallback: false,
527
- apiError: "",
528
- indexSummary: {
529
- documentCount: localIndex.documentCount,
530
- vocabularySize: localIndex.vocabularySize,
531
- },
532
- results: localQuery.results,
533
- };
534
- }
535
-
536
- try {
537
- const response = await fetchImpl(String(apiEndpoint), {
538
- method: "POST",
539
- headers: {
540
- "content-type": "application/json",
541
- ...(normalizeString(apiKey) ? { authorization: `Bearer ${apiKey}` } : {}),
542
- },
543
- body: JSON.stringify({
544
- query: normalizeString(query),
545
- limit: Math.max(1, Math.floor(normalizeNumber(limit, 12))),
546
- documents,
547
- }),
548
- });
549
- if (!response.ok) {
550
- throw new Error(`Memory API request failed (${response.status}).`);
551
- }
552
- const payload = await response.json();
553
- const apiResults = Array.isArray(payload?.results) ? payload.results : [];
554
- if (apiResults.length === 0) {
555
- throw new Error("Memory API response missing results.");
556
- }
557
- return {
558
- providerRequested: normalizedProvider,
559
- providerUsed: "api",
560
- apiFallback: false,
561
- apiError: "",
562
- indexSummary: {
563
- documentCount: localIndex.documentCount,
564
- vocabularySize: localIndex.vocabularySize,
565
- },
566
- results: apiResults.slice(0, Math.max(1, Math.floor(normalizeNumber(limit, 12)))),
567
- };
568
- } catch (error) {
569
- return {
570
- providerRequested: normalizedProvider,
571
- providerUsed: "local",
572
- apiFallback: true,
573
- apiError: normalizeString(error?.message || error),
574
- indexSummary: {
575
- documentCount: localIndex.documentCount,
576
- vocabularySize: localIndex.vocabularySize,
577
- },
578
- results: localQuery.results,
579
- };
580
- }
581
- }
1
+ import fsp from "node:fs/promises";
2
+ import path from "node:path";
3
+
4
+ function normalizeString(value) {
5
+ return String(value || "").trim();
6
+ }
7
+
8
+ function normalizeNumber(value, fallback = 0) {
9
+ const parsed = Number(value);
10
+ if (!Number.isFinite(parsed)) {
11
+ return fallback;
12
+ }
13
+ return parsed;
14
+ }
15
+
16
+ function normalizeSeverity(value) {
17
+ const severity = normalizeString(value).toUpperCase();
18
+ if (severity === "P0" || severity === "P1" || severity === "P2" || severity === "P3") {
19
+ return severity;
20
+ }
21
+ return "P3";
22
+ }
23
+
24
+ export function tokenize(value) {
25
+ return Array.from(
26
+ new Set(
27
+ normalizeString(value)
28
+ .toLowerCase()
29
+ .split(/[^a-z0-9_]+/g)
30
+ .map((token) => token.trim())
31
+ .filter((token) => token.length >= 2)
32
+ )
33
+ );
34
+ }
35
+
36
+ function tokenizeWithFrequency(value) {
37
+ const tokens = normalizeString(value)
38
+ .toLowerCase()
39
+ .split(/[^a-z0-9_]+/g)
40
+ .map((token) => token.trim())
41
+ .filter((token) => token.length >= 2);
42
+ const frequency = new Map();
43
+ for (const token of tokens) {
44
+ frequency.set(token, (frequency.get(token) || 0) + 1);
45
+ }
46
+ return {
47
+ tokens,
48
+ uniqueTokens: Array.from(new Set(tokens)),
49
+ frequency,
50
+ };
51
+ }
52
+
53
+ function toPosixPath(value) {
54
+ return normalizeString(value).replace(/\\/g, "/");
55
+ }
56
+
57
+ function clipSnippet(text, maxLength = 180) {
58
+ const normalized = normalizeString(text).replace(/\s+/g, " ");
59
+ if (normalized.length <= maxLength) {
60
+ return normalized;
61
+ }
62
+ return `${normalized.slice(0, maxLength - 3)}...`;
63
+ }
64
+
65
+ const SEVERITY_SCORE = Object.freeze({
66
+ P0: 1,
67
+ P1: 0.9,
68
+ P2: 0.65,
69
+ P3: 0.4,
70
+ });
71
+
72
+ function parseTimestamp(value) {
73
+ const millis = Date.parse(value);
74
+ if (!Number.isFinite(millis)) {
75
+ return 0;
76
+ }
77
+ return millis;
78
+ }
79
+
80
+ function buildDocumentVector({ frequency, tokenCount, idfByToken }) {
81
+ const vector = new Map();
82
+ const denominator = Math.max(1, tokenCount);
83
+ for (const [token, count] of frequency.entries()) {
84
+ const idf = idfByToken.get(token) || 0;
85
+ const tf = count / denominator;
86
+ const weight = tf * idf;
87
+ if (weight > 0) {
88
+ vector.set(token, weight);
89
+ }
90
+ }
91
+ return vector;
92
+ }
93
+
94
+ function vectorMagnitude(vector) {
95
+ let sum = 0;
96
+ for (const value of vector.values()) {
97
+ sum += value * value;
98
+ }
99
+ return Math.sqrt(sum);
100
+ }
101
+
102
+ function cosineSimilarity(leftVector, leftMagnitude, rightVector, rightMagnitude) {
103
+ if (!leftVector || !rightVector || leftMagnitude <= 0 || rightMagnitude <= 0) {
104
+ return 0;
105
+ }
106
+ const [smaller, larger] =
107
+ leftVector.size <= rightVector.size ? [leftVector, rightVector] : [rightVector, leftVector];
108
+ let dot = 0;
109
+ for (const [token, value] of smaller.entries()) {
110
+ const counterpart = larger.get(token);
111
+ if (counterpart) {
112
+ dot += value * counterpart;
113
+ }
114
+ }
115
+ if (dot <= 0) {
116
+ return 0;
117
+ }
118
+ return dot / (leftMagnitude * rightMagnitude);
119
+ }
120
+
121
+ function buildDeterministicScore({ document, queryTokens, cosine, recency }) {
122
+ const tokenSet = document.tokenSet;
123
+ const overlapCount = queryTokens.filter((token) => tokenSet.has(token)).length;
124
+ const tokenOverlap = queryTokens.length > 0 ? overlapCount / queryTokens.length : 0;
125
+ const queryText = normalizeString(document.queryTextForExact).toLowerCase();
126
+ const exactMatch =
127
+ queryTokens.length > 0 &&
128
+ queryTokens.every((token) => queryText.includes(token))
129
+ ? 1
130
+ : 0;
131
+ const severityScore = SEVERITY_SCORE[normalizeSeverity(document.severity)] || 0.35;
132
+ return {
133
+ exactMatch,
134
+ tokenOverlap,
135
+ cosine,
136
+ recency,
137
+ severityScore,
138
+ score:
139
+ exactMatch * 0.25 +
140
+ tokenOverlap * 0.25 +
141
+ cosine * 0.3 +
142
+ recency * 0.1 +
143
+ severityScore * 0.1,
144
+ };
145
+ }
146
+
147
+ function resolveSpecCandidates({ targetPath, specFile }) {
148
+ if (normalizeString(specFile)) {
149
+ const absoluteCandidate = path.isAbsolute(specFile)
150
+ ? specFile
151
+ : path.join(targetPath, specFile);
152
+ return [absoluteCandidate];
153
+ }
154
+ return [path.join(targetPath, "SPEC.md"), path.join(targetPath, "docs", "spec.md")];
155
+ }
156
+
157
+ async function loadSpecDocument({ targetPath, specFile = "" }) {
158
+ const candidates = resolveSpecCandidates({ targetPath, specFile });
159
+ for (const candidate of candidates) {
160
+ try {
161
+ const text = await fsp.readFile(candidate, "utf-8");
162
+ const normalizedPath = toPosixPath(path.resolve(candidate));
163
+ return {
164
+ documentId: `spec:${normalizedPath}`,
165
+ sourceType: "spec",
166
+ sourcePath: normalizedPath,
167
+ severity: "P3",
168
+ updatedAt: new Date().toISOString(),
169
+ text,
170
+ metadata: {
171
+ category: "spec",
172
+ },
173
+ };
174
+ } catch {
175
+ // Continue to next candidate.
176
+ }
177
+ }
178
+ return null;
179
+ }
180
+
181
+ async function loadAuditHistoryDocuments({ outputRoot, excludeRunId = "", maxAuditRuns = 3 }) {
182
+ const auditsDirectory = path.join(outputRoot, "audits");
183
+ let runEntries = [];
184
+ try {
185
+ runEntries = await fsp.readdir(auditsDirectory, { withFileTypes: true });
186
+ } catch {
187
+ return [];
188
+ }
189
+ const runIds = runEntries
190
+ .filter((entry) => entry.isDirectory() && entry.name.startsWith("audit-"))
191
+ .map((entry) => entry.name)
192
+ .filter((runId) => runId !== normalizeString(excludeRunId))
193
+ .sort((left, right) => right.localeCompare(left))
194
+ .slice(0, Math.max(0, Math.floor(Number(maxAuditRuns || 0))));
195
+
196
+ const documents = [];
197
+ for (const runId of runIds) {
198
+ const reportPath = path.join(auditsDirectory, runId, "AUDIT_REPORT.json");
199
+ try {
200
+ const report = JSON.parse(await fsp.readFile(reportPath, "utf-8"));
201
+ documents.push({
202
+ documentId: `audit:${runId}:summary`,
203
+ sourceType: "audit-history",
204
+ sourcePath: toPosixPath(reportPath),
205
+ severity: report.summary?.blocking ? "P1" : "P3",
206
+ updatedAt: normalizeString(report.generatedAt) || new Date().toISOString(),
207
+ text: [
208
+ `run_id ${runId}`,
209
+ `summary p0 ${report.summary?.P0 || 0}`,
210
+ `p1 ${report.summary?.P1 || 0}`,
211
+ `p2 ${report.summary?.P2 || 0}`,
212
+ `p3 ${report.summary?.P3 || 0}`,
213
+ `risk_surfaces ${(report.ingest?.riskSurfaces || []).join(" ")}`,
214
+ ].join(" "),
215
+ metadata: {
216
+ category: "audit-summary",
217
+ runId,
218
+ },
219
+ });
220
+
221
+ let findingIndex = 0;
222
+ for (const agent of report.agentResults || []) {
223
+ for (const finding of agent.findings || []) {
224
+ if (findingIndex >= 60) {
225
+ break;
226
+ }
227
+ findingIndex += 1;
228
+ documents.push({
229
+ documentId: `audit:${runId}:finding:${findingIndex}`,
230
+ sourceType: "audit-history",
231
+ sourcePath: toPosixPath(reportPath),
232
+ severity: normalizeSeverity(finding.severity),
233
+ updatedAt: normalizeString(report.generatedAt) || new Date().toISOString(),
234
+ text: [finding.file, finding.layer, finding.ruleId, finding.message].filter(Boolean).join(" "),
235
+ metadata: {
236
+ category: "audit-finding",
237
+ runId,
238
+ agentId: agent.agentId,
239
+ file: toPosixPath(finding.file),
240
+ line: normalizeNumber(finding.line, 0),
241
+ },
242
+ });
243
+ }
244
+ }
245
+ } catch {
246
+ // Ignore malformed historical run artifacts.
247
+ }
248
+ }
249
+ return documents;
250
+ }
251
+
252
+ function buildIngestDocuments(ingest = {}) {
253
+ const summary = ingest.summary || {};
254
+ const frameworkList = Array.isArray(ingest.frameworks) ? ingest.frameworks : [];
255
+ const riskSurfaces = Array.isArray(ingest.riskSurfaces) ? ingest.riskSurfaces : [];
256
+ const documents = [
257
+ {
258
+ documentId: "ingest:summary",
259
+ sourceType: "ingest",
260
+ sourcePath: "",
261
+ severity: "P3",
262
+ updatedAt: new Date().toISOString(),
263
+ text: [
264
+ `files_scanned ${summary.filesScanned || 0}`,
265
+ `total_loc ${summary.totalLoc || 0}`,
266
+ `frameworks ${frameworkList.join(" ")}`,
267
+ ].join(" "),
268
+ metadata: {
269
+ category: "ingest-summary",
270
+ },
271
+ },
272
+ ];
273
+ for (const [index, surface] of riskSurfaces.entries()) {
274
+ const surfaceName = normalizeString(surface?.surface || surface);
275
+ if (!surfaceName) {
276
+ continue;
277
+ }
278
+ documents.push({
279
+ documentId: `ingest:surface:${index + 1}`,
280
+ sourceType: "ingest",
281
+ sourcePath: "",
282
+ severity: "P2",
283
+ updatedAt: new Date().toISOString(),
284
+ text: `risk_surface ${surfaceName}`,
285
+ metadata: {
286
+ category: "ingest-risk-surface",
287
+ surface: surfaceName,
288
+ },
289
+ });
290
+ }
291
+ return documents;
292
+ }
293
+
294
+ function buildLocalDocument(raw = {}, index = 0) {
295
+ const text = normalizeString(raw.text);
296
+ const tokenized = tokenizeWithFrequency(text);
297
+ const updatedAt = normalizeString(raw.updatedAt) || new Date().toISOString();
298
+ const sequence = index + 1;
299
+ return {
300
+ documentId: normalizeString(raw.documentId) || `doc-${String(sequence).padStart(6, "0")}`,
301
+ sourceType: normalizeString(raw.sourceType) || "memory",
302
+ sourcePath: toPosixPath(raw.sourcePath),
303
+ severity: normalizeSeverity(raw.severity),
304
+ updatedAt,
305
+ text,
306
+ snippet: clipSnippet(text),
307
+ metadata: raw.metadata && typeof raw.metadata === "object" ? raw.metadata : {},
308
+ sequence,
309
+ tokenSet: new Set(tokenized.uniqueTokens),
310
+ tokenFrequency: tokenized.frequency,
311
+ tokenCount: tokenized.tokens.length,
312
+ queryTextForExact: text,
313
+ timestampMs: parseTimestamp(updatedAt),
314
+ };
315
+ }
316
+
317
+ export function buildDocumentsFromBlackboardEntries(entries = []) {
318
+ return (entries || []).map((entry, index) => ({
319
+ documentId: `blackboard:${normalizeString(entry.entryId) || index + 1}`,
320
+ sourceType: "blackboard",
321
+ sourcePath: "",
322
+ severity: normalizeSeverity(entry.severity),
323
+ updatedAt: normalizeString(entry.timestamp) || new Date().toISOString(),
324
+ text: [
325
+ entry.agentId,
326
+ entry.source,
327
+ entry.file,
328
+ entry.layer,
329
+ entry.ruleId,
330
+ entry.message,
331
+ entry.note,
332
+ ]
333
+ .filter(Boolean)
334
+ .join(" "),
335
+ metadata: {
336
+ category: "blackboard-entry",
337
+ entryId: entry.entryId || "",
338
+ line: normalizeNumber(entry.line, 0),
339
+ file: toPosixPath(entry.file),
340
+ },
341
+ }));
342
+ }
343
+
344
+ export async function buildSharedMemoryCorpus({
345
+ outputRoot = "",
346
+ targetPath = "",
347
+ ingest = {},
348
+ specFile = "",
349
+ excludeRunId = "",
350
+ maxAuditRuns = 3,
351
+ } = {}) {
352
+ const resolvedOutputRoot = path.resolve(String(outputRoot || "."));
353
+ const resolvedTargetPath = path.resolve(String(targetPath || "."));
354
+
355
+ const [historyDocuments, specDocument] = await Promise.all([
356
+ loadAuditHistoryDocuments({
357
+ outputRoot: resolvedOutputRoot,
358
+ excludeRunId,
359
+ maxAuditRuns,
360
+ }),
361
+ loadSpecDocument({
362
+ targetPath: resolvedTargetPath,
363
+ specFile,
364
+ }),
365
+ ]);
366
+
367
+ const documents = [
368
+ ...buildIngestDocuments(ingest),
369
+ ...historyDocuments,
370
+ ...(specDocument ? [specDocument] : []),
371
+ ];
372
+
373
+ const sourceCounts = documents.reduce(
374
+ (counts, document) => {
375
+ const key = normalizeString(document.sourceType) || "unknown";
376
+ counts[key] = (counts[key] || 0) + 1;
377
+ return counts;
378
+ },
379
+ {}
380
+ );
381
+
382
+ return {
383
+ documents,
384
+ sourceCounts,
385
+ hasSpecDocument: Boolean(specDocument),
386
+ historyRunDocumentCount: historyDocuments.length,
387
+ };
388
+ }
389
+
390
+ export function buildLocalHybridIndex(documents = []) {
391
+ const preparedDocuments = (documents || [])
392
+ .map((document, index) => buildLocalDocument(document, index))
393
+ .filter((document) => document.text.length > 0);
394
+
395
+ const documentCount = preparedDocuments.length;
396
+ const docFrequency = new Map();
397
+ for (const document of preparedDocuments) {
398
+ for (const token of document.tokenSet.values()) {
399
+ docFrequency.set(token, (docFrequency.get(token) || 0) + 1);
400
+ }
401
+ }
402
+
403
+ const idfByToken = new Map();
404
+ for (const [token, frequency] of docFrequency.entries()) {
405
+ const idf = Math.log((documentCount + 1) / (frequency + 1)) + 1;
406
+ idfByToken.set(token, idf);
407
+ }
408
+
409
+ const timestampValues = preparedDocuments
410
+ .map((document) => document.timestampMs)
411
+ .filter((value) => Number.isFinite(value) && value > 0);
412
+ const minTimestamp = timestampValues.length > 0 ? Math.min(...timestampValues) : 0;
413
+ const maxTimestamp = timestampValues.length > 0 ? Math.max(...timestampValues) : 0;
414
+
415
+ const entries = preparedDocuments.map((document) => {
416
+ const vector = buildDocumentVector({
417
+ frequency: document.tokenFrequency,
418
+ tokenCount: document.tokenCount,
419
+ idfByToken,
420
+ });
421
+ const magnitude = vectorMagnitude(vector);
422
+ let recency = 0.5;
423
+ if (maxTimestamp > minTimestamp && document.timestampMs > 0) {
424
+ recency = (document.timestampMs - minTimestamp) / (maxTimestamp - minTimestamp);
425
+ }
426
+ return {
427
+ ...document,
428
+ vector,
429
+ magnitude,
430
+ recency,
431
+ };
432
+ });
433
+
434
+ return {
435
+ schemaVersion: "1.0.0",
436
+ generatedAt: new Date().toISOString(),
437
+ documentCount,
438
+ vocabularySize: idfByToken.size,
439
+ idfByToken,
440
+ entries,
441
+ };
442
+ }
443
+
444
+ export function queryLocalHybridIndex(index, { query = "", limit = 12, minScore = 0 } = {}) {
445
+ const queryText = normalizeString(query);
446
+ const queryTokenized = tokenizeWithFrequency(queryText);
447
+ const queryTokens = queryTokenized.uniqueTokens;
448
+ const queryVector = buildDocumentVector({
449
+ frequency: queryTokenized.frequency,
450
+ tokenCount: queryTokenized.tokens.length,
451
+ idfByToken: index.idfByToken || new Map(),
452
+ });
453
+ const queryMagnitude = vectorMagnitude(queryVector);
454
+
455
+ const normalizedLimit = Math.max(1, Math.floor(normalizeNumber(limit, 12)));
456
+ const normalizedMinScore = Math.max(0, Math.min(1, normalizeNumber(minScore, 0)));
457
+ const results = (index.entries || [])
458
+ .map((document) => {
459
+ const cosine = cosineSimilarity(queryVector, queryMagnitude, document.vector, document.magnitude);
460
+ const signals = buildDeterministicScore({
461
+ document,
462
+ queryTokens,
463
+ cosine,
464
+ recency: document.recency,
465
+ });
466
+ return {
467
+ documentId: document.documentId,
468
+ sourceType: document.sourceType,
469
+ sourcePath: document.sourcePath,
470
+ severity: document.severity,
471
+ updatedAt: document.updatedAt,
472
+ snippet: document.snippet,
473
+ metadata: document.metadata,
474
+ score: Number(signals.score.toFixed(6)),
475
+ scoreBreakdown: {
476
+ exactMatch: Number(signals.exactMatch.toFixed(6)),
477
+ tokenOverlap: Number(signals.tokenOverlap.toFixed(6)),
478
+ cosine: Number(signals.cosine.toFixed(6)),
479
+ recency: Number(signals.recency.toFixed(6)),
480
+ severity: Number(signals.severityScore.toFixed(6)),
481
+ },
482
+ };
483
+ })
484
+ .filter((result) => result.score >= normalizedMinScore)
485
+ .sort((left, right) => {
486
+ if (right.score !== left.score) {
487
+ return right.score - left.score;
488
+ }
489
+ return left.documentId.localeCompare(right.documentId);
490
+ })
491
+ .slice(0, normalizedLimit);
492
+
493
+ return {
494
+ query: queryText,
495
+ limit: normalizedLimit,
496
+ minScore: normalizedMinScore,
497
+ queryTokenCount: queryTokens.length,
498
+ results,
499
+ };
500
+ }
501
+
502
+ export async function queryHybridRetriever({
503
+ query = "",
504
+ documents = [],
505
+ limit = 12,
506
+ provider = "local",
507
+ apiEndpoint = "",
508
+ apiKey = "",
509
+ fetchImpl = globalThis.fetch,
510
+ } = {}) {
511
+ const normalizedProvider = normalizeString(provider).toLowerCase() || "local";
512
+ const localIndex = buildLocalHybridIndex(documents);
513
+ const localQuery = queryLocalHybridIndex(localIndex, {
514
+ query,
515
+ limit,
516
+ });
517
+
518
+ const canUseApi =
519
+ (normalizedProvider === "api" || normalizedProvider === "auto") &&
520
+ normalizeString(apiEndpoint) &&
521
+ typeof fetchImpl === "function";
522
+ if (!canUseApi) {
523
+ return {
524
+ providerRequested: normalizedProvider,
525
+ providerUsed: "local",
526
+ apiFallback: false,
527
+ apiError: "",
528
+ indexSummary: {
529
+ documentCount: localIndex.documentCount,
530
+ vocabularySize: localIndex.vocabularySize,
531
+ },
532
+ results: localQuery.results,
533
+ };
534
+ }
535
+
536
+ try {
537
+ const response = await fetchImpl(String(apiEndpoint), {
538
+ method: "POST",
539
+ headers: {
540
+ "content-type": "application/json",
541
+ ...(normalizeString(apiKey) ? { authorization: `Bearer ${apiKey}` } : {}),
542
+ },
543
+ body: JSON.stringify({
544
+ query: normalizeString(query),
545
+ limit: Math.max(1, Math.floor(normalizeNumber(limit, 12))),
546
+ documents,
547
+ }),
548
+ });
549
+ if (!response.ok) {
550
+ throw new Error(`Memory API request failed (${response.status}).`);
551
+ }
552
+ const payload = await response.json();
553
+ const apiResults = Array.isArray(payload?.results) ? payload.results : [];
554
+ if (apiResults.length === 0) {
555
+ throw new Error("Memory API response missing results.");
556
+ }
557
+ return {
558
+ providerRequested: normalizedProvider,
559
+ providerUsed: "api",
560
+ apiFallback: false,
561
+ apiError: "",
562
+ indexSummary: {
563
+ documentCount: localIndex.documentCount,
564
+ vocabularySize: localIndex.vocabularySize,
565
+ },
566
+ results: apiResults.slice(0, Math.max(1, Math.floor(normalizeNumber(limit, 12)))),
567
+ };
568
+ } catch (error) {
569
+ return {
570
+ providerRequested: normalizedProvider,
571
+ providerUsed: "local",
572
+ apiFallback: true,
573
+ apiError: normalizeString(error?.message || error),
574
+ indexSummary: {
575
+ documentCount: localIndex.documentCount,
576
+ vocabularySize: localIndex.vocabularySize,
577
+ },
578
+ results: localQuery.results,
579
+ };
580
+ }
581
+ }