Skip to content

Commit 3468400

Browse files
fix(memory): keep a symlinked memory file linked when saving
1 parent bbb701e commit 3468400

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎src/memory/__tests__/atomic-save.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,47 @@ describe("KnowledgeGraphManager persistence durability", () => {
142142
expect(await fs.readdir(testDir)).toEqual(["memory.jsonl"]);
143143
});
144144

145+
it("writes through a symlinked memory file instead of replacing the link", async () => {
146+
const targetDir = path.join(testDir, "synced");
147+
await fs.mkdir(targetDir);
148+
const targetPath = path.join(targetDir, "memory.jsonl");
149+
await fs.writeFile(targetPath, "");
150+
await fs.symlink(targetPath, testFilePath);
151+
152+
const manager = new KnowledgeGraphManager(testFilePath);
153+
await manager.createEntities([
154+
{
155+
name: "Alice",
156+
entityType: "person",
157+
observations: ["works at Acme Corp"],
158+
},
159+
]);
160+
161+
expect((await fs.lstat(testFilePath)).isSymbolicLink()).toBe(true);
162+
expect(await fs.readFile(targetPath, "utf-8")).toContain("Alice");
163+
expect(await fs.readdir(targetDir)).toEqual(["memory.jsonl"]);
164+
});
165+
166+
it("creates the target of a symlinked memory file that does not exist yet", async () => {
167+
const targetDir = path.join(testDir, "synced");
168+
await fs.mkdir(targetDir);
169+
await fs.symlink(path.join("synced", "memory.jsonl"), testFilePath);
170+
171+
const manager = new KnowledgeGraphManager(testFilePath);
172+
await manager.createEntities([
173+
{
174+
name: "Alice",
175+
entityType: "person",
176+
observations: ["works at Acme Corp"],
177+
},
178+
]);
179+
180+
expect((await fs.lstat(testFilePath)).isSymbolicLink()).toBe(true);
181+
expect(
182+
await fs.readFile(path.join(targetDir, "memory.jsonl"), "utf-8"),
183+
).toContain("Alice");
184+
});
185+
145186
it("still persists graph contents correctly across reloads", async () => {
146187
const manager = new KnowledgeGraphManager(testFilePath);
147188
await manager.createEntities([

‎src/memory/index.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,23 @@ export class KnowledgeGraphManager {
475475
// bits before the rename, as src/filesystem/lib.ts preserves them for
476476
// the same write pattern. A graph file that does not exist yet has
477477
// nothing to preserve.
478+
//
479+
// Resolve symlinks so the rename replaces the link's target, not the link.
480+
const targetPath = await fs
481+
.realpath(this.memoryFilePath)
482+
.catch(async () => {
483+
const link = await fs
484+
.readlink(this.memoryFilePath)
485+
.catch(() => undefined);
486+
return link
487+
? path.resolve(path.dirname(this.memoryFilePath), link)
488+
: this.memoryFilePath;
489+
});
490+
478491
let mode: number | undefined;
479492
try {
480-
await fs.access(this.memoryFilePath, fs.constants.W_OK);
481-
mode = (await fs.stat(this.memoryFilePath)).mode & 0o777;
493+
await fs.access(targetPath, fs.constants.W_OK);
494+
mode = (await fs.stat(targetPath)).mode & 0o777;
482495
} catch (error) {
483496
if (
484497
!(error instanceof Error && "code" in error && error.code === "ENOENT")
@@ -487,10 +500,10 @@ export class KnowledgeGraphManager {
487500
}
488501
}
489502

490-
const directory = path.dirname(this.memoryFilePath);
503+
const directory = path.dirname(targetPath);
491504
const tempFilePath = path.join(
492505
directory,
493-
`${path.basename(this.memoryFilePath)}.${randomBytes(16).toString("hex")}.tmp`,
506+
`${path.basename(targetPath)}.${randomBytes(16).toString("hex")}.tmp`,
494507
);
495508

496509
try {
@@ -506,7 +519,7 @@ export class KnowledgeGraphManager {
506519
if (mode !== undefined) {
507520
await fs.chmod(tempFilePath, mode);
508521
}
509-
await fs.rename(tempFilePath, this.memoryFilePath);
522+
await fs.rename(tempFilePath, targetPath);
510523
} catch (error) {
511524
// Never leave a stray temp file behind on failure.
512525
await fs.unlink(tempFilePath).catch(() => {});

0 commit comments

Comments
 (0)