@blackbelt-technology/pi-dashboard-kb 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -0
- package/bin/kb.mjs +84 -0
- package/bin/lib/engine-fingerprint.mjs +183 -0
- package/dist/adoc-chunker.d.ts +29 -0
- package/dist/adoc-chunker.js +274 -0
- package/dist/archive-guard.d.ts +12 -0
- package/dist/archive-guard.js +154 -0
- package/dist/chunker.d.ts +3 -0
- package/dist/chunker.js +5 -3
- package/dist/cli.js +137 -19
- package/dist/config.d.ts +49 -0
- package/dist/config.js +43 -6
- package/dist/dox-triage.d.ts +67 -0
- package/dist/dox-triage.js +183 -0
- package/dist/dox.d.ts +68 -7
- package/dist/dox.js +370 -94
- package/dist/eval.d.ts +30 -0
- package/dist/eval.js +120 -13
- package/dist/gitignore.d.ts +24 -0
- package/dist/gitignore.js +205 -0
- package/dist/index.d.ts +25 -17
- package/dist/index.js +17 -8
- package/dist/indexer.d.ts +9 -1
- package/dist/indexer.js +40 -11
- package/dist/net-guard.d.ts +35 -0
- package/dist/net-guard.js +285 -0
- package/dist/render.js +37 -2
- package/dist/search-opts.d.ts +22 -0
- package/dist/search-opts.js +21 -0
- package/dist/sources.d.ts +29 -0
- package/dist/sources.js +230 -32
- package/dist/sqlite-store.d.ts +72 -2
- package/dist/sqlite-store.js +482 -78
- package/dist/staleness.d.ts +25 -0
- package/dist/staleness.js +69 -0
- package/dist/trust.d.ts +33 -1
- package/dist/trust.js +72 -5
- package/dist/types.d.ts +59 -0
- package/dist/verdict.d.ts +45 -0
- package/dist/verdict.js +368 -0
- package/engine-fingerprint.json +5 -0
- package/package.json +8 -4
- package/src/adoc-chunker.ts +308 -0
- package/src/archive-guard.ts +144 -0
- package/src/chunker.ts +5 -3
- package/src/cli.ts +130 -20
- package/src/config.ts +81 -6
- package/src/dox-triage.ts +231 -0
- package/src/dox.ts +390 -70
- package/src/eval.ts +136 -14
- package/src/gitignore.ts +215 -0
- package/src/index.ts +45 -20
- package/src/indexer.ts +46 -10
- package/src/net-guard.ts +288 -0
- package/src/render.ts +35 -3
- package/src/search-opts.ts +52 -0
- package/src/sources.ts +232 -28
- package/src/sqlite-store.ts +448 -67
- package/src/staleness.ts +76 -0
- package/src/trust.ts +81 -8
- package/src/types.ts +58 -0
- package/src/verdict.ts +412 -0
- package/src/AGENTS.md +0 -23
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// SSRF guard for KB remote sources (change: harden-untrusted-content-ingestion,
|
|
2
|
+
// design D2/D3). Zero deps — kb stays self-contained.
|
|
3
|
+
//
|
|
4
|
+
// - `isNonPublicAddress`: one canonical classifier (net.BlockList) incl. every
|
|
5
|
+
// IPv6-embedded IPv4 form.
|
|
6
|
+
// - `guardedLookup`: dns.lookup that rejects non-public answers; passed as the
|
|
7
|
+
// `lookup` option of https.request so the check is on the address the socket
|
|
8
|
+
// actually uses (closes DNS rebinding).
|
|
9
|
+
// - `guardedFetch`: https-only fetch with manual, re-validated redirects, a
|
|
10
|
+
// cumulative byte/time budget, bounded decompression, 2xx-only.
|
|
11
|
+
import dns from "node:dns";
|
|
12
|
+
import https from "node:https";
|
|
13
|
+
import { BlockList, isIP } from "node:net";
|
|
14
|
+
import { createBrotliDecompress, createGunzip, createInflate, createInflateRaw } from "node:zlib";
|
|
15
|
+
// ── address classifier ───────────────────────────────────────────────────────
|
|
16
|
+
const V4_BLOCKED = [
|
|
17
|
+
["0.0.0.0", 8], ["10.0.0.0", 8], ["100.64.0.0", 10], ["127.0.0.0", 8], ["169.254.0.0", 16],
|
|
18
|
+
["172.16.0.0", 12], ["192.0.0.0", 24], ["192.0.2.0", 24], ["192.88.99.0", 24], ["192.168.0.0", 16],
|
|
19
|
+
["198.18.0.0", 15], ["198.51.100.0", 24], ["203.0.113.0", 24], ["224.0.0.0", 4], ["240.0.0.0", 4],
|
|
20
|
+
];
|
|
21
|
+
const V6_BLOCKED = [
|
|
22
|
+
["::", 128], ["::1", 128], ["100::", 64], ["2001:2::", 48], ["2001:10::", 28], ["2001:20::", 28],
|
|
23
|
+
["2001:db8::", 32], ["fc00::", 7], ["fe80::", 10], ["fec0::", 10], ["ff00::", 8],
|
|
24
|
+
// RFC 8215 local-use NAT64: /48 layout splits the IPv4 around the `u` octet, so
|
|
25
|
+
// extraction would be wrong — and the prefix is never public-internet. Block whole.
|
|
26
|
+
["64:ff9b:1::", 48],
|
|
27
|
+
];
|
|
28
|
+
const v4List = new BlockList();
|
|
29
|
+
for (const [a, p] of V4_BLOCKED)
|
|
30
|
+
v4List.addSubnet(a, p, "ipv4");
|
|
31
|
+
const v6List = new BlockList();
|
|
32
|
+
for (const [a, p] of V6_BLOCKED)
|
|
33
|
+
v6List.addSubnet(a, p, "ipv6");
|
|
34
|
+
/** Expand a (valid) IPv6 literal into 8 16-bit groups. Handles `::` and a trailing dotted quad. */
|
|
35
|
+
function v6Groups(ip) {
|
|
36
|
+
let s = ip;
|
|
37
|
+
const dotted = s.match(/(\d+\.\d+\.\d+\.\d+)$/);
|
|
38
|
+
if (dotted) {
|
|
39
|
+
const o = dotted[1].split(".").map(Number);
|
|
40
|
+
if (o.some((n) => n > 255))
|
|
41
|
+
return null;
|
|
42
|
+
s = `${s.slice(0, -dotted[1].length) + ((o[0] << 8) | o[1]).toString(16)}:${((o[2] << 8) | o[3]).toString(16)}`;
|
|
43
|
+
}
|
|
44
|
+
const halves = s.split("::");
|
|
45
|
+
if (halves.length > 2)
|
|
46
|
+
return null;
|
|
47
|
+
const head = halves[0] ? halves[0].split(":") : [];
|
|
48
|
+
const tail = halves.length === 2 && halves[1] ? halves[1].split(":") : [];
|
|
49
|
+
const fill = halves.length === 2 ? 8 - head.length - tail.length : 0;
|
|
50
|
+
if (fill < 0 || (halves.length === 1 && head.length !== 8))
|
|
51
|
+
return null;
|
|
52
|
+
const groups = [...head, ...Array(fill).fill("0"), ...tail].map((g) => parseInt(g, 16));
|
|
53
|
+
return groups.length === 8 && groups.every((g) => g >= 0 && g <= 0xffff) ? groups : null;
|
|
54
|
+
}
|
|
55
|
+
const v4Of = (hi, lo) => `${hi >> 8}.${hi & 255}.${lo >> 8}.${lo & 255}`;
|
|
56
|
+
/**
|
|
57
|
+
* True when `ip` is anything but a public-internet unicast address. Fails
|
|
58
|
+
* closed: an unparseable value is non-public. Deliberately NOT named
|
|
59
|
+
* `isBlockedAddress` (server webhook-url.ts applies a different policy).
|
|
60
|
+
*/
|
|
61
|
+
export function isNonPublicAddress(input) {
|
|
62
|
+
let ip = input.trim();
|
|
63
|
+
if (ip.startsWith("[") && ip.endsWith("]"))
|
|
64
|
+
ip = ip.slice(1, -1);
|
|
65
|
+
const zone = ip.indexOf("%");
|
|
66
|
+
if (zone >= 0)
|
|
67
|
+
ip = ip.slice(0, zone);
|
|
68
|
+
const fam = isIP(ip);
|
|
69
|
+
if (fam === 4)
|
|
70
|
+
return v4List.check(ip, "ipv4");
|
|
71
|
+
if (fam !== 6)
|
|
72
|
+
return true;
|
|
73
|
+
const g = v6Groups(ip);
|
|
74
|
+
if (!g)
|
|
75
|
+
return true;
|
|
76
|
+
if (v6List.check(ip, "ipv6"))
|
|
77
|
+
return true;
|
|
78
|
+
const [g0, g1, g2, g3, g4, g5, g6, g7] = g;
|
|
79
|
+
const embedded = [];
|
|
80
|
+
const low = v4Of(g6, g7);
|
|
81
|
+
const upperZero = g0 === 0 && g1 === 0 && g2 === 0 && g3 === 0;
|
|
82
|
+
if (upperZero && g4 === 0 && g5 === 0xffff)
|
|
83
|
+
embedded.push(low); // IPv4-mapped ::ffff:0:0/96
|
|
84
|
+
if (upperZero && g4 === 0xffff && g5 === 0)
|
|
85
|
+
embedded.push(low); // IPv4-translated ::ffff:0:0:0/96
|
|
86
|
+
if (upperZero && g4 === 0 && g5 === 0)
|
|
87
|
+
embedded.push(low); // IPv4-compatible ::/96
|
|
88
|
+
if (g0 === 0x64 && g1 === 0xff9b && g2 === 0 && g3 === 0 && g4 === 0 && g5 === 0)
|
|
89
|
+
embedded.push(low); // NAT64 64:ff9b::/96
|
|
90
|
+
if (g0 === 0x2002)
|
|
91
|
+
embedded.push(v4Of(g1, g2)); // 6to4
|
|
92
|
+
if (g0 === 0x2001 && g1 === 0) {
|
|
93
|
+
embedded.push(v4Of(g2, g3)); // Teredo server
|
|
94
|
+
embedded.push(v4Of(g6 ^ 0xffff, g7 ^ 0xffff)); // Teredo client (obfuscated)
|
|
95
|
+
}
|
|
96
|
+
return embedded.some((v4) => v4List.check(v4, "ipv4"));
|
|
97
|
+
}
|
|
98
|
+
function blockedError(host, addr) {
|
|
99
|
+
const e = new Error(`refusing to connect to ${host}: ${addr} is a non-public address`);
|
|
100
|
+
e.code = "ESSRF";
|
|
101
|
+
return e;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Build a `lookup` for https.request: resolves with `all:true`, rejects when ANY
|
|
105
|
+
* answer is blocked, and answers in the shape the caller asked for (`options.all`).
|
|
106
|
+
*/
|
|
107
|
+
export function makeGuardedLookup(g = {}) {
|
|
108
|
+
const resolveAll = g.lookup ?? ((h, o, cb) => dns.lookup(h, { ...o, all: true }, cb));
|
|
109
|
+
const blocked = g.isBlocked ?? isNonPublicAddress;
|
|
110
|
+
const fn = (hostname, options, cb) => {
|
|
111
|
+
const opts = typeof options === "object" && options ? options : typeof options === "number" ? { family: options } : {};
|
|
112
|
+
resolveAll(hostname, { ...opts, all: true }, (err, addrs) => {
|
|
113
|
+
if (err)
|
|
114
|
+
return cb(err);
|
|
115
|
+
if (!addrs.length)
|
|
116
|
+
return cb(Object.assign(new Error(`no address for ${hostname}`), { code: "ENOTFOUND" }));
|
|
117
|
+
const bad = addrs.find((a) => blocked(a.address, hostname));
|
|
118
|
+
if (bad)
|
|
119
|
+
return cb(blockedError(hostname, bad.address));
|
|
120
|
+
if (opts.all)
|
|
121
|
+
return cb(null, addrs);
|
|
122
|
+
cb(null, addrs[0].address, addrs[0].family);
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
return fn; // documented cast: array-callback overload
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Resolve `host` and require EVERY answer to be public (IP literals are checked
|
|
129
|
+
* directly). Returns the checked addresses so a caller can pin one (git/curl).
|
|
130
|
+
*/
|
|
131
|
+
export async function assertPublicHost(host, g = {}) {
|
|
132
|
+
const blocked = g.isBlocked ?? isNonPublicAddress;
|
|
133
|
+
const bare = host.startsWith("[") && host.endsWith("]") ? host.slice(1, -1) : host;
|
|
134
|
+
if (isIP(bare)) {
|
|
135
|
+
if (blocked(bare, bare))
|
|
136
|
+
throw blockedError(host, bare);
|
|
137
|
+
return [{ address: bare, family: isIP(bare) }];
|
|
138
|
+
}
|
|
139
|
+
const resolveAll = g.lookup ?? ((h, o, cb) => dns.lookup(h, { ...o, all: true }, cb));
|
|
140
|
+
const addrs = await new Promise((res, rej) => resolveAll(bare, { all: true }, (err, a) => (err ? rej(err) : res(a))));
|
|
141
|
+
if (!addrs.length)
|
|
142
|
+
throw Object.assign(new Error(`no address for ${host}`), { code: "ENOTFOUND" });
|
|
143
|
+
const bad = addrs.find((a) => blocked(a.address, bare));
|
|
144
|
+
if (bad)
|
|
145
|
+
throw blockedError(host, bad.address);
|
|
146
|
+
return addrs;
|
|
147
|
+
}
|
|
148
|
+
const DEFAULT_MAX_BYTES = 50 * 1024 * 1024;
|
|
149
|
+
/** Reject IP-literal hosts that are non-public (Node never calls `lookup` for literals). */
|
|
150
|
+
function checkLiteral(url, blocked) {
|
|
151
|
+
const host = url.hostname.startsWith("[") ? url.hostname.slice(1, -1) : url.hostname;
|
|
152
|
+
if (isIP(host) && blocked(host, host))
|
|
153
|
+
throw blockedError(url.hostname, host);
|
|
154
|
+
}
|
|
155
|
+
function pickDecoder(encoding, first) {
|
|
156
|
+
switch ((encoding ?? "identity").toLowerCase()) {
|
|
157
|
+
case "identity":
|
|
158
|
+
case "":
|
|
159
|
+
return null;
|
|
160
|
+
case "gzip":
|
|
161
|
+
case "x-gzip":
|
|
162
|
+
return createGunzip();
|
|
163
|
+
case "br":
|
|
164
|
+
return createBrotliDecompress();
|
|
165
|
+
case "deflate": {
|
|
166
|
+
// zlib header: CMF/FLG with (CMF*256+FLG) % 31 == 0 and CM == 8.
|
|
167
|
+
const zlibWrapped = first.length >= 2 && (first[0] & 0x0f) === 8 && ((first[0] << 8) | first[1]) % 31 === 0;
|
|
168
|
+
return zlibWrapped ? createInflate() : createInflateRaw();
|
|
169
|
+
}
|
|
170
|
+
default:
|
|
171
|
+
throw new Error(`unsupported content-encoding: ${encoding}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/** GET `rawUrl` over https, returning the (decoded) body. */
|
|
175
|
+
export async function guardedFetch(rawUrl, opts = {}) {
|
|
176
|
+
const maxBytes = opts.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
177
|
+
const maxRedirects = opts.maxRedirects ?? 3;
|
|
178
|
+
const timeoutMs = opts.timeoutMs ?? 60_000;
|
|
179
|
+
const blocked = opts.isBlocked ?? isNonPublicAddress;
|
|
180
|
+
const lookup = makeGuardedLookup(opts);
|
|
181
|
+
const ac = new AbortController();
|
|
182
|
+
const timer = setTimeout(() => ac.abort(new Error(`timed out after ${timeoutMs} ms fetching ${rawUrl}`)), timeoutMs);
|
|
183
|
+
const budget = maxBytes;
|
|
184
|
+
try {
|
|
185
|
+
let current = new URL(rawUrl);
|
|
186
|
+
for (let hop = 0;; hop++) {
|
|
187
|
+
if (current.protocol !== "https:")
|
|
188
|
+
throw new Error(`only https:// sources are allowed (got ${current.protocol}//)`);
|
|
189
|
+
checkLiteral(current, blocked);
|
|
190
|
+
const res = await new Promise((resolve, reject) => {
|
|
191
|
+
const req = https.request(current, { method: "GET", headers: { "accept-encoding": "identity", "user-agent": "pi-kb" }, lookup, signal: ac.signal, agent: false, ...opts.tls }, resolve);
|
|
192
|
+
req.on("error", reject);
|
|
193
|
+
req.end();
|
|
194
|
+
});
|
|
195
|
+
const status = res.statusCode ?? 0;
|
|
196
|
+
if (status >= 300 && status < 400 && res.headers.location) {
|
|
197
|
+
res.on("error", () => { }); // destroy() below must not raise an unhandled event
|
|
198
|
+
res.destroy(); // never read a 3xx body
|
|
199
|
+
if (hop >= maxRedirects)
|
|
200
|
+
throw new Error(`too many redirects (max ${maxRedirects}) fetching ${rawUrl}`);
|
|
201
|
+
current = new URL(res.headers.location, current);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (status < 200 || status >= 300) {
|
|
205
|
+
res.on("error", () => { });
|
|
206
|
+
res.destroy();
|
|
207
|
+
throw new Error(`HTTP ${status} fetching ${current.href}`);
|
|
208
|
+
}
|
|
209
|
+
return await readBody(res, budget, ac.signal, maxBytes);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
catch (e) {
|
|
213
|
+
if (ac.signal.aborted && ac.signal.reason instanceof Error)
|
|
214
|
+
throw ac.signal.reason;
|
|
215
|
+
throw e;
|
|
216
|
+
}
|
|
217
|
+
finally {
|
|
218
|
+
clearTimeout(timer);
|
|
219
|
+
ac.abort(); // tear down any lingering socket
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function readBody(res, budget, signal, maxBytes) {
|
|
223
|
+
return new Promise((resolve, reject) => {
|
|
224
|
+
const chunks = [];
|
|
225
|
+
let total = 0; // decoded bytes
|
|
226
|
+
let wire = 0; // compressed bytes
|
|
227
|
+
let decoder = null;
|
|
228
|
+
let started = false;
|
|
229
|
+
let done = false;
|
|
230
|
+
const fail = (e) => {
|
|
231
|
+
if (done)
|
|
232
|
+
return;
|
|
233
|
+
done = true;
|
|
234
|
+
res.destroy();
|
|
235
|
+
decoder?.destroy();
|
|
236
|
+
reject(e);
|
|
237
|
+
};
|
|
238
|
+
const finish = () => {
|
|
239
|
+
if (done)
|
|
240
|
+
return;
|
|
241
|
+
done = true;
|
|
242
|
+
resolve(Buffer.concat(chunks));
|
|
243
|
+
};
|
|
244
|
+
const sink = (c) => {
|
|
245
|
+
total += c.length;
|
|
246
|
+
if (total > budget)
|
|
247
|
+
return fail(new Error(`response exceeds ${maxBytes} bytes`));
|
|
248
|
+
chunks.push(c);
|
|
249
|
+
};
|
|
250
|
+
signal.addEventListener("abort", () => fail(signal.reason instanceof Error ? signal.reason : new Error("aborted")), { once: true });
|
|
251
|
+
res.on("error", fail);
|
|
252
|
+
res.on("aborted", () => fail(new Error("connection closed before the response completed")));
|
|
253
|
+
res.on("data", (c) => {
|
|
254
|
+
if (done)
|
|
255
|
+
return;
|
|
256
|
+
wire += c.length;
|
|
257
|
+
if (wire > budget)
|
|
258
|
+
return fail(new Error(`response exceeds ${maxBytes} bytes`));
|
|
259
|
+
if (!started) {
|
|
260
|
+
started = true;
|
|
261
|
+
try {
|
|
262
|
+
decoder = pickDecoder(res.headers["content-encoding"], c);
|
|
263
|
+
}
|
|
264
|
+
catch (e) {
|
|
265
|
+
return fail(e);
|
|
266
|
+
}
|
|
267
|
+
if (decoder) {
|
|
268
|
+
decoder.on("data", sink);
|
|
269
|
+
decoder.on("end", finish);
|
|
270
|
+
decoder.on("error", fail);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (decoder)
|
|
274
|
+
decoder.write(c);
|
|
275
|
+
else
|
|
276
|
+
sink(c);
|
|
277
|
+
});
|
|
278
|
+
res.on("end", () => {
|
|
279
|
+
if (decoder)
|
|
280
|
+
decoder.end();
|
|
281
|
+
else
|
|
282
|
+
finish();
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
}
|
package/dist/render.js
CHANGED
|
@@ -1,12 +1,47 @@
|
|
|
1
1
|
const SNIPPET_MAX = 160;
|
|
2
|
+
/** Leaf of a `A > B > C` breadcrumb. 47% of a rendered page was breadcrumb text
|
|
3
|
+
* and 38% of that was a prefix repeated inside one file group, while the leaf
|
|
4
|
+
* carries the discriminating token. Full `headingPath` stays in `KbHit` and in
|
|
5
|
+
* the JSON format so `kb_get(path, section)` addressing still works.
|
|
6
|
+
* See change: fix-kb-search-retrieval-quality (design D5). */
|
|
7
|
+
function leafHeading(headingPath) {
|
|
8
|
+
const parts = headingPath.split(" > ");
|
|
9
|
+
return parts[parts.length - 1] || headingPath;
|
|
10
|
+
}
|
|
11
|
+
/** Inline trust-verdict form shared by both text renders: `LABEL (n of m
|
|
12
|
+
* subjects checked)`. null/absent renders NOTHING — never a placeholder.
|
|
13
|
+
* See change: add-kb-trust-verdicts-and-search-guard. */
|
|
14
|
+
function verdictText(v) {
|
|
15
|
+
if (!v)
|
|
16
|
+
return null;
|
|
17
|
+
return `${v.label} (${v.counts.checked} of ${v.counts.total} subject${v.counts.total === 1 ? "" : "s"} checked)`;
|
|
18
|
+
}
|
|
2
19
|
/** Render hits to text. `rank` is a 1-based ordinal over the given (post-limit) list. */
|
|
3
20
|
export function renderHits(hits, opts) {
|
|
4
21
|
const { leading, parentGlyph, multiline } = opts;
|
|
5
22
|
return hits
|
|
6
23
|
.map((h, i) => {
|
|
7
24
|
const lead = leading === "rank" ? String(i + 1) : h.score.toFixed(2);
|
|
8
|
-
const head = `${lead} ${h.path} :: ${h.headingPath}`;
|
|
9
|
-
const
|
|
25
|
+
const head = `${lead} ${h.path} :: ${leafHeading(h.headingPath)}`;
|
|
26
|
+
const marks = [];
|
|
27
|
+
if (h.akaPaths?.length)
|
|
28
|
+
marks.push(`(+${h.akaPaths.length} dup)`);
|
|
29
|
+
// Per-source match count from source-level dedup: a free relevance signal
|
|
30
|
+
// ("this file is the topic authority") in five tokens.
|
|
31
|
+
const sup = h.suppressedSections ?? 0;
|
|
32
|
+
if (sup > 0)
|
|
33
|
+
marks.push(`(+${sup} more section${sup === 1 ? "" : "s"})`);
|
|
34
|
+
// Trust verdict: a LABEL, never a ranking signal. null renders nothing.
|
|
35
|
+
const vt = verdictText(h.verdict);
|
|
36
|
+
if (vt)
|
|
37
|
+
marks.push(vt);
|
|
38
|
+
// Record-type mark (design D5, fix-kb-search-lane-composition): makes a
|
|
39
|
+
// wrong-lane page legible, so the `doc_type` fallback self-suggests.
|
|
40
|
+
// Topic prose (`doc`) is the unmarked majority — marking it would grow
|
|
41
|
+
// every page for no signal. This is the RECORD type, not the lane origin.
|
|
42
|
+
if (h.docType === "agents" || h.docType === "source-md")
|
|
43
|
+
marks.push(`[${h.docType}]`);
|
|
44
|
+
const dup = marks.join(" ");
|
|
10
45
|
const snippet = h.snippet.replace(/\s+/g, " ").slice(0, SNIPPET_MAX);
|
|
11
46
|
if (multiline) {
|
|
12
47
|
const lines = [head];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { KbConfig, ResolvedSource } from "./config.js";
|
|
2
|
+
import type { SearchOpts } from "./types.js";
|
|
3
|
+
/** Explicit, flag-derived / call-site-decided departures from the config.
|
|
4
|
+
* `undefined` = no override, the field takes its config value. Passing a
|
|
5
|
+
* value for a field the caller does not honour is the point: the difference
|
|
6
|
+
* between the surfaces becomes a visible code-level decision, not a silent
|
|
7
|
+
* omission (e.g. the extension pins `expandGraph: false, rerank: false`). */
|
|
8
|
+
export interface SearchOptsOverrides {
|
|
9
|
+
sourceDedup?: boolean;
|
|
10
|
+
laneQuota?: number;
|
|
11
|
+
coverageRerank?: boolean;
|
|
12
|
+
expandParent?: boolean;
|
|
13
|
+
expandGraph?: boolean;
|
|
14
|
+
rerank?: boolean;
|
|
15
|
+
queryExpansion?: SearchOpts["queryExpansion"];
|
|
16
|
+
}
|
|
17
|
+
export declare function searchOptsFromConfig(cfg: KbConfig & {
|
|
18
|
+
resolvedSources?: ResolvedSource[];
|
|
19
|
+
}, opts?: {
|
|
20
|
+
sources?: ResolvedSource[];
|
|
21
|
+
overrides?: SearchOptsOverrides;
|
|
22
|
+
}): SearchOpts;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function searchOptsFromConfig(cfg, opts = {}) {
|
|
2
|
+
const o = opts.overrides ?? {};
|
|
3
|
+
const sources = opts.sources ?? cfg.resolvedSources ?? [];
|
|
4
|
+
return {
|
|
5
|
+
fieldWeights: cfg.ranking.fieldWeights,
|
|
6
|
+
proximityBoost: cfg.ranking.proximityBoost,
|
|
7
|
+
diversity: cfg.ranking.diversity,
|
|
8
|
+
sourceDedup: o.sourceDedup ?? cfg.ranking.sourceDedup,
|
|
9
|
+
laneQuota: o.laneQuota ?? cfg.ranking.laneQuota,
|
|
10
|
+
// Config-only: no CLI flag and no call-site override departs from it, so
|
|
11
|
+
// there is nothing to override. See change: fix-kb-search-lane-composition.
|
|
12
|
+
laneLeadMargin: cfg.ranking.laneLeadMargin,
|
|
13
|
+
coverageRerank: o.coverageRerank ?? cfg.ranking.coverageRerank,
|
|
14
|
+
queryExpansion: o.queryExpansion ?? cfg.queryExpansion.mode,
|
|
15
|
+
prf: cfg.queryExpansion.prf,
|
|
16
|
+
expandParent: o.expandParent ?? cfg.expand.parent,
|
|
17
|
+
expandGraph: o.expandGraph ?? cfg.expand.graph,
|
|
18
|
+
rerank: o.rerank ?? cfg.rerank.enabled,
|
|
19
|
+
rootPriority: Object.fromEntries(sources.map((s) => [s.id, s.priority])),
|
|
20
|
+
};
|
|
21
|
+
}
|
package/dist/sources.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { SourceConfig } from "./config.js";
|
|
2
|
+
import { type LookupAll } from "./net-guard.js";
|
|
3
|
+
/** Thrown when a remote source has no trust record (and none was granted). Classifiable by callers. */
|
|
4
|
+
export declare class KbUntrustedSourceError extends Error {
|
|
5
|
+
readonly spec: SourceConfig;
|
|
6
|
+
constructor(spec: SourceConfig);
|
|
7
|
+
}
|
|
2
8
|
export type KbSourceKind = "filesystem" | "npm" | "git" | "https";
|
|
3
9
|
export interface ResolvedSource {
|
|
4
10
|
id: string;
|
|
@@ -12,6 +18,22 @@ export interface ResolveCtx {
|
|
|
12
18
|
cacheDir: string;
|
|
13
19
|
refresh?: boolean;
|
|
14
20
|
promptTrust?: (s: SourceConfig) => Promise<boolean>;
|
|
21
|
+
/** Test seams only (change: harden-untrusted-content-ingestion). Never set in production. */
|
|
22
|
+
testHooks?: {
|
|
23
|
+
/** Replaces `guardedFetch` for the https resolver. */
|
|
24
|
+
fetch?: (url: string) => Promise<Buffer>;
|
|
25
|
+
/** Replaces the `git` binary: receives argv, returns stdout. */
|
|
26
|
+
git?: (args: string[]) => string | Promise<string>;
|
|
27
|
+
/** Replaces `dns.lookup` for the git host check. */
|
|
28
|
+
lookup?: LookupAll;
|
|
29
|
+
/** Overrides the archive entry-count / expanded-byte limits. */
|
|
30
|
+
archiveLimits?: {
|
|
31
|
+
maxEntries?: number;
|
|
32
|
+
maxExpandedBytes?: number;
|
|
33
|
+
};
|
|
34
|
+
/** Replaces `renameSync` in the stage→dest swap. */
|
|
35
|
+
rename?: (from: string, to: string) => void;
|
|
36
|
+
};
|
|
15
37
|
}
|
|
16
38
|
export interface SourceResolver {
|
|
17
39
|
kind: KbSourceKind;
|
|
@@ -21,9 +43,16 @@ export interface SourceResolver {
|
|
|
21
43
|
export declare function classifyRef(ref: string): KbSourceKind;
|
|
22
44
|
/** Dedup identity (mirrors computeIdentity). cwd resolves relative paths. */
|
|
23
45
|
export declare function sourceIdentity(spec: SourceConfig, cwd?: string): string;
|
|
46
|
+
export declare function cacheKey(spec: SourceConfig): string;
|
|
24
47
|
export declare const filesystemResolver: SourceResolver;
|
|
25
48
|
export declare const npmResolver: SourceResolver;
|
|
26
49
|
export declare const gitResolver: SourceResolver;
|
|
50
|
+
/**
|
|
51
|
+
* Crash recovery: restore the newest ABANDONED backup (past the grace window) when
|
|
52
|
+
* `dest` is gone — a crash between the two swap renames — then prune only
|
|
53
|
+
* backups old enough to be debris. Exported for the interleaving test.
|
|
54
|
+
*/
|
|
55
|
+
export declare function recoverBackups(dest: string): void;
|
|
27
56
|
export declare const httpsResolver: SourceResolver;
|
|
28
57
|
export declare function resolverFor(kind: KbSourceKind): SourceResolver;
|
|
29
58
|
/** Resolve all configured sources to local dirs. Filesystem sources are sync
|