1
0

cache.spec.ts 32 KB

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