cache.spec.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. * SessionProjectionCache behavior: mandatory-point writes (turn/end, detach),
  3. * count/interval throttling between them, fail-soft durability (a failed
  4. * write logs and stays stale, never throws into the event path), and the
  5. * synchronous cached listing read. The durable medium is the
  6. * `session_projcache` storage domain in per-record layout: one
  7. * version-stamped document per session under the json backend root at
  8. * `<root>/session_projcache/sessions/<id>.json`. Reads never touch the
  9. * medium — they come from the domain's in-memory tables, which writes mutate
  10. * only after durability.
  11. */
  12. import { afterEach, describe, expect, it, vi } from 'vitest'
  13. import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
  14. import { tmpdir } from 'node:os'
  15. import { dirname, join } from 'node:path'
  16. import { Context } from '@deepseek-ai/cordis'
  17. import { z } from 'zod'
  18. import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session'
  19. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  20. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  21. import type { ProjectionDefinition } from '@deepseek-ai/dsh-session-projection'
  22. import Storage from '@deepseek-ai/dsh-storage'
  23. import {
  24. apply as storageJsonApply, Config as storageJsonConfig, inject as storageJsonInject, name as storageJsonName,
  25. } from '@deepseek-ai/dsh-storage-json'
  26. import {
  27. apply as storageDomainApply, Config as storageDomainConfig, inject as storageDomainInject, name as storageDomainName,
  28. } from '@deepseek-ai/dsh-storage-domain'
  29. import SessionProjectionCache from '../src/index.ts'
  30. import { checkpointRecord, projectionCacheDomainSpec } from '../src/spec.ts'
  31. import type { CheckpointRecord } from '../src/spec.ts'
  32. declare module '@deepseek-ai/dsh-session-projection/types' {
  33. interface SessionProjectionStateMap {
  34. 'cache-test/marks': MarksState
  35. 'cache-test/marks2': Map<string, string>
  36. 'cache-test/count': number
  37. }
  38. interface SessionProjectionMap {
  39. 'cache-test/marks': { marks: string[] }
  40. }
  41. }
  42. declare module '@deepseek-ai/dsh-session/types' {
  43. interface SessionEventMap {
  44. 'cache-test/mark': { marks: string[] }
  45. }
  46. interface OutOfBandSessionEventMap {
  47. 'cache-test/mark': true
  48. }
  49. }
  50. type MarksState = { marks: string[] } | null
  51. const marksUnit = (stateVersion = 1) => ({
  52. key: 'cache-test/marks',
  53. stateSchema: z.object({ marks: z.array(z.string()) }).nullable(),
  54. init: () => null,
  55. apply: (state, event) => (event.type === 'cache-test/mark' ? (event).data : state),
  56. wire: {
  57. viewSchema: z.object({ marks: z.array(z.string()) }),
  58. view: state => state ?? { marks: [] },
  59. },
  60. stateVersion,
  61. }) satisfies ProjectionDefinition<'cache-test/marks', MarksState>
  62. /** One session's record document on the per-record medium. */
  63. const recordPath = (root: string, id: Session['id']): string =>
  64. join(root, projectionCacheDomainSpec.name, 'sessions', `${String(id)}.json`)
  65. /** Header shape for cachedSnapshot calls. */
  66. const headerOf = (id: SessionId, createdAt = 0, cwd?: string) =>
  67. ({ version: 0, id, createdAt, ...cwd === undefined ? {} : { cwd } })
  68. interface HarnessOptions {
  69. root?: string
  70. config?: { writeEveryEvents: number; writeIntervalMs: number }
  71. stateVersion?: number
  72. }
  73. const contexts: Context[] = []
  74. const roots: string[] = []
  75. async function harness(options: HarnessOptions = {}) {
  76. const root = options.root ?? await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  77. roots.push(root)
  78. const ctx = new Context()
  79. contexts.push(ctx)
  80. // The cache opens its domain through the storage stack; the json backend
  81. // lands the per-record tree under this tmp root.
  82. await ctx.plugin(Storage)
  83. await ctx.plugin({ name: storageJsonName, inject: storageJsonInject, apply: storageJsonApply, Config: storageJsonConfig }, { root })
  84. await ctx.plugin({ name: storageDomainName, inject: storageDomainInject, apply: storageDomainApply, Config: storageDomainConfig }, { backend: 'json' })
  85. await ctx.plugin(SessionStore)
  86. await ctx.plugin(SessionProjectionRegistry)
  87. ctx.sessionProjections.register(marksUnit(options.stateVersion))
  88. const fiber = await ctx.plugin(SessionProjectionCache, options.config ?? { writeEveryEvents: 100, writeIntervalMs: 60_000 })
  89. return { ctx, root, fiber, cache: ctx.sessionProjectionCache }
  90. }
  91. const mark = (session: Session, marks: string[]): SessionEvent =>
  92. session.append('cache-test/mark', { marks })
  93. const endTurn = (session: Session): SessionEvent =>
  94. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  95. /** The stored record for one session id (undefined = absent or unreadable). */
  96. async function storedRecord(root: string, id: Session['id']): Promise<CheckpointRecord | undefined> {
  97. try {
  98. const document = JSON.parse(await readFile(recordPath(root, id), 'utf8')) as { record: unknown }
  99. return checkpointRecord.parse(document.record)
  100. } catch {
  101. return undefined
  102. }
  103. }
  104. /** The stored rows for one session id (undefined = absent or unreadable). */
  105. async function storedRows(root: string, id: Session['id']): Promise<CheckpointRecord['rows'] | undefined> {
  106. return (await storedRecord(root, id))?.rows
  107. }
  108. /** Pre-seed one session's record document with a stored checkpoint record. */
  109. async function seedRecord(
  110. root: string,
  111. id: string,
  112. rows: CheckpointRecord['rows'],
  113. identity: CheckpointRecord['identity'] = { createdAt: 0 },
  114. ): Promise<void> {
  115. const path = recordPath(root, SessionId(id))
  116. await mkdir(dirname(path), { recursive: true })
  117. await writeFile(path, JSON.stringify({ version: projectionCacheDomainSpec.version, record: { identity, rows } }))
  118. }
  119. /** Wait until queued fail-soft writes (event-listener fire-and-forget over real fs I/O) drain. */
  120. const settle = () => new Promise(resolve => setTimeout(resolve, 40))
  121. afterEach(async () => {
  122. vi.useRealTimers()
  123. await Promise.all(contexts.splice(0).map(ctx => ctx.fiber.dispose()))
  124. await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 })))
  125. })
  126. describe('SessionProjectionCache write policy', () => {
  127. it('writes a durable checkpoint at turn/end (mandatory point)', async () => {
  128. const { ctx, root } = await harness()
  129. const session = ctx.sessions.create(SessionId('turn-end'))
  130. mark(session, ['a'])
  131. // Creation already wrote the init cut; the mark is throttled, so the
  132. // stored row is still the creation-time cut (no marks folded).
  133. await settle()
  134. expect((await storedRows(root, session.id))?.['cache-test/marks']?.seq).toBe(-1)
  135. const end = endTurn(session)
  136. await settle()
  137. const rows = await storedRows(root, session.id)
  138. expect(rows?.['cache-test/marks']).toEqual({ ver: 1, seq: end.seq, val: { marks: ['a'] } })
  139. })
  140. it('writes a checkpoint at session creation, capturing the seed-derived cut', async () => {
  141. const { ctx, root } = await harness()
  142. // A forked child seeded with its ancestor's title-like event: no
  143. // conversation follows, yet the creation write must capture the fold so
  144. // a crash or a live-held fork still lists the derived value.
  145. const session = ctx.sessions.create(SessionId('seeded'), {
  146. seed: [{ type: 'cache-test/mark', seq: 0, time: 1, data: { marks: ['seed'] } }] as SessionEvent[],
  147. })
  148. await settle()
  149. expect((await storedRows(root, session.id))?.['cache-test/marks']?.val)
  150. .toEqual({ marks: ['seed'] })
  151. })
  152. it('writes at session disposal (detach, the live-to-cold moment)', async () => {
  153. const { ctx, root } = await harness()
  154. // Sessions dispose with their owning fiber: create in a child plugin.
  155. let session: Session | undefined
  156. const owner = await ctx.plugin(Object.assign((inner: Context) => {
  157. session = inner.sessions.create(SessionId('detach'))
  158. }, { inject: ['sessions'] }))
  159. if (session === undefined) throw new Error('session was not created')
  160. mark(session, ['live'])
  161. await owner.dispose()
  162. await settle()
  163. expect((await storedRows(root, session.id))?.['cache-test/marks']?.val).toEqual({ marks: ['live'] })
  164. })
  165. it('flushes when the in-turn event count reaches the configured threshold', async () => {
  166. const { ctx, root } = await harness({ config: { writeEveryEvents: 3, writeIntervalMs: 60_000 } })
  167. const session = ctx.sessions.create(SessionId('count'))
  168. mark(session, ['1'])
  169. mark(session, ['2'])
  170. await settle()
  171. expect((await storedRows(root, session.id))?.['cache-test/marks']?.seq).toBe(-1) // still the creation cut
  172. mark(session, ['3'])
  173. await settle()
  174. expect((await storedRows(root, session.id))?.['cache-test/marks']?.val).toEqual({ marks: ['3'] })
  175. })
  176. it('flushes on the configured interval when the count threshold is not reached', async () => {
  177. const { ctx, root } = await harness({ config: { writeEveryEvents: 100, writeIntervalMs: 20 } })
  178. const session = ctx.sessions.create(SessionId('interval'))
  179. mark(session, ['slow'])
  180. await new Promise(resolve => setTimeout(resolve, 10)) // before the interval
  181. expect((await storedRows(root, session.id))?.['cache-test/marks']?.seq).toBe(-1) // still the creation cut
  182. await settle() // past the interval; the fire-and-forget write lands
  183. expect((await storedRows(root, session.id))?.['cache-test/marks']?.val).toEqual({ marks: ['slow'] })
  184. })
  185. it('write() on a never-dirty session checkpoints directly and rejects a non-JSON unit state', async () => {
  186. const { ctx, root } = await harness()
  187. // Never dirtied: no events — write() still lands the init-derived cut.
  188. const clean = ctx.sessions.create(SessionId('clean-write'))
  189. await ctx.sessionProjectionCache.write(clean)
  190. expect((await storedRows(root, clean.id))?.['cache-test/marks']).toEqual({ ver: 1, seq: -1, val: null })
  191. // A unit whose state violates the plain-JSON contract fails the write loud.
  192. ctx.sessionProjections.register({
  193. key: 'cache-test/marks2',
  194. stateSchema: z.custom<Map<string, string>>(() => true),
  195. init: () => new Map<string, string>(),
  196. apply: state => state,
  197. stateVersion: 1,
  198. })
  199. await expect(ctx.sessionProjectionCache.write(clean)).rejects.toThrow('not losslessly JSON-serializable')
  200. })
  201. it('plugin disposal clears armed interval timers and leaves cleaned sessions alone', async () => {
  202. vi.useFakeTimers()
  203. const { ctx, root, fiber } = await harness({ config: { writeEveryEvents: 100, writeIntervalMs: 5000 } })
  204. const armed = ctx.sessions.create(SessionId('armed'))
  205. const cleaned = ctx.sessions.create(SessionId('cleaned'))
  206. mark(armed, ['pending']) // timer armed, no write yet
  207. mark(cleaned, ['done'])
  208. endTurn(cleaned) // mandatory write; markClean leaves {pending: 0, timer: undefined} in the map
  209. await vi.advanceTimersByTimeAsync(0)
  210. await fiber.dispose()
  211. // The armed timer died with the plugin: advancing time writes nothing.
  212. await vi.advanceTimersByTimeAsync(10_000)
  213. // Only the creation cut exists: the armed mark never wrote.
  214. expect((await storedRows(root, armed.id))?.['cache-test/marks']?.seq).toBe(-1)
  215. })
  216. it('contains a durable write failure: logs a warning, event path unharmed, next write self-heals', async () => {
  217. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  218. roots.push(root)
  219. const ctx = new Context()
  220. contexts.push(ctx)
  221. await ctx.plugin(Storage)
  222. await ctx.plugin({ name: storageJsonName, inject: storageJsonInject, apply: storageJsonApply, Config: storageJsonConfig }, { root })
  223. await ctx.plugin({ name: storageDomainName, inject: storageDomainInject, apply: storageDomainApply, Config: storageDomainConfig }, { backend: 'json' })
  224. await ctx.plugin(SessionStore)
  225. await ctx.plugin(SessionProjectionRegistry)
  226. ctx.sessionProjections.register(marksUnit())
  227. await ctx.plugin(SessionProjectionCache, { writeEveryEvents: 100, writeIntervalMs: 60_000 })
  228. const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => {})
  229. // A directory where the record document must land makes the atomic
  230. // rename fail — including the creation write, so no row ever lands.
  231. const blocker = recordPath(root, SessionId('fail-soft'))
  232. await mkdir(blocker, { recursive: true })
  233. const session = ctx.sessions.create(SessionId('fail-soft'))
  234. mark(session, ['x'])
  235. endTurn(session)
  236. await settle()
  237. expect(await storedRows(root, session.id)).toBeUndefined()
  238. expect(warn).toHaveBeenCalledWith(expect.stringContaining('turn/end write for "fail-soft" failed'))
  239. // Self-heal: once the blocker clears, the next mandatory point writes.
  240. await rm(recordPath(root, session.id), { recursive: true })
  241. mark(session, ['y'])
  242. endTurn(session)
  243. await settle()
  244. expect((await storedRows(root, session.id))?.['cache-test/marks']?.val).toEqual({ marks: ['y'] })
  245. })
  246. })
  247. describe('SessionProjectionCache listing read', () => {
  248. it('serves identity-matching rows with the cut watermark and refuses unrelated ones', async () => {
  249. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  250. roots.push(root)
  251. await seedRecord(root, 'listed', { 'cache-test/marks': { ver: 1, seq: 4, val: { marks: ['t'] } } })
  252. const { cache } = await harness({ root })
  253. const id = SessionId('listed')
  254. // Matching header: values plus the watermark the client seeds under.
  255. expect(cache.cachedSnapshot(headerOf(id))).toEqual({ asOfSeq: 4, values: { 'cache-test/marks': { marks: ['t'] } } })
  256. // A recreated id (different createdAt): the record is unrelated — no block.
  257. expect(cache.cachedSnapshot(headerOf(id, 777))).toBeUndefined()
  258. // Unknown id: no block.
  259. expect(cache.cachedSnapshot(headerOf(SessionId('never-cached')))).toBeUndefined()
  260. })
  261. it('returns undefined when the stored record is version-mismatched', async () => {
  262. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  263. roots.push(root)
  264. // A stale version-stamped document is discarded at open: absent record.
  265. const path = recordPath(root, SessionId('all-stale'))
  266. await mkdir(dirname(path), { recursive: true })
  267. await writeFile(path, JSON.stringify({
  268. version: projectionCacheDomainSpec.version + 1,
  269. record: { identity: { createdAt: 0 }, rows: { 'cache-test/marks': { ver: 1, seq: 4, val: { marks: ['old'] } } } },
  270. }))
  271. const { cache } = await harness({ root })
  272. expect(cache.cachedSnapshot(headerOf(SessionId('all-stale')))).toBeUndefined()
  273. })
  274. it('returns undefined when every stored row is version-mismatched', async () => {
  275. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  276. roots.push(root)
  277. // A current document whose rows all fail the live unit's stateVersion:
  278. // the listing view is empty, so no block is served.
  279. await seedRecord(root, 'row-stale', { 'cache-test/marks': { ver: 99, seq: 4, val: { marks: ['old'] } } })
  280. const { cache } = await harness({ root })
  281. expect(cache.cachedSnapshot(headerOf(SessionId('row-stale')))).toBeUndefined()
  282. })
  283. it('binds identity on cwd too: a matching cwd serves, a moved session does not', async () => {
  284. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  285. roots.push(root)
  286. await seedRecord(root, 'homed', { 'cache-test/marks': { ver: 1, seq: 2, val: { marks: ['w'] } } }, { createdAt: 0, cwd: '/work' })
  287. const { cache } = await harness({ root })
  288. const id = SessionId('homed')
  289. expect(cache.cachedSnapshot(headerOf(id, 0, '/work'))?.values['cache-test/marks']).toEqual({ marks: ['w'] })
  290. expect(cache.cachedSnapshot(headerOf(id, 0, '/elsewhere'))).toBeUndefined()
  291. expect(cache.cachedSnapshot(headerOf(id, 0))).toBeUndefined()
  292. })
  293. it('returns undefined for a malformed record document (refold from the log on the caller side)', async () => {
  294. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  295. roots.push(root)
  296. const path = recordPath(root, SessionId('malformed'))
  297. await mkdir(dirname(path), { recursive: true })
  298. await writeFile(path, 'not json at all')
  299. const { cache } = await harness({ root })
  300. expect(cache.cachedSnapshot(headerOf(SessionId('malformed')))).toBeUndefined()
  301. })
  302. })
  303. describe('SessionProjectionCache cold-read seeding', () => {
  304. /** One session's event log: turn/start, one mark per group, turn/end. */
  305. const storedLog = (marks: string[][]): SessionEvent[] => {
  306. const events: SessionEvent[] = [
  307. { type: 'turn/start', seq: 0, time: 0, data: { turn: 1 } },
  308. ]
  309. for (const m of marks) {
  310. events.push({ type: 'cache-test/mark', seq: events.length, time: events.length, data: { marks: m } })
  311. }
  312. events.push({ type: 'turn/end', seq: events.length, time: events.length, data: { turn: 1, reason: { kind: 'completed' } } })
  313. return events
  314. }
  315. it('hydratePrepared seeds from a matching row and retries from the exact log on a malformed one', async () => {
  316. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  317. roots.push(root)
  318. // Records land on disk before the domain opens, so the in-memory table
  319. // picks them up at init.
  320. await seedRecord(root, 'prepared-seeded', {
  321. 'cache-test/marks': { ver: 1, seq: 1, val: { marks: ['cached'] } },
  322. })
  323. await seedRecord(root, 'prepared-fallback', {
  324. 'cache-test/marks': { ver: 1, seq: 1, val: { marks: 'malformed' } },
  325. })
  326. const { cache } = await harness({ root })
  327. const events = storedLog([['fresh']])
  328. // A matching row hydrates the prepared Session without a persistence read.
  329. const seeded = headerOf(SessionId('prepared-seeded'))
  330. const seededSession = Session.create(seeded.id, events, seeded)
  331. expect(cache.hydratePrepared(seededSession, seeded, events)).toEqual({
  332. asOfSeq: 2,
  333. values: { 'cache-test/marks': { marks: ['cached'] } },
  334. })
  335. // A malformed row cannot seed the fold; hydration falls back to the
  336. // exact log so a valid Session stays readable.
  337. const fallback = headerOf(SessionId('prepared-fallback'))
  338. const fallbackSession = Session.create(fallback.id, events, fallback)
  339. expect(cache.hydratePrepared(fallbackSession, fallback, events)).toEqual({
  340. asOfSeq: 2,
  341. values: { 'cache-test/marks': { marks: ['fresh'] } },
  342. })
  343. // No row at all: hydrate from init over the exact log.
  344. const bare = headerOf(SessionId('prepared-bare'))
  345. const bareSession = Session.create(bare.id, events, bare)
  346. expect(cache.hydratePrepared(bareSession, bare, events)).toEqual({
  347. asOfSeq: 2,
  348. values: { 'cache-test/marks': { marks: ['fresh'] } },
  349. })
  350. })
  351. it('coldSnapshot traverses the full log but applies only the events after each cached watermark', async () => {
  352. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  353. roots.push(root)
  354. // A cached row covering the prefix through seq 2 (three applies folded).
  355. await seedRecord(root, 'cold-snap', {
  356. 'cache-test/count': { ver: 1, seq: 2, val: 3 },
  357. }, { createdAt: 9 })
  358. const { cache, ctx } = await harness({ root })
  359. const apply = vi.fn((_state: number, _event: SessionEvent) => 1)
  360. ctx.sessionProjections.register({
  361. key: 'cache-test/count',
  362. stateSchema: z.number().int().nonnegative(),
  363. init: () => 0,
  364. apply,
  365. stateVersion: 1,
  366. } satisfies ProjectionDefinition<'cache-test/count', number>)
  367. const meta = headerOf(SessionId('cold-snap'), 9)
  368. const events = Array.from({ length: 5 }, (_, seq) => ({
  369. type: 'cache-test/mark', seq, time: seq, data: { marks: [`m${seq}`] },
  370. })) as SessionEvent[]
  371. const snapshot = cache.coldSnapshot(meta, events)
  372. // The full log was traversed, but the fold applied only seqs 3 and 4.
  373. expect(apply).toHaveBeenCalledTimes(2)
  374. expect(apply.mock.calls.map(call => call[1].seq)).toEqual([3, 4])
  375. expect(snapshot.asOfSeq).toBe(4)
  376. // Host-only unit: folded but not served; the refreshed row is written
  377. // back (fail-soft, fire-and-forget) once the write lands.
  378. expect(Object.keys(snapshot.values)).not.toContain('cache-test/count')
  379. await settle()
  380. expect((await storedRows(root, meta.id))?.['cache-test/count']?.seq).toBe(4)
  381. // No cached row yet: the first cold read folds from init over the full
  382. // log and creates the cache row (the `?? {}` seed path).
  383. const fresh = headerOf(SessionId('cold-fresh'), 10)
  384. cache.coldSnapshot(fresh, events)
  385. expect(apply).toHaveBeenCalledTimes(7) // 2 tail + 5 full
  386. await settle()
  387. expect((await storedRows(root, fresh.id))?.['cache-test/count']?.seq).toBe(4)
  388. })
  389. it('coldSnapshot write-back is fail-soft: a failed durable write logs and never throws', async () => {
  390. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  391. roots.push(root)
  392. const ctx = new Context()
  393. contexts.push(ctx)
  394. await ctx.plugin(Storage)
  395. await ctx.plugin({ name: storageJsonName, inject: storageJsonInject, apply: storageJsonApply, Config: storageJsonConfig }, { root })
  396. await ctx.plugin({ name: storageDomainName, inject: storageDomainInject, apply: storageDomainApply, Config: storageDomainConfig }, { backend: 'json' })
  397. await ctx.plugin(SessionStore)
  398. await ctx.plugin(SessionProjectionRegistry)
  399. ctx.sessionProjections.register(marksUnit())
  400. await ctx.plugin(SessionProjectionCache, { writeEveryEvents: 100, writeIntervalMs: 60_000 })
  401. const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => {})
  402. // A directory where the record document must land makes the write-back
  403. // fail; the cold read itself still succeeds and never throws.
  404. const meta = headerOf(SessionId('cold-fail'))
  405. await mkdir(recordPath(root, meta.id), { recursive: true })
  406. expect(ctx.sessionProjectionCache.coldSnapshot(meta, [])).toBeDefined()
  407. await settle()
  408. expect(warn).toHaveBeenCalledWith(expect.stringContaining('cold-read write-back for "cold-fail" failed'))
  409. })
  410. })