Skip to content

Feature: Codex-style conversation compaction and auto-compaction in plugin #5

@riatzukiza

Description

@riatzukiza

Summary

Implement Codex-style conversation compaction inside the plugin, including a /compact-like flow and optional auto-compaction heuristics, while staying compatible with opencode's existing behavior (and future core-level compaction support).

Background

openai/codex treats compaction as a first-class capability:

  • codex-rs/core/src/compact.rs defines a SUMMARIZATION_PROMPT and compaction logic.
  • codex-rs/core/src/tasks/compact.rs and docs/slash_commands.md expose a /compact slash command.
  • openai_model_info.rs and docs/config.md reference auto_compact_token_limit, enabling automatic history compaction based on model token limits.

In this plugin:

  • In CODEX_MODE we currently strip OpenCode's own auto-compaction prompt because it references a summary file path that doesn't exist for our stateless ChatGPT backend.
  • We rely on store: false + include: ["reasoning.encrypted_content"] to keep enough context across turns but don't offer a plugin-specific compact/summary flow.

There is now a core feature request in sst/opencode for a provider-agnostic /compact command and auto-compaction, but the plugin can already start building a Codex-specific compaction story on top of today's API.

Proposed Behavior

  1. Plugin-level /codex-compact command

    • Implement a plugin-specific slash command or special user input pattern (e.g. /codex-compact) handled entirely in the plugin.
    • Behavior:
      • Use a compaction prompt derived from Codex CLI's SUMMARIZATION_PROMPT to ask the model for a handoff summary of the current conversation.
      • Replace the existing conversation history (as seen by the plugin) with:
        • The summary message, and
        • The current encrypted reasoning content (reasoning.encrypted_content) if available.
    • Ensure the compaction turn itself is not treated as user instructions for the actual task (i.e., subsequent turns operate on the summary, not the compaction instructions).
  2. Optional auto-compaction heuristics (plugin-side)

    • Estimate conversation size using a cheap token approximation over:
      • body.input, and
      • reasoning.encrypted_content (when present).
    • When an approximate token threshold is reached (configurable in the plugin config file), automatically run the plugin-level compaction flow before sending the next user request to Codex.
    • Keep this behavior opt-in via plugin config (e.g. enableCodexCompaction, autoCompactTokenLimit).
  3. Integration with prompt_cache_key and forks

    • When compaction runs, ensure that:
      • The prompt_cache_key remains stable for simple resume flows (same conversation identity).
      • If a compaction is used as a branch point (fork), it can cooperate with the fork-aware prompt_cache_key behavior (see related issue for fork-aware caching).
    • Add tests that mirror Codex's compact_resume_fork.rs semantics as closely as possible with the information we have.
  4. Forward compatibility with opencode core

    • Design the plugin-level compaction logic so that, if opencode later introduces a generic /compact command and provider hooks (as proposed in sst/opencode), the plugin can:
      • Map the core /compact action onto its existing compaction prompt and state update.
      • Gradually deprecate the plugin-only /codex-compact alias in favor of the core command.

Rationale

  • Gives users of the Codex plugin a clear, documented way to compact long conversations, independent of the host's current compaction implementation.
  • Aligns better with Codex CLI features users may already rely on (/compact, auto_compact_token_limit).
  • Provides a concrete design target for future integration with opencode core compaction hooks.

Acceptance Criteria

  • A /codex-compact (or equivalent) command exists and is handled entirely in the plugin.
  • Compaction produces a stable, reversible summary that can be used for subsequent turns without breaking the Codex-mode bridge or reasoning continuity.
  • Optional auto-compaction behavior is configurable and disabled by default.
  • New tests cover:
    • Basic compaction (manual command).
    • Auto-compaction when token thresholds are exceeded.
    • Interactions with prompt_cache_key and forks (in combination with the fork-aware caching feature).

Metadata

Metadata

Assignees

No one assigned

    Labels

    compactionIssues related to conversation compaction and summary handlingeffort-largeLarge effort issues - more than 12 hours of workpriority-highHigh priority issues - breaking bugs or critical functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions