Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Compare leaves a /chat?new=<uuid> chat detached unless its thread id is re-attached on return."""
from __future__ import annotations
import re
import textwrap
from _node_harness import (
WORKDIR,
read,
require_node,
run_harness,
slice_between,
source_path,
)
PROVIDER = source_path("studio/frontend/src/features/chat/runtime-provider.tsx")
TEMP = WORKDIR / "temp" / "nonce_chat_resume_restore"
SOURCES = (PROVIDER,)
def _restore_effect() -> str:
"""The component's single ``useEffect`` body, verbatim."""
component = slice_between(
read(PROVIDER),
"function NonceThreadResumeRestore(",
"\nconst THREAD_READ_RETRY_MS =",
)
match = re.search(r"useEffect\(\(\) => \{\n(.*?)\n \}, \[([^\]]*)\]\);", component, re.S)
assert match, "NonceThreadResumeRestore effect not found"
deps = [name.strip() for name in match.group(2).split(",") if name.strip()]
assert deps == ["aui", "enabled", "mainThreadId"], f"emulator does not bind {deps}"
return match.group(1)
def _harness_source() -> str:
return f"""
export const state = {{ activeThreadId: null }};
export const sets = [];
const useChatRuntimeStore = {{
getState: () => ({{
...state,
setActiveThreadId: (id) => {{
state.activeThreadId = id;
sets.push(id);
}},
}}),
}};
// The real predicate: assistant-ui mints local ids for a thread with no row behind it yet.
const isAssistantLocalThreadId = (id) =>
typeof id === "string" && id.startsWith("__LOCALID_");
// Which threads have been persisted. A thread NOT listed here is a placeholder the
// runtime minted and nothing was ever written for -- what an untouched project landing
// holds after a compare round trip. Keyed by thread id, exactly as getItemById is.
export const remoteIds = {{}};
const aui = {{
threads: () => ({{
__internal_getAssistantRuntime: () => ({{
threads: {{
getItemById: (id) => ({{
getState: () => ({{ remoteId: remoteIds[id] }}),
}}),
}},
}}),
}}),
}};
// One mounted component: a ref that survives renders, and an effect re-run only when a
// dependency actually changed.
export function mount(initialEnabled) {{
const wasEnabledRef = {{ current: initialEnabled }};
let last = null;
return function render(enabled, mainThreadId) {{
const deps = [aui, enabled, mainThreadId];
if (last !== null && last[1] === deps[1] && last[2] === deps[2]) return;
last = deps;
(() => {{
{_restore_effect()}
}})();
}};
}}
export function report(extra) {{
console.log(JSON.stringify({{
activeThreadId: state.activeThreadId,
sets,
...extra,
}}));
}}
"""
def _run(body: str) -> dict:
require_node(SOURCES)
script = textwrap.dedent(
f"""
// @ts-nocheck
import {{ mount, report, state, sets, remoteIds }} from "./harness.ts";
{textwrap.dedent(body)}
"""
)
return run_harness(TEMP, _harness_source(), script, sources = SOURCES)
def test_returning_from_compare_reattaches_the_materialized_thread() -> None:
"""The bug, end to end: hide the view, clear the id as Compare does, show it again."""
out = _run(
"""
remoteIds["thread-42"] = "remote-42"; // materialized: a row exists for it
const render = mount(true);
render(true, "thread-42"); // the chat, materialized and attached
state.activeThreadId = "thread-42";
render(false, "thread-42"); // Compare opens: hidden, and it clears the id
state.activeThreadId = null;
sets.length = 0;
render(true, "thread-42"); // back again
report({});
"""
)
assert (
out["activeThreadId"] == "thread-42"
), "a chat that comes back visible must be attached to the thread it is showing"
assert out["sets"] == ["thread-42"], "exactly one restore, on the resume"
def test_a_resume_never_overwrites_an_id_somebody_else_already_set() -> None:
"""The restore only fills a hole. Racing whoever owns the id would be worse than the gap."""
out = _run(
"""
const render = mount(true);
render(false, "thread-42");
state.activeThreadId = "thread-99";
sets.length = 0;
render(true, "thread-42");
report({});
"""
)
assert out["sets"] == []
assert out["activeThreadId"] == "thread-99"
def test_a_fresh_new_chat_is_not_attached_on_its_first_render() -> None:
"""Mounting is not resuming. An empty New Chat has no thread yet, and publishing one
would price and pair a chat the user has not started."""
out = _run(
"""
const render = mount(true);
render(true, "thread-42");
report({});
"""
)
assert out["sets"] == []
assert out["activeThreadId"] is None
def test_a_materialized_chat_keeping_its_runtime_id_is_still_restored() -> None:
"""A materialized chat keeps its __LOCALID_ id after initialize() persists it, so no id-prefix check."""
out = _run(
"""
// initialize() wrote the row under the minted id and handed it back, so this
// thread IS persisted despite its shape. That is the whole point of the test.
remoteIds["__LOCALID_7"] = "__LOCALID_7";
const render = mount(true);
render(false, "__LOCALID_7");
state.activeThreadId = null;
sets.length = 0;
render(true, "__LOCALID_7");
report({});
"""
)
assert out["sets"] == ["__LOCALID_7"]
assert out["activeThreadId"] == "__LOCALID_7"
def test_a_view_with_no_thread_at_all_publishes_nothing() -> None:
"""Nothing to attach to. `null` is already the state, and writing it again would
clear the contextUsage the store keys off it."""
out = _run(
"""
const render = mount(true);
render(false, null);
sets.length = 0;
render(true, null);
report({});
"""
)
assert out["sets"] == []
assert out["activeThreadId"] is None
def test_staying_hidden_never_restores() -> None:
"""A backgrounded provider must not write the active thread the visible view owns."""
out = _run(
"""
const render = mount(true);
render(false, "thread-42");
state.activeThreadId = null;
sets.length = 0;
render(false, "thread-7");
report({});
"""
)
assert out["sets"] == []
def test_the_provider_gates_the_restore_on_a_nonce_view_that_is_visible() -> None:
"""The emulator cannot see JSX props, so the enable expression is pinned here instead."""
jsx = slice_between(
read(PROVIDER),
"<NonceThreadResumeRestore",
"<ThreadScopedSettingsSync",
)
for clause in (
'modelType === "base"',
"!pairId",
"!!newThreadNonce",
"!initialThreadId",
"!backgrounded",
):
assert clause in jsx, f"NonceThreadResumeRestore must be gated on {clause}"
def test_the_harness_binds_every_name_the_effect_reaches() -> None:
"""The effect is replayed verbatim, so a name this harness does not define becomes a
ReferenceError -- which reads as "the restore never fired", a bug that is not there."""
body = _restore_effect()
for name in re.findall(r"\b([A-Za-z_$][\w$]*)\s*\(", body):
if name in {"if", "return", "for", "while", "switch", "catch"}:
continue
assert name in _harness_source(), f"the effect calls {name}(), which the harness omits"
def test_an_untouched_landing_is_not_read_into_a_chat_on_resume() -> None:
"""Blank placeholders share the __LOCALID_ shape of a sent chat; only a missing row tells them apart."""
out = _run(
"""
const render = mount(true);
render(false, "__LOCALID_9"); // hidden by Compare, nothing ever written for it
state.activeThreadId = null;
sets.length = 0;
render(true, "__LOCALID_9"); // back to the landing
report({});
"""
)
assert out["sets"] == [], "an unpersisted placeholder must not be published"
assert out["activeThreadId"] is None, "so the landing stays a landing"