1
0

compaction-basic.spec.ts 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  1. import { describe, expect, expectTypeOf, it, vi } from 'vitest'
  2. import { Context } from '@deepseek-ai/cordis'
  3. import { AttachmentId } from '@deepseek-ai/dsh-attachment'
  4. import BasicCompactionEngine from '@deepseek-ai/dsh-compaction-basic'
  5. import type { BasicCompactionConfig } from '@deepseek-ai/dsh-compaction-basic'
  6. import { selectCompactableRange } from '@deepseek-ai/dsh-compaction-basic/src/region.ts'
  7. import { frameSummary } from '@deepseek-ai/dsh-compaction-basic/src/summarizer.ts'
  8. import type { SummarizationInput, SummaryResult } from '@deepseek-ai/dsh-compaction-basic/src/summarizer.ts'
  9. import { CompactionId, toolPairingBalancedAfter, toolPairingBalancedBefore } from '@deepseek-ai/dsh-compaction'
  10. import {
  11. resolveCompactSpec,
  12. resolveConfig,
  13. resolveTargetPolicy,
  14. } from '@deepseek-ai/dsh-compaction-basic/src/config.ts'
  15. import type { CompactionResult } from '@deepseek-ai/dsh-compaction'
  16. import LlmRuntime, { createUserMessage, ToolCallId, CONTEXT_WINDOW_EXCEEDED_CODE, createSystemMessage, createToolResultMessage, LlmAdapter , createMessage } from '@deepseek-ai/dsh-llm'
  17. import type {
  18. ContentBlock,
  19. GenerateOptions,
  20. LlmFailure,
  21. LlmResolvedModelInfo,
  22. Message,
  23. StreamChunk,
  24. TokenUsage,
  25. } from '@deepseek-ai/dsh-llm'
  26. import SessionStore, { Session, SessionId, SessionSeq } from '@deepseek-ai/dsh-session'
  27. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  28. import TokenMeter from '@deepseek-ai/dsh-token-meter'
  29. import { agentEvents, type Agent, type RequestErrorAction } from '@deepseek-ai/dsh-agent'
  30. import ToolResultPruner from '@deepseek-ai/dsh-compaction-tool-result-pruner'
  31. const SIGNAL = new AbortController().signal
  32. const MODEL = 'test-model'
  33. class ContextAdapter extends LlmAdapter {
  34. constructor(private readonly contextWindow: number) {
  35. super()
  36. }
  37. override resolveModel(provider: string, model: string): Promise<LlmResolvedModelInfo> {
  38. return Promise.resolve({
  39. provider,
  40. id: model,
  41. name: model,
  42. context: { contextWindow: this.contextWindow },
  43. })
  44. }
  45. override async * stream(): AsyncIterable<StreamChunk> {
  46. yield { type: 'finish', reason: { kind: 'stop' } }
  47. }
  48. }
  49. class RoutedContextAdapter extends LlmAdapter {
  50. constructor(private readonly windows: Readonly<Record<string, number>>) {
  51. super()
  52. }
  53. override resolveModel(provider: string, model: string): Promise<LlmResolvedModelInfo> {
  54. const contextWindow = this.windows[provider]
  55. return Promise.resolve({
  56. provider,
  57. id: model,
  58. name: model,
  59. ...contextWindow === undefined ? {} : { context: { contextWindow } },
  60. })
  61. }
  62. override async * stream(): AsyncIterable<StreamChunk> {
  63. yield { type: 'finish', reason: { kind: 'stop' } }
  64. }
  65. }
  66. function createContext(contextWindow = 1_000): Context {
  67. const ctx = new Context()
  68. void new LlmRuntime(ctx)
  69. // The registry is a required injection of TokenMeter (its three
  70. // projection units register in the constructor); mount it synchronously.
  71. new SessionProjectionRegistry(ctx)
  72. void new TokenMeter(ctx)
  73. ctx.llm.registerAdapter([MODEL, 'actual', 'unlisted-provider'], new ContextAdapter(contextWindow))
  74. return ctx
  75. }
  76. function agent(session: Session, model?: string): Agent {
  77. return {
  78. session,
  79. options: model === undefined ? {} : { provider: model, model },
  80. } as Agent
  81. }
  82. /** Flatten every text fragment the summarizer received, recursing tool-result blocks. */
  83. function summarizedText(input: SummarizationInput): string {
  84. const collect = (blocks: readonly ContentBlock[]): string =>
  85. blocks.map(block =>
  86. block.type === 'text' ? block.text
  87. : block.type === 'tool-result' ? collect(block.content)
  88. : '').join('\n')
  89. return input.messages.map(message => collect(message.content)).join('\n')
  90. }
  91. /** A minimal replayed prefix carrying one user message of the given text. */
  92. function promptInput(text: string): SummarizationInput {
  93. return { messages: [createUserMessage({
  94. content: [{ type: 'text', text }],
  95. source: { kind: 'plugin', plugin: 'test' },
  96. })] }
  97. }
  98. const SYSTEM_PROMPT_PLUGIN = '@deepseek-ai/dsh-system-prompt'
  99. /**
  100. * Closed two-message turns followed by one open turn for durable compaction events.
  101. * A `system` text puts a `system/message` at surface node 0 before turn 1's user message.
  102. */
  103. function conversation(turns = 4, text = 'fixture '.repeat(40).trim(), system?: string): Session {
  104. const session = Session.create(SessionId(`conversation-${turns}`))
  105. for (let turn = 1; turn <= turns; turn += 1) {
  106. session.append('turn/start', { turn })
  107. if (turn === 1 && system !== undefined) {
  108. session.append('system/message', {
  109. turn,
  110. step: 1,
  111. message: createSystemMessage(system, SYSTEM_PROMPT_PLUGIN),
  112. }, { surfaceOp: 'append' })
  113. }
  114. session.append('user/message', createUserMessage({
  115. content: [{ type: 'text', text: `${text} user ${turn}` }],
  116. source: { kind: 'user' },
  117. }), { surfaceOp: 'append' })
  118. session.append('step/start', { turn, step: 1 })
  119. if (turn === 1) {
  120. session.append('request/header', {
  121. header: { config: { provider: MODEL, model: MODEL } },
  122. reason: 'initial',
  123. })
  124. }
  125. session.append('assistant/message', {
  126. stream: [],
  127. turn,
  128. step: 1,
  129. message: createMessage({
  130. role: 'assistant',
  131. content: [{ type: 'text', text: `${text} assistant ${turn}` }],
  132. source: {
  133. kind: 'model',
  134. ...{ provider: MODEL, model: MODEL },
  135. },
  136. }),
  137. }, { surfaceOp: 'append' })
  138. session.append('step/end', { turn, step: 1 })
  139. session.append('turn/end', { turn, reason: { kind: 'completed' } })
  140. }
  141. session.append('turn/start', {
  142. turn: turns + 1,
  143. })
  144. return session
  145. }
  146. function toolConversation(): Session {
  147. const session = Session.create(SessionId('tools'))
  148. for (let turn = 1; turn <= 3; turn += 1) {
  149. const callId = ToolCallId(`call-${turn}`)
  150. session.append('turn/start', { turn })
  151. session.append('user/message', createUserMessage({
  152. content: [{ type: 'text', text: `request ${turn} `.repeat(300) }],
  153. source: { kind: 'user' },
  154. }), { surfaceOp: 'append' })
  155. session.append('step/start', { turn, step: 1 })
  156. if (turn === 1) {
  157. session.append('request/header', {
  158. header: { config: { provider: MODEL, model: MODEL } },
  159. reason: 'initial',
  160. })
  161. }
  162. session.append('assistant/message', {
  163. stream: [],
  164. turn,
  165. step: 1,
  166. message: createMessage({
  167. role: 'assistant',
  168. content: [
  169. { type: 'text', text: `calling ${turn} `.repeat(300) },
  170. { type: 'tool-call', id: callId, name: 'read', arguments: '{}' },
  171. ],
  172. source: {
  173. kind: 'model',
  174. ...{ provider: MODEL, model: MODEL },
  175. },
  176. }),
  177. }, { surfaceOp: 'append' })
  178. session.append('tool/call', { turn, step: 1, callId, name: 'read', arguments: '{}' })
  179. session.append('tool/result', {
  180. turn,
  181. step: 1,
  182. message: createToolResultMessage({
  183. callId,
  184. content: [{ type: 'text', text: `result ${turn} `.repeat(300) }],
  185. isError: false,
  186. }),
  187. }, { surfaceOp: 'append' })
  188. session.append('step/end', { turn, step: 1 })
  189. session.append('turn/end', { turn, reason: { kind: 'completed' } })
  190. }
  191. session.append('turn/start', { turn: 4 })
  192. return session
  193. }
  194. /** One closed routed tool step followed by an open turn for rewrite events. */
  195. function oversizedToolResult(chars = 3_000, withCompactablePrompt = false): Session {
  196. const session = Session.create(SessionId(`oversized-tool-${chars}`))
  197. const callId = ToolCallId('oversized')
  198. session.append('turn/start', { turn: 1 })
  199. if (withCompactablePrompt) {
  200. session.append('user/message', createUserMessage({
  201. content: [{ type: 'text', text: 'older history '.repeat(200) }],
  202. source: { kind: 'user' },
  203. }), { surfaceOp: 'append' })
  204. }
  205. session.append('step/start', { turn: 1, step: 1 })
  206. session.append('request/header', {
  207. header: { config: { provider: MODEL, model: MODEL } },
  208. reason: 'initial',
  209. })
  210. session.append('assistant/message', {
  211. stream: [],
  212. turn: 1,
  213. step: 1,
  214. message: createMessage({
  215. role: 'assistant',
  216. content: [{ type: 'tool-call', id: callId, name: 'bash', arguments: '{}' }],
  217. source: {
  218. kind: 'model',
  219. ...{ provider: MODEL, model: MODEL },
  220. },
  221. }),
  222. }, { surfaceOp: 'append' })
  223. session.append('tool/call', { turn: 1, step: 1, callId, name: 'bash', arguments: '{}' })
  224. session.append('tool/result', {
  225. turn: 1,
  226. step: 1,
  227. message: createToolResultMessage({
  228. callId,
  229. content: [{ type: 'text', text: 'X'.repeat(chars) }],
  230. isError: false,
  231. }),
  232. meta: { presentation: 'preserved' },
  233. }, { surfaceOp: 'append' })
  234. session.append('step/end', { turn: 1, step: 1 })
  235. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  236. session.append('turn/start', { turn: 2 })
  237. return session
  238. }
  239. class TestCompactionEngine extends BasicCompactionEngine {
  240. summary: ContentBlock[] = [{ type: 'text', text: 'small checkpoint' }]
  241. rawOutput: ContentBlock[] | undefined
  242. usage: TokenUsage | undefined
  243. summaryProvider = 'summary-provider'
  244. summaryModel = 'summary-model'
  245. error: unknown
  246. mutateDuringSummary: (() => void) | undefined
  247. calls: Array<{ input: SummarizationInput; signal: AbortSignal | undefined }> = []
  248. override async summarize(
  249. input: SummarizationInput,
  250. _agent: Agent,
  251. signal?: AbortSignal,
  252. ): Promise<{
  253. summary: ContentBlock[]
  254. rawOutput?: ContentBlock[]
  255. provider: string
  256. model: string
  257. maxTokens?: number
  258. usage?: TokenUsage
  259. }> {
  260. this.calls.push({ input, signal })
  261. this.mutateDuringSummary?.()
  262. if (this.error !== undefined) throw this.error
  263. return {
  264. summary: this.summary,
  265. ...this.rawOutput === undefined ? {} : { rawOutput: this.rawOutput },
  266. provider: this.summaryProvider,
  267. model: this.summaryModel,
  268. maxTokens: 123,
  269. ...this.usage === undefined ? {} : { usage: this.usage },
  270. }
  271. }
  272. }
  273. function service(
  274. config: BasicCompactionConfig = { auto: false },
  275. ctx = createContext(),
  276. ): TestCompactionEngine {
  277. return new TestCompactionEngine(ctx, config)
  278. }
  279. async function compactIfNeeded(
  280. compact: BasicCompactionEngine,
  281. session: Session,
  282. trigger: 'pressure' | 'context-overflow' = 'pressure',
  283. model: string | undefined = MODEL,
  284. ): Promise<CompactionResult | null> {
  285. return compact.compactIfNeeded(agent(session, model), trigger, SIGNAL)
  286. }
  287. describe('compact configuration and defaults', () => {
  288. it('uses low-friction service-wide defaults', () => {
  289. const resolved = resolveConfig({})
  290. expect(resolved).toEqual({
  291. thresholdRatio: 0.8,
  292. retainRatio: 0.16,
  293. summarizationProvider: '',
  294. summarizationModel: '',
  295. maxTokens: 8192,
  296. compactionRetries: 1,
  297. maxOverflowRetries: 1,
  298. modelPolicies: [],
  299. auto: true,
  300. })
  301. expect(Object.isFrozen(resolved)).toBe(true)
  302. })
  303. it('resolves threshold and retention overrides independently', () => {
  304. const thresholdOnly = resolveConfig({
  305. thresholdRatio: 0.5,
  306. })
  307. expect(thresholdOnly).toMatchObject({
  308. thresholdRatio: 0.5,
  309. retainRatio: 0.16,
  310. })
  311. const retentionOnly = resolveConfig({
  312. retainTokens: 70,
  313. })
  314. expect(retentionOnly).toMatchObject({
  315. thresholdRatio: 0.8,
  316. retainTokens: 70,
  317. })
  318. expect(retentionOnly).not.toHaveProperty('retainRatio')
  319. })
  320. it('merges exact provider/model policy overrides and scales ratios per model', () => {
  321. const config = resolveConfig({
  322. thresholdRatio: 0.8,
  323. retainRatio: 0.1,
  324. modelPolicies: [{
  325. provider: 'small-provider',
  326. model: 'shared-id',
  327. thresholdRatio: 0.5,
  328. retainTokens: 120,
  329. }],
  330. })
  331. const small = resolveTargetPolicy(config, {
  332. provider: 'small-provider',
  333. model: 'shared-id',
  334. })
  335. const otherProvider = resolveTargetPolicy(config, {
  336. provider: 'large-provider',
  337. model: 'shared-id',
  338. })
  339. expect(resolveCompactSpec(small, 1_000)).toMatchObject({
  340. thresholdTokens: 500,
  341. retainTokens: 120,
  342. })
  343. expect(resolveCompactSpec(otherProvider, 2_000)).toMatchObject({
  344. thresholdTokens: 1_600,
  345. retainTokens: 200,
  346. })
  347. const ratioOverride = resolveTargetPolicy(resolveConfig({
  348. retainTokens: 200,
  349. modelPolicies: [{
  350. provider: 'ratio-provider',
  351. model: 'ratio-model',
  352. thresholdRatio: 0.6,
  353. retainRatio: 0.2,
  354. summarizationProvider: 'summary-provider',
  355. summarizationModel: 'summary-model',
  356. maxTokens: 512,
  357. compactionRetries: 2,
  358. maxOverflowRetries: 3,
  359. }],
  360. }), { provider: 'ratio-provider', model: 'ratio-model' })
  361. expect(resolveCompactSpec(ratioOverride, 2_000)).toMatchObject({
  362. thresholdTokens: 1_200,
  363. retainTokens: 400,
  364. summarizationProvider: 'summary-provider',
  365. summarizationModel: 'summary-model',
  366. maxTokens: 512,
  367. compactionRetries: 2,
  368. maxOverflowRetries: 3,
  369. })
  370. })
  371. it('inherits, clears, and replaces the summarization target as a pair', () => {
  372. const config = resolveConfig({
  373. summarizationProvider: 'default-provider',
  374. summarizationModel: 'default-model',
  375. modelPolicies: [
  376. { provider: 'inherit-provider', model: MODEL },
  377. {
  378. provider: 'clear-provider',
  379. model: MODEL,
  380. summarizationProvider: '',
  381. summarizationModel: '',
  382. },
  383. {
  384. provider: 'replace-provider',
  385. model: MODEL,
  386. summarizationProvider: 'replacement-provider',
  387. summarizationModel: 'replacement-model',
  388. },
  389. ],
  390. })
  391. expect(resolveTargetPolicy(config, { provider: 'inherit-provider', model: MODEL }))
  392. .toMatchObject({
  393. summarizationProvider: 'default-provider',
  394. summarizationModel: 'default-model',
  395. })
  396. expect(resolveTargetPolicy(config, { provider: 'clear-provider', model: MODEL }))
  397. .toMatchObject({ summarizationProvider: '', summarizationModel: '' })
  398. expect(resolveTargetPolicy(config, { provider: 'replace-provider', model: MODEL }))
  399. .toMatchObject({
  400. summarizationProvider: 'replacement-provider',
  401. summarizationModel: 'replacement-model',
  402. })
  403. })
  404. it('validates common values and pressure-policy invariants', () => {
  405. const bad = [
  406. [{ maxTokens: 0 }, /maxTokens/],
  407. [{ compactionRetries: -1 }, /compactionRetries/],
  408. [{ maxOverflowRetries: -1 }, /maxOverflowRetries/],
  409. [{ auto: 'yes' }, /auto must be a boolean/],
  410. [{ summarizationProvider: 1 }, /summarizationProvider must be a string/],
  411. [{ summarizationModel: 1 }, /summarizationModel must be a string/],
  412. [{ summarizationProvider: MODEL }, /must be set together/],
  413. [{ summarizationModel: MODEL }, /must be set together/],
  414. [{ summarizationProvider: '' }, /must be set together/],
  415. [{ summarizationModel: '' }, /must be set together/],
  416. [{ thresholdRatio: 0 }, /number in \(0, 1\]/],
  417. [{ thresholdRatio: 1.1 }, /number in \(0, 1\]/],
  418. [{ retainRatio: 0.9 }, /retainRatio \(0.9\) must be less than the resolved thresholdRatio \(0.8\)/],
  419. [{ thresholdRatio: 0.1 }, /retainRatio \(0.16\) must be less than the resolved thresholdRatio \(0.1\)/],
  420. [{ retainTokens: -1 }, /non-negative integer/],
  421. [{ retainRatio: 0.2, retainTokens: 100 }, /mutually exclusive/],
  422. [{ modelPolicies: {} }, /modelPolicies must be an array/],
  423. [{ modelPolicies: [1] }, /modelPolicies\[0\] must be an object/],
  424. [{ modelPolicies: [null] }, /modelPolicies\[0\] must be an object/],
  425. [{ modelPolicies: [[]] }, /modelPolicies\[0\] must be an object/],
  426. [{ modelPolicies: [{ provider: 1, model: MODEL }] }, /provider must be a non-empty string/],
  427. [{ modelPolicies: [{ provider: '', model: MODEL }] }, /provider must be a non-empty string/],
  428. [{ modelPolicies: [{ provider: MODEL, model: 1 }] }, /model must be a non-empty string/],
  429. [{ modelPolicies: [{ provider: MODEL, model: '' }] }, /model must be a non-empty string/],
  430. [{ modelPolicies: [{ provider: MODEL, model: MODEL, summarizationProvider: 1 }] }, /summarizationProvider must be a string/],
  431. [{
  432. summarizationProvider: 'default-provider',
  433. summarizationModel: 'default-model',
  434. modelPolicies: [{ provider: MODEL, model: MODEL, summarizationModel: '' }],
  435. }, /modelPolicies\[0\].*must be set together/],
  436. [{
  437. summarizationProvider: 'default-provider',
  438. summarizationModel: 'default-model',
  439. modelPolicies: [{ provider: MODEL, model: MODEL, summarizationProvider: '' }],
  440. }, /modelPolicies\[0\].*must be set together/],
  441. [{ modelPolicies: [{ provider: MODEL, model: MODEL, retainRatio: 0.2, retainTokens: 100 }] }, /mutually exclusive/],
  442. [
  443. { modelPolicies: [{ provider: MODEL, model: MODEL, thresholdRatio: 0.1 }] },
  444. /modelPolicies\[0\]: retainRatio \(0.16\).*thresholdRatio \(0.1\)/,
  445. ],
  446. [
  447. { modelPolicies: [{ provider: MODEL, model: MODEL, retainRatio: 0.9 }] },
  448. /modelPolicies\[0\]: retainRatio \(0.9\).*thresholdRatio \(0.8\)/,
  449. ],
  450. [{ modelPolicies: [{ provider: MODEL, model: MODEL }, { provider: MODEL, model: MODEL }] }, /duplicate model policy/],
  451. [{ models: { [MODEL]: { retainTokens: 10 } } }, /BasicCompactionConfig: unknown key "models"/],
  452. [{ thresholdRato: 0.5 }, /BasicCompactionConfig: unknown key "thresholdRato"/],
  453. ] as Array<[unknown, RegExp]>
  454. for (const [config, pattern] of bad) {
  455. expect(() => resolveConfig(config as BasicCompactionConfig)).toThrow(pattern)
  456. }
  457. const invalidPressure = resolveTargetPolicy(resolveConfig({
  458. thresholdRatio: 0.5,
  459. retainTokens: 500,
  460. }), { provider: MODEL, model: MODEL })
  461. expect(() => resolveCompactSpec(invalidPressure, 1_000)).toThrow(/less than threshold/)
  462. expect(() => resolveCompactSpec(invalidPressure, 1.5)).toThrow(/positive integer/)
  463. expect(() => resolveCompactSpec(invalidPressure, 0)).toThrow(/positive integer/)
  464. })
  465. })
  466. describe('pressure measurement and retention', () => {
  467. const compactConfig: BasicCompactionConfig = {
  468. auto: false,
  469. thresholdRatio: 0.5,
  470. retainTokens: 180,
  471. }
  472. it('skips when no durable routed model exists instead of using AgentOptions fallback', async () => {
  473. const compact = service(compactConfig)
  474. const session = Session.create(SessionId('headerless'))
  475. session.append('turn/start', { turn: 1 })
  476. await expect(compact.compactIfNeeded(agent(session, MODEL), 'pressure', SIGNAL))
  477. .resolves.toBeNull()
  478. expect(compact.calls).toHaveLength(0)
  479. })
  480. it('meters an unlisted model when its provider adapter supplies context metadata', async () => {
  481. const compact = service(compactConfig)
  482. const session = conversation()
  483. session.append('request/header', {
  484. header: { config: { provider: 'unlisted-provider', model: 'unlisted-model' } },
  485. reason: 'resume',
  486. })
  487. await expect(compactIfNeeded(compact, session))
  488. .resolves.not.toBeNull()
  489. })
  490. it('forwards turn cancellation to proactive model metadata resolution', async () => {
  491. const ctx = createContext()
  492. const resolveModelInfo = vi.spyOn(ctx.llm, 'resolveModelInfo')
  493. const compact = service(compactConfig, ctx)
  494. const session = conversation()
  495. const signal = new AbortController().signal
  496. await expect(compact.compactIfNeeded(agent(session, MODEL), 'pressure', signal))
  497. .resolves.not.toBeNull()
  498. expect(resolveModelInfo).toHaveBeenCalledWith(MODEL, MODEL, signal)
  499. })
  500. it('re-resolves capacity after a same-model-id provider switch in one session', async () => {
  501. const ctx = new Context()
  502. void new LlmRuntime(ctx)
  503. new SessionProjectionRegistry(ctx)
  504. void new TokenMeter(ctx)
  505. ctx.llm.registerAdapter(['large', 'small'], new RoutedContextAdapter({
  506. large: 10_000,
  507. small: 1_000,
  508. }))
  509. const compact = service({
  510. auto: false,
  511. thresholdRatio: 0.5,
  512. retainRatio: 0.1,
  513. }, ctx)
  514. const session = conversation(4)
  515. session.append('request/header', {
  516. header: { config: { provider: 'large', model: 'shared-id' } },
  517. reason: 'resume',
  518. })
  519. await expect(compactIfNeeded(compact, session)).resolves.toBeNull()
  520. session.append('request/header', {
  521. header: { config: { provider: 'small', model: 'shared-id' } },
  522. reason: 'change',
  523. })
  524. await expect(compactIfNeeded(compact, session)).resolves.not.toBeNull()
  525. })
  526. it('requires capacity only for proactive pressure, not provider-confirmed overflow', async () => {
  527. const ctx = new Context()
  528. void new LlmRuntime(ctx)
  529. new SessionProjectionRegistry(ctx)
  530. void new TokenMeter(ctx)
  531. ctx.llm.registerAdapter(['unknown-context'], new ContextAdapter(1_000))
  532. vi.spyOn(ctx.llm, 'resolveModelInfo').mockImplementation((provider, model) => Promise.resolve({
  533. provider,
  534. id: model,
  535. name: model,
  536. }))
  537. const compact = service(compactConfig, ctx)
  538. const session = conversation(4)
  539. session.append('request/header', {
  540. header: { config: { provider: 'unknown-context', model: 'model' } },
  541. reason: 'resume',
  542. })
  543. await expect(compactIfNeeded(compact, session, 'pressure'))
  544. .rejects.toThrow(/no context capacity for unknown-context\/model/)
  545. await expect(compactIfNeeded(compact, session, 'context-overflow'))
  546. .resolves.not.toBeNull()
  547. })
  548. it('declines forced overflow when the whole surface is one indivisible tool pair', async () => {
  549. const compact = service(compactConfig)
  550. const session = Session.create(SessionId('single-tool-pair'))
  551. const callId = ToolCallId('single-call')
  552. session.append('turn/start', { turn: 1 })
  553. session.append('step/start', { turn: 1, step: 1 })
  554. session.append('request/header', {
  555. header: { config: { provider: MODEL, model: MODEL } },
  556. reason: 'initial',
  557. })
  558. session.append('assistant/message', {
  559. stream: [],
  560. turn: 1,
  561. step: 1,
  562. message: createMessage({
  563. role: 'assistant',
  564. content: [{ type: 'tool-call', id: callId, name: 'read', arguments: '{}' }],
  565. source: {
  566. kind: 'model',
  567. ...{ provider: MODEL, model: MODEL },
  568. },
  569. }),
  570. }, { surfaceOp: 'append' })
  571. session.append('tool/call', { turn: 1, step: 1, callId, name: 'read', arguments: '{}' })
  572. session.append('tool/result', {
  573. turn: 1,
  574. step: 1,
  575. message: createToolResultMessage({
  576. callId,
  577. content: [{ type: 'text', text: 'result' }],
  578. isError: false,
  579. }),
  580. }, { surfaceOp: 'append' })
  581. session.append('step/end', { turn: 1, step: 1 })
  582. const generation = session.surface.replaceGeneration
  583. await expect(compactIfNeeded(compact, session, 'context-overflow')).resolves.toBeNull()
  584. expect(session.surface.replaceGeneration).toBe(generation)
  585. expect(session.snapshotEvents().some(event => event.type === 'compaction/start')).toBe(false)
  586. })
  587. it('does nothing below threshold and compacts a priced head above threshold', async () => {
  588. const compact = service(compactConfig)
  589. expect(await compactIfNeeded(compact, conversation(2))).toBeNull()
  590. const session = conversation(4)
  591. const head = session.surface.nodes[0]!
  592. const result = await compactIfNeeded(compact, session)
  593. expect(result).not.toBeNull()
  594. expect(result?.shadowedSeqs[0]).toBe(head)
  595. expect(result?.shadowedSeqs.length).toBeGreaterThan(2)
  596. expect(session.surface.nodes.length).toBeLessThan(8)
  597. })
  598. it('starts the range after a system head and keeps that node at surface position 0', async () => {
  599. const ctx = createContext()
  600. const compact = service(compactConfig, ctx)
  601. const session = conversation(4, undefined, 'SYSTEM HEAD')
  602. const [head, firstUser] = session.surface.nodes
  603. expect(session.eventAt(head!)?.type).toBe('system/message')
  604. const range = selectCompactableRange(session, ctx.tokenMeter.measure(session), compactConfig.retainTokens!)
  605. expect(range?.start).toBe(firstUser)
  606. const result = await compactIfNeeded(compact, session)
  607. expect(result).not.toBeNull()
  608. expect(result?.shadowedSeqs).not.toContain(head)
  609. expect(result?.shadowedSeqs[0]).toBe(firstUser)
  610. expect(session.surface.nodes[0]).toBe(head)
  611. expect(session.deriveMessages()[0]).toMatchObject({
  612. role: 'system',
  613. content: [{ type: 'text', text: 'SYSTEM HEAD' }],
  614. })
  615. expect(compact.calls[0]!.input.messages[0]).toEqual(session.deriveMessages()[0])
  616. expect(compact.calls[0]!.input.messages.filter(message => message.role === 'system')).toHaveLength(1)
  617. })
  618. it('declines when only the system head precedes the retained tail', () => {
  619. const ctx = createContext()
  620. const session = conversation(1, undefined, 'SYSTEM HEAD')
  621. const priced = ctx.tokenMeter.measure(session)
  622. const [, user, assistant] = priced.nodes
  623. expect(priced.nodes).toHaveLength(3)
  624. expect(selectCompactableRange(session, priced, user!.tokens + assistant!.tokens)).toBeNull()
  625. })
  626. it('counts the durable routed request envelope without putting it on the surface', async () => {
  627. const compact = service({
  628. auto: false,
  629. thresholdRatio: 0.9,
  630. retainTokens: 50,
  631. })
  632. const session = conversation(2, 'x'.repeat(600))
  633. expect(await compactIfNeeded(compact, session)).toBeNull()
  634. session.append('request/header', {
  635. header: {
  636. config: { provider: MODEL, model: MODEL },
  637. tools: [{ name: 'bulk', description: 's'.repeat(2_000), parameters: { type: 'object' } }],
  638. },
  639. reason: 'resume',
  640. })
  641. const result = await compactIfNeeded(compact, session)
  642. expect(result).not.toBeNull()
  643. })
  644. it('uses the latest logged request envelope without an AgentOptions override', async () => {
  645. const ctx = createContext()
  646. const compact = service({
  647. auto: false,
  648. thresholdRatio: 0.5,
  649. retainTokens: 180,
  650. }, ctx)
  651. const session = conversation(4)
  652. session.append('request/header', {
  653. header: { config: { provider: 'actual', model: 'actual' } },
  654. reason: 'initial',
  655. })
  656. const measure = vi.spyOn(ctx.tokenMeter, 'measure')
  657. const result = await compactIfNeeded(compact, session, 'pressure', 'fallback')
  658. expect(result).not.toBeNull()
  659. expect(session.requestHeader()?.config.model).toBe('actual')
  660. expect(measure.mock.calls[0]).toEqual([session])
  661. })
  662. it('declines when envelope pressure is high but the surface has no compactable range', async () => {
  663. const compact = service(compactConfig)
  664. const bulkTools = [{ name: 'bulk', description: 'x'.repeat(100_000), parameters: { type: 'object' } }]
  665. const empty = Session.create(SessionId('empty'))
  666. empty.append('turn/start', { turn: 1 })
  667. empty.append('request/header', {
  668. header: { config: { provider: MODEL, model: MODEL }, tools: bulkTools },
  669. reason: 'initial',
  670. })
  671. expect(await compactIfNeeded(compact, empty)).toBeNull()
  672. const retained = conversation(1)
  673. retained.append('request/header', {
  674. header: { config: { provider: MODEL, model: MODEL }, tools: bulkTools },
  675. reason: 'resume',
  676. })
  677. expect(await compactIfNeeded(compact, retained)).toBeNull()
  678. })
  679. it('uses one unified measurement for each pressure-and-retention decision', async () => {
  680. const ctx = createContext()
  681. const compact = service(compactConfig, ctx)
  682. const measure = vi.spyOn(ctx.tokenMeter, 'measure')
  683. const stop = new Error('stop after first decision')
  684. vi.spyOn(compact, 'compactRegion').mockRejectedValueOnce(stop)
  685. await expect(compactIfNeeded(compact, conversation(4))).rejects.toBe(stop)
  686. expect(measure).toHaveBeenCalledTimes(1)
  687. })
  688. it('bounds retries when a shrinking checkpoint remains above threshold', async () => {
  689. const compact = service({
  690. auto: false,
  691. compactionRetries: 0,
  692. thresholdRatio: 0.3,
  693. retainTokens: 180,
  694. })
  695. compact.summary = Array.from({ length: 7 }, (_, index) => ({
  696. type: 'text',
  697. text: `summary ${index}`,
  698. }))
  699. await expect(compactIfNeeded(compact, conversation(4)))
  700. .rejects.toThrow(/still above threshold after 1 compaction attempts/)
  701. })
  702. it('rounds a retention cut head-ward to preserve tool-call/result pairing', async () => {
  703. const compact = service({
  704. auto: false,
  705. thresholdRatio: 0.8,
  706. retainTokens: 80,
  707. }, createContext(4_000))
  708. const session = toolConversation()
  709. const result = await compactIfNeeded(compact, session)
  710. expect(result).not.toBeNull()
  711. const messages = session.deriveMessages()
  712. const calls = new Set<string>()
  713. for (const message of messages) {
  714. for (const block of message.content) {
  715. if (block.type === 'tool-call') calls.add(block.id)
  716. if (block.type === 'tool-result') expect(calls.has(block.toolCallId)).toBe(true)
  717. }
  718. }
  719. })
  720. it('rejects a priced surface that is not the current positional surface', () => {
  721. const ctx = createContext()
  722. const session = conversation(2)
  723. const priced = ctx.tokenMeter.measure(session)
  724. expect(() => selectCompactableRange(session, {
  725. ...priced,
  726. nodes: priced.nodes.slice(1),
  727. }, 1)).toThrow(/does not match/)
  728. })
  729. it('declines when rounding a cut would consume the only tool pair', () => {
  730. const ctx = createContext()
  731. const session = Session.create(SessionId('one-tool-pair'))
  732. const callId = ToolCallId('only')
  733. session.append('turn/start', { turn: 1 })
  734. session.append('step/start', { turn: 1, step: 1 })
  735. session.append('assistant/message', {
  736. stream: [],
  737. turn: 1,
  738. step: 1,
  739. message: createMessage({
  740. role: 'assistant',
  741. content: [{ type: 'tool-call', id: callId, name: 'read', arguments: '{}' }],
  742. source: {
  743. kind: 'model',
  744. ...{ provider: MODEL, model: MODEL },
  745. },
  746. }),
  747. }, { surfaceOp: 'append' })
  748. session.append('tool/call', { turn: 1, step: 1, callId, name: 'read', arguments: '{}' })
  749. session.append('tool/result', {
  750. turn: 1,
  751. step: 1,
  752. message: createToolResultMessage({
  753. callId,
  754. content: [{ type: 'text', text: 'result' }],
  755. isError: false,
  756. }),
  757. }, { surfaceOp: 'append' })
  758. session.append('step/end', { turn: 1, step: 1 })
  759. const priced = ctx.tokenMeter.measure(session)
  760. expect(selectCompactableRange(session, priced, 1)).toBeNull()
  761. })
  762. })
  763. describe('optional model-free tool-result pruning', () => {
  764. const pruneConfig = { thresholdChars: 100, headChars: 20, tailChars: 10 }
  765. it('does not prune a below-pressure session opportunistically', async () => {
  766. const ctx = createContext(10_000)
  767. const prune = new ToolResultPruner(ctx, pruneConfig)
  768. const compact = new TestCompactionEngine(ctx, {
  769. auto: false,
  770. thresholdRatio: 0.8,
  771. retainTokens: 100,
  772. })
  773. const session = oversizedToolResult()
  774. const pruneSession = vi.spyOn(prune, 'pruneSession')
  775. expect(await compactIfNeeded(compact, session)).toBeNull()
  776. expect(pruneSession).not.toHaveBeenCalled()
  777. expect(compact.calls).toHaveLength(0)
  778. expect(session.surface.replaceGeneration).toBe(0)
  779. })
  780. it('skips LLM summarization when pruning alone clears pressure', async () => {
  781. const ctx = createContext(1_000)
  782. void new ToolResultPruner(ctx, pruneConfig)
  783. const compact = new TestCompactionEngine(ctx, {
  784. auto: false,
  785. thresholdRatio: 0.5,
  786. retainTokens: 50,
  787. })
  788. const session = oversizedToolResult()
  789. expect(ctx.tokenMeter.measure(session).totalTokens).toBeGreaterThanOrEqual(500)
  790. expect(await compactIfNeeded(compact, session)).toBeNull()
  791. expect(ctx.tokenMeter.measure(session).totalTokens).toBeLessThan(500)
  792. expect(compact.calls).toHaveLength(0)
  793. expect(session.surface.replaceGeneration).toBe(1)
  794. })
  795. it('summarizes the pruned surface when pruning is insufficient', async () => {
  796. const ctx = createContext(2_000)
  797. void new ToolResultPruner(ctx, pruneConfig)
  798. const compact = new TestCompactionEngine(ctx, {
  799. auto: false,
  800. thresholdRatio: 0.5,
  801. retainTokens: 50,
  802. })
  803. const session = toolConversation()
  804. expect(await compactIfNeeded(compact, session)).not.toBeNull()
  805. expect(compact.calls).toHaveLength(1)
  806. expect(summarizedText(compact.calls[0]!.input)).toContain('tool result middle pruned')
  807. expect(summarizedText(compact.calls[0]!.input)).not.toContain('result 1 '.repeat(300))
  808. })
  809. it('retains the original compaction-basic behavior without the optional plugin', async () => {
  810. const ctx = createContext(2_000)
  811. const compact = new TestCompactionEngine(ctx, {
  812. auto: false,
  813. thresholdRatio: 0.5,
  814. retainTokens: 50,
  815. })
  816. const session = oversizedToolResult(3_000, true)
  817. expect(await compactIfNeeded(compact, session)).not.toBeNull()
  818. expect(compact.calls).toHaveLength(1)
  819. const original = session.snapshotEvents().find(event => event.type === 'tool/result')
  820. expect(original?.type === 'tool/result' && original.data.message.content[0].content[0])
  821. .toEqual({ type: 'text', text: 'X'.repeat(3_000) })
  822. expect(session.snapshotEvents().filter(event =>
  823. event.type === 'tool/result' && event.surfaceOp !== 'append')).toHaveLength(0)
  824. })
  825. })
  826. describe('compaction region transaction', () => {
  827. it('lands a framed, replayable checkpoint with exact source seqs and token price', async () => {
  828. const compact = service()
  829. compact.rawOutput = [
  830. { type: 'reasoning', text: 'private compact thought' },
  831. ...compact.summary,
  832. ]
  833. compact.usage = { inputTokens: 40, outputTokens: 5 }
  834. const session = conversation(3)
  835. const before = [...session.surface.nodes]
  836. const result = await compact.compactRegion(
  837. before[0]!,
  838. before[3]!,
  839. agent(session, MODEL),
  840. SIGNAL,
  841. )
  842. expect(result.shadowedSeqs).toEqual(before.slice(0, 4))
  843. expect(result.shadowedTokenCount).toBeGreaterThan(0)
  844. expect(compact.calls[0]).toMatchObject({ signal: SIGNAL })
  845. expect(summarizedText(compact.calls[0]!.input)).toContain('fixture user 1')
  846. const summary = session.snapshotEvents().findLast(event => event.type === 'compaction/summary')
  847. expect(summary?.data).toMatchObject({
  848. shadowedSeqs: result.shadowedSeqs,
  849. shadowedTokenCount: result.shadowedTokenCount,
  850. provider: 'summary-provider',
  851. model: 'summary-model',
  852. maxTokens: 123,
  853. rawOutput: compact.rawOutput,
  854. usage: compact.usage,
  855. })
  856. expect(summary?.data).not.toHaveProperty('llmStreamCall')
  857. const head = session.deriveMessages()[0]!
  858. expect(head.content[0]?.type).toBe('text')
  859. expect(head.content[0]?.type === 'text' ? head.content[0].text : '').toContain('<compacted-summary>')
  860. expect(head.content.at(-1)).toEqual({ type: 'text', text: '</compacted-summary>' })
  861. const replay = Session.create(SessionId('replay'), session.snapshotEvents())
  862. expect(replay.deriveMessages()).toEqual(session.deriveMessages())
  863. })
  864. it('replays the system head and latest routed tools so the summarizer reuses the cache', async () => {
  865. const compact = service()
  866. const session = conversation(3, undefined, 'CONVERSATION SYSTEM')
  867. const tools = [{ name: 'do_thing', description: 'd', parameters: { type: 'object' } }]
  868. session.append('request/header', {
  869. header: { config: { provider: MODEL, model: MODEL }, tools },
  870. reason: 'resume',
  871. })
  872. const nodes = session.surface.nodes
  873. const prefix = session.deriveMessages().slice(0, 3)
  874. await compact.compactRegion(nodes[1]!, nodes[2]!, agent(session, MODEL), SIGNAL)
  875. const { input } = compact.calls[0]!
  876. expect(input).toEqual({ messages: prefix, tools })
  877. })
  878. it('omits the summarizer system prompt for an empty system head or a system-less surface', async () => {
  879. const compact = service()
  880. const emptyHead = conversation(3, undefined, '')
  881. const emptyNodes = emptyHead.surface.nodes
  882. expect(emptyHead.eventAt(emptyNodes[0]!)?.type).toBe('system/message')
  883. await compact.compactRegion(emptyNodes[1]!, emptyNodes[2]!, agent(emptyHead, MODEL), SIGNAL)
  884. expect(compact.calls[0]!.input.messages[0]).toMatchObject({ role: 'user' })
  885. expect(compact.calls[0]!.input.messages.some(message => message.role === 'system')).toBe(false)
  886. expect(emptyHead.surface.nodes[0]).toBe(emptyNodes[0])
  887. const headless = conversation(3)
  888. const nodes = headless.surface.nodes
  889. await compact.compactRegion(nodes[0]!, nodes[1]!, agent(headless, MODEL), SIGNAL)
  890. expect(compact.calls[1]!.input.messages.some(message => message.role === 'system')).toBe(false)
  891. expect(compact.calls[1]!.input.messages[0]).toMatchObject({ role: 'user' })
  892. })
  893. it('surfaces the session rejection of a range that covers the system head', async () => {
  894. const compact = service()
  895. const session = conversation(3, undefined, 'PROTECTED SYSTEM')
  896. const nodes = [...session.surface.nodes]
  897. await expect(compact.compactRegion(nodes[0]!, nodes[3]!, agent(session, MODEL), SIGNAL))
  898. .rejects.toThrow(/node 0 holds the system prompt/)
  899. expect(session.surface.nodes).toEqual(nodes)
  900. const end = session.snapshotEvents().findLast(event => event.type === 'compaction/end')
  901. expect(end?.type === 'compaction/end' && end.data.error).toBeDefined()
  902. })
  903. it.each([
  904. ['start missing', 9_001, undefined, /start seq 9001 not found/],
  905. ['end missing', undefined, 9_002, /end seq 9002 not found/],
  906. ])('rejects %s', async (_label, startOverride, endOverride, pattern) => {
  907. const compact = service()
  908. const session = conversation(2)
  909. const nodes = session.surface.nodes
  910. await expect(compact.compactRegion(
  911. startOverride === undefined ? nodes[0]! : SessionSeq(startOverride),
  912. endOverride === undefined ? nodes[1]! : SessionSeq(endOverride),
  913. agent(session, MODEL),
  914. )).rejects.toThrow(pattern)
  915. })
  916. it('rejects reversed and tool-unbalanced positional boundaries', async () => {
  917. const compact = service()
  918. const plain = conversation(2)
  919. const nodes = plain.surface.nodes
  920. await expect(compact.compactRegion(
  921. nodes[2]!,
  922. nodes[1]!,
  923. agent(plain, MODEL),
  924. )).rejects.toThrow(/is after end/)
  925. const tools = toolConversation()
  926. const toolNodes = tools.surface.nodes
  927. await expect(compact.compactRegion(
  928. toolNodes[2]!,
  929. toolNodes[4]!,
  930. agent(tools, MODEL),
  931. )).rejects.toThrow(/start seq .* not a balanced boundary/)
  932. await expect(compact.compactRegion(
  933. toolNodes[0]!,
  934. toolNodes[1]!,
  935. agent(tools, MODEL),
  936. )).rejects.toThrow(/end seq .* not a balanced boundary/)
  937. })
  938. it('requires an open turn and an idle compaction bracket', async () => {
  939. const compact = service()
  940. const closed = conversation(1)
  941. closed.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
  942. const nodes = closed.surface.nodes
  943. await expect(compact.compactRegion(
  944. nodes[0]!,
  945. nodes[1]!,
  946. agent(closed, MODEL),
  947. )).rejects.toThrow(/no open turn/)
  948. const locked = conversation(1)
  949. locked.append('compaction/start', {
  950. compactionId: CompactionId('locked-compaction'),
  951. turn: 2,
  952. })
  953. const lockedNodes = locked.surface.nodes
  954. await expect(compact.compactRegion(
  955. lockedNodes[0]!,
  956. lockedNodes[1]!,
  957. agent(locked, MODEL),
  958. )).rejects.toThrow(/already in progress/)
  959. })
  960. it('rejects a session with no turn boundary at all', async () => {
  961. const compact = service()
  962. const session = Session.create(SessionId('turnless'))
  963. session.append('user/message', createUserMessage({
  964. content: [{ type: 'text', text: 'orphan' }],
  965. source: { kind: 'user' },
  966. }), { surfaceOp: 'append' })
  967. const node = session.surface.nodes[0]!
  968. await expect(compact.compactRegion(
  969. node,
  970. node,
  971. agent(session, MODEL),
  972. )).rejects.toThrow(/no open turn/)
  973. })
  974. it('rejects a meter snapshot that changed before summarization began', async () => {
  975. const ctx = createContext()
  976. const meter = ctx.tokenMeter
  977. const original = meter.measure.bind(meter)
  978. vi.spyOn(meter, 'measure').mockImplementationOnce((session) => {
  979. const measurement = original(session)
  980. return { ...measurement, nodes: measurement.nodes.slice(1) }
  981. })
  982. const compact = service({ auto: false }, ctx)
  983. const session = conversation(2)
  984. const nodes = session.surface.nodes
  985. await expect(compact.compactRegion(
  986. nodes[0]!,
  987. nodes[2]!,
  988. agent(session, MODEL),
  989. )).rejects.toThrow(/selected surface changed/)
  990. })
  991. it('records summarizer failures without mutating the surface', async () => {
  992. const compact = service()
  993. compact.error = new Error('summary unavailable')
  994. const session = conversation(2)
  995. const before = session.surface.nodes
  996. await expect(compact.compactRegion(
  997. before[0]!,
  998. before[2]!,
  999. agent(session, MODEL),
  1000. )).rejects.toThrow('summary unavailable')
  1001. expect(session.surface.nodes).toEqual(before)
  1002. expect(session.snapshotEvents().findLast(event => event.type === 'compaction/end')?.data)
  1003. .toMatchObject({ error: 'summary unavailable' })
  1004. })
  1005. it('stringifies non-Error failures in the durable end bracket', async () => {
  1006. const compact = service()
  1007. compact.error = 'plain failure'
  1008. const session = conversation(2)
  1009. const nodes = session.surface.nodes
  1010. await expect(compact.compactRegion(
  1011. nodes[0]!,
  1012. nodes[2]!,
  1013. agent(session, MODEL),
  1014. )).rejects.toBe('plain failure')
  1015. expect(session.snapshotEvents().findLast(event => event.type === 'compaction/end')?.data)
  1016. .toMatchObject({ error: 'plain failure' })
  1017. })
  1018. it('tolerates concurrent log-only appends while the selected surface is stable', async () => {
  1019. const compact = service()
  1020. const session = conversation(2)
  1021. compact.mutateDuringSummary = () => {
  1022. session.append('request/header', {
  1023. header: { config: { provider: MODEL, model: MODEL } },
  1024. reason: 'change',
  1025. })
  1026. }
  1027. const nodes = session.surface.nodes
  1028. await expect(compact.compactRegion(
  1029. nodes[0]!,
  1030. nodes[2]!,
  1031. agent(session, MODEL),
  1032. )).resolves.toMatchObject({ shadowedSeqs: nodes.slice(0, 3) })
  1033. expect(session.snapshotEvents().some(event => event.type === 'compaction/summary')).toBe(true)
  1034. })
  1035. it('rejects concurrent surface appends before committing the replacement', async () => {
  1036. const compact = service()
  1037. const session = conversation(2)
  1038. compact.mutateDuringSummary = () => {
  1039. session.append('user/message', createUserMessage({
  1040. content: [{ type: 'text', text: 'concurrent surface mutation' }],
  1041. source: { kind: 'plugin', plugin: 'test' },
  1042. }), { surfaceOp: 'append' })
  1043. }
  1044. const nodes = session.surface.nodes
  1045. await expect(compact.compactRegion(
  1046. nodes[0]!,
  1047. nodes[2]!,
  1048. agent(session, MODEL),
  1049. )).rejects.toThrow(/session surface changed/)
  1050. expect(session.snapshotEvents().some(event => event.type === 'compaction/summary')).toBe(false)
  1051. })
  1052. it('rejects a non-shrinking framed summary under the conversation meter', async () => {
  1053. const compact = service()
  1054. compact.summary = Array.from({ length: 100 }, (_, index) => ({
  1055. type: 'text',
  1056. text: `verbose ${index}`,
  1057. }))
  1058. const session = conversation(2)
  1059. const nodes = session.surface.nodes
  1060. await expect(compact.compactRegion(
  1061. nodes[0]!,
  1062. nodes[2]!,
  1063. agent(session, MODEL),
  1064. )).rejects.toThrow(/summary is not smaller/)
  1065. expect(session.snapshotEvents().some(event => event.type === 'compaction/summary')).toBe(false)
  1066. })
  1067. it('lets a model-independent custom summarizer compact without a conversation model', async () => {
  1068. const compact = service()
  1069. const session = Session.create(SessionId('model-less-region'))
  1070. session.append('turn/start', { turn: 1 })
  1071. session.append('user/message', createUserMessage({
  1072. content: [{ type: 'text', text: 'history '.repeat(100) }],
  1073. source: { kind: 'user' },
  1074. }), { surfaceOp: 'append' })
  1075. session.append('step/start', { turn: 1, step: 1 })
  1076. session.append('assistant/message', {
  1077. stream: [],
  1078. turn: 1,
  1079. step: 1,
  1080. message: createMessage({
  1081. role: 'assistant',
  1082. content: [{ type: 'text', text: 'answer '.repeat(100) }],
  1083. source: {
  1084. kind: 'model',
  1085. ...{ provider: 'historical', model: 'historical' },
  1086. },
  1087. }),
  1088. }, { surfaceOp: 'append' })
  1089. session.append('step/end', { turn: 1, step: 1 })
  1090. const nodes = session.surface.nodes
  1091. await expect(compact.compactRegion(
  1092. nodes[0]!,
  1093. nodes[1]!,
  1094. agent(session),
  1095. )).resolves.toMatchObject({ shadowedSeqs: [nodes[0]!, nodes[1]!] })
  1096. })
  1097. })
  1098. class ScriptedAdapter extends LlmAdapter {
  1099. lastOptions: GenerateOptions | undefined
  1100. usage: TokenUsage | undefined
  1101. constructor(
  1102. private readonly blocks: readonly ContentBlock[],
  1103. private readonly finish: (StreamChunk & { type: 'finish' })['reason'] = { kind: 'stop' },
  1104. ) {
  1105. super()
  1106. }
  1107. override async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
  1108. this.lastOptions = options
  1109. for (const [index, block] of this.blocks.entries()) {
  1110. yield { type: 'block-start', index, blockType: block.type }
  1111. if (block.type === 'text') {
  1112. yield { type: 'text-delta', index, text: block.text }
  1113. } else if (block.type === 'reasoning') {
  1114. yield { type: 'reasoning-delta', index, text: block.text }
  1115. } else {
  1116. yield { type: 'block-end', index, block }
  1117. }
  1118. }
  1119. if (this.usage !== undefined) yield { type: 'usage', usage: this.usage }
  1120. yield { type: 'finish', reason: this.finish }
  1121. }
  1122. }
  1123. class ExposedCompactionEngine extends BasicCompactionEngine {
  1124. runSummarize(
  1125. input: SummarizationInput,
  1126. owner: Agent,
  1127. signal?: AbortSignal,
  1128. ): Promise<{
  1129. summary: ContentBlock[]
  1130. rawOutput?: ContentBlock[]
  1131. provider: string
  1132. model: string
  1133. maxTokens?: number
  1134. usage?: TokenUsage
  1135. }> {
  1136. return this.summarize(input, owner, signal)
  1137. }
  1138. }
  1139. async function summarizerHarness(
  1140. blocks: readonly ContentBlock[],
  1141. finish?: (StreamChunk & { type: 'finish' })['reason'],
  1142. model = MODEL,
  1143. config: BasicCompactionConfig = { auto: false },
  1144. ): Promise<{ ctx: Context; adapter: ScriptedAdapter; compact: ExposedCompactionEngine }> {
  1145. const ctx = new Context()
  1146. await ctx.plugin(LlmRuntime)
  1147. await ctx.plugin(SessionProjectionRegistry)
  1148. void new TokenMeter(ctx)
  1149. const adapter = new ScriptedAdapter(blocks, finish)
  1150. ctx.llm.registerAdapter([model], adapter)
  1151. const compact = new ExposedCompactionEngine(ctx, config)
  1152. return { ctx, adapter, compact }
  1153. }
  1154. describe('default one-shot summarizer', () => {
  1155. it.each([undefined, '', 'SYSTEM HEAD\n精确前缀\n'])('preserves the routed prefix through region summarization with system %j', async (system) => {
  1156. const { adapter, compact } = await summarizerHarness([{ type: 'text', text: 'summary' }])
  1157. const session = conversation(3, undefined, system)
  1158. const tools = [{ name: 'do_thing', description: 'd', parameters: { type: 'object' } }]
  1159. session.append('request/header', {
  1160. header: { config: { provider: MODEL, model: MODEL }, tools },
  1161. reason: 'resume',
  1162. })
  1163. const nodes = [...session.surface.nodes]
  1164. const start = system === undefined ? 0 : 1
  1165. const prefix = session.deriveMessages().slice(0, system ? 3 : 2)
  1166. const result = await compact.compactRegion(nodes[start]!, nodes[start + 1]!, agent(session, MODEL), SIGNAL)
  1167. expect(adapter.lastOptions).not.toHaveProperty('system')
  1168. expect(adapter.lastOptions?.tools).toEqual(tools)
  1169. expect(adapter.lastOptions?.messages.slice(0, -1)).toEqual(prefix)
  1170. expect(adapter.lastOptions?.messages.at(-1)).toMatchObject({ role: 'user' })
  1171. expect(result.shadowedSeqs).toEqual(nodes.slice(start, start + 2))
  1172. if (system !== undefined) expect(session.surface.nodes[0]).toBe(nodes[0])
  1173. })
  1174. it('requires complete raw output when a subclass marks one local LLM stream call', () => {
  1175. expectTypeOf<{
  1176. summary: ContentBlock[]
  1177. llmStreamCall: true
  1178. provider: string
  1179. model: string
  1180. }>().not.toExtend<SummaryResult>()
  1181. })
  1182. it('uses configured model/default cap, forwards cancellation, and keeps only safe text', async () => {
  1183. const { adapter, compact } = await summarizerHarness([
  1184. { type: 'reasoning', text: 'private' },
  1185. { type: 'text', text: 'public summary' },
  1186. { type: 'tool-call', id: ToolCallId('unexpected'), name: 'x', arguments: '{}' },
  1187. ], undefined, MODEL, {
  1188. auto: false,
  1189. summarizationProvider: MODEL,
  1190. summarizationModel: MODEL,
  1191. maxTokens: 321,
  1192. })
  1193. const session = conversation(1)
  1194. adapter.usage = { inputTokens: 12, outputTokens: 3 }
  1195. const output = await compact.runSummarize(promptInput('transcript'), agent(session, 'fallback'), SIGNAL)
  1196. expect(output).toEqual({
  1197. summary: [{ type: 'text', text: 'public summary' }],
  1198. rawOutput: [
  1199. { type: 'reasoning', text: 'private' },
  1200. { type: 'text', text: 'public summary' },
  1201. { type: 'tool-call', id: ToolCallId('unexpected'), name: 'x', arguments: '{}' },
  1202. ],
  1203. llmStreamCall: true,
  1204. provider: MODEL,
  1205. model: MODEL,
  1206. maxTokens: 321,
  1207. usage: adapter.usage,
  1208. })
  1209. expect(adapter.lastOptions).toMatchObject({
  1210. provider: MODEL,
  1211. model: MODEL,
  1212. maxTokens: 321,
  1213. signal: SIGNAL,
  1214. sessionId: session.id,
  1215. purpose: 'compaction',
  1216. })
  1217. const instruction = adapter.lastOptions?.messages.at(-1)?.content[0]
  1218. expect(instruction?.type === 'text' ? instruction.text : '').toContain('## Primary Request and Intent')
  1219. })
  1220. it('replays the conversation prefix and appends the instruction as the final message', async () => {
  1221. const { adapter, compact } = await summarizerHarness([{ type: 'text', text: 'summary' }])
  1222. const tools = [{ name: 'do_thing', description: 'd', parameters: { type: 'object' } }]
  1223. const prefix: Message = createUserMessage({
  1224. content: [
  1225. { type: 'text', text: 'earlier turn' },
  1226. {
  1227. type: 'image',
  1228. attachment: {
  1229. attachmentId: AttachmentId(`sha256:${'a'.repeat(64)}`),
  1230. mediaType: 'image/png',
  1231. bytes: 1,
  1232. width: 1,
  1233. height: 1,
  1234. },
  1235. },
  1236. ],
  1237. source: { kind: 'plugin', plugin: 'test' },
  1238. })
  1239. const system = createSystemMessage('REPLAYED SYSTEM', SYSTEM_PROMPT_PLUGIN)
  1240. await compact.runSummarize({
  1241. tools,
  1242. messages: [system, prefix],
  1243. }, agent(conversation(1), MODEL))
  1244. expect(adapter.lastOptions).not.toHaveProperty('system')
  1245. expect(adapter.lastOptions?.tools).toEqual(tools)
  1246. const messages = adapter.lastOptions?.messages ?? []
  1247. expect(messages.slice(0, -1)).toEqual([system, prefix])
  1248. const last = messages.at(-1)?.content[0]
  1249. const lastText = last?.type === 'text' ? last.text : ''
  1250. expect(lastText).toContain('Write concise English engineering prose.')
  1251. expect(lastText).toContain('numeric values, function signatures, and syntax fragments.')
  1252. expect(lastText).toContain('## Primary Request and Intent')
  1253. })
  1254. it('applies the routed model policy without changing the replayed prefix', async () => {
  1255. const { ctx, compact } = await summarizerHarness(
  1256. [{ type: 'text', text: 'unused default summary' }],
  1257. undefined,
  1258. MODEL,
  1259. {
  1260. auto: false,
  1261. maxTokens: 111,
  1262. modelPolicies: [{
  1263. provider: MODEL,
  1264. model: MODEL,
  1265. summarizationProvider: 'policy-summary',
  1266. summarizationModel: 'policy-summary',
  1267. maxTokens: 222,
  1268. }],
  1269. },
  1270. )
  1271. const policyAdapter = new ScriptedAdapter([{ type: 'text', text: 'policy summary' }])
  1272. ctx.llm.registerAdapter(['policy-summary'], policyAdapter)
  1273. const prefix: Message = createUserMessage({
  1274. content: [{ type: 'text', text: 'warm prefix' }],
  1275. source: { kind: 'plugin', plugin: 'test' },
  1276. })
  1277. const system = createSystemMessage('WARM SYSTEM', SYSTEM_PROMPT_PLUGIN)
  1278. const output = await compact.runSummarize({
  1279. messages: [system, prefix],
  1280. }, agent(conversation(1), 'fallback'))
  1281. expect(output).toMatchObject({
  1282. provider: 'policy-summary',
  1283. model: 'policy-summary',
  1284. maxTokens: 222,
  1285. })
  1286. expect(policyAdapter.lastOptions).toMatchObject({
  1287. provider: 'policy-summary',
  1288. model: 'policy-summary',
  1289. maxTokens: 222,
  1290. })
  1291. expect(policyAdapter.lastOptions).not.toHaveProperty('system')
  1292. expect(policyAdapter.lastOptions?.messages.slice(0, -1)).toEqual([system, prefix])
  1293. })
  1294. it('resolves the latest routed provider/model before the AgentOptions pair', async () => {
  1295. const { adapter, compact } = await summarizerHarness([{ type: 'text', text: 'summary' }], undefined, 'routed')
  1296. const session = conversation(1)
  1297. session.append('request/header', {
  1298. header: { config: { provider: 'routed', model: 'routed' } },
  1299. reason: 'initial',
  1300. })
  1301. const output = await compact.runSummarize(promptInput('history'), agent(session, 'fallback'))
  1302. expect(output.provider).toBe('routed')
  1303. expect(output.model).toBe('routed')
  1304. expect(adapter.lastOptions?.provider).toBe('routed')
  1305. expect(adapter.lastOptions?.model).toBe('routed')
  1306. })
  1307. it('records the model actually dispatched after one-shot stream routing', async () => {
  1308. const { ctx, compact } = await summarizerHarness([{ type: 'text', text: 'unused' }])
  1309. const routedAdapter = new ScriptedAdapter([{ type: 'text', text: 'routed summary' }])
  1310. ctx.llm.registerAdapter(['routed-summary-provider'], routedAdapter)
  1311. ctx.on('llm/stream', (options, next) => {
  1312. options.provider = 'routed-summary-provider'
  1313. options.model = 'routed-summary-model'
  1314. return next()
  1315. })
  1316. const session = conversation(3, 'large history '.repeat(500))
  1317. const nodes = session.surface.nodes
  1318. await compact.compactRegion(nodes[0]!, nodes[3]!, agent(session, MODEL), SIGNAL)
  1319. expect(session.snapshotEvents().findLast(event => event.type === 'compaction/summary')?.data).toMatchObject({
  1320. summary: [{ type: 'text', text: 'routed summary' }],
  1321. llmStreamCall: true,
  1322. provider: 'routed-summary-provider',
  1323. model: 'routed-summary-model',
  1324. })
  1325. expect(routedAdapter.lastOptions?.provider).toBe('routed-summary-provider')
  1326. expect(routedAdapter.lastOptions?.model).toBe('routed-summary-model')
  1327. })
  1328. it('fails clearly when no complete summarization target can be resolved', async () => {
  1329. const ctx = new Context()
  1330. await ctx.plugin(LlmRuntime)
  1331. await ctx.plugin(SessionProjectionRegistry)
  1332. void new TokenMeter(ctx)
  1333. const compact = new ExposedCompactionEngine(ctx, { auto: false })
  1334. await expect(compact.runSummarize(promptInput('history'), agent(Session.create(SessionId('model-less')))))
  1335. .rejects.toThrow(/no provider\/model available for summarization/)
  1336. })
  1337. it('uses a complete AgentOptions target when no durable route exists', async () => {
  1338. const { adapter, compact } = await summarizerHarness([{ type: 'text', text: 'summary' }])
  1339. const session = Session.create(SessionId('headerless-summary'))
  1340. await expect(compact.runSummarize(promptInput('history'), agent(session, MODEL))).resolves.toMatchObject({
  1341. provider: MODEL,
  1342. model: MODEL,
  1343. })
  1344. expect(adapter.lastOptions).toMatchObject({ provider: MODEL, model: MODEL })
  1345. })
  1346. it.each([
  1347. { provider: '', model: MODEL },
  1348. { provider: MODEL },
  1349. { provider: MODEL, model: '' },
  1350. ])('rejects incomplete AgentOptions target %#', async (options) => {
  1351. const { compact } = await summarizerHarness([{ type: 'text', text: 'unused' }])
  1352. const owner = {
  1353. session: Session.create(SessionId(`incomplete-${String(options.model)}`)),
  1354. options,
  1355. } as Agent
  1356. await expect(compact.runSummarize(promptInput('history'), owner))
  1357. .rejects.toThrow(/no provider\/model available for summarization/)
  1358. })
  1359. it.each([
  1360. [{ kind: 'error', failure: { message: 'provider failed', code: 'PROVIDER' } }, 'PROVIDER', /provider failed/],
  1361. [{ kind: 'error', failure: { message: 'opaque', code: 'UNKNOWN' } }, 'UNKNOWN', /opaque/],
  1362. [{ kind: 'aborted', failure: { message: 'summarization aborted', code: 'ABORTED' } }, 'ABORTED', /aborted/],
  1363. [{ kind: 'max-tokens' }, 'MAX_TOKENS', /token cap/],
  1364. ] as Array<[(StreamChunk & { type: 'finish' })['reason'], string | undefined, RegExp]>) (
  1365. 'rejects terminal finish %#',
  1366. async (finish, code, pattern) => {
  1367. const { compact } = await summarizerHarness([], finish)
  1368. let thrown: unknown
  1369. try {
  1370. await compact.runSummarize(promptInput('history'), agent(conversation(1), MODEL))
  1371. } catch (error: unknown) {
  1372. thrown = error
  1373. }
  1374. expect(thrown).toBeInstanceOf(Error)
  1375. expect((thrown as Error).message).toMatch(pattern)
  1376. expect((thrown as Error & { code?: string }).code).toBe(code)
  1377. },
  1378. )
  1379. it('rejects empty or reasoning-only successful output', async () => {
  1380. const { compact } = await summarizerHarness([{ type: 'reasoning', text: 'private' }])
  1381. await expect(compact.runSummarize(promptInput('history'), agent(conversation(1), MODEL)))
  1382. .rejects.toThrow(/no text summary content/)
  1383. })
  1384. it('rejects image summary output instead of silently dropping it', async () => {
  1385. const { compact } = await summarizerHarness([
  1386. {
  1387. type: 'image',
  1388. attachment: {
  1389. attachmentId: AttachmentId(`sha256:${'b'.repeat(64)}`),
  1390. mediaType: 'image/png',
  1391. bytes: 1,
  1392. width: 1,
  1393. height: 1,
  1394. },
  1395. },
  1396. { type: 'text', text: 'partial summary' },
  1397. ])
  1398. await expect(compact.runSummarize(promptInput('history'), agent(conversation(1), MODEL)))
  1399. .rejects.toMatchObject({ code: 'UNSUPPORTED_CONTENT' })
  1400. })
  1401. it('rejects image summary output nested in a tool result', async () => {
  1402. const { compact } = await summarizerHarness([{
  1403. type: 'tool-result',
  1404. toolCallId: ToolCallId('summary-tool'),
  1405. content: [{
  1406. type: 'image',
  1407. attachment: {
  1408. attachmentId: AttachmentId(`sha256:${'c'.repeat(64)}`),
  1409. mediaType: 'image/png',
  1410. bytes: 1,
  1411. width: 1,
  1412. height: 1,
  1413. },
  1414. }],
  1415. }])
  1416. await expect(compact.runSummarize(promptInput('history'), agent(conversation(1), MODEL)))
  1417. .rejects.toMatchObject({ code: 'UNSUPPORTED_CONTENT' })
  1418. })
  1419. })
  1420. describe('automatic listener and loader composition', () => {
  1421. function preStep(ctx: Context, owner: Agent, signal = SIGNAL) {
  1422. return agentEvents(ctx, owner).waterfall(
  1423. 'agent/pre-step', { messages: [], turn: 1, step: 1, signal },
  1424. () => Promise.resolve({ kind: 'enter' as const, messages: [] }),
  1425. )
  1426. }
  1427. function recover(
  1428. ctx: Context,
  1429. owner: Agent,
  1430. error: Error & { code?: string },
  1431. signal = SIGNAL,
  1432. next: () => Promise<RequestErrorAction> = () => Promise.resolve(undefined),
  1433. ): Promise<boolean> {
  1434. const failure: LlmFailure = { message: error.message, code: error.code ?? 'UNKNOWN' }
  1435. const turn = owner.session.snapshotEvents().findLast(event => event.type === 'turn/start')?.data.turn ?? 1
  1436. return agentEvents(ctx, owner).waterfall(
  1437. 'agent/request-error',
  1438. { turn, step: 1, provider: 'test', failure, retryPolicy: undefined, signal },
  1439. next,
  1440. ).then(action => action?.kind === 'retry')
  1441. }
  1442. function overflow(message = 'provider overflow'): Error & { code: string } {
  1443. return Object.assign(new Error(message), { code: CONTEXT_WINDOW_EXCEEDED_CODE })
  1444. }
  1445. it('compacts before a step above threshold using the durable routed model and remains idle below it', async () => {
  1446. const ctx = createContext()
  1447. const compact = new TestCompactionEngine(ctx, {
  1448. thresholdRatio: 0.5,
  1449. retainTokens: 180,
  1450. })
  1451. const pressured = conversation(4)
  1452. await preStep(ctx, agent(pressured, 'unconfigured-agent-fallback'))
  1453. expect(pressured.snapshotEvents().some(event => event.type === 'compaction/summary')).toBe(true)
  1454. const small = conversation(1)
  1455. await preStep(ctx, agent(small, MODEL))
  1456. expect(small.snapshotEvents().some(event => event.type === 'compaction/start')).toBe(false)
  1457. expect(compact.calls).toHaveLength(1)
  1458. })
  1459. it('skips pre-step pressure when the step signal is already aborted', async () => {
  1460. const ctx = createContext()
  1461. const compact = new TestCompactionEngine(ctx, {
  1462. thresholdRatio: 0.5,
  1463. retainTokens: 180,
  1464. })
  1465. const pressured = conversation(4)
  1466. const compactIfNeeded = vi.spyOn(compact, 'compactIfNeeded')
  1467. await expect(preStep(ctx, agent(pressured, MODEL), AbortSignal.abort('step aborted')))
  1468. .resolves.toEqual({ kind: 'enter', messages: [] })
  1469. expect(compactIfNeeded).not.toHaveBeenCalled()
  1470. expect(pressured.snapshotEvents().some(event => event.type === 'compaction/start')).toBe(false)
  1471. })
  1472. it('warns and continues after operational failures, including non-Errors', async () => {
  1473. const ctx = createContext()
  1474. const warnings: string[] = []
  1475. ctx.logger.warn = ((message: string) => void warnings.push(message)) as typeof ctx.logger.warn
  1476. const compact = new TestCompactionEngine(ctx, {
  1477. thresholdRatio: 0.5,
  1478. retainTokens: 180,
  1479. })
  1480. compact.error = 'temporary failure'
  1481. const session = conversation(4)
  1482. await expect(preStep(ctx, agent(session, MODEL))).resolves.toEqual({ kind: 'enter', messages: [] })
  1483. expect(warnings).toContainEqual(expect.stringContaining('temporary failure'))
  1484. expect(session.snapshotEvents().some(event => event.type === 'compaction/summary')).toBe(false)
  1485. })
  1486. it('warns once per routed target when proactive pressure has no context metadata', async () => {
  1487. const ctx = createContext()
  1488. const warnings: string[] = []
  1489. ctx.logger.warn = ((message: string) => void warnings.push(message)) as typeof ctx.logger.warn
  1490. vi.spyOn(ctx.llm, 'resolveModelInfo').mockImplementation((provider, model) => Promise.resolve({
  1491. provider,
  1492. id: model,
  1493. name: model,
  1494. }))
  1495. void new TestCompactionEngine(ctx, {
  1496. thresholdRatio: 0.5,
  1497. retainTokens: 180,
  1498. })
  1499. const session = conversation(4)
  1500. await preStep(ctx, agent(session, MODEL))
  1501. await preStep(ctx, agent(session, MODEL))
  1502. expect(warnings).toEqual([
  1503. expect.stringContaining(`no context capacity for ${MODEL}/${MODEL}`),
  1504. ])
  1505. })
  1506. it('warns once per routed target when absolute retention exceeds its resolved threshold', async () => {
  1507. const ctx = createContext()
  1508. const warnings: string[] = []
  1509. ctx.logger.warn = ((message: string) => void warnings.push(message)) as typeof ctx.logger.warn
  1510. void new TestCompactionEngine(ctx, {
  1511. thresholdRatio: 0.5,
  1512. retainTokens: 500,
  1513. })
  1514. const session = conversation(4)
  1515. await preStep(ctx, agent(session, MODEL))
  1516. await preStep(ctx, agent(session, MODEL))
  1517. expect(warnings).toEqual([
  1518. expect.stringContaining('retainTokens (500) must be less than threshold tokens 500'),
  1519. ])
  1520. })
  1521. it('force-compacts below normal pressure for canonical overflow and retries only after replacement', async () => {
  1522. const ctx = createContext(10_000)
  1523. void new TestCompactionEngine(ctx, {
  1524. thresholdRatio: 1,
  1525. retainTokens: 900,
  1526. })
  1527. const session = conversation(3)
  1528. const beforeGeneration = session.surface.replaceGeneration
  1529. const retainedSeq = session.surface.nodes.at(-1)!
  1530. const threshold = 10_000
  1531. expect(ctx.tokenMeter.measure(session).totalTokens).toBeLessThan(threshold)
  1532. const decision = await recover(ctx, agent(session, 'unconfigured-agent-fallback'), overflow())
  1533. expect(decision).toBe(true)
  1534. expect(session.surface.replaceGeneration).toBe(beforeGeneration + 1)
  1535. expect(session.snapshotEvents().some(event => event.type === 'compaction/summary')).toBe(true)
  1536. expect(session.surface.nodes).toContain(retainedSeq)
  1537. })
  1538. it('authorizes overflow retry when pruning alone advances an indivisible surface', async () => {
  1539. const ctx = createContext(10_000)
  1540. void new ToolResultPruner(ctx, {
  1541. thresholdChars: 100,
  1542. headChars: 20,
  1543. tailChars: 10,
  1544. })
  1545. const compact = new TestCompactionEngine(ctx, {
  1546. thresholdRatio: 1,
  1547. retainTokens: 900,
  1548. })
  1549. const session = oversizedToolResult()
  1550. expect(await recover(ctx, agent(session, MODEL), overflow())).toBe(true)
  1551. expect(session.surface.replaceGeneration).toBe(1)
  1552. expect(session.snapshotEvents().some(event => event.type === 'compaction/summary')).toBe(false)
  1553. expect(compact.calls).toHaveLength(0)
  1554. })
  1555. it('continues overflow recovery with summarization on the pruned surface', async () => {
  1556. const ctx = createContext(10_000)
  1557. void new ToolResultPruner(ctx, {
  1558. thresholdChars: 100,
  1559. headChars: 20,
  1560. tailChars: 10,
  1561. })
  1562. const compact = new TestCompactionEngine(ctx, {
  1563. thresholdRatio: 1,
  1564. retainTokens: 900,
  1565. })
  1566. const session = toolConversation()
  1567. expect(await recover(ctx, agent(session, MODEL), overflow())).toBe(true)
  1568. expect(session.snapshotEvents().some(event => event.type === 'compaction/summary')).toBe(true)
  1569. expect(compact.calls).toHaveLength(1)
  1570. expect(summarizedText(compact.calls[0]!.input)).toContain('tool result middle pruned')
  1571. })
  1572. it('retries from a durable prune when later overflow summarization throws', async () => {
  1573. const ctx = createContext(10_000)
  1574. const warnings: string[] = []
  1575. ctx.logger.warn = ((message: string) => void warnings.push(message)) as typeof ctx.logger.warn
  1576. void new ToolResultPruner(ctx, {
  1577. thresholdChars: 100,
  1578. headChars: 20,
  1579. tailChars: 10,
  1580. })
  1581. const compact = new TestCompactionEngine(ctx, {
  1582. thresholdRatio: 1,
  1583. retainTokens: 900,
  1584. })
  1585. compact.error = new Error('summary unavailable after prune')
  1586. const session = oversizedToolResult(3_000, true)
  1587. expect(await recover(ctx, agent(session, MODEL), overflow())).toBe(true)
  1588. expect(session.surface.replaceGeneration).toBe(1)
  1589. expect(session.snapshotEvents().filter(event => event.type === 'tool/result')).toHaveLength(2)
  1590. expect(session.snapshotEvents().findLast(event => event.type === 'compaction/end')?.data)
  1591. .toMatchObject({ error: 'summary unavailable after prune' })
  1592. expect(warnings).toContainEqual(expect.stringContaining('retrying from the replacement surface'))
  1593. })
  1594. it('lets cancellation win when summary throws after a durable prune', async () => {
  1595. const ctx = createContext(10_000)
  1596. const controller = new AbortController()
  1597. void new ToolResultPruner(ctx, {
  1598. thresholdChars: 100,
  1599. headChars: 20,
  1600. tailChars: 10,
  1601. })
  1602. const compact = new TestCompactionEngine(ctx, {
  1603. thresholdRatio: 1,
  1604. retainTokens: 900,
  1605. })
  1606. compact.mutateDuringSummary = () => { controller.abort('cancelled during summary') }
  1607. compact.error = new Error('summary cancelled after prune')
  1608. const session = oversizedToolResult(3_000, true)
  1609. expect(await recover(ctx, agent(session, MODEL), overflow(), controller.signal)).toBe(false)
  1610. expect(session.surface.replaceGeneration).toBe(1)
  1611. })
  1612. it('preserves the newest whole tool-call/result pair during forced overflow compaction', async () => {
  1613. const ctx = createContext()
  1614. void new TestCompactionEngine(ctx, {
  1615. thresholdRatio: 1,
  1616. retainTokens: 90,
  1617. })
  1618. const session = toolConversation()
  1619. const newestAssistant = session.surface.nodes.at(-2)!
  1620. const newestResult = session.surface.nodes.at(-1)!
  1621. expect(await recover(ctx, agent(session, MODEL), overflow())).toBe(true)
  1622. const currentAssistant = session.surface.nodes.find(node => node === newestAssistant)
  1623. const currentResult = session.surface.nodes.find(node => node === newestResult)
  1624. expect(currentAssistant).toBeDefined()
  1625. expect(currentResult).toBeDefined()
  1626. expect(toolPairingBalancedBefore(session, currentAssistant!)).toBe(true)
  1627. expect(toolPairingBalancedAfter(session, currentResult!)).toBe(true)
  1628. })
  1629. it('does not retry when a backend reports success without replacing the surface', async () => {
  1630. const ctx = createContext()
  1631. const compact = new TestCompactionEngine(ctx)
  1632. const session = conversation(2)
  1633. const fakeResult: CompactionResult = {
  1634. compactionId: CompactionId('fake-compaction'),
  1635. startSeq: SessionSeq(1),
  1636. summarySeq: SessionSeq(2),
  1637. endSeq: SessionSeq(3),
  1638. summary: [{ type: 'text', text: 'fake' }],
  1639. shadowedRange: { start: SessionSeq(1), end: SessionSeq(2) },
  1640. shadowedSeqs: [SessionSeq(1), SessionSeq(2)],
  1641. shadowedTokenCount: 10,
  1642. }
  1643. vi.spyOn(compact, 'compactIfNeeded').mockResolvedValue(fakeResult)
  1644. expect(await recover(ctx, agent(session, MODEL), overflow())).toBe(false)
  1645. expect(session.surface.replaceGeneration).toBe(0)
  1646. })
  1647. it('delegates downstream exactly once when no replacement is available', async () => {
  1648. const ctx = createContext()
  1649. const compact = new TestCompactionEngine(ctx)
  1650. vi.spyOn(compact, 'compactIfNeeded').mockResolvedValue(null)
  1651. const downstream = new Error('downstream recovery failed')
  1652. let calls = 0
  1653. await expect(recover(
  1654. ctx,
  1655. agent(conversation(2), MODEL),
  1656. overflow(),
  1657. SIGNAL,
  1658. () => {
  1659. calls += 1
  1660. return Promise.reject(downstream)
  1661. },
  1662. )).rejects.toBe(downstream)
  1663. expect(calls).toBe(1)
  1664. })
  1665. it('preserves the original provider error when recovery throws', async () => {
  1666. const ctx = createContext()
  1667. const warnings: string[] = []
  1668. ctx.logger.warn = ((message: string) => void warnings.push(message)) as typeof ctx.logger.warn
  1669. const compact = new TestCompactionEngine(ctx)
  1670. compact.error = new Error('summary unavailable')
  1671. const original = overflow('original provider overflow')
  1672. expect(await recover(ctx, agent(conversation(3), MODEL), original)).toBe(false)
  1673. expect(original).toMatchObject({
  1674. message: 'original provider overflow',
  1675. code: CONTEXT_WINDOW_EXCEEDED_CODE,
  1676. })
  1677. expect(warnings).toContainEqual(expect.stringContaining('preserving the original request error'))
  1678. })
  1679. it('delegates once when overflow recovery throws a non-Error value', async () => {
  1680. const ctx = createContext()
  1681. const warnings: string[] = []
  1682. ctx.logger.warn = ((message: string) => void warnings.push(message)) as typeof ctx.logger.warn
  1683. const compact = new TestCompactionEngine(ctx)
  1684. compact.error = 'non-error recovery failure'
  1685. const session = conversation(3)
  1686. const generation = session.surface.replaceGeneration
  1687. const original = overflow('original provider failure')
  1688. let delegations = 0
  1689. const decision = await recover(ctx, agent(session, MODEL), original, SIGNAL, () => {
  1690. delegations += 1
  1691. return Promise.resolve(undefined)
  1692. })
  1693. expect(decision).toBe(false)
  1694. expect(delegations).toBe(1)
  1695. expect(session.surface.replaceGeneration).toBe(generation)
  1696. expect(original).toMatchObject({
  1697. message: 'original provider failure',
  1698. code: CONTEXT_WINDOW_EXCEEDED_CODE,
  1699. })
  1700. expect(warnings).toContainEqual(expect.stringContaining('non-error recovery failure'))
  1701. })
  1702. it('recovers an overflow for an unlisted routed model', async () => {
  1703. const ctx = createContext()
  1704. void new TestCompactionEngine(ctx)
  1705. const session = conversation(2)
  1706. session.append('request/header', {
  1707. header: { config: { provider: 'unknown-routed-provider', model: 'unknown-routed-model' } },
  1708. reason: 'resume',
  1709. })
  1710. expect(await recover(ctx, agent(session, MODEL), overflow('unlisted-model overflow')))
  1711. .toBe(true)
  1712. })
  1713. it('delegates canonical overflow when no durable routed target exists', async () => {
  1714. const ctx = createContext()
  1715. void new TestCompactionEngine(ctx)
  1716. const session = Session.create(SessionId('headerless-overflow'))
  1717. session.append('turn/start', {
  1718. turn: 1,
  1719. })
  1720. await expect(recover(ctx, agent(session, MODEL), overflow())).resolves.toBe(false)
  1721. })
  1722. it('honors retry caps and ignores non-context failures', async () => {
  1723. const ctx = createContext()
  1724. const compact = new TestCompactionEngine(ctx, { maxOverflowRetries: 1 })
  1725. const compactSpy = vi.spyOn(compact, 'compactIfNeeded')
  1726. const owner = agent(conversation(3), MODEL)
  1727. expect(await recover(ctx, owner, Object.assign(new Error('rate limit'), { code: 'RATE_LIMIT' })))
  1728. .toBe(false)
  1729. expect(await recover(ctx, owner, overflow())).toBe(true)
  1730. compactSpy.mockClear()
  1731. expect(await recover(ctx, owner, overflow())).toBe(false)
  1732. expect(compactSpy).not.toHaveBeenCalled()
  1733. })
  1734. it('applies the routed model override to the overflow retry cap', async () => {
  1735. const ctx = createContext()
  1736. const compact = new TestCompactionEngine(ctx, {
  1737. maxOverflowRetries: 2,
  1738. modelPolicies: [{
  1739. provider: MODEL,
  1740. model: MODEL,
  1741. maxOverflowRetries: 1,
  1742. }],
  1743. })
  1744. const compactSpy = vi.spyOn(compact, 'compactIfNeeded')
  1745. const owner = agent(conversation(3), MODEL)
  1746. expect(await recover(ctx, owner, overflow())).toBe(true)
  1747. compactSpy.mockClear()
  1748. expect(await recover(ctx, owner, overflow())).toBe(false)
  1749. expect(compactSpy).not.toHaveBeenCalled()
  1750. })
  1751. it('does not retry when cancellation lands during an awaited compaction', async () => {
  1752. const ctx = createContext()
  1753. const compact = new TestCompactionEngine(ctx)
  1754. const controller = new AbortController()
  1755. compact.mutateDuringSummary = () => { controller.abort('cancelled during summary') }
  1756. const session = conversation(3)
  1757. const generation = session.surface.replaceGeneration
  1758. expect(await recover(ctx, agent(session, MODEL), overflow(), controller.signal)).toBe(false)
  1759. expect(session.surface.replaceGeneration).toBe(generation + 1)
  1760. })
  1761. it('maxOverflowRetries:0 disables recovery without disabling post-step pressure', async () => {
  1762. const ctx = createContext()
  1763. void new TestCompactionEngine(ctx, {
  1764. maxOverflowRetries: 0,
  1765. thresholdRatio: 0.5,
  1766. retainTokens: 180,
  1767. })
  1768. const session = conversation(4)
  1769. await preStep(ctx, agent(session, MODEL))
  1770. const summaries = session.snapshotEvents().filter(event => event.type === 'compaction/summary').length
  1771. expect(summaries).toBe(1)
  1772. expect(await recover(ctx, agent(session, MODEL), overflow())).toBe(false)
  1773. expect(session.snapshotEvents().filter(event => event.type === 'compaction/summary')).toHaveLength(summaries)
  1774. })
  1775. it('auto:false installs neither automatic listener', async () => {
  1776. const ctx = createContext()
  1777. void new TestCompactionEngine(ctx, {
  1778. auto: false,
  1779. thresholdRatio: 0.5,
  1780. retainTokens: 180,
  1781. })
  1782. const session = conversation(4)
  1783. await preStep(ctx, agent(session, MODEL))
  1784. expect(session.snapshotEvents().some(event => event.type === 'compaction/start')).toBe(false)
  1785. expect(await recover(ctx, agent(session, MODEL), overflow())).toBe(false)
  1786. })
  1787. it('loads and disposes the real zero-config service stack', async () => {
  1788. const ctx = new Context()
  1789. await ctx.plugin(LlmRuntime)
  1790. await ctx.plugin(SessionStore)
  1791. await ctx.plugin(SessionProjectionRegistry)
  1792. const meterFiber = await ctx.plugin(TokenMeter)
  1793. const compactFiber = await ctx.plugin(BasicCompactionEngine, { auto: false })
  1794. expect(ctx.get('compaction')).toBeInstanceOf(BasicCompactionEngine)
  1795. await compactFiber.dispose()
  1796. expect(ctx.get('compaction')).toBeUndefined()
  1797. await meterFiber.dispose()
  1798. expect(ctx.get('tokenMeter')).toBeUndefined()
  1799. })
  1800. it('removes its automatic listener with the plugin fiber', async () => {
  1801. const ctx = new Context()
  1802. await ctx.plugin(LlmRuntime)
  1803. await ctx.plugin(SessionProjectionRegistry)
  1804. await ctx.plugin(TokenMeter)
  1805. const fiber = await ctx.plugin(TestCompactionEngine, {
  1806. thresholdRatio: 0.5,
  1807. retainTokens: 180,
  1808. })
  1809. await fiber.dispose()
  1810. const session = conversation(4)
  1811. await preStep(ctx, agent(session, MODEL))
  1812. expect(session.snapshotEvents().some(event => event.type === 'compaction/start')).toBe(false)
  1813. expect(await recover(ctx, agent(session, MODEL), overflow())).toBe(false)
  1814. })
  1815. })
  1816. describe('route-priced image pressure', () => {
  1817. const IMAGE_VISUAL_TOKENS = 300
  1818. const IMAGE_HANDLE_TEXT = 'request preview'
  1819. class PricedContextAdapter extends ContextAdapter {
  1820. override imageRequestPricing(): { priceImages: (images: readonly unknown[]) => Array<{ visualTokens: number; text: string }> } {
  1821. return {
  1822. priceImages: images => images.map(() => ({
  1823. visualTokens: IMAGE_VISUAL_TOKENS,
  1824. text: IMAGE_HANDLE_TEXT,
  1825. })),
  1826. }
  1827. }
  1828. }
  1829. function pricedContext(contextWindow = 1_000): Context {
  1830. const ctx = new Context()
  1831. void new LlmRuntime(ctx)
  1832. new SessionProjectionRegistry(ctx)
  1833. void new TokenMeter(ctx)
  1834. ctx.llm.registerAdapter([MODEL], new PricedContextAdapter(contextWindow))
  1835. return ctx
  1836. }
  1837. /** Closed short-text turns whose user messages each carry one image. */
  1838. function imageConversation(turns = 4): Session {
  1839. const session = Session.create(SessionId(`image-dense-${turns}`))
  1840. for (let turn = 1; turn <= turns; turn += 1) {
  1841. session.append('turn/start', { turn })
  1842. session.append('user/message', createUserMessage({
  1843. content: [
  1844. { type: 'text', text: `image turn ${turn}` },
  1845. {
  1846. type: 'image',
  1847. attachment: {
  1848. attachmentId: AttachmentId(`sha256:${String(turn).repeat(8)}`),
  1849. mediaType: 'image/png',
  1850. bytes: 2048,
  1851. width: 800,
  1852. height: 800,
  1853. name: `shot-${turn}`,
  1854. },
  1855. },
  1856. ],
  1857. source: { kind: 'user' },
  1858. }), { surfaceOp: 'append' })
  1859. session.append('step/start', { turn, step: 1 })
  1860. if (turn === 1) {
  1861. session.append('request/header', {
  1862. header: { config: { provider: MODEL, model: MODEL } },
  1863. reason: 'initial',
  1864. })
  1865. }
  1866. session.append('assistant/message', {
  1867. stream: [],
  1868. turn,
  1869. step: 1,
  1870. message: createMessage({
  1871. role: 'assistant',
  1872. content: [{ type: 'text', text: `ok ${turn}` }],
  1873. source: {
  1874. kind: 'model',
  1875. ...{ provider: MODEL, model: MODEL },
  1876. },
  1877. }),
  1878. }, { surfaceOp: 'append' })
  1879. session.append('step/end', { turn, step: 1 })
  1880. session.append('turn/end', { turn, reason: { kind: 'completed' } })
  1881. }
  1882. session.append('turn/start', { turn: turns + 1 })
  1883. return session
  1884. }
  1885. it('selects an image-dense range only when the routed price counts visual tokens', () => {
  1886. const session = imageConversation()
  1887. const routed = pricedContext().tokenMeter.measure(session)
  1888. const neutral = createContext().tokenMeter.measure(session)
  1889. expect(routed.surfaceTokens).toBeGreaterThan(neutral.surfaceTokens + 4 * IMAGE_VISUAL_TOKENS - 200)
  1890. expect(routed.nodes.map(node => node.seq)).toEqual(neutral.nodes.map(node => node.seq))
  1891. expect(routed.nodes.map(node => node.heuristicTokens)).toEqual(neutral.nodes.map(node => node.tokens))
  1892. // The same verbatim tail budget retains almost everything under the
  1893. // neutral heuristic but forces a cut once visual tokens are counted.
  1894. expect(selectCompactableRange(session, neutral, 350)).toBeNull()
  1895. const range = selectCompactableRange(session, routed, 350)
  1896. expect(range).not.toBeNull()
  1897. })
  1898. it('accepts a summary larger than the span heuristic when the route price shrinks', async () => {
  1899. // A single short image message prices below a framed summary under the
  1900. // fixed heuristic but far above it under the route: the shrink comparison
  1901. // must ask whether the replacement lowers route pressure.
  1902. const ctx = pricedContext(1_000)
  1903. const session = imageConversation(1)
  1904. const before = ctx.tokenMeter.measure(session)
  1905. const imageNode = before.nodes[0]!
  1906. const compact = new TestCompactionEngine(ctx, { auto: false })
  1907. compact.summary = [{
  1908. type: 'text',
  1909. text: 'summary text sized between the heuristic and route prices of the shadowed image message, '
  1910. + 'long enough that the fixed heuristic alone would reject it as not smaller '
  1911. + 'while the route-priced comparison accepts the pressure reduction.',
  1912. }]
  1913. const framed = ctx.tokenMeter.estimateMessage(createUserMessage({
  1914. content: frameSummary(compact.summary),
  1915. source: { kind: 'plugin', plugin: 'test' },
  1916. }))
  1917. expect(framed).toBeGreaterThan(imageNode.heuristicTokens)
  1918. expect(framed).toBeLessThan(imageNode.tokens)
  1919. const result = await compact.compactRegion(imageNode.seq, imageNode.seq, agent(session), SIGNAL)
  1920. expect(result.shadowedSeqs).toEqual([imageNode.seq])
  1921. expect(result.shadowedTokenCount).toBe(imageNode.heuristicTokens)
  1922. })
  1923. it('triggers pressure compaction from routed visual tokens and logs heuristic shadow prices', async () => {
  1924. const ctx = pricedContext(1_000)
  1925. const session = imageConversation()
  1926. const before = ctx.tokenMeter.measure(session)
  1927. const compact = new TestCompactionEngine(ctx, {
  1928. auto: false,
  1929. thresholdRatio: 0.8,
  1930. retainTokens: 350,
  1931. })
  1932. // The same history stays below the 800-token threshold without pricing.
  1933. const neutralResult = await compactIfNeeded(service({
  1934. auto: false,
  1935. thresholdRatio: 0.8,
  1936. retainTokens: 350,
  1937. }), session)
  1938. expect(neutralResult).toBeNull()
  1939. const result = await compact.compactIfNeeded(agent(session), 'pressure', SIGNAL)
  1940. expect(result).not.toBeNull()
  1941. const summaryEvent = session.snapshotEvents().find(event => event.type === 'compaction/summary')
  1942. expect(summaryEvent).toBeDefined()
  1943. const shadowedHeuristic = before.nodes
  1944. .filter(node => result?.shadowedSeqs.includes(node.seq))
  1945. .reduce((total, node) => total + node.heuristicTokens, 0)
  1946. expect(summaryEvent?.data.shadowedTokenCount).toBe(shadowedHeuristic)
  1947. })
  1948. })