compact-basic.spec.ts 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { BasicCompactService } from '@deepseek-ai/dsh-compact-basic'
  4. import type { BasicCompactConfig } from '@deepseek-ai/dsh-compact-basic'
  5. import type { ContentBlock, GenerateOptions, Message, StreamChunk } from '@deepseek-ai/dsh-llm'
  6. import { CallId, LlmAdapter, LlmService } from '@deepseek-ai/dsh-llm'
  7. import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session'
  8. import type { SessionEvent, SurfaceEvent } from '@deepseek-ai/dsh-session'
  9. import * as Invariants from '@deepseek-ai/dsh-invariants'
  10. import type { Agent } from '@deepseek-ai/dsh-agent'
  11. /** A never-aborted signal for the required `compactIfNeeded`/listener arg. */
  12. const SIGNAL = new AbortController().signal
  13. /**
  14. * A BasicCompactService with summarize() stubbed (no real model call) and a
  15. * predictable token estimate, for deterministic unit tests of the algorithm.
  16. */
  17. class TestCompactService extends BasicCompactService {
  18. /** Track calls to summarize for test assertions. */
  19. summarizeCalls: { text: string; model: string }[] = []
  20. /** The fixed summary to return. */
  21. mockSummary: ContentBlock[] = [{ type: 'text', text: 'Test summary of compacted content.' }]
  22. /** If set, summarize() throws this error. */
  23. summarizeError: Error | null = null
  24. override estimateContentTokens(blocks: readonly ContentBlock[]): number {
  25. // 10 tokens per block — predictable for retention/threshold math.
  26. return blocks.length * 10
  27. }
  28. override async summarize(text: string, agent: Agent): Promise<ContentBlock[]> {
  29. const model = this.config.summarizationModel || agent.options.model || ''
  30. this.summarizeCalls.push({ text, model })
  31. if (this.summarizeError) throw this.summarizeError
  32. return this.mockSummary
  33. }
  34. }
  35. /**
  36. * Create a test service with a throwaway context (auto disabled — no model).
  37. * A small `summarizationMaxTokens` baseline keeps the convergence invariant
  38. * (`summarizationMaxTokens + retainTokens <= contextWindow * thresholdRatio`)
  39. * satisfied for the tiny windows these tests use; a test may override it.
  40. */
  41. function createTestService(config: BasicCompactConfig = {}): TestCompactService {
  42. return new TestCompactService(new Context(), { auto: false, summarizationMaxTokens: 1, ...config })
  43. }
  44. /**
  45. * Build a multi-turn session with surface markers (simulating real agent-loop
  46. * output). Compaction always runs inside an OPEN turn (the loop fires the
  47. * `agent/pre-step` seam after a turn's start and before a step's start), so by
  48. * default the session is left with a trailing open turn: turns `1..turns`
  49. * close, then one more `turn/start` opens with no matching `turn/end`. Pass
  50. * `{ leaveOpen: false }` for a fully-closed session (e.g. to assert that manual
  51. * compaction is rejected when no turn is open).
  52. */
  53. function multiTurnSession(turns: number, messagesPerTurn: number = 2, opts: { leaveOpen?: boolean } = {}): Session {
  54. const leaveOpen = opts.leaveOpen ?? true
  55. const s = new Session(SessionId('test'))
  56. for (let t = 1; t <= turns; t++) {
  57. s.append('turn/start', { turn: t, trigger: { kind: 'message', source: { kind: 'user' } } })
  58. s.append('step/start', { turn: t, step: 1 })
  59. for (let m = 0; m < messagesPerTurn; m++) {
  60. s.append('user/message', {
  61. content: [{ type: 'text', text: `turn ${t} user message ${m + 1}` }],
  62. source: { kind: 'user' },
  63. }, { surfaceOp: 'append' })
  64. s.append('assistant/message', {
  65. turn: t, step: 1,
  66. content: [{ type: 'text', text: `turn ${t} assistant response ${m + 1}` }],
  67. }, { surfaceOp: 'append' })
  68. }
  69. s.append('step/end', { turn: t, step: 1 })
  70. s.append('turn/end', { turn: t, reason: { kind: 'completed' } })
  71. }
  72. // Open one more turn so compaction's events are turn-enclosed, as they are
  73. // when the loop runs the auto-compaction listener mid-turn.
  74. if (leaveOpen) {
  75. s.append('turn/start', { turn: turns + 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  76. }
  77. return s
  78. }
  79. /** Build a session with tool calls for richer extraction tests. */
  80. function sessionWithTools(): Session {
  81. const s = new Session(SessionId('tools'))
  82. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  83. s.append('step/start', { turn: 1, step: 1 })
  84. s.append('user/message', {
  85. content: [{ type: 'text', text: 'read file x' }],
  86. source: { kind: 'user' },
  87. }, { surfaceOp: 'append' })
  88. s.append('assistant/message', {
  89. turn: 1, step: 1,
  90. content: [
  91. { type: 'text', text: 'Let me read that file.' },
  92. { type: 'tool-call', id: CallId('c1'), name: 'bash', arguments: '{"command":"cat x"}' },
  93. ],
  94. }, { surfaceOp: 'append' })
  95. s.append('tool/call', { turn: 1, step: 1, callId: CallId('c1'), name: 'bash', arguments: '{"command":"cat x"}' })
  96. s.append('tool/result', {
  97. turn: 1, step: 1, callId: CallId('c1'),
  98. content: [{ type: 'text', text: 'hello world' }],
  99. isError: false,
  100. }, { surfaceOp: 'append' })
  101. s.append('assistant/message', {
  102. turn: 1, step: 1,
  103. content: [{ type: 'text', text: 'The file contains: hello world' }],
  104. }, { surfaceOp: 'append' })
  105. s.append('step/end', { turn: 1, step: 1 })
  106. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  107. // Open a trailing turn so compaction's events are turn-enclosed (as they are
  108. // when the loop runs the auto-compaction listener mid-turn).
  109. s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
  110. return s
  111. }
  112. /**
  113. * Build a session of `turns` turns, each a SINGLE step containing an
  114. * assistant/message that issues a tool-call plus its tool/result — the real
  115. * multi-node-step shape (a step is two surface nodes: the assistant and the
  116. * result). Each turn is preceded by a user/message. Used to exercise
  117. * step-alignment: a region boundary must not fall between the assistant and its
  118. * result.
  119. */
  120. function toolTurnSession(turns: number): Session {
  121. const s = new Session(SessionId('tools-multi'))
  122. for (let t = 1; t <= turns; t++) {
  123. s.append('turn/start', { turn: t, trigger: { kind: 'message', source: { kind: 'user' } } })
  124. s.append('user/message', {
  125. content: [{ type: 'text', text: `turn ${t} request` }],
  126. source: { kind: 'user' },
  127. }, { surfaceOp: 'append' })
  128. s.append('step/start', { turn: t, step: 1 })
  129. s.append('assistant/message', {
  130. turn: t, step: 1,
  131. content: [
  132. { type: 'text', text: `turn ${t} calling tool` },
  133. { type: 'tool-call', id: CallId(`c${t}`), name: 'bash', arguments: '{"command":"ls"}' },
  134. ],
  135. }, { surfaceOp: 'append' })
  136. s.append('tool/call', { turn: t, step: 1, callId: CallId(`c${t}`), name: 'bash', arguments: '{"command":"ls"}' })
  137. s.append('tool/result', {
  138. turn: t, step: 1, callId: CallId(`c${t}`),
  139. content: [{ type: 'text', text: `turn ${t} output` }],
  140. isError: false,
  141. }, { surfaceOp: 'append' })
  142. s.append('step/end', { turn: t, step: 1 })
  143. s.append('turn/end', { turn: t, reason: { kind: 'completed' } })
  144. }
  145. // Open a trailing turn so compaction's events are turn-enclosed.
  146. s.append('turn/start', { turn: turns + 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  147. return s
  148. }
  149. /**
  150. * Assert the derived transcript has NO orphaned tool-result: every
  151. * `tool-result` block's `toolCallId` must be matched by a preceding `tool-call`
  152. * block in an earlier (assistant) message. A dangling tool-result is exactly
  153. * what splitting a step at compaction produces, and every provider rejects it.
  154. */
  155. function expectNoOrphanToolResults(messages: Message[]): void {
  156. const seenCallIds = new Set<string>()
  157. for (const msg of messages) {
  158. for (const block of msg.content) {
  159. if (block.type === 'tool-call') seenCallIds.add(block.id)
  160. if (block.type === 'tool-result') {
  161. expect(seenCallIds.has(block.toolCallId),
  162. `orphaned tool-result for callId ${block.toolCallId} (no preceding tool-call)`).toBe(true)
  163. }
  164. }
  165. }
  166. }
  167. describe('BasicCompactService step-alignment (never split a tool-call/result pair)', () => {
  168. it('compactIfNeeded rounds the retained boundary head-ward to keep a whole step (no orphaned tool-result)', async () => {
  169. // 3 turns, each one step = { assistant(tool-call), tool/result }. Surface
  170. // (9 nodes): user1, asst1, res1, user2, asst2, res2, user3, asst3, res3 —
  171. // 10/20/10 tokens. The tail→head walk retains by whole units; the compacted
  172. // region always ends on a step boundary, so no step's tool-call is split
  173. // from its result. retainTokens=55 keeps the recent tail; the older steps
  174. // compact intact.
  175. const svc = createTestService({ contextWindow: 200, thresholdRatio: 0.5, retainTokens: 55 })
  176. const session = toolTurnSession(3)
  177. const result = await compactIfNeeded(svc, session, '', 'm', SIGNAL)
  178. expect(result).not.toBeNull()
  179. expect(result!.shadowedSeqs.length).toBeGreaterThan(0)
  180. // No dangling tool-result: every compacted/retained step stayed whole.
  181. expectNoOrphanToolResults(session.deriveMessages())
  182. // The most-recent step's result is retained verbatim (still on the surface).
  183. const lastResultSeq = session.events.findLast(e => e.type === 'tool/result')!.seq
  184. expect(result!.shadowedSeqs).not.toContain(lastResultSeq)
  185. })
  186. it('compactIfNeeded returns null when the only compactable region is an un-splittable single step', async () => {
  187. // The surface is exactly ONE step: [assistant(tool-call), tool/result]. Over
  188. // threshold (by the derived role overhead), the tail→head walk stops with the
  189. // retained boundary at the tool/result — which is NOT a step-aligned start (its
  190. // issuing assistant precedes it in the same step). Rounding head-ward to find a
  191. // clean boundary reaches index 0, so there is no step-aligned cutoff in the
  192. // compactable range: compactIfNeeded declines rather than splitting the step.
  193. const s = new Session(SessionId('one-step'))
  194. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  195. s.append('step/start', { turn: 1, step: 1 })
  196. s.append('assistant/message', {
  197. turn: 1, step: 1,
  198. content: [{ type: 'text', text: 'calling' }, { type: 'tool-call', id: CallId('c1'), name: 'bash', arguments: '{}' }],
  199. }, { surfaceOp: 'append' })
  200. s.append('tool/call', { turn: 1, step: 1, callId: CallId('c1'), name: 'bash', arguments: '{}' })
  201. s.append('tool/result', { turn: 1, step: 1, callId: CallId('c1'), content: [{ type: 'text', text: 'out' }], isError: false }, { surfaceOp: 'append' })
  202. s.append('step/end', { turn: 1, step: 1 })
  203. // Turn stays open.
  204. const svc = createTestService({ contextWindow: 100, thresholdRatio: 0.1, retainTokens: 5 })
  205. const result = await compactIfNeeded(svc, s, '', 'm', SIGNAL)
  206. expect(result).toBeNull()
  207. expect(s.events.some(e => e.type === 'compact/start')).toBe(false)
  208. })
  209. it('compactRegion rejects a start that splits a step (unbalanced boundary)', async () => {
  210. const svc = createTestService()
  211. const session = toolTurnSession(1)
  212. const nodes = session.surface.nodes // [user, asst(tool-call), result]
  213. const userSeq = nodes[0]!.seq
  214. const resultSeq = nodes[2]!.seq
  215. // start = the tool/result: its issuing assistant precedes it IN THE SAME STEP,
  216. // so starting here would orphan that assistant's tool-call. end is fine (user).
  217. await expect(compactRegion(svc, session, resultSeq, resultSeq, 'm'))
  218. .rejects.toThrow(/start seq .* is not a balanced boundary/)
  219. expect(userSeq).toBeLessThan(resultSeq) // sanity: ordering as expected
  220. })
  221. it('compactRegion rejects an end that splits a step (unbalanced boundary)', async () => {
  222. const svc = createTestService()
  223. const session = toolTurnSession(1)
  224. const nodes = session.surface.nodes
  225. const userSeq = nodes[0]!.seq
  226. const asstSeq = nodes[1]!.seq
  227. // end = the assistant/message: its tool/result follows IN THE SAME STEP, so
  228. // ending here would strand that result. start is fine (the pre-step user).
  229. await expect(compactRegion(svc, session, userSeq, asstSeq, 'm'))
  230. .rejects.toThrow(/end seq .* is not a balanced boundary/)
  231. })
  232. it('compactRegion rejects an end inside an open tail step', async () => {
  233. const svc = createTestService()
  234. const s = new Session(SessionId('open-tail'))
  235. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  236. s.append('user/message', { content: [{ type: 'text', text: 'go' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  237. s.append('step/start', { turn: 1, step: 1 })
  238. s.append('assistant/message', {
  239. turn: 1, step: 1,
  240. content: [{ type: 'tool-call', id: CallId('c1'), name: 'bash', arguments: '{}' }],
  241. }, { surfaceOp: 'append' })
  242. const nodes = s.surface.nodes // [user, asst]
  243. const userSeq = nodes[0]!.seq
  244. const asstSeq = nodes[1]!.seq
  245. await expect(compactRegion(svc, s, userSeq, asstSeq, 'm'))
  246. .rejects.toThrow(/end seq .* is not a balanced boundary/)
  247. })
  248. it('compactRegion accepts step-aligned boundaries (pre-step user → last result of a closed step)', async () => {
  249. const svc = createTestService()
  250. const session = toolTurnSession(2)
  251. const nodes = session.surface.nodes // [user1, asst1, res1, user2, asst2, res2]
  252. const startSeq = nodes[0]!.seq // pre-step user1 (free boundary)
  253. const endSeq = nodes[2]!.seq // res1 = last node of turn 1's closed step
  254. const result = await compactRegion(svc, session, startSeq, endSeq, 'm')
  255. expect(result.shadowedRange).toEqual({ start: startSeq, end: endSeq })
  256. expectNoOrphanToolResults(session.deriveMessages())
  257. })
  258. it('compactRegion accepts a single inter-step node (start === end on a pre-step user/message)', async () => {
  259. const svc = createTestService()
  260. const session = toolTurnSession(1)
  261. const nodes = session.surface.nodes
  262. const userSeq = nodes[0]!.seq // pre-step user: free boundary both ways
  263. const result = await compactRegion(svc, session, userSeq, userSeq, 'm')
  264. expect(result.shadowedRange).toEqual({ start: userSeq, end: userSeq })
  265. })
  266. it('compactRegion accepts an injection-turn context node (no step at all)', async () => {
  267. const svc = createTestService()
  268. const s = new Session(SessionId('inject'))
  269. // An idle inject(): turn/start → context/message, NO step. A later turn is
  270. // open so compaction's events are turn-enclosed.
  271. s.append('turn/start', { turn: 1, trigger: { kind: 'injection', source: { kind: 'user' } } })
  272. s.append('context/message', { content: [{ type: 'text', text: 'ctx' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  273. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  274. s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
  275. const nodes = s.surface.nodes
  276. const ctxSeq = nodes[0]!.seq
  277. const result = await compactRegion(svc, s, ctxSeq, ctxSeq, 'm')
  278. expect(result.shadowedRange).toEqual({ start: ctxSeq, end: ctxSeq })
  279. })
  280. })
  281. describe('BasicCompactService.estimateEventTokens', () => {
  282. it('returns 0 for non-message events (boundary, chunk, step/end, tool/call)', () => {
  283. const svc = createTestService()
  284. expect(svc.estimateEventTokens({ type: 'turn/start', seq: 0, time: 1, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } } })).toBe(0)
  285. expect(svc.estimateEventTokens({ type: 'step/start', seq: 1, time: 2, data: { turn: 1, step: 1 } })).toBe(0)
  286. expect(svc.estimateEventTokens({ type: 'assistant/chunk', seq: 2, time: 3, data: { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'h' } } })).toBe(0)
  287. expect(svc.estimateEventTokens({ type: 'step/end', seq: 3, time: 4, data: { turn: 1, step: 1 } })).toBe(0)
  288. expect(svc.estimateEventTokens({ type: 'tool/call', seq: 4, time: 5, data: { turn: 1, step: 1, callId: CallId('c1'), name: 'read', arguments: '{}' } })).toBe(0)
  289. })
  290. it('returns estimate for message-producing events', () => {
  291. const svc = createTestService()
  292. const userEvent: SessionEvent = { type: 'user/message', seq: 0, time: 1, data: { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } } }
  293. expect(svc.estimateEventTokens(userEvent)).toBe(10)
  294. const asstEvent: SessionEvent = { type: 'assistant/message', seq: 1, time: 2, data: { turn: 1, step: 1, content: [{ type: 'text', text: 'a' }, { type: 'text', text: 'b' }] } }
  295. expect(svc.estimateEventTokens(asstEvent)).toBe(20)
  296. const toolEvent: SessionEvent = { type: 'tool/result', seq: 2, time: 3, data: { turn: 1, step: 1, callId: CallId('c1'), content: [{ type: 'text', text: 'output' }], isError: false } }
  297. expect(svc.estimateEventTokens(toolEvent)).toBe(10)
  298. })
  299. })
  300. describe('BasicCompactService.estimateTokens', () => {
  301. it('sums token estimates across messages', () => {
  302. const svc = createTestService()
  303. const messages: Message[] = [
  304. { role: 'user', content: [{ type: 'text', text: 'hello' }] },
  305. { role: 'assistant', content: [{ type: 'text', text: 'hi' }, { type: 'text', text: 'there' }] },
  306. ]
  307. // 1 block * 10 + 4 (role) + 2 blocks * 10 + 4 (role) = 10 + 4 + 20 + 4 = 38
  308. expect(svc.estimateTokens(messages)).toBe(38)
  309. })
  310. it('includes system prompt in the estimate', () => {
  311. const svc = createTestService()
  312. const messages: Message[] = [
  313. { role: 'user', content: [{ type: 'text', text: 'hi' }] },
  314. ]
  315. const systemPrompt = 'You are a helpful assistant.'
  316. // 1 block * 10 + 4 (role) + ceil(28/4) = 10 + 4 + 7 = 21
  317. expect(svc.estimateTokens(messages, systemPrompt)).toBe(21)
  318. })
  319. })
  320. describe('BasicCompactService.compactRegion', () => {
  321. it('shadows surface nodes and inserts a summary via user/message', async () => {
  322. const svc = createTestService()
  323. const session = multiTurnSession(3, 1) // 3 turns, 2 surface nodes each = 6 nodes
  324. const nodes = session.surface.nodes
  325. expect(nodes.length).toBe(6)
  326. const firstSeq = nodes[0]!.seq
  327. const secondSeq = nodes[1]!.seq
  328. const result = await compactRegion(svc, session, firstSeq, secondSeq, 'test-model')
  329. expect(result.shadowedSeqs).toEqual([firstSeq, secondSeq])
  330. expect(result.shadowedRange.start).toBe(firstSeq)
  331. expect(result.shadowedRange.end).toBe(secondSeq)
  332. expect(result.summary).toEqual(svc.mockSummary)
  333. const events = session.events
  334. const startEvent = events.findLast(e => e.type === 'compact/start')
  335. const summaryEvent = events.findLast(e => e.type === 'compact/summary')
  336. const endEvent = events.findLast(e => e.type === 'compact/end')
  337. expect(startEvent).toBeDefined()
  338. expect(summaryEvent).toBeDefined()
  339. expect(endEvent).toBeDefined()
  340. // compact/* events are log-only — no surfaceOp (type system enforces this).
  341. const startRaw = startEvent as unknown as { surfaceOp?: unknown }
  342. expect(startRaw.surfaceOp).toBeUndefined()
  343. // The user/message carries the replace surfaceOp.
  344. const userMsg = events.findLast(e => e.type === 'user/message')!
  345. const surfaceUserMsg = userMsg as SurfaceEvent
  346. expect(surfaceUserMsg.surfaceOp).toEqual({ op: 'replace', start: firstSeq, end: secondSeq })
  347. expect(surfaceUserMsg.sourceEventSeqs).toContain(startEvent!.seq)
  348. expect(surfaceUserMsg.sourceEventSeqs).toContain(summaryEvent!.seq)
  349. expect(surfaceUserMsg.sourceEventSeqs).toContain(firstSeq)
  350. expect(surfaceUserMsg.sourceEventSeqs).toContain(secondSeq)
  351. // compact/end is appended AFTER the replacement (the lock brackets the whole
  352. // op), so the replacement cannot reference it — sourceEventSeqs may only
  353. // reference earlier seqs.
  354. expect(surfaceUserMsg.sourceEventSeqs).not.toContain(endEvent!.seq)
  355. expect(endEvent!.seq).toBeGreaterThan(userMsg.seq)
  356. // Surface now has: summary user/message + retained 4 nodes = 5 nodes.
  357. const newNodes = session.surface.nodes
  358. expect(newNodes.length).toBe(5)
  359. expect(newNodes[0]!.seq).toBe(userMsg.seq)
  360. // deriveMessages() produces the framed summary as a user-role message:
  361. // a checkpoint preamble + tag-wrapped summary blocks.
  362. const derived = session.deriveMessages()
  363. expect(derived.length).toBe(5)
  364. expect(derived[0]!.role).toBe('user')
  365. const framed = derived[0]!.content
  366. expect(framed[0]).toMatchObject({ type: 'text' })
  367. expect((framed[0] as { text: string }).text).toContain('<compacted-summary>')
  368. expect(framed).toContainEqual(svc.mockSummary[0])
  369. expect((framed[framed.length - 1] as { text: string }).text).toBe('</compacted-summary>')
  370. })
  371. it('throws when start or end are not surface nodes', async () => {
  372. const svc = createTestService()
  373. const session = multiTurnSession(1, 1)
  374. await expect(compactRegion(svc, session, 999, 1000, 'm'))
  375. .rejects.toThrow(/start seq 999 not found in surface/)
  376. })
  377. it('throws when start is positioned after end on the surface', async () => {
  378. const svc = createTestService()
  379. const session = multiTurnSession(2, 1)
  380. const nodes = session.surface.nodes
  381. await expect(compactRegion(svc, session, nodes[1]!.seq, nodes[0]!.seq, 'm'))
  382. .rejects.toThrow(/is after end seq .* on the surface/)
  383. })
  384. it('throws when compaction is already in progress', async () => {
  385. const svc = createTestService()
  386. const session = multiTurnSession(2, 1)
  387. const nodes = session.surface.nodes
  388. session.append('compact/start', { turn: 2 })
  389. await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm'))
  390. .rejects.toThrow(/compaction already in progress/)
  391. })
  392. it('appends compact/end with error on summarize failure', async () => {
  393. const svc = createTestService()
  394. svc.summarizeError = new Error('model unavailable')
  395. const session = multiTurnSession(2, 1)
  396. const nodes = session.surface.nodes
  397. await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm'))
  398. .rejects.toThrow('model unavailable')
  399. const endEvent = session.events.findLast(e => e.type === 'compact/end')
  400. expect(endEvent).toBeDefined()
  401. // multiTurnSession(2,…) closes turns 1-2 and leaves turn 3 open; compaction
  402. // stamps the open turn.
  403. expect(endEvent!.data).toMatchObject({ turn: 3, error: 'model unavailable' })
  404. // No replace-op user/message was appended (summarize failed).
  405. const userMsgsAfter = session.events.filter(e => e.type === 'user/message')
  406. const replaceMsgs = userMsgsAfter.filter((e) => {
  407. const se = e as unknown as { surfaceOp?: unknown }
  408. return se.surfaceOp !== undefined && typeof se.surfaceOp !== 'string'
  409. })
  410. expect(replaceMsgs.length).toBe(0)
  411. })
  412. it('extracts conversation text for summarization', async () => {
  413. const svc = createTestService()
  414. const session = multiTurnSession(1, 2)
  415. const nodes = session.surface.nodes
  416. await compactRegion(svc, session, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
  417. expect(svc.summarizeCalls.length).toBe(1)
  418. const { text, model } = svc.summarizeCalls[0]!
  419. expect(model).toBe('m')
  420. expect(text).toContain('User: turn 1 user message 1')
  421. expect(text).toContain('Assistant: turn 1 assistant response 1')
  422. })
  423. it('frames the landed summary with a checkpoint preamble and tags, keeping raw provenance', async () => {
  424. const svc = createTestService()
  425. svc.mockSummary = [{ type: 'text', text: 'STRUCTURED SUMMARY' }]
  426. const session = multiTurnSession(3, 1)
  427. const nodes = session.surface.nodes
  428. const result = await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm')
  429. // Provenance (compact/summary) carries the RAW, unframed summary.
  430. expect(result.summary).toEqual([{ type: 'text', text: 'STRUCTURED SUMMARY' }])
  431. const summaryEvent = session.events.findLast(e => e.type === 'compact/summary')!
  432. expect(summaryEvent.data).toMatchObject({ summary: [{ type: 'text', text: 'STRUCTURED SUMMARY' }] })
  433. // The landed surface node is framed: preamble + tag-wrapped summary.
  434. const landed = session.deriveMessages()[0]!.content
  435. expect((landed[0] as { text: string }).text).toContain('checkpoint')
  436. expect((landed[0] as { text: string }).text).toContain('<compacted-summary>')
  437. expect(landed).toContainEqual({ type: 'text', text: 'STRUCTURED SUMMARY' })
  438. expect((landed[landed.length - 1] as { text: string }).text).toBe('</compacted-summary>')
  439. })
  440. it('extracts tool-call and tool-result context', async () => {
  441. const svc = createTestService()
  442. const session = sessionWithTools()
  443. const nodes = session.surface.nodes
  444. const firstSeq = nodes[0]!.seq
  445. const lastSeq = nodes[nodes.length - 1]!.seq
  446. await compactRegion(svc, session, firstSeq, lastSeq, 'm')
  447. expect(svc.summarizeCalls.length).toBe(1)
  448. const { text } = svc.summarizeCalls[0]!
  449. expect(text).toContain('read file x')
  450. expect(text).toContain('bash')
  451. expect(text).toContain('Tool result')
  452. })
  453. })
  454. describe('BasicCompactService.compactIfNeeded', () => {
  455. it('returns null when tokens are under threshold', async () => {
  456. const svc = createTestService({ contextWindow: 128000, thresholdRatio: 0.8 })
  457. const session = multiTurnSession(1, 1)
  458. expect(await compactIfNeeded(svc, session, '', 'm', SIGNAL)).toBeNull()
  459. })
  460. it('compacts when tokens exceed threshold', async () => {
  461. const svc = createTestService({ contextWindow: 100, thresholdRatio: 0.5, retainTokens: 10 })
  462. const session = multiTurnSession(3, 1) // 6 surface nodes, 10 tokens each = 60
  463. const result = await compactIfNeeded(svc, session, '', 'm', SIGNAL)
  464. expect(result).not.toBeNull()
  465. expect(result!.shadowedSeqs.length).toBeGreaterThan(0)
  466. })
  467. it('walks tail→head and retains nodes within token budget', async () => {
  468. const svc = createTestService({ contextWindow: 100, thresholdRatio: 0.2, retainTokens: 15 })
  469. const session = multiTurnSession(5, 1) // 10 surface nodes = ~100 tokens
  470. const result = await compactIfNeeded(svc, session, '', 'm', SIGNAL)
  471. expect(result).not.toBeNull()
  472. const nodes = session.surface.nodes
  473. expect(result!.shadowedSeqs.length).toBeGreaterThan(0)
  474. expect(result!.shadowedSeqs).not.toContain(nodes[nodes.length - 1]!.seq)
  475. })
  476. it('returns null when the whole surface fits the retain budget (over threshold by role/system overhead)', async () => {
  477. // threshold = floor(470*0.1) = 47. The 4 surface nodes weigh 10 each (raw 40
  478. // for the retention walk), but the derived estimate adds 4 role tokens per
  479. // message → 56 ≥ 47, so the threshold check passes and the walk runs. The
  480. // walk accumulates all 40 < retainTokens (45) without crossing the budget,
  481. // so keepFromIdx reaches 0 and compaction declines. The invariant holds:
  482. // summarizationMaxTokens (1) + retainTokens (45) = 46 < threshold 47.
  483. const svc = createTestService({ contextWindow: 470, thresholdRatio: 0.1, retainTokens: 45 })
  484. const session = multiTurnSession(2, 1)
  485. expect(await compactIfNeeded(svc, session, '', 'm', SIGNAL)).toBeNull()
  486. })
  487. it('compacts a runaway turn: its early CLOSED steps summarize while recent steps stay verbatim', async () => {
  488. // The REGRESSION that motivated dropping turn-protection. A single in-flight
  489. // (open) turn has grown past the threshold on its own: several CLOSED steps,
  490. // each [assistant(tool-call), tool/result]. Retention is turn-agnostic, so
  491. // the turn's OWN early closed steps are eligible — they compact while the
  492. // recent tail stays verbatim, and the harness survives.
  493. //
  494. // On the OLD layer-2 code this test FAILS: the entire open turn was retained
  495. // verbatim (protectedIdx = first open-turn node = 0), so compactIfNeeded
  496. // returned null and shadowedSeqs would be empty — the runaway turn could
  497. // never compact and the next model call would overflow the window.
  498. const svc = createTestService({ contextWindow: 300, thresholdRatio: 0.1, retainTokens: 25 })
  499. const s = new Session(SessionId('runaway'))
  500. // ONE open turn with 5 closed steps; each step is [asst(tool-call), result].
  501. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  502. s.append('user/message', { content: [{ type: 'text', text: 'do a big multi-step task' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  503. for (let step = 1; step <= 5; step++) {
  504. s.append('step/start', { turn: 1, step })
  505. s.append('assistant/message', {
  506. turn: 1, step,
  507. content: [{ type: 'text', text: `step ${step}` }, { type: 'tool-call', id: CallId(`c${step}`), name: 'bash', arguments: '{}' }],
  508. }, { surfaceOp: 'append' })
  509. s.append('tool/call', { turn: 1, step, callId: CallId(`c${step}`), name: 'bash', arguments: '{}' })
  510. s.append('tool/result', { turn: 1, step, callId: CallId(`c${step}`), content: [{ type: 'text', text: `out ${step}` }], isError: false }, { surfaceOp: 'append' })
  511. s.append('step/end', { turn: 1, step })
  512. }
  513. // The turn stays OPEN (no turn/end) — the model is mid-turn, about to run
  514. // step 6. Surface: user + 5×[asst, result] = 11 nodes.
  515. const nodesBefore = s.surface.nodes.length
  516. expect(nodesBefore).toBe(11)
  517. const result = await compactIfNeeded(svc, s, '', 'm', SIGNAL)
  518. expect(result).not.toBeNull()
  519. // Early steps of the SAME open turn were shadowed (impossible under layer 2).
  520. expect(result!.shadowedSeqs.length).toBeGreaterThan(0)
  521. // The most-recent step's tool result is retained verbatim (still on surface).
  522. const lastResultSeq = s.events.findLast(e => e.type === 'tool/result')!.seq
  523. expect(result!.shadowedSeqs).not.toContain(lastResultSeq)
  524. expect(s.surface.nodes.some(n => n.seq === lastResultSeq)).toBe(true)
  525. // No orphaned tool-result survives (whole-step boundaries respected).
  526. expectNoOrphanToolResults(s.deriveMessages())
  527. })
  528. it('returns null for an empty surface', async () => {
  529. const svc = createTestService({ contextWindow: 100, thresholdRatio: 0.5, retainTokens: 10 })
  530. const session = new Session(SessionId('empty'))
  531. expect(await compactIfNeeded(svc, session, '', 'm', SIGNAL)).toBeNull()
  532. })
  533. it('compacts again after a prior summary node heads the surface (the summary stays eligible)', async () => {
  534. // After the first compaction lands a replacement summary node at the head,
  535. // a second compaction (still over threshold) re-consolidates it with newer
  536. // context — head-anchoring means the prior checkpoint is always re-included,
  537. // never stranded. retainTokens=25 leaves a couple of retained nodes after
  538. // the first compaction (so the surface is [summary, …retained], not just
  539. // [summary]).
  540. const svc = createTestService({ contextWindow: 300, thresholdRatio: 0.1, retainTokens: 25 })
  541. const s = multiTurnSession(4, 1) // turns 1-4 closed, turn 5 open (no surface yet)
  542. const first = await compactIfNeeded(svc, s, '', 'm', SIGNAL)
  543. expect(first).not.toBeNull()
  544. // The summary node now heads the surface with a fresh high seq.
  545. const summaryHeadSeq = s.surface.nodes[0]!.seq
  546. const turn5StartSeq = s.events.filter(e => e.type === 'turn/start').at(-1)!.seq
  547. expect(summaryHeadSeq).toBeGreaterThan(turn5StartSeq)
  548. // Append a verbatim node in the open turn (a step's output), still over
  549. // threshold, then compact again — the older summary + closed turns compact,
  550. // the fresh nodes are retained.
  551. s.append('step/start', { turn: 5, step: 1 })
  552. s.append('user/message', { content: [{ type: 'text', text: 'turn 5 work' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  553. s.append('assistant/message', { turn: 5, step: 1, content: [{ type: 'text', text: 'reply 5' }] }, { surfaceOp: 'append' })
  554. s.append('step/end', { turn: 5, step: 1 })
  555. const second = await compactIfNeeded(svc, s, '', 'm', SIGNAL)
  556. expect(second).not.toBeNull()
  557. expect(second!.shadowedSeqs.length).toBeGreaterThan(0)
  558. // The fresh open-turn nodes were NOT compacted.
  559. const turn5UserSeq = s.events.find(e => e.type === 'user/message' && e.data.content.some(b => b.type === 'text' && b.text === 'turn 5 work'))!.seq
  560. expect(second!.shadowedSeqs).not.toContain(turn5UserSeq)
  561. })
  562. })
  563. describe('BasicCompactService replay equivalence', () => {
  564. it('produces identical deriveMessages() after seeding from compacted log', async () => {
  565. const svc = createTestService()
  566. const session = multiTurnSession(3, 1)
  567. const nodes = session.surface.nodes
  568. await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm')
  569. const derived = session.deriveMessages()
  570. const replayed = new Session(SessionId('replay'), [...session.events])
  571. expect(replayed.deriveMessages()).toEqual(derived)
  572. })
  573. })
  574. describe('BasicCompactService blocking (compaction in progress)', () => {
  575. it('detects in-progress compaction from unmatched compact/start', async () => {
  576. const svc = createTestService()
  577. const session = multiTurnSession(1, 1)
  578. session.append('compact/start', { turn: 1 })
  579. const nodes = session.surface.nodes
  580. // Whole step (user → assistant) is a step-aligned region, so the call reaches
  581. // the in-progress check rather than being rejected for splitting a step.
  582. await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm'))
  583. .rejects.toThrow(/compaction already in progress/)
  584. })
  585. it('allows compaction after compact/end is appended', async () => {
  586. const svc = createTestService()
  587. const session = multiTurnSession(2, 1)
  588. const nodes = session.surface.nodes
  589. session.append('compact/start', { turn: 1 })
  590. session.append('compact/end', { turn: 1 })
  591. const result = await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm')
  592. expect(result).toBeDefined()
  593. })
  594. it('is not wedged by an orphaned compact/start from a prior (now-closed) turn', async () => {
  595. // A crash mid-compaction left a compact/start with no compact/end; the turn
  596. // it lived in was later closed (persistence repair appends turn/end). A
  597. // whole-log scan would treat that stale start as an active lock forever. The
  598. // scan is scoped to the current turn, so a NEW turn compacts normally.
  599. const svc = createTestService()
  600. const s = new Session(SessionId('stale-lock'))
  601. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  602. s.append('step/start', { turn: 1, step: 1 })
  603. s.append('user/message', { content: [{ type: 'text', text: 'turn 1' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  604. s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 'reply 1' }] }, { surfaceOp: 'append' })
  605. s.append('compact/start', { turn: 1 }) // ← orphaned: no matching compact/end
  606. s.append('step/end', { turn: 1, step: 1 })
  607. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } }) // repair closed the turn
  608. // A new open turn.
  609. s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
  610. const nodes = s.surface.nodes
  611. // The stale start is before the turn/end, so it is NOT seen as in-progress.
  612. const result = await compactRegion(svc, s, nodes[0]!.seq, nodes[1]!.seq, 'm')
  613. expect(result).toBeDefined()
  614. })
  615. })
  616. describe('BasicCompactService token estimation (char/4 heuristic)', () => {
  617. it('estimates text blocks with char/4 + overhead', () => {
  618. const svc = new BasicCompactService(new Context(), { auto: false })
  619. // 'this is a somewhat longer text block' = 36 → ceil(36/4)+4 = 13; 'short' = 5 → 2+4 = 6
  620. const blocks: ContentBlock[] = [
  621. { type: 'text', text: 'this is a somewhat longer text block' },
  622. { type: 'text', text: 'short' },
  623. ]
  624. expect(svc.estimateContentTokens(blocks)).toBe(19)
  625. })
  626. it('estimates reasoning blocks same as text', () => {
  627. const svc = new BasicCompactService(new Context(), { auto: false })
  628. // 'thinking about this...' = 22 → ceil(22/4)+4 = 10
  629. expect(svc.estimateContentTokens([{ type: 'reasoning', text: 'thinking about this...' }])).toBe(10)
  630. })
  631. it('estimates tool-call blocks from name + arguments', () => {
  632. const svc = new BasicCompactService(new Context(), { auto: false })
  633. // 'bash' = 4 → 1; '{"command":"ls"}' = 16 → 4; + 4 overhead = 9
  634. expect(svc.estimateContentTokens([
  635. { type: 'tool-call', id: CallId('c1'), name: 'bash', arguments: '{"command":"ls"}' },
  636. ])).toBe(9)
  637. })
  638. it('estimates tool-result blocks recursively', () => {
  639. const svc = new BasicCompactService(new Context(), { auto: false })
  640. // inner text 5 → 2+4 = 6; outer 6 + 4 overhead = 10
  641. expect(svc.estimateContentTokens([
  642. { type: 'tool-result', toolCallId: CallId('c1'), content: [{ type: 'text', text: 'hello' }], isError: false },
  643. ])).toBe(10)
  644. })
  645. it('estimates image blocks at fixed 85 tokens', () => {
  646. const svc = new BasicCompactService(new Context(), { auto: false })
  647. expect(svc.estimateContentTokens([{ type: 'image', url: 'https://example.com/img.png' }])).toBe(85)
  648. })
  649. it('returns 0 for empty content blocks', () => {
  650. const svc = new BasicCompactService(new Context(), { auto: false })
  651. expect(svc.estimateContentTokens([])).toBe(0)
  652. })
  653. })
  654. describe('BasicCompactService HMR safety', () => {
  655. it('registers as ctx.compact', () => {
  656. const ctx = new Context()
  657. void new BasicCompactService(ctx, { auto: false })
  658. expect(ctx.compact).toBeDefined()
  659. expect(ctx.compact).toBeInstanceOf(BasicCompactService)
  660. })
  661. it('disposing the plugin fiber unregisters ctx.compact', async () => {
  662. // Mount through the real plugin fiber (the Loader path), then dispose it and
  663. // confirm the service registration is torn down. LlmService is mounted first
  664. // so the service's `inject: ['llm']` resolves and the fiber activates. (The
  665. // sibling-fiber ctx.llm resolution this same setup also exercises is covered
  666. // under the "llm inject (real plugin-load path)" suite.)
  667. const ctx = new Context()
  668. await ctx.plugin(LlmService)
  669. const fiber = await ctx.plugin(BasicCompactService, { auto: false })
  670. expect(ctx.get('compact')).toBeInstanceOf(BasicCompactService)
  671. await fiber.dispose()
  672. expect(ctx.get('compact')).toBeUndefined()
  673. })
  674. })
  675. describe('BasicCompactService convergence invariant (config)', () => {
  676. it('throws when summarizationMaxTokens + retainTokens exceeds the threshold', () => {
  677. // threshold = floor(1000 * 0.5) = 500; 200 + 400 = 600 is not below 500 → reject.
  678. expect(() => new BasicCompactService(new Context(), {
  679. auto: false, contextWindow: 1000, thresholdRatio: 0.5, retainTokens: 400, summarizationMaxTokens: 200,
  680. })).toThrow(/not below the compaction threshold/)
  681. })
  682. it('rejects the boundary case (sum equals the threshold — would re-trigger)', () => {
  683. // threshold = floor(1000 * 0.5) = 500; 100 + 400 = 500 is NOT below 500, so
  684. // post-compaction history would sit exactly at threshold and re-compact.
  685. expect(() => new BasicCompactService(new Context(), {
  686. auto: false, contextWindow: 1000, thresholdRatio: 0.5, retainTokens: 400, summarizationMaxTokens: 100,
  687. })).toThrow(/not below the compaction threshold/)
  688. })
  689. it('accepts the case just below the threshold', () => {
  690. // threshold = floor(1000 * 0.5) = 500; 99 + 400 = 499 < 500 → allowed.
  691. expect(() => new BasicCompactService(new Context(), {
  692. auto: false, contextWindow: 1000, thresholdRatio: 0.5, retainTokens: 400, summarizationMaxTokens: 99,
  693. })).not.toThrow()
  694. })
  695. it('the default config satisfies the invariant', () => {
  696. // 2048 + 20480 = 22528 ≤ floor(128000 * 0.8) = 102400.
  697. expect(() => new BasicCompactService(new Context(), { auto: false })).not.toThrow()
  698. })
  699. })
  700. /** An adapter that emits a fixed summary text, for exercising the real summarize() path. */
  701. class ScriptedAdapter extends LlmAdapter {
  702. lastOptions: GenerateOptions | null = null
  703. constructor(private summaryText: string) {
  704. super()
  705. }
  706. async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
  707. this.lastOptions = options
  708. yield { type: 'block-start', index: 0, blockType: 'text' }
  709. yield { type: 'text-delta', index: 0, text: this.summaryText }
  710. yield { type: 'finish', reason: { kind: 'stop' } }
  711. }
  712. }
  713. /** Wire a real LlmService + scripted adapter into a context. */
  714. async function ctxWithModel(summaryText: string, model = 'test-model'): Promise<{ ctx: Context; adapter: ScriptedAdapter }> {
  715. const ctx = new Context()
  716. await ctx.plugin(LlmService)
  717. const adapter = new ScriptedAdapter(summaryText)
  718. ctx.llm.registerAdapter([model], adapter)
  719. return { ctx, adapter }
  720. }
  721. /** An adapter whose stream ends with a finish chunk of the given reason (no content). */
  722. class FinishOnlyAdapter extends LlmAdapter {
  723. constructor(private reason: StreamChunk & { type: 'finish' }) {
  724. super()
  725. }
  726. async * stream(): AsyncIterable<StreamChunk> {
  727. yield this.reason
  728. }
  729. }
  730. /** Wire a real LlmService + finish-only adapter into a context. */
  731. async function ctxWithFinish(reason: (StreamChunk & { type: 'finish' })['reason'], model = 'test-model'): Promise<Context> {
  732. const ctx = new Context()
  733. await ctx.plugin(LlmService)
  734. ctx.llm.registerAdapter([model], new FinishOnlyAdapter({ type: 'finish', reason }))
  735. return ctx
  736. }
  737. /** A minimal Agent stub carrying just session + options (enough for the listeners). */
  738. function stubAgent(session: Session, model?: string): Agent {
  739. return { session, options: { model } } as unknown as Agent
  740. }
  741. function compactIfNeeded(
  742. svc: BasicCompactService,
  743. session: Session,
  744. fullSystemPrompt: string,
  745. model: string,
  746. signal: AbortSignal,
  747. ) {
  748. return svc.compactIfNeeded(stubAgent(session, model), 1, 1, fullSystemPrompt, signal)
  749. }
  750. function compactRegion(
  751. svc: BasicCompactService,
  752. session: Session,
  753. start: number,
  754. end: number,
  755. model: string,
  756. signal?: AbortSignal,
  757. ) {
  758. return svc.compactRegion(session, start, end, stubAgent(session, model), 1, 1, signal)
  759. }
  760. function summarize(svc: BasicCompactService, text: string, model: string) {
  761. return svc.summarize(text, stubAgent(new Session(SessionId('summary')), model), 1, 1)
  762. }
  763. describe('BasicCompactService.summarize (real ctx.llm.stream)', () => {
  764. it('summarizes via the registered adapter and returns its content', async () => {
  765. const { ctx, adapter } = await ctxWithModel('SUMMARY TEXT')
  766. const svc = new BasicCompactService(ctx, { auto: false, summarizationMaxTokens: 512 })
  767. const summary = await summarize(svc, 'User: hi\n\nAssistant: hello', 'test-model')
  768. expect(summary).toEqual([{ type: 'text', text: 'SUMMARY TEXT' }])
  769. // The fixed system prompt and maxTokens flow through.
  770. expect(adapter.lastOptions!.system).toContain('compaction engine')
  771. expect(adapter.lastOptions!.system).toContain('## Next Step')
  772. expect(adapter.lastOptions!.maxTokens).toBe(512)
  773. expect(adapter.lastOptions!.messages[0]!.content[0]).toMatchObject({ type: 'text' })
  774. })
  775. it('throws when no model is provided', async () => {
  776. const { ctx } = await ctxWithModel('x')
  777. const svc = new BasicCompactService(ctx, { auto: false })
  778. await expect(summarize(svc, 'text', '')).rejects.toThrow(/no model available/)
  779. })
  780. it('rethrows when the stream ends with a finish-error chunk', async () => {
  781. const ctx = await ctxWithFinish({ kind: 'error', message: 'provider 401', code: 'UNAUTHORIZED' })
  782. const svc = new BasicCompactService(ctx, { auto: false })
  783. await expect(summarize(svc, 'text', 'test-model')).rejects.toMatchObject({ message: 'provider 401', code: 'UNAUTHORIZED' })
  784. })
  785. it('rethrows a finish-error chunk without a code (code stays undefined)', async () => {
  786. const ctx = await ctxWithFinish({ kind: 'error', message: 'opaque failure' })
  787. const svc = new BasicCompactService(ctx, { auto: false })
  788. const error = await summarize(svc, 'text', 'test-model').then(() => null, (e: unknown) => e as Error & { code?: string })
  789. expect(error?.message).toBe('opaque failure')
  790. expect(error?.code).toBeUndefined()
  791. })
  792. it('rethrows when the stream ends with a finish-aborted chunk', async () => {
  793. const ctx = await ctxWithFinish({ kind: 'aborted' })
  794. const svc = new BasicCompactService(ctx, { auto: false })
  795. await expect(summarize(svc, 'text', 'test-model')).rejects.toMatchObject({ message: 'summarization stream aborted', code: 'ABORTED' })
  796. })
  797. it('fails closed on a max-tokens finish (an incomplete checkpoint must not commit)', async () => {
  798. const ctx = await ctxWithFinish({ kind: 'max-tokens' })
  799. const svc = new BasicCompactService(ctx, { auto: false })
  800. await expect(summarize(svc, 'text', 'test-model')).rejects.toMatchObject({ code: 'MAX_TOKENS' })
  801. })
  802. it('compactRegion leaves the surface intact when summarization hits max-tokens', async () => {
  803. const ctx = await ctxWithFinish({ kind: 'max-tokens' })
  804. const svc = new BasicCompactService(ctx, { auto: false })
  805. const session = multiTurnSession(2, 1)
  806. const before = [...session.surface.nodes]
  807. const nodes = session.surface.nodes
  808. await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'test-model'))
  809. .rejects.toMatchObject({ code: 'MAX_TOKENS' })
  810. // No replacement landed — the surface is byte-identical, and the lock was
  811. // released with the error (compact/end carries it).
  812. expect(session.surface.nodes).toEqual(before)
  813. const endEvent = session.events.findLast(e => e.type === 'compact/end')!
  814. const endData = endEvent.data as { error?: string }
  815. expect(endData.error).toContain('truncated')
  816. })
  817. it('compactRegion uses the real summarizer end-to-end', async () => {
  818. const { ctx } = await ctxWithModel('CONDENSED')
  819. const svc = new BasicCompactService(ctx, { auto: false })
  820. const session = multiTurnSession(2, 1)
  821. const nodes = session.surface.nodes
  822. const result = await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'test-model')
  823. expect(result.summary).toEqual([{ type: 'text', text: 'CONDENSED' }])
  824. // The raw summary is wrapped in the checkpoint framing on the surface.
  825. expect(session.deriveMessages()[0]!.content).toContainEqual({ type: 'text', text: 'CONDENSED' })
  826. })
  827. })
  828. describe('BasicCompactService auto-compaction (agent/pre-step listener)', () => {
  829. /** Fire the agent/pre-step serial checkpoint as the loop does. */
  830. function firePreStep(ctx: Context, agent: Agent, step: number, fullSystemPrompt: string): Promise<unknown> {
  831. return ctx.serial('agent/pre-step', agent, 1, step, fullSystemPrompt, SIGNAL)
  832. }
  833. it('compacts (mutating the surface) when over threshold', async () => {
  834. const { ctx } = await ctxWithModel('SUMMARY')
  835. void new BasicCompactService(ctx, { contextWindow: 200, thresholdRatio: 0.5, retainTokens: 20, summarizationMaxTokens: 50 })
  836. const session = multiTurnSession(5, 1) // 10 surface nodes
  837. const agent = stubAgent(session, 'test-model')
  838. const before = session.surface.nodes.length
  839. await firePreStep(ctx, agent, 1, '')
  840. // The surface shrank in place, and a summary checkpoint landed.
  841. expect(session.surface.nodes.length).toBeLessThan(before)
  842. expect(session.events.some(e => e.type === 'compact/summary')).toBe(true)
  843. // The re-derived head message is the framed summary checkpoint.
  844. expect(session.deriveMessages()[0]!.content).toContainEqual({ type: 'text', text: 'SUMMARY' })
  845. })
  846. it('compacts mid-turn on steps after the first (the surface grows within a turn)', async () => {
  847. const { ctx } = await ctxWithModel('SUMMARY')
  848. void new BasicCompactService(ctx, { contextWindow: 100, thresholdRatio: 0.5, retainTokens: 10, summarizationMaxTokens: 30 })
  849. const session = multiTurnSession(3, 1) // over the 0.5 threshold
  850. const agent = stubAgent(session, 'test-model')
  851. // A step-2 checkpoint (a tool-heavy turn's later step) must still compact —
  852. // the surface accumulated assistant/message + tool/result nodes since step 1.
  853. await firePreStep(ctx, agent, 2, '')
  854. expect(session.events.some(e => e.type === 'compact/start')).toBe(true)
  855. })
  856. it('does nothing when under threshold', async () => {
  857. const { ctx } = await ctxWithModel('SUMMARY')
  858. void new BasicCompactService(ctx, { contextWindow: 128000, thresholdRatio: 0.8 })
  859. const session = multiTurnSession(1, 1)
  860. const agent = stubAgent(session, 'test-model')
  861. await firePreStep(ctx, agent, 1, '')
  862. expect(session.events.some(e => e.type === 'compact/start')).toBe(false)
  863. })
  864. it('leaves the surface intact when compaction fails (summarize rejects)', async () => {
  865. // No adapter registered for this model → summarize() rejects → caught, the
  866. // surface is untouched (the loop derives the full history).
  867. const ctx = new Context()
  868. await ctx.plugin(LlmService)
  869. void new BasicCompactService(ctx, { contextWindow: 300, thresholdRatio: 0.1, retainTokens: 10, summarizationMaxTokens: 1 })
  870. const session = multiTurnSession(3, 1)
  871. const agent = stubAgent(session, 'missing-model')
  872. const before = session.surface.nodes.length
  873. await firePreStep(ctx, agent, 1, '')
  874. // No summary landed; the surface is unchanged.
  875. expect(session.events.some(e => e.type === 'compact/summary')).toBe(false)
  876. expect(session.surface.nodes.length).toBe(before)
  877. })
  878. it('does not register the listener when auto is false', async () => {
  879. const { ctx } = await ctxWithModel('SUMMARY')
  880. void new BasicCompactService(ctx, { auto: false, contextWindow: 100, thresholdRatio: 0.1, retainTokens: 5, summarizationMaxTokens: 1 })
  881. const session = multiTurnSession(3, 1)
  882. const agent = stubAgent(session, 'test-model')
  883. await firePreStep(ctx, agent, 1, '')
  884. expect(session.events.some(e => e.type === 'compact/start')).toBe(false)
  885. })
  886. it('routes summarization through agent/request so router agents can choose the model', async () => {
  887. const { ctx, adapter } = await ctxWithModel('ROUTED SUMMARY', 'routed-model')
  888. ctx.on('agent/request', async (_agent, _turn, _step, options, next) => {
  889. options.model = 'routed-model'
  890. return next()
  891. })
  892. void new BasicCompactService(ctx, { contextWindow: 200, thresholdRatio: 0.5, retainTokens: 20, summarizationMaxTokens: 50 })
  893. const session = multiTurnSession(5, 1)
  894. const agent = stubAgent(session)
  895. await ctx.serial('agent/pre-step', agent, 1, 1, '', SIGNAL)
  896. expect(adapter.lastOptions?.model).toBe('routed-model')
  897. expect(session.events.some(e => e.type === 'compact/summary')).toBe(true)
  898. expect(session.deriveMessages()[0]!.content).toContainEqual({ type: 'text', text: 'ROUTED SUMMARY' })
  899. })
  900. it('removes the auto pre-step listener when the plugin fiber is disposed', async () => {
  901. const { ctx } = await ctxWithModel('SUMMARY')
  902. const fiber = await ctx.plugin(BasicCompactService, {
  903. contextWindow: 200,
  904. thresholdRatio: 0.5,
  905. retainTokens: 20,
  906. summarizationMaxTokens: 50,
  907. })
  908. const session = multiTurnSession(5, 1)
  909. const agent = stubAgent(session, 'test-model')
  910. await fiber.dispose()
  911. await firePreStep(ctx, agent, 1, '')
  912. expect(session.events.some(e => e.type === 'compact/start')).toBe(false)
  913. expect(ctx.get('compact')).toBeUndefined()
  914. })
  915. })
  916. describe('BasicCompactService._extractText branches', () => {
  917. it('renders reasoning, context, and steering messages', async () => {
  918. const svc = createTestService()
  919. const s = new Session(SessionId('rich'))
  920. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  921. s.append('step/start', { turn: 1, step: 1 })
  922. s.append('context/message', {
  923. content: [{ type: 'text', text: 'project context here' }],
  924. source: { kind: 'user' },
  925. }, { surfaceOp: 'append' })
  926. s.append('assistant/message', {
  927. turn: 1, step: 1,
  928. content: [{ type: 'reasoning', text: 'thinking hard' }, { type: 'text', text: 'answer' }],
  929. }, { surfaceOp: 'append' })
  930. s.append('steering/message', {
  931. turn: 1,
  932. content: [{ type: 'text', text: 'steer this way' }],
  933. source: { kind: 'user' },
  934. }, { surfaceOp: 'append' })
  935. s.append('step/end', { turn: 1, step: 1 })
  936. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  937. s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
  938. const nodes = s.surface.nodes
  939. await compactRegion(svc, s, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
  940. const { text } = svc.summarizeCalls[0]!
  941. expect(text).toContain('[Context: project context here]')
  942. expect(text).toContain('[reasoning: thinking hard]')
  943. expect(text).toContain('[Steering: steer this way]')
  944. })
  945. it('labels tool errors distinctly from tool results', async () => {
  946. const svc = createTestService()
  947. const s = new Session(SessionId('toolerr'))
  948. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  949. s.append('step/start', { turn: 1, step: 1 })
  950. s.append('user/message', { content: [{ type: 'text', text: 'run it' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  951. s.append('assistant/message', {
  952. turn: 1, step: 1,
  953. content: [{ type: 'tool-call', id: CallId('c9'), name: 'bash', arguments: '{}' }],
  954. }, { surfaceOp: 'append' })
  955. s.append('tool/call', { turn: 1, step: 1, callId: CallId('c9'), name: 'bash', arguments: '{}' })
  956. s.append('tool/result', {
  957. turn: 1, step: 1, callId: CallId('c9'),
  958. content: [{ type: 'text', text: 'boom failure' }],
  959. isError: true,
  960. }, { surfaceOp: 'append' })
  961. s.append('step/end', { turn: 1, step: 1 })
  962. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  963. s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
  964. const nodes = s.surface.nodes
  965. await compactRegion(svc, s, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
  966. expect(svc.summarizeCalls[0]!.text).toContain('Tool error (call c9): boom failure')
  967. })
  968. })
  969. describe('BasicCompactService edge cases', () => {
  970. it('renders bare and nested tool-result placeholders and unknown blocks', async () => {
  971. const svc = createTestService()
  972. const s = new Session(SessionId('toolresult'))
  973. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  974. s.append('step/start', { turn: 1, step: 1 })
  975. // assistant/message carrying a nested tool-result block, an unknown block,
  976. // and the tool-call that the following tool/result answers (so the surface
  977. // is tool-pairing balanced).
  978. s.append('assistant/message', {
  979. turn: 1, step: 1,
  980. content: [
  981. { type: 'tool-result', toolCallId: CallId('n1'), content: [{ type: 'image', url: 'https://x/n.png' }] },
  982. { type: 'custom-widget', payload: 'x' } as unknown as ContentBlock,
  983. { type: 'tool-call', id: CallId('b1'), name: 'bash', arguments: '{}' },
  984. ],
  985. }, { surfaceOp: 'append' })
  986. // tool/result whose content is itself only non-text → bare '[tool-result]'.
  987. s.append('tool/call', { turn: 1, step: 1, callId: CallId('b1'), name: 'bash', arguments: '{}' })
  988. s.append('tool/result', {
  989. turn: 1, step: 1, callId: CallId('b1'),
  990. content: [{ type: 'tool-result', toolCallId: CallId('inner'), content: [] }],
  991. isError: false,
  992. }, { surfaceOp: 'append' })
  993. s.append('step/end', { turn: 1, step: 1 })
  994. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  995. s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
  996. const nodes = s.surface.nodes
  997. await compactRegion(svc, s, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
  998. const { text } = svc.summarizeCalls[0]!
  999. expect(text).toContain('[tool-result: [image]]') // nested tool-result with content
  1000. expect(text).toContain('[custom-widget]') // unknown block placeholder
  1001. expect(text).toContain('Tool result (call b1): [tool-result]') // empty nested → bare placeholder
  1002. })
  1003. it('estimates unknown block types via JSON length (default branch)', () => {
  1004. const svc = new BasicCompactService(new Context(), { auto: false })
  1005. // A block whose type is none of the known kinds — exercises the default arm.
  1006. const unknown = { type: 'custom-widget', payload: 'some data' } as unknown as ContentBlock
  1007. expect(svc.estimateContentTokens([unknown])).toBeGreaterThan(0)
  1008. })
  1009. it('compacts once without re-checking a post-compaction threshold', async () => {
  1010. const { ctx } = await ctxWithModel('SUMMARY')
  1011. // Even with a window so tiny the post-compaction history still exceeds the
  1012. // threshold, the agnostic listener does NOT re-gate or warn — it compacts
  1013. // once (the single check lives in compactIfNeeded) and proceeds.
  1014. const warnings: string[] = []
  1015. ctx.logger.warn = ((msg: string) => void warnings.push(msg)) as typeof ctx.logger.warn
  1016. void new BasicCompactService(ctx, { contextWindow: 300, thresholdRatio: 0.1, retainTokens: 5, summarizationMaxTokens: 5 })
  1017. const session = multiTurnSession(4, 1)
  1018. const agent = stubAgent(session, 'test-model')
  1019. await ctx.serial('agent/pre-step', agent, 1, 1, '', SIGNAL)
  1020. expect(session.events.some(e => e.type === 'compact/summary')).toBe(true)
  1021. // The surface was mutated; the head message is the framed summary checkpoint.
  1022. expect(session.deriveMessages()[0]!.content).toContainEqual({ type: 'text', text: 'SUMMARY' })
  1023. // No cascade warning is emitted.
  1024. expect(warnings.length).toBe(0)
  1025. })
  1026. it('rejects compaction when no turn is open (compaction events must be turn-enclosed)', async () => {
  1027. const svc = createTestService()
  1028. // A session whose only turn has CLOSED — scanning back from the tail hits
  1029. // turn/end before any turn/start, so there is no open turn to enclose
  1030. // compaction's compact/* + replacement events, which the log contract forbids.
  1031. const s = new Session(SessionId('noturn'))
  1032. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  1033. s.append('step/start', { turn: 1, step: 1 })
  1034. s.append('user/message', { content: [{ type: 'text', text: 'orphan' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  1035. s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'text', text: 'reply' }] }, { surfaceOp: 'append' })
  1036. s.append('step/end', { turn: 1, step: 1 })
  1037. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  1038. const nodes = s.surface.nodes
  1039. await expect(compactRegion(svc, s, nodes[0]!.seq, nodes[1]!.seq, 'm'))
  1040. .rejects.toThrow(/no open turn/)
  1041. // The lock was never acquired — no compact/start landed.
  1042. expect(s.events.some(e => e.type === 'compact/start')).toBe(false)
  1043. })
  1044. it('rejects compaction on a session with no turn boundaries at all', async () => {
  1045. const svc = createTestService()
  1046. // No turn events whatsoever — the open-turn scan falls through to the end
  1047. // of the log and finds none, so compaction is rejected (its events have no
  1048. // turn to enclose them).
  1049. const s = new Session(SessionId('turnless'))
  1050. s.append('user/message', { content: [{ type: 'text', text: 'orphan' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  1051. const nodes = s.surface.nodes
  1052. await expect(compactRegion(svc, s, nodes[0]!.seq, nodes[0]!.seq, 'm'))
  1053. .rejects.toThrow(/no open turn/)
  1054. expect(s.events.some(e => e.type === 'compact/start')).toBe(false)
  1055. })
  1056. it('compactIfNeeded returns null for empty surface even when over threshold', async () => {
  1057. const svc = createTestService({ contextWindow: 1000, thresholdRatio: 0.1, retainTokens: 5 })
  1058. const session = new Session(SessionId('empty-but-pressured'))
  1059. // No surface nodes, but a large system prompt pushes the estimate over threshold.
  1060. const bigPrompt = 'x'.repeat(800) // ceil(800/4) = 200 tokens >> threshold 100
  1061. expect(await compactIfNeeded(svc, session, bigPrompt, 'm', SIGNAL)).toBeNull()
  1062. })
  1063. it('compactRegion throws when end is not a surface node (start valid)', async () => {
  1064. const svc = createTestService()
  1065. const session = multiTurnSession(1, 1)
  1066. const nodes = session.surface.nodes
  1067. await expect(compactRegion(svc, session, nodes[0]!.seq, 9999, 'm'))
  1068. .rejects.toThrow(/end seq 9999 not found in surface/)
  1069. })
  1070. it('compactRegion stringifies a non-Error thrown by summarize', async () => {
  1071. const svc = createTestService()
  1072. // Throw a non-Error value to exercise the String(error) branch in the catch.
  1073. svc.summarizeError = 'plain string failure' as unknown as Error
  1074. const session = multiTurnSession(1, 1)
  1075. const nodes = session.surface.nodes
  1076. // Whole step (user → assistant): a step-aligned region that reaches summarize.
  1077. await expect(compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'm')).rejects.toBe('plain string failure')
  1078. const endEvent = session.events.findLast(e => e.type === 'compact/end')!
  1079. expect(endEvent.data).toMatchObject({ error: 'plain string failure' })
  1080. })
  1081. it('auto-compaction listener stringifies a non-Error and proceeds', async () => {
  1082. const { ctx } = await ctxWithModel('SUMMARY')
  1083. const warnings: string[] = []
  1084. ctx.logger.warn = ((msg: string) => void warnings.push(msg)) as typeof ctx.logger.warn
  1085. const svc = new TestCompactService(ctx, { contextWindow: 300, thresholdRatio: 0.1, retainTokens: 10, summarizationMaxTokens: 10 })
  1086. svc.summarizeError = 'boom' as unknown as Error
  1087. const session = multiTurnSession(3, 1)
  1088. const agent = stubAgent(session, 'test-model')
  1089. const before = session.surface.nodes.length
  1090. await ctx.serial('agent/pre-step', agent, 1, 1, '', SIGNAL)
  1091. // The failure was swallowed; the surface is untouched and a warning logged.
  1092. expect(session.surface.nodes.length).toBe(before)
  1093. expect(session.events.some(e => e.type === 'compact/summary')).toBe(false)
  1094. expect(warnings.some(w => w.includes('compaction failed: boom'))).toBe(true)
  1095. })
  1096. it('auto-compaction listener takes the result-null branch (nothing to compact)', async () => {
  1097. const { ctx } = await ctxWithModel('SUMMARY')
  1098. // A large system prompt pushes the listener's estimate over threshold, but
  1099. // retainTokens is huge so compactIfNeeded walks everything and returns null.
  1100. // threshold = floor(2000*0.1) = 200; invariant: 5 + 150 = 155 ≤ 200.
  1101. const svc = new TestCompactService(ctx, { contextWindow: 2000, thresholdRatio: 0.1, retainTokens: 150, summarizationMaxTokens: 5 })
  1102. const session = multiTurnSession(2, 1)
  1103. const agent = stubAgent(session, 'test-model')
  1104. const bigSystem = 'x'.repeat(900) // ceil(900/4)=225 > threshold 200
  1105. await ctx.serial('agent/pre-step', agent, 1, 1, bigSystem, SIGNAL)
  1106. expect(session.events.some(e => e.type === 'compact/start')).toBe(false)
  1107. expect(svc.summarizeCalls.length).toBe(0)
  1108. })
  1109. it('skips messages whose extracted text is empty across all kinds', async () => {
  1110. const svc = createTestService()
  1111. const s = new Session(SessionId('empties'))
  1112. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  1113. // Step 1: an empty-text user, an empty-reasoning assistant with NO tool-call
  1114. // (balanced: nothing to answer), and empty context/steering — all extract to
  1115. // nothing and are skipped.
  1116. s.append('step/start', { turn: 1, step: 1 })
  1117. s.append('user/message', { content: [{ type: 'text', text: '' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  1118. s.append('assistant/message', { turn: 1, step: 1, content: [{ type: 'reasoning', text: '' }] }, { surfaceOp: 'append' })
  1119. s.append('context/message', { content: [], source: { kind: 'user' } }, { surfaceOp: 'append' })
  1120. s.append('steering/message', { turn: 1, content: [{ type: 'text', text: '' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  1121. s.append('step/end', { turn: 1, step: 1 })
  1122. // Step 2: a tool exchange whose tool/result has empty content → empty
  1123. // extraction → skipped. The assistant carries the matching tool-call so the
  1124. // surface stays tool-pairing balanced; its text extracts to the tool-call
  1125. // placeholder (the one surviving line).
  1126. s.append('step/start', { turn: 1, step: 2 })
  1127. s.append('assistant/message', {
  1128. turn: 1, step: 2,
  1129. content: [{ type: 'tool-call', id: CallId('z1'), name: 'bash', arguments: '{}' }],
  1130. }, { surfaceOp: 'append' })
  1131. s.append('tool/call', { turn: 1, step: 2, callId: CallId('z1'), name: 'bash', arguments: '{}' })
  1132. s.append('tool/result', { turn: 1, step: 2, callId: CallId('z1'), content: [], isError: false }, { surfaceOp: 'append' })
  1133. s.append('step/end', { turn: 1, step: 2 })
  1134. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  1135. s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
  1136. const nodes = s.surface.nodes
  1137. await compactRegion(svc, s, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
  1138. // Every empty-content message (user text, empty reasoning, empty-content
  1139. // tool/result, empty context, empty steering) extracted to nothing and was
  1140. // skipped — the only surviving line is the assistant's tool-call (which a
  1141. // balanced surface requires to answer the tool/result).
  1142. expect(svc.summarizeCalls[0]!.text).toBe('Assistant: [tool-call: bash({})]')
  1143. })
  1144. it('renders non-text blocks as type-tagged placeholders across all message kinds', async () => {
  1145. const svc = createTestService()
  1146. const s = new Session(SessionId('placeholders'))
  1147. s.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  1148. s.append('step/start', { turn: 1, step: 1 })
  1149. // user/message with only an image block → '[image]' placeholder.
  1150. s.append('user/message', { content: [{ type: 'image', url: 'https://x/y.png' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  1151. // assistant/message with an image block AND the tool-call its tool/result
  1152. // answers (so the surface is tool-pairing balanced) → '[image]' placeholder.
  1153. s.append('assistant/message', {
  1154. turn: 1, step: 1,
  1155. content: [
  1156. { type: 'image', url: 'https://x/z.png' },
  1157. { type: 'tool-call', id: CallId('e1'), name: 'bash', arguments: '{}' },
  1158. ],
  1159. }, { surfaceOp: 'append' })
  1160. // tool/result with an image block → '[image]' placeholder.
  1161. s.append('tool/call', { turn: 1, step: 1, callId: CallId('e1'), name: 'bash', arguments: '{}' })
  1162. s.append('tool/result', { turn: 1, step: 1, callId: CallId('e1'), content: [{ type: 'image', url: 'https://x/r.png' }], isError: false }, { surfaceOp: 'append' })
  1163. // context/message and steering/message with image content.
  1164. s.append('context/message', { content: [{ type: 'image', url: 'https://x/c.png' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  1165. s.append('steering/message', { turn: 1, content: [{ type: 'image', url: 'https://x/s.png' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  1166. s.append('step/end', { turn: 1, step: 1 })
  1167. s.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  1168. s.append('turn/start', { turn: 2, trigger: { kind: 'message', source: { kind: 'user' } } })
  1169. const nodes = s.surface.nodes
  1170. await compactRegion(svc, s, nodes[0]!.seq, nodes[nodes.length - 1]!.seq, 'm')
  1171. const { text } = svc.summarizeCalls[0]!
  1172. // Every non-text block surfaces as a placeholder rather than being dropped.
  1173. expect(text).toContain('User: [image]')
  1174. expect(text).toContain('Assistant: [image]')
  1175. expect(text).toContain('Tool result (call e1): [image]')
  1176. expect(text).toContain('[Context: [image]]')
  1177. expect(text).toContain('[Steering: [image]]')
  1178. })
  1179. })
  1180. describe('BasicCompactService positional range (surface seqs are not monotonic after a replace)', () => {
  1181. it('compacts a second region after the first replace lands a high-seq summary at the head position', async () => {
  1182. // A replace inserts the new summary node (a high seq) AT the shadowed
  1183. // range's surface position, so the surface becomes
  1184. // [highSeqSummary, …olderRetainedLowerSeqs]. A second compaction over a
  1185. // range whose start node has a HIGHER seq than its end node must still
  1186. // succeed — the range is positional, not a numeric seq interval.
  1187. const svc = createTestService({ auto: false })
  1188. const session = multiTurnSession(4, 1)
  1189. // First compaction: shadow the two oldest surface nodes.
  1190. const nodes0 = session.surface.nodes
  1191. const first = await compactRegion(svc, session, nodes0[0]!.seq, nodes0[1]!.seq, 'm')
  1192. // The summary node now sits at the head with a seq HIGHER than the
  1193. // retained older nodes that follow it — the non-monotonic surface. (The
  1194. // head is the user/message replace node, appended after the compact/summary
  1195. // provenance event, so its seq is at least first.summarySeq.)
  1196. const nodes1 = session.surface.nodes
  1197. expect(nodes1[0]!.seq).toBeGreaterThanOrEqual(first.summarySeq)
  1198. expect(nodes1[0]!.seq).toBeGreaterThan(nodes1[1]!.seq)
  1199. // Second compaction: shadow [summary(head) … turn-2's step end]. The start
  1200. // seq (the head summary node) is GREATER than the end seq (an older retained
  1201. // node), so the range is a SURFACE-POSITION span, not a numeric seq interval.
  1202. // The end must land on a step boundary (turn-2's assistant message closes
  1203. // its step).
  1204. const startSeq = nodes1[0]!.seq
  1205. const endSeq = nodes1[2]!.seq
  1206. expect(startSeq).toBeGreaterThan(endSeq)
  1207. const second = await compactRegion(svc, session, startSeq, endSeq, 'm')
  1208. // Exactly the three nodes at surface positions [0..2] are shadowed, in
  1209. // surface order — the positional slice, regardless of their seq values.
  1210. expect(second.shadowedSeqs).toEqual([nodes1[0]!.seq, nodes1[1]!.seq, nodes1[2]!.seq])
  1211. // The surface still derives cleanly: a new head replace node + the rest.
  1212. const finalNodes = session.surface.nodes
  1213. expect(finalNodes[0]!.seq).toBeGreaterThanOrEqual(second.summarySeq)
  1214. expect(session.deriveMessages().length).toBe(finalNodes.length)
  1215. })
  1216. it('extracts the second-compaction transcript in surface order, not log-seq order', async () => {
  1217. const svc = createTestService({ auto: false })
  1218. const session = multiTurnSession(3, 1)
  1219. // First compaction shadows the oldest two surface nodes, landing a high-seq
  1220. // summary node at the head.
  1221. const n0 = session.surface.nodes
  1222. await compactRegion(svc, session, n0[0]!.seq, n0[1]!.seq, 'm')
  1223. // Second compaction spans [head summary … turn-2's step end]. The head's seq
  1224. // is higher than the older retained nodes' seqs, so a log-seq-order walk
  1225. // would emit the older messages BEFORE the checkpoint.
  1226. const n1 = session.surface.nodes
  1227. svc.summarizeCalls = []
  1228. await compactRegion(svc, session, n1[0]!.seq, n1[2]!.seq, 'm')
  1229. // The extracted transcript follows surface order: the checkpoint (head)
  1230. // first, then the older retained messages — matching deriveMessages().
  1231. const { text } = svc.summarizeCalls[0]!
  1232. const checkpointIdx = text.indexOf('compacted-summary')
  1233. const olderIdx = text.indexOf('turn 2 user')
  1234. expect(checkpointIdx).toBeGreaterThanOrEqual(0)
  1235. expect(olderIdx).toBeGreaterThan(checkpointIdx)
  1236. })
  1237. })
  1238. describe('BasicCompactService llm inject (real plugin-load path)', () => {
  1239. it('declares llm in static inject so a sibling fiber can resolve ctx.llm', () => {
  1240. // summarize() reads ctx.llm; the inject lets the cordis ctx proxy resolve a
  1241. // sibling LlmService when this service is mounted as its own plugin fiber.
  1242. // Asserting the declaration (and exercising the real mount below) guards the
  1243. // resolution that root-ctx unit tests cannot, since they share one fiber.
  1244. expect(BasicCompactService.inject).toContain('llm')
  1245. })
  1246. it('resolves ctx.llm and summarizes when mounted as a sibling plugin of LlmService', async () => {
  1247. const ctx = new Context()
  1248. await ctx.plugin(LlmService)
  1249. ctx.llm.registerAdapter(['test-model'], new ScriptedAdapter('CONDENSED'))
  1250. // Mount the service through its real plugin fiber (NOT new …(rootCtx)), so
  1251. // the sibling-fiber ctx.llm resolution actually exercises the inject.
  1252. const fiber = await ctx.plugin(BasicCompactService, { auto: false })
  1253. const svc = ctx.compact as BasicCompactService
  1254. const session = multiTurnSession(2, 1)
  1255. const nodes = session.surface.nodes
  1256. const result = await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'test-model')
  1257. expect(result.summary).toEqual([{ type: 'text', text: 'CONDENSED' }])
  1258. // Tear the fiber down so this test owns no leaked registration; the
  1259. // dedicated cleanup assertion lives in the "HMR safety" suite.
  1260. await fiber.dispose()
  1261. expect(ctx.get('compact')).toBeUndefined()
  1262. })
  1263. })
  1264. describe('BasicCompactService under the real invariants plugin', () => {
  1265. /**
  1266. * Drive compaction through a session whose `session/event` listeners include
  1267. * the real dev-mode invariants plugin (as a real app loads it via agent-core).
  1268. * The invariants throw on append, so a passing run proves the compaction
  1269. * sequence is contract-valid: every event is turn-enclosed, and the positional
  1270. * replace op is accepted even when the surface is no longer seq-ordered.
  1271. */
  1272. async function setup(): Promise<{ ctx: Context; session: Session; svc: BasicCompactService }> {
  1273. const ctx = new Context()
  1274. await ctx.plugin(SessionStore)
  1275. await ctx.plugin(Invariants, {})
  1276. await ctx.plugin(LlmService)
  1277. ctx.llm.registerAdapter(['test-model'], new ScriptedAdapter('CONDENSED'))
  1278. await ctx.plugin(BasicCompactService, { auto: false })
  1279. const session = ctx.sessions.create()
  1280. return { ctx, session, svc: ctx.compact as BasicCompactService }
  1281. }
  1282. /** Append one closed turn of [user, assistant] surface nodes via the store. */
  1283. function closedTurn(session: Session, turn: number): void {
  1284. session.append('turn/start', { turn, trigger: { kind: 'message', source: { kind: 'user' } } })
  1285. session.append('step/start', { turn, step: 1 })
  1286. session.append('user/message', { content: [{ type: 'text', text: `turn ${turn} user` }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  1287. session.append('assistant/message', { turn, step: 1, content: [{ type: 'text', text: `turn ${turn} assistant` }] }, { surfaceOp: 'append' })
  1288. session.append('step/end', { turn, step: 1 })
  1289. session.append('turn/end', { turn, reason: { kind: 'completed' } })
  1290. }
  1291. it('runs a turn-enclosed compaction whose positional replace the invariants accept', async () => {
  1292. const { session, svc } = await setup()
  1293. closedTurn(session, 1)
  1294. closedTurn(session, 2)
  1295. // Open turn 3, as the loop has when the auto-compaction listener fires.
  1296. session.append('turn/start', { turn: 3, trigger: { kind: 'message', source: { kind: 'user' } } })
  1297. const nodes = session.surface.nodes
  1298. // No invariant throws here: compact/* + the replacement are all in turn 3.
  1299. const result = await compactRegion(svc, session, nodes[0]!.seq, nodes[1]!.seq, 'test-model')
  1300. expect(result.shadowedSeqs.length).toBe(2)
  1301. expect(session.surface.nodes[0]!.seq).toBeGreaterThan(session.surface.nodes[1]!.seq)
  1302. })
  1303. it('accepts a second compaction over the non-monotonic surface left by the first', async () => {
  1304. const { session, svc } = await setup()
  1305. closedTurn(session, 1)
  1306. closedTurn(session, 2)
  1307. closedTurn(session, 3)
  1308. session.append('turn/start', { turn: 4, trigger: { kind: 'message', source: { kind: 'user' } } })
  1309. const n0 = session.surface.nodes
  1310. await compactRegion(svc, session, n0[0]!.seq, n0[1]!.seq, 'test-model')
  1311. // Surface head now carries a higher seq than the older retained nodes. A
  1312. // second compaction spanning [head … a later closed-step end] must pass the
  1313. // invariants' positional replace check even though startSeq > endSeq.
  1314. const n1 = session.surface.nodes
  1315. expect(n1[0]!.seq).toBeGreaterThan(n1[2]!.seq)
  1316. const second = await compactRegion(svc, session, n1[0]!.seq, n1[2]!.seq, 'test-model')
  1317. expect(second.shadowedSeqs).toEqual([n1[0]!.seq, n1[1]!.seq, n1[2]!.seq])
  1318. })
  1319. })