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.
- package/README.md +1009 -996
- package/bin/create-sentinelayer.js +5 -5
- package/bin/sentinelayer-cli.js +4 -4
- package/bin/sl.js +5 -5
- package/package.json +64 -63
- package/src/agents/ai-governance/index.js +12 -0
- package/src/agents/ai-governance/tools/base.js +171 -0
- package/src/agents/ai-governance/tools/eval-regression.js +47 -0
- package/src/agents/ai-governance/tools/hitl-audit.js +81 -0
- package/src/agents/ai-governance/tools/index.js +52 -0
- package/src/agents/ai-governance/tools/prompt-drift.js +42 -0
- package/src/agents/ai-governance/tools/provenance-check.js +69 -0
- package/src/agents/backend/index.js +12 -0
- package/src/agents/backend/tools/base.js +189 -0
- package/src/agents/backend/tools/circuit-breaker-check.js +123 -0
- package/src/agents/backend/tools/idempotency-audit.js +105 -0
- package/src/agents/backend/tools/index.js +87 -0
- package/src/agents/backend/tools/retry-audit.js +132 -0
- package/src/agents/backend/tools/timeout-audit.js +144 -0
- package/src/agents/code-quality/index.js +12 -0
- package/src/agents/code-quality/tools/base.js +159 -0
- package/src/agents/code-quality/tools/complexity-measure.js +197 -0
- package/src/agents/code-quality/tools/coupling-analysis.js +81 -0
- package/src/agents/code-quality/tools/cycle-detect.js +49 -0
- package/src/agents/code-quality/tools/dep-graph.js +196 -0
- package/src/agents/code-quality/tools/index.js +89 -0
- package/src/agents/data-layer/index.js +12 -0
- package/src/agents/data-layer/tools/base.js +181 -0
- package/src/agents/data-layer/tools/index-audit.js +165 -0
- package/src/agents/data-layer/tools/index.js +83 -0
- package/src/agents/data-layer/tools/migration-scan.js +135 -0
- package/src/agents/data-layer/tools/query-explain.js +120 -0
- package/src/agents/data-layer/tools/tenancy-scan.js +166 -0
- package/src/agents/documentation/index.js +12 -0
- package/src/agents/documentation/tools/api-diff.js +91 -0
- package/src/agents/documentation/tools/base.js +151 -0
- package/src/agents/documentation/tools/dead-link-check.js +58 -0
- package/src/agents/documentation/tools/docstring-coverage.js +78 -0
- package/src/agents/documentation/tools/index.js +52 -0
- package/src/agents/documentation/tools/readme-freshness.js +61 -0
- package/src/agents/envelope/fix-cycle.js +45 -0
- package/src/agents/envelope/index.js +31 -0
- package/src/agents/envelope/loop.js +150 -0
- package/src/agents/envelope/pulse.js +18 -0
- package/src/agents/envelope/stream.js +40 -0
- package/src/agents/infrastructure/index.js +12 -0
- package/src/agents/infrastructure/tools/base.js +171 -0
- package/src/agents/infrastructure/tools/checkov-run.js +32 -0
- package/src/agents/infrastructure/tools/drift-detect.js +59 -0
- package/src/agents/infrastructure/tools/iam-least-priv-check.js +78 -0
- package/src/agents/infrastructure/tools/index.js +52 -0
- package/src/agents/infrastructure/tools/tflint-run.js +31 -0
- package/src/agents/jules/config/definition.js +160 -160
- package/src/agents/jules/config/system-prompt.js +182 -182
- package/src/agents/jules/error-intake.js +51 -51
- package/src/agents/jules/fix-cycle.js +17 -17
- package/src/agents/jules/loop.js +460 -450
- package/src/agents/jules/pulse.js +10 -10
- package/src/agents/jules/stream.js +187 -186
- package/src/agents/jules/swarm/file-scanner.js +74 -74
- package/src/agents/jules/swarm/index.js +11 -11
- package/src/agents/jules/swarm/orchestrator.js +362 -362
- package/src/agents/jules/swarm/pattern-hunter.js +123 -123
- package/src/agents/jules/swarm/sub-agent.js +315 -309
- package/src/agents/jules/tools/aidenid-email.js +189 -189
- package/src/agents/jules/tools/auth-audit.js +1708 -1691
- package/src/agents/jules/tools/dispatch.js +340 -335
- package/src/agents/jules/tools/file-edit.js +2 -2
- package/src/agents/jules/tools/file-read.js +2 -2
- package/src/agents/jules/tools/frontend-analyze.js +570 -570
- package/src/agents/jules/tools/glob.js +2 -2
- package/src/agents/jules/tools/grep.js +2 -2
- package/src/agents/jules/tools/index.js +29 -29
- package/src/agents/jules/tools/path-guards.js +2 -2
- package/src/agents/jules/tools/runtime-audit.js +507 -507
- package/src/agents/jules/tools/shell.js +2 -2
- package/src/agents/jules/tools/url-policy.js +100 -100
- package/src/agents/mode.js +113 -0
- package/src/agents/observability/index.js +12 -0
- package/src/agents/observability/tools/alert-audit.js +39 -0
- package/src/agents/observability/tools/base.js +181 -0
- package/src/agents/observability/tools/dashboard-gap.js +42 -0
- package/src/agents/observability/tools/index.js +54 -0
- package/src/agents/observability/tools/log-schema-check.js +74 -0
- package/src/agents/observability/tools/span-coverage.js +74 -0
- package/src/agents/persona-visuals.js +102 -61
- package/src/agents/release/index.js +12 -0
- package/src/agents/release/tools/base.js +181 -0
- package/src/agents/release/tools/changelog-diff.js +86 -0
- package/src/agents/release/tools/feature-flag-audit.js +126 -0
- package/src/agents/release/tools/index.js +61 -0
- package/src/agents/release/tools/rollback-verify.js +129 -0
- package/src/agents/release/tools/semver-check.js +109 -0
- package/src/agents/reliability/index.js +12 -0
- package/src/agents/reliability/tools/backpressure-check.js +129 -0
- package/src/agents/reliability/tools/base.js +181 -0
- package/src/agents/reliability/tools/chaos-probe.js +109 -0
- package/src/agents/reliability/tools/graceful-degradation-check.js +114 -0
- package/src/agents/reliability/tools/health-check-audit.js +111 -0
- package/src/agents/reliability/tools/index.js +87 -0
- package/src/agents/run-persona.js +109 -0
- package/src/agents/security/index.js +12 -0
- package/src/agents/security/tools/authz-audit.js +134 -0
- package/src/agents/security/tools/base.js +190 -0
- package/src/agents/security/tools/crypto-review.js +175 -0
- package/src/agents/security/tools/index.js +97 -0
- package/src/agents/security/tools/sast-scan.js +175 -0
- package/src/agents/security/tools/secrets-scan.js +216 -0
- package/src/agents/shared-tools/dispatch-core.js +320 -315
- package/src/agents/shared-tools/file-edit.js +180 -180
- package/src/agents/shared-tools/file-read.js +100 -100
- package/src/agents/shared-tools/glob.js +168 -168
- package/src/agents/shared-tools/grep.js +228 -228
- package/src/agents/shared-tools/index.js +46 -46
- package/src/agents/shared-tools/path-guards.js +161 -161
- package/src/agents/shared-tools/shell.js +383 -383
- package/src/agents/supply-chain/index.js +12 -0
- package/src/agents/supply-chain/tools/attestation-check.js +42 -0
- package/src/agents/supply-chain/tools/base.js +151 -0
- package/src/agents/supply-chain/tools/index.js +52 -0
- package/src/agents/supply-chain/tools/lockfile-integrity.js +73 -0
- package/src/agents/supply-chain/tools/package-verify.js +56 -0
- package/src/agents/supply-chain/tools/sbom-diff.js +34 -0
- package/src/agents/testing/index.js +12 -0
- package/src/agents/testing/tools/base.js +202 -0
- package/src/agents/testing/tools/coverage-gap.js +144 -0
- package/src/agents/testing/tools/flake-detect.js +125 -0
- package/src/agents/testing/tools/index.js +85 -0
- package/src/agents/testing/tools/mutation-test.js +143 -0
- package/src/agents/testing/tools/snapshot-diff.js +103 -0
- package/src/ai/aidenid.js +1021 -1009
- package/src/ai/client.js +553 -553
- package/src/ai/domain-target-store.js +268 -268
- package/src/ai/identity-store.js +270 -270
- package/src/ai/proxy.js +137 -137
- package/src/ai/site-store.js +145 -145
- package/src/audit/agents/architecture.js +180 -180
- package/src/audit/agents/compliance.js +179 -179
- package/src/audit/agents/documentation.js +165 -165
- package/src/audit/agents/performance.js +145 -145
- package/src/audit/agents/security.js +215 -215
- package/src/audit/agents/testing.js +172 -172
- package/src/audit/orchestrator.js +557 -557
- package/src/audit/package.js +204 -204
- package/src/audit/registry.js +284 -284
- package/src/audit/replay.js +103 -103
- package/src/auth/gate.js +428 -371
- package/src/auth/http.js +681 -611
- package/src/auth/service.js +1106 -1106
- package/src/auth/session-store.js +813 -813
- package/src/cli.js +257 -252
- package/src/commands/ai/identity-lifecycle.js +1338 -1338
- package/src/commands/ai/provision-governance.js +1272 -1272
- package/src/commands/ai/shared.js +147 -147
- package/src/commands/ai.js +11 -11
- package/src/commands/apply.js +12 -12
- package/src/commands/audit.js +1171 -1166
- package/src/commands/auth.js +419 -419
- package/src/commands/chat.js +184 -191
- package/src/commands/config.js +184 -184
- package/src/commands/cost.js +311 -311
- package/src/commands/daemon/core.js +850 -850
- package/src/commands/daemon/extended.js +1048 -1048
- package/src/commands/daemon/shared.js +213 -213
- package/src/commands/daemon.js +11 -11
- package/src/commands/guide.js +174 -174
- package/src/commands/ingest.js +58 -58
- package/src/commands/init.js +55 -55
- package/src/commands/legacy-args.js +20 -10
- package/src/commands/mcp.js +461 -461
- package/src/commands/omargate.js +63 -29
- package/src/commands/persona.js +65 -20
- package/src/commands/plugin.js +260 -260
- package/src/commands/policy.js +132 -132
- package/src/commands/prompt.js +238 -238
- package/src/commands/review.js +704 -704
- package/src/commands/scan.js +865 -872
- package/src/commands/session.js +1238 -0
- package/src/commands/spec.js +771 -716
- package/src/commands/swarm.js +651 -651
- package/src/commands/telemetry.js +202 -202
- package/src/commands/watch.js +511 -511
- package/src/config/agent-dictionary.js +182 -182
- package/src/config/io.js +56 -56
- package/src/config/paths.js +18 -18
- package/src/config/schema.js +55 -55
- package/src/config/service.js +184 -184
- package/src/coord/events-log.js +141 -0
- package/src/coord/handshake.js +719 -0
- package/src/coord/index.js +35 -0
- package/src/coord/paths.js +84 -0
- package/src/coord/priority.js +62 -0
- package/src/coord/tarjan.js +157 -0
- package/src/cost/budget.js +235 -235
- package/src/cost/history.js +188 -188
- package/src/cost/tokenizer.js +160 -0
- package/src/cost/tracker.js +232 -171
- package/src/daemon/artifact-lineage.js +896 -534
- package/src/daemon/assignment-ledger.js +1083 -770
- package/src/daemon/ast-drift.js +496 -0
- package/src/daemon/ast-parser-layer.js +258 -258
- package/src/daemon/budget-governor.js +633 -633
- package/src/daemon/callgraph-overlay.js +646 -646
- package/src/daemon/error-worker.js +1209 -626
- package/src/daemon/fix-cycle.js +384 -377
- package/src/daemon/hybrid-mapper.js +929 -929
- package/src/daemon/ingest-refresh.js +79 -11
- package/src/daemon/jira-lifecycle.js +767 -632
- package/src/daemon/operator-control.js +657 -657
- package/src/daemon/pulse.js +327 -327
- package/src/daemon/reliability-lane.js +471 -471
- package/src/daemon/scope-engine.js +1068 -0
- package/src/daemon/watchdog.js +971 -971
- package/src/events/schema.js +190 -0
- package/src/guide/generator.js +316 -316
- package/src/ingest/engine.js +933 -918
- package/src/ingest/ownership.js +380 -0
- package/src/interactive/index.js +97 -97
- package/src/legacy-cli.js +3228 -2994
- package/src/mcp/registry.js +695 -695
- package/src/memory/blackboard.js +301 -301
- package/src/memory/retrieval.js +581 -581
- package/src/orchestrator/kai-chen.js +126 -0
- package/src/plugin/manifest.js +553 -553
- package/src/policy/packs.js +144 -144
- package/src/prompt/generator.js +136 -118
- package/src/review/ai-review.js +672 -679
- package/src/review/compliance-pack.js +389 -0
- package/src/review/investor-dd-config.js +54 -0
- package/src/review/investor-dd-file-loop.js +303 -0
- package/src/review/investor-dd-file-router.js +406 -0
- package/src/review/investor-dd-html-report.js +233 -0
- package/src/review/investor-dd-notification.js +120 -0
- package/src/review/investor-dd-orchestrator.js +405 -0
- package/src/review/investor-dd-persona-runner.js +275 -0
- package/src/review/live-validator.js +253 -0
- package/src/review/local-review.js +1351 -1305
- package/src/review/omargate-interactive.js +68 -68
- package/src/review/omargate-orchestrator.js +492 -300
- package/src/review/persona-prompts.js +484 -296
- package/src/review/reconciliation-rules.js +329 -0
- package/src/review/replay.js +235 -235
- package/src/review/report.js +664 -664
- package/src/review/reproducibility-chain.js +136 -0
- package/src/review/scan-modes.js +147 -42
- package/src/review/spec-binding.js +487 -487
- package/src/scaffold/generator.js +67 -67
- package/src/scaffold/templates.js +150 -150
- package/src/scan/generator.js +418 -418
- package/src/scan/gh-secrets.js +107 -107
- package/src/session/agent-registry.js +359 -0
- package/src/session/analytics.js +479 -0
- package/src/session/daemon.js +1396 -0
- package/src/session/file-locks.js +666 -0
- package/src/session/paths.js +37 -0
- package/src/session/recap.js +567 -0
- package/src/session/redact.js +82 -0
- package/src/session/runtime-bridge.js +762 -0
- package/src/session/scoring.js +406 -0
- package/src/session/setup-guides.js +304 -0
- package/src/session/store.js +704 -0
- package/src/session/stream.js +333 -0
- package/src/session/sync.js +753 -0
- package/src/session/tasks.js +1054 -0
- package/src/session/templates.js +188 -0
- package/src/spec/generator.js +619 -519
- package/src/spec/regenerate.js +237 -237
- package/src/spec/templates.js +91 -91
- package/src/swarm/dashboard.js +247 -247
- package/src/swarm/factory.js +363 -363
- package/src/swarm/pentest.js +934 -934
- package/src/swarm/registry.js +419 -419
- package/src/swarm/report.js +158 -158
- package/src/swarm/runtime.js +569 -576
- package/src/swarm/scenario-dsl.js +272 -272
- package/src/telemetry/ledger.js +302 -302
- package/src/telemetry/session-tracker.js +234 -234
- package/src/telemetry/sync.js +203 -203
- package/src/ui/command-hints.js +13 -13
- package/src/ui/markdown.js +220 -220
package/src/memory/retrieval.js
CHANGED
|
@@ -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
|
+
}
|