Skip to content

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.

Studio: show status between tool rounds and while context compaction runs (#16608)

oobaboogaf2d72cbffe63
diff --git a/studio/backend/core/inference/checkpoint.py b/studio/backend/core/inference/checkpoint.py
index 637e100ad72..5f43cc53327 100644
--- a/studio/backend/core/inference/checkpoint.py
+++ b/studio/backend/core/inference/checkpoint.py
@@ -388,8 +388,10 @@ def fit_checkpoint_context(
force_reset: bool = False,
sticky_manual: bool = False,
replay_only: bool = False,
+ on_evict: Optional[Callable[[], None]] = None,
) -> tuple[list[dict], Optional[dict[str, Any]]]:
- """can_reset=False blocks new epochs (unsearchable reset loses data) but still replays one in force."""
+ """can_reset=False blocks new epochs (unsearchable reset loses data) but still replays one in force.
+ on_evict runs only when a new epoch drops turns, never for a replay."""
if context_length <= 1:
return messages, None
@@ -470,6 +472,8 @@ def fit_checkpoint_context(
# primitive itself protects system, developer, final and newest user groups. The turn in progress is held too,
# released oldest first until the reset leaves the headroom free.
if (current_tokens > prompt_target or force_reset) and _resolved(can_reset):
+ if on_evict is not None:
+ on_evict()
live = _live_turn_groups(messages)
ratio = clamp_compaction_headroom_ratio(headroom_ratio)
hold_target = prompt_target - int(
diff --git a/studio/backend/core/inference/context_window.py b/studio/backend/core/inference/context_window.py
index 3ae005da261..df76a5934c8 100644
--- a/studio/backend/core/inference/context_window.py
+++ b/studio/backend/core/inference/context_window.py
@@ -899,6 +899,7 @@ def fit_rolling_context(
headroom_ratio: Optional[float] = None,
estimate_message: Callable[[dict], int] = estimate_message_tokens,
replay_only: bool = False,
+ on_evict: Optional[Callable[[], None]] = None,
) -> tuple[list[dict], Optional[dict[str, Any]]]:
"""Drops oldest turns; reserve_tokens never affects trimming; sticky_dropped replays the boundary."""
if context_length <= 1:
@@ -948,6 +949,8 @@ def fit_rolling_context(
headroom = int(prompt_target * ratio) if keeps_boundary else 0
trim_target = max(1, prompt_target - reserve_tokens - headroom)
+ if on_evict is not None and current_tokens > trim_target:
+ on_evict()
while current_tokens > trim_target:
keep_ratio = trim_target / max(1, current_tokens)
if min_bite:
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index f7d24f82511..880fa8b1ca2 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -368,13 +368,38 @@ def _fit_rolling_remembered(messages, kwargs):
return fitted, truncation
+def _once(on_compacting):
+ """``on_compacting`` at most once per fit, never raising: a progress report must not break a fit."""
+ fired = False
+
+ def fire() -> None:
+ nonlocal fired
+ if fired:
+ return
+ fired = True
+ try:
+ on_compacting()
+ except Exception: # noqa: BLE001
+ logger.debug("compaction progress report failed", exc_info = True)
+
+ return fire
+
+
+def _compaction_reporter(perf_callback):
+ report = getattr(perf_callback, "compacting", None)
+ return report if callable(report) else None
+
+
def _fit_with_instruction_pins(
messages,
*,
anchor_ids = None,
+ on_compacting = None,
**kwargs,
):
"""Pins are recomputed each fit (inline recall swaps the user dict), dropped if they block a fit, and must never mutate the anchor set the caller passed."""
+ if on_compacting is not None:
+ kwargs["on_evict"] = _once(on_compacting)
anchors = set(anchor_ids or ())
try:
from core.inference import instruction_pin
@@ -37004,6 +37029,7 @@ class LlamaCppBackend:
)
openai_messages, truncation = _fit_with_instruction_pins(
openai_messages,
+ on_compacting = _compaction_reporter(perf_callback),
context_length = self._effective_context_length,
max_tokens = payload["max_tokens"],
count_tokens = lambda fitted: self.count_chat_tokens(
@@ -37970,6 +37996,7 @@ class LlamaCppBackend:
)
conversation, truncation = _fit_with_instruction_pins(
conversation,
+ on_compacting = _compaction_reporter(perf_callback),
context_length = self._effective_context_length,
max_tokens = _iteration_max_tokens,
count_tokens = lambda fitted: self.count_chat_tokens(
@@ -38160,6 +38187,7 @@ class LlamaCppBackend:
try:
conversation, truncation = _fit_with_instruction_pins(
conversation,
+ on_compacting = _compaction_reporter(perf_callback),
context_length = self._effective_context_length,
max_tokens = _iteration_max_tokens,
count_tokens = lambda fitted: self.count_chat_tokens(
@@ -40592,6 +40620,7 @@ class LlamaCppBackend:
)
conversation, truncation = _fit_with_instruction_pins(
conversation,
+ on_compacting = _compaction_reporter(perf_callback),
context_length = self._effective_context_length,
max_tokens = _final_max_tokens,
count_tokens = lambda fitted: self.count_chat_tokens(
@@ -40750,6 +40779,7 @@ class LlamaCppBackend:
try:
conversation, truncation = _fit_with_instruction_pins(
conversation,
+ on_compacting = _compaction_reporter(perf_callback),
context_length = self._effective_context_length,
max_tokens = max_tokens,
count_tokens = lambda fitted: self.count_chat_tokens(
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 7d531432038..201f66be547 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -7179,8 +7179,23 @@ class _PrefillProgressSignal:
self._decode: Optional[dict] = None
self._decode_sent_at = 0.0
self._decode_started = False
+ # Counters, not a frame: one fit reports once, and the event loop sends each new report once.
+ self._compactions = 0
+ self._compactions_sent = 0
self.wants_timings = inner is not None or live
+ def round_ended(self) -> None:
+ """A tool result closed the pass: its unsent prefill and decode figures must not follow into the next round."""
+ self._prefill_sent = self._prefill
+ self._decode = None
+ self._decode_started = False
+
+ def compacting(self) -> None:
+ if self._live:
+ # Cleared first: a figure from the last pass sent after this report would clear the label on the client.
+ self.round_ended()
+ self._compactions += 1
+
@property
def needs_phase(self) -> bool:
return self._inner is not None and getattr(self._inner, "needs_phase", True)
@@ -7255,6 +7270,11 @@ class _PrefillProgressSignal:
once per ``_LIVE_DECODE_FRAME_INTERVAL_S``. None when nothing is due."""
if not self._live:
return None
+ # Before prefill: a pending report always precedes the prompt pass that follows its fit.
+ compactions = self._compactions
+ if compactions != self._compactions_sent:
+ self._compactions_sent = compactions
+ return 'data: {"type":"generation_progress","phase":"compacting"}\n\n'
prefill = self._prefill
if prefill is not None and prefill is not self._prefill_sent:
self._prefill_sent = prefill
@@ -32443,6 +32463,8 @@ async def produce_openai_chat_completions(
continue
if event["type"] in ("tool_start", "tool_end"):
+ if event["type"] == "tool_end":
+ _gguf_prefill_signal.round_ended()
if event["type"] == "tool_start":
# Gated with the card since the stamp is set once: a caller that cannot
# see the card would otherwise report the tool run as its TTFT.
diff --git a/studio/backend/tests/test_live_generation_progress.py b/studio/backend/tests/test_live_generation_progress.py
index fa981375f00..481c985be43 100644
--- a/studio/backend/tests/test_live_generation_progress.py
+++ b/studio/backend/tests/test_live_generation_progress.py
@@ -231,3 +231,222 @@ def test_stall_wait_still_sends_the_comment_after_the_stall_interval(monkeypatch
def test_providers_cannot_forge_the_frame(line):
assert sanitize_provider_sse_line(line) is None
assert is_ui_control_sse_line(line)
+
+
+def test_a_compaction_report_becomes_one_frame_ahead_of_the_prefill():
+ signal = route._PrefillProgressSignal(None, live = True)
+ signal.compacting()
+ signal({"prompt_progress": {"total": 4000, "processed": 512}})
+ assert _frame(signal.live_frame()) == {"type": "generation_progress", "phase": "compacting"}
+ assert _frame(signal.live_frame())["phase"] == "prefill"
+ assert signal.live_frame() is None
+ signal.compacting()
+ assert _frame(signal.live_frame())["phase"] == "compacting"
+
+
+def test_a_compaction_report_is_dropped_when_not_live():
+ signal = route._PrefillProgressSignal(None)
+ signal.compacting()
+ assert signal.live_frame() is None
+
+
+def test_the_last_passs_unsent_figures_never_follow_a_compaction_report(monkeypatch):
+ # The tool call decoded within the throttle, so its decode figures were still pending when the fit began.
+ monkeypatch.setattr(route, "_LIVE_DECODE_FRAME_INTERVAL_S", 0.0)
+ signal = route._PrefillProgressSignal(None, live = True)
+ signal({"prompt_progress": {"total": 4000, "processed": 4000}})
+ signal({"predicted_n": 30, "predicted_per_second": 20.0})
+ signal.compacting()
+ assert _frame(signal.live_frame())["phase"] == "compacting"
+ assert signal.live_frame() is None, "a decode frame here would clear the label on the client"
+
+
+def test_a_tool_result_drops_the_last_passs_unsent_figures(monkeypatch):
+ monkeypatch.setattr(route, "_LIVE_DECODE_FRAME_INTERVAL_S", 0.0)
+ signal = route._PrefillProgressSignal(None, live = True)
+ signal({"prompt_progress": {"total": 4000, "processed": 4000}})
+ signal({"predicted_n": 30, "predicted_per_second": 20.0})
+ signal.round_ended()
+ assert signal.live_frame() is None
+ signal({"prompt_progress": {"total": 4100, "processed": 4050}})
+ assert _frame(signal.live_frame())["processed"] == 4050
+
+
+def test_stall_wait_sends_the_compaction_frame_while_the_fit_blocks(monkeypatch):
+ monkeypatch.setattr(route, "_LIVE_PROGRESS_POLL_S", 0.01)
+ signal = route._PrefillProgressSignal(None, live = True)
+
+ async def scenario():
+ release = asyncio.Event()
+
+ async def step():
+ await release.wait()
+ return "context_truncated"
+
+ task = asyncio.create_task(step())
+ lines = []
+
+ async def collect():
+ async for line in route._stall_wait_frames(task, signal, first_timeout = 15.0):
+ lines.append(line)
+
+ collector = asyncio.create_task(collect())
+ signal.compacting()
+ await asyncio.sleep(0.05)
+ # Sent while the step is still blocked, not when the fit returns.
+ sent_before_release = list(lines)
+ release.set()
+ await collector
+ return sent_before_release, lines
+
+ before, lines = asyncio.run(scenario())
+ assert [_frame(line)["phase"] for line in before] == ["compacting"]
+ assert len(lines) == 1
+
+
+def _chars(candidate):
+ return sum(len(str(message.get("content", ""))) for message in candidate)
+
+
+def _fit(
+ messages,
+ on_compacting,
+ counter = _chars,
+ **kwargs,
+):
+ from core.inference import llama_cpp
+ return llama_cpp._fit_with_instruction_pins(
+ messages,
+ context_length = 1000,
+ max_tokens = 128,
+ count_tokens = counter,
+ on_compacting = on_compacting,
+ **kwargs,
+ )
+
+
+def _turns(size):
+ return [
+ {"role": "user", "content": "A" * size},
+ {"role": "assistant", "content": "B" * size},
+ {"role": "user", "content": "C" * 50},
+ ]
+
+
+def test_the_fit_reports_once_when_it_starts_evicting():
+ reports = []
+ fitted, info = _fit(_turns(600), lambda: reports.append(True))
+ assert info is not None and info["dropped_messages"] > 0
+ assert reports == [True], "one report per fit, however many counts the trim takes"
+
+
+def test_a_fit_that_keeps_everything_reports_nothing():
+ reports = []
+ fitted, info = _fit(_turns(100), lambda: reports.append(True))
+ assert info is None
+ assert reports == []
+
+
+def test_a_forced_reset_reports(monkeypatch):
+ from core.inference import llama_cpp
+
+ monkeypatch.setattr(llama_cpp, "_archive_is_degraded", lambda: False)
+ reports = []
+ fitted, info = _fit(
+ _turns(100),
+ lambda: reports.append(True),
+ force_reset = True,
+ context_policy = "checkpoint",
+ can_reset = True,
+ )
+ assert info is not None and info["checkpoint_started"]
+ assert reports == [True]
+
+
+def test_a_replay_only_fit_never_reports():
+ reports = []
+ _fit(_turns(600), lambda: reports.append(True), replay_only = True, sticky_dropped = 1)
+ assert reports == []
+
+
+def _compacted_chat(recent_size):
+ """Four turns a previous request already cut (the saved boundary), then the turns still in the window."""
+ old = [
+ {"role": role, "content": letter * 300}
+ for role, letter in (("user", "A"), ("assistant", "B"), ("user", "C"), ("assistant", "D"))
+ ]
+ recent = [
+ {"role": "user", "content": "E" * recent_size},
+ {"role": "assistant", "content": "F" * recent_size},
+ {"role": "user", "content": "G" * 50},
+ ]
+ return old + recent
+
+
+_REPLAYS = {
+ "rolling": {"context_policy": "rolling", "sticky_is_checkpoint": False, "keeps_boundary": True},
+ "checkpoint": {
+ "context_policy": "checkpoint",
+ "sticky_is_checkpoint": True,
+ "can_reset": False,
+ },
+}
+
+
[email protected]("policy", sorted(_REPLAYS))
+def test_replaying_a_saved_cut_is_not_reported(policy):
+ # The full history is over budget on every request after the first compaction; replaying the cut it
+ # already made evicts nothing new, so "Compacting context..." must not flash on each later turn.
+ reports = []
+ fitted, info = _fit(
+ _compacted_chat(100), lambda: reports.append(True), sticky_dropped = 4, **_REPLAYS[policy]
+ )
+ assert info is not None and info["fits"] and info["dropped_messages"] == 4
+ assert reports == []
+
+
[email protected]("policy", sorted(_REPLAYS))
+def test_a_replay_that_no_longer_fits_is_reported(policy):
+ reports = []
+ fitted, info = _fit(
+ _compacted_chat(500), lambda: reports.append(True), sticky_dropped = 4, **_REPLAYS[policy]
+ )
+ assert info is not None and info["dropped_messages"] > 4
+ assert reports == [True]
+
+
+def test_a_new_checkpoint_past_a_saved_cut_is_reported(monkeypatch):
+ # No system message, so the checkpoint block adds one, and a block the template prices at nothing leaves the
+ # reset's own count under budget: only the eviction decision itself shows that a new epoch started.
+ from core.inference import llama_cpp
+
+ monkeypatch.setattr(llama_cpp, "_archive_is_degraded", lambda: False)
+ reports = []
+ fitted, info = _fit(
+ _compacted_chat(450),
+ lambda: reports.append(True),
+ counter = lambda candidate: _chars([m for m in candidate if m.get("role") != "system"]),
+ sticky_dropped = 4,
+ context_policy = "checkpoint",
+ sticky_is_checkpoint = True,
+ can_reset = True,
+ )
+ assert info is not None and info["checkpoint_started"] and info["dropped_messages"] > 4
+ assert reports == [True]
+
+
+def test_a_failing_report_never_breaks_the_fit():
+ def boom():
+ raise RuntimeError("closed stream")
+
+ fitted, info = _fit(_turns(600), boom)
+ assert info is not None and info["fits"] is True
+
+
+def test_only_a_live_signal_is_a_compaction_reporter():
+ from core.inference import llama_cpp
+
+ signal = route._PrefillProgressSignal(None, live = True)
+ assert llama_cpp._compaction_reporter(signal) == signal.compacting
+ assert llama_cpp._compaction_reporter(lambda sample: None) is None
+ assert llama_cpp._compaction_reporter(None) is None
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index f596d1ba2e0..c9a5a606d85 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -231,7 +231,12 @@ import {
providerModelSupportsStudioTools,
} from "@/features/chat/external-providers";
import { toolStatusKind } from "@/features/chat/utils/tool-status";
-import { formatPrefillDetails, formatPrefillTimeLeft, prefillPercent } from "@/features/chat";
+import {
+ betweenToolRounds,
+ formatPrefillDetails,
+ formatPrefillTimeLeft,
+ prefillPercent,
+} from "@/features/chat";
import { replySourceMarkdown } from "@/features/chat/utils/reply-source-markdown";
import { toolResultModelText } from "@/features/chat/api/chat-adapter";
import {
@@ -7732,7 +7737,22 @@ const GeneratingIndicator: FC = () => {
return <GeneratingStatus />;
};
-// Mounted only under the empty running reply, so the thread's other messages subscribe to nothing new.
+// GeneratingIndicator's status for the gap after a tool result, until the next pass streams its first part.
+const NextRoundIndicator: FC = () => {
+ const show = useAuiState(({ message }) =>
+ betweenToolRounds(message.status?.type === "running", message.content),
+ );
+ if (!show) {
+ return null;
+ }
+ return (
+ <div data-slot="next-round-indicator" className="mt-2">
+ <GeneratingStatus />
+ </div>
+ );
+};
+
+// Mounted only under a running reply waiting on the model, so the thread's other messages subscribe to nothing new.
const GeneratingStatus: FC = () => {
// Keyed like DiffusionCanvas: a first turn reads "__default" until the thread persists.
const threadKey =
@@ -7747,9 +7767,22 @@ const GeneratingStatus: FC = () => {
const decoding = useChatRuntimeStore(
(s) => s.liveGenerationByThreadId[threadKey]?.decode != null,
);
+ const compacting = useChatRuntimeStore(
+ (s) => s.liveGenerationByThreadId[threadKey]?.compacting === true,
+ );
const showDetails = useChatPreferencesStore((s) => s.showGenerationDetails);
const locale = useLocale();
const t = useT();
+ if (compacting) {
+ return (
+ <span
+ data-slot="compacting-progress"
+ className="generating-glint text-sm text-muted-foreground"
+ >
+ {t("generationStats.compactingContext")}
+ </span>
+ );
+ }
const percent = prefill ? prefillPercent(prefill) : null;
// Held at 100% until the first token: going back to Working... for that moment would flash too.
if (prefill === null || !labelShown || percent === null || decoding) {
@@ -8323,6 +8356,7 @@ const AssistantMessage: FC = () => {
<DiffusionCanvas />
<MessagePrimitive.Parts components={ASSISTANT_PART_COMPONENTS} />
+ <NextRoundIndicator />
<SourcesGroup />
<MessageHtmlArtifacts />
<ContinueMessageBar />
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.ts
index 0450fb4776b..8332eda8227 100644
--- a/studio/frontend/src/features/chat/api/chat-adapter.ts
+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts
@@ -7199,6 +7199,7 @@ export function createOpenAIStreamAdapter(
}
if (chunk.context_truncated) {
+ runtime.setLiveCompacting(liveThreadKey(serverCancel), false);
contextTruncation = mergeContextTruncation(
contextTruncation,
chunk.context_truncated,
@@ -7289,6 +7290,8 @@ export function createOpenAIStreamAdapter(
runtime.setLivePrefill(liveThreadKey(serverCancel), update.prefill);
} else if (update?.phase === "decode") {
runtime.setLiveDecode(liveThreadKey(serverCancel), update.decode);
+ } else if (update?.phase === "compacting") {
+ runtime.setLiveCompacting(liveThreadKey(serverCancel), true);
}
continue;
}
@@ -7666,6 +7669,8 @@ export function createOpenAIStreamAdapter(
);
}
} else if (toolEvent.type === "tool_end") {
+ // The round boundary: the last pass's prompt and decode figures must not stand in for the next one's.
+ runtime.clearLiveGenerationForThread(liveThreadKey(serverCancel));
const backendToolCallId =
(toolEvent.tool_call_id as string) || "";
const id = resolveToolPartId(backendToolCallId);
diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.ts
index 733ecd7621f..a198fd2fe13 100644
--- a/studio/frontend/src/features/chat/index.ts
+++ b/studio/frontend/src/features/chat/index.ts
@@ -93,6 +93,7 @@ export { resolveStagedDiffusionClassification } from "./lib/gpu-placement";
export { describePlacement, offloadCountsFrom, offloadWarning } from "./lib/partial-offload";
export type { PlacementPool } from "./lib/partial-offload";
export {
+ betweenToolRounds,
formatLiveDecode,
formatPrefillDetails,
formatPrefillTimeLeft,
diff --git a/studio/frontend/src/features/chat/lib/generation-progress.ts b/studio/frontend/src/features/chat/lib/generation-progress.ts
index a9d5ab734dd..ae2ba7bc180 100644
--- a/studio/frontend/src/features/chat/lib/generation-progress.ts
+++ b/studio/frontend/src/features/chat/lib/generation-progress.ts
@@ -18,9 +18,24 @@ function tokenCount(count: number, t: Translate, locale?: string): string {
});
}
+/** The newest part is a finished tool call, so the next pass has not streamed yet. Sources trail a recovered reply. */
+export function betweenToolRounds(
+ running: boolean,
+ content: readonly { type: string; result?: unknown }[],
+): boolean {
+ if (!running) return false;
+ for (let i = content.length - 1; i >= 0; i--) {
+ const part = content[i];
+ if (part.type === "source") continue;
+ return part.type === "tool-call" && part.result !== undefined;
+ }
+ return false;
+}
+
export type GenerationProgressUpdate =
| { phase: "prefill"; prefill: LivePrefillProgress }
- | { phase: "decode"; decode: LiveDecodeProgress };
+ | { phase: "decode"; decode: LiveDecodeProgress }
+ | { phase: "compacting" };
function count(value: unknown): number | null {
return typeof value === "number" && Number.isFinite(value) && value >= 0
@@ -40,6 +55,7 @@ export function readGenerationProgress(
): GenerationProgressUpdate | null {
if (frame === null || typeof frame !== "object") return null;
const f = frame as Record<string, unknown>;
+ if (f.phase === "compacting") return { phase: "compacting" };
if (f.phase === "prefill") {
const processed = count(f.processed);
if (processed === null) return null;
diff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsx
index 6b2daba46f5..ac24b969426 100644
--- a/studio/frontend/src/features/chat/runtime-provider.tsx
+++ b/studio/frontend/src/features/chat/runtime-provider.tsx
@@ -1764,6 +1764,12 @@ function scheduleGenerationRecovery(
context_truncated?: OpenAIChatChunk["context_truncated"];
quote_cut?: boolean;
};
+ if (
+ caughtUp(update.run) &&
+ (chunk as { _toolEvent?: { type?: unknown } })._toolEvent?.type === "tool_end"
+ ) {
+ useChatRuntimeStore.getState().clearLiveGenerationForThread(threadId);
+ }
// Live figures only once caught up: a replayed backlog would flash every old sample.
if ("_generationProgress" in chunk) {
if (caughtUp(update.run)) {
@@ -1773,6 +1779,13 @@ function scheduleGenerationRecovery(
store.setLivePrefill(threadId, progress.prefill);
} else if (progress?.phase === "decode") {
store.setLiveDecode(threadId, progress.decode);
+ } else if (progress?.phase === "compacting") {
+ store.setLiveCompacting(threadId, true);
+ }
+ // A frame can end the replay: show what it rebuilt, or a replayed tool result stays hidden.
+ if (update.run.status !== lastPublishedStatus) {
+ lastPublishedStatus = update.run.status;
+ await publish(update.run);
}
}
continue;
@@ -1795,6 +1808,7 @@ function scheduleGenerationRecovery(
firstChunkAt ??= update.event.createdAt;
}
if (chunk.context_truncated) {
+ useChatRuntimeStore.getState().setLiveCompacting(threadId, false);
const toolCalls = carried
.map((entry) => entry.part as { type?: unknown } | null)
.filter((part) => part?.type === "tool-call") as Array<{
diff --git a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
index 627eeb9b3c6..c3e74e8dc69 100644
--- a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
+++ b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
@@ -448,6 +448,8 @@ export type LiveGenerationProgress = {
/** Whether "Processing prompt" is up for this pass; see prefillLabelShown. */
prefillShown: boolean;
decode: LiveDecodeProgress | null;
+ /** The context fit is evicting turns; ends with this pass's context_truncated or its first prefill sample. */
+ compacting?: boolean;
};
export type PendingImageEditReference = {
threadId: string | null;
@@ -2737,6 +2739,7 @@ type ChatRuntimeStore = {
clearActiveDiffusionCanvasForThread: (threadId: string | null) => void;
setLivePrefill: (threadId: string | null, prefill: LivePrefillProgress) => void;
setLiveDecode: (threadId: string | null, decode: LiveDecodeProgress) => void;
+ setLiveCompacting: (threadId: string | null, compacting: boolean) => void;
clearLiveGenerationForThread: (threadId: string | null) => void;
setAutoHealToolCalls: (enabled: boolean) => void;
setNudgeToolCalls: (enabled: boolean) => void;
@@ -5995,6 +5998,19 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
},
};
}),
+ setLiveCompacting: (threadId, compacting) =>
+ set((state) => {
+ const key = threadId || "__default";
+ const current = state.liveGenerationByThreadId[key];
+ if (!compacting && !current?.compacting) return state;
+ // A new pass: the last round's prefill and decode figures describe a prompt the fit is replacing.
+ const next: LiveGenerationProgress = compacting
+ ? { prefill: null, prefillShown: false, decode: null, compacting: true }
+ : { ...current!, compacting: false };
+ return {
+ liveGenerationByThreadId: { ...state.liveGenerationByThreadId, [key]: next },
+ };
+ }),
clearLiveGenerationForThread: (threadId) =>
set((state) => {
const key = threadId || "__default";
diff --git a/studio/frontend/src/i18n/locales/ar.ts b/studio/frontend/src/i18n/locales/ar.ts
index 9c2f16df18e..6c40d644063 100644
--- a/studio/frontend/src/i18n/locales/ar.ts
+++ b/studio/frontend/src/i18n/locales/ar.ts
@@ -5483,6 +5483,7 @@ export const ar = {
generationStats: {
working: "جارٍ التنفيذ...",
processingPrompt: "جارٍ معالجة الطلب {percent}%",
+ compactingContext: "جارٍ ضغط السياق...",
timeLeft: "يتبقى {eta}",
tokensOne: "التوكنات: {count}",
tokens: "التوكنات: {count}",
diff --git a/studio/frontend/src/i18n/locales/cs.ts b/studio/frontend/src/i18n/locales/cs.ts
index a521e9f6808..4ac1df8a6c5 100644
--- a/studio/frontend/src/i18n/locales/cs.ts
+++ b/studio/frontend/src/i18n/locales/cs.ts
@@ -5909,6 +5909,7 @@ export const cs = {
generationStats: {
working: "Probíhá zpracování...",
processingPrompt: "Zpracování promptu {percent} %",
+ compactingContext: "Zhušťování kontextu...",
timeLeft: "zbývá {eta}",
tokensOne: "tokeny: {count}",
tokens: "tokeny: {count}",
diff --git a/studio/frontend/src/i18n/locales/de.ts b/studio/frontend/src/i18n/locales/de.ts
index 237772dafee..fd4b7f0881f 100644
--- a/studio/frontend/src/i18n/locales/de.ts
+++ b/studio/frontend/src/i18n/locales/de.ts
@@ -5557,6 +5557,7 @@ export const de = {
generationStats: {
working: "Wird bearbeitet...",
processingPrompt: "Prompt-Verarbeitung {percent}%",
+ compactingContext: "Kontext wird komprimiert...",
timeLeft: "noch {eta}",
tokensOne: "{count} Token",
tokens: "{count} Tokens",
diff --git a/studio/frontend/src/i18n/locales/en.ts b/studio/frontend/src/i18n/locales/en.ts
index d54f9382e4e..b09e5b148a2 100644
--- a/studio/frontend/src/i18n/locales/en.ts
+++ b/studio/frontend/src/i18n/locales/en.ts
@@ -5527,6 +5527,7 @@ export const en = {
generationStats: {
working: "Working...",
processingPrompt: "Processing prompt {percent}%",
+ compactingContext: "Compacting context...",
// {eta}: a short duration such as "12s" or "2m 10s".
timeLeft: "{eta} left",
tokensOne: "{count} token",
diff --git a/studio/frontend/src/i18n/locales/es-419.ts b/studio/frontend/src/i18n/locales/es-419.ts
index 6722e6c7e7f..c0253cecbcd 100644
--- a/studio/frontend/src/i18n/locales/es-419.ts
+++ b/studio/frontend/src/i18n/locales/es-419.ts
@@ -5980,6 +5980,7 @@ export const es419 = {
generationStats: {
working: "Procesando...",
processingPrompt: "Procesando el prompt {percent}%",
+ compactingContext: "Compactando el contexto...",
timeLeft: "quedan {eta}",
tokensOne: "{count} token",
tokens: "{count} tokens",
diff --git a/studio/frontend/src/i18n/locales/es.ts b/studio/frontend/src/i18n/locales/es.ts
index f8952dd7bf6..da6f8b602b7 100644
--- a/studio/frontend/src/i18n/locales/es.ts
+++ b/studio/frontend/src/i18n/locales/es.ts
@@ -5556,6 +5556,7 @@ export const es = {
generationStats: {
working: "Procesando...",
processingPrompt: "Procesando el prompt {percent}%",
+ compactingContext: "Compactando el contexto...",
timeLeft: "quedan {eta}",
tokensOne: "{count} token",
tokens: "{count} tokens",
diff --git a/studio/frontend/src/i18n/locales/fa.ts b/studio/frontend/src/i18n/locales/fa.ts
index 27d20ed5b3f..920dc8c8f7b 100644
--- a/studio/frontend/src/i18n/locales/fa.ts
+++ b/studio/frontend/src/i18n/locales/fa.ts
@@ -5861,6 +5861,7 @@ export const fa = {
generationStats: {
working: "در حال انجام...",
processingPrompt: "پردازش پرامپت {percent}%",
+ compactingContext: "در حال فشرده‌سازی زمینه...",
timeLeft: "{eta} مانده",
tokensOne: "{count} توکن",
tokens: "{count} توکن",
diff --git a/studio/frontend/src/i18n/locales/fr.ts b/studio/frontend/src/i18n/locales/fr.ts
index 40c0df0657b..af285bd53ff 100644
--- a/studio/frontend/src/i18n/locales/fr.ts
+++ b/studio/frontend/src/i18n/locales/fr.ts
@@ -5567,6 +5567,7 @@ export const fr = {
generationStats: {
working: "Traitement en cours...",
processingPrompt: "Traitement du prompt {percent} %",
+ compactingContext: "Compactage du contexte...",
timeLeft: "reste {eta}",
tokensOne: "{count} token",
tokens: "{count} tokens",
diff --git a/studio/frontend/src/i18n/locales/he.ts b/studio/frontend/src/i18n/locales/he.ts
index 34f4fa3e998..47bbdf35e7b 100644
--- a/studio/frontend/src/i18n/locales/he.ts
+++ b/studio/frontend/src/i18n/locales/he.ts
@@ -5581,6 +5581,7 @@ export const he = {
generationStats: {
working: "מעבד...",
processingPrompt: "עיבוד הפרומפט {percent}%",
+ compactingContext: "דוחס את ההקשר...",
timeLeft: "נותרו {eta}",
tokensOne: "{count} טוקן",
tokens: "{count} טוקנים",
diff --git a/studio/frontend/src/i18n/locales/hi.ts b/studio/frontend/src/i18n/locales/hi.ts
index 78feaec6131..6c939153def 100644
--- a/studio/frontend/src/i18n/locales/hi.ts
+++ b/studio/frontend/src/i18n/locales/hi.ts
@@ -5494,6 +5494,7 @@ export const hi = {
generationStats: {
working: "प्रक्रिया जारी है...",
processingPrompt: "प्रॉम्प्ट प्रोसेस हो रहा है {percent}%",
+ compactingContext: "संदर्भ संक्षिप्त किया जा रहा है...",
timeLeft: "{eta} बाकी",
tokensOne: "{count} टोकन",
tokens: "{count} टोकन",
diff --git a/studio/frontend/src/i18n/locales/id.ts b/studio/frontend/src/i18n/locales/id.ts
index 32ab2feff15..53eca6d6d98 100644
--- a/studio/frontend/src/i18n/locales/id.ts
+++ b/studio/frontend/src/i18n/locales/id.ts
@@ -5893,6 +5893,7 @@ export const id = {
generationStats: {
working: "Memproses...",
processingPrompt: "Memproses prompt {percent}%",
+ compactingContext: "Memadatkan konteks...",
timeLeft: "{eta} lagi",
tokensOne: "{count} token",
tokens: "{count} token",
diff --git a/studio/frontend/src/i18n/locales/it.ts b/studio/frontend/src/i18n/locales/it.ts
index 050824323e1..1907b183e11 100644
--- a/studio/frontend/src/i18n/locales/it.ts
+++ b/studio/frontend/src/i18n/locales/it.ts
@@ -5552,6 +5552,7 @@ export const it = {
generationStats: {
working: "Elaborazione...",
processingPrompt: "Elaborazione del prompt {percent}%",
+ compactingContext: "Compattazione del contesto...",
timeLeft: "{eta} rimanenti",
tokensOne: "{count} token",
tokens: "{count} token",
diff --git a/studio/frontend/src/i18n/locales/ja.ts b/studio/frontend/src/i18n/locales/ja.ts
index 547da14b699..02c7a90e096 100644
--- a/studio/frontend/src/i18n/locales/ja.ts
+++ b/studio/frontend/src/i18n/locales/ja.ts
@@ -5434,6 +5434,7 @@ export const ja = {
generationStats: {
working: "処理中...",
processingPrompt: "プロンプトを処理中 {percent}%",
+ compactingContext: "コンテキストを圧縮中...",
timeLeft: "残り {eta}",
tokensOne: "{count} トークン",
tokens: "{count} トークン",
diff --git a/studio/frontend/src/i18n/locales/ko.ts b/studio/frontend/src/i18n/locales/ko.ts
index 45d34af8fb1..ba8b891199b 100644
--- a/studio/frontend/src/i18n/locales/ko.ts
+++ b/studio/frontend/src/i18n/locales/ko.ts
@@ -5474,6 +5474,7 @@ export const ko = {
generationStats: {
working: "처리 중...",
processingPrompt: "프롬프트 처리 중 {percent}%",
+ compactingContext: "컨텍스트 압축 중...",
timeLeft: "{eta} 남음",
tokensOne: "토큰 {count}개",
tokens: "토큰 {count}개",
diff --git a/studio/frontend/src/i18n/locales/nl.ts b/studio/frontend/src/i18n/locales/nl.ts
index cd498c5ad3b..32fb6934c50 100644
--- a/studio/frontend/src/i18n/locales/nl.ts
+++ b/studio/frontend/src/i18n/locales/nl.ts
@@ -5950,6 +5950,7 @@ export const nl = {
generationStats: {
working: "Bezig...",
processingPrompt: "Prompt verwerken {percent}%",
+ compactingContext: "Context comprimeren...",
timeLeft: "nog {eta}",
tokensOne: "{count} token",
tokens: "{count} tokens",
diff --git a/studio/frontend/src/i18n/locales/pl.ts b/studio/frontend/src/i18n/locales/pl.ts
index 95b111f0499..8b3cd174226 100644
--- a/studio/frontend/src/i18n/locales/pl.ts
+++ b/studio/frontend/src/i18n/locales/pl.ts
@@ -5945,6 +5945,7 @@ export const pl = {
generationStats: {
working: "Przetwarzanie...",
processingPrompt: "Przetwarzanie promptu {percent}%",
+ compactingContext: "Kompaktowanie kontekstu...",
timeLeft: "zostało {eta}",
tokensOne: "tokeny: {count}",
tokens: "tokeny: {count}",
diff --git a/studio/frontend/src/i18n/locales/pt-br.ts b/studio/frontend/src/i18n/locales/pt-br.ts
index 9fcaecb32f0..8003b82a9dc 100644
--- a/studio/frontend/src/i18n/locales/pt-br.ts
+++ b/studio/frontend/src/i18n/locales/pt-br.ts
@@ -5512,6 +5512,7 @@ export const ptBR = {
generationStats: {
working: "Processando...",
processingPrompt: "Processando prompt {percent}%",
+ compactingContext: "Compactando o contexto...",
timeLeft: "faltam {eta}",
tokensOne: "{count} token",
tokens: "{count} tokens",
diff --git a/studio/frontend/src/i18n/locales/ru.ts b/studio/frontend/src/i18n/locales/ru.ts
index c319a0aeff6..510210d7471 100644
--- a/studio/frontend/src/i18n/locales/ru.ts
+++ b/studio/frontend/src/i18n/locales/ru.ts
@@ -5511,6 +5511,7 @@ export const ru = {
generationStats: {
working: "Обработка...",
processingPrompt: "Обработка промпта {percent}%",
+ compactingContext: "Сжатие контекста...",
timeLeft: "осталось {eta}",
tokensOne: "токены: {count}",
tokens: "токены: {count}",
diff --git a/studio/frontend/src/i18n/locales/sv.ts b/studio/frontend/src/i18n/locales/sv.ts
index b3233c8faf7..48458cc17c5 100644
--- a/studio/frontend/src/i18n/locales/sv.ts
+++ b/studio/frontend/src/i18n/locales/sv.ts
@@ -5726,6 +5726,7 @@ export const sv = {
generationStats: {
working: "Arbetar...",
processingPrompt: "Bearbetar prompt {percent}%",
+ compactingContext: "Komprimerar kontext...",
timeLeft: "{eta} kvar",
tokensOne: "{count} token",
tokens: "{count} token",
diff --git a/studio/frontend/src/i18n/locales/th.ts b/studio/frontend/src/i18n/locales/th.ts
index e096257d559..711991965c7 100644
--- a/studio/frontend/src/i18n/locales/th.ts
+++ b/studio/frontend/src/i18n/locales/th.ts
@@ -5613,6 +5613,7 @@ export const th = {
generationStats: {
working: "กำลังดำเนินการ...",
processingPrompt: "กำลังประมวลผลพรอมต์ {percent}%",
+ compactingContext: "กำลังบีบอัดบริบท...",
timeLeft: "เหลืออีก {eta}",
tokensOne: "{count} โทเค็น",
tokens: "{count} โทเค็น",
diff --git a/studio/frontend/src/i18n/locales/tr.ts b/studio/frontend/src/i18n/locales/tr.ts
index 41f9922b74e..cc7a1d71a25 100644
--- a/studio/frontend/src/i18n/locales/tr.ts
+++ b/studio/frontend/src/i18n/locales/tr.ts
@@ -5889,6 +5889,7 @@ export const tr = {
generationStats: {
working: "İşleniyor...",
processingPrompt: "İstem işleniyor %{percent}",
+ compactingContext: "Bağlam sıkıştırılıyor...",
timeLeft: "{eta} kaldı",
tokensOne: "{count} token",
tokens: "{count} token",
diff --git a/studio/frontend/src/i18n/locales/uk.ts b/studio/frontend/src/i18n/locales/uk.ts
index 52ccbefd86a..6ec0d7fb2be 100644
--- a/studio/frontend/src/i18n/locales/uk.ts
+++ b/studio/frontend/src/i18n/locales/uk.ts
@@ -5936,6 +5936,7 @@ export const uk = {
generationStats: {
working: "Виконання...",
processingPrompt: "Обробка запиту {percent}%",
+ compactingContext: "Стиснення контексту...",
timeLeft: "залишилось {eta}",
tokensOne: "токени: {count}",
tokens: "токени: {count}",
diff --git a/studio/frontend/src/i18n/locales/vi.ts b/studio/frontend/src/i18n/locales/vi.ts
index e1f36813767..0e52826ad4f 100644
--- a/studio/frontend/src/i18n/locales/vi.ts
+++ b/studio/frontend/src/i18n/locales/vi.ts
@@ -5892,6 +5892,7 @@ export const vi = {
generationStats: {
working: "Đang xử lý...",
processingPrompt: "Đang xử lý lời nhắc {percent}%",
+ compactingContext: "Đang thu gọn ngữ cảnh...",
timeLeft: "còn {eta}",
tokensOne: "{count} token",
tokens: "{count} token",
diff --git a/studio/frontend/src/i18n/locales/zh-CN.ts b/studio/frontend/src/i18n/locales/zh-CN.ts
index 4e3cd76e02b..e8b516b9d5e 100644
--- a/studio/frontend/src/i18n/locales/zh-CN.ts
+++ b/studio/frontend/src/i18n/locales/zh-CN.ts
@@ -5385,6 +5385,7 @@ export const zhCN = {
generationStats: {
working: "正在处理...",
processingPrompt: "正在处理提示词 {percent}%",
+ compactingContext: "正在压缩上下文...",
timeLeft: "剩余 {eta}",
tokensOne: "{count} 个 token",
tokens: "{count} 个 token",
diff --git a/studio/frontend/src/i18n/locales/zh-TW.ts b/studio/frontend/src/i18n/locales/zh-TW.ts
index a6d38736488..02090c74953 100644
--- a/studio/frontend/src/i18n/locales/zh-TW.ts
+++ b/studio/frontend/src/i18n/locales/zh-TW.ts
@@ -5564,6 +5564,7 @@ export const zhTW = {
generationStats: {
working: "處理中...",
processingPrompt: "正在處理提示詞 {percent}%",
+ compactingContext: "正在壓縮上下文...",
timeLeft: "剩餘 {eta}",
tokensOne: "{count} 個 token",
tokens: "{count} 個 token",
diff --git a/studio/frontend/tests/generation-details.test.ts b/studio/frontend/tests/generation-details.test.ts
index cd90b9e2691..364a66f992a 100644
--- a/studio/frontend/tests/generation-details.test.ts
+++ b/studio/frontend/tests/generation-details.test.ts
@@ -4,12 +4,14 @@
// Live prefill / decode figures, the Generation details setting, and the loaded placement readout.
import assert from "node:assert/strict";
+import { register } from "node:module";
import test from "node:test";
import type { LiveDecodeProgress, LivePrefillProgress } from "../src/features/chat/stores/chat-runtime-store.ts";
import { readSrc, registerBundlerResolver } from "./helpers/kit.ts";
registerBundlerResolver();
+register("./thread-sampling-resolver.mjs", import.meta.url);
const {
formatLiveDecode,
formatPrefillDetails,
@@ -19,6 +21,7 @@ const {
prefillSecondsLeft,
withPrefillFit,
readGenerationProgress,
+ betweenToolRounds,
} = await import("../src/features/chat/lib/generation-progress.ts");
const { describePlacement, loadedPlacementFrom } = await import(
"../src/features/chat/lib/partial-offload.ts"
@@ -188,6 +191,76 @@ test("malformed frames are dropped rather than drawn as NaN", () => {
assert.equal(readGenerationProgress({ phase: "warmup" }), null);
});
+test("a compaction frame reads as its own phase and survives a persisted run", () => {
+ assert.deepEqual(readGenerationProgress({ phase: "compacting" }), { phase: "compacting" });
+ const frame = { type: "generation_progress", phase: "compacting" };
+ const normalized = normalizeChatGenerationChunkPayload(frame) as { _generationProgress?: unknown };
+ assert.deepEqual(normalized._generationProgress, frame);
+ assert.equal(generationChunkCountsTowardTiming(normalized), false);
+});
+
+test("compaction shows until the pass's context_truncated or first prefill sample, live or followed", async () => {
+ await import("./helpers/store-stubs/settings-http.ts");
+ await import("./helpers/store-stubs/chat-history-storage.ts");
+ const { useChatRuntimeStore } = await import("../src/features/chat/stores/chat-runtime-store.ts");
+ const runtime = useChatRuntimeStore.getState();
+ const live = () => useChatRuntimeStore.getState().liveGenerationByThreadId.t;
+ runtime.setLiveDecode("t", { tokens: 5, tokPerS: 9 } as LiveDecodeProgress);
+ runtime.setLiveCompacting("t", true);
+ assert.deepEqual(live(), { prefill: null, prefillShown: false, decode: null, compacting: true });
+ runtime.setLiveCompacting("t", false);
+ assert.equal(live()?.compacting, false);
+ runtime.setLiveCompacting("t", true);
+ runtime.setLivePrefill("t", { processed: 1, total: 10 } as LivePrefillProgress);
+ assert.notEqual(live()?.compacting, true);
+ runtime.setLiveCompacting("t", true);
+ runtime.setLiveDecode("t", { tokens: 6, tokPerS: 9 } as LiveDecodeProgress);
+ assert.notEqual(live()?.compacting, true);
+ const adapter = readSrc("features/chat/api/chat-adapter.ts");
+ assert.match(adapter, /update\?\.phase === "compacting"\) \{\s*runtime\.setLiveCompacting\(liveThreadKey\(serverCancel\), true\);/);
+ assert.match(adapter, /if \(chunk\.context_truncated\) \{\s*runtime\.setLiveCompacting\(liveThreadKey\(serverCancel\), false\);/);
+ const provider = readSrc("features/chat/runtime-provider.tsx");
+ assert.match(provider, /progress\?\.phase === "compacting"\) \{\s*store\.setLiveCompacting\(threadId, true\);/);
+ assert.match(provider, /if \(chunk\.context_truncated\) \{\s*useChatRuntimeStore\.getState\(\)\.setLiveCompacting\(threadId, false\);/);
+ const thread = readSrc("components/assistant-ui/thread.tsx");
+ const status = thread.slice(thread.indexOf("const GeneratingStatus"), thread.indexOf("// Placeholder when stop fires"));
+ assert.match(status, /t\("generationStats\.compactingContext"\)/);
+ assert.doesNotMatch(status, />\s*Compacting/);
+});
+
+test("a tool result drops the last pass's figures, live or followed, so the next round starts at Working", () => {
+ const adapter = readSrc("features/chat/api/chat-adapter.ts");
+ assert.match(
+ adapter,
+ /toolEvent\.type === "tool_end"\) \{\s*\/\/[^\n]*\n\s*runtime\.clearLiveGenerationForThread\(liveThreadKey\(serverCancel\)\);/,
+ );
+ const provider = readSrc("features/chat/runtime-provider.tsx");
+ assert.match(
+ provider,
+ /caughtUp\(update\.run\) &&\s*\(chunk as \{ _toolEvent\?: \{ type\?: unknown \} \}\)\._toolEvent\?\.type === "tool_end"\s*\) \{\s*useChatRuntimeStore\.getState\(\)\.clearLiveGenerationForThread\(threadId\);/,
+ );
+});
+
+test("the status line comes back between tool rounds, after the finished call", () => {
+ const thread = readSrc("components/assistant-ui/thread.tsx");
+ const indicator = thread.slice(thread.indexOf("const NextRoundIndicator"), thread.indexOf("// Mounted only under a running reply"));
+ assert.match(indicator, /betweenToolRounds\(message\.status\?\.type === "running", message\.content\)/);
+ assert.match(indicator, /<GeneratingStatus \/>/);
+ // After the parts, so it sits under the call it follows rather than above the reply.
+ assert.match(thread, /<MessagePrimitive\.Parts components=\{ASSISTANT_PART_COMPONENTS\} \/>\s*<NextRoundIndicator \/>/);
+});
+
+test("a finished call shows the next round's status, past the sources a recovered reply ends with", () => {
+ const done = { type: "tool-call", result: "ok" };
+ const source = { type: "source" };
+ assert.equal(betweenToolRounds(true, [done]), true);
+ assert.equal(betweenToolRounds(true, [done, source, source]), true);
+ assert.equal(betweenToolRounds(false, [done]), false);
+ assert.equal(betweenToolRounds(true, [{ type: "tool-call" }, source]), false, "still running");
+ assert.equal(betweenToolRounds(true, [done, { type: "text" }, source]), false, "the next pass streamed");
+ assert.equal(betweenToolRounds(true, [source]), false);
+});
+
test("decode frames keep draft acceptance only when both counts are there", () => {
const update = readGenerationProgress({ phase: "decode", tokens: 40, tok_per_s: 90, draft_n: 23, draft_accepted: 30 });
assert.equal(update?.phase, "decode");
diff --git a/studio/frontend/tests/generation-tool-recovery.test.ts b/studio/frontend/tests/generation-tool-recovery.test.ts
index 80905ba5d92..31dd955b17a 100644
--- a/studio/frontend/tests/generation-tool-recovery.test.ts
+++ b/studio/frontend/tests/generation-tool-recovery.test.ts
@@ -33,6 +33,9 @@ const { orderBySelectedBranch } = await import(
const { contextUsageMarkers } = await import(
"../src/features/chat/utils/saved-context-usage.ts"
);
+const { readGenerationProgress } = await import(
+ "../src/features/chat/lib/generation-progress.ts"
+);
const start = (id = "call_0") => ({
type: "tool_start",
@@ -495,6 +498,8 @@ async function recoverRun(
viewContent?: unknown[];
metadata?: Record<string, unknown>;
stallBefore?: number;
+ /** The real follower drops repeated seqs; a repeat would reach the publish a lone event skips. */
+ yieldOnce?: boolean;
} = {},
) {
class ChatGenerationStalledError extends Error {}
@@ -518,6 +523,10 @@ async function recoverRun(
// The real store has these; a stand-in missing them would make approval recovery look broken.
setToolConfirmation() {},
clearToolConfirmation() {},
+ setLivePrefill() {},
+ setLiveDecode() {},
+ setLiveCompacting() {},
+ clearLiveGenerationForThread() {},
};
const run = {
id: "run",
@@ -543,6 +552,7 @@ async function recoverRun(
...parser,
createGenerationToolRecovery,
contextUsageMarkers,
+ readGenerationProgress,
orderBySelectedBranch,
providerCompactionConnectionKey,
RUN_CHECKPOINT_INTERVAL_MS,
@@ -569,7 +579,7 @@ async function recoverRun(
},
};
yield update;
- yield update;
+ if (!options.yieldOnce) yield update;
}
if (options.stallBefore) throw new ChatGenerationStalledError();
},
@@ -682,6 +692,22 @@ test("the recovery scheduler persists provider compaction metadata", async () =>
);
});
+test("a progress frame that ends the replay shows the tool result it rebuilt", async () => {
+ for (const progress of [
+ { phase: "compacting" },
+ { phase: "prefill", processed: 10, total: 100 },
+ ]) {
+ const { snapshots } = await recoverRun(
+ [],
+ [{ _toolEvent: start() }, { _toolEvent: end() }, { _generationProgress: progress }],
+ { stallBefore: 3, yieldOnce: true },
+ );
+ assert.equal(snapshots.length, 2, `${progress.phase}: published before the stall`);
+ assert.equal(snapshots[0].metadata.generationSettled, false);
+ assert.equal(snapshots[0].content.at(-1)?.result, "ok");
+ }
+});
+
test("reopening a run that finished without the tab saves and renders only its end", async () => {
const payloads = Array.from({ length: 400 }, (_, index) => ({
choices: [{ delta: { content: `w${index} ` } }],