cache.spec.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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, {
  19. Session,
  20. SessionId,
  21. SessionLogOffset,
  22. SessionSeq,
  23. } from '@deepseek-ai/dsh-session'
  24. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  25. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  26. import type { ProjectionDefinition } from '@deepseek-ai/dsh-session-projection'
  27. import Storage from '@deepseek-ai/dsh-storage'
  28. import {
  29. apply as storageJsonApply, Config as storageJsonConfig, inject as storageJsonInject, name as storageJsonName,
  30. } from '@deepseek-ai/dsh-storage-json'
  31. import {
  32. apply as storageDomainApply, Config as storageDomainConfig, inject as storageDomainInject, name as storageDomainName,
  33. } from '@deepseek-ai/dsh-storage-domain'
  34. import SessionProjectionCache from '../src/index.ts'
  35. import { checkpointRecord, projectionCacheDomainSpec } from '../src/spec.ts'
  36. import type { CheckpointRecord } from '../src/spec.ts'
  37. declare module '@deepseek-ai/dsh-session-projection/types' {
  38. interface SessionProjectionStateMap {
  39. 'cache-test/marks': MarksState
  40. 'cache-test/marks2': Map<string, string>
  41. 'cache-test/count': number
  42. 'cache-test/secret': string
  43. 'cache-test/marks3': MarksState
  44. }
  45. interface SessionProjectionMap {
  46. 'cache-test/marks': { marks: string[] }
  47. 'cache-test/marks3': { marks: string[] }
  48. }
  49. }
  50. declare module '@deepseek-ai/dsh-session/types' {
  51. interface SessionEventMap {
  52. 'cache-test/mark': { marks: string[] }
  53. }
  54. interface OutOfBandSessionEventMap {
  55. 'cache-test/mark': true
  56. }
  57. }
  58. type MarksState = { marks: string[] } | null
  59. const marksUnit = (stateVersion = 1) => ({
  60. key: 'cache-test/marks',
  61. stateSchema: z.object({ marks: z.array(z.string()) }).nullable(),
  62. init: () => null,
  63. apply: (state, event) => (event.type === 'cache-test/mark' ? (event).data : state),
  64. wire: {
  65. viewSchema: z.object({ marks: z.array(z.string()) }),
  66. view: state => state ?? { marks: [] },
  67. },
  68. stateVersion,
  69. }) satisfies ProjectionDefinition<'cache-test/marks', MarksState>
  70. const marks3Unit = {
  71. key: 'cache-test/marks3',
  72. stateSchema: z.object({ marks: z.array(z.string()) }).nullable(),
  73. init: () => null,
  74. apply: state => state,
  75. wire: {
  76. viewSchema: z.object({ marks: z.array(z.string()) }),
  77. view: state => state ?? { marks: [] },
  78. },
  79. stateVersion: 1,
  80. } satisfies ProjectionDefinition<'cache-test/marks3', MarksState>
  81. const secretUnit = {
  82. key: 'cache-test/secret',
  83. stateSchema: z.string(),
  84. init: () => '',
  85. apply: state => state,
  86. stateVersion: 1,
  87. } satisfies ProjectionDefinition<'cache-test/secret', string>
  88. /** One session's record document on the per-record medium. */
  89. const recordPath = (root: string, id: Session['id']): string =>
  90. join(root, projectionCacheDomainSpec.name, 'sessions', `${String(id)}.json`)
  91. /** Header shape for cachedSnapshot calls. */
  92. const headerOf = (id: SessionId, createdAt = 0, cwd?: string) =>
  93. ({ version: 0, id, createdAt, isSeeded: false, ...cwd === undefined ? {} : { cwd } })
  94. interface HarnessOptions {
  95. root?: string
  96. config?: { writeEveryEvents: number; writeIntervalMs: number }
  97. stateVersion?: number
  98. }
  99. const contexts: Context[] = []
  100. const roots: string[] = []
  101. async function harness(options: HarnessOptions = {}) {
  102. const root = options.root ?? await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  103. roots.push(root)
  104. const ctx = new Context()
  105. contexts.push(ctx)
  106. // The cache opens its domain through the storage stack; the json backend
  107. // lands the per-record tree under this tmp root.
  108. await ctx.plugin(Storage)
  109. await ctx.plugin({ name: storageJsonName, inject: storageJsonInject, apply: storageJsonApply, Config: storageJsonConfig }, { root })
  110. await ctx.plugin({ name: storageDomainName, inject: storageDomainInject, apply: storageDomainApply, Config: storageDomainConfig }, { backend: 'json' })
  111. await ctx.plugin(SessionStore)
  112. await ctx.plugin(SessionProjectionRegistry)
  113. ctx.sessionProjections.register(marksUnit(options.stateVersion))
  114. const fiber = await ctx.plugin(SessionProjectionCache, options.config ?? { writeEveryEvents: 100, writeIntervalMs: 60_000 })
  115. return { ctx, root, fiber, cache: ctx.sessionProjectionCache }
  116. }
  117. const mark = (session: Session, marks: string[]): SessionEvent =>
  118. session.append('cache-test/mark', { marks })
  119. const endTurn = (session: Session): SessionEvent =>
  120. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  121. /** The stored record for one session id (undefined = absent or unreadable). */
  122. async function storedRecord(root: string, id: Session['id']): Promise<CheckpointRecord | undefined> {
  123. try {
  124. const document = JSON.parse(await readFile(recordPath(root, id), 'utf8')) as { record: unknown }
  125. return checkpointRecord.parse(document.record)
  126. } catch {
  127. return undefined
  128. }
  129. }
  130. /** The stored rows for one session id (undefined = absent or unreadable). */
  131. async function storedRows(root: string, id: Session['id']): Promise<CheckpointRecord['rows'] | undefined> {
  132. return (await storedRecord(root, id))?.rows
  133. }
  134. /** Pre-seed one session's record document with a stored checkpoint record. */
  135. async function seedRecord(
  136. root: string,
  137. id: string,
  138. rows: CheckpointRecord['rows'],
  139. identity: CheckpointRecord['identity'] = {
  140. createdAt: 0,
  141. isSeeded: false,
  142. inheritedEventCount: SessionLogOffset(0),
  143. },
  144. ): Promise<void> {
  145. const path = recordPath(root, SessionId(id))
  146. await mkdir(dirname(path), { recursive: true })
  147. await writeFile(path, JSON.stringify({ version: projectionCacheDomainSpec.version, record: { identity, rows } }))
  148. }
  149. afterEach(async () => {
  150. vi.useRealTimers()
  151. await Promise.all(contexts.splice(0).map(ctx => ctx.fiber.dispose()))
  152. await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 })))
  153. })
  154. describe('SessionProjectionCache write policy', () => {
  155. it('writes a durable checkpoint at turn/end (mandatory point)', async () => {
  156. const { ctx, root } = await harness()
  157. const session = ctx.sessions.create(SessionId('turn-end'))
  158. mark(session, ['a'])
  159. // Creation already wrote the init cut; the mark is throttled, so the
  160. // stored row is still the creation-time cut (no marks folded).
  161. await vi.waitFor(async () => {
  162. expect((await storedRows(root, session.id))?.['cache-test/marks']?.seq).toBe(-1)
  163. }, { timeout: 5_000 })
  164. const end = endTurn(session)
  165. await vi.waitFor(async () => {
  166. expect((await storedRows(root, session.id))?.['cache-test/marks'])
  167. .toEqual({ ver: 1, seq: end.seq, val: { marks: ['a'] } })
  168. }, { timeout: 5_000 })
  169. })
  170. it('writes a checkpoint at session creation, capturing the seed-derived cut', async () => {
  171. const { ctx, root } = await harness()
  172. // A forked child seeded with its ancestor's title-like event: no
  173. // conversation follows, yet the creation write must capture the fold so
  174. // a crash or a live-held fork still lists the derived value.
  175. const session = ctx.sessions.create(SessionId('seeded'), {
  176. seed: [{ type: 'cache-test/mark', seq: 0, time: 1, data: { marks: ['seed'] } }] as SessionEvent[],
  177. })
  178. await vi.waitFor(async () => {
  179. expect((await storedRows(root, session.id))?.['cache-test/marks']?.val)
  180. .toEqual({ marks: ['seed'] })
  181. }, { timeout: 5_000 })
  182. })
  183. it('writes at session disposal (detach, the live-to-cold moment)', async () => {
  184. const { ctx, root } = await harness()
  185. // Sessions dispose with their owning fiber: create in a child plugin.
  186. let session: Session | undefined
  187. const owner = await ctx.plugin(Object.assign((inner: Context) => {
  188. session = inner.sessions.create(SessionId('detach'))
  189. }, { inject: ['sessions'] }))
  190. if (session === undefined) throw new Error('session was not created')
  191. mark(session, ['live'])
  192. await owner.dispose()
  193. const detached = session
  194. await vi.waitFor(async () => {
  195. expect((await storedRows(root, detached.id))?.['cache-test/marks']?.val).toEqual({ marks: ['live'] })
  196. }, { timeout: 5_000 })
  197. })
  198. it('flushes when the in-turn event count reaches the configured threshold', async () => {
  199. const { ctx, root } = await harness({ config: { writeEveryEvents: 3, writeIntervalMs: 60_000 } })
  200. const session = ctx.sessions.create(SessionId('count'))
  201. mark(session, ['1'])
  202. mark(session, ['2'])
  203. await vi.waitFor(async () => {
  204. expect((await storedRows(root, session.id))?.['cache-test/marks']?.seq).toBe(-1) // still the creation cut
  205. }, { timeout: 5_000 })
  206. mark(session, ['3'])
  207. await vi.waitFor(async () => {
  208. expect((await storedRows(root, session.id))?.['cache-test/marks']?.val).toEqual({ marks: ['3'] })
  209. }, { timeout: 5_000 })
  210. })
  211. it('flushes on the configured interval when the count threshold is not reached', async () => {
  212. const { ctx, cache } = await harness({ config: { writeEveryEvents: 100, writeIntervalMs: 20 } })
  213. const write = vi.spyOn(cache, 'write').mockResolvedValue()
  214. vi.useFakeTimers()
  215. const session = ctx.sessions.create(SessionId('interval'))
  216. write.mockClear()
  217. mark(session, ['slow'])
  218. await vi.advanceTimersByTimeAsync(19)
  219. expect(write).not.toHaveBeenCalled()
  220. await vi.advanceTimersByTimeAsync(1)
  221. expect(write).toHaveBeenCalledExactlyOnceWith(session)
  222. })
  223. it('write() on a never-dirty session checkpoints directly and rejects a non-JSON unit state', async () => {
  224. const { ctx, root } = await harness()
  225. // Never dirtied: no events — write() still lands the init-derived cut.
  226. const clean = ctx.sessions.create(SessionId('clean-write'))
  227. await ctx.sessionProjectionCache.write(clean)
  228. expect((await storedRows(root, clean.id))?.['cache-test/marks']).toEqual({ ver: 1, seq: -1, val: null })
  229. // A unit whose state violates the plain-JSON contract fails the write loud.
  230. ctx.sessionProjections.register({
  231. key: 'cache-test/marks2',
  232. stateSchema: z.custom<Map<string, string>>(() => true),
  233. init: () => new Map<string, string>(),
  234. apply: state => state,
  235. stateVersion: 1,
  236. })
  237. await expect(ctx.sessionProjectionCache.write(clean)).rejects.toThrow('not losslessly JSON-serializable')
  238. })
  239. it('plugin disposal clears armed interval timers and leaves cleaned sessions alone', async () => {
  240. vi.useFakeTimers()
  241. const { ctx, root, fiber } = await harness({ config: { writeEveryEvents: 100, writeIntervalMs: 5000 } })
  242. const armed = ctx.sessions.create(SessionId('armed'))
  243. const cleaned = ctx.sessions.create(SessionId('cleaned'))
  244. mark(armed, ['pending']) // timer armed, no write yet
  245. mark(cleaned, ['done'])
  246. endTurn(cleaned) // mandatory write; markClean leaves {pending: 0, timer: undefined} in the map
  247. await vi.advanceTimersByTimeAsync(0)
  248. await fiber.dispose()
  249. // The armed timer died with the plugin: advancing time writes nothing.
  250. await vi.advanceTimersByTimeAsync(10_000)
  251. // Only the creation cut exists: the armed mark never wrote.
  252. expect((await storedRows(root, armed.id))?.['cache-test/marks']?.seq).toBe(-1)
  253. })
  254. it('contains a durable write failure: logs a warning, event path unharmed, next write self-heals', async () => {
  255. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  256. roots.push(root)
  257. const ctx = new Context()
  258. contexts.push(ctx)
  259. await ctx.plugin(Storage)
  260. await ctx.plugin({ name: storageJsonName, inject: storageJsonInject, apply: storageJsonApply, Config: storageJsonConfig }, { root })
  261. await ctx.plugin({ name: storageDomainName, inject: storageDomainInject, apply: storageDomainApply, Config: storageDomainConfig }, { backend: 'json' })
  262. await ctx.plugin(SessionStore)
  263. await ctx.plugin(SessionProjectionRegistry)
  264. ctx.sessionProjections.register(marksUnit())
  265. await ctx.plugin(SessionProjectionCache, { writeEveryEvents: 100, writeIntervalMs: 60_000 })
  266. const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => {})
  267. // A directory where the record document must land makes the atomic
  268. // rename fail — including the creation write, so no row ever lands.
  269. const blocker = recordPath(root, SessionId('fail-soft'))
  270. await mkdir(blocker, { recursive: true })
  271. const session = ctx.sessions.create(SessionId('fail-soft'))
  272. mark(session, ['x'])
  273. endTurn(session)
  274. // The failed creation/turn-end writes are fire-and-forget: wait for the
  275. // warn (the write actually failed), then assert no row landed — the
  276. // property under test is that a failed write leaves no partial row.
  277. await vi.waitFor(() => {
  278. expect(warn).toHaveBeenCalledWith(expect.stringContaining('turn/end write for "fail-soft" failed'))
  279. }, { timeout: 5_000 })
  280. await vi.waitFor(async () => {
  281. expect(await storedRows(root, session.id)).toBeUndefined()
  282. }, { timeout: 5_000 })
  283. // Self-heal: once the blocker clears, the next mandatory point writes.
  284. await rm(recordPath(root, session.id), { recursive: true })
  285. mark(session, ['y'])
  286. endTurn(session)
  287. await vi.waitFor(async () => {
  288. expect((await storedRows(root, session.id))?.['cache-test/marks']?.val).toEqual({ marks: ['y'] })
  289. }, { timeout: 5_000 })
  290. })
  291. })
  292. describe('SessionProjectionCache listing read', () => {
  293. it('refuses a checkpoint created for a different inherited cut', async () => {
  294. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  295. roots.push(root)
  296. const id = SessionId('cut-identity')
  297. await seedRecord(
  298. root,
  299. id,
  300. { 'cache-test/marks': { ver: 1, seq: SessionSeq(1), val: { marks: ['seed'] } } },
  301. {
  302. createdAt: 0,
  303. isSeeded: true,
  304. inheritedEventCount: SessionLogOffset(2),
  305. },
  306. )
  307. const { cache } = await harness({ root })
  308. const seededHeader = { ...headerOf(id), isSeeded: true }
  309. expect(cache.cachedSnapshot(seededHeader, SessionLogOffset(2))?.values['cache-test/marks'])
  310. .toEqual({ marks: ['seed'] })
  311. expect(cache.cachedSnapshot(seededHeader, SessionLogOffset(1))).toBeUndefined()
  312. expect(() => cache.cachedSnapshot(headerOf(id), SessionLogOffset(1)))
  313. .toThrow('unseeded projection-cache identity inherited event count must be 0')
  314. })
  315. it('serves a creation-time checkpoint at the before-first-event cursor', async () => {
  316. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  317. roots.push(root)
  318. await seedRecord(root, 'before-first-event', {
  319. 'cache-test/marks': { ver: 1, seq: -1, val: null },
  320. })
  321. const { cache } = await harness({ root })
  322. expect(cache.cachedSnapshot(headerOf(SessionId('before-first-event')), SessionLogOffset(0)))
  323. .toEqual({ asOfSeq: -1, values: { 'cache-test/marks': { marks: [] } } })
  324. })
  325. it('keeps host-only checkpoint state out of cached wire snapshots', async () => {
  326. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  327. roots.push(root)
  328. await seedRecord(root, 'host-state', {
  329. 'cache-test/marks': { ver: 1, seq: SessionSeq(4), val: { marks: ['wire'] } },
  330. 'cache-test/secret': { ver: 1, seq: SessionSeq(4), val: 'private prompt text' },
  331. })
  332. const { ctx, cache } = await harness({ root })
  333. ctx.sessionProjections.register(secretUnit)
  334. const header = headerOf(SessionId('host-state'))
  335. expect(cache.cachedSnapshot(header, SessionLogOffset(0))).toEqual({
  336. asOfSeq: 4,
  337. values: { 'cache-test/marks': { marks: ['wire'] } },
  338. })
  339. expect(JSON.stringify(cache.cachedSnapshot(header, SessionLogOffset(0))))
  340. .not.toContain('private prompt text')
  341. })
  342. it('serves identity-matching rows with the cut watermark and refuses unrelated ones', async () => {
  343. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  344. roots.push(root)
  345. await seedRecord(root, 'listed', {
  346. 'cache-test/marks': { ver: 1, seq: SessionSeq(4), val: { marks: ['t'] } },
  347. })
  348. const { cache } = await harness({ root })
  349. const id = SessionId('listed')
  350. // Matching header: values plus the watermark the client seeds under.
  351. expect(cache.cachedSnapshot(headerOf(id), SessionLogOffset(0)))
  352. .toEqual({ asOfSeq: 4, values: { 'cache-test/marks': { marks: ['t'] } } })
  353. // A recreated id (different createdAt): the record is unrelated — no block.
  354. expect(cache.cachedSnapshot(headerOf(id, 777), SessionLogOffset(0))).toBeUndefined()
  355. // Unknown id: no block.
  356. expect(cache.cachedSnapshot(headerOf(SessionId('never-cached')), SessionLogOffset(0)))
  357. .toBeUndefined()
  358. })
  359. it('carries ONE cut across multiple served rows: the lowest watermark wins', async () => {
  360. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  361. roots.push(root)
  362. // Equal watermarks: whichever row is visited second cannot lower the cut,
  363. // so the one-cut fold sees both a lowering and a non-lowering row in
  364. // every iteration order.
  365. await seedRecord(root, 'multi-row', {
  366. 'cache-test/marks': { ver: 1, seq: SessionSeq(4), val: { marks: ['a'] } },
  367. 'cache-test/marks3': { ver: 1, seq: SessionSeq(4), val: { marks: ['b'] } },
  368. })
  369. const { ctx, cache } = await harness({ root })
  370. ctx.sessionProjections.register(marks3Unit)
  371. const block = cache.cachedSnapshot(headerOf(SessionId('multi-row')), SessionLogOffset(0))
  372. expect(block?.values).toEqual({
  373. 'cache-test/marks': { marks: ['a'] },
  374. 'cache-test/marks3': { marks: ['b'] },
  375. })
  376. expect(block?.asOfSeq).toBe(4)
  377. })
  378. it('returns undefined when the stored record is version-mismatched', async () => {
  379. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  380. roots.push(root)
  381. // A stale version-stamped document is discarded at open: absent record.
  382. const path = recordPath(root, SessionId('all-stale'))
  383. await mkdir(dirname(path), { recursive: true })
  384. await writeFile(path, JSON.stringify({
  385. version: projectionCacheDomainSpec.version + 1,
  386. record: {
  387. identity: { createdAt: 0, isSeeded: false, inheritedEventCount: 0 },
  388. rows: { 'cache-test/marks': { ver: 1, seq: 4, val: { marks: ['old'] } } },
  389. },
  390. }))
  391. const { cache } = await harness({ root })
  392. expect(cache.cachedSnapshot(headerOf(SessionId('all-stale')), SessionLogOffset(0)))
  393. .toBeUndefined()
  394. })
  395. it('serves a pre-lineage record (accepted old version) to an unseeded caller only', async () => {
  396. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  397. roots.push(root)
  398. // A document stamped with an accepted older version whose identity
  399. // predates the lineage fields: absent lineage reads as unseeded.
  400. const path = recordPath(root, SessionId('pre-lineage'))
  401. await mkdir(dirname(path), { recursive: true })
  402. await writeFile(path, JSON.stringify({
  403. version: 4,
  404. record: {
  405. identity: { createdAt: 0 },
  406. rows: { 'cache-test/marks': { ver: 1, seq: 4, val: { marks: ['kept'] } } },
  407. },
  408. }))
  409. const { cache } = await harness({ root })
  410. const id = SessionId('pre-lineage')
  411. // Unseeded caller: the absent lineage is exactly its identity — served.
  412. expect(cache.cachedSnapshot(headerOf(id), SessionLogOffset(0)))
  413. .toEqual({ asOfSeq: 4, values: { 'cache-test/marks': { marks: ['kept'] } } })
  414. // Seeded caller: the lineage-less record cannot vouch for the cut — refused.
  415. expect(cache.cachedSnapshot({ ...headerOf(id), isSeeded: true }, SessionLogOffset(2)))
  416. .toBeUndefined()
  417. })
  418. it('returns undefined when every stored row is version-mismatched', async () => {
  419. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  420. roots.push(root)
  421. // A current document whose rows all fail the live unit's stateVersion:
  422. // the listing view is empty, so no block is served.
  423. await seedRecord(root, 'row-stale', {
  424. 'cache-test/marks': { ver: 99, seq: SessionSeq(4), val: { marks: ['old'] } },
  425. })
  426. const { cache } = await harness({ root })
  427. expect(cache.cachedSnapshot(headerOf(SessionId('row-stale')), SessionLogOffset(0)))
  428. .toBeUndefined()
  429. })
  430. it('binds identity on cwd too: a matching cwd serves, a moved session does not', async () => {
  431. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  432. roots.push(root)
  433. await seedRecord(root, 'homed', {
  434. 'cache-test/marks': { ver: 1, seq: SessionSeq(2), val: { marks: ['w'] } },
  435. }, {
  436. createdAt: 0,
  437. cwd: '/work',
  438. isSeeded: false,
  439. inheritedEventCount: SessionLogOffset(0),
  440. })
  441. const { cache } = await harness({ root })
  442. const id = SessionId('homed')
  443. expect(cache.cachedSnapshot(headerOf(id, 0, '/work'), SessionLogOffset(0))?.values['cache-test/marks'])
  444. .toEqual({ marks: ['w'] })
  445. expect(cache.cachedSnapshot(headerOf(id, 0, '/elsewhere'), SessionLogOffset(0))).toBeUndefined()
  446. expect(cache.cachedSnapshot(headerOf(id, 0), SessionLogOffset(0))).toBeUndefined()
  447. })
  448. it('returns undefined for a malformed record document (refold from the log on the caller side)', async () => {
  449. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  450. roots.push(root)
  451. const path = recordPath(root, SessionId('malformed'))
  452. await mkdir(dirname(path), { recursive: true })
  453. await writeFile(path, 'not json at all')
  454. const { cache } = await harness({ root })
  455. expect(cache.cachedSnapshot(headerOf(SessionId('malformed')), SessionLogOffset(0)))
  456. .toBeUndefined()
  457. })
  458. })
  459. describe('SessionProjectionCache cold-read seeding', () => {
  460. /** One session's event log: turn/start, one mark per group, turn/end. */
  461. const storedLog = (marks: string[][]): SessionEvent[] => {
  462. const events: SessionEvent[] = [
  463. { type: 'turn/start', seq: SessionSeq(0), time: 0, data: { turn: 1 } },
  464. ]
  465. for (const m of marks) {
  466. events.push({
  467. type: 'cache-test/mark',
  468. seq: SessionSeq(events.length),
  469. time: events.length,
  470. data: { marks: m },
  471. })
  472. }
  473. events.push({
  474. type: 'turn/end',
  475. seq: SessionSeq(events.length),
  476. time: events.length,
  477. data: { turn: 1, reason: { kind: 'completed' } },
  478. })
  479. return events
  480. }
  481. it('hydratePrepared seeds from a matching row and retries from the exact log on a malformed one', async () => {
  482. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  483. roots.push(root)
  484. // Records land on disk before the domain opens, so the in-memory table
  485. // picks them up at init.
  486. await seedRecord(root, 'prepared-seeded', {
  487. 'cache-test/marks': { ver: 1, seq: SessionSeq(1), val: { marks: ['cached'] } },
  488. })
  489. await seedRecord(root, 'prepared-fallback', {
  490. 'cache-test/marks': { ver: 1, seq: SessionSeq(1), val: { marks: 'malformed' } },
  491. })
  492. const { cache } = await harness({ root })
  493. const events = storedLog([['fresh']])
  494. // A matching row hydrates the prepared Session without a persistence read.
  495. const seeded = headerOf(SessionId('prepared-seeded'))
  496. const seededSession = Session.create(seeded.id, events, seeded)
  497. expect(cache.hydratePrepared(seededSession, events)).toEqual({
  498. asOfSeq: 2,
  499. values: { 'cache-test/marks': { marks: ['cached'] } },
  500. })
  501. // A malformed row cannot seed the fold; hydration falls back to the
  502. // exact log so a valid Session stays readable.
  503. const fallback = headerOf(SessionId('prepared-fallback'))
  504. const fallbackSession = Session.create(fallback.id, events, fallback)
  505. expect(cache.hydratePrepared(fallbackSession, events)).toEqual({
  506. asOfSeq: 2,
  507. values: { 'cache-test/marks': { marks: ['fresh'] } },
  508. })
  509. // No row at all: hydrate from init over the exact log.
  510. const bare = headerOf(SessionId('prepared-bare'))
  511. const bareSession = Session.create(bare.id, events, bare)
  512. expect(cache.hydratePrepared(bareSession, events)).toEqual({
  513. asOfSeq: 2,
  514. values: { 'cache-test/marks': { marks: ['fresh'] } },
  515. })
  516. })
  517. it('coldSnapshot traverses the full log but applies only the events after each cached watermark', async () => {
  518. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  519. roots.push(root)
  520. // A cached row covering the prefix through seq 2 (three applies folded).
  521. await seedRecord(root, 'cold-snap', {
  522. 'cache-test/count': { ver: 1, seq: SessionSeq(2), val: 3 },
  523. }, {
  524. createdAt: 9,
  525. isSeeded: false,
  526. inheritedEventCount: SessionLogOffset(0),
  527. })
  528. const { cache, ctx } = await harness({ root })
  529. const apply = vi.fn((_state: number, _event: SessionEvent) => 1)
  530. ctx.sessionProjections.register({
  531. key: 'cache-test/count',
  532. stateSchema: z.number().int().nonnegative(),
  533. init: () => 0,
  534. apply,
  535. stateVersion: 1,
  536. } satisfies ProjectionDefinition<'cache-test/count', number>)
  537. const meta = headerOf(SessionId('cold-snap'), 9)
  538. const events = Array.from({ length: 5 }, (_, seq) => ({
  539. type: 'cache-test/mark', seq: SessionSeq(seq), time: seq, data: { marks: [`m${seq}`] },
  540. })) as SessionEvent[]
  541. const snapshot = cache.coldSnapshot(meta, SessionLogOffset(0), events)
  542. // The full log was traversed, but the fold applied only seqs 3 and 4.
  543. expect(apply).toHaveBeenCalledTimes(2)
  544. expect(apply.mock.calls.map(call => call[1].seq)).toEqual([3, 4])
  545. expect(snapshot.asOfSeq).toBe(4)
  546. // Host-only unit: folded but not served; the refreshed row is written
  547. // back (fail-soft, fire-and-forget) once the write lands.
  548. expect(Object.keys(snapshot.values)).not.toContain('cache-test/count')
  549. await vi.waitFor(async () => {
  550. expect((await storedRows(root, meta.id))?.['cache-test/count']?.seq).toBe(4)
  551. })
  552. // No cached row yet: the first cold read folds from init over the full
  553. // log and creates the cache row (the `?? {}` seed path).
  554. const fresh = headerOf(SessionId('cold-fresh'), 10)
  555. cache.coldSnapshot(fresh, SessionLogOffset(0), events)
  556. expect(apply).toHaveBeenCalledTimes(7) // 2 tail + 5 full
  557. await vi.waitFor(async () => {
  558. expect((await storedRows(root, fresh.id))?.['cache-test/count']?.seq).toBe(4)
  559. })
  560. })
  561. it('coldSnapshot write-back is fail-soft: a failed durable write logs and never throws', async () => {
  562. const root = await mkdtemp(join(tmpdir(), 'dsh-projcache-'))
  563. roots.push(root)
  564. const ctx = new Context()
  565. contexts.push(ctx)
  566. await ctx.plugin(Storage)
  567. await ctx.plugin({ name: storageJsonName, inject: storageJsonInject, apply: storageJsonApply, Config: storageJsonConfig }, { root })
  568. await ctx.plugin({ name: storageDomainName, inject: storageDomainInject, apply: storageDomainApply, Config: storageDomainConfig }, { backend: 'json' })
  569. await ctx.plugin(SessionStore)
  570. await ctx.plugin(SessionProjectionRegistry)
  571. ctx.sessionProjections.register(marksUnit())
  572. await ctx.plugin(SessionProjectionCache, { writeEveryEvents: 100, writeIntervalMs: 60_000 })
  573. const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => {})
  574. // A directory where the record document must land makes the write-back
  575. // fail; the cold read itself still succeeds and never throws.
  576. const meta = headerOf(SessionId('cold-fail'))
  577. await mkdir(recordPath(root, meta.id), { recursive: true })
  578. expect(ctx.sessionProjectionCache.coldSnapshot(meta, SessionLogOffset(0), [])).toBeDefined()
  579. // The failed write-back is fire-and-forget: poll for the warn instead of
  580. // assuming a fixed settle window (slow runners exceed it).
  581. await vi.waitFor(() => {
  582. expect(warn).toHaveBeenCalledWith(expect.stringContaining('cold-read write-back for "cold-fail" failed'))
  583. }, { timeout: 5_000 })
  584. })
  585. })