@aragon-agent/cli 0.6.2 → 0.6.3

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 (118) hide show
  1. package/CHANGELOG.md +216 -0
  2. package/README.md +202 -0
  3. package/dist/agent/controller.d.ts +74 -1
  4. package/dist/agent/controller.js +200 -1
  5. package/dist/agent/headless.d.ts +8 -0
  6. package/dist/agent/headless.js +47 -0
  7. package/dist/agent/reducer.d.ts +120 -0
  8. package/dist/agent/reducer.js +201 -2
  9. package/dist/cli.js +224 -5
  10. package/dist/commands/builtins.js +73 -23
  11. package/dist/commands/registry.d.ts +27 -0
  12. package/dist/compaction/archive.d.ts +175 -0
  13. package/dist/compaction/archive.js +267 -0
  14. package/dist/compaction/child.d.ts +97 -0
  15. package/dist/compaction/child.js +113 -0
  16. package/dist/compaction/command.d.ts +41 -0
  17. package/dist/compaction/command.js +342 -0
  18. package/dist/compaction/compactor.d.ts +268 -0
  19. package/dist/compaction/compactor.js +689 -0
  20. package/dist/compaction/digest.d.ts +62 -0
  21. package/dist/compaction/digest.js +164 -0
  22. package/dist/compaction/limits.d.ts +165 -0
  23. package/dist/compaction/limits.js +172 -0
  24. package/dist/compaction/pressure.d.ts +135 -0
  25. package/dist/compaction/pressure.js +167 -0
  26. package/dist/compaction/summarize-call.d.ts +70 -0
  27. package/dist/compaction/summarize-call.js +86 -0
  28. package/dist/compaction/summary-prompt.d.ts +85 -0
  29. package/dist/compaction/summary-prompt.js +197 -0
  30. package/dist/compaction/tail-budget.d.ts +52 -0
  31. package/dist/compaction/tail-budget.js +67 -0
  32. package/dist/compaction/types.d.ts +179 -0
  33. package/dist/compaction/types.js +22 -0
  34. package/dist/compaction/wiring.d.ts +255 -0
  35. package/dist/compaction/wiring.js +556 -0
  36. package/dist/config/app-paths.d.ts +14 -0
  37. package/dist/config/app-paths.js +16 -0
  38. package/dist/config/cli-commands.d.ts +41 -0
  39. package/dist/config/cli-commands.js +77 -1
  40. package/dist/config/env.d.ts +4 -1
  41. package/dist/config/env.js +92 -2
  42. package/dist/config/load.d.ts +31 -0
  43. package/dist/config/load.js +63 -1
  44. package/dist/config/schema.d.ts +159 -0
  45. package/dist/config/schema.js +137 -0
  46. package/dist/config/store.js +17 -1
  47. package/dist/config/ui-state.d.ts +28 -1
  48. package/dist/config/ui-state.js +9 -0
  49. package/dist/exec/events.d.ts +50 -1
  50. package/dist/exec/runner.js +42 -0
  51. package/dist/input/mouse-events.d.ts +31 -5
  52. package/dist/input/mouse-events.js +86 -27
  53. package/dist/input/stdin-mouse-filter.d.ts +8 -3
  54. package/dist/logging/install.d.ts +51 -1
  55. package/dist/logging/install.js +28 -0
  56. package/dist/logging/logger.d.ts +1 -1
  57. package/dist/session/persist.js +19 -0
  58. package/dist/team/report.js +7 -1
  59. package/dist/team/runtime.d.ts +10 -0
  60. package/dist/team/runtime.js +7 -0
  61. package/dist/team/subagent.d.ts +31 -1
  62. package/dist/team/subagent.js +42 -15
  63. package/dist/team/types.d.ts +8 -0
  64. package/dist/ui/ActivityLine.d.ts +19 -1
  65. package/dist/ui/ActivityLine.js +10 -2
  66. package/dist/ui/App.d.ts +48 -1
  67. package/dist/ui/App.js +294 -13
  68. package/dist/ui/Composer.d.ts +13 -1
  69. package/dist/ui/Composer.js +2 -2
  70. package/dist/ui/PromptInput.d.ts +34 -1
  71. package/dist/ui/PromptInput.js +54 -3
  72. package/dist/ui/StatusBar.d.ts +40 -0
  73. package/dist/ui/StatusBar.js +13 -4
  74. package/dist/ui/TeamPanel.js +7 -1
  75. package/dist/ui/Transcript.d.ts +21 -1
  76. package/dist/ui/Transcript.js +32 -2
  77. package/dist/ui/clipboard.d.ts +52 -0
  78. package/dist/ui/clipboard.js +91 -0
  79. package/dist/ui/entries/CompactionCard.d.ts +75 -0
  80. package/dist/ui/entries/CompactionCard.js +158 -0
  81. package/dist/ui/frame-differ.d.ts +46 -0
  82. package/dist/ui/frame-differ.js +94 -10
  83. package/dist/ui/gauge.d.ts +16 -1
  84. package/dist/ui/gauge.js +25 -6
  85. package/dist/ui/glyphs.d.ts +11 -0
  86. package/dist/ui/glyphs.js +2 -0
  87. package/dist/ui/layout/ScrollViewport.d.ts +43 -6
  88. package/dist/ui/layout/ScrollViewport.js +128 -21
  89. package/dist/ui/layout/budget.d.ts +10 -3
  90. package/dist/ui/layout/budget.js +10 -3
  91. package/dist/ui/layout/follow-state.d.ts +137 -0
  92. package/dist/ui/layout/follow-state.js +150 -0
  93. package/dist/ui/layout/virtual-window.d.ts +11 -0
  94. package/dist/ui/layout/virtual-window.js +34 -0
  95. package/dist/ui/overlays/HelpOverlay.js +6 -1
  96. package/dist/ui/overlays/SettingsScreen.d.ts +47 -1
  97. package/dist/ui/overlays/SettingsScreen.js +60 -1
  98. package/dist/ui/palettes.d.ts +16 -0
  99. package/dist/ui/palettes.js +6 -0
  100. package/dist/ui/screen.d.ts +22 -0
  101. package/dist/ui/screen.js +70 -8
  102. package/dist/ui/selection/highlight.d.ts +39 -0
  103. package/dist/ui/selection/highlight.js +64 -0
  104. package/dist/ui/selection/screen-mirror.d.ts +24 -0
  105. package/dist/ui/selection/screen-mirror.js +35 -0
  106. package/dist/ui/selection/selection-controller.d.ts +88 -0
  107. package/dist/ui/selection/selection-controller.js +282 -0
  108. package/dist/ui/selection/selection.d.ts +90 -0
  109. package/dist/ui/selection/selection.js +223 -0
  110. package/dist/ui/stdout-frame-writer.d.ts +33 -0
  111. package/dist/ui/stdout-frame-writer.js +15 -0
  112. package/dist/ui/theme.d.ts +11 -0
  113. package/dist/ui/theme.js +2 -0
  114. package/dist/ui/transcript-text.js +23 -0
  115. package/dist/ui/use-startup-notices.d.ts +27 -1
  116. package/dist/ui/use-startup-notices.js +43 -11
  117. package/dist/ui/use-wheel-routing.js +8 -0
  118. package/package.json +4 -2
