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: copy full prompts from the api monitor (#11340)

Maheswar Kumara9feb9512634
diff --git a/studio/backend/core/inference/api_monitor.py b/studio/backend/core/inference/api_monitor.py
index d27d92bd092..44ef860cff9 100644
--- a/studio/backend/core/inference/api_monitor.py
+++ b/studio/backend/core/inference/api_monitor.py
@@ -10,6 +10,7 @@ import json
import logging
import math
import os
+import sys
import threading
import time
import uuid
@@ -32,7 +33,7 @@ TerminalCallback = Callable[[ApiUsageReceipt], None]
_MAX_ENTRIES = 50
-_MAX_PROMPT_CHARS = 12000
+_MAX_PROMPT_BYTES = 64 * 1024 * 1024
_MAX_REPLY_CHARS = 12000
_PREVIEW_CHARS = 360
_MAX_STREAM_TOOL_CALLS = 64
@@ -69,6 +70,12 @@ def _finite_float_or_none(value: Any) -> Optional[float]:
return number if math.isfinite(number) else None
+def _advance_updated_at(entry: "ApiMonitorEntry", now: Optional[float] = None) -> None:
+ """Advance the row's freshness key even on a coarse or regressing wall clock."""
+ observed = time.time() if now is None else now
+ entry.updated_at = max(observed, math.nextafter(entry.updated_at, math.inf))
+
+
def _trim(text: Optional[str], limit: int) -> str:
if not text:
return ""
@@ -216,11 +223,13 @@ class ApiMonitorEntry:
openai_stream_tool_calls: list[_OpenAIStreamToolCall] = field(default_factory = list)
openai_stream_last_tool_indexes: dict[int, int] = field(default_factory = dict)
openai_stream_last_segment_was_tool: bool = False
+ prompt_complete: bool = True
def snapshot(
self,
*,
include_details: bool = True,
+ include_prompt: bool = True,
attributed: bool = True,
) -> dict[str, Any]:
duration_ms = None
@@ -264,7 +273,7 @@ class ApiMonitorEntry:
"via_api_key": self.via_api_key and attributed,
"prompt_preview": _trim(self.prompt, _PREVIEW_CHARS),
"reply_preview": _trim(self.reply, _PREVIEW_CHARS),
- "prompt_truncated": len(self.prompt) > _PREVIEW_CHARS,
+ "prompt_truncated": not self.prompt_complete or len(self.prompt) > _PREVIEW_CHARS,
"reply_truncated": len(self.reply) > _PREVIEW_CHARS,
"status": self.status,
"started_at": self.started_at,
@@ -292,7 +301,8 @@ class ApiMonitorEntry:
"stop_reason": self.stop_reason,
}
if include_details:
- payload["prompt"] = self.prompt
+ if include_prompt and self.prompt_complete:
+ payload["prompt"] = self.prompt
payload["reply"] = self.reply
return payload
@@ -374,7 +384,7 @@ class ApiMonitor:
method = method,
# str(): a raw JSON body can carry any type, and a non-string breaks the UI.
model = str(model) if model else "default",
- prompt = _trim(prompt, _MAX_PROMPT_CHARS),
+ prompt = prompt or "",
status = "running",
started_at = now,
updated_at = now,
@@ -386,6 +396,16 @@ class ApiMonitor:
with self._lock:
self._entries.appendleft(entry)
self._trim_terminal_locked()
+ remaining = _MAX_PROMPT_BYTES
+ for retained in self._entries:
+ if not retained.prompt_complete or len(retained.prompt) <= _PREVIEW_CHARS:
+ continue
+ prompt_bytes = sys.getsizeof(retained.prompt)
+ if prompt_bytes <= remaining:
+ remaining -= prompt_bytes
+ else:
+ retained.prompt = _trim(retained.prompt, _PREVIEW_CHARS)
+ retained.prompt_complete = False
return entry.id
def record_lifecycle(
@@ -447,7 +467,7 @@ class ApiMonitor:
entry = self._find_locked(entry_id)
if entry is not None:
entry.model = model
- entry.updated_at = time.time()
+ _advance_updated_at(entry)
def set_progress(self, entry_id: Optional[str], progress: Optional[float]) -> None:
"""Update an open download row's percentage (clamped to 0-100)."""
@@ -457,7 +477,7 @@ class ApiMonitor:
entry = self._find_locked(entry_id)
if entry is not None and entry.status == "running":
entry.progress = min(100.0, max(0.0, float(progress)))
- entry.updated_at = time.time()
+ _advance_updated_at(entry)
def discard(self, entry_id: Optional[str]) -> None:
"""Drop a row that turned out not to be an event (an already-satisfied load)."""
@@ -499,10 +519,10 @@ class ApiMonitor:
if len(entry.reply) >= _MAX_REPLY_CHARS:
if not entry.reply.endswith("..."):
entry.reply = _trim(entry.reply + text, _MAX_REPLY_CHARS)
- entry.updated_at = time.time()
+ _advance_updated_at(entry)
return
entry.reply = _trim(entry.reply + text, _MAX_REPLY_CHARS)
- entry.updated_at = time.time()
+ _advance_updated_at(entry)
def accumulate_openai_tool_call(
self,
@@ -601,7 +621,7 @@ class ApiMonitor:
entry.first_token_monotonic = now
if entry.first_decode_monotonic is None:
entry.first_decode_monotonic = now
- entry.updated_at = time.time()
+ _advance_updated_at(entry)
def take_openai_tool_calls(
self,
@@ -642,7 +662,7 @@ class ApiMonitor:
entry.openai_stream_last_tool_indexes[choice_index] = remaining_indexes[
choice_index
]
- entry.updated_at = time.time()
+ _advance_updated_at(entry)
return [(state.name, state.arguments) for state in selected], separate
def mark_first_token(
@@ -676,7 +696,7 @@ class ApiMonitor:
if entry is None:
return
entry.reply = _trim(text, _MAX_REPLY_CHARS)
- entry.updated_at = time.time()
+ _advance_updated_at(entry)
def set_perf(
self,
@@ -711,7 +731,7 @@ class ApiMonitor:
entry.decode_ms = decode_ms
if stop_reason is not None:
entry.stop_reason = str(stop_reason)
- entry.updated_at = time.time()
+ _advance_updated_at(entry)
def note_stop_reason(self, entry_id: Optional[str], reason: Optional[str]) -> None:
"""Record one choice's finish reason, without publishing it yet.
@@ -728,7 +748,7 @@ class ApiMonitor:
if entry is None:
return
entry.stop_reasons_seen.add(str(reason))
- entry.updated_at = time.time()
+ _advance_updated_at(entry)
@staticmethod
def _settle_stop_reason_locked(entry: ApiMonitorEntry, completed: bool) -> None:
@@ -781,7 +801,7 @@ class ApiMonitor:
entry.total_tokens = (entry.prompt_tokens or 0) + (entry.completion_tokens or 0)
if context_length is not None:
entry.context_length = context_length
- entry.updated_at = time.time()
+ _advance_updated_at(entry)
def finish(
self,
@@ -805,7 +825,7 @@ class ApiMonitor:
self._settle_stop_reason_locked(entry, status == "completed")
now = time.time()
entry.status = status
- entry.updated_at = now
+ _advance_updated_at(entry, now)
entry.finished_at = now
entry.finished_monotonic = time.monotonic()
self._entries.remove(entry)
@@ -840,6 +860,7 @@ class ApiMonitor:
# Already terminal; refresh error text only.
if error:
entry.error = _trim(error, 1000)
+ _advance_updated_at(entry)
return
self._fail_locked(entry, error)
notification = self._terminal_notification_locked(entry)
@@ -854,7 +875,7 @@ class ApiMonitor:
now = time.time()
entry.status = "error"
entry.error = _trim(error, 1000)
- entry.updated_at = now
+ _advance_updated_at(entry, now)
entry.finished_at = now
entry.finished_monotonic = time.monotonic()
self._entries.remove(entry)
@@ -935,6 +956,7 @@ class ApiMonitor:
entry_id: str,
*,
subject: Optional[str] = None,
+ include_prompt: bool = True,
) -> Optional[dict[str, Any]]:
with self._lock:
entry = self._find_locked(entry_id)
@@ -944,6 +966,7 @@ class ApiMonitor:
return None
return entry.snapshot(
include_details = True,
+ include_prompt = include_prompt,
attributed = self._attributed(entry, subject),
)
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 9987d58778b..d539a3709d4 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -18503,9 +18503,13 @@ async def clear_api_monitor(current_subject: str = Depends(get_current_subject))
@studio_router.get("/monitor/{entry_id}")
-async def get_api_monitor_entry(entry_id: str, current_subject: str = Depends(get_current_subject)):
+async def get_api_monitor_entry(
+ entry_id: str,
+ current_subject: str = Depends(get_current_subject),
+ include_prompt: bool = True,
+):
"""Return full prompt/reply details for one OpenAI-compatible API request."""
- entry = api_monitor.get(entry_id, subject = current_subject)
+ entry = api_monitor.get(entry_id, subject = current_subject, include_prompt = include_prompt)
if entry is None:
raise HTTPException(status_code = 404, detail = "Monitor entry not found")
return entry
diff --git a/studio/backend/tests/test_api_monitor_full_prompt.py b/studio/backend/tests/test_api_monitor_full_prompt.py
new file mode 100644
index 00000000000..efd2e5e07db
--- /dev/null
+++ b/studio/backend/tests/test_api_monitor_full_prompt.py
@@ -0,0 +1,149 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import sys
+
+import pytest
+
+import core.inference.api_monitor as monitor_module
+from core.inference.api_monitor import ApiMonitor
+from utils.account_context import AccountContext, run_as
+
+
+def _start(monitor, prompt):
+ return monitor.start(
+ endpoint = "/v1/chat/completions",
+ method = "POST",
+ model = "test-model",
+ prompt = prompt,
+ subject = "alice",
+ )
+
+
[email protected]("length", [0, 359, 360, 361, 11999, 12000, 12001, 1000000])
[email protected]("character", ["x", "🦥"])
+def test_full_prompt_survives_detail_reads(length, character):
+ prompt = (character * (length - 6) + "\nEND\t ") if length else ""
+ monitor = ApiMonitor()
+ entry_id = _start(monitor, prompt)
+
+ for completed in (False, True):
+ if completed:
+ monitor.finish(entry_id)
+ detail = monitor.get(entry_id, subject = "alice")
+ assert detail["prompt"] == prompt
+ [summary] = monitor.snapshot(include_details = False, subject = "alice")
+ assert "prompt" not in summary
+ assert len(summary["prompt_preview"]) <= 360
+ assert summary["prompt_truncated"] is (len(prompt) > 360)
+
+
+def test_full_prompt_remains_account_and_subject_scoped():
+ monitor = ApiMonitor()
+ alice = AccountContext("alice-id", "alice")
+ replacement = AccountContext("replacement-id", "alice")
+ prompt = "private prompt\n" * 2000
+ entry_id = run_as(alice, _start, monitor, prompt)
+
+ assert run_as(alice, monitor.get, entry_id, subject = "alice")["prompt"] == prompt
+ assert run_as(alice, monitor.get, entry_id, subject = "bob") is None
+ assert run_as(replacement, monitor.get, entry_id, subject = "alice") is None
+ assert run_as(replacement, monitor.snapshot, subject = "alice") == []
+
+
+def test_full_prompt_follows_history_retention_and_clear():
+ monitor = ApiMonitor(max_entries = 1)
+ prompt = "long prompt\n" * 2000
+ running_id = _start(monitor, prompt)
+ old_id = _start(monitor, prompt + "old")
+ monitor.finish(old_id)
+ recent_id = _start(monitor, prompt + "recent")
+ monitor.finish(recent_id)
+
+ assert monitor.get(old_id, subject = "alice") is None
+ assert monitor.get(running_id, subject = "alice")["prompt"] == prompt
+ assert monitor.get(recent_id, subject = "alice")["prompt"] == prompt + "recent"
+ monitor.clear(subject = "alice")
+ assert monitor.get(recent_id, subject = "alice") is None
+ assert monitor.get(running_id, subject = "alice")["prompt"] == prompt
+ monitor.finish(running_id)
+ monitor.clear(subject = "alice")
+ assert monitor.snapshot(subject = "alice") == []
+
+
[email protected]("character", ["x", "🦥"])
[email protected]("terminal", [None, "finish", "fail"])
+def test_full_prompt_budget_preserves_rows_and_recent_details(monkeypatch, character, terminal):
+ prompt = character * 2000
+ monkeypatch.setattr(
+ monitor_module, "_MAX_PROMPT_BYTES", 2 * sys.getsizeof(prompt), raising = False
+ )
+ monitor = ApiMonitor()
+ ids = []
+ for _ in range(3):
+ entry_id = _start(monitor, prompt)
+ ids.append(entry_id)
+ if terminal == "finish":
+ monitor.finish(entry_id)
+ elif terminal == "fail":
+ monitor.fail(entry_id, "context limit")
+
+ oldest = monitor.get(ids[0], subject = "alice")
+ assert "prompt" not in oldest
+ assert oldest["prompt_truncated"] is True
+ assert len(oldest["prompt_preview"]) == 360
+ for entry_id in ids[1:]:
+ assert monitor.get(entry_id, subject = "alice")["prompt"] == prompt
+ assert len(monitor.snapshot(subject = "alice")) == 3
+ assert monitor.active_count(subject = "alice") == (3 if terminal is None else 0)
+
+
+def test_oversized_prompt_keeps_preview_without_evicting_smaller_details(monkeypatch):
+ prompt = "x" * 2000
+ monkeypatch.setattr(monitor_module, "_MAX_PROMPT_BYTES", sys.getsizeof(prompt), raising = False)
+ monitor = ApiMonitor()
+ retained = _start(monitor, prompt)
+ oversized = _start(monitor, prompt * 2)
+ monitor.set_reply(oversized, "reply remains available")
+
+ assert monitor.get(retained)["prompt"] == prompt
+ detail = monitor.get(oversized)
+ assert "prompt" not in detail
+ assert detail["prompt_truncated"] is True
+ assert detail["reply"] == "reply remains available"
+ assert len(detail["prompt_preview"]) == 360
+
+
+def test_eviction_releases_prompt_budget_for_new_requests(monkeypatch):
+ prompt = "x" * 2000
+ monkeypatch.setattr(monitor_module, "_MAX_PROMPT_BYTES", sys.getsizeof(prompt), raising = False)
+ monitor = ApiMonitor(max_entries = 1)
+ first = _start(monitor, prompt)
+ monitor.finish(first)
+ second = _start(monitor, prompt)
+ monitor.finish(second)
+ assert monitor.get(first) is None
+ assert monitor.get(second)["prompt"] == prompt
+ monitor.clear()
+ third = _start(monitor, prompt)
+ assert monitor.get(third)["prompt"] == prompt
+
+
+def test_reply_refresh_can_omit_immutable_prompt(monkeypatch):
+ monkeypatch.setattr(monitor_module.time, "time", lambda: 1_700_000_000.0)
+ monitor = ApiMonitor()
+ prompt = "long prompt\n" * 2000
+ entry_id = _start(monitor, prompt)
+ first = monitor.get(entry_id, subject = "alice")
+ for chunk in ("first", " second", " third"):
+ monitor.append_reply(entry_id, chunk)
+ refresh = monitor.get(entry_id, subject = "alice", include_prompt = False)
+ full = monitor.get(entry_id, subject = "alice")
+ assert "prompt" not in refresh
+ assert refresh == {key: value for key, value in full.items() if key != "prompt"}
+ assert full["prompt"] == prompt
+ assert refresh["reply"] == "first second third"
+ assert refresh["updated_at"] > first["updated_at"]
+ assert monitor.get(entry_id, subject = "bob", include_prompt = False) is None
+ monitor.finish(entry_id)
+ assert monitor.get(entry_id, include_prompt = False)["status"] == "completed"
diff --git a/studio/frontend/src/features/api-monitor/api-monitor-page.tsx b/studio/frontend/src/features/api-monitor/api-monitor-page.tsx
index 0df28027d62..77f447dc3dd 100644
--- a/studio/frontend/src/features/api-monitor/api-monitor-page.tsx
+++ b/studio/frontend/src/features/api-monitor/api-monitor-page.tsx
@@ -349,6 +349,10 @@ function PayloadBlock({
loading?: boolean;
tone?: "error";
}): ReactElement {
+ const preview =
+ body.length > 12_000
+ ? `${body.slice(0, 11_997).replace(/[\uD800-\uDBFF]$/, "")}...`
+ : body;
return (
<section className="flex min-w-0 flex-col gap-1.5">
<div className="flex items-center justify-between gap-2">
@@ -356,12 +360,12 @@ function PayloadBlock({
{title}
</h3>
<div className="flex items-center gap-1">
- {truncated ? (
+ {truncated || preview !== body ? (
<span className="text-ui-10 text-muted-foreground">
preview only
</span>
) : null}
- {body ? (
+ {body && !truncated ? (
<CopyButton value={body} label={`Copy ${title.toLowerCase()}`} />
) : null}
</div>
@@ -374,7 +378,7 @@ function PayloadBlock({
tone === "error" && "bg-red-500/5 text-red-700 dark:text-red-400",
)}
>
- {loading && !body ? "Loading…" : body || "–"}
+ {loading && !body ? "Loading…" : preview || "–"}
</pre>
</section>
);
diff --git a/studio/frontend/src/features/api-monitor/use-api-monitor.ts b/studio/frontend/src/features/api-monitor/use-api-monitor.ts
index 21031bca996..3394c614f40 100644
--- a/studio/frontend/src/features/api-monitor/use-api-monitor.ts
+++ b/studio/frontend/src/features/api-monitor/use-api-monitor.ts
@@ -16,6 +16,7 @@ import { type MonitorStats, computeStats } from "./stats";
/** Poll cadence while live. Matches the settings console it replaces. */
const POLL_INTERVAL_MS = 1500;
+const MAX_CACHED_PROMPT_CHARS = 64 * 1024 * 1024;
export { computeStats };
export type { MonitorStats };
@@ -27,6 +28,42 @@ export type MonitorStatusFilter =
| "error"
| "cancelled";
+function retainDetail(
+ previous: Record<string, ApiMonitorEntry>,
+ id: string,
+ entry: ApiMonitorEntry,
+ cachedPrompt: string | undefined,
+): Record<string, ApiMonitorEntry> {
+ const refreshed =
+ cachedPrompt == null ? entry : { ...entry, prompt: cachedPrompt };
+ const retained: Record<string, ApiMonitorEntry> = { [id]: refreshed };
+ let promptChars = refreshed.prompt?.length ?? 0;
+ for (const [cachedId, cached] of Object.entries(previous)) {
+ if (cachedId === id) {
+ continue;
+ }
+ const cachedChars = cached.prompt?.length ?? 0;
+ if (promptChars + cachedChars > MAX_CACHED_PROMPT_CHARS) {
+ continue;
+ }
+ retained[cachedId] = cached;
+ promptChars += cachedChars;
+ }
+ return retained;
+}
+
+function dropDetail(
+ previous: Record<string, ApiMonitorEntry>,
+ id: string,
+): Record<string, ApiMonitorEntry> {
+ if (!(id in previous)) {
+ return previous;
+ }
+ const next = { ...previous };
+ delete next[id];
+ return next;
+}
+
export function filterEntries(
entries: ApiMonitorEntry[],
status: MonitorStatusFilter,
@@ -94,20 +131,49 @@ export function useApiMonitor({
);
// Mirrors `loadingDetails` outside React state so the guard sees same-tick writes.
const inFlightDetails = useRef<Set<string>>(new Set());
+ const retainedEntryIds = useRef<Set<string>>(new Set());
+ const listRequestGeneration = useRef(0);
+ const manualLoadGeneration = useRef(0);
+ const detailRequestGeneration = useRef(0);
+
+ const updateData = useCallback((next: ApiMonitorResponse): void => {
+ const ids = new Set(next.entries.map((entry) => entry.id));
+ retainedEntryIds.current = ids;
+ setData(next);
+ setDetails((previous) => {
+ const expired = Object.keys(previous).filter((id) => !ids.has(id));
+ if (expired.length === 0) return previous;
+ const retained = { ...previous };
+ for (const id of expired) delete retained[id];
+ return retained;
+ });
+ }, []);
const load = useCallback(async (): Promise<void> => {
+ const requestGeneration = ++listRequestGeneration.current;
+ const manualGeneration = ++manualLoadGeneration.current;
setRefreshing(true);
try {
const next = await getApiMonitor();
- setData(next);
+ if (listRequestGeneration.current !== requestGeneration) {
+ return;
+ }
+ updateData(next);
setError(null);
} catch (err: unknown) {
+ if (listRequestGeneration.current !== requestGeneration) {
+ return;
+ }
setError(err instanceof Error ? err.message : "Monitor unavailable");
} finally {
- setRefreshing(false);
- setLoading(false);
+ if (manualLoadGeneration.current === manualGeneration) {
+ setRefreshing(false);
+ }
+ if (listRequestGeneration.current === requestGeneration) {
+ setLoading(false);
+ }
}
- }, []);
+ }, [updateData]);
useEffect(() => {
if (paused) {
@@ -117,19 +183,34 @@ export function useApiMonitor({
let timer: number | undefined;
function poll(): void {
+ const requestGeneration = ++listRequestGeneration.current;
getApiMonitor()
.then((next) => {
- if (cancelled) return;
- setData(next);
+ if (
+ cancelled ||
+ listRequestGeneration.current !== requestGeneration
+ ) {
+ return;
+ }
+ updateData(next);
setError(null);
})
.catch((err: unknown) => {
- if (cancelled) return;
+ if (
+ cancelled ||
+ listRequestGeneration.current !== requestGeneration
+ ) {
+ return;
+ }
setError(err instanceof Error ? err.message : "Monitor unavailable");
})
.finally(() => {
- if (cancelled) return;
- setLoading(false);
+ if (cancelled) {
+ return;
+ }
+ if (listRequestGeneration.current === requestGeneration) {
+ setLoading(false);
+ }
timer = window.setTimeout(poll, intervalMs);
});
}
@@ -141,39 +222,51 @@ export function useApiMonitor({
window.clearTimeout(timer);
}
};
- }, [paused, intervalMs]);
+ }, [paused, intervalMs, updateData]);
// Returns whether a fetch started: recording "fetched revision N" when the guard
// refused would skip that revision once updated_at settles.
- const requestDetail = useCallback((id: string): boolean => {
- if (inFlightDetails.current.has(id)) {
- return false;
- }
- inFlightDetails.current.add(id);
- setLoadingDetails((prev) => new Set(prev).add(id));
- getApiMonitorEntry(id)
- .then((entry) => {
- setDetails((prev) => ({ ...prev, [id]: entry }));
- })
- .catch(() => {
- // Aged out of the ring buffer: drop the stale copy so the row previews show.
- setDetails((prev) => {
- if (!(id in prev)) return prev;
- const next = { ...prev };
- delete next[id];
- return next;
- });
- })
- .finally(() => {
- inFlightDetails.current.delete(id);
- setLoadingDetails((prev) => {
- const next = new Set(prev);
- next.delete(id);
- return next;
+ const requestDetail = useCallback(
+ (id: string): boolean => {
+ if (inFlightDetails.current.has(id)) {
+ return false;
+ }
+ inFlightDetails.current.add(id);
+ const requestGeneration = detailRequestGeneration.current;
+ setLoadingDetails((prev) => new Set(prev).add(id));
+ const cachedPrompt = details[id]?.prompt;
+ getApiMonitorEntry(id, cachedPrompt == null)
+ .then((entry) => {
+ if (
+ detailRequestGeneration.current !== requestGeneration ||
+ !retainedEntryIds.current.has(id)
+ ) {
+ return;
+ }
+ setDetails((prev) => retainDetail(prev, id, entry, cachedPrompt));
+ })
+ .catch(() => {
+ if (detailRequestGeneration.current !== requestGeneration) {
+ return;
+ }
+ // Aged out of the ring buffer: drop the stale copy so the row previews show.
+ setDetails((prev) => dropDetail(prev, id));
+ })
+ .finally(() => {
+ if (detailRequestGeneration.current !== requestGeneration) {
+ return;
+ }
+ inFlightDetails.current.delete(id);
+ setLoadingDetails((prev) => {
+ const next = new Set(prev);
+ next.delete(id);
+ return next;
+ });
});
- });
- return true;
- }, []);
+ return true;
+ },
+ [details],
+ );
// The Clear log button discards this promise, so a failed DELETE has to land in the
// error banner here: rethrowing leaves an unhandled rejection and a log that silently
@@ -182,7 +275,14 @@ export function useApiMonitor({
(): Promise<void> =>
clearMonitor({
clearRemote: clearApiMonitor,
- resetDetails: () => setDetails({}),
+ resetDetails: () => {
+ listRequestGeneration.current += 1;
+ detailRequestGeneration.current += 1;
+ retainedEntryIds.current = new Set();
+ inFlightDetails.current.clear();
+ setLoadingDetails(new Set());
+ setDetails({});
+ },
reload: load,
onError: setError,
}),
diff --git a/studio/frontend/src/features/chat/api/chat-api.ts b/studio/frontend/src/features/chat/api/chat-api.ts
index 4ef2e9c4f88..97fafde32f5 100644
--- a/studio/frontend/src/features/chat/api/chat-api.ts
+++ b/studio/frontend/src/features/chat/api/chat-api.ts
@@ -226,9 +226,12 @@ export async function getApiMonitor(): Promise<ApiMonitorResponse> {
return parseJsonOrThrow<ApiMonitorResponse>(response);
}
-export async function getApiMonitorEntry(id: string): Promise<ApiMonitorEntry> {
+export async function getApiMonitorEntry(
+ id: string,
+ includePrompt = true,
+): Promise<ApiMonitorEntry> {
const response = await authFetch(
- `/api/inference/monitor/${encodeURIComponent(id)}`,
+ `/api/inference/monitor/${encodeURIComponent(id)}${includePrompt ? "" : "?include_prompt=false"}`,
);
return parseJsonOrThrow<ApiMonitorEntry>(response);
}
diff --git a/studio/frontend/tests/api-monitor-request-ordering.test.ts b/studio/frontend/tests/api-monitor-request-ordering.test.ts
new file mode 100644
index 00000000000..b76a6938641
--- /dev/null
+++ b/studio/frontend/tests/api-monitor-request-ordering.test.ts
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { readSrc } from "./helpers/kit.ts";
+
+const USE_API_MONITOR = readSrc("features/api-monitor/use-api-monitor.ts");
+const LIST_GENERATION_GUARD =
+ /const requestGeneration = \+\+listRequestGeneration\.current;[\s\S]{0,300}?listRequestGeneration\.current !== requestGeneration/;
+const CLEAR_INVALIDATION =
+ /detailRequestGeneration\.current \+= 1;[\s\S]{0,200}?retainedEntryIds\.current = new Set\(\);/;
+const DETAIL_GENERATION_GUARD =
+ /detailRequestGeneration\.current !== requestGeneration \|\|[\s\S]{0,100}?!retainedEntryIds\.current\.has\(id\)/;
+
+function deferred<T>() {
+ let resolve!: (value: T) => void;
+ const promise = new Promise<T>((done) => {
+ resolve = done;
+ });
+ return { promise, resolve };
+}
+
+test("only the newest monitor list response publishes", async () => {
+ let generation = 0;
+ let published = "";
+ const load = async (response: Promise<string>) => {
+ const requestGeneration = ++generation;
+ const value = await response;
+ if (generation !== requestGeneration) {
+ return;
+ }
+ published = value;
+ };
+ const older = deferred<string>();
+ const newer = deferred<string>();
+ const first = load(older.promise);
+ const second = load(newer.promise);
+
+ newer.resolve("new");
+ await second;
+ older.resolve("old");
+ await first;
+
+ assert.equal(published, "new");
+ assert.match(USE_API_MONITOR, LIST_GENERATION_GUARD);
+});
+
+test("clearing invalidates details that were already in flight", async () => {
+ let generation = 0;
+ const details: string[] = [];
+ const pending = deferred<string>();
+ const requestGeneration = generation;
+ const receive = pending.promise.then((value) => {
+ if (generation !== requestGeneration) {
+ return;
+ }
+ details.push(value);
+ });
+
+ generation += 1;
+ details.length = 0;
+ pending.resolve("late secret");
+ await receive;
+
+ assert.deepEqual(details, []);
+ assert.match(USE_API_MONITOR, CLEAR_INVALIDATION);
+ assert.match(USE_API_MONITOR, DETAIL_GENERATION_GUARD);
+});