Repository navigation
Feature: generic /compact command, auto-compaction, and fork-aware conversations #4317
Copy link
Copy link
Closed as not planned
Closed as not planned
Copy link
Description
Summary
Add a provider-agnostic /compact command and auto-compaction support to opencode core, along with fork-aware conversation IDs that play nicely with Codex-style prompt_cache_key semantics.
Background
In openai/codex (codex-rs), compaction is a first-class feature:
codex-rs/core/src/compact.rs,core/src/tasks/compact.rs, anddocs/slash_commands.mddefine:- A
/compactslash command that summarizes conversation history. - An
auto_compact_token_limitbased on model metadata (openai_model_info.rs). - A
SUMMARIZATION_PROMPTused to create a handoff summary.
- A
- Tests like
core/tests/suite/compact_resume_fork.rsverify interactions between compaction, resume, and forks, especially with respect toprompt_cache_key.
In the Codex OAuth plugin for opencode:
- We currently strip OpenCode’s own auto-compaction system prompts in CODEX_MODE because they reference a
summary filepath that doesn’t exist for our stateless ChatGPT backend. - We rely on
store: false+include: ["reasoning.encrypted_content"]to preserve context, but we do not have a coherent/compactstory at the host level.
Problem
Without core support for compaction and fork-aware conversation IDs:
- Each provider has to reinvent compaction semantics (or avoid them entirely), leading to inconsistent behavior across backends.
- Codex-style
prompt_cache_keyreuse across resume/fork flows is hard to get right because opencode doesn’t expose explicit fork events to providers. - The Codex plugin can emulate compaction internally, but it cannot:
- Register a shared
/compactslash command. - Coordinate auto-compaction decisions across sessions in a provider-agnostic way.
- Register a shared
Proposed Changes
-
Add a generic
/compactcommand in opencode core- Define a new slash command (e.g.
/compact) that:- Requests a summary of the current conversation from the active provider.
- Replaces (or augments) conversation history with the returned summary so future turns operate on a compacted context.
- Design the protocol so that providers receive a clear “compact this conversation” signal, not just a normal user message.
- Define a new slash command (e.g.
-
Introduce
autoCompactTokenLimitin core configuration- Add a per-provider and per-model
autoCompactTokenLimitfield in opencode’s config model (analogous to Codex’sauto_compact_token_limit). - Track approximate token usage per conversation (input + output), and when a configurable threshold is crossed:
- Automatically trigger the compact action (e.g. run
/compactunder the hood) before sending the next user prompt.
- Automatically trigger the compact action (e.g. run
- Allow users to override or disable auto-compaction globally and per-project.
- Add a per-provider and per-model
-
Fork-aware conversation IDs and provider hooks
- Extend the core session manager so that:
- Conversations have explicit, stable IDs.
- Forks (branching from an existing conversation) are modeled as first-class operations with
parentConversationIdand optionalforkId/branchId.
- Provide a provider hook (e.g.
onConversationFork) that:- Allows providers to adjust their own cache keys (
prompt_cache_keyfor Codex) when forks occur. - Enables Codex-like semantics where:
- Overrides do not change
prompt_cache_key. - Forks do generate new cache keys derived from the parent.
- Overrides do not change
- Allows providers to adjust their own cache keys (
- Extend the core session manager so that:
-
Provider API surface for compaction
- Define a provider capability flag (e.g.
supportsCompaction) and a method likecompactConversation(context) -> summary. - Implement this in the Codex plugin by:
- Reusing the official
compactprompt from Codex CLI or a close approximation. - Leveraging encrypted reasoning content to maintain continuity.
- Reusing the official
- Other providers (OpenAI Platform, local models, etc.) can opt in with their own prompts.
- Define a provider capability flag (e.g.
Why This Belongs in Core
- Compaction and conversation forking are cross-provider concerns: every backend has context limits and benefits from summarization.
- A generic
/compactcommand and auto-compaction framework let plugins focus on the prompt and model details instead of reimplementing session semantics. - Codex-specific details (like
prompt_cache_key) become easier to integrate cleanly when opencode’s session model exposes the right hooks.
Acceptance Criteria
- There is a new
/compactcommand in opencode that providers can implement. - Configuration supports
autoCompactTokenLimit(or similar) per provider/model. - Conversations and forks have explicit IDs exposed to providers.
- Providers can implement fork-aware caching (Codex:
prompt_cache_key, others: own fields) with clear lifecycle events. - The Codex OAuth plugin and at least one non-Codex provider are able to adopt the new compaction hooks in a consistent way.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
github-actions commentedon Nov 14, 2025
This issue might be a duplicate of existing issues. Please check:
compactdefault model and prompt #2465: Requests the ability to override compact default model and prompt, which relates to your provider-agnostic compaction frameworkFeel free to ignore if none of these address your specific case.
riatzukiza commentedon Nov 14, 2025
Small clarification: opencode already exposes a '/compact' command, but today it works by injecting a provider-specific system prompt that assumes a file-based summary path (used by the existing OpenAI integration). From the Codex plugin's point of view, that behavior is tightly coupled to one provider implementation and can't be reused safely for the ChatGPT backend (we currently strip those prompts in CODEX_MODE for this reason).
The intent of this feature request is not to add a brand-new command name, but to generalize the existing '/compact' command into a provider-agnostic capability with a structured hook, for example:
That way:
If it helps, I'm happy to draft a more concrete provider API shape for this (capability flag + hook signature) as a follow-up.
disarticulate commentedon Mar 21, 2026
can opencode just autoclose all the issues, then open the ones they're actually going to work on.
All these issues sitting stale because everyones generating issues instead of pushing pull requersts, etc. It'd make it easier to understand what opencode staff actually care about.
alario-tang commentedon Mar 31, 2026
I think the current compaction architecture is on the right track, but long-session robustness still feels limited by how reactive the recovery path is.
After reading the current flow in:
packages/opencode/src/session/prompt.tspackages/opencode/src/session/processor.tspackages/opencode/src/session/compaction.tsthe main pattern seems to be:
That is clean, but probably leaves resilience on the table in tool-heavy or media-heavy sessions.
The improvements that seem highest leverage to me are:
The current
processor.ts -> compaction.tshandoff is readable, so this feels like something that could be added incrementally rather than requiring a rewrite.I think the biggest missing piece is compaction self-retry. Right now if compaction overflows, the flow stops. A retry path that progressively drops the oldest rounds until the summary request fits would probably make pathological long sessions much more recoverable.
nilltadios commentedon May 3, 2026
I think there is a narrower cache issue that fits under this broader provider-agnostic compaction work.
Current
/compactbuilds a special compaction request inpackages/opencode/src/session/compaction.tsby converting history withstripMedia: trueandtoolOutputMaxChars: 2000, then appending the compaction prompt as the final user message.Appending the compaction prompt at the end is good for prefix caching, but rewriting the prior history can break provider prompt-cache prefix matches. For providers with discounted cached input,
/compactcan become much more expensive than a normal turn over the same session.A minimal fix might be:
/compactwith the same model-message history a normal chat turn would send.Related prior issues:
/compactprompt caching misses and was auto-closed for inactivity.github-actions commentedon Jul 4, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.