Skip to content

Commit 56802ca

Browse files
authored
Merge pull request #5625 from modelcontextprotocol/v2/chore/5540-memory-scale-round-trip-test
test(memory): pin exact read-back at 5,000-entity scale
2 parents 77d1bf3 + deb57b7 commit 56802ca

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Pins that a graph of thousands of records reads back exactly as written
2+
// (#5540). This is a correctness test, not a benchmark: it asserts nothing
3+
// about speed.
4+
//
5+
// Why it exists: the rest of this suite works on graphs of a few dozen
6+
// records at most, so on small files. Here the file is about 600 KB, several
7+
// times a 64 KiB stream chunk, written by ten full rewrites of a growing file
8+
// and one more for the relations. A change that only breaks
9+
// past a size no small test reaches would pass the rest of the suite: a
10+
// default cap or page size on read_graph or search_nodes, a move from
11+
// whole-file reads and writes to streamed ones that drops or splits a record
12+
// at a chunk boundary (the observations hold multi-byte characters for that
13+
// reason), or a rewrite that loses, duplicates or reorders records once a
14+
// second batch lands on a large file.
15+
//
16+
// Why it goes through an MCP client rather than KnowledgeGraphManager: the
17+
// place a default limit would be added is the tool layer, and the tool layer
18+
// is also where a result this large meets the SDK's input and output schema
19+
// validation and its JSON-RPC serialization. Driving the manager directly
20+
// would leave all of that unpinned at scale. The reader is a second
21+
// connection, so a second server with its own manager: it holds nothing the
22+
// writer held in memory, which makes reading through it the equivalent of
23+
// reading after a restart.
24+
//
25+
// Every record is compared, not a count and a sample: a count cannot see an
26+
// observation attached to the wrong entity or a relation with a swapped
27+
// endpoint.
28+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
29+
import { call, connect, makeTempGraph, readFileText } from "./helpers.js";
30+
import type { Connection } from "./helpers.js";
31+
import type { Entity, Relation } from "../index.js";
32+
33+
const ENTITY_COUNT = 5000;
34+
const BATCH_SIZE = 500;
35+
const RELATION_COUNT = 999;
36+
37+
// Zero-padded, so no name or observation is a substring of another and a
38+
// search for one matches exactly one entity.
39+
const pad = (n: number): string => String(n).padStart(4, "0");
40+
41+
const ENTITIES: Entity[] = Array.from({ length: ENTITY_COUNT }, (_, n) => ({
42+
name: `entity-${pad(n)}`,
43+
entityType: `bulk-${n % 7}`,
44+
observations: [`obs-${pad(n)}`, `観測 ${pad(n)} é`],
45+
}));
46+
47+
// A chain over the first thousand entities: entity-0000 → entity-0001 → …
48+
const RELATIONS: Relation[] = Array.from(
49+
{ length: RELATION_COUNT },
50+
(_, n) => ({
51+
from: `entity-${pad(n)}`,
52+
to: `entity-${pad(n + 1)}`,
53+
relationType: "next",
54+
}),
55+
);
56+
57+
// The setup makes eleven writes, each a full rewrite under the file lock.
58+
// Measured on an Apple-silicon Mac, setup and all three tests together take
59+
// 0.3 to 0.4 s, uninstrumented or under coverage (0.96 s once, on a loaded
60+
// machine). The default 5 s hook timeout is too little headroom over that
61+
// for a slow Windows runner (#5570); thirty seconds is a wide margin that
62+
// still fails a write path whose cost has stopped being linear.
63+
const SETUP_TIMEOUT_MS = 30_000;
64+
65+
describe("round trip at 5,000-entity scale", () => {
66+
let cleanup: () => Promise<void>;
67+
let filePath: string;
68+
let reader: Connection;
69+
70+
beforeAll(async () => {
71+
const temp = await makeTempGraph();
72+
cleanup = temp.cleanup;
73+
filePath = temp.filePath;
74+
75+
const writer = await connect(filePath);
76+
try {
77+
for (let start = 0; start < ENTITY_COUNT; start += BATCH_SIZE) {
78+
const batch = ENTITIES.slice(start, start + BATCH_SIZE);
79+
const created = await call(writer.client, "create_entities", {
80+
entities: batch,
81+
});
82+
// Each call reports exactly its own batch as created: nothing
83+
// skipped as a duplicate, nothing carried over from another batch.
84+
expect(created.isError).toBeFalsy();
85+
expect(created.structuredContent).toEqual({ entities: batch });
86+
}
87+
const created = await call(writer.client, "create_relations", {
88+
relations: RELATIONS,
89+
});
90+
expect(created.isError).toBeFalsy();
91+
expect(created.structuredContent).toEqual({ relations: RELATIONS });
92+
} finally {
93+
await writer.close();
94+
}
95+
96+
reader = await connect(filePath);
97+
}, SETUP_TIMEOUT_MS);
98+
99+
afterAll(async () => {
100+
await reader?.close();
101+
await cleanup?.();
102+
});
103+
104+
it("read_graph returns every entity and relation, in order, with no options", async () => {
105+
const result = await call(reader.client, "read_graph");
106+
107+
expect(result.isError).toBeFalsy();
108+
expect(result.structuredContent).toEqual({
109+
entities: ENTITIES,
110+
relations: RELATIONS,
111+
});
112+
});
113+
114+
it("search_nodes finds the one entity holding an observation, with both relations that touch it", async () => {
115+
const result = await call(reader.client, "search_nodes", {
116+
query: "obs-0500",
117+
});
118+
119+
expect(result.isError).toBeFalsy();
120+
expect(result.structuredContent).toEqual({
121+
entities: [ENTITIES[500]],
122+
relations: [RELATIONS[499], RELATIONS[500]],
123+
});
124+
});
125+
126+
it("stores one record per line: the entities, then the relations", async () => {
127+
const lines = (await readFileText(filePath))
128+
.split("\n")
129+
.filter((line) => line !== "");
130+
131+
expect(lines.map((line) => JSON.parse(line) as unknown)).toEqual([
132+
...ENTITIES.map((entity) => ({ type: "entity", ...entity })),
133+
...RELATIONS.map((relation) => ({ type: "relation", ...relation })),
134+
]);
135+
});
136+
});

0 commit comments

Comments
 (0)