#6661 [GUI] Host shutdown exits without draining the chat-save queue, dropping the last edit

open · @xvcezrqpbxtpzblrt · 0 Kommentare · Auf GitHub

Server & APIWindowsLinux

Beschreibung

Thanks for the report. Please read the contributing guide first and search open and closed issues before filing.

**Write only what you saw.** Where a claim below is from reading code rather than a run, it is marked as static analysis in Reproduction status.

> This report is a static source review of the official tree. It was not reproduced on a running build; Reproduction status below says so explicitly.

### Area


[GUI]  (the desktop app)

### Desktop app, Terminal or source?


Desktop app (Windows, Linux or Mac)

### What happened
On a clean quit of the desktop app, the App Host can exit while a chat save that the page has already handed to it is still sitting in its write queue. The save is cut off, so the newest edit to that chat is missing from the chat file at the next launch. The host already owns a queue-drain helper for exactly this moment, but its own shutdown path never calls it. Instead, that path destroys every connection, including an in-flight chat-save request, and exits the process immediately.

This is a finding from reading the source only. I did not run the app and did not observe the failure.

### Smallest steps or command that reproduce it
Not reproduced locally. To verify the hypothesis:

1. In the desktop app, let a reply stream into a large chat, so scheduled saves are several seconds apart.
2. Quit the app from the tray before the next scheduled save, or close the window before the page's last-moment copy is written.
3. Relaunch and open the same chat; check whether the last edit is in the chat file.
4. Instrument the host to report whether pending saves existed when shutdown reached the final process exit.

### Reproduction status
Unconfirmed static analysis (I read code or ran a scanner; I did not see it fail)
### Strata version

0.2.19 · engine 0.1.65 (source tree under review, static analysis; not a running-build query)

### Graphics card, memory and system
Not applicable; no local run was performed.

### Model, size and settings
Not applicable; the affected path is chat persistence, not inference.

### Logs
No log was produced; no local run was performed.

### Why every desktop user benefits from a fix
Every desktop session ends through this same host shutdown path, and every chat reaches disk through the same save queue. Saves are deliberately spread out: the source states that a chat of about 330 KB or more is written every five seconds during a streamed reply. That means a pending save is the normal state at quit time, not an edge case. The page has a last-moment fallback, but it only runs when the page is going away; quitting from the tray does not fire it, and the fallback lives in per-port browser storage, which is exactly what the move to host-side chat files was meant to remove. Draining the host's own queue at shutdown makes chat durability independent of the page, for everyone.

### Source evidence (verbatim; indentation flattened to two spaces for Notepad)
app/host/server/chat-store.mjs lines 66-68. Saves are chained, and this chain is the only guarantee that an accepted save reached disk:

  // A chain, so saves of one host never overlap: the file, the index and the conflicts folder change one save at a time.
  let chain = Promise.resolve();
  const queue = work => { const run = chain.then(work, work); chain = run.catch(() => {}); return run; };

app/host/server/chat-store.mjs lines 201-202. The drain helper exists and explicitly names shutdown as its purpose:

  /** Wait until every save queued so far is on disk (tests, shutdown). */
  idle: () => chain,

A search of the whole source tree finds no call to this idle helper. The only other idle call in the host is monitor.idle at app/host/server/index.mjs line 771, which is unrelated to chat storage.

app/host/server/index.mjs lines 2438-2446. The shutdown path never awaits the drain, closes all connections, and exits:

  let stopping=false;
  const shutdown = async () => {
    if(stopping)return;stopping=true;
    clearInterval(taskTimer);tasks.close();
    const timeout=setTimeout(()=>process.exit(0),control?45000:10000);timeout.unref();   // stopping an engine through Control: drained (15 s), then ended; the shell's deadline is longer
    server.close();server.closeAllConnections?.();
    await Promise.allSettled([localShare.stop(),cloudflare.stop(),native.close(),agent.close()]);
    process.exit(0);
  };

app/host/server/index.mjs lines 1417-1422. That shutdown function is the ordinary quit path requested by the shell:

  // The shell asks for a clean stop (engines this app started are stopped through Control) before it ends the process.
  if (path === '/api/control/shutdown' && method === 'POST') {
    if (!fromShell()) return error(res, 403, 'Not allowed.');
    json(res, 202, { stopping: true });
    setImmediate(() => void shutdown());
    return;
  }

app/desktop/src-tauri/src/main.rs lines 604-605 and 608. The shell sends that request on each normal window close or quit:

  /// A clean stop first: the host stops, through Control, the engines this app started, then exits. Engines another client
  /// started (the Terminal) are not touched. If it does not finish in time the process is ended.
  let _ = http_post(&format!("http://127.0.0.1:{port}/api/control/shutdown"), &[("x-strata-shutdown", token)]);

app/host/src/lib/session-persist.ts lines 270-274. The write is intentionally delayed, so a pending save at quit is expected:

  /**
   * #5923: each save rewrites a changed chat whole, so a reply streamed into a 1 MB chat wrote about 1 MB a second, growing with the chat. The
   * wait before a scheduled save grows with the size of the chats that changed (their last saved size): a small chat still saves within a
   * second; a chat of about 330 KB or more every 5 s (at most 5 s of a streaming reply to lose if the app crashes; a turn's end saves at once).
   */

app/host/src/lib/store.ts lines 1389-1399. The page-side fallback exists but depends on the page going away:

  /** The page is going away: send what can still be sent, and keep a last-moment copy of what the host has not confirmed (taken back at the next start). */
  function leavingPage() {
    void sessionWriter.flush();
    const unconfirmed = sessionWriter.notOnHost();
    if (unconfirmed.length) keepLastMomentCopy(browserStorage, unconfirmed);
  }

  if (typeof window !== 'undefined' && typeof document !== 'undefined' && window.addEventListener) {
    window.addEventListener('pagehide', leavingPage);
    document.addEventListener?.('visibilitychange', () => { if (document.visibilityState === 'hidden') leavingPage(); });
  }

### Suggested improvement
In shutdown, stop accepting new work, then await the host's own drains, including chatStore.idle, before closing all connections and calling the final process exit. That makes the existing drain helper serve the purpose its own comment documents, and removes the dependence on a page-side fallback that a tray quit never triggers.

All file paths above are relative to the repository root. No credentials, tokens or private paths are included in this report.

### Duplicate search

Searched open+closed issues for `chat save shutdown`, `chatStore idle`, `chat not saved quit`, `lose last edit`. Closest, none a duplicate: #5923 (closed, the delayed-save design that creates the pending-save window — a performance fix, not the shutdown drain). No existing report covers the host never awaiting `chatStore.idle()` on the clean-quit path.

Mehr auf der Site

Links zu Install, Modellen, Releases.