Feature: generic /compact command, auto-compaction, and fork-aware conversations #2851

Open
opened 2026-02-16 17:37:32 -05:00 by yindo · 2 comments
Owner

Originally created by @riatzukiza on GitHub (Nov 13, 2025).

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, and docs/slash_commands.md define:
    • A /compact slash command that summarizes conversation history.
    • An auto_compact_token_limit based on model metadata (openai_model_info.rs).
    • A SUMMARIZATION_PROMPT used to create a handoff summary.
  • Tests like core/tests/suite/compact_resume_fork.rs verify interactions between compaction, resume, and forks, especially with respect to prompt_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 file path 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 /compact story 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_key reuse 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 /compact slash command.
    • Coordinate auto-compaction decisions across sessions in a provider-agnostic way.

Proposed Changes

  1. Add a generic /compact command 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.
  2. Introduce autoCompactTokenLimit in core configuration

    • Add a per-provider and per-model autoCompactTokenLimit field in opencode’s config model (analogous to Codex’s auto_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 /compact under the hood) before sending the next user prompt.
    • Allow users to override or disable auto-compaction globally and per-project.
  3. 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 parentConversationId and optional forkId/branchId.
    • Provide a provider hook (e.g. onConversationFork) that:
      • Allows providers to adjust their own cache keys (prompt_cache_key for 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.
  4. Provider API surface for compaction

    • Define a provider capability flag (e.g. supportsCompaction) and a method like compactConversation(context) -> summary.
    • Implement this in the Codex plugin by:
      • Reusing the official compact prompt from Codex CLI or a close approximation.
      • Leveraging encrypted reasoning content to maintain continuity.
    • Other providers (OpenAI Platform, local models, etc.) can opt in with their own prompts.

Why This Belongs in Core

  • Compaction and conversation forking are cross-provider concerns: every backend has context limits and benefits from summarization.
  • A generic /compact command 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 /compact command 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.
Originally created by @riatzukiza on GitHub (Nov 13, 2025). ## 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`, and `docs/slash_commands.md` define: - A `/compact` slash command that summarizes conversation history. - An `auto_compact_token_limit` based on model metadata (`openai_model_info.rs`). - A `SUMMARIZATION_PROMPT` used to create a handoff summary. - Tests like `core/tests/suite/compact_resume_fork.rs` verify interactions between compaction, resume, and forks, especially with respect to `prompt_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 file` path 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 `/compact` story 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_key` reuse 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 `/compact` slash command. - Coordinate auto-compaction decisions across sessions in a provider-agnostic way. ## Proposed Changes 1. **Add a generic `/compact` command 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. 2. **Introduce `autoCompactTokenLimit` in core configuration** - Add a per-provider and per-model `autoCompactTokenLimit` field in opencode’s config model (analogous to Codex’s `auto_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 `/compact` under the hood) before sending the next user prompt. - Allow users to override or disable auto-compaction globally and per-project. 3. **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 `parentConversationId` and optional `forkId`/`branchId`. - Provide a provider hook (e.g. `onConversationFork`) that: - Allows providers to adjust their own cache keys (`prompt_cache_key` for 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. 4. **Provider API surface for compaction** - Define a provider capability flag (e.g. `supportsCompaction`) and a method like `compactConversation(context) -> summary`. - Implement this in the Codex plugin by: - Reusing the official `compact` prompt from Codex CLI or a close approximation. - Leveraging encrypted reasoning content to maintain continuity. - Other providers (OpenAI Platform, local models, etc.) can opt in with their own prompts. ## Why This Belongs in Core - Compaction and conversation forking are cross-provider concerns: every backend has context limits and benefits from summarization. - A generic `/compact` command 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 `/compact` command 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.
yindo added the discussion label 2026-02-16 17:37:32 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Nov 13, 2025):

This issue might be a duplicate of existing issues. Please check:

  • #3153: Focuses on auto-compaction for subagents specifically, which overlaps with your proposed auto-compaction system
  • #3032: Proposes soft compaction / AI global workspace metabolism with sophisticated context management, similar to your compaction and fork-aware conversation approach
  • #2465: Requests the ability to override compact default model and prompt, which relates to your provider-agnostic compaction framework
  • #1759: Requests fork session functionality, which directly relates to your fork-aware conversation IDs proposal
  • #1020: Proposes visual conversation timeline with branching functionality, which overlaps with your conversation forking and branch management ideas

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Nov 13, 2025): This issue might be a duplicate of existing issues. Please check: - #3153: Focuses on auto-compaction for subagents specifically, which overlaps with your proposed auto-compaction system - #3032: Proposes soft compaction / AI global workspace metabolism with sophisticated context management, similar to your compaction and fork-aware conversation approach - #2465: Requests the ability to override compact default model and prompt, which relates to your provider-agnostic compaction framework - #1759: Requests fork session functionality, which directly relates to your fork-aware conversation IDs proposal - #1020: Proposes visual conversation timeline with branching functionality, which overlaps with your conversation forking and branch management ideas Feel free to ignore if none of these address your specific case.
Author
Owner

@riatzukiza commented on GitHub (Nov 13, 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:

  • Core: expose a semantic 'compact this conversation' action and surface it as '/compact' in the UI.
  • Providers: implement a compactConversation(context) -> summary/updated-history hook (or similar) that maps this semantic action into whatever compaction prompt/state update makes sense for that backend.

That way:

  • Existing behavior for the current OpenAI integration can be preserved under the same '/compact' name.
  • Providers like the Codex OAuth plugin can implement compaction in a way that works with a stateless ChatGPT backend (no shared summary file), instead of having to strip the host's compaction prompts.

If it helps, I'm happy to draft a more concrete provider API shape for this (capability flag + hook signature) as a follow-up.

@riatzukiza commented on GitHub (Nov 13, 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: - Core: expose a semantic 'compact this conversation' action and surface it as '/compact' in the UI. - Providers: implement a compactConversation(context) -> summary/updated-history hook (or similar) that maps this semantic action into whatever compaction prompt/state update makes sense for that backend. That way: - Existing behavior for the current OpenAI integration can be preserved under the same '/compact' name. - Providers like the Codex OAuth plugin can implement compaction in a way that works with a stateless ChatGPT backend (no shared summary file), instead of having to strip the host's compaction prompts. If it helps, I'm happy to draft a more concrete provider API shape for this (capability flag + hook signature) as a follow-up.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#2851