MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

Restart during in-flight reflection scoring orphans finalized episodes (rTask NULL, never re-scored)

Open chiefmojo opened this issue 2 months ago • 3 comments

Summary

When the bridge daemon is restarted (or the scoring watchdog interrupts) during in-flight reflection scoring, the affected episode is left finalized with rTask = NULL and no recoveryReason stamp. It then lingers in a half-recovered state that the consistency watchdog flags as will_be_dirty every tick, until a human hand-patches r_task in the database.

Environment

  • Version: memos-local-plugin v2.0.16
  • algorithm.lightweightMemory.enabled: false
  • autoRecovery at default (enabled)

Observed behavior

Two independent occurrences in the same session on 2026-09-09:

  1. A chitchat episode (ep_7fw8xfeb4skp, 7 traces) — finalized, rTask = NULL, no stamp.
  2. A task episode (ep_c28q4aka3s7z, 12 traces) — finalized, rTask = NULL, no stamp.

Both triggered the Will trigger rescore (combined): 1 consistency alert and did not self-heal; each was resolved by manually setting r_task = 0.0.

Root cause

The reflection scoring pipeline is not restart-safe. Log evidence for ep_c28q4aka3s7z shows the full interrupt sequence:

capture.reflect.scoring.start   episodeId="ep_c28q4aka3s7z" steps=12
capture.reflect.trace.scored    episodeId="ep_c28q4aka3s7z" ... alpha=0.5 usable=true
  → (fresh daemon boot: llm init + sqlite.open, NO capture.reflect.done)

The daemon was restarted after scoring began but before it wrote rTask, leaving the episode finalized with:

  • rTask = NULL
  • trace_ids_json length > 0
  • closeReason = "finalized"
  • recoveryReason = absent ← the crux

The asymmetry in episodeRewardIsDirty()

The dirty-check guard at core/pipeline/memory-core.ts (≈ lines 1941–1954) anticipates this exact race:

if (
  ep.rTask == null &&
  (ep.traceIds?.length ?? 0) > 0 &&
  // Episodes already attempted by a recovery path carry DIRTY_REWARD_RESCORE.
  // Excluding them prevents a crash-respawn loop when the watchdog fires
  // mid-scoring and leaves rTask null: without this guard the next startup
  // would re-pick the episode via closeReason="finalized" indefinitely.
  meta.recoveryReason !== RECOVERY_REASONS.DIRTY_REWARD_RESCORE &&
  (meta.closeReason === "finalized" || ...)
) {
  return true;
}

The DIRTY_REWARD_RESCORE stamp — the thing that would move an interrupted episode out of the "should rescore" branch — is only written by the recovery path (recoverDirtyClosedEpisodes(), line ~1784), not by first-pass scoring.

Consequence: an interruption during first-pass scoring leaves no stamp. The episode then sits in the episodeRewardIsDirty() → true branch with no clean path out:

  • The consistency watchdog's will_be_dirty clause flags it (because recoveryReason != dirty_reward_rescore and rTask IS NULL).
  • The bridge's own dirty-rescan either never re-visits it promptly, or the episode falls into a state where the rescan and the watchdog disagree indefinitely.

The guard comment explicitly says the exclusion is meant to prevent a crash-respawn loop — but that protection only exists for episodes that already carry the stamp, i.e. episodes interrupted during a recovery pass. First-pass interruptions are unhandled.

Proposed fix direction

One of:

  1. On startup / periodic scan, treat finalized + rTask == null + traces > 0 + recoveryReason == null as rescorable and actually re-run the reward pass (rather than only excluding stamped episodes).
  2. Or stamp first-pass-interrupted episodes with DIRTY_REWARD_RESCORE at close time (or on the next scan), so they consistently drop out of both the dirty-check and the watchdog — accepting that their reward is lost, but without the perpetual alert.

Either resolves the disagreement between the consistency watchdog and episodeRewardIsDirty() that currently requires manual r_task patching.

Reproduction

  1. Start a task-intent conversation with enough turns (≥ 2 QA rounds) to trigger reflection scoring.
  2. Restart the bridge daemon after capture.reflect.scoring.start is logged but before capture.reflect.done.
  3. Observe: the episode finalizes with rTask = NULL and no recoveryReason, then trips Will trigger rescore (combined): 1 on each consistency check without self-healing.

