|
| 1 | +// Characterization tests for the graph file being destroyed from outside the |
| 2 | +// server between two operations (#5539): deleted, truncated to zero bytes, |
| 3 | +// replaced by a directory, or joined by the temp remnant of a killed run. |
| 4 | +// The other suites pin these states only as a server finds them at its first |
| 5 | +// call (no file yet, a directory at the path) or as a save fails from inside |
| 6 | +// (a mocked write or rename). Here one server stays connected across the |
| 7 | +// change, so each case also pins that nothing read before it is served or |
| 8 | +// written back afterwards. |
| 9 | +// |
| 10 | +// Driven through an SDK Client over the in-memory transport rather than |
| 11 | +// KnowledgeGraphManager directly, as the issue proposed: what these cases |
| 12 | +// promise is what a client is told (an empty graph, or a tool error rather |
| 13 | +// than an empty graph), and a manager call skips the error mapping that |
| 14 | +// produces it. |
| 15 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 16 | +import { promises as fs } from "fs"; |
| 17 | +import path from "path"; |
| 18 | +import type { Client } from "@modelcontextprotocol/client"; |
| 19 | +import { |
| 20 | + call, |
| 21 | + connect, |
| 22 | + makeTempGraph, |
| 23 | + readFileText, |
| 24 | + textOf, |
| 25 | +} from "./helpers.js"; |
| 26 | +import type { Connection } from "./helpers.js"; |
| 27 | + |
| 28 | +const alice = { |
| 29 | + name: "Alice", |
| 30 | + entityType: "person", |
| 31 | + observations: ["Works at Acme"], |
| 32 | +}; |
| 33 | +const bob = { name: "Bob", entityType: "person", observations: [] }; |
| 34 | +const aliceLine = |
| 35 | + '{"type":"entity","name":"Alice","entityType":"person","observations":["Works at Acme"]}'; |
| 36 | +const bobLine = |
| 37 | + '{"type":"entity","name":"Bob","entityType":"person","observations":[]}'; |
| 38 | +const emptyGraph = { entities: [], relations: [] }; |
| 39 | + |
| 40 | +describe("the graph file destroyed from outside between operations (#5539)", () => { |
| 41 | + let dir: string; |
| 42 | + let filePath: string; |
| 43 | + let cleanup: () => Promise<void>; |
| 44 | + let conn: Connection; |
| 45 | + let client: Client; |
| 46 | + let errorSpy: ReturnType<typeof vi.spyOn>; |
| 47 | + |
| 48 | + // Every case starts from a connected server that has saved Alice. |
| 49 | + beforeEach(async () => { |
| 50 | + ({ dir, filePath, cleanup } = await makeTempGraph()); |
| 51 | + errorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 52 | + conn = await connect(filePath); |
| 53 | + client = conn.client; |
| 54 | + await call(client, "create_entities", { entities: [alice] }); |
| 55 | + expect(await readFileText(filePath)).toBe(`${aliceLine}\n`); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(async () => { |
| 59 | + await conn.close(); |
| 60 | + await cleanup(); |
| 61 | + vi.restoreAllMocks(); |
| 62 | + }); |
| 63 | + |
| 64 | + describe("deleted", () => { |
| 65 | + beforeEach(async () => { |
| 66 | + await fs.unlink(filePath); |
| 67 | + }); |
| 68 | + |
| 69 | + it("reads as an empty graph, and a read does not recreate the file", async () => { |
| 70 | + const result = await call(client, "read_graph"); |
| 71 | + |
| 72 | + expect(result.isError).toBeUndefined(); |
| 73 | + expect(result.structuredContent).toEqual(emptyGraph); |
| 74 | + expect(await fs.readdir(dir)).toEqual([]); |
| 75 | + expect(errorSpy).not.toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("recreates the file on the next write, holding only what that write added", async () => { |
| 79 | + const created = await call(client, "create_entities", { |
| 80 | + entities: [bob], |
| 81 | + }); |
| 82 | + |
| 83 | + expect(created.isError).toBeUndefined(); |
| 84 | + expect(created.structuredContent).toEqual({ entities: [bob] }); |
| 85 | + expect(await readFileText(filePath)).toBe(`${bobLine}\n`); |
| 86 | + expect(await fs.readdir(dir)).toEqual(["memory.jsonl"]); |
| 87 | + expect((await call(client, "read_graph")).structuredContent).toEqual({ |
| 88 | + entities: [bob], |
| 89 | + relations: [], |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("refuses a write that depends on a deleted entity, creating nothing", async () => { |
| 94 | + const result = await call(client, "add_observations", { |
| 95 | + observations: [{ entityName: "Alice", contents: ["Prefers email"] }], |
| 96 | + }); |
| 97 | + |
| 98 | + expect(result.isError).toBe(true); |
| 99 | + expect(textOf(result)).toBe("Entity with name Alice not found"); |
| 100 | + expect(await fs.readdir(dir)).toEqual([]); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe("truncated to zero bytes", () => { |
| 105 | + beforeEach(async () => { |
| 106 | + await fs.writeFile(filePath, ""); |
| 107 | + }); |
| 108 | + |
| 109 | + it("reads as an empty graph, logging nothing and leaving the file as it is", async () => { |
| 110 | + const result = await call(client, "read_graph"); |
| 111 | + |
| 112 | + expect(result.isError).toBeUndefined(); |
| 113 | + expect(result.structuredContent).toEqual(emptyGraph); |
| 114 | + expect(await readFileText(filePath)).toBe(""); |
| 115 | + expect(errorSpy).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + it("keeps working: the next write saves only what it added", async () => { |
| 119 | + const created = await call(client, "create_entities", { |
| 120 | + entities: [bob], |
| 121 | + }); |
| 122 | + |
| 123 | + expect(created.isError).toBeUndefined(); |
| 124 | + expect(created.structuredContent).toEqual({ entities: [bob] }); |
| 125 | + expect(await readFileText(filePath)).toBe(`${bobLine}\n`); |
| 126 | + expect(await fs.readdir(dir)).toEqual(["memory.jsonl"]); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + // persistence.test.ts pins a directory that is at the path when the server |
| 131 | + // first looks. This is the file a running server already saved turning |
| 132 | + // into one: the calls must fail rather than report an empty graph, which a |
| 133 | + // later save would then write over whatever replaces the directory. |
| 134 | + describe("replaced by a directory", () => { |
| 135 | + beforeEach(async () => { |
| 136 | + await fs.unlink(filePath); |
| 137 | + await fs.mkdir(filePath); |
| 138 | + }); |
| 139 | + |
| 140 | + it("fails reads and writes with EISDIR, leaving nothing behind", async () => { |
| 141 | + for (const [name, args] of [ |
| 142 | + ["read_graph", {}], |
| 143 | + ["create_entities", { entities: [bob] }], |
| 144 | + ["delete_entities", { entityNames: ["Alice"] }], |
| 145 | + ] as const) { |
| 146 | + const result = await call(client, name, args); |
| 147 | + expect(result.isError).toBe(true); |
| 148 | + // Only the code is asserted: the text after it is Node's own, and |
| 149 | + // whether it ends with the path differs by platform (#5076, #5162). |
| 150 | + expect(textOf(result)).toMatch(/^EISDIR: /); |
| 151 | + expect(result.structuredContent).toBeUndefined(); |
| 152 | + } |
| 153 | + |
| 154 | + // No lock file and no temp file beside the directory, nothing in it. |
| 155 | + expect(await fs.readdir(dir)).toEqual(["memory.jsonl"]); |
| 156 | + expect(await fs.readdir(filePath)).toEqual([]); |
| 157 | + }); |
| 158 | + |
| 159 | + it("serves again once the directory is gone", async () => { |
| 160 | + expect((await call(client, "read_graph")).isError).toBe(true); |
| 161 | + await fs.rmdir(filePath); |
| 162 | + |
| 163 | + const created = await call(client, "create_entities", { |
| 164 | + entities: [bob], |
| 165 | + }); |
| 166 | + |
| 167 | + expect(created.isError).toBeUndefined(); |
| 168 | + expect(await readFileText(filePath)).toBe(`${bobLine}\n`); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + // A run killed between saveGraph's temp-file write and its rename leaves |
| 173 | + // `<name>.<32 hex>.tmp` beside the graph file. This one holds a complete |
| 174 | + // record and a cut-off one, so a load that read it would show either. |
| 175 | + describe("beside an orphaned temp file from a killed run", () => { |
| 176 | + const remnantName = "memory.jsonl.0123456789abcdef0123456789abcdef.tmp"; |
| 177 | + const remnantText = |
| 178 | + '{"type":"entity","name":"Ghost","entityType":"person","observations":[]}\n' + |
| 179 | + '{"type":"entity","name":"Trunc'; |
| 180 | + let remnantPath: string; |
| 181 | + |
| 182 | + beforeEach(async () => { |
| 183 | + remnantPath = path.join(dir, remnantName); |
| 184 | + await fs.writeFile(remnantPath, remnantText); |
| 185 | + }); |
| 186 | + |
| 187 | + it("loads only the graph file", async () => { |
| 188 | + const result = await call(client, "read_graph"); |
| 189 | + |
| 190 | + expect(result.structuredContent).toEqual({ |
| 191 | + entities: [alice], |
| 192 | + relations: [], |
| 193 | + }); |
| 194 | + expect(errorSpy).not.toHaveBeenCalled(); |
| 195 | + }); |
| 196 | + |
| 197 | + it("saves past it, leaving the remnant untouched", async () => { |
| 198 | + const created = await call(client, "create_entities", { |
| 199 | + entities: [bob], |
| 200 | + }); |
| 201 | + |
| 202 | + expect(created.isError).toBeUndefined(); |
| 203 | + expect(await readFileText(filePath)).toBe(`${aliceLine}\n${bobLine}\n`); |
| 204 | + expect(await readFileText(remnantPath)).toBe(remnantText); |
| 205 | + expect((await fs.readdir(dir)).sort()).toEqual([ |
| 206 | + "memory.jsonl", |
| 207 | + remnantName, |
| 208 | + ]); |
| 209 | + expect(errorSpy).not.toHaveBeenCalled(); |
| 210 | + }); |
| 211 | + }); |
| 212 | +}); |
0 commit comments