compaction.mdx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ---
  2. title: "Compaction"
  3. description: ""
  4. ---
  5. Compaction replaces the active model context from an older part of a session
  6. with a generated checkpoint. The checkpoint contains a structured summary and
  7. a serialized tail of recent context, so the agent can continue with more room
  8. in the model's context window.
  9. Compaction is lossy, but it does not delete the earlier durable session
  10. messages. After a successful compaction, V2 builds model requests from the
  11. latest completed checkpoint and the messages that follow it.
  12. ## Automatic compaction
  13. Automatic compaction is enabled by default. Before a model call, V2 estimates
  14. the size of the final system prompt, messages, and advertised tools. It starts
  15. compaction when:
  16. ```text
  17. estimated tokens > context limit - max(requested output tokens, buffer)
  18. ```
  19. The estimate is approximate: V2 JSON-serializes the request and assumes four
  20. characters per token. When compaction succeeds, V2 rebuilds the request from
  21. the new checkpoint and retries the step without promoting the input again.
  22. V2 also recognizes provider errors classified as context overflow. If an
  23. overflow occurs before the provider produces assistant output or other retry
  24. evidence, V2 can compact and retry that step once. This recovery is attempted
  25. even when `auto` is `false`; `auto` controls only the preflight size check. A
  26. second overflow after recovery is returned as an error.
  27. ## Manual compaction
  28. In the TUI, run:
  29. ```text
  30. /compact
  31. ```
  32. `/summarize` is an alias. The default keybind is `<leader>c`, configured as
  33. `session_compact`.
  34. A manual request is durably admitted and wakes the session runner. It can
  35. compact short histories that would not trigger automatic compaction. If the
  36. session is busy, compaction runs at the next safe drain boundary before later
  37. steered or queued prompts are promoted. Repeated requests while one is pending
  38. coalesce into that pending request. Whether compaction completes or fails, the
  39. barrier is then settled so later prompts can proceed.
  40. The CLI has no separate `compact` subcommand. Use the TUI command or the server
  41. API. For example:
  42. ```bash
  43. opencode2 api v2.session.compact \
  44. --param sessionID=ses_example \
  45. --data '{}'
  46. ```
  47. The equivalent raw request is:
  48. ```bash
  49. opencode2 api post /api/session/ses_example/compact --data '{}'
  50. ```
  51. `POST /api/session/:sessionID/compact` returns the admitted compaction input;
  52. it does not wait for summary generation. Clients can call
  53. `client.session.compact({ sessionID })` and then wait for the session or follow
  54. the `session.compaction.*` events. Supplying an optional message `id` makes an
  55. exact retry idempotent, but reusing an ID owned by another record returns a
  56. conflict.
  57. ## Configuration
  58. Add `compaction` to any [OpenCode configuration file](/config):
  59. ```jsonc title="opencode.jsonc"
  60. {
  61. "$schema": "https://opencode.ai/config.json",
  62. "compaction": {
  63. "auto": true,
  64. "prune": false,
  65. "keep": {
  66. "tokens": 8000
  67. },
  68. "buffer": 20000
  69. }
  70. }
  71. ```
  72. | Field | Default | V2 behavior |
  73. | --- | ---: | --- |
  74. | `auto` | `true` | Runs the preflight context-size check. It does not disable manual compaction or one-shot provider-overflow recovery. |
  75. | `prune` | None | Accepted by the V2 schema, but currently has no runtime effect. V2 does not prune old tool outputs in place. |
  76. | `keep.tokens` | `8000` | Approximate number of tokens from the newest serialized conversation context to retain beside the summary. |
  77. | `buffer` | `20000` | Token reserve used by the automatic threshold. The requested model output allowance wins when it is larger. |
  78. `keep.tokens` and `buffer` accept non-negative integers. Larger `keep.tokens`
  79. preserves more recent detail but leaves less room for future work. Larger
  80. `buffer` triggers preflight compaction earlier.
  81. ## Checkpoint contents
  82. V2 uses the session's selected or default model to generate the summary, with
  83. tools disabled and at most 4096 output tokens. The summary records the
  84. objective, important details, completed and active work, blockers, next moves,
  85. and relevant files.
  86. The newest serialized context up to `keep.tokens` is retained separately. This
  87. is not a byte-for-byte transcript: tool output is limited to 2000 characters,
  88. and file or media attachments become textual descriptors rather than embedded
  89. data. On later compactions, V2 updates the previous summary and carries forward
  90. its retained recent context before selecting a new tail.
  91. The completed compaction is presented to the model as historical conversation
  92. context, explicitly not as new instructions. Running and failed compactions are
  93. not included in model context.
  94. ## Compaction advances the instruction epoch
  95. Conversation compaction and instruction synchronization are separate. Before
  96. promoting pending input, V2 compares live instruction sources with the latest
  97. admitted values. Ordinary changes become durable value deltas; their
  98. model-facing System messages are derived during request assembly rather than
  99. persisted.
  100. Completed compaction advances the instruction epoch at the exact ended-event
  101. sequence and makes the currently admitted values initial. It does not reread
  102. sources or publish an instruction event. Session movement and committed revert
  103. clear the instruction fold so the next safe boundary requires one complete
  104. source read. See [Instructions](/instructions) for source ordering and update
  105. behavior.
  106. ## Current limitations
  107. - `prune` is reserved configuration; V1-style in-place tool-output pruning is
  108. not implemented in V2.
  109. - Compaction requires a resolvable model with a positive catalog context limit.
  110. There is no separate compaction-model setting or fallback model.
  111. - Summary generation can fail if the summary prompt itself cannot fit beside
  112. its output allowance, the model returns no summary, or the provider fails.
  113. - Automatic and overflow compaction need older conversation context that can be
  114. replaced. A provider overflow can still surface when there is no compressible
  115. head or fixed instructions and tool schemas dominate the request.
  116. - Overflow recovery retries only once per step. Token estimation is heuristic,
  117. so it cannot prevent every provider-specific overflow.
  118. - Earlier durable messages remain stored even though they are no longer in the
  119. active model context.
  120. V1 used additional tail-turn and pruning behavior. Those V1 details are only
  121. migration context; the settings and behavior on this page describe V2.