feat(compaction): add Headroom context compaction middleware by Srinath279 · Pull Request #3568 · bytedance/deer-flow · GitHub
Skip to content

feat(compaction): add Headroom context compaction middleware#3568

Open
Srinath279 wants to merge 1 commit into
bytedance:mainfrom
Srinath279:feat/headroom-context-compaction
Open

feat(compaction): add Headroom context compaction middleware#3568
Srinath279 wants to merge 1 commit into
bytedance:mainfrom
Srinath279:feat/headroom-context-compaction

Conversation

@Srinath279

Copy link
Copy Markdown

Compress the message history sent to the LLM — primarily large tool outputs, logs, and search results — at the model-call boundary using the optional headroom-ai package.

Design:

  • Non-destructive: runs in wrap_model_call and only rewrites the request copy of the messages via request.override(...). Persisted ThreadState keeps full originals, so it is reversible. Complementary to (not a replacement for) SummarizationMiddleware.
  • Structure-preserving: only string content is mapped back onto the original LangChain messages, so message IDs and tool_calls are intact. Any message-count change or error falls back to the originals (fail_open).
  • Optional dependency: transparent no-op when headroom-ai is absent; middleware is only added to the chain when compaction.enabled.
  • Event-loop safe: async hook offloads compression via asyncio.to_thread.

Wiring: lead agent (after summarization) + subagent runtime. Config: new compaction section in AppConfig / config.example.yaml (config_version 12 -> 13). New [compaction] extra (headroom-ai). Docs: backend/docs/compaction.md + CLAUDE.md.
Tests: test_compaction_config.py, test_compaction_middleware.py (use a compress_fn injection seam; never import the heavy ML stack).

Fixes #

Why

What changed

Surface area

  • Frontend UI — page / component / setting / interaction under frontend/
  • Backend API — endpoint / SSE event / request-response shape under backend/app
  • Agents / LangGraph — agent node, graph wiring, langgraph.json, or prompt change
  • Sandboxdocker/ or sandboxed execution
  • Skills — change under skills/
  • Dependencies — new/upgraded entry in backend/pyproject.toml or frontend/package.json (say what it buys us)
  • Default behavior change — changes existing behavior without the user opting in (default model, default setting, data shape)
  • Docs / tests / CI only — no runtime behavior change

Screenshots / Recording

Bug fix verification

Validation

AI assistance

Tool(s) used:

How you used it:

  • I've read and understand every line of this change and take responsibility for it — it's not unreviewed AI output.

Compress the message history sent to the LLM — primarily large tool
outputs, logs, and search results — at the model-call boundary using the
optional `headroom-ai` package.

Design:
- Non-destructive: runs in `wrap_model_call` and only rewrites the request
  copy of the messages via `request.override(...)`. Persisted ThreadState
  keeps full originals, so it is reversible. Complementary to (not a
  replacement for) SummarizationMiddleware.
- Structure-preserving: only string content is mapped back onto the
  original LangChain messages, so message IDs and tool_calls are intact.
  Any message-count change or error falls back to the originals (fail_open).
- Optional dependency: transparent no-op when `headroom-ai` is absent;
  middleware is only added to the chain when `compaction.enabled`.
- Event-loop safe: async hook offloads compression via asyncio.to_thread.

Wiring: lead agent (after summarization) + subagent runtime.
Config: new `compaction` section in AppConfig / config.example.yaml
(config_version 12 -> 13). New `[compaction]` extra (headroom-ai).
Docs: backend/docs/compaction.md + CLAUDE.md.
Tests: test_compaction_config.py, test_compaction_middleware.py (use a
compress_fn injection seam; never import the heavy ML stack).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@CLAassistant

Copy link
Copy Markdown

@github-actions github-actions Bot added area:agents Agents, subagents, graph wiring, prompts, langgraph.json area:backend Gateway / runtime / core backend under backend/ area:deps Dependency manifests / lockfiles area:docs Documentation and Markdown only needs-validation Touches front/back contract surface; needs real-path validation risk:high High risk: backend API, agents, sandbox, auth, deps, CI size/XL PR changes 700+ lines labels Jun 14, 2026
@WillemJiang

Copy link
Copy Markdown
Collaborator

@Srinath279, thanks for your contribution. To address the CLA complaint, please make sure your git commit author email is set in your GitHub profile's email settings.

@WillemJiang

Copy link
Copy Markdown
Collaborator

@WillemJiang WillemJiang added this to the 2.1.0 milestone Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agents Agents, subagents, graph wiring, prompts, langgraph.json area:backend Gateway / runtime / core backend under backend/ area:deps Dependency manifests / lockfiles area:docs Documentation and Markdown only needs-validation Touches front/back contract surface; needs real-path validation risk:high High risk: backend API, agents, sandbox, auth, deps, CI size/XL PR changes 700+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants