surface.spec.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. import { describe, expect, it } from 'vitest'
  2. import type { SessionEvent, SurfaceEvent, SurfaceEventType } from '@deepseek-ai/dsh-session'
  3. import {
  4. Session,
  5. SessionId,
  6. SessionLogOffset,
  7. SessionSeq,
  8. foldSurface,
  9. isAppendSurfaceEvent,
  10. isReplacementSurfaceEvent,
  11. isSurfaceEligibleType,
  12. isSurfaceEvent,
  13. } from '@deepseek-ai/dsh-session'
  14. import { SurfaceManager } from '@deepseek-ai/dsh-session/surface'
  15. import {
  16. createMessage,
  17. createSystemMessage,
  18. createToolResultMessage,
  19. createUserMessage,
  20. freezeMessage,
  21. ToolCallId,
  22. MessageId,
  23. } from '@deepseek-ai/dsh-llm'
  24. type TestSurfaceOp = 'append' | { op: 'replace'; start: number; end: number }
  25. function surfaceOp(value: TestSurfaceOp): SurfaceEvent['surfaceOp'] {
  26. return value === 'append'
  27. ? value
  28. : { op: 'replace', start: SessionSeq(value.start), end: SessionSeq(value.end) }
  29. }
  30. function replacementMessage(text: string) {
  31. return createUserMessage({
  32. content: [{ type: 'text', text }],
  33. source: { kind: 'plugin', plugin: 'test' },
  34. })
  35. }
  36. function sourceSeqs(...values: number[]) {
  37. return values.map(SessionSeq)
  38. }
  39. /** Build a minimal session with turn boundaries and a single user message. */
  40. function surfaceSession(): Session {
  41. const s = Session.create(SessionId('ss'))
  42. s.append('turn/start', { turn: 1 })
  43. s.append('user/message', createUserMessage({
  44. content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' },
  45. }), { surfaceOp: 'append' })
  46. s.append('assistant/message', {
  47. stream: [],
  48. turn: 1, step: 1,
  49. message: createMessage({
  50. role: 'assistant',
  51. content: [{ type: 'text', text: 'hi' }],
  52. source: {
  53. kind: 'model',
  54. ...{ provider: 'mock', model: 'mock' },
  55. },
  56. }),
  57. }, { surfaceOp: 'append' })
  58. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  59. return s
  60. }
  61. function provenanceEvent(seq: SessionSeq, sourceEventSeqs: unknown): SessionEvent {
  62. return {
  63. type: 'user/message',
  64. seq,
  65. time: seq,
  66. data: createUserMessage({
  67. content: [], source: { kind: 'user' },
  68. }),
  69. surfaceOp: 'append',
  70. ...sourceEventSeqs === undefined ? {} : { sourceEventSeqs },
  71. } as unknown as SessionEvent
  72. }
  73. function toolResultEvent(
  74. seq: SessionSeq,
  75. callId: string,
  76. op: TestSurfaceOp = 'append',
  77. sourceEventSeqs?: number[],
  78. ): SessionEvent {
  79. return {
  80. type: 'tool/result',
  81. seq,
  82. time: seq,
  83. data: {
  84. turn: 1,
  85. step: 1,
  86. message: createToolResultMessage({
  87. callId: ToolCallId(callId),
  88. content: [{ type: 'text', text: `result ${seq}` }],
  89. isError: false,
  90. }),
  91. },
  92. surfaceOp: surfaceOp(op),
  93. ...sourceEventSeqs === undefined ? {} : { sourceEventSeqs: sourceSeqs(...sourceEventSeqs) },
  94. }
  95. }
  96. describe('foldSurface source-event references', () => {
  97. it('accepts absent or valid source-event references and complete replacement coverage', () => {
  98. const events = [
  99. provenanceEvent(SessionSeq(0), undefined),
  100. provenanceEvent(SessionSeq(1), undefined),
  101. {
  102. ...provenanceEvent(SessionSeq(2), [0, 1]),
  103. surfaceOp: surfaceOp({ op: 'replace', start: 0, end: 1 }),
  104. },
  105. ] as SessionEvent[]
  106. expect(() => foldSurface(events)).not.toThrow()
  107. })
  108. it('rejects source-event references on a non-surface event', () => {
  109. const event = {
  110. type: 'turn/start',
  111. seq: SessionSeq(0),
  112. time: 1,
  113. data: { turn: 1 },
  114. sourceEventSeqs: sourceSeqs(0),
  115. } as unknown as SessionEvent
  116. expect(() => foldSurface([event])).toThrow(/cannot carry sourceEventSeqs/)
  117. })
  118. it('rejects obsolete source-event provenance on an assistant message', () => {
  119. const event = {
  120. type: 'assistant/message',
  121. seq: SessionSeq(0),
  122. time: 0,
  123. data: {
  124. turn: 1,
  125. step: 1,
  126. message: createMessage({
  127. role: 'assistant',
  128. content: [],
  129. source: {
  130. kind: 'model',
  131. ...{ provider: 'mock', model: 'mock' },
  132. },
  133. }),
  134. stream: [],
  135. },
  136. surfaceOp: 'append',
  137. sourceEventSeqs: [],
  138. } as SessionEvent
  139. expect(() => foldSurface([event])).toThrow(/embeds its source stream/)
  140. })
  141. it.each([
  142. ['a non-array', [{ ...provenanceEvent(SessionSeq(0), undefined), sourceEventSeqs: 'invalid' }], /must be an array/],
  143. ['an empty array', [provenanceEvent(SessionSeq(0), [])], /must not be empty/],
  144. ['duplicates', [provenanceEvent(SessionSeq(0), undefined), provenanceEvent(SessionSeq(1), [0, 0])], /must not contain duplicates/],
  145. ['a sparse array', [provenanceEvent(SessionSeq(0), Array<number>(1))], /densely contain/],
  146. ['a non-number', [{ ...provenanceEvent(SessionSeq(0), undefined), sourceEventSeqs: ['0'] }], /non-negative safe integers/],
  147. ['a fractional number', [provenanceEvent(SessionSeq(0), [0.5])], /non-negative safe integers/],
  148. ['a negative number', [provenanceEvent(SessionSeq(0), [-1])], /non-negative safe integers/],
  149. ['a self reference', [provenanceEvent(SessionSeq(0), [0])], /must reference earlier events/],
  150. ['a non-contiguous event seq', [provenanceEvent(SessionSeq(0), undefined), provenanceEvent(SessionSeq(2), [1])], /seq 2 is not contiguous; expected 1/],
  151. ['incomplete replacement coverage', [
  152. provenanceEvent(SessionSeq(0), undefined),
  153. provenanceEvent(SessionSeq(1), undefined),
  154. { ...provenanceEvent(SessionSeq(2), [0]), surfaceOp: { op: 'replace', start: 0, end: 1 } },
  155. ], /missing 1/],
  156. ] as const)(
  157. 'rejects %s',
  158. (_name, events, expected) => {
  159. expect(() => foldSurface(events as unknown as SessionEvent[])).toThrow(expected)
  160. },
  161. )
  162. })
  163. describe('foldSurface tool-result rewrites', () => {
  164. it('rejects a replacement spanning multiple current nodes', () => {
  165. const events = [
  166. provenanceEvent(SessionSeq(0), undefined),
  167. provenanceEvent(SessionSeq(1), undefined),
  168. toolResultEvent(SessionSeq(2), 'rewrite', { op: 'replace', start: 0, end: 1 }, [0, 1]),
  169. ]
  170. expect(() => foldSurface(events)).toThrow(/must rewrite exactly one current node/)
  171. })
  172. it('rejects a replacement targeting a non-result node', () => {
  173. const events = [
  174. provenanceEvent(SessionSeq(0), undefined),
  175. toolResultEvent(SessionSeq(1), 'rewrite', { op: 'replace', start: 0, end: 0 }, [0]),
  176. ]
  177. expect(() => foldSurface(events)).toThrow(/must target a current tool\/result/)
  178. })
  179. it('rejects changes outside tool-result content', () => {
  180. const events = [
  181. toolResultEvent(SessionSeq(0), 'original'),
  182. toolResultEvent(SessionSeq(1), 'changed', { op: 'replace', start: 0, end: 0 }, [0]),
  183. ]
  184. expect(() => foldSurface(events)).toThrow(/may change only content/)
  185. })
  186. it.each([
  187. ['toolCallId', { toolCallId: ToolCallId('changed') }],
  188. ['isError', { isError: true }],
  189. ] as const)('rejects a replacement that changes the result block %s', (_field, patch) => {
  190. const original = toolResultEvent(SessionSeq(0), 'original')
  191. const data = original.data as Extract<SessionEvent, { type: 'tool/result' }>['data']
  192. const result = data.message.content[0]
  193. const replacement = {
  194. ...original,
  195. seq: SessionSeq(1),
  196. time: 1,
  197. data: {
  198. ...data,
  199. message: freezeMessage({
  200. ...data.message,
  201. content: [{ ...result, ...patch }] as [typeof result],
  202. }),
  203. },
  204. surfaceOp: surfaceOp({ op: 'replace', start: 0, end: 0 }),
  205. sourceEventSeqs: sourceSeqs(0),
  206. } as SessionEvent
  207. expect(() => foldSurface([original, replacement])).toThrow(/may change only content/)
  208. })
  209. it('compares array-valued rest fields structurally (meta arrays: equal accepted, drifted rejected)', () => {
  210. const withMeta = (seq: SessionSeq, meta: unknown, op: TestSurfaceOp = 'append', sourceEventSeqs?: number[]): SessionEvent => {
  211. const event = toolResultEvent(SessionSeq(seq), 'c-meta', op, sourceEventSeqs)
  212. const data = event.data as Extract<SessionEvent, { type: 'tool/result' }>['data']
  213. return {
  214. ...event,
  215. data: {
  216. ...data,
  217. message: freezeMessage({ ...data.message, id: MessageId('meta-message') }),
  218. meta,
  219. },
  220. } as SessionEvent
  221. }
  222. // Structurally equal arrays (fresh references) pass the rest-field equality.
  223. expect(() => foldSurface([
  224. withMeta(SessionSeq(0), { tags: ['a', { n: 1 }] }),
  225. withMeta(SessionSeq(1), { tags: ['a', { n: 1 }] }, { op: 'replace', start: 0, end: 0 }, [0]),
  226. ])).not.toThrow()
  227. // Same length, drifted element: the array branch must reject.
  228. expect(() => foldSurface([
  229. withMeta(SessionSeq(0), { tags: ['a'] }),
  230. withMeta(SessionSeq(1), { tags: ['b'] }, { op: 'replace', start: 0, end: 0 }, [0]),
  231. ])).toThrow(/may change only content/)
  232. // Array vs non-array on one side: the mixed-shape guard rejects.
  233. expect(() => foldSurface([
  234. withMeta(SessionSeq(0), { tags: ['a'] }),
  235. withMeta(SessionSeq(1), { tags: 'a' }, { op: 'replace', start: 0, end: 0 }, [0]),
  236. ])).toThrow(/may change only content/)
  237. // Same key count, different key names: the hasOwn branch rejects.
  238. expect(() => foldSurface([
  239. withMeta(SessionSeq(0), { left: 1 }),
  240. withMeta(SessionSeq(1), { right: 1 }, { op: 'replace', start: 0, end: 0 }, [0]),
  241. ])).toThrow(/may change only content/)
  242. // Different key counts: the key-length branch rejects.
  243. expect(() => foldSurface([
  244. withMeta(SessionSeq(0), { one: 1 }),
  245. withMeta(SessionSeq(1), { one: 1, two: 2 }, { op: 'replace', start: 0, end: 0 }, [0]),
  246. ])).toThrow(/may change only content/)
  247. })
  248. })
  249. describe('SurfaceManager', () => {
  250. it('folds a contiguous window without materializing earlier event sequences', () => {
  251. const baseSeq = 400_000
  252. const events = [
  253. provenanceEvent(SessionSeq(baseSeq), undefined),
  254. provenanceEvent(SessionSeq(baseSeq + 1), undefined),
  255. {
  256. ...provenanceEvent(SessionSeq(baseSeq + 2), [baseSeq]),
  257. surfaceOp: surfaceOp({ op: 'replace', start: baseSeq, end: baseSeq }),
  258. },
  259. ] as SessionEvent[]
  260. const surface = new SurfaceManager(events, SessionLogOffset(baseSeq))
  261. expect(surface.nodes).toEqual([baseSeq + 2, baseSeq + 1])
  262. expect(surface.replaceGeneration).toBe(1)
  263. })
  264. it('validates tool-result rewrites against a nonzero window offset', () => {
  265. const baseSeq = 400_000
  266. const original = toolResultEvent(SessionSeq(baseSeq), 'call')
  267. const events: SessionEvent[] = [
  268. original,
  269. {
  270. ...original,
  271. seq: SessionSeq(baseSeq + 1),
  272. time: baseSeq + 1,
  273. surfaceOp: surfaceOp({ op: 'replace', start: baseSeq, end: baseSeq }),
  274. sourceEventSeqs: sourceSeqs(baseSeq),
  275. } as SessionEvent,
  276. ]
  277. expect(new SurfaceManager(events, SessionLogOffset(baseSeq)).nodes).toEqual([baseSeq + 1])
  278. })
  279. it('rejects a replacement that crosses a loaded window head', () => {
  280. const baseSeq = 400_000
  281. const events = [
  282. provenanceEvent(SessionSeq(baseSeq), undefined),
  283. {
  284. ...provenanceEvent(SessionSeq(baseSeq + 1), [baseSeq - 1, baseSeq]),
  285. surfaceOp: surfaceOp({ op: 'replace', start: baseSeq - 1, end: baseSeq }),
  286. },
  287. ] as SessionEvent[]
  288. expect(() => new SurfaceManager(events, SessionLogOffset(baseSeq)).nodes)
  289. .toThrow(`surface replace: start seq ${baseSeq - 1} not found in surface`)
  290. })
  291. it('shares ordered entries and nested replacement ranges with foldSurface', () => {
  292. const s = Session.create(SessionId('shared-fold'))
  293. s.append('user/message', createUserMessage({
  294. content: [{ type: 'text', text: 'a' }], source: { kind: 'user' },
  295. }), { surfaceOp: 'append' })
  296. s.append('user/message', createUserMessage({
  297. content: [{ type: 'text', text: 'b' }], source: { kind: 'user' },
  298. }), { surfaceOp: 'append' })
  299. s.append('user/message', replacementMessage('summary'), {
  300. surfaceOp: surfaceOp({ op: 'replace', start: 0, end: 0 }),
  301. sourceEventSeqs: sourceSeqs(0),
  302. })
  303. s.append('user/message', replacementMessage('summary 2'), {
  304. surfaceOp: surfaceOp({ op: 'replace', start: 2, end: 1 }),
  305. sourceEventSeqs: sourceSeqs(2, 1),
  306. })
  307. const folded = foldSurface(s.snapshotEvents())
  308. expect(folded.nodes).toEqual(s.surface.nodes)
  309. expect(folded.replacements).toEqual([
  310. { seq: 2, start: 0, end: 0, shadowedSeqs: [0] },
  311. { seq: 3, start: 2, end: 1, shadowedSeqs: [2, 1] },
  312. ])
  313. folded.nodes[0] = SessionSeq(99)
  314. folded.replacements[0]!.shadowedSeqs.push(SessionSeq(99))
  315. expect(s.surface.nodes).toEqual([3])
  316. expect(foldSurface(s.snapshotEvents()).nodes).toEqual([3])
  317. expect(foldSurface(s.snapshotEvents()).replacements[0]!.shadowedSeqs).toEqual([0])
  318. })
  319. it('does not retain fold-only replacement history in incremental state', () => {
  320. const s = Session.create(SessionId('incremental-state'))
  321. s.append('user/message', createUserMessage({
  322. content: [{ type: 'text', text: 'a' }], source: { kind: 'user' },
  323. }), { surfaceOp: 'append' })
  324. s.append('user/message', replacementMessage('b'), {
  325. surfaceOp: surfaceOp({ op: 'replace', start: 0, end: 0 }),
  326. sourceEventSeqs: sourceSeqs(0),
  327. })
  328. expect(s.surface.nodes).toEqual([1])
  329. const manager = s.surface as unknown as { _state: object }
  330. expect(Object.hasOwn(manager._state, 'replacements')).toBe(false)
  331. expect(foldSurface(s.snapshotEvents()).replacements).toEqual([
  332. { seq: 1, start: 0, end: 0, shadowedSeqs: [0] },
  333. ])
  334. })
  335. it('foldSurface reports the same invalid replacement failures as the incremental manager', () => {
  336. const events = [
  337. provenanceEvent(SessionSeq(0), undefined),
  338. { ...provenanceEvent(SessionSeq(1), [0]), surfaceOp: { op: 'replace', start: 42, end: 0 } },
  339. ] as SessionEvent[]
  340. expect(() => foldSurface(events)).toThrow(/start seq 42 not found/)
  341. expect(() => Session.create(SessionId('shared-fold-invalid'), events))
  342. .toThrow(/start seq 42 not found/)
  343. })
  344. it('rejects negative-zero replacement event sequences', () => {
  345. const event = {
  346. ...provenanceEvent(SessionSeq(0), [0]),
  347. surfaceOp: { op: 'replace', start: -0, end: -0 },
  348. } as SessionEvent
  349. expect(() => foldSurface([event])).toThrow(/invalid replace surfaceOp/)
  350. })
  351. it('leaves incremental state unchanged when candidate validation fails', () => {
  352. const s = Session.create(SessionId('atomic-validation'))
  353. s.append('user/message', createUserMessage({
  354. content: [{ type: 'text', text: 'a' }], source: { kind: 'user' },
  355. }), { surfaceOp: 'append' })
  356. const surface = s.surface
  357. const nodes = surface.nodes
  358. expect(nodes).toEqual(foldSurface(s.snapshotEvents()).nodes)
  359. expect(surface.replaceGeneration).toBe(0)
  360. expect(() => s.append(
  361. 'assistant/message',
  362. {
  363. stream: [],
  364. turn: 1, step: 1,
  365. message: createMessage({
  366. role: 'assistant',
  367. content: [{ type: 'text', text: 'invalid' }],
  368. source: {
  369. kind: 'model',
  370. ...{ provider: 'mock', model: 'mock' },
  371. },
  372. }),
  373. },
  374. { surfaceOp: surfaceOp({ op: 'replace', start: 0, end: 0 }) },
  375. )).toThrow(/missing 0/)
  376. expect(s.snapshotEvents()).toHaveLength(1)
  377. expect(s.surface).toBe(surface)
  378. expect(surface.nodes).toEqual([0])
  379. expect(surface.replaceGeneration).toBe(0)
  380. expect(surface.nodes).toEqual(foldSurface(s.snapshotEvents()).nodes)
  381. s.append('user/message', createUserMessage({
  382. content: [{ type: 'text', text: 'b' }], source: { kind: 'user' },
  383. }), { surfaceOp: 'append' })
  384. expect(surface.nodes).toBe(nodes)
  385. expect(surface.nodes).toEqual([0, 1])
  386. expect(surface.replaceGeneration).toBe(0)
  387. expect(surface.nodes).toEqual(foldSurface(s.snapshotEvents()).nodes)
  388. })
  389. it('foldSurface rejects a surface-eligible event without its mandatory marker', () => {
  390. const malformed: SessionEvent = {
  391. type: 'user/message',
  392. seq: SessionSeq(0),
  393. time: 1,
  394. data: createUserMessage({
  395. content: [{ type: 'text', text: 'hidden' }], source: { kind: 'user' },
  396. }),
  397. }
  398. expect(() => foldSurface([malformed]))
  399. .toThrow(/surface-eligible and requires a surfaceOp marker/)
  400. })
  401. it('foldSurface rejects surfaceOp on a non-surface event', () => {
  402. const malformed = {
  403. type: 'turn/start',
  404. seq: SessionSeq(0),
  405. time: 1,
  406. data: { turn: 1 },
  407. surfaceOp: 'append',
  408. } as unknown as SessionEvent
  409. expect(() => foldSurface([malformed]))
  410. .toThrow(/not surface-eligible and cannot carry surfaceOp/)
  411. })
  412. it('folds an ordered sequence list from surfaceOp: append markers', () => {
  413. const s = surfaceSession()
  414. const nodes = s.surface.nodes
  415. // Only the user/message and assistant/message carry surfaceOp: 'append'.
  416. // The turn boundaries do not have surface markers.
  417. expect(nodes).toEqual([1, 2])
  418. })
  419. it('empty surface yields empty nodes', () => {
  420. const s = Session.create(SessionId('empty'))
  421. s.append('turn/start', { turn: 1 })
  422. s.append('step/start', { turn: 1, step: 1 })
  423. s.append('step/end', { turn: 1, step: 1 })
  424. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  425. expect(s.surface.nodes.length).toBe(0)
  426. expect(s.deriveMessages()).toEqual([])
  427. })
  428. it('picks up new events incrementally (delta processing)', () => {
  429. const s = surfaceSession()
  430. expect(s.surface.nodes.length).toBe(2)
  431. s.append('tool/result', {
  432. turn: 1, step: 1,
  433. message: createToolResultMessage({
  434. callId: ToolCallId('c1'),
  435. content: [{ type: 'text', text: 'ok' }],
  436. isError: false,
  437. }),
  438. }, { surfaceOp: 'append' })
  439. expect(s.surface.nodes.length).toBe(3)
  440. expect(s.surface.nodes[2]!).toBe(4) // seq 4: after turn/end at seq 3
  441. })
  442. it('replays identically from a seeded log with surface markers', () => {
  443. const original = surfaceSession()
  444. original.append('tool/result', {
  445. turn: 1, step: 1,
  446. message: createToolResultMessage({
  447. callId: ToolCallId('c1'),
  448. content: [{ type: 'text', text: 'ok' }],
  449. isError: false,
  450. }),
  451. }, { surfaceOp: 'append' })
  452. const replayed = Session.create(SessionId('replay'), original.snapshotEvents())
  453. expect(replayed.surface.nodes).toEqual([1, 2, 4])
  454. expect(replayed.deriveMessages()).toEqual(original.deriveMessages())
  455. })
  456. it('rebuild with replace operation splices out shadowed nodes', () => {
  457. const s = surfaceSession()
  458. s.append('user/message',
  459. replacementMessage('summary'),
  460. { surfaceOp: surfaceOp({ op: 'replace', start: 1, end: 2 }), sourceEventSeqs: sourceSeqs(1, 2) },
  461. )
  462. expect(s.surface.nodes).toEqual([4])
  463. })
  464. it('replace with both ends at real nodes splices only the range', () => {
  465. const s = Session.create(SessionId('range'))
  466. s.append('user/message', createUserMessage({
  467. content: [{ type: 'text', text: 'a' }], source: { kind: 'user' },
  468. }), { surfaceOp: 'append' }) // seq 0
  469. s.append('user/message', createUserMessage({
  470. content: [{ type: 'text', text: 'b' }], source: { kind: 'user' },
  471. }), { surfaceOp: 'append' }) // seq 1
  472. s.append('user/message', createUserMessage({
  473. content: [{ type: 'text', text: 'c' }], source: { kind: 'user' },
  474. }), { surfaceOp: 'append' }) // seq 2
  475. // Replace seq 0 through 1 inclusive: shadow a and b, keep c.
  476. s.append('user/message',
  477. replacementMessage('summary'),
  478. { surfaceOp: surfaceOp({ op: 'replace', start: 0, end: 1 }), sourceEventSeqs: sourceSeqs(0, 1) },
  479. ) // seq 3
  480. expect(s.surface.nodes).toEqual([3, 2])
  481. })
  482. it('single-node replacement (start === end)', () => {
  483. const s = Session.create(SessionId('single'))
  484. s.append('user/message', createUserMessage({
  485. content: [{ type: 'text', text: 'a' }], source: { kind: 'user' },
  486. }), { surfaceOp: 'append' }) // seq 0
  487. s.append('user/message', createUserMessage({
  488. content: [{ type: 'text', text: 'b' }], source: { kind: 'user' },
  489. }), { surfaceOp: 'append' }) // seq 1
  490. // Replace only seq 1 (single node).
  491. s.append('user/message',
  492. replacementMessage('x'),
  493. { surfaceOp: surfaceOp({ op: 'replace', start: 1, end: 1 }), sourceEventSeqs: sourceSeqs(1) },
  494. ) // seq 2
  495. expect(s.surface.nodes).toEqual([0, 2])
  496. })
  497. it('throws when replace start is not found', () => {
  498. const s = Session.create(SessionId('bad-start'))
  499. s.append('user/message', createUserMessage({
  500. content: [{ type: 'text', text: 'a' }], source: { kind: 'user' },
  501. }), { surfaceOp: 'append' }) // seq 0
  502. expect(() => s.append('user/message',
  503. replacementMessage('y'),
  504. { surfaceOp: surfaceOp({ op: 'replace', start: 5, end: 0 }), sourceEventSeqs: sourceSeqs(0) },
  505. )).toThrow(/surface replace: start seq 5 not found/)
  506. })
  507. it('throws when replace end is not found', () => {
  508. const s = Session.create(SessionId('bad-end'))
  509. s.append('user/message', createUserMessage({
  510. content: [{ type: 'text', text: 'a' }], source: { kind: 'user' },
  511. }), { surfaceOp: 'append' }) // seq 0
  512. expect(() => s.append('user/message',
  513. replacementMessage('y'),
  514. { surfaceOp: surfaceOp({ op: 'replace', start: 0, end: 99 }), sourceEventSeqs: sourceSeqs(0) },
  515. )).toThrow(/surface replace: end seq 99 not found/)
  516. })
  517. it('throws when start is after end', () => {
  518. const s = Session.create(SessionId('reversed'))
  519. s.append('user/message', createUserMessage({
  520. content: [{ type: 'text', text: 'a' }], source: { kind: 'user' },
  521. }), { surfaceOp: 'append' }) // seq 0
  522. s.append('user/message', createUserMessage({
  523. content: [{ type: 'text', text: 'b' }], source: { kind: 'user' },
  524. }), { surfaceOp: 'append' }) // seq 1
  525. // start=1, end=0 would be reversed order.
  526. expect(() => s.append('user/message',
  527. replacementMessage('y'),
  528. { surfaceOp: surfaceOp({ op: 'replace', start: 1, end: 0 }), sourceEventSeqs: sourceSeqs(1, 0) },
  529. )).toThrow(/start seq 1.*after end seq 0/)
  530. })
  531. it('sourceEventSeqs is snapshot so caller mutation does not affect logged event', () => {
  532. const s = Session.create(SessionId('immutable'))
  533. s.append('user/message', createUserMessage({
  534. content: [{ type: 'text', text: 'source' }], source: { kind: 'user' },
  535. }), { surfaceOp: 'append' })
  536. const sources = sourceSeqs(0)
  537. s.append('user/message', replacementMessage('h'), {
  538. surfaceOp: 'append',
  539. sourceEventSeqs: sources,
  540. })
  541. // Mutate caller's array after append.
  542. sources.push(SessionSeq(1))
  543. sources[0] = SessionSeq(99)
  544. const logged = s.snapshotEvents()[1]! as SurfaceEvent
  545. expect(logged.sourceEventSeqs).toEqual([0])
  546. })
  547. it('replace starting at non-head position preserves surrounding order', () => {
  548. const s = Session.create(SessionId('mid-replace'))
  549. s.append('user/message', createUserMessage({
  550. content: [{ type: 'text', text: 'a' }], source: { kind: 'user' },
  551. }), { surfaceOp: 'append' }) // seq 0
  552. s.append('user/message', createUserMessage({
  553. content: [{ type: 'text', text: 'b' }], source: { kind: 'user' },
  554. }), { surfaceOp: 'append' }) // seq 1
  555. s.append('user/message', createUserMessage({
  556. content: [{ type: 'text', text: 'c' }], source: { kind: 'user' },
  557. }), { surfaceOp: 'append' }) // seq 2
  558. // Replace the middle node (seq 1) only, keeping seq 0 and seq 2.
  559. s.append('user/message',
  560. replacementMessage('x'),
  561. { surfaceOp: surfaceOp({ op: 'replace', start: 1, end: 1 }), sourceEventSeqs: sourceSeqs(1) },
  562. ) // seq 3
  563. expect(s.surface.nodes).toEqual([0, 3, 2])
  564. })
  565. it('surfaceOp replace object is snapshot so caller mutation is isolated', () => {
  566. const s = Session.create(SessionId('immutable-op'))
  567. s.append('user/message', createUserMessage({
  568. content: [{ type: 'text', text: 'a' }], source: { kind: 'user' },
  569. }), { surfaceOp: 'append' })
  570. const op = { op: 'replace' as const, start: SessionSeq(0), end: SessionSeq(0) }
  571. s.append('user/message', replacementMessage('s'), {
  572. surfaceOp: op,
  573. sourceEventSeqs: sourceSeqs(0),
  574. })
  575. // Mutate caller's object after append.
  576. op.start = SessionSeq(99)
  577. const logged = s.snapshotEvents()[1]! as SurfaceEvent
  578. expect(logged.surfaceOp).toEqual({ op: 'replace', start: 0, end: 0 })
  579. })
  580. })
  581. describe('deriveMessages with surface', () => {
  582. it('uses the surface path when surface markers are present', () => {
  583. const s = surfaceSession()
  584. const messages = s.deriveMessages()
  585. expect(messages).toHaveLength(2)
  586. expect(messages[0]!.role).toBe('user')
  587. expect(messages[0]!.content[0]).toMatchObject({ type: 'text', text: 'hello' })
  588. expect(messages[1]!.role).toBe('assistant')
  589. expect(messages[1]!.content[0]).toMatchObject({ type: 'text', text: 'hi' })
  590. })
  591. it('surface path skips non-surface events (attempts, boundaries)', () => {
  592. const s = Session.create(SessionId('filter'))
  593. s.append('turn/start', { turn: 1 })
  594. s.append('assistant/attempt', {
  595. turn: 1, step: 1,
  596. stream: [{ type: 'text-chunks', time0: 1, index: 0, dt: [1], texts: ['h', 'i'] }],
  597. })
  598. s.append('user/message', createUserMessage({
  599. content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' },
  600. }), { surfaceOp: 'append' })
  601. s.append('assistant/message', {
  602. stream: [],
  603. turn: 1, step: 1,
  604. message: createMessage({
  605. role: 'assistant',
  606. content: [{ type: 'text', text: 'hi' }],
  607. source: {
  608. kind: 'model',
  609. ...{ provider: 'mock', model: 'mock' },
  610. },
  611. }),
  612. }, { surfaceOp: 'append' })
  613. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  614. // Attempts and boundaries are NOT in the surface, so only 2 messages.
  615. expect(s.deriveMessages()).toHaveLength(2)
  616. })
  617. it('deriveMessages via surface respects replace (shadowed nodes are excluded)', () => {
  618. const s = Session.create(SessionId('compacted'))
  619. s.append('user/message', createUserMessage({
  620. content: [{ type: 'text', text: 'original' }], source: { kind: 'user' },
  621. }), { surfaceOp: 'append' })
  622. s.append('user/message', replacementMessage('compacted'), {
  623. surfaceOp: surfaceOp({ op: 'replace', start: 0, end: 0 }),
  624. sourceEventSeqs: sourceSeqs(0),
  625. })
  626. // Only the compaction node is visible.
  627. const messages = s.deriveMessages()
  628. expect(messages).toHaveLength(1)
  629. expect(messages[0]!.content[0]).toMatchObject({ type: 'text', text: 'compacted' })
  630. })
  631. it('injected-context and user messages appear on surface', () => {
  632. const s = Session.create(SessionId('ctx'))
  633. s.append('user/message', createUserMessage({
  634. content: [{ type: 'text', text: 'file changed' }], source: { kind: 'plugin', plugin: 'watcher' },
  635. }), { surfaceOp: 'append' })
  636. s.append('user/message', createUserMessage({
  637. content: [{ type: 'text', text: 'focus' }],
  638. source: { kind: 'user' },
  639. }), { surfaceOp: 'append' })
  640. const messages = s.deriveMessages()
  641. expect(messages).toHaveLength(2)
  642. expect(messages[0]!.content).toEqual([{ type: 'text', text: 'file changed' }])
  643. expect(messages[1]!.content).toEqual([{ type: 'text', text: 'focus' }])
  644. })
  645. })
  646. describe('Session.append surface opts', () => {
  647. it('records sourceEventSeqs and surfaceOp on a source-derived event', () => {
  648. const s = Session.create(SessionId('opts'))
  649. s.append('turn/start', { turn: 1 })
  650. s.append('step/start', { turn: 1, step: 1 })
  651. const event = s.append(
  652. 'user/message',
  653. createUserMessage({
  654. content: [{ type: 'text', text: 'h' }],
  655. source: { kind: 'plugin', plugin: 'test' },
  656. }),
  657. { surfaceOp: 'append', sourceEventSeqs: sourceSeqs(0, 1) },
  658. )
  659. expect(event.sourceEventSeqs).toEqual([0, 1])
  660. expect(event.surfaceOp).toBe('append')
  661. // The logged event matches the returned event.
  662. expect((s.snapshotEvents()[2]! as SurfaceEvent).sourceEventSeqs).toEqual([0, 1])
  663. expect((s.snapshotEvents()[2]! as SurfaceEvent).surfaceOp).toBe('append')
  664. })
  665. it('deriveMessages skips a surface node that derives to null (empty assistant/message)', () => {
  666. // An empty-content assistant/message is surface-eligible (it can host usage)
  667. // but _deriveOneMessage returns null for it, so the surface derivation path's
  668. // null-check is exercised — the node is on the surface yet produces no message.
  669. const seed: SessionEvent[] = [
  670. { type: 'turn/start', seq: SessionSeq(0), time: 1, data: { turn: 1 } },
  671. { type: 'step/start', seq: SessionSeq(1), time: 2, data: { turn: 1, step: 1 } },
  672. { type: 'assistant/message', seq: SessionSeq(2), time: 3, data: {
  673. stream: [],
  674. turn: 1, step: 1,
  675. message: createMessage({
  676. role: 'assistant',
  677. content: [],
  678. source: {
  679. kind: 'model',
  680. ...{ provider: 'mock', model: 'mock' },
  681. },
  682. }),
  683. }, surfaceOp: 'append' },
  684. { type: 'step/end', seq: SessionSeq(3), time: 4, data: { turn: 1, step: 1 } },
  685. { type: 'turn/end', seq: SessionSeq(4), time: 5, data: { turn: 1, reason: { kind: 'completed' } } },
  686. ]
  687. const s = Session.create(SessionId('nomessage'), seed)
  688. // The empty assistant/message is on the surface but _deriveOneMessage returns null for it.
  689. expect(s.deriveMessages()).toHaveLength(0)
  690. })
  691. it('a non-surface event carries no surface fields', () => {
  692. const s = Session.create(SessionId('noopts'))
  693. s.append('turn/start', { turn: 1 })
  694. expect((s.snapshotEvents()[0] as SessionEvent<SurfaceEventType>).sourceEventSeqs).toBeUndefined()
  695. expect((s.snapshotEvents()[0] as SessionEvent<SurfaceEventType>).surfaceOp).toBeUndefined()
  696. })
  697. it('surfaceOp primitives are not cloned (they are immutable)', () => {
  698. const s = Session.create(SessionId('prim'))
  699. const event = s.append('assistant/message', {
  700. stream: [],
  701. turn: 1, step: 1,
  702. message: createMessage({
  703. role: 'assistant',
  704. content: [],
  705. source: {
  706. kind: 'model',
  707. ...{ provider: 'mock', model: 'mock' },
  708. },
  709. }),
  710. }, { surfaceOp: 'append' })
  711. // The string 'append' is a primitive — identity-preserving is fine.
  712. expect(event.surfaceOp).toBe('append')
  713. })
  714. it('isSurfaceEvent rejects a surface-eligible type missing its surfaceOp marker', () => {
  715. // A raw event (not built via append, which mandates the marker) of a
  716. // surface-eligible type but with no surfaceOp must NOT narrow to a
  717. // SurfaceEvent — it would otherwise be silently dropped from the surface.
  718. const noMarker: SessionEvent = {
  719. type: 'user/message', seq: SessionSeq(0), time: 1,
  720. data: createUserMessage({
  721. content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' },
  722. }),
  723. }
  724. expect(isSurfaceEvent(noMarker)).toBe(false)
  725. // A non-surface type is rejected too (the type gate).
  726. const boundary: SessionEvent = { type: 'turn/start', seq: SessionSeq(1), time: 1, data: { turn: 1 } }
  727. expect(isSurfaceEvent(boundary)).toBe(false)
  728. // A properly-marked surface event narrows.
  729. const marked = { ...noMarker, surfaceOp: 'append' } as SurfaceEvent
  730. expect(isSurfaceEvent(marked)).toBe(true)
  731. })
  732. })
  733. describe('surface type guards', () => {
  734. it('isSurfaceEligibleType is true only for message-producing types', () => {
  735. expect(isSurfaceEligibleType('user/message')).toBe(true)
  736. expect(isSurfaceEligibleType('assistant/message')).toBe(true)
  737. expect(isSurfaceEligibleType('tool/result')).toBe(true)
  738. expect(isSurfaceEligibleType('turn/start')).toBe(false)
  739. expect(isSurfaceEligibleType('assistant/attempt')).toBe(false)
  740. })
  741. it('isSurfaceEvent narrows a fully-formed surface event', () => {
  742. const s = surfaceSession()
  743. const userMessage = s.snapshotEvents().find(e => e.type === 'user/message')!
  744. expect(isSurfaceEvent(userMessage)).toBe(true)
  745. })
  746. it('isSurfaceEvent rejects a non-surface-eligible type', () => {
  747. const s = surfaceSession()
  748. const turnStart = s.snapshotEvents().find(e => e.type === 'turn/start')!
  749. expect(isSurfaceEvent(turnStart)).toBe(false)
  750. })
  751. it('isSurfaceEvent rejects a surface-eligible type missing its surfaceOp marker', () => {
  752. // A surface-eligible type whose mandatory surfaceOp is absent — the state a
  753. // seed/load log can carry before the marker is validated. surfaceOp is
  754. // optional on SessionEvent, so this is a representable runtime value.
  755. const markerless: SessionEvent = {
  756. type: 'user/message',
  757. seq: SessionSeq(0),
  758. time: 0,
  759. data: createUserMessage({
  760. content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' },
  761. }),
  762. }
  763. expect(isSurfaceEligibleType(markerless.type)).toBe(true)
  764. expect(isSurfaceEvent(markerless)).toBe(false)
  765. })
  766. it('splits surface events into append-origin and replacement by their marker', () => {
  767. const s = surfaceSession()
  768. s.append('user/message', createUserMessage({
  769. content: [{ type: 'text', text: 'checkpoint' }], source: { kind: 'plugin', plugin: 'compact' },
  770. }), { surfaceOp: surfaceOp({ op: 'replace', start: 1, end: 2 }), sourceEventSeqs: sourceSeqs(1, 2) })
  771. const appended = s.snapshotEvents().find(e => e.type === 'user/message')!
  772. const replacement = s.snapshotEvents().at(-1)!
  773. expect(isAppendSurfaceEvent(appended)).toBe(true)
  774. expect(isReplacementSurfaceEvent(appended)).toBe(false)
  775. expect(isAppendSurfaceEvent(replacement)).toBe(false)
  776. expect(isReplacementSurfaceEvent(replacement)).toBe(true)
  777. })
  778. it('rejects log-only and markerless events from both marker guards', () => {
  779. const s = surfaceSession()
  780. const turnStart = s.snapshotEvents().find(e => e.type === 'turn/start')!
  781. // A surface-eligible type whose mandatory marker is absent has no origin at
  782. // all: it never entered the surface.
  783. const markerless: SessionEvent = {
  784. type: 'user/message',
  785. seq: SessionSeq(0),
  786. time: 0,
  787. data: createUserMessage({
  788. content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' },
  789. }),
  790. }
  791. expect(isAppendSurfaceEvent(turnStart)).toBe(false)
  792. expect(isReplacementSurfaceEvent(turnStart)).toBe(false)
  793. expect(isAppendSurfaceEvent(markerless)).toBe(false)
  794. expect(isReplacementSurfaceEvent(markerless)).toBe(false)
  795. })
  796. })
  797. describe('SurfaceManager.replaceGeneration', () => {
  798. it('folds the pending log delta on access and counts replaces', () => {
  799. const s = Session.create(SessionId('gen'))
  800. s.append('turn/start', { turn: 1 })
  801. s.append('user/message', createUserMessage({
  802. content: [{ type: 'text', text: 'one' }], source: { kind: 'user' },
  803. }), { surfaceOp: 'append' })
  804. s.append('user/message', createUserMessage({
  805. content: [{ type: 'text', text: 'two' }], source: { kind: 'user' },
  806. }), { surfaceOp: 'append' })
  807. // Read the generation FIRST — before nodes — so the getter itself folds
  808. // the pending delta rather than piggybacking on a nodes read.
  809. expect(s.surface.replaceGeneration).toBe(0)
  810. const nodes = s.surface.nodes
  811. s.append('user/message', createUserMessage({
  812. content: [{ type: 'text', text: 'summary' }], source: { kind: 'plugin', plugin: 'compact' },
  813. }), { surfaceOp: { op: 'replace', start: nodes[0]!, end: nodes[1]! }, sourceEventSeqs: [nodes[0]!, nodes[1]!] })
  814. expect(s.surface.replaceGeneration).toBe(1)
  815. })
  816. })
  817. describe('system/message surface node', () => {
  818. function systemEvent(seq: number, text: string, op: TestSurfaceOp = 'append', sources?: number[]): SessionEvent {
  819. return {
  820. type: 'system/message',
  821. seq: SessionSeq(seq),
  822. time: seq,
  823. data: { turn: 1, step: 1, message: createSystemMessage(text, 'test-plugin') },
  824. surfaceOp: surfaceOp(op),
  825. ...sources === undefined ? {} : { sourceEventSeqs: sourceSeqs(...sources) },
  826. }
  827. }
  828. function userEvent(seq: number, op: TestSurfaceOp = 'append', sources?: number[]): SessionEvent {
  829. return {
  830. type: 'user/message',
  831. seq: SessionSeq(seq),
  832. time: seq,
  833. data: createUserMessage({ content: [{ type: 'text', text: `u${seq}` }], source: { kind: 'user' } }),
  834. surfaceOp: surfaceOp(op),
  835. ...sources === undefined ? {} : { sourceEventSeqs: sourceSeqs(...sources) },
  836. }
  837. }
  838. it('projects a system node as the leading system-role message and an empty one as no message', () => {
  839. const s = Session.create(SessionId('sys'))
  840. s.append('turn/start', { turn: 1 })
  841. s.append('step/start', { turn: 1, step: 1 })
  842. s.append('system/message', { turn: 1, step: 1, message: createSystemMessage('be brief', 'p') }, { surfaceOp: 'append' })
  843. s.append('user/message', createUserMessage({ content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }), { surfaceOp: 'append' })
  844. expect(s.deriveMessages().map(message => message.role)).toEqual(['system', 'user'])
  845. expect(s.deriveMessages()[0]?.content).toEqual([{ type: 'text', text: 'be brief' }])
  846. const head = s.surface.nodes[0] as SessionSeq
  847. s.append('system/message', { turn: 1, step: 1, message: createSystemMessage('', 'p') }, {
  848. surfaceOp: { op: 'replace', start: head, end: head },
  849. sourceEventSeqs: [head],
  850. })
  851. expect(s.deriveMessages().map(message => message.role)).toEqual(['user'])
  852. expect(s.surface.nodes).toHaveLength(2)
  853. })
  854. it('replaces node 0 with a new system node and rejects other rewrites of the head', () => {
  855. const replaced = foldSurface([
  856. systemEvent(0, 'v1'), userEvent(1), systemEvent(2, 'v2', { op: 'replace', start: 0, end: 0 }, [0]),
  857. ])
  858. expect(replaced.nodes).toEqual(sourceSeqs(2, 1))
  859. expect(() => foldSurface([
  860. systemEvent(0, 'v1'), userEvent(1),
  861. userEvent(2, { op: 'replace', start: 0, end: 1 }, [0, 1]),
  862. ])).toThrow(/node 0 holds the system prompt/)
  863. expect(() => foldSurface([
  864. systemEvent(0, 'v1'), userEvent(1), systemEvent(2, 'v2', { op: 'replace', start: 0, end: 1 }, [0, 1]),
  865. ])).toThrow(/node 0 holds the system prompt/)
  866. })
  867. it('leaves later system nodes and a non-system head unprotected', () => {
  868. const later = foldSurface([
  869. systemEvent(0, 'v1'), userEvent(1), systemEvent(2, 'v2'), userEvent(3),
  870. userEvent(4, { op: 'replace', start: 1, end: 3 }, [1, 2, 3]),
  871. ])
  872. expect(later.nodes).toEqual(sourceSeqs(0, 4))
  873. const plainHead = foldSurface([
  874. userEvent(0), userEvent(1),
  875. userEvent(2, { op: 'replace', start: 0, end: 1 }, [0, 1]),
  876. ])
  877. expect(plainHead.nodes).toEqual(sourceSeqs(2))
  878. })
  879. it('rejects a seeded system/message with a non-system role or non-plugin source', () => {
  880. const good = systemEvent(0, 'v1')
  881. const badRole = { ...good, data: { ...good.data, message: { ...(good.data as { message: object }).message, role: 'user' } } }
  882. expect(() => Session.create(SessionId('bad-role'), [badRole as SessionEvent])).toThrow(/role "system"/)
  883. const badSource = { ...good, data: { ...good.data, message: { ...(good.data as { message: object }).message, source: { kind: 'user' } } } }
  884. expect(() => Session.create(SessionId('bad-source'), [badSource as SessionEvent])).toThrow(/plugin source/)
  885. })
  886. })