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

[Bug] Huge session /message HTTP 500; unsafe part truncate caused blank ThreadView; schema_safe DB fix

Open NMDX0721 opened this issue 2 months ago • 0 comments

[Bug] Huge session /message returns HTTP 500; bad truncation caused blank ThreadView; schema_safe DB repair fixed load

English title (for labels/search):
GET /session/{id}/message HTTP 500 on ~845MB session parts; frontend white-screen after aggressive part truncation; schema-safe attachment URL shortening resolved open-loop.


Summary

On MiMo Desktop (domestic) 26.914.142245 / Windows 11 x64, opening the long-running session 「校内2」 (ses_01f0db20cffeWgFFD05BByQuE3) never finished loading. Logs showed repeated:

GET /session/ses_01f0db20cffeWgFFD05BByQuE3/message -> HTTP 500

Root cause: the session (and its fork) stored ~845MB of part.data, almost entirely from read tool results embedding images in state.attachments (~16–18MB each) plus large type=file parts (base64 url). The engine serialized the whole history in one response and failed.

A second, related product issue: after an unsafe local truncation (replacing attachments with {_truncated:…}), the UI showed 「loads then goes blank」 even when /message returned 200. A schema-safe truncation (keep type/mime/url/id/sessionID/messageID, only shorten data: URLs) restored normal open.


Environment

Item Value
App MiMo Desktop (国内版)
Version 26.914.142245 (also observed 26.909.91205 pre-update)
OS Windows 11 / Windows_NT 10.0.26300 x64
RAM 32GB
Data dir %USERPROFILE%\.mimocode_home\data\mimocode.db
Engine logs %USERPROFILE%\.mimocode_home\data\log\
Desktop logs %APPDATA%\Xiaomi MiMo\logs\

Session IDs

