compact-basic.spec.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. import { describe, expect, it, vi } from 'vitest'
  2. import { Context } from 'cordis'
  3. import BasicCompactService, { resolveConfig } from '@deepseek-ai/dsh-compact-basic'
  4. import type { BasicCompactConfig } from '@deepseek-ai/dsh-compact-basic'
  5. import { selectCompactableRange } from '@deepseek-ai/dsh-compact-basic/src/region.ts'
  6. import type { CompactionResult } from '@deepseek-ai/dsh-compact'
  7. import LlmService, { CallId, LlmAdapter } from '@deepseek-ai/dsh-llm'
  8. import type { ContentBlock, GenerateOptions, Message, StreamChunk } from '@deepseek-ai/dsh-llm'
  9. import { Session, SessionId } from '@deepseek-ai/dsh-session'
  10. import TokenMeterService from '@deepseek-ai/dsh-token-meter'
  11. import type { Agent } from '@deepseek-ai/dsh-agent'
  12. const SIGNAL = new AbortController().signal
  13. const MODEL = 'test-model'
  14. function createContext(contextWindow = 1_000): Context {
  15. const ctx = new Context()
  16. void new TokenMeterService(ctx, { contextWindow })
  17. return ctx
  18. }
  19. function agent(session: Session, model?: string): Agent {
  20. return { session, options: model === undefined ? {} : { provider: model, model } } as Agent
  21. }
  22. /** Closed two-message turns followed by one open turn for durable compaction events. */
  23. function conversation(turns = 4, text = 'fixture '.repeat(40).trim()): Session {
  24. const session = new Session(SessionId(`conversation-${turns}`))
  25. for (let turn = 1; turn <= turns; turn += 1) {
  26. session.append('turn/start', { turn, trigger: { kind: 'message', source: { kind: 'user' } } })
  27. session.append('user/message', {
  28. content: [{ type: 'text', text: `${text} user ${turn}` }],
  29. source: { kind: 'user' },
  30. }, { surfaceOp: 'append' })
  31. session.append('step/start', { turn, step: 1 })
  32. session.append('assistant/message', {
  33. provenance: { provider: MODEL, model: MODEL },
  34. turn,
  35. step: 1,
  36. content: [{ type: 'text', text: `${text} assistant ${turn}` }],
  37. }, { surfaceOp: 'append' })
  38. session.append('step/end', { turn, step: 1 })
  39. session.append('turn/end', { turn, reason: { kind: 'completed' } })
  40. }
  41. session.append('turn/start', {
  42. turn: turns + 1,
  43. trigger: { kind: 'message', source: { kind: 'user' } },
  44. })
  45. return session
  46. }
  47. function toolConversation(): Session {
  48. const session = new Session(SessionId('tools'))
  49. for (let turn = 1; turn <= 3; turn += 1) {
  50. const callId = CallId(`call-${turn}`)
  51. session.append('turn/start', { turn, trigger: { kind: 'message', source: { kind: 'user' } } })
  52. session.append('user/message', {
  53. content: [{ type: 'text', text: `request ${turn} `.repeat(300) }],
  54. source: { kind: 'user' },
  55. }, { surfaceOp: 'append' })
  56. session.append('step/start', { turn, step: 1 })
  57. session.append('assistant/message', {
  58. provenance: { provider: MODEL, model: MODEL },
  59. turn,
  60. step: 1,
  61. content: [
  62. { type: 'text', text: `calling ${turn} `.repeat(300) },
  63. { type: 'tool-call', id: callId, name: 'read', arguments: '{}' },
  64. ],
  65. }, { surfaceOp: 'append' })
  66. session.append('tool/call', { turn, step: 1, callId, name: 'read', arguments: '{}' })
  67. session.append('tool/result', {
  68. turn,
  69. step: 1,
  70. callId,
  71. content: [{ type: 'text', text: `result ${turn} `.repeat(300) }],
  72. isError: false,
  73. }, { surfaceOp: 'append' })
  74. session.append('step/end', { turn, step: 1 })
  75. session.append('turn/end', { turn, reason: { kind: 'completed' } })
  76. }
  77. session.append('turn/start', { turn: 4, trigger: { kind: 'message', source: { kind: 'user' } } })
  78. return session
  79. }
  80. class TestCompactService extends BasicCompactService {
  81. summary: ContentBlock[] = [{ type: 'text', text: 'small checkpoint' }]
  82. summaryProvider = 'summary-provider'
  83. summaryModel = 'summary-model'
  84. error: unknown
  85. mutateDuringSummary: (() => void) | undefined
  86. calls: Array<{ text: string; signal: AbortSignal | undefined }> = []
  87. override async summarize(
  88. text: string,
  89. _agent: Agent,
  90. signal?: AbortSignal,
  91. ): Promise<{ summary: ContentBlock[]; provider: string; model: string; maxTokens?: number }> {
  92. this.calls.push({ text, signal })
  93. this.mutateDuringSummary?.()
  94. if (this.error !== undefined) throw this.error
  95. return {
  96. summary: this.summary,
  97. provider: this.summaryProvider,
  98. model: this.summaryModel,
  99. maxTokens: 123,
  100. }
  101. }
  102. }
  103. function service(
  104. config: BasicCompactConfig = { auto: false },
  105. ctx = createContext(),
  106. ): TestCompactService {
  107. return new TestCompactService(ctx, config)
  108. }
  109. async function compactIfNeeded(
  110. compact: BasicCompactService,
  111. session: Session,
  112. model: string | undefined = MODEL,
  113. system = '',
  114. prefix: readonly Message[] = [],
  115. ): Promise<CompactionResult | null> {
  116. return compact.compactIfNeeded(agent(session, model), system, prefix, SIGNAL)
  117. }
  118. describe('compact configuration and defaults', () => {
  119. it('uses low-friction service-wide defaults', () => {
  120. const ctx = createContext()
  121. const resolved = resolveConfig({}, ctx.tokenMeter)
  122. expect(resolved).toEqual({
  123. thresholdRatio: 0.8,
  124. retainTokens: 160,
  125. summarizationProvider: '',
  126. summarizationModel: '',
  127. maxTokens: 8192,
  128. compactionRetries: 1,
  129. auto: true,
  130. })
  131. expect(Object.isFrozen(resolved)).toBe(true)
  132. })
  133. it('resolves threshold and retention overrides independently', () => {
  134. const ctx = createContext()
  135. const thresholdOnly = resolveConfig({
  136. thresholdRatio: 0.5,
  137. }, ctx.tokenMeter)
  138. expect(thresholdOnly).toMatchObject({
  139. thresholdRatio: 0.5,
  140. retainTokens: 160,
  141. })
  142. const retentionOnly = resolveConfig({
  143. retainTokens: 70,
  144. }, ctx.tokenMeter)
  145. expect(retentionOnly).toMatchObject({
  146. thresholdRatio: 0.8,
  147. retainTokens: 70,
  148. })
  149. })
  150. it('validates common values and pressure-policy invariants', () => {
  151. const ctx = createContext()
  152. const bad = [
  153. [{ maxTokens: 0 }, /maxTokens/],
  154. [{ compactionRetries: -1 }, /compactionRetries/],
  155. [{ auto: 'yes' }, /auto must be a boolean/],
  156. [{ summarizationProvider: 1 }, /summarizationProvider must be a string/],
  157. [{ summarizationModel: 1 }, /summarizationModel must be a string/],
  158. [{ summarizationProvider: MODEL }, /must both be set or both be empty/],
  159. [{ summarizationModel: MODEL }, /must both be set or both be empty/],
  160. [{ thresholdRatio: 0 }, /number in \(0, 1\]/],
  161. [{ thresholdRatio: 1.1 }, /number in \(0, 1\]/],
  162. [{ retainTokens: -1 }, /non-negative integer/],
  163. [{ thresholdRatio: 0.5, retainTokens: 500 }, /less than threshold/],
  164. [{ models: { [MODEL]: { retainTokens: 10 } } }, /BasicCompactConfig: unknown key "models"/],
  165. [{ thresholdRato: 0.5 }, /BasicCompactConfig: unknown key "thresholdRato"/],
  166. ] as Array<[unknown, RegExp]>
  167. for (const [config, pattern] of bad) {
  168. expect(() => resolveConfig(config as BasicCompactConfig, ctx.tokenMeter)).toThrow(pattern)
  169. }
  170. })
  171. })
  172. describe('pressure measurement and retention', () => {
  173. const compactConfig: BasicCompactConfig = {
  174. auto: false,
  175. thresholdRatio: 0.5,
  176. retainTokens: 180,
  177. }
  178. it('skips the provisional check only when no routed or fallback model exists', async () => {
  179. const compact = service(compactConfig)
  180. const session = conversation()
  181. expect(await compact.compactIfNeeded(agent(session), '', [], SIGNAL)).toBeNull()
  182. expect(compact.calls).toHaveLength(0)
  183. })
  184. it('meters any routed model without profile resolution', async () => {
  185. const compact = service(compactConfig)
  186. await expect(compactIfNeeded(compact, conversation(), 'unlisted-model'))
  187. .resolves.not.toBeNull()
  188. })
  189. it('does nothing below threshold and compacts a priced head above threshold', async () => {
  190. const compact = service(compactConfig)
  191. expect(await compactIfNeeded(compact, conversation(2))).toBeNull()
  192. const session = conversation(4)
  193. const result = await compactIfNeeded(compact, session)
  194. expect(result).not.toBeNull()
  195. expect(result?.shadowedSeqs.length).toBeGreaterThan(2)
  196. expect(session.surface.nodes.length).toBeLessThan(8)
  197. })
  198. it('counts the current prompt and request prefix without putting either on the surface', async () => {
  199. const compact = service({
  200. auto: false,
  201. thresholdRatio: 0.7,
  202. retainTokens: 50,
  203. })
  204. const session = conversation(2, 'x'.repeat(200))
  205. expect(await compactIfNeeded(compact, session)).toBeNull()
  206. const prefix: Message[] = [{
  207. role: 'user',
  208. content: [{ type: 'text', text: 'p'.repeat(1_000) }],
  209. }]
  210. const result = await compactIfNeeded(compact, session, MODEL, 's'.repeat(1_000), prefix)
  211. expect(result).not.toBeNull()
  212. expect(prefix).toHaveLength(1)
  213. expect(session.events.some(event => event.type === 'context/message')).toBe(false)
  214. })
  215. it('uses the latest logged routed model in the provisional request envelope', async () => {
  216. const ctx = createContext()
  217. const compact = service({
  218. auto: false,
  219. thresholdRatio: 0.5,
  220. retainTokens: 180,
  221. }, ctx)
  222. const session = conversation(4)
  223. session.append('request/header', {
  224. header: { config: { provider: 'actual', model: 'actual' } },
  225. reason: 'initial',
  226. })
  227. const measure = vi.spyOn(ctx.tokenMeter, 'measure')
  228. const result = await compactIfNeeded(compact, session, 'fallback')
  229. expect(result).not.toBeNull()
  230. expect(measure.mock.calls[0]?.[1]?.config.provider).toBe('actual')
  231. expect(measure.mock.calls[0]?.[1]?.config.model).toBe('actual')
  232. })
  233. it('declines when envelope pressure is high but the surface has no compactable range', async () => {
  234. const compact = service(compactConfig)
  235. const empty = new Session(SessionId('empty'))
  236. expect(await compactIfNeeded(compact, empty, MODEL, 'x'.repeat(100_000))).toBeNull()
  237. const retained = conversation(1)
  238. expect(await compactIfNeeded(compact, retained, MODEL, 'x'.repeat(100_000))).toBeNull()
  239. })
  240. it('uses one unified measurement for each pressure-and-retention decision', async () => {
  241. const ctx = createContext()
  242. const compact = service(compactConfig, ctx)
  243. const measure = vi.spyOn(ctx.tokenMeter, 'measure')
  244. const stop = new Error('stop after first decision')
  245. vi.spyOn(compact, 'compactRegion').mockRejectedValueOnce(stop)
  246. await expect(compactIfNeeded(compact, conversation(4))).rejects.toBe(stop)
  247. expect(measure).toHaveBeenCalledTimes(1)
  248. })
  249. it('bounds retries when a shrinking checkpoint remains above threshold', async () => {
  250. const compact = service({
  251. auto: false,
  252. compactionRetries: 0,
  253. thresholdRatio: 0.3,
  254. retainTokens: 180,
  255. })
  256. compact.summary = Array.from({ length: 7 }, (_, index) => ({
  257. type: 'text',
  258. text: `summary ${index}`,
  259. }))
  260. await expect(compactIfNeeded(compact, conversation(4)))
  261. .rejects.toThrow(/still above threshold after 1 compaction attempts/)
  262. })
  263. it('rounds a retention cut head-ward to preserve tool-call/result pairing', async () => {
  264. const compact = service({
  265. auto: false,
  266. thresholdRatio: 0.8,
  267. retainTokens: 80,
  268. }, createContext(4_000))
  269. const session = toolConversation()
  270. const result = await compactIfNeeded(compact, session)
  271. expect(result).not.toBeNull()
  272. const messages = session.deriveMessages()
  273. const calls = new Set<string>()
  274. for (const message of messages) {
  275. for (const block of message.content) {
  276. if (block.type === 'tool-call') calls.add(block.id)
  277. if (block.type === 'tool-result') expect(calls.has(block.toolCallId)).toBe(true)
  278. }
  279. }
  280. })
  281. it('rejects a priced surface that is not the current positional surface', () => {
  282. const ctx = createContext()
  283. const session = conversation(2)
  284. const priced = ctx.tokenMeter.measure(session)
  285. expect(() => selectCompactableRange(session, {
  286. ...priced,
  287. nodes: priced.nodes.slice(1),
  288. }, 1)).toThrow(/does not match/)
  289. })
  290. it('declines when rounding a cut would consume the only tool pair', () => {
  291. const ctx = createContext()
  292. const session = new Session(SessionId('one-tool-pair'))
  293. const callId = CallId('only')
  294. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  295. session.append('step/start', { turn: 1, step: 1 })
  296. session.append('assistant/message', {
  297. provenance: { provider: MODEL, model: MODEL },
  298. turn: 1,
  299. step: 1,
  300. content: [{ type: 'tool-call', id: callId, name: 'read', arguments: '{}' }],
  301. }, { surfaceOp: 'append' })
  302. session.append('tool/call', { turn: 1, step: 1, callId, name: 'read', arguments: '{}' })
  303. session.append('tool/result', {
  304. turn: 1,
  305. step: 1,
  306. callId,
  307. content: [{ type: 'text', text: 'result' }],
  308. isError: false,
  309. }, { surfaceOp: 'append' })
  310. session.append('step/end', { turn: 1, step: 1 })
  311. const priced = ctx.tokenMeter.measure(session)
  312. expect(selectCompactableRange(session, priced, 1)).toBeNull()
  313. })
  314. })
  315. describe('compaction region transaction', () => {
  316. it('rejects an agent that does not own the exact target session before mutation', async () => {
  317. const compact = service()
  318. const target = conversation(2)
  319. const owner = conversation(1)
  320. const targetEvents = [...target.events]
  321. const ownerEvents = [...owner.events]
  322. const nodes = target.surface.nodes
  323. await expect(compact.compactRegion(
  324. target,
  325. nodes[0]!,
  326. nodes[1]!,
  327. agent(owner),
  328. )).rejects.toThrow('compactRegion: agent.session must be the exact target session')
  329. expect(target.events).toEqual(targetEvents)
  330. expect(owner.events).toEqual(ownerEvents)
  331. expect(compact.calls).toEqual([])
  332. })
  333. it('lands a framed, replayable checkpoint with exact pricing provenance', async () => {
  334. const compact = service()
  335. const session = conversation(3)
  336. const before = session.surface.nodes
  337. const result = await compact.compactRegion(
  338. session,
  339. before[0]!,
  340. before[3]!,
  341. agent(session, MODEL),
  342. SIGNAL,
  343. )
  344. expect(result.shadowedSeqs).toEqual(before.slice(0, 4))
  345. expect(result.shadowedTokenCount).toBeGreaterThan(0)
  346. expect(compact.calls[0]).toMatchObject({ signal: SIGNAL })
  347. expect(compact.calls[0]?.text).toContain('fixture user 1')
  348. const summary = session.events.findLast(event => event.type === 'compact/summary')
  349. expect(summary?.data).toMatchObject({
  350. shadowedSeqs: result.shadowedSeqs,
  351. shadowedTokenCount: result.shadowedTokenCount,
  352. provider: 'summary-provider',
  353. model: 'summary-model',
  354. maxTokens: 123,
  355. })
  356. const head = session.deriveMessages()[0]!
  357. expect(head.content[0]?.type).toBe('text')
  358. expect(head.content[0]?.type === 'text' ? head.content[0].text : '').toContain('<compacted-summary>')
  359. expect(head.content.at(-1)).toEqual({ type: 'text', text: '</compacted-summary>' })
  360. const replay = new Session(SessionId('replay'), [...session.events])
  361. expect(replay.deriveMessages()).toEqual(session.deriveMessages())
  362. })
  363. it.each([
  364. ['start missing', 9_001, undefined, /start seq 9001 not found/],
  365. ['end missing', undefined, 9_002, /end seq 9002 not found/],
  366. ])('rejects %s', async (_label, startOverride, endOverride, pattern) => {
  367. const compact = service()
  368. const session = conversation(2)
  369. const nodes = session.surface.nodes
  370. await expect(compact.compactRegion(
  371. session,
  372. startOverride ?? nodes[0]!,
  373. endOverride ?? nodes[1]!,
  374. agent(session, MODEL),
  375. )).rejects.toThrow(pattern)
  376. })
  377. it('rejects reversed and tool-unbalanced positional boundaries', async () => {
  378. const compact = service()
  379. const plain = conversation(2)
  380. const nodes = plain.surface.nodes
  381. await expect(compact.compactRegion(
  382. plain,
  383. nodes[2]!,
  384. nodes[1]!,
  385. agent(plain, MODEL),
  386. )).rejects.toThrow(/is after end/)
  387. const tools = toolConversation()
  388. const toolNodes = tools.surface.nodes
  389. await expect(compact.compactRegion(
  390. tools,
  391. toolNodes[2]!,
  392. toolNodes[4]!,
  393. agent(tools, MODEL),
  394. )).rejects.toThrow(/start seq .* not a balanced boundary/)
  395. await expect(compact.compactRegion(
  396. tools,
  397. toolNodes[0]!,
  398. toolNodes[1]!,
  399. agent(tools, MODEL),
  400. )).rejects.toThrow(/end seq .* not a balanced boundary/)
  401. })
  402. it('requires an open turn and an idle compaction bracket', async () => {
  403. const compact = service()
  404. const closed = conversation(1)
  405. closed.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
  406. const nodes = closed.surface.nodes
  407. await expect(compact.compactRegion(
  408. closed,
  409. nodes[0]!,
  410. nodes[1]!,
  411. agent(closed, MODEL),
  412. )).rejects.toThrow(/no open turn/)
  413. const locked = conversation(1)
  414. locked.append('compact/start', { turn: 2 })
  415. const lockedNodes = locked.surface.nodes
  416. await expect(compact.compactRegion(
  417. locked,
  418. lockedNodes[0]!,
  419. lockedNodes[1]!,
  420. agent(locked, MODEL),
  421. )).rejects.toThrow(/already in progress/)
  422. })
  423. it('rejects a session with no turn boundary at all', async () => {
  424. const compact = service()
  425. const session = new Session(SessionId('turnless'))
  426. session.append('user/message', {
  427. content: [{ type: 'text', text: 'orphan' }],
  428. source: { kind: 'user' },
  429. }, { surfaceOp: 'append' })
  430. const node = session.surface.nodes[0]!
  431. await expect(compact.compactRegion(
  432. session,
  433. node,
  434. node,
  435. agent(session, MODEL),
  436. )).rejects.toThrow(/no open turn/)
  437. })
  438. it('rejects a meter snapshot that changed before summarization began', async () => {
  439. const ctx = createContext()
  440. const meter = ctx.tokenMeter
  441. const original = meter.measure.bind(meter)
  442. vi.spyOn(meter, 'measure').mockImplementationOnce((session) => {
  443. const measurement = original(session)
  444. return { ...measurement, nodes: measurement.nodes.slice(1) }
  445. })
  446. const compact = service({ auto: false }, ctx)
  447. const session = conversation(2)
  448. const nodes = session.surface.nodes
  449. await expect(compact.compactRegion(
  450. session,
  451. nodes[0]!,
  452. nodes[2]!,
  453. agent(session, MODEL),
  454. )).rejects.toThrow(/selected surface changed/)
  455. })
  456. it('records summarizer failures without mutating the surface', async () => {
  457. const compact = service()
  458. compact.error = new Error('summary unavailable')
  459. const session = conversation(2)
  460. const before = session.surface.nodes
  461. await expect(compact.compactRegion(
  462. session,
  463. before[0]!,
  464. before[2]!,
  465. agent(session, MODEL),
  466. )).rejects.toThrow('summary unavailable')
  467. expect(session.surface.nodes).toEqual(before)
  468. expect(session.events.findLast(event => event.type === 'compact/end')?.data)
  469. .toMatchObject({ error: 'summary unavailable' })
  470. })
  471. it('stringifies non-Error failures in the durable end bracket', async () => {
  472. const compact = service()
  473. compact.error = 'plain failure'
  474. const session = conversation(2)
  475. const nodes = session.surface.nodes
  476. await expect(compact.compactRegion(
  477. session,
  478. nodes[0]!,
  479. nodes[2]!,
  480. agent(session, MODEL),
  481. )).rejects.toBe('plain failure')
  482. expect(session.events.findLast(event => event.type === 'compact/end')?.data)
  483. .toMatchObject({ error: 'plain failure' })
  484. })
  485. it('rejects concurrent durable appends before committing the replacement', async () => {
  486. const compact = service()
  487. const session = conversation(2)
  488. compact.mutateDuringSummary = () => {
  489. session.append('request/header', {
  490. header: { config: { provider: MODEL, model: MODEL } },
  491. reason: 'initial',
  492. })
  493. }
  494. const nodes = session.surface.nodes
  495. await expect(compact.compactRegion(
  496. session,
  497. nodes[0]!,
  498. nodes[2]!,
  499. agent(session, MODEL),
  500. )).rejects.toThrow(/session log changed/)
  501. expect(session.events.some(event => event.type === 'compact/summary')).toBe(false)
  502. })
  503. it('rejects a non-shrinking framed summary under the conversation meter', async () => {
  504. const compact = service()
  505. compact.summary = Array.from({ length: 100 }, (_, index) => ({
  506. type: 'text',
  507. text: `verbose ${index}`,
  508. }))
  509. const session = conversation(2)
  510. const nodes = session.surface.nodes
  511. await expect(compact.compactRegion(
  512. session,
  513. nodes[0]!,
  514. nodes[2]!,
  515. agent(session, MODEL),
  516. )).rejects.toThrow(/summary is not smaller/)
  517. expect(session.events.some(event => event.type === 'compact/summary')).toBe(false)
  518. })
  519. it('lets a model-independent custom summarizer compact without a conversation model', async () => {
  520. const compact = service()
  521. const session = conversation(1)
  522. const nodes = session.surface.nodes
  523. await expect(compact.compactRegion(
  524. session,
  525. nodes[0]!,
  526. nodes[1]!,
  527. agent(session),
  528. )).resolves.toMatchObject({ shadowedSeqs: [nodes[0]!, nodes[1]!] })
  529. })
  530. })
  531. class ScriptedAdapter extends LlmAdapter {
  532. lastOptions: GenerateOptions | undefined
  533. constructor(
  534. private readonly blocks: readonly ContentBlock[],
  535. private readonly finish: (StreamChunk & { type: 'finish' })['reason'] = { kind: 'stop' },
  536. ) {
  537. super()
  538. }
  539. override async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
  540. this.lastOptions = options
  541. for (const [index, block] of this.blocks.entries()) {
  542. yield { type: 'block-start', index, blockType: block.type }
  543. if (block.type === 'text') {
  544. yield { type: 'text-delta', index, text: block.text }
  545. } else if (block.type === 'reasoning') {
  546. yield { type: 'reasoning-delta', index, text: block.text }
  547. } else {
  548. yield { type: 'block-end', index, block }
  549. }
  550. }
  551. yield { type: 'finish', reason: this.finish }
  552. }
  553. }
  554. async function summarizerHarness(
  555. blocks: readonly ContentBlock[],
  556. finish?: (StreamChunk & { type: 'finish' })['reason'],
  557. model = MODEL,
  558. config: BasicCompactConfig = { auto: false },
  559. ): Promise<{ ctx: Context; adapter: ScriptedAdapter; compact: BasicCompactService }> {
  560. const ctx = new Context()
  561. await ctx.plugin(LlmService)
  562. void new TokenMeterService(ctx, { contextWindow: 1_000 })
  563. const adapter = new ScriptedAdapter(blocks, finish)
  564. ctx.llm.registerAdapter([model], adapter)
  565. const compact = new BasicCompactService(ctx, config)
  566. return { ctx, adapter, compact }
  567. }
  568. describe('default one-shot summarizer', () => {
  569. it('uses configured model/default cap, forwards cancellation, and keeps only safe text', async () => {
  570. const { adapter, compact } = await summarizerHarness([
  571. { type: 'reasoning', text: 'private' },
  572. { type: 'text', text: 'public summary' },
  573. { type: 'tool-call', id: CallId('unexpected'), name: 'x', arguments: '{}' },
  574. ], undefined, MODEL, {
  575. auto: false,
  576. summarizationProvider: MODEL,
  577. summarizationModel: MODEL,
  578. maxTokens: 321,
  579. })
  580. const session = conversation(1)
  581. const output = await compact.summarize('transcript', agent(session, 'fallback'), SIGNAL)
  582. expect(output).toEqual({
  583. summary: [{ type: 'text', text: 'public summary' }],
  584. provider: MODEL,
  585. model: MODEL,
  586. maxTokens: 321,
  587. })
  588. expect(adapter.lastOptions).toMatchObject({
  589. provider: MODEL,
  590. model: MODEL,
  591. maxTokens: 321,
  592. signal: SIGNAL,
  593. sessionId: session.id,
  594. })
  595. expect(adapter.lastOptions?.system).toContain('## Primary Request and Intent')
  596. })
  597. it('resolves the latest routed provider/model before the AgentOptions pair', async () => {
  598. const { adapter, compact } = await summarizerHarness([{ type: 'text', text: 'summary' }], undefined, 'routed')
  599. const session = conversation(1)
  600. session.append('request/header', {
  601. header: { config: { provider: 'routed', model: 'routed' } },
  602. reason: 'initial',
  603. })
  604. const output = await compact.summarize('history', agent(session, 'fallback'))
  605. expect(output.provider).toBe('routed')
  606. expect(output.model).toBe('routed')
  607. expect(adapter.lastOptions?.provider).toBe('routed')
  608. expect(adapter.lastOptions?.model).toBe('routed')
  609. })
  610. it('fails clearly when no complete summarization target can be resolved', async () => {
  611. const ctx = new Context()
  612. await ctx.plugin(LlmService)
  613. void new TokenMeterService(ctx)
  614. const compact = new BasicCompactService(ctx, { auto: false })
  615. await expect(compact.summarize('history', agent(new Session(SessionId('model-less')))))
  616. .rejects.toThrow(/no provider\/model available for summarization/)
  617. })
  618. it.each([
  619. [{ kind: 'error', message: 'provider failed', code: 'PROVIDER' }, 'PROVIDER', /provider failed/],
  620. [{ kind: 'error', message: 'opaque' }, undefined, /opaque/],
  621. [{ kind: 'aborted' }, 'ABORTED', /aborted/],
  622. [{ kind: 'max-tokens' }, 'MAX_TOKENS', /token cap/],
  623. ] as Array<[(StreamChunk & { type: 'finish' })['reason'], string | undefined, RegExp]>) (
  624. 'rejects terminal finish %#',
  625. async (finish, code, pattern) => {
  626. const { compact } = await summarizerHarness([], finish)
  627. let thrown: unknown
  628. try {
  629. await compact.summarize('history', agent(conversation(1), MODEL))
  630. } catch (error: unknown) {
  631. thrown = error
  632. }
  633. expect(thrown).toBeInstanceOf(Error)
  634. expect((thrown as Error).message).toMatch(pattern)
  635. expect((thrown as Error & { code?: string }).code).toBe(code)
  636. },
  637. )
  638. it('rejects empty or reasoning-only successful output', async () => {
  639. const { compact } = await summarizerHarness([{ type: 'reasoning', text: 'private' }])
  640. await expect(compact.summarize('history', agent(conversation(1), MODEL)))
  641. .rejects.toThrow(/no text summary content/)
  642. })
  643. })
  644. describe('automatic listener and loader composition', () => {
  645. function preStep(ctx: Context, owner: Agent): Promise<unknown> {
  646. return ctx.serial('agent/pre-step', owner, 1, 1, '', [], SIGNAL)
  647. }
  648. it('compacts above threshold and remains idle below it', async () => {
  649. const ctx = createContext()
  650. const compact = new TestCompactService(ctx, {
  651. thresholdRatio: 0.5,
  652. retainTokens: 180,
  653. })
  654. const pressured = conversation(4)
  655. await preStep(ctx, agent(pressured, MODEL))
  656. expect(pressured.events.some(event => event.type === 'compact/summary')).toBe(true)
  657. const small = conversation(1)
  658. await preStep(ctx, agent(small, MODEL))
  659. expect(small.events.some(event => event.type === 'compact/start')).toBe(false)
  660. expect(compact.calls).toHaveLength(1)
  661. })
  662. it('warns and continues after operational failures, including non-Errors', async () => {
  663. const ctx = createContext()
  664. const warnings: string[] = []
  665. ctx.logger.warn = ((message: string) => void warnings.push(message)) as typeof ctx.logger.warn
  666. const compact = new TestCompactService(ctx, {
  667. thresholdRatio: 0.5,
  668. retainTokens: 180,
  669. })
  670. compact.error = 'temporary failure'
  671. const session = conversation(4)
  672. await expect(preStep(ctx, agent(session, MODEL))).resolves.toBeUndefined()
  673. expect(warnings).toContainEqual(expect.stringContaining('temporary failure'))
  674. expect(session.events.some(event => event.type === 'compact/summary')).toBe(false)
  675. })
  676. it('auto:false installs no listener', async () => {
  677. const ctx = createContext()
  678. void new TestCompactService(ctx, {
  679. auto: false,
  680. thresholdRatio: 0.5,
  681. retainTokens: 180,
  682. })
  683. const session = conversation(4)
  684. await preStep(ctx, agent(session, MODEL))
  685. expect(session.events.some(event => event.type === 'compact/start')).toBe(false)
  686. })
  687. it('loads and disposes the real zero-config service stack', async () => {
  688. const ctx = new Context()
  689. await ctx.plugin(LlmService)
  690. const meterFiber = await ctx.plugin(TokenMeterService)
  691. const compactFiber = await ctx.plugin(BasicCompactService, { auto: false })
  692. expect(ctx.tokenMeter.contextWindow).toBe(128_000)
  693. expect(ctx.get('compact')).toBeInstanceOf(BasicCompactService)
  694. await compactFiber.dispose()
  695. expect(ctx.get('compact')).toBeUndefined()
  696. await meterFiber.dispose()
  697. expect(ctx.get('tokenMeter')).toBeUndefined()
  698. })
  699. it('removes its automatic listener with the plugin fiber', async () => {
  700. const ctx = new Context()
  701. await ctx.plugin(LlmService)
  702. await ctx.plugin(TokenMeterService, { contextWindow: 1_000 })
  703. const fiber = await ctx.plugin(TestCompactService, {
  704. thresholdRatio: 0.5,
  705. retainTokens: 180,
  706. })
  707. await fiber.dispose()
  708. const session = conversation(4)
  709. await preStep(ctx, agent(session, MODEL))
  710. expect(session.events.some(event => event.type === 'compact/start')).toBe(false)
  711. })
  712. })