surface.spec.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. import { describe, expect, it } from 'vitest'
  2. import type { SessionEvent, SurfaceEvent, SurfaceEventType } from '@deepseek-ai/dsh-session'
  3. import { Session, SessionId, foldSurface, isSurfaceEligibleType, isSurfaceEvent } from '@deepseek-ai/dsh-session'
  4. import { CallId } from '@deepseek-ai/dsh-llm'
  5. /** Build a minimal session with turn boundaries and a single user message. */
  6. function surfaceSession(): Session {
  7. const s = new Session(SessionId('ss'))
  8. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  9. s.append('user/message', { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  10. s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 'hi' }] }, { surfaceOp: 'append' })
  11. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  12. return s
  13. }
  14. describe('SurfaceManager', () => {
  15. it('shares exact nodes and nested replacement ranges with foldSurface', () => {
  16. const s = new Session(SessionId('shared-fold'))
  17. s.append('user/message', { content: [{ type: 'text', text: 'a' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  18. s.append('user/message', { content: [{ type: 'text', text: 'b' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  19. s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 'summary' }] }, { surfaceOp: { op: 'replace', start: 0, end: 0 }, sourceEventSeqs: [0] })
  20. s.append('assistant/message', { turn: 1, step: 2, content: [{ type: 'text', text: 'summary 2' }] }, { surfaceOp: { op: 'replace', start: 2, end: 1 }, sourceEventSeqs: [2, 1] })
  21. const folded = foldSurface(s.events)
  22. expect(folded.nodes).toEqual(s.surface.nodes)
  23. expect(folded.replacements).toEqual([
  24. { seq: 2, start: 0, end: 0, shadowedSeqs: [0] },
  25. { seq: 3, start: 2, end: 1, shadowedSeqs: [2, 1] },
  26. ])
  27. folded.nodes[0]!.next = 99
  28. folded.replacements[0]!.shadowedSeqs.push(99)
  29. expect(s.surface.nodes).toEqual([{ seq: 3, prev: null, next: null }])
  30. expect(foldSurface(s.events).replacements[0]!.shadowedSeqs).toEqual([0])
  31. })
  32. it('does not retain fold-only replacement history in incremental state', () => {
  33. const s = new Session(SessionId('incremental-state'))
  34. s.append('user/message', { content: [{ type: 'text', text: 'a' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  35. s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 'b' }] }, { surfaceOp: { op: 'replace', start: 0, end: 0 } })
  36. expect(s.surface.nodes).toEqual([{ seq: 1, prev: null, next: null }])
  37. const manager = s.surface as unknown as { _state: object }
  38. expect(Object.hasOwn(manager._state, 'replacements')).toBe(false)
  39. expect(foldSurface(s.events).replacements).toEqual([
  40. { seq: 1, start: 0, end: 0, shadowedSeqs: [0] },
  41. ])
  42. })
  43. it('foldSurface reports the same invalid replacement failures as the incremental manager', () => {
  44. const s = new Session(SessionId('shared-fold-invalid'))
  45. s.append('user/message', { content: [{ type: 'text', text: 'a' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  46. s.append('assistant/message', { turn: 1, step: 1, content: [] }, { surfaceOp: { op: 'replace', start: 42, end: 0 }, sourceEventSeqs: [0] })
  47. expect(() => foldSurface(s.events)).toThrow(/start seq 42 not found/)
  48. expect(() => s.surface.nodes).toThrow(/start seq 42 not found/)
  49. })
  50. it('foldSurface rejects a surface-eligible event without its mandatory marker', () => {
  51. const malformed: SessionEvent = {
  52. type: 'user/message',
  53. seq: 0,
  54. time: 1,
  55. data: { content: [{ type: 'text', text: 'hidden' }], source: { kind: 'user' } },
  56. }
  57. expect(() => foldSurface([malformed]))
  58. .toThrow(/surface event "user\/message" \(seq 0\) carries no surfaceOp marker/)
  59. })
  60. it('rebuilds a linked list from surfaceOp: append markers', () => {
  61. const s = surfaceSession()
  62. const nodes = s.surface.nodes
  63. // Only the user/message and assistant/message carry surfaceOp: 'append'.
  64. // The turn boundaries do not have surface markers.
  65. expect(nodes.length).toBe(2)
  66. expect(nodes[0]!.seq).toBe(1) // user/message (turn/start is seq 0)
  67. expect(nodes[0]!.prev).toBeNull()
  68. expect(nodes[0]!.next).toBe(2) // assistant/message (seq 2)
  69. expect(nodes[1]!.seq).toBe(2)
  70. expect(nodes[1]!.prev).toBe(1)
  71. expect(nodes[1]!.next).toBeNull()
  72. })
  73. it('empty surface yields empty nodes', () => {
  74. const s = new Session(SessionId('empty'))
  75. // Only turn boundaries, no surface nodes.
  76. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  77. s.append('step/start', { turn: 1, step: 1 })
  78. s.append('step/end', { turn: 1, step: 1 })
  79. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  80. expect(s.surface.nodes.length).toBe(0)
  81. // deriveMessages returns empty array
  82. expect(s.deriveMessages()).toEqual([])
  83. })
  84. it('picks up new events incrementally (delta processing)', () => {
  85. const s = surfaceSession()
  86. expect(s.surface.nodes.length).toBe(2)
  87. // Append another surface node
  88. s.append('tool/result', { turn: 1, step: 1, callId: CallId('c1'), content: [{ type: 'text', text: 'ok' }], isError: false }, { surfaceOp: 'append' })
  89. expect(s.surface.nodes.length).toBe(3)
  90. expect(s.surface.nodes[2]!.seq).toBe(4) // seq 4: after turn/end at seq 3
  91. expect(s.surface.nodes[2]!.prev).toBe(2)
  92. expect(s.surface.nodes[1]!.next).toBe(4)
  93. })
  94. it('replays identically from a seeded log with surface markers', () => {
  95. const original = surfaceSession()
  96. original.append('tool/result', { turn: 1, step: 1, callId: CallId('c1'), content: [{ type: 'text', text: 'ok' }], isError: false }, { surfaceOp: 'append' })
  97. const replayed = new Session(SessionId('replay'), [...original.events])
  98. // Surface rebuilds from the seeded log's markers.
  99. expect(replayed.surface.nodes.map(n => n.seq)).toEqual([1, 2, 4])
  100. expect(replayed.deriveMessages()).toEqual(original.deriveMessages())
  101. })
  102. it('rebuild with replace operation splices out shadowed nodes', () => {
  103. const s = surfaceSession()
  104. // Replace surface seqs 1 (user) and 2 (assistant) with the summary.
  105. s.append('assistant/message',
  106. { turn: 2, step: 1, content: [{ type: 'text', text: 'summary' }] },
  107. { surfaceOp: { op: 'replace', start: 1, end: 2 }, sourceEventSeqs: [1, 2] },
  108. )
  109. expect(s.surface.nodes.length).toBe(1)
  110. expect(s.surface.nodes[0]!.seq).toBe(4) // seq of the compaction marker
  111. expect(s.surface.nodes[0]!.prev).toBeNull()
  112. expect(s.surface.nodes[0]!.next).toBeNull()
  113. })
  114. it('replace with both ends at real nodes splices only the range', () => {
  115. const s = new Session(SessionId('range'))
  116. s.append('user/message', { content: [{ type: 'text', text: 'a' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 0
  117. s.append('user/message', { content: [{ type: 'text', text: 'b' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 1
  118. s.append('user/message', { content: [{ type: 'text', text: 'c' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 2
  119. // Replace seq 0 through 1 inclusive: shadow a and b, keep c.
  120. s.append('assistant/message',
  121. { turn: 1, step: 1, content: [{ type: 'text', text: 'summary' }] },
  122. { surfaceOp: { op: 'replace', start: 0, end: 1 }, sourceEventSeqs: [0, 1] },
  123. ) // seq 3
  124. expect(s.surface.nodes.map(n => n.seq)).toEqual([3, 2])
  125. // Links: 3 ↔ 2
  126. expect(s.surface.nodes[0]!.prev).toBeNull()
  127. expect(s.surface.nodes[0]!.next).toBe(2)
  128. expect(s.surface.nodes[1]!.prev).toBe(3)
  129. expect(s.surface.nodes[1]!.next).toBeNull()
  130. })
  131. it('single-node replacement (start === end)', () => {
  132. const s = new Session(SessionId('single'))
  133. s.append('user/message', { content: [{ type: 'text', text: 'a' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 0
  134. s.append('user/message', { content: [{ type: 'text', text: 'b' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 1
  135. // Replace only seq 1 (single node).
  136. s.append('assistant/message',
  137. { turn: 1, step: 1, content: [{ type: 'text', text: 'x' }] },
  138. { surfaceOp: { op: 'replace', start: 1, end: 1 }, sourceEventSeqs: [1] },
  139. ) // seq 2
  140. expect(s.surface.nodes.map(n => n.seq)).toEqual([0, 2])
  141. expect(s.surface.nodes[0]!.next).toBe(2)
  142. expect(s.surface.nodes[1]!.prev).toBe(0)
  143. })
  144. it('throws when replace start is not found', () => {
  145. const s = new Session(SessionId('bad-start'))
  146. s.append('user/message', { content: [{ type: 'text', text: 'a' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 0
  147. s.append('assistant/message',
  148. { turn: 1, step: 1, content: [{ type: 'text', text: 'y' }] },
  149. { surfaceOp: { op: 'replace', start: 5, end: 0 }, sourceEventSeqs: [5, 0] },
  150. )
  151. expect(() => s.surface.nodes).toThrow(/surface replace: start seq 5 not found/)
  152. })
  153. it('throws when replace end is not found', () => {
  154. const s = new Session(SessionId('bad-end'))
  155. s.append('user/message', { content: [{ type: 'text', text: 'a' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 0
  156. s.append('assistant/message',
  157. { turn: 1, step: 1, content: [{ type: 'text', text: 'y' }] },
  158. { surfaceOp: { op: 'replace', start: 0, end: 99 }, sourceEventSeqs: [0] },
  159. )
  160. expect(() => s.surface.nodes).toThrow(/surface replace: end seq 99 not found/)
  161. })
  162. it('throws when start is after end', () => {
  163. const s = new Session(SessionId('reversed'))
  164. s.append('user/message', { content: [{ type: 'text', text: 'a' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 0
  165. s.append('user/message', { content: [{ type: 'text', text: 'b' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 1
  166. // start=1, end=0 would be reversed order.
  167. s.append('assistant/message',
  168. { turn: 1, step: 1, content: [{ type: 'text', text: 'y' }] },
  169. { surfaceOp: { op: 'replace', start: 1, end: 0 }, sourceEventSeqs: [1, 0] },
  170. )
  171. expect(() => s.surface.nodes).toThrow(/start seq 1.*after end seq 0/)
  172. })
  173. it('sourceEventSeqs is snapshot so caller mutation does not affect logged event', () => {
  174. const s = new Session(SessionId('immutable'))
  175. const sources = [10, 20]
  176. s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 'h' }] }, { surfaceOp: 'append', sourceEventSeqs: sources })
  177. // Mutate caller's array after append.
  178. sources.push(30)
  179. sources[0] = 99
  180. const logged = s.events[0]! as SurfaceEvent
  181. expect(logged.sourceEventSeqs).toEqual([10, 20])
  182. })
  183. it('replace starting at non-head position links to previous node correctly', () => {
  184. const s = new Session(SessionId('mid-replace'))
  185. s.append('user/message', { content: [{ type: 'text', text: 'a' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 0
  186. s.append('user/message', { content: [{ type: 'text', text: 'b' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 1
  187. s.append('user/message', { content: [{ type: 'text', text: 'c' }], source: { kind: 'user' } }, { surfaceOp: 'append' }) // seq 2
  188. // Replace the middle node (seq 1) only, keeping seq 0 and seq 2.
  189. s.append('assistant/message',
  190. { turn: 1, step: 1, content: [{ type: 'text', text: 'x' }] },
  191. { surfaceOp: { op: 'replace', start: 1, end: 1 }, sourceEventSeqs: [1] },
  192. ) // seq 3
  193. expect(s.surface.nodes.map(n => n.seq)).toEqual([0, 3, 2])
  194. // Links: 0 → 3 → 2
  195. expect(s.surface.nodes[0]!.prev).toBeNull()
  196. expect(s.surface.nodes[0]!.next).toBe(3)
  197. expect(s.surface.nodes[1]!.prev).toBe(0)
  198. expect(s.surface.nodes[1]!.next).toBe(2)
  199. expect(s.surface.nodes[2]!.prev).toBe(3)
  200. expect(s.surface.nodes[2]!.next).toBeNull()
  201. })
  202. it('surfaceOp replace object is snapshot so caller mutation is isolated', () => {
  203. const s = new Session(SessionId('immutable-op'))
  204. s.append('user/message', { content: [{ type: 'text', text: 'a' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  205. const op = { op: 'replace' as const, start: 0, end: 0 }
  206. s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 's' }] }, { surfaceOp: op, sourceEventSeqs: [0] })
  207. // Mutate caller's object after append.
  208. op.start = 99
  209. const logged = s.events[1]! as SurfaceEvent
  210. expect(logged.surfaceOp).toEqual({ op: 'replace', start: 0, end: 0 })
  211. })
  212. })
  213. describe('deriveMessages with surface', () => {
  214. it('uses the surface path when surface markers are present', () => {
  215. const s = surfaceSession()
  216. const messages = s.deriveMessages()
  217. expect(messages).toHaveLength(2)
  218. expect(messages[0]!.role).toBe('user')
  219. expect(messages[0]!.content[0]).toMatchObject({ type: 'text', text: 'hello' })
  220. expect(messages[1]!.role).toBe('assistant')
  221. expect(messages[1]!.content[0]).toMatchObject({ type: 'text', text: 'hi' })
  222. })
  223. it('surface path skips non-surface events (chunks, boundaries)', () => {
  224. const s = new Session(SessionId('filter'))
  225. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  226. s.append('assistant/chunk', { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'h' } })
  227. s.append('assistant/chunk', { turn: 1, step: 1, chunk: { type: 'text-delta', index: 1, text: 'i' } })
  228. s.append('user/message', { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  229. s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 'hi' }] }, { surfaceOp: 'append' })
  230. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  231. // Chunks and boundaries are NOT in the surface, so only 2 messages.
  232. expect(s.deriveMessages()).toHaveLength(2)
  233. })
  234. it('deriveMessages via surface respects replace (shadowed nodes are excluded)', () => {
  235. const s = new Session(SessionId('compacted'))
  236. s.append('user/message', { content: [{ type: 'text', text: 'original' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  237. s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 'compacted' }] }, { surfaceOp: { op: 'replace', start: 0, end: 0 }, sourceEventSeqs: [0] })
  238. // Only the compaction node is visible.
  239. const messages = s.deriveMessages()
  240. expect(messages).toHaveLength(1)
  241. expect(messages[0]!.content[0]).toMatchObject({ type: 'text', text: 'compacted' })
  242. })
  243. it('context/message and steering/message appear on surface', () => {
  244. const s = new Session(SessionId('ctx'))
  245. s.append('context/message', { content: [{ type: 'text', text: 'file changed' }], source: { kind: 'plugin', plugin: 'watcher' } }, { surfaceOp: 'append' })
  246. s.append('steering/message', { turn: 1, content: [{ type: 'text', text: 'focus' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  247. const messages = s.deriveMessages()
  248. expect(messages).toHaveLength(2)
  249. expect(messages[0]!.content[0]).toMatchObject({ type: 'text', text: '<context source="plugin">' })
  250. expect(messages[1]!.content[0]).toMatchObject({ type: 'text', text: '<steering source="user">' })
  251. })
  252. })
  253. describe('Session.append surface opts', () => {
  254. it('records sourceEventSeqs and surfaceOp on the event', () => {
  255. const s = new Session(SessionId('opts'))
  256. const event = s.append('assistant/message',
  257. { turn: 1, step: 1, content: [{ type: 'text', text: 'h' }] },
  258. { surfaceOp: 'append', sourceEventSeqs: [3, 5, 7] },
  259. )
  260. expect(event.sourceEventSeqs).toEqual([3, 5, 7])
  261. expect(event.surfaceOp).toBe('append')
  262. // The logged event matches the returned event.
  263. expect((s.events[0]! as SurfaceEvent).sourceEventSeqs).toEqual([3, 5, 7])
  264. expect((s.events[0]! as SurfaceEvent).surfaceOp).toBe('append')
  265. })
  266. it('deriveMessages skips a surface node that derives to null (empty assistant/message)', () => {
  267. // An empty-content assistant/message is surface-eligible (it can host usage)
  268. // but _deriveOneMessage returns null for it, so the surface derivation path's
  269. // null-check is exercised — the node is on the surface yet produces no message.
  270. const seed: SessionEvent[] = [
  271. { type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } },
  272. { type: 'step/start', seq: 1, time: 2, data: { turn: 1, step: 1 } },
  273. { type: 'assistant/message', seq: 2, time: 3, data: { turn: 1, step: 1, content: [] }, surfaceOp: 'append' },
  274. { type: 'step/end', seq: 3, time: 4, data: { turn: 1, step: 1 } },
  275. { type: 'turn/end', seq: 4, time: 5, data: { turn: 1, reason: { kind: 'completed' } } },
  276. ]
  277. const s = new Session(SessionId('nomessage'), seed)
  278. // The empty assistant/message is on the surface but _deriveOneMessage returns null for it.
  279. expect(s.deriveMessages()).toHaveLength(0)
  280. })
  281. it('a non-surface event carries no surface fields', () => {
  282. const s = new Session(SessionId('noopts'))
  283. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  284. expect((s.events[0] as SessionEvent<SurfaceEventType>).sourceEventSeqs).toBeUndefined()
  285. expect((s.events[0] as SessionEvent<SurfaceEventType>).surfaceOp).toBeUndefined()
  286. })
  287. it('surfaceOp primitives are not cloned (they are immutable)', () => {
  288. const s = new Session(SessionId('prim'))
  289. const event = s.append('assistant/message', { turn: 1, step: 1, content: [] }, { surfaceOp: 'append' })
  290. // The string 'append' is a primitive — identity-preserving is fine.
  291. expect(event.surfaceOp).toBe('append')
  292. })
  293. it('isSurfaceEvent rejects a surface-eligible type missing its surfaceOp marker', () => {
  294. // A raw event (not built via append, which mandates the marker) of a
  295. // surface-eligible type but with no surfaceOp must NOT narrow to a
  296. // SurfaceEvent — it would otherwise be silently dropped from the surface.
  297. const noMarker: SessionEvent = {
  298. type: 'user/message', seq: 0, time: 1,
  299. data: { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } },
  300. }
  301. expect(isSurfaceEvent(noMarker)).toBe(false)
  302. // A non-surface type is rejected too (the type gate).
  303. const boundary: SessionEvent = { type: 'turn/start', seq: 1, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } }
  304. expect(isSurfaceEvent(boundary)).toBe(false)
  305. // A properly-marked surface event narrows.
  306. const marked = { ...noMarker, surfaceOp: 'append' } as SurfaceEvent
  307. expect(isSurfaceEvent(marked)).toBe(true)
  308. })
  309. })
  310. describe('surface type guards', () => {
  311. it('isSurfaceEligibleType is true only for message-producing types', () => {
  312. expect(isSurfaceEligibleType('user/message')).toBe(true)
  313. expect(isSurfaceEligibleType('assistant/message')).toBe(true)
  314. expect(isSurfaceEligibleType('tool/result')).toBe(true)
  315. expect(isSurfaceEligibleType('context/message')).toBe(true)
  316. expect(isSurfaceEligibleType('steering/message')).toBe(true)
  317. expect(isSurfaceEligibleType('turn/start')).toBe(false)
  318. expect(isSurfaceEligibleType('assistant/chunk')).toBe(false)
  319. })
  320. it('isSurfaceEvent narrows a fully-formed surface event', () => {
  321. const s = surfaceSession()
  322. const userMessage = s.events.find(e => e.type === 'user/message')!
  323. expect(isSurfaceEvent(userMessage)).toBe(true)
  324. })
  325. it('isSurfaceEvent rejects a non-surface-eligible type', () => {
  326. const s = surfaceSession()
  327. const turnStart = s.events.find(e => e.type === 'turn/start')!
  328. expect(isSurfaceEvent(turnStart)).toBe(false)
  329. })
  330. it('isSurfaceEvent rejects a surface-eligible type missing its surfaceOp marker', () => {
  331. // A surface-eligible type whose mandatory surfaceOp is absent — the state a
  332. // seed/load log can carry before the marker is validated. surfaceOp is
  333. // optional on SessionEvent, so this is a representable runtime value.
  334. const markerless: SessionEvent = {
  335. type: 'user/message',
  336. seq: 0,
  337. time: 0,
  338. data: { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } },
  339. }
  340. expect(isSurfaceEligibleType(markerless.type)).toBe(true)
  341. expect(isSurfaceEvent(markerless)).toBe(false)
  342. })
  343. })
  344. describe('SurfaceManager.replaceGeneration', () => {
  345. it('folds the pending log delta on access and counts replacements', () => {
  346. const s = new Session(SessionId('gen'))
  347. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  348. s.append('user/message', { content: [{ type: 'text', text: 'one' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  349. s.append('user/message', { content: [{ type: 'text', text: 'two' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  350. // Read the generation FIRST — before nodes — so the getter itself folds
  351. // the pending delta rather than piggybacking on a nodes read.
  352. expect(s.surface.replaceGeneration).toBe(0)
  353. const nodes = s.surface.nodes
  354. s.append('context/message', {
  355. content: [{ type: 'text', text: 'summary' }], source: { kind: 'plugin', plugin: 'compact' },
  356. }, { surfaceOp: { op: 'replace', start: nodes[0]!.seq, end: nodes[1]!.seq }, sourceEventSeqs: [nodes[0]!.seq, nodes[1]!.seq] })
  357. expect(s.surface.replaceGeneration).toBe(1)
  358. })
  359. })