Purpose ID
校内2 (primary) ses_01f0db20cffeWgFFD05BByQuE3
校内 (fork #1) ses_ffe5f466da181ffeOcUnsWua3A
Other 500s e.g. ses_-ffe5f465f8e4fffe2pMUxiorx, ses_-ffe5f465f83bcffec1X009fT7

Expected vs actual

Expected

  1. Opening any historical conversation loads messages (or shows a clear error).
  2. Session compact/summarize reduces stored part payload and sets time_compacting.
  3. history_fts migrations keep table schema in sync with code (kind column).
  4. If /message is huge, paginate/stream or cap tool output; never silent 500 + infinite spinner.
  5. If messages load (HTTP 200) but render fails, show error/empty placeholder instead of pure blank.

Actual

  1. 校内2 / fork / siblings: /message → HTTP 500 (1.5–9.7s), while /task, /recovery → 200 → infinite loading.
  2. Built-in compact on fork1 completed (session.compacted) but compaction prefix snapshot missing, time_compacting stayed NULL, part bytes unchanged (~845MB); DB grew ~3GB → ~6.5GB.
  3. Every engine boot:
    history.backfill ... SQLiteError: table history_fts has no column named kind
    and history.writer write failed (same error).
  4. After wrong local repair: HTTP 200 + ThreadView blank (white-screen scrollHeight: 4900 → 859).
  5. After schema_safe repair: live DB ~802MB, 校内2 /message 200 (~136ms), no further 500s in 2026-09-20 logs.

Reproduction (original bug)

  1. Open MiMo Desktop 26.914.142245 on Windows.
  2. Open conversation 校内2.
  3. Observe perpetual loading.
  4. Check %APPDATA%\Xiaomi MiMo\logs\*.log → repeated
    GET /session/ses_01f0db20.../message -> HTTP 500.

(With the schema_safe DB installed, step 3 succeeds — keep a 6.5GB sample backup if you need to repro on stock data.)


Full timeline of our attempts

# Action Result
1 Diagnosed logs Only /message 500; meta endpoints 200
2 Read-only SQLite 校内2: 2842 msgs / 9582 parts / ~886MB part bytes; 79 parts >1MB; whole DB ~3GB then ~6.1GB after compact
3 Part type breakdown tool ~792MB (almost all read); file ~50MB; text/reasoning negligible
4 Structure probe Huge bytes were in state.attachments (list, ~17MB) not state.output (output was short string "Image read successfully")
5 Built-in compact on fork1 summarize OK (~175s) + session.compacted, but prefix snapshot missing, no size drop, still 500
6 Engine history_fts Columns: part_id, session_id, message_id, project_id, tool_name, body, time_created — missing kind
7 Full DB backup (never touched original until swap) SQLite online backup + SHA256 under session workspace db_backup\
8 Unsafe repair #1 (work copy) Truncated only state.output → no size change
9 Unsafe repair #2 Replaced state.attachments / file parts with {_truncated} objects → DB shrank (845MB→17MB/session) but attachments lost url
10 First replace-db.ps1 Partial: process check OK; Python -c quoting SyntaxError on backup/verify; move+install still ran (PS native errors didn’t stop script). Rollback name in script never created.
11 Observed UI after unsafe install /message 200 but blank timeline; white-screen scrollHeight 4900→859; loadEngineSessions reconcile racing ThreadView height clear
12 Frontend analysis (from app.asar) streaming || finished || msgs.length>0 → xe="content" without empty placeholder; Lve(old, []) can wipe store; loadConvoMessages has silent catch {} / early return on race
13 API proof Engine GET /session/{id}/message 200, ~3.7MB, 73–74 items; transform keeps 73/73; not an empty API
14 Schema-safe repair Keep attachment keys type/mime/url/id/sessionID/messageID; only shorten data: URL; add history_fts.kind; VACUUM
15 Install schema_safe Live DB ~802MB, quick_check=ok, 校内2 part ~15MB, msgs 2851 intact
16 Verify 2026-09-20 logs: 校内2 /message 200 (~136ms); recent 500s only from 2026-09-19 22:31 (bad truncation era)

Do not use for install: mimocode.db.current_839mb, mimocode.db.current_20260919_222019 (broken attachment shape).
Rollback large DB (if needed): E:\NMDX\NMDX-User Workspace\mimocode.db.original_20260919_205057 (~6.55GB) — note: restores 500 risk.


Evidence (logs)

# Infinite load
engineFetch GET /session/ses_01f0db20cffeWgFFD05BByQuE3/message -> HTTP 500 elapsed=3401ms
engineFetch GET /session/ses_ffe5f466da181ffeOcUnsWua3A/message -> HTTP 500 elapsed=4471ms

# history_fts migration broken
WARN service=history.backfill session=ses_01f0db20... cause=SQLiteError: table history_fts has no column named kind
WARN service=history.writer cause=SQLiteError: table history_fts has no column named kind write failed

# compact ineffective
INFO service=session.checkpoint ... deltaApproxBytes=13063663
WARN compaction prefix snapshot missing

# After schema_safe install (success)
engineFetch GET /session/ses_01f0db20cffeWgFFD05BByQuE3/message -> HTTP 200 elapsed=136ms

White-screen pattern (after unsafe truncate / session switch):

white-screen-up scrollHeight: 4900 → 859
GET .../message -> HTTP 200
# xe=content but msgs effectively empty / height cleared → pure blank

Data mass (before repair)

Metric 校内2 fork #1
messages 2842–2851 2838
parts 9582+ 9563
part bytes ~845–886 MB ~845 MB
largest single part ~17.9 MB ~17.9 MB
tool parts >100KB 230 ~209 oversized tools

Largest parts were type=tool tool=read with path like ...\Downloads\XiaoAi\相机\MVIMG_*.jpg and bulk in state.attachments.


Related issues (product)

A. /message payload / 500

  • One-shot JSON of full history with embedded media is not bounded.
  • Need: pagination, cursor, strip/externalize attachments[].url (file refs), or hard cap per response.

B. Compact does not reclaim space

  • session.compacted + missing prefix snapshot + time_compacting still NULL.
  • Users cannot recover from oversized sessions via official compact.

C. history_fts schema drift

  • Code expects kind; table has no kind → every backfill/writer fails.
  • Blocks search/history index; noisy WARN on every boot.

D. Frontend blank timeline (independent of DB health)

  • ThreadView: when status is content but msgs=[] (or height reset to viewport 859), no empty state → looks like data loss.
  • loadConvoMessages: silent failure / race does not write store; Lve(prev, []) can clear messages.
  • loadEngineSessions reconcile (~30s) can clear height in same tick as successful /message.

E. Compaction / share side noise

  • share-next ... status=503 failed to sync share (secondary).
  • Fork from huge session: fork failed ... aborted due to timeout.

Suggested fixes (priority)

P0 – Engine

  1. Bound /session/{id}/message (page/stream/limit; never unbounded 500).
  2. Fix history_fts migration: add kind (or align code to schema) and re-run backfill cleanly.
  3. Compact must: update time_compacting, persist prefix snapshot, delete or externalize oversized state.attachments / file payloads; fail loudly if not.

P0 – Desktop UI 4. If xe==="content" and msgs.length===0, show loading/error/empty — never blank. 5. loadConvoMessages: no silent catch {}; do not apply empty payload over non-empty store without explicit clear reason; surface fetch errors. 6. Don’t clear scroll height on session reconcile when same convo is active.

P1 7. When ingesting read on binary/image, store file reference not full base64 in part forever (or GC old attachments). 8. Share sync 503 / fork timeout: clear user-visible errors.


What fixed it for us (local workaround, not a product feature)

  1. Backup original DB (SQLite online backup + copies on E:\NMDX\NMDX-User Workspace\).
  2. schema_safe truncation: for parts >64KB:
    • state.attachments[]: keep type, mime, url, id, sessionID, messageID; shorten url if data: and huge.
    • type=file parts: same key preservation; shorten url.
    • Optionally add history_fts.kind.
    • VACUUM.
  3. Fully quit Desktop → install repaired file as mimocode.db → restart.
  4. Verify /message 200 and attachment objects still have url.

Never install truncation that replaces whole attachments with a non-schema object — that causes blank UI even with HTTP 200.


Acceptance criteria for fix

  • [ ] Session with ≥800MB part bytes loads (or pages) without HTTP 500.
  • [ ] Compact reduces SUM(LENGTH(part.data)) and sets time_compacting.
  • [ ] No history_fts has no column named kind on startup after upgrade.
  • [ ] /message 200 + empty store never renders pure white; shows placeholder or error.
  • [ ] Image/file attachments still render when present (url intact).

Labels (suggested)

bug desktop engine history performance ui


Contact / attachments

Local evidence retained by reporter: Desktop + engine logs for 2026-09-19/20, SQLite backups, export markdown of affected chats. Can attach redacted log excerpts on request.


Follow-up (2026-09-20): post-repair "network error" / AI_DownloadError

After installing the schema_safe (~802MB) DB to fix /message HTTP 500, old conversations failed on the next turn with UI 网络错误 / reconnecting, even though outbound HTTPS was fine (GitHub 200, model host probe 200).

Root cause

Aggressive local truncation left undownloadable image placeholders in part JSON url / attachments[].url fields, e.g.:

  • Invalid/truncated 1x1 PNG data URLs
  • data:image/png;base64,TRUNCATED / data:image/jpeg;base64,TRUNCATED_BY_REPAIR
  • Empty/xxx/data:image/...;base64,... placeholders

On each prompt, engine convertToLanguageModelPrompt → downloadAssets → download() does fetch() on those URLs → AI_DownloadError: Failed to download data:image/...: TypeError: fetch failed (hundreds of times). UI surfaces this as network failure / reconnect loop.

ERROR service=llm ... error={"error":{"name":"AI_DownloadError","url":"data:image/png;base64,TRUNCATED_BY_REPAIR"}} stream error
ERROR service=session.processor ... Failed to download data:image/png;base64,TRUNCATED_BY_REPAIR: TypeError: fetch failed
    at download (.../app.asar/out/main/node.mjs)
    at downloadAssets
    at convertToLanguageModelPrompt
    at streamStep

Local mitigation (Desktop left running)

  1. SQLite online backup while Desktop stayed open: mimocode.db.pre_fix_broken_img_20260920_124537 (~908MB, quick_check=ok).
  2. Replaced broken url / attachment URLs with a valid 1x1 PNG data URL (decoded \x89PNG header verified).
  3. Did not rewrite free-text in message bodies that merely mentioned those strings.
  4. Post-check: 0 bad tokens left in url fields; sample PNGs 100/100 decodable; quick_check=ok.
  5. Last AI_DownloadError burst was before this URL fix; no restart required.

Why write without quitting Desktop: history is SQLite with WAL + busy_timeout. Readers (UI/engine) and one writer can coexist; we opened a connection with a long busy timeout, UPDATE + COMMIT. Swapping the main DB file still requires the process closed — in-place row updates do not.

Expected product fixes (add to P0)

  1. downloadAssets must tolerate bad data: URLs: skip undecodable/placeholder images instead of failing the whole streamStep (or replace with a text stub). One broken history image must not abort the turn as "network error".
  2. Repair/compaction tools that write placeholder URLs must keep them fetchable (valid PNG) or strip them from the prompt pipeline.
  3. Surface AI_DownloadError distinctly from true network connectivity errors in the Desktop UI.

Acceptance (updated)

  • [ ] Old session with placeholder/invalid data:image history can send/receive without AI_DownloadError or UI 网络错误
  • [ ] Broken image URL is skipped or substituted; turn continues
  • [ ] Still: /message no 500; compact reclaims bytes; history_fts.kind present; ThreadView never blank on 200+empty store

NMDX0721 avatar Sep 20 '26 04:09 NMDX0721