Skip to content

Sub-agent session store entries persist after archiveAfterMinutes sweep (sessions.delete failure silently swallowed) #49000

Closed

Description

@100yenadmin

Description

When agents.defaults.subagents.archiveAfterMinutes triggers a sweep, the subagent run record is deleted from runs.json but the corresponding session store entry in sessions.json persists indefinitely if the sessions.delete gateway RPC fails.

Root Cause

In subagent-registry.ts, sweepSubagentRuns():

  1. The run record is deleted from the in-memory map and persisted to disk
  2. sessions.delete is called via callGateway()
  3. The call is wrapped in try/catch {} that silently swallows all errors

If the gateway is temporarily unavailable (restart, timeout, connection issue), the session store entry is never cleaned up, and there's no retry or reconciliation mechanism.

Expected Behavior

Session store entries should be reliably cleaned up when a subagent run is swept.

Actual Behavior

Session store entries accumulate indefinitely. On a system with archiveAfterMinutes: 720, we observed 5 orphaned session store entries aged 3-40 hours with no corresponding runs.json record.

Suggested Fix

  1. Either move subagentRuns.delete() to after successful sessions.delete, or implement retry
  2. Add reverse orphan reconciliation in reconcileOrphanedRestoredRuns() — scan sessions.json for subagent-keyed entries that have no matching runs.json record, and clean them up on startup

Environment

  • OpenClaw 2026.3.13
  • macOS arm64
  • archiveAfterMinutes: 720

Reproduction

  1. Set agents.defaults.subagents.archiveAfterMinutes to a small value (e.g., 5)
  2. Spawn several subagents (mode: "run")
  3. Wait for sweep interval (60s)
  4. Restart gateway while sweep is in progress, OR trigger gateway restart via config change during sweep window
  5. Observe: runs.json entries cleaned up, but sessions.json entries persist

Activity

  1. Ryce commented on Mar 17, 2026

    @Ryce
  2. Hollychou924 commented on Mar 17, 2026

    @Hollychou924
  3. astroclaw commented on Mar 17, 2026

    @astroclaw

    This is closely related to #29795 (sub-agents persist after reboot) — both stem from the same root problem: runs.json and sessions.json are two independent sources of truth with no atomic operation or reconciliation between them.

    The ordering fix (delete sessions first, then runs) is necessary but not sufficient — a crash between the two operations still leaves orphans. The reconciliation approach is the real fix.

    One practical addition to the suggested reconciliation in reconcileOrphanedRestoredRuns(): add a grace period (e.g. 5 min) before deleting orphaned session entries on startup, to avoid a race where a subagent session was just created but the runs.json write hasn't flushed yet. This edge case is especially relevant on systems with slow disk I/O or during high subagent spawn rates.

    For anyone hitting this now: you can manually clean up orphaned entries by stopping the gateway, diffing the session keys in sessions.json against runs.json, removing entries that exist only in sessions, and restarting.

  4. steipete commented on Apr 25, 2026

    @steipete
    Contributor

    Closing this as implemented after Codex review.

    Current main already keeps archived sub-agent runs when sessions.delete fails, retries cleanup on later sweeps, and has regression coverage for the reported failure mode. The changelog records this as shipped in v2026.4.10.

    What I checked:

    • Sweep now retries instead of dropping run record first: sweepSubagentRuns() calls sessions.delete, logs and continues on failure, and only deletes the in-memory run after the gateway delete succeeds. (src/agents/subagent-registry.ts:606)
    • Regression test covers failed delete then later successful retry: Test keeps archived delete-mode runs for retry when sessions.delete fails asserts the run is still present after the first delete failure and is removed only after the second sweep succeeds. Adjacent test also verifies overlapping sweeps do not duplicate in-flight cleanup. (src/agents/subagent-registry.archive.e2e.test.ts:124)
    • Release notes say this shipped: The 2026.4.10 changelog section includes: Agents/subagents: preserve archived delete-mode runs until sessions.delete succeeds and prevent overlapping archive sweeps from duplicating in-flight cleanup attempts. (#61801). (CHANGELOG.md:863)

    So I’m closing this as already implemented rather than keeping a duplicate issue open.

    Review notes: reviewed against 01bc49c88c0c; fix evidence: release v2026.4.10.

  5. locked as resolved and limited conversation to collaborators on Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions