• Will Chen's avatar
    feat: add context compaction system for local-agent mode (#2515) · d857371a
    Will Chen 提交于
    Automatically compact long conversations when approaching context window
    limits (80% of model context or 180k tokens, whichever is first).
    
    Changes:
    - Add enableContextCompaction setting (default: enabled) in Settings >
    AI
    - Add database fields to track compaction state and backup paths
    - Create compaction handler that generates structured summaries via LLM
    - Store original messages in app data directory before compaction
    - Show visual indicator using dyad-status tag when compaction occurs
    - Integrate compaction checks into local_agent_handler
    <!-- devin-review-badge-begin -->
    
    ---
    
    <a href="https://app.devin.ai/review/dyad-sh/dyad/pull/2515"
    target="_blank">
      <picture>
    <source media="(prefers-color-scheme: dark)"
    srcset="https://static.devin.ai/assets/gh-open-in-devin-review-dark.svg?v=1">
    <img
    src="https://static.devin.ai/assets/gh-open-in-devin-review-light.svg?v=1"
    alt="Open with Devin">
      </picture>
    </a>
    <!-- devin-review-badge-end -->
    
    <!-- CURSOR_SUMMARY -->
    ---
    
    > [!NOTE]
    > **Medium Risk**
    > Touches core `local-agent` chat streaming and adds new
    persistence/migration paths plus LLM-driven summarization, so
    regressions could affect message ordering/history sent to models. Scoped
    behind a setting (default on) with added unit/E2E tests, reducing risk.
    > 
    > **Overview**
    > Adds **automatic context compaction** for `local-agent` chats: when
    token usage crosses a threshold, the chat is marked for compaction and
    the next turn generates an LLM summary, stores an XML transcript backup
    under `.dyad/chats/<id>/compaction-*.md`, and inserts a
    `is_compaction_summary` assistant message with a new `<dyad-compaction>`
    indicator.
    > 
    > Introduces a user-facing toggle `enableContextCompaction` (default on)
    wired into settings schema/defaults/search and Settings UI, plus new DB
    fields on `chats` (`pending_compaction`, `compacted_at`,
    `compaction_backup_path`) and `messages` (`is_compaction_summary`).
    Updates local-agent streaming to (a) run pending compaction before
    processing, (b) send only post-compaction history to the LLM, and (c)
    update the correct placeholder message by ID to avoid overwriting the
    compaction summary; includes new unit/E2E coverage and test-fixture
    support for streaming usage data/snapshot scrubbing.
    > 
    > <sup>Written by [Cursor
    Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
    df1cdaacbe3e9f3fb47473a3e6965e302a75fe7a. This will update automatically
    on new commits. Configure
    [here](https://cursor.com/dashboard?tab=bugbot).</sup>
    <!-- /CURSOR_SUMMARY -->
    
    <!-- This is an auto-generated description by cubic. -->
    ---
    ## Summary by cubic
    Adds automatic context compaction for local-agent mode to summarize long
    conversations when token usage reaches 80% of the model’s context window
    or 180k tokens. Original messages are preserved and backed up, a
    structured summary is inserted, and a dyad-compaction indicator is
    shown.
    
    - **New Features**
    - New toggle in Settings > AI: enableContextCompaction (default on) with
    UI switch and searchable setting.
    - DB fields to track compaction and backups (chats: compacted_at,
    compaction_backup_path, pending_compaction; messages:
    is_compaction_summary).
    - Compaction handler backs up LLM-visible messages to
    .dyad/chats/<chatId>/compaction-*.md, generates structured summaries
    with a system prompt, inserts a summary message, and emits
    chat:compaction:complete.
    - Local-agent flow: pending compaction runs at the start of the next
    turn; LLM history includes only post-compaction messages; streaming
    updates target the placeholder message by ID to avoid overwriting the
    summary.
    - Token threshold checks integrated into local_agent_handler (min(80% of
    context window, 180k)).
    
    <sup>Written for commit df1cdaacbe3e9f3fb47473a3e6965e302a75fe7a.
    Summary will update on new commits.</sup>
    
    <!-- End of auto-generated description by cubic. -->
    
    ---------
    Co-authored-by: 's avatarClaude Opus 4.5 <noreply@anthropic.com>
    d857371a
DyadCompaction.tsx 3.3 KB