request-header.spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * Request-header utility tests: canonical form, the system line-diff
  3. * (prefix/suffix trim), the name-keyed tools delta, config replacement, the
  4. * round-trip contract (including the reorder case the encoding cannot
  5. * express), and the log fold. These pin the reconstruction algebra: for every
  6. * logged delta, apply(prev, delta) === next, and folding a log prefix yields
  7. * the header its next request was built under.
  8. */
  9. import { describe, expect, it } from 'vitest'
  10. import { Session, SessionId, applyHeaderDelta, canonicalHeader, diffHeader, foldRequestHeader } from '@deepseek-ai/dsh-session'
  11. import type { EpochHeader, SessionEvent } from '@deepseek-ai/dsh-session'
  12. import type { ToolSchema } from '@deepseek-ai/dsh-llm'
  13. const CONFIG = { model: 'm' }
  14. function tool(name: string, description = 'd'): ToolSchema {
  15. return { name, description, parameters: { type: 'object' } }
  16. }
  17. /** Round-trip helper: diff must reproduce `next` from `prev` exactly. */
  18. function roundTrip(prev: EpochHeader, next: EpochHeader): ReturnType<typeof diffHeader> {
  19. const delta = diffHeader(prev, next)
  20. if (delta !== undefined) {
  21. expect(applyHeaderDelta(prev, delta)).toEqual(canonicalHeader(next))
  22. }
  23. return delta
  24. }
  25. describe('canonicalHeader', () => {
  26. it('normalizes empty system and empty tools to absent fields', () => {
  27. expect(canonicalHeader({ config: CONFIG, system: '', tools: [] })).toEqual({ config: CONFIG })
  28. const full = canonicalHeader({ config: CONFIG, system: 's', tools: [tool('a')] })
  29. expect(full.system).toBe('s')
  30. expect(full.tools).toHaveLength(1)
  31. })
  32. })
  33. describe('diffHeader / applyHeaderDelta', () => {
  34. it('returns undefined for equal headers', () => {
  35. const header = canonicalHeader({ config: CONFIG, system: 'a\nb', tools: [tool('t')] })
  36. expect(diffHeader(header, header)).toBeUndefined()
  37. })
  38. it('encodes a mid-prompt line change as a prefix/suffix trim', () => {
  39. const prev = canonicalHeader({ config: CONFIG, system: 'keep1\nold\nkeep2\nkeep3' })
  40. const next = canonicalHeader({ config: CONFIG, system: 'keep1\nnew A\nnew B\nkeep2\nkeep3' })
  41. const delta = roundTrip(prev, next)
  42. expect(delta?.system).toEqual({ keepStart: 1, keepEnd: 2, insert: ['new A', 'new B'] })
  43. expect(delta?.tools).toBeUndefined()
  44. expect(delta?.config).toBeUndefined()
  45. })
  46. it('degenerates to a full replacement when nothing is shared, and round-trips absence transitions', () => {
  47. const none = canonicalHeader({ config: CONFIG })
  48. const some = canonicalHeader({ config: CONFIG, system: 'x\ny' })
  49. const gained = roundTrip(none, some)
  50. expect(gained?.system).toEqual({ keepStart: 0, keepEnd: 0, insert: ['x', 'y'] })
  51. const lost = roundTrip(some, none)
  52. expect(lost?.system).toEqual({ keepStart: 0, keepEnd: 0, insert: [] })
  53. })
  54. it('does not double-count overlapping prefix and suffix (repeated lines)', () => {
  55. const prev = canonicalHeader({ config: CONFIG, system: 'a\na' })
  56. const next = canonicalHeader({ config: CONFIG, system: 'a\na\na' })
  57. roundTrip(prev, next)
  58. })
  59. it('encodes tool addition, removal, and in-place schema change by name', () => {
  60. const prev = canonicalHeader({ config: CONFIG, tools: [tool('keep'), tool('drop'), tool('edit', 'before')] })
  61. const next = canonicalHeader({ config: CONFIG, tools: [tool('keep'), tool('edit', 'after'), tool('new')] })
  62. const delta = roundTrip(prev, next)
  63. expect(delta?.tools?.added.map(t => t.name)).toEqual(['new'])
  64. expect(delta?.tools?.removed).toEqual(['drop'])
  65. expect(delta?.tools?.changed.map(t => t.name)).toEqual(['edit'])
  66. })
  67. it('round-trips a tool set gained from a tool-less header and lost back to one', () => {
  68. const none = canonicalHeader({ config: CONFIG })
  69. const some = canonicalHeader({ config: CONFIG, tools: [tool('t')] })
  70. const gained = roundTrip(none, some)
  71. expect(gained?.tools?.added.map(t => t.name)).toEqual(['t'])
  72. const lost = roundTrip(some, none)
  73. expect(lost?.tools?.removed).toEqual(['t'])
  74. })
  75. it('cannot express a pure reordering — the writer detects it via the round-trip check', () => {
  76. const prev = canonicalHeader({ config: CONFIG, tools: [tool('a'), tool('b')] })
  77. const next = canonicalHeader({ config: CONFIG, tools: [tool('b'), tool('a')] })
  78. const delta = diffHeader(prev, next)
  79. // A delta IS produced (the lists differ)…
  80. expect(delta).toBeDefined()
  81. // …but applying it cannot reproduce the new order — exactly the case the
  82. // writer's guard turns into a 'fallback' snapshot.
  83. expect(applyHeaderDelta(prev, delta!)).not.toEqual(next)
  84. })
  85. it('replaces the config whole and leaves untouched parts alone', () => {
  86. const prev = canonicalHeader({ config: { model: 'm' }, system: 's', tools: [tool('t')] })
  87. const next = canonicalHeader({ config: { model: 'm2', temperature: 0.1 }, system: 's', tools: [tool('t')] })
  88. const delta = roundTrip(prev, next)
  89. expect(delta).toEqual({ config: { model: 'm2', temperature: 0.1 } })
  90. })
  91. })
  92. describe('foldRequestHeader', () => {
  93. function headerEvents(session: Session): readonly SessionEvent[] {
  94. return session.events
  95. }
  96. it('returns undefined on a log with no header events', () => {
  97. const session = new Session(SessionId('fold-none'))
  98. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  99. expect(foldRequestHeader(headerEvents(session))).toBeUndefined()
  100. })
  101. it('folds snapshot then deltas into the header in force, skipping unrelated events', () => {
  102. const session = new Session(SessionId('fold'))
  103. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  104. const first = canonicalHeader({ config: { model: 'm' }, system: 'a\nb', tools: [tool('t')] })
  105. session.append('request/header', { header: first, reason: 'initial' })
  106. session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  107. const second = canonicalHeader({ config: { model: 'm' }, system: 'a\nc', tools: [tool('t')] })
  108. session.append('request/header-delta', diffHeader(first, second)!)
  109. expect(foldRequestHeader(headerEvents(session))).toEqual(second)
  110. // A later snapshot replaces the state wholesale (the 'resume'/'fallback' anchor).
  111. const third = canonicalHeader({ config: { model: 'other' } })
  112. session.append('request/header', { header: third, reason: 'resume' })
  113. expect(foldRequestHeader(headerEvents(session))).toEqual(third)
  114. })
  115. it('throws on a delta before any snapshot (corrupt log)', () => {
  116. const session = new Session(SessionId('fold-corrupt'))
  117. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  118. session.append('request/header-delta', { config: { model: 'x' } })
  119. expect(() => foldRequestHeader(headerEvents(session))).toThrow(/before any request\/header snapshot/)
  120. })
  121. })