Skip to content

[FEATURE]: Improve compaction prompt to preserve critical context details #16512

Description

@doomsday616

Problem

I use GitHub Copilot as my provider. Disabling auto-compaction is not an option — Copilot enforces a hard token limit and the session crashes with context overflow if compaction is off. So I'm forced to rely on it.

The compaction prompt in packages/opencode/src/session/compaction.ts is too generic. It tells the LLM to summarize "what was done, what we're doing, which files we're working on, what we're going to do next" — but doesn't instruct it to preserve:

  • User-pasted content (conversation logs, error output, config snippets) — gets summarized into nothing. Post-compaction the AI has no idea what was pasted.
  • Exact technical details — flag names, config field paths, error messages, version numbers get generalized into vague descriptions.
  • User-stated constraints and preferences — "don't do X", workflow instructions, communication style constraints vanish.
  • Debugging state — which hypotheses were tested and ruled out, with what evidence.

The result: after compaction the AI repeats questions already answered, forgets user constraints, and loses investigation state. Often worse than starting a fresh session.

Compaction threshold (related)

Additionally, for a 200k context model with OUTPUT_TOKEN_MAX capped at 32k, compaction fires at 168k (84%). With Copilot overhead it feels closer to 75%. The only available knob OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX also caps actual output length — not a compaction-only setting. I'd love a compaction.threshold percentage config, but that's already tracked in #11930.

Proposed solution

Make the default compaction prompt more aggressive about preserving specifics. At minimum, instruct the compaction LLM to:

  1. Keep exact names, paths, values, error messages, URLs, and version numbers — never generalize them.
  2. Reproduce key parts of user-pasted content verbatim instead of summarizing.
  3. Preserve all user-stated behavioral constraints and preferences.
  4. Preserve debugging state: what was tried, what was ruled out, what evidence was found.

Workaround

The experimental.session.compacting plugin hook can fully replace the compaction prompt today:

// ~/.config/opencode/plugins/better-compaction.ts
export const BetterCompactionPlugin = async (ctx) => {
  return {
    "experimental.session.compacting": async (input, output) => {
      output.prompt = `You are compacting a conversation to allow it to continue in a new context window. Your job is to produce a continuation prompt that preserves ALL information needed to seamlessly resume the work.

CRITICAL RULES:
1. NEVER generalize or summarize away specific details. Keep exact names, paths, values, error messages, flag names, config keys, URLs, and version numbers.
2. If the user pasted external content (conversation logs, error output, code snippets, config files), reproduce the KEY PARTS verbatim.
3. Preserve ALL user-stated constraints, preferences, and instructions.
4. Preserve the investigation/debugging state: what hypotheses were tested, what was ruled out with what evidence.
5. Preserve emotional context and communication style preferences.

Use this template:
---
## Goal
[Specific goal(s)]

## Instructions
- [User instructions, behavioral constraints, communication preferences]

## Discoveries
[Exact technical details: config values, paths, flag names, versions, what was tried and what happened]

## User-Pasted Content
[Verbatim key parts of any content the user pasted]

## Accomplished
### ✅ Completed
[Specific completed work]
### ❌ Not Solved
[Unsolved items with investigation state]
### ⏭️ Next Steps
[Planned next actions]

## Relevant files / directories
[Files read/modified/created, with key external references]
---`
    },
  }
}

System info

  • OS: macOS 15 (Tahoe), Apple M3 Pro arm64
  • OpenCode: v1.2.16
  • Provider: GitHub Copilot (via antigravity-auth plugin)
  • Model: claude-opus-4-6 (200k context, 128k output)

Related issues

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions