index.ts 34 KB

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