Skip to content

Studio: stop the false "Could not sync pinned chats" toast during a desktop update - #16188

Merged
danielhanchen merged 8 commits into
unslothai:mainfrom
oobabooga:pin-sync-update-toast
Nov 1, 2026
Merged

danielhanchen merged 8 commits into
unslothai:mainfrom
oobabooga:pin-sync-update-toast

Conversation

@oobabooga

@oobabooga oobabooga commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Problem

During a desktop update, the "Updating backend..." screen can show a red "Could not sync pinned chats. Try again when connected." toast. The update stops the backend on purpose, so the pin sync cannot succeed until it finishes, and the error is false.

Root cause

TauriUpdateLayer draws the update screen over the app without unmounting it. The pin sync hook, usePinnedChatsSync, keeps polling the backend every 30 s and on window focus. Every failed request reaches the sync runtime's onError in sync-pinned-chats.ts, which always shows the toast.

#11224 fixed the same symptom for the model status and RAG document refreshes. It added isBackendDownForDesktopUpdate(), a flag that is set while the update screen is up, and isSilencedDesktopUpdateFailure(), which hides "could not reach the backend" errors while that flag is set. The pin sync from #15293 was added later and never used them.

That helper alone also misses pin failures that the stopped backend causes:

  • The pins API aborts every request after 10 s. A request in flight when the update starts can end in that AbortError.
  • If the backend stops after the response headers arrive, the body read fails with an untagged TypeError or the 10 s abort. For writes and legacy reads, parseJsonOrThrow turned both into null, which became a "Cannot read properties of null" TypeError.
  • Pin edits are saved one at a time in order. If the first fails, the next fails with "An earlier pin change is still waiting to save.", a plain Error.
  • The helper checks the flag when the error is reported, not when the request failed. A failed write makes a recovery read before reporting, and the edits queued behind it report after that. If the update screen drops in between, those failures toast.

Fix

  • use-pinned-chats-sync.ts: skip the poll while isBackendDownForDesktopUpdate() is true.
  • api/chat-pins-api.ts: record, when each pin request fails, whether the backend was down for the update at issue or at failure; tag a body that breaks off on a 200 as a transport failure.
  • api/chat-settings-api.ts: parseJsonOrThrow tags a broken-off body and re-throws an AbortError / TimeoutError on a 200 instead of returning null. Invalid JSON still returns null. Only the pin calls pass an abort signal, so other settings callers see no new behavior.
  • utils/pinned-chats-sync.ts: throw the queued-edit error as PinnedChatsBlockedError, with the failure it waits on as its cause.
  • utils/pinned-chats-update-failure.ts (new, lazily loaded): isPinFailureUnderDesktopUpdate() decides whether a failure came from the update.
  • sync-pinned-chats.ts: onError skips the toast when that check is true.

Behavior boundaries

  • Outside a desktop update, pin sync errors still show the toast.
  • During an update, an HTTP error the backend returned, a malformed response, or a validation error still shows the toast.
  • Pin edits made before or during the update stay queued in local storage and are saved after it.

Verification

Live A/B: a page running as the desktop webview (Tauri internals mocked) mounted the real sync hook, UpdateScreen and toaster, against a real Studio backend that was killed the way the update stops it.

Scenario main this PR
Update screen up, backend killed, 30 s poll fires (the reported case) toast no toast, no request sent
Pin read in flight when the update kills the backend, ends in the 10 s abort toast at 10.6 s no toast
Two pin edits queued when the update kills the backend toast no toast; both saved after the update
No update, backend dies (control) toast toast

Without the AbortError / TimeoutError check, the second scenario still shows the toast at 10.7 s.

New behavioral tests in pinned-chats-update-failure.test.ts run the real pins API and sync runtime with fetch stubbed. Each failure case above stays silent under the update and queued edits are saved afterwards. The same failures still show the toast outside an update, and a malformed 200 shows it even during one. The cut-off body, timeout-after-headers and late report tests fail without this change. The focused pin, settings and desktop-update tests (201), typecheck, eslint, build and the bundle size check pass.

Not tested: a real desktop update on Windows or macOS.

UI evidence (merge base vs this PR)

Both sides are real Studio installs built from each tree, opened as the desktop webview (Tauri IPC mocked: preflight attaches to that Studio, an update is offered and start_backend_update never finishes). After the update screen is up, every /api request is refused and the pin sync is woken by a focus event.

Update screen up, backend unreachable. Before: the false "Could not sync pinned chats" toast. After: no toast and no pin request sent (0 vs 5 pin requests).

Update screen, before vs after

Control, no update and backend unreachable: both sides still show the pin sync toast.

No update, backend down, before vs after

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-30T19:21:00.796924Z
We couldn’t complete this review. Comment @codex review to try again.
044bb63 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@oobabooga

Copy link
Copy Markdown
Member Author

Reached Codex review convergence at oobabooga#440.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 1, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 1, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Review record for #16188, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#657.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror 9c80635bd2 (this PR's 0352cd248)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 9c80635bd2

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 9c80635bd2 (this PR's 0352cd248).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@danielhanchen
danielhanchen merged commit b7c7cde into unslothai:main Nov 1, 2026
danielhanchen added a commit that referenced this pull request Nov 17, 2026
## Summary

Follow-up to #18087 for #18063. People still see "Could not sync pinned
chats. Try again when connected." after updating, even though nothing
was lost and the pins sync on the next refresh.

#16188 and #18087 each silenced a list of error shapes (update window,
timeouts, transport failures, 5xx). Failures outside those lists still
toast. The ones that happen right after an update or restart:

- **401 whose token refresh could not finish.** The access token expires
after 60 minutes, so it is usually stale when the app is reopened after
an update. If the refresh call meets a busy backend (it answers 503
while auth.db is busy, and the client gives up after 8 s) or a
restarting one, `authFetch` hands back the original 401. That is a
terminal 4xx to `isExpectedPinSyncInterruption`, so every 30 s poll
toasted. `isRetryablePinError` already treats the same 401 as retryable
and keeps the edit queued.
- **An edit waiting behind a trimmed bulk pin.** Pin `a, b`, pin `c`,
then unpin `a` before the first save finishes. The first entry is
replaced with a new id in its slot, and the edits behind it throw
`PinnedChatsBlockedError` with no cause. Nothing failed, every edit
saves, and it toasted twice.
- **A proxy or login page answering a pins read** (SyntaxError on a
200).

The other half is why the reads are slow in the first place. Legacy
localStorage pin ids are re-sent to `POST /api/chat/pins/migrate` on
every refresh (30 s poll plus focus, visibility and online), and the
server always took `BEGIN IMMEDIATE` with a 30 s busy timeout, even when
every id was already pinned. Right after an update the legacy chat
import and the attachment inventory work hold the studio.db writer lock,
so each refresh queued a writer transaction that outlived the 10 s
client abort and kept a threadpool worker waiting.

## Changes

- `pinned-chats-sync.ts`: `onError` now gets an `editLost` flag. It is
true only when the sync discarded a pin change: a non-retryable
rejection that `recover` drops, or a `setPinned` that could not be
queued. Background reads and retryable writes report `editLost = false`,
because the journal retries them.
- `pinned-chats-update-failure.ts`: `shouldReportPinSyncFailure(error,
editLost)` keeps the existing update-window and interruption checks and
adds the `editLost` condition. `sync-pinned-chats.ts` toasts only when
it returns true. The tests use the same function, so they exercise the
real decision.
- `studio_db.migrate_chat_pins`: the merge is computed from a read
first. When it adds nothing and the stored value is clean, it returns
without taking the writer lock. Otherwise it takes `BEGIN IMMEDIATE` and
recomputes, as before. The response is identical in both cases.

What does not change: the toast text, when pins are retried, the journal
format, and the API. A pin change the server rejects for good still
shows the toast.

## Verification

- `node --experimental-strip-types --test tests/pinned-chats-*.test.ts
tests/desktop-update-refresh-toast.test.ts
tests/settings-retry.test.ts`: 122 passed.
- New tests in `pinned-chats-update-failure.test.ts` run the real pins
API and sync runtime with `fetch` stubbed:
  - a 401 on refresh stays silent and the next refresh syncs;
  - a proxy page on a read stays silent;
- the trimmed bulk pin sequence stays silent and ends with `["b", "c"]`
saved;
  - a 400 on a pin write still toasts once (positive control).
- With the production files reverted to main and the tests kept, the
three silence tests fail and the positive control passes.
- `python -m pytest studio/backend/tests/test_pinned_chat_settings.py`:
60 passed. The new test holds the studio.db writer lock from another
connection. A repeated migrate answers right away. A migrate that adds a
pin still waits for the lock. On main the repeated migrate fails with
`database is locked`.
- `npm run typecheck`, eslint on the changed files, and ruff are clean.

Fixes #18063

## Live check (real Studio installs, base vs this PR)

Two Studio installs on the same Linux host, merge base `d9aa650` and
this branch, driven by Chromium:

| Scenario | main | this PR |
| --- | --- | --- |
| Repeat legacy pin migrate while another process holds the studio.db
writer lock for 15 s | 15.09 s (waits for the lock; the client aborts at
10 s) | 0.00 s, same answer |
| Chat page opened with an expired access token while
`/api/auth/refresh` answers 502 (backend restarting behind a tunnel or
proxy). `GET /api/chat/pins` returns a real 401 | pin toast at 1.2 s |
no pin toast in 45 s of polling the page every 0.5 s |

![Chat page with an expired token and a refresh that cannot finish: main
shows the pinned-chat sync toast, this PR does
not](https://raw.githubusercontent.com/danielhanchen/unsloth-staging-2/pr-media/pr/18139/pr18139_before_after-416d18861166.png)

The AFTER frame was taken at the end of the 45 s window. The result
comes from polling the page text, not from this one frame. "Failed to
refresh models" is a separate toast with the same 401 cause and is not
changed here.

Cross-platform staging ran the scoped tests (60 backend, 27 frontend,
new tests included) green on `ubuntu-latest`, `ubuntu-24.04-arm`,
`macos-15` and `windows-latest`. `windows-11-arm` did not reach the
tests: `pyarrow` (via `datasets`) has no win-arm64 wheel and the runner
failed to build it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants