Skip to content

feat: add double-buffer context management#4858

Open
marklubin wants to merge 1 commit intoAider-AI:mainfrom
marklubin:feat/double-buffer-context
Open

feat: add double-buffer context management#4858
marklubin wants to merge 1 commit intoAider-AI:mainfrom
marklubin:feat/double-buffer-context

Conversation

@marklubin
Copy link

Summary

Adds opt-in double-buffer context management that produces higher-quality chat history summaries by checkpointing early (at 60% of max_chat_history_tokens) while the model still has full attention, rather than waiting until the history is too big and attention is degraded.

How it works:

  1. At 60% capacity, a background thread runs ChatSummary.summarize() on a snapshot of done_messages
  2. Normal operation continues uninterrupted (the checkpoint is fire-and-forget)
  3. At 85% capacity, instead of the standard stop-the-world summarization, the pre-computed summary is swapped in

The feature is opt-in via --double-buffer and the existing summarization flow is completely unchanged when not enabled.

New files:

  • aider/double_buffer.py: DoubleBufferManager with background checkpoint via threading, Phase enum
  • tests/basic/test_double_buffer.py: 18 tests (phase transitions, thresholds, error handling)

Modified files:

  • aider/coders/base_coder.py: summarize_start() delegates to _double_buffer_check() when --double-buffer is enabled
  • aider/args.py: --double-buffer, --checkpoint-threshold (default 0.60), --swap-threshold (default 0.85)

Design:

  • Reuses existing ChatSummary for summarization (no new LLM calls)
  • Background thread mirrors existing summarizer_thread pattern in base_coder.py
  • Falls back to standard summarization if no double-buffer action applies

Algorithm description: https://marklubin.me/posts/hopping-context-windows/

Test plan

  • 18 new unit tests pass (pytest tests/basic/test_double_buffer.py -v)
  • No regressions in existing tests
  • black --line-length 100 --preview clean
  • isort --profile black clean
  • flake8 clean (no warnings)
  • Manual test: run with --double-buffer --verbose on a long session and observe checkpoint/swap log messages

Implements a two-phase compaction strategy that checkpoints at 60%
of max_chat_history_tokens and swaps at 85%, producing higher-quality
summaries by running summarization while the model still has full
attention rather than at the capacity cliff.

New files:
- aider/double_buffer.py: DoubleBufferManager with background
  checkpoint via threading, phase tracking, swap logic
- tests/basic/test_double_buffer.py: 18 tests covering transitions,
  thresholds, error handling

Modified files:
- aider/coders/base_coder.py: opt-in integration via summarize_start()
  delegation to _double_buffer_check() when enabled
- aider/args.py: --double-buffer, --checkpoint-threshold,
  --swap-threshold CLI flags

The feature is opt-in (--double-buffer flag) and the existing
summarization flow is completely unchanged when not enabled.

Algorithm: https://marklubin.me/posts/hopping-context-windows/
@CLAassistant
Copy link

CLAassistant commented Feb 25, 2026

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants