Repository navigation
Expand file tree
/
Copy pathcompaction.ts
More file actions
1237 lines (1130 loc) · 58.1 KB
/
Copy pathcompaction.ts
File metadata and controls
1237 lines (1130 loc) · 58.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
export * as SessionCompaction from "./compaction.js"
import {
AIError,
type ContentPart,
InvalidProviderOutputError,
InvalidRequestError,
isContextOverflowFailure,
LLMClient,
LLMEvent,
LLMRequest,
Message,
type ToolEntry,
UnknownProviderError,
type Usage,
} from "@opencode/ai/effect"
import type { StreamOptions } from "@opencode/ai/route"
import type { SessionCompactionResult } from "@opencode/plugin/effect/session"
import type { SessionError } from "@opencode/schema/session-error"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import { Context, Effect, Exit, Fiber, Layer, Result, Scope, Stream } from "effect"
import { Agent } from "../agent.js"
import { Bus } from "../bus.js"
import { Database } from "../database/database.js"
import { llmClient } from "../effect/app-node-platform.js"
import { Instructions } from "../instructions/index.js"
import { Model } from "../model.js"
import { State } from "../state.js"
import { Token } from "../util/token.js"
import type { SessionContext } from "./context.js"
import { SessionEvent } from "./event.js"
import { SessionHistory } from "./history.js"
import type { SessionMessage } from "./message.js"
import { SessionModelRequest } from "./model-request.js"
import { SessionRunnerModel } from "./runner/model.js"
import { SessionRunnerRetry } from "./runner/retry.js"
import { toLLMMessages } from "./runner/to-llm-message.js"
import { SessionRecentText } from "./recent-text.js"
import { contentFilterError, toSessionError } from "./to-session-error.js"
import { SessionUsage } from "./usage.js"
export type Settings = {
auto: boolean
/** Tokens kept free below the model's limits before compacting. Unset keeps 15%, and at least 16k, free. */
buffer?: number
/** Tokens of recent conversation kept verbatim beside the summary. */
keep: number
/** Summarize in the background as the context nears the threshold, so automatic compaction can swap it in. */
precompute: boolean
}
export type Editor = {
configure: (settings: Partial<Settings>) => void
}
/** A prompt's size: `measured` is what the provider reported at the latest response, `estimated` is the text since. */
export type Estimate = { readonly measured: number; readonly estimated: number }
export type Trigger =
/** `overflow`: the provider just rejected this context as too long. `estimate` is `estimatePrompt(context)`. */
| { readonly reason: "auto" | "overflow"; readonly context: SessionContext.Loaded; readonly estimate?: Estimate }
/** `inputID` is the `/compact` inbox item, whose message shows the outcome. */
| { readonly reason: "manual"; readonly context: SessionContext.Loaded; readonly inputID: SessionMessage.ID }
export type Outcome =
/** Only `auto` skips: the context fits, or automatic compaction is off. */
| { readonly status: "skipped" }
| { readonly status: "completed" }
| { readonly status: "failed"; readonly error: SessionError.Error }
export interface Interface extends State.Transformable<Editor> {
readonly compact: (trigger: Trigger) => Effect.Effect<Outcome>
/**
* Starts a background summary when the context nears the threshold. Never waits for it. Loads `context` only when
* background summaries are on. `estimate`, when given, is `estimatePrompt` of the loaded context.
*/
readonly precompute: <E>(
context: Effect.Effect<SessionContext.Loaded, E>,
estimate?: Estimate,
) => Effect.Effect<void, E>
}
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionCompaction") {}
/**
* A summary fills `text`, and references the messages it keeps verbatim with `metadata.keepID`; history
* rebuilds their text from that. A native compaction fills `native.messages` with its replacement window.
*/
type Result = {
readonly text: string
readonly native?: SessionMessage.Native
readonly metadata?: Record<string, unknown>
}
type Failure = {
readonly error: SessionError.Error
}
type Prepared = Effect.Success<ReturnType<SessionModelRequest.Interface["compaction"]>>
type Streamed = {
readonly text: string
}
/** A summary run off the critical path: it covers only `messages`, streams no deltas, and bills into `usage`. */
type Background = {
readonly messages: ReadonlyArray<SessionMessage.Info>
readonly usage: (usage: SessionUsage.Recorded) => void
}
/** A background summary of everything before `keepID`, held until automatic compaction is due. */
type Precomputed = {
readonly firstID: SessionMessage.ID | undefined
readonly keepID: SessionMessage.ID
readonly model: SessionContext.Loaded["model"]["ref"]
readonly initial: string
/** The longest tail since `keepID` that compaction still accepts beside this summary. */
readonly tailLimit: number
/** Background summaries that failed since the last compaction, this one included once it has. */
readonly failures: number
readonly usage: SessionUsage.Recorded[]
readonly fiber: Fiber.Fiber<Result, Failure>
}
const NOTHING_TO_COMPACT: Failure = { error: { type: "compaction.unavailable", message: "Nothing to compact yet" } }
/** After each "too long" rejection, the next attempt aims at this share of the first rejected request's size. */
const SHRINK_STEPS = [0.85, 0.7, 0.5, 0.35]
// The least of the window kept free for the last reply before compaction and for the summary itself.
const RESERVE_MIN = 16_000
/**
* Share of the window kept free before automatic compaction. Above `RESERVE_MIN`, the margin between it and
* `summaryRoom` absorbs a large step past the trigger, so the whole conversation still goes out unchanged and reuses
* the cache. Single steps grew by up to ~22k tokens at p99 in real sessions.
*/
const RESERVE_SHARE = 0.15
/** A common window size, assumed for the compaction request when the model's window is unknown. */
const UNKNOWN_WINDOW = 200_000
/** A summary gets at least this much output, or the room left if less, even when the estimate margin leaves less. */
const SUMMARY_OUTPUT_FLOOR = 32_000
/**
* How far one exchange of a summary transcript is shortened, in the order the levels apply: whole; tool outputs cut;
* tool outputs left out and tool inputs cut; only the user's text, cut; omitted.
*/
const OUTPUTS_CLIPPED = 1
const OUTPUTS_REMOVED = 2
const USER_ONLY = 3
const OMITTED = 4
const TRANSCRIPT_LEVELS = [0, OUTPUTS_CLIPPED, OUTPUTS_REMOVED, USER_ONLY, OMITTED]
/** Characters kept, from the start and end, of a cut tool input, tool output, or user message. */
const TRANSCRIPT_CLIP = 2_000
const IMAGE_TOKEN_ESTIMATE = 1_500
const PDF_TOKEN_ESTIMATE = 2_000
/** Share of the compaction ceiling at which a background summary starts. */
const PRECOMPUTE_RATIO = 0.85
/** Measured prompt tokens below which a background summary is not worth its cost. */
const PRECOMPUTE_MIN_TOKENS = 64_000
/**
* The most text a swapped-in summary keeps verbatim beside it: twice `keep`, at least 30k, at most a quarter of the
* budget. A longer tail would leave the context near the threshold, so compaction summarizes from scratch instead.
*/
const PRECOMPUTED_TAIL_MIN = 30_000
const PRECOMPUTED_TAIL_SHARE = 0.25
/** Failed background summaries after which no new one starts until the next compaction. */
const PRECOMPUTE_MAX_FAILURES = 2
const SUMMARY_TEMPLATE = `You MUST use this format for your response (you may omit sections that aren't applicable). Do not include the <template> tags in your response.
<template>
## Objective
- [one or two brief sentences describing what the user is trying to accomplish]
## Requirements
- [constraints, preferences, requirements, and scope boundaries stated by the user, or "(none)"]
## Decisions
- [decisions already made and why, or "(none)"]
## Work State
Break the objective into smaller goals and report which are completed, which are being worked on, and which are blocked.
### Completed
- [goals that have been completed; otherwise "(none)"]
### Active
- [goals currently being worked on; otherwise "(none)"]
### Blocked
- [anything blocking progress, and why; otherwise "(none)"]
## Next Move
1. [ordered list of next actions, or "(none)"]
## Relevant Files
List the files and directories, other than the current working directory, that another agent would need to open to continue this work. Include at most 15, most important first. Do not list every file that was read or changed. Include paths outside the current working directory when relevant. If none, write "(none)".
- \`[file or directory path]\`: [brief reason it matters]
## Important Context
- [facts the next agent cannot continue without and cannot easily find on its own; or "(none)"]
</template>`
const SUMMARY_RULES = `Rules:
- Keep each section concise. Use terse, single-line bullets, not prose paragraphs or nested lists.
- Prefer short references over detailed restatement. It is fine to leave out information the next agent can recover from the code or the files listed above.
- Preserve exact file paths, symbols, commands, error strings, URLs, and identifiers.
- Carry forward only user questions or requests that remain unanswered or require further action. Do not repeat ones that newer history has answered or resolved. Preserve exact wording when carrying one forward.
- Preserve consequential workflow state, including whether changes are uncommitted, committed, pushed, under review, or merged.
- Do not mention the summary process or that context was compacted.`
export const buildPrompt = (update: boolean, legacy = false) => {
const shared = [
"Summarize only what the user and the assistant said and did. Leave out instructions and setup the assistant was given rather than told by the user: repository conventions, instruction files such as AGENTS.md, and environment details like the session ID. The next agent receives current versions of all of these separately.",
SUMMARY_TEMPLATE,
SUMMARY_RULES,
"Do not continue the task or call tools.",
"Return only the structured summary in the requested format. Do not include a preamble, explanation, or other commentary.",
]
if (!update) {
return [
"You MUST summarize the conversation above into a structured summary that will be given to another agent to resume the work.",
...shared,
].join("\n\n")
}
return [
"Update the existing checkpoint in the conversation above into one consolidated summary.",
// Before #48058 (Sep 2026), summaries kept nearly everything: every file touched, per-edit changelogs,
// restated AGENTS.md conventions, and an "Additional Context" section that grew on every update. The
// preserve-first update prompt would carry that detail forward indefinitely, so a checkpoint from that
// template is rewritten once at the current level of detail. The rewrite uses the new headings, so later
// updates skip this. Remove once no session still in use has a latest checkpoint older than #48058.
...(legacy
? [
"The existing checkpoint was written with an earlier format that recorded far more detail than this one asks for. Rewrite it at the level of detail described below rather than carrying its detail forward. Keep its requirements, decisions, and open questions; they came from earlier conversation with the user.",
]
: []),
"Newer history always takes precedence over the existing checkpoint. Preserve previous information unless newer history clearly contradicts, supersedes, resolves, or makes it stale. If something is no longer relevant to continuing the work, you may remove it.",
"Incorporate newer requirements, decisions, progress, and context. Reconcile Work State and Next Move: move completed work out of Active, remove resolved blockers and answered questions, and preserve unresolved or pending work.",
"Return only the updated Markdown sections. Do not reproduce the `<conversation-checkpoint>`, `<summary>`, or `<recent-context>` wrapper tags from the previous checkpoint.",
...shared,
].join("\n\n")
}
const NUDGE =
"The previous response did not fill in the required summary template. Do not call tools. Return the summary as text using the exact section headings from the template."
/** Summaries written with the previous template carry this catch-all heading. */
const LEGACY_HEADING = "## Additional Context"
export const layer = Layer.effect(
Service,
Effect.gen(function* () {
const agents = yield* Agent.Service
const bus = yield* Bus.Service
const llm = yield* LLMClient.Service
const catalog = yield* Model.Service
const models = yield* SessionRunnerModel.Service
const db = (yield* Database.Service).db
const requests = yield* SessionModelRequest.Service
const scope = yield* Scope.Scope
const precomputed = new Map<SessionContext.Loaded["session"]["id"], Precomputed>()
const state = State.create<Settings, Editor>({
name: "session-compaction",
initial: () => ({ auto: true, keep: 15_000, precompute: false }),
editor: (settings) => ({
configure: (update) => {
Object.assign(settings, update)
},
}),
})
/** The model that writes the summary, and the request size it aims for. */
const selectModel = Effect.fnUntraced(function* (
context: SessionContext.Loaded,
buffer: number | undefined,
native: boolean,
) {
const agent = native ? undefined : yield* agents.get(Agent.ID.make("compaction"))
const model =
(agent?.model &&
(yield* models
.resolve({ ...context.session, model: agent.model }, catalog.available)
.pipe(Effect.orElseSucceed(() => undefined)))) ??
context.model
const ceiling = calculateCeiling(model.limit, buffer)
// An unknown window never triggers auto compaction, but the compaction request still needs a size to aim for.
const cap = Number.isFinite(ceiling)
? ceiling
: calculateCeiling({ ...model.limit, context: UNKNOWN_WINDOW }, buffer)
return { context: model === context.model ? context : { ...context, model }, cap }
})
const compact = Effect.fn("SessionCompaction.compact")(function* (trigger: Trigger): Effect.fn.Return<Outcome> {
const settings = state.get()
const context = trigger.context
// Only the user compacts when automatic compaction is off, overflow included.
if (trigger.reason !== "manual" && !settings.auto) return { status: "skipped" }
if (
trigger.reason === "auto" &&
!due(context, calculateCeiling(context.model.limit, settings.buffer), trigger.estimate)
)
return { status: "skipped" }
const held = precomputed.get(context.session.id)
precomputed.delete(context.session.id)
const native = context.model.compaction?.type === "native"
const model = yield* selectModel(context, settings.buffer, native)
// The provider just rejected this context, so the estimate ran low; the first attempt already aims below it.
const budget =
trigger.reason === "overflow"
? Math.min(model.cap, Math.floor(total(trigger.estimate ?? estimatePrompt(context)) * SHRINK_STEPS[0]))
: model.cap
const selected = model.context === context ? trigger : { ...trigger, context: model.context }
// The runner opened the manual compaction's message when it delivered the `/compact` item. An automatic one
// opens here, before any wait, so a background summary still being written shows as a running compaction.
if (trigger.reason !== "manual")
yield* bus.publish(SessionEvent.Compaction.Started, { sessionID: context.session.id, reason: "auto" })
const swapped = held ? yield* usePrecomputed(selected, held, budget, settings) : undefined
const compaction = swapped
? Effect.succeed(swapped)
: native
? compactNatively(selected, budget, settings.keep)
: summarize(selected, budget, settings.keep)
return yield* compaction.pipe(
Effect.matchEffect({
onSuccess: (result) => publish(selected, result),
onFailure: (failure) => publish(selected, failure),
}),
)
})
/**
* Once the context passes `PRECOMPUTE_RATIO` of the ceiling, summarizes everything before the latest response in
* the background. That is the prefix the provider just processed, so the summary request reads it from the prompt
* cache. The summary is held, not applied: swapping it in now would rewrite the prefix and throw away the cache
* for every step until compaction is actually due. One summary is held per Session until compaction takes it, or
* until it fails or falls too far behind, when a newer response replaces it.
*/
const precompute = Effect.fn("SessionCompaction.precompute")(function* <E>(
load: Effect.Effect<SessionContext.Loaded, E>,
given?: Estimate,
) {
const settings = state.get()
if (!settings.precompute || !settings.auto) return
const context = yield* load
if (context.model.compaction?.type === "native") return
const ceiling = calculateCeiling(context.model.limit, settings.buffer)
if (!Number.isFinite(ceiling)) return
const estimate = given ?? estimatePrompt(context)
if (due(context, ceiling, estimate)) return
if (total(estimate) < ceiling * PRECOMPUTE_RATIO) return
const anchorIndex = context.messages.findLastIndex((message) => hasMeasuredPrompt(message, context.model.ref))
const anchor = context.messages[anchorIndex]
if (anchorIndex <= 0 || anchor?.type !== "assistant" || !anchor.tokens) return
if (anchor.tokens.input + anchor.tokens.cache.read + anchor.tokens.cache.write < PRECOMPUTE_MIN_TOKENS) return
const sessionID = context.session.id
const existing = precomputed.get(sessionID)
const exit = existing?.fiber.pollUnsafe()
const failed = exit !== undefined && Exit.isFailure(exit)
const failures = (existing?.failures ?? 0) + (failed ? 1 : 0)
if (existing) {
const index = heldIndex(existing, context)
if (index >= 0 && !failed && tailTokens(context, index) <= existing.tailLimit) return
// Summarizing up to the same response again would repeat the same request.
if (index >= 0 && (existing.keepID === anchor.id || failures >= PRECOMPUTE_MAX_FAILURES)) return
yield* Fiber.interrupt(existing.fiber)
precomputed.delete(sessionID)
}
// After a model switch nothing is cached for the new model yet; wait until its first response has written it.
if (anchor.model.id !== context.model.ref.id || anchor.model.variant !== context.model.ref.variant) return
const model = yield* selectModel(context, settings.buffer, false)
const usage: SessionUsage.Recorded[] = []
const fiber = yield* summarize({ reason: "auto", context: model.context }, model.cap, settings.keep, {
messages: context.messages.slice(0, anchorIndex),
usage: (recorded) => usage.push(recorded),
}).pipe(Effect.forkIn(scope))
precomputed.set(sessionID, {
firstID: context.messages[0]?.id,
keepID: anchor.id,
model: context.model.ref,
initial: Instructions.join(context.initial),
tailLimit: tailLimit(settings.keep, model.cap),
failures,
usage,
fiber,
})
})
/**
* The held summary plus everything since it, kept verbatim, when it still describes this conversation and that
* tail is short. Waits for a summary still being written. Undefined, after stopping the background work, when it
* cannot be used; compaction then summarizes as usual.
*/
const usePrecomputed = Effect.fnUntraced(function* (
trigger: Trigger,
held: Precomputed,
budget: number,
settings: Settings,
): Effect.fn.Return<Result | undefined> {
const context = trigger.context
const index = heldIndex(held, context)
const usable =
settings.precompute &&
trigger.reason !== "overflow" &&
context.model.compaction?.type !== "native" &&
index >= 0 &&
tailTokens(context, index) <= tailLimit(settings.keep, budget)
if (!usable) {
yield* Fiber.interrupt(held.fiber)
return undefined
}
const exit = yield* Fiber.await(held.fiber).pipe(Effect.onInterrupt(() => Fiber.interrupt(held.fiber)))
if (Exit.isFailure(exit)) return undefined
// Background calls were billed as they finished; the compaction's message still shows what it cost.
held.usage.forEach((usage) => addSpent(context.session.id, usage))
return { ...exit.value, metadata: { keepID: held.keepID, ...exit.value.metadata } }
})
const due = (context: SessionContext.Loaded, ceiling: number, estimate?: Estimate) => {
const messages = context.messages
// A compaction just completed; let the runner rebuild the request from it first.
const last = messages.at(-1)
if (last?.type === "compaction" && last.status === "completed") return false
// An encrypted native window estimates as nothing, so wait for a response to measure it.
const measured = messages.findLastIndex((message) => hasMeasuredPrompt(message, context.model.ref))
if (measured < messages.findLastIndex(SessionHistory.isNativeCompaction)) return false
return total(estimate ?? estimatePrompt(context)) >= ceiling
}
/**
* first: [E1 … E8][E9 E10] → [S1 + "E9 E10"]
* again: [S1 + "E9 E10"][E11 … E18][E19] → [S2 + "E19"] S2 covers the whole current conversation
*
* The model is sent the whole conversation, including the tail kept beside the summary, then the summary prompt.
* Keeping the normal request's prefix intact lets the summary reuse its prompt cache:
*
* [system][S1 + "E9 E10"][E11 … E18][E19][prompt] plus one nudge if the reply skips the template
*
* Later requests open with the checkpoint as one user message:
*
* <conversation-checkpoint>
* <summary>S2</summary>
* <recent-context>E19</recent-context>
* </conversation-checkpoint>
* [new messages…]
*
* E = one exchange: a user message and the replies to it. S = a summary. [S + "…"] = one checkpoint: a summary
* plus the newest `keep` tokens of messages, referenced by their first message and sent as text.
*/
const summarize = Effect.fnUntraced(function* (
trigger: Trigger,
budget: number,
keep: number,
background?: Background,
): Effect.fn.Return<Result, Failure> {
const context = trigger.context
const messages = background?.messages ?? context.messages
const split = splitConversation(messages, keep)
if (split === undefined) return yield* Effect.fail(NOTHING_TO_COMPACT)
// A background summary covers all of its messages; what follows them is kept when it is swapped in.
const keepID = background ? undefined : split.keepID
const previous = previousCompaction(messages)
const prompt = buildPrompt(previous !== undefined, previous?.summary.includes(LEGACY_HEADING) ?? false)
const headings = SUMMARY_TEMPLATE.split("\n").filter((line) => line.startsWith("##"))
const filled = (text: string) => text.split("\n").some((line) => headings.includes(line.trim()))
const prepared = yield* prepare(context, messages, budget)
// Size the unchanged request with the number that decided to compact: the last response's count plus an
// estimate of what came after it. Characters overstate a conversation whose old reasoning the provider drops.
// A hook can still make this request larger than that count; the provider then rejects it and it is resent as
// text. An overflow already proved the count low, so it keeps the character estimate.
const measured = trigger.reason === "overflow" ? undefined : measuredPrompt({ ...context, messages })
// Hooks saw the request without the summary prompt, so it is appended here. A reply that ignores the
// template gets one reminder before it counts as a failure.
const send = (request: LLMRequest, size?: Estimate) =>
Effect.gen(function* () {
const prompted = fitOutput(
LLMRequest.update(request, { messages: [...request.messages, Message.user(prompt)] }),
context.model.limit,
size && { ...size, estimated: size.estimated + Token.estimate(prompt) },
)
const reply = yield* stream(context, prompted, prepared.options, background)
if (filled(reply.text)) return reply
const nudged = fitOutput(
LLMRequest.update(prompted, { messages: [...prompted.messages, Message.user(NUDGE)] }),
context.model.limit,
size && { ...size, estimated: size.estimated + Token.estimate(prompt) + Token.estimate(NUDGE) },
)
const retry = yield* stream(context, nudged, prepared.options, background)
if (filled(retry.text)) return retry
return yield* Effect.fail<Failure>({
error: {
type: "compaction.failed",
message: retry.text.trim()
? "Compaction summary did not match the required template"
: "Compaction produced no summary",
},
})
})
const overhead = Token.estimate(prompt) + Token.estimate(NUDGE)
// Automatic compaction starts past the ceiling, so the whole conversation is sent unchanged while the window can
// still fit the summary. Below that, the history before the tail is sent instead, as before: unchanged when it fits,
// which still reuses the cache for that shorter prefix, and as text otherwise. Dropping exchanges to make room
// for the tail would leave them in neither the summary nor the tail. Where 15% of the window is under
// `RESERVE_MIN`, the ceiling equals `summaryRoom`, so automatic compaction always takes this fallback.
const fit =
trigger.reason === "overflow"
? budget
: Math.max(budget, summaryRoom(context.model.limit, prepared.request.generation?.maxTokens))
const result = yield* deliver(
trigger,
prepared,
budget - overhead,
send,
{ fit: fit - overhead, history: withoutTail(prepared.request, keepID), measured },
background,
)
if (!keepID) return result
// A compaction hook may choose its own kept range.
return { ...result, metadata: { keepID, ...result.metadata } }
})
/**
* endpoint: [window] → [U U][item] the provider picks which messages to keep
* trigger: [window][compaction_trigger] → [item]
* → [U U][item] we add the newest user messages within `keep`
*
* Later requests open with that replacement window in place of the history it covers:
*
* [U U][item][new messages…]
*
* window = the previous compaction and everything since. item = one encrypted compaction item, which only
* replays on the model that made it. U = a user message as typed.
*/
const compactNatively = Effect.fnUntraced(function* (
trigger: Trigger,
budget: number,
keep: number,
): Effect.fn.Return<Result, Failure> {
const context = trigger.context
if (!context.messages.some(SessionRecentText.messageToText)) return yield* Effect.fail(NOTHING_TO_COMPACT)
const unsupported = (message: string) =>
Effect.fail<Failure>({ error: { type: "provider.unsupported-operation", message } })
const prepared = yield* prepare(context, context.messages, budget, "session")
const toResult = (window: ReadonlyArray<Message>, usage: Usage | undefined) =>
spend(context.session.id, usage && SessionUsage.record(usage, context.model.cost)).pipe(
Effect.as<Result>({
text: "",
// Plain JSON, not the schema encoder: provider metadata can hold undefined entries, which it rejects.
native: { messages: JSON.parse(JSON.stringify(window)) },
}),
)
const send = (request: LLMRequest) => {
if (LLMClient.canCompact(request, { mechanism: "trigger" })) {
return Effect.gen(function* () {
const history = yield* SessionHistory.load(db, context.session.id, "local").pipe(Effect.orDie)
const retained = recentUserMessages(history, context.model, keep)
const response = yield* llm.compact(request, { ...prepared.options, mechanism: "trigger" })
return yield* toResult([...retained, Message.assistant(response.checkpoint)], response.usage)
})
}
if (LLMClient.canCompact(request)) {
return llm
.compact(request, { mechanism: "endpoint", http: prepared.options.http })
.pipe(Effect.flatMap((response) => toResult(response.replacement, response.usage)))
}
return unsupported(`Native compaction is not supported for ${request.model.provider}/${request.model.route.id}`)
}
return yield* deliver(trigger, prepared, budget, send)
})
/**
* Sends the request as-is if it is estimated to fit `shrink.fit`, else `shrink.history` as-is if it fits `target`,
* and otherwise `shrink.history` as text aimed at `target` (see `flattenAndDropOldest`). `shrink.measured`, when
* given, sizes the unchanged request until the provider rejects it; after that it is only sent as text.
* Limits and estimates can be wrong, so until the provider rejects something as too long, a request that cannot
* be made to fit is sent whole: unchanged, or as text once its media has to go. A "too long" rejection means the
* estimate ran low, so the next attempts aim at 85%, 70%, 50%, then 35% of the first rejected request's estimate,
* each shortening the transcript again from scratch, and a rejection after those gives up. A "payload too large"
* rejection is about bytes, which inline media almost always accounts for, so one on the unchanged request resends
* it as text, which carries no media, without counting; one on text counts as "too long". Other provider errors
* resend the same request under the session's retry policy. A `Failure` from `send` is a reply that cannot be
* used, and is never retried.
*/
const deliver = Effect.fnUntraced(function* (
trigger: Trigger,
prepared: Prepared,
target: number,
send: (request: LLMRequest, size?: Estimate) => Effect.Effect<Result, AIError | Failure>,
shrink: { readonly fit: number; readonly history: LLMRequest; readonly measured?: Estimate } = {
fit: target,
history: prepared.request,
},
background?: Background,
): Effect.fn.Return<Result, Failure> {
const context = trigger.context
if (prepared.event.result) return yield* fromHook(context, prepared.event.result, background)
const policy = yield* SessionRunnerRetry.policy(context.session.id)
let rejections = 0
let rejected: number | undefined
let asText = false
let fit = shrink.fit
while (true) {
const measured = rejections === 0 ? shrink.measured : undefined
const unchanged = asText
? undefined
: (measured ? total(measured) : estimateRequest(prepared.request)) <= fit
? prepared.request
: estimateRequest(shrink.history) <= target
? shrink.history
: undefined
const request =
unchanged ??
flattenAndDropOldest(shrink.history, context, target) ??
(rejections > 0
? undefined
: asText
? flattenAndDropOldest(shrink.history, context, Number.POSITIVE_INFINITY)
: shrink.history)
if (!request) {
return yield* Effect.fail<Failure>({
error: {
type: "compaction.failed",
message:
"The compaction request cannot be reduced further without losing the latest exchange or checkpoint",
},
})
}
const size = request === prepared.request ? measured : undefined
const attempt = yield* Effect.result(send(request, size))
if (Result.isSuccess(attempt)) return attempt.success
const cause = attempt.failure
if (!(cause instanceof AIError)) return yield* Effect.fail(cause)
const error = toSessionError(cause)
const tooLarge = cause.reason._tag === "InvalidRequest" && cause.reason.classification === "payload-too-large"
if (tooLarge && (request === prepared.request || request === shrink.history)) {
asText = true
continue
}
if (tooLarge || isContextOverflowFailure(cause)) {
const step = SHRINK_STEPS[rejections]
if (step === undefined) return yield* Effect.fail<Failure>({ error })
rejected ??= size ? total(size) : estimateRequest(request)
// The characters can still say the rejected request fits, so it only goes out again as text.
if (size) asText = true
target = Math.floor(rejected * step)
fit = target
rejections++
continue
}
const decision = yield* policy({
cause,
error,
agent: context.agent.id,
model: context.model.ref,
hook: prepared.retry,
retry: SessionRunnerRetry.isRetryable(cause),
})
if (!decision.retry) return yield* Effect.fail<Failure>({ error })
yield* Effect.sleep(decision.delay)
}
})
/**
* History that has to change is sent as text rather than as edited messages: `[previous compaction][transcript]`,
* the conversation since that compaction as one text message. A previous summary always stays in front, while
* its verbatim recent part becomes the oldest transcript entry; a previous native window stays exactly as the
* provider made it. The transcript has no tool definitions, which the summary does not need and which no longer
* share a cached prefix, and leaves out reasoning, media, and instruction updates.
*
* When the whole transcript does not fit, exchanges are shortened oldest first, one level at a time, until it
* does (see `flattenMessage` for the levels): every exchange reaches a level before any reaches the next, so
* detail is lost from the oldest history first and whole exchanges are omitted only as a last resort. The newest
* exchange is never omitted; undefined when the transcript cannot fit even so.
*/
const flattenAndDropOldest = (request: LLMRequest, context: SessionContext.Loaded, target: number) => {
const previous = previousCompaction(context.messages)
const end =
request.messages.findLastIndex(
(message) =>
(previous !== undefined && message.id === previous.id) ||
message.content.some((part) => part.type === "compaction"),
) + 1
const summary = previous !== undefined && !SessionHistory.isNativeCompaction(previous) ? previous : undefined
// TODO: Read the previous summary from the hooked request once it no longer merges summary and recent into one
// message. Until then it is rebuilt from storage, which drops a compaction hook's edits to that message.
const lead = summary
? toLLMMessages([{ ...summary, metadata: { ...summary.metadata, recent: "" } }], context.model.ref)
: request.messages.slice(0, end)
const recent = summary ? SessionRecentText.kept(summary) : ""
// A user message and everything after it, up to the next one, is one exchange. Each is rendered at every level
// once, so shortening is running arithmetic over these sizes. The previous summary's recent part is already
// shortened text: whole until it is omitted.
const exchanges = [
...(recent ? [TRANSCRIPT_LEVELS.map((level) => (level === OMITTED ? "" : recent))] : []),
...request.messages
.slice(end)
.reduce<Message[][]>((groups, message) => {
if (message.role === "user" || groups.length === 0) groups.push([message])
else groups[groups.length - 1].push(message)
return groups
}, [])
.map((group) =>
TRANSCRIPT_LEVELS.map((level) =>
group
.map((message) => flattenMessage(message, level))
.filter(Boolean)
.join("\n"),
),
)
.filter((renders) => renders[0]),
]
if (exchanges.length === 0) return undefined
const note = (omitted: number) =>
omitted ? `[${omitted} older ${omitted === 1 ? "exchange" : "exchanges"} omitted]\n\n` : ""
const size = (text: string) => (text ? Token.estimate(text) + 1 : 0)
// Room for the longest possible note is set aside before choosing what to keep.
const room =
target -
estimateRequest({ system: request.system, tools: [], messages: lead }) -
Token.estimate(note(exchanges.length))
const levels = exchanges.map(() => 0)
let total = exchanges.reduce((sum, renders) => sum + size(renders[0]), 0)
const newest = exchanges.length - 1
for (const level of TRANSCRIPT_LEVELS.slice(1)) {
for (let index = 0; index < exchanges.length && total > room; index++) {
// The newest exchange is what the summary most needs; it is shortened but never left out.
if (index === newest && !exchanges[index][level]) continue
total += size(exchanges[index][level]) - size(exchanges[index][levels[index]])
levels[index] = level
}
}
if (total > room) return undefined
// Includes exchanges with no user text, which are already gone at `USER_ONLY`.
const omitted = levels.filter((level, index) => !exchanges[index][level]).length
const text = exchanges
.map((renders, index) => renders[levels[index]])
.filter(Boolean)
.join("\n\n")
return LLMRequest.update(request, { tools: [], messages: [...lead, Message.user(note(omitted) + text)] })
}
const stream = (
context: SessionContext.Loaded,
request: LLMRequest,
options: StreamOptions,
background?: Background,
) => {
const sessionID = context.session.id
const unusable = (error: SessionError.Error) => Effect.fail<Failure>({ error })
return llm.stream(request, options).pipe(
Stream.runFoldEffect(
(): Streamed => ({ text: "" }),
(streamed, event): Effect.Effect<Streamed, AIError | Failure> => {
if (LLMEvent.is.providerError(event)) {
if (event.classification === undefined)
return unusable({ type: "provider.error", message: event.message })
return Effect.fail(
new AIError({
reason: new InvalidRequestError({ message: event.message, classification: event.classification }),
}),
)
}
if (LLMEvent.is.textDelta(event)) {
const next = { ...streamed, text: streamed.text + event.text }
if (background) return Effect.succeed(next)
return bus.publish(SessionEvent.Compaction.Delta, { sessionID, text: event.text }).pipe(Effect.as(next))
}
if (LLMEvent.is.stepFinish(event)) {
return spend(sessionID, SessionUsage.record(event.usage, context.model.cost), background?.usage).pipe(
Effect.as(streamed),
)
}
if (!LLMEvent.is.finish(event)) return Effect.succeed(streamed)
switch (event.reason.normalized) {
case "unknown":
return Effect.fail(
new AIError({
reason: new InvalidProviderOutputError({
message: "The provider response ended with an unknown finish reason.",
classification: "incomplete-stream",
}),
}),
)
case "error":
return Effect.fail(
new AIError({ reason: new UnknownProviderError({ message: "Compaction generation failed" }) }),
)
case "length":
return unusable({
type: "compaction.failed",
message: "Compaction summary reached the output token limit",
})
case "content-filter":
return unusable(contentFilterError("Compaction summary was blocked by the provider", event.reason))
default:
return Effect.succeed(streamed)
}
},
),
)
}
/**
* The conversation as the runner would send it, after request hooks.
*
* The output limit leaves room for `budget`, the most `deliver` sends. The request prepared here can be larger
* when the conversation overshot the threshold, and is only shrunk to fit after hooks have seen it, so sizing the
* output to it would leave next to no room. A prompt the estimate undersells is rejected and shrunk like any
* other.
*/
const prepare = (
context: SessionContext.Loaded,
messages: ReadonlyArray<SessionMessage.Info>,
budget: number,
webSocket?: "session",
) => {
const base = transcript(context, messages)
return requests.compaction({
session: context.session,
agent: context.agent.id,
model: context.model,
tools: context.tools,
system: base.system,
messages: base.messages,
webSocket,
inputTokens: { measured: budget, estimated: 0 },
})
}
/** A request hook supplied the summary itself, so no model call happens. */
const fromHook = (context: SessionContext.Loaded, supplied: SessionCompactionResult, background?: Background) => {
const usage = supplied.tokens && {
tokens: supplied.tokens,
cost: SessionUsage.calculateCost(context.model.cost, supplied.tokens),
}
return spend(context.session.id, usage, background?.usage).pipe(
Effect.as<Result>({
text: supplied.summary,
native: supplied.native,
metadata: supplied.metadata,
}),
)
}
/**
* Each model call is billed as soon as it finishes, so failed and interrupted compactions are billed too. The
* compaction's message shows the total across all of its calls. A session never runs two compactions at once;
* a background summary keeps its own total until a compaction takes it.
*/
const spent = new Map<SessionContext.Loaded["session"]["id"], SessionUsage.Recorded>()
const addSpent = (sessionID: SessionContext.Loaded["session"]["id"], usage: SessionUsage.Recorded) => {
const total = spent.get(sessionID)
spent.set(sessionID, total ? SessionUsage.add(total, usage) : usage)
}
const spend = (
sessionID: SessionContext.Loaded["session"]["id"],
usage: SessionUsage.Recorded | undefined,
record = (recorded: SessionUsage.Recorded) => addSpent(sessionID, recorded),
) =>
Effect.gen(function* () {
if (!usage) return
record(usage)
yield* bus.publish(SessionEvent.UsageRecorded, { sessionID, source: "compaction", ...usage })
})
const publish = Effect.fnUntraced(function* (
trigger: Trigger,
outcome: Result | Failure,
): Effect.fn.Return<Outcome> {
const context = trigger.context
const sessionID = context.session.id
const reason = trigger.reason === "manual" ? "manual" : "auto"
const usage = spent.get(sessionID)
if ("error" in outcome) {
yield* bus.publish(SessionEvent.Compaction.Failed, {
sessionID,
reason,
inputID: trigger.reason === "manual" ? trigger.inputID : undefined,
error: outcome.error,
...usage,
})
return { status: "failed", error: outcome.error }
}
yield* bus.publish(
SessionEvent.Compaction.Ended,
{
sessionID,
reason,
model: context.model.ref,
native: outcome.native,
text: outcome.text,
...usage,
},
{ metadata: outcome.metadata },
)
return { status: "completed" }
})
return Service.of({
transform: state.transform,
reload: state.reload,
// A manual compaction settles through its `/compact` inbox item, which the runner owns.
compact: (trigger) =>
compact(trigger).pipe(
Effect.onInterrupt(() =>
trigger.reason === "manual"
? Effect.void
: publish(trigger, { error: { type: "compaction.interrupted", message: "Compaction was interrupted" } }),
),
Effect.ensuring(Effect.sync(() => spent.delete(trigger.context.session.id))),
),
precompute,
})
}),
)
export const node = makeLocationNode({
service: Service,
layer,
deps: [Agent.node, Bus.node, Database.node, llmClient, Model.node, SessionModelRequest.node, SessionRunnerModel.node],
})
/**
* Where the messages kept beside a held summary start, or -1 when the summary no longer describes this
* conversation: a compaction or revert since replaced its start, or the model or instructions changed.
*/
const heldIndex = (held: Precomputed, context: SessionContext.Loaded) => {
if (held.firstID !== context.messages[0]?.id || held.initial !== Instructions.join(context.initial)) return -1
if (held.model.providerID !== context.model.ref.providerID || held.model.id !== context.model.ref.id) return -1
if (held.model.variant !== context.model.ref.variant) return -1
return context.messages.findIndex((message) => message.id === held.keepID)
}
/** Estimated text a swapped-in summary keeps verbatim: every message from `index` on. */
const tailTokens = (context: SessionContext.Loaded, index: number) =>
Token.estimate(context.messages.slice(index).map(SessionRecentText.messageToText).filter(Boolean).join("\n\n"))
const tailLimit = (keep: number, budget: number) =>
Math.min(Math.max(keep * 2, PRECOMPUTED_TAIL_MIN), Math.floor(budget * PRECOMPUTED_TAIL_SHARE))
/** History loads from the latest completed compaction, so a previous one is always the first message. */
const previousCompaction = (messages: ReadonlyArray<SessionMessage.Info>) => {
const [first] = messages
return first?.type === "compaction" && first.status === "completed" ? first : undefined
}
const transcript = (context: SessionContext.Loaded, messages: ReadonlyArray<SessionMessage.Info>) =>
SessionModelRequest.baseTranscript({
agent: context.agent.info,
model: context.model,
tools: context.tools,
initial: context.initial,
messages,
})
/**
* The newest messages within `keep` tokens, from `keepID` on, are kept verbatim beside the summary.
* Undefined when there is nothing to compact.
*/
const splitConversation = (messages: ReadonlyArray<SessionMessage.Info>, keep: number) => {
const entries = messages.flatMap((message) => {
const text = SessionRecentText.messageToText(message)
return text ? [{ message, text }] : []
})
if (entries.length === 0) return undefined
const first = entries[recentStart(entries, keep, previousCompaction(messages))]
return {
keepID: first?.message.id,
}
}
/** The request without the retained tail, which starts at the message with ID `from`. */
const withoutTail = (request: LLMRequest, from: SessionMessage.ID | undefined) => {
const index = from === undefined ? -1 : request.messages.findIndex((message) => message.id === from)