|
| 1 | +// Characterization tests for #5541: what a stored record that fails the schema |
| 2 | +// costs once it is in the graph file. #4885 made the server skip such a line |
| 3 | +// on read and write it back verbatim on every save, so that a write never |
| 4 | +// drops data it could not read (persistence.test.ts pins both). The |
| 5 | +// consequence pinned here is that the record is then out of every tool's |
| 6 | +// reach: the read tools do not show it, the delete tools report its name as |
| 7 | +// not found, the add tools refuse it as missing, and creating an entity under |
| 8 | +// its name stores a second record beside it instead of replacing it. Only an |
| 9 | +// edit of the file by hand removes or repairs the line. |
| 10 | +// |
| 11 | +// The tests take no position on whether a remediation tool should exist. They |
| 12 | +// state today's contract, and a change that adds one changes these assertions |
| 13 | +// visibly. |
| 14 | +// |
| 15 | +// The claim is about tools, so each case drives the server through an SDK |
| 16 | +// Client over an in-memory transport (helpers.ts) and asserts on the tool's |
| 17 | +// result and on the file's exact bytes: the "not found" wording a client reads |
| 18 | +// is produced in the tool handlers, which a call on KnowledgeGraphManager |
| 19 | +// would skip. Every write tool is exercised, since the claim is "no tool". |
| 20 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 21 | +import { promises as fs } from "fs"; |
| 22 | +import { call, connect, makeTempGraph, readFileText } from "./helpers.js"; |
| 23 | +import type { Connection } from "./helpers.js"; |
| 24 | + |
| 25 | +const alice = { |
| 26 | + name: "Alice", |
| 27 | + entityType: "person", |
| 28 | + observations: ["Works at Acme"], |
| 29 | +}; |
| 30 | +const bob = { name: "Bob", entityType: "person", observations: [] }; |
| 31 | +const carol = { name: "Carol", entityType: "person", observations: [] }; |
| 32 | +const aliceManagesBob = { from: "Alice", to: "Bob", relationType: "manages" }; |
| 33 | + |
| 34 | +const aliceLine = JSON.stringify({ type: "entity", ...alice }); |
| 35 | +const bobLine = JSON.stringify({ type: "entity", ...bob }); |
| 36 | +const carolLine = JSON.stringify({ type: "entity", ...carol }); |
| 37 | +const aliceManagesBobLine = JSON.stringify({ |
| 38 | + type: "relation", |
| 39 | + ...aliceManagesBob, |
| 40 | +}); |
| 41 | + |
| 42 | +// An entity record with no entityType, and a relation record with no |
| 43 | +// relationType: each parses as JSON and fails its schema. |
| 44 | +const invalidEntityLine = |
| 45 | + '{"type":"entity","name":"Broken","observations":["missing entityType"]}'; |
| 46 | +const invalidRelationLine = '{"type":"relation","from":"Alice","to":"Bob"}'; |
| 47 | + |
| 48 | +// In the order a save writes them (entities, relations, then the lines it |
| 49 | +// could not read), so a save that changes nothing rewrites the same bytes. |
| 50 | +const seedLines = [ |
| 51 | + aliceLine, |
| 52 | + bobLine, |
| 53 | + aliceManagesBobLine, |
| 54 | + invalidEntityLine, |
| 55 | + invalidRelationLine, |
| 56 | +]; |
| 57 | + |
| 58 | +function fileOf(lines: string[]): string { |
| 59 | + return lines.join("\n") + "\n"; |
| 60 | +} |
| 61 | + |
| 62 | +describe("a schema-invalid record is out of every tool's reach (#5541)", () => { |
| 63 | + let filePath: string; |
| 64 | + let cleanup: () => Promise<void>; |
| 65 | + let conn: Connection; |
| 66 | + |
| 67 | + beforeEach(async () => { |
| 68 | + ({ filePath, cleanup } = await makeTempGraph()); |
| 69 | + await fs.writeFile(filePath, fileOf(seedLines)); |
| 70 | + // Every load logs "Skipping invalid entity/relation in memory file". |
| 71 | + vi.spyOn(console, "error").mockImplementation(() => {}); |
| 72 | + conn = await connect(filePath); |
| 73 | + }); |
| 74 | + |
| 75 | + afterEach(async () => { |
| 76 | + vi.restoreAllMocks(); |
| 77 | + await conn.close(); |
| 78 | + await cleanup(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("is not shown by read_graph, search_nodes or open_nodes", async () => { |
| 82 | + const { client } = conn; |
| 83 | + |
| 84 | + expect((await call(client, "read_graph")).structuredContent).toEqual({ |
| 85 | + entities: [alice, bob], |
| 86 | + relations: [aliceManagesBob], |
| 87 | + }); |
| 88 | + expect( |
| 89 | + (await call(client, "search_nodes", { query: "Broken" })) |
| 90 | + .structuredContent, |
| 91 | + ).toEqual({ entities: [], relations: [] }); |
| 92 | + expect( |
| 93 | + (await call(client, "search_nodes", { query: "missing entityType" })) |
| 94 | + .structuredContent, |
| 95 | + ).toEqual({ entities: [], relations: [] }); |
| 96 | + expect( |
| 97 | + (await call(client, "open_nodes", { names: ["Broken"] })) |
| 98 | + .structuredContent, |
| 99 | + ).toEqual({ entities: [], relations: [] }); |
| 100 | + // Reading writes nothing. |
| 101 | + expect(await readFileText(filePath)).toBe(fileOf(seedLines)); |
| 102 | + }); |
| 103 | + |
| 104 | + it("is reported as not found by delete_entities, and outlives it and a later unrelated write", async () => { |
| 105 | + const { client } = conn; |
| 106 | + |
| 107 | + const deleted = await call(client, "delete_entities", { |
| 108 | + entityNames: ["Broken"], |
| 109 | + }); |
| 110 | + |
| 111 | + expect(deleted.isError).toBeUndefined(); |
| 112 | + expect(deleted.structuredContent).toEqual({ |
| 113 | + success: true, |
| 114 | + message: "Deleted 0 of 1 entities. Not found: Broken", |
| 115 | + }); |
| 116 | + expect(await readFileText(filePath)).toBe(fileOf(seedLines)); |
| 117 | + |
| 118 | + await call(client, "create_entities", { entities: [carol] }); |
| 119 | + |
| 120 | + expect(await readFileText(filePath)).toBe( |
| 121 | + fileOf([ |
| 122 | + aliceLine, |
| 123 | + bobLine, |
| 124 | + carolLine, |
| 125 | + aliceManagesBobLine, |
| 126 | + invalidEntityLine, |
| 127 | + invalidRelationLine, |
| 128 | + ]), |
| 129 | + ); |
| 130 | + expect((await call(client, "read_graph")).structuredContent).toEqual({ |
| 131 | + entities: [alice, bob, carol], |
| 132 | + relations: [aliceManagesBob], |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + it("keeps its observations when delete_observations names them", async () => { |
| 137 | + const result = await call(conn.client, "delete_observations", { |
| 138 | + deletions: [ |
| 139 | + { entityName: "Broken", observations: ["missing entityType"] }, |
| 140 | + ], |
| 141 | + }); |
| 142 | + |
| 143 | + expect(result.isError).toBeUndefined(); |
| 144 | + expect(result.structuredContent).toEqual({ |
| 145 | + success: true, |
| 146 | + message: "Deleted 0 of 1 observations. Entities not found: Broken", |
| 147 | + }); |
| 148 | + expect(await readFileText(filePath)).toBe(fileOf(seedLines)); |
| 149 | + }); |
| 150 | + |
| 151 | + it("is refused as missing by add_observations, so it cannot be repaired in place", async () => { |
| 152 | + const result = await call(conn.client, "add_observations", { |
| 153 | + observations: [{ entityName: "Broken", contents: ["a new fact"] }], |
| 154 | + }); |
| 155 | + |
| 156 | + expect(result.isError).toBe(true); |
| 157 | + expect(result.content).toEqual([ |
| 158 | + { type: "text", text: "Entity with name Broken not found" }, |
| 159 | + ]); |
| 160 | + expect(await readFileText(filePath)).toBe(fileOf(seedLines)); |
| 161 | + }); |
| 162 | + |
| 163 | + it("is refused as a relation's end by create_relations", async () => { |
| 164 | + const result = await call(conn.client, "create_relations", { |
| 165 | + relations: [{ from: "Alice", to: "Broken", relationType: "knows" }], |
| 166 | + }); |
| 167 | + |
| 168 | + expect(result.isError).toBe(true); |
| 169 | + expect(result.content).toEqual([ |
| 170 | + { type: "text", text: "Entity with name Broken not found" }, |
| 171 | + ]); |
| 172 | + expect(await readFileText(filePath)).toBe(fileOf(seedLines)); |
| 173 | + }); |
| 174 | + |
| 175 | + // create_entities does not see the invalid record, so the name is free: the |
| 176 | + // new entity is stored as a second record under the same name, and the |
| 177 | + // invalid line stays. Deleting by that name then removes only the new one. |
| 178 | + it("is not replaced by create_entities under its name, which adds a second record beside it", async () => { |
| 179 | + const { client } = conn; |
| 180 | + const replacement = { |
| 181 | + name: "Broken", |
| 182 | + entityType: "thing", |
| 183 | + observations: ["recreated"], |
| 184 | + }; |
| 185 | + |
| 186 | + const created = await call(client, "create_entities", { |
| 187 | + entities: [replacement], |
| 188 | + }); |
| 189 | + |
| 190 | + expect(created.structuredContent).toEqual({ entities: [replacement] }); |
| 191 | + expect(await readFileText(filePath)).toBe( |
| 192 | + fileOf([ |
| 193 | + aliceLine, |
| 194 | + bobLine, |
| 195 | + JSON.stringify({ type: "entity", ...replacement }), |
| 196 | + aliceManagesBobLine, |
| 197 | + invalidEntityLine, |
| 198 | + invalidRelationLine, |
| 199 | + ]), |
| 200 | + ); |
| 201 | + |
| 202 | + const deleted = await call(client, "delete_entities", { |
| 203 | + entityNames: ["Broken"], |
| 204 | + }); |
| 205 | + |
| 206 | + expect(deleted.structuredContent).toEqual({ |
| 207 | + success: true, |
| 208 | + message: "Entities deleted successfully", |
| 209 | + }); |
| 210 | + expect(await readFileText(filePath)).toBe(fileOf(seedLines)); |
| 211 | + }); |
| 212 | + |
| 213 | + describe("an invalid relation record", () => { |
| 214 | + // The record has no relationType, and delete_relations requires one, so |
| 215 | + // no call can name it: the same call without the field is rejected. |
| 216 | + it("matches no delete_relations call, whatever relationType is given", async () => { |
| 217 | + const { client } = conn; |
| 218 | + |
| 219 | + const unnamed = await call(client, "delete_relations", { |
| 220 | + relations: [{ from: "Alice", to: "Bob" }], |
| 221 | + }); |
| 222 | + expect(unnamed.isError).toBe(true); |
| 223 | + expect(unnamed.content).toEqual([ |
| 224 | + { |
| 225 | + type: "text", |
| 226 | + text: "Input validation error: Invalid arguments for tool delete_relations: relations.0.relationType: Invalid input: expected string, received undefined", |
| 227 | + }, |
| 228 | + ]); |
| 229 | + |
| 230 | + const empty = await call(client, "delete_relations", { |
| 231 | + relations: [{ from: "Alice", to: "Bob", relationType: "" }], |
| 232 | + }); |
| 233 | + expect(empty.structuredContent).toEqual({ |
| 234 | + success: true, |
| 235 | + message: "Deleted 0 of 1 relations. The rest matched nothing.", |
| 236 | + }); |
| 237 | + expect(await readFileText(filePath)).toBe(fileOf(seedLines)); |
| 238 | + }); |
| 239 | + |
| 240 | + // delete_entities removes every valid relation that touches the entity; |
| 241 | + // the invalid one that names it is left behind. |
| 242 | + it("is left behind when delete_entities removes both entities it names", async () => { |
| 243 | + const { client } = conn; |
| 244 | + |
| 245 | + const deleted = await call(client, "delete_entities", { |
| 246 | + entityNames: ["Alice", "Bob"], |
| 247 | + }); |
| 248 | + |
| 249 | + expect(deleted.structuredContent).toEqual({ |
| 250 | + success: true, |
| 251 | + message: "Entities deleted successfully", |
| 252 | + }); |
| 253 | + expect(await readFileText(filePath)).toBe( |
| 254 | + fileOf([invalidEntityLine, invalidRelationLine]), |
| 255 | + ); |
| 256 | + expect((await call(client, "read_graph")).structuredContent).toEqual({ |
| 257 | + entities: [], |
| 258 | + relations: [], |
| 259 | + }); |
| 260 | + }); |
| 261 | + }); |
| 262 | +}); |
0 commit comments