index.ts 34 KB

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