Skip to content

Commit 01062fc

Browse files
cliffhallclaude
andcommitted
fix(memory): store an observation repeated in one add_observations call once
addObservations filtered the incoming contents against what the entity already held, but not against each other, so contents: ["x", "x"] stored "x" twice and reported it twice in addedObservations. De-duplicate the contents first, keeping the first occurrence's order. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 446be48 commit 01062fc

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-memory": patch
3+
---
4+
5+
`add_observations` stores an observation repeated within one call once, and reports it once in `addedObservations`, instead of storing it twice.

‎src/memory/__tests__/knowledge-graph.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,21 @@ describe("KnowledgeGraphManager", () => {
236236
]);
237237
}).rejects.toThrow("Entity with name NonExistent not found");
238238
});
239+
240+
it("should store and report an observation repeated in one call once", async () => {
241+
await manager.createEntities([
242+
{ name: "Alice", entityType: "person", observations: [] },
243+
]);
244+
245+
const results = await manager.addObservations([
246+
{ entityName: "Alice", contents: ["x", "y", "x"] },
247+
]);
248+
249+
expect(results[0].addedObservations).toEqual(["x", "y"]);
250+
const graph = await manager.readGraph();
251+
const alice = graph.entities.find((e) => e.name === "Alice");
252+
expect(alice?.observations).toEqual(["x", "y"]);
253+
});
239254
});
240255

241256
describe("deleteEntities", () => {

‎src/memory/__tests__/tools.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,26 @@ describe("memory tools over the protocol", () => {
651651
relations: [],
652652
});
653653
});
654+
655+
it("stores and reports an observation repeated in one call once", async () => {
656+
const result = await call(client, "add_observations", {
657+
observations: [
658+
{ entityName: "Alice", contents: ["plays chess", "plays chess"] },
659+
],
660+
});
661+
const expected = [
662+
{ entityName: "Alice", addedObservations: ["plays chess"] },
663+
];
664+
expect(result.isError).toBeUndefined();
665+
expect(result.structuredContent).toEqual({ results: expected });
666+
expect(await readGraph()).toEqual({
667+
entities: [
668+
{ ...alice, observations: [...alice.observations, "plays chess"] },
669+
bob,
670+
],
671+
relations: [],
672+
});
673+
});
654674
});
655675

656676
describe("delete_entities", () => {

‎src/memory/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,9 @@ export class KnowledgeGraphManager {
633633
if (!entity) {
634634
throw new Error(`Entity with name ${o.entityName} not found`);
635635
}
636-
const newObservations = o.contents.filter(
636+
// A Set keeps the first occurrence of each string, so a repeat within
637+
// one call is stored and reported once (#5201).
638+
const newObservations = [...new Set(o.contents)].filter(
637639
(content) => !entity.observations.includes(content),
638640
);
639641
entity.observations.push(...newObservations);

0 commit comments

Comments
 (0)