|
1 | 1 | // Memory Host SDK tests cover session files behavior. |
2 | 2 | import fsSync from "node:fs"; |
| 3 | +import fsPromises from "node:fs/promises"; |
3 | 4 | import os from "node:os"; |
4 | 5 | import path from "node:path"; |
5 | | -import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; |
| 6 | +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 7 | +import { emitSessionTranscriptUpdate } from "../../../../src/sessions/transcript-events.js"; |
6 | 8 | import { |
7 | 9 | buildSessionEntry, |
8 | 10 | listSessionFilesForAgent, |
| 11 | + resetSessionFilesListingCache, |
9 | 12 | sessionPathForFile, |
10 | 13 | type SessionFileEntry, |
11 | 14 | } from "./session-files.js"; |
@@ -74,6 +77,136 @@ describe("listSessionFilesForAgent", () => { |
74 | 77 | }); |
75 | 78 | }); |
76 | 79 |
|
| 80 | +describe("listSessionFilesForAgent listing cache", () => { |
| 81 | + let sessionsDir: string; |
| 82 | + |
| 83 | + beforeEach(() => { |
| 84 | + resetSessionFilesListingCache(); |
| 85 | + sessionsDir = path.join(tmpDir, "agents", "main", "sessions"); |
| 86 | + fsSync.mkdirSync(sessionsDir, { recursive: true }); |
| 87 | + }); |
| 88 | + |
| 89 | + afterEach(() => { |
| 90 | + vi.restoreAllMocks(); |
| 91 | + vi.useRealTimers(); |
| 92 | + resetSessionFilesListingCache(); |
| 93 | + }); |
| 94 | + |
| 95 | + function writeSession(name: string): void { |
| 96 | + fsSync.writeFileSync(path.join(sessionsDir, name), ""); |
| 97 | + } |
| 98 | + |
| 99 | + function baseNames(files: string[]): string[] { |
| 100 | + return files.map((filePath) => path.basename(filePath)).toSorted(); |
| 101 | + } |
| 102 | + |
| 103 | + it("coalesces concurrent reads into a single READDIR", async () => { |
| 104 | + writeSession("a.jsonl"); |
| 105 | + const readdirSpy = vi.spyOn(fsPromises, "readdir"); |
| 106 | + |
| 107 | + const [first, second] = await Promise.all([ |
| 108 | + listSessionFilesForAgent("main"), |
| 109 | + listSessionFilesForAgent("main"), |
| 110 | + ]); |
| 111 | + |
| 112 | + expect(readdirSpy).toHaveBeenCalledTimes(1); |
| 113 | + expect(first).toEqual(second); |
| 114 | + expect(baseNames(first)).toEqual(["a.jsonl"]); |
| 115 | + }); |
| 116 | + |
| 117 | + it("serves the cached snapshot within the TTL and skips repeat scans", async () => { |
| 118 | + writeSession("a.jsonl"); |
| 119 | + const readdirSpy = vi.spyOn(fsPromises, "readdir"); |
| 120 | + |
| 121 | + const firstCall = await listSessionFilesForAgent("main"); |
| 122 | + // Mutate the dir without emitting a transcript event; the cache must hold. |
| 123 | + writeSession("b.jsonl"); |
| 124 | + const secondCall = await listSessionFilesForAgent("main"); |
| 125 | + |
| 126 | + expect(readdirSpy).toHaveBeenCalledTimes(1); |
| 127 | + expect(baseNames(firstCall)).toEqual(["a.jsonl"]); |
| 128 | + expect(baseNames(secondCall)).toEqual(["a.jsonl"]); |
| 129 | + }); |
| 130 | + |
| 131 | + it("returns an isolated copy so caller mutation cannot corrupt the cache", async () => { |
| 132 | + writeSession("a.jsonl"); |
| 133 | + const readdirSpy = vi.spyOn(fsPromises, "readdir"); |
| 134 | + |
| 135 | + // Concurrent burst exercises the in-flight originator + joiner return paths; |
| 136 | + // a shared reference here would poison the snapshot for everyone. |
| 137 | + const [a, b] = await Promise.all([ |
| 138 | + listSessionFilesForAgent("main"), |
| 139 | + listSessionFilesForAgent("main"), |
| 140 | + ]); |
| 141 | + a.length = 0; |
| 142 | + b.push("/phantom.jsonl"); |
| 143 | + |
| 144 | + // Sequential cache hit exercises the snapshot return path. |
| 145 | + const cached = await listSessionFilesForAgent("main"); |
| 146 | + cached.length = 0; |
| 147 | + |
| 148 | + const afterMutation = await listSessionFilesForAgent("main"); |
| 149 | + |
| 150 | + // One scan total; no caller's mutation leaked into the cached snapshot. |
| 151 | + expect(readdirSpy).toHaveBeenCalledTimes(1); |
| 152 | + expect(baseNames(afterMutation)).toEqual(["a.jsonl"]); |
| 153 | + }); |
| 154 | + |
| 155 | + it("refreshes the snapshot after the TTL elapses", async () => { |
| 156 | + vi.useFakeTimers({ toFake: ["Date"] }); |
| 157 | + writeSession("a.jsonl"); |
| 158 | + const readdirSpy = vi.spyOn(fsPromises, "readdir"); |
| 159 | + |
| 160 | + await listSessionFilesForAgent("main"); |
| 161 | + writeSession("b.jsonl"); |
| 162 | + vi.setSystemTime(Date.now() + 1_001); |
| 163 | + const refreshed = await listSessionFilesForAgent("main"); |
| 164 | + |
| 165 | + expect(readdirSpy).toHaveBeenCalledTimes(2); |
| 166 | + expect(baseNames(refreshed)).toEqual(["a.jsonl", "b.jsonl"]); |
| 167 | + }); |
| 168 | + |
| 169 | + it("invalidates the snapshot when a transcript update lands for the agent", async () => { |
| 170 | + writeSession("a.jsonl"); |
| 171 | + const readdirSpy = vi.spyOn(fsPromises, "readdir"); |
| 172 | + |
| 173 | + await listSessionFilesForAgent("main"); |
| 174 | + writeSession("b.jsonl"); |
| 175 | + emitSessionTranscriptUpdate({ sessionFile: path.join(sessionsDir, "b.jsonl") }); |
| 176 | + const afterInvalidate = await listSessionFilesForAgent("main"); |
| 177 | + |
| 178 | + expect(readdirSpy).toHaveBeenCalledTimes(2); |
| 179 | + expect(baseNames(afterInvalidate)).toEqual(["a.jsonl", "b.jsonl"]); |
| 180 | + }); |
| 181 | + |
| 182 | + it("does not cache a transient scan failure and retries on the next call", async () => { |
| 183 | + writeSession("a.jsonl"); |
| 184 | + const transient = Object.assign(new Error("nfs blip"), { code: "EIO" }); |
| 185 | + const readdirSpy = vi.spyOn(fsPromises, "readdir").mockRejectedValueOnce(transient); |
| 186 | + |
| 187 | + // A transient READDIR failure is surfaced to this caller as empty... |
| 188 | + const failed = await listSessionFilesForAgent("main"); |
| 189 | + expect(failed).toEqual([]); |
| 190 | + // ...but must not be cached: the next call within the TTL re-scans and |
| 191 | + // recovers the real listing instead of serving the false-empty snapshot. |
| 192 | + const recovered = await listSessionFilesForAgent("main"); |
| 193 | + expect(baseNames(recovered)).toEqual(["a.jsonl"]); |
| 194 | + expect(readdirSpy).toHaveBeenCalledTimes(2); |
| 195 | + }); |
| 196 | + |
| 197 | + it("caches an absent sessions dir as empty without re-scanning", async () => { |
| 198 | + fsSync.rmSync(sessionsDir, { recursive: true, force: true }); |
| 199 | + const readdirSpy = vi.spyOn(fsPromises, "readdir"); |
| 200 | + |
| 201 | + const first = await listSessionFilesForAgent("main"); |
| 202 | + const second = await listSessionFilesForAgent("main"); |
| 203 | + |
| 204 | + expect(first).toEqual([]); |
| 205 | + expect(second).toEqual([]); |
| 206 | + expect(readdirSpy).toHaveBeenCalledTimes(1); |
| 207 | + }); |
| 208 | +}); |
| 209 | + |
77 | 210 | describe("sessionPathForFile", () => { |
78 | 211 | it("includes the owning agent id when the transcript lives under an agent sessions dir", () => { |
79 | 212 | const absPath = path.join( |
|
0 commit comments