openclaw - 💡(How to fix) Fix Proactive Compaction Configuration & Scheduler (OpenClaw #41963) [1 participants]

Official PRs (…)
ON THIS PAGE

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#52692Fetched 2026-04-08 01:20:16
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Author
Participants

Code Example

{
  "token_threshold": 250000,
  "message_threshold": 50,
  "interval_sec": 900,
  "overlap_raw": 10,
  "summarizer_model": "step-3.5-flash:free"
}
RAW_BUFFERClick to expand / collapse

Proactive Compaction Configuration & Scheduler (OpenClaw #41963)

Problem

Currently, compaction likely runs reactively (when context nears limit). This can cause delays mid-task. We want proactive compaction that keeps context slim and prevents overflow automatically.

Proposed Solution

Define a background scheduler that periodically checks context size and triggers summarization even during idle periods. Configuration stored in ~/.openclaw/compaction.json.

Configuration Schema

{
  "token_threshold": 250000,
  "message_threshold": 50,
  "interval_sec": 900,
  "overlap_raw": 10,
  "summarizer_model": "step-3.5-flash:free"
}
  • token_threshold: trigger if total tokens in active context >= this value.
  • message_threshold: trigger if number of messages >= this value.
  • interval_sec: also trigger if this many seconds have passed since last compaction (guarantee minimum frequency).
  • overlap_raw: number of recent raw messages to retain after summarization (maintain recency).
  • summarizer_model: LLM to use for summarization; choose cost-effective model.

Scheduler Design

  • Run in background within agent process (non-blocking).
  • On each agent heartbeat (or timer tick), evaluate conditions.
  • If any threshold exceeded OR time since last compaction > interval_sec, run compaction:
    1. Take current context messages.
    2. Preserve last overlap_raw messages as raw.
    3. Summarize older messages using summarizer_model.
    4. Replace context with summary + overlap.
    5. Record compaction event in memory.
  • Use asyncio to avoid blocking main agent loop.

Integration

  • Modify agent main loop to initialize CompactionScheduler(config).
  • Scheduler calls agent.compact_context() method.
  • Configuration file loaded at startup; hot-reload on SIGHUP (optional).

Effort

  • Config loader: 2h
  • Scheduler background task: 4h
  • Compaction logic adjustments (already exists?): 4h
  • Testing (simulate large context): 4h
  • Documentation: 2h
  • Total: ~16h

Monitoring

  • Log each compaction event: tokens_before, tokens_after, duration, model used.
  • Prometheus metric or simple counter for observability.

Related

  • Builds on ToolResultCompactor (#41962) which handles individual tool truncation.
  • Works with two-strike enforcement to avoid runaway sessions.

extent analysis

Fix Plan

To implement proactive compaction, follow these steps:

  • Step 1: Create a configuration loader
    • Load configuration from ~/.openclaw/compaction.json at startup
    • Handle SIGHUP for hot-reloading configuration (optional)
    • Example code:

import json import os import signal

def load_config(): config_path = os.path.join(os.path.expanduser("~"), ".openclaw", "compaction.json") with open(config_path, "r") as f: return json.load(f)

def reload_config(signum, frame): global config config = load_config()

config = load_config() signal.signal(signal.SIGHUP, reload_config)

*   **Step 2: Implement the CompactionScheduler class**
    *   Initialize with loaded configuration
    *   Run in background using `asyncio`
    *   Evaluate conditions and trigger compaction if necessary
    *   Example code:
        ```python
import asyncio

class CompactionScheduler:
    def __init__(self, config):
        self.config = config
        self.last_compaction = 0

    async def run(self):
        while True:
            await asyncio.sleep(1)
            if self.should_compact():
                await self.compact_context()

    def should_compact(self):
        # Evaluate conditions based on configuration
        # ...
        return True  # Replace with actual logic

    async def compact_context(self):
        # Call agent.compact_context() method
        # ...
        pass
  • Step 3: Integrate with agent main loop
    • Initialize CompactionScheduler with loaded configuration
    • Start scheduler in background
    • Example code:

scheduler = CompactionScheduler(config) asyncio.create_task(scheduler.run())

*   **Step 4: Implement compaction logic**
    *   Preserve last `overlap_raw` messages as raw
    *   Summarize older messages using `summarizer_model`
    *   Replace context with summary + overlap
    *   Record compaction event in memory
    *   Example code:
        ```python
def compact_context(self):
    # Get current context messages
    context = # ...

    # Preserve last overlap_raw messages as raw
    raw_messages = context[-self.config["overlap_raw"]:]

    # Summarize older messages using summarizer_model
    summary = # ...

    # Replace context with summary + overlap
    new_context = summary + raw_messages

    # Record compaction event in memory
    # ...

Verification

To verify that the fix worked:

  • Monitor compaction events and logs
  • Check Prometheus metrics or simple counter for observability
  • Simulate large context and verify that compaction is triggered correctly

Extra

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING