derived-cache.spec.ts 5.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Derived-message cache contract against a scratch oracle: project new nodes
  3. * once, rebuild on surface replacements, return fresh arrays over shared
  4. * frozen messages, and remain value-equal to replay at every step.
  5. */
  6. import { describe, expect, it } from 'vitest'
  7. import { Session, SessionId } from '@deepseek-ai/dsh-session'
  8. function userText(session: Session, text: string): void {
  9. session.append('user/message', { content: [{ type: 'text', text }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  10. }
  11. /** From-scratch oracle: replay the log into a fresh session and derive. */
  12. function scratch(session: Session): unknown {
  13. return new Session(SessionId(`${session.id}-scratch-${session.seq}`), [...session.events]).deriveMessages()
  14. }
  15. describe('derived-message cache', () => {
  16. it('stays deep-equal to a from-scratch replay derivation as the log grows', () => {
  17. const session = new Session(SessionId('cache-grow'))
  18. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  19. userText(session, 'one')
  20. expect(session.deriveMessages()).toEqual(scratch(session))
  21. userText(session, 'two')
  22. session.append('assistant/message', { provenance: { provider: 'mock', model: 'mock' }, turn: 1, step: 1, content: [{ type: 'text', text: 'reply' }] }, { surfaceOp: 'append' })
  23. expect(session.deriveMessages()).toEqual(scratch(session))
  24. session.append('assistant/message', { provenance: { provider: 'mock', model: 'mock' }, turn: 1, step: 2, content: [], usage: { inputTokens: 1, outputTokens: 0 } }, { surfaceOp: 'append' })
  25. expect(session.deriveMessages()).toEqual(scratch(session))
  26. })
  27. it('rebuilds on a surface replace and still matches scratch', () => {
  28. const session = new Session(SessionId('cache-replace'))
  29. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  30. userText(session, 'one')
  31. userText(session, 'two')
  32. const beforeReplace = session.deriveMessages()
  33. expect(beforeReplace).toHaveLength(2)
  34. const nodes = session.surface.nodes
  35. session.append('context/message', {
  36. content: [{ type: 'text', text: 'summary' }], source: { kind: 'plugin', plugin: 'compact' },
  37. }, { surfaceOp: { op: 'replace', start: nodes[0]!, end: nodes[1]! }, sourceEventSeqs: [nodes[0]!, nodes[1]!] })
  38. expect(session.deriveMessages()).toHaveLength(1)
  39. expect(session.deriveMessages()).toEqual(scratch(session))
  40. expect(beforeReplace).toHaveLength(2)
  41. })
  42. it('returns a fresh array per call: later appends never grow a held snapshot', () => {
  43. const session = new Session(SessionId('cache-snapshot'))
  44. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  45. userText(session, 'one')
  46. const first = session.deriveMessages()
  47. userText(session, 'two')
  48. const second = session.deriveMessages()
  49. expect(first).toHaveLength(1)
  50. expect(second).toHaveLength(2)
  51. // Array snapshots share their frozen message projections.
  52. expect(second[0]).toBe(first[0])
  53. expect(Object.isFrozen(first[0])).toBe(true)
  54. })
  55. })
  56. describe('Session.deriveEventMessage — the per-event projection', () => {
  57. it('projects one appended event exactly as the full derivation projects its node', () => {
  58. const session = new Session(SessionId('per-event'))
  59. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  60. const event = session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  61. // Full and per-event derivation share one projection.
  62. expect(session.deriveEventMessage(event)).toEqual(session.deriveMessages().at(-1))
  63. })
  64. it('reuses the logged event\'s already frozen content', () => {
  65. const session = new Session(SessionId('per-event-clone'))
  66. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  67. const event = session.append('user/message', { content: [{ type: 'text', text: 'orig' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  68. const message = session.deriveEventMessage(event)!
  69. expect(message.content).toBe(event.data.content)
  70. expect(Object.isFrozen(message.content)).toBe(true)
  71. expect(Object.isFrozen(message.content[0])).toBe(true)
  72. expect(() => { (message.content[0] as { text: string }).text = 'mutated' }).toThrow()
  73. expect(session.deriveMessages().at(-1)!.content).toEqual([{ type: 'text', text: 'orig' }])
  74. })
  75. it('projects null for events that produce no message (boundaries, empty assistant)', () => {
  76. const session = new Session(SessionId('per-event-null'))
  77. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  78. const boundary = session.append('step/start', { turn: 1, step: 1 })
  79. expect(session.deriveEventMessage(boundary)).toBeNull()
  80. const empty = session.append('assistant/message', { provenance: { provider: 'mock', model: 'mock' }, turn: 1, step: 1, content: [] }, { surfaceOp: 'append' })
  81. expect(session.deriveEventMessage(empty)).toBeNull()
  82. })
  83. })