Skip to content

Commit 07e81b3

Browse files
authored
Merge branch 'main' into fix/time-docker-local-timezone-expansion
2 parents 92c1353 + d73f99e commit 07e81b3

12 files changed

Lines changed: 689 additions & 94 deletions

‎package-lock.json‎

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/filesystem/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"@modelcontextprotocol/sdk": "^1.30.0",
2929
"diff": "^8.0.3",
3030
"glob": "^13.0.6",
31-
"minimatch": "^10.0.1"
31+
"minimatch": "^10.0.1",
32+
"zod": "^4.0.0"
3233
},
3334
"devDependencies": {
3435
"@types/diff": "^5.0.9",

‎src/memory/README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Example:
7272
- `to` (string): Target entity name
7373
- `relationType` (string): Relationship type in active voice
7474
- Skips duplicate relations
75+
- Fails if either the source or target entity doesn't exist
7576

7677
- **add_observations**
7778
- Add new observations to existing entities
@@ -86,15 +87,15 @@ Example:
8687
- Remove entities and their relations
8788
- Input: `entityNames` (string[])
8889
- Cascading deletion of associated relations
89-
- Silent operation if entity doesn't exist
90+
- No error if an entity doesn't exist; the response reports which names were not found
9091

9192
- **delete_observations**
9293
- Remove specific observations from entities
9394
- Input: `deletions` (array of objects)
9495
- Each object contains:
9596
- `entityName` (string): Target entity
9697
- `observations` (string[]): Observations to remove
97-
- Silent operation if observation doesn't exist
98+
- No error if an observation doesn't exist; the response reports how many were deleted
9899

99100
- **delete_relations**
100101
- Remove specific relations from the graph
@@ -103,7 +104,7 @@ Example:
103104
- `from` (string): Source entity name
104105
- `to` (string): Target entity name
105106
- `relationType` (string): Relationship type
106-
- Silent operation if relation doesn't exist
107+
- No error if a relation doesn't exist; the response reports how many were deleted
107108

108109
- **read_graph**
109110
- Read the entire knowledge graph
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2+
import { promises as fs } from 'fs';
3+
import path from 'path';
4+
import { fileURLToPath } from 'url';
5+
import { KnowledgeGraphManager, Entity, Relation } from '../index.js';
6+
7+
/**
8+
* The delete tools stay silent when a target is absent, which the README
9+
* documents. What they must not do is report a deletion that did not happen:
10+
* an agent that mistypes a name is told its memory is clean while the data is
11+
* still on disk, and nothing in the response contradicts that.
12+
*/
13+
describe('delete reporting', () => {
14+
let manager: KnowledgeGraphManager;
15+
let testFilePath: string;
16+
17+
const entities: Entity[] = [
18+
{ name: 'Alice', entityType: 'person', observations: ['works at Acme Corp', 'likes tea'] },
19+
{ name: 'Bob', entityType: 'person', observations: ['likes programming'] },
20+
];
21+
const relations: Relation[] = [{ from: 'Alice', to: 'Bob', relationType: 'works_with' }];
22+
23+
beforeEach(async () => {
24+
testFilePath = path.join(
25+
path.dirname(fileURLToPath(import.meta.url)),
26+
`test-delete-reporting-${Date.now()}-${Math.random().toString(16).slice(2)}.jsonl`
27+
);
28+
manager = new KnowledgeGraphManager(testFilePath);
29+
await manager.createEntities(entities);
30+
await manager.createRelations(relations);
31+
});
32+
33+
afterEach(async () => {
34+
try {
35+
await fs.unlink(testFilePath);
36+
} catch {
37+
// the file is gone already
38+
}
39+
});
40+
41+
describe('deleteEntities', () => {
42+
it('reports which names matched and which did not', async () => {
43+
const result = await manager.deleteEntities(['Alice', 'Alise']);
44+
expect(result).toEqual({ deleted: ['Alice'], notFound: ['Alise'] });
45+
});
46+
47+
it('reports nothing deleted when no name matches', async () => {
48+
const result = await manager.deleteEntities(['Nobody']);
49+
expect(result).toEqual({ deleted: [], notFound: ['Nobody'] });
50+
51+
const graph = await manager.readGraph();
52+
expect(graph.entities).toHaveLength(2);
53+
});
54+
55+
it('still deletes the entity and its relations', async () => {
56+
await manager.deleteEntities(['Alice']);
57+
58+
const graph = await manager.readGraph();
59+
expect(graph.entities.map(e => e.name)).toEqual(['Bob']);
60+
expect(graph.relations).toHaveLength(0);
61+
});
62+
});
63+
64+
describe('deleteObservations', () => {
65+
it('counts only the observations that were present', async () => {
66+
const result = await manager.deleteObservations([
67+
{ entityName: 'Alice', observations: ['likes tea', 'never said this'] },
68+
]);
69+
expect(result).toEqual({ deletedCount: 1, missingEntities: [] });
70+
});
71+
72+
it('names an entity that does not exist', async () => {
73+
const result = await manager.deleteObservations([
74+
{ entityName: 'Carol', observations: ['anything'] },
75+
]);
76+
expect(result).toEqual({ deletedCount: 0, missingEntities: ['Carol'] });
77+
});
78+
});
79+
80+
describe('deleteRelations', () => {
81+
it('counts only the relations that matched', async () => {
82+
const result = await manager.deleteRelations([
83+
{ from: 'Alice', to: 'Bob', relationType: 'works_with' },
84+
{ from: 'Alice', to: 'Bob', relationType: 'never_existed' },
85+
]);
86+
expect(result).toEqual({ deletedCount: 1 });
87+
});
88+
89+
it('reports nothing deleted when the relation type is wrong', async () => {
90+
const result = await manager.deleteRelations([
91+
{ from: 'Alice', to: 'Bob', relationType: 'manages' },
92+
]);
93+
expect(result).toEqual({ deletedCount: 0 });
94+
95+
const graph = await manager.readGraph();
96+
expect(graph.relations).toHaveLength(1);
97+
});
98+
});
99+
});

‎src/memory/__tests__/file-path.test.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
22
import { promises as fs } from 'fs';
33
import path from 'path';
4+
import os from 'os';
45
import { fileURLToPath } from 'url';
5-
import { ensureMemoryFilePath, defaultMemoryPath } from '../index.js';
6+
import { ensureMemoryFilePath, defaultMemoryPath, expandHome } from '../index.js';
67

78
describe('ensureMemoryFilePath', () => {
89
const testDir = path.dirname(fileURLToPath(import.meta.url));
@@ -72,6 +73,15 @@ describe('ensureMemoryFilePath', () => {
7273
expect(path.isAbsolute(result)).toBe(true);
7374
}
7475
});
76+
77+
it('should expand a leading "~/" to the home directory', async () => {
78+
process.env.MEMORY_FILE_PATH = '~/custom-memory.jsonl';
79+
80+
const result = await ensureMemoryFilePath();
81+
82+
expect(result).toBe(path.join(os.homedir(), 'custom-memory.jsonl'));
83+
expect(path.isAbsolute(result)).toBe(true);
84+
});
7585
});
7686

7787
describe('without MEMORY_FILE_PATH environment variable', () => {
@@ -154,3 +164,29 @@ describe('ensureMemoryFilePath', () => {
154164
});
155165
});
156166
});
167+
168+
describe('expandHome', () => {
169+
it('expands a bare "~" to the home directory', () => {
170+
expect(expandHome('~')).toBe(os.homedir());
171+
});
172+
173+
it('expands a leading "~/" to the home directory', () => {
174+
expect(expandHome('~/notes/memory.jsonl')).toBe(
175+
path.join(os.homedir(), 'notes/memory.jsonl')
176+
);
177+
});
178+
179+
it('leaves a "~" not followed by a separator unchanged', () => {
180+
expect(expandHome('~backup.jsonl')).toBe('~backup.jsonl');
181+
});
182+
183+
it('leaves absolute paths unchanged', () => {
184+
expect(expandHome('/var/data/memory.jsonl')).toBe('/var/data/memory.jsonl');
185+
});
186+
187+
it('leaves relative paths unchanged', () => {
188+
expect(expandHome(path.join('data', 'memory.jsonl'))).toBe(
189+
path.join('data', 'memory.jsonl')
190+
);
191+
});
192+
});

0 commit comments

Comments
 (0)