sensemaking 0.26.0 → 0.27.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 +36 -4
- package/dist/cjs/cli/index.d.cts +2 -2
- package/dist/cjs/cli/index.d.ts +2 -2
- package/dist/cjs/cli/index.js +2 -2
- package/dist/cjs/cli/index.js.map +1 -1
- package/dist/cjs/cli/peek.js +43 -2
- package/dist/cjs/cli/peek.js.map +1 -1
- package/dist/cjs/cli/search.js +38 -6
- package/dist/cjs/cli/search.js.map +1 -1
- package/dist/cjs/commands/index.d.cts +2 -2
- package/dist/cjs/commands/index.d.ts +2 -2
- package/dist/cjs/commands/index.js.map +1 -1
- package/dist/cjs/commands/peek.d.cts +9 -1
- package/dist/cjs/commands/peek.d.ts +9 -1
- package/dist/cjs/commands/peek.js +72 -29
- package/dist/cjs/commands/peek.js.map +1 -1
- package/dist/cjs/commands/search.d.cts +6 -2
- package/dist/cjs/commands/search.d.ts +6 -2
- package/dist/cjs/commands/search.js +31 -4
- package/dist/cjs/commands/search.js.map +1 -1
- package/dist/cjs/commands/signals.d.cts +19 -5
- package/dist/cjs/commands/signals.d.ts +19 -5
- package/dist/cjs/commands/signals.js +64 -11
- package/dist/cjs/commands/signals.js.map +1 -1
- package/dist/cjs/config/load.js +6 -0
- package/dist/cjs/config/load.js.map +1 -1
- package/dist/cjs/index.d.cts +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/cli/index.d.ts +2 -2
- package/dist/esm/cli/index.js +2 -2
- package/dist/esm/cli/index.js.map +1 -1
- package/dist/esm/cli/peek.js +20 -2
- package/dist/esm/cli/peek.js.map +1 -1
- package/dist/esm/cli/search.js +12 -2
- package/dist/esm/cli/search.js.map +1 -1
- package/dist/esm/commands/index.d.ts +2 -2
- package/dist/esm/commands/index.js.map +1 -1
- package/dist/esm/commands/peek.d.ts +9 -1
- package/dist/esm/commands/peek.js +26 -12
- package/dist/esm/commands/peek.js.map +1 -1
- package/dist/esm/commands/search.d.ts +6 -2
- package/dist/esm/commands/search.js +11 -3
- package/dist/esm/commands/search.js.map +1 -1
- package/dist/esm/commands/signals.d.ts +19 -5
- package/dist/esm/commands/signals.js +64 -14
- package/dist/esm/commands/signals.js.map +1 -1
- package/dist/esm/config/load.js +6 -0
- package/dist/esm/config/load.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/sense/EXAMPLES.md +3 -3
- package/skills/sense/SKILL.md +7 -1
- package/skills/sense/references/search.md +17 -1
- package/skills/sense/references/sql.md +1 -1
- package/skills/sense/references/terminology.md +32 -0
- package/skills/sense-setup/references/store-benchmarks.md +5 -5
|
@@ -274,6 +274,9 @@ async function lineRangeFor(store, path, line) {
|
|
|
274
274
|
return row ? `L${row.start_line}-${row.end_line}` : null;
|
|
275
275
|
}
|
|
276
276
|
function validateSearchOptions(opts) {
|
|
277
|
+
if (opts.explain !== undefined && typeof opts.explain !== 'boolean') {
|
|
278
|
+
throw new SenseError('SEARCH_OPTION_INVALID', `search option "explain" must be a boolean, got ${String(opts.explain)}`);
|
|
279
|
+
}
|
|
277
280
|
for (const [name, value] of [
|
|
278
281
|
[
|
|
279
282
|
'k',
|
|
@@ -425,7 +428,7 @@ async function searchSnapshot(store, cfg, terms, opts, prepared, preparedSignatu
|
|
|
425
428
|
const syntaxError = bareTermSyntaxError(terms);
|
|
426
429
|
if (syntaxError) throw syntaxError;
|
|
427
430
|
try {
|
|
428
|
-
matchRows = await wordsCandidates(store, candidates, terms, whereJoin, whereCond, scopeCond, fetch, signals.words);
|
|
431
|
+
matchRows = await wordsCandidates(store, candidates, terms, whereJoin, whereCond, scopeCond, fetch, signals.words, opts.explain);
|
|
429
432
|
} catch (err) {
|
|
430
433
|
throw searchError(err, terms, scope);
|
|
431
434
|
}
|
|
@@ -433,11 +436,11 @@ async function searchSnapshot(store, cfg, terms, opts, prepared, preparedSignatu
|
|
|
433
436
|
// Every store's lexical `snippets` is NULL; the snippet text is always computed below.
|
|
434
437
|
// Distinguish from via='link' rows, which never appear in matchRows.
|
|
435
438
|
const matchedPaths = new Set(matchRows.map((r)=>r.path));
|
|
436
|
-
if (signals.links !== undefined) await linksCandidates(store, candidates, matchRows, allPaths, allowedPaths, fetch, signals.links);
|
|
439
|
+
if (signals.links !== undefined) await linksCandidates(store, candidates, matchRows, allPaths, allowedPaths, fetch, signals.links, opts.explain);
|
|
437
440
|
let chunkLines = new Map();
|
|
438
441
|
let chunkSimilarity = new Map();
|
|
439
442
|
if (semanticEnabled && signals.vectors !== undefined && prepared !== undefined) {
|
|
440
|
-
({ chunkLines, chunkSimilarity } = await vectorsCandidates(store, prepared, candidates, fetch, allowedPaths, allPaths.length, signals.vectors));
|
|
443
|
+
({ chunkLines, chunkSimilarity } = await vectorsCandidates(store, prepared, candidates, fetch, allowedPaths, allPaths.length, signals.vectors, opts.explain));
|
|
441
444
|
}
|
|
442
445
|
await store.exec('DROP TABLE IF EXISTS _search');
|
|
443
446
|
// DOUBLE, not REAL: sqlite's REAL is an 8-byte double but duckdb's is a 4-byte float, and the
|
|
@@ -474,5 +477,10 @@ async function searchSnapshot(store, cfg, terms, opts, prepared, preparedSignatu
|
|
|
474
477
|
});
|
|
475
478
|
for (const row of rows)row.snippets = [];
|
|
476
479
|
await hydrateSearchRows(store, cfg, rows, matchedPaths, terms, opts);
|
|
480
|
+
if (opts.explain) {
|
|
481
|
+
var _ref;
|
|
482
|
+
var _candidates_get;
|
|
483
|
+
for (const row of rows)row.explanation = (_ref = (_candidates_get = candidates.get(row.path)) === null || _candidates_get === void 0 ? void 0 : _candidates_get.explanation) !== null && _ref !== void 0 ? _ref : [];
|
|
484
|
+
}
|
|
477
485
|
return rows;
|
|
478
486
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/commands/search.ts"],"sourcesContent":["import { channel } from 'node:diagnostics_channel';\nimport { stemmer } from 'stemmer';\nimport type { EffectiveSearch, ResolvedConfig } from '../config/index.ts';\nimport { embedConfig, featureEnabled, resolveSearch } from '../config/index.ts';\nimport type { PreparedSemanticQuery } from '../embed/query.ts';\nimport { ensureDocumentEmbeddings, prepareSemanticQuery } from '../embed/query.ts';\nimport { localModelMissing, MODEL_FILENAMES } from '../embed/store.ts';\nimport { SenseError } from '../errors.ts';\nimport { serialQuery } from '../lib/serial-query.ts';\nimport { bareTermSyntaxError, searchError } from '../output/search-error.ts';\nimport type { LexicalHit, Store } from '../store/types.ts';\nimport { foldForSearch, hasUnspacedRun, searchTokens, unspacedRuns } from '../text/segment.ts';\nimport { materializeScope, narrowByWhere, rawScope, scopeHasEmbeddings } from './scope.ts';\nimport type { Candidates, SearchResultVia } from './signals.ts';\nimport { linksCandidates, vectorsCandidates, wordsCandidates } from './signals.ts';\nimport { assertQuerySnapshot } from './snapshot.ts';\n\nexport type { SearchResultVia } from './signals.ts';\n\n// Default budget behind --snippet-char-limit/--snippet-count-limit: the limit is whatever the\n// caller passes, these only say what happens when they pass nothing (DESIGN.md \"Search snippets\").\nexport const SNIPPET_CHAR_LIMIT_DEFAULT = 80;\nexport const SNIPPET_COUNT_LIMIT_DEFAULT = 1;\n\nconst searchCandidatesRead = channel('sensemaking.search.candidates-read');\n\n// Bare terms from an FTS5 query string: strips operators/quoting, not real terms, so the\n// snippet scan matches what the query matched -- FTS5 operators are uppercase-only, so this is too.\nfunction extractBareTerms(query: string): string[] {\n const cleaned = query\n .replace(/\"/g, ' ')\n .replace(/[()*]/g, ' ')\n .replace(/\\b(AND|OR|NOT|NEAR)\\b(\\/\\d+)?/g, ' ');\n return cleaned\n .split(/\\s+/)\n .map((tok) => tok.replace(/^[A-Za-z_]\\w*:/, '')) // column filter, e.g. title:term\n .flatMap((tok) => searchTokens(tok).map(({ text }) => text))\n .map((tok) => tok.toLowerCase().trim())\n .filter((tok) => tok.length > 0);\n}\n\n// House convention (src/chunk/group.ts): Intl.Segmenter word boundaries, not a bare \\b, which\n// is ASCII-only. Built on first use: construction is a few ms, paid only when a term matches.\nlet wordSegmenter: Intl.Segmenter | undefined;\n\nfunction findOccurrences(text: string, terms: string[]): Array<{ start: number; end: number; term: string }> {\n const occ: Array<{ start: number; end: number; term: string }> = [];\n const textLower = text.toLowerCase();\n\n // Unspaced scripts (UNSPACED_SCRIPTS) mark no word boundaries: the sense-setup skill promises\n // substring semantics there, so a matching term is found wherever it occurs in the run.\n const unspacedTerms = terms.filter(hasUnspacedRun);\n if (unspacedTerms.length > 0) {\n for (const run of unspacedRuns(text)) {\n const runLower = textLower.slice(run.start, run.end);\n for (const term of unspacedTerms) {\n let idx = 0;\n for (;;) {\n const found = runLower.indexOf(term, idx);\n if (found === -1) break;\n occ.push({ start: run.start + found, end: run.start + found + term.length, term });\n idx = found + term.length;\n }\n }\n }\n }\n\n // Spaced scripts: a word is marked when its porter stem equals a query term's stem, the same\n // algorithm both indexes tokenize with (sqlite's TOKENIZE = 'porter unicode61'; duckdb's fts default).\n const spacedTerms = terms.filter((t) => !hasUnspacedRun(t));\n if (spacedTerms.length > 0) {\n const termByStem = new Map(spacedTerms.map((t) => [stemmer(foldForSearch(t)), t]));\n const stemInitials = new Set(Array.from(termByStem.keys(), (term) => term[0]));\n for (const match of searchTokens(text)) {\n const segment = match.text;\n if (match.unspaced) continue;\n const folded = foldForSearch(segment);\n // Porter stemming changes suffixes only, so a different initial cannot match a query stem.\n if (!stemInitials.has(folded[0])) continue;\n const term = termByStem.get(stemmer(folded));\n if (term !== undefined) occ.push({ start: match.start, end: match.end, term });\n }\n }\n\n return occ.sort((a, b) => a.start - b.start || b.end - a.end);\n}\n\n// Segment boundaries of the whole text, word granularity: the only cut points a snippet edge\n// may land on. One rule for every script (DESIGN.md \"Cutting\") -- a regex \\b is ASCII-only and\n// would mis-cut Japanese, Chinese and Thai, which Intl.Segmenter divides by dictionary instead.\nfunction wordBoundaries(text: string): number[] {\n wordSegmenter ??= new Intl.Segmenter('en', { granularity: 'word' });\n const bounds = [0];\n for (const { index, segment } of wordSegmenter.segment(text)) bounds.push(index + segment.length);\n return bounds;\n}\n\n// bounds is sorted: snaps are binary searches, since each runs once per candidate window.\nfunction snapForward(bounds: number[], pos: number): number {\n let lo = 0;\n let hi = bounds.length - 1;\n while (lo < hi) {\n const mid = (lo + hi) >> 1;\n if (bounds[mid] < pos) lo = mid + 1;\n else hi = mid;\n }\n return bounds[lo];\n}\n\nfunction snapBackward(bounds: number[], pos: number): number {\n let lo = 0;\n let hi = bounds.length - 1;\n let result = bounds[0];\n while (lo < hi) {\n const mid = (lo + hi + 1) >> 1;\n if (bounds[mid] <= pos) {\n lo = mid;\n result = bounds[mid];\n } else hi = mid - 1;\n }\n return result;\n}\n\nfunction firstBoundAfter(bounds: number[], pos: number): number | undefined {\n let lo = 0;\n let hi = bounds.length;\n while (lo < hi) {\n const mid = (lo + hi) >> 1;\n if (bounds[mid] <= pos) lo = mid + 1;\n else hi = mid;\n }\n return bounds[lo];\n}\n\n// A raw, unmarked upper bound: [start, end) inside [anchor, anchor+charLimit), edges on\n// boundaries. Markup (guillemets, ellipses) added on top of this can push the rendered result\n// past charLimit, which is what fitEnd() below corrects for; this is only the starting guess.\n// A single segment spanning the whole target window (a word longer than the char limit) is the\n// one case where holding the boundary rule means the budget is not a hard ceiling.\nfunction cutWindow(text: string, bounds: number[], anchor: number, charLimit: number): { start: number; end: number } {\n const rawEnd = Math.min(text.length, anchor + charLimit);\n const start = snapForward(bounds, anchor);\n let end = snapBackward(bounds, rawEnd);\n if (end <= start) end = firstBoundAfter(bounds, start) ?? text.length;\n return { start, end: Math.min(end, text.length) };\n}\n\n// The largest boundary <= upperBound, > floor, whose rendered [*, end) fits charLimit. Markup\n// only ever adds characters as end grows, so rendered length is non-decreasing in end and\n// shrinking from upperBound is guaranteed to find the true fit rather than guess at it.\n// `floor` is the one end this never shrinks past (the anchor's own marked word, or the first\n// word in the unmarked fallback): DESIGN.md's budget is a ceiling except here, where returning\n// nothing is worse than a single word that overruns it.\nfunction fitEnd(render: (end: number) => string, bounds: number[], floor: number, upperBound: number, charLimit: number): number {\n let lo = 0;\n let hi = bounds.length - 1;\n while (lo < hi) {\n const mid = (lo + hi + 1) >> 1;\n if (bounds[mid] <= upperBound) lo = mid;\n else hi = mid - 1;\n }\n for (; lo >= 0 && bounds[lo] > floor; lo--) {\n if (render(bounds[lo]).length <= charLimit) return bounds[lo];\n }\n return floor;\n}\n\ninterface Span {\n start: number;\n end: number;\n}\n\ninterface SnippetWindow extends Span {\n distinct: number;\n count: number;\n}\n\n// occ is start-sorted: a window's first candidate occurrence is a binary search, not a walk.\nfunction firstOccAtOrAfter(occ: Array<{ start: number; end: number; term: string }>, pos: number): number {\n let lo = 0;\n let hi = occ.length;\n while (lo < hi) {\n const mid = (lo + hi) >> 1;\n if (occ[mid].start < pos) lo = mid + 1;\n else hi = mid;\n }\n return lo;\n}\n\nfunction renderWindow(text: string, occ: Array<{ start: number; end: number; term: string }>, win: Span): string {\n let out = '';\n let cursor = win.start;\n for (let i = firstOccAtOrAfter(occ, win.start); i < occ.length && occ[i].start < win.end; i++) {\n const o = occ[i];\n if (o.end > win.end) continue;\n // Occurrences of duplicate or substring-overlapping terms (\"test tests\") can overlap;\n // emitting each would duplicate document text. Keep the first, absorb the rest.\n if (o.start < cursor) continue;\n out += `${text.slice(cursor, o.start)}«${text.slice(o.start, o.end)}»`;\n cursor = o.end;\n }\n out += text.slice(cursor, win.end);\n const prefix = win.start > 0 ? '…' : '';\n const suffix = win.end < text.length ? '…' : '';\n return `${prefix}${out.replace(/\\s+/g, ' ')}${suffix}`;\n}\n\n// Scores every occurrence-anchored candidate window: most distinct terms wins, ties broken by\n// most total hits -- coverage now that occurrences are real words, not the false-fragment counts\n// a substring scan produced. Selection itself is unevaluated (DESIGN.md \"Selection, unsettled\").\n// The window's `end` is fitted to the *rendered* text, markup included, not the raw span: a\n// window dense with marks (every guillemet pair adds two characters) shrinks to fit the same\n// budget a sparse one fills almost exactly.\nfunction scoreWindows(occ: Array<{ start: number; end: number; term: string }>, bounds: number[], text: string, charLimit: number): SnippetWindow[] {\n const candidates: SnippetWindow[] = [];\n for (let i = 0; i < occ.length; i++) {\n const winEnd = occ[i].start + charLimit;\n const seen = new Set<string>();\n let count = 0;\n for (let j = i; j < occ.length && occ[j].start < winEnd; j++) {\n seen.add(occ[j].term);\n count++;\n }\n const start = snapForward(bounds, occ[i].start);\n const floor = Math.max(start, occ[i].end);\n const upperBound = Math.max(floor, cutWindow(text, bounds, occ[i].start, charLimit).end);\n const end = fitEnd((e) => renderWindow(text, occ, { start, end: e }), bounds, floor, upperBound, charLimit);\n candidates.push({ start, end, distinct: seen.size, count });\n }\n return candidates;\n}\n\nfunction overlaps(a: Span, b: Span): boolean {\n return a.start < b.end && b.start < a.end;\n}\n\n// Same scoring function as a single snippet, kept rather than discarded (DESIGN.md \"Several\n// snippets per note\"): rank by score, take the top non-overlapping windows, then reorder to the\n// note's own order, since several passages from one note read as an abridged note.\nfunction selectWindows(candidates: SnippetWindow[], countLimit: number): { chosen: SnippetWindow[]; best: SnippetWindow } {\n const ranked = candidates.slice().sort((a, b) => b.distinct - a.distinct || b.count - a.count);\n const chosen: SnippetWindow[] = [];\n for (const c of ranked) {\n if (chosen.length >= countLimit) break;\n if (chosen.some((s) => overlaps(s, c))) continue;\n chosen.push(c);\n }\n chosen.sort((a, b) => a.start - b.start);\n return { chosen, best: ranked[0] };\n}\n\n// Builds bounded marked passages and returns the best window's offset for section lookup.\nexport function computeSnippets(text: string, terms: string[], charLimit: number, countLimit: number): { snippets: string[]; offset: number } {\n const occ = findOccurrences(text, terms);\n const bounds = wordBoundaries(text);\n if (occ.length === 0) {\n // A term matched only via title/summary, not this body text, finds no occurrence here;\n // fall back to the doc's start, unmarked.\n const render = (end: number): string => `${text.slice(0, end).replace(/\\s+/g, ' ').trim()}${end < text.length ? '…' : ''}`;\n const floor = bounds.find((b) => b > 0) ?? text.length;\n const upperBound = Math.max(floor, cutWindow(text, bounds, 0, charLimit).end);\n const end = fitEnd(render, bounds, floor, upperBound, charLimit);\n return { snippets: [render(end)], offset: 0 };\n }\n const candidates = scoreWindows(occ, bounds, text, charLimit);\n const { chosen, best } = selectWindows(candidates, countLimit);\n return { snippets: chosen.map((win) => renderWindow(text, occ, win)), offset: best.start };\n}\n\n// Converts an offset in the original text to its one-based line number.\nexport function lineNumberAt(text: string, offset: number): number {\n let line = 1;\n for (let i = 0; i < offset; i++) if (text.charCodeAt(i) === 10) line++;\n return line;\n}\n\nasync function lineRangeFor(store: Store, path: string, line: number): Promise<string | null> {\n const stmt = await store.prepare('SELECT start_line, end_line FROM sections WHERE \"path\" = ? AND start_line <= ? AND end_line >= ? ORDER BY start_line DESC LIMIT 1');\n const row = (await stmt.get(path, line, line)) as { start_line: number; end_line: number } | undefined;\n return row ? `L${row.start_line}-${row.end_line}` : null;\n}\n\n/** Options for the root {@link search} API. */\nexport interface SearchOptions {\n k?: number;\n where?: string; // SQL fragment against frontmatter alias `f`, e.g. \"f.status = 'active'\"\n preset?: string; // named preset; unknown name throws listing declared presets, undefined -> \"default\"\n include?: string[]; // ad hoc scope override (repeatable --include); independent of exclude\n exclude?: string[]; // ad hoc scope override (repeatable --exclude); independent of include\n noExclude?: boolean; // --no-exclude: drop the preset's exclude for this command\n snippetCharLimit?: number; // --snippet-char-limit; defaults to SNIPPET_CHAR_LIMIT_DEFAULT\n snippetCountLimit?: number; // --snippet-count-limit; defaults to SNIPPET_COUNT_LIMIT_DEFAULT\n /** Cancels supported provider I/O and stops between phases after active native work finishes. */\n signal?: AbortSignal;\n}\n\nexport type SearchResult = {\n path: string;\n title: string;\n summary: string;\n snippets: string[];\n via: SearchResultVia;\n score: number;\n lines: string | null;\n similarity?: number | null;\n};\n\nexport interface SearchHydrationRow {\n path: string;\n snippets?: string[];\n lines?: string | null;\n}\n\nfunction validateSearchOptions(opts: SearchOptions): void {\n for (const [name, value] of [\n ['k', opts.k],\n ['snippetCharLimit', opts.snippetCharLimit],\n ['snippetCountLimit', opts.snippetCountLimit],\n ] as const) {\n if (value !== undefined && (!Number.isFinite(value) || !Number.isInteger(value) || value <= 0)) {\n throw new SenseError('SEARCH_OPTION_INVALID', `search option \"${name}\" must be a positive finite integer, got ${String(value)}`);\n }\n }\n}\n\n// Hydrates already-ranked rows with snippets and containing-section ranges without reranking.\nexport async function hydrateSearchRows(store: Store, cfg: ResolvedConfig, rows: SearchHydrationRow[], matchedPaths: Set<string>, terms: string, opts: Pick<SearchOptions, 'snippetCharLimit' | 'snippetCountLimit'> = {}): Promise<void> {\n if (matchedPaths.size === 0) return;\n const bareTerms = extractBareTerms(terms);\n const charLimit = opts.snippetCharLimit ?? SNIPPET_CHAR_LIMIT_DEFAULT;\n const countLimit = opts.snippetCountLimit ?? SNIPPET_COUNT_LIMIT_DEFAULT;\n const sourceStmt = await store.prepare('SELECT text FROM indexed_sources WHERE \"path\" = ?');\n for (const row of rows) {\n if (!matchedPaths.has(row.path)) continue;\n const source = (await sourceStmt.get(row.path)) as { text: string } | undefined;\n if (!source) throw new SenseError('INDEX_NOT_READY', `indexed source text is missing for ${String(row.path)}; run \\`sense build --force\\` (or library build(config, { force: true })) before querying`);\n const text = source.text;\n const { snippets, offset } = computeSnippets(text, bareTerms, charLimit, countLimit);\n row.snippets = snippets;\n if (row.lines == null) row.lines = featureEnabled(cfg, 'sections') ? await lineRangeFor(store, row.path, lineNumberAt(text, offset)) : null;\n }\n}\n\n/**\n * Searches one committed published generation. Provider work precedes the final read transaction;\n * cancellation waits for active native work and transaction cleanup before rejection.\n */\nexport function search(store: Store, cfg: ResolvedConfig, terms: string, opts: SearchOptions = {}): Promise<SearchResult[]> {\n if (opts.signal?.aborted) return Promise.reject(opts.signal.reason);\n return serialQuery(store, () => searchIndexed(store, cfg, terms, opts));\n}\n\ninterface SearchScope {\n signature: string;\n effective: EffectiveSearch;\n allPaths: string[];\n scopePaths: Set<string>;\n allowedPaths: Set<string>;\n semanticEligible: boolean;\n}\n\nasync function readSearchScope(store: Store, cfg: ResolvedConfig, terms: string, opts: SearchOptions): Promise<SearchScope> {\n const signature = await assertQuerySnapshot(store, cfg);\n const effective = resolveSearch(cfg, opts);\n const allPaths = ((await (await store.prepare('SELECT \"path\" FROM frontmatter')).all()) as Array<{ path: string }>).map((r) => r.path);\n const scopePaths = await rawScope(store, cfg, opts, allPaths);\n let allowedPaths: Set<string>;\n try {\n allowedPaths = await narrowByWhere(store, scopePaths, effective.where);\n } catch (err) {\n throw searchError(err as Error, terms, effective.where);\n }\n const wantsVectors = effective.signals.vectors !== undefined;\n if (wantsVectors) await ensureDocumentEmbeddings(store, cfg, allowedPaths);\n const semanticEligible = wantsVectors && (await scopeHasEmbeddings(store, cfg, allowedPaths));\n return { signature, effective, allPaths, scopePaths, allowedPaths, semanticEligible };\n}\n\ninterface SemanticPreparationNeeded {\n signature: string;\n}\n\nfunction needsSemanticPreparation(result: SearchResult[] | SemanticPreparationNeeded): result is SemanticPreparationNeeded {\n return !Array.isArray(result);\n}\n\nasync function searchIndexed(store: Store, cfg: ResolvedConfig, terms: string, opts: SearchOptions): Promise<SearchResult[]> {\n validateSearchOptions(opts);\n const initial = resolveSearch(cfg, opts);\n opts.signal?.throwIfAborted();\n // A downloadable HF id proceeds -- getProvider fetches it lazily on consent. Only a\n // local path with missing files errors here, since nothing will ever fetch it for itself.\n const wantsVectors = initial.signals.vectors !== undefined;\n if (wantsVectors) {\n const e = embedConfig(cfg); // validate.ts guarantees this is set whenever \"vectors\" is declared\n if (localModelMissing(e)) {\n throw new SenseError('EMBED_MODEL_MISSING', `preset \"${initial.presetName}\" searches with vectors, but the local model path \"${e.model}\" is missing ${MODEL_FILENAMES}; point embed.model at a directory containing them, or drop \"vectors\" from that preset's signals to search without them`);\n }\n }\n\n let prepared: PreparedSemanticQuery | undefined;\n let preparedSignature: string | undefined;\n if (wantsVectors) {\n // This first snapshot only decides whether a semantic query can contribute. It closes before\n // provider/model work, and the complete scope/readiness decision is repeated below.\n const preflight = await store.transaction(() => readSearchScope(store, cfg, terms, opts));\n opts.signal?.throwIfAborted();\n if (preflight.semanticEligible) {\n preparedSignature = preflight.signature;\n prepared = await prepareSemanticQuery(cfg, terms, { signal: opts.signal });\n opts.signal?.throwIfAborted();\n }\n }\n\n opts.signal?.throwIfAborted();\n let result = await store.transaction(() => searchSnapshot(store, cfg, terms, opts, prepared, preparedSignature));\n opts.signal?.throwIfAborted();\n if (!needsSemanticPreparation(result)) return result;\n\n // The scope became vector-eligible after an empty/ineligible preflight. Leave that snapshot,\n // prepare once, then take a fresh final snapshot rather than waiting on the provider inside it.\n preparedSignature = result.signature;\n prepared = await prepareSemanticQuery(cfg, terms, { signal: opts.signal });\n opts.signal?.throwIfAborted();\n result = await store.transaction(() => searchSnapshot(store, cfg, terms, opts, prepared, preparedSignature));\n opts.signal?.throwIfAborted();\n if (needsSemanticPreparation(result)) throw new SenseError('INDEX_NOT_READY', 'the semantic search scope changed while its query was being prepared; retry the search against a stable published index');\n return result;\n}\n\nasync function searchSnapshot(store: Store, cfg: ResolvedConfig, terms: string, opts: SearchOptions, prepared: PreparedSemanticQuery | undefined, preparedSignature: string | undefined): Promise<SearchResult[] | SemanticPreparationNeeded> {\n const { signature, effective, allPaths, scopePaths, allowedPaths, semanticEligible } = await readSearchScope(store, cfg, terms, opts);\n if (prepared !== undefined && signature !== preparedSignature) {\n throw new SenseError('INDEX_NOT_READY', 'the index configuration changed while the semantic query was being prepared; retry after the current build finishes, or rebuild the index for this configuration');\n }\n if (semanticEligible && prepared === undefined) return { signature };\n\n const { k, signals } = effective;\n const fetch = Math.max(k * 3, 30);\n const scopeActive = scopePaths.size < allPaths.length;\n const semanticEnabled = semanticEligible && prepared !== undefined;\n\n // --where applies inside the candidate query (a post-filter would drop matches ranked past\n // the pool) and again on the final select, for link-derived rows.\n const scope = effective.where;\n const whereJoin = scope ? `JOIN frontmatter f ON f.\"path\" = content.path` : '';\n const whereCond = scope ? `AND (${scope})` : '';\n // Filtered before LIMIT, not after, or scoped notes ranking below the global top-`fetch`\n // never reach the filter; joined against a temp table since real scopes exceed SQLITE_MAX_VARIABLE_NUMBER.\n if (scopeActive) await materializeScope(store, '_search_scope', scopePaths);\n const scopeCond = scopeActive ? `AND content.path IN (SELECT \"path\" FROM _search_scope)` : '';\n\n const candidates: Candidates = new Map();\n let matchRows: LexicalHit[] = [];\n const hasEmptyPhrase = [...terms.matchAll(/\"([^\"]*)\"/g)].some((match) => !/[\\p{L}\\p{N}_]/u.test(match[1]));\n const hasBareSyntaxOnly = /[()]/u.test(terms.replace(/\"[^\"]*\"/g, ' '));\n // A query that is only whitespace or punctuation has no lexical rows; other declared signals\n // compose normally. In particular, unspaced-script punctuation can rewrite to an empty MATCH.\n if (signals.words !== undefined && terms.trim() !== '' && (/[\\p{L}\\p{N}_]/u.test(terms) || hasBareSyntaxOnly) && !hasEmptyPhrase) {\n const syntaxError = bareTermSyntaxError(terms);\n if (syntaxError) throw syntaxError;\n try {\n matchRows = await wordsCandidates(store, candidates, terms, whereJoin, whereCond, scopeCond, fetch, signals.words);\n } catch (err) {\n throw searchError(err as Error, terms, scope);\n }\n }\n // Every store's lexical `snippets` is NULL; the snippet text is always computed below.\n // Distinguish from via='link' rows, which never appear in matchRows.\n const matchedPaths = new Set(matchRows.map((r) => r.path));\n\n if (signals.links !== undefined) await linksCandidates(store, candidates, matchRows, allPaths, allowedPaths, fetch, signals.links);\n\n let chunkLines = new Map<string, string>();\n let chunkSimilarity = new Map<string, number>();\n if (semanticEnabled && signals.vectors !== undefined && prepared !== undefined) {\n ({ chunkLines, chunkSimilarity } = await vectorsCandidates(store, prepared, candidates, fetch, allowedPaths, allPaths.length, signals.vectors));\n }\n\n await store.exec('DROP TABLE IF EXISTS _search');\n // DOUBLE, not REAL: sqlite's REAL is an 8-byte double but duckdb's is a 4-byte float, and the\n // rounded score/similarity are printed at full precision in rows.\n await store.exec('CREATE TEMP TABLE _search (\"path\" TEXT PRIMARY KEY, score DOUBLE, via TEXT, lines TEXT, similarity DOUBLE)');\n if (candidates.size > 0) {\n await store.runBatch(\n 'INSERT INTO _search (\"path\", score, via, lines, similarity) VALUES (?, ?, ?, ?, ?)',\n [...candidates].map(([path, c]) => [path, c.score, c.via, chunkLines.get(path) ?? null, chunkSimilarity.get(path) ?? null])\n );\n }\n\n // Reapplies --where even though _search is already scope+where filtered, since the join to\n // frontmatter is already needed for the path column.\n const where = scope ? `WHERE (${scope})` : '';\n // lines: semantic rows carry their chunk's range, lexical rows gain one below,\n // everything else stays null; similarity stays semantic-only.\n const similarityCol = semanticEnabled ? ', _search.similarity' : '';\n // NULL AS snippets holds this column's position (between summary and via) for every row;\n // overwritten below, always to an array, never left null (DESIGN.md).\n const selectStmt = await store.prepare(\n `SELECT f.\"path\" AS path, content.title, content.summary, NULL AS snippets, _search.via, round(_search.score, 4) AS score, _search.lines${similarityCol}\n FROM _search JOIN frontmatter f ON f.\"path\" = _search.\"path\" JOIN content ON content.path = _search.\"path\"\n ${where} ORDER BY _search.score DESC, _search.\"path\" ASC LIMIT ?`\n );\n const rows = (await selectStmt.all(k)) as SearchResult[];\n if (searchCandidatesRead.hasSubscribers) searchCandidatesRead.publish({ paths: rows.map((row) => row.path) });\n for (const row of rows) row.snippets = [];\n\n await hydrateSearchRows(store, cfg, rows, matchedPaths, terms, opts);\n\n return rows;\n}\n"],"names":["channel","stemmer","embedConfig","featureEnabled","resolveSearch","ensureDocumentEmbeddings","prepareSemanticQuery","localModelMissing","MODEL_FILENAMES","SenseError","serialQuery","bareTermSyntaxError","searchError","foldForSearch","hasUnspacedRun","searchTokens","unspacedRuns","materializeScope","narrowByWhere","rawScope","scopeHasEmbeddings","linksCandidates","vectorsCandidates","wordsCandidates","assertQuerySnapshot","SNIPPET_CHAR_LIMIT_DEFAULT","SNIPPET_COUNT_LIMIT_DEFAULT","searchCandidatesRead","extractBareTerms","query","cleaned","replace","split","map","tok","flatMap","text","toLowerCase","trim","filter","length","wordSegmenter","findOccurrences","terms","occ","textLower","unspacedTerms","run","runLower","slice","start","end","term","idx","found","indexOf","push","spacedTerms","t","termByStem","Map","stemInitials","Set","Array","from","keys","match","segment","unspaced","folded","has","get","undefined","sort","a","b","wordBoundaries","Intl","Segmenter","granularity","bounds","index","snapForward","pos","lo","hi","mid","snapBackward","result","firstBoundAfter","cutWindow","anchor","charLimit","rawEnd","Math","min","fitEnd","render","floor","upperBound","firstOccAtOrAfter","renderWindow","win","out","cursor","i","o","prefix","suffix","scoreWindows","candidates","winEnd","seen","count","j","add","max","e","distinct","size","overlaps","selectWindows","countLimit","ranked","chosen","c","some","s","best","computeSnippets","find","snippets","offset","lineNumberAt","line","charCodeAt","lineRangeFor","store","path","stmt","prepare","row","start_line","end_line","validateSearchOptions","opts","name","value","k","snippetCharLimit","snippetCountLimit","Number","isFinite","isInteger","String","hydrateSearchRows","cfg","rows","matchedPaths","bareTerms","sourceStmt","source","lines","search","signal","aborted","Promise","reject","reason","searchIndexed","readSearchScope","signature","effective","allPaths","all","r","scopePaths","allowedPaths","where","err","wantsVectors","signals","vectors","semanticEligible","needsSemanticPreparation","isArray","initial","throwIfAborted","presetName","model","prepared","preparedSignature","preflight","transaction","searchSnapshot","fetch","scopeActive","semanticEnabled","scope","whereJoin","whereCond","scopeCond","matchRows","hasEmptyPhrase","matchAll","test","hasBareSyntaxOnly","words","syntaxError","links","chunkLines","chunkSimilarity","exec","runBatch","score","via","similarityCol","selectStmt","hasSubscribers","publish","paths"],"mappings":"AAAA,SAASA,OAAO,QAAQ,2BAA2B;AACnD,SAASC,OAAO,QAAQ,UAAU;AAElC,SAASC,WAAW,EAAEC,cAAc,EAAEC,aAAa,QAAQ,qBAAqB;AAEhF,SAASC,wBAAwB,EAAEC,oBAAoB,QAAQ,oBAAoB;AACnF,SAASC,iBAAiB,EAAEC,eAAe,QAAQ,oBAAoB;AACvE,SAASC,UAAU,QAAQ,eAAe;AAC1C,SAASC,WAAW,QAAQ,yBAAyB;AACrD,SAASC,mBAAmB,EAAEC,WA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2E,IAAI,CAAC;IACjB,IAAItG,WAAWS,IAAI,GAAG,GAAG;QACvB,MAAMkB,MAAM4E,QAAQ,CAClB,sFACA;eAAIvG;SAAW,CAAC7E,GAAG,CAAC,CAAC,CAACyG,MAAMb,EAAE;gBAA4BqF,iBAA8BC;mBAArD;gBAACzE;gBAAMb,EAAEyF,KAAK;gBAAEzF,EAAE0F,GAAG;iBAAEL,kBAAAA,WAAW3I,GAAG,CAACmE,mBAAfwE,6BAAAA,kBAAwB;iBAAMC,uBAAAA,gBAAgB5I,GAAG,CAACmE,mBAApByE,kCAAAA,uBAA6B;aAAK;;IAE9H;IAEA,2FAA2F;IAC3F,qDAAqD;IACrD,MAAMjC,QAAQoB,QAAQ,CAAC,OAAO,EAAEA,MAAM,CAAC,CAAC,GAAG;IAC3C,+EAA+E;IAC/E,8DAA8D;IAC9D,MAAMkB,gBAAgBnB,kBAAkB,yBAAyB;IACjE,yFAAyF;IACzF,sEAAsE;IACtE,MAAMoB,aAAa,MAAMhF,MAAMG,OAAO,CACpC,CAAC,uIAAuI,EAAE4E,cAAc;;OAErJ,EAAEtC,MAAM,wDAAwD,CAAC;IAEtE,MAAMrB,OAAQ,MAAM4D,WAAW3C,GAAG,CAAC1B;IACnC,IAAIzH,qBAAqB+L,cAAc,EAAE/L,qBAAqBgM,OAAO,CAAC;QAAEC,OAAO/D,KAAK5H,GAAG,CAAC,CAAC4G,MAAQA,IAAIH,IAAI;IAAE;IAC3G,KAAK,MAAMG,OAAOgB,KAAMhB,IAAIV,QAAQ,GAAG,EAAE;IAEzC,MAAMwB,kBAAkBlB,OAAOmB,KAAKC,MAAMC,cAAcnH,OAAOsG;IAE/D,OAAOY;AACT"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/commands/search.ts"],"sourcesContent":["import { channel } from 'node:diagnostics_channel';\nimport { stemmer } from 'stemmer';\nimport type { EffectiveSearch, ResolvedConfig } from '../config/index.ts';\nimport { embedConfig, featureEnabled, resolveSearch } from '../config/index.ts';\nimport type { PreparedSemanticQuery } from '../embed/query.ts';\nimport { ensureDocumentEmbeddings, prepareSemanticQuery } from '../embed/query.ts';\nimport { localModelMissing, MODEL_FILENAMES } from '../embed/store.ts';\nimport { SenseError } from '../errors.ts';\nimport { serialQuery } from '../lib/serial-query.ts';\nimport { bareTermSyntaxError, searchError } from '../output/search-error.ts';\nimport type { LexicalHit, Store } from '../store/types.ts';\nimport { foldForSearch, hasUnspacedRun, searchTokens, unspacedRuns } from '../text/segment.ts';\nimport { materializeScope, narrowByWhere, rawScope, scopeHasEmbeddings } from './scope.ts';\nimport type { Candidates, SearchResultVia, SearchSignalContribution } from './signals.ts';\nimport { linksCandidates, vectorsCandidates, wordsCandidates } from './signals.ts';\nimport { assertQuerySnapshot } from './snapshot.ts';\n\nexport type { SearchResultVia, SearchSignalContribution } from './signals.ts';\n\n// Default budget behind --snippet-char-limit/--snippet-count-limit: the limit is whatever the\n// caller passes, these only say what happens when they pass nothing (DESIGN.md \"Search snippets\").\nexport const SNIPPET_CHAR_LIMIT_DEFAULT = 80;\nexport const SNIPPET_COUNT_LIMIT_DEFAULT = 1;\n\nconst searchCandidatesRead = channel('sensemaking.search.candidates-read');\n\n// Bare terms from an FTS5 query string: strips operators/quoting, not real terms, so the\n// snippet scan matches what the query matched -- FTS5 operators are uppercase-only, so this is too.\nfunction extractBareTerms(query: string): string[] {\n const cleaned = query\n .replace(/\"/g, ' ')\n .replace(/[()*]/g, ' ')\n .replace(/\\b(AND|OR|NOT|NEAR)\\b(\\/\\d+)?/g, ' ');\n return cleaned\n .split(/\\s+/)\n .map((tok) => tok.replace(/^[A-Za-z_]\\w*:/, '')) // column filter, e.g. title:term\n .flatMap((tok) => searchTokens(tok).map(({ text }) => text))\n .map((tok) => tok.toLowerCase().trim())\n .filter((tok) => tok.length > 0);\n}\n\n// House convention (src/chunk/group.ts): Intl.Segmenter word boundaries, not a bare \\b, which\n// is ASCII-only. Built on first use: construction is a few ms, paid only when a term matches.\nlet wordSegmenter: Intl.Segmenter | undefined;\n\nfunction findOccurrences(text: string, terms: string[]): Array<{ start: number; end: number; term: string }> {\n const occ: Array<{ start: number; end: number; term: string }> = [];\n const textLower = text.toLowerCase();\n\n // Unspaced scripts (UNSPACED_SCRIPTS) mark no word boundaries: the sense-setup skill promises\n // substring semantics there, so a matching term is found wherever it occurs in the run.\n const unspacedTerms = terms.filter(hasUnspacedRun);\n if (unspacedTerms.length > 0) {\n for (const run of unspacedRuns(text)) {\n const runLower = textLower.slice(run.start, run.end);\n for (const term of unspacedTerms) {\n let idx = 0;\n for (;;) {\n const found = runLower.indexOf(term, idx);\n if (found === -1) break;\n occ.push({ start: run.start + found, end: run.start + found + term.length, term });\n idx = found + term.length;\n }\n }\n }\n }\n\n // Spaced scripts: a word is marked when its porter stem equals a query term's stem, the same\n // algorithm both indexes tokenize with (sqlite's TOKENIZE = 'porter unicode61'; duckdb's fts default).\n const spacedTerms = terms.filter((t) => !hasUnspacedRun(t));\n if (spacedTerms.length > 0) {\n const termByStem = new Map(spacedTerms.map((t) => [stemmer(foldForSearch(t)), t]));\n const stemInitials = new Set(Array.from(termByStem.keys(), (term) => term[0]));\n for (const match of searchTokens(text)) {\n const segment = match.text;\n if (match.unspaced) continue;\n const folded = foldForSearch(segment);\n // Porter stemming changes suffixes only, so a different initial cannot match a query stem.\n if (!stemInitials.has(folded[0])) continue;\n const term = termByStem.get(stemmer(folded));\n if (term !== undefined) occ.push({ start: match.start, end: match.end, term });\n }\n }\n\n return occ.sort((a, b) => a.start - b.start || b.end - a.end);\n}\n\n// Segment boundaries of the whole text, word granularity: the only cut points a snippet edge\n// may land on. One rule for every script (DESIGN.md \"Cutting\") -- a regex \\b is ASCII-only and\n// would mis-cut Japanese, Chinese and Thai, which Intl.Segmenter divides by dictionary instead.\nfunction wordBoundaries(text: string): number[] {\n wordSegmenter ??= new Intl.Segmenter('en', { granularity: 'word' });\n const bounds = [0];\n for (const { index, segment } of wordSegmenter.segment(text)) bounds.push(index + segment.length);\n return bounds;\n}\n\n// bounds is sorted: snaps are binary searches, since each runs once per candidate window.\nfunction snapForward(bounds: number[], pos: number): number {\n let lo = 0;\n let hi = bounds.length - 1;\n while (lo < hi) {\n const mid = (lo + hi) >> 1;\n if (bounds[mid] < pos) lo = mid + 1;\n else hi = mid;\n }\n return bounds[lo];\n}\n\nfunction snapBackward(bounds: number[], pos: number): number {\n let lo = 0;\n let hi = bounds.length - 1;\n let result = bounds[0];\n while (lo < hi) {\n const mid = (lo + hi + 1) >> 1;\n if (bounds[mid] <= pos) {\n lo = mid;\n result = bounds[mid];\n } else hi = mid - 1;\n }\n return result;\n}\n\nfunction firstBoundAfter(bounds: number[], pos: number): number | undefined {\n let lo = 0;\n let hi = bounds.length;\n while (lo < hi) {\n const mid = (lo + hi) >> 1;\n if (bounds[mid] <= pos) lo = mid + 1;\n else hi = mid;\n }\n return bounds[lo];\n}\n\n// A raw, unmarked upper bound: [start, end) inside [anchor, anchor+charLimit), edges on\n// boundaries. Markup (guillemets, ellipses) added on top of this can push the rendered result\n// past charLimit, which is what fitEnd() below corrects for; this is only the starting guess.\n// A single segment spanning the whole target window (a word longer than the char limit) is the\n// one case where holding the boundary rule means the budget is not a hard ceiling.\nfunction cutWindow(text: string, bounds: number[], anchor: number, charLimit: number): { start: number; end: number } {\n const rawEnd = Math.min(text.length, anchor + charLimit);\n const start = snapForward(bounds, anchor);\n let end = snapBackward(bounds, rawEnd);\n if (end <= start) end = firstBoundAfter(bounds, start) ?? text.length;\n return { start, end: Math.min(end, text.length) };\n}\n\n// The largest boundary <= upperBound, > floor, whose rendered [*, end) fits charLimit. Markup\n// only ever adds characters as end grows, so rendered length is non-decreasing in end and\n// shrinking from upperBound is guaranteed to find the true fit rather than guess at it.\n// `floor` is the one end this never shrinks past (the anchor's own marked word, or the first\n// word in the unmarked fallback): DESIGN.md's budget is a ceiling except here, where returning\n// nothing is worse than a single word that overruns it.\nfunction fitEnd(render: (end: number) => string, bounds: number[], floor: number, upperBound: number, charLimit: number): number {\n let lo = 0;\n let hi = bounds.length - 1;\n while (lo < hi) {\n const mid = (lo + hi + 1) >> 1;\n if (bounds[mid] <= upperBound) lo = mid;\n else hi = mid - 1;\n }\n for (; lo >= 0 && bounds[lo] > floor; lo--) {\n if (render(bounds[lo]).length <= charLimit) return bounds[lo];\n }\n return floor;\n}\n\ninterface Span {\n start: number;\n end: number;\n}\n\ninterface SnippetWindow extends Span {\n distinct: number;\n count: number;\n}\n\n// occ is start-sorted: a window's first candidate occurrence is a binary search, not a walk.\nfunction firstOccAtOrAfter(occ: Array<{ start: number; end: number; term: string }>, pos: number): number {\n let lo = 0;\n let hi = occ.length;\n while (lo < hi) {\n const mid = (lo + hi) >> 1;\n if (occ[mid].start < pos) lo = mid + 1;\n else hi = mid;\n }\n return lo;\n}\n\nfunction renderWindow(text: string, occ: Array<{ start: number; end: number; term: string }>, win: Span): string {\n let out = '';\n let cursor = win.start;\n for (let i = firstOccAtOrAfter(occ, win.start); i < occ.length && occ[i].start < win.end; i++) {\n const o = occ[i];\n if (o.end > win.end) continue;\n // Occurrences of duplicate or substring-overlapping terms (\"test tests\") can overlap;\n // emitting each would duplicate document text. Keep the first, absorb the rest.\n if (o.start < cursor) continue;\n out += `${text.slice(cursor, o.start)}«${text.slice(o.start, o.end)}»`;\n cursor = o.end;\n }\n out += text.slice(cursor, win.end);\n const prefix = win.start > 0 ? '…' : '';\n const suffix = win.end < text.length ? '…' : '';\n return `${prefix}${out.replace(/\\s+/g, ' ')}${suffix}`;\n}\n\n// Scores every occurrence-anchored candidate window: most distinct terms wins, ties broken by\n// most total hits -- coverage now that occurrences are real words, not the false-fragment counts\n// a substring scan produced. Selection itself is unevaluated (DESIGN.md \"Selection, unsettled\").\n// The window's `end` is fitted to the *rendered* text, markup included, not the raw span: a\n// window dense with marks (every guillemet pair adds two characters) shrinks to fit the same\n// budget a sparse one fills almost exactly.\nfunction scoreWindows(occ: Array<{ start: number; end: number; term: string }>, bounds: number[], text: string, charLimit: number): SnippetWindow[] {\n const candidates: SnippetWindow[] = [];\n for (let i = 0; i < occ.length; i++) {\n const winEnd = occ[i].start + charLimit;\n const seen = new Set<string>();\n let count = 0;\n for (let j = i; j < occ.length && occ[j].start < winEnd; j++) {\n seen.add(occ[j].term);\n count++;\n }\n const start = snapForward(bounds, occ[i].start);\n const floor = Math.max(start, occ[i].end);\n const upperBound = Math.max(floor, cutWindow(text, bounds, occ[i].start, charLimit).end);\n const end = fitEnd((e) => renderWindow(text, occ, { start, end: e }), bounds, floor, upperBound, charLimit);\n candidates.push({ start, end, distinct: seen.size, count });\n }\n return candidates;\n}\n\nfunction overlaps(a: Span, b: Span): boolean {\n return a.start < b.end && b.start < a.end;\n}\n\n// Same scoring function as a single snippet, kept rather than discarded (DESIGN.md \"Several\n// snippets per note\"): rank by score, take the top non-overlapping windows, then reorder to the\n// note's own order, since several passages from one note read as an abridged note.\nfunction selectWindows(candidates: SnippetWindow[], countLimit: number): { chosen: SnippetWindow[]; best: SnippetWindow } {\n const ranked = candidates.slice().sort((a, b) => b.distinct - a.distinct || b.count - a.count);\n const chosen: SnippetWindow[] = [];\n for (const c of ranked) {\n if (chosen.length >= countLimit) break;\n if (chosen.some((s) => overlaps(s, c))) continue;\n chosen.push(c);\n }\n chosen.sort((a, b) => a.start - b.start);\n return { chosen, best: ranked[0] };\n}\n\n// Builds bounded marked passages and returns the best window's offset for section lookup.\nexport function computeSnippets(text: string, terms: string[], charLimit: number, countLimit: number): { snippets: string[]; offset: number } {\n const occ = findOccurrences(text, terms);\n const bounds = wordBoundaries(text);\n if (occ.length === 0) {\n // A term matched only via title/summary, not this body text, finds no occurrence here;\n // fall back to the doc's start, unmarked.\n const render = (end: number): string => `${text.slice(0, end).replace(/\\s+/g, ' ').trim()}${end < text.length ? '…' : ''}`;\n const floor = bounds.find((b) => b > 0) ?? text.length;\n const upperBound = Math.max(floor, cutWindow(text, bounds, 0, charLimit).end);\n const end = fitEnd(render, bounds, floor, upperBound, charLimit);\n return { snippets: [render(end)], offset: 0 };\n }\n const candidates = scoreWindows(occ, bounds, text, charLimit);\n const { chosen, best } = selectWindows(candidates, countLimit);\n return { snippets: chosen.map((win) => renderWindow(text, occ, win)), offset: best.start };\n}\n\n// Converts an offset in the original text to its one-based line number.\nexport function lineNumberAt(text: string, offset: number): number {\n let line = 1;\n for (let i = 0; i < offset; i++) if (text.charCodeAt(i) === 10) line++;\n return line;\n}\n\nasync function lineRangeFor(store: Store, path: string, line: number): Promise<string | null> {\n const stmt = await store.prepare('SELECT start_line, end_line FROM sections WHERE \"path\" = ? AND start_line <= ? AND end_line >= ? ORDER BY start_line DESC LIMIT 1');\n const row = (await stmt.get(path, line, line)) as { start_line: number; end_line: number } | undefined;\n return row ? `L${row.start_line}-${row.end_line}` : null;\n}\n\n/** Options for the root {@link search} API. */\nexport interface SearchOptions {\n k?: number;\n /** Collects ranking contributions for returned candidates; omitted or false preserves ordinary output. */\n explain?: boolean;\n where?: string; // SQL fragment against frontmatter alias `f`, e.g. \"f.status = 'active'\"\n preset?: string; // named preset; unknown name throws listing declared presets, undefined -> \"default\"\n include?: string[]; // ad hoc scope override (repeatable --include); independent of exclude\n exclude?: string[]; // ad hoc scope override (repeatable --exclude); independent of include\n noExclude?: boolean; // --no-exclude: drop the preset's exclude for this command\n snippetCharLimit?: number; // --snippet-char-limit; defaults to SNIPPET_CHAR_LIMIT_DEFAULT\n snippetCountLimit?: number; // --snippet-count-limit; defaults to SNIPPET_COUNT_LIMIT_DEFAULT\n /** Cancels supported provider I/O and stops between phases after active native work finishes. */\n signal?: AbortSignal;\n}\n\nexport type SearchResult = {\n path: string;\n title: string;\n summary: string;\n snippets: string[];\n via: SearchResultVia;\n score: number;\n lines: string | null;\n similarity?: number | null;\n /** Present only with explain: true, in words/links/vectors order; contributions can differ from via labels. */\n explanation?: SearchSignalContribution[];\n};\n\nexport interface SearchHydrationRow {\n path: string;\n snippets?: string[];\n lines?: string | null;\n}\n\nfunction validateSearchOptions(opts: SearchOptions): void {\n if (opts.explain !== undefined && typeof opts.explain !== 'boolean') {\n throw new SenseError('SEARCH_OPTION_INVALID', `search option \"explain\" must be a boolean, got ${String(opts.explain)}`);\n }\n for (const [name, value] of [\n ['k', opts.k],\n ['snippetCharLimit', opts.snippetCharLimit],\n ['snippetCountLimit', opts.snippetCountLimit],\n ] as const) {\n if (value !== undefined && (!Number.isFinite(value) || !Number.isInteger(value) || value <= 0)) {\n throw new SenseError('SEARCH_OPTION_INVALID', `search option \"${name}\" must be a positive finite integer, got ${String(value)}`);\n }\n }\n}\n\n// Hydrates already-ranked rows with snippets and containing-section ranges without reranking.\nexport async function hydrateSearchRows(store: Store, cfg: ResolvedConfig, rows: SearchHydrationRow[], matchedPaths: Set<string>, terms: string, opts: Pick<SearchOptions, 'snippetCharLimit' | 'snippetCountLimit'> = {}): Promise<void> {\n if (matchedPaths.size === 0) return;\n const bareTerms = extractBareTerms(terms);\n const charLimit = opts.snippetCharLimit ?? SNIPPET_CHAR_LIMIT_DEFAULT;\n const countLimit = opts.snippetCountLimit ?? SNIPPET_COUNT_LIMIT_DEFAULT;\n const sourceStmt = await store.prepare('SELECT text FROM indexed_sources WHERE \"path\" = ?');\n for (const row of rows) {\n if (!matchedPaths.has(row.path)) continue;\n const source = (await sourceStmt.get(row.path)) as { text: string } | undefined;\n if (!source) throw new SenseError('INDEX_NOT_READY', `indexed source text is missing for ${String(row.path)}; run \\`sense build --force\\` (or library build(config, { force: true })) before querying`);\n const text = source.text;\n const { snippets, offset } = computeSnippets(text, bareTerms, charLimit, countLimit);\n row.snippets = snippets;\n if (row.lines == null) row.lines = featureEnabled(cfg, 'sections') ? await lineRangeFor(store, row.path, lineNumberAt(text, offset)) : null;\n }\n}\n\n/**\n * Searches one committed published generation. Provider work precedes the final read transaction;\n * cancellation waits for active native work and transaction cleanup before rejection.\n */\nexport function search(store: Store, cfg: ResolvedConfig, terms: string, opts: SearchOptions = {}): Promise<SearchResult[]> {\n if (opts.signal?.aborted) return Promise.reject(opts.signal.reason);\n return serialQuery(store, () => searchIndexed(store, cfg, terms, opts));\n}\n\ninterface SearchScope {\n signature: string;\n effective: EffectiveSearch;\n allPaths: string[];\n scopePaths: Set<string>;\n allowedPaths: Set<string>;\n semanticEligible: boolean;\n}\n\nasync function readSearchScope(store: Store, cfg: ResolvedConfig, terms: string, opts: SearchOptions): Promise<SearchScope> {\n const signature = await assertQuerySnapshot(store, cfg);\n const effective = resolveSearch(cfg, opts);\n const allPaths = ((await (await store.prepare('SELECT \"path\" FROM frontmatter')).all()) as Array<{ path: string }>).map((r) => r.path);\n const scopePaths = await rawScope(store, cfg, opts, allPaths);\n let allowedPaths: Set<string>;\n try {\n allowedPaths = await narrowByWhere(store, scopePaths, effective.where);\n } catch (err) {\n throw searchError(err as Error, terms, effective.where);\n }\n const wantsVectors = effective.signals.vectors !== undefined;\n if (wantsVectors) await ensureDocumentEmbeddings(store, cfg, allowedPaths);\n const semanticEligible = wantsVectors && (await scopeHasEmbeddings(store, cfg, allowedPaths));\n return { signature, effective, allPaths, scopePaths, allowedPaths, semanticEligible };\n}\n\ninterface SemanticPreparationNeeded {\n signature: string;\n}\n\nfunction needsSemanticPreparation(result: SearchResult[] | SemanticPreparationNeeded): result is SemanticPreparationNeeded {\n return !Array.isArray(result);\n}\n\nasync function searchIndexed(store: Store, cfg: ResolvedConfig, terms: string, opts: SearchOptions): Promise<SearchResult[]> {\n validateSearchOptions(opts);\n const initial = resolveSearch(cfg, opts);\n opts.signal?.throwIfAborted();\n // A downloadable HF id proceeds -- getProvider fetches it lazily on consent. Only a\n // local path with missing files errors here, since nothing will ever fetch it for itself.\n const wantsVectors = initial.signals.vectors !== undefined;\n if (wantsVectors) {\n const e = embedConfig(cfg); // validate.ts guarantees this is set whenever \"vectors\" is declared\n if (localModelMissing(e)) {\n throw new SenseError('EMBED_MODEL_MISSING', `preset \"${initial.presetName}\" searches with vectors, but the local model path \"${e.model}\" is missing ${MODEL_FILENAMES}; point embed.model at a directory containing them, or drop \"vectors\" from that preset's signals to search without them`);\n }\n }\n\n let prepared: PreparedSemanticQuery | undefined;\n let preparedSignature: string | undefined;\n if (wantsVectors) {\n // This first snapshot only decides whether a semantic query can contribute. It closes before\n // provider/model work, and the complete scope/readiness decision is repeated below.\n const preflight = await store.transaction(() => readSearchScope(store, cfg, terms, opts));\n opts.signal?.throwIfAborted();\n if (preflight.semanticEligible) {\n preparedSignature = preflight.signature;\n prepared = await prepareSemanticQuery(cfg, terms, { signal: opts.signal });\n opts.signal?.throwIfAborted();\n }\n }\n\n opts.signal?.throwIfAborted();\n let result = await store.transaction(() => searchSnapshot(store, cfg, terms, opts, prepared, preparedSignature));\n opts.signal?.throwIfAborted();\n if (!needsSemanticPreparation(result)) return result;\n\n // The scope became vector-eligible after an empty/ineligible preflight. Leave that snapshot,\n // prepare once, then take a fresh final snapshot rather than waiting on the provider inside it.\n preparedSignature = result.signature;\n prepared = await prepareSemanticQuery(cfg, terms, { signal: opts.signal });\n opts.signal?.throwIfAborted();\n result = await store.transaction(() => searchSnapshot(store, cfg, terms, opts, prepared, preparedSignature));\n opts.signal?.throwIfAborted();\n if (needsSemanticPreparation(result)) throw new SenseError('INDEX_NOT_READY', 'the semantic search scope changed while its query was being prepared; retry the search against a stable published index');\n return result;\n}\n\nasync function searchSnapshot(store: Store, cfg: ResolvedConfig, terms: string, opts: SearchOptions, prepared: PreparedSemanticQuery | undefined, preparedSignature: string | undefined): Promise<SearchResult[] | SemanticPreparationNeeded> {\n const { signature, effective, allPaths, scopePaths, allowedPaths, semanticEligible } = await readSearchScope(store, cfg, terms, opts);\n if (prepared !== undefined && signature !== preparedSignature) {\n throw new SenseError('INDEX_NOT_READY', 'the index configuration changed while the semantic query was being prepared; retry after the current build finishes, or rebuild the index for this configuration');\n }\n if (semanticEligible && prepared === undefined) return { signature };\n\n const { k, signals } = effective;\n const fetch = Math.max(k * 3, 30);\n const scopeActive = scopePaths.size < allPaths.length;\n const semanticEnabled = semanticEligible && prepared !== undefined;\n\n // --where applies inside the candidate query (a post-filter would drop matches ranked past\n // the pool) and again on the final select, for link-derived rows.\n const scope = effective.where;\n const whereJoin = scope ? `JOIN frontmatter f ON f.\"path\" = content.path` : '';\n const whereCond = scope ? `AND (${scope})` : '';\n // Filtered before LIMIT, not after, or scoped notes ranking below the global top-`fetch`\n // never reach the filter; joined against a temp table since real scopes exceed SQLITE_MAX_VARIABLE_NUMBER.\n if (scopeActive) await materializeScope(store, '_search_scope', scopePaths);\n const scopeCond = scopeActive ? `AND content.path IN (SELECT \"path\" FROM _search_scope)` : '';\n\n const candidates: Candidates = new Map();\n let matchRows: LexicalHit[] = [];\n const hasEmptyPhrase = [...terms.matchAll(/\"([^\"]*)\"/g)].some((match) => !/[\\p{L}\\p{N}_]/u.test(match[1]));\n const hasBareSyntaxOnly = /[()]/u.test(terms.replace(/\"[^\"]*\"/g, ' '));\n // A query that is only whitespace or punctuation has no lexical rows; other declared signals\n // compose normally. In particular, unspaced-script punctuation can rewrite to an empty MATCH.\n if (signals.words !== undefined && terms.trim() !== '' && (/[\\p{L}\\p{N}_]/u.test(terms) || hasBareSyntaxOnly) && !hasEmptyPhrase) {\n const syntaxError = bareTermSyntaxError(terms);\n if (syntaxError) throw syntaxError;\n try {\n matchRows = await wordsCandidates(store, candidates, terms, whereJoin, whereCond, scopeCond, fetch, signals.words, opts.explain);\n } catch (err) {\n throw searchError(err as Error, terms, scope);\n }\n }\n // Every store's lexical `snippets` is NULL; the snippet text is always computed below.\n // Distinguish from via='link' rows, which never appear in matchRows.\n const matchedPaths = new Set(matchRows.map((r) => r.path));\n\n if (signals.links !== undefined) await linksCandidates(store, candidates, matchRows, allPaths, allowedPaths, fetch, signals.links, opts.explain);\n\n let chunkLines = new Map<string, string>();\n let chunkSimilarity = new Map<string, number>();\n if (semanticEnabled && signals.vectors !== undefined && prepared !== undefined) {\n ({ chunkLines, chunkSimilarity } = await vectorsCandidates(store, prepared, candidates, fetch, allowedPaths, allPaths.length, signals.vectors, opts.explain));\n }\n\n await store.exec('DROP TABLE IF EXISTS _search');\n // DOUBLE, not REAL: sqlite's REAL is an 8-byte double but duckdb's is a 4-byte float, and the\n // rounded score/similarity are printed at full precision in rows.\n await store.exec('CREATE TEMP TABLE _search (\"path\" TEXT PRIMARY KEY, score DOUBLE, via TEXT, lines TEXT, similarity DOUBLE)');\n if (candidates.size > 0) {\n await store.runBatch(\n 'INSERT INTO _search (\"path\", score, via, lines, similarity) VALUES (?, ?, ?, ?, ?)',\n [...candidates].map(([path, c]) => [path, c.score, c.via, chunkLines.get(path) ?? null, chunkSimilarity.get(path) ?? null])\n );\n }\n\n // Reapplies --where even though _search is already scope+where filtered, since the join to\n // frontmatter is already needed for the path column.\n const where = scope ? `WHERE (${scope})` : '';\n // lines: semantic rows carry their chunk's range, lexical rows gain one below,\n // everything else stays null; similarity stays semantic-only.\n const similarityCol = semanticEnabled ? ', _search.similarity' : '';\n // NULL AS snippets holds this column's position (between summary and via) for every row;\n // overwritten below, always to an array, never left null (DESIGN.md).\n const selectStmt = await store.prepare(\n `SELECT f.\"path\" AS path, content.title, content.summary, NULL AS snippets, _search.via, round(_search.score, 4) AS score, _search.lines${similarityCol}\n FROM _search JOIN frontmatter f ON f.\"path\" = _search.\"path\" JOIN content ON content.path = _search.\"path\"\n ${where} ORDER BY _search.score DESC, _search.\"path\" ASC LIMIT ?`\n );\n const rows = (await selectStmt.all(k)) as SearchResult[];\n if (searchCandidatesRead.hasSubscribers) searchCandidatesRead.publish({ paths: rows.map((row) => row.path) });\n for (const row of rows) row.snippets = [];\n\n await hydrateSearchRows(store, cfg, rows, matchedPaths, terms, opts);\n if (opts.explain) {\n for (const row of rows) row.explanation = candidates.get(row.path)?.explanation ?? [];\n }\n\n return rows;\n}\n"],"names":["channel","stemmer","embedConfig","featureEnabled","resolveSearch","ensureDocumentEmbeddings","prepareSemanticQuery","localModelMissing","MODEL_FILENAMES","SenseError","serialQuery","bareTermSyntaxError","searchError","foldForSearch","hasUnspacedRun","searchTokens","unspacedRuns","materializeScope","narrowByWhere","rawScope","scopeHasEmbeddings","linksCandidates","vectorsCandidates","wordsCandidates","assertQuerySnapshot","SNIPPET_CHAR_LIMIT_DEFAULT","SNIPPET_COUNT_LIMIT_DEFAULT","searchCandidatesRead","extractBareTerms","query","cleaned","replace","split","map","tok","flatMap","text","toLowerCase","trim","filter","length","wordSegmenter","findOccurrences","terms","occ","textLower","unspacedTerms","run","runLower","slice","start","end","term","idx","found","indexOf","push","spacedTerms","t","termByStem","Map","stemInitials","Set","Array","from","keys","match","segment","unspaced","folded","has","get","undefined","sort","a","b","wordBoundaries","Intl","Segmenter","granularity","bounds","index","snapForward","pos","lo","hi","mid","snapBackward","result","firstBoundAfter","cutWindow","anchor","charLimit","rawEnd","Math","min","fitEnd","render","floor","upperBound","firstOccAtOrAfter","renderWindow","win","out","cursor","i","o","prefix","suffix","scoreWindows","candidates","winEnd","seen","count","j","add","max","e","distinct","size","overlaps","selectWindows","countLimit","ranked","chosen","c","some","s","best","computeSnippets","find","snippets","offset","lineNumberAt","line","charCodeAt","lineRangeFor","store","path","stmt","prepare","row","start_line","end_line","validateSearchOptions","opts","explain","String","name","value","k","snippetCharLimit","snippetCountLimit","Number","isFinite","isInteger","hydrateSearchRows","cfg","rows","matchedPaths","bareTerms","sourceStmt","source","lines","search","signal","aborted","Promise","reject","reason","searchIndexed","readSearchScope","signature","effective","allPaths","all","r","scopePaths","allowedPaths","where","err","wantsVectors","signals","vectors","semanticEligible","needsSemanticPreparation","isArray","initial","throwIfAborted","presetName","model","prepared","preparedSignature","preflight","transaction","searchSnapshot","fetch","scopeActive","semanticEnabled","scope","whereJoin","whereCond","scopeCond","matchRows","hasEmptyPhrase","matchAll","test","hasBareSyntaxOnly","words","syntaxError","links","chunkLines","chunkSimilarity","exec","runBatch","score","via","similarityCol","selectStmt","hasSubscribers","publish","paths","explanation"],"mappings":"AAAA,SAASA,OAAO,QAAQ,2BAA2B;AACnD,SAASC,OAAO,QAAQ,UAAU;AAElC,SAASC,WAAW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C,IAAI3H,qBAAqBgM,cAAc,EAAEhM,qBAAqBiM,OAAO,CAAC;QAAEC,OAAO/D,KAAK7H,GAAG,CAAC,CAAC4G,MAAQA,IAAIH,IAAI;IAAE;IAC3G,KAAK,MAAMG,OAAOiB,KAAMjB,IAAIV,QAAQ,GAAG,EAAE;IAEzC,MAAMyB,kBAAkBnB,OAAOoB,KAAKC,MAAMC,cAAcpH,OAAOsG;IAC/D,IAAIA,KAAKC,OAAO,EAAE;;YAC0BpC;QAA1C,KAAK,MAAM+B,OAAOiB,KAAMjB,IAAIiF,WAAW,YAAGhH,kBAAAA,WAAWvC,GAAG,CAACsE,IAAIH,IAAI,eAAvB5B,sCAAAA,gBAA0BgH,WAAW,uCAAI,EAAE;IACvF;IAEA,OAAOhE;AACT"}
|
|
@@ -1,13 +1,27 @@
|
|
|
1
|
+
import type { SignalName } from '../config/index.js';
|
|
1
2
|
import type { PreparedSemanticQuery } from '../embed/query.js';
|
|
2
3
|
import type { LexicalHit, Store } from '../store/types.js';
|
|
3
4
|
export type SearchResultVia = 'match' | 'match+link' | 'link' | 'match+vector' | 'match+link+vector' | 'link+vector' | 'vector';
|
|
4
|
-
|
|
5
|
+
/** One participating signal's contribution captured during the existing ranking computation. */
|
|
6
|
+
export interface SearchSignalContribution {
|
|
7
|
+
signal: SignalName;
|
|
8
|
+
/** One-based position; fusion uses weight / (60 + rank - 1). */
|
|
9
|
+
rank: number;
|
|
10
|
+
/** Effective weight from the selected preset. */
|
|
11
|
+
weight: number;
|
|
12
|
+
/** Unrounded value added to the fused score, independent of the rounded public score and via label. */
|
|
13
|
+
contribution: number;
|
|
14
|
+
}
|
|
15
|
+
interface Candidate {
|
|
5
16
|
score: number;
|
|
6
17
|
via: SearchResultVia;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export
|
|
10
|
-
export declare function
|
|
18
|
+
explanation?: SearchSignalContribution[];
|
|
19
|
+
}
|
|
20
|
+
export type Candidates = Map<string, Candidate>;
|
|
21
|
+
export declare function wordsCandidates(store: Store, candidates: Candidates, query: string, whereJoin: string, whereCond: string, scopeCond: string, fetch: number, weight: number, explain?: boolean): Promise<LexicalHit[]>;
|
|
22
|
+
export declare function linksCandidates(store: Store, candidates: Candidates, matchRows: LexicalHit[], allPaths: string[], allowedPaths: Set<string>, fetch: number, weight: number, explain?: boolean): Promise<void>;
|
|
23
|
+
export declare function vectorsCandidates(store: Store, prepared: PreparedSemanticQuery, candidates: Candidates, fetch: number, allowedPaths: Set<string>, totalPaths: number, weight: number, explain?: boolean): Promise<{
|
|
11
24
|
chunkLines: Map<string, string>;
|
|
12
25
|
chunkSimilarity: Map<string, number>;
|
|
13
26
|
}>;
|
|
27
|
+
export {};
|
|
@@ -13,7 +13,7 @@ function withVector(via) {
|
|
|
13
13
|
}
|
|
14
14
|
// BM25 match rows (store.lexical.query), folded into `candidates` by reciprocal rank scaled by
|
|
15
15
|
// the preset's declared weight for this signal. Every other signal is seeded from its output.
|
|
16
|
-
export async function wordsCandidates(store, candidates, query, whereJoin, whereCond, scopeCond, fetch, weight) {
|
|
16
|
+
export async function wordsCandidates(store, candidates, query, whereJoin, whereCond, scopeCond, fetch, weight, explain = false) {
|
|
17
17
|
const matchRows = await store.lexical.query(query, {
|
|
18
18
|
whereJoin,
|
|
19
19
|
whereCond,
|
|
@@ -21,16 +21,26 @@ export async function wordsCandidates(store, candidates, query, whereJoin, where
|
|
|
21
21
|
limit: fetch
|
|
22
22
|
});
|
|
23
23
|
matchRows.forEach((r, i)=>{
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
const contribution = weight / (RRF_K + i);
|
|
25
|
+
const candidate = {
|
|
26
|
+
score: contribution,
|
|
26
27
|
via: 'match'
|
|
27
|
-
}
|
|
28
|
+
};
|
|
29
|
+
if (explain) candidate.explanation = [
|
|
30
|
+
{
|
|
31
|
+
signal: 'words',
|
|
32
|
+
rank: i + 1,
|
|
33
|
+
weight,
|
|
34
|
+
contribution
|
|
35
|
+
}
|
|
36
|
+
];
|
|
37
|
+
candidates.set(r.path, candidate);
|
|
28
38
|
});
|
|
29
39
|
return matchRows;
|
|
30
40
|
}
|
|
31
41
|
// Personalized-PageRank expansion from the word-match seeds, folded into `candidates` at this
|
|
32
42
|
// signal's weight. A no-op when there is nothing to seed from (matchRows empty).
|
|
33
|
-
export async function linksCandidates(store, candidates, matchRows, allPaths, allowedPaths, fetch, weight) {
|
|
43
|
+
export async function linksCandidates(store, candidates, matchRows, allPaths, allowedPaths, fetch, weight, explain = false) {
|
|
34
44
|
if (matchRows.length === 0) return;
|
|
35
45
|
const stmt = await store.prepare(LINK_EDGES_SQL);
|
|
36
46
|
const edges = toEdges(await stmt.all());
|
|
@@ -46,36 +56,76 @@ export async function linksCandidates(store, candidates, matchRows, allPaths, al
|
|
|
46
56
|
...personalizedRank(allPaths, edges, seeds)
|
|
47
57
|
].filter(([path, score])=>score > 1e-9 && allowedPaths.has(path)).sort((a, b)=>b[1] - a[1] || Buffer.compare(Buffer.from(a[0]), Buffer.from(b[0]))).slice(0, fetch);
|
|
48
58
|
ranked.forEach(([path], i)=>{
|
|
59
|
+
const contribution = weight / (RRF_K + i);
|
|
49
60
|
const existing = candidates.get(path);
|
|
50
61
|
if (existing) {
|
|
51
|
-
existing.score +=
|
|
62
|
+
existing.score += contribution;
|
|
63
|
+
if (explain) {
|
|
64
|
+
var _existing, _explanation;
|
|
65
|
+
(_explanation = (_existing = existing).explanation) !== null && _explanation !== void 0 ? _explanation : _existing.explanation = [];
|
|
66
|
+
existing.explanation.push({
|
|
67
|
+
signal: 'links',
|
|
68
|
+
rank: i + 1,
|
|
69
|
+
weight,
|
|
70
|
+
contribution
|
|
71
|
+
});
|
|
72
|
+
}
|
|
52
73
|
if (linked.has(path)) existing.via = 'match+link';
|
|
53
74
|
} else {
|
|
54
|
-
|
|
55
|
-
score:
|
|
75
|
+
const candidate = {
|
|
76
|
+
score: contribution,
|
|
56
77
|
via: 'link'
|
|
57
|
-
}
|
|
78
|
+
};
|
|
79
|
+
if (explain) candidate.explanation = [
|
|
80
|
+
{
|
|
81
|
+
signal: 'links',
|
|
82
|
+
rank: i + 1,
|
|
83
|
+
weight,
|
|
84
|
+
contribution
|
|
85
|
+
}
|
|
86
|
+
];
|
|
87
|
+
candidates.set(path, candidate);
|
|
58
88
|
}
|
|
59
89
|
});
|
|
60
90
|
}
|
|
61
91
|
// Vector expansion at the swept flat-region constants (pool = fetch), folded into `candidates`
|
|
62
92
|
// at this signal's weight; each row also carries its best chunk's line range and similarity.
|
|
63
|
-
export async function vectorsCandidates(store, prepared, candidates, fetch, allowedPaths, totalPaths, weight) {
|
|
93
|
+
export async function vectorsCandidates(store, prepared, candidates, fetch, allowedPaths, totalPaths, weight, explain = false) {
|
|
64
94
|
const chunkLines = new Map();
|
|
65
95
|
const chunkSimilarity = new Map();
|
|
66
96
|
const vec = await withMaterializedVectorScope(store, allowedPaths, ()=>semanticCandidates(store, prepared, fetch, allowedPaths), totalPaths);
|
|
67
97
|
vec.forEach(({ path, lines, similarity }, i)=>{
|
|
98
|
+
const contribution = weight / (RRF_K + i);
|
|
68
99
|
chunkLines.set(path, lines);
|
|
69
100
|
chunkSimilarity.set(path, similarity);
|
|
70
101
|
const existing = candidates.get(path);
|
|
71
102
|
if (existing) {
|
|
72
|
-
existing.score +=
|
|
103
|
+
existing.score += contribution;
|
|
104
|
+
if (explain) {
|
|
105
|
+
var _existing, _explanation;
|
|
106
|
+
(_explanation = (_existing = existing).explanation) !== null && _explanation !== void 0 ? _explanation : _existing.explanation = [];
|
|
107
|
+
existing.explanation.push({
|
|
108
|
+
signal: 'vectors',
|
|
109
|
+
rank: i + 1,
|
|
110
|
+
weight,
|
|
111
|
+
contribution
|
|
112
|
+
});
|
|
113
|
+
}
|
|
73
114
|
existing.via = withVector(existing.via);
|
|
74
115
|
} else {
|
|
75
|
-
|
|
76
|
-
score:
|
|
116
|
+
const candidate = {
|
|
117
|
+
score: contribution,
|
|
77
118
|
via: 'vector'
|
|
78
|
-
}
|
|
119
|
+
};
|
|
120
|
+
if (explain) candidate.explanation = [
|
|
121
|
+
{
|
|
122
|
+
signal: 'vectors',
|
|
123
|
+
rank: i + 1,
|
|
124
|
+
weight,
|
|
125
|
+
contribution
|
|
126
|
+
}
|
|
127
|
+
];
|
|
128
|
+
candidates.set(path, candidate);
|
|
79
129
|
}
|
|
80
130
|
});
|
|
81
131
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/commands/signals.ts"],"sourcesContent":["import type { PreparedSemanticQuery } from '../embed/query.ts';\nimport { semanticCandidates } from '../embed/query.ts';\nimport { LINK_EDGES_SQL, toEdges } from '../features/index.ts';\nimport { personalizedRank } from '../graph/graph.ts';\nimport type { LexicalHit, Store } from '../store/types.ts';\nimport { withMaterializedVectorScope } from './scope.ts';\n\n// RRF composes whatever the preset declares; the constant lives here since it belongs to\n// the fusion these three functions share, not to search() itself.\nconst RRF_K = 60;\n\nexport type SearchResultVia = 'match' | 'match+link' | 'link' | 'match+vector' | 'match+link+vector' | 'link+vector' | 'vector';\n\nexport
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/ai/sensemaking/src/commands/signals.ts"],"sourcesContent":["import type { SignalName } from '../config/index.ts';\nimport type { PreparedSemanticQuery } from '../embed/query.ts';\nimport { semanticCandidates } from '../embed/query.ts';\nimport { LINK_EDGES_SQL, toEdges } from '../features/index.ts';\nimport { personalizedRank } from '../graph/graph.ts';\nimport type { LexicalHit, Store } from '../store/types.ts';\nimport { withMaterializedVectorScope } from './scope.ts';\n\n// RRF composes whatever the preset declares; the constant lives here since it belongs to\n// the fusion these three functions share, not to search() itself.\nconst RRF_K = 60;\n\nexport type SearchResultVia = 'match' | 'match+link' | 'link' | 'match+vector' | 'match+link+vector' | 'link+vector' | 'vector';\n\n/** One participating signal's contribution captured during the existing ranking computation. */\nexport interface SearchSignalContribution {\n signal: SignalName;\n /** One-based position; fusion uses weight / (60 + rank - 1). */\n rank: number;\n /** Effective weight from the selected preset. */\n weight: number;\n /** Unrounded value added to the fused score, independent of the rounded public score and via label. */\n contribution: number;\n}\n\ninterface Candidate {\n score: number;\n via: SearchResultVia;\n explanation?: SearchSignalContribution[];\n}\n\nexport type Candidates = Map<string, Candidate>;\n\nfunction withVector(via: SearchResultVia): SearchResultVia {\n if (via === 'match') return 'match+vector';\n if (via === 'match+link') return 'match+link+vector';\n if (via === 'link') return 'link+vector';\n return via;\n}\n\n// BM25 match rows (store.lexical.query), folded into `candidates` by reciprocal rank scaled by\n// the preset's declared weight for this signal. Every other signal is seeded from its output.\nexport async function wordsCandidates(store: Store, candidates: Candidates, query: string, whereJoin: string, whereCond: string, scopeCond: string, fetch: number, weight: number, explain = false): Promise<LexicalHit[]> {\n const matchRows = await store.lexical.query(query, { whereJoin, whereCond, scopeCond, limit: fetch });\n matchRows.forEach((r, i) => {\n const contribution = weight / (RRF_K + i);\n const candidate: Candidate = { score: contribution, via: 'match' };\n if (explain) candidate.explanation = [{ signal: 'words', rank: i + 1, weight, contribution }];\n candidates.set(r.path, candidate);\n });\n return matchRows;\n}\n\n// Personalized-PageRank expansion from the word-match seeds, folded into `candidates` at this\n// signal's weight. A no-op when there is nothing to seed from (matchRows empty).\nexport async function linksCandidates(store: Store, candidates: Candidates, matchRows: LexicalHit[], allPaths: string[], allowedPaths: Set<string>, fetch: number, weight: number, explain = false): Promise<void> {\n if (matchRows.length === 0) return;\n const stmt = await store.prepare(LINK_EDGES_SQL);\n const edges = toEdges((await stmt.all()) as Array<{ src: string; dst: string }>);\n if (edges.length === 0) return;\n // Gates the label only, not the score: restart mass ranks every seed without an incident\n // edge, but dropping it from the score reweights fusion (FEVER hit@10 0.997 -> 0.907).\n const linked = new Set(edges.flat());\n const seeds = new Map(matchRows.map((r, i) => [r.path, 1 / (i + 1)]));\n const ranked = [...personalizedRank(allPaths, edges, seeds)]\n .filter(([path, score]) => score > 1e-9 && allowedPaths.has(path))\n .sort((a, b) => b[1] - a[1] || Buffer.compare(Buffer.from(a[0]), Buffer.from(b[0])))\n .slice(0, fetch);\n ranked.forEach(([path], i) => {\n const contribution = weight / (RRF_K + i);\n const existing = candidates.get(path);\n if (existing) {\n existing.score += contribution;\n if (explain) {\n existing.explanation ??= [];\n existing.explanation.push({ signal: 'links', rank: i + 1, weight, contribution });\n }\n if (linked.has(path)) existing.via = 'match+link';\n } else {\n const candidate: Candidate = { score: contribution, via: 'link' };\n if (explain) candidate.explanation = [{ signal: 'links', rank: i + 1, weight, contribution }];\n candidates.set(path, candidate);\n }\n });\n}\n\n// Vector expansion at the swept flat-region constants (pool = fetch), folded into `candidates`\n// at this signal's weight; each row also carries its best chunk's line range and similarity.\nexport async function vectorsCandidates(store: Store, prepared: PreparedSemanticQuery, candidates: Candidates, fetch: number, allowedPaths: Set<string>, totalPaths: number, weight: number, explain = false): Promise<{ chunkLines: Map<string, string>; chunkSimilarity: Map<string, number> }> {\n const chunkLines = new Map<string, string>();\n const chunkSimilarity = new Map<string, number>();\n const vec = await withMaterializedVectorScope(store, allowedPaths, () => semanticCandidates(store, prepared, fetch, allowedPaths), totalPaths);\n vec.forEach(({ path, lines, similarity }, i) => {\n const contribution = weight / (RRF_K + i);\n chunkLines.set(path, lines);\n chunkSimilarity.set(path, similarity);\n const existing = candidates.get(path);\n if (existing) {\n existing.score += contribution;\n if (explain) {\n existing.explanation ??= [];\n existing.explanation.push({ signal: 'vectors', rank: i + 1, weight, contribution });\n }\n existing.via = withVector(existing.via);\n } else {\n const candidate: Candidate = { score: contribution, via: 'vector' };\n if (explain) candidate.explanation = [{ signal: 'vectors', rank: i + 1, weight, contribution }];\n candidates.set(path, candidate);\n }\n });\n return { chunkLines, chunkSimilarity };\n}\n"],"names":["semanticCandidates","LINK_EDGES_SQL","toEdges","personalizedRank","withMaterializedVectorScope","RRF_K","withVector","via","wordsCandidates","store","candidates","query","whereJoin","whereCond","scopeCond","fetch","weight","explain","matchRows","lexical","limit","forEach","r","i","contribution","candidate","score","explanation","signal","rank","set","path","linksCandidates","allPaths","allowedPaths","length","stmt","prepare","edges","all","linked","Set","flat","seeds","Map","map","ranked","filter","has","sort","a","b","Buffer","compare","from","slice","existing","get","push","vectorsCandidates","prepared","totalPaths","chunkLines","chunkSimilarity","vec","lines","similarity"],"mappings":"AAEA,SAASA,kBAAkB,QAAQ,oBAAoB;AACvD,SAASC,cAAc,EAAEC,OAAO,QAAQ,uBAAuB;AAC/D,SAASC,gBAAgB,QAAQ,oBAAoB;AAErD,SAASC,2BAA2B,QAAQ,aAAa;AAEzD,yFAAyF;AACzF,kEAAkE;AAClE,MAAMC,QAAQ;AAuBd,SAASC,WAAWC,GAAoB;IACtC,IAAIA,QAAQ,SAAS,OAAO;IAC5B,IAAIA,QAAQ,cAAc,OAAO;IACjC,IAAIA,QAAQ,QAAQ,OAAO;IAC3B,OAAOA;AACT;AAEA,+FAA+F;AAC/F,8FAA8F;AAC9F,OAAO,eAAeC,gBAAgBC,KAAY,EAAEC,UAAsB,EAAEC,KAAa,EAAEC,SAAiB,EAAEC,SAAiB,EAAEC,SAAiB,EAAEC,KAAa,EAAEC,MAAc,EAAEC,UAAU,KAAK;IAChM,MAAMC,YAAY,MAAMT,MAAMU,OAAO,CAACR,KAAK,CAACA,OAAO;QAAEC;QAAWC;QAAWC;QAAWM,OAAOL;IAAM;IACnGG,UAAUG,OAAO,CAAC,CAACC,GAAGC;QACpB,MAAMC,eAAeR,SAAUX,CAAAA,QAAQkB,CAAAA;QACvC,MAAME,YAAuB;YAAEC,OAAOF;YAAcjB,KAAK;QAAQ;QACjE,IAAIU,SAASQ,UAAUE,WAAW,GAAG;YAAC;gBAAEC,QAAQ;gBAASC,MAAMN,IAAI;gBAAGP;gBAAQQ;YAAa;SAAE;QAC7Fd,WAAWoB,GAAG,CAACR,EAAES,IAAI,EAAEN;IACzB;IACA,OAAOP;AACT;AAEA,8FAA8F;AAC9F,iFAAiF;AACjF,OAAO,eAAec,gBAAgBvB,KAAY,EAAEC,UAAsB,EA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AQQ;gBAAa;YACnF;YACAgC,SAASjD,GAAG,GAAGD,WAAWkD,SAASjD,GAAG;QACxC,OAAO;YACL,MAAMkB,YAAuB;gBAAEC,OAAOF;gBAAcjB,KAAK;YAAS;YAClE,IAAIU,SAASQ,UAAUE,WAAW,GAAG;gBAAC;oBAAEC,QAAQ;oBAAWC,MAAMN,IAAI;oBAAGP;oBAAQQ;gBAAa;aAAE;YAC/Fd,WAAWoB,GAAG,CAACC,MAAMN;QACvB;IACF;IACA,OAAO;QAAEqC;QAAYC;IAAgB;AACvC"}
|
package/dist/esm/config/load.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { channel } from 'node:diagnostics_channel';
|
|
1
2
|
import { existsSync, readFileSync } from 'node:fs';
|
|
2
3
|
import { dirname, join, resolve } from 'node:path';
|
|
3
4
|
import { SenseError } from '../errors.js';
|
|
@@ -7,6 +8,7 @@ import { unknownConfigKeys, validateConfig, validateLegacyScan } from './validat
|
|
|
7
8
|
// v1 and v2 were `scan`/`find`-shaped; v3 introduced `presets`. Only the former need the
|
|
8
9
|
// legacy shape check before migrating.
|
|
9
10
|
const FIRST_PRESET_VERSION = 3;
|
|
11
|
+
const migrationReady = channel('sensemaking.config.migration-ready');
|
|
10
12
|
// v1 -> v2 migration only: the features v2 introduced as opt-out (embed was opt-in then).
|
|
11
13
|
const V2_OPT_OUT_NAMES = [
|
|
12
14
|
'links',
|
|
@@ -282,6 +284,10 @@ export function loadConfig(explicitPath, options = {}) {
|
|
|
282
284
|
migratedFrom = result.from;
|
|
283
285
|
if (options.writeMigration !== false) {
|
|
284
286
|
const migrated = `${JSON.stringify(cfg, null, 2)}\n`;
|
|
287
|
+
if (migrationReady.hasSubscribers) migrationReady.publish({
|
|
288
|
+
configPath,
|
|
289
|
+
from: migratedFrom
|
|
290
|
+
});
|
|
285
291
|
try {
|
|
286
292
|
writeFileAtomic(configPath, migrated);
|
|
287
293
|
} catch (err) {
|