Skip to content

Critical: Session Compaction System Not Functioning - Sessions Exceed 32k Limit #24165

Description

@ianbaistowdavies-spec

Bug Report: Session Compaction System Not Working

Date: 2026-02-23
Reporter: Ian Baistow-Davies ([email protected])
OpenClaw Version: 2026.2.22-2
Platform: Windows 11 (10.0.26200), Node 22.13.1
Issue Type: Critical - Memory/Performance


Summary

Session compaction is not functioning despite being configured with mode: "safeguard" and contextTokens: 32000. Active Telegram sessions are exceeding the 32k token limit (reaching 126-159%) within minutes of activity, indicating the compaction system is not triggering at the expected 80% threshold (25.6k tokens).


Expected Behavior

According to OpenClaw documentation:

  1. Compaction should trigger at 80% capacity:

    • With 32k context: compaction at ~25.6k tokens
    • Mode "safeguard": Conservative automatic compaction
  2. Context pruning should remove old content:

    • TTL: 15 minutes
    • Mode: "cache-ttl"
    • Old messages should be pruned after idle period
  3. Sessions should not exceed configured limit:

    • contextTokens: 32000
    • Sessions should stay within or compact before reaching limit

Actual Behavior

Timeline of Observed Issues

Initial State (Before Update)

  • Date: 2026-02-23 02:00 AM
  • Telegram session: 51k/32k tokens (159%)
  • Age: Only 22 minutes old
  • Issue: Uncontrolled growth, compaction never triggered

After Update to 2026.2.22-2 (First Check)

  • Date: 2026-02-23 04:17 AM
  • Telegram session: 21k/32k tokens (65%)
  • Status: Appeared fixed - session properly managed
  • Conclusion: Update seemed to include compaction fixes

After Force Reinstall (Current State)

  • Date: 2026-02-23 04:30 AM
  • Telegram session: 40k/32k tokens (126%)
  • Age: Only 13 minutes
  • Issue: Rapid growth resumed - 3.1k tokens/minute growth rate

Key Observations

  1. Compaction never triggers:

    • Sessions routinely exceed 32k limit
    • No evidence of compaction at 80% threshold (25.6k)
    • Sessions grow unbounded until system intervention
  2. Context pruning ineffective:

    • Sessions retain full history despite 15m TTL
    • Stale cron sessions (2+ hours old) still contain 74k tokens
    • No automatic cleanup of old context
  3. Growth rate unsustainable:

    • Telegram session: 40k tokens in 13 minutes = ~3.1k tokens/min
    • At this rate: 93k tokens/hour
    • Cost impact: Significant (prompt caching only 49% effective)

Configuration

File: C:\Users\ianba\.openclaw\openclaw.json

{
  "agents": {
    "defaults": {
      "contextTokens": 32000,
      "contextPruning": {
        "mode": "cache-ttl",
        "ttl": "15m"
      },
      "compaction": {
        "mode": "safeguard"
      },
      "maxConcurrent": 1
    }
  }
}

Session Configuration:

  • Default model: google/gemini-2.5-flash
  • Context window: 32k tokens
  • Session target: "main" (persistent sessions)
  • DM scope: "per-channel-peer"

Evidence

Current Session Status (2026-02-23 04:30 AM)

Sessions
┌──────────────────────────────────────────────┬────────┬─────────┬──────────────────┬────────────────────────────────┐
│ Key                                          │ Kind   │ Age     │ Model            │ Tokens                         │
├──────────────────────────────────────────────┼────────┼─────────┼──────────────────┼────────────────────────────────┤
│ agent:main:telegram:direct:7249…             │ direct │ 13m ago │ gemini-2.5-flash │ 40k/32k (126%) · 🗄️49% cached │
│ agent:main:main                              │ direct │ 13m ago │ gemini-2.5-flash │ 28k/32k (87%)                  │
│ agent:main:cron:3db4f6c4-8692-4…             │ direct │ 2h ago  │ gemini-2.5-flash │ 74k/32k (232%) · 🗄️2% cached  │
└──────────────────────────────────────────────┴────────┴─────────┴──────────────────┴────────────────────────────────┘

Growth Pattern Analysis

Time Telegram Session Growth Rate
02:00 AM 51k (159%) - -
04:17 AM (after update) 21k (65%) Reset -
04:30 AM (after reinstall) 40k (126%) +19k in 13 min 3.1k/min

Expected behavior: Should have compacted at 25.6k (80%)
Actual behavior: Grew from 21k → 40k without compaction


Impact Assessment

Performance Impact

  • Memory usage: Sessions accumulating unbounded context
  • API costs: Higher token usage on every request
  • Response latency: Larger context = slower processing

Cost Impact (Estimated Annual)

Telegram session alone:

  • Current: 40k tokens/request at ~12 requests/hour
  • Expected: 25k tokens/request (with proper compaction)
  • Excess: 15k tokens × 12 requests × 24 hours × 365 days
  • Additional cost: ~$789/year (at $0.0001/1k input tokens)

All bloated sessions combined:

  • Estimated additional cost: $2,000-3,000/year

Related Issues

1. Isolated Session Mode Bug (Issue #24101)

  • Status: Previously reported 2026-02-23
  • Issue: Isolated cron sessions accumulate context despite being marked "isolated"
  • Evidence: pairing-watch session at 74k tokens after 22 hours
  • Relation: Both issues suggest compaction system fundamentally broken

2. Memory System Module Import Failure (Resolved)

  • Issue: Hash mismatch in module imports after npm update
  • Error: Cannot find module 'manager-DRT2R_zd.js'
  • Resolution: Force reinstall with npm install -g openclaw@latest --force
  • Status: ✅ Fixed - Memory system now operational

Reproduction Steps

  1. Install OpenClaw 2026.2.22-2
  2. Configure session with:
    {
      "contextTokens": 32000,
      "compaction": { "mode": "safeguard" },
      "contextPruning": { "mode": "cache-ttl", "ttl": "15m" }
    }
  3. Start Telegram conversation with agent
  4. Send 10-15 messages with tool calls over 10-15 minutes
  5. Check openclaw status → session will exceed 32k tokens
  6. Expected: Compaction at ~25.6k tokens
  7. Actual: No compaction, session grows to 40k+ tokens

Attempted Solutions

1. ✅ Updated to 2026.2.22-2

  • Result: Temporarily appeared to fix (session at 21k)
  • Outcome: Issue returned after reinstall (session at 40k)

2. ❌ Attempted "aggressive" compaction mode

  • Error: "Invalid input" - mode not recognized
  • Available modes: Unclear (documentation needed)

3. ✅ Force reinstall to fix module hash mismatch

  • Result: Memory system fixed, but compaction still broken

4. ⏳ Pending: Restart gateway to clear stale sessions

  • Status: Not yet attempted for this test

System Information

Environment:

OS: Windows 11 (10.0.26200 x64)
Node: 22.13.1
OpenClaw: 2026.2.22-2
Gateway: ws://127.0.0.1:18789 (local loopback)
Tailscale: serve · ianlaptop.tail713286.ts.net

Agents: 5 total (main, friends, rory-a, rory-b, switcher)
Memory: 42 files · 90 chunks · vector ready · fts unavailable
Channels: Telegram (2 accounts enabled)


Requested Actions

  1. Investigate compaction system:

    • Why does "safeguard" mode not trigger at 80% threshold?
    • Are there logs showing compaction attempts or failures?
    • Is there a configuration issue preventing compaction?
  2. Document valid compaction modes:

    • What modes are available? (safeguard, aggressive, off, on?)
    • What are the differences between modes?
    • When should each mode be used?
  3. Fix or document workaround:

    • If compaction is broken, provide hotfix or manual compaction command
    • If configuration issue, provide correct configuration example
    • If by design, document how to prevent unbounded growth
  4. Context pruning investigation:

    • Why do 2+ hour old sessions still retain full context?
    • Is "cache-ttl" mode working as documented?
    • Should TTL be shorter? Different mode?

Additional Notes

Workaround Currently Using

Manual session cleanup:

  1. Archive bloated sessions periodically
  2. Delete session files to force restart
  3. Monitor with custom PowerShell scripts

Script: C:\Users\ianba\Rorys_play_area\scripts\backup\archive-bloated-sessions.ps1

Frequency: Weekly or when sessions exceed 500KB

Effectiveness: Temporary - sessions rebloat within hours/days


Logs

Memory system log snippet:

[31m[memory][39m [33mfts unavailable: no such module: fts5[39m

(Note: FTS unavailability is separate issue, not related to compaction)

Session store locations:

  • Main: C:\Users\ianba\.openclaw\agents\main\sessions\sessions.json
  • Friends: C:\Users\ianba\.openclaw\agents\friends\sessions\sessions.json

Session file sizes:

  • Telegram session: ~1.1 MB (before cleanup)
  • Largest cleaned: 4.4 MB
  • Total cleaned: 17.49 MB across 14 sessions

Priority Justification

Priority: CRITICAL

Reasons:

  1. Cost impact: $2-3k/year in excess API costs
  2. Performance degradation: Slower responses, higher memory usage
  3. System stability: Unbounded growth will eventually cause crashes
  4. User experience: Degraded performance for all users affected
  5. Scope: Appears to affect all session types (main, isolated, telegram)

User Impact:

  • All OpenClaw users with active conversations
  • Especially high-frequency users (many messages/day)
  • Both isolated and persistent session modes affected

Related Files

Configuration: C:\Users\ianba\.openclaw\openclaw.json
Session storage: C:\Users\ianba\.openclaw\agents\main\sessions\
Archive: C:\Users\ianba\Rorys_play_area\archive\sessions-2026-02-23-0411\
Cleanup script: C:\Users\ianba\Rorys_play_area\scripts\backup\archive-bloated-sessions.ps1
Previous bug report: C:\Users\ianba\Rorys_play_area\BUG-REPORT-ISOLATED-MODE.md (Issue #24101)


Report Generated: 2026-02-23 04:35 AM GMT
Contact: [email protected]
Telegram: @IDMcrnYork (User ID: 7249188495)

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleMarked as stale due to inactivity

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions