dsh-compact-saviour 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/ACCEPTANCE.md +16 -0
  2. package/LICENSE +21 -0
  3. package/README.md +110 -0
  4. package/THIRD_PARTY_NOTICES.md +50 -0
  5. package/cordis.patch.yml +3 -0
  6. package/cordis.yml +2 -0
  7. package/docs/development.md +28 -0
  8. package/dshx.yml +16 -0
  9. package/lib/client.js +5933 -0
  10. package/lib/client.js.map +1 -0
  11. package/lib/dsh-compact-saviour.js +1206 -0
  12. package/lib/types/client/context-elements.d.ts +6 -0
  13. package/lib/types/client/context-elements.d.ts.map +1 -0
  14. package/lib/types/client/context-elements.js +41 -0
  15. package/lib/types/client/context-elements.js.map +1 -0
  16. package/lib/types/client/index.d.ts +10 -0
  17. package/lib/types/client/index.d.ts.map +1 -0
  18. package/lib/types/client/index.js +217 -0
  19. package/lib/types/client/index.js.map +1 -0
  20. package/lib/types/client/meter-bridge.d.ts +12 -0
  21. package/lib/types/client/meter-bridge.d.ts.map +1 -0
  22. package/lib/types/client/meter-bridge.js +89 -0
  23. package/lib/types/client/meter-bridge.js.map +1 -0
  24. package/lib/types/client/ring-control.d.ts +32 -0
  25. package/lib/types/client/ring-control.d.ts.map +1 -0
  26. package/lib/types/client/ring-control.js +191 -0
  27. package/lib/types/client/ring-control.js.map +1 -0
  28. package/lib/types/client/satellite.d.ts +32 -0
  29. package/lib/types/client/satellite.d.ts.map +1 -0
  30. package/lib/types/client/satellite.js +163 -0
  31. package/lib/types/client/satellite.js.map +1 -0
  32. package/lib/types/client/waiting-copy.d.ts +10 -0
  33. package/lib/types/client/waiting-copy.d.ts.map +1 -0
  34. package/lib/types/client/waiting-copy.js +31 -0
  35. package/lib/types/client/waiting-copy.js.map +1 -0
  36. package/lib/types/compress.d.ts +31 -0
  37. package/lib/types/compress.d.ts.map +1 -0
  38. package/lib/types/compress.js +265 -0
  39. package/lib/types/compress.js.map +1 -0
  40. package/lib/types/controller.d.ts +31 -0
  41. package/lib/types/controller.d.ts.map +1 -0
  42. package/lib/types/controller.js +323 -0
  43. package/lib/types/controller.js.map +1 -0
  44. package/lib/types/dsh-compact-saviour.d.ts +10 -0
  45. package/lib/types/dsh-compact-saviour.d.ts.map +1 -0
  46. package/lib/types/dsh-compact-saviour.js +104 -0
  47. package/lib/types/dsh-compact-saviour.js.map +1 -0
  48. package/lib/types/message-source.d.ts +9 -0
  49. package/lib/types/message-source.d.ts.map +1 -0
  50. package/lib/types/message-source.js +2 -0
  51. package/lib/types/message-source.js.map +1 -0
  52. package/lib/types/pressure.d.ts +17 -0
  53. package/lib/types/pressure.d.ts.map +1 -0
  54. package/lib/types/pressure.js +71 -0
  55. package/lib/types/pressure.js.map +1 -0
  56. package/lib/types/shared.d.ts +42 -0
  57. package/lib/types/shared.d.ts.map +1 -0
  58. package/lib/types/shared.js +2 -0
  59. package/lib/types/shared.js.map +1 -0
  60. package/lib/types/summary-types.d.ts +20 -0
  61. package/lib/types/summary-types.d.ts.map +1 -0
  62. package/lib/types/summary-types.js +7 -0
  63. package/lib/types/summary-types.js.map +1 -0
  64. package/lib/types/vendor/region.d.ts +65 -0
  65. package/lib/types/vendor/region.d.ts.map +1 -0
  66. package/lib/types/vendor/region.js +412 -0
  67. package/lib/types/vendor/region.js.map +1 -0
  68. package/package.json +101 -0
  69. package/scripts/link-harness-dependencies.mjs +41 -0
  70. package/scripts/normalize-artifacts.mjs +7 -0
  71. package/src/client/assets.d.ts +1 -0
  72. package/src/client/context-elements.ts +39 -0
  73. package/src/client/index.tsx +214 -0
  74. package/src/client/meter-bridge.ts +65 -0
  75. package/src/client/ring-control.ts +134 -0
  76. package/src/client/satellite.ts +121 -0
  77. package/src/client/style.css +54 -0
  78. package/src/client/vendor/LICENSE +21 -0
  79. package/src/client/vendor/open-bot-motion.cjs +1673 -0
  80. package/src/client/vendor/open-bot-motion.d.cts +9 -0
  81. package/src/client/waiting-copy.tsx +29 -0
  82. package/src/compress.ts +210 -0
  83. package/src/controller.ts +248 -0
  84. package/src/dsh-compact-saviour.ts +82 -0
  85. package/src/message-source.ts +4 -0
  86. package/src/pressure.ts +69 -0
  87. package/src/shared.ts +13 -0
  88. package/src/summary-types.ts +11 -0
  89. package/src/vendor/region.ts +583 -0
  90. package/tests/compression.test.ts +130 -0
  91. package/tests/controller.test.ts +279 -0
  92. package/tests/interaction-fixture.tsx +38 -0
  93. package/tests/interaction.browser.mjs +201 -0
  94. package/tests/meter-bridge.browser.mjs +83 -0
  95. package/tests/motion.test.ts +26 -0
  96. package/tests/peer-range.test.ts +23 -0
  97. package/tests/pressure.test.ts +11 -0
  98. package/tests/transactions.test.ts +56 -0
  99. package/tsconfig.json +10 -0
  100. package/tsdown.config.ts +13 -0
