MiMo-Code icon indicating copy to clipboard operation
MiMo-Code copied to clipboard

BUG-Old sessions (pre-0.1.12) unresponsive after upgrade: message ID format change breaks string-comparison ordering

Open NMDX0721 opened this issue 3 months ago • 1 comments

Summary

After upgrading to 0.1.12, all OLD sessions (created with 0.1.10/0.1.11) stop responding: new user messages are persisted to the DB but the prompt loop never executes. Messages appear "eaten", render in the WRONG position (above older assistant replies), and TUI shows them as QUEUED even after an assistant reply exists. Sessions freshly created in 0.1.12 work fine.

Additional symptom: forced session reset

When the prompt loop repeatedly fails (classification=final exiting loop), the system may automatically reset the session context — deleting historical messages and starting fresh. On this machine:

  • RailRevivalSim (438 messages → 8) — full history wiped
  • Max开发 (hundreds of messages → 13) — effectively reset
  • RailwayRenaissance session was also observed as a "new conversation" after reset

The reset mechanism is unclear but appears to be a safety valve triggered when the session loop detects irresolvable stuck state (possibly via compact/summarize, checkpoint-writer failure, or a distinct compaction heuristic). The original messages are physically deleted from the database (not just compressed), so recovery requires a pre-reset backup. This compounds the user impact: not only do messages get eaten, but the entire conversation history can be silently lost.

Critically, resetting the session does NOT fix the underlying ID-ordering problem — new messages in the reset session will still be eaten until the root cause is addressed.


Root Cause (confirmed with binary + DB evidence)

Message ID format changed between versions, breaking string-comparison ordering.

Version Message ID format Example
≤0.1.11 msg_<hex> with 5th char a letter msg_fdc599680001ACBpSgkTgZzxuC
0.1.12 msg_000... with 5th char a digit msg_0009fd508001UqxullkteCT2Ws

Since ASCII '0' (0x30) < 'f' (0x66), every new-format ID sorts before every old-format ID in a mixed session.

1. Server prompt loop misclassifies message order

Extracted (deobfuscated) classifier from mimo.exe 0.1.12:

function Cs(H) {
  let $ = H.assistant;
  if (H.parts.some(t => t.type==="tool" && !t.metadata?.providerExecuted && t.state.status!=="error")) return {type:"continue"};
  if (!$.finish) return {type:"continue"};
  if ($.finish==="tool-calls" && !$.error && H.lastUser.id<$.id && ...) return {type:"text-tool-call"};
  if ($.finish==="tool-calls") return {type:"continue"};
  if (H.phase==="existing-assistant" && !(H.lastUser.id < $.id)) return {type:"continue"};
  //                                  ^^^^^^^^^^^^^^^^^^^^^^^^^ string ID comparison!
  ...
  if (parts have text) return {type:"final"};   // <-- affected sessions land here
}

For old sessions: "msg_0009..." < "msg_fdc5..." is ALWAYS true (string compare), so the classifier skips the continue branch and returns {type:"final"} → loop exits without calling the model. Log evidence from a real session:

INFO service=session.prompt session.id=ses_02015043dffe466a0lhrMpNGLA step=0 loop
INFO service=session.prompt session.id=ses_02015043dffe466a0lhrMpNGLA classification=final exiting loop

2. TUI renders messages ordered by ID string → mixed sessions show new messages ABOVE old replies

Users literally see their new message inserted before already-answered content, and a new reply generated in the wrong spot.

3. TUI QUEUED badge stuck

Orphan assistant messages (no time.completed) wedge the TUI pending pointer; subsequent messages stay QUEUED even after a real reply exists. Two confirmed orphan assistant rows (zero text parts, created but never completed) were found in the affected sessions.

Scope

Real-user sessions with leftover old-format IDs after the upgrade. This machine: 28 sessions, 3692 old-format message IDs (e.g. sessions created under 0.1.10/0.1.11 with hundreds/thousands of messages — Claw remote approval 1374 msgs, Fix mimo.cmd 657, AP课程 276, etc.).

Related upstream issues (MiMo Code is an opencode fork)

  • anomalyco/opencode#13577 — same root cause: string ID comparison in loop termination (lastUser.id < lastAssistant.id)
  • anomalyco/opencode#30809 — out-of-order message IDs → wrong parent binding in prompt loop; upstream fix direction: bind completion to parentID relationship instead of ID sort order
  • anomalyco/opencode#16856 — QUEUED tag stuck on orphan assistant message (time.completed == null)
  • anomalyco/opencode#21524 — idle session not woken by prompt

The upstream #30809 fix (compare via parentID/timestamp instead of ID strings) is the correct official fix.

Full investigation log (what we tried, in order)

  1. Hypothesis: orphaned actor state. actor_registry showed thousands of rows with last_error="orphaned: process restarted", last_outcome="failure", and a few non-persistent subagent rows. Deleted 365 orphaned main actor rows → did NOT fix (symptom persisted; also wiped a lot of legitimate actor state — lesson: don't scrub the registry before confirming the code path).
  2. Hypothesis: config corruption. mimocode.jsonc had an invalid mcp.github entry (https://api.github.com/mcp, not a valid MCP endpoint) → CLI fatal ConfigInvalidError on startup. Fixed by removing the entry. Separate issue, not the root cause.
  3. Hypothesis: stale git lock blocking checkpoint-writer. D:\Unity Project\RailwayRenaissance\.git\index.lock existed (leftover from a killed git during an orphaned run). Removed it — unblocked one background checkpoint-writer, but main-session messages still didn't execute.
  4. Hypothesis: checkpoint-writer zombie looping. Observed > checkpoint-writer agent responding instead of the main agent; permissions auto-rejected headless. Not the root cause.
  5. Confirmed root cause by extracting the classifier Cs from the binary and matching the classification=final exiting loop log line, then correlating with the ID format split in the DB.
  6. Confirmed secondary effect — TUI ordering + QUEUED badge — by checking message sort order (ORDER BY id places all msg_000... before all msg_fd...) and finding orphan assistant rows missing time.completed.

What I did to unblock (data-level workaround, NOT a patch)

All on local DB copies (backups kept at ~/.mimocode_home/data/mimocode.db.bak_*):

  1. Removed invalid mcp.github config (separate startup-fatal bug).
  2. Wiped obsolete actor rows / cleared stuck states where needed (explored, partly reverted).
  3. Rewrote 3692 old-format message IDs to msg_{time:016x}{rand} in 28 real sessions, keeping part.message_id, message.data.parentID, and session.last_checkpoint_message_id in sync, so ID order == time order again.
  4. Flipped the last assistant's finish to "tool-calls" in 22 sessions so the classifier takes the continue branch (temporary band-aid; no longer needed after ID rewrite).
  5. Patched orphan assistant messages with time.completed so TUI clears the QUEUED badge.
  6. Restored one fully reset session (RailRevivalSim, 438 messages) from a pre-reset DB backup copy.

After the ID rewrite the sessions respond correctly and retain history (verified with mimo run -s <session> and in the TUI).

Suggested official fix

  1. Required: in the prompt-loop classifier (and any other message-ordering logic), compare by time.created or by parentID relationship — never by raw message.id string order (align with opencode#30809). Fix both the classification branch shown above and the loop-termination condition.
  2. Also: TUI message ordering should use time_created, not ID strings, so mixed-format sessions render chronologically.
  3. Defensive / migration: on load, detect sessions containing mixed ID formats and normalize, or at least sort by timestamp everywhere IDs are compared.
  4. TUI: make the pending/QUEUED computation resilient to orphan assistant messages without time.completed (opencode#16856).

Environment

  • MiMo Code 0.1.12 (@mimo-ai/cli), Windows 11
  • Sessions created with 0.1.10 / 0.1.11 and continued after upgrading to 0.1.12
  • DB: ~/.mimocode_home/data/mimocode.db (SQLite, WAL mode)

NMDX0721 avatar Aug 14 '26 14:08 NMDX0721

same problem

onnoink avatar Aug 17 '26 06:08 onnoink