@agentmemory/agentmemory 0.7.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/.claude-plugin/marketplace.json +14 -0
- package/.github/workflows/ci.yml +22 -0
- package/.github/workflows/publish.yml +28 -0
- package/AGENTS.md +113 -0
- package/LICENSE +190 -0
- package/README.md +828 -0
- package/assets/banner.png +0 -0
- package/assets/demo.gif +0 -0
- package/assets/demo.mp4 +0 -0
- package/benchmark/QUALITY.md +73 -0
- package/benchmark/REAL-EMBEDDINGS.md +67 -0
- package/benchmark/SCALE.md +110 -0
- package/benchmark/dataset.ts +293 -0
- package/benchmark/quality-eval.ts +643 -0
- package/benchmark/real-embeddings-eval.ts +405 -0
- package/benchmark/scale-eval.ts +398 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +137 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/docker-compose.yml +14 -0
- package/dist/hooks/notification.d.mts +1 -0
- package/dist/hooks/notification.mjs +45 -0
- package/dist/hooks/notification.mjs.map +1 -0
- package/dist/hooks/post-tool-failure.d.mts +1 -0
- package/dist/hooks/post-tool-failure.mjs +45 -0
- package/dist/hooks/post-tool-failure.mjs.map +1 -0
- package/dist/hooks/post-tool-use.d.mts +1 -0
- package/dist/hooks/post-tool-use.mjs +53 -0
- package/dist/hooks/post-tool-use.mjs.map +1 -0
- package/dist/hooks/pre-compact.d.mts +1 -0
- package/dist/hooks/pre-compact.mjs +50 -0
- package/dist/hooks/pre-compact.mjs.map +1 -0
- package/dist/hooks/pre-tool-use.d.mts +1 -0
- package/dist/hooks/pre-tool-use.mjs +69 -0
- package/dist/hooks/pre-tool-use.mjs.map +1 -0
- package/dist/hooks/prompt-submit.d.mts +1 -0
- package/dist/hooks/prompt-submit.mjs +40 -0
- package/dist/hooks/prompt-submit.mjs.map +1 -0
- package/dist/hooks/session-end.d.mts +1 -0
- package/dist/hooks/session-end.mjs +61 -0
- package/dist/hooks/session-end.mjs.map +1 -0
- package/dist/hooks/session-start.d.mts +1 -0
- package/dist/hooks/session-start.mjs +42 -0
- package/dist/hooks/session-start.mjs.map +1 -0
- package/dist/hooks/stop.d.mts +1 -0
- package/dist/hooks/stop.mjs +33 -0
- package/dist/hooks/stop.mjs.map +1 -0
- package/dist/hooks/subagent-start.d.mts +1 -0
- package/dist/hooks/subagent-start.mjs +43 -0
- package/dist/hooks/subagent-start.mjs.map +1 -0
- package/dist/hooks/subagent-stop.d.mts +1 -0
- package/dist/hooks/subagent-stop.mjs +45 -0
- package/dist/hooks/subagent-stop.mjs.map +1 -0
- package/dist/hooks/task-completed.d.mts +1 -0
- package/dist/hooks/task-completed.mjs +46 -0
- package/dist/hooks/task-completed.mjs.map +1 -0
- package/dist/iii-config.yaml +51 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +13776 -0
- package/dist/index.mjs.map +1 -0
- package/dist/src-QxitMPfJ.mjs +13775 -0
- package/dist/src-QxitMPfJ.mjs.map +1 -0
- package/dist/standalone.d.mts +1 -0
- package/dist/standalone.mjs +1155 -0
- package/dist/standalone.mjs.map +1 -0
- package/dist/transformers-BX_tgxdO.mjs +38684 -0
- package/dist/transformers-BX_tgxdO.mjs.map +1 -0
- package/dist/transformers-KMm1i9no.mjs +38683 -0
- package/dist/transformers-KMm1i9no.mjs.map +1 -0
- package/docker-compose.yml +14 -0
- package/iii-config.yaml +51 -0
- package/package.json +59 -0
- package/plugin/.claude-plugin/plugin.json +10 -0
- package/plugin/hooks/hooks.json +77 -0
- package/plugin/scripts/diagnostics.mjs +551 -0
- package/plugin/scripts/notification.mjs +45 -0
- package/plugin/scripts/post-tool-failure.mjs +45 -0
- package/plugin/scripts/post-tool-use.mjs +53 -0
- package/plugin/scripts/pre-compact.mjs +50 -0
- package/plugin/scripts/pre-tool-use.mjs +69 -0
- package/plugin/scripts/prompt-submit.mjs +40 -0
- package/plugin/scripts/session-end.mjs +61 -0
- package/plugin/scripts/session-start.mjs +42 -0
- package/plugin/scripts/stop.mjs +33 -0
- package/plugin/scripts/subagent-start.mjs +43 -0
- package/plugin/scripts/subagent-stop.mjs +45 -0
- package/plugin/scripts/task-completed.mjs +46 -0
- package/plugin/skills/forget/SKILL.md +32 -0
- package/plugin/skills/recall/SKILL.md +18 -0
- package/plugin/skills/remember/SKILL.md +25 -0
- package/plugin/skills/session-history/SKILL.md +17 -0
- package/src/auth.ts +12 -0
- package/src/cli.ts +159 -0
- package/src/config.ts +221 -0
- package/src/eval/metrics-store.ts +65 -0
- package/src/eval/quality.ts +51 -0
- package/src/eval/schemas.ts +124 -0
- package/src/eval/self-correct.ts +28 -0
- package/src/eval/validator.ts +31 -0
- package/src/functions/actions.ts +288 -0
- package/src/functions/audit.ts +61 -0
- package/src/functions/auto-forget.ts +169 -0
- package/src/functions/branch-aware.ts +169 -0
- package/src/functions/cascade.ts +80 -0
- package/src/functions/checkpoints.ts +209 -0
- package/src/functions/claude-bridge.ts +161 -0
- package/src/functions/compress.ts +194 -0
- package/src/functions/consolidate.ts +212 -0
- package/src/functions/consolidation-pipeline.ts +258 -0
- package/src/functions/context.ts +169 -0
- package/src/functions/crystallize.ts +293 -0
- package/src/functions/dedup.ts +57 -0
- package/src/functions/diagnostics.ts +785 -0
- package/src/functions/enrich.ts +132 -0
- package/src/functions/evict.ts +163 -0
- package/src/functions/export-import.ts +508 -0
- package/src/functions/facets.ts +248 -0
- package/src/functions/file-index.ts +106 -0
- package/src/functions/flow-compress.ts +214 -0
- package/src/functions/frontier.ts +196 -0
- package/src/functions/governance.ts +131 -0
- package/src/functions/graph-retrieval.ts +277 -0
- package/src/functions/graph.ts +275 -0
- package/src/functions/leases.ts +216 -0
- package/src/functions/lessons.ts +253 -0
- package/src/functions/mesh.ts +434 -0
- package/src/functions/migrate.ts +165 -0
- package/src/functions/observe.ts +144 -0
- package/src/functions/obsidian-export.ts +310 -0
- package/src/functions/patterns.ts +138 -0
- package/src/functions/privacy.ts +39 -0
- package/src/functions/profile.ts +155 -0
- package/src/functions/query-expansion.ts +186 -0
- package/src/functions/relations.ts +237 -0
- package/src/functions/remember.ts +162 -0
- package/src/functions/retention.ts +235 -0
- package/src/functions/routines.ts +289 -0
- package/src/functions/search.ts +80 -0
- package/src/functions/sentinels.ts +417 -0
- package/src/functions/signals.ts +186 -0
- package/src/functions/sketches.ts +274 -0
- package/src/functions/sliding-window.ts +257 -0
- package/src/functions/smart-search.ts +115 -0
- package/src/functions/snapshot.ts +219 -0
- package/src/functions/summarize.ts +155 -0
- package/src/functions/team.ts +147 -0
- package/src/functions/temporal-graph.ts +476 -0
- package/src/functions/timeline.ts +138 -0
- package/src/functions/verify.ts +117 -0
- package/src/health/monitor.ts +110 -0
- package/src/health/thresholds.ts +73 -0
- package/src/hooks/notification.ts +52 -0
- package/src/hooks/post-tool-failure.ts +58 -0
- package/src/hooks/post-tool-use.ts +62 -0
- package/src/hooks/pre-compact.ts +60 -0
- package/src/hooks/pre-tool-use.ts +72 -0
- package/src/hooks/prompt-submit.ts +46 -0
- package/src/hooks/session-end.ts +71 -0
- package/src/hooks/session-start.ts +48 -0
- package/src/hooks/stop.ts +39 -0
- package/src/hooks/subagent-start.ts +49 -0
- package/src/hooks/subagent-stop.ts +54 -0
- package/src/hooks/task-completed.ts +54 -0
- package/src/index.ts +342 -0
- package/src/mcp/in-memory-kv.ts +61 -0
- package/src/mcp/server.ts +1455 -0
- package/src/mcp/standalone.ts +177 -0
- package/src/mcp/tools-registry.ts +769 -0
- package/src/mcp/transport.ts +91 -0
- package/src/prompts/compression.ts +67 -0
- package/src/prompts/consolidation.ts +48 -0
- package/src/prompts/graph-extraction.ts +35 -0
- package/src/prompts/summary.ts +38 -0
- package/src/prompts/xml.ts +26 -0
- package/src/providers/agent-sdk.ts +34 -0
- package/src/providers/anthropic.ts +35 -0
- package/src/providers/circuit-breaker.ts +82 -0
- package/src/providers/embedding/cohere.ts +46 -0
- package/src/providers/embedding/gemini.ts +54 -0
- package/src/providers/embedding/index.ts +39 -0
- package/src/providers/embedding/local.ts +52 -0
- package/src/providers/embedding/openai.ts +45 -0
- package/src/providers/embedding/openrouter.ts +51 -0
- package/src/providers/embedding/voyage.ts +46 -0
- package/src/providers/fallback-chain.ts +31 -0
- package/src/providers/index.ts +84 -0
- package/src/providers/openrouter.ts +71 -0
- package/src/providers/resilient.ts +37 -0
- package/src/state/hybrid-search.ts +295 -0
- package/src/state/index-persistence.ts +63 -0
- package/src/state/keyed-mutex.ts +18 -0
- package/src/state/kv.ts +33 -0
- package/src/state/schema.ts +71 -0
- package/src/state/search-index.ts +245 -0
- package/src/state/stemmer.ts +104 -0
- package/src/state/synonyms.ts +63 -0
- package/src/state/vector-index.ts +130 -0
- package/src/telemetry/setup.ts +116 -0
- package/src/triggers/api.ts +1904 -0
- package/src/triggers/events.ts +71 -0
- package/src/types.ts +769 -0
- package/src/version.ts +1 -0
- package/src/viewer/index.html +2497 -0
- package/src/viewer/server.ts +207 -0
- package/src/xenova.d.ts +3 -0
- package/test/actions.test.ts +490 -0
- package/test/audit.test.ts +108 -0
- package/test/auto-forget.test.ts +188 -0
- package/test/cascade.test.ts +277 -0
- package/test/checkpoints.test.ts +493 -0
- package/test/circuit-breaker.test.ts +107 -0
- package/test/claude-bridge.test.ts +178 -0
- package/test/confidence.test.ts +247 -0
- package/test/consistency.test.ts +61 -0
- package/test/consolidation-pipeline.test.ts +251 -0
- package/test/crystallize.test.ts +521 -0
- package/test/diagnostics.test.ts +638 -0
- package/test/embedding-provider.test.ts +49 -0
- package/test/enrich.test.ts +209 -0
- package/test/eval.test.ts +300 -0
- package/test/export-import.test.ts +251 -0
- package/test/facets.test.ts +448 -0
- package/test/fallback-chain.test.ts +93 -0
- package/test/frontier.test.ts +485 -0
- package/test/governance.test.ts +147 -0
- package/test/graph-retrieval.test.ts +186 -0
- package/test/graph.test.ts +160 -0
- package/test/helpers/mocks.ts +40 -0
- package/test/hybrid-search.test.ts +145 -0
- package/test/index-persistence.test.ts +124 -0
- package/test/integration.test.ts +265 -0
- package/test/leases.test.ts +399 -0
- package/test/mcp-prompts.test.ts +218 -0
- package/test/mcp-resources.test.ts +286 -0
- package/test/mcp-standalone.test.ts +113 -0
- package/test/mesh.test.ts +700 -0
- package/test/privacy.test.ts +87 -0
- package/test/profile.test.ts +161 -0
- package/test/query-expansion.test.ts +154 -0
- package/test/relations.test.ts +198 -0
- package/test/retention.test.ts +245 -0
- package/test/routines.test.ts +497 -0
- package/test/schema-fingerprint.test.ts +81 -0
- package/test/schema.test.ts +42 -0
- package/test/search-index.test.ts +128 -0
- package/test/sentinels.test.ts +626 -0
- package/test/signals.test.ts +410 -0
- package/test/sketches.test.ts +549 -0
- package/test/sliding-window.test.ts +199 -0
- package/test/smart-search.test.ts +169 -0
- package/test/snapshot.test.ts +165 -0
- package/test/team.test.ts +156 -0
- package/test/temporal-graph.test.ts +378 -0
- package/test/timeline.test.ts +148 -0
- package/test/vector-index.test.ts +79 -0
- package/test/verify.test.ts +209 -0
- package/test/xml.test.ts +65 -0
- package/tsconfig.json +22 -0
- package/tsdown.config.ts +62 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("iii-sdk", () => ({
|
|
4
|
+
getContext: () => ({
|
|
5
|
+
logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn() },
|
|
6
|
+
}),
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
import { registerGovernanceFunction } from "../src/functions/governance.js";
|
|
10
|
+
import type { Memory, AuditEntry } from "../src/types.js";
|
|
11
|
+
|
|
12
|
+
function mockKV() {
|
|
13
|
+
const store = new Map<string, Map<string, unknown>>();
|
|
14
|
+
return {
|
|
15
|
+
get: async <T>(scope: string, key: string): Promise<T | null> => {
|
|
16
|
+
return (store.get(scope)?.get(key) as T) ?? null;
|
|
17
|
+
},
|
|
18
|
+
set: async <T>(scope: string, key: string, data: T): Promise<T> => {
|
|
19
|
+
if (!store.has(scope)) store.set(scope, new Map());
|
|
20
|
+
store.get(scope)!.set(key, data);
|
|
21
|
+
return data;
|
|
22
|
+
},
|
|
23
|
+
delete: async (scope: string, key: string): Promise<void> => {
|
|
24
|
+
store.get(scope)?.delete(key);
|
|
25
|
+
},
|
|
26
|
+
list: async <T>(scope: string): Promise<T[]> => {
|
|
27
|
+
const entries = store.get(scope);
|
|
28
|
+
return entries ? (Array.from(entries.values()) as T[]) : [];
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function mockSdk() {
|
|
34
|
+
const functions = new Map<string, Function>();
|
|
35
|
+
return {
|
|
36
|
+
registerFunction: (opts: { id: string }, handler: Function) => {
|
|
37
|
+
functions.set(opts.id, handler);
|
|
38
|
+
},
|
|
39
|
+
registerTrigger: () => {},
|
|
40
|
+
trigger: async (id: string, data: unknown) => {
|
|
41
|
+
const fn = functions.get(id);
|
|
42
|
+
if (!fn) throw new Error(`No function: ${id}`);
|
|
43
|
+
return fn(data);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function makeMemory(id: string, type: Memory["type"] = "pattern"): Memory {
|
|
49
|
+
return {
|
|
50
|
+
id,
|
|
51
|
+
createdAt: "2026-02-01T00:00:00Z",
|
|
52
|
+
updatedAt: "2026-02-01T00:00:00Z",
|
|
53
|
+
type,
|
|
54
|
+
title: `Memory ${id}`,
|
|
55
|
+
content: `Content for ${id}`,
|
|
56
|
+
concepts: ["test"],
|
|
57
|
+
files: [],
|
|
58
|
+
sessionIds: ["ses_1"],
|
|
59
|
+
strength: 5,
|
|
60
|
+
version: 1,
|
|
61
|
+
isLatest: true,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
describe("Governance Functions", () => {
|
|
66
|
+
let sdk: ReturnType<typeof mockSdk>;
|
|
67
|
+
let kv: ReturnType<typeof mockKV>;
|
|
68
|
+
|
|
69
|
+
beforeEach(async () => {
|
|
70
|
+
sdk = mockSdk();
|
|
71
|
+
kv = mockKV();
|
|
72
|
+
registerGovernanceFunction(sdk as never, kv as never);
|
|
73
|
+
|
|
74
|
+
await kv.set("mem:memories", "mem_1", makeMemory("mem_1", "pattern"));
|
|
75
|
+
await kv.set("mem:memories", "mem_2", makeMemory("mem_2", "bug"));
|
|
76
|
+
await kv.set("mem:memories", "mem_3", makeMemory("mem_3", "pattern"));
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("governance-delete removes specified memories", async () => {
|
|
80
|
+
const result = (await sdk.trigger("mem::governance-delete", {
|
|
81
|
+
memoryIds: ["mem_1"],
|
|
82
|
+
reason: "outdated",
|
|
83
|
+
})) as { success: boolean; deleted: number; total: number };
|
|
84
|
+
|
|
85
|
+
expect(result.success).toBe(true);
|
|
86
|
+
expect(result.deleted).toBe(1);
|
|
87
|
+
expect(result.total).toBe(1);
|
|
88
|
+
|
|
89
|
+
const remaining = await kv.list("mem:memories");
|
|
90
|
+
expect(remaining.length).toBe(2);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("governance-delete handles non-existent IDs gracefully", async () => {
|
|
94
|
+
const result = (await sdk.trigger("mem::governance-delete", {
|
|
95
|
+
memoryIds: ["nonexistent_1", "nonexistent_2"],
|
|
96
|
+
})) as { success: boolean; deleted: number; total: number };
|
|
97
|
+
|
|
98
|
+
expect(result.success).toBe(true);
|
|
99
|
+
expect(result.deleted).toBe(0);
|
|
100
|
+
expect(result.total).toBe(2);
|
|
101
|
+
|
|
102
|
+
const remaining = await kv.list("mem:memories");
|
|
103
|
+
expect(remaining.length).toBe(3);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("governance-bulk deletes by type filter", async () => {
|
|
107
|
+
const result = (await sdk.trigger("mem::governance-bulk", {
|
|
108
|
+
type: ["pattern"],
|
|
109
|
+
})) as { success: boolean; deleted: number };
|
|
110
|
+
|
|
111
|
+
expect(result.success).toBe(true);
|
|
112
|
+
expect(result.deleted).toBe(2);
|
|
113
|
+
|
|
114
|
+
const remaining = await kv.list<Memory>("mem:memories");
|
|
115
|
+
expect(remaining.length).toBe(1);
|
|
116
|
+
expect(remaining[0].type).toBe("bug");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("governance-bulk respects dryRun", async () => {
|
|
120
|
+
const result = (await sdk.trigger("mem::governance-bulk", {
|
|
121
|
+
type: ["pattern"],
|
|
122
|
+
dryRun: true,
|
|
123
|
+
})) as { success: boolean; dryRun: boolean; wouldDelete: number; ids: string[] };
|
|
124
|
+
|
|
125
|
+
expect(result.success).toBe(true);
|
|
126
|
+
expect(result.dryRun).toBe(true);
|
|
127
|
+
expect(result.wouldDelete).toBe(2);
|
|
128
|
+
expect(result.ids).toContain("mem_1");
|
|
129
|
+
expect(result.ids).toContain("mem_3");
|
|
130
|
+
|
|
131
|
+
const remaining = await kv.list("mem:memories");
|
|
132
|
+
expect(remaining.length).toBe(3);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("audit-query returns audit entries", async () => {
|
|
136
|
+
await sdk.trigger("mem::governance-delete", {
|
|
137
|
+
memoryIds: ["mem_1"],
|
|
138
|
+
reason: "cleanup",
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const entries = (await sdk.trigger("mem::audit-query", {})) as AuditEntry[];
|
|
142
|
+
|
|
143
|
+
expect(entries.length).toBe(1);
|
|
144
|
+
expect(entries[0].operation).toBe("delete");
|
|
145
|
+
expect(entries[0].functionId).toBe("mem::governance-delete");
|
|
146
|
+
});
|
|
147
|
+
});
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
2
|
+
import { GraphRetrieval } from "../src/functions/graph-retrieval.js";
|
|
3
|
+
import type { GraphNode, GraphEdge } from "../src/types.js";
|
|
4
|
+
|
|
5
|
+
function mockKV(
|
|
6
|
+
nodes: GraphNode[] = [],
|
|
7
|
+
edges: GraphEdge[] = [],
|
|
8
|
+
) {
|
|
9
|
+
const store = new Map<string, Map<string, unknown>>();
|
|
10
|
+
const nodesMap = new Map<string, unknown>();
|
|
11
|
+
for (const n of nodes) nodesMap.set(n.id, n);
|
|
12
|
+
store.set("mem:graph:nodes", nodesMap);
|
|
13
|
+
|
|
14
|
+
const edgesMap = new Map<string, unknown>();
|
|
15
|
+
for (const e of edges) edgesMap.set(e.id, e);
|
|
16
|
+
store.set("mem:graph:edges", edgesMap);
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
get: async <T>(scope: string, key: string): Promise<T | null> => {
|
|
20
|
+
return (store.get(scope)?.get(key) as T) ?? null;
|
|
21
|
+
},
|
|
22
|
+
set: async <T>(scope: string, key: string, data: T): Promise<T> => {
|
|
23
|
+
if (!store.has(scope)) store.set(scope, new Map());
|
|
24
|
+
store.get(scope)!.set(key, data);
|
|
25
|
+
return data;
|
|
26
|
+
},
|
|
27
|
+
delete: async (scope: string, key: string): Promise<void> => {
|
|
28
|
+
store.get(scope)?.delete(key);
|
|
29
|
+
},
|
|
30
|
+
list: async <T>(scope: string): Promise<T[]> => {
|
|
31
|
+
const entries = store.get(scope);
|
|
32
|
+
return entries ? (Array.from(entries.values()) as T[]) : [];
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function makeNode(
|
|
38
|
+
id: string,
|
|
39
|
+
name: string,
|
|
40
|
+
type: GraphNode["type"] = "concept",
|
|
41
|
+
obsIds: string[] = ["obs_1"],
|
|
42
|
+
): GraphNode {
|
|
43
|
+
return {
|
|
44
|
+
id,
|
|
45
|
+
type,
|
|
46
|
+
name,
|
|
47
|
+
properties: {},
|
|
48
|
+
sourceObservationIds: obsIds,
|
|
49
|
+
createdAt: new Date().toISOString(),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function makeEdge(
|
|
54
|
+
id: string,
|
|
55
|
+
sourceNodeId: string,
|
|
56
|
+
targetNodeId: string,
|
|
57
|
+
type: GraphEdge["type"] = "related_to",
|
|
58
|
+
weight = 0.8,
|
|
59
|
+
): GraphEdge {
|
|
60
|
+
return {
|
|
61
|
+
id,
|
|
62
|
+
type,
|
|
63
|
+
sourceNodeId,
|
|
64
|
+
targetNodeId,
|
|
65
|
+
weight,
|
|
66
|
+
sourceObservationIds: ["obs_1"],
|
|
67
|
+
createdAt: new Date().toISOString(),
|
|
68
|
+
tcommit: new Date().toISOString(),
|
|
69
|
+
isLatest: true,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
describe("GraphRetrieval", () => {
|
|
74
|
+
it("finds entities by name", async () => {
|
|
75
|
+
const nodes = [
|
|
76
|
+
makeNode("n1", "React", "library", ["obs_1"]),
|
|
77
|
+
makeNode("n2", "Vue", "library", ["obs_2"]),
|
|
78
|
+
];
|
|
79
|
+
const kv = mockKV(nodes, []);
|
|
80
|
+
const retrieval = new GraphRetrieval(kv as never);
|
|
81
|
+
|
|
82
|
+
const results = await retrieval.searchByEntities(["React"]);
|
|
83
|
+
expect(results.length).toBeGreaterThan(0);
|
|
84
|
+
expect(results[0].obsId).toBe("obs_1");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("finds entities by partial name match", async () => {
|
|
88
|
+
const nodes = [makeNode("n1", "auth-middleware", "function", ["obs_1"])];
|
|
89
|
+
const kv = mockKV(nodes, []);
|
|
90
|
+
const retrieval = new GraphRetrieval(kv as never);
|
|
91
|
+
|
|
92
|
+
const results = await retrieval.searchByEntities(["auth"]);
|
|
93
|
+
expect(results.length).toBeGreaterThan(0);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("traverses graph edges to find related observations", async () => {
|
|
97
|
+
const nodes = [
|
|
98
|
+
makeNode("n1", "React", "library", ["obs_1"]),
|
|
99
|
+
makeNode("n2", "Component", "concept", ["obs_2"]),
|
|
100
|
+
];
|
|
101
|
+
const edges = [makeEdge("e1", "n1", "n2", "uses")];
|
|
102
|
+
const kv = mockKV(nodes, edges);
|
|
103
|
+
const retrieval = new GraphRetrieval(kv as never);
|
|
104
|
+
|
|
105
|
+
const results = await retrieval.searchByEntities(["React"], 2);
|
|
106
|
+
const obsIds = results.map((r) => r.obsId);
|
|
107
|
+
expect(obsIds).toContain("obs_1");
|
|
108
|
+
expect(obsIds).toContain("obs_2");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("returns empty for no matches", async () => {
|
|
112
|
+
const kv = mockKV([], []);
|
|
113
|
+
const retrieval = new GraphRetrieval(kv as never);
|
|
114
|
+
const results = await retrieval.searchByEntities(["nonexistent"]);
|
|
115
|
+
expect(results).toEqual([]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("expands from existing chunks", async () => {
|
|
119
|
+
const nodes = [
|
|
120
|
+
makeNode("n1", "auth.ts", "file", ["obs_1"]),
|
|
121
|
+
makeNode("n2", "jwt", "concept", ["obs_2"]),
|
|
122
|
+
];
|
|
123
|
+
const edges = [makeEdge("e1", "n1", "n2", "uses")];
|
|
124
|
+
const kv = mockKV(nodes, edges);
|
|
125
|
+
const retrieval = new GraphRetrieval(kv as never);
|
|
126
|
+
|
|
127
|
+
const results = await retrieval.expandFromChunks(["obs_1"]);
|
|
128
|
+
const obsIds = results.map((r) => r.obsId);
|
|
129
|
+
expect(obsIds).toContain("obs_2");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("does not duplicate already-seen observations in expansion", async () => {
|
|
133
|
+
const nodes = [makeNode("n1", "file.ts", "file", ["obs_1", "obs_2"])];
|
|
134
|
+
const kv = mockKV(nodes, []);
|
|
135
|
+
const retrieval = new GraphRetrieval(kv as never);
|
|
136
|
+
|
|
137
|
+
const results = await retrieval.expandFromChunks(["obs_1"]);
|
|
138
|
+
const obsIds = results.map((r) => r.obsId);
|
|
139
|
+
expect(obsIds).not.toContain("obs_1");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("performs temporal query - current state", async () => {
|
|
143
|
+
const nodes = [makeNode("n1", "Alice", "person", ["obs_1"])];
|
|
144
|
+
const edges = [
|
|
145
|
+
makeEdge("e1", "n1", "n1", "located_in" as any, 0.9),
|
|
146
|
+
{
|
|
147
|
+
...makeEdge("e2", "n1", "n1", "located_in" as any, 0.9),
|
|
148
|
+
tvalid: "2024-06-01",
|
|
149
|
+
isLatest: true,
|
|
150
|
+
},
|
|
151
|
+
];
|
|
152
|
+
const kv = mockKV(nodes, edges);
|
|
153
|
+
const retrieval = new GraphRetrieval(kv as never);
|
|
154
|
+
|
|
155
|
+
const result = await retrieval.temporalQuery("Alice");
|
|
156
|
+
expect(result.entity).toBeDefined();
|
|
157
|
+
expect(result.entity!.name).toBe("Alice");
|
|
158
|
+
expect(result.currentState.length).toBeGreaterThan(0);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("returns null entity for unknown name", async () => {
|
|
162
|
+
const kv = mockKV([], []);
|
|
163
|
+
const retrieval = new GraphRetrieval(kv as never);
|
|
164
|
+
const result = await retrieval.temporalQuery("Unknown");
|
|
165
|
+
expect(result.entity).toBeNull();
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("scores closer paths higher", async () => {
|
|
169
|
+
const nodes = [
|
|
170
|
+
makeNode("n1", "React", "library", ["obs_1"]),
|
|
171
|
+
makeNode("n2", "Hook", "concept", ["obs_2"]),
|
|
172
|
+
makeNode("n3", "State", "concept", ["obs_3"]),
|
|
173
|
+
];
|
|
174
|
+
const edges = [
|
|
175
|
+
makeEdge("e1", "n1", "n2", "uses", 0.9),
|
|
176
|
+
makeEdge("e2", "n2", "n3", "related_to", 0.8),
|
|
177
|
+
];
|
|
178
|
+
const kv = mockKV(nodes, edges);
|
|
179
|
+
const retrieval = new GraphRetrieval(kv as never);
|
|
180
|
+
|
|
181
|
+
const results = await retrieval.searchByEntities(["React"], 3);
|
|
182
|
+
const directScore = results.find((r) => r.obsId === "obs_1")?.score ?? 0;
|
|
183
|
+
const indirectScore = results.find((r) => r.obsId === "obs_3")?.score ?? 0;
|
|
184
|
+
expect(directScore).toBeGreaterThan(indirectScore);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("iii-sdk", () => ({
|
|
4
|
+
getContext: () => ({
|
|
5
|
+
logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn() },
|
|
6
|
+
}),
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
import { registerGraphFunction } from "../src/functions/graph.js";
|
|
10
|
+
import type {
|
|
11
|
+
CompressedObservation,
|
|
12
|
+
GraphNode,
|
|
13
|
+
GraphEdge,
|
|
14
|
+
GraphQueryResult,
|
|
15
|
+
} from "../src/types.js";
|
|
16
|
+
|
|
17
|
+
function mockKV() {
|
|
18
|
+
const store = new Map<string, Map<string, unknown>>();
|
|
19
|
+
return {
|
|
20
|
+
get: async <T>(scope: string, key: string): Promise<T | null> => {
|
|
21
|
+
return (store.get(scope)?.get(key) as T) ?? null;
|
|
22
|
+
},
|
|
23
|
+
set: async <T>(scope: string, key: string, data: T): Promise<T> => {
|
|
24
|
+
if (!store.has(scope)) store.set(scope, new Map());
|
|
25
|
+
store.get(scope)!.set(key, data);
|
|
26
|
+
return data;
|
|
27
|
+
},
|
|
28
|
+
delete: async (scope: string, key: string): Promise<void> => {
|
|
29
|
+
store.get(scope)?.delete(key);
|
|
30
|
+
},
|
|
31
|
+
list: async <T>(scope: string): Promise<T[]> => {
|
|
32
|
+
const entries = store.get(scope);
|
|
33
|
+
return entries ? (Array.from(entries.values()) as T[]) : [];
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function mockSdk() {
|
|
39
|
+
const functions = new Map<string, Function>();
|
|
40
|
+
return {
|
|
41
|
+
registerFunction: (opts: { id: string }, handler: Function) => {
|
|
42
|
+
functions.set(opts.id, handler);
|
|
43
|
+
},
|
|
44
|
+
registerTrigger: () => {},
|
|
45
|
+
trigger: async (id: string, data: unknown) => {
|
|
46
|
+
const fn = functions.get(id);
|
|
47
|
+
if (!fn) throw new Error(`No function: ${id}`);
|
|
48
|
+
return fn(data);
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const mockProvider = {
|
|
54
|
+
name: "test",
|
|
55
|
+
compress: vi.fn().mockResolvedValue(`<entities>
|
|
56
|
+
<entity type="file" name="src/index.ts"><property key="path">src/index.ts</property></entity>
|
|
57
|
+
<entity type="function" name="main"><property key="lang">typescript</property></entity>
|
|
58
|
+
</entities>
|
|
59
|
+
<relationships>
|
|
60
|
+
<relationship type="uses" source="src/index.ts" target="main" weight="0.9"/>
|
|
61
|
+
</relationships>`),
|
|
62
|
+
summarize: vi.fn(),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const testObs: CompressedObservation = {
|
|
66
|
+
id: "obs_1",
|
|
67
|
+
sessionId: "ses_1",
|
|
68
|
+
timestamp: "2026-02-01T10:00:00Z",
|
|
69
|
+
type: "file_edit",
|
|
70
|
+
title: "Edit index file",
|
|
71
|
+
facts: ["Modified main function"],
|
|
72
|
+
narrative: "Updated index.ts with main function",
|
|
73
|
+
concepts: ["typescript", "entry-point"],
|
|
74
|
+
files: ["src/index.ts"],
|
|
75
|
+
importance: 7,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
describe("Graph Functions", () => {
|
|
79
|
+
let sdk: ReturnType<typeof mockSdk>;
|
|
80
|
+
let kv: ReturnType<typeof mockKV>;
|
|
81
|
+
|
|
82
|
+
beforeEach(() => {
|
|
83
|
+
sdk = mockSdk();
|
|
84
|
+
kv = mockKV();
|
|
85
|
+
vi.clearAllMocks();
|
|
86
|
+
registerGraphFunction(sdk as never, kv as never, mockProvider as never);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("graph-extract creates nodes and edges from XML response", async () => {
|
|
90
|
+
const result = (await sdk.trigger("mem::graph-extract", {
|
|
91
|
+
observations: [testObs],
|
|
92
|
+
})) as { success: boolean; nodesAdded: number; edgesAdded: number };
|
|
93
|
+
|
|
94
|
+
expect(result.success).toBe(true);
|
|
95
|
+
expect(result.nodesAdded).toBe(2);
|
|
96
|
+
expect(result.edgesAdded).toBe(1);
|
|
97
|
+
|
|
98
|
+
const nodes = await kv.list<GraphNode>("mem:graph:nodes");
|
|
99
|
+
expect(nodes.length).toBe(2);
|
|
100
|
+
expect(nodes.find((n) => n.name === "src/index.ts")).toBeDefined();
|
|
101
|
+
expect(nodes.find((n) => n.name === "main")).toBeDefined();
|
|
102
|
+
|
|
103
|
+
const edges = await kv.list<GraphEdge>("mem:graph:edges");
|
|
104
|
+
expect(edges.length).toBe(1);
|
|
105
|
+
expect(edges[0].type).toBe("uses");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("graph-query with search returns matching nodes", async () => {
|
|
109
|
+
await sdk.trigger("mem::graph-extract", { observations: [testObs] });
|
|
110
|
+
|
|
111
|
+
const result = (await sdk.trigger("mem::graph-query", {
|
|
112
|
+
query: "index",
|
|
113
|
+
})) as GraphQueryResult;
|
|
114
|
+
|
|
115
|
+
expect(result.nodes.length).toBeGreaterThanOrEqual(1);
|
|
116
|
+
expect(result.nodes.some((n) => n.name.includes("index"))).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("graph-query with startNodeId does BFS traversal", async () => {
|
|
120
|
+
await sdk.trigger("mem::graph-extract", { observations: [testObs] });
|
|
121
|
+
|
|
122
|
+
const nodes = await kv.list<GraphNode>("mem:graph:nodes");
|
|
123
|
+
const fileNode = nodes.find((n) => n.name === "src/index.ts")!;
|
|
124
|
+
|
|
125
|
+
const result = (await sdk.trigger("mem::graph-query", {
|
|
126
|
+
startNodeId: fileNode.id,
|
|
127
|
+
maxDepth: 2,
|
|
128
|
+
})) as GraphQueryResult;
|
|
129
|
+
|
|
130
|
+
expect(result.nodes.length).toBeGreaterThanOrEqual(1);
|
|
131
|
+
expect(result.edges.length).toBeGreaterThanOrEqual(1);
|
|
132
|
+
expect(result.depth).toBe(2);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("graph-stats returns counts by type", async () => {
|
|
136
|
+
await sdk.trigger("mem::graph-extract", { observations: [testObs] });
|
|
137
|
+
|
|
138
|
+
const result = (await sdk.trigger("mem::graph-stats", {})) as {
|
|
139
|
+
totalNodes: number;
|
|
140
|
+
totalEdges: number;
|
|
141
|
+
nodesByType: Record<string, number>;
|
|
142
|
+
edgesByType: Record<string, number>;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
expect(result.totalNodes).toBe(2);
|
|
146
|
+
expect(result.totalEdges).toBe(1);
|
|
147
|
+
expect(result.nodesByType.file).toBe(1);
|
|
148
|
+
expect(result.nodesByType.function).toBe(1);
|
|
149
|
+
expect(result.edgesByType.uses).toBe(1);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("graph-extract returns error for empty observations", async () => {
|
|
153
|
+
const result = (await sdk.trigger("mem::graph-extract", {
|
|
154
|
+
observations: [],
|
|
155
|
+
})) as { success: boolean; error: string };
|
|
156
|
+
|
|
157
|
+
expect(result.success).toBe(false);
|
|
158
|
+
expect(result.error).toContain("No observations");
|
|
159
|
+
});
|
|
160
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
type Handler = (data: unknown) => Promise<unknown>;
|
|
4
|
+
|
|
5
|
+
export function mockKV() {
|
|
6
|
+
const store = new Map<string, Map<string, unknown>>();
|
|
7
|
+
return {
|
|
8
|
+
get: async <T>(scope: string, key: string): Promise<T | null> => {
|
|
9
|
+
return (store.get(scope)?.get(key) as T) ?? null;
|
|
10
|
+
},
|
|
11
|
+
set: async <T>(scope: string, key: string, data: T): Promise<T> => {
|
|
12
|
+
if (!store.has(scope)) store.set(scope, new Map());
|
|
13
|
+
store.get(scope)!.set(key, data);
|
|
14
|
+
return data;
|
|
15
|
+
},
|
|
16
|
+
delete: async (scope: string, key: string): Promise<void> => {
|
|
17
|
+
store.get(scope)?.delete(key);
|
|
18
|
+
},
|
|
19
|
+
list: async <T>(scope: string): Promise<T[]> => {
|
|
20
|
+
const entries = store.get(scope);
|
|
21
|
+
return entries ? (Array.from(entries.values()) as T[]) : [];
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function mockSdk() {
|
|
27
|
+
const functions = new Map<string, Handler>();
|
|
28
|
+
return {
|
|
29
|
+
registerFunction: (opts: { id: string }, handler: Handler) => {
|
|
30
|
+
functions.set(opts.id, handler);
|
|
31
|
+
},
|
|
32
|
+
registerTrigger: vi.fn(),
|
|
33
|
+
trigger: async (id: string, data: unknown) => {
|
|
34
|
+
const fn = functions.get(id);
|
|
35
|
+
if (!fn) throw new Error(`No function: ${id}`);
|
|
36
|
+
return fn(data);
|
|
37
|
+
},
|
|
38
|
+
triggerVoid: vi.fn(),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
2
|
+
import { HybridSearch } from "../src/state/hybrid-search.js";
|
|
3
|
+
import { SearchIndex } from "../src/state/search-index.js";
|
|
4
|
+
import type { CompressedObservation, EmbeddingProvider } from "../src/types.js";
|
|
5
|
+
|
|
6
|
+
function makeObs(
|
|
7
|
+
overrides: Partial<CompressedObservation> = {},
|
|
8
|
+
): CompressedObservation {
|
|
9
|
+
return {
|
|
10
|
+
id: "obs_1",
|
|
11
|
+
sessionId: "ses_1",
|
|
12
|
+
timestamp: new Date().toISOString(),
|
|
13
|
+
type: "file_edit",
|
|
14
|
+
title: "Edit auth middleware",
|
|
15
|
+
subtitle: "JWT validation",
|
|
16
|
+
facts: ["Added token check"],
|
|
17
|
+
narrative: "Modified the auth middleware to validate JWT tokens",
|
|
18
|
+
concepts: ["authentication", "jwt"],
|
|
19
|
+
files: ["src/middleware/auth.ts"],
|
|
20
|
+
importance: 7,
|
|
21
|
+
...overrides,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function mockKV() {
|
|
26
|
+
const store = new Map<string, Map<string, unknown>>();
|
|
27
|
+
return {
|
|
28
|
+
get: async <T>(scope: string, key: string): Promise<T | null> => {
|
|
29
|
+
return (store.get(scope)?.get(key) as T) ?? null;
|
|
30
|
+
},
|
|
31
|
+
set: async <T>(scope: string, key: string, data: T): Promise<T> => {
|
|
32
|
+
if (!store.has(scope)) store.set(scope, new Map());
|
|
33
|
+
store.get(scope)!.set(key, data);
|
|
34
|
+
return data;
|
|
35
|
+
},
|
|
36
|
+
delete: async (scope: string, key: string): Promise<void> => {
|
|
37
|
+
store.get(scope)?.delete(key);
|
|
38
|
+
},
|
|
39
|
+
list: async <T>(scope: string): Promise<T[]> => {
|
|
40
|
+
const entries = store.get(scope);
|
|
41
|
+
return entries ? (Array.from(entries.values()) as T[]) : [];
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe("HybridSearch", () => {
|
|
47
|
+
let bm25: SearchIndex;
|
|
48
|
+
let kv: ReturnType<typeof mockKV>;
|
|
49
|
+
|
|
50
|
+
beforeEach(() => {
|
|
51
|
+
bm25 = new SearchIndex();
|
|
52
|
+
kv = mockKV();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("returns BM25-only results when no vector index is provided", async () => {
|
|
56
|
+
const obs = makeObs({ id: "obs_1", sessionId: "ses_1" });
|
|
57
|
+
bm25.add(obs);
|
|
58
|
+
await kv.set("mem:obs:ses_1", "obs_1", obs);
|
|
59
|
+
|
|
60
|
+
const hybrid = new HybridSearch(bm25, null, null, kv as never);
|
|
61
|
+
const results = await hybrid.search("auth");
|
|
62
|
+
|
|
63
|
+
expect(results.length).toBe(1);
|
|
64
|
+
expect(results[0].observation.id).toBe("obs_1");
|
|
65
|
+
expect(results[0].vectorScore).toBe(0);
|
|
66
|
+
expect(results[0].bm25Score).toBeGreaterThan(0);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("returns empty results for no-match query", async () => {
|
|
70
|
+
const obs = makeObs({ id: "obs_1", sessionId: "ses_1" });
|
|
71
|
+
bm25.add(obs);
|
|
72
|
+
await kv.set("mem:obs:ses_1", "obs_1", obs);
|
|
73
|
+
|
|
74
|
+
const hybrid = new HybridSearch(bm25, null, null, kv as never);
|
|
75
|
+
const results = await hybrid.search("database");
|
|
76
|
+
expect(results).toEqual([]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("combinedScore is derived from bm25Score when no vector index", async () => {
|
|
80
|
+
const obs = makeObs({ id: "obs_1", sessionId: "ses_1" });
|
|
81
|
+
bm25.add(obs);
|
|
82
|
+
await kv.set("mem:obs:ses_1", "obs_1", obs);
|
|
83
|
+
|
|
84
|
+
const hybrid = new HybridSearch(bm25, null, null, kv as never);
|
|
85
|
+
const results = await hybrid.search("auth");
|
|
86
|
+
|
|
87
|
+
expect(results[0].combinedScore).toBeGreaterThan(0);
|
|
88
|
+
expect(results[0].vectorScore).toBe(0);
|
|
89
|
+
expect(results[0].graphScore).toBe(0);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("results are sorted by combinedScore descending", async () => {
|
|
93
|
+
const obs1 = makeObs({
|
|
94
|
+
id: "obs_1",
|
|
95
|
+
sessionId: "ses_1",
|
|
96
|
+
title: "auth handler",
|
|
97
|
+
narrative: "auth auth auth module",
|
|
98
|
+
concepts: ["auth"],
|
|
99
|
+
});
|
|
100
|
+
const obs2 = makeObs({
|
|
101
|
+
id: "obs_2",
|
|
102
|
+
sessionId: "ses_1",
|
|
103
|
+
title: "database setup",
|
|
104
|
+
narrative: "auth connection config",
|
|
105
|
+
concepts: ["database"],
|
|
106
|
+
});
|
|
107
|
+
bm25.add(obs1);
|
|
108
|
+
bm25.add(obs2);
|
|
109
|
+
await kv.set("mem:obs:ses_1", "obs_1", obs1);
|
|
110
|
+
await kv.set("mem:obs:ses_1", "obs_2", obs2);
|
|
111
|
+
|
|
112
|
+
const hybrid = new HybridSearch(bm25, null, null, kv as never);
|
|
113
|
+
const results = await hybrid.search("auth");
|
|
114
|
+
|
|
115
|
+
expect(results.length).toBe(2);
|
|
116
|
+
expect(results[0].combinedScore).toBeGreaterThanOrEqual(
|
|
117
|
+
results[1].combinedScore,
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("respects limit parameter", async () => {
|
|
122
|
+
for (let i = 0; i < 10; i++) {
|
|
123
|
+
const obs = makeObs({
|
|
124
|
+
id: `obs_${i}`,
|
|
125
|
+
sessionId: "ses_1",
|
|
126
|
+
title: `auth feature ${i}`,
|
|
127
|
+
});
|
|
128
|
+
bm25.add(obs);
|
|
129
|
+
await kv.set("mem:obs:ses_1", `obs_${i}`, obs);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const hybrid = new HybridSearch(bm25, null, null, kv as never);
|
|
133
|
+
const results = await hybrid.search("auth", 3);
|
|
134
|
+
expect(results.length).toBe(3);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("skips observations not found in KV", async () => {
|
|
138
|
+
const obs = makeObs({ id: "obs_missing", sessionId: "ses_1" });
|
|
139
|
+
bm25.add(obs);
|
|
140
|
+
|
|
141
|
+
const hybrid = new HybridSearch(bm25, null, null, kv as never);
|
|
142
|
+
const results = await hybrid.search("auth");
|
|
143
|
+
expect(results).toEqual([]);
|
|
144
|
+
});
|
|
145
|
+
});
|