agent-knowledge 1.3.27 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/README.md +6 -1
- package/agent-desk-plugin.json +1 -1
- package/dist/knowledge/graph.d.ts +21 -4
- package/dist/knowledge/graph.d.ts.map +1 -1
- package/dist/knowledge/graph.js +71 -15
- package/dist/knowledge/graph.js.map +1 -1
- package/dist/knowledge/search.d.ts +12 -0
- package/dist/knowledge/search.d.ts.map +1 -1
- package/dist/knowledge/search.js +13 -3
- package/dist/knowledge/search.js.map +1 -1
- package/dist/search/boosts.d.ts +63 -0
- package/dist/search/boosts.d.ts.map +1 -0
- package/dist/search/boosts.js +214 -0
- package/dist/search/boosts.js.map +1 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +38 -9
- package/dist/server.js.map +1 -1
- package/dist/sessions/indexer.d.ts +3 -0
- package/dist/sessions/indexer.d.ts.map +1 -1
- package/dist/sessions/indexer.js +9 -1
- package/dist/sessions/indexer.js.map +1 -1
- package/dist/sessions/search.d.ts.map +1 -1
- package/dist/sessions/search.js +9 -1
- package/dist/sessions/search.js.map +1 -1
- package/dist/tool-handlers.d.ts.map +1 -1
- package/dist/tool-handlers.js +34 -7
- package/dist/tool-handlers.js.map +1 -1
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -1
- package/dist/vectorstore/chunker.d.ts +13 -1
- package/dist/vectorstore/chunker.d.ts.map +1 -1
- package/dist/vectorstore/chunker.js +8 -1
- package/dist/vectorstore/chunker.js.map +1 -1
- package/dist/wakeup.d.ts +37 -0
- package/dist/wakeup.d.ts.map +1 -0
- package/dist/wakeup.js +132 -0
- package/dist/wakeup.js.map +1 -0
- package/docs/USER-MANUAL.md +31 -10
- package/package.json +3 -1
- package/scripts/hooks/precompact-flush.mjs +174 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.4.1 (2026-04-08)
|
|
4
|
+
|
|
5
|
+
Documentation cleanup. No code or behavior changes vs 1.4.0.
|
|
6
|
+
|
|
7
|
+
## 1.4.0 (2026-04-08)
|
|
8
|
+
|
|
9
|
+
Seven additions focused on retrieval quality, temporal reasoning, and measurable evaluation.
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- **`knowledge` action `wakeup`.** Returns a token-budgeted "L0 + L1" context blob — identity from `~/agent-knowledge/identity.md` plus the highest-weighted entries — for session-start hydration. Default budget 800 tokens. Weight = `recency × log(size+1)`. Optional `category` param narrows L1 to one category. Folded into the existing `knowledge` tool as a 6th action so the MCP surface stays at six tools.
|
|
14
|
+
- **Temporal validity on `knowledge_graph` edges.** New columns `valid_from` / `valid_to` (nullable, ALTER-TABLE migration is backwards compatible). New `invalidate` action sets `valid_to` without deleting the edge so history is preserved. New `as_of` parameter on `list` and `traverse` returns point-in-time snapshots — `links('projects/auth.md', undefined, '2026-02-15')` returns the assignees who were valid in February. New `isEdgeValidAt` helper.
|
|
15
|
+
- **Hybrid scoring boosts (`src/search/boosts.ts`).** Two boosts wired into `hybridSearch`: a proper-noun boost (capitalized non-stopword tokens in the query, max 40% score uplift) and a Gaussian temporal-proximity boost when the query mentions a date or relative time (`yesterday`, `last week`, `march 2025`, `2024`). Combined cap at +66.7%. `hasAnyBoostSignal` short-circuits when no signal is present so default behavior is unchanged.
|
|
16
|
+
- **`category_mode` on `knowledge_search`.** New option `'filter' | 'boost'` (default `'filter'` — backwards compatible). In `'boost'` mode, `category` becomes a 1.25× score multiplier instead of a hard WHERE filter. Use `'boost'` when you might be wrong about the category — hard filters silently discard the right answer when the metadata doesn't match.
|
|
17
|
+
- **`indexVerbatim` config flag.** Toggle verbatim per-message session indexing via `KNOWLEDGE_INDEX_VERBATIM` env var or persisted config. Default `true`. Indexer now drops messages shorter than 30 chars (low-signal acks).
|
|
18
|
+
- **PreCompact / Stop hook.** New `scripts/hooks/precompact-flush.mjs` — reads hook JSON from stdin, dynamic-imports `dist/sessions/summary.js`, writes a flushed session summary to `~/agent-knowledge/sessions/<project>/<event>-<stamp>-<sid>.md` so distillation has an anchor when the host compacts the conversation. Fail-open. Wire-up snippet for Claude Code `settings.json` is in the file header. Not auto-installed.
|
|
19
|
+
- **Benchmark suite (`bench/`).** Two runners: a small smoke benchmark over `~/agent-knowledge/` (22 hand-authored fixtures) and a full LongMemEval runner (`bench/longmemeval.ts`) against the public Wu et al. 2024 dataset. The LongMemEval runner supports `--raw`, `--boosts`, `--semantic`, and `--hybrid` modes and prints per-question-type breakdowns. `npm run bench` (uses `tsx`).
|
|
20
|
+
|
|
21
|
+
### Tests
|
|
22
|
+
|
|
23
|
+
- New e2e suite `tests/e2e/v14-features.e2e.test.ts` (10 tests): wakeup default identity, wakeup with `identity.md`, token-budget truncation, scope filter, `categoryMode` filter vs boost behavior, full KG temporal lifecycle (link → list as_of → invalidate → list as_of), `graph()` traversal with `as_of`, weight-based L1 ordering.
|
|
24
|
+
- New unit suite `tests/boosts.test.ts` (14 tests): proper-noun extraction (stopword filtering, dedup), boost ramp, temporal reference parsing, Gaussian decay, combined-boost cap.
|
|
25
|
+
- Extended `tests/graph.test.ts` with 9 temporal cases.
|
|
26
|
+
- **Total: 390 tests passing**, up from 352.
|
|
27
|
+
|
|
28
|
+
### Internal
|
|
29
|
+
|
|
30
|
+
- `chunkSession` accepts an options object alongside the legacy `maxChars` number for backwards compatibility.
|
|
31
|
+
- Version bumped across `package.json`, `server.json`, `agent-desk-plugin.json`.
|
|
32
|
+
|
|
3
33
|
## 1.3.27 (2026-04-08)
|
|
4
34
|
|
|
5
35
|
### Added
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](LICENSE)
|
|
4
4
|
[](https://nodejs.org)
|
|
5
|
-
[]()
|
|
6
6
|
[]()
|
|
7
7
|
|
|
8
8
|
**Cross-session memory and recall for AI coding assistants** -- works with Claude Code, Cursor, OpenCode, Cline, Continue.dev, and Aider out of the box. Git-synced knowledge base, hybrid semantic+TF-IDF search, auto-distillation with secrets scrubbing.
|
|
@@ -53,6 +53,11 @@ No configuration needed. Additional session roots can be added via the `EXTRA_SE
|
|
|
53
53
|
- **Fuzzy matching** -- typo-tolerant search using Levenshtein distance
|
|
54
54
|
- **6 search scopes** -- errors, plans, configs, tools, files, decisions
|
|
55
55
|
- **6 MCP tools** -- consolidated action-based interface (knowledge, knowledge_search, knowledge_session, knowledge_graph, knowledge_analyze, knowledge_admin)
|
|
56
|
+
- **Wakeup context (L0+L1)** -- `knowledge` action `wakeup` returns a token-budgeted identity + top-weight entries blob for session-start hydration
|
|
57
|
+
- **Temporal knowledge graph** -- edges support `valid_from` / `valid_to` validity windows; `as_of` queries return point-in-time snapshots; `invalidate` action marks facts as ended without deleting them
|
|
58
|
+
- **Hybrid scoring boosts** -- proper-noun and temporal-proximity boosts on top of TF-IDF + semantic blend, capped at +66.7%, short-circuit when no signals are present
|
|
59
|
+
- **Category as boost (not filter)** -- opt into `category_mode: "boost"` so a wrong category guess down-ranks instead of discarding the right answer
|
|
60
|
+
- **Verbatim session indexing** -- per-message chunks (≥30 chars) embedded into the vector store so raw conversation is retrievable; toggle with `KNOWLEDGE_INDEX_VERBATIM=false`
|
|
56
61
|
- **Configurable git URL** -- `knowledge_admin(action: "config")` for runtime setup, persisted at XDG/AppData location
|
|
57
62
|
- **Cross-machine persistence** -- knowledge syncs via git, sessions read from local storage of each tool
|
|
58
63
|
- **Real-time dashboard** -- browse, search, and manage at `localhost:3423`
|
package/agent-desk-plugin.json
CHANGED
|
@@ -12,7 +12,13 @@ export interface Edge {
|
|
|
12
12
|
rel_type: RelationshipType;
|
|
13
13
|
strength: number;
|
|
14
14
|
created_at: string;
|
|
15
|
+
/** ISO date (YYYY-MM-DD or full ISO timestamp) when this fact became true. Null = always valid. */
|
|
16
|
+
valid_from: string | null;
|
|
17
|
+
/** ISO date when this fact stopped being true. Null = still valid. */
|
|
18
|
+
valid_to: string | null;
|
|
15
19
|
}
|
|
20
|
+
/** Returns true if the edge is valid at the given ISO date (or now). */
|
|
21
|
+
export declare function isEdgeValidAt(edge: Edge, asOf?: string): boolean;
|
|
16
22
|
export interface GraphNode {
|
|
17
23
|
path: string;
|
|
18
24
|
depth: number;
|
|
@@ -33,8 +39,17 @@ export declare class KnowledgeGraph {
|
|
|
33
39
|
private init;
|
|
34
40
|
/**
|
|
35
41
|
* Create or update an edge between two entries.
|
|
42
|
+
*
|
|
43
|
+
* Optional `validFrom`/`validTo` mark this fact's temporal validity window.
|
|
44
|
+
* Both are ISO date strings (YYYY-MM-DD or full ISO timestamp); null = unbounded.
|
|
36
45
|
*/
|
|
37
|
-
link(source: string, target: string, relType: RelationshipType, strength?: number): Edge;
|
|
46
|
+
link(source: string, target: string, relType: RelationshipType, strength?: number, validFrom?: string | null, validTo?: string | null): Edge;
|
|
47
|
+
/**
|
|
48
|
+
* Mark an existing edge as no longer valid by setting `valid_to`.
|
|
49
|
+
* If relType is omitted, invalidates all matching edges between source and target.
|
|
50
|
+
* Returns number of edges updated.
|
|
51
|
+
*/
|
|
52
|
+
invalidate(source: string, target: string, relType: RelationshipType | undefined, validTo: string): number;
|
|
38
53
|
/**
|
|
39
54
|
* Remove edge(s) between two entries.
|
|
40
55
|
* If relType is omitted, removes all edges between source and target.
|
|
@@ -42,13 +57,15 @@ export declare class KnowledgeGraph {
|
|
|
42
57
|
unlink(source: string, target: string, relType?: RelationshipType): number;
|
|
43
58
|
/**
|
|
44
59
|
* List edges, optionally filtered by entry path and/or relationship type.
|
|
60
|
+
* If `asOf` is set, only edges valid at that date are returned.
|
|
45
61
|
*/
|
|
46
|
-
links(entry?: string, relType?: RelationshipType): Edge[];
|
|
62
|
+
links(entry?: string, relType?: RelationshipType, asOf?: string): Edge[];
|
|
47
63
|
/**
|
|
48
64
|
* BFS traversal from a starting entry, returning all nodes and edges
|
|
49
|
-
* within `depth` hops.
|
|
65
|
+
* within `depth` hops. If `asOf` is set, only edges valid at that date
|
|
66
|
+
* are followed.
|
|
50
67
|
*/
|
|
51
|
-
graph(entry: string, depth?: number): GraphResult;
|
|
68
|
+
graph(entry: string, depth?: number, asOf?: string): GraphResult;
|
|
52
69
|
/**
|
|
53
70
|
* Get 1-hop connected entries for a given entry path.
|
|
54
71
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../../src/knowledge/graph.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAYH,eAAO,MAAM,kBAAkB,6HASrB,CAAC;AAEX,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEnE,MAAM,WAAW,IAAI;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,gBAAgB,CAAC;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../../src/knowledge/graph.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAYH,eAAO,MAAM,kBAAkB,6HASrB,CAAC;AAEX,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEnE,MAAM,WAAW,IAAI;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,gBAAgB,CAAC;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,mGAAmG;IACnG,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,sEAAsE;IACtE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAED,wEAAwE;AACxE,wBA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MAAM,GAAG,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IAetF,qCAAqC;IACrC,KAAK,IAAI,IAAI;CAUd;AAMD,wBAAgB,iBAAiB,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,cAAc,CAKjE"}
|
package/dist/knowledge/graph.js
CHANGED
|
@@ -19,6 +19,16 @@ export const RELATIONSHIP_TYPES = [
|
|
|
19
19
|
'alternative_to',
|
|
20
20
|
'builds_on',
|
|
21
21
|
];
|
|
22
|
+
/** Returns true if the edge is valid at the given ISO date (or now). */
|
|
23
|
+
export function isEdgeValidAt(edge, asOf) {
|
|
24
|
+
if (!asOf)
|
|
25
|
+
return edge.valid_to === null;
|
|
26
|
+
if (edge.valid_from && edge.valid_from > asOf)
|
|
27
|
+
return false;
|
|
28
|
+
if (edge.valid_to && edge.valid_to < asOf)
|
|
29
|
+
return false;
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
22
32
|
/**
|
|
23
33
|
* Knowledge graph backed by SQLite.
|
|
24
34
|
* Lazily initializes the database on first access.
|
|
@@ -49,11 +59,22 @@ export class KnowledgeGraph {
|
|
|
49
59
|
rel_type TEXT NOT NULL,
|
|
50
60
|
strength REAL DEFAULT 0.5,
|
|
51
61
|
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
|
|
62
|
+
valid_from TEXT,
|
|
63
|
+
valid_to TEXT,
|
|
52
64
|
PRIMARY KEY (source, target, rel_type)
|
|
53
65
|
);
|
|
54
66
|
CREATE INDEX IF NOT EXISTS idx_edges_source ON edges(source);
|
|
55
67
|
CREATE INDEX IF NOT EXISTS idx_edges_target ON edges(target);
|
|
56
68
|
`);
|
|
69
|
+
// Backwards-compat migration: existing DBs may not have valid_from/valid_to
|
|
70
|
+
for (const col of ['valid_from', 'valid_to']) {
|
|
71
|
+
try {
|
|
72
|
+
this.db.exec(`ALTER TABLE edges ADD COLUMN ${col} TEXT`);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// column already exists
|
|
76
|
+
}
|
|
77
|
+
}
|
|
57
78
|
this.initialized = true;
|
|
58
79
|
}
|
|
59
80
|
catch (err) {
|
|
@@ -63,8 +84,11 @@ export class KnowledgeGraph {
|
|
|
63
84
|
}
|
|
64
85
|
/**
|
|
65
86
|
* Create or update an edge between two entries.
|
|
87
|
+
*
|
|
88
|
+
* Optional `validFrom`/`validTo` mark this fact's temporal validity window.
|
|
89
|
+
* Both are ISO date strings (YYYY-MM-DD or full ISO timestamp); null = unbounded.
|
|
66
90
|
*/
|
|
67
|
-
link(source, target, relType, strength = 0.5) {
|
|
91
|
+
link(source, target, relType, strength = 0.5, validFrom = null, validTo = null) {
|
|
68
92
|
this.init();
|
|
69
93
|
if (!this.db)
|
|
70
94
|
throw new Error('Graph database not available');
|
|
@@ -78,16 +102,38 @@ export class KnowledgeGraph {
|
|
|
78
102
|
throw new Error('Cannot create self-referencing edge');
|
|
79
103
|
}
|
|
80
104
|
this.db
|
|
81
|
-
.prepare(`INSERT INTO edges (source, target, rel_type, strength)
|
|
82
|
-
VALUES (?, ?, ?, ?)
|
|
105
|
+
.prepare(`INSERT INTO edges (source, target, rel_type, strength, valid_from, valid_to)
|
|
106
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
83
107
|
ON CONFLICT(source, target, rel_type)
|
|
84
|
-
DO UPDATE SET strength = excluded.strength
|
|
85
|
-
|
|
108
|
+
DO UPDATE SET strength = excluded.strength,
|
|
109
|
+
valid_from = excluded.valid_from,
|
|
110
|
+
valid_to = excluded.valid_to`)
|
|
111
|
+
.run(source, target, relType, strength, validFrom, validTo);
|
|
86
112
|
const row = this.db
|
|
87
113
|
.prepare('SELECT * FROM edges WHERE source = ? AND target = ? AND rel_type = ?')
|
|
88
114
|
.get(source, target, relType);
|
|
89
115
|
return row;
|
|
90
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Mark an existing edge as no longer valid by setting `valid_to`.
|
|
119
|
+
* If relType is omitted, invalidates all matching edges between source and target.
|
|
120
|
+
* Returns number of edges updated.
|
|
121
|
+
*/
|
|
122
|
+
invalidate(source, target, relType, validTo) {
|
|
123
|
+
this.init();
|
|
124
|
+
if (!this.db)
|
|
125
|
+
throw new Error('Graph database not available');
|
|
126
|
+
if (relType) {
|
|
127
|
+
const result = this.db
|
|
128
|
+
.prepare('UPDATE edges SET valid_to = ? WHERE source = ? AND target = ? AND rel_type = ?')
|
|
129
|
+
.run(validTo, source, target, relType);
|
|
130
|
+
return result.changes;
|
|
131
|
+
}
|
|
132
|
+
const result = this.db
|
|
133
|
+
.prepare('UPDATE edges SET valid_to = ? WHERE source = ? AND target = ?')
|
|
134
|
+
.run(validTo, source, target);
|
|
135
|
+
return result.changes;
|
|
136
|
+
}
|
|
91
137
|
/**
|
|
92
138
|
* Remove edge(s) between two entries.
|
|
93
139
|
* If relType is omitted, removes all edges between source and target.
|
|
@@ -109,37 +155,46 @@ export class KnowledgeGraph {
|
|
|
109
155
|
}
|
|
110
156
|
/**
|
|
111
157
|
* List edges, optionally filtered by entry path and/or relationship type.
|
|
158
|
+
* If `asOf` is set, only edges valid at that date are returned.
|
|
112
159
|
*/
|
|
113
|
-
links(entry, relType) {
|
|
160
|
+
links(entry, relType, asOf) {
|
|
114
161
|
this.init();
|
|
115
162
|
if (!this.db)
|
|
116
163
|
return [];
|
|
164
|
+
let rows;
|
|
117
165
|
if (entry && relType) {
|
|
118
|
-
|
|
166
|
+
rows = this.db
|
|
119
167
|
.prepare(`SELECT * FROM edges
|
|
120
168
|
WHERE (source = ? OR target = ?) AND rel_type = ?
|
|
121
169
|
ORDER BY created_at DESC`)
|
|
122
170
|
.all(entry, entry, relType);
|
|
123
171
|
}
|
|
124
|
-
if (entry) {
|
|
125
|
-
|
|
172
|
+
else if (entry) {
|
|
173
|
+
rows = this.db
|
|
126
174
|
.prepare(`SELECT * FROM edges
|
|
127
175
|
WHERE source = ? OR target = ?
|
|
128
176
|
ORDER BY created_at DESC`)
|
|
129
177
|
.all(entry, entry);
|
|
130
178
|
}
|
|
131
|
-
if (relType) {
|
|
132
|
-
|
|
179
|
+
else if (relType) {
|
|
180
|
+
rows = this.db
|
|
133
181
|
.prepare('SELECT * FROM edges WHERE rel_type = ? ORDER BY created_at DESC')
|
|
134
182
|
.all(relType);
|
|
135
183
|
}
|
|
136
|
-
|
|
184
|
+
else {
|
|
185
|
+
rows = this.db.prepare('SELECT * FROM edges ORDER BY created_at DESC').all();
|
|
186
|
+
}
|
|
187
|
+
if (asOf !== undefined) {
|
|
188
|
+
return rows.filter((e) => isEdgeValidAt(e, asOf));
|
|
189
|
+
}
|
|
190
|
+
return rows;
|
|
137
191
|
}
|
|
138
192
|
/**
|
|
139
193
|
* BFS traversal from a starting entry, returning all nodes and edges
|
|
140
|
-
* within `depth` hops.
|
|
194
|
+
* within `depth` hops. If `asOf` is set, only edges valid at that date
|
|
195
|
+
* are followed.
|
|
141
196
|
*/
|
|
142
|
-
graph(entry, depth = 2) {
|
|
197
|
+
graph(entry, depth = 2, asOf) {
|
|
143
198
|
this.init();
|
|
144
199
|
if (!this.db)
|
|
145
200
|
return { nodes: [], edges: [] };
|
|
@@ -151,9 +206,10 @@ export class KnowledgeGraph {
|
|
|
151
206
|
const item = queue.shift();
|
|
152
207
|
if (item.currentDepth >= depth)
|
|
153
208
|
continue;
|
|
154
|
-
const
|
|
209
|
+
const allEdges = this.db
|
|
155
210
|
.prepare('SELECT * FROM edges WHERE source = ? OR target = ?')
|
|
156
211
|
.all(item.path, item.path);
|
|
212
|
+
const edges = asOf !== undefined ? allEdges.filter((e) => isEdgeValidAt(e, asOf)) : allEdges;
|
|
157
213
|
for (const edge of edges) {
|
|
158
214
|
const isDuplicate = resultEdges.some((e) => e.source === edge.source && e.target === edge.target && e.rel_type === edge.rel_type);
|
|
159
215
|
if (!isDuplicate) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph.js","sourceRoot":"","sources":["../../src/knowledge/graph.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAIxC,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAG/C,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,YAAY;IACZ,YAAY;IACZ,YAAY;IACZ,aAAa;IACb,aAAa;IACb,SAAS;IACT,gBAAgB;IAChB,WAAW;CACH,CAAC;
|
|
1
|
+
{"version":3,"file":"graph.js","sourceRoot":"","sources":["../../src/knowledge/graph.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAC/B,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAIxC,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAG/C,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,YAAY;IACZ,YAAY;IACZ,YAAY;IACZ,aAAa;IACb,aAAa;IACb,SAAS;IACT,gBAAgB;IAChB,WAAW;CACH,CAAC;AAgBX,wEAAwE;AACxE,MAAM,UAAU,aAAa,CAAC,IAAU,EAAE,IAAa;IACrD,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC;IACzC,IAAI,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,UAAU,GAAG,IAAI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AAK,EAAE,CAAC;QACnB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,CAAC,KAAK,CAAC,yCAAyC,GAAG,EAAE,CAAC,CAAC;QAChE,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC;YACf,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;QAC3B,CAAC;IACH,CAAC;CACF;AAED,gFAAgF;AAEhF,IAAI,cAAc,GAA0B,IAAI,CAAC;AAEjD,MAAM,UAAU,iBAAiB,CAAC,MAAe;IAC/C,IAAI,CAAC,cAAc,EAAE,CAAC;QACpB,cAAc,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC;IAC9C,CAAC;IACD,OAAO,cAAc,CAAC;AACxB,CAAC"}
|
|
@@ -3,7 +3,19 @@ export interface SearchOptions {
|
|
|
3
3
|
category?: string;
|
|
4
4
|
maxResults?: number;
|
|
5
5
|
caseSensitive?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* How to use `category`:
|
|
8
|
+
* - 'filter' (default, current behavior): only entries in `category` are indexed.
|
|
9
|
+
* - 'boost': all entries are indexed; matching-category entries get a score multiplier.
|
|
10
|
+
*
|
|
11
|
+
* Hard filters silently discard the right evidence when the metadata
|
|
12
|
+
* doesn't match the answer's location. The boost path keeps the candidate
|
|
13
|
+
* in the pool while still favoring the matching category.
|
|
14
|
+
*/
|
|
15
|
+
categoryMode?: 'filter' | 'boost';
|
|
6
16
|
}
|
|
17
|
+
/** Score multiplier applied to entries whose category matches in 'boost' mode. */
|
|
18
|
+
export declare const CATEGORY_BOOST_MULTIPLIER = 1.25;
|
|
7
19
|
export interface SearchResult {
|
|
8
20
|
entry: KnowledgeEntry;
|
|
9
21
|
score: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../src/knowledge/search.ts"],"names":[],"mappings":"AAGA,OAAO,EAA0B,cAAc,EAAE,MAAM,YAAY,CAAC;AAEpE,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../src/knowledge/search.ts"],"names":[],"mappings":"AAGA,OAAO,EAA0B,cAAc,EAAE,MAAM,YAAY,CAAC;AAEpE,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB;;;;;;;;OAQG;IACH,YAAY,CAAC,EAAE,QAAQ,GAAG,OAAO,CAAC;CACnC;AAED,kFAAkF;AAClF,eAAO,MAAM,yBAAyB,OAAO,CAAC;AAE9C,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,cAAc,CAAC;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;CACjB;AAeD,0EAA0E;AAC1E,wBAAgB,6BAA6B,IAAI,IAAI,CAEpD;AAsCD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,aAAkB,GAC1B,KAAK,CAAC,YAAY,CAAC,CA2ErB"}
|
package/dist/knowledge/search.js
CHANGED
|
@@ -2,6 +2,8 @@ import { TfIdfIndex } from '../search/tfidf.js';
|
|
|
2
2
|
import { getEntryScoring, computeFinalScore } from './scoring.js';
|
|
3
3
|
import { buildExcerpt } from '../search/excerpt.js';
|
|
4
4
|
import { listEntries, readEntry } from './store.js';
|
|
5
|
+
/** Score multiplier applied to entries whose category matches in 'boost' mode. */
|
|
6
|
+
export const CATEGORY_BOOST_MULTIPLIER = 1.25;
|
|
5
7
|
let _knowledgeIndexCache = null;
|
|
6
8
|
const KNOWLEDGE_INDEX_TTL = 60_000; // 60 seconds, matching session search cache
|
|
7
9
|
/** Invalidate the knowledge TF-IDF index cache (call on write/delete). */
|
|
@@ -42,8 +44,11 @@ function getOrBuildKnowledgeIndex(dir, category) {
|
|
|
42
44
|
* if TF-IDF returns no results (useful for exact phrase matches).
|
|
43
45
|
*/
|
|
44
46
|
export function searchKnowledge(dir, query, options = {}) {
|
|
45
|
-
const { category, maxResults = 10, caseSensitive = false } = options;
|
|
46
|
-
|
|
47
|
+
const { category, maxResults = 10, caseSensitive = false, categoryMode = 'filter' } = options;
|
|
48
|
+
// In 'boost' mode, build the index over the full corpus (no category restriction).
|
|
49
|
+
// In 'filter' mode (default), keep current behavior of restricting the index to one category.
|
|
50
|
+
const indexCategory = categoryMode === 'boost' ? undefined : category;
|
|
51
|
+
const { index, documents } = getOrBuildKnowledgeIndex(dir, indexCategory);
|
|
47
52
|
if (documents.length === 0)
|
|
48
53
|
return [];
|
|
49
54
|
// Search using TF-IDF
|
|
@@ -62,7 +67,12 @@ export function searchKnowledge(dir, query, options = {}) {
|
|
|
62
67
|
const scoreInfo = scores.get(doc.entry.path);
|
|
63
68
|
const maturity = (scoreInfo?.maturity ?? 'candidate');
|
|
64
69
|
const lastAccessed = scoreInfo?.last_accessed ?? null;
|
|
65
|
-
|
|
70
|
+
let finalScore = computeFinalScore(result.score, lastAccessed, maturity);
|
|
71
|
+
// Category boost: in 'boost' mode, give matching-category entries an
|
|
72
|
+
// edge instead of dropping non-matching entries.
|
|
73
|
+
if (categoryMode === 'boost' && category && doc.entry.category === category) {
|
|
74
|
+
finalScore *= CATEGORY_BOOST_MULTIPLIER;
|
|
75
|
+
}
|
|
66
76
|
results.push({
|
|
67
77
|
entry: doc.entry,
|
|
68
78
|
score: finalScore,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search.js","sourceRoot":"","sources":["../../src/knowledge/search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EA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AC,OAAO,EAAE,KAAK,EAAE,EAAE,aAAa,EAAE,YAAY,EAAE,GAAG,EAAE,CAAC;aAChF,CAAC,CAAC;YACH,IAAI,YAAY,CAAC,MAAM,IAAI,UAAU;gBAAE,MAAM;QAC/C,CAAC;IACH,CAAC;IAED,OAAO,YAAY,CAAC;AACtB,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Search-time scoring boosts.
|
|
3
|
+
*
|
|
4
|
+
* Two boosts applied on top of the TF-IDF + semantic blend:
|
|
5
|
+
*
|
|
6
|
+
* 1. **Proper-noun boost** — capitalized non-stopword tokens in the query
|
|
7
|
+
* are matched against document text. Embeddings under-weight proper
|
|
8
|
+
* nouns; this rescues them.
|
|
9
|
+
* 2. **Temporal proximity boost** — when the query mentions a date or
|
|
10
|
+
* relative time, documents whose timestamp is near that date get a
|
|
11
|
+
* Gaussian boost.
|
|
12
|
+
*
|
|
13
|
+
* Boosts are additive and capped so the maximum combined boost cannot
|
|
14
|
+
* exceed `MAX_TOTAL_BOOST` (≈ 40% distance reduction in distance space).
|
|
15
|
+
* Queries without proper nouns or date tokens short-circuit via
|
|
16
|
+
* `hasAnyBoostSignal` so default behavior is preserved.
|
|
17
|
+
*/
|
|
18
|
+
/** Maximum boost any single signal can contribute (in score-multiplier space). */
|
|
19
|
+
export declare const MAX_PROPER_NOUN_BOOST = 0.4;
|
|
20
|
+
export declare const MAX_TEMPORAL_BOOST = 0.4;
|
|
21
|
+
/** Cap on the combined boost so a doc cannot more than ~1.67x its score. */
|
|
22
|
+
export declare const MAX_TOTAL_BOOST = 0.667;
|
|
23
|
+
/** Extract capitalized non-stopword tokens from a query (proper nouns). */
|
|
24
|
+
export declare function extractProperNouns(query: string): string[];
|
|
25
|
+
/**
|
|
26
|
+
* Compute a proper-noun boost for a document.
|
|
27
|
+
* Returns 0 when no nouns match, scales linearly up to MAX_PROPER_NOUN_BOOST
|
|
28
|
+
* when 2+ nouns match.
|
|
29
|
+
*/
|
|
30
|
+
export declare function properNounBoost(nouns: string[], docText: string): number;
|
|
31
|
+
/**
|
|
32
|
+
* Try to parse a temporal reference out of a query.
|
|
33
|
+
* Returns the anchor date in epoch milliseconds, or null if no reference found.
|
|
34
|
+
*
|
|
35
|
+
* Recognizes:
|
|
36
|
+
* - explicit years: "2024", "2025"
|
|
37
|
+
* - month names: "march", "april 2025"
|
|
38
|
+
* - relative: "today", "yesterday", "tomorrow", "last week/month/year"
|
|
39
|
+
*/
|
|
40
|
+
export declare function parseTemporalReference(query: string, now?: Date): number | null;
|
|
41
|
+
/**
|
|
42
|
+
* Gaussian proximity factor.
|
|
43
|
+
* Returns 1 at the anchor, ~0.6 at one sigma away, ~0.13 at two sigma.
|
|
44
|
+
* `sigmaDays` defaults to 30 days — half a sprint.
|
|
45
|
+
*/
|
|
46
|
+
export declare function temporalProximityBoost(anchorMs: number, docMs: number | null | undefined, sigmaDays?: number): number;
|
|
47
|
+
export interface BoostContext {
|
|
48
|
+
properNouns: string[];
|
|
49
|
+
temporalAnchorMs: number | null;
|
|
50
|
+
}
|
|
51
|
+
export declare function buildBoostContext(query: string, now?: Date): BoostContext;
|
|
52
|
+
export declare function hasAnyBoostSignal(ctx: BoostContext): boolean;
|
|
53
|
+
export interface ApplyBoostInput {
|
|
54
|
+
docText?: string;
|
|
55
|
+
docTimestamp?: string | null;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Apply boosts to a base score.
|
|
59
|
+
* `score` is multiplied by `(1 + min(pnBoost + tpBoost, MAX_TOTAL_BOOST))`.
|
|
60
|
+
* Returns the boosted score (≥ original score).
|
|
61
|
+
*/
|
|
62
|
+
export declare function applyBoosts(score: number, ctx: BoostContext, input: ApplyBoostInput): number;
|
|
63
|
+
//# sourceMappingURL=boosts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"boosts.d.ts","sourceRoot":"","sources":["../../src/search/boosts.ts"],"names":[],"mappings":"AAAA;;;;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