Relevant prior issues reviewed (#1782, #1966, #1841, #1452) all describe different failure modes (abandoned-episode skip, ghost-trace over-scoring, startup-hang).

chiefmojo avatar Sep 10 '26 04:09 chiefmojo

Additional path to the same symptom — scoring completes with nothing scoreable

Following up on the report above, there is a second and distinct way an episode ends up finalized with rTask = NULL.

The case in the original report is interrupted scoring: capture.reflect.scoring.start fires, then the process goes down before capture.reflect.done, so no reward is ever written.

This one is different — the scoring pass completes normally:

capture.reflect.scoring.start  steps=1  mode="batch_windowed_binary"
capture.reflect.trace.scored   alpha=0  usable=false  reason="SOCIAL_ONLY"  reflection="IRRELEVANT"
capture.reflect.done           traces=1  warnings=0  totalMs=967

done fires, warnings=0, and nothing is interrupted — the next daemon boot in that log was ~84s after scoring finished. The episode had exactly one trace, and that single trace was scored unusable (usable=false, reason="SOCIAL_ONLY"). No reward object is written into the episode meta, so rTask remains NULL.

episodeRewardIsDirty() then returns true for the episode via the clause in core/pipeline/memory-core.ts (the rTask == null && traceIds.length > 0 && closeReason === "finalized" branch), so the row is treated as permanently dirty. The 10-minute auto-rescore picks it up, re-runs scoring, reaches the identical conclusion, and the episode never clears — an indefinite no-op rescore loop that also keeps a consistency watchdog alerting on it.

Net effect: the dirty predicate cannot distinguish "scoring was orphaned before it could run" from "scoring ran to completion and found nothing scoreable." Both present as finalized + rTask == null + traceIds.length > 0, so both match the same clause — but one wants a re-run, and the other wants a terminal value.

Suggestion: when a scoring pass completes and yields zero usable traces, write the reward anyway (e.g. rTask = 0) so the episode reaches a terminal state — or otherwise mark it so the branch above stops matching. That turns the completed-but-nothing-scoreable case into a self-resolving one instead of an endless rescore.

Happy to supply fuller logs if that would help.

chiefmojo avatar Sep 10 '26 16:09 chiefmojo

Confirmation of the interrupted-scoring path, with a measured exposure window

Same fingerprint again, third site in our fleet, memos-local-plugin v2.0.16 — and this one has the line-level evidence of the interrupt, which may help size the fix.

Episode ep_751fqmpz1tee (13 traces, finalized):

17:06:28.864 INFO  [core.capture] capture.reflect.scoring.start episodeId="ep_751fqmpz1tee" steps=13 mode="batch_windowed_binary" model="google/gemini-2.5-flash-lite"
   <- nothing further for this episode; the next event in the log is a fresh boot
17:15:57.522 WARN  [core.pipeline.bootstrap] config.warning ...          <- new daemon boot (config load)
17:15:57.537 INFO  [storage] sqlite.open ... wal=true busyTimeoutMs=5000
17:15:59.263 INFO  [storage.migration] migration.applied version=907 ...

No capture.reflect.done and no capture.reflect.trace.scored for the episode at any point. Resulting row: closeReason = "finalized", rTask = NULL, traceIds length 13, recoveryReason absent — same shape as both cases in the report.

The one measurement I'd add to the writeup: the in-flight window here was ~9.5 minutes — scoring began at 17:06:28.864 and the restart landed at 17:15:57. The pass was still running when the process went away. So this isn't a narrow race you'd have to be unlucky to hit: with batch_windowed_binary over 13 steps, any restart inside a several-minute window orphans the episode. That argues for resolving it at the write boundary (report options 1 or 2) rather than relying on restart discipline.

Worth noting the restart was a deliberate, planned rollout of a new build rather than a crash — the boot reconciler and autoRecovery both ran and neither claimed this episode, because it carries no recoveryReason to match on.

Manually setting r_task = 0.0 cleared the alert the same minute, consistent with the other two cases. Happy to supply the surrounding log or the raw row if useful.

chiefmojo avatar Sep 15 '26 21:09 chiefmojo

The crux is that "finalized" was written before the score that justifies it — a half-completed state the watchdog can see (will_be_dirty) but nothing can repair without a human. Restart-safety is exactly the property an append-only log buys you: in Vestige Strata every write passes a gate and returns a receipt, so a state transition either landed with its receipt or never happened — there is no window where the fact of completion exists without the evidence of it. Concretely for your pipeline: append scoring.start and scoring.done as log records (not column updates), and recovery becomes "replay anything with a start and no done" — your recoveryReason stamp would then be derived from the log instead of being one more thing to forget to write. (Disclosure: I built Strata — free and open: https://github.com/samvallad33/vestige · brew install samvallad33/tap/vestige · OpenClaw: clawhub install vestige-strata)

samvallad33 avatar Oct 01 '26 10:10 samvallad33