history-fold.spec.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { createMessage, createUserMessage } from '@deepseek-ai/dsh-llm'
  2. import type { SessionEvent } from '@deepseek-ai/dsh-session/types'
  3. import { describe, expect, it } from 'vitest'
  4. import { projectConversationHistory } from '../src/client/session-history/history-fold.ts'
  5. import { compactHistoryInspectionEntries } from '../src/client/sessions/history.ts'
  6. import { inspectRequests } from '../src/client/sessions/request-inspection.ts'
  7. import { ev } from './event-script.ts'
  8. const at = (seq: number, event: Record<string, unknown>): SessionEvent =>
  9. ({ seq, time: 1_700_000_000_000 + seq, ...event }) as unknown as SessionEvent
  10. describe('projectConversationHistory', () => {
  11. it('names an injected context node from its durable source, like the live adapter', () => {
  12. // The fold declares its own node mapping (jscpd:ignore in the source), so
  13. // the provenance projection is pinned on both sides independently.
  14. const injected = at(0, {
  15. type: 'user/message',
  16. surfaceOp: 'append',
  17. data: createUserMessage({
  18. content: [{ type: 'text', text: '<available_skills>…</available_skills>' }],
  19. // A plugin source, because the client program does not see the host
  20. // packages that merge richer source kinds; those arms are pinned in
  21. // context-provenance.spec.ts.
  22. source: { kind: 'plugin', plugin: 'dsh-tool-skill', form: 'catalog' },
  23. }),
  24. })
  25. const { contexts } = projectConversationHistory([{ event: injected }])
  26. expect(contexts[contexts.length - 1]?.nodes).toMatchObject([{
  27. kind: 'context',
  28. seq: 0,
  29. provenance: { role: 'inject', label: 'dsh-tool-skill' },
  30. form: 'catalog',
  31. }])
  32. })
  33. it('projects next-step human input as durable steering', () => {
  34. const steering = createUserMessage({
  35. content: [{ type: 'text', text: 'change course' }],
  36. source: { kind: 'user' },
  37. })
  38. const events = [
  39. at(0, { type: 'agent/inbox/spliced', data: {
  40. target: 'next-step', start: 0, inserted: [steering],
  41. } }),
  42. at(1, { type: 'agent/inbox/spliced', data: {
  43. target: 'next-step', start: 0, removedCount: 1, inserted: [],
  44. } }),
  45. at(2, { type: 'user/message', surfaceOp: 'append', data: steering }),
  46. ]
  47. const projection = projectConversationHistory(events.map(event => ({ event })))
  48. expect(projection.eventNodes).toMatchObject([{
  49. kind: 'steering', messageId: steering.id, seq: 2,
  50. }])
  51. })
  52. it('projects a high-sequence history window without synthesizing its unloaded prefix', () => {
  53. const baseSeq = 400_000
  54. const events = [
  55. ev.user(baseSeq, 'loaded tail'),
  56. at(baseSeq + 1, {
  57. type: 'assistant/message',
  58. surfaceOp: { op: 'replace', start: baseSeq, end: baseSeq },
  59. sourceEventSeqs: [baseSeq],
  60. data: {
  61. turn: 80,
  62. step: 1,
  63. message: createMessage({
  64. role: 'assistant',
  65. content: [{ type: 'text', text: 'tail summary' }],
  66. source: { kind: 'model', provider: 'fake', model: 'fake' },
  67. }),
  68. },
  69. }),
  70. ]
  71. const projection = projectConversationHistory(events.map(event => ({ event })))
  72. expect(projection.eventNodes.map(node => node.seq)).toEqual([baseSeq, baseSeq + 1])
  73. expect(projection.contexts.map(context => ({
  74. originSeq: context.originSeq,
  75. nodes: context.nodes.map(node => node.seq),
  76. }))).toEqual([
  77. { originSeq: undefined, nodes: [baseSeq] },
  78. { originSeq: baseSeq + 1, nodes: [baseSeq + 1] },
  79. ])
  80. })
  81. it('projects frozen surface generations without widening the core live surface', () => {
  82. const events = [
  83. ev.user(0, 'a'),
  84. ev.user(1, 'b'),
  85. at(2, {
  86. type: 'assistant/message',
  87. surfaceOp: { op: 'replace', start: 0, end: 0 },
  88. sourceEventSeqs: [0],
  89. data: {
  90. turn: 1,
  91. step: 1,
  92. message: createMessage({
  93. role: 'assistant',
  94. content: [{ type: 'text', text: 'summary' }],
  95. source: { kind: 'model', provider: 'fake', model: 'fake' },
  96. }),
  97. },
  98. }),
  99. at(3, {
  100. type: 'assistant/message',
  101. surfaceOp: { op: 'replace', start: 2, end: 1 },
  102. sourceEventSeqs: [2, 1],
  103. data: {
  104. turn: 1,
  105. step: 2,
  106. message: createMessage({
  107. role: 'assistant',
  108. content: [{ type: 'text', text: 'summary 2' }],
  109. source: { kind: 'model', provider: 'fake', model: 'fake' },
  110. }),
  111. },
  112. }),
  113. ]
  114. expect(projectConversationHistory(events.map(event => ({ event }))).contexts.map(context => ({
  115. id: context.id,
  116. parentId: context.parentId,
  117. originSeq: context.originSeq,
  118. nodes: context.nodes.map(node => node.seq),
  119. }))).toEqual([
  120. { id: 0, parentId: undefined, originSeq: undefined, nodes: [0, 1] },
  121. { id: 1, parentId: 0, originSeq: 2, nodes: [2, 1] },
  122. { id: 2, parentId: 1, originSeq: 3, nodes: [3] },
  123. ])
  124. })
  125. it('projects assistant timing and the active request header from history', () => {
  126. const projection = projectConversationHistory([
  127. ev.stepStart(0, 1, 2),
  128. at(1, { type: 'request/header', data: {
  129. reason: 'initial',
  130. header: {
  131. config: { provider: 'fake', model: 'first' },
  132. tools: [],
  133. },
  134. } }),
  135. ev.chunkStart(2, 1, 2),
  136. ev.chunkText(3, 1, 'token', 2),
  137. ev.assistant(4, 1, 'done', 2),
  138. ev.stepStart(5, 2, 1),
  139. ev.chunkText(6, 2, 'next', 1),
  140. ev.assistant(7, 2, 'next done', 1),
  141. ].map(event => ({ event })))
  142. expect(projection.eventNodes[0]).toMatchObject({
  143. kind: 'assistant',
  144. timing: {
  145. stepStartTime: 1_700_000_000_000,
  146. firstTokenTime: 1_700_000_000_003,
  147. completedTime: 1_700_000_000_004,
  148. },
  149. requestConfig: { provider: 'fake', model: 'first' },
  150. })
  151. expect(projection.eventNodes.at(-1)).toMatchObject({
  152. timing: {
  153. stepStartTime: 1_700_000_000_005,
  154. firstTokenTime: 1_700_000_000_006,
  155. completedTime: 1_700_000_000_007,
  156. },
  157. requestConfig: { provider: 'fake', model: 'first' },
  158. })
  159. })
  160. it('drops completed token payloads without changing inspection projections', () => {
  161. const events = [
  162. ev.user(0, 'before'),
  163. ev.stepStart(1, 1, 0),
  164. ev.chunkStart(2, 1),
  165. ev.chunkText(3, 1, ''),
  166. ev.chunkText(4, 1, 'first'),
  167. ev.chunkText(5, 1, ' discarded'),
  168. at(6, { type: 'assistant/chunk', data: {
  169. turn: 1,
  170. step: 0,
  171. chunk: { type: 'usage', usage: { inputTokens: 4, outputTokens: 2 } },
  172. } }),
  173. ev.assistant(7, 1, 'first discarded'),
  174. ev.compactSummary(8, 'summary', 0, 7),
  175. ev.compactCheckpoint(9, 8, 0, 7),
  176. ev.stepStart(10, 2, 0),
  177. ev.chunkStart(11, 2),
  178. ev.chunkText(12, 2, 'interrupted'),
  179. ev.turnEnd(13, 2, 'aborted'),
  180. ]
  181. const raw = events.map(event => ({ event }))
  182. const compacted = compactHistoryInspectionEntries(raw)
  183. expect(compacted.map(entry => entry.event.seq)).toEqual([
  184. 0, 1, 4, 6, 7, 8, 9, 10, 11, 12, 13,
  185. ])
  186. expect(projectConversationHistory(compacted)).toEqual(projectConversationHistory(raw))
  187. expect(inspectRequests(compacted)).toEqual(inspectRequests(raw))
  188. })
  189. })