Skip to content

fix(memory-core): prevent dreaming-narrative session leaks (#66358)#1

Open
chiyouYCH wants to merge 1508 commits into
mainfrom
fix/dreaming-session-leak-66358
Open

fix(memory-core): prevent dreaming-narrative session leaks (#66358)#1
chiyouYCH wants to merge 1508 commits into
mainfrom
fix/dreaming-session-leak-66358

Conversation

@chiyouYCH

Copy link
Copy Markdown
Owner

Problem

runDreamingSweepPhases passes params.nowMs through to runLightDreaming / runRemDreaming without normalizing it. Each of those functions (and the nested generateAndAppendDreamNarrative) recomputes Date.now() independently:

// runDreamingSweepPhases passes params.nowMs (possibly undefined)
await runLightDreaming({ ..., nowMs: params.nowMs });

// runLightDreaming re-computes independently
const nowMs = Number.isFinite(params.nowMs) ? params.nowMs : Date.now();

// generateAndAppendDreamNarrative re-computes again
const sessionKey = `dreaming-narrative-${phase}-${nowMs}`;

The session key written at creation and the key used in the finally block's deleteSession can differ by a few milliseconds, so the deletion hits a non-existent key and the session leaks.

Fix

  1. runDreamingSweepPhases: Normalize nowMs once at the top and pass the consistent value to all phases. Add a defensive deleteSession call after each phase as a safety net.

  2. generateAndAppendDreamNarrative finally block: Guard params.subagent with if (params.subagent) to prevent TypeError if the runtime becomes unavailable mid-flight.

Testing

Manual trigger of the Dreaming sweep confirms no new dreaming-narrative-* sessions remain in the session store after sweep completion.

Fixes openclaw#66358.

@jalehman
jalehman force-pushed the fix/dreaming-session-leak-66358 branch 2 times, most recently from 3805afb to 88964cc Compare April 20, 2026 22:32
steipete and others added 18 commits April 20, 2026 23:33
…enclaw#66358)

Root cause: `runDreamingSweepPhases` passed `params.nowMs` through to each
phase without normalizing it first. Each phase recomputed `Date.now()` at
call time, producing a slightly different timestamp than the session key
used by `generateAndAppendDreamNarrative`. The resulting session key
mismatch meant the `deleteSession` call cleaned up a different (or
non-existent) key than the one that was created.

Fix:
- Normalize `nowMs` once at the top of `runDreamingSweepPhases` and
  pass the consistent value to all phases.
- Add a defensive `deleteSession` call in `runDreamingSweepPhases` for
  each phase after `runLightDreaming`/`runRemDreaming` completes. This
  acts as a safety net even if the narrative function's primary cleanup
  is skipped.
- Guard the `finally` block in `generateAndAppendDreamNarrative` with
  `if (params.subagent)` to prevent TypeError if the subagent runtime
  becomes unavailable mid-flight.

This fixes the observed session list pollution where
`dreaming-narrative-*-<timestamp>` entries accumulated indefinitely.
@jalehman
jalehman force-pushed the fix/dreaming-session-leak-66358 branch from 88964cc to 51f72b2 Compare April 20, 2026 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Dreaming creates redundant sessions in session list after sweep