coordinator-contract.ts 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. import { createUserMessage } from '@deepseek-ai/dsh-llm'
  2. /**
  3. * Shared write-path orchestration contract for backends using {@link PersistenceCoordinator}.
  4. * Unlike the public storage-semantics suite in `contract.ts`, it covers SessionStore event wiring,
  5. * lazy creation, fork seed persistence, four adoption/collision cases, crash-tail repair, reload,
  6. * flush, and disposal quiescence through public APIs rather than storage primitives.
  7. *
  8. * Each real backend supplies a shared storage scope and optional torn-tail injector; backend specs
  9. * retain only storage-mechanics tests, while these scenarios run once per backend.
  10. * @module @deepseek-ai/dsh-session-persistence/tests/coordinator-contract
  11. */
  12. import { describe, expect, it, vi } from 'vitest'
  13. import { Context, type Fiber } from '@deepseek-ai/cordis'
  14. import { scopeTarget } from '@deepseek-ai/dsh-scope'
  15. import SessionStore, { SESSION_FORMAT_VERSION, Session, SessionId } from '@deepseek-ai/dsh-session'
  16. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  17. import { meta, oneTurnLog, appendLog } from './contract.ts'
  18. /**
  19. * The backend-specific capabilities the orchestration suite needs beyond the
  20. * public service API. A fresh fixture is created per test (isolated storage);
  21. * the suite mounts/disposes backend instances on it and cleans it up at the end.
  22. */
  23. export interface CoordinatorFixture {
  24. /** Mount the real backend through `ctx.plugin` over shared storage and return only that fiber. */
  25. mount: (ctx: Context) => Promise<Fiber>
  26. /**
  27. * Inject a never-committed partial record after the durable region so `loadCore` reaches
  28. * `commitRepair`. Omit only when the backend structurally cannot produce torn tails.
  29. */
  30. corruptTail?: (id: SessionId, cwd: string | undefined) => Promise<void>
  31. /** Tear down the storage scope (remove the temp dir / file). */
  32. cleanup: () => Promise<void>
  33. }
  34. /** A constant absolute cwd; jsonl keys directories off it, memory/sqlite ignore it. */
  35. const WORK = '/w'
  36. const OTHER = '/other'
  37. /** Append a whole event log to a live session, event by event (drives session/event). */
  38. function send(session: Session, events: readonly SessionEvent[]): void {
  39. appendLog(session, events)
  40. }
  41. /** A valid persisted log from immediately before messages gained wrappers and identities. */
  42. function legacyMessageLog(): SessionEvent[] {
  43. return [
  44. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1 } },
  45. {
  46. type: 'user/message',
  47. seq: 1,
  48. time: 2,
  49. data: { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } },
  50. surfaceOp: 'append',
  51. },
  52. { type: 'step/start', seq: 2, time: 3, data: { turn: 1, step: 1 } },
  53. {
  54. type: 'assistant/message',
  55. seq: 3,
  56. time: 4,
  57. data: {
  58. turn: 1,
  59. step: 1,
  60. content: [{ type: 'tool-call', id: 'call-1', name: 'read', arguments: '{}' }],
  61. provenance: { provider: 'mock', model: 'mock' },
  62. },
  63. surfaceOp: 'append',
  64. },
  65. {
  66. type: 'tool/call',
  67. seq: 4,
  68. time: 5,
  69. data: { turn: 1, step: 1, callId: 'call-1', name: 'read', arguments: '{}' },
  70. },
  71. {
  72. type: 'tool/result',
  73. seq: 5,
  74. time: 6,
  75. data: {
  76. turn: 1,
  77. step: 1,
  78. callId: 'call-1',
  79. content: [{ type: 'text', text: 'full result' }],
  80. isError: false,
  81. },
  82. sourceEventSeqs: [4],
  83. surfaceOp: 'append',
  84. },
  85. {
  86. type: 'tool/result',
  87. seq: 6,
  88. time: 8,
  89. data: {
  90. turn: 1,
  91. step: 1,
  92. callId: 'call-1',
  93. content: [{ type: 'text', text: 'pruned' }],
  94. isError: false,
  95. },
  96. sourceEventSeqs: [5],
  97. surfaceOp: { op: 'replace', start: 5, end: 5 },
  98. },
  99. { type: 'step/end', seq: 7, time: 9, data: { turn: 1, step: 1 } },
  100. { type: 'turn/end', seq: 8, time: 10, data: { turn: 1, reason: { kind: 'completed' } } },
  101. ] as unknown as SessionEvent[]
  102. }
  103. /** A complete log in the durable event vocabulary of the react-loop refactor base. */
  104. function preReactLoopLog(): SessionEvent[] {
  105. const prompt = createUserMessage({
  106. content: [{ type: 'text', text: 'old prompt' }],
  107. source: { kind: 'user' },
  108. })
  109. const steering = createUserMessage({
  110. content: [{ type: 'text', text: 'old steering' }],
  111. source: { kind: 'user' },
  112. })
  113. return [
  114. {
  115. type: 'turn/start', seq: 0, time: 1,
  116. data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } },
  117. },
  118. { type: 'user/message', seq: 1, time: 2, data: prompt, surfaceOp: 'append' },
  119. { type: 'step/start', seq: 2, time: 3, data: { turn: 1, step: 1 } },
  120. {
  121. type: 'steering/message', seq: 3, time: 4,
  122. data: { turn: 1, message: steering },
  123. surfaceOp: 'append',
  124. },
  125. { type: 'step/end', seq: 4, time: 5, data: { turn: 1, step: 1 } },
  126. { type: 'turn/end', seq: 5, time: 6, data: { turn: 1, reason: { kind: 'completed' } } },
  127. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2, trigger: { kind: 'retry' } } },
  128. { type: 'step/start', seq: 7, time: 8, data: { turn: 2, step: 1 } },
  129. { type: 'step/end', seq: 8, time: 9, data: { turn: 2, step: 1 } },
  130. {
  131. type: 'turn/end', seq: 9, time: 10,
  132. data: {
  133. turn: 2,
  134. reason: {
  135. kind: 'error',
  136. step: 1,
  137. failure: { message: 'old provider failure', code: 'SERVER' },
  138. },
  139. },
  140. },
  141. {
  142. type: 'turn/start', seq: 10, time: 11,
  143. data: { turn: 3, trigger: { kind: 'message', source: { kind: 'user' } } },
  144. },
  145. { type: 'turn/end', seq: 11, time: 12, data: { turn: 3, reason: { kind: 'aborted' } } },
  146. {
  147. type: 'turn/start', seq: 12, time: 13,
  148. data: { turn: 4, trigger: { kind: 'message', source: { kind: 'user' } } },
  149. },
  150. { type: 'turn/end', seq: 13, time: 14, data: { turn: 4, reason: { kind: 'disposed' } } },
  151. {
  152. type: 'turn/start', seq: 14, time: 15,
  153. data: { turn: 5, trigger: { kind: 'message', source: { kind: 'user' } } },
  154. },
  155. { type: 'step/start', seq: 15, time: 16, data: { turn: 5, step: 1 } },
  156. { type: 'step/end', seq: 16, time: 17, data: { turn: 5, step: 1 } },
  157. {
  158. type: 'turn/end', seq: 17, time: 18,
  159. data: { turn: 5, reason: { kind: 'error', step: 1, message: 'old thrown value' } },
  160. },
  161. {
  162. type: 'turn/start', seq: 18, time: 19,
  163. data: { turn: 6, trigger: { kind: 'message', source: { kind: 'user' } } },
  164. },
  165. {
  166. type: 'turn/end', seq: 19, time: 20,
  167. data: {
  168. turn: 6,
  169. reason: {
  170. kind: 'error',
  171. step: 0,
  172. failure: {
  173. message: 'old detailed provider failure',
  174. code: 'RATE_LIMIT',
  175. status: 429,
  176. providerRetryAfterMs: 1000,
  177. requestId: 'request-1',
  178. },
  179. },
  180. },
  181. },
  182. {
  183. type: 'turn/start', seq: 20, time: 21,
  184. data: { turn: 7, trigger: { kind: 'message', source: { kind: 'user' } } },
  185. },
  186. {
  187. type: 'turn/end', seq: 21, time: 22,
  188. data: { turn: 7, reason: { kind: 'error', step: 0, message: 'old coded error', code: 'CODED' } },
  189. },
  190. ] as unknown as SessionEvent[]
  191. }
  192. /** A live session created inside its OWN fiber, so it survives a backend reload. */
  193. async function liveSessionInFiber(
  194. ctx: Context, id: string, cwd: string | undefined,
  195. ): Promise<Session> {
  196. let session!: Session
  197. await ctx.plugin(Object.assign((inner: Context) => {
  198. session = inner.sessions.create(SessionId(id), cwd !== undefined ? { meta: { cwd } } : undefined)
  199. }, { inject: ['sessions'] }))
  200. return session
  201. }
  202. /**
  203. * Run the coordinator orchestration suite against a backend. `makeFixture()`
  204. * MUST return a fresh fixture (isolated storage) each call.
  205. */
  206. export function runCoordinatorContract(name: string, makeFixture: () => Promise<CoordinatorFixture>): void {
  207. describe(`PersistenceCoordinator orchestration: ${name}`, () => {
  208. /** Mount SessionStore + a backend instance on a fresh context over the fixture's storage. */
  209. async function freshCtx(fix: CoordinatorFixture): Promise<{ ctx: Context; fiber: Fiber }> {
  210. const ctx = new Context()
  211. await ctx.plugin(SessionStore)
  212. const fiber = await fix.mount(ctx)
  213. return { ctx, fiber }
  214. }
  215. // --- write path: live session → flush → reload ---
  216. it('persists a live session driven through the store, surviving reload', async () => {
  217. const fix = await makeFixture()
  218. const { ctx, fiber } = await freshCtx(fix)
  219. try {
  220. const session = ctx.sessions.create(SessionId('live'), { meta: { cwd: WORK } })
  221. send(session, oneTurnLog())
  222. await ctx.sessions.flush(session)
  223. const loaded = await ctx.sessionPersistence.load(SessionId('live'))
  224. expect(loaded.events).toHaveLength(6)
  225. expect(loaded.meta.cwd).toBe(WORK)
  226. } finally {
  227. await fiber.dispose()
  228. await fix.cleanup()
  229. }
  230. })
  231. it('rejects crash-repair load while a live session owns the persisted prefix', async () => {
  232. const fix = await makeFixture()
  233. const { ctx, fiber } = await freshCtx(fix)
  234. let session!: Session
  235. const sessionFiber = await ctx.plugin(Object.assign((inner: Context) => {
  236. session = inner.sessions.create(SessionId('live-load'), { meta: { cwd: WORK } })
  237. }, { inject: ['sessions'] }))
  238. try {
  239. session.append('turn/start', { turn: 1 })
  240. await ctx.sessions.flush(session)
  241. await expect(ctx.sessionPersistence.load(session.id))
  242. .rejects.toThrow(`cannot load session "${session.id}" while its live turn is open`)
  243. send(session, oneTurnLog().slice(1))
  244. await ctx.sessions.flush(session)
  245. await sessionFiber.dispose()
  246. await vi.waitFor(async () => {
  247. const loaded = await ctx.sessionPersistence.load(session.id)
  248. expect(loaded.events.map(event => event.type)).toEqual(oneTurnLog().map(event => event.type))
  249. expect(loaded.events.at(-1)).toMatchObject({
  250. type: 'turn/end',
  251. data: { reason: { kind: 'completed' } },
  252. })
  253. })
  254. } finally {
  255. await sessionFiber.dispose()
  256. await fiber.dispose()
  257. await fix.cleanup()
  258. }
  259. })
  260. it('rechecks live ownership after a cold load enters the per-id chain', async () => {
  261. const fix = await makeFixture()
  262. const { ctx, fiber } = await freshCtx(fix)
  263. try {
  264. const id = SessionId('queued-load-live-race')
  265. const header = meta(id, WORK)
  266. const start: SessionEvent = {
  267. type: 'turn/start',
  268. seq: 0,
  269. time: 1,
  270. data: { turn: 1 },
  271. }
  272. await ctx.sessionPersistence.create(header)
  273. await ctx.sessionPersistence.append(id, [start])
  274. const loading = ctx.sessionPersistence.load(id)
  275. const live = ctx.sessions.create(id, { seed: [start], meta: header })
  276. await expect(loading).rejects.toThrow(/live turn is open/)
  277. live.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  278. await ctx.sessions.flush(live)
  279. const loaded = await ctx.sessionPersistence.load(id)
  280. // The constructor's end-seed event persisted between the stored
  281. // turn/start and the turn/end appended live.
  282. expect(loaded.events.map(event => event.type)).toEqual(['turn/start', 'session/end-seed', 'turn/end'])
  283. expect(loaded.events.at(-1)).toMatchObject({
  284. type: 'turn/end',
  285. data: { reason: { kind: 'completed' } },
  286. })
  287. } finally {
  288. await fiber.dispose()
  289. await fix.cleanup()
  290. }
  291. })
  292. it('does not load an unmaterialized empty live session', async () => {
  293. const fix = await makeFixture()
  294. const { ctx, fiber } = await freshCtx(fix)
  295. try {
  296. const session = ctx.sessions.create(SessionId('empty-live'), { meta: { cwd: WORK } })
  297. await expect(ctx.sessionPersistence.load(session.id)).rejects.toThrow(/not found/)
  298. } finally {
  299. await fiber.dispose()
  300. await fix.cleanup()
  301. }
  302. })
  303. it('round-trips the seed boundary (seedLength) through persistence', async () => {
  304. // A forked child records how many leading events were inherited via the seed; the
  305. // boundary must survive a reload (so a resume/replay can tell the inherited prefix from
  306. // the child's own events). JSONL stores it in the header; SQLite uses `seed_length`.
  307. const fix = await makeFixture()
  308. const { ctx, fiber } = await freshCtx(fix)
  309. try {
  310. let session!: Session
  311. const sessionFiber = await ctx.plugin(Object.assign((inner: Context) => {
  312. session = inner.sessions.create(SessionId('forked-child'), { meta: { cwd: WORK, seedLength: 3 } })
  313. }, { inject: ['sessions'] }))
  314. send(session, oneTurnLog())
  315. await ctx.sessions.flush(session)
  316. await sessionFiber.dispose()
  317. const loaded = await ctx.sessionPersistence.load(SessionId('forked-child'))
  318. expect(loaded.meta.seedLength).toBe(3)
  319. } finally {
  320. await fiber.dispose()
  321. await fix.cleanup()
  322. }
  323. })
  324. it('round-trips the delegation depth through persistence', async () => {
  325. // A subagent child's recursion budget lives in its header; a reload that
  326. // dropped it would reset the child to top-level and un-bound maxDepth
  327. // (JSONL stores it in the header line; SQLite uses `delegation_depth`).
  328. const fix = await makeFixture()
  329. const { ctx, fiber } = await freshCtx(fix)
  330. try {
  331. let session!: Session
  332. const sessionFiber = await ctx.plugin(Object.assign((inner: Context) => {
  333. session = inner.sessions.create(SessionId('delegated-child'), {
  334. meta: { cwd: WORK, parentSession: SessionId('root'), delegationDepth: 2 },
  335. })
  336. }, { inject: ['sessions'] }))
  337. send(session, oneTurnLog())
  338. await ctx.parallel('session/flush', session)
  339. await sessionFiber.dispose()
  340. const loaded = await ctx.sessionPersistence.load(SessionId('delegated-child'))
  341. expect(loaded.meta.delegationDepth).toBe(2)
  342. } finally {
  343. await fiber.dispose()
  344. await fix.cleanup()
  345. }
  346. })
  347. it('source-frozen events cannot be mutated after buffering and persist unchanged', async () => {
  348. const fix = await makeFixture()
  349. const { ctx, fiber } = await freshCtx(fix)
  350. try {
  351. const session = ctx.sessions.create(SessionId('mutate'), { meta: { cwd: WORK } })
  352. session.append('turn/start', { turn: 1 })
  353. const ev = session.append('user/message', createUserMessage({
  354. content: [{ type: 'text', text: 'original' }], source: { kind: 'user' },
  355. }), { surfaceOp: 'append' })
  356. expect(() => {
  357. ;(ev.data as { content: { type: 'text'; text: string }[] }).content[0]!.text = 'HACKED'
  358. }).toThrow(TypeError)
  359. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  360. await ctx.sessions.flush(session)
  361. const loaded = await ctx.sessionPersistence.load(SessionId('mutate'))
  362. const message = loaded.events.find(event => event.type === 'user/message')
  363. expect(message?.type === 'user/message' && (message.data.content[0] as { text: string }).text).toBe('original')
  364. } finally {
  365. await fiber.dispose()
  366. await fix.cleanup()
  367. }
  368. })
  369. it('load and inspect return immutable identified-message snapshots', async () => {
  370. const fix = await makeFixture()
  371. const { ctx, fiber } = await freshCtx(fix)
  372. try {
  373. const id = SessionId('immutable-read')
  374. const session = ctx.sessions.create(id, { meta: { cwd: WORK } })
  375. send(session, oneTurnLog())
  376. await ctx.sessions.flush(session)
  377. for (const snapshot of [
  378. await ctx.sessionPersistence.load(id),
  379. await ctx.sessionPersistence.inspect(id),
  380. ]) {
  381. const event = snapshot.events.find(candidate => candidate.type === 'user/message')
  382. if (event?.type !== 'user/message') throw new Error('fixture lacks user/message')
  383. expect(Object.isFrozen(event.data)).toBe(true)
  384. expect(Object.isFrozen(event.data.content)).toBe(true)
  385. expect(() => {
  386. ;(event.data as { id: string }).id = 'rewritten'
  387. }).toThrow(TypeError)
  388. expect(() => {
  389. ;(event.data.content[0] as { type: 'text'; text: string }).text = 'rewritten'
  390. }).toThrow(TypeError)
  391. }
  392. } finally {
  393. await fiber.dispose()
  394. await fix.cleanup()
  395. }
  396. })
  397. it('loads pre-identity message logs into resumable current sessions', async () => {
  398. const fix = await makeFixture()
  399. const { ctx, fiber } = await freshCtx(fix)
  400. try {
  401. const id = SessionId('legacy-message-load')
  402. await ctx.sessionPersistence.create(meta(id, WORK))
  403. await ctx.sessionPersistence.append(id, legacyMessageLog())
  404. for (const snapshot of [
  405. await ctx.sessionPersistence.inspect(id),
  406. await ctx.sessionPersistence.load(id),
  407. ]) {
  408. const messages: { id: string }[] = []
  409. for (const event of snapshot.events) {
  410. if (event.type === 'user/message') messages.push(event.data)
  411. else if (event.type === 'assistant/message'
  412. || event.type === 'tool/result') messages.push(event.data.message)
  413. }
  414. expect(messages.map(message => message.id)).toEqual([
  415. `legacy-message:${id}:1`,
  416. `legacy-message:${id}:3`,
  417. `legacy-message:${id}:5`,
  418. `legacy-message:${id}:5`,
  419. ])
  420. expect(messages.every(message => Object.isFrozen(message))).toBe(true)
  421. const resumed = Session.create(id, snapshot.events, snapshot.meta)
  422. expect(resumed.deriveMessages().map(message => message.id)).toEqual([
  423. `legacy-message:${id}:1`,
  424. `legacy-message:${id}:3`,
  425. `legacy-message:${id}:5`,
  426. ])
  427. }
  428. const replacementSuffix = await ctx.sessionPersistence.readFrom(id, 6)
  429. expect(replacementSuffix.events[0]).toMatchObject({
  430. type: 'tool/result',
  431. seq: 6,
  432. data: { message: { id: `legacy-message:${id}:5` } },
  433. })
  434. } finally {
  435. await fiber.dispose()
  436. await fix.cleanup()
  437. }
  438. })
  439. it('loads pre-react-loop session logs into resumable current sessions', async () => {
  440. const fix = await makeFixture()
  441. const { ctx, fiber } = await freshCtx(fix)
  442. try {
  443. const id = SessionId('pre-react-loop-load')
  444. const log = preReactLoopLog()
  445. const legacySteering = log[3] as unknown as { data: { message: { id: string } } }
  446. await ctx.sessionPersistence.create(meta(id, WORK))
  447. await ctx.sessionPersistence.append(id, log)
  448. const snapshots = [
  449. await ctx.sessionPersistence.inspect(id),
  450. await ctx.sessionPersistence.readFrom(id, 0),
  451. await ctx.sessionPersistence.load(id),
  452. ]
  453. for (const snapshot of snapshots) {
  454. expect(snapshot.events.some(event => (event.type as string) === 'steering/message')).toBe(false)
  455. expect(snapshot.events.filter(event => event.type === 'turn/start').map(event => event.data))
  456. .toEqual([
  457. { turn: 1 }, { turn: 2 }, { turn: 3 }, { turn: 4 }, { turn: 5 }, { turn: 6 }, { turn: 7 },
  458. ])
  459. expect(snapshot.events.filter(event => event.type === 'turn/end').map(event => event.data)).toEqual([
  460. { turn: 1, reason: { kind: 'completed' } },
  461. {
  462. turn: 2,
  463. reason: { kind: 'error', error: { message: 'old provider failure', code: 'SERVER' } },
  464. },
  465. { turn: 3, reason: { kind: 'aborted', reason: { kind: 'legacy' } } },
  466. { turn: 4, reason: { kind: 'aborted', reason: { kind: 'disposed' } } },
  467. {
  468. turn: 5,
  469. reason: { kind: 'error', error: { message: 'old thrown value', code: 'UNKNOWN' } },
  470. },
  471. {
  472. turn: 6,
  473. reason: {
  474. kind: 'error',
  475. error: {
  476. message: 'old detailed provider failure',
  477. code: 'RATE_LIMIT',
  478. status: 429,
  479. providerRetryAfterMs: 1000,
  480. requestId: 'request-1',
  481. },
  482. },
  483. },
  484. {
  485. turn: 7,
  486. reason: { kind: 'error', error: { message: 'old coded error', code: 'CODED' } },
  487. },
  488. ])
  489. const resumed = Session.create(id, snapshot.events, snapshot.meta)
  490. expect(resumed.deriveMessages().map(message => message.content)).toEqual([
  491. [{ type: 'text', text: 'old prompt' }],
  492. [{ type: 'text', text: 'old steering' }],
  493. ])
  494. }
  495. const suffix = await ctx.sessionPersistence.readFrom(id, 3)
  496. expect(suffix.events[0]).toMatchObject({
  497. type: 'user/message',
  498. seq: 3,
  499. data: { id: legacySteering.data.message.id },
  500. })
  501. expect(suffix.events.filter(event => event.type === 'turn/end')
  502. .every(event => !Object.hasOwn(event.data, 'step'))).toBe(true)
  503. const flatId = SessionId('pre-react-loop-flat-steering')
  504. await ctx.sessionPersistence.create(meta(flatId, WORK))
  505. await ctx.sessionPersistence.append(flatId, [{
  506. type: 'steering/message',
  507. seq: 0,
  508. time: 1,
  509. data: {
  510. turn: 1,
  511. content: [{ type: 'text', text: 'flat steering' }],
  512. source: { kind: 'user' },
  513. },
  514. surfaceOp: 'append',
  515. } as unknown as SessionEvent])
  516. expect((await ctx.sessionPersistence.inspect(flatId)).events[0]).toMatchObject({
  517. type: 'user/message',
  518. data: {
  519. id: `legacy-message:${flatId}:0`,
  520. role: 'user',
  521. content: [{ type: 'text', text: 'flat steering' }],
  522. },
  523. })
  524. const extendedId = SessionId('current-extended-turn-end')
  525. await ctx.sessionPersistence.create(meta(extendedId, WORK))
  526. await ctx.sessionPersistence.append(extendedId, [
  527. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1 } },
  528. {
  529. type: 'turn/end', seq: 1, time: 2,
  530. data: { turn: 1, reason: { kind: 'extension-reason' } },
  531. } as unknown as SessionEvent,
  532. ])
  533. expect((await ctx.sessionPersistence.inspect(extendedId)).events[1]).toMatchObject({
  534. type: 'turn/end',
  535. data: { reason: { kind: 'extension-reason' } },
  536. })
  537. } finally {
  538. await fiber.dispose()
  539. await fix.cleanup()
  540. }
  541. })
  542. it('rejects malformed persisted message events before returning them', async () => {
  543. const fix = await makeFixture()
  544. const { ctx, fiber } = await freshCtx(fix)
  545. try {
  546. const id = SessionId('invalid-message-read')
  547. await ctx.sessionPersistence.create(meta(id, WORK))
  548. await ctx.sessionPersistence.append(id, [{
  549. type: 'user/message',
  550. seq: 0,
  551. time: 1,
  552. surfaceOp: 'append',
  553. data: {
  554. id: 'wrong-role',
  555. role: 'assistant',
  556. content: [{ type: 'text', text: 'wrong' }],
  557. source: { kind: 'user' },
  558. },
  559. } as unknown as SessionEvent])
  560. await expect(ctx.sessionPersistence.inspect(id))
  561. .rejects.toThrow('message must have role "user"')
  562. await expect(ctx.sessionPersistence.load(id))
  563. .rejects.toThrow('message must have role "user"')
  564. const malformedLegacy: { id: string; event: SessionEvent; message: string }[] = [
  565. {
  566. id: 'invalid-old-turn-start',
  567. event: {
  568. type: 'turn/start', seq: 0, time: 1,
  569. data: { turn: 1, trigger: null },
  570. } as unknown as SessionEvent,
  571. message: 'malformed pre-react-loop turn/start',
  572. },
  573. {
  574. id: 'invalid-old-steering',
  575. event: {
  576. type: 'steering/message', seq: 0, time: 1, surfaceOp: 'append',
  577. data: { turn: 1, content: [], source: { kind: 'user' }, extra: true },
  578. } as unknown as SessionEvent,
  579. message: 'malformed pre-react-loop steering/message',
  580. },
  581. {
  582. id: 'invalid-old-steering-data',
  583. event: {
  584. type: 'steering/message', seq: 0, time: 1, surfaceOp: 'append', data: null,
  585. } as unknown as SessionEvent,
  586. message: 'malformed pre-react-loop steering/message',
  587. },
  588. {
  589. id: 'invalid-old-turn-end',
  590. event: {
  591. type: 'turn/end', seq: 0, time: 1,
  592. data: { turn: 1, reason: { kind: 'completed', extra: true } },
  593. } as unknown as SessionEvent,
  594. message: 'malformed pre-react-loop turn/end',
  595. },
  596. {
  597. id: 'invalid-old-turn-end-reason',
  598. event: {
  599. type: 'turn/end', seq: 0, time: 1,
  600. data: { turn: 1, reason: null },
  601. } as unknown as SessionEvent,
  602. message: 'malformed pre-react-loop turn/end',
  603. },
  604. {
  605. id: 'unsupported-intermediate-turn-end-step',
  606. event: {
  607. type: 'turn/end', seq: 0, time: 1,
  608. data: { turn: 1, step: 1, reason: { kind: 'completed' } },
  609. } as unknown as SessionEvent,
  610. message: 'malformed pre-react-loop turn/end',
  611. },
  612. {
  613. id: 'invalid-old-turn-end-aborted',
  614. event: {
  615. type: 'turn/end', seq: 0, time: 1,
  616. data: { turn: 1, reason: { kind: 'aborted', extra: true } },
  617. } as unknown as SessionEvent,
  618. message: 'malformed pre-react-loop turn/end',
  619. },
  620. {
  621. id: 'invalid-old-turn-end-disposed',
  622. event: {
  623. type: 'turn/end', seq: 0, time: 1,
  624. data: { turn: 1, reason: { kind: 'disposed', extra: true } },
  625. } as unknown as SessionEvent,
  626. message: 'malformed pre-react-loop turn/end',
  627. },
  628. {
  629. id: 'invalid-old-turn-end-error-step',
  630. event: {
  631. type: 'turn/end', seq: 0, time: 1,
  632. data: { turn: 1, reason: { kind: 'error', step: -1, message: 'bad step' } },
  633. } as unknown as SessionEvent,
  634. message: 'malformed pre-react-loop turn/end',
  635. },
  636. {
  637. id: 'invalid-old-turn-end-error-code',
  638. event: {
  639. type: 'turn/end', seq: 0, time: 1,
  640. data: { turn: 1, reason: { kind: 'error', step: 0, message: 'bad code', code: 1 } },
  641. } as unknown as SessionEvent,
  642. message: 'malformed pre-react-loop turn/end',
  643. },
  644. ]
  645. for (const malformed of malformedLegacy) {
  646. const malformedId = SessionId(malformed.id)
  647. await ctx.sessionPersistence.create(meta(malformedId, WORK))
  648. await ctx.sessionPersistence.append(malformedId, [malformed.event])
  649. await expect(ctx.sessionPersistence.inspect(malformedId)).rejects.toThrow(malformed.message)
  650. await expect(ctx.sessionPersistence.readFrom(malformedId, 0)).rejects.toThrow(malformed.message)
  651. }
  652. for (const type of ['tool/result'] as const) {
  653. const malformedId = SessionId(`invalid-${type}`)
  654. await ctx.sessionPersistence.create(meta(malformedId, WORK))
  655. await ctx.sessionPersistence.append(malformedId, [{
  656. type,
  657. seq: 0,
  658. time: 1,
  659. surfaceOp: 'append',
  660. data: { message: null },
  661. } as unknown as SessionEvent])
  662. await expect(ctx.sessionPersistence.inspect(malformedId))
  663. .rejects.toThrow('lacks an identified message')
  664. }
  665. // An out-of-repo event type passes only with the envelope's ignorable
  666. // marker (unknown-type refusal otherwise), and its non-object data is
  667. // not message-validated.
  668. const pluginId = SessionId('non-object-plugin-event')
  669. await ctx.sessionPersistence.create(meta(pluginId, WORK))
  670. await ctx.sessionPersistence.append(pluginId, [{
  671. type: 'plugin/test',
  672. seq: 0,
  673. time: 1,
  674. data: null,
  675. ignorable: true,
  676. } as unknown as SessionEvent])
  677. await expect(ctx.sessionPersistence.inspect(pluginId))
  678. .resolves.toMatchObject({ events: [{ type: 'plugin/test', data: null, ignorable: true }] })
  679. await expect(ctx.sessionPersistence.readFrom(pluginId, 0))
  680. .resolves.toMatchObject({ events: [{ type: 'plugin/test', data: null, ignorable: true }] })
  681. for (const type of ['user/message', 'assistant/message'] as const) {
  682. const missingContentId = SessionId(`invalid-${type}-without-content`)
  683. await ctx.sessionPersistence.create(meta(missingContentId, WORK))
  684. await ctx.sessionPersistence.append(missingContentId, [{
  685. type,
  686. seq: 0,
  687. time: 1,
  688. surfaceOp: 'append',
  689. data: {},
  690. } as unknown as SessionEvent])
  691. await expect(ctx.sessionPersistence.readFrom(missingContentId, 0))
  692. .rejects.toThrow('lacks an identified message')
  693. }
  694. } finally {
  695. await fiber.dispose()
  696. await fix.cleanup()
  697. }
  698. })
  699. it('append snapshots the batch: mutating the caller array/events after the call is ignored', async () => {
  700. const fix = await makeFixture()
  701. const { ctx, fiber } = await freshCtx(fix)
  702. try {
  703. const m = meta('snapshot', WORK)
  704. await ctx.sessionPersistence.create(m)
  705. const events = structuredClone(oneTurnLog()) // seqs 0..5
  706. const userMsg = events[1] // the user/message event
  707. const p = ctx.sessionPersistence.append(m.id, events)
  708. // Mutate the caller's array AND an event object after the call but before
  709. // the queued op runs: the snapshot taken at call time must shield the copy.
  710. events.push({ type: 'turn/start', seq: 6, time: 99, data: { turn: 2 } })
  711. if (userMsg?.type === 'user/message') {
  712. (userMsg.data as { content: unknown[] }).content = [{ type: 'text', text: 'MUTATED' }]
  713. }
  714. await p
  715. const loaded = await ctx.sessionPersistence.load(m.id)
  716. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5]) // not 0..6
  717. const persisted = JSON.stringify(loaded.events)
  718. expect(persisted).toContain('hi') // original content
  719. expect(persisted).not.toContain('MUTATED')
  720. } finally {
  721. await fiber.dispose()
  722. await fix.cleanup()
  723. }
  724. })
  725. // --- fork / resume ---
  726. it('fork: a seeded new session persists its seed once (no double-write on a no-op flush)', async () => {
  727. const fix = await makeFixture()
  728. const { ctx, fiber } = await freshCtx(fix)
  729. try {
  730. const seed = oneTurnLog()
  731. // A fork: a brand-new id whose seed came from elsewhere.
  732. const forked = ctx.sessions.create(SessionId('forked'), { seed, meta: { cwd: WORK } })
  733. await ctx.sessions.flush(forked) // onCreated persisted the seed
  734. const loaded = await ctx.sessionPersistence.load(SessionId('forked'))
  735. // Fork is where the marker earns its keep: the inherited prefix may
  736. // carry a bracket the still-running parent owns.
  737. expect(loaded.events.slice(0, seed.length)).toEqual(seed)
  738. expect(loaded.events.at(-1)).toMatchObject({ type: 'session/end-seed', seq: seed.length })
  739. // A flush with no NEW events must not double-write.
  740. await ctx.sessions.flush(forked)
  741. const reloaded = await ctx.sessionPersistence.load(SessionId('forked'))
  742. expect(reloaded.events).toEqual(loaded.events)
  743. } finally {
  744. await fiber.dispose()
  745. await fix.cleanup()
  746. }
  747. })
  748. it('resume: a re-created session seeded with the loaded log does not re-append its seed and continues the seq', async () => {
  749. // Separate backend lifecycles distinguish persisted-seed adoption from an in-memory continuation.
  750. const fix = await makeFixture()
  751. const first = await freshCtx(fix)
  752. try {
  753. const s1 = first.ctx.sessions.create(SessionId('resumed'), { meta: { cwd: WORK } })
  754. send(s1, oneTurnLog())
  755. await first.ctx.sessions.flush(s1)
  756. } finally {
  757. await first.fiber.dispose()
  758. }
  759. const second = await freshCtx(fix)
  760. try {
  761. const loaded = await second.ctx.sessionPersistence.load(SessionId('resumed'))
  762. const s2 = second.ctx.sessions.create(SessionId('resumed'), { seed: loaded.events, meta: { cwd: WORK } })
  763. await second.ctx.sessions.flush(s2) // let onCreated adopt
  764. s2.append('turn/start', { turn: 2 })
  765. s2.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
  766. await second.ctx.sessions.flush(s2)
  767. const reloaded = await second.ctx.sessionPersistence.load(SessionId('resumed'))
  768. // 0-5 the resumed seed, 6 end-seed, 7-8 the new turn.
  769. expect(reloaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8])
  770. expect(reloaded.events[6]).toMatchObject({ type: 'session/end-seed' })
  771. } finally {
  772. await second.fiber.dispose()
  773. await fix.cleanup()
  774. }
  775. })
  776. // --- HMR ---
  777. it('HMR: applying the plugin seeds existing live sessions', async () => {
  778. const fix = await makeFixture()
  779. const ctx = new Context()
  780. await ctx.plugin(SessionStore)
  781. // A session exists BEFORE the persistence plugin is applied.
  782. const session = ctx.sessions.create(SessionId('pre-existing'), { meta: { cwd: WORK } })
  783. session.append('turn/start', { turn: 1 })
  784. session.append('user/message', createUserMessage({
  785. content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' },
  786. }), { surfaceOp: 'append' })
  787. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  788. const fiber = await fix.mount(ctx)
  789. try {
  790. // The plugin seeded it on apply; a subsequent flush persists its events.
  791. await ctx.sessions.flush(session)
  792. const loaded = await ctx.sessionPersistence.load(SessionId('pre-existing'))
  793. expect(loaded.events.length).toBeGreaterThanOrEqual(2)
  794. } finally {
  795. await fiber.dispose()
  796. await fix.cleanup()
  797. }
  798. })
  799. it('HMR: dispose drains remaining buffers', async () => {
  800. const fix = await makeFixture()
  801. const ctx = new Context()
  802. await ctx.plugin(SessionStore)
  803. const fiber = await fix.mount(ctx)
  804. const session = await liveSessionInFiber(ctx, 'drain', WORK)
  805. session.append('turn/start', { turn: 1 })
  806. session.append('user/message', createUserMessage({
  807. content: [{ type: 'text', text: 'buffered' }], source: { kind: 'user' },
  808. }), { surfaceOp: 'append' })
  809. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  810. // No explicit flush — dispose must drain.
  811. await fiber.dispose()
  812. // A fresh backend instance reads what the disposed one drained.
  813. const second = await freshCtx(fix)
  814. try {
  815. const loaded = await second.ctx.sessionPersistence.load(SessionId('drain'))
  816. expect(loaded.events.length).toBeGreaterThanOrEqual(2)
  817. } finally {
  818. await second.fiber.dispose()
  819. await fix.cleanup()
  820. }
  821. })
  822. it('HMR: reloading the backend adopts a still-live, already-materialized session', async () => {
  823. const fix = await makeFixture()
  824. const ctx = new Context()
  825. await ctx.plugin(SessionStore)
  826. // The session lives in its OWN fiber so it survives the backend reload.
  827. const session = await liveSessionInFiber(ctx, 'hmr-adopt', WORK)
  828. try {
  829. // Backend instance 1 materializes the session.
  830. const backend1 = await fix.mount(ctx)
  831. session.append('turn/start', { turn: 1 })
  832. session.append('user/message', createUserMessage({
  833. content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' },
  834. }), { surfaceOp: 'append' })
  835. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  836. await ctx.sessions.flush(session)
  837. // Hot-reload: dispose instance 1, mount instance 2 over the same storage while the
  838. // session stays live. The new instance has no coordinator state but must adopt the
  839. // materialized prefix, then persist another turn rather than rejecting it as a collision.
  840. await backend1.dispose()
  841. await fix.mount(ctx)
  842. session.append('turn/start', { turn: 2 })
  843. session.append('user/message', createUserMessage({
  844. content: [{ type: 'text', text: 'again' }], source: { kind: 'user' },
  845. }), { surfaceOp: 'append' })
  846. session.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
  847. await expect(ctx.sessions.flush(session)).resolves.not.toThrow()
  848. const loaded = await ctx.sessionPersistence.load(SessionId('hmr-adopt'))
  849. expect(loaded.events.filter(e => e.type === 'turn/start')).toHaveLength(2)
  850. } finally {
  851. await ctx.fiber.dispose()
  852. await fix.cleanup()
  853. }
  854. })
  855. it('HMR: adoption persists the live SUFFIX that was ahead of the stored prefix', async () => {
  856. const fix = await makeFixture()
  857. const ctx = new Context()
  858. await ctx.plugin(SessionStore)
  859. const session = await liveSessionInFiber(ctx, 'hmr-suffix', WORK)
  860. try {
  861. // Instance 1 flushes turn 1.
  862. const backend1 = await fix.mount(ctx)
  863. session.append('turn/start', { turn: 1 })
  864. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  865. await ctx.sessions.flush(session)
  866. // Append turn 2 to the LIVE session, then dispose instance 1 WITHOUT
  867. // flushing turn 2: it is now ONLY in the live session's events; the new
  868. // backend never buffered it via session/event.
  869. await backend1.dispose()
  870. session.append('turn/start', { turn: 2 })
  871. session.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
  872. // Instance 2 adopts the stored prefix (turn 1) and MUST also persist the
  873. // live suffix (turn 2) carried in the session's events.
  874. await fix.mount(ctx)
  875. await ctx.sessions.flush(session)
  876. const loaded = await ctx.sessionPersistence.load(SessionId('hmr-suffix'))
  877. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3])
  878. expect(loaded.events.filter(e => e.type === 'turn/start')).toHaveLength(2)
  879. } finally {
  880. await ctx.fiber.dispose()
  881. await fix.cleanup()
  882. }
  883. })
  884. it('HMR adoption does NOT crash-repair an active open turn as interrupted (truncate without closers)', async () => {
  885. const fix = await makeFixture()
  886. const ctx = new Context()
  887. await ctx.plugin(SessionStore)
  888. const session = await liveSessionInFiber(ctx, 'hmr-open', WORK)
  889. try {
  890. const first = await fix.mount(ctx)
  891. session.append('turn/start', { turn: 1 })
  892. session.append('step/start', { turn: 1, step: 1 })
  893. await ctx.sessions.flush(session)
  894. // Crash-tail a torn fragment past the (open) committed turn, then reload.
  895. await first.dispose()
  896. if (fix.corruptTail) await fix.corruptTail(SessionId('hmr-open'), WORK)
  897. const second = await fix.mount(ctx)
  898. // The live session is still the authority: it appends the REAL step/turn
  899. // end. Adoption must truncate the torn tail but NOT synthesize closers.
  900. session.append('step/end', { turn: 1, step: 1 })
  901. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  902. await ctx.sessions.flush(session)
  903. const loaded = await ctx.sessionPersistence.load(SessionId('hmr-open'))
  904. expect(loaded.events.map(e => e.type)).toEqual(['turn/start', 'step/start', 'step/end', 'turn/end'])
  905. expect(loaded.events.at(-1)).toMatchObject({ type: 'turn/end', data: { reason: { kind: 'completed' } } })
  906. await second.dispose()
  907. } finally {
  908. await ctx.fiber.dispose()
  909. await fix.cleanup()
  910. }
  911. })
  912. // --- collision / id reuse ---
  913. it('a NEW live session colliding on a persisted id is rejected, not silently adopted', async () => {
  914. const fix = await makeFixture()
  915. const first = await freshCtx(fix)
  916. try {
  917. const s1 = first.ctx.sessions.create(SessionId('collide'), { meta: { cwd: WORK } })
  918. send(s1, oneTurnLog())
  919. await first.ctx.sessions.flush(s1)
  920. } finally {
  921. await first.fiber.dispose()
  922. }
  923. // A fresh backend + a NEW live session with the same id but NO explicit resume. onCreated
  924. // treats it as new; create() rejects because a log already exists, and `flush()` surfaces
  925. // that initialization rejection.
  926. const second = await freshCtx(fix)
  927. try {
  928. const s2 = second.ctx.sessions.create(SessionId('collide'), { meta: { cwd: WORK } })
  929. s2.append('turn/start', { turn: 1 })
  930. await expect(second.ctx.sessions.flush(s2))
  931. .rejects.toThrow(/already has a persisted log|id collision/)
  932. } finally {
  933. await second.fiber.dispose()
  934. await fix.cleanup()
  935. }
  936. })
  937. it('an abandoned lazy session (never materialized) releases its id for reuse', async () => {
  938. const fix = await makeFixture()
  939. const { ctx, fiber } = await freshCtx(fix)
  940. try {
  941. // A live session created then disposed BEFORE its first append: cursor 0,
  942. // never materialized. A new live session reusing the id must reclaim it.
  943. let firstSession!: Session
  944. const firstFiber = await ctx.plugin(Object.assign((inner: Context) => {
  945. firstSession = inner.sessions.create(SessionId('abandoned'), { meta: { cwd: WORK } })
  946. }, { inject: ['sessions'] }))
  947. await ctx.sessions.flush(firstSession) // register the lazy state
  948. await firstFiber.dispose() // disposed before any append → never materialized
  949. let reuse!: Session
  950. await ctx.plugin(Object.assign((inner: Context) => {
  951. reuse = inner.sessions.create(SessionId('abandoned'), { meta: { cwd: WORK } })
  952. }, { inject: ['sessions'] }))
  953. await expect(ctx.sessions.flush(reuse)).resolves.toBe(true)
  954. reuse.append('turn/start', { turn: 1 })
  955. reuse.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  956. await ctx.sessions.flush(reuse)
  957. const loaded = await ctx.sessionPersistence.load(SessionId('abandoned'))
  958. expect(loaded.events.map(e => e.seq)).toEqual([0, 1])
  959. } finally {
  960. await fiber.dispose()
  961. await fix.cleanup()
  962. }
  963. })
  964. it('session disposal drains buffered events before retiring ownership', async () => {
  965. const fix = await makeFixture()
  966. const { ctx, fiber } = await freshCtx(fix)
  967. try {
  968. let first!: Session
  969. const firstFiber = await ctx.plugin(Object.assign((inner: Context) => {
  970. first = inner.sessions.create(SessionId('buffered'), { meta: { cwd: WORK } })
  971. }, { inject: ['sessions'] }))
  972. await ctx.sessions.flush(first)
  973. // Append a turn but do NOT flush — events sit in the write-behind buffer.
  974. first.append('turn/start', { turn: 1 })
  975. first.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  976. await firstFiber.dispose()
  977. // Disposal is an observe-only notification. Poll storage rather than
  978. // assuming the owning fiber awaits the coordinator's detached drain.
  979. await vi.waitFor(async () => {
  980. expect((await ctx.sessionPersistence.list()).map(meta => meta.id)).toContain(SessionId('buffered'))
  981. })
  982. expect((await ctx.sessionPersistence.load(SessionId('buffered'))).events.map(event => event.seq)).toEqual([0, 1])
  983. let reuse!: Session
  984. await ctx.plugin(Object.assign((inner: Context) => {
  985. reuse = inner.sessions.create(SessionId('buffered'), { meta: { cwd: WORK } })
  986. }, { inject: ['sessions'] }))
  987. await expect(ctx.sessions.flush(reuse)).rejects.toThrow(/persisted log|id collision/)
  988. } finally {
  989. await fiber.dispose()
  990. await fix.cleanup()
  991. }
  992. })
  993. it('initFor is idempotent: re-emitting session/created does not re-initialize', async () => {
  994. const fix = await makeFixture()
  995. const { ctx, fiber } = await freshCtx(fix)
  996. try {
  997. const session = ctx.sessions.create(SessionId('idem'), { meta: { cwd: WORK } })
  998. session.append('turn/start', { turn: 1 })
  999. session.append('user/message', createUserMessage({
  1000. content: [{ type: 'text', text: 'x' }], source: { kind: 'user' },
  1001. }), { surfaceOp: 'append' })
  1002. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  1003. await ctx.sessions.flush(session)
  1004. // Re-emit session/created for the SAME live session (idempotent initFor).
  1005. ctx.emit(scopeTarget(session, undefined), 'session/created', session)
  1006. await ctx.sessions.flush(session)
  1007. const loaded = await ctx.sessionPersistence.load(SessionId('idem'))
  1008. expect(loaded.events).toHaveLength(3) // not doubled
  1009. } finally {
  1010. await fiber.dispose()
  1011. await fix.cleanup()
  1012. }
  1013. })
  1014. // --- ownerless-state claim (public create()/load() then a live session arrives) ---
  1015. it('a live session claims cursor-0 ownerless state created via the public API and persists its seed', async () => {
  1016. const fix = await makeFixture()
  1017. const { ctx, fiber } = await freshCtx(fix)
  1018. try {
  1019. // create() registers ownerless state with cursor 0 (lazy, nothing persisted).
  1020. await ctx.sessionPersistence.create(meta('lazy-claim', WORK))
  1021. // A live session with that id arrives and claims it (cursor 0 matches
  1022. // trivially), persisting its seed.
  1023. const live = ctx.sessions.create(SessionId('lazy-claim'), { seed: oneTurnLog(), meta: { cwd: WORK } })
  1024. await expect(ctx.sessions.flush(live)).resolves.toBe(true)
  1025. const loaded = await ctx.sessionPersistence.load(SessionId('lazy-claim'))
  1026. // Seeded 0-5 plus the constructor's end-seed event at 6.
  1027. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6])
  1028. expect(loaded.events.at(-1)).toMatchObject({ type: 'session/end-seed' })
  1029. } finally {
  1030. await fiber.dispose()
  1031. await fix.cleanup()
  1032. }
  1033. })
  1034. it('a fresh session reusing a previously-loaded id is rejected (ownerless guard)', async () => {
  1035. const fix = await makeFixture()
  1036. const { ctx, fiber } = await freshCtx(fix)
  1037. try {
  1038. // Materialize a log, then load() it WITHOUT a live session — ownerless
  1039. // state, cursor at the persisted length.
  1040. await ctx.sessionPersistence.create(meta('preview', WORK))
  1041. await ctx.sessionPersistence.append(SessionId('preview'), oneTurnLog())
  1042. await ctx.sessionPersistence.load(SessionId('preview'))
  1043. // A FRESH (empty-seed) live session reusing that id must be rejected: its
  1044. // seq 0..cursor-1 events would otherwise be filtered as already-persisted.
  1045. let fresh!: Session
  1046. await ctx.plugin(Object.assign((inner: Context) => {
  1047. fresh = inner.sessions.create(SessionId('preview'), { meta: { cwd: WORK } })
  1048. }, { inject: ['sessions'] }))
  1049. await expect(ctx.sessions.flush(fresh))
  1050. .rejects.toThrow(/do not match this live session|already has a persisted log|id collision/)
  1051. } finally {
  1052. await fiber.dispose()
  1053. await fix.cleanup()
  1054. }
  1055. })
  1056. it('a live session whose complete seed matches loaded ownerless state claims it without appending', async () => {
  1057. const fix = await makeFixture()
  1058. const { ctx, fiber } = await freshCtx(fix)
  1059. try {
  1060. const id = SessionId('claim-exact')
  1061. const completeSeed = [
  1062. ...oneTurnLog(),
  1063. { type: 'session/end-seed', seq: 6, time: 7, data: {} },
  1064. ] as SessionEvent[]
  1065. await ctx.sessionPersistence.create(meta(id, WORK))
  1066. await ctx.sessionPersistence.append(id, completeSeed)
  1067. const { events } = await ctx.sessionPersistence.load(id)
  1068. const live = ctx.sessions.create(id, { seed: events, meta: { cwd: WORK } })
  1069. await expect(ctx.sessions.flush(live)).resolves.toBe(true)
  1070. expect((await ctx.sessionPersistence.load(id)).events).toEqual(events)
  1071. } finally {
  1072. await fiber.dispose()
  1073. await fix.cleanup()
  1074. }
  1075. })
  1076. it('a live session whose seed matches the loaded prefix claims ownerless state and persists the suffix', async () => {
  1077. const fix = await makeFixture()
  1078. const { ctx, fiber } = await freshCtx(fix)
  1079. try {
  1080. // Materialize and load (ownerless, cursor = 6).
  1081. const storedMeta = meta('claim', WORK)
  1082. await ctx.sessionPersistence.create(storedMeta)
  1083. await ctx.sessionPersistence.append(SessionId('claim'), oneTurnLog())
  1084. const { events, meta: durableMeta } = await ctx.sessionPersistence.load(SessionId('claim'))
  1085. // A live session SEEDED with the loaded log PLUS a new turn claims the
  1086. // ownerless state and persists only the suffix.
  1087. let cont!: Session
  1088. const contFiber = await ctx.plugin(Object.assign((inner: Context) => {
  1089. cont = inner.sessions.create(SessionId('claim'), { seed: [
  1090. ...events,
  1091. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2 } },
  1092. { type: 'turn/end', seq: 7, time: 8, data: { turn: 2, reason: { kind: 'completed' } } },
  1093. ], meta: { cwd: WORK, createdAt: 2000 } })
  1094. }, { inject: ['sessions'] }))
  1095. await ctx.sessions.flush(cont)
  1096. const loaded = await ctx.sessionPersistence.load(SessionId('claim'))
  1097. // 6-7 the claimed suffix; 8 end-seed after the whole seed.
  1098. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8])
  1099. expect(loaded.events.at(-1)).toMatchObject({ type: 'session/end-seed' })
  1100. expect(loaded.meta).toEqual(durableMeta)
  1101. expect(loaded.meta.createdAt).toBe(1000)
  1102. await contFiber.dispose()
  1103. await vi.waitFor(async () => {
  1104. expect((await ctx.sessionPersistence.load(SessionId('claim'))).meta).toEqual(durableMeta)
  1105. })
  1106. } finally {
  1107. await fiber.dispose()
  1108. await fix.cleanup()
  1109. }
  1110. })
  1111. it('a live session at a DIFFERENT cwd cannot claim cursor-0 ownerless state (cwd scope)', async () => {
  1112. const fix = await makeFixture()
  1113. const { ctx, fiber } = await freshCtx(fix)
  1114. try {
  1115. // create() registers ownerless state at cwd /a (cursor 0 — claims would
  1116. // otherwise match trivially on the seed).
  1117. await ctx.sessionPersistence.create(meta('wrong-cwd-claim', OTHER))
  1118. // A live session reusing the id but at cwd WORK must NOT claim it — the
  1119. // cwd scope is the fence (without it, WORK events would append under the
  1120. // OTHER header). Rejected as a collision.
  1121. const live = ctx.sessions.create(SessionId('wrong-cwd-claim'), { seed: oneTurnLog(), meta: { cwd: WORK } })
  1122. await expect(ctx.sessions.flush(live)).rejects.toThrow(/different cwd|id collision/)
  1123. } finally {
  1124. await fiber.dispose()
  1125. await fix.cleanup()
  1126. }
  1127. })
  1128. it('a live session at a DIFFERENT cwd cannot claim loaded-prefix ownerless state (cwd scope)', async () => {
  1129. const fix = await makeFixture()
  1130. const { ctx, fiber } = await freshCtx(fix)
  1131. try {
  1132. // Materialize + load at cwd OTHER (ownerless, cursor = 6).
  1133. await ctx.sessionPersistence.create(meta('wrong-cwd-load', OTHER))
  1134. await ctx.sessionPersistence.append(SessionId('wrong-cwd-load'), oneTurnLog())
  1135. const { events } = await ctx.sessionPersistence.load(SessionId('wrong-cwd-load'))
  1136. // A live session whose SEED matches the loaded prefix but whose cwd is
  1137. // WORK must still be rejected — the cwd guard runs before the seed check.
  1138. const live = ctx.sessions.create(SessionId('wrong-cwd-load'), { seed: events, meta: { cwd: WORK } })
  1139. await expect(ctx.sessions.flush(live)).rejects.toThrow(/different cwd|id collision/)
  1140. } finally {
  1141. await fiber.dispose()
  1142. await fix.cleanup()
  1143. }
  1144. })
  1145. it('a no-cwd ownerless state cannot be claimed by a live session WITH a cwd (cwd scope, undefined side)', async () => {
  1146. const fix = await makeFixture()
  1147. const { ctx, fiber } = await freshCtx(fix)
  1148. try {
  1149. // Ownerless state created WITHOUT a cwd (the `_no-cwd` project directory).
  1150. await ctx.sessionPersistence.create(meta('no-cwd-state'))
  1151. // A live session reusing the id but WITH cwd WORK is a cwd mismatch
  1152. // (undefined vs WORK) and must be rejected.
  1153. const live = ctx.sessions.create(SessionId('no-cwd-state'), { seed: oneTurnLog(), meta: { cwd: WORK } })
  1154. await expect(ctx.sessions.flush(live)).rejects.toThrow(/different cwd|id collision/)
  1155. } finally {
  1156. await fiber.dispose()
  1157. await fix.cleanup()
  1158. }
  1159. })
  1160. // --- append adopts a storage-only session (fresh instance, no prior create/load) ---
  1161. it('append adopts a storage-only session (fresh instance) and continues the seq', async () => {
  1162. const fix = await makeFixture()
  1163. const first = await freshCtx(fix)
  1164. try {
  1165. const m = meta('adopt-append', WORK)
  1166. await first.ctx.sessionPersistence.create(m)
  1167. await first.ctx.sessionPersistence.append(m.id, oneTurnLog())
  1168. } finally {
  1169. await first.fiber.dispose()
  1170. }
  1171. // A fresh instance appends a second turn WITHOUT a prior create/load: append
  1172. // must adopt the stored session (cursor = stored length) and continue.
  1173. const second = await freshCtx(fix)
  1174. try {
  1175. await second.ctx.sessionPersistence.append(SessionId('adopt-append'), [
  1176. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2 } },
  1177. { type: 'turn/end', seq: 7, time: 8, data: { turn: 2, reason: { kind: 'completed' } } },
  1178. ])
  1179. const loaded = await second.ctx.sessionPersistence.load(SessionId('adopt-append'))
  1180. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7])
  1181. } finally {
  1182. await second.fiber.dispose()
  1183. await fix.cleanup()
  1184. }
  1185. })
  1186. // --- small public-API edges that the coordinator owns uniformly ---
  1187. it('append of an empty batch is a no-op (stays lazy)', async () => {
  1188. const fix = await makeFixture()
  1189. const { ctx, fiber } = await freshCtx(fix)
  1190. try {
  1191. const m = meta('empty-batch', WORK)
  1192. await ctx.sessionPersistence.create(m)
  1193. await ctx.sessionPersistence.append(m.id, [])
  1194. expect((await ctx.sessionPersistence.list()).map(h => h.id)).not.toContain(m.id)
  1195. } finally {
  1196. await fiber.dispose()
  1197. await fix.cleanup()
  1198. }
  1199. })
  1200. it('load and inspect reject a missing session', async () => {
  1201. const fix = await makeFixture()
  1202. const { ctx, fiber } = await freshCtx(fix)
  1203. try {
  1204. await expect(ctx.sessionPersistence.load(SessionId('nope'))).rejects.toThrow(/not found/)
  1205. await expect(ctx.sessionPersistence.inspect(SessionId('nope'))).rejects.toThrow(/not found/)
  1206. } finally {
  1207. await fiber.dispose()
  1208. await fix.cleanup()
  1209. }
  1210. })
  1211. it('create rejects a duplicate id (in memory and on a persisted log)', async () => {
  1212. const fix = await makeFixture()
  1213. const first = await freshCtx(fix)
  1214. try {
  1215. const m = meta('dup', WORK)
  1216. await first.ctx.sessionPersistence.create(m)
  1217. // Same in-memory state.
  1218. await expect(first.ctx.sessionPersistence.create(m)).rejects.toThrow(/already exists in this backend/)
  1219. await first.ctx.sessionPersistence.append(m.id, oneTurnLog())
  1220. } finally {
  1221. await first.fiber.dispose()
  1222. }
  1223. // A fresh instance over the same storage sees the persisted log.
  1224. const second = await freshCtx(fix)
  1225. try {
  1226. await expect(second.ctx.sessionPersistence.create(meta('dup', WORK)))
  1227. .rejects.toThrow(/already has a persisted log on disk/)
  1228. } finally {
  1229. await second.fiber.dispose()
  1230. await fix.cleanup()
  1231. }
  1232. })
  1233. it('rejects a newer format version on load, naming the upgrade direction', async () => {
  1234. const fix = await makeFixture()
  1235. const { ctx, fiber } = await freshCtx(fix)
  1236. try {
  1237. const m = { version: 99, id: SessionId('v99'), createdAt: 1, cwd: WORK }
  1238. await ctx.sessionPersistence.create(m)
  1239. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  1240. const failure = await ctx.sessionPersistence.load(m.id).then(() => undefined, (error: unknown) => error as Error)
  1241. expect(failure?.name).toBe('SessionFormatUnsupportedError')
  1242. expect(failure?.message).toMatch(/written by a newer harness.*upgrade the harness/)
  1243. } finally {
  1244. await fiber.dispose()
  1245. await fix.cleanup()
  1246. }
  1247. })
  1248. it('rejects an older format version on load without claiming an upgrade path', async () => {
  1249. const fix = await makeFixture()
  1250. const { ctx, fiber } = await freshCtx(fix)
  1251. try {
  1252. const m = { version: -1, id: SessionId('v-older'), createdAt: 1, cwd: WORK }
  1253. await ctx.sessionPersistence.create(m)
  1254. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  1255. const failure = await ctx.sessionPersistence.load(m.id).then(() => undefined, (error: unknown) => error as Error)
  1256. expect(failure?.name).toBe('SessionFormatUnsupportedError')
  1257. expect(failure?.message).toMatch(/older than the supported v0.*no upgrade path/)
  1258. } finally {
  1259. await fiber.dispose()
  1260. await fix.cleanup()
  1261. }
  1262. })
  1263. it('rejects an unknown event type on load unless the event is marked ignorable', async () => {
  1264. const fix = await makeFixture()
  1265. const { ctx, fiber } = await freshCtx(fix)
  1266. try {
  1267. const required = meta('unknown-required', WORK)
  1268. await ctx.sessionPersistence.create(required)
  1269. await ctx.sessionPersistence.append(required.id, [
  1270. ...oneTurnLog(),
  1271. { type: 'future/event', seq: oneTurnLog().length, time: 99, data: { payload: 1 } } as unknown as SessionEvent,
  1272. ])
  1273. const failure = await ctx.sessionPersistence.load(required.id).then(() => undefined, (error: unknown) => error as Error)
  1274. expect(failure?.name).toBe('SessionFormatUnsupportedError')
  1275. expect(failure?.message).toMatch(/event type "future\/event".*not marked ignorable/)
  1276. const skippable = meta('unknown-ignorable', WORK)
  1277. await ctx.sessionPersistence.create(skippable)
  1278. await ctx.sessionPersistence.append(skippable.id, [
  1279. ...oneTurnLog(),
  1280. { type: 'future/event', seq: oneTurnLog().length, time: 99, data: { payload: 1 }, ignorable: true } as unknown as SessionEvent,
  1281. ])
  1282. const loaded = await ctx.sessionPersistence.load(skippable.id)
  1283. expect(loaded.events.some(event => (event.type as string) === 'future/event')).toBe(true)
  1284. } finally {
  1285. await fiber.dispose()
  1286. await fix.cleanup()
  1287. }
  1288. })
  1289. it('round-trips a header with parentSession (fork lineage)', async () => {
  1290. const fix = await makeFixture()
  1291. const { ctx, fiber } = await freshCtx(fix)
  1292. try {
  1293. const m = { version: SESSION_FORMAT_VERSION, id: SessionId('forked-child'), createdAt: 1, cwd: WORK, parentSession: SessionId('the-parent') }
  1294. await ctx.sessionPersistence.create(m)
  1295. await ctx.sessionPersistence.append(m.id, oneTurnLog())
  1296. const loaded = await ctx.sessionPersistence.load(m.id)
  1297. expect(loaded.meta.parentSession).toBe('the-parent')
  1298. } finally {
  1299. await fiber.dispose()
  1300. await fix.cleanup()
  1301. }
  1302. })
  1303. it('flush before init resolves uses cursor 0', async () => {
  1304. const fix = await makeFixture()
  1305. const { ctx, fiber } = await freshCtx(fix)
  1306. try {
  1307. // Append directly to a live session and flush IMMEDIATELY, before the
  1308. // async onCreated init has necessarily set state (exercises the
  1309. // state-undefined cursor path).
  1310. const session = ctx.sessions.create(SessionId('flush-nostate'), { meta: { cwd: WORK } })
  1311. session.append('turn/start', { turn: 1 })
  1312. session.append('user/message', createUserMessage({
  1313. content: [{ type: 'text', text: 'q' }], source: { kind: 'user' },
  1314. }), { surfaceOp: 'append' })
  1315. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  1316. await ctx.sessions.flush(session)
  1317. const loaded = await ctx.sessionPersistence.load(SessionId('flush-nostate'))
  1318. expect(loaded.events).toHaveLength(3)
  1319. } finally {
  1320. await fiber.dispose()
  1321. await fix.cleanup()
  1322. }
  1323. })
  1324. // --- crash-tail repair THROUGH the coordinator (real storage torn tail) ---
  1325. it('torn-tail load: a never-committed tail is truncated and the open turn closed during load (commitRepair w/ tornMarker)', async () => {
  1326. const fix = await makeFixture()
  1327. if (!fix.corruptTail) {
  1328. // A memory-style store has no torn tails (every write is atomic in RAM),
  1329. // so there is no tornMarker path to exercise. Assert that explicitly
  1330. // instead of silently skipping, then bail.
  1331. expect(fix.corruptTail).toBeUndefined()
  1332. await fix.cleanup()
  1333. return
  1334. }
  1335. const first = await freshCtx(fix)
  1336. try {
  1337. const m = meta('torn', WORK)
  1338. await first.ctx.sessionPersistence.create(m)
  1339. await first.ctx.sessionPersistence.append(m.id, oneTurnLog()) // committed 0..5 (balanced)
  1340. // A second turn whose real events are durable but never closed (open turn).
  1341. await first.ctx.sessionPersistence.append(m.id, [
  1342. { type: 'turn/start', seq: 6, time: 7, data: { turn: 2 } },
  1343. { type: 'step/start', seq: 7, time: 8, data: { turn: 2, step: 1 } },
  1344. ])
  1345. } finally {
  1346. await first.fiber.dispose()
  1347. }
  1348. // Inject a torn fragment past the committed region (never-committed tail).
  1349. await fix.corruptTail(SessionId('torn'), WORK)
  1350. // A FRESH instance loads: the torn tail is truncated (tornMarker !==
  1351. // undefined) AND the open turn 2 is closed with synthetic step/end +
  1352. // turn/end {interrupted} — commitRepair runs with BOTH a torn marker and
  1353. // closers. The preserved real events (0..7) are never truncated.
  1354. const second = await freshCtx(fix)
  1355. try {
  1356. const loaded = await second.ctx.sessionPersistence.load(SessionId('torn'))
  1357. expect(loaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  1358. expect(loaded.events.map(e => e.type)).toEqual([
  1359. 'turn/start', 'user/message', 'step/start', 'assistant/message', 'step/end', 'turn/end', // turn 1
  1360. 'turn/start', 'step/start', 'step/end', 'turn/end', // turn 2: real + synthetic closers
  1361. ])
  1362. const last = loaded.events.at(-1)!
  1363. expect(last.type === 'turn/end' && last.data.reason).toEqual({ kind: 'interrupted' })
  1364. // The repair is durable: the next append continues at the balanced length
  1365. // (seq 10) and a reload round-trips identically.
  1366. await second.ctx.sessionPersistence.append(SessionId('torn'), [
  1367. { type: 'turn/start', seq: 10, time: 9, data: { turn: 3 } },
  1368. { type: 'turn/end', seq: 11, time: 10, data: { turn: 3, reason: { kind: 'completed' } } },
  1369. ])
  1370. const reloaded = await second.ctx.sessionPersistence.load(SessionId('torn'))
  1371. expect(reloaded.events.map(e => e.seq)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
  1372. } finally {
  1373. await second.fiber.dispose()
  1374. await fix.cleanup()
  1375. }
  1376. })
  1377. })
  1378. }