Skip to content

Studio chat autosave: skip the full-thread echo the client discards - #18918

Merged
danielhanchen merged 3 commits into
mainfrom
perf/r38-thread-sync-no-echo
Nov 23, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
perf/r38-thread-sync-no-echo

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

After every chat turn the autosave PUTs the whole exported thread to /api/chat/threads/{id}/messages. The route writes the rows, then sync_chat_messages re-reads the entire thread, the route builds a ChatMessage per row and serialises all of it back. The autosave caller (syncExportedRepositoryToBackend from runtime-provider.tsx) never looks at that echo; only the delete path reads it, to republish the fork boundary. On a 150-turn chat that is a ~720 KB response per turn, and with ten inline images it is ~20 MB re-read, serialised, sent and parsed per turn for nothing.

Change

  • Backend: PUT /api/chat/threads/{id}/messages takes an opt-in echoMessages query flag (camelCase alias, like allowGenerationEdit on the single-message route). Default true keeps today's response. With echoMessages=false the route passes return_rows=False to sync_chat_messages, which skips the trailing list_chat_messages re-read, and answers {"messages": []}. The write transaction, stored rows, status codes, error paths and the X-Unsloth-Chat-Revision header are unchanged.
  • sync_chat_messages(..., return_rows=True) uses the same signature and docstring as Studio: skip no-op writes and the discarded re-read when importing external sessions #18862, so the two branches merge without conflict whichever lands first.
  • Frontend: syncChatMessages / syncStoredChatMessages take echo?: boolean. syncExportedRepositoryToBackend sends echo: false only on the non-pruning autosave; any call with pruneMissing or deletedMessageIds always keeps the echo, so publishForkBoundary still receives the stored rows. With echo: false the call resolves to the records it sent, so the result is the same whether the backend honours the flag or is an older one that ignores it and echoes as before.

Results

Real routes/chat_history.py through FastAPI TestClient on a temp studio.db, 150 turns (300 rows, ~717 KB payload), each timed PUT is an autosave that resends the thread with the last reply edited. 9 timed PUTs per process after a warm-up, 3 processes per arm, medians per process (shared host, so expect a few ms of jitter):

Thread Arm PUT median (ms), 3 processes Response bytes
text only base 33.99, 26.12, 36.90 735,399
text only base A/A 33.86, 26.44, 31.01 735,399
text only head, default 31.64, 32.55, 26.41 735,399
text only head, echoMessages=false 22.56, 23.07, 22.09 15
10 inline ~2 MB images base 274.41, 272.57, 255.86 20,735,899
10 inline ~2 MB images base A/A 260.34, 239.47, 246.12 20,735,899
10 inline ~2 MB images head, default 256.43, 266.71, 250.68 20,735,899
10 inline ~2 MB images head, echoMessages=false 140.12, 134.32, 151.45 15

The text-only PUT drops from about 26 to 37 ms down to about 22 ms, and the image-heavy one from about 240 to 275 ms down to about 135 to 150 ms. Both changes are outside the A/A spread. The default response matches base byte for byte. A base backend sent echoMessages=false ignores it and echoes the full body as before (735,399 and 20,735,899 bytes). On the client, JSON.parse of the echoed body in Node's V8 takes 0.85 ms for the text-only thread and about 19 ms for the image one; with the flag that parse is now a 15-byte body. Real HTTP transfer of the 20 MB echo over a socket is not part of the numbers above and is also saved.

Tests

  • studio/backend/tests/test_chat_sync_no_echo.py: stored rows identical with and without the flag across insert, append and edit; the response is {"messages": []} and the revision header is still well formed; the default and echoMessages=true still echo; prune still deletes under the flag; the 404 and 400 paths are unchanged; sync_chat_messages(return_rows=False) skips the re-read and returns None. On base, 3 of its 5 tests fail (the flag is ignored there); all pass on head.
  • studio/frontend/tests/thread-sync-no-echo.test.ts: echo: false adds ?echoMessages=false and resolves to the sent records; an echoing older backend is still accepted; the default call and any prune or delete call send no flag and return the server rows; a failed sync still throws; the autosave sends echo: false while deleteThreadMessage keeps the echo.
  • 40 backend test files that touch chat history routes, storage, sync and revisions (3,563 tests): base and head have the same failing IDs, apart from the new test's expected base failures.
  • Full frontend npm test on base and head, plus npm run typecheck on head.

