sentinelayer-cli 0.1.2 → 0.4.4

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 (129) hide show
  1. package/README.md +998 -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 +63 -54
  6. package/src/agents/jules/config/definition.js +209 -209
  7. package/src/agents/jules/config/system-prompt.js +175 -175
  8. package/src/agents/jules/error-intake.js +51 -51
  9. package/src/agents/jules/fix-cycle.js +377 -377
  10. package/src/agents/jules/loop.js +367 -367
  11. package/src/agents/jules/pulse.js +327 -319
  12. package/src/agents/jules/stream.js +186 -186
  13. package/src/agents/jules/swarm/file-scanner.js +74 -74
  14. package/src/agents/jules/swarm/index.js +11 -11
  15. package/src/agents/jules/swarm/orchestrator.js +362 -362
  16. package/src/agents/jules/swarm/pattern-hunter.js +123 -123
  17. package/src/agents/jules/swarm/sub-agent.js +308 -308
  18. package/src/agents/jules/tools/auth-audit.js +557 -222
  19. package/src/agents/jules/tools/dispatch.js +327 -327
  20. package/src/agents/jules/tools/file-edit.js +180 -180
  21. package/src/agents/jules/tools/file-read.js +100 -100
  22. package/src/agents/jules/tools/frontend-analyze.js +570 -570
  23. package/src/agents/jules/tools/glob.js +168 -168
  24. package/src/agents/jules/tools/grep.js +228 -228
  25. package/src/agents/jules/tools/index.js +29 -29
  26. package/src/agents/jules/tools/path-guards.js +161 -161
  27. package/src/agents/jules/tools/runtime-audit.js +503 -493
  28. package/src/agents/jules/tools/shell.js +383 -383
  29. package/src/agents/jules/tools/url-policy.js +100 -0
  30. package/src/ai/aidenid.js +972 -945
  31. package/src/ai/client.js +508 -508
  32. package/src/ai/domain-target-store.js +268 -268
  33. package/src/ai/identity-store.js +270 -270
  34. package/src/ai/site-store.js +145 -145
  35. package/src/audit/agents/architecture.js +180 -180
  36. package/src/audit/agents/compliance.js +179 -179
  37. package/src/audit/agents/documentation.js +165 -165
  38. package/src/audit/agents/performance.js +145 -145
  39. package/src/audit/agents/security.js +215 -215
  40. package/src/audit/agents/testing.js +172 -172
  41. package/src/audit/orchestrator.js +557 -557
  42. package/src/audit/package.js +204 -204
  43. package/src/audit/registry.js +284 -284
  44. package/src/audit/replay.js +103 -103
  45. package/src/auth/gate.js +45 -11
  46. package/src/auth/http.js +270 -113
  47. package/src/auth/service.js +891 -848
  48. package/src/auth/session-store.js +359 -345
  49. package/src/cli.js +252 -252
  50. package/src/commands/ai/identity-lifecycle.js +1338 -1337
  51. package/src/commands/ai/provision-governance.js +1272 -1246
  52. package/src/commands/ai/shared.js +147 -147
  53. package/src/commands/ai.js +11 -11
  54. package/src/commands/apply.js +12 -12
  55. package/src/commands/audit.js +1166 -1166
  56. package/src/commands/auth.js +375 -366
  57. package/src/commands/chat.js +191 -191
  58. package/src/commands/config.js +184 -184
  59. package/src/commands/cost.js +311 -311
  60. package/src/commands/daemon/core.js +850 -850
  61. package/src/commands/daemon/extended.js +1048 -1048
  62. package/src/commands/daemon/shared.js +213 -213
  63. package/src/commands/daemon.js +11 -11
  64. package/src/commands/guide.js +174 -174
  65. package/src/commands/ingest.js +58 -58
  66. package/src/commands/init.js +55 -55
  67. package/src/commands/legacy-args.js +10 -10
  68. package/src/commands/mcp.js +461 -404
  69. package/src/commands/omargate.js +15 -15
  70. package/src/commands/persona.js +20 -20
  71. package/src/commands/plugin.js +260 -260
  72. package/src/commands/policy.js +132 -132
  73. package/src/commands/prompt.js +238 -238
  74. package/src/commands/review.js +704 -704
  75. package/src/commands/scan.js +866 -788
  76. package/src/commands/spec.js +716 -716
  77. package/src/commands/swarm.js +651 -651
  78. package/src/commands/telemetry.js +202 -202
  79. package/src/commands/watch.js +510 -510
  80. package/src/config/agent-dictionary.js +182 -182
  81. package/src/config/io.js +56 -56
  82. package/src/config/paths.js +18 -18
  83. package/src/config/schema.js +55 -55
  84. package/src/config/service.js +184 -184
  85. package/src/cost/budget.js +235 -235
  86. package/src/cost/history.js +188 -188
  87. package/src/cost/tracker.js +171 -171
  88. package/src/daemon/artifact-lineage.js +534 -534
  89. package/src/daemon/assignment-ledger.js +770 -770
  90. package/src/daemon/ast-parser-layer.js +258 -258
  91. package/src/daemon/budget-governor.js +633 -633
  92. package/src/daemon/callgraph-overlay.js +646 -646
  93. package/src/daemon/error-worker.js +626 -626
  94. package/src/daemon/hybrid-mapper.js +929 -929
  95. package/src/daemon/jira-lifecycle.js +632 -632
  96. package/src/daemon/operator-control.js +657 -657
  97. package/src/daemon/reliability-lane.js +471 -471
  98. package/src/daemon/watchdog.js +971 -971
  99. package/src/guide/generator.js +316 -316
  100. package/src/ingest/engine.js +918 -918
  101. package/src/legacy-cli.js +2592 -2435
  102. package/src/mcp/registry.js +695 -695
  103. package/src/memory/blackboard.js +301 -301
  104. package/src/memory/retrieval.js +581 -581
  105. package/src/plugin/manifest.js +553 -553
  106. package/src/policy/packs.js +144 -144
  107. package/src/prompt/generator.js +118 -106
  108. package/src/review/ai-review.js +669 -669
  109. package/src/review/local-review.js +1295 -1284
  110. package/src/review/replay.js +235 -235
  111. package/src/review/report.js +664 -664
  112. package/src/review/spec-binding.js +487 -487
  113. package/src/scaffold/generator.js +67 -0
  114. package/src/scaffold/templates.js +150 -0
  115. package/src/scan/generator.js +418 -351
  116. package/src/scan/gh-secrets.js +107 -0
  117. package/src/spec/generator.js +519 -519
  118. package/src/spec/regenerate.js +237 -237
  119. package/src/spec/templates.js +91 -91
  120. package/src/swarm/dashboard.js +247 -247
  121. package/src/swarm/factory.js +363 -363
  122. package/src/swarm/pentest.js +934 -934
  123. package/src/swarm/registry.js +419 -419
  124. package/src/swarm/report.js +158 -158
  125. package/src/swarm/runtime.js +576 -576
  126. package/src/swarm/scenario-dsl.js +272 -272
  127. package/src/telemetry/ledger.js +302 -302
  128. package/src/telemetry/sync.js +107 -61
  129. 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
+ }