@@ -0,0 +1,689 @@
1
+ /**
2
+ * `Compactor` — the `ContextManager` implementation (context-auto-compaction
3
+ * §3.4 / §3.6 / §3.7 / §3.8).
4
+ *
5
+ * ASCII ONLY: `src/compaction/**` is inside the glyph scanner's scope.
6
+ *
7
+ * THIS IS THE POLICY HALF OF THE FEATURE. The engine owns exactly one thing -
8
+ * structural validity - and every other decision is here: when to fire, what to
9
+ * keep, which model summarizes, what the prompt asks for, what happens when the
10
+ * call fails, and when to stop spending.
11
+ */
12
+ import { planCompaction, } from '@aragon-agent/core';
13
+ import { getLogger } from '../logging/logger.js';
14
+ import { resolveFastTier } from '../fast/resolve.js';
15
+ import { COMPACTION_LIMITS } from './limits.js';
16
+ import { attemptTailRelief, reliefSummary, tailBudgetTokens } from './tail-budget.js';
17
+ import { buildDigest, isPriorSummaryBlock } from './digest.js';
18
+ import { buildAnchor, buildCompactedBlock, countProtectedPrefix, readGeneration, truncationBody, } from './summary-prompt.js';
19
+ import { accumulateUsage, errorText, runSummarizeCall } from './summarize-call.js';
20
+ import { computeEstimateOffset, computePressure, shouldCompactAt } from './pressure.js';
21
+ export class Compactor {
22
+ deps;
23
+ log = getLogger().child('compaction');
24
+ // --- Per-run state (reset on `agent_start`) ---
25
+ compactionsThisRun = 0;
26
+ lastCompactionTurn = -Infinity;
27
+ // --- Per-session state ---
28
+ compactions = 0;
29
+ tokensReclaimed = 0;
30
+ sessionUsage = { inputTokens: 0, outputTokens: 0 };
31
+ pricingUnknown = false;
32
+ inFlight = false;
33
+ consecutiveNoProgress = 0;
34
+ selfDisabled = false;
35
+ selfDisabledReason;
36
+ pendingManual = null;
37
+ generation = 0;
38
+ /** Calibration for every ESTIMATED figure (D-23). Recomputed on every turn_end. */
39
+ estimateOffset;
40
+ /** The last pressure computed, so the status bar and `/compact status` agree. */
41
+ lastPressure = null;
42
+ /**
43
+ * The history LENGTH the measurement in `lastUsage` covered (hardening W1).
44
+ *
45
+ * `undefined` DISABLES THE DELTA and reproduces round 1's arithmetic exactly.
46
+ * Every host-side mutation that is not an append must clear it - see
47
+ * `invalidateMeasurement`.
48
+ */
49
+ measuredPrefixLength;
50
+ /**
51
+ * The messages the last splice dropped, held ONLY until the wiring collects
52
+ * them for the archive (W4 / RV-3).
53
+ *
54
+ * NOT ON `CompactionRecord`: that record is dispatched into the reducer and
55
+ * retained in view state for the card's lifetime, so putting a megabyte of
56
+ * history on it would park the dropped conversation in React state - precisely
57
+ * the memory this compaction just reclaimed.
58
+ *
59
+ * SET ONLY WHEN ARCHIVING IS ON, so with the key off the slice is never
60
+ * retained and W4 costs nothing.
61
+ */
62
+ lastDropped = null;
63
+ // --- Abort plumbing for the summarization call (see `summarize-call.ts`) ---
64
+ callAbort = null;
65
+ callAbortReason = null;
66
+ /** Guard 2's ceiling for this instance (hardening §3.4.2 mechanism B / DH-16). */
67
+ maxPerRun;
68
+ constructor(deps, opts) {
69
+ this.deps = deps;
70
+ this.maxPerRun = opts?.maxPerRun ?? COMPACTION_LIMITS.maxPerRun;
71
+ }
72
+ // =========================================================================
73
+ // Lifetime
74
+ // =========================================================================
75
+ /** `agent_start` — the per-run cap resets, the session totals do not. */
76
+ onRunStart() {
77
+ this.compactionsThisRun = 0;
78
+ this.lastCompactionTurn = -Infinity;
79
+ }
80
+ /**
81
+ * `turn_end` — recompute the calibration offset (D-23).
82
+ *
83
+ * RE-MEASURED EVERY TURN, not once, so it tracks a toolset that changed
84
+ * mid-session (`/tools`, a skill that registers one).
85
+ */
86
+ onTurnEnd(usage, messages, systemPrompt) {
87
+ this.estimateOffset = computeEstimateOffset(usage, messages, systemPrompt);
88
+ // THE SAME ARRAY `computeEstimateOffset` JUST MEASURED, so the two numbers
89
+ // describe the same request by construction (hardening §3.2.3).
90
+ this.measuredPrefixLength = messages.length;
91
+ }
92
+ /**
93
+ * Any host-side history mutation that is NOT an append (hardening §3.2.3).
94
+ *
95
+ * `/clear`, `/resume`, and the splice itself: after any of them a recorded
96
+ * index means nothing, and the delta would be measured against the wrong
97
+ * array. `estimateAppendedTokens` bounds-checks as well, so a MISSED call here
98
+ * costs accuracy - the number degrades to round 1's - and never correctness.
99
+ */
100
+ invalidateMeasurement() {
101
+ this.measuredPrefixLength = undefined;
102
+ }
103
+ /**
104
+ * The dropped messages, released as they are returned (W4 / RV-3).
105
+ *
106
+ * `take` RATHER THAN `get`: the reference is dropped on read, so a wiring that
107
+ * declines to archive (key off, or `applied: false`) cannot pin it and a second
108
+ * read cannot double-write. `settlePending` calls this on EVERY verdict and
109
+ * discards on the ones that do not archive, which is what guarantees the array
110
+ * cannot outlive one compaction regardless of which branch was taken.
111
+ */
112
+ takeLastDropped() {
113
+ const dropped = this.lastDropped;
114
+ this.lastDropped = null;
115
+ return dropped;
116
+ }
117
+ /** Cancel an in-flight summarization (Esc, `dispose`). */
118
+ abort() {
119
+ if (this.callAbort) {
120
+ this.callAbortReason = 'cancelled';
121
+ this.callAbort.abort();
122
+ }
123
+ }
124
+ // =========================================================================
125
+ // The manual queue (§4.4 / D-17)
126
+ // =========================================================================
127
+ /**
128
+ * Ask for a compaction at the next turn boundary.
129
+ *
130
+ * THE BOUNDARY IS THE ONLY MOMENT THE ENGINE IS SINGLE-THREADED WITH RESPECT
131
+ * TO THE HISTORY, by construction. Replacing `messages` under a live loop is a
132
+ * race; a flag `shouldCompact` honours there is not.
133
+ */
134
+ queueManual(instructions) {
135
+ this.pendingManual = instructions ? { instructions } : {};
136
+ }
137
+ hasPendingManual() {
138
+ return this.pendingManual !== null;
139
+ }
140
+ clearPendingManual() {
141
+ this.pendingManual = null;
142
+ }
143
+ // =========================================================================
144
+ // ContextManager — the sync gate
145
+ // =========================================================================
146
+ /**
147
+ * SYNCHRONOUS AND CHEAP. Two deliberate effects: the pressure cache, and
148
+ * `deps.onPressure` (hardening DH-15).
149
+ *
150
+ * THE MEASUREMENT IS TAKEN ONCE, ABOVE THE GUARDS, AND REPORTED
151
+ * UNCONDITIONALLY. Below them it is skipped on five paths — the per-run cap,
152
+ * the cooldown, manual, overflow and guard 4 — and TWO OF THOSE NEVER CLEAR
153
+ * WITHIN A RUN. A gauge fed from the measurement would therefore go dark
154
+ * exactly when compaction is exhausted and the window is still filling, which
155
+ * is the one state where the corrected number is the user's only warning. It
156
+ * would silently revert to the measured base — the very figure W1 exists
157
+ * because it under-reports.
158
+ *
159
+ * `!config.enabled` STAYS THE FIRST STATEMENT, so a session that turned the
160
+ * feature off pays nothing at all and AC-1's byte-identity claim is untouched.
161
+ */
162
+ shouldCompact(probe) {
163
+ const config = this.deps.getConfig().compaction;
164
+ if (!config.enabled)
165
+ return false;
166
+ // THE HISTORY IS PASSED IN, NEVER OMITTED (§3.4.2). Without it the estimate
167
+ // branch of `computePressure` measures an EMPTY array and reports 0 %, so a
168
+ // resumed 180 k-token session — where `lastUsage` is `undefined` by
169
+ // definition and this branch is the only one available — would never trigger.
170
+ // The measured branch reads `messages` only to estimate what was appended
171
+ // after the measurement (hardening §3.2.2); the slice is one turn wide.
172
+ const pressure = this.measure({
173
+ ...(probe.lastUsage ? { lastUsage: probe.lastUsage } : {}),
174
+ messages: this.deps.getMessages(),
175
+ systemPrompt: this.deps.getSystemPrompt(),
176
+ });
177
+ this.deps.onPressure?.(pressure);
178
+ const manual = this.pendingManual !== null;
179
+ // Guard 2 — the per-run cap. BOTH TRIGGERS, HARD. It is the bound that makes
180
+ // every other guard's failure mode "spends a little" rather than "spends
181
+ // forever". Read from the INSTANCE, not the module: a child manager gets a
182
+ // tighter one (W3 / DH-16).
183
+ if (this.compactionsThisRun >= this.maxPerRun)
184
+ return false;
185
+ // Guard 1 — cooldown, in `turnIndex` units. A compaction that just ran needs
186
+ // a real turn before its effect is measurable, because `lastUsage` is still
187
+ // the pre-compaction number until the next `turn_end`.
188
+ //
189
+ // BYPASSED FOR OVERFLOW, and that is not a courtesy: the cooldown cannot be
190
+ // allowed to block the only path that makes an already-refused request
191
+ // sendable. A manual request is a user instruction and bypasses it too.
192
+ const cooled = probe.turnIndex - this.lastCompactionTurn >= COMPACTION_LIMITS.minTurnsBetween;
193
+ if (probe.trigger !== 'overflow' && !manual && !cooled)
194
+ return false;
195
+ // An explicit `/compact` is a user instruction: it does not consult the
196
+ // threshold, and guard 4 does not suppress it.
197
+ if (manual)
198
+ return true;
199
+ // The reactive path is answering a request the provider has ALREADY REFUSED,
200
+ // so it never consults occupancy — and guard 4 must not be able to switch it
201
+ // off, which is the entire answer to R-5 (D-24 / P1-13).
202
+ if (probe.trigger === 'overflow')
203
+ return true;
204
+ // Guard 4 — self-disable suppresses the PRESSURE trigger only.
205
+ if (this.selfDisabled)
206
+ return false;
207
+ return shouldCompactAt(pressure, config.threshold, {
208
+ maxOutputTokens: this.effectiveMaxOutputTokens(),
209
+ });
210
+ }
211
+ // =========================================================================
212
+ // ContextManager — the work
213
+ // =========================================================================
214
+ async compact(ctx) {
215
+ const manual = this.pendingManual;
216
+ const instructions = manual?.instructions;
217
+ this.pendingManual = null;
218
+ const uiTrigger = manual ? 'manual' : ctx.trigger;
219
+ const config = this.deps.getConfig().compaction;
220
+ const summarizer = this.resolveSummarizer();
221
+ this.compactionsThisRun += 1;
222
+ this.lastCompactionTurn = ctx.turnIndex;
223
+ this.compactions += 1;
224
+ this.inFlight = true;
225
+ const index = this.compactions;
226
+ this.deps.emit({
227
+ type: 'compaction_start',
228
+ index,
229
+ trigger: uiTrigger,
230
+ model: summarizer?.ref.modelId ?? '',
231
+ });
232
+ const started = Date.now();
233
+ try {
234
+ return await this.runCompaction({
235
+ ctx,
236
+ index,
237
+ uiTrigger,
238
+ config,
239
+ summarizer,
240
+ ...(instructions ? { instructions } : {}),
241
+ started,
242
+ });
243
+ }
244
+ finally {
245
+ this.inFlight = false;
246
+ this.callAbort = null;
247
+ this.callAbortReason = null;
248
+ }
249
+ }
250
+ // =========================================================================
251
+ // Internals
252
+ // =========================================================================
253
+ async runCompaction(args) {
254
+ const { ctx, config, summarizer } = args;
255
+ const before = ctx.messages;
256
+ const protectedPrefix = countProtectedPrefix(before);
257
+ // THE ONE LEVER THE GUARDS DO NOT HAVE (D-24). A tail that alone exceeds the
258
+ // window is the single failure mode none of the four guards can fix — they
259
+ // can only stop spending on it. An overflow attempt halves `keepRecentTurns`
260
+ // FOR ITSELF, in memory, never persisted: it is applied exactly where the
261
+ // provider has PROVEN the request impossible.
262
+ const keepRecentTurns = ctx.trigger === 'overflow'
263
+ ? Math.max(1, Math.floor(config.keepRecentTurns / 2))
264
+ : config.keepRecentTurns;
265
+ const plan = planCompaction(before, { keepRecentTurns, protectedPrefix });
266
+ if (!plan) {
267
+ // THE CASE THAT KILLS A RUN TODAY (hardening §3.3.3 / W2). "Nothing to
268
+ // drop" and "the tail is too big" are the SAME outcome in round 1, and both
269
+ // are terminal: every rung of the ladder operates on the head, and there is
270
+ // no head left. Relief is the one lever that still applies, so the
271
+ // `plan === null` branch tries it before conceding.
272
+ const reliefOnly = this.relieveInPlace(before, protectedPrefix, ctx.systemPrompt);
273
+ if (reliefOnly) {
274
+ this.checkProgress(ctx, args.uiTrigger, reliefOnly.messages);
275
+ return this.finish({ ...args, tailRelief: reliefSummary(reliefOnly) }, {
276
+ action: 'replace',
277
+ // `truncated` ACROSS THE PORT (DH-5 / DH-20). The engine's question
278
+ // is only "was this history reduced without a summary", and the
279
+ // answer is yes. `finish` maps this reason to the CLI's own
280
+ // `'relieved'`, which is what stops the card claiming a summary that
281
+ // never ran.
282
+ mode: 'truncated',
283
+ messages: reliefOnly.messages,
284
+ reason: 'tail_relief_only',
285
+ }, { droppedMessages: 0 }, keepRecentTurns);
286
+ }
287
+ this.noteNoProgress(args.uiTrigger, 'nothing_to_drop');
288
+ return this.finish(args, { action: 'keep', reason: 'nothing_to_drop' }, null, keepRecentTurns);
289
+ }
290
+ const anchor = buildAnchor(before);
291
+ if (!anchor) {
292
+ // A history with no `user` message at all cannot have a goal anchor, and
293
+ // D-6 makes the anchor non-negotiable. Refusing is the honest outcome.
294
+ return this.finish(args, { action: 'keep', reason: 'no_anchor' }, null, keepRecentTurns);
295
+ }
296
+ const head = before.slice(protectedPrefix, plan.cutIndex);
297
+ const priorBlock = before.slice(0, protectedPrefix).find((m) => isPriorSummaryBlock(m));
298
+ // The prior block is carried into the DIGEST rather than the tail, so the
299
+ // summarizer merges it forward (D-22).
300
+ const digestHead = priorBlock ? [priorBlock, ...head] : head;
301
+ const digest = buildDigest({
302
+ head: digestHead,
303
+ ...(summarizer
304
+ ? { summarizerWindow: this.deps.getModelInfoFor(summarizer.ref).contextWindow }
305
+ : {}),
306
+ });
307
+ const ladder = await this.runLadder({
308
+ ctx,
309
+ summarizer,
310
+ digest: digest.text,
311
+ hasPriorSummary: priorBlock !== undefined,
312
+ ...(args.instructions ? { instructions: args.instructions } : {}),
313
+ });
314
+ if (ladder.kind === 'aborted') {
315
+ return this.finish(args, { action: 'keep', reason: 'aborted' }, null, keepRecentTurns);
316
+ }
317
+ if (ladder.kind === 'failed' && config.onFailure === 'stop') {
318
+ // Rung 4. The run proceeds and will probably hit `context_overflow`, which
319
+ // is what the user asked for by setting this.
320
+ this.deps.notify('error', `Context compaction failed (${ladder.reason}) and compaction.onFailure is "stop". ` +
321
+ 'The next request may exceed the context window. Try /compact again, or /clear, or /reset.');
322
+ return this.finish(args, { action: 'keep', reason: `summarize_failed: ${ladder.reason}` }, null, keepRecentTurns);
323
+ }
324
+ const truncated = ladder.kind === 'failed';
325
+ const mode = truncated ? 'truncated' : 'summarized';
326
+ const generation = readGeneration(before) + 1;
327
+ const block = buildCompactedBlock({
328
+ summary: truncated ? truncationBody(plan.droppedMessages) : ladder.text,
329
+ replaced: plan.droppedMessages,
330
+ turns: countUserTurns(head),
331
+ anchor: anchor.shape,
332
+ generation,
333
+ truncated,
334
+ });
335
+ const spliced = [anchor.message, block, ...before.slice(plan.cutIndex)];
336
+ // THE TAIL'S OWN SIZE IS ONLY KNOWABLE AFTER THE HEAD IS GONE. Projecting it
337
+ // before the splice would be projecting against a number that is about to
338
+ // change by 80 %. `from: 2` because the anchor and the block are never
339
+ // eligible: the anchor is the user's own goal statement (D-6 makes it
340
+ // non-negotiable) and the block is the summary this compaction just paid for.
341
+ const relief = this.relieveInPlace(spliced, 2, ctx.systemPrompt);
342
+ const messages = relief ? relief.messages : spliced;
343
+ // ARCHIVED BEFORE THE VERDICT, HELD UNTIL THE WIRING COLLECTS IT (W4 / RV-3).
344
+ // `head` is the only place the dropped messages exist as an array, and this
345
+ // function has returned long before `settlePending` runs. Retained ONLY when
346
+ // the key is on, so `compaction.archive: false` never holds a reference.
347
+ if (config.archive)
348
+ this.lastDropped = [...head];
349
+ if (truncated) {
350
+ // Rung 3 is a VISIBLE, BOUNDED DEGRADATION rather than silent data loss:
351
+ // it says so on the card, on the JSON event stream, and inside the block
352
+ // the model itself reads.
353
+ this.deps.notify('warn', `Context compaction could not summarize (${ladder.reason}); ` +
354
+ `${plan.droppedMessages} earlier messages were dropped without a summary.`);
355
+ }
356
+ this.generation = generation;
357
+ // GUARD 3 SEES THE RELIEVED HISTORY, so relief counts as progress for free -
358
+ // `checkProgress` measures the final array, which is the one being adopted.
359
+ // A test pins it (AC-H7), because "for free" is the kind of property a
360
+ // refactor breaks silently.
361
+ this.checkProgress(ctx, args.uiTrigger, messages);
362
+ return this.finish({ ...args, ...(relief ? { tailRelief: reliefSummary(relief) } : {}) }, {
363
+ action: 'replace',
364
+ mode,
365
+ messages,
366
+ ...(truncated ? {} : { summary: ladder.text }),
367
+ ...(truncated ? { reason: `truncated: ${ladder.reason}` } : {}),
368
+ }, { droppedMessages: plan.droppedMessages, summary: truncated ? undefined : ladder.text }, keepRecentTurns);
369
+ }
370
+ /** Tail relief against this session's window (hardening §3.3.3 / W2). */
371
+ relieveInPlace(messages, from, systemPrompt) {
372
+ return attemptTailRelief({
373
+ messages,
374
+ from,
375
+ systemPrompt,
376
+ budget: tailBudgetTokens({
377
+ contextWindow: this.modelInfo().contextWindow,
378
+ maxOutputTokens: this.effectiveMaxOutputTokens(),
379
+ }),
380
+ log: (event, fields) => this.log.info(event, fields),
381
+ });
382
+ }
383
+ /**
384
+ * The failure ladder (§3.7).
385
+ *
386
+ * Rung 1 is the fast tier (or the main model); rung 2 is ONE retry on the MAIN
387
+ * model when rung 1 used the fast tier, because "a fast model that cannot
388
+ * summarize" is the most likely single failure. Rungs 3 and 4 are the caller's,
389
+ * because they depend on `onFailure`.
390
+ */
391
+ async runLadder(args) {
392
+ const { summarizer } = args;
393
+ if (!summarizer)
394
+ return { kind: 'failed', reason: 'no_summarizer_model' };
395
+ const first = await this.callOnce(summarizer.ref, args);
396
+ if (first.kind === 'ok' || first.kind === 'aborted')
397
+ return first;
398
+ // Rung 2 — retry, on the MAIN model when rung 1 was the fast tier.
399
+ const mainRef = this.mainRef();
400
+ const retryRef = summarizer.fromFastTier && mainRef.modelId !== summarizer.ref.modelId ? mainRef : summarizer.ref;
401
+ this.log.warn('summarize_retry', { reason: first.reason, model: retryRef.modelId });
402
+ const second = await this.callOnce(retryRef, args);
403
+ if (second.kind === 'ok' || second.kind === 'aborted')
404
+ return second;
405
+ return { kind: 'failed', reason: second.reason };
406
+ }
407
+ async callOnce(ref, args) {
408
+ const apiKey = this.deps.getApiKey(ref.providerId);
409
+ if (!apiKey)
410
+ return { kind: 'failed', reason: 'no_api_key' };
411
+ if (args.ctx.signal.aborted)
412
+ return { kind: 'aborted' };
413
+ // The controller is owned HERE, not in `summarize-call.ts`, so the caller can
414
+ // stamp WHICH of "timeout" and "cancelled" happened before aborting — the
415
+ // distinction the retry layer flattens away (see that file's header).
416
+ const controller = new AbortController();
417
+ this.callAbort = controller;
418
+ this.callAbortReason = null;
419
+ const onRunAbort = () => {
420
+ this.callAbortReason = 'cancelled';
421
+ controller.abort();
422
+ };
423
+ args.ctx.signal.addEventListener('abort', onRunAbort, { once: true });
424
+ try {
425
+ const outcome = await runSummarizeCall({
426
+ ref,
427
+ apiKey,
428
+ digest: args.digest,
429
+ hasPriorSummary: args.hasPriorSummary,
430
+ ...(args.instructions ? { instructions: args.instructions } : {}),
431
+ signal: controller.signal,
432
+ now: Date.now(),
433
+ complete: (providerId, request) => this.deps.complete(providerId, request),
434
+ onTimeout: () => {
435
+ this.callAbortReason = 'timeout';
436
+ controller.abort();
437
+ },
438
+ });
439
+ if (!outcome.ok) {
440
+ if (this.callAbortReason === 'cancelled' || args.ctx.signal.aborted)
441
+ return { kind: 'aborted' };
442
+ const reason = this.callAbortReason === 'timeout' ? 'timeout' : errorText(outcome.error);
443
+ return { kind: 'failed', reason };
444
+ }
445
+ this.noteUsage(ref, outcome.usage);
446
+ if (outcome.text.trim().length === 0)
447
+ return { kind: 'failed', reason: 'empty_summary' };
448
+ return { kind: 'ok', text: outcome.text.trim() };
449
+ }
450
+ finally {
451
+ args.ctx.signal.removeEventListener('abort', onRunAbort);
452
+ }
453
+ }
454
+ /**
455
+ * Guard 3 — the progress requirement (§3.8).
456
+ *
457
+ * ADVISORY ON OVERFLOW, NEVER A VETO (D-24 / P1-13). The arithmetic that makes
458
+ * this guard sensible for the proactive trigger INVERTS on the reactive one: a
459
+ * pressure compaction that reclaims little has spent money to buy two turns and
460
+ * should stop, while an overflow compaction that reclaims little is the
461
+ * difference between a continuing run and a dead one, because the provider has
462
+ * already refused the request.
463
+ *
464
+ * The projection is ESTIMATED — no usage exists for a history that has not been
465
+ * sent yet — and is therefore CALIBRATED with `estimateOffset` (D-23).
466
+ *
467
+ * IT TAKES THE *UI* TRIGGER, not `ctx.trigger`. A queued `/compact` reaches the
468
+ * loop as an ordinary `pressure` checkpoint (that is what the queue IS), so
469
+ * charging its result to guard 4's counter would let a user who types
470
+ * `/compact` twice on a short conversation self-disable the proactive trigger
471
+ * for the rest of the session — and be told, wrongly, that their recent turns
472
+ * exceed the threshold. `uiTrigger` is the only value that knows the difference.
473
+ */
474
+ checkProgress(ctx, uiTrigger, after) {
475
+ const config = this.deps.getConfig().compaction;
476
+ const info = this.modelInfo();
477
+ const projected = computePressure({
478
+ messages: after,
479
+ systemPrompt: ctx.systemPrompt,
480
+ contextWindow: info.contextWindow,
481
+ windowKnown: true,
482
+ ...(this.estimateOffset !== undefined ? { estimateOffset: this.estimateOffset } : {}),
483
+ });
484
+ const target = config.threshold - COMPACTION_LIMITS.minReclaimRatio;
485
+ const madeProgress = projected.ratio < target;
486
+ this.log.info('compaction_progress', {
487
+ trigger: uiTrigger,
488
+ projectedRatio: Number(projected.ratio.toFixed(3)),
489
+ target: Number(target.toFixed(3)),
490
+ madeProgress,
491
+ });
492
+ if (madeProgress) {
493
+ this.consecutiveNoProgress = 0;
494
+ return;
495
+ }
496
+ this.noteNoProgress(uiTrigger, 'insufficient_reclaim');
497
+ }
498
+ /**
499
+ * Guard 4 — stuck detection (§3.8).
500
+ *
501
+ * PRESSURE ONLY, AND `pressure` HERE MEANS THE *UI* TRIGGER. A session that
502
+ * self-disables still survives an overflow rather than dying at the next
503
+ * request, and if the tail really is too large the overflow attempt is the one
504
+ * that can do something about it (D-24). A `manual` compaction is excluded for
505
+ * a different reason: this guard exists to stop UNREQUESTED spend, and
506
+ * `/compact` on a conversation with nothing worth dropping is a user who asked
507
+ * — twice — not a runaway loop.
508
+ */
509
+ noteNoProgress(trigger, reason) {
510
+ if (trigger !== 'pressure')
511
+ return;
512
+ this.consecutiveNoProgress += 1;
513
+ if (this.consecutiveNoProgress < COMPACTION_LIMITS.stuckLimit || this.selfDisabled)
514
+ return;
515
+ this.selfDisabled = true;
516
+ this.selfDisabledReason = reason;
517
+ this.log.warn('compaction_self_disabled', { reason });
518
+ // THE WORDING NAMES THE ACTUAL CAUSE AND THE ACTUAL REMEDIES. A notice that
519
+ // says only "compaction is off" teaches the user that the feature is broken;
520
+ // this one tells them which number to change.
521
+ this.deps.notify('warn', 'Auto-compaction is off for this session: the most recent ' +
522
+ `${this.deps.getConfig().compaction.keepRecentTurns} turns alone exceed the threshold. ` +
523
+ 'Lower compaction.keepRecentTurns, or use /clear or /reset.');
524
+ }
525
+ finish(args, outcome, applied, keepRecentTurnsUsed) {
526
+ const before = args.ctx.messages.length;
527
+ const after = outcome.action === 'replace' ? outcome.messages.length : before;
528
+ const tokensBefore = this.lastPressure?.occupied ?? 0;
529
+ const record = {
530
+ index: args.index,
531
+ trigger: args.uiTrigger,
532
+ // `'relieved'` IS A CLI-ONLY WORD (RV-5 / DH-20). The port reports
533
+ // `'truncated'` for a relief-only compaction, which is accurate for the
534
+ // engine's narrower question; the card's `summarized N messages with
535
+ // <model>` line would otherwise render "summarized 0 messages with
536
+ // claude-haiku-4-5" for a compaction in which no model was called.
537
+ mode: reliefOnly(outcome) ? 'relieved' : outcome.action === 'replace' ? outcome.mode : 'none',
538
+ // OPTIMISTIC AND CORRECTED BY THE WIRING (§3.2.1). Only the ENGINE knows
539
+ // whether the splice passed the structural gate, and it says so on
540
+ // `compaction_end`; the wiring rewrites this before the card settles.
541
+ applied: outcome.action === 'replace',
542
+ ...(outcome.action === 'keep' ? { reason: outcome.reason } : {}),
543
+ ...(outcome.action === 'replace' && outcome.reason ? { reason: outcome.reason } : {}),
544
+ messagesBefore: before,
545
+ messagesAfter: after,
546
+ tokensBefore,
547
+ tokensAfter: 0, // filled by the wiring from the engine's own estimator
548
+ ...(applied?.summary ? { summary: applied.summary } : {}),
549
+ // NO MODEL ON A RELIEF-ONLY RECORD, because no model was called. The card
550
+ // names the summarizer from this field, and naming one here would be the
551
+ // same claim `mode: 'relieved'` exists to stop making.
552
+ model: reliefOnly(outcome) ? '' : args.summarizer?.ref.modelId ?? '',
553
+ durationMs: Date.now() - args.started,
554
+ ...(args.ctx.trigger === 'overflow' ? { keepRecentTurnsUsed } : {}),
555
+ ...(args.tailRelief ? { tailRelief: args.tailRelief } : {}),
556
+ };
557
+ this.log.info('compaction', {
558
+ trigger: args.uiTrigger,
559
+ before,
560
+ after,
561
+ droppedMessages: applied?.droppedMessages ?? 0,
562
+ mode: record.mode,
563
+ model: record.model,
564
+ durationMs: record.durationMs,
565
+ });
566
+ this.deps.emit({ type: 'compaction_end', record });
567
+ return outcome;
568
+ }
569
+ // =========================================================================
570
+ // Reporting helpers
571
+ // =========================================================================
572
+ /**
573
+ * The pressure the trigger and the gauge both read.
574
+ *
575
+ * CACHED on every evaluation, so `/compact status` reports the same number the
576
+ * last decision used rather than recomputing one that has since moved.
577
+ */
578
+ measure(probe) {
579
+ const info = this.modelInfo();
580
+ const pressure = computePressure({
581
+ ...(probe.lastUsage ? { lastUsage: probe.lastUsage } : {}),
582
+ messages: probe.messages ?? [],
583
+ systemPrompt: probe.systemPrompt ?? '',
584
+ contextWindow: info.contextWindow,
585
+ windowKnown: this.deps.isPricedModel({
586
+ providerId: this.mainRef().providerId,
587
+ modelId: this.mainRef().modelId,
588
+ }),
589
+ ...(this.estimateOffset !== undefined ? { estimateOffset: this.estimateOffset } : {}),
590
+ ...(this.measuredPrefixLength !== undefined
591
+ ? { measuredPrefixLength: this.measuredPrefixLength }
592
+ : {}),
593
+ });
594
+ this.lastPressure = pressure;
595
+ return pressure;
596
+ }
597
+ lastMeasured() {
598
+ return this.lastPressure;
599
+ }
600
+ /** Written by the wiring once the engine has reported its verdict. */
601
+ recordApplied(tokensBefore, tokensAfter) {
602
+ const reclaimed = Math.max(0, tokensBefore - tokensAfter);
603
+ this.tokensReclaimed += reclaimed;
604
+ }
605
+ sessionTotals() {
606
+ return {
607
+ compactions: this.compactions,
608
+ tokensReclaimed: this.tokensReclaimed,
609
+ usage: this.sessionUsage,
610
+ pricingUnknown: this.pricingUnknown,
611
+ inFlight: this.inFlight,
612
+ selfDisabled: this.selfDisabled,
613
+ ...(this.selfDisabledReason ? { selfDisabledReason: this.selfDisabledReason } : {}),
614
+ generation: this.generation,
615
+ };
616
+ }
617
+ /** `/compact on` after a self-disable — the user overriding the guard. */
618
+ clearSelfDisable() {
619
+ this.selfDisabled = false;
620
+ this.selfDisabledReason = undefined;
621
+ this.consecutiveNoProgress = 0;
622
+ }
623
+ isSelfDisabled() {
624
+ return this.selfDisabled;
625
+ }
626
+ /**
627
+ * Which model summarizes (§3.6.4).
628
+ *
629
+ * SAY THE DEFAULT OUT LOUD (P2-8): FOR ALMOST EVERYONE THIS IS THE MAIN MODEL.
630
+ * `resolveFastTier` returns `{ok: false, reason: 'disabled'}` on
631
+ * `fast.enabled !== true` alone, and `fast.enabled` defaults to `false` — so
632
+ * `useFastTier: true` describes a PREFERENCE, not the common path, and the
633
+ * out-of-the-box first compaction is a ~30 k-token call on the session's own
634
+ * model. Three things make that the right trade rather than a hidden cost: it
635
+ * happens at most `maxPerRun` times per run, the alternative is losing the
636
+ * entire run, and the spend is on the card with the summarizer's own price.
637
+ */
638
+ resolveSummarizer() {
639
+ const config = this.deps.getConfig();
640
+ if (config.compaction.useFastTier) {
641
+ const tier = resolveFastTier(config, this.deps.hasKey);
642
+ if (tier.ok)
643
+ return { ref: tier.ref, fromFastTier: true };
644
+ }
645
+ const ref = this.mainRef();
646
+ if (!this.deps.hasKey(ref.providerId))
647
+ return null;
648
+ return { ref, fromFastTier: false };
649
+ }
650
+ mainRef() {
651
+ const config = this.deps.getConfig();
652
+ return {
653
+ providerId: config.provider,
654
+ modelId: config.model,
655
+ ...(config.baseUrl ? { baseUrl: config.baseUrl } : {}),
656
+ };
657
+ }
658
+ modelInfo() {
659
+ return this.deps.getModelInfoFor(this.mainRef());
660
+ }
661
+ effectiveMaxOutputTokens() {
662
+ const config = this.deps.getConfig();
663
+ // `undefined` means AUTO; the model's own ceiling is the honest bound then.
664
+ return config.maxTokens ?? this.modelInfo().maxOutputTokens;
665
+ }
666
+ noteUsage(ref, usage) {
667
+ this.sessionUsage = accumulateUsage(this.sessionUsage, usage);
668
+ // UNKNOWN PRICING IS NOT ZERO PRICING. It is sticky: once a summarization has
669
+ // run on an unpriced model the session total is no longer a complete figure,
670
+ // and a later priced call does not make it one again.
671
+ if (!this.deps.isPricedModel(ref))
672
+ this.pricingUnknown = true;
673
+ this.deps.emit({ type: 'usage', usage });
674
+ }
675
+ }
676
+ /** A "turn" is a `user`-role message; `tool_result` is a separate role. */
677
+ function countUserTurns(messages) {
678
+ return messages.reduce((acc, m) => (m.role === 'user' ? acc + 1 : acc), 0);
679
+ }
680
+ /**
681
+ * The relief-only outcome, recognized by the one reason string that produces it.
682
+ *
683
+ * A REASON RATHER THAN A NEW `mode` ON THE PORT (DH-5): `CompactionOutcome.mode`
684
+ * is a public contract with two members, and widening it to carry a distinction
685
+ * only the CLI's card acts on would be a breaking change for a reporting nicety.
686
+ */
687
+ function reliefOnly(outcome) {
688
+ return outcome.action === 'replace' && outcome.reason === 'tail_relief_only';
689
+ }