The autosave after every turn PUTs the whole thread and throws the echoed
rows away. An opt-in echoMessages=false query flag lets that caller skip
the server re-read and serialisation; deletes and every other caller keep
the echo, and an older backend that ignores the flag still works.
@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

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

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-23T04:22:16.590555Z
We couldn’t complete this review. Comment @codex review to try again.
9795e34 Draft marked ready
ℹ️ 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. 🎉

Reviewed commit: 4273806ac5

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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 9795e34ef6

ℹ️ 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 marked this pull request as ready for review November 23, 2026 04:22
@danielhanchen
danielhanchen merged commit fe0bbca into main Nov 23, 2026
@danielhanchen
danielhanchen deleted the perf/r38-thread-sync-no-echo branch November 23, 2026 04:22
danielhanchen added a commit that referenced this pull request Nov 23, 2026
…VFP4 nested kernel, Kaggle input via env) (#19027)

## Summary

Four tests are red on `main` and fail on every PR whose staging run
includes them (seen on #18918, #18925 and #19000). Each was reproduced
on `main` before fixing. In all four the code under test is right and
the test is not.

| Failing test (shard) | Introduced by | Cause | Fix |
|---|---|---|---|
|
`test_diffusion_ddp_effective_precision.py::test_real_gloo_two_ranks[fp8,*]`
(`(Python 3.13, diffusion)`) | #18967 | The verdicts are right (`SAME`,
`REFUSED ...`), but gloo prints `[Gloo] Rank N is connected to 1 peer
ranks ...` on the child's stdout first, and the test required the output
to start with the verdict. | Read the verdict from the child's last
line. |
|
`test_diffusion_base_precision.py::test_resolve_auto_uses_klein_variant_size`
and `test_auto_sizes_flux2_klein_9b_off_its_own_weights[*]` (`(Python
3.13, diffusion)`): `'nf4' == 'fp8'` | #18967 | Near the 8-bit band (20
GB free against the 18.2 GB dense 9B), Auto's fp8 pick also checks host
RAM through `_auto_swap_host_ram_ok`, because that load holds the bf16
base on the host. The tests fake the GPU but read the runner's real RAM,
so a 16 GB CI runner gets `nf4` (correctly) and a large dev box gets
`fp8`. | An autouse fixture pins host RAM high, like the file's existing
`_not_rocm` pin, since these cases simulate GPU tiers. New
`test_klein_9b_near_the_8bit_band_falls_back_to_nf4_on_a_small_host`
pins the small-host fallback explicitly. |
|
`tests/test_nvfp4_qat_backward_skip.py::test_cpu_forward_kernel_is_restored_on_exit`
(`Repo tests (CPU, rest)`): `there's already a kernel registered from
python overriding nvfp4_qat_linear_v2's behavior for CPU` | #18864 | The
test enters `_cpu_forward_kernel(op)` inside the module-scoped `op`
fixture, which has already registered that kernel. torch 2.10 refuses a
second Python registration for the same op, key and namespace, so the
test fails on every run. | The `op` fixture is per test, and the restore
test takes the bare op (new `raw_op` fixture), so it checks a real
register-then-restore from a clean state. |
|
`tests/kaggle/test_fire_and_forget.py::test_every_gate_budget_covers_the_reaper_window`
(`Repo tests (CPU, rest-2)`): `the collector's default age ceiling is
not readable from the workflow` | #18969 | #18969 correctly moved the
dispatch input out of the script into `MAX_AGE_HOURS: ${{
inputs.max_age_hours \|\| '3' }}`, and the test's regex only knew the
inline form. | Read the default from either form, and when it is an env
var, assert that variable is what reaches `--max-age-hours`. |

Only tests change.

## Testing

- The four files: all red on `main`. With this change, 98 passed / 6
skipped (the two diffusion files) and 125 passed (the NVFP4 and Kaggle
files); `tests/kaggle/` as a whole: 1081 passed.
- Sabotage, each guard still catches what it is for:
- **gloo:** making `check_effective_precision` a no-op fails the
`REFUSED` case.
- **klein:** removing the host RAM gate from `_resolve_base_precision`
fails the new small-host test. Forcing a 16 GB runner over the whole
file fails the same 5 cases CI did, plus the new one.
- **NVFP4:** keeping the test library alive instead of destroying it
fails the restore test.
- **Kaggle:** a workflow default of `9` fails (it no longer matches
`collect.DEFAULT_MAX_AGE_HOURS`), and so does not passing
`$MAX_AGE_HOURS` to `--max-age-hours` (`MAX_AGE_HOURS never reaches
--max-age-hours`).
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