MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

fix(reward): scoring pipeline dirty-scan and skip-path fixes

Open chiefmojo opened this issue 6 months ago • 0 comments

Summary

Six targeted fixes to the dirty-episode recovery and scoring skip path. These close infinite rescore loops that appeared in companion infrastructure after the v2.0.5 regression fixes (PR #1784) landed.

1. Paginate dirty-closed episode scan

collectDirtyClosedEpisodes() replaces the fixed list({ limit: 500 }) call. Walks all pages so installations with >500 closed dirty episodes are fully recovered on startup, not silently truncated.

2. Set r_task=0 on skipped episodes

When reward.ts skips an episode (too few exchanges, trivial content), it sets reward.skipped: true but returns without writing r_task. The episode stays in the dirty-scan queue and is re-queued on every startup pass. Fix: write r_task=0 on skip exit.

3. Clear rewardDirty flag on skip path

rewardDirty was only cleared on successful scoring. Skipped episodes kept the dirty flag and re-entered the scan on the next pass. Fix: clear rewardDirty on the skip path alongside setting r_task=0.

4. Drain reward after dirty-closed recovery in lightweight mode

recoverDirtyClosedEpisodes() emits episode.finalized and calls flush(). In lightweight mode flush() returns before draining the reward subscriber. Fix: explicitly call rewardRunner.run() for each recovered episode that remains dirty after flush.

5. Clear rewardDirty before recovery scoring

Recovery stamped closeReason and recoveryReason but did not clear rewardDirty. If the watchdog fires mid-scoring and leaves rTask null, the next startup re-picks the episode indefinitely. Fix: add rewardDirty: undefined to the updateMeta call before recovery scoring begins.

6. Prevent open-episode crash-respawn loop on watchdog interrupt

episodeRewardIsDirty() matched any episode with closeReason="finalized" and no rTask, including those already stamped with recoveryReason="dirty_reward_rescore" by recovery. Adds a guard: episodes already in a recovery path are excluded from the dirty scan.

Test plan

  • [ ] Fresh DB: startup recovery with 0 dirty episodes — no change in behavior
  • [ ] DB with >500 closed dirty episodes: all pages are walked, all are recovered
  • [ ] Episode with too few exchanges: scored once, r_task=0 written, not re-queued on next startup
  • [ ] Lightweight mode: recovery episodes are fully scored after flush()
  • [ ] Watchdog interrupt simulation: episode stamped with recoveryReason="dirty_reward_rescore" is not re-picked on next startup

🤖 Generated with Claude Code

chiefmojo avatar Jun 01 '26 01:06 chiefmojo