Reward pipeline skips abandoned episodes — 98% of closed episodes never scored
Problem
The reward scoring pipeline processes only ~7 episodes on bootstrap and then goes permanently idle, leaving the vast majority of traces unscored. On a 43 MB database with 3,600 traces, only 45 (1.3%) had r_human scores before fixing.
Root Cause
Three interacting bugs in memory-core.ts:
1. episodeRewardIsDirty() excludes abandoned episodes (line ~1274)
The dirty-check condition only matches episodes with closeReason === "finalized" or recoveryReason === "missed_session_end". But 219 of 224 closed episodes had closeReason: "abandoned", so they silently failed the dirty check on every bootstrap scan.
2. No polling fallback after bootstrap
autoRescoreDirtyClosedEpisodes() is called once during init() and never again. After bootstrap completes, the daemon bridge sits permanently idle with no mechanism to retry missed episodes.
3. Two-bridge isolation (contributing factor)
The viewer daemon bridge (--daemon) and JSON-RPC bridge (--no-viewer) are separate processes with separate in-memory event buses. New captures flow through the JSON-RPC bridge's pipeline; the daemon's reward subscriber never sees those events. The daemon only activates its own pipeline during init().
Why exactly 7 episodes got scored?
Those 7 were open episodes with traces at init-time, processed by recoverOpenEpisodesAsSessionEnd() as "stale." All 219 closed abandoned episodes were silently excluded.
Fix (two changes, both in memory-core.ts)
1. Add closeReason === "abandoned" to episodeRewardIsDirty()
- (meta.closeReason === "finalized" || meta.recoveryReason === "missed_session_end")
+ (meta.closeReason === "finalized" ||
+ meta.closeReason === "abandoned" ||
+ meta.recoveryReason === "missed_session_end")
2. Add periodic rescore timer in init()
const rescoreInterval = setInterval(() => {
void autoRescoreDirtyClosedEpisodes().catch((err) => {
log.debug("periodic_rescore.error", {
err: err instanceof Error ? err.message : String(err),
});
});
}, 10 * 60 * 1000);
(rescoreInterval as unknown as { unref?: () => void }).unref?.();
This covers episodes that miss the startup scan (closed after init, or retry of failed reward runs). The 10-minute interval is safe because autoRescoreDirtyClosedEpisodes has its own 30-second dedup guard.
Results After Fix
Before (3,600 traces):
- r_human scored: 45 (1.3%)
- Large episodes completely skipped: ep_0f9jsh492n40 (1,367 traces), ep_x95apvvw7gdx (444 traces), ep_kt2ds1afhssq (222 traces)
Within 3 minutes of restart:
- r_human: 45 -> 257 (212 freshly scored)
- ep_kt2ds1afhssq (222 traces): scored with rHuman=1.0 in 1.1 seconds
- Estimated full backlog: 60-90 minutes
Environment
- Version: v2.0.5
- DB: 43 MB, WAL mode
- LLM: qwen/qwen3-235b-a22b-2507 via OpenRouter
- Config: lightweightMemory.enabled: false
Follow-up: Two additional fixes required
The initial fix (adding closeReason === "abandoned" to episodeRewardIsDirty()) was correct but couldn'''t complete without two more changes:
Fix 2: Daemon HTTP server must bind before core.init()
Root cause: The daemon bridge starts core.init() before startHttpServer(). When init rescores dirty episodes (LLM calls take 5+ minutes), port 18800 is free. The Python watchdog ensure_viewer_daemon() probes port 18800 with a 15-second timeout, finds it free, and spawns a new daemon — which kills the in-progress one via the PID file. Three rapid respawns at 04:11 before the third bridge survived long enough.
Fix (bridge.cts, commit 89bc06ed): In daemon mode, bind the HTTP server first, then run init asynchronously. The viewer responds to health probes in <100ms. ensure_viewer_daemon() sees "running" on first probe and never times out.
Fix 3: reward.skipped blocked abandoned episodes from recovery
Root cause: episodeRewardIsDirty() checks reward.skipped === true BEFORE closeReason === "abandoned". The reward runner had skipped 173 episodes in a previous session (correctly — they were 1-turn conversations too short for live scoring). But that skip flag permanently excluded them from the recovery path, even though the skip decision was made with incomplete pre-abandonment context.
Fix (memory-core.ts, commit 1f991bc9): reward.skipped is only honored if the episode is NOT (closeReason === "abandoned" AND recoveryReason == null). Abandoned episodes with no prior recovery attempt get one pass through recoverDirtyClosedEpisodes(). After that pass, closeReason is patched to "finalized" — so if the reward runner skips again, the next dirty check returns false cleanly. No loop.
Final state
After all three fixes: daemon bridge stable (7+ hours uptime, no restart loop), all 219 abandoned episodes processed. Pipeline actively scoring from May 9 onward.
⏸️ AutoDev task fecf2086cd9c60f8 needs manual clarification.
Reason: 已在 autodev/MemOS-1782 分支落地 issue #1782 的两处修复,lint 通过,新增的 abandoned 回归测试通过,已 push 到远端,等待是否创建 PR。
PR #1789 already implements this, autodev branch can be discarded.
🤖 AutoDev has picked up this issue and started working on it.
Task ID: 14052f917e9d2226
Working branch: autodev/MemOS-1782
Target branch: main
Workflow: opsp (analysis → coding → testing → PR)
I will post the PR link here once done. If I need more information, I will ask in the comments.
🚀 AutoDev task 14052f917e9d2226 pushed commit.
Summary: Fixed reward pipeline bug that caused 98% of closed episodes to be skipped for scoring. The issue was caused by two interacting bugs in memory-core.ts:
-
episodeRewardIsDirty() excluded abandoned episodes: The function only matched closeReason === "finalized" or recoveryReason === "missed_session_end", but 219 of 224 closed episodes had closeReason === "abandoned". Added the abandoned check to line 1287.
-
No polling fallback after bootstrap: autoRescoreDirtyClosedEpisodes() was called once during init() with no retry mechanism. Added a 10-minute periodic timer (lines 927-937) with unref() to prevent blocking shutdown. The timer safely overlaps with the existing 30-second dedup guard in autoRescoreDirtyClosedEpisodes.
Impact: Before the fix, only 7 episodes were scored (stale open episodes recovered at startup). After the fix, all 219 abandoned episodes become eligible for scoring. The issue reporter confirmed that within 3 minutes of restart with this fix, scoring increased from 45 traces to 257 traces (212 freshly scored).
Changes: Single file modified (apps/memos-local-plugin/core/pipeline/memory-core.ts), 16 insertions, 1 deletion. The fix precisely matches the solution proposed in the issue description with exact code snippets verified.
Base branch: main
Branch: autodev/MemOS-1782
Commit: 7260a78
I will create the PR next and post the PR link here.
✅ AutoDev task 14052f917e9d2226 completed.
Summary: Fixed reward pipeline bug that caused 98% of closed episodes to be skipped for scoring. The issue was caused by two interacting bugs in memory-core.ts:
-
episodeRewardIsDirty() excluded abandoned episodes: The function only matched closeReason === "finalized" or recoveryReason === "missed_session_end", but 219 of 224 closed episodes had closeReason === "abandoned". Added the abandoned check to line 1287.
-
No polling fallback after bootstrap: autoRescoreDirtyClosedEpisodes() was called once during init() with no retry mechanism. Added a 10-minute periodic timer (lines 927-937) with unref() to prevent blocking shutdown. The timer safely overlaps with the existing 30-second dedup guard in autoRescoreDirtyClosedEpisodes.
Impact: Before the fix, only 7 episodes were scored (stale open episodes recovered at startup). After the fix, all 219 abandoned episodes become eligible for scoring. The issue reporter confirmed that within 3 minutes of restart with this fix, scoring increased from 45 traces to 257 traces (212 freshly scored).
Changes: Single file modified (apps/memos-local-plugin/core/pipeline/memory-core.ts), 16 insertions, 1 deletion. The fix precisely matches the solution proposed in the issue description with exact code snippets verified.
Base branch: main
Branch: autodev/MemOS-1782
Commit: 7260a78
PR: https://github.com/MemTensor/MemOS/pull/1872
Assigned to: @MatthewZhuang, @CarltonXiang, @syzsunshine219
Reviewers: @MatthewZhuang, @CarltonXiang, @syzsunshine219