compact-basic.spec.ts 86 KB

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