@@ -0,0 +1,69 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ import type { EpochHeader, Session } from '@deepseek-ai/dsh-session';
3
+ import type { TokenMeasurement } from '@deepseek-ai/dsh-token-meter';
4
+ import type { ContextView, Target } from './shared.js';
5
+
6
+ export function estimateRequest(measurement: TokenMeasurement, header?: EpochHeader): number {
7
+ return measurement.surfaceTokens + (header?.tools?.length ? Math.ceil(JSON.stringify(header.tools).length / 4) + 4 : 0);
8
+ }
9
+
10
+ /** Reject impossible historical anchors only; normal provider calibration remains authoritative. */
11
+ export function pressureEstimate(measurement: TokenMeasurement, capacity: number, header?: EpochHeader): number | undefined {
12
+ const estimate = estimateRequest(measurement, header);
13
+ return measurement.baseline.kind === 'usage' && measurement.baseline.tokens > capacity * 1.1
14
+ && estimate < capacity * .75 ? estimate : undefined;
15
+ }
16
+
17
+ /** A reversible decoration of the public meter; no log or billing projection is rewritten. */
18
+ export class PressureMeter {
19
+ private capacities = new Map<string, { capacity: number; expires: number }>();
20
+ private original: Context['tokenMeter']['measure'];
21
+ constructor(private ctx: Context) {
22
+ const meter = ctx.tokenMeter, own = Object.getOwnPropertyDescriptor(meter, 'measure');
23
+ this.original = meter.measure;
24
+ const self = this;
25
+ const measure: typeof meter.measure = function(this: Context['tokenMeter'], session, requestHeader) {
26
+ const value = self.original.call(this, session, requestHeader);
27
+ const header = requestHeader ?? session.requestHeader();
28
+ const cached = header && self.capacities.get(self.key(header.config));
29
+ const estimate = cached && cached.expires > Date.now() ? pressureEstimate(value, cached.capacity, header) : undefined;
30
+ return estimate === undefined ? value : Object.freeze({ ...value,
31
+ baseline: Object.freeze({ kind: 'estimated' as const, tokens: estimate }), surfaceDeltaTokens: 0, totalTokens: estimate,
32
+ });
33
+ };
34
+ ctx.effect(() => {
35
+ meter.measure = measure;
36
+ return () => {
37
+ if (Object.getOwnPropertyDescriptor(meter, 'measure')?.value !== measure) return;
38
+ if (own) Object.defineProperty(meter, 'measure', own); else Reflect.deleteProperty(meter, 'measure');
39
+ };
40
+ });
41
+ }
42
+ private key(target: { provider?: string; model?: string }): string { return JSON.stringify([target.provider, target.model]); }
43
+ async refresh(session: Session, target?: Target, signal?: AbortSignal): Promise<ContextView | undefined> {
44
+ const logged = session.requestHeader();
45
+ target ??= logged?.config;
46
+ if (!target?.provider || !target.model) return;
47
+ const key = this.key(target);
48
+ let cached = this.capacities.get(key);
49
+ if (!cached || cached.expires <= Date.now()) {
50
+ const info = await this.ctx.llm.resolveModelInfo(target.provider, target.model, signal);
51
+ const capacity = info.context?.contextWindow;
52
+ if (!capacity || !Number.isFinite(capacity) || capacity <= 0) return;
53
+ cached = { capacity, expires: Date.now() + 60_000 };
54
+ if (this.capacities.size >= 64) this.capacities.delete(this.capacities.keys().next().value!);
55
+ this.capacities.set(key, cached);
56
+ }
57
+ const header = logged && { ...logged, config: { ...logged.config, provider: target.provider, model: target.model } };
58
+ const raw = this.original.call(this.ctx.tokenMeter, session, header);
59
+ const estimate = pressureEstimate(raw, cached.capacity, header);
60
+ const switched = logged && this.key(logged.config) !== key;
61
+ const breakdown = this.ctx.sessionProjections.stateOf(session, 'contextBreakdown')?.breakdown;
62
+ return {
63
+ usedTokens: estimate ?? raw.totalTokens, reportedTokens: raw.totalTokens, contextWindow: cached.capacity,
64
+ corrected: estimate !== undefined || !!switched,
65
+ ...(estimate !== undefined ? { reason: 'invalid-usage' as const } : switched ? { reason: 'model-switch' as const } : {}),
66
+ ...(breakdown ? { parts: [breakdown.systemTokens, breakdown.toolsTokens, breakdown.messageTokens] } : {}),
67
+ };
68
+ }
69
+ }
package/src/shared.ts ADDED
@@ -0,0 +1,13 @@
1
+ export interface Config { enabled: boolean; mode?: 'direct' | 'rescue'; provider: string; model: string; reasoningEffort: string }
2
+ export interface Target { provider: string; model: string; reasoningEffort?: string }
3
+ export interface ModelOption { provider: string; id: string; name: string }
4
+ export interface JobView {
5
+ phase: 'idle' | 'queued' | 'running' | 'done' | 'failed';
6
+ message: string; failures: number; completed?: number; total?: number; current?: Target;
7
+ beforeTokens?: number; afterTokens?: number;
8
+ }
9
+ export interface ContextView {
10
+ usedTokens: number; reportedTokens: number; contextWindow: number; corrected: boolean;
11
+ reason?: 'invalid-usage' | 'model-switch'; parts?: number[];
12
+ }
13
+ export interface State { config: Config; job: JobView; available: boolean; context?: ContextView }
@@ -0,0 +1,11 @@
1
+ import type { ContentBlock, Message, ToolSchema, TokenUsage } from '@deepseek-ai/dsh-llm';
2
+ export interface SummarizationInput { readonly tools?: readonly ToolSchema[]; readonly messages: readonly Message[] }
3
+ export type SummaryResult = {
4
+ summary: ContentBlock[]; provider: string; model: string; maxTokens?: number; usage?: TokenUsage;
5
+ } & ({ rawOutput: ContentBlock[]; llmStreamCall: true } | { rawOutput?: ContentBlock[]; llmStreamCall?: never });
6
+ export function frameSummary(summary: readonly ContentBlock[]): ContentBlock[] {
7
+ return [
8
+ { type: 'text', text: '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.\n\n<compacted-summary>' },
9
+ ...summary, { type: 'text', text: '</compacted-summary>' },
10
+ ];
11
+ }
@@ -0,0 +1,583 @@
1
+ // Adapted from deepseek-ai/deepseek-harness v0.1.5-rc.2 (MIT). See THIRD_PARTY_NOTICES.md.
2
+ /**
3
+ * Surface retention selection and the shared log-recorded compaction
4
+ * transaction for automatic open-turn and manual idle-session compaction.
5
+ *
6
+ * @module @deepseek-ai/dsh-compaction-basic/region
7
+ */
8
+
9
+ import { randomUUID } from 'node:crypto'
10
+ import { isDeepStrictEqual } from 'node:util'
11
+ import {
12
+ CompactionId,
13
+ ManualCompactionError,
14
+ compactCheckpointSource,
15
+ toolPairingBalancedAfter,
16
+ toolPairingBalancedBefore,
17
+ } from '@deepseek-ai/dsh-compaction'
18
+ import type { CompactionResult } from '@deepseek-ai/dsh-compaction'
19
+ import type { CommandId } from '@deepseek-ai/dsh-commands/brand'
20
+ import { createUserMessage, errorChain } from '@deepseek-ai/dsh-llm'
21
+ import type { Message, UserMessage } from '@deepseek-ai/dsh-llm'
22
+ import type { TokenMeasurement, TokenMeter } from '@deepseek-ai/dsh-token-meter'
23
+ import { SessionSeq, type Session, type SessionEvent } from '@deepseek-ai/dsh-session'
24
+ import type { Agent } from '@deepseek-ai/dsh-agent'
25
+ import { frameSummary } from '../summary-types.js'
26
+ import type { SummarizationInput, SummaryResult } from '../summary-types.js'
27
+
28
+ interface RegionDependencies {
29
+ readonly meter: TokenMeter
30
+ summarize(input: SummarizationInput, agent: Agent, signal?: AbortSignal): Promise<SummaryResult>
31
+ }
32
+
33
+ /** One validated inclusive span of current surface positions. */
34
+ interface SurfaceSelection {
35
+ readonly start: SessionSeq
36
+ readonly end: SessionSeq
37
+ readonly startIdx: number
38
+ readonly endIdx: number
39
+ readonly shadowedSeqs: readonly SessionSeq[]
40
+ }
41
+
42
+ /** A selection with its priced snapshot and the replay input built from it. */
43
+ interface PreparedCompaction extends SurfaceSelection {
44
+ readonly measurement: TokenMeasurement
45
+ readonly selectedNodes: TokenMeasurement['nodes']
46
+ readonly shadowedTokenCount: number
47
+ /** Route-priced total of the selected span; the shrink comparison's unit. */
48
+ readonly shadowedRouteTokenCount: number
49
+ readonly input: SummarizationInput
50
+ }
51
+
52
+ type SummarizedCompaction = PreparedCompaction & SummaryResult & {
53
+ readonly checkpointMessage: UserMessage
54
+ }
55
+
56
+ interface CompactionTransactionOptions {
57
+ /** `current-turn` derives a numbered owner; `null` writes a standalone bracket. */
58
+ readonly owner: 'current-turn' | null
59
+ /** Surface relationship that must survive asynchronous summarization. */
60
+ readonly stability: 'whole-surface' | 'selected-span'
61
+ /** Optional durability checkpoint after a successfully closed bracket. */
62
+ readonly flush?: () => Promise<void>
63
+ /** Manual command that initiated this transaction, when present. */
64
+ readonly sourceCommandId?: CommandId
65
+ }
66
+
67
+ interface CompactionEntryState {
68
+ readonly openTurn: number | null
69
+ readonly unmatchedCompactionStart: SessionEvent<'compaction/start'> | undefined
70
+ readonly latestEndSeedSeq: SessionSeq | undefined
71
+ }
72
+
73
+ /**
74
+ * Rejects a summary whose replacement boundaries are no longer the ones it was
75
+ * built from, distinguished from summarizer and shrink failures so a manual
76
+ * caller can report the two causes differently.
77
+ */
78
+ class SurfaceChangedError extends Error {}
79
+
80
+ /** Whether the summary may still replace the span it was built from. */
81
+ type StabilityCheck = (
82
+ dependencies: RegionDependencies,
83
+ session: Session,
84
+ prepared: PreparedCompaction,
85
+ ) => void
86
+
87
+ /** Failure captured after `compaction/start` has committed. */
88
+ interface TransactionFailure {
89
+ readonly error: unknown
90
+ readonly stage: 'summary' | 'commit'
91
+ }
92
+
93
+ /**
94
+ * The `system/message` holding surface node 0, or `undefined` when another
95
+ * message-producing event starts the surface.
96
+ * @param session - session supplying the log behind the current surface.
97
+ * @param headSeq - seq at surface node 0 of a non-empty surface.
98
+ * @returns the system head event, or `undefined` without one.
99
+ */
100
+ function systemHead(session: Session, headSeq: SessionSeq): SessionEvent<'system/message'> | undefined {
101
+ // Surface nodes are current log seqs, so the event exists.
102
+ // oxlint-disable-next-line typescript/no-non-null-assertion
103
+ const head = session.eventAt(headSeq)!
104
+ return head.type === 'system/message' ? head : undefined
105
+ }
106
+
107
+ /**
108
+ * Resolve the next range starting at the first non-system surface node while
109
+ * retaining a priced recent tail and never splitting an assistant
110
+ * tool-call/result pair. A `system/message` at surface node 0 is never inside
111
+ * the range; without one the range starts at node 0.
112
+ * @param session - session supplying authoritative current surface positions.
113
+ * @param measurement - unified pressure and surface measurement from the conversation meter.
114
+ * @param retainTokens - minimum recent tail budget retained verbatim.
115
+ * @returns the inclusive positional seq range to compact, or `null`.
116
+ */
117
+ export function selectCompactableRange(
118
+ session: Session,
119
+ measurement: TokenMeasurement,
120
+ retainTokens: number,
121
+ ): { start: SessionSeq; end: SessionSeq } | null {
122
+ const pricedNodes = measurement.nodes
123
+ if (pricedNodes.length === 0) return null
124
+
125
+ const surfaceNodes = session.surface.nodes
126
+ if (surfaceNodes.length !== pricedNodes.length
127
+ || surfaceNodes.some((seq, index) => seq !== pricedNodes[index]?.seq)) {
128
+ throw new Error('compaction: token-meter surface does not match the current session surface')
129
+ }
130
+ // oxlint-disable-next-line typescript/no-non-null-assertion
131
+ const firstIdx = systemHead(session, surfaceNodes[0]!) === undefined ? 0 : 1
132
+
133
+ let accumulated = 0
134
+ let keepFromIdx = pricedNodes.length
135
+ for (let index = pricedNodes.length - 1; index >= 0; index -= 1) {
136
+ // oxlint-disable-next-line typescript/no-non-null-assertion
137
+ accumulated += pricedNodes[index]!.tokens
138
+ keepFromIdx = index
139
+ if (accumulated >= retainTokens) break
140
+ }
141
+ if (keepFromIdx <= firstIdx) return null
142
+
143
+ while (keepFromIdx > firstIdx) {
144
+ // oxlint-disable-next-line typescript/no-non-null-assertion
145
+ if (toolPairingBalancedBefore(session, surfaceNodes[keepFromIdx]!)) break
146
+ keepFromIdx -= 1
147
+ }
148
+ if (keepFromIdx <= firstIdx) return null
149
+
150
+ // oxlint-disable-next-line typescript/no-non-null-assertion
151
+ const first = surfaceNodes[firstIdx]!
152
+ // oxlint-disable-next-line typescript/no-non-null-assertion
153
+ const cutoff = surfaceNodes[keepFromIdx - 1]!
154
+ return { start: first, end: cutoff }
155
+ }
156
+
157
+ /**
158
+ * Run the single compaction transaction over one selected positional span.
159
+ * Selection and validation are read-only. Idle/log validation and
160
+ * `compaction/start` are synchronously adjacent, so the durable opening marker is
161
+ * the compaction lock before summarization yields. Every later failure makes
162
+ * exactly one `compaction/end` attempt; a failed close deliberately leaves the
163
+ * unmatched start detectable.
164
+ * @param dependencies - conversation meter and dynamically dispatched summarizer hook.
165
+ * @param session - session whose surface is mutated.
166
+ * @param start - inclusive first surface-node seq.
167
+ * @param end - inclusive last surface-node seq.
168
+ * @param agent - agent used by the summarizer.
169
+ * @param options - bracket owner, stability rule, and optional durability checkpoint.
170
+ * @param signal - optional summarization cancellation signal.
171
+ * @returns the successful durable compaction result.
172
+ */
173
+ export async function compactSurfaceRegion(
174
+ dependencies: RegionDependencies,
175
+ session: Session,
176
+ start: SessionSeq,
177
+ end: SessionSeq,
178
+ agent: Agent,
179
+ options: CompactionTransactionOptions,
180
+ signal?: AbortSignal,
181
+ ): Promise<CompactionResult> {
182
+ if (options.owner === null) signal?.throwIfAborted()
183
+ const selection = validateSurfaceRegion(session, start, end)
184
+ const entryState = inspectCompactionEntryState(session)
185
+ assertCompactionInactive(
186
+ entryState.unmatchedCompactionStart,
187
+ entryState.latestEndSeedSeq,
188
+ 'compaction',
189
+ )
190
+
191
+ let owner: number | null
192
+ if (options.owner === null) {
193
+ if (entryState.openTurn !== null) {
194
+ throw new ManualCompactionError('busy', 'manual compaction: the session already has an open turn')
195
+ }
196
+ owner = null
197
+ } else {
198
+ if (entryState.openTurn === null) {
199
+ throw new Error('compactRegion: no open turn — automatic compaction events must be enclosed in a turn')
200
+ }
201
+ owner = entryState.openTurn
202
+ }
203
+
204
+ const compactionId = CompactionId(randomUUID())
205
+ const lifecycle = {
206
+ compactionId,
207
+ ...options.sourceCommandId === undefined ? {} : { sourceCommandId: options.sourceCommandId },
208
+ turn: owner,
209
+ }
210
+ const startEvent = session.append('compaction/start', lifecycle)
211
+ const assertStable: StabilityCheck = options.stability === 'whole-surface'
212
+ ? assertWholeSurfaceUnchanged
213
+ : assertSelectedSpanStable
214
+ let failure: TransactionFailure | undefined
215
+ let flushFailure: unknown
216
+ let result: CompactionResult | undefined
217
+ let closed = false
218
+ let closing = false
219
+ let stage: TransactionFailure['stage'] = 'summary'
220
+
221
+ try {
222
+ const prepared = prepareCompaction(dependencies, session, selection)
223
+ const summarized = await summarizeCompaction(
224
+ dependencies,
225
+ prepared,
226
+ agent,
227
+ compactionId,
228
+ options.sourceCommandId,
229
+ signal,
230
+ )
231
+ if (options.owner === null) signal?.throwIfAborted()
232
+ assertStable(dependencies, session, summarized)
233
+ stage = 'commit'
234
+ const pending = commitCompactionBody(session, startEvent, summarized)
235
+ closing = true
236
+ const endEvent = session.append('compaction/end', lifecycle)
237
+ closed = true
238
+ result = completeCompaction(pending, endEvent)
239
+ } catch (error: unknown) {
240
+ failure = { error, stage: closing ? 'commit' : stage }
241
+ if (!closing) {
242
+ closing = true
243
+ try {
244
+ session.append('compaction/end', { ...lifecycle, error: errorChain(error) })
245
+ closed = true
246
+ } catch (closeError: unknown) {
247
+ failure = { error: closeError, stage: 'commit' }
248
+ }
249
+ }
250
+ }
251
+
252
+ if (closed && options.flush !== undefined) {
253
+ try {
254
+ await options.flush()
255
+ } catch (error: unknown) {
256
+ flushFailure = error
257
+ }
258
+ }
259
+
260
+ if (options.owner === null) signal?.throwIfAborted()
261
+ if (failure !== undefined) {
262
+ if (options.owner === null) throwManualFailure(failure)
263
+ throw failure.error
264
+ }
265
+ if (flushFailure !== undefined) {
266
+ throw new ManualCompactionError(
267
+ 'persistence',
268
+ 'manual compaction durability checkpoint failed',
269
+ { cause: flushFailure },
270
+ )
271
+ }
272
+ /* v8 ignore next -- every path without a result records and throws a failure above. */
273
+ if (result === undefined) throw new Error('compaction committed without a result')
274
+ return result
275
+ }
276
+
277
+ /** Classify one closed manual attempt without weakening cancellation precedence. */
278
+ function throwManualFailure(failure: TransactionFailure): never {
279
+ if (failure.stage === 'commit') {
280
+ throw new ManualCompactionError(
281
+ 'commit',
282
+ 'manual compaction did not commit cleanly',
283
+ { cause: failure.error },
284
+ )
285
+ }
286
+ if (failure.error instanceof SurfaceChangedError) {
287
+ throw new ManualCompactionError(
288
+ 'changed',
289
+ 'the compacted history changed during manual compaction',
290
+ { cause: failure.error },
291
+ )
292
+ }
293
+ throw new ManualCompactionError(
294
+ 'summary',
295
+ 'manual compaction could not produce a smaller summary',
296
+ { cause: failure.error },
297
+ )
298
+ }
299
+
300
+ /**
301
+ * Reject a durable unmatched compaction marker unless a later constructor-seed
302
+ * boundary proves that its owner belongs to an earlier session lifecycle.
303
+ * @param unmatchedCompactionStart - latest unmatched opening marker, if any.
304
+ * @param latestEndSeedSeq - newest constructor-seed boundary, if any.
305
+ * @param stage - operation label included in the busy diagnostic.
306
+ */
307
+ function assertCompactionInactive(
308
+ unmatchedCompactionStart: SessionEvent<'compaction/start'> | undefined,
309
+ latestEndSeedSeq: SessionSeq | undefined,
310
+ stage: string,
311
+ ): void {
312
+ if (unmatchedCompactionStart === undefined
313
+ || (latestEndSeedSeq !== undefined
314
+ && latestEndSeedSeq > unmatchedCompactionStart.seq)) return
315
+ throw new ManualCompactionError(
316
+ 'busy',
317
+ `${stage}: compaction already in progress; the session compaction lock is already active`,
318
+ )
319
+ }
320
+
321
+ /**
322
+ * Recheck the durable compaction lock after an asynchronous policy decision.
323
+ * @param session - session whose latest marker state is inspected.
324
+ * @param stage - operation label included in the busy diagnostic.
325
+ */
326
+ export function assertNoActiveCompaction(session: Session, stage: string): void {
327
+ const entryState = inspectCompactionEntryState(session)
328
+ assertCompactionInactive(
329
+ entryState.unmatchedCompactionStart,
330
+ entryState.latestEndSeedSeq,
331
+ stage,
332
+ )
333
+ }
334
+
335
+ /** Validate one requested surface-position span before asynchronous work begins. */
336
+ function validateSurfaceRegion(session: Session, start: SessionSeq, end: SessionSeq): SurfaceSelection {
337
+ const nodes = session.surface.nodes
338
+ const startIdx = nodes.indexOf(start)
339
+ const endIdx = nodes.indexOf(end)
340
+ if (startIdx === -1) throw new Error(`compactRegion: start seq ${start} not found in surface`)
341
+ if (endIdx === -1) throw new Error(`compactRegion: end seq ${end} not found in surface`)
342
+ if (startIdx > endIdx) {
343
+ throw new Error(
344
+ `compactRegion: start seq ${start} (position ${startIdx}) is after end seq ${end} (position ${endIdx}) on the surface`,
345
+ )
346
+ }
347
+ // oxlint-disable-next-line typescript/no-non-null-assertion
348
+ if (!toolPairingBalancedBefore(session, nodes[startIdx]!)) {
349
+ throw new Error(`compactRegion: start seq ${start} is not a balanced boundary (would split a step's tool-call/result pair)`)
350
+ }
351
+ // oxlint-disable-next-line typescript/no-non-null-assertion
352
+ if (!toolPairingBalancedAfter(session, nodes[endIdx]!)) {
353
+ throw new Error(`compactRegion: end seq ${end} is not a balanced boundary (would split a step, or the step is still open)`)
354
+ }
355
+
356
+ return { start, end, startIdx, endIdx, shadowedSeqs: nodes.slice(startIdx, endIdx + 1) }
357
+ }
358
+
359
+ /** Snapshot pricing and replay input for a validated surface range. */
360
+ function prepareCompaction(
361
+ dependencies: RegionDependencies,
362
+ session: Session,
363
+ selection: SurfaceSelection,
364
+ ): PreparedCompaction {
365
+ const measurement = dependencies.meter.measure(session)
366
+ const selectedNodes = measurement.nodes.slice(selection.startIdx, selection.endIdx + 1)
367
+ if (selectedNodes.length !== selection.shadowedSeqs.length
368
+ || selectedNodes.some((node, index) => node.seq !== selection.shadowedSeqs[index])) {
369
+ throw new SurfaceChangedError('compaction: selected surface changed before summarization began')
370
+ }
371
+ return {
372
+ ...selection,
373
+ measurement,
374
+ selectedNodes,
375
+ // The shadow-price protocol prices replacements with the fixed heuristic
376
+ // so the O(1) projection fold stays in agreement with its own appends;
377
+ // retention, range selection, and the shrink comparison read the
378
+ // route-priced `tokens` instead.
379
+ shadowedTokenCount: selectedNodes.reduce((total, node) => total + node.heuristicTokens, 0),
380
+ shadowedRouteTokenCount: selectedNodes.reduce((total, node) => total + node.tokens, 0),
381
+ input: buildSummarizationInput(session, selection.shadowedSeqs),
382
+ }
383
+ }
384
+
385
+ /** Run the summarizer and frame its replacement checkpoint. */
386
+ async function summarizeCompaction(
387
+ dependencies: RegionDependencies,
388
+ prepared: PreparedCompaction,
389
+ agent: Agent,
390
+ compactionId: CompactionResult['compactionId'],
391
+ sourceCommandId: CommandId | undefined,
392
+ signal?: AbortSignal,
393
+ ): Promise<SummarizedCompaction> {
394
+ const summaryResult = await dependencies.summarize(prepared.input, agent, signal)
395
+ const checkpointMessage = createUserMessage({
396
+ content: frameSummary(summaryResult.summary),
397
+ source: compactCheckpointSource(compactionId, sourceCommandId),
398
+ })
399
+ // The checkpoint is text-only, so its fixed-heuristic price IS its route
400
+ // price; comparing it against the span's route price asks the real
401
+ // question — does the replacement lower the next request's pressure.
402
+ const framedSummaryTokenCount = dependencies.meter.estimateMessage(checkpointMessage)
403
+ if (framedSummaryTokenCount >= prepared.shadowedRouteTokenCount) {
404
+ throw new Error(
405
+ `summary is not smaller than the shadowed content (${framedSummaryTokenCount} estimated framed tokens >= ${prepared.shadowedRouteTokenCount})`,
406
+ )
407
+ }
408
+ return {
409
+ ...prepared,
410
+ ...summaryResult,
411
+ checkpointMessage,
412
+ }
413
+ }
414
+
415
+ /** Reject a summary prepared against any earlier surface generation. */
416
+ function assertWholeSurfaceUnchanged(
417
+ dependencies: RegionDependencies,
418
+ session: Session,
419
+ prepared: PreparedCompaction,
420
+ ): void {
421
+ const current = dependencies.meter.measure(session)
422
+ if (!isDeepStrictEqual(current.nodes, prepared.measurement.nodes)) {
423
+ throw new SurfaceChangedError('compaction: session surface changed during summarization')
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Require only that the selected span remain the same present, contiguous,
429
+ * equally priced, balanced replacement target. Nodes added outside it remain
430
+ * visible and do not invalidate the summary.
431
+ */
432
+ function assertSelectedSpanStable(
433
+ dependencies: RegionDependencies,
434
+ session: Session,
435
+ prepared: PreparedCompaction,
436
+ ): void {
437
+ let current: SurfaceSelection
438
+ try {
439
+ current = validateSurfaceRegion(session, prepared.start, prepared.end)
440
+ } catch (error: unknown) {
441
+ throw new SurfaceChangedError(
442
+ 'compaction: the selected span is no longer a valid replacement target',
443
+ { cause: error },
444
+ )
445
+ }
446
+ if (!isDeepStrictEqual([...current.shadowedSeqs], [...prepared.shadowedSeqs])) {
447
+ throw new SurfaceChangedError('compaction: the selected span changed during summarization')
448
+ }
449
+ const measured = dependencies.meter.measure(session).nodes.slice(current.startIdx, current.endIdx + 1)
450
+ if (!isDeepStrictEqual(measured, prepared.selectedNodes)) {
451
+ throw new SurfaceChangedError('compaction: the selected span was rewritten during summarization')
452
+ }
453
+ }
454
+
455
+ /** Append one completed summary record and replacement body without yielding. */
456
+ function commitCompactionBody(
457
+ session: Session,
458
+ startEvent: SessionEvent<'compaction/start'>,
459
+ summarized: SummarizedCompaction,
460
+ ): Omit<CompactionResult, 'endSeq'> {
461
+ const {
462
+ start,
463
+ end,
464
+ shadowedSeqs,
465
+ shadowedTokenCount,
466
+ summary,
467
+ provider,
468
+ model,
469
+ maxTokens,
470
+ usage,
471
+ checkpointMessage,
472
+ } = summarized
473
+ const callProvenance = summarized.llmStreamCall === true
474
+ ? { rawOutput: summarized.rawOutput, llmStreamCall: true as const }
475
+ : summarized.rawOutput === undefined ? {} : { rawOutput: summarized.rawOutput }
476
+ const summaryEvent = session.append('compaction/summary', {
477
+ compactionId: startEvent.data.compactionId,
478
+ ...startEvent.data.sourceCommandId === undefined
479
+ ? {}
480
+ : { sourceCommandId: startEvent.data.sourceCommandId },
481
+ summary,
482
+ ...callProvenance,
483
+ shadowedRange: { start, end },
484
+ shadowedSeqs: [...shadowedSeqs],
485
+ shadowedTokenCount,
486
+ provider,
487
+ model,
488
+ ...maxTokens === undefined ? {} : { maxTokens },
489
+ ...usage === undefined ? {} : { usage },
490
+ })
491
+ session.append('user/message', checkpointMessage, {
492
+ surfaceOp: { op: 'replace', startSeq: start, endSeq: end },
493
+ sourceEventSeqs: [startEvent.seq, summaryEvent.seq, ...shadowedSeqs],
494
+ })
495
+ return {
496
+ compactionId: startEvent.data.compactionId,
497
+ ...startEvent.data.sourceCommandId === undefined
498
+ ? {}
499
+ : { sourceCommandId: startEvent.data.sourceCommandId },
500
+ startSeq: startEvent.seq,
501
+ summarySeq: summaryEvent.seq,
502
+ summary,
503
+ shadowedRange: { start, end },
504
+ shadowedSeqs: [...shadowedSeqs],
505
+ shadowedTokenCount,
506
+ }
507
+ }
508
+
509
+ /** Attach the successfully appended close event to a pending result. */
510
+ function completeCompaction(
511
+ pending: Omit<CompactionResult, 'endSeq'>,
512
+ endEvent: SessionEvent<'compaction/end'>,
513
+ ): CompactionResult {
514
+ return { ...pending, endSeq: endEvent.seq }
515
+ }
516
+
517
+ /**
518
+ * Reconstruct the last routed request's cacheable prefix for the shadowed
519
+ * region: the system prompt held by the `system/message` at surface node 0,
520
+ * the header's tool schemas, then the region's own derived messages in surface
521
+ * order. The summarizer appends only the compaction instruction after this, so
522
+ * the call is a genuine prefix of the conversation and reuses the provider's
523
+ * KV cache. A surface without a system head, or whose head projects to no
524
+ * message, contributes no leading system message.
525
+ * @param session - session supplying the surface head, request header, and per-node projection.
526
+ * @param shadowedSeqs - the surface-node seqs, in order, being compacted.
527
+ * @returns the replayed conversation prefix to condense.
528
+ */
529
+ function buildSummarizationInput(
530
+ session: Session,
531
+ shadowedSeqs: readonly SessionSeq[],
532
+ ): SummarizationInput {
533
+ const header = session.requestHeader()
534
+ // shadowedSeqs are current surface seqs, so the surface has a node 0.
535
+ // oxlint-disable-next-line typescript/no-non-null-assertion
536
+ const head = systemHead(session, session.surface.nodes[0]!)
537
+ const system = head === undefined ? null : session.deriveEventMessage(head)
538
+ const regionMessages = shadowedSeqs
539
+ // shadowedSeqs are current surface seqs, so each is a valid log index.
540
+ // oxlint-disable-next-line typescript/no-non-null-assertion
541
+ .map(seq => session.deriveEventMessage(session.eventAt(seq)!))
542
+ .filter((message): message is Message => message !== null)
543
+ return {
544
+ ...header?.tools === undefined ? {} : { tools: header.tools },
545
+ messages: system === null ? regionMessages : [system, ...regionMessages],
546
+ }
547
+ }
548
+
549
+ /** Inspect open-turn, unmatched-compaction, and latest seed-boundary state independently. */
550
+ function inspectCompactionEntryState(session: Session): CompactionEntryState {
551
+ let openTurn: number | null = null
552
+ let openTurnStateKnown = false
553
+ let unmatchedCompactionStart: SessionEvent<'compaction/start'> | undefined
554
+ let compactionEntryStateKnown = false
555
+ let latestEndSeedSeq: SessionSeq | undefined
556
+ for (let seq = session.seq - 1; seq >= 0; seq -= 1) {
557
+ // oxlint-disable-next-line typescript/no-non-null-assertion
558
+ const event = session.eventAt(SessionSeq(seq))!
559
+ if (latestEndSeedSeq === undefined && event.type === 'session/end-seed') {
560
+ latestEndSeedSeq = event.seq
561
+ }
562
+ if (!compactionEntryStateKnown) {
563
+ if (event.type === 'compaction/start') {
564
+ unmatchedCompactionStart = event
565
+ compactionEntryStateKnown = true
566
+ } else if (event.type === 'compaction/end') {
567
+ compactionEntryStateKnown = true
568
+ }
569
+ }
570
+ if (!openTurnStateKnown) {
571
+ if (event.type === 'turn/start') {
572
+ openTurn = event.data.turn
573
+ openTurnStateKnown = true
574
+ } else if (event.type === 'turn/end') {
575
+ openTurnStateKnown = true
576
+ }
577
+ }
578
+ if (openTurnStateKnown
579
+ && compactionEntryStateKnown
580
+ && latestEndSeedSeq !== undefined) break
581
+ }
582
+ return { openTurn, unmatchedCompactionStart, latestEndSeedSeq }
583
+ }