Skip to content

feat(studio): run chats in parallel in the Chat tab - #7455

Merged
danielhanchen merged 64 commits into
mainfrom
feat/parallel-chats
Jul 28, 2026
Merged

danielhanchen merged 64 commits into
mainfrom
feat/parallel-chats

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Jul 26, 2026 •

Copy link
Copy Markdown
Member

Clicking New Chat used to cancel whatever the current conversation was generating. This makes the Chat tab behave like the rest of Studio: leaving a conversation lets it keep going, the sidebar shows which chats are still running, and Stop is per conversation.

unsloth run already serves with --parallel 4, but plain unsloth studio launched llama-server with a single decode slot, so the admission queue serialised every chat regardless of what the UI did. Both entry points now default to the same slot count.

Behaviour

  • New Chat leaves the previous conversation generating. Its runtime stays mounted, it keeps streaming into its own thread, and its sidebar row spins.
  • Every row in Recents gets its own spinner. Off the Chat route the nav item spins and reads "Return to N chats", the same way Train and Export already work.
  • Stop, tool calls, uploads, nudging and self healing are isolated per conversation. Stopping one closes only that upstream stream. Siblings keep decoding and llama-server is never signalled.
  • Changing the model, or any setting that needs Apply, still ends every running chat, because they all decode on one llama-server. The UI now says so first and names the chats, and the backend refuses the swap with 409 unless the caller opts in.

Backend

  • New studio/backend/state/active_generations.py: a registry of in-flight generations. _TrackedCancel registers into it, so every streaming path is covered without its own bookkeeping. Entries hold the same threading.Event as the existing per-run cancel registry, so cancelling reuses the per-request path instead of inventing a second teardown.
  • /load and /unload gate on it. The load path checks twice, the second time under inference_lifecycle_gate(), since a chat can start while the request queues. force_cancel_active on the request cancels instead of raising. The frontend guard is bypassable from a second tab or curl; this one is not.
  • New GET /api/inference/active-generations returns the running conversations plus the slot count actually in use, so a model swap can name the chats it would interrupt, including runs the current tab cannot see.
  • _PARALLEL_DEFAULT_PLAIN goes 1 to 4 in studio/backend/run.py and unsloth_cli/commands/studio.py, with a test pinning the two together. Loads that do not fit in VRAM still fall back to fewer slots.

Frontend

  • New Chat and thread switches stop the prompt queue without cancelling the prompt already dispatched.
  • A per-run server cancel handle, so Stop reaches a conversation generating off screen. cancelByThreadId only ever holds the visible thread's cancelRun(), which is not enough once background chats exist (deleting one, or a confirmed reload).
  • Tool output store keys are scoped per conversation. Local GGUF names every tool call call_0, so two chats mid tool call shared one store entry.
  • The composer's "Running Python: ..." badge is scoped per conversation too. It read one global string, so a chat mid tool call put its badge above every other composer, including a brand new empty chat. Its elapsed counter also restarted on every thread switch, and a run ending anywhere cleared the badge everywhere. The status is now keyed by thread and carries the moment it started, so the counter resumes instead of restarting.
  • Confirmation dialog before a model swap, listing the chats it would stop, with "Keep generating" and "Stop and reload".

Tool approvals

Three problems surfaced by running several chats at once, all from the approval prompt behaving as though only one chat could ever exist.

  • A gated call did not stream its arguments, so the chat stayed blank for as long as the model took to write the payload, which for a large file is minutes. Nothing runs before the decision either way, and the code is what is being approved, so python and terminal now stream their card while gated. render_html stays suppressed, since its card renders the payload.
  • The status read "Running ..." with a climbing timer while the call had not started. It now says it is waiting for approval, then switches to running once allowed.
  • The admission lease was held across the wait, so four unanswered prompts held all four decode slots and no other chat could start while llama-server sat idle. A parked run keeps its lease but no longer counts against capacity.

It also fixes a rendering bug that parallel chats surfaced: assistant-ui keys message parts by index, so switching conversations hands the same MarkdownText instance a different message, and Streamdown keeps its parsed blocks in internal state and only extends them while streaming. A conversation could show the previously viewed one's answer until the page was reloaded. Streamdown is now keyed per message.

Tool sandboxes were already per conversation and stay that way: the session id a chat sends is its thread id, so _get_workdir puts each one in its own ~/studio_sandbox/<thread id> at mode 0700. Chats inside a project deliberately share the project workspace instead.

Tool cards

Streaming a gated call's arguments exposed three more problems in the card itself.

  • The card was rendered twice. The provisional card that streams the arguments is keyed by the backend tool call id, but the tool_start that opens the approval prompt was keyed by the approval id, so it opened a second card. Only the second one ever received tool_end, leaving the first spinning "Running" for the rest of the conversation. The approval prompt now reuses the card already on screen.
  • The terminal card had no body at all, only a 60-character trigger label, so a long heredoc showed a truncated first line and nothing else for as long as it took to write. It now renders the command the way the Python card renders its script.
  • Neither cell is capped any more. The Python script used to stop at 10k characters with a "... (truncated)" marker. Both render in full; above 20k characters the block stays plain monospace rather than paying for shiki, and it stays plain while the model is still writing so a long payload does not re-tokenize on every fragment.

Both cells moved inside the collapsible, so one chevron now hides the code together with the output, and Copy / Download exist only while the card is open. A card parked on the prompt says so rather than counting up "Running", which the composer badge already did.

Verification

Playwright against unsloth/gemma-4-E2B-it-GGUF:UD-Q4_K_XL on one GPU:

  • Four conversations streaming at once, backend reporting count: 4, four sidebar spinners.
  • New Chat issued no /api/inference/cancel. Chat 1 had 3062 characters when it was left and 16225 on return, still streaming.
  • Stop on chat 1 took the active count from 4 to 3 with the other three still streaming, and the llama-server PID was 355324 before and after.
  • Reload guard: /load and /unload return 409 naming the running thread, the chat keeps streaming through the refusal, and the forced load then ends it.
  • Two concurrent tool-calling chats end with the right answer in each, live, after switching between them and after a reload, 3 runs out of 3.
  • Tool badge: shown in the chat that owns the run, absent in a new empty chat opened next to it, and still counting on return (1s before leaving, 12s after).
  • Sandboxes: four concurrent tool calls landed in four directories named after their threads, and none could see a sibling's file. Repeated through the UI with two conversations, reading the paths out of the tools' own stdout.
  • Approvals: with four prompts left open, every gated call streamed its code, none reported "Running", and a fresh chat answered in 0.4s. Before the change the same request waited 290s and never answered while the GPU was idle.
  • Tool cards, through the UI on a gated terminal call and a gated python call: one card per call rather than two, the whole command and script visible with their tail sentinel, the body reading "Waiting for approval" and never "Running", Copy and Download present while open and gone when collapsed and back on reopen, nothing left spinning at the end, and no "(truncated)" marker anywhere in the transcript. 8 of 8 checks on both tools.

Tests:

  • studio/backend/tests/test_active_generations.py, 22 cases: registry lifecycle, overlapping runs on one conversation, JSON safety of the snapshot, per-thread against global cancel, an 8 thread concurrency stress, the 409 and force paths, and the parallel default agreement between the CLI and the backend.
  • test_gated_python_call_still_streams_its_arguments and two admission-queue cases covering a parked slot being freed and unpark never leaking capacity.
  • studio/backend/tests/test_tool_sandbox_per_thread.py, 11 cases: a directory per conversation, stable across turns, private to its own chat, the project workspace exception, session ids that try to escape the sandbox root, and the 0700 mode. HOME is redirected to a tmp dir so nothing touches the real sandbox tree.
  • Full studio backend suite passes, with the same pre-existing failures as main (test_studio_api.py wants a live keyed server, test_training_worker_flash_attn.py wants network installs).
  • The registry tests also run green on ubuntu-latest, windows-latest and macos-14. Nothing added here is POSIX specific: it is a dict plus a threading.Lock, and cancelling is an HTTP connection close.

Loading
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.

1 participant