compact-basic.spec.ts 83 KB

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