Skip to content

Commit c4f8da4

Browse files
cliffhallclaude
andcommitted
fix(memory): hold only additive calls to the limits; refuse blank values
A call that adds nothing is no longer checked against the limits, since re-saving can grow a file not written in canonical form. An empty or blank limit variable now stops startup instead of leaving the limit off, as the README says. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 1c00bfa commit c4f8da4

3 files changed

Lines changed: 51 additions & 17 deletions

File tree

‎src/memory/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ On Windows, use:
255255
- `MEMORY_MAX_RELATIONS`: The most relations the graph may hold. Unset by default (no limit).
256256
- `MEMORY_MAX_FILE_BYTES`: The most bytes the memory file may hold. Unset by default (no limit).
257257

258-
The three limits are opt-in, and each is described under [Limiting how large the graph grows](#limiting-how-large-the-graph-grows). A limit that is set must be a positive whole number; any other value stops the server at startup with an error naming the variable.
258+
The three limits are opt-in, and each is described under [Limiting how large the graph grows](#limiting-how-large-the-graph-grows). A variable that is set must hold a positive whole number; any other value, an empty one included, stops the server at startup with an error naming the variable.
259259

260260
When `MEMORY_FILE_PATH` is not set and the server directory still holds a legacy `memory.json` but no `memory.jsonl`, the server renames `memory.json` to `memory.jsonl` at startup. If that rename fails, the server exits with an error naming both paths and the cause, rather than starting on an empty graph; fix the cause (a permission problem, say) and restart, or set `MEMORY_FILE_PATH` to choose a file explicitly.
261261

‎src/memory/__tests__/write-limits.test.ts‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// the graph file, which a refused call must leave byte for byte unchanged.
66
// The last block runs main() to show the environment reaches the server.
77
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
8+
import { promises as fs } from "fs";
89
import path from "path";
910
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
1011
import type { CallToolResult } from "@modelcontextprotocol/server";
@@ -29,13 +30,13 @@ describe("graphLimitsFromEnv", () => {
2930
expect(graphLimitsFromEnv({})).toEqual({});
3031
});
3132

32-
it("treats an empty or blank variable as unset", () => {
33-
expect(
34-
graphLimitsFromEnv({
35-
MEMORY_MAX_ENTITIES: "",
36-
MEMORY_MAX_RELATIONS: " ",
37-
}),
38-
).toEqual({});
33+
it("refuses an empty or blank variable rather than treating it as unset", () => {
34+
expect(() => graphLimitsFromEnv({ MEMORY_MAX_ENTITIES: "" })).toThrow(
35+
'MEMORY_MAX_ENTITIES must be a positive whole number, got ""',
36+
);
37+
expect(() => graphLimitsFromEnv({ MEMORY_MAX_RELATIONS: " " })).toThrow(
38+
'MEMORY_MAX_RELATIONS must be a positive whole number, got " "',
39+
);
3940
});
4041

4142
it("reads each limit from its variable, trimming whitespace", () => {
@@ -259,6 +260,26 @@ describe("write limits", () => {
259260
);
260261
});
261262

263+
it("answers a call that adds nothing even when re-saving grows the file", async () => {
264+
// A record with an extra space and no final newline: the save rewrites
265+
// it in canonical form, a few bytes larger, though nothing was added.
266+
const record =
267+
'{"type":"entity", "name":"A","entityType":"person","observations":["x"]}';
268+
await fs.writeFile(filePath, record);
269+
const client = await clientWith({
270+
maxFileBytes: Buffer.byteLength(record, "utf-8"),
271+
});
272+
273+
const entities = await call(client, "create_entities", {
274+
entities: [entity("A")],
275+
});
276+
expect(entities.isError).toBeUndefined();
277+
const observations = await call(client, "add_observations", {
278+
observations: [{ entityName: "A", contents: ["x"] }],
279+
});
280+
expect(observations.isError).toBeUndefined();
281+
});
282+
262283
it("refuses the first write to a new file that would already be too large", async () => {
263284
const client = await clientWith({ maxFileBytes: 10 });
264285
const refused = await call(client, "create_entities", {

‎src/memory/index.ts‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,20 @@ export const GRAPH_LIMIT_ENV: Readonly<Record<keyof GraphLimits, string>> = {
192192
maxFileBytes: "MEMORY_MAX_FILE_BYTES",
193193
};
194194

195-
// Read the limits from the environment. An unset or empty variable leaves its
196-
// limit off; anything other than a positive whole number stops startup, since
197-
// a misspelt limit silently ignored would leave the store unbounded while the
198-
// operator believes it capped.
195+
// Read the limits from the environment. Only an unset variable leaves its
196+
// limit off; any other value that is not a positive whole number, an empty
197+
// one included, stops startup, since a limit silently ignored would leave the
198+
// store unbounded while the operator believes it capped.
199199
export function graphLimitsFromEnv(
200200
env: NodeJS.ProcessEnv = process.env,
201201
): GraphLimits {
202202
const limits: GraphLimits = {};
203203
for (const key of Object.keys(GRAPH_LIMIT_ENV) as (keyof GraphLimits)[]) {
204204
const name = GRAPH_LIMIT_ENV[key];
205-
const raw = env[name]?.trim();
206-
if (raw === undefined || raw === "") {
205+
if (env[name] === undefined) {
207206
continue;
208207
}
208+
const raw = env[name].trim();
209209
const value = Number(raw);
210210
if (!/^\d+$/.test(raw) || !Number.isSafeInteger(value) || value < 1) {
211211
throw new Error(
@@ -739,7 +739,13 @@ export class KnowledgeGraphManager {
739739
!entities.slice(0, index).some((earlier) => earlier.name === e.name),
740740
);
741741
graph.entities.push(...newEntities);
742-
await this.saveGraph(graph, unreadable, size);
742+
// Only a call that adds something is held to the limits: re-saving can
743+
// grow an existing file that was not written in this exact form.
744+
await this.saveGraph(
745+
graph,
746+
unreadable,
747+
newEntities.length > 0 ? size : undefined,
748+
);
743749
return newEntities;
744750
});
745751
}
@@ -771,7 +777,12 @@ export class KnowledgeGraphManager {
771777
.some((earlier) => isSameRelation(earlier, r)),
772778
);
773779
graph.relations.push(...newRelations);
774-
await this.saveGraph(graph, unreadable, size);
780+
// Held to the limits only when it adds something (see createEntities).
781+
await this.saveGraph(
782+
graph,
783+
unreadable,
784+
newRelations.length > 0 ? size : undefined,
785+
);
775786
return newRelations;
776787
});
777788
}
@@ -794,7 +805,9 @@ export class KnowledgeGraphManager {
794805
entity.observations.push(...newObservations);
795806
return { entityName: o.entityName, addedObservations: newObservations };
796807
});
797-
await this.saveGraph(graph, unreadable, size);
808+
// Held to the limits only when it adds something (see createEntities).
809+
const added = results.some((r) => r.addedObservations.length > 0);
810+
await this.saveGraph(graph, unreadable, added ? size : undefined);
798811
return results;
799812
});
800813
}

0 commit comments

Comments
 (0)