Skip to content

fix(studio): quiet transient pinned-chat sync failures - #18087

Merged
danielhanchen merged 3 commits into
unslothai:mainfrom
bezabyte:codex/issue-18063-pin-sync-noise
Nov 17, 2026
Merged

danielhanchen merged 3 commits into
unslothai:mainfrom
bezabyte:codex/issue-18063-pin-sync-noise

Conversation

@bezabyte

@bezabyte bezabyte commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #18063.

Pinned-chat reconciliation is a background task, but transient backend/network failures were surfaced as an error toast while the app was still loading or recovering. The pins read times out after 10 s, so a slow backend showed the toast even though the pin journal retries on the next refresh.

This change:

  • treats transport errors, request timeouts, rate limits, and 5xx responses as retryable background interruptions instead of user-facing errors;
  • keeps terminal validation/client errors visible;
  • preserves the durable pin journal and existing desktop-update suppression behavior.

Verification

  • node --experimental-strip-types --test tests/pinned-chats-update-failure.test.ts tests/pinned-chats-sync.test.ts tests/pinned-chats-transport.test.ts tests/pinned-chats-journal.test.ts tests/desktop-update-refresh-toast.test.ts (103 passed)
  • git diff --check
  • Frontend npm run typecheck was run; it still reports unrelated baseline errors in attachment-content.ts and markdown-data-images.ts, with no errors in the changed files.

Before / after

The pins request is held for 12 s in the browser, so it outlasts the 10 s timeout the way a busy backend does. Before: the "Could not sync pinned chats" toast appears. After: no toast; the pin sync retries on the next refresh.

Before and after: Chat page with a slow pins request

@danielhanchen danielhanchen self-assigned this Nov 17, 2026
Drop the refresh backoff (not needed to stop the toast, and it delayed
cross-tab pin sync), reuse isTerminalSettingsRejection for the status
check, and add a test where the pins read outlasts the 10s timeout
outside a desktop update: nothing toasts and the pin saves on the next
refresh.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T06:24:51.814973Z d1552a2 Manual request
ℹ️ 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: d1552a260f

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 17, 2026
danielhanchen added a commit that referenced this pull request Nov 17, 2026
…s since #18081) (#18100)

The Studio `diffusion` CPU shard has 16 failures in
`tests/test_diffusion_backend.py` on main since #18081 (seen on the
staging run of #18087, a frontend-only PR):

```
core/inference/diffusion_lora_files.py:532: in _lora_components
    return list({id(m): m for m in roots if isinstance(m, torch.nn.Module)}.values())
E   AttributeError: module 'torch' has no attribute 'nn'
E   ValueError: Failed to apply LoRA: module 'torch' has no attribute 'nn'
```

The other 15 (`test_dense_build_cancellation_boundaries[*]`,
`test_dense_quant_lora_bake_attaches_before_quantize`,
`test_a_pipeline_pick_bakes_its_adapters_before_quantising`) are the
same error, surfacing as "The requested LoRA adapters could not be
applied".

## Cause

#18081 made `load_adapter` snapshot the `requires_grad` flags of the
pipe's `torch.nn.Module` components so unloading can restore them. That
is correct with real torch. The `fake_runtime` fixture installs a
minimal fake `torch` module with no `nn`, so every adapter load in these
tests now raises.

## Fix

The fake torch gets `nn.Module` (an empty class). The fake pipe
components are not modules, so the snapshot is empty and the tests
exercise the same paths as before. No production change.

## Verification

`tests/test_diffusion_backend.py`, `-n 4`:

- main: 21 failed (the 16 CI failures, plus 5
`test_download_plan_for_minimax_h3_image_*` cases that fail on this host
only, from its local diffusers build, and pass in CI).
- this branch: the 16 pass; only the same 5 host-only cases remain.
danielhanchen added a commit that referenced this pull request Nov 17, 2026
… -bf16 check by its new call (10 failures since #18061) (#18110)

`tests/python/test_loader_resolution_reporting.py` has 10 failures on
main in the CPU `studio` shard since #18061 (seen on the staging runs of
#18084 and #18087):

```
unsloth/models/loader.py:1555: in <module>
    if bf16_repo_loads_16bit(model_name, load_in_16bit):
E   NameError: name 'bf16_repo_loads_16bit' is not defined
test_adapter_base_resolution_updates_report_before_base_config_load[*]
E   StopIteration
```

This is the same root cause as #18096, in a second harness that test did
not cover.

## Cause

The test runs slices of the real `from_pretrained` bodies in a
hand-built scope. #18061 moved the inline
`model_name.lower().endswith("-bf16") and (...)` check into
`mapper.bf16_repo_loads_16bit`, which is correct, but:

- the scopes do not define `bf16_repo_loads_16bit`, so 8 cases die with
`NameError`;
- the adapter test found the end of its slice by the `if
model_name.lower().endswith(...)` statement in the `is_peft` block,
which no longer exists, so `next()` raises `StopIteration`.

## Fix

- `_mapper_function` lifts the real `bf16_repo_loads_16bit` out of
`mapper.py` by AST (the file deliberately avoids importing unsloth), and
each scope gets it, so the check runs for real instead of being stubbed.
- `_is_bf16_check` finds the slice end by either form (the helper call
or the old inline check).

## Verification

- main: 10 failed, 2 passed.
- this branch: 12 passed.
danielhanchen added a commit that referenced this pull request Nov 17, 2026
…imports (order-dependent m-r failure) (#18112)

`studio/backend/tests/test_research_connection_context.py::test_window_probe_reads_the_connection_under_its_config_guard`
fails on some workers in the Studio `m-r` shard on unrelated PRs
(staging runs of #18003 and #18087):

```
assert guarded and all(guarded)
E   assert ([False, False, False, False, False, False, ...] and False)
```

## Cause

The test looks up held locks in
`provider_credentials._provider_config_locks` after `from routes import
provider_credentials`. Several other test files
(`test_credential_routes.py`,
`test_provider_max_output_tokens_contract.py`,
`test_provider_model_capabilities.py`, ...) load private copies of
`routes.provider_credentials` and pop it from `sys.modules`. On a worker
where the real module had been imported before that, the `routes`
package attribute still points at the old module while `sys.modules` has
none. The probe's lazy `from routes.provider_credentials import
provider_config_guard` then imports a fresh copy and locks that copy's
table, while the test inspects the old one, so every read looks
unguarded.

The probe does take the guard; only the test was looking at the wrong
module.

## Fix

The test resolves `routes.provider_credentials` through
`importlib.import_module` (the same `sys.modules` lookup the probe's
import uses) and patches `get_provider_api_key` on the
`credential_secrets` module `core.research_runs` actually calls.

## Verification

- With a preceding test that imports `routes.provider_credentials` and
pops it from `sys.modules` (the state those files leave):
  - main: fails with the CI assertion (`[False, False, ...]`).
  - this branch: passes.
- Alone: 7 passed.
- Sabotage: dropping `async with provider_config_guard(...)` from
`_guarded_connection_client` makes the test fail on this branch, so it
still checks the guard.
@danielhanchen
danielhanchen merged commit cdf6d8e into unslothai:main Nov 17, 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.

[Bug] Weird Notice that states "Could not sync pinned chats. Try again when connected." + Laggy Interface

3 participants