Skip to content

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

fix(graph): floor the snapshot-reported total at the nodes it returns (#1385)

cbsincool6e3134fddadc
diff --git a/src/functions/graph.ts b/src/functions/graph.ts
index 38add02b..fedf97aa 100644
--- a/src/functions/graph.ts
+++ b/src/functions/graph.ts
@@ -151,9 +151,10 @@ function paginateFromSnapshot(
const filteredNodes = filterType
? snap.topNodes.filter((n) => n.type === filterType)
: snap.topNodes;
- const total = filterType
+ const totalRaw = filterType
? snap.stats.nodesByType[filterType] ?? 0
: snap.stats.totalNodes;
+ const total = Math.max(totalRaw, filteredNodes.length);
const pageNodes = filteredNodes.slice(offset, offset + limit);
const pageIds = new Set(pageNodes.map((n) => n.id));
const pageEdges = snap.topEdges.filter(
diff --git a/test/graph.test.ts b/test/graph.test.ts
index aae56da6..0149702c 100644
--- a/test/graph.test.ts
+++ b/test/graph.test.ts
@@ -597,6 +597,71 @@ describe("Graph Functions", () => {
// the oversized-corpus rebuild refusal. The hot path never enumerates
// any more, but the rebuild endpoint AND the BFS / query branches
// still call kv.list — both need explicit failure-mode tests.
+ describe("snapshot-reported total floor (#1382)", () => {
+ function seedSnapshot(
+ stats: { totalNodes: number; nodesByType: Record<string, number> },
+ topNodeCount: number,
+ ) {
+ const topNodes = Array.from({ length: topNodeCount }, (_, i) => ({
+ id: `n_${i}`,
+ type: "file",
+ name: `node-${i}`,
+ properties: {},
+ sourceObservationIds: [`obs_${i}`],
+ firstSeen: "2026-01-01T00:00:00Z",
+ lastSeen: "2026-01-01T00:00:00Z",
+ observationCount: 1,
+ stale: false,
+ }));
+ return kv.set("mem:graph:snapshot", "current", {
+ version: 1,
+ topNodes,
+ topEdges: [],
+ topDegrees: {},
+ stats: {
+ totalNodes: stats.totalNodes,
+ totalEdges: 0,
+ nodesByType: stats.nodesByType,
+ edgesByType: {},
+ },
+ updatedAt: "2026-01-01T00:00:00Z",
+ dirty: false,
+ });
+ }
+
+ it("does not report fewer nodes than it returns when the counter is low", async () => {
+ await seedSnapshot({ totalNodes: 101, nodesByType: { file: 101 } }, 343);
+
+ const result = (await sdk.trigger("mem::graph-query", {})) as GraphQueryResult;
+
+ expect(result.nodes.length).toBe(343);
+ expect(result.totalNodes).toBe(343);
+ expect(result.truncated).toBe(false);
+ });
+
+ it("leaves a healthy counter above the top-N cap alone and raises the banner", async () => {
+ await seedSnapshot({ totalNodes: 40000, nodesByType: { file: 40000 } }, 343);
+
+ const result = (await sdk.trigger("mem::graph-query", {})) as GraphQueryResult;
+
+ expect(result.nodes.length).toBe(343);
+ expect(result.totalNodes).toBe(40000);
+ expect(result.truncated).toBe(true);
+ });
+
+ it("applies the same floor to the type-filtered total", async () => {
+ await seedSnapshot({ totalNodes: 101, nodesByType: { file: 101 } }, 343);
+
+ const result = (await sdk.trigger("mem::graph-query", {
+ nodeType: "file",
+ })) as GraphQueryResult;
+
+ expect(result.nodes.length).toBe(343);
+ expect(result.totalNodes).toBe(343);
+ expect(result.truncated).toBe(false);
+ });
+ });
+
describe("budget + tooLarge guards (#814 v2)", () => {
function slowKV(delayMs: number) {
const base = mockKV();