index.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /**
  2. * `BasicCompactService`: the first implementation of the
  3. * `@deepseek-ai/dsh-compact` seam. It owns the entire compaction strategy:
  4. *
  5. * - **Token estimation** — chars/`charsPerToken` heuristic (config, default 4)
  6. * with per-block structural overhead.
  7. * - **Retention policy** — walk surface nodes tail→head, keep recent nodes up
  8. * to a token budget, compact everything older. The cutoff is snapped forward
  9. * to the next balanced tool-pairing boundary so a compacted region never
  10. * splits a step's tool-call/result pair (an open tail step is never crossed —
  11. * compaction declines and retries once it closes).
  12. * - **Summarization** — `ctx.llm.stream()` assembled via `BlockAssembler`
  13. * (the single model-call surface; same path the loop uses) with a fixed
  14. * condense-the-history system prompt routed through `agent/request`.
  15. * - **Surface mutation** — a single `user/message` replace node carries the
  16. * summary; `compact/*` events are log-only lock + provenance records.
  17. * - **Auto-compaction** — an `agent/pre-step` listener delegates to
  18. * {@link BasicCompactService.compactIfNeeded} before EVERY step (so a
  19. * tool-heavy turn that grows the surface mid-turn still compacts); it owns the
  20. * sole token-pressure check.
  21. *
  22. * A different backend (real tokenizer, template summarizer, turn-count
  23. * retention) either subclasses this and overrides the {@link
  24. * BasicCompactService.estimateContentTokens} / {@link
  25. * BasicCompactService.summarize} hooks, or implements the abstract
  26. * {@link CompactService} from scratch.
  27. *
  28. * @module @deepseek-ai/dsh-compact-basic
  29. */
  30. import { Context } from 'cordis'
  31. import { CompactService } from '@deepseek-ai/dsh-compact'
  32. import type { CompactionResult } from '@deepseek-ai/dsh-compact'
  33. import { BlockAssembler } from '@deepseek-ai/dsh-llm'
  34. import type { ContentBlock, FinishReason, GenerateOptions, Message } from '@deepseek-ai/dsh-llm'
  35. import type { Session, SessionEvent } from '@deepseek-ai/dsh-session'
  36. import { isToolPairingBalanced } from '@deepseek-ai/dsh-session'
  37. import type { Agent } from '@deepseek-ai/dsh-agent'
  38. import type { BasicCompactConfig, ResolvedConfig } from './types.ts'
  39. import { resolveConfig } from './types.ts'
  40. export type { BasicCompactConfig, ResolvedConfig } from './types.ts'
  41. export { resolveConfig } from './types.ts'
  42. /** Per-block structural overhead for JSON framing / type tag. */
  43. const BLOCK_OVERHEAD = 4
  44. /** Heuristic token count for an image block (~85 tokens for low-res URL). */
  45. const IMAGE_TOKEN_COST = 85
  46. /** Role-field framing overhead added per message in {@link BasicCompactService.estimateTokens}. */
  47. const ROLE_OVERHEAD = 4
  48. /** Tags wrapping the structured summary inside the landed checkpoint node. */
  49. const SUMMARY_OPEN_TAG = '<compacted-summary>'
  50. const SUMMARY_CLOSE_TAG = '</compacted-summary>'
  51. /**
  52. * The summarization system prompt: instructs the model to condense the
  53. * conversation into a fixed, fully-populated structure rather than freeform
  54. * bullets. The fixed structure guarantees coverage of the things a resuming
  55. * model needs (original intent, pending work, the next step, critical context)
  56. * and is stable across compaction cycles, so a prior checkpoint can be merged
  57. * in place. The final rule keys off {@link SUMMARY_OPEN_TAG}: when the
  58. * transcript already contains a prior checkpoint, the model consolidates rather
  59. * than re-summarizing it verbatim (a cheap incremental-merge that needs no
  60. * extra log/event machinery — the tag travels on the summary surface node).
  61. */
  62. const SUMMARIZE_SYSTEM_PROMPT = [
  63. 'You are a compaction engine for an AI coding assistant. Condense the conversation transcript into a structured checkpoint that lets another model resume the work with no loss of essential context.',
  64. '',
  65. 'Output EXACTLY the Markdown structure below: keep every section, in order. Use terse bullets, not prose paragraphs. Write "(none)" for an empty section — never drop a section.',
  66. '',
  67. '## Primary Request and Intent',
  68. "- [the user's original and evolving goals; quote verbatim where the exact wording matters]",
  69. '',
  70. '## Key Technical Concepts',
  71. '- [technologies, frameworks, patterns, and conventions in play]',
  72. '',
  73. '## Files and Code',
  74. '- [exact path: why it matters, key changes or snippets]',
  75. '',
  76. '## Errors and Fixes',
  77. '- [error: how it was resolved, plus any related user feedback]',
  78. '',
  79. '## Pending Tasks',
  80. '- [explicitly requested work not yet completed]',
  81. '',
  82. '## Current Work',
  83. '- [precisely what was in progress at this checkpoint]',
  84. '',
  85. '## Next Step',
  86. '- [the single next action, directly in line with the most recent request, or "(none)"]',
  87. '',
  88. '## Critical Context',
  89. '- [decisions and their rationale, constraints, user preferences, open questions, data needed to continue]',
  90. '',
  91. 'Rules:',
  92. '- Preserve exact file paths, commands, error strings, identifiers, and function signatures.',
  93. '- Capture user feedback and explicit instructions faithfully, especially corrections.',
  94. '- Do NOT mention this summarization process or that the context was compacted.',
  95. `- If the transcript already contains a ${SUMMARY_OPEN_TAG} block, it is a PRIOR checkpoint. Do not copy it forward verbatim: preserve still-true facts, drop stale ones, and merge newer information into a single consolidated summary under the same structure.`,
  96. ].join('\n')
  97. /**
  98. * Framing prepended to the landed summary so a resuming model reads it as a
  99. * checkpoint rather than a fresh user request, and continues the task from it.
  100. * It summarizes an earlier span of the conversation; the messages that follow
  101. * are the continuation. Because region compaction can be invoked manually, a
  102. * surface may hold several checkpoints, so the framing does NOT claim that
  103. * everything after it is recent or verbatim — only that the captured context
  104. * should be built on, not restated.
  105. */
  106. const CHECKPOINT_PREAMBLE =
  107. 'This is an automatically generated checkpoint condensing an earlier span of the conversation to free up context. Treat the captured context as established background and build on it without restating it. Continue the task directly from the messages that follow, without acknowledging this checkpoint.'
  108. /**
  109. * Map a terminal `FinishReason` to the error a SUMMARIZATION must throw, or
  110. * `undefined` for an acceptable finish. `FinishReason` is merge-extensible.
  111. *
  112. * Compaction fails CLOSED on a truncated summary: `error`, `aborted`, AND
  113. * `max-tokens` all raise. Unlike an ordinary agent turn — where `max-tokens` is
  114. * a normal "the model hit its budget" outcome the loop keeps — a summary cut off
  115. * at the token cap is an INCOMPLETE checkpoint, and committing it would shadow
  116. * (discard) the real history it summarizes. Raising here keeps the original
  117. * surface intact (the caller appends `compact/end` with the error and the auto
  118. * path proceeds with full history). `stop`/future kinds are accepted.
  119. */
  120. function finishError(finish: FinishReason): Error | undefined {
  121. switch (finish.kind) {
  122. case 'error': {
  123. const error = new Error(finish.message) as Error & { code?: string }
  124. if (finish.code !== undefined) error.code = finish.code
  125. return error
  126. }
  127. case 'aborted': {
  128. const error = new Error('summarization stream aborted') as Error & { code?: string }
  129. error.code = 'ABORTED'
  130. return error
  131. }
  132. case 'max-tokens': {
  133. const error = new Error('summarization truncated at the token cap (incomplete checkpoint)') as Error & { code?: string }
  134. error.code = 'MAX_TOKENS'
  135. return error
  136. }
  137. default:
  138. return undefined
  139. }
  140. }
  141. /**
  142. * Basic, dependency-light compaction backend: estimates the surface's token
  143. * footprint, summarizes the stale prefix through the model, and shadows it
  144. * behind a durable checkpoint. Every threshold/budget knob is required config
  145. * ({@link BasicCompactConfig}); the estimator's text density is the
  146. * `charsPerToken` knob.
  147. */
  148. export class BasicCompactService extends CompactService {
  149. static inject = ['llm']
  150. /** Resolved configuration (`auto` defaulted). */
  151. readonly config: ResolvedConfig
  152. constructor(ctx: Context, config: BasicCompactConfig) {
  153. super(ctx)
  154. this.config = resolveConfig(config)
  155. if (this.config.auto) {
  156. // Auto-compaction: delegate to compactIfNeeded before EVERY step. This is
  157. // LOAD-BEARING for runaway-turn survival: a tool-heavy ReAct turn appends
  158. // an assistant/message and a tool/result per step, so the surface (and the
  159. // derived token count) grows WITHIN a turn. The only moment to rescue a
  160. // turn that alone approaches the window is the next step's pre-step
  161. // checkpoint; gating to a turn's first step would let a runaway turn
  162. // overflow before the next turn's check. The listener owns NO threshold
  163. // logic — compactIfNeeded is the single place that decides whether to
  164. // compact, and its in-progress lock serializes concurrent attempts.
  165. //
  166. // It runs on `agent/pre-step` (a serial surface-mutation checkpoint fired
  167. // AFTER turn/start but BEFORE step/start), NOT `agent/request`: compaction
  168. // mutates the session surface, and the loop derives the request `messages`
  169. // AFTER this fires — so a single derive already reflects the compaction,
  170. // with no double-derive and no need to rewrite an already-assembled
  171. // `messages` array. Firing pre-step (outside any open step) keeps the
  172. // log-only `compact/*` records and the replacement node cleanly outside a
  173. // step, so a crash mid-compaction leaves an inert orphan the turn-repair
  174. // closes — never a half-open step.
  175. ctx.on('agent/pre-step', async (agent: Agent, turn: number, step: number, fullSystemPrompt: string, signal: AbortSignal) => {
  176. try {
  177. const result = await this.compactIfNeeded(agent, turn, step, fullSystemPrompt, signal)
  178. if (result) {
  179. const after = this.estimateTokens(agent.session.deriveMessages(), fullSystemPrompt)
  180. ctx.logger.info(
  181. `compaction: shadowed ${result.shadowedSeqs.length} surface nodes ` +
  182. `(seqs ${result.shadowedRange.start}-${result.shadowedRange.end}, ` +
  183. `~${result.shadowedTokenCount} tokens) ` +
  184. `→ ${after} estimated tokens after compaction`,
  185. )
  186. }
  187. } catch (error: unknown) {
  188. // A failed compaction must not prevent the model call — the surface is
  189. // untouched on failure, so the loop derives the full history and the
  190. // call proceeds.
  191. const msg = error instanceof Error ? error.message : String(error)
  192. ctx.logger.warn(`compaction failed: ${msg}; proceeding with full history`)
  193. }
  194. })
  195. }
  196. }
  197. // ---- Token estimation (overridable hooks) ----
  198. // TODO: chars/charsPerToken is a coarse heuristic. Replace with an exact
  199. // count — a real tokenizer, or the provider's post-response `usage` (input
  200. // tokens) fed back as a correction — so threshold decisions match the
  201. // model's actual budget.
  202. /**
  203. * Estimate the token count of content blocks — chars divided by the
  204. * `charsPerToken` config, with per-block overhead. Override in a subclass to
  205. * plug in a real tokenizer.
  206. */
  207. estimateContentTokens(blocks: readonly ContentBlock[]): number {
  208. const { charsPerToken } = this.config
  209. let tokens = 0
  210. for (const block of blocks) {
  211. switch (block.type) {
  212. case 'text':
  213. case 'reasoning':
  214. tokens += Math.ceil(block.text.length / charsPerToken) + BLOCK_OVERHEAD
  215. break
  216. case 'tool-call':
  217. tokens += Math.ceil(block.name.length / charsPerToken)
  218. + Math.ceil(block.arguments.length / charsPerToken)
  219. + BLOCK_OVERHEAD
  220. break
  221. case 'tool-result':
  222. tokens += this.estimateContentTokens(block.content) + BLOCK_OVERHEAD
  223. break
  224. case 'image':
  225. tokens += IMAGE_TOKEN_COST
  226. break
  227. default:
  228. // Unknown block types (merge-extensible ContentBlockMap):
  229. // estimate conservatively via JSON stringify.
  230. tokens += BLOCK_OVERHEAD + Math.ceil(JSON.stringify(block).length / charsPerToken)
  231. }
  232. }
  233. return tokens
  234. }
  235. /**
  236. * Estimate token count for a single session event. Returns 0 for non-message
  237. * event types (boundaries, chunks, usage, errors, compact markers).
  238. */
  239. estimateEventTokens(event: SessionEvent): number {
  240. switch (event.type) {
  241. case 'user/message':
  242. case 'assistant/message':
  243. case 'context/message':
  244. case 'steering/message':
  245. case 'tool/result':
  246. return this.estimateContentTokens(event.data.content)
  247. default:
  248. return 0
  249. }
  250. }
  251. /** Estimate total tokens across a list of messages plus optional system prompt. */
  252. estimateTokens(messages: readonly Message[], systemPrompt?: string): number {
  253. let total = 0
  254. for (const msg of messages) {
  255. total += this.estimateContentTokens(msg.content)
  256. total += ROLE_OVERHEAD
  257. }
  258. if (systemPrompt) total += Math.ceil(systemPrompt.length / this.config.charsPerToken)
  259. return total
  260. }
  261. /**
  262. * Summarize conversation text into content blocks via `agent/request` plus
  263. * `ctx.llm.stream()` assembled through a `BlockAssembler` (the single
  264. * model-call surface).
  265. * Override in a subclass for a template or remote summarizer.
  266. *
  267. * Honors the adapter failure contract: an adapter may report a model failure
  268. * by throwing from `stream()` (propagated here) OR by ending the stream with
  269. * a `finish {kind:'error'|'aborted'}` chunk — the latter is re-thrown so a
  270. * provider error never yields an empty summary.
  271. *
  272. * Forwards `signal` into `GenerateOptions.signal` so an abort/dispose tears
  273. * down the in-flight summarization rather than orphaning the model call.
  274. */
  275. async summarize(text: string, agent: Agent, turn: number, step: number, signal?: AbortSignal): Promise<ContentBlock[]> {
  276. const assembler = new BlockAssembler()
  277. const options: GenerateOptions = {
  278. model: this.config.summarizationModel || agent.options.model || '',
  279. messages: [{
  280. role: 'user',
  281. content: [{ type: 'text', text: `Summarize this conversation history:\n\n${text}\n\nSummary:` }],
  282. }],
  283. system: SUMMARIZE_SYSTEM_PROMPT,
  284. maxTokens: this.config.maxTokens,
  285. sessionId: agent.session.id,
  286. }
  287. // exactOptionalPropertyTypes: only set `signal` when present — assigning
  288. // `undefined` to an optional `signal?: AbortSignal` is a type error.
  289. if (signal) options.signal = signal
  290. const request = await this.ctx.waterfall('agent/request', agent, turn, step, options, () => Promise.resolve(options))
  291. if (!request.model) {
  292. throw new Error('no model available for summarization: set BasicCompactConfig.summarizationModel, AgentOptions.model, or supply one via the agent/request waterfall')
  293. }
  294. for await (const chunk of this.ctx.llm.stream(request)) {
  295. assembler.push(chunk)
  296. }
  297. const error = finishError(assembler.finish)
  298. if (error) throw error
  299. const summary = this._textOnly(assembler.message().content)
  300. if (!summary.some(block => block.type === 'text' && block.text.trim().length > 0)) {
  301. throw new Error('summarization produced no text summary content')
  302. }
  303. return summary
  304. }
  305. // ---- Core API (implements the abstract contract) ----
  306. /**
  307. * The sole token-pressure gate: estimate the current surface-derived history,
  308. * and if it exceeds the threshold (`contextWindow * thresholdRatio`), compact
  309. * the oldest surface nodes outside the `retainTokens` budget. The auto-
  310. * compaction listener delegates here rather than pre-checking, so this is the
  311. * only place the decision lives.
  312. *
  313. * Retention is a UNIFORM tail→head walk over the whole surface — turn
  314. * boundaries play NO role. Walking node-by-node from the tail and summing
  315. * token estimates, once the retained total reaches `retainTokens` the cutoff
  316. * is rounded to a balanced tool-pairing boundary: if the cut before the
  317. * retained node is unbalanced (an unanswered tool-call sits before it — i.e.
  318. * it is mid-step), the walk continues head-ward until the cut is balanced so
  319. * the whole step is retained (never splitting a step's tool-calls from their
  320. * results); if it stopped on a free node (a node belonging to no step), that
  321. * cut is already balanced. This always rounds toward retaining MORE (retained
  322. * ≥ `retainTokens`) and is boundary-safe by construction — no separate snap
  323. * pass.
  324. *
  325. * The compacted range is always anchored at the surface HEAD (`nodes[0]`):
  326. * auto-compaction re-consolidates any prior head checkpoint into one fresh
  327. * checkpoint. Declines (`null`) when nothing is over threshold, when the whole
  328. * surface fits the retain budget, or when no balanced cutoff exists in the
  329. * compactable range (its only content is an open tail step — retry once it
  330. * closes).
  331. */
  332. override async compactIfNeeded(
  333. agent: Agent,
  334. turn: number,
  335. step: number,
  336. fullSystemPrompt: string,
  337. signal: AbortSignal,
  338. ): Promise<CompactionResult | null> {
  339. const session = agent.session
  340. const threshold = Math.floor(this.config.contextWindow * this.config.thresholdRatio)
  341. let result: CompactionResult | null = null
  342. for (let attempt = 0; attempt <= this.config.compactionRetries; attempt++) {
  343. const totalTokens = this.estimateTokens(session.deriveMessages(), fullSystemPrompt)
  344. if (totalTokens < threshold) return result
  345. const range = this._compactableRange(session)
  346. if (range === null) {
  347. /* v8 ignore else -- defensive for non-standard subclass mutations; the concrete replace keeps a compactable head checkpoint. */
  348. if (result === null) return null
  349. /* v8 ignore next -- paired with the ignored defensive branch above. */
  350. break
  351. }
  352. result = await this.compactRegion(session, range.start, range.end, agent, turn, step, signal)
  353. }
  354. const totalTokens = this.estimateTokens(session.deriveMessages(), fullSystemPrompt)
  355. if (totalTokens < threshold) return result
  356. throw new Error(
  357. `compaction still above threshold after ${this.config.compactionRetries + 1} compaction attempts `
  358. + `(${totalTokens} estimated tokens >= threshold ${threshold})`,
  359. )
  360. }
  361. override async compactRegion(
  362. session: Session,
  363. start: number,
  364. end: number,
  365. agent: Agent,
  366. turn: number,
  367. step: number,
  368. signal?: AbortSignal,
  369. ): Promise<CompactionResult> {
  370. // Resolve the range by surface POSITION, not numeric seq interval. A prior
  371. // replace lands a fresh high-seq summary node AT the shadowed range's
  372. // position, so the surface order (head→tail) no longer tracks seq order —
  373. // `[newSummarySeq, olderRetainedSeq, …]` is normal. Indexing into the
  374. // ordered node list and slicing it is the only correct way to read a range;
  375. // a `node.seq >= start && node.seq <= end` interval test would mis-collect
  376. // nodes (and `start > end` would falsely reject) once that happens.
  377. const nodes = session.surface.nodes
  378. const startIdx = nodes.findIndex(n => n.seq === start)
  379. const endIdx = nodes.findIndex(n => n.seq === end)
  380. if (startIdx === -1) throw new Error(`compactRegion: start seq ${start} not found in surface`)
  381. if (endIdx === -1) throw new Error(`compactRegion: end seq ${end} not found in surface`)
  382. if (startIdx > endIdx) {
  383. throw new Error(`compactRegion: start seq ${start} (position ${startIdx}) is after end seq ${end} (position ${endIdx}) on the surface`)
  384. }
  385. // The region must never split a step's assistant-message tool-calls from
  386. // their tool/results (which would orphan one side and produce a transcript
  387. // every provider rejects). A region is safe iff BOTH its edges are balanced
  388. // cuts: the cut before `start`, and the cut after `end`. A node that belongs
  389. // to no step (pre-step user message, inter-step steering, injection context)
  390. // is a balanced (free) boundary; an `end` inside an open (unclosed) tail step
  391. // leaves the cut after it unbalanced (the open tool-call has no result yet),
  392. // so it is rejected. See dsh-session's tool-pairing balance check.
  393. const events = session.events
  394. if (!isToolPairingBalanced(nodes, events, start)) {
  395. throw new Error(`compactRegion: start seq ${start} is not a balanced boundary (would split a step's tool-call/result pair)`)
  396. }
  397. // The cut after `end` is named by `end`'s surface successor, or `null` when
  398. // `end` is the tail.
  399. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  400. const afterEnd: number | null = nodes[endIdx]!.next
  401. if (!isToolPairingBalanced(nodes, events, afterEnd)) {
  402. throw new Error(`compactRegion: end seq ${end} is not a balanced boundary (would split a step, or the step is still open)`)
  403. }
  404. if (this._isCompactionInProgress(session)) {
  405. throw new Error('compaction already in progress')
  406. }
  407. // Compaction's events (compact/* and the replacement user/message) must be
  408. // turn-enclosed: the session-log contract rejects any plugin event appended
  409. // outside an open turn. Auto-compaction satisfies this — it runs on the
  410. // `agent/pre-step` seam, after `turn/start` and before `step/start`, so
  411. // strictly inside the open turn (but outside any step). A manual call on a
  412. // fully-closed session has no turn to enclose the events, so reject rather
  413. // than emit an un-enclosed run.
  414. const openTurn = this._openTurn(session)
  415. if (openTurn === null) {
  416. throw new Error('compactRegion: no open turn — compaction events must be enclosed in a turn')
  417. }
  418. // Slice the ordered surface nodes [startIdx, endIdx] inclusive — the
  419. // shadowed range is positional, so this is the set the replace op covers.
  420. const shadowedSeqs = nodes.slice(startIdx, endIdx + 1).map(n => n.seq)
  421. // --- Acquire lock ---
  422. const startEvent = session.append('compact/start', { turn: openTurn })
  423. try {
  424. // --- Extract text and summarize ---
  425. const text = this._extractText(session, shadowedSeqs)
  426. const summary = await this.summarize(text, agent, turn, step, signal)
  427. // Estimate token count of the shadowed content for provenance.
  428. let shadowedTokenCount = 0
  429. for (const seq of shadowedSeqs) {
  430. // seq comes from a surface node — always a valid log index by construction.
  431. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  432. shadowedTokenCount += this.estimateEventTokens(session.events[seq]!)
  433. }
  434. const framedSummary = this._frameSummary(summary)
  435. const framedSummaryTokenCount = this.estimateContentTokens(framedSummary)
  436. if (framedSummaryTokenCount >= shadowedTokenCount) {
  437. throw new Error(
  438. `summary is not smaller than the shadowed content (${framedSummaryTokenCount} estimated framed tokens >= ${shadowedTokenCount})`,
  439. )
  440. }
  441. // --- Provenance record (log-only) ---
  442. const summaryEvent = session.append('compact/summary', {
  443. summary,
  444. shadowedRange: { start, end },
  445. shadowedSeqs,
  446. shadowedTokenCount,
  447. })
  448. // --- Surface replacement ---
  449. // The user/message directly shadows all compacted surface nodes with a
  450. // single replace op. It is the ONLY surface event in the compaction
  451. // sequence — compact/start, compact/summary, and compact/end are log-only
  452. // (surfaceOp is rejected by the compiler for non-SurfaceEventType).
  453. // The landed content is FRAMED (checkpoint preamble + tag-wrapped summary);
  454. // the compact/summary provenance event above holds the raw model output.
  455. session.append('user/message', {
  456. content: framedSummary,
  457. source: { kind: 'plugin', plugin: 'compact' },
  458. }, {
  459. surfaceOp: { op: 'replace', start, end },
  460. sourceEventSeqs: [startEvent.seq, summaryEvent.seq, ...shadowedSeqs],
  461. })
  462. // --- Release lock (log-only) ---
  463. // Appended LAST so the lock brackets the WHOLE operation: a crash between
  464. // compact/start and here leaves a detectable orphaned lock (a compact/start
  465. // with no matching compact/end) rather than a compact/end that falsely
  466. // claims compaction finished before the surface replacement landed.
  467. const endEvent = session.append('compact/end', { turn: openTurn })
  468. return {
  469. startSeq: startEvent.seq,
  470. summarySeq: summaryEvent.seq,
  471. endSeq: endEvent.seq,
  472. summary,
  473. shadowedRange: { start, end },
  474. shadowedSeqs,
  475. shadowedTokenCount,
  476. }
  477. } catch (error: unknown) {
  478. // Always release the lock — append compact/end with the error so a
  479. // wedged lock is impossible.
  480. const msg = error instanceof Error ? error.message : String(error)
  481. session.append('compact/end', { turn: openTurn, error: msg })
  482. throw error
  483. }
  484. }
  485. // ---- Internal helpers ----
  486. /**
  487. * Frame the raw summary blocks into the content that lands on the surface:
  488. * a checkpoint preamble (so a resuming model reads it as a checkpoint, not a
  489. * fresh user request) followed by the summary wrapped in
  490. * {@link SUMMARY_OPEN_TAG}/{@link SUMMARY_CLOSE_TAG}. The tags make a prior
  491. * checkpoint detectable in the transcript on the next compaction cycle, which
  492. * triggers the merge rule in the summarization prompt. The raw, unframed
  493. * `summary` is preserved separately on the `compact/summary` provenance event.
  494. */
  495. private _frameSummary(summary: readonly ContentBlock[]): ContentBlock[] {
  496. return [
  497. { type: 'text', text: `${CHECKPOINT_PREAMBLE}\n\n${SUMMARY_OPEN_TAG}` },
  498. ...summary,
  499. { type: 'text', text: SUMMARY_CLOSE_TAG },
  500. ]
  501. }
  502. /**
  503. * Whether a compaction is currently in progress for `session` — an unmatched
  504. * `compact/start` (no later `compact/end`) WITHIN the current turn.
  505. *
  506. * The scan is scoped to the current turn: walking back from the tail it stops
  507. * at the first `turn/end` (the boundary closing the prior turn). A
  508. * `compact/start` left orphaned by a crash mid-compaction lives in a turn that
  509. * persistence repair then closes with a synthetic `turn/end`; scoping here so
  510. * that a stale orphan from a PAST turn cannot wedge compaction forever (it sits
  511. * before the nearest `turn/end`, so the scan never reaches it). An in-progress
  512. * compaction's `compact/start` is always in the still-open current turn,
  513. * before any `turn/end`, so it is still detected.
  514. */
  515. private _isCompactionInProgress(session: Session): boolean {
  516. const events = session.events
  517. for (let i = events.length - 1; i >= 0; i--) {
  518. // Index bounded by i >= 0 and i < events.length — never undefined.
  519. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  520. const e = events[i]!
  521. if (e.type === 'compact/start') return true
  522. if (e.type === 'compact/end') break
  523. // A turn/end bounds the scan: anything before it belongs to a prior
  524. // (closed) turn and cannot be an in-progress compaction of THIS turn.
  525. if (e.type === 'turn/end') break
  526. }
  527. return false
  528. }
  529. /** Resolve the next head-anchored compactable surface range, or `null`. */
  530. private _compactableRange(session: Session): { start: number; end: number } | null {
  531. const nodes = session.surface.nodes
  532. if (nodes.length === 0) return null
  533. const events = session.events
  534. const retainBudget = this.config.retainTokens
  535. // Walk tail→head summing per-node token estimates. `keepFromIdx` is the
  536. // index of the OLDEST node we retain verbatim; everything strictly older
  537. // (`[0, keepFromIdx - 1]`) is the compactable range.
  538. let accumulated = 0
  539. let keepFromIdx = nodes.length // nothing retained yet
  540. for (let i = nodes.length - 1; i >= 0; i--) {
  541. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  542. const node = nodes[i]!
  543. const event = events[node.seq]
  544. /* v8 ignore next -- node.seq is a surface-node seq, always a valid log index by construction */
  545. if (event) accumulated += this.estimateEventTokens(event)
  546. keepFromIdx = i
  547. if (accumulated >= retainBudget) break
  548. }
  549. // The whole surface fits the retain budget — nothing to compact.
  550. if (keepFromIdx === 0) return null
  551. // Round the cutoff to a tool-pairing boundary: if the cut before
  552. // `nodes[keepFromIdx]` is unbalanced (an unanswered tool-call sits before
  553. // it — i.e. it is mid-step), extend the retained side head-ward until the
  554. // cut is balanced, so the compacted range ends without splitting an
  555. // assistant↔result pair. A node that belongs to no step is already a
  556. // balanced (free) boundary. Decline if no balanced cut exists at or below
  557. // `keepFromIdx` (the compactable range is only an un-splittable open tail
  558. // step — retry once it closes).
  559. while (keepFromIdx > 0) {
  560. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  561. if (isToolPairingBalanced(nodes, events, nodes[keepFromIdx]!.seq)) break
  562. keepFromIdx -= 1
  563. }
  564. if (keepFromIdx === 0) return null
  565. // The compacted range is [head … keepFromIdx - 1], anchored at the head.
  566. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  567. const firstSeq = nodes[0]!.seq
  568. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  569. const cutoffSeq = nodes[keepFromIdx - 1]!.seq
  570. return { start: firstSeq, end: cutoffSeq }
  571. }
  572. /**
  573. * Keep ONLY text blocks from the model-produced summary before storing it.
  574. *
  575. * The summary lands on the surface as a synthesized `user/message` (see
  576. * {@link _frameSummary}), so the only block type that is both useful and safe
  577. * there is `text`. A model assistant message can otherwise carry `reasoning`
  578. * (private chain-of-thought, must not leak into the durable checkpoint) and
  579. * `tool-call` blocks — and a surviving `tool-call` in a user message would be
  580. * an orphaned call with no matching `tool-result`, exactly the tool-pairing
  581. * breakage compaction works to avoid. Filtering to text drops both.
  582. */
  583. private _textOnly(blocks: readonly ContentBlock[]): ContentBlock[] {
  584. return blocks.filter((block): block is Extract<ContentBlock, { type: 'text' }> => block.type === 'text')
  585. }
  586. /**
  587. * The turn number of the currently OPEN turn — a `turn/start` not yet
  588. * followed by its `turn/end` — or `null` if the session has no open turn.
  589. *
  590. * Compaction's events must be enclosed in a turn, so scanning back from the
  591. * tail: a `turn/start` means that turn is open (return it); a `turn/end` means
  592. * the most recent turn already closed (return null). The whole compaction
  593. * sequence (compact/start … compact/end) is stamped with this turn.
  594. */
  595. private _openTurn(session: Session): number | null {
  596. for (let i = session.events.length - 1; i >= 0; i--) {
  597. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  598. const e = session.events[i]!
  599. if (e.type === 'turn/start') return e.data.turn
  600. if (e.type === 'turn/end') return null
  601. }
  602. return null
  603. }
  604. /**
  605. * Extract plain-text conversation from a set of surface node seqs, for
  606. * feeding into the summarization model. Walks the seqs in the order given
  607. * (surface order, as `compactRegion` slices the surface-node list) so the
  608. * summary follows the conversation as the model sees it — which, after a
  609. * `replace`, is NOT ascending log-seq order (a high-seq summary node heads the
  610. * surface before older retained lower-seq nodes).
  611. */
  612. private _extractText(session: Session, seqs: number[]): string {
  613. const lines: string[] = []
  614. // Walk seqs in the order given (surface order, as compactRegion slices the
  615. // surface-node list) — NOT ascending log-seq order. After a replace the
  616. // summary node carries a fresh high seq while sitting at the head of the
  617. // surface before older retained lower-seq nodes, so a log-order scan would
  618. // feed the transcript out of order and break the checkpoint-merge prompt.
  619. for (const seq of seqs) {
  620. const event = session.events[seq]
  621. /* v8 ignore next -- seq is a surface-node seq, always a valid log index by construction */
  622. if (!event) continue
  623. switch (event.type) {
  624. case 'user/message': {
  625. const text = this._blocksToText(event.data.content)
  626. if (text) lines.push(`User: ${text}`)
  627. break
  628. }
  629. case 'assistant/message': {
  630. const text = this._blocksToText(event.data.content)
  631. if (text) lines.push(`Assistant: ${text}`)
  632. break
  633. }
  634. case 'tool/result': {
  635. const text = this._blocksToText(event.data.content)
  636. const label = event.data.isError ? 'Tool error' : 'Tool result'
  637. if (text) lines.push(`${label} (call ${event.data.callId}): ${text}`)
  638. break
  639. }
  640. case 'context/message': {
  641. const text = this._blocksToText(event.data.content)
  642. if (text) lines.push(`[Context: ${text}]`)
  643. break
  644. }
  645. case 'steering/message': {
  646. const text = this._blocksToText(event.data.content)
  647. if (text) lines.push(`[Steering: ${text}]`)
  648. break
  649. }
  650. // SessionEventMap is merge-extensible — unknown types are
  651. // non-message events that carry no extractable text.
  652. /* v8 ignore next 2 -- seqs only name surface nodes, always one of the 5 handled SurfaceEventTypes; unreachable */
  653. default:
  654. break
  655. }
  656. }
  657. return lines.join('\n\n')
  658. }
  659. /**
  660. * Render content blocks to a single plain-text string for the summarization
  661. * prompt. Text and reasoning contribute their text; every other block type
  662. * contributes a type-tagged placeholder (`[image]`, `[tool-call: name(args)]`,
  663. * …) so the summarizer is told what non-text content existed in the region
  664. * rather than silently losing it. Blocks join with newlines; empty-text
  665. * blocks contribute nothing.
  666. */
  667. private _blocksToText(blocks: readonly ContentBlock[]): string {
  668. const parts: string[] = []
  669. for (const block of blocks) {
  670. switch (block.type) {
  671. case 'text':
  672. if (block.text) parts.push(block.text)
  673. break
  674. case 'reasoning':
  675. if (block.text) parts.push(`[reasoning: ${block.text}]`)
  676. break
  677. case 'tool-call':
  678. parts.push(`[tool-call: ${block.name}(${block.arguments})]`)
  679. break
  680. case 'tool-result': {
  681. const inner = this._blocksToText(block.content)
  682. parts.push(inner ? `[tool-result: ${inner}]` : '[tool-result]')
  683. break
  684. }
  685. case 'image':
  686. parts.push('[image]')
  687. break
  688. // ContentBlockMap is merge-extensible — render an unknown block as a
  689. // bare type-tagged placeholder so a plugin-added block type is still
  690. // signalled to the summarizer rather than dropped.
  691. default:
  692. parts.push(`[${(block as ContentBlock).type}]`)
  693. }
  694. }
  695. return parts.join('\n')
  696. }
  697. }
  698. export default BasicCompactService