Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

fix(graph): keep each observation's best graph score (#1560)

Dmitrii Zhukov6a182c410ed3
diff --git a/src/functions/graph-retrieval.ts b/src/functions/graph-retrieval.ts
index 115f523b..05f07045 100644
--- a/src/functions/graph-retrieval.ts
+++ b/src/functions/graph-retrieval.ts
@@ -162,15 +162,17 @@ export class GraphQuery {
}
private async append(
- reader: GraphIndexReader, node: GraphNode, results: GraphRetrievalResult[], seen: Set<string>,
+ reader: GraphIndexReader, node: GraphNode, results: Map<string, GraphRetrievalResult>, excluded: Set<string>,
score: number, pathLength: number, layer: SearchLayer,
): Promise<void> {
for (const id of node.sourceObservationIds ?? []) {
- if (seen.has(id)) continue;
+ if (excluded.has(id)) continue;
+ const kept = results.get(id);
+ if (kept && kept.score >= score) continue;
const evidence = await this.source(reader, id);
if (!evidence || !matchesSearchLayer(id, evidence.observation.sessionId, layer, evidence.layer)) continue;
- seen.add(id);
- results.push({ obsId: id, sessionId: evidence.observation.sessionId, score, pathLength,
+ results.delete(id);
+ results.set(id, { obsId: id, sessionId: evidence.observation.sessionId, score, pathLength,
graphContext: `[graph evidence] ${evidence.observation.title.slice(0, 512)}` });
}
}
@@ -232,18 +234,19 @@ export class GraphQuery {
}
const nodes = [...selected.values()].sort((a, b) =>
Number(nodeNames(b).some((n) => exactNames.has(n))) - Number(nodeNames(a).some((n) => exactNames.has(n))) || compareText(normalizeGraphIndexText(a.name), normalizeGraphIndexText(b.name)) || compareText(a.id, b.id));
- const results: GraphRetrievalResult[] = [];
- const seen = new Set<string>();
- for (const node of nodes) await this.append(reader, node, results, seen, 1, 0, targetLayer);
+ const results = new Map<string, GraphRetrievalResult>();
+ const excluded = new Set<string>();
+ for (const node of nodes) await this.append(reader, node, results, excluded, 1, 0, targetLayer);
+ for (const id of results.keys()) excluded.add(id);
for (const node of nodes) {
for (const path of await this.traverse(reader, node, maxDepth)) {
const weights = path.slice(1).map((step) => step.edge!.weight);
const score = weights.reduce((sum, weight) => sum + weight, 0) / weights.length / path.length;
- await this.append(reader, path[path.length - 1].node, results, seen, score, path.length, targetLayer);
+ await this.append(reader, path[path.length - 1].node, results, excluded, score, path.length, targetLayer);
}
}
- results.sort((a, b) => b.score - a.score);
- return this.finish(reader, results.slice(0, maxResults));
+ const ranked = [...results.values()].sort((a, b) => b.score - a.score);
+ return this.finish(reader, ranked.slice(0, maxResults));
});
}
@@ -265,15 +268,15 @@ export class GraphQuery {
}
if (nodes.size >= MAX_START_NODES) break;
}
- const results: GraphRetrievalResult[] = [];
- const seen = new Set(ids);
+ const results = new Map<string, GraphRetrievalResult>();
+ const excluded = new Set(ids);
for (const node of [...nodes.values()].sort((a, b) => compareText(a.id, b.id))) {
for (const path of await this.traverse(reader, node, maxDepth)) {
- await this.append(reader, path[path.length - 1].node, results, seen, 0.5 / (path.length + 1), path.length, targetLayer);
+ await this.append(reader, path[path.length - 1].node, results, excluded, 0.5 / (path.length + 1), path.length, targetLayer);
}
}
- results.sort((a, b) => b.score - a.score);
- return this.finish(reader, results.slice(0, maxResults));
+ const ranked = [...results.values()].sort((a, b) => b.score - a.score);
+ return this.finish(reader, ranked.slice(0, maxResults));
});
}
diff --git a/test/graph-retrieval.test.ts b/test/graph-retrieval.test.ts
index 2d157022..f6a00f98 100644
--- a/test/graph-retrieval.test.ts
+++ b/test/graph-retrieval.test.ts
@@ -138,13 +138,15 @@ describe("GraphRetrieval", () => {
});
it("does not duplicate already-seen observations in expansion", async () => {
- const nodes = [makeNode("n1", "file.ts", "file", ["obs_1", "obs_2"])];
- const kv = await mockKV(nodes, []);
+ const nodes = [
+ makeNode("n1", "file.ts", "file", ["obs_1"]),
+ makeNode("n2", "jwt", "concept", ["obs_1", "obs_2"]),
+ ];
+ const kv = await mockKV(nodes, [makeEdge("e1", "n1", "n2", "uses")]);
const retrieval = new GraphRetrieval(kv as never);
const results = await retrieval.expandFromChunks(["obs_1"]);
- const obsIds = results.map((r) => r.obsId);
- expect(obsIds).not.toContain("obs_1");
+ expect(results.map((r) => r.obsId)).toEqual(["obs_2"]);
});
it("performs temporal query - current state", async () => {
@@ -271,6 +273,118 @@ describe("GraphRetrieval", () => {
expect(root!.pathLength).toBe(0);
});
+ it.each([
+ { first: "Alpha", second: "Zeta" },
+ { first: "Zeta", second: "Alpha" },
+ ])("keeps the best score for an observation $first and $second both reach", async ({ first, second }) => {
+ const weak = `${first.toLowerCase()}_weak`;
+ const strong = `${second.toLowerCase()}_strong`;
+ const nodes = [
+ makeNode(weak, first, "concept", ["obs_weak"]),
+ makeNode(strong, second, "concept", ["obs_strong"]),
+ makeNode("mid", "Mid", "concept", ["obs_mid"]),
+ makeNode("target", "Target", "concept", ["obs_target"]),
+ ];
+ const edges = [
+ makeEdge("e1", weak, "mid", "related_to", 0.2),
+ makeEdge("e2", "mid", "target", "related_to", 0.2),
+ makeEdge("e3", strong, "target", "related_to", 0.9),
+ ];
+ const retrieval = new GraphRetrieval((await mockKV(nodes, edges)) as never);
+ const hit = (results: Awaited<ReturnType<typeof retrieval.searchByEntities>>) =>
+ results.find((r) => r.obsId === "obs_target");
+
+ const viaWeak = hit(await retrieval.searchByEntities([first], 2))!;
+ const viaStrong = hit(await retrieval.searchByEntities([second], 2))!;
+ expect(viaStrong.score).toBeGreaterThan(viaWeak.score);
+
+ const both = await retrieval.searchByEntities([first, second], 2);
+ expect(both.filter((r) => r.obsId === "obs_target")).toHaveLength(1);
+ expect(hit(both)).toMatchObject({ score: viaStrong.score, pathLength: viaStrong.pathLength });
+ });
+
+ it.each([
+ { far: "a_far", near: "b_near" },
+ { far: "b_far", near: "a_near" },
+ ])("keeps the closest chunk-expansion score whichever of $far and $near runs first", async ({ far, near }) => {
+ const nodes = [
+ makeNode(far, far, "concept", ["seed"]),
+ makeNode(near, near, "concept", ["seed"]),
+ makeNode("mid", "mid", "concept", ["obs_mid"]),
+ makeNode("target", "target", "concept", ["obs_target"]),
+ makeNode("sibling", "sibling", "concept", ["obs_sibling"]),
+ ];
+ const edges = [
+ makeEdge("e1", far, "mid", "related_to", 0.8),
+ makeEdge("e2", "mid", "target", "related_to", 0.8),
+ makeEdge("e3", near, "target", "related_to", 0.8),
+ makeEdge("e4", near, "sibling", "related_to", 0.8),
+ ];
+ const kv = await mockKV(nodes, edges);
+ const results = await new GraphRetrieval(kv as never).expandFromChunks(["seed"], 2, 10);
+
+ const target = results.find((r) => r.obsId === "obs_target");
+ const sibling = results.find((r) => r.obsId === "obs_sibling");
+ expect(results.filter((r) => r.obsId === "obs_target")).toHaveLength(1);
+ expect(target).toMatchObject({ score: sibling!.score, pathLength: sibling!.pathLength });
+ });
+
+ it("ranks a re-scored observation after the ones that reached its score first", async () => {
+ const nodes = [
+ makeNode("start", "Quasar", "concept", ["obs_start"]),
+ makeNode("hub", "Hub", "concept", ["obs_hub"]),
+ makeNode("weak", "Weak", "concept", ["obs_b"]),
+ makeNode("x_first", "First", "concept", ["obs_a"]),
+ makeNode("y_second", "Second", "concept", ["obs_b"]),
+ ];
+ const edges = [
+ makeEdge("e1", "start", "hub", "related_to", 0.9),
+ makeEdge("e2", "start", "weak", "related_to", 0.5),
+ makeEdge("e3", "hub", "x_first", "related_to", 0.9),
+ makeEdge("e4", "hub", "y_second", "related_to", 0.9),
+ ];
+ const kv = await mockKV(nodes, edges);
+ const results = await new GraphRetrieval(kv as never).searchByEntities(["Quasar"], 2);
+
+ const a = results.findIndex((r) => r.obsId === "obs_a");
+ const b = results.findIndex((r) => r.obsId === "obs_b");
+ expect(results[b].score).toBe(results[a].score);
+ expect(b).toBeGreaterThan(a);
+ });
+
+ it("keeps the first record when a later start node ties on score", async () => {
+ const nodes = [
+ makeNode("aaa", "Aaa", "concept", ["obs_aaa"]),
+ makeNode("bbb", "Bbb", "concept", ["obs_bbb"]),
+ makeNode("target", "Target", "concept", ["obs_target"]),
+ makeNode("other", "Other", "concept", ["obs_other"]),
+ ];
+ const edges = [
+ makeEdge("e1", "aaa", "target", "related_to", 0.6),
+ makeEdge("e2", "aaa", "other", "related_to", 0.6),
+ makeEdge("e3", "bbb", "target", "related_to", 0.6),
+ ];
+ const kv = await mockKV(nodes, edges);
+ const results = await new GraphRetrieval(kv as never).searchByEntities(["Aaa", "Bbb"], 1);
+
+ const target = results.findIndex((r) => r.obsId === "obs_target");
+ const other = results.findIndex((r) => r.obsId === "obs_other");
+ expect(results[target].score).toBe(results[other].score);
+ expect(target).toBeLessThan(other);
+ });
+
+ it("keeps a start node's own observation at 1.0 when an oversized weight scores a path higher", async () => {
+ const nodes = [
+ makeNode("alpha", "Alpha", "concept", ["obs_alpha"]),
+ makeNode("beta", "Beta", "concept", ["obs_beta"]),
+ ];
+ const kv = await mockKV(nodes, [makeEdge("e1", "alpha", "beta", "related_to", 3)]);
+ const results = await new GraphRetrieval(kv as never).searchByEntities(["Alpha", "Beta"], 2);
+
+ expect(results).toHaveLength(2);
+ for (const result of results) expect(result).toMatchObject({ score: 1, pathLength: 0 });
+ });
+
it("respects maxDepth bound (Dijkstra stops at edge-count depth)", async () => {
// Chain n1 -> n2 -> n3 -> n4. With maxDepth=2 we should reach n3
// but not n4 — edge-count semantics preserved from the old BFS.