@esankhan3/anvil-knowledge-core 0.1.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 +194 -0
- package/dist/__tests__/chunker.test.d.ts +10 -0
- package/dist/__tests__/chunker.test.d.ts.map +1 -0
- package/dist/__tests__/chunker.test.js +161 -0
- package/dist/__tests__/chunker.test.js.map +1 -0
- package/dist/__tests__/query-classifier.test.d.ts +5 -0
- package/dist/__tests__/query-classifier.test.d.ts.map +1 -0
- package/dist/__tests__/query-classifier.test.js +158 -0
- package/dist/__tests__/query-classifier.test.js.map +1 -0
- package/dist/__tests__/retriever-defaults.test.d.ts +12 -0
- package/dist/__tests__/retriever-defaults.test.d.ts.map +1 -0
- package/dist/__tests__/retriever-defaults.test.js +37 -0
- package/dist/__tests__/retriever-defaults.test.js.map +1 -0
- package/dist/__tests__/structural-hasher.test.d.ts +5 -0
- package/dist/__tests__/structural-hasher.test.d.ts.map +1 -0
- package/dist/__tests__/structural-hasher.test.js +185 -0
- package/dist/__tests__/structural-hasher.test.js.map +1 -0
- package/dist/ast-graph-builder.d.ts +38 -0
- package/dist/ast-graph-builder.d.ts.map +1 -0
- package/dist/ast-graph-builder.js +1041 -0
- package/dist/ast-graph-builder.js.map +1 -0
- package/dist/chunker.d.ts +47 -0
- package/dist/chunker.d.ts.map +1 -0
- package/dist/chunker.js +428 -0
- package/dist/chunker.js.map +1 -0
- package/dist/claude-runner.d.ts +10 -0
- package/dist/claude-runner.d.ts.map +1 -0
- package/dist/claude-runner.js +10 -0
- package/dist/claude-runner.js.map +1 -0
- package/dist/config.d.ts +36 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +83 -0
- package/dist/config.js.map +1 -0
- package/dist/cross-repo-detector.d.ts +7 -0
- package/dist/cross-repo-detector.d.ts.map +1 -0
- package/dist/cross-repo-detector.js +990 -0
- package/dist/cross-repo-detector.js.map +1 -0
- package/dist/embedder.d.ts +84 -0
- package/dist/embedder.d.ts.map +1 -0
- package/dist/embedder.js +378 -0
- package/dist/embedder.js.map +1 -0
- package/dist/file-walker.d.ts +18 -0
- package/dist/file-walker.d.ts.map +1 -0
- package/dist/file-walker.js +221 -0
- package/dist/file-walker.js.map +1 -0
- package/dist/git-diff.d.ts +32 -0
- package/dist/git-diff.d.ts.map +1 -0
- package/dist/git-diff.js +145 -0
- package/dist/git-diff.js.map +1 -0
- package/dist/graph-metrics.d.ts +31 -0
- package/dist/graph-metrics.d.ts.map +1 -0
- package/dist/graph-metrics.js +277 -0
- package/dist/graph-metrics.js.map +1 -0
- package/dist/graph-query.d.ts +8 -0
- package/dist/graph-query.d.ts.map +1 -0
- package/dist/graph-query.js +79 -0
- package/dist/graph-query.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/indexer.d.ts +98 -0
- package/dist/indexer.d.ts.map +1 -0
- package/dist/indexer.js +635 -0
- package/dist/indexer.js.map +1 -0
- package/dist/project-graph-builder-core.d.ts +48 -0
- package/dist/project-graph-builder-core.d.ts.map +1 -0
- package/dist/project-graph-builder-core.js +213 -0
- package/dist/project-graph-builder-core.js.map +1 -0
- package/dist/project-graph-builder.d.ts +39 -0
- package/dist/project-graph-builder.d.ts.map +1 -0
- package/dist/project-graph-builder.js +640 -0
- package/dist/project-graph-builder.js.map +1 -0
- package/dist/query-classifier.d.ts +38 -0
- package/dist/query-classifier.d.ts.map +1 -0
- package/dist/query-classifier.js +199 -0
- package/dist/query-classifier.js.map +1 -0
- package/dist/query-router.d.ts +82 -0
- package/dist/query-router.d.ts.map +1 -0
- package/dist/query-router.js +278 -0
- package/dist/query-router.js.map +1 -0
- package/dist/rag-evaluator.d.ts +53 -0
- package/dist/rag-evaluator.d.ts.map +1 -0
- package/dist/rag-evaluator.js +217 -0
- package/dist/rag-evaluator.js.map +1 -0
- package/dist/repo-profiler.d.ts +62 -0
- package/dist/repo-profiler.d.ts.map +1 -0
- package/dist/repo-profiler.js +488 -0
- package/dist/repo-profiler.js.map +1 -0
- package/dist/reranker.d.ts +8 -0
- package/dist/reranker.d.ts.map +1 -0
- package/dist/reranker.js +250 -0
- package/dist/reranker.js.map +1 -0
- package/dist/retriever.d.ts +42 -0
- package/dist/retriever.d.ts.map +1 -0
- package/dist/retriever.js +320 -0
- package/dist/retriever.js.map +1 -0
- package/dist/semantic-edge-detector.d.ts +28 -0
- package/dist/semantic-edge-detector.d.ts.map +1 -0
- package/dist/semantic-edge-detector.js +191 -0
- package/dist/semantic-edge-detector.js.map +1 -0
- package/dist/service-mesh-inferrer.d.ts +57 -0
- package/dist/service-mesh-inferrer.d.ts.map +1 -0
- package/dist/service-mesh-inferrer.js +322 -0
- package/dist/service-mesh-inferrer.js.map +1 -0
- package/dist/structural-hasher.d.ts +57 -0
- package/dist/structural-hasher.d.ts.map +1 -0
- package/dist/structural-hasher.js +350 -0
- package/dist/structural-hasher.js.map +1 -0
- package/dist/tree-sitter-parser.d.ts +71 -0
- package/dist/tree-sitter-parser.d.ts.map +1 -0
- package/dist/tree-sitter-parser.js +633 -0
- package/dist/tree-sitter-parser.js.map +1 -0
- package/dist/types.d.ts +179 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/vector-store.d.ts +56 -0
- package/dist/vector-store.d.ts.map +1 -0
- package/dist/vector-store.js +299 -0
- package/dist/vector-store.js.map +1 -0
- package/dist/workspace-detector.d.ts +11 -0
- package/dist/workspace-detector.d.ts.map +1 -0
- package/dist/workspace-detector.js +587 -0
- package/dist/workspace-detector.js.map +1 -0
- package/package.json +41 -0
package/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# @anvil/knowledge-core
|
|
2
|
+
|
|
3
|
+
**Your codebase, retrievable.**
|
|
4
|
+
|
|
5
|
+
A code-aware knowledge stack — AST-level chunking, hybrid retrieval,
|
|
6
|
+
project graphs, and cross-repo edge detection. Built so agents see
|
|
7
|
+
the *whole* codebase, not just the file you happened to open.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## The problem with grep + embeddings
|
|
12
|
+
|
|
13
|
+
Code search works one of two ways: lexical (grep, fast, dumb) or
|
|
14
|
+
embedding-based (vector DB, slow on edits, blind to structure). Both
|
|
15
|
+
miss the same thing: code has *graph* structure. A function isn't
|
|
16
|
+
just a chunk of text — it imports things, gets called by things,
|
|
17
|
+
implements things, lives in a package, talks to other repos.
|
|
18
|
+
|
|
19
|
+
**knowledge-core treats the codebase like a graph and a corpus at
|
|
20
|
+
the same time.** Tree-sitter parses every supported language into
|
|
21
|
+
AST chunks. A vector store + BM25 + a project graph all answer the
|
|
22
|
+
same query, fused into one ranked list. The retriever knows when
|
|
23
|
+
you're asking about an identifier vs an error code vs a natural-
|
|
24
|
+
language question and weights the signals accordingly.
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { KnowledgeIndexer, getRetriever } from '@anvil/knowledge-core';
|
|
28
|
+
|
|
29
|
+
// Index once — incremental thereafter.
|
|
30
|
+
const indexer = new KnowledgeIndexer({ project: 'space-tourism' });
|
|
31
|
+
await indexer.indexProject();
|
|
32
|
+
|
|
33
|
+
// Retrieve — vector + BM25 + 1-hop graph + cross-encoder rerank.
|
|
34
|
+
const retriever = await getRetriever('space-tourism');
|
|
35
|
+
const result = await retriever.retrieve(
|
|
36
|
+
'where do we validate booking seat tiers?',
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
console.log(result.chunks.length, 'chunks'); // ranked, deduped
|
|
40
|
+
console.log(result.graphContext); // related symbols
|
|
41
|
+
console.log(result.totalTokens); // budgeted
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## What you get
|
|
47
|
+
|
|
48
|
+
### AST-aware chunking
|
|
49
|
+
`tree-sitter` WASM parses TypeScript, JavaScript, TSX, Go, Python,
|
|
50
|
+
Rust, Java, and PHP into proper function / class / method
|
|
51
|
+
boundaries. No more chunks that cut a function in half. Regex
|
|
52
|
+
fallback for languages without a grammar so nothing is unindexed.
|
|
53
|
+
|
|
54
|
+
### Incremental by default
|
|
55
|
+
The indexer reads `git rev-parse HEAD`, diffs against the last
|
|
56
|
+
indexed SHA, and re-chunks only what changed. Deleted files get
|
|
57
|
+
removed from the vector store. Embedding is independently
|
|
58
|
+
incremental — it diffs new chunk IDs against LanceDB and only
|
|
59
|
+
embeds the deltas. Big repos stay fast forever.
|
|
60
|
+
|
|
61
|
+
### Hybrid retrieval, four phases
|
|
62
|
+
1. **Vector ⫽ BM25 in parallel** — semantic recall + lexical recall.
|
|
63
|
+
2. **Reciprocal Rank Fusion** — combine without one dominating.
|
|
64
|
+
3. **AST tripartite expansion** — pull in callers, callees, and
|
|
65
|
+
type references via the project graph.
|
|
66
|
+
4. **Cross-encoder rerank** — Qwen3-Reranker by default; Cohere /
|
|
67
|
+
Voyage / OpenAI-compatible swappable.
|
|
68
|
+
|
|
69
|
+
The query classifier picks adaptive weights — identifiers lean
|
|
70
|
+
BM25, natural-language leans vector, error codes lean both.
|
|
71
|
+
|
|
72
|
+
### Six embedding providers, four rerankers
|
|
73
|
+
**Embed:** Codestral · Voyage · OpenAI · Ollama · Gemini OAuth ·
|
|
74
|
+
OpenAI-compatible · `auto` (picks based on what's configured).
|
|
75
|
+
**Rerank:** Ollama (default) · Cohere · Voyage · OpenAI-compatible.
|
|
76
|
+
Plug in whatever fits your cost/quality/latency curve.
|
|
77
|
+
|
|
78
|
+
### Project graph
|
|
79
|
+
A `graphology` directed multi-graph stitches every repo together.
|
|
80
|
+
Nodes are entities (functions, classes, types, modules), edges are
|
|
81
|
+
imports / calls / inheritance / contains / type-refs. Louvain
|
|
82
|
+
community detection clusters semantically related code so retrieval
|
|
83
|
+
can surface "the auth subsystem" rather than four scattered files.
|
|
84
|
+
|
|
85
|
+
### Cross-repo edge detection
|
|
86
|
+
Fourteen strategies covering shared types, Kafka topics, HTTP
|
|
87
|
+
endpoints, gRPC, databases, env vars, npm/workspace deps, k8s,
|
|
88
|
+
docker-compose, proto definitions, Redis, S3, and shared constants.
|
|
89
|
+
Plus an LLM-inferred semantic edge layer for the cases regex can't
|
|
90
|
+
catch.
|
|
91
|
+
|
|
92
|
+
### LLM-driven where it matters
|
|
93
|
+
Repo profiling (fingerprint files → LLM → typed `RepoProfile`,
|
|
94
|
+
cached by fingerprint hash), semantic edge inference, project
|
|
95
|
+
summary generation, RAG quality evaluation. All routed through
|
|
96
|
+
`@anvil/agent-core` so the same router, retries, and cost ledger
|
|
97
|
+
that power the agent stack power knowledge ingestion.
|
|
98
|
+
|
|
99
|
+
### Structural hashing
|
|
100
|
+
`computeStructuralHash` canonicalizes source — strips comments,
|
|
101
|
+
collapses whitespace, normalizes identifiers — and SHA-256s the
|
|
102
|
+
result. Used for chunk dedup *and* shared with `@anvil/memory-core`
|
|
103
|
+
so memory drift detection and chunk dedup speak the same language.
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## Architecture at a glance
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
git repo(s)
|
|
111
|
+
│
|
|
112
|
+
▼
|
|
113
|
+
┌─────────────────────────────────────────────────────────┐
|
|
114
|
+
│ Indexing │
|
|
115
|
+
│ walk + git diff → tree-sitter chunks → AST graph │
|
|
116
|
+
│ │ │ │ │
|
|
117
|
+
│ ▼ ▼ ▼ │
|
|
118
|
+
│ LanceDB BM25 (FTS) project graph │
|
|
119
|
+
│ (vectors) + cross-repo edges │
|
|
120
|
+
└─────────────────────────────────────────────────────────┘
|
|
121
|
+
│
|
|
122
|
+
▼
|
|
123
|
+
┌─────────────────────────────────────────────────────────┐
|
|
124
|
+
│ HybridRetriever │
|
|
125
|
+
│ query classifier ─▶ adaptive weights │
|
|
126
|
+
│ vector ⫽ BM25 → RRF → AST expansion → rerank │
|
|
127
|
+
└─────────────────────────────────────────────────────────┘
|
|
128
|
+
│
|
|
129
|
+
▼
|
|
130
|
+
ranked chunks + graph context
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Two consumers, one stack: `@esankhan3/anvil-cli` indexes via
|
|
134
|
+
`anvil index` and retrieves during pipelines;
|
|
135
|
+
`@esankhan3/code-search-mcp` exposes the same retriever as MCP
|
|
136
|
+
tools that any agent can call.
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## Storage layout
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
~/.anvil/knowledge-base/<project>/
|
|
144
|
+
chunks.json # canonical chunks (consumed by embedder)
|
|
145
|
+
deleted_files.json # incremental embed cleanup
|
|
146
|
+
system_graph_v2.json # merged project graph
|
|
147
|
+
PROJECT_GRAPH.json # LLM-generated semantic graph
|
|
148
|
+
PROJECT_SUMMARY.md # human-readable companion
|
|
149
|
+
lancedb/ # vector store
|
|
150
|
+
<repo>/
|
|
151
|
+
profile.json # cached RepoProfile
|
|
152
|
+
graph.json # per-repo AST graph
|
|
153
|
+
GRAPH_REPORT.md # quality report
|
|
154
|
+
index_meta.json # { lastIndexedSha, files, chunkCount }
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Everything is on disk, inspectable, git-friendly where it makes
|
|
158
|
+
sense (project graph + summary).
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
## Philosophy
|
|
163
|
+
|
|
164
|
+
**Chunks should follow code shape, not byte count.** AST boundaries
|
|
165
|
+
keep functions whole. Retrieval quality starts at the chunker.
|
|
166
|
+
|
|
167
|
+
**No single retrieval signal is enough.** Vector misses identifiers.
|
|
168
|
+
BM25 misses paraphrases. Graph misses everything text-shaped.
|
|
169
|
+
Hybrid + adaptive weighting is the only honest answer.
|
|
170
|
+
|
|
171
|
+
**Incremental or nothing.** Anvil indexes on every pipeline run.
|
|
172
|
+
A full re-index isn't acceptable; the engine treats the previous
|
|
173
|
+
SHA as a load-bearing input.
|
|
174
|
+
|
|
175
|
+
**No vendor lock-in.** LanceDB on disk. `graphology` in-process.
|
|
176
|
+
Pluggable embedders + rerankers. Swap any layer without rewriting
|
|
177
|
+
the rest.
|
|
178
|
+
|
|
179
|
+
**Two surfaces, one engine.** CLI users get retrieval through
|
|
180
|
+
pipelines; MCP users get it through tool calls. The retrieval
|
|
181
|
+
pipeline is the same code path.
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## Status
|
|
186
|
+
|
|
187
|
+
Stable: chunking, AST graph, hybrid retrieval, incremental
|
|
188
|
+
indexing, six embedders, four rerankers, fourteen cross-repo
|
|
189
|
+
strategies, project graph, structural hashing. In flight: richer
|
|
190
|
+
graph queries and a deeper RAG-eval harness.
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Part of [Anvil](../../) — the AI development pipeline.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for chunker.ts — AST-aware code chunking.
|
|
3
|
+
*
|
|
4
|
+
* We cannot easily call chunkRepo() (it walks a real directory), so we
|
|
5
|
+
* test the chunking logic by writing temp files and invoking chunkRepo
|
|
6
|
+
* on a small temp directory, or by testing the observable behavior through
|
|
7
|
+
* the public API.
|
|
8
|
+
*/
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=chunker.test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chunker.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/chunker.test.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for chunker.ts — AST-aware code chunking.
|
|
3
|
+
*
|
|
4
|
+
* We cannot easily call chunkRepo() (it walks a real directory), so we
|
|
5
|
+
* test the chunking logic by writing temp files and invoking chunkRepo
|
|
6
|
+
* on a small temp directory, or by testing the observable behavior through
|
|
7
|
+
* the public API.
|
|
8
|
+
*/
|
|
9
|
+
import { describe, it, beforeEach, afterEach } from 'node:test';
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
import { mkdirSync, writeFileSync, rmSync, existsSync } from 'node:fs';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
import { tmpdir } from 'node:os';
|
|
14
|
+
import { randomBytes } from 'node:crypto';
|
|
15
|
+
import { chunkRepo } from '@esankhan3/anvil-knowledge-core';
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// Helpers
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
function createTempDir() {
|
|
20
|
+
const dir = join(tmpdir(), `chunker-test-${randomBytes(6).toString('hex')}`);
|
|
21
|
+
mkdirSync(dir, { recursive: true });
|
|
22
|
+
return dir;
|
|
23
|
+
}
|
|
24
|
+
function cleanupDir(dir) {
|
|
25
|
+
if (existsSync(dir)) {
|
|
26
|
+
rmSync(dir, { recursive: true, force: true });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Tests
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
describe('chunkRepo', () => {
|
|
33
|
+
let tempDir;
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
tempDir = createTempDir();
|
|
36
|
+
});
|
|
37
|
+
afterEach(() => {
|
|
38
|
+
cleanupDir(tempDir);
|
|
39
|
+
});
|
|
40
|
+
it('chunks a simple TypeScript file into expected chunks', async () => {
|
|
41
|
+
const code = [
|
|
42
|
+
'import { readFile } from "node:fs";',
|
|
43
|
+
'',
|
|
44
|
+
'export function greet(name: string): string {',
|
|
45
|
+
' return `Hello, ${name}!`;',
|
|
46
|
+
'}',
|
|
47
|
+
'',
|
|
48
|
+
'export function farewell(name: string): string {',
|
|
49
|
+
' return `Goodbye, ${name}!`;',
|
|
50
|
+
'}',
|
|
51
|
+
].join('\n');
|
|
52
|
+
writeFileSync(join(tempDir, 'utils.ts'), code);
|
|
53
|
+
const result = await chunkRepo(tempDir, 'test-repo', 'test-project', { maxTokens: 512 });
|
|
54
|
+
assert.ok(result.chunks.length >= 2, `Expected >= 2 chunks, got ${result.chunks.length}`);
|
|
55
|
+
// Should have at least one chunk with entityName 'greet' and one with 'farewell'
|
|
56
|
+
const names = result.chunks.map((c) => c.entityName).filter(Boolean);
|
|
57
|
+
assert.ok(names.includes('greet'), 'Should have a "greet" chunk');
|
|
58
|
+
assert.ok(names.includes('farewell'), 'Should have a "farewell" chunk');
|
|
59
|
+
});
|
|
60
|
+
it('preserves entity boundaries — functions are not split mid-body', async () => {
|
|
61
|
+
// A function small enough to fit in one chunk should not be split
|
|
62
|
+
const code = [
|
|
63
|
+
'export function compute(x: number): number {',
|
|
64
|
+
' const a = x * 2;',
|
|
65
|
+
' const b = a + 1;',
|
|
66
|
+
' return b;',
|
|
67
|
+
'}',
|
|
68
|
+
].join('\n');
|
|
69
|
+
writeFileSync(join(tempDir, 'compute.ts'), code);
|
|
70
|
+
const result = await chunkRepo(tempDir, 'test-repo', 'test-project', { maxTokens: 512 });
|
|
71
|
+
// The function should be in exactly one chunk
|
|
72
|
+
const computeChunks = result.chunks.filter((c) => c.entityName === 'compute');
|
|
73
|
+
assert.equal(computeChunks.length, 1, 'Small function should be a single chunk');
|
|
74
|
+
// The chunk should contain the full function body
|
|
75
|
+
assert.ok(computeChunks[0].content.includes('return b;'), 'Chunk should contain full body');
|
|
76
|
+
});
|
|
77
|
+
it('respects max token limits by splitting large content', async () => {
|
|
78
|
+
// Create a file with many functions that would exceed a small token limit
|
|
79
|
+
const lines = [];
|
|
80
|
+
for (let i = 0; i < 20; i++) {
|
|
81
|
+
lines.push(`export function fn${i}() {`);
|
|
82
|
+
// Add enough lines to make the chunk substantial
|
|
83
|
+
for (let j = 0; j < 10; j++) {
|
|
84
|
+
lines.push(` const v${j} = ${j} + ${i};`);
|
|
85
|
+
}
|
|
86
|
+
lines.push(' return 0;');
|
|
87
|
+
lines.push('}');
|
|
88
|
+
lines.push('');
|
|
89
|
+
}
|
|
90
|
+
writeFileSync(join(tempDir, 'big.ts'), lines.join('\n'));
|
|
91
|
+
// Use a very small maxTokens to force splitting
|
|
92
|
+
const result = await chunkRepo(tempDir, 'test-repo', 'test-project', { maxTokens: 64 });
|
|
93
|
+
// With a 64-token limit, each function chunk should be small
|
|
94
|
+
for (const chunk of result.chunks) {
|
|
95
|
+
const tokenEstimate = Math.ceil(chunk.contextualizedContent.length / 4);
|
|
96
|
+
// Allow some tolerance since splitting is at line boundaries
|
|
97
|
+
assert.ok(tokenEstimate <= 128, `Chunk "${chunk.entityName}" has ~${tokenEstimate} tokens, expected <= ~128 (2x budget for boundary tolerance)`);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
it('empty file produces no chunks', async () => {
|
|
101
|
+
writeFileSync(join(tempDir, 'empty.ts'), '');
|
|
102
|
+
const result = await chunkRepo(tempDir, 'test-repo', 'test-project', { maxTokens: 512 });
|
|
103
|
+
// Empty file should produce zero or at most one empty chunk
|
|
104
|
+
const emptyChunks = result.chunks.filter((c) => c.filePath.includes('empty.ts'));
|
|
105
|
+
// An empty file has no content and no boundaries, so it should produce 0 chunks
|
|
106
|
+
// (the module chunk would have empty content which gets trimmed)
|
|
107
|
+
assert.ok(emptyChunks.length === 0 || emptyChunks.every((c) => c.content.trim() === ''), 'Empty file should produce no meaningful chunks');
|
|
108
|
+
});
|
|
109
|
+
it('sets correct metadata on chunks', async () => {
|
|
110
|
+
const code = 'export function hello() { return "hi"; }';
|
|
111
|
+
writeFileSync(join(tempDir, 'meta.ts'), code);
|
|
112
|
+
const result = await chunkRepo(tempDir, 'test-repo', 'test-project', { maxTokens: 512 });
|
|
113
|
+
assert.ok(result.chunks.length > 0);
|
|
114
|
+
const chunk = result.chunks.find((c) => c.entityName === 'hello');
|
|
115
|
+
assert.ok(chunk, 'Should find "hello" chunk');
|
|
116
|
+
assert.equal(chunk.repoName, 'test-repo');
|
|
117
|
+
assert.equal(chunk.project, 'test-project');
|
|
118
|
+
assert.equal(chunk.language, 'typescript');
|
|
119
|
+
assert.equal(chunk.entityType, 'function');
|
|
120
|
+
assert.ok(chunk.id.length > 0, 'Chunk should have an id');
|
|
121
|
+
assert.ok(chunk.tokens > 0, 'Chunk should have token estimate');
|
|
122
|
+
assert.ok(chunk.contextPrefix.length > 0, 'Chunk should have context prefix');
|
|
123
|
+
assert.ok(chunk.contextualizedContent.length > chunk.content.length, 'Contextualized content should include prefix');
|
|
124
|
+
});
|
|
125
|
+
it('returns changedFiles and fileIndex', async () => {
|
|
126
|
+
writeFileSync(join(tempDir, 'a.ts'), 'const a = 1;');
|
|
127
|
+
writeFileSync(join(tempDir, 'b.ts'), 'const b = 2;');
|
|
128
|
+
const result = await chunkRepo(tempDir, 'test-repo', 'test-project', { maxTokens: 512 });
|
|
129
|
+
// Both files should be in changedFiles (no cache provided)
|
|
130
|
+
assert.ok(result.changedFiles.length >= 2);
|
|
131
|
+
assert.ok(Object.keys(result.fileIndex).length >= 2);
|
|
132
|
+
});
|
|
133
|
+
it('skips unchanged files when cache is provided', async () => {
|
|
134
|
+
writeFileSync(join(tempDir, 'cached.ts'), 'const cached = 1;');
|
|
135
|
+
// First pass — get the file index
|
|
136
|
+
const first = await chunkRepo(tempDir, 'test-repo', 'test-project', { maxTokens: 512 });
|
|
137
|
+
assert.ok(first.changedFiles.length > 0);
|
|
138
|
+
// Second pass with cache — file is unchanged
|
|
139
|
+
const second = await chunkRepo(tempDir, 'test-repo', 'test-project', { maxTokens: 512 }, first.fileIndex);
|
|
140
|
+
assert.equal(second.changedFiles.length, 0, 'No files should be changed on second pass');
|
|
141
|
+
assert.equal(second.chunks.length, 0, 'No new chunks needed');
|
|
142
|
+
});
|
|
143
|
+
it('detects deleted files compared to cache', async () => {
|
|
144
|
+
writeFileSync(join(tempDir, 'willdelete.ts'), 'const x = 1;');
|
|
145
|
+
const first = await chunkRepo(tempDir, 'test-repo', 'test-project', { maxTokens: 512 });
|
|
146
|
+
// Delete the file
|
|
147
|
+
rmSync(join(tempDir, 'willdelete.ts'));
|
|
148
|
+
const second = await chunkRepo(tempDir, 'test-repo', 'test-project', { maxTokens: 512 }, first.fileIndex);
|
|
149
|
+
assert.ok(second.deletedFiles.some((f) => f.includes('willdelete.ts')), 'Should detect deleted file');
|
|
150
|
+
});
|
|
151
|
+
it('handles non-source files gracefully (skips them)', async () => {
|
|
152
|
+
writeFileSync(join(tempDir, 'readme.md'), '# Hello');
|
|
153
|
+
writeFileSync(join(tempDir, 'data.json'), '{"key": "value"}');
|
|
154
|
+
writeFileSync(join(tempDir, 'code.ts'), 'export const x = 1;');
|
|
155
|
+
const result = await chunkRepo(tempDir, 'test-repo', 'test-project', { maxTokens: 512 });
|
|
156
|
+
// Only the .ts file should produce chunks
|
|
157
|
+
const filePaths = result.chunks.map((c) => c.filePath);
|
|
158
|
+
assert.ok(filePaths.every((p) => p.endsWith('.ts')), 'Only TS files should be chunked');
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
//# sourceMappingURL=chunker.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chunker.test.js","sourceRoot":"","sources":["../../src/__tests__/chunker.test.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAChE,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACvE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;AAE5D,8EAA8E;AAC9E,UAAU;AACV,8EAA8E;AAE9E,SAAS,aAAa;IACpB,MAAM,GA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,KAAK,CAAC,SAAS,CAChB,CAAC;QAEF,MAAM,CAAC,EAAE,CACP,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC,EAC5D,4BAA4B,CAC7B,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,kDAAkD,EAAE,KAAK,IAAI,EAAE;QAChE,aAAa,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,EAAE,SAAS,CAAC,CAAC;QACrD,aAAa,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,EAAE,kBAAkB,CAAC,CAAC;QAC9D,aAAa,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC,EAAE,qBAAqB,CAAC,CAAC;QAE/D,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC,CAAC;QAEzF,0CAA0C;QAC1C,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;QACvD,MAAM,CAAC,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,iCAAiC,CAAC,CAAC;IAC1F,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-classifier.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/query-classifier.test.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for query-classifier.ts — query classification and weight assignment.
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it } from 'node:test';
|
|
5
|
+
import assert from 'node:assert/strict';
|
|
6
|
+
import { classifyQuery, routeQueryToRepos } from '@esankhan3/anvil-knowledge-core';
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// classifyQuery
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
describe('classifyQuery', () => {
|
|
11
|
+
it('classifies camelCase identifier', () => {
|
|
12
|
+
const result = classifyQuery('getUserById');
|
|
13
|
+
assert.equal(result.type, 'identifier');
|
|
14
|
+
assert.equal(result.shouldUseTrigram, true);
|
|
15
|
+
});
|
|
16
|
+
it('classifies snake_case identifier', () => {
|
|
17
|
+
const result = classifyQuery('get_user_by_id');
|
|
18
|
+
assert.equal(result.type, 'identifier');
|
|
19
|
+
});
|
|
20
|
+
it('classifies PascalCase identifier', () => {
|
|
21
|
+
const result = classifyQuery('UserService');
|
|
22
|
+
assert.equal(result.type, 'identifier');
|
|
23
|
+
});
|
|
24
|
+
it('classifies natural language question', () => {
|
|
25
|
+
const result = classifyQuery('how does authentication work');
|
|
26
|
+
assert.equal(result.type, 'natural-language');
|
|
27
|
+
assert.equal(result.shouldUseTrigram, false);
|
|
28
|
+
});
|
|
29
|
+
it('classifies long natural language phrase', () => {
|
|
30
|
+
const result = classifyQuery('explain the data pipeline flow from ingestion to storage');
|
|
31
|
+
assert.equal(result.type, 'natural-language');
|
|
32
|
+
});
|
|
33
|
+
it('classifies file path', () => {
|
|
34
|
+
const result = classifyQuery('src/utils/auth.ts');
|
|
35
|
+
assert.equal(result.type, 'path');
|
|
36
|
+
assert.equal(result.shouldUseTrigram, true);
|
|
37
|
+
});
|
|
38
|
+
it('classifies Windows-style path', () => {
|
|
39
|
+
const result = classifyQuery('src\\utils\\auth.ts');
|
|
40
|
+
assert.equal(result.type, 'path');
|
|
41
|
+
});
|
|
42
|
+
it('classifies ERR_ error code', () => {
|
|
43
|
+
const result = classifyQuery('ERR_MODULE_NOT_FOUND');
|
|
44
|
+
assert.equal(result.type, 'error-code');
|
|
45
|
+
assert.equal(result.shouldUseTrigram, true);
|
|
46
|
+
});
|
|
47
|
+
it('classifies hex error code', () => {
|
|
48
|
+
const result = classifyQuery('0xDEADBEEF');
|
|
49
|
+
assert.equal(result.type, 'error-code');
|
|
50
|
+
});
|
|
51
|
+
it('classifies numeric error code', () => {
|
|
52
|
+
const result = classifyQuery('E12345');
|
|
53
|
+
assert.equal(result.type, 'error-code');
|
|
54
|
+
});
|
|
55
|
+
it('classifies HTTP status as error-code in short query', () => {
|
|
56
|
+
const result = classifyQuery('404');
|
|
57
|
+
assert.equal(result.type, 'error-code');
|
|
58
|
+
});
|
|
59
|
+
it('classifies mixed query with identifier and question word', () => {
|
|
60
|
+
const result = classifyQuery('how does getUserById handle null input');
|
|
61
|
+
assert.equal(result.type, 'mixed');
|
|
62
|
+
assert.equal(result.shouldUseTrigram, true);
|
|
63
|
+
});
|
|
64
|
+
it('classifies mixed query with identifier and long phrase', () => {
|
|
65
|
+
const result = classifyQuery('getUserById authentication service layer middleware pipeline');
|
|
66
|
+
assert.equal(result.type, 'mixed');
|
|
67
|
+
});
|
|
68
|
+
it('treats empty query as natural-language', () => {
|
|
69
|
+
const result = classifyQuery('');
|
|
70
|
+
assert.equal(result.type, 'natural-language');
|
|
71
|
+
assert.equal(result.shouldUseTrigram, false);
|
|
72
|
+
});
|
|
73
|
+
it('treats whitespace-only query as natural-language', () => {
|
|
74
|
+
const result = classifyQuery(' ');
|
|
75
|
+
assert.equal(result.type, 'natural-language');
|
|
76
|
+
});
|
|
77
|
+
// --- Weight structure ---
|
|
78
|
+
it('returns weights that sum to 1.0 for identifier', () => {
|
|
79
|
+
const { weights } = classifyQuery('getUserById');
|
|
80
|
+
const sum = weights.vector + weights.bm25 + weights.graph;
|
|
81
|
+
assert.ok(Math.abs(sum - 1.0) < 0.001, `Weights sum to ${sum}, expected ~1.0`);
|
|
82
|
+
});
|
|
83
|
+
it('returns weights that sum to 1.0 for natural-language', () => {
|
|
84
|
+
const { weights } = classifyQuery('how does authentication work');
|
|
85
|
+
const sum = weights.vector + weights.bm25 + weights.graph;
|
|
86
|
+
assert.ok(Math.abs(sum - 1.0) < 0.001, `Weights sum to ${sum}, expected ~1.0`);
|
|
87
|
+
});
|
|
88
|
+
it('returns weights that sum to 1.0 for path', () => {
|
|
89
|
+
const { weights } = classifyQuery('src/utils/auth.ts');
|
|
90
|
+
const sum = weights.vector + weights.bm25 + weights.graph;
|
|
91
|
+
assert.ok(Math.abs(sum - 1.0) < 0.001, `Weights sum to ${sum}, expected ~1.0`);
|
|
92
|
+
});
|
|
93
|
+
it('returns weights that sum to 1.0 for error-code', () => {
|
|
94
|
+
const { weights } = classifyQuery('ERR_MODULE_NOT_FOUND');
|
|
95
|
+
const sum = weights.vector + weights.bm25 + weights.graph;
|
|
96
|
+
assert.ok(Math.abs(sum - 1.0) < 0.001, `Weights sum to ${sum}, expected ~1.0`);
|
|
97
|
+
});
|
|
98
|
+
it('returns weights that sum to 1.0 for mixed', () => {
|
|
99
|
+
const { weights } = classifyQuery('how does getUserById handle null input');
|
|
100
|
+
const sum = weights.vector + weights.bm25 + weights.graph;
|
|
101
|
+
assert.ok(Math.abs(sum - 1.0) < 0.001, `Weights sum to ${sum}, expected ~1.0`);
|
|
102
|
+
});
|
|
103
|
+
it('identifier has higher bm25 weight than vector', () => {
|
|
104
|
+
const { weights } = classifyQuery('getUserById');
|
|
105
|
+
assert.ok(weights.bm25 > weights.vector, 'bm25 should dominate for identifiers');
|
|
106
|
+
});
|
|
107
|
+
it('natural-language has higher vector weight than bm25', () => {
|
|
108
|
+
const { weights } = classifyQuery('how does authentication work');
|
|
109
|
+
assert.ok(weights.vector > weights.bm25, 'vector should dominate for NL queries');
|
|
110
|
+
});
|
|
111
|
+
it('always returns an explanation string', () => {
|
|
112
|
+
const queries = [
|
|
113
|
+
'getUserById',
|
|
114
|
+
'how does auth work',
|
|
115
|
+
'src/utils/auth.ts',
|
|
116
|
+
'ERR_MODULE_NOT_FOUND',
|
|
117
|
+
'how getUserById works in the pipeline layer',
|
|
118
|
+
'',
|
|
119
|
+
];
|
|
120
|
+
for (const q of queries) {
|
|
121
|
+
const result = classifyQuery(q);
|
|
122
|
+
assert.equal(typeof result.explanation, 'string');
|
|
123
|
+
assert.ok(result.explanation.length > 0, `Empty explanation for query: "${q}"`);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
// ---------------------------------------------------------------------------
|
|
128
|
+
// routeQueryToRepos
|
|
129
|
+
// ---------------------------------------------------------------------------
|
|
130
|
+
describe('routeQueryToRepos', () => {
|
|
131
|
+
const profiles = [
|
|
132
|
+
{ name: 'auth-service', domain: 'authentication', description: 'Handles user login and JWT tokens', technologies: ['typescript', 'express'] },
|
|
133
|
+
{ name: 'billing-api', domain: 'billing', description: 'Stripe integration for subscriptions', technologies: ['python', 'fastapi'] },
|
|
134
|
+
{ name: 'frontend-app', domain: 'ui', description: 'React dashboard for user management', technologies: ['typescript', 'react'] },
|
|
135
|
+
];
|
|
136
|
+
it('returns all repos when profiles is empty', () => {
|
|
137
|
+
const result = routeQueryToRepos('auth', []);
|
|
138
|
+
assert.deepEqual(result, []);
|
|
139
|
+
});
|
|
140
|
+
it('returns all repos when query has no matching tokens', () => {
|
|
141
|
+
const result = routeQueryToRepos('', profiles);
|
|
142
|
+
assert.equal(result.length, 3);
|
|
143
|
+
});
|
|
144
|
+
it('scores auth-service higher for authentication query', () => {
|
|
145
|
+
const result = routeQueryToRepos('authentication login', profiles);
|
|
146
|
+
assert.ok(result.includes('auth-service'), 'auth-service should be in results');
|
|
147
|
+
assert.equal(result[0], 'auth-service', 'auth-service should be first');
|
|
148
|
+
});
|
|
149
|
+
it('scores billing-api higher for billing query', () => {
|
|
150
|
+
const result = routeQueryToRepos('stripe subscription billing', profiles);
|
|
151
|
+
assert.ok(result.includes('billing-api'));
|
|
152
|
+
});
|
|
153
|
+
it('respects maxRepos option', () => {
|
|
154
|
+
const result = routeQueryToRepos('typescript', profiles, { maxRepos: 1 });
|
|
155
|
+
assert.ok(result.length <= 1);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
//# sourceMappingURL=query-classifier.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-classifier.test.js","sourceRoot":"","sources":["../../src/__tests__/query-classifier.test.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAEnF,8EAA8E;AAC9E,gBAAgB;AAChB,8EAA8E;AAE9E,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC7B,EAAE,CAAC,iCAAiC,EAAE,GAAG,EAAE;QACzC,MAAM,MAAM,GAAG,aAAa,CAAC,aAAa,CAAC,CAAC;QAC5C,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;QACxC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,gBAAgB,EAAE,IAAI,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,kCAAkC,EAAE,GAAG,EAAE;QAC1C,MAAM,MAAM,GAAG,aAAa,CAAC,gBAAgB,CAAC,CAAC;QAC/C,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,kCAAkC,EAAE,GAAG,EAAE;QAC1C,MAAM,MAAM,GAAG,aAAa,CAAC,aAAa,CAAC,CAAC;QAC5C,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,sCAAsC,EAAE,GAAG,EAAE;QAC9C,MAAM,MAAM,G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*
|
|
4
|
+
* Pins the retrieval defaults so an accidental edit doesn't silently revert
|
|
5
|
+
* the cost saving:
|
|
6
|
+
* • `reranker` defaults to 'ollama' so HybridRetriever runs the cross-
|
|
7
|
+
* encoder step on every query without explicit opt-in,
|
|
8
|
+
* • `maxChunks` is small (≤10) — the reranker picks the best from the
|
|
9
|
+
* larger fused+AST candidate pool, so emitting a tight top-K is safe.
|
|
10
|
+
*/
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=retriever-defaults.test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retriever-defaults.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/retriever-defaults.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 6 — default-on local reranker + tightened top-K.
|
|
3
|
+
*
|
|
4
|
+
* Pins the retrieval defaults so an accidental edit doesn't silently revert
|
|
5
|
+
* the cost saving:
|
|
6
|
+
* • `reranker` defaults to 'ollama' so HybridRetriever runs the cross-
|
|
7
|
+
* encoder step on every query without explicit opt-in,
|
|
8
|
+
* • `maxChunks` is small (≤10) — the reranker picks the best from the
|
|
9
|
+
* larger fused+AST candidate pool, so emitting a tight top-K is safe.
|
|
10
|
+
*/
|
|
11
|
+
import { describe, it } from 'node:test';
|
|
12
|
+
import assert from 'node:assert/strict';
|
|
13
|
+
import { DEFAULT_CONFIG } from '@esankhan3/anvil-knowledge-core';
|
|
14
|
+
import { createReranker } from '@esankhan3/anvil-knowledge-core';
|
|
15
|
+
describe('knowledge defaults — Phase 6', () => {
|
|
16
|
+
it('uses ollama as the default reranker so rerank is always on', () => {
|
|
17
|
+
assert.equal(DEFAULT_CONFIG.retrieval.reranker, 'ollama');
|
|
18
|
+
});
|
|
19
|
+
it('keeps the default top-K tight (≤10) so rerank can re-narrow the pool', () => {
|
|
20
|
+
assert.ok(DEFAULT_CONFIG.retrieval.maxChunks <= 10, `expected maxChunks ≤ 10 (got ${DEFAULT_CONFIG.retrieval.maxChunks}); ` +
|
|
21
|
+
'Phase 6 relies on a tight top-K to cut chunk-token spend.');
|
|
22
|
+
assert.ok(DEFAULT_CONFIG.retrieval.maxChunks >= 5, `expected maxChunks ≥ 5 (got ${DEFAULT_CONFIG.retrieval.maxChunks}); ` +
|
|
23
|
+
'too small a top-K starves the model of evidence.');
|
|
24
|
+
});
|
|
25
|
+
it('createReranker("ollama") returns a Reranker (not null)', () => {
|
|
26
|
+
const r = createReranker('ollama');
|
|
27
|
+
assert.ok(r, 'expected createReranker("ollama") to return a Reranker instance');
|
|
28
|
+
assert.equal(typeof r.rerank, 'function');
|
|
29
|
+
});
|
|
30
|
+
it('createReranker default branch returns a Reranker (so unknown values still default-on)', () => {
|
|
31
|
+
// Cast through unknown — the factory's `default` branch is the safety net
|
|
32
|
+
// for forward-compat (new provider names land in config before code).
|
|
33
|
+
const r = createReranker('something-unknown');
|
|
34
|
+
assert.ok(r, 'unknown provider should fall through to the default Ollama reranker');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
//# sourceMappingURL=retriever-defaults.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retriever-defaults.test.js","sourceRoot":"","sources":["../../src/__tests__/retriever-defaults.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,MAAM,MAAM,oBAAoB,CAAC;AAExC,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AAEjE,QAAQ,CAAC,8BAA8B,EAAE,GAAG,EAAE;IAC5C,EAAE,CAAC,4DAA4D,EAAE,GAAG,EAAE;QACpE,MAAM,CAAC,KAAK,CAAC,cAAc,CAAC,SAAS,CAAC,QAAQ,EAAE,QAAQ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