Skip to content

Studio: keep every tab signed in when a refresh answer is lost - #18353

Merged
danielhanchen merged 6 commits into
mainfrom
studio-refresh-reuse-grace
Nov 20, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-refresh-reuse-grace

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Every open Unsloth Studio tab could still sign out at once when the answer to a successful session refresh never reached the browser. With this change the session survives that.

What was wrong

Refresh tokens are single-use. When /api/auth/refresh rotates a token, the server deletes the old one and returns a new pair. If that answer is lost on the way back, the browser keeps the old token, which is already gone on the server:

  • a tab is closed or reloaded while the refresh is in flight
  • a reverse proxy times out the request after the server committed
  • a phone or tablet suspends the page mid-request

The next refresh sends the spent token, gets a 401, and the client clears the tokens in shared storage. That signs out every tab on that browser.

#16468 (Web Locks) and #16755 (plain-HTTP peer wait) fixed the two-tab race reported on Discord. This lost-answer case is the one path that still ends the session.

The fix

A token rotated by /api/auth/refresh stays exchangeable for 60 seconds (REFRESH_REUSE_GRACE_SECONDS). Presenting it again in that window rotates once more and returns a fresh pair instead of a 401. This makes a retried refresh idempotent, the same idea as Supabase's refresh token reuse interval and Okta's grace period.

  • Revocation is unchanged. Logout, password changes, account revokes and renames delete the grace rows in the same transaction as the live tokens. All five call sites go through one helper, _delete_refresh_rows. A password change is also caught by the existing credential generation check.
  • Downgrade safe. The grace rows live in a new refresh_token_reuse table, and the spent token is still deleted from refresh_tokens. An older build never reads the new table, so it cannot revive a spent token.
  • Bounded. One spent token mints at most REFRESH_REUSE_MAX (8) pairs inside the window; later replays get a 401, which the client already answers from a peer's stored pair.
  • Only stamped tokens get the window. A token without a credential generation stamp (legacy rows from before that column existed) stays strictly single-use, so every reuse row is voided by any credential change, including the CLI password reset that rotates the secret with its own SQL.
  • Scope. Only the rotation path opts in. consume_refresh_token stays strictly single-use, and expired rows and rows older than the window are purged on every rotation.
  • Frontend. Without Web Locks (plain HTTP), two tabs racing on one token now both get a 200. A tab whose own 200 lands after a peer already stored a pair now reuses the peer's pair through the existing peerRotated check, as the 401 path already did, instead of handing its caller the original 401.

No new UI: the only visible change is that the tabs stay signed in. Desktop, which runs on a secure origin with Web Locks, follows the same path as before.

Tests

check merge base this PR
retry after a lost rotation answer 401 Invalid or expired refresh token 200, new pair works
two tabs racing on one token first 200, second 401 both 200
spent token after the window ends 401 401
logout, password change or CLI reset inside the window 401 401
legacy unstamped token reused 401 401
one spent token replayed 10 times in the window 401 8 pairs, then 401
tests/test_refresh_reuse_grace.py (11) 3 fail, 8 pass 11 pass
tests/auth-refresh-transient.test.ts 23 pass, 2 fail 25 pass
backend auth suites (11 files, 387 tests) 1 fail same 1 fail
full frontend node --test (16,708 tests) same 2 new tests fail 0 fail
frontend typecheck pass
  • The one backend failure on both sides, test_health_response_reports_desktop_capability_fields, already fails on main.
  • The two tests in test_auth_db_lock_contention.py that pinned "immediate reuse is a 401" now assert the 401 after the window instead.
  • Mutation checks: dropping the grace-row delete from the revoke helper fails test_logout_inside_the_window_still_ends_the_session; recording unstamped tokens fails test_a_legacy_unstamped_token_gets_no_window.
  • Staging on Linux, macOS (Apple Silicon) and Windows ran the PR's backend and node tests at the final code: 32 backend tests passed and the node refresh suite 25 pass / 0 fail on each OS.
  • Studio regression journeys (auth, settings, route crawl, full access) base vs head: no regression, 43 of 43 steps unchanged.

Before and after in Studio

Two isolated Studio installs (merge base daf25ce59 vs this branch). Two tabs are signed in; page JS posts the stored refresh token to /api/auth/refresh and drops the 200, so the server has rotated the token while the browser still holds the spent one. The access token is then invalidated and both tabs reload. Before, the spent token gets a 401 and both tabs land on the login page; after, both tabs stay signed in on the chat page.

Two tabs after a lost refresh answer, before and after

A refresh token is single-use, so when the answer to a successful rotation never
reached the browser (tab closed or reloaded mid-refresh, proxy timeout, phone
suspending the page) the browser kept the spent token, its next refresh got a 401
and every open tab was signed out. A just-rotated token now rotates again for 60
seconds; logout, password changes and account revokes still end it at once. The
window lives in its own table, so an older build never sees a spent token.

Plain-HTTP tabs racing on one token now both get a 200; a tab whose own answer
lands after a peer stored a pair reuses the peer's pair, as the 401 path did.
@danielhanchen danielhanchen self-assigned this Nov 18, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 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-20T11:16:24.360135Z 8f37565 Manual request
🔒 Security Review ✅ Completed 2026-11-18T16:04:55.996368Z df15d8b Manual request

Security findings

Finding details are still loading. Check the individual review comments.

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

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: df15d8b902

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

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…oids

An unstamped legacy refresh token skips the credential generation check, so a
CLI password reset (which rotates the secret with its own SQL) could not void its
reuse row. Only stamped tokens get the window now.

[skip ci]
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 18, 2026
Each replay inside the reuse window inserted another refresh token without
consuming anything. Cap replays per spent token at REFRESH_REUSE_MAX (8): enough
for a lost answer and a burst of racing tabs, whose extra 401s the client already
answers from the peer's stored pair.

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 84ebd8c1a2

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

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
danielhanchen added a commit that referenced this pull request Nov 19, 2026
…ng, MLX batch sizes, QAT CUDA skip) (#18471)

## Summary

Four tests fail on `main` in the "Repo tests (CPU, studio)" shard, so
every PR that runs it is red (seen on the staging runs of #18353 and
others). Reproduced on `main` at `415cdcf1be`:

```
FAILED tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe
FAILED tests/studio/test_model_picker_contracts.py::test_batch_sizes_setting_wired_end_to_end
FAILED tests/studio/test_model_picker_contracts.py::test_batch_sizes_reach_an_api_load_through_the_server_mirror
FAILED tests/studio/test_chat_ui_picker_collapsed_row.py::test_a_loaded_model_still_opens_the_picker_collapsed
```

Each was attributed to the PR that changed the code it reads, and each
was checked for whether the PR or the test is wrong.

## Root causes and changes

### Picker collapsed after a load (#18445): test was wrong

#18445 added the "Show full model list" setting, so the load effect now
calls `setExpanded(useChatRuntimeStore.getState().pickerExpandLoaded)`
instead of a literal `?? false`. The setting defaults to off, so a fresh
browser (every CI driver) still opens the picker collapsed, which is
what this test protects: the drivers' expand click is still needed.

`test_a_loaded_model_still_opens_the_picker_collapsed` now accepts
either form, and when the setting drives it, also asserts the store
default is `loadBool(PICKER_EXPAND_LOADED_KEY, false)`. Flipping that
default to `true` fails the test, which the old literal check could not
see.

### Batch sizes sent on a load (#18426): test was wrong

#18426 brought MLX up to GGUF parity, so `n_batch` / `n_ubatch` are now
sent for `(target.isGguf || mlx)` and copied for MLX overrides too. GGUF
behaviour is unchanged; the tests pinned the old GGUF-only literal.

- `test_batch_sizes_setting_wired_end_to_end`: matches the gate with a
regex that allows widening to MLX but still requires `target.isGguf` and
"only when set". Dropping the gate (sending the fields to every engine)
fails it.
- `test_batch_sizes_reach_an_api_load_through_the_server_mirror`:
replaces the source-text check on the GGUF block with a behavioural
check through `_override_load_kwargs`, which the test already imports.
Dropping `n_ubatch` from the copied keys fails it.

### QAT stream test skip guard (#18434): PR was wrong

`tests/test_qat_weight_memo_guards.py` gated
`test_cached_weight_not_served_to_another_stream` on
`torch.cuda.is_available()`, which `tests/_zoo_aggressive_cuda_spoof.py`
patches to `True` process-wide, so on a CPU runner the test un-skips and
calls `.cuda()` when it shares a session with the spoofing suites. The
existing guard caught this. The skip now gates on `has_real_cuda()`
first, as the other CUDA-only tests do.

## Testing

- The four tests plus their files: `241 passed, 1 skipped` on this
branch; the same four fail on `main`.
- Sabotage, each against this branch:
- store default `pickerExpandLoaded` set to `true`: the picker test
fails;
- `n_batch` sent without the GGUF/MLX gate: the end-to-end batch test
fails;
- `n_ubatch` dropped from the backend override copy: the server-mirror
test fails;
  - QAT skip guard reverted: the skip-guard test fails.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 8f37565914

ℹ️ 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
danielhanchen merged commit b19b59f into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the studio-refresh-reuse-grace branch November 20, 2026 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant