openclaw - 💡(How to fix) Fix [Bug]: Subagent sessions accumulate indefinitely in sessions.json — no automatic cleanup after completion [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

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#75499Fetched 2026-05-02 05:33:52
View on GitHub
Comments
1
Participants
2
Timeline
2
Reactions
2
Timeline (top)
closed ×1commented ×1

Subagent sessions spawned via are never cleaned up from after they complete. Over time this causes sessions.json to grow large (in our case: 70 stale subagent entries accumulated over ~1 month).

Error Message

  1. A subagent session is spawned via (runtime=subagent, no explicit sessionKey)
  2. The subagent completes its work and presumably terminates
  3. Result: The subagent entry persists indefinitely in

Root Cause

Root cause hypothesis

Fix Action

Fix / Workaround

Workaround (manual)

Code Example

# Rough pattern - remove all subagent entries
import json
with open('sessions.json') as f:
    data = json.load(f)
before = len(data)
for k in list(data.keys()):
    if ':subagent:' in k:
        del data[k]
print(f'Removed {before - len(data)} entries')
RAW_BUFFERClick to expand / collapse

Summary

Subagent sessions spawned via are never cleaned up from after they complete. Over time this causes sessions.json to grow large (in our case: 70 stale subagent entries accumulated over ~1 month).

Bug type

Behavior bug — session store grows without bound

Observed behavior

  1. A subagent session is spawned via (runtime=subagent, no explicit sessionKey)
  2. The subagent completes its work and presumably terminates
  3. Result: The subagent entry persists indefinitely in

Example entries found in sessions.json (anonymized):

  • key: agent:main:subagent:<uuid> (70 instances total)
  • labels include: morning-report-2026-04-26, push-china-opensource-v2, ai-memory-dark-cover, etc.
  • All have but are never removed

Expected behavior

Completed subagent sessions should be automatically removed from sessions.json (similar to how cron execution records are managed, or how dream sessions have an archive mechanism).

Root cause hypothesis

When a subagent run completes, there is no cleanup handler that removes its entry from sessions.json. The entries are effectively orphaned within the session store even though the subagent process has exited.

Workaround (manual)

Manually editing sessions.json to remove keys works but is not sustainable:

# Rough pattern - remove all subagent entries
import json
with open('sessions.json') as f:
    data = json.load(f)
before = len(data)
for k in list(data.keys()):
    if ':subagent:' in k:
        del data[k]
print(f'Removed {before - len(data)} entries')

Environment

  • OpenClaw: 2026.4.14
  • Node: v24.13.0
  • OS: macOS 26.3 (ARM)
  • Channels: webchat, feishu, wechat, qq, etc.

Labels

bug, component:agents, component:session

extent analysis

TL;DR

Implement a cleanup handler to automatically remove completed subagent sessions from sessions.json.

Guidance

  • Investigate the subagent termination process to identify why the cleanup handler is not triggered, focusing on the interaction between the subagent and the session store.
  • Verify that the subagent process is properly exiting and that its completion is correctly signaled to the session store.
  • Consider implementing a periodic task to clean up stale subagent entries from sessions.json as a temporary workaround until a permanent fix is in place.
  • Review the session store management for other types of sessions (e.g., cron execution records, dream sessions) to identify potential patterns or mechanisms that could be applied to subagent sessions.

Example

# Example of a periodic cleanup task
import json
import schedule
import time

def cleanup_subagent_sessions():
    with open('sessions.json') as f:
        data = json.load(f)
    for k in list(data.keys()):
        if ':subagent:' in k:
            del data[k]
    with open('sessions.json', 'w') as f:
        json.dump(data, f)

schedule.every(1).day.at("00:00").do(cleanup_subagent_sessions)  # Run daily at midnight

while True:
    schedule.run_pending()
    time.sleep(1)

Notes

The provided workaround is manual and not sustainable, so a more automated solution is needed. The example cleanup task is a basic illustration and may need to be adapted to the specific requirements and constraints of the system.

Recommendation

Apply a workaround, such as the periodic cleanup task, until a permanent fix can be implemented to automatically remove completed subagent sessions from sessions.json. This will help mitigate the issue of growing session store size.

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…

FAQ

Expected behavior

Completed subagent sessions should be automatically removed from sessions.json (similar to how cron execution records are managed, or how dream sessions have an archive mechanism).

Still need to ship something?

×6

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

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix [Bug]: Subagent sessions accumulate indefinitely in sessions.json — no automatic cleanup after completion [1 comments, 2 participants]