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.

Fix chat context counts and token usage details (#14076)

Eyera2fd608deb8f2
diff --git a/studio/backend/core/inference/checkpoint.py b/studio/backend/core/inference/checkpoint.py
index 5df1e3a476b..161b3ce25ac 100644
--- a/studio/backend/core/inference/checkpoint.py
+++ b/studio/backend/core/inference/checkpoint.py
@@ -509,6 +509,7 @@ def fit_checkpoint_context(
headroom_ratio: Optional[float] = None,
force_reset: bool = False,
sticky_manual: bool = False,
+ replay_only: bool = False,
) -> tuple[list[dict], Optional[dict[str, Any]]]:
"""Fit a chat by resetting the epoch, keeping the newest turn and a carried-forward X.
@@ -593,6 +594,20 @@ def fit_checkpoint_context(
fitted = candidate
dropped = replayed
+ if replay_only:
+ projected, block = _project(fitted) if dropped else (messages, "")
+ current_tokens = count_tokens(projected) if dropped else initial_tokens
+ return projected, {
+ "dropped_messages": dropped,
+ "prompt_tokens_before": initial_tokens,
+ "prompt_tokens_after": current_tokens,
+ "context_length": context_length,
+ "fits": current_tokens <= prompt_target,
+ "checkpoint": True,
+ "checkpoint_started": False,
+ "carried_forward_chars": len(block),
+ }
+
projected, block = _project(fitted)
current_tokens = count_tokens(projected)
# What `current_tokens` prices, tracked separately because `projected` is rebound below on a path that does not
diff --git a/studio/backend/core/inference/context_window.py b/studio/backend/core/inference/context_window.py
index 8ddbee5ef56..b0111074008 100644
--- a/studio/backend/core/inference/context_window.py
+++ b/studio/backend/core/inference/context_window.py
@@ -831,6 +831,7 @@ def fit_rolling_context(
keeps_boundary: bool = False,
headroom_ratio: Optional[float] = None,
estimate_message: Callable[[dict], int] = estimate_message_tokens,
+ replay_only: bool = False,
) -> tuple[list[dict], Optional[dict[str, Any]]]:
"""Fit a chat into its context by dropping oldest complete turns; the current turn is never
clipped.
@@ -864,6 +865,15 @@ def fit_rolling_context(
dropped_total = dropped
current_tokens = count_tokens(fitted)
+ if replay_only:
+ return fitted, {
+ "dropped_messages": dropped_total,
+ "prompt_tokens_before": initial_tokens,
+ "prompt_tokens_after": current_tokens,
+ "context_length": context_length,
+ "fits": current_tokens <= prompt_target,
+ }
+
# Phase two: take a chunk out rather than skimming to the brim, so the boundary can stay put.
trim_target = prompt_target
# Keyed on the ratio, not `headroom`, which is zeroed for threadless and incognito requests that chose nothing.
diff --git a/studio/backend/core/inference/external_provider.py b/studio/backend/core/inference/external_provider.py
index 5de4f935ae5..0f6450e5f95 100644
--- a/studio/backend/core/inference/external_provider.py
+++ b/studio/backend/core/inference/external_provider.py
@@ -3879,7 +3879,9 @@ class ExternalProviderClient:
)
delta_usage = event.get("usage")
if isinstance(delta_usage, dict):
- last_usage.update(delta_usage)
+ last_usage.update(
+ {k: v for k, v in delta_usage.items() if v is not None}
+ )
# Compaction iterations are not in top-level input/output_tokens; fold them into
# compaction_{input,output}_tokens for billing.
iterations = delta_usage.get("iterations")
@@ -8225,6 +8227,27 @@ def _build_usage_chunk(
# OpenAI-shaped totals so monitors and cost consumers do not undercount the turn.
compaction_input = last_usage.get("compaction_input_tokens") or 0
compaction_output = last_usage.get("compaction_output_tokens") or 0
+ context_tokens = uncached_input + cache_creation + cache_read + completion_tokens
+ # Top-level counts sum every sampling iteration (server tools add some); the last one is the context.
+ iterations = last_usage.get("iterations")
+ final = next(
+ (
+ it
+ for it in reversed(iterations if isinstance(iterations, list) else [])
+ if isinstance(it, dict) and it.get("type") in ("message", "fallback_message")
+ ),
+ None,
+ )
+ if final is not None:
+ context_tokens = sum(
+ int(final.get(field) or 0)
+ for field in (
+ "input_tokens",
+ "cache_creation_input_tokens",
+ "cache_read_input_tokens",
+ "output_tokens",
+ )
+ )
prompt_tokens = uncached_input + cache_creation + cache_read + compaction_input
completion_tokens += compaction_output
if not (prompt_tokens or completion_tokens):
@@ -8233,6 +8256,7 @@ def _build_usage_chunk(
"prompt_tokens": prompt_tokens,
"completion_tokens": completion_tokens,
"total_tokens": prompt_tokens + completion_tokens,
+ "context_tokens": context_tokens,
"prompt_tokens_details": {"cached_tokens": cache_read},
"cache_creation_input_tokens": cache_creation,
"cache_read_input_tokens": cache_read,
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index 706dbde8cce..cfebc9b733f 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -248,6 +248,14 @@ def _archive_is_degraded() -> bool:
return False
+def _archive_write_failed() -> bool:
+ try:
+ from core.rag import conversation_archive
+ return bool(conversation_archive.degraded())
+ except Exception: # noqa: BLE001 -- an unreadable flag is "healthy", never an error
+ return False
+
+
def _compaction_fit_kwargs(
context_policy: Optional[str] = None, compaction_headroom_ratio: Optional[float] = None
) -> dict:
@@ -298,6 +306,9 @@ def _fit_context(messages, **kwargs):
# /compact: only a checkpoint reset can honour it, since only its turns are archived.
force_reset = bool(kwargs.pop("force_reset", False))
sticky_manual = bool(kwargs.pop("sticky_manual", False))
+ replay_only = bool(kwargs.pop("replay_only", False))
+ if replay_only and int(kwargs.get("sticky_dropped") or 0) <= 0:
+ return messages, None
if kwargs.get("count_tokens") is not None:
try:
messages = fit_oversized_attachments(
@@ -308,6 +319,23 @@ def _fit_context(messages, **kwargs):
)
except Exception: # noqa: BLE001 -- the fit below still refuses what cannot fit
logger.debug("attachment fit failed", exc_info = True)
+ if replay_only:
+ if sticky_is_checkpoint:
+ from core.inference import checkpoint
+
+ # Generation's wording: unsearchable without a reset, else the last write's verdict
+ # (not the live probe, which runs an embedding forward per count).
+ return checkpoint.fit_checkpoint_context(
+ messages,
+ can_reset = False,
+ searchable = (lambda: recall_offered and not _archive_write_failed())
+ if can_reset
+ else False,
+ sticky_manual = sticky_manual,
+ replay_only = True,
+ **kwargs,
+ )
+ return fit_rolling_context(messages, replay_only = True, **kwargs)
try:
from core.inference import checkpoint
@@ -449,7 +477,8 @@ def _fit_with_instruction_pins(
# prompt WITH the standing instruction for one without, the very thing this seam
# prevents. `dropped_messages` separates them.
if (
- pins
+ not kwargs.get("replay_only")
+ and pins
and truncation
and not truncation.get("fits")
and not int(truncation.get("dropped_messages") or 0)
diff --git a/studio/backend/core/inference/studio_tool_loop.py b/studio/backend/core/inference/studio_tool_loop.py
index 676b797a55a..22b19a0b959 100644
--- a/studio/backend/core/inference/studio_tool_loop.py
+++ b/studio/backend/core/inference/studio_tool_loop.py
@@ -585,6 +585,7 @@ class _Turn:
# result twice
hosted_results: dict[str, dict[str, Any]] = field(default_factory = dict)
provider_compaction: dict[str, Any] | None = None
+ usage: dict[str, Any] | None = None
def note_reasoning_extra(self, extra: Any) -> None:
"""Accumulate Gemini's per-part replay envelopes across one provider turn."""
@@ -1263,7 +1264,7 @@ def _merge_usage(totals: dict[str, Any], usage: Any) -> None:
if not isinstance(usage, dict):
return
for field, value in usage.items():
- if isinstance(value, bool):
+ if isinstance(value, bool) or field == "context_tokens":
continue
if isinstance(value, int):
totals[field] = totals.get(field, 0) + value
@@ -1278,6 +1279,34 @@ def _merge_usage(totals: dict[str, Any], usage: Any) -> None:
bucket[detail] = bucket.get(detail, 0) + count
+def _update_usage_snapshot(snapshot: dict[str, Any], usage: dict[str, Any]) -> None:
+ for field, value in usage.items():
+ if isinstance(value, bool):
+ continue
+ if isinstance(value, int):
+ snapshot[field] = value
+ elif field in _USAGE_DETAIL_FIELDS and isinstance(value, dict):
+ bucket = snapshot.setdefault(field, {})
+ if not isinstance(bucket, dict):
+ continue
+ for detail, count in value.items():
+ if isinstance(count, int) and not isinstance(count, bool):
+ bucket[detail] = count
+
+
+def _usage_int(usage: dict[str, Any], field: str) -> int:
+ value = usage.get(field)
+ return value if isinstance(value, int) and not isinstance(value, bool) else 0
+
+
+def _pass_context_tokens(usage: dict[str, Any]) -> int:
+ own = _usage_int(usage, "context_tokens")
+ if own > 0:
+ return own
+ parts = _usage_int(usage, "prompt_tokens") + _usage_int(usage, "completion_tokens")
+ return max(parts, _usage_int(usage, "total_tokens"))
+
+
def _usage_chunk_line(
model: str, totals: dict[str, Any], timings: dict[str, Any] | None
) -> str | None:
@@ -1596,7 +1625,10 @@ async def stream_with_studio_tools(
if isinstance(upstream_model, str) and upstream_model:
model_name = upstream_model
if "usage" in payload:
- _merge_usage(usage_totals, payload.get("usage"))
+ if isinstance(payload.get("usage"), dict):
+ if turn.usage is None:
+ turn.usage = {}
+ _update_usage_snapshot(turn.usage, payload["usage"])
if isinstance(payload.get("timings"), dict):
last_timings = payload["timings"]
if _is_usage_only(payload):
@@ -1734,6 +1766,17 @@ async def stream_with_studio_tools(
except (RuntimeError, GeneratorExit):
pass
+ if turn.usage is not None:
+ parts = _usage_int(turn.usage, "prompt_tokens") + _usage_int(
+ turn.usage, "completion_tokens"
+ )
+ if parts > _usage_int(turn.usage, "total_tokens"):
+ turn.usage["total_tokens"] = parts
+ _merge_usage(usage_totals, turn.usage)
+ context = _pass_context_tokens(turn.usage)
+ if context > 0:
+ usage_totals["context_tokens"] = context
+
# The provider turn is over, whichever way it ended. Said explicitly because a turn closed on [DONE] alone
# carries no finish_reason for the stripper to read the boundary off, and the loop consumes that sentinel.
if policy.on_provider_turn_end is not None:
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index 614350b2942..bef6e2c29dc 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -3382,6 +3382,10 @@ class ChatCountTokensRequest(ReasoningControlsRequest):
...,
description = "Conversation messages in OpenAI chat form",
)
+ max_tokens: Optional[int] = Field(None, ge = 1)
+ context_overflow: Optional[Literal["error", "truncate_middle", "truncate_oldest"]] = None
+ context_policy: Optional[Literal["checkpoint", "rolling"]] = None
+ compaction_headroom_ratio: Optional[float] = Field(None, ge = 0.0, le = 0.9)
tools: Optional[list[dict]] = Field(
None,
description = "Optional OpenAI tool definitions included in the prompt",
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 97cbd50856a..b2aec809672 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -44396,6 +44396,72 @@ def _count_model_id(model_id, via_api_key):
]
+def _count_chat_context_tokens(backend, payload, messages, tools, template_kwargs):
+ from core.inference.llama_cpp import (
+ _can_reset_epoch,
+ _compaction_fit_kwargs,
+ _fit_with_instruction_pins,
+ _memory_tool_withheld,
+ _sticky_compaction_details,
+ )
+
+ def count_tokens(candidate):
+ return backend.count_chat_tokens(
+ candidate,
+ None,
+ tools,
+ strict = True,
+ chat_template_kwargs = template_kwargs,
+ # A generation can start between /apply-template and /tokenize.
+ should_abort = lambda: model_slots.routed_generation_count() > 0,
+ )
+
+ context_length = getattr(backend, "_effective_context_length", None)
+ thread_id = getattr(payload, "thread_id", None)
+ if (
+ _rolling_context_policy(payload) is not None
+ and context_length
+ and thread_id
+ and not _takes_tool_passthrough(payload, backend)
+ ):
+ policy = _request_context_policy(payload)
+ ratio = _request_compaction_headroom_ratio(payload)
+ can_reset = _can_reset_epoch(
+ thread_id,
+ backend.supports_tools,
+ tools_withheld = _memory_tool_withheld(thread_id, tools),
+ )
+ sticky, checkpoint, manual = _sticky_compaction_details(
+ thread_id,
+ messages,
+ context_policy = policy,
+ can_reset = can_reset,
+ compaction_headroom_ratio = ratio,
+ )
+ if sticky > 0:
+ fitted, truncation = _fit_with_instruction_pins(
+ messages,
+ context_length = context_length,
+ max_tokens = getattr(payload, "max_tokens", None),
+ count_tokens = count_tokens,
+ sticky_dropped = sticky,
+ sticky_is_checkpoint = checkpoint,
+ sticky_manual = manual,
+ can_reset = can_reset,
+ keeps_boundary = True,
+ recall_offered = any(
+ (tool.get("function") or {}).get("name") == "search_conversation"
+ for tool in tools or []
+ ),
+ replay_only = True,
+ **_compaction_fit_kwargs(policy, ratio),
+ )
+ if truncation is not None:
+ return int(truncation["prompt_tokens_after"])
+ messages = fitted
+ return count_tokens(messages)
+
+
@router.post("/chat/count_tokens")
async def chat_count_tokens(
payload: ChatCountTokensRequest,
@@ -44600,12 +44666,25 @@ async def chat_count_tokens(
status_code = 503,
detail = "Cannot count tokens until enabled MCP tools have been discovered.",
)
- if not _takes_passthrough and (_tools_on or _mcp_on) and llama_backend.supports_tools:
+ if (
+ not _takes_passthrough
+ and llama_backend.supports_tools
+ and (
+ _tools_on
+ or _mcp_on
+ or (
+ not _client_disabled_tool_calls
+ and payload.max_tool_calls_per_message != 0
+ and await asyncio.to_thread(_checkpoint_recall_may_enable_tools, payload)
+ )
+ )
+ ):
tools_to_use = await _select_request_tools(
payload,
tools_on = _tools_on,
mcp_allowed = _mcp_allowed,
supports_vision = bool(getattr(llama_backend, "is_vision", False)),
+ checkpoint_fitted = _rolling_context_policy(payload) is not None,
)
# Appended in the position _select_request_tools would have used, so the order matches.
tools_to_use = tools_to_use + _mcp_tools
@@ -44631,6 +44710,12 @@ async def chat_count_tokens(
tools_to_use,
rag_scope = payload.rag_scope,
)
+ _count_nudge = _apply_compaction_nudge(
+ _count_nudge,
+ tools_to_use,
+ checkpoint_fitted = _rolling_context_policy(payload) is not None,
+ payload = payload,
+ )
openai_messages = _append_to_system_message(openai_messages, _count_nudge)
# The GGUF tool path strips leaked markup from replayed history before rendering,
@@ -44684,15 +44769,12 @@ async def chat_count_tokens(
try:
count = await asyncio.to_thread(
- llama_backend.count_chat_tokens,
+ _count_chat_context_tokens,
+ llama_backend,
+ payload,
openai_messages,
- None,
openai_tools,
- strict = True,
- chat_template_kwargs = _template_kwargs,
- # Polled between /apply-template and /tokenize: admission and the work are separate
- # steps, so a run starting in between is caught here and the second round trip is not.
- should_abort = lambda: model_slots.routed_generation_count() > 0,
+ _template_kwargs,
)
except CountAborted:
raise HTTPException(
diff --git a/studio/backend/storage/studio_db.py b/studio/backend/storage/studio_db.py
index 234faf641a0..28452d19a6c 100644
--- a/studio/backend/storage/studio_db.py
+++ b/studio/backend/storage/studio_db.py
@@ -3900,35 +3900,57 @@ def _content_part_id(part: dict) -> Optional[str]:
return f"{_CONTENT_PART_ID_PREFIX}{hashlib.sha256(canonical).hexdigest()}"
-def _chat_attachment_tombstones_for_messages(
+def _chat_message_revision(metadata: Any) -> int:
+ revision = metadata.get("revision") if isinstance(metadata, dict) else None
+ return revision if type(revision) is int and revision > 0 else 0
+
+
+def _chat_attachment_deletions_for_messages(
conn: sqlite3.Connection, thread_id: str, message_ids: list[str]
-) -> dict[str, set[str]]:
- tombstones = {message_id: set() for message_id in message_ids}
+) -> dict[str, tuple[set[str], int]]:
+ deletions: dict[str, tuple[set[str], int]] = {}
unique_ids = list(dict.fromkeys(message_ids))
for start in range(0, len(unique_ids), _SQLITE_IN_CHUNK_SIZE):
chunk = unique_ids[start : start + _SQLITE_IN_CHUNK_SIZE]
placeholders = ",".join("?" for _ in chunk)
rows = conn.execute(
f"""
- SELECT message_id, attachment_id
- FROM chat_attachment_tombstones
- WHERE thread_id = ? AND message_id IN ({placeholders})
+ SELECT t.message_id, t.attachment_id, m.metadata_json
+ FROM chat_attachment_tombstones AS t
+ LEFT JOIN chat_messages AS m ON m.id = t.message_id AND m.thread_id = t.thread_id
+ WHERE t.thread_id = ? AND t.message_id IN ({placeholders})
""",
(thread_id, *chunk),
).fetchall()
for row in rows:
- tombstones[row["message_id"]].add(row["attachment_id"])
- return tombstones
+ message_id = row["message_id"]
+ if message_id not in deletions:
+ deletions[message_id] = (
+ set(),
+ _chat_message_revision(_json_loads(row["metadata_json"], {})),
+ )
+ deletions[message_id][0].add(row["attachment_id"])
+ return deletions
-def _reconcile_chat_message_uploads(message: dict, tombstones: set[str]) -> dict:
+def _reconcile_chat_message_uploads(
+ message: dict,
+ tombstones: set[str],
+ stored_revision: int = 0,
+) -> dict:
"""Strip uploads previously deleted through the Data tab from a stale write."""
if not tombstones:
return message
reconciled = dict(message)
+ removed_ids: set[str] = set()
attachments = message.get("attachments")
if isinstance(attachments, list):
+ removed_ids.update(
+ str(attachment.get("id") or "")
+ for attachment in attachments
+ if isinstance(attachment, dict) and str(attachment.get("id") or "") in tombstones
+ )
reconciled["attachments"] = [
attachment
for attachment in attachments
@@ -3937,11 +3959,22 @@ def _reconcile_chat_message_uploads(message: dict, tombstones: set[str]) -> dict
content = message.get("content")
if isinstance(content, list):
- reconciled["content"] = [
- part
- for part in content
- if not (isinstance(part, dict) and (_content_part_id(part) or "") in tombstones)
- ]
+ kept_content = []
+ for part in content:
+ part_id = _content_part_id(part) if isinstance(part, dict) else None
+ if part_id is not None and part_id in tombstones:
+ removed_ids.add(part_id)
+ else:
+ kept_content.append(part)
+ reconciled["content"] = kept_content
+ metadata = message.get("metadata")
+ incoming_revision = _chat_message_revision(metadata)
+ revision = max(stored_revision, incoming_revision + len(removed_ids))
+ if revision != incoming_revision:
+ reconciled["metadata"] = {
+ **(metadata if isinstance(metadata, dict) else {}),
+ "revision": revision,
+ }
return reconciled
@@ -4177,14 +4210,14 @@ def upsert_chat_message(
message["threadId"],
[message["id"]],
)
- tombstones = _chat_attachment_tombstones_for_messages(
+ deletions = _chat_attachment_deletions_for_messages(
conn,
message["threadId"],
[message["id"]],
)
reconciled = _reconcile_chat_message_uploads(
message,
- tombstones.get(message["id"], set()),
+ *deletions.get(message["id"], (set(), 0)),
)
content_json = json.dumps(reconciled.get("content", []))
attachments_json = (
@@ -4324,13 +4357,14 @@ def sync_chat_messages(
thread_id,
[m["id"] for m in messages],
)
- tombstones = _chat_attachment_tombstones_for_messages(
+ deletions = _chat_attachment_deletions_for_messages(
conn,
thread_id,
[m["id"] for m in messages],
)
reconciled_messages = [
- _reconcile_chat_message_uploads(m, tombstones.get(m["id"], set())) for m in messages
+ _reconcile_chat_message_uploads(m, *deletions.get(m["id"], (set(), 0)))
+ for m in messages
]
serialized_messages = [
(
@@ -4479,9 +4513,19 @@ def _reseat_protected_messages(conn, thread_id: str, reseat_parents: dict) -> No
parents they pointed at need not, and a dangling parent makes the whole thread unimportable on
the next load rather than just that turn."""
for message_id, parent_id in reseat_parents.items():
+ row = conn.execute(
+ "SELECT parent_id, metadata_json FROM chat_messages WHERE thread_id = ? AND id = ?",
+ (thread_id, message_id),
+ ).fetchone()
+ if row is None or (row["parent_id"] or None) == parent_id:
+ continue
+ metadata = _json_loads(row["metadata_json"], {})
+ if not isinstance(metadata, dict):
+ metadata = {}
+ metadata["revision"] = _chat_message_revision(metadata) + 1
conn.execute(
- "UPDATE chat_messages SET parent_id = ? WHERE thread_id = ? AND id = ?",
- (parent_id, thread_id, message_id),
+ "UPDATE chat_messages SET parent_id = ?, metadata_json = ? WHERE thread_id = ? AND id = ?",
+ (parent_id, json.dumps(metadata), thread_id, message_id),
)
@@ -5164,7 +5208,7 @@ def delete_chat_attachment(message_id: str, attachment_id: str) -> bool:
_ensure_chat_attachment_inventory_current(conn)
row = conn.execute(
"""
- SELECT thread_id, attachments_json, content_json
+ SELECT thread_id, attachments_json, content_json, metadata_json
FROM chat_messages WHERE id = ?
""",
(message_id,),
@@ -5213,13 +5257,17 @@ def delete_chat_attachment(message_id: str, attachment_id: str) -> bool:
if not deleted_attachment and not deleted_content:
conn.rollback()
return False
+ metadata = _json_loads(row["metadata_json"], {})
+ if not isinstance(metadata, dict):
+ metadata = {}
+ metadata["revision"] = _chat_message_revision(metadata) + 1
conn.execute(
"""
UPDATE chat_messages
- SET attachments_json = ?, content_json = ?
+ SET attachments_json = ?, content_json = ?, metadata_json = ?
WHERE id = ?
""",
- (updated_attachments_json, updated_content_json, message_id),
+ (updated_attachments_json, updated_content_json, json.dumps(metadata), message_id),
)
_record_chat_attachment_tombstone(
conn,
diff --git a/studio/backend/tests/test_chat_attachments.py b/studio/backend/tests/test_chat_attachments.py
index 3e2621d3ed3..d97dd0ae2e2 100644
--- a/studio/backend/tests/test_chat_attachments.py
+++ b/studio/backend/tests/test_chat_attachments.py
@@ -2,6 +2,7 @@
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import base64
+import copy
import json
import os
import sqlite3
@@ -268,6 +269,105 @@ def test_delete_chat_attachment_keeps_others(tmp_path, monkeypatch):
assert [r["id"] for r in studio_db.list_chat_attachments()] == ["att-2"]
[email protected]("content_part", [False, True])
[email protected]("revision", [None, 4])
+def test_attachment_deletion_invalidates_saved_usage_inputs(
+ tmp_path, monkeypatch, content_part, revision
+):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ message = _message("msg-1", attachments = [] if content_part else [_image_attachment()])
+ message["metadata"] = {"customField": "preserved"}
+ if revision is not None:
+ message["metadata"]["revision"] = revision
+ if content_part:
+ message["content"].append({"type": "image", "image": PNG_DATA_URL})
+ studio_db.upsert_chat_message(message)
+ attachment_id = studio_db.list_chat_attachments()[0]["id"]
+
+ assert studio_db.delete_chat_attachment("msg-1", attachment_id) is True
+ saved = studio_db.get_chat_message("thread-1", "msg-1")
+ assert saved["metadata"] == {"customField": "preserved", "revision": (revision or 0) + 1}
+ assert studio_db.delete_chat_attachment("msg-1", attachment_id) is False
+ assert studio_db.get_chat_message("thread-1", "msg-1")["metadata"] == saved["metadata"]
+
+
[email protected]("batch", [False, True])
[email protected]("content_parts", [False, True])
[email protected]("pruned", [False, True])
+def test_stale_attachment_writes_cannot_restore_a_saved_usage_revision(
+ tmp_path, monkeypatch, batch, content_parts, pruned
+):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ original = _message("msg-1", attachments = [])
+ original["metadata"] = {"revision": 4, "customField": "preserved"}
+ if content_parts:
+ original["content"].extend(
+ [
+ {"type": "image", "image": PNG_DATA_URL},
+ {"type": "audio", "audio": "data:audio/wav;base64,AAAA"},
+ ]
+ )
+ else:
+ original["attachments"] = [_image_attachment("att-1"), _image_attachment("att-2")]
+ studio_db.upsert_chat_message(original)
+ attachment_ids = [item["id"] for item in studio_db.list_chat_attachments()]
+
+ def write(message):
+ if batch:
+ studio_db.sync_chat_messages("thread-1", [message])
+ else:
+ studio_db.upsert_chat_message(message)
+ return studio_db.get_chat_message("thread-1", "msg-1")
+
+ assert studio_db.delete_chat_attachment("msg-1", attachment_ids[0]) is True
+ intermediate = studio_db.get_chat_message("thread-1", "msg-1")
+ assert intermediate["metadata"]["revision"] == 5
+ assert write(original)["metadata"]["revision"] == 5
+ assert studio_db.delete_chat_attachment("msg-1", attachment_ids[1]) is True
+ if pruned:
+ studio_db.sync_chat_messages("thread-1", [], prune_missing = True)
+ assert studio_db.get_chat_message("thread-1", "msg-1") is None
+
+ for stale in [original, intermediate, original]:
+ saved = write(stale)
+ assert saved["metadata"] == {"revision": 6, "customField": "preserved"}
+ assert saved["content"] == [{"type": "text", "text": "hello"}]
+ assert saved["attachments"] == []
+ assert original["metadata"]["revision"] == 4
+ assert intermediate["metadata"]["revision"] == 5
+
+ clean = copy.deepcopy(saved)
+ clean["metadata"]["revision"] = 4
+ assert write(clean)["metadata"]["revision"] == 6
+ clean["metadata"]["revision"] = 7
+ assert write(clean)["metadata"]["revision"] == 7
+ revised_stale = copy.deepcopy(original)
+ revised_stale["metadata"]["revision"] = 7
+ assert write(revised_stale)["metadata"]["revision"] == 9
+ assert write(revised_stale)["metadata"]["revision"] == 9
+
+
+def test_attachment_and_content_sharing_a_deleted_id_advance_revision_once(tmp_path, monkeypatch):
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread())
+ part = {"type": "image", "image": PNG_DATA_URL}
+ attachment_id = studio_db._content_part_id(part)
+ message = _message("msg-1", attachments = [_image_attachment(attachment_id)])
+ message["content"].append(part)
+ message["metadata"] = {"revision": 4}
+ studio_db.upsert_chat_message(message)
+
+ assert studio_db.delete_chat_attachment("msg-1", attachment_id) is True
+ studio_db.sync_chat_messages("thread-1", [], prune_missing = True)
+ saved = studio_db.upsert_chat_message(message)
+
+ assert saved["metadata"]["revision"] == 5
+ assert saved["attachments"] == []
+ assert saved["content"] == [{"type": "text", "text": "hello"}]
+
+
def test_delete_last_chat_attachment_stores_empty_list(tmp_path, monkeypatch):
_seed(tmp_path, monkeypatch, [_image_attachment()])
assert studio_db.delete_chat_attachment("msg-1", "att-1") is True
diff --git a/studio/backend/tests/test_chat_history_storage.py b/studio/backend/tests/test_chat_history_storage.py
index 384cd54a1e5..74a7f0f8f51 100644
--- a/studio/backend/tests/test_chat_history_storage.py
+++ b/studio/backend/tests/test_chat_history_storage.py
@@ -1149,6 +1149,63 @@ def _without(messages: list[dict], *drop: str) -> list[dict]:
return [dict(m) for m in messages if m["id"] not in drop]
[email protected]("owner", ["generation", "research"])
[email protected]("revision", [None, 4])
+def test_reparented_managed_prompt_invalidates_saved_usage_after_reload(
+ tmp_path, monkeypatch, owner, revision
+):
+ from storage import chat_generation_runs_db, research_runs_db
+
+ _reset_studio_db(tmp_path, monkeypatch)
+ studio_db.upsert_chat_thread(_thread("src"))
+ prompt = _stored_message(id = "prompt", parentId = "ancestor")
+ prompt["metadata"] = {"kept": "server value"}
+ if revision is not None:
+ prompt["metadata"]["revision"] = revision
+ studio_db.sync_chat_messages(
+ "src",
+ [
+ _stored_message(id = "ancestor", role = "assistant", createdAt = 1),
+ prompt,
+ _stored_message(id = "sibling", createdAt = 10),
+ ],
+ )
+ common = dict(
+ run_id = "run-1",
+ owner_subject = "owner",
+ thread_id = "src",
+ user_message_id = "prompt",
+ assistant_message_id = "report",
+ )
+ if owner == "generation":
+ chat_generation_runs_db.create_run(
+ **common,
+ request_payload = {"model": "local", "messages": [{"role": "user", "content": "Hello"}]},
+ )
+ else:
+ research_runs_db.create_run(**common, config = {}, created_at = 11)
+ before = {m["id"]: m for m in studio_db.list_chat_messages("src")}
+ payload = _without(list(before.values()), "ancestor")
+ edited = next(m for m in payload if m["id"] == "prompt")
+ edited["parentId"] = None
+ edited["metadata"] = {"revision": 999, "untrusted": True}
+ edited["content"] = [{"type": "text", "text": "client drift"}]
+
+ studio_db.sync_chat_messages("src", payload, prune_missing = True)
+ reloaded = {m["id"]: m for m in studio_db.list_chat_messages("src")}
+
+ assert reloaded["prompt"]["parentId"] is None
+ assert reloaded["prompt"]["metadata"] == {
+ **before["prompt"]["metadata"],
+ "revision": (revision or 0) + 1,
+ }
+ assert reloaded["prompt"]["content"] == before["prompt"]["content"]
+ assert reloaded["report"] == before["report"]
+ assert reloaded["sibling"] == before["sibling"]
+ studio_db.sync_chat_messages("src", payload, prune_missing = True)
+ assert studio_db.list_chat_messages("src") == list(reloaded.values())
+
+
def test_deleting_an_ancestor_relinks_the_research_prompt(tmp_path, monkeypatch):
# The headline case: assistant-ui relinks the protected prompt to the deleted node's parent,
# and the guard must read that as the repair it is rather than an edit.
@@ -1231,9 +1288,10 @@ def test_the_reseat_does_not_carry_any_other_edit(tmp_path, monkeypatch, field,
prompt = next(m for m in synced if m["id"] == "prompt")
# The deleted ancestor was the root, so the repair is a reseat to the root.
assert prompt["parentId"] is None
- # .get on both sides: an absent key is how a None metadata comes back, and the point is
- # that the client's value is not there either way.
- assert prompt.get(field) == stored.get(field)
+ expected = stored.get(field)
+ if field == "metadata":
+ expected = {**(expected or {}), "revision": 1}
+ assert prompt.get(field) == expected
assert prompt.get(field) != value
diff --git a/studio/backend/tests/test_checkpoint_compaction.py b/studio/backend/tests/test_checkpoint_compaction.py
index 2f4910aa24a..898a3d2f5ba 100644
--- a/studio/backend/tests/test_checkpoint_compaction.py
+++ b/studio/backend/tests/test_checkpoint_compaction.py
@@ -60,6 +60,57 @@ def _fit(messages, **kwargs):
return fit_checkpoint_context(messages, **kwargs)
[email protected]("checkpoint_boundary", [True, False])
+def test_recount_cannot_deepen_an_existing_compaction_boundary(monkeypatch, checkpoint_boundary):
+ from core.inference import llama_cpp
+
+ def unexpected():
+ pytest.fail("Recounting must not probe or modify the archive")
+
+ monkeypatch.setattr(llama_cpp, "_archive_is_degraded", unexpected)
+ messages = _thread(pad = 12)
+ fitted, truncation = llama_cpp._fit_context(
+ messages,
+ context_length = 1200,
+ max_tokens = 200,
+ count_tokens = count,
+ sticky_dropped = 2,
+ sticky_is_checkpoint = checkpoint_boundary,
+ replay_only = True,
+ )
+
+ assert truncation["fits"] is False
+ assert truncation["dropped_messages"] == 2
+ assert truncation["prompt_tokens_after"] == count(fitted)
+ assert ("<carried_forward>" in str(fitted)) is checkpoint_boundary
+
+
+ ("can_reset", "write_failed", "searchable"),
+ [(False, False, False), (True, False, True), (True, True, False)],
+)
+def test_recount_replays_the_block_wording_generation_renders(
+ monkeypatch, can_reset, write_failed, searchable
+):
+ from core.inference import checkpoint, llama_cpp
+ from core.rag import conversation_archive
+
+ monkeypatch.setattr(llama_cpp, "_archive_is_degraded", lambda: pytest.fail("no live probe"))
+ monkeypatch.setattr(conversation_archive, "degraded", lambda: write_failed)
+ fitted, _ = llama_cpp._fit_context(
+ _thread(pad = 12),
+ context_length = 1200,
+ max_tokens = 200,
+ count_tokens = count,
+ sticky_dropped = 2,
+ can_reset = can_reset,
+ recall_offered = True,
+ replay_only = True,
+ )
+ tail = checkpoint._SEARCHABLE if searchable else checkpoint._NOT_SEARCHABLE
+ assert tail in str(fitted)
+
+
def _turn(**overrides):
"""One stored conversation turn, with per-test overrides."""
return {
diff --git a/studio/backend/tests/test_external_provider_usage_chunk.py b/studio/backend/tests/test_external_provider_usage_chunk.py
index 291f014a7e2..e8b9d8687d1 100644
--- a/studio/backend/tests/test_external_provider_usage_chunk.py
+++ b/studio/backend/tests/test_external_provider_usage_chunk.py
@@ -53,6 +53,7 @@ def test_build_usage_chunk_anthropic_shape():
assert usage["cache_read_input_tokens"] == 18901
# OpenAI-style mirror for clients that key off prompt_tokens_details.
assert usage["prompt_tokens_details"]["cached_tokens"] == 18901
+ assert usage["context_tokens"] == 8 + 1367 + 18901 + 862
def test_build_usage_chunk_anthropic_includes_compaction_iteration_tokens():
@@ -73,6 +74,33 @@ def test_build_usage_chunk_anthropic_includes_compaction_iteration_tokens():
assert usage["total_tokens"] == 207_500
assert usage["compaction_input_tokens"] == 180_000
assert usage["compaction_output_tokens"] == 3_500
+ assert usage["context_tokens"] == 24_000
+
+
+def test_build_usage_chunk_anthropic_context_is_the_last_message_iteration():
+ line = _build_usage_chunk(
+ "chatcmpl-iterations",
+ "anthropic",
+ {
+ "input_tokens": 4_400,
+ "output_tokens": 400,
+ "iterations": [
+ {"type": "message", "input_tokens": 2_000, "output_tokens": 100},
+ {"type": "compaction", "input_tokens": 9_000, "output_tokens": 300},
+ {
+ "type": "message",
+ "input_tokens": 2_400,
+ "cache_read_input_tokens": 0,
+ "output_tokens": 300,
+ },
+ {"type": "advisor_message", "input_tokens": 50_000, "output_tokens": 10},
+ ],
+ },
+ )
+ usage = json.loads(line[len("data: ") :])["usage"]
+ assert usage["prompt_tokens"] == 4_400
+ assert usage["completion_tokens"] == 400
+ assert usage["context_tokens"] == 2_700
def test_build_usage_chunk_openai_shape():
@@ -505,6 +533,61 @@ def test_custom_provider_test_endpoint_requires_model_id(monkeypatch):
assert "model ID" in result.message
+def test_anthropic_stream_keeps_input_counts_a_delta_reports_as_null(monkeypatch):
+ sse_events = [
+ {
+ "type": "message_start",
+ "message": {
+ "usage": {
+ "input_tokens": 500,
+ "output_tokens": 0,
+ "cache_creation_input_tokens": 100,
+ "cache_read_input_tokens": 4000,
+ }
+ },
+ },
+ {
+ "type": "message_delta",
+ "delta": {"stop_reason": "end_turn"},
+ "usage": {
+ "input_tokens": None,
+ "cache_creation_input_tokens": None,
+ "cache_read_input_tokens": None,
+ "output_tokens": 70,
+ },
+ },
+ {"type": "message_stop"},
+ ]
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 200,
+ content = _anthropic_sse(sse_events),
+ headers = {"content-type": "text/event-stream"},
+ )
+
+ _mock_http_client(monkeypatch, handler)
+
+ async def run():
+ client = _make_anthropic_client()
+ return await _collect(
+ client._stream_anthropic(
+ messages = [{"role": "user", "content": "ping"}],
+ model = "claude-opus-4-7",
+ temperature = 0.7,
+ top_p = 0.95,
+ max_tokens = 64,
+ )
+ )
+
+ usages = _usage_chunks(_drive(run()))
+ assert len(usages) == 1
+ assert usages[0]["prompt_tokens"] == 4600
+ assert usages[0]["completion_tokens"] == 70
+ assert usages[0]["context_tokens"] == 4670
+ assert usages[0]["cache_read_input_tokens"] == 4000
+
+
def test_anthropic_stream_emits_usage_chunk_before_done(monkeypatch):
sse_events = [
{
@@ -558,6 +641,7 @@ def test_anthropic_stream_emits_usage_chunk_before_done(monkeypatch):
assert u["cache_creation_input_tokens"] == 6253
assert u["cache_read_input_tokens"] == 5713
assert u["prompt_tokens_details"]["cached_tokens"] == 5713
+ assert u["context_tokens"] == 7 + 6253 + 5713 + 1066
# Usage chunk must come before [DONE].
data_lines = [ln for ln in lines if ln.startswith("data:")]
diff --git a/studio/backend/tests/test_external_tool_name_and_usage.py b/studio/backend/tests/test_external_tool_name_and_usage.py
index 10918aa7999..9522839b026 100644
--- a/studio/backend/tests/test_external_tool_name_and_usage.py
+++ b/studio/backend/tests/test_external_tool_name_and_usage.py
@@ -291,6 +291,7 @@ def test_usage_riding_on_a_content_chunk_is_counted_once(executed):
assert len(reports) == 1, reports
assert reports[0]["choices"] == []
assert reports[0]["usage"]["total_tokens"] == 15
+ assert reports[0]["usage"]["context_tokens"] == 15
def test_usage_totals_still_sum_across_turns(executed):
@@ -316,6 +317,7 @@ def test_usage_totals_still_sum_across_turns(executed):
reports = _usage_chunks(out)
assert len(reports) == 1
assert reports[0]["usage"]["total_tokens"] == 5
+ assert reports[0]["usage"]["context_tokens"] == 5
def test_the_summed_usage_carries_the_last_turns_timings(executed):
@@ -351,6 +353,7 @@ def test_the_summed_usage_carries_the_last_turns_timings(executed):
reports = _usage_chunks(out)
assert len(reports) == 1
assert reports[0]["usage"]["total_tokens"] == 17
+ assert reports[0]["usage"]["context_tokens"] == 10
assert reports[0]["timings"] == last
# Timings belong only on the summary chunk.
assert not any('"timings"' in line for line in out if '"content"' in line)
@@ -363,13 +366,13 @@ _USAGE_NO_TIMINGS = "data: " + json.dumps(
@pytest.mark.parametrize(
- "answer_turn, total",
+ "answer_turn, total, context",
[
- pytest.param([_finish("stop"), _USAGE_NO_TIMINGS], 17, id = "usage-without-timings"),
- pytest.param([_ANSWER, _finish("stop")], 7, id = "no-usage-chunk"),
+ pytest.param([_finish("stop"), _USAGE_NO_TIMINGS], 17, 10, id = "usage-without-timings"),
+ pytest.param([_ANSWER, _finish("stop")], 7, 7, id = "no-usage-chunk"),
],
)
-def test_a_last_turn_without_timings_reports_none(executed, answer_turn, total):
+def test_a_last_turn_without_timings_reports_none(executed, answer_turn, total, context):
turn_one = "data: " + json.dumps(
{
"choices": [],
@@ -392,6 +395,7 @@ def test_a_last_turn_without_timings_reports_none(executed, answer_turn, total):
reports = _usage_chunks(_run(transport))
assert len(reports) == 1
assert reports[0]["usage"]["total_tokens"] == total
+ assert reports[0]["usage"]["context_tokens"] == context
assert "timings" not in reports[0]
diff --git a/studio/backend/tests/test_external_tool_stream_abuse.py b/studio/backend/tests/test_external_tool_stream_abuse.py
index 627b38ebc71..2edc9d34c4f 100644
--- a/studio/backend/tests/test_external_tool_stream_abuse.py
+++ b/studio/backend/tests/test_external_tool_stream_abuse.py
@@ -1069,6 +1069,7 @@ def test_usage_collapses_to_at_most_one_chunk(executed):
assert len(usage_chunks) == 1
assert usage_chunks[0]["usage"]["prompt_tokens"] == 15
assert usage_chunks[0]["usage"]["completion_tokens"] == 5
+ assert usage_chunks[0]["usage"]["context_tokens"] == 8
def test_a_stream_with_no_usage_emits_no_usage_chunk(executed):
@@ -1079,7 +1080,6 @@ def test_a_stream_with_no_usage_emits_no_usage_chunk(executed):
def test_a_forged_usage_only_chunk_cannot_multiply_the_count(executed):
- """Usage-only chunks are withheld and summed, however many arrive."""
turn = [_raw({"choices": [], "usage": {"prompt_tokens": 1}}) for _ in range(50)]
turn += [_sse({"content": "hi"}), _sse(finish = "stop"), _DONE]
transport = FakeTransport([turn])
@@ -1087,7 +1087,8 @@ def test_a_forged_usage_only_chunk_cannot_multiply_the_count(executed):
usage_chunks = [payload for payload in _payloads(lines) if "usage" in payload]
assert len(usage_chunks) == 1
- assert usage_chunks[0]["usage"]["prompt_tokens"] == 50
+ assert usage_chunks[0]["usage"]["prompt_tokens"] == 1
+ assert usage_chunks[0]["usage"]["context_tokens"] == 1
def test_a_truncated_turn_closes_the_card_an_id_less_call_painted(executed):
diff --git a/studio/backend/tests/test_managed_engine_tools.py b/studio/backend/tests/test_managed_engine_tools.py
index ea1d040c3e6..2fec433cb34 100644
--- a/studio/backend/tests/test_managed_engine_tools.py
+++ b/studio/backend/tests/test_managed_engine_tools.py
@@ -262,6 +262,7 @@ def test_studio_loop_executes_and_replays_native_tool_calls(native, monkeypatch)
assert executed == [("python", {"code": "print(3973)"})]
assert result["choices"][0]["message"]["content"] == "3973"
assert result["usage"]["prompt_tokens"] == 20
+ assert result["usage"]["context_tokens"] == 13
assert "tool_calls" not in result["choices"][0]["message"]
assert any(
m.get("role") == "tool" and m.get("content") == "3973" for m in native[1][-1]["messages"]
diff --git a/studio/backend/tests/test_openai_auto_switch.py b/studio/backend/tests/test_openai_auto_switch.py
index 2c366326907..1d5d0ebeb3c 100644
--- a/studio/backend/tests/test_openai_auto_switch.py
+++ b/studio/backend/tests/test_openai_auto_switch.py
@@ -5158,6 +5158,277 @@ def _counted_body(payload):
return json.loads(response.body)
+ ("fields", "recall_expected"),
+ [
+ ({}, True),
+ ({"tool_choice": "none"}, False),
+ ({"max_tool_calls_per_message": 0}, False),
+ ({"max_tool_calls_per_message": 1}, True),
+ ({"tool_choice": "none", "enable_tools": True}, True),
+ ],
+)
+def test_chat_count_tokens_respects_checkpoint_recall_eligibility(
+ monkeypatch, fields, recall_expected
+):
+ _, counted = _count_tokens_backend(monkeypatch, supports_tools = True)
+ monkeypatch.setattr(_tp, "get_tool_policy", lambda: None)
+ monkeypatch.setattr(inference_route, "_thread_has_conversation_archive", lambda _thread: True)
+ monkeypatch.setattr(inference_route, "_thread_has_checkpoint", lambda *_args: True)
+ payload = _count_request(
+ [
+ {"role": "user", "content": "Continue."},
+ {"role": "assistant", "content": "Done."},
+ ],
+ **{
+ "thread_id": "compacted-thread",
+ "context_overflow": "truncate_oldest",
+ "context_policy": "checkpoint",
+ "enable_tools": False,
+ "enabled_tools": [],
+ **fields,
+ },
+ )
+
+ assert _counted_body(payload)["input_tokens"] == 10
+ assert [tool["function"]["name"] for tool in counted["tools"] or []] == (
+ ["search_conversation"] if recall_expected else []
+ )
+ assert ("search_conversation" in json.dumps(counted["messages"])) is recall_expected
+
+
[email protected]("policy", ["checkpoint", "rolling"])
+def test_chat_count_tokens_replays_saved_compaction_without_archive_work(monkeypatch, policy):
+ from core.rag import conversation_archive
+
+ _count_tokens_backend(monkeypatch, supports_tools = True)
+ backend = inference_route.get_llama_cpp_backend()
+ backend._effective_context_length = 2048
+ counted = []
+
+ def count(messages, *_args, **_kwargs):
+ counted.append(messages)
+ return sum(max(1, len(str(message.get("content", ""))) // 4) for message in messages)
+
+ def unexpected(*_args, **_kwargs):
+ pytest.fail("A token recount must not write or query the conversation archive")
+
+ backend.count_chat_tokens = count
+ monkeypatch.setattr(_tp, "get_tool_policy", lambda: None)
+ monkeypatch.setattr(conversation_archive, "enabled", lambda: True)
+ monkeypatch.setattr(conversation_archive, "can_archive", lambda _thread: True)
+ monkeypatch.setattr(conversation_archive, "has_archive", lambda _thread: True)
+ monkeypatch.setattr(conversation_archive, "archive_turns", unexpected)
+ monkeypatch.setattr(llama_cpp_mod, "_archive_is_degraded", unexpected)
+ monkeypatch.setattr(llama_cpp_mod, "_archive_and_recall", unexpected)
+ messages = []
+ for index in range(4):
+ messages.extend(
+ [
+ {"role": "user", "content": f"Old passage {index}: " + "x" * 3200},
+ {"role": "assistant", "content": f"Read passage {index}."},
+ ]
+ )
+ messages.extend(
+ [
+ {"role": "user", "content": "Continue with the current question."},
+ {"role": "assistant", "content": "Here is the current answer."},
+ ]
+ )
+ stored = [
+ {**message, "id": f"m-{index}", "parentId": f"m-{index - 1}" if index else None}
+ for index, message in enumerate(messages)
+ ]
+ stored[-1]["metadata"] = {
+ "contextTruncation": {
+ "fits": True,
+ "checkpoint": policy == "checkpoint",
+ "boundary_messages": 6,
+ "boundary_anchor": messages[6]["content"],
+ }
+ }
+ monkeypatch.setattr(studio_db, "list_chat_messages", lambda _thread: stored)
+ payload = _count_request(
+ messages,
+ thread_id = "compacted-thread",
+ context_overflow = "truncate_oldest",
+ context_policy = policy,
+ max_tokens = 256,
+ enable_tools = policy == "rolling",
+ enabled_tools = [],
+ )
+
+ body = _counted_body(payload)
+
+ assert 0 < body["input_tokens"] < 2048
+ rendered = json.dumps(counted[-1])
+ assert "Old passage 0" not in rendered
+ assert "Old passage 3" in rendered
+ assert "Here is the current answer." in rendered
+ assert "search_conversation" in rendered
+ assert body["input_tokens"] == count(counted[-1])
+
+
[email protected]("context_overflow", [None, "error", "truncate_oldest"])
+def test_chat_count_tokens_keeps_uncompacted_branches_intact(monkeypatch, context_overflow):
+ _count_tokens_backend(monkeypatch)
+ backend = inference_route.get_llama_cpp_backend()
+ backend._effective_context_length = 2048
+ messages = [
+ {"role": "user", "content": "old passage " + "x" * 12000},
+ {"role": "assistant", "content": "old answer"},
+ {"role": "user", "content": "latest question"},
+ ]
+ seen = []
+
+ def count(candidate, *_args, **_kwargs):
+ seen.append(candidate)
+ return 3200
+
+ backend.count_chat_tokens = count
+ monkeypatch.setattr(studio_db, "list_chat_messages", lambda _thread: [])
+ body = _counted_body(
+ _count_request(
+ messages,
+ thread_id = "uncompacted-thread",
+ context_overflow = context_overflow,
+ enable_tools = False,
+ )
+ )
+
+ assert body["input_tokens"] == 3200
+ assert seen == [messages]
+
+
+ "fields",
+ [
+ {"tools": [{"type": "function", "function": {"name": "client_tool"}}]},
+ {"response_format": {"type": "json_object"}},
+ ],
+ ids = ["client_tools", "guided_json"],
+)
+def test_chat_count_tokens_does_not_replay_studio_compaction_on_passthrough(monkeypatch, fields):
+ _count_tokens_backend(monkeypatch, supports_tools = True)
+ backend = inference_route.get_llama_cpp_backend()
+ backend._effective_context_length = 2048
+ messages = []
+ for index in range(3):
+ messages.extend(
+ [
+ {"role": "user", "content": f"Passage {index}: " + "x" * 3200},
+ {"role": "assistant", "content": "Read."},
+ ]
+ )
+ stored = [
+ {**message, "id": f"m-{index}", "parentId": f"m-{index - 1}" if index else None}
+ for index, message in enumerate(messages)
+ ]
+ stored[-1]["metadata"] = {
+ "contextTruncation": {
+ "fits": True,
+ "checkpoint": True,
+ "manual": True,
+ "boundary_messages": 2,
+ "boundary_anchor": messages[2]["content"],
+ }
+ }
+ seen = []
+
+ def count(candidate, *_args, **_kwargs):
+ seen.append(candidate)
+ return sum(len(message["content"]) // 4 for message in candidate)
+
+ backend.count_chat_tokens = count
+ monkeypatch.setattr(_tp, "get_tool_policy", lambda: None)
+ monkeypatch.setattr(studio_db, "list_chat_messages", lambda _thread: stored)
+ payload = _count_request(
+ messages,
+ thread_id = "compacted-thread",
+ context_overflow = "truncate_oldest",
+ context_policy = "checkpoint",
+ max_tokens = 256,
+ enable_tools = False,
+ **fields,
+ )
+
+ body = _counted_body(payload)
+
+ assert seen == [messages]
+ assert body["input_tokens"] == sum(len(message["content"]) // 4 for message in messages)
+
+
[email protected]("checkpoint_boundary", [True, False])
[email protected]("context_length", [2048, 16384])
+def test_chat_count_tokens_prices_saved_boundary_without_enforcing_reply_budget(
+ monkeypatch, checkpoint_boundary, context_length
+):
+ _count_tokens_backend(monkeypatch)
+ backend = inference_route.get_llama_cpp_backend()
+ backend._effective_context_length = context_length
+ messages = []
+ for index in range(3):
+ messages.extend(
+ [
+ {"role": "user", "content": f"Passage {index}: " + "x" * 3200},
+ {"role": "assistant", "content": "Read."},
+ ]
+ )
+ counted = []
+
+ def count(candidate, *_args, **_kwargs):
+ counted.append(candidate)
+ return sum(len(message["content"]) // 4 for message in candidate)
+
+ backend.count_chat_tokens = count
+ monkeypatch.setattr(llama_cpp_mod, "_can_reset_epoch", lambda *_args, **_kwargs: False)
+ monkeypatch.setattr(
+ llama_cpp_mod,
+ "_sticky_compaction_details",
+ lambda *_args, **_kwargs: (2, checkpoint_boundary, False),
+ )
+ payload = _count_request(
+ messages,
+ thread_id = "compacted-thread",
+ context_overflow = "truncate_oldest",
+ context_policy = "checkpoint" if checkpoint_boundary else "rolling",
+ max_tokens = 512,
+ enable_tools = False,
+ )
+
+ body = _counted_body(payload)
+
+ if context_length == 2048:
+ assert 1536 < body["input_tokens"] < 2048
+ assert "Passage 0" not in json.dumps(counted[-1])
+ assert len(counted) == 2
+ else:
+ assert counted == [messages]
+ assert body["input_tokens"] == sum(len(message["content"]) // 4 for message in counted[-1])
+
+
+def test_chat_count_tokens_checks_checkpoint_recall_off_the_event_loop(monkeypatch):
+ _count_tokens_backend(monkeypatch, supports_tools = True)
+ event_loop_thread = threading.get_ident()
+ checked = []
+
+ def recall_eligible(_payload):
+ checked.append(threading.get_ident())
+ assert checked[-1] != event_loop_thread
+ return False
+
+ monkeypatch.setattr(inference_route, "_checkpoint_recall_may_enable_tools", recall_eligible)
+ payload = _count_request(
+ [{"role": "user", "content": "Continue."}],
+ thread_id = "compacted-thread",
+ context_overflow = "truncate_oldest",
+ enable_tools = False,
+ )
+
+ assert _counted_body(payload)["input_tokens"] == 10
+ assert len(checked) == 1
+
+
@pytest.mark.parametrize(
"content",
[
@@ -5186,6 +5457,7 @@ def test_chat_count_tokens_forwards_enabled_tools(monkeypatch):
tools_on,
mcp_allowed,
supports_vision = False,
+ checkpoint_fitted = False,
):
gate.update(tools_on = tools_on, mcp_allowed = mcp_allowed)
return [{"type": "function", "function": {"name": "web_search"}}]
@@ -5238,6 +5510,7 @@ def test_chat_count_tokens_strips_replayed_tool_markup(monkeypatch, fields, expe
tools_on,
mcp_allowed,
supports_vision = False,
+ checkpoint_fitted = False,
):
return [{"type": "function", "function": {"name": "web_search"}}]
@@ -5346,6 +5619,7 @@ def test_chat_count_tokens_prices_the_route_the_completion_takes(
tools_on,
mcp_allowed,
supports_vision = False,
+ checkpoint_fitted = False,
):
return [{"type": "function", "function": {"name": "web_search"}}]
@@ -5471,6 +5745,7 @@ def test_chat_count_tokens_dates_only_api_server_tool_prompts(monkeypatch):
tools_on,
mcp_allowed,
supports_vision = False,
+ checkpoint_fitted = False,
):
return [{"type": "function", "function": {"name": "web_search"}}]
@@ -5883,6 +6158,7 @@ def test_chat_count_tokens_counts_an_empty_chat_the_cli_policy_fills(monkeypatch
tools_on,
mcp_allowed,
supports_vision = False,
+ checkpoint_fitted = False,
):
return [{"type": "function", "function": {"name": "web_search"}}]
diff --git a/studio/backend/tests/test_oversized_attachment_fit.py b/studio/backend/tests/test_oversized_attachment_fit.py
index 47ea4616421..1fb74f33cdc 100644
--- a/studio/backend/tests/test_oversized_attachment_fit.py
+++ b/studio/backend/tests/test_oversized_attachment_fit.py
@@ -7,6 +7,8 @@ from __future__ import annotations
import copy
+import pytest
+
from core.inference import checkpoint, llama_cpp
from core.inference.context_window import evicted_messages, fit_oversized_attachments
@@ -140,6 +142,29 @@ def test_the_compaction_fit_serves_the_turn_instead_of_refusing_it(monkeypatch):
assert truncation is None or truncation.get("fits") is not False
[email protected]("checkpoint_boundary", [True, False])
+def test_a_recount_cuts_an_oversized_attachment_before_replaying_the_boundary(checkpoint_boundary):
+ messages = [
+ {"role": "system", "content": "you are helpful"},
+ {"role": "user", "content": "hi"},
+ {"role": "assistant", "content": "hello"},
+ {"role": "user", "content": "What does line 1000 say?\n" + note() + FILE},
+ ]
+
+ fitted, truncation = llama_cpp._fit_context(
+ messages,
+ context_length = 2048,
+ max_tokens = 512,
+ count_tokens = count,
+ sticky_dropped = 2,
+ sticky_is_checkpoint = checkpoint_boundary,
+ replay_only = True,
+ )
+
+ assert "of 2000 lines are above" in fitted[-1]["content"]
+ assert truncation["prompt_tokens_after"] == count(fitted) <= 2048 - 512
+
+
def _follow_up(first = None):
first = first or _messages()
return first + [
diff --git a/studio/backend/tests/test_rag_nudge_roster_compat.py b/studio/backend/tests/test_rag_nudge_roster_compat.py
index 2668ceb6a25..b87cdf1672c 100644
--- a/studio/backend/tests/test_rag_nudge_roster_compat.py
+++ b/studio/backend/tests/test_rag_nudge_roster_compat.py
@@ -657,6 +657,7 @@ def test_count_tokens_prices_the_same_roster_the_completion_sends(rag_conn, monk
tools_on,
mcp_allowed,
supports_vision = False,
+ checkpoint_fitted = False,
):
return TOOLS
diff --git a/studio/backend/tests/test_studio_tool_loop.py b/studio/backend/tests/test_studio_tool_loop.py
index 4e32ada2e6e..fdd2cea1a50 100644
--- a/studio/backend/tests/test_studio_tool_loop.py
+++ b/studio/backend/tests/test_studio_tool_loop.py
@@ -1027,6 +1027,366 @@ def test_usage_is_summed_into_one_chunk(executed):
assert usages[0]["completion_tokens"] == 12
assert usages[0]["total_tokens"] == 42
assert usages[0]["prompt_tokens_details"]["cached_tokens"] == 10
+ assert usages[0]["context_tokens"] == 27
+
+
+def _usage_line(prompt, completion, **extra):
+ usage = {
+ "prompt_tokens": prompt,
+ "completion_tokens": completion,
+ "total_tokens": prompt + completion,
+ }
+ usage.update(extra)
+ return "data: " + json.dumps({"choices": [], "usage": usage})
+
+
+def _usage_payloads(lines):
+ found = []
+ for line in lines:
+ raw = line[6:] if line.startswith("data: ") else ""
+ if not raw or raw == "[DONE]":
+ continue
+ payload = json.loads(raw)
+ if "usage" in payload:
+ found.append(payload["usage"])
+ return found
+
+
+_WEB_SEARCH_CALL = _sse(
+ {
+ "tool_calls": [
+ {"index": 0, "id": "c1", "function": {"name": "web_search", "arguments": "{}"}}
+ ]
+ }
+)
+
+
+def test_usage_snapshots_sum_once_per_turn_while_context_uses_the_last_turn(executed):
+ transport = FakeTransport(
+ [
+ [
+ _WEB_SEARCH_CALL,
+ _usage_line(
+ 10,
+ 2,
+ prompt_tokens_details = {"cached_tokens": 4},
+ completion_tokens_details = {"reasoning_tokens": 1},
+ cache_creation_input_tokens = 3,
+ cache_read_input_tokens = 4,
+ ),
+ _sse(finish = "tool_calls"),
+ _usage_line(
+ 10,
+ 5,
+ prompt_tokens_details = {"cached_tokens": 6},
+ completion_tokens_details = {"reasoning_tokens": 2},
+ cache_creation_input_tokens = 5,
+ cache_read_input_tokens = 6,
+ ),
+ _DONE,
+ ],
+ [
+ _sse({"content": "done"}),
+ _sse(finish = "stop"),
+ _usage_line(
+ 20,
+ 7,
+ prompt_tokens_details = {"cached_tokens": 8},
+ completion_tokens_details = {"reasoning_tokens": 3},
+ cache_creation_input_tokens = 7,
+ cache_read_input_tokens = 8,
+ ),
+ _DONE,
+ ],
+ ]
+ )
+ usages = _usage_payloads(_run(transport))
+
+ assert len(usages) == 1
+ assert usages[0]["prompt_tokens"] == 30
+ assert usages[0]["completion_tokens"] == 12
+ assert usages[0]["total_tokens"] == 42
+ assert usages[0]["prompt_tokens_details"] == {"cached_tokens": 14}
+ assert usages[0]["completion_tokens_details"] == {"reasoning_tokens": 5}
+ assert usages[0]["cache_creation_input_tokens"] == 12
+ assert usages[0]["cache_read_input_tokens"] == 14
+ assert usages[0]["context_tokens"] == 27
+
+
+def test_a_turn_reporting_its_own_context_keeps_it_while_billing_sums(executed):
+ transport = FakeTransport(
+ [
+ [_WEB_SEARCH_CALL, _sse(finish = "tool_calls"), _usage_line(50, 3), _DONE],
+ [
+ _sse({"content": "done"}),
+ _sse(finish = "stop"),
+ _usage_line(203_000, 4_500, context_tokens = 24_000),
+ _DONE,
+ ],
+ ]
+ )
+ usages = _usage_payloads(_run(transport))
+
+ assert len(usages) == 1
+ assert usages[0]["prompt_tokens"] == 203_050
+ assert usages[0]["completion_tokens"] == 4_503
+ assert usages[0]["total_tokens"] == 207_553
+ assert usages[0]["context_tokens"] == 24_000
+
+
[email protected]("last_usage", [None, {}, {"total_tokens": 0}])
+def test_a_final_turn_without_context_usage_keeps_the_latest_reported_context(executed, last_usage):
+ final_turn = [_sse({"content": "done"}), _sse(finish = "stop")]
+ if last_usage is not None:
+ final_turn.append("data: " + json.dumps({"choices": [], "usage": last_usage}))
+ final_turn.append(_DONE)
+ transport = FakeTransport(
+ [
+ [_WEB_SEARCH_CALL, _sse(finish = "tool_calls"), _usage_line(100, 5), _DONE],
+ [
+ _sse(
+ {
+ "tool_calls": [
+ {
+ "index": 0,
+ "id": "c2",
+ "function": {
+ "name": "web_search",
+ "arguments": '{"query":"follow-up"}',
+ },
+ }
+ ]
+ }
+ ),
+ _sse(finish = "tool_calls"),
+ _usage_line(200, 10),
+ _DONE,
+ ],
+ final_turn,
+ ]
+ )
+
+ usages = _usage_payloads(_run(transport))
+
+ assert len(transport.requests) == 3
+ assert len(usages) == 1
+ assert usages[0]["prompt_tokens"] == 300
+ assert usages[0]["completion_tokens"] == 15
+ assert usages[0]["total_tokens"] == 315
+ assert usages[0]["context_tokens"] == 210
+
+
+def test_a_partial_report_after_a_full_one_keeps_the_fields_it_omits(executed):
+ transport = FakeTransport(
+ [
+ [
+ _sse({"content": "done"}),
+ "data: "
+ + json.dumps(
+ {
+ "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}],
+ "usage": {
+ "prompt_tokens": 100,
+ "completion_tokens": 20,
+ "total_tokens": 120,
+ },
+ }
+ ),
+ "data: "
+ + json.dumps(
+ {"choices": [], "usage": {"completion_tokens": 20, "total_tokens": None}}
+ ),
+ _DONE,
+ ],
+ ]
+ )
+ usages = _usage_payloads(_run(transport))
+
+ assert len(usages) == 1
+ assert usages[0]["prompt_tokens"] == 100
+ assert usages[0]["completion_tokens"] == 20
+ assert usages[0]["total_tokens"] == 120
+ assert usages[0]["context_tokens"] == 120
+
+
[email protected]("finish", ["stop", "length"])
+def test_one_response_cumulative_usage_is_not_billed_per_chunk(executed, finish):
+ chunks = [
+ _sse(
+ {"content": "answer "},
+ usage = {
+ "prompt_tokens": 2_000,
+ "completion_tokens": output,
+ "total_tokens": 2_000 + output,
+ "completion_tokens_details": {"reasoning_tokens": min(output, 12_000)},
+ },
+ )
+ for output in range(1_000, 20_001, 1_000)
+ ]
+ chunks.extend(
+ [
+ _sse(finish = finish),
+ _usage_line(2_000, 20_000, completion_tokens_details = {"reasoning_tokens": 12_000}),
+ _DONE,
+ ]
+ )
+ transport = FakeTransport([chunks])
+
+ lines = _run(transport)
+
+ assert len(transport.requests) == 1
+ assert executed == []
+ assert _visible_text(lines) == "answer " * 20
+ assert _usage_payloads(lines) == [
+ {
+ "prompt_tokens": 2_000,
+ "completion_tokens": 20_000,
+ "total_tokens": 22_000,
+ "completion_tokens_details": {"reasoning_tokens": 12_000},
+ "context_tokens": 22_000,
+ }
+ ]
+
+
+def test_partial_usage_preserves_nested_counts_and_reconciles_the_total(executed):
+ reports = [
+ {
+ "prompt_tokens": 100,
+ "completion_tokens": 20,
+ "total_tokens": 120,
+ "prompt_tokens_details": {"cached_tokens": 60, "audio_tokens": 10},
+ "completion_tokens_details": {"reasoning_tokens": 12, "audio_tokens": 3},
+ "cache_creation": {"ephemeral_5m_input_tokens": 50, "ephemeral_1h_input_tokens": 20},
+ },
+ {
+ "prompt_tokens": None,
+ "completion_tokens": 25,
+ "total_tokens": None,
+ "prompt_tokens_details": {"cached_tokens": 70, "audio_tokens": None},
+ "completion_tokens_details": {"reasoning_tokens": None, "audio_tokens": 4},
+ "cache_creation": {"ephemeral_5m_input_tokens": None, "ephemeral_1h_input_tokens": 30},
+ },
+ None,
+ {},
+ {"prompt_tokens": None, "completion_tokens_details": None, "cache_creation": {}},
+ ]
+ transport = FakeTransport(
+ [
+ [
+ _sse({"content": "done"}),
+ *("data: " + json.dumps({"choices": [], "usage": report}) for report in reports),
+ _sse(finish = "stop"),
+ _DONE,
+ ]
+ ]
+ )
+
+ assert _usage_payloads(_run(transport)) == [
+ {
+ "prompt_tokens": 100,
+ "completion_tokens": 25,
+ "total_tokens": 125,
+ "prompt_tokens_details": {"cached_tokens": 70, "audio_tokens": 10},
+ "completion_tokens_details": {"reasoning_tokens": 12, "audio_tokens": 4},
+ "cache_creation": {"ephemeral_5m_input_tokens": 50, "ephemeral_1h_input_tokens": 30},
+ "context_tokens": 125,
+ }
+ ]
+
+
+def test_final_usage_corrections_can_lower_or_zero_counts(executed):
+ transport = FakeTransport(
+ [
+ [
+ _sse({"content": "done"}),
+ _usage_line(
+ 100,
+ 20,
+ prompt_tokens_details = {"cached_tokens": 60},
+ completion_tokens_details = {"reasoning_tokens": 12},
+ ),
+ _usage_line(
+ 80,
+ 0,
+ prompt_tokens_details = {"cached_tokens": 0},
+ completion_tokens_details = {"reasoning_tokens": 0},
+ ),
+ _sse(finish = "stop"),
+ _DONE,
+ ]
+ ]
+ )
+
+ assert _usage_payloads(_run(transport)) == [
+ {
+ "prompt_tokens": 80,
+ "completion_tokens": 0,
+ "total_tokens": 80,
+ "prompt_tokens_details": {"cached_tokens": 0},
+ "completion_tokens_details": {"reasoning_tokens": 0},
+ "context_tokens": 80,
+ }
+ ]
+
+
[email protected]("ending", ["cancel", "error_event", "exception"])
+def test_interrupted_usage_is_not_counted_repeatedly(executed, ending):
+ class InterruptedTransport(FakeTransport):
+ def stream(self, *, cancel_event, **kwargs):
+ async def generate():
+ yield _sse({"content": "partial"})
+ yield _usage_line(100, 2)
+ yield _usage_line(100, 5)
+ if ending == "cancel":
+ cancel_event.set()
+ elif ending == "error_event":
+ yield 'data: {"error":{"message":"provider stopped"}}'
+ else:
+ raise RuntimeError("provider stopped")
+
+ return generate()
+
+ transport = InterruptedTransport([])
+ if ending == "exception":
+ with pytest.raises(RuntimeError, match = "provider stopped"):
+ _run(transport)
+ else:
+ lines = _run(transport)
+ assert _usage_payloads(lines) == [
+ {
+ "prompt_tokens": 100,
+ "completion_tokens": 5,
+ "total_tokens": 105,
+ "context_tokens": 105,
+ }
+ ]
+ if ending == "error_event":
+ assert any('"error"' in line for line in lines)
+ assert executed == []
+
+
+ "last_usage",
+ [{"total_tokens": 300}, {"completion_tokens": 50, "total_tokens": 300}],
+ ids = ["total_only", "total_above_its_parts"],
+)
+def test_a_turn_whose_total_exceeds_its_parts_prices_the_context_from_it(executed, last_usage):
+ transport = FakeTransport(
+ [
+ [_WEB_SEARCH_CALL, _sse(finish = "tool_calls"), _usage_line(100, 5), _DONE],
+ [
+ _sse({"content": "done"}),
+ _sse(finish = "stop"),
+ "data: " + json.dumps({"choices": [], "usage": last_usage}),
+ _DONE,
+ ],
+ ]
+ )
+ usages = _usage_payloads(_run(transport))
+
+ assert len(usages) == 1
+ assert usages[0]["total_tokens"] == 405
+ assert usages[0]["context_tokens"] == 300
def test_a_repeated_identical_call_does_not_re_execute(executed):
diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.ts
index 2fdd1f676a4..1f044b88809 100644
--- a/studio/frontend/scripts/check-bundle-budget.ts
+++ b/studio/frontend/scripts/check-bundle-budget.ts
@@ -301,8 +301,11 @@ export const BUDGET = {
// Explicit browser download names retain the prior startup margin.
// The About tab's iGPU memory row (#14317): merged with main at ca0252968 the build measures 6,848,044 B raw /
// 2,062,332 B transfer (Node 24, 96 chunks). Raised to that plus the same 160 B raw / 189 B transfer margin.
- transferBytes: 2_062_521,
- rawBytes: 6_848_204,
+ // Token usage tracking (#14076) keeps the saved-count matching, recount scheduling and their review guards in the
+ // eager chat runtime: merged with main at 4e0fe5b99 the build measures 6,857,322 B raw / 2,064,700 B transfer
+ // (Node 24, 96 chunks). Raised to that plus the same 160 B raw / 189 B transfer margin.
+ transferBytes: 2_064_889,
+ rawBytes: 6_857_482,
};
// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering the
diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.ts
index abcec00c0e6..d6f1adf6ae9 100644
--- a/studio/frontend/src/features/chat/api/chat-adapter.ts
+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts
@@ -132,6 +132,8 @@ import {
releasePreStreamRunReservation,
} from "../utils/pre-stream-run-reservation";
import { readThreadCreationClaim } from "../utils/chat-thread-creation-claim";
+import { contextUsageMarkers, threadBranchRecords } from "../utils/saved-context-usage";
+import { contextUsageReasoningKey } from "../utils/context-usage-reasoning";
import {
apiCompactionRequestFields,
ggufCompactionRequestFields,
@@ -2240,7 +2242,9 @@ export async function buildLocalTokenCountHistory(
/** The reasoning fields a completion would send: llama-server falls back to load-time
* --chat-template-kwargs only for omitted keys, so sending none misreports the mode. */
-export function buildLocalTokenCountReasoning(): Record<string, unknown> {
+export function buildLocalTokenCountReasoning(
+ state = useChatRuntimeStore.getState(),
+): Record<string, unknown> {
const {
supportsReasoning,
reasoningStyle,
@@ -2249,7 +2253,7 @@ export function buildLocalTokenCountReasoning(): Record<string, unknown> {
reasoningEffortLevels,
supportsPreserveThinking,
preserveThinking,
- } = useChatRuntimeStore.getState();
+ } = state;
// Same clamp the request build applies: a level the loaded template does not offer is dropped by the backend.
const localReasoningEffort = clampReasoningEffortToLevels(
reasoningEffort,
@@ -2303,7 +2307,21 @@ export async function buildLocalTokenCountExtras(
// No budget, because the completion sends none either, so a policy that injects tools
// past this false gets the server default on both sides.
// The completion always sends thread_id; the server dates a thread's prompt from it.
- const threadField = threadId ? { thread_id: threadId } : {};
+ const threadField = {
+ ...(threadId ? { thread_id: threadId } : {}),
+ max_tokens: state.params.maxTokens,
+ ...ggufCompactionRequestFields({
+ isGguf: isServedByLlamaCpp({
+ loadedIsGguf: state.loadedIsGguf,
+ activeGgufVariant: state.activeGgufVariant,
+ activeNativePathToken: state.activeNativePathToken,
+ checkpoint: state.params.checkpoint,
+ }),
+ isMlx: state.loadedIsMlx === true,
+ isManagedEngine: state.loadedEngine === "vllm",
+ autoCompactEnabled: state.autoCompactEnabled,
+ }),
+ };
if (!supportsTools) {
return {
enable_tools: false,
@@ -6471,6 +6489,14 @@ export function createOpenAIStreamAdapter(
: {}
: { thinking: { type: reasoningEnabled ? "enabled" : "disabled" } }
: {};
+ const contextReasoningKey = isExternalRequest
+ ? undefined
+ : contextUsageReasoningKey({
+ ...localReasoningFields,
+ ...(runtime.supportsPreserveThinking
+ ? { preserve_thinking: runtime.preserveThinking }
+ : {}),
+ });
// Decided before the continuation yield below, which an abort during load saves as is.
// A carried thought is reasoning whatever this request's thinking setting says.
setParseThink(
@@ -6547,10 +6573,15 @@ export function createOpenAIStreamAdapter(
// the bar after a switch to B. A first turn is adopted onto an id mid-run and autosave moves
// activeThreadId with it, so read the adopted key, or the bar stays blank for life.
const publishContextUsage = (
- usage: NonNullable<
+ reported: NonNullable<
ReturnType<typeof useChatRuntimeStore.getState>["contextUsage"]
>,
) => {
+ const usage = {
+ ...reported,
+ modelId: params.checkpoint,
+ ...(contextReasoningKey ? { reasoningKey: contextReasoningKey } : {}),
+ };
const usageKey = liveThreadKey(serverCancel);
const usageThreadKey = usageKey === "__default" ? null : usageKey;
const store = useChatRuntimeStore.getState();
@@ -9020,6 +9051,11 @@ export function createOpenAIStreamAdapter(
cachedTokens,
cacheWriteTokens,
modelId: params.checkpoint,
+ ...(contextReasoningKey ? { reasoningKey: contextReasoningKey } : {}),
+ ...contextUsageMarkers([
+ ...threadBranchRecords(rawMessages),
+ { id: "", role: "assistant", content: finalContent },
+ ]),
}
: undefined,
responseDetails: buildResponseDetails(finishedAt),
diff --git a/studio/frontend/src/features/chat/api/chat-api.ts b/studio/frontend/src/features/chat/api/chat-api.ts
index f4f483012e2..04defb1ca98 100644
--- a/studio/frontend/src/features/chat/api/chat-api.ts
+++ b/studio/frontend/src/features/chat/api/chat-api.ts
@@ -472,11 +472,11 @@ export async function countChatInputTokens(payload: {
/** Run the selected tools here rather than as the provider's hosted builtins. */
run_tools_locally?: boolean;
// `model` is informational: the endpoint counts with whatever is resident and reports which.
-}): Promise<{ input_tokens: number; model?: string }> {
+}, serializedPayload = JSON.stringify(payload)): Promise<{ input_tokens: number; model?: string }> {
const response = await authFetch("/api/inference/chat/count_tokens", {
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify(payload),
+ body: serializedPayload,
});
return parseJsonOrThrow<{ input_tokens: number; model?: string }>(response);
}
diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsx
index 2fd304c3374..624029c2069 100644
--- a/studio/frontend/src/features/chat/chat-page.tsx
+++ b/studio/frontend/src/features/chat/chat-page.tsx
@@ -179,6 +179,7 @@ import {
type ChatModelSwitchTarget,
} from "./components/chat-model-notice-switch";
import { ContextUsageBar } from "./components/context-usage-bar";
+import { contextUsageRows } from "./lib/context-usage-bar-state";
import { ModelLoadInlineStatus } from "./components/model-load-status";
import { ProjectSwitcher } from "./components/project-switcher";
import { EditProjectDialog } from "./components/edit-project-dialog";
@@ -301,7 +302,11 @@ import {
} from "./utils/document-annotations";
import { requestTemporaryPromptQueueStop } from "./utils/prompt-queue-boundary";
import { savedBranchHead } from "./utils/branch-head";
-import { estimateContextUsage } from "./utils/estimate-chat-tokens";
+import { branchContextUsage, estimateBranch } from "./utils/saved-context-usage";
+import {
+ currentContextUsageReasoningKey,
+ refreshContextUsage,
+} from "./utils/refresh-context-usage";
import { orderBySelectedBranch } from "./utils/message-order";
import { isAssistantLocalThreadId } from "./utils/thread-ids";
import {
@@ -456,31 +461,6 @@ function getExternalProviderDropdownRank(providerType: string): number {
return EXTERNAL_PROVIDER_DROPDOWN_ORDER[providerType] ?? 2;
}
-type RuntimeStoreState = ReturnType<typeof useChatRuntimeStore.getState>;
-
-// The newest saved usage if the active checkpoint and window could have produced it, else null.
-function savedUsageFor(
- messages: MessageRecord[],
- store: RuntimeStoreState,
-): RuntimeStoreState["contextUsage"] {
- const msg = [...messages].sort((a, b) => b.createdAt - a.createdAt)[0];
- const usage = msg?.metadata?.contextUsage as RuntimeStoreState["contextUsage"];
- if (!usage) return null;
- const activeCheckpoint = store.params.checkpoint;
- const usageModelId = (usage as { modelId?: unknown }).modelId;
- if (typeof usageModelId === "string" && usageModelId) {
- if (!activeCheckpoint || usageModelId !== activeCheckpoint) {
- return null;
- }
- }
- // llama.cpp stops at the window, so a count past it is stale; MLX runs past it, so its count stands.
- const limit = store.loadedIsGguf ? store.loadedContextLength : null;
- if (typeof limit === "number" && limit > 0 && (usage.contextTokens ?? usage.totalTokens ?? 0) > limit) {
- return null;
- }
- return usage;
-}
-
function messageHasImage(message: MessageRecord): boolean {
const contentParts = Array.isArray(message.content) ? message.content : [];
if (contentParts.some((part) => part.type === "image")) {
@@ -2816,6 +2796,15 @@ export function ChatPage({
(state) => state.nativeContextLength,
);
const contextUsage = useChatRuntimeStore((state) => state.contextUsage);
+ const loadedEngine = useChatRuntimeStore((state) => state.loadedEngine);
+ const usageRows = useMemo(
+ () =>
+ contextUsageRows(
+ contextUsage,
+ isExternalModelId(contextUsage?.modelId) || loadedEngine !== "auto",
+ ),
+ [contextUsage, loadedEngine],
+ );
const loadedIsGguf = useChatRuntimeStore((state) => state.loadedIsGguf);
const loadedContextUnboundedWhenBatched = useChatRuntimeStore(
(state) => state.loadedContextUnboundedWhenBatched,
@@ -4371,12 +4360,24 @@ export function ChatPage({
messages,
savedBranchHead(threadId, messages),
);
- const usage = savedUsageFor(branch, store) ?? estimateContextUsage(branch);
+ const checkpoint = store.params.checkpoint;
+ const localWindow =
+ checkpoint && !isExternalModelId(checkpoint) ? store.loadedContextLength : null;
+ const usage =
+ branchContextUsage(
+ branch,
+ checkpoint,
+ localWindow,
+ store.loadedIsGguf ? localWindow : null,
+ currentContextUsageReasoningKey(),
+ ) ??
+ estimateBranch(branch);
if (!usage) return;
// key usage by the restored thread because this read can outlast a thread switch.
store.setThreadContextUsage(threadId, usage);
if (store.activeThreadId === threadId) {
store.setContextUsage(usage);
+ if (usage.estimated) void refreshContextUsage({ threadId });
}
})
.catch((error) => {
@@ -4899,15 +4900,11 @@ export function ChatPage({
used={contextUsage?.contextTokens ?? contextUsage?.totalTokens ?? null}
// null on external providers; the bar handles that.
total={loadedContextLength}
- cached={contextUsage?.cachedTokens}
- cacheWrites={contextUsage?.cacheWriteTokens}
- promptTokens={contextUsage?.promptTokens}
+ cached={usageRows.cached}
+ cacheWrites={usageRows.cacheWrites}
+ promptTokens={usageRows.promptTokens}
// A tool turn's completionTokens sums every pass; the context holds only the last one.
- completionTokens={
- contextUsage?.contextTokens !== undefined
- ? contextUsage.contextTokens - contextUsage.promptTokens
- : contextUsage?.completionTokens
- }
+ completionTokens={usageRows.completionTokens}
compactionCount={compactionCount}
isMlx={isServedByMlx(
Boolean(loadedIsGguf),
diff --git a/studio/frontend/src/features/chat/lib/context-usage-bar-state.ts b/studio/frontend/src/features/chat/lib/context-usage-bar-state.ts
index 47c8e4009b4..8c8814a8275 100644
--- a/studio/frontend/src/features/chat/lib/context-usage-bar-state.ts
+++ b/studio/frontend/src/features/chat/lib/context-usage-bar-state.ts
@@ -4,6 +4,7 @@
// its own plain module so the node suite can drive it: the component is JSX the runner cannot import
export const formatTokenCount = (n: number): string => {
+ if (n >= 999_950) return `${(n / 1_000_000).toFixed(1)}M`;
if (n >= 1000) return `${(n / 1000).toFixed(1)}k`;
return String(n);
};
@@ -34,6 +35,46 @@ export type ContextUsageBarInput = {
estimated?: boolean;
};
+export type ContextUsageRows = Pick<
+ ContextUsageBarInput,
+ "promptTokens" | "completionTokens" | "cached" | "cacheWrites"
+>;
+
+export function contextUsageRows(
+ usage:
+ | {
+ promptTokens: number;
+ completionTokens: number;
+ contextTokens?: number;
+ cachedTokens?: number;
+ cacheWriteTokens?: number;
+ }
+ | null
+ | undefined,
+ summedPrompt: boolean,
+): ContextUsageRows {
+ if (!usage) return {};
+ const context = usage.contextTokens;
+ if (context === undefined) {
+ return {
+ promptTokens: usage.promptTokens,
+ completionTokens: usage.completionTokens,
+ cached: usage.cachedTokens,
+ cacheWrites: usage.cacheWriteTokens,
+ };
+ }
+ const describesContext = summedPrompt
+ ? context === usage.promptTokens + usage.completionTokens
+ : context >= usage.promptTokens;
+ if (!describesContext) return {};
+ return {
+ promptTokens: usage.promptTokens,
+ completionTokens: context - usage.promptTokens,
+ cached: usage.cachedTokens,
+ cacheWrites: usage.cacheWriteTokens,
+ };
+}
+
/**
* Which limit warning the tooltip carries, if any.
*
@@ -109,7 +150,7 @@ export function deriveContextUsageBar({
}: ContextUsageBarInput): ContextUsageBarState | null {
const limit = typeof total === "number" && total > 0 ? total : null;
const usedTokens =
- typeof used === "number" && Number.isFinite(used) ? used : null;
+ typeof used === "number" && Number.isFinite(used) && used >= 0 ? used : null;
// no per-turn rows and no limit advice: neither is known from a guess
if (estimated && usedTokens !== null && usedTokens > 0) {
diff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsx
index 09ce3103c08..21a8760c681 100644
--- a/studio/frontend/src/features/chat/runtime-provider.tsx
+++ b/studio/frontend/src/features/chat/runtime-provider.tsx
@@ -42,6 +42,8 @@ import {
useRef,
} from "react";
import { toast } from "sonner";
+import { useBrowserAgentStore } from "@/features/browser/agent/agent-store";
+import { browserAgentToolsForTurn } from "@/features/browser/agent/tools";
import { StudioDictationAdapter } from "./adapters/studio-dictation-adapter";
import { StudioSpeechSynthesisAdapter } from "./adapters/studio-speech-synthesis-adapter";
import {
@@ -141,6 +143,7 @@ import {
ingestResearchUpdate,
useResearchRunStore,
} from "./stores/research-run-store";
+import { usePromptQueueUI } from "./stores/prompt-queue-ui-store";
import { ToolPaneScopeContext, toolPaneScope } from "./tool-output-scope";
import { ChatProjectScopeContext } from "./chat-project-scope";
import { readThreadCreationClaim } from "./utils/chat-thread-creation-claim";
@@ -212,7 +215,15 @@ import {
orderParentsFirst,
resolveSavedBranchHead,
} from "./utils/message-order";
-import { estimateContextUsage } from "./utils/estimate-chat-tokens";
+import {
+ branchContextUsage,
+ contextUsageMarkers,
+ estimateBranch,
+ revisionTotal,
+ sameContextUsage,
+ threadBranchRecords,
+ withMessageRevision,
+} from "./utils/saved-context-usage";
import {
awaitStoredChatThreadWrites,
deleteStoredChatThreads,
@@ -266,6 +277,8 @@ import {
requestTemporaryPromptQueueStop,
} from "./utils/prompt-queue-boundary";
import {
+ currentContextUsageReasoningKey,
+ currentContextUsageSettingsKey,
refreshContextUsage,
setActiveBranchReader,
} from "./utils/refresh-context-usage";
@@ -1374,7 +1387,10 @@ function toThreadMessage(m: MessageRecord): ThreadMessage {
content: content as Extract<ThreadMessage, { role: "user" }>["content"],
attachments: cloneAttachments(m.attachments),
metadata: {
- custom: m.metadata?.createdAtEstimated === true ? { createdAtEstimated: true } : {},
+ custom: {
+ ...(m.metadata?.createdAtEstimated === true ? { createdAtEstimated: true } : {}),
+ ...(typeof m.metadata?.revision === "number" ? { revision: m.metadata.revision } : {}),
+ },
},
};
}
@@ -1437,6 +1453,7 @@ const generationRecoveries = new Map<string, GenerationRecovery>();
function scheduleGenerationRecovery(
threadId: string,
storedMessage: MessageRecord,
+ records: MessageRecord[],
aui: ReturnType<typeof useAui>,
): void {
const metadata = (storedMessage.metadata ?? {}) as Record<string, unknown>;
@@ -1451,6 +1468,7 @@ function scheduleGenerationRecovery(
return;
}
const views = new Set([aui]);
+ const history = orderBySelectedBranch(records, storedMessage.id).slice(0, -1);
const recovery = (async () => {
let cursor = Number(metadata.generationSeq ?? 0);
@@ -1616,6 +1634,7 @@ function scheduleGenerationRecovery(
timings: recoveryTimings,
});
if (nextMetadata.generationSettled === true) {
+ const content = rebuild();
nextMetadata = recoveredGenerationFinalMetadata({
current: nextMetadata,
run,
@@ -1623,7 +1642,11 @@ function scheduleGenerationRecovery(
timings: recoveryTimings,
firstChunkAt,
totalChunks,
- toolCalls: toolNames(rebuild()),
+ toolCalls: toolNames(content),
+ markers: contextUsageMarkers([
+ ...history,
+ { id: storedMessage.id, role: "assistant", content },
+ ]),
});
}
const settled = nextMetadata.generationSettled === true;
@@ -2511,7 +2534,7 @@ function useStudioRuntimeAdapters(
typeof (message.metadata as Record<string, unknown> | undefined)
?.generationRunId === "string"
) {
- scheduleGenerationRecovery(remoteId, message, aui);
+ scheduleGenerationRecovery(remoteId, message, messages, aui);
}
}
})
@@ -2598,6 +2621,11 @@ function useStudioRuntimeAdapters(
attachments?: readonly { id: string }[];
}
).attachments;
+ if (
+ !ids.includes(attachmentId) &&
+ !(Array.isArray(attachments) &&
+ attachments.some((attachment) => attachment.id === attachmentId))
+ ) return item;
return {
...item,
message: {
@@ -2605,6 +2633,7 @@ function useStudioRuntimeAdapters(
content: latestContent.filter(
(_, index) => ids[index] !== attachmentId,
),
+ metadata: withMessageRevision(item.message.metadata),
...(Array.isArray(attachments)
? {
attachments: attachments.filter(
@@ -2641,6 +2670,7 @@ function useStudioRuntimeAdapters(
attachments: attachments.filter(
(attachment) => attachment.id !== attachmentId,
),
+ metadata: withMessageRevision(message.metadata),
} as typeof message,
};
}
@@ -2664,6 +2694,7 @@ function useStudioRuntimeAdapters(
message: {
...message,
content: content.filter((_, i) => i !== idx),
+ metadata: withMessageRevision(message.metadata),
} as typeof message,
};
}
@@ -2863,6 +2894,7 @@ function useStudioRuntimeAdapters(
content: cloneContent(message.content),
metadata: { ...(message.metadata ?? {}) },
},
+ msgs,
aui,
);
}
@@ -2877,36 +2909,19 @@ function useStudioRuntimeAdapters(
);
const branch = orderBySelectedBranch(msgs, headId);
- // restore usage from the selected branch's last assistant message when the model matches
- const lastAssistant = [...branch]
- .reverse()
- .find((m) => m.role === "assistant");
- const savedUsage = (lastAssistant?.metadata as Record<string, unknown>)
- ?.contextUsage as
- | {
- promptTokens: number;
- completionTokens: number;
- totalTokens: number;
- contextTokens?: number;
- cachedTokens: number;
- cacheWriteTokens?: number;
- modelId?: string;
- }
- | undefined;
const store = useChatRuntimeStore.getState();
- // reject usage beyond a local GGUF window; MLX and external providers have no local bound
- const localLimit = store.loadedIsGguf ? store.loadedContextLength : null;
- const withinLocalLimit =
- !localLimit || (savedUsage?.contextTokens ?? savedUsage?.totalTokens ?? 0) <= localLimit;
- // trust legacy usage only when a known local window prevents cross-provider attribution
- const modelMatches = savedUsage?.modelId
- ? savedUsage.modelId === store.params.checkpoint
- : typeof store.loadedContextLength === "number" &&
- store.loadedContextLength > 0;
- // retain the usage object so TypeScript preserves narrowing for later writes
- const restoredUsage =
- savedUsage && withinLocalLimit && modelMatches ? savedUsage : null;
- const shownUsage = restoredUsage ?? estimateContextUsage(branch);
+ const checkpoint = store.params.checkpoint;
+ const localWindow =
+ checkpoint && !isExternalModelId(checkpoint) ? store.loadedContextLength : null;
+ const shownUsage =
+ branchContextUsage(
+ branch,
+ checkpoint,
+ localWindow,
+ store.loadedIsGguf ? localWindow : null,
+ currentContextUsageReasoningKey(),
+ ) ??
+ estimateBranch(branch);
if (shownUsage) {
// key usage by the loaded thread because the active thread may change during awaits
store.setThreadContextUsage(remoteId, shownUsage);
@@ -2915,7 +2930,7 @@ function useStudioRuntimeAdapters(
}
}
// recount only without exact usage; model switches qualify, and compare panes never own the bar (#7450)
- if (!restoredUsage && modelType === "base" && !pairId) {
+ if ((!shownUsage || shownUsage.estimated) && modelType === "base" && !pairId) {
void refreshContextUsage({ threadId: remoteId });
}
@@ -3814,7 +3829,9 @@ function ActiveBranchRegistrar({
}
setActiveBranchReader(() => {
try {
- return aui.thread().getState().messages;
+ if (aui.threads().getState().mainThreadId !== useChatRuntimeStore.getState().activeThreadId) return null;
+ const state = aui.thread().getState();
+ return state.isLoading ? null : state.messages;
} catch {
// without a mounted thread, recount from stored records
return null;
@@ -3860,10 +3877,134 @@ function ThreadContextUsageRecount({
const checkpoint = useChatRuntimeStore((s) => s.params.checkpoint);
const loadedContextLength = useChatRuntimeStore((s) => s.loadedContextLength);
const modelLoading = useChatRuntimeStore((s) => s.modelLoading);
+ const reasoningSettings = useChatRuntimeStore(currentContextUsageReasoningKey);
+ const browserTools = useBrowserAgentStore(() => browserAgentToolsForTurn().join(","));
+ const promptSettings = useChatRuntimeStore((state) => currentContextUsageSettingsKey(state, browserTools));
+ const threadSettingsPending = useChatRuntimeStore((s) => s.threadScopedSettingsPending);
+ const threadLoading = useAuiState(({ thread }) => thread.isLoading);
+ const savedUsage = useAuiState(({ thread }) => thread.messages.at(-1)?.metadata?.custom?.contextUsage);
+ // When prompt settings last changed per thread and model: older saved counts priced another prompt.
+ const promptChangedAtRef = useRef(new Map<string, number>());
+ const pricedReasoningRef = useRef<{
+ threadId: string | null;
+ checkpoint: string;
+ settings: string | undefined;
+ pending: boolean;
+ loading: boolean;
+ promptSettings: string;
+ savedUsage: unknown;
+ stale: boolean;
+ } | null>(null);
// subscribe to run state so a recount skipped while busy is retried when the run ends
const runActive = useChatRuntimeStore((s) =>
Object.values(s.runningByThreadId).some(Boolean),
);
+ const ownRunActive = useChatRuntimeStore((s) =>
+ Boolean(s.activeThreadId && s.runningByThreadId[s.activeThreadId]),
+ );
+ const queuedPromptPending = usePromptQueueUI((s) => {
+ const entry = activeThreadId ? s.byThreadId[activeThreadId] : undefined;
+ return Boolean(entry && !entry.paused);
+ });
+ const tipId = useAuiState(({ thread }) => thread.messages.at(-1)?.id);
+ const tipRunning = useAuiState(({ thread }) => {
+ const last = thread.messages.at(-1);
+ return last?.role === "assistant" && last.status?.type === "running";
+ });
+ const branchLength = useAuiState(({ thread }) => thread.messages.length);
+ const branchRevision = useAuiState(({ thread }) => revisionTotal(thread.messages));
+ const pricedTipRef = useRef<{
+ threadId: string;
+ tipId: string | undefined;
+ length: number;
+ revision: number;
+ ran: boolean;
+ } | null>(null);
+
+ useEffect(() => {
+ const previous = pricedReasoningRef.current;
+ if (
+ previous &&
+ previous.threadId === activeThreadId &&
+ previous.checkpoint === checkpoint &&
+ !previous.pending &&
+ !previous.loading &&
+ previous.promptSettings !== promptSettings
+ ) {
+ promptChangedAtRef.current.set(JSON.stringify([activeThreadId, checkpoint]), Date.now());
+ }
+ const stale = Boolean(
+ previous &&
+ previous.threadId === activeThreadId &&
+ previous.checkpoint === checkpoint &&
+ previous.savedUsage === savedUsage &&
+ !previous.pending &&
+ !previous.loading &&
+ (previous.stale || previous.promptSettings !== promptSettings),
+ );
+ pricedReasoningRef.current = {
+ threadId: activeThreadId,
+ checkpoint,
+ settings: reasoningSettings,
+ pending: threadSettingsPending,
+ loading: threadLoading,
+ promptSettings,
+ savedUsage,
+ stale,
+ };
+ if (
+ !enabled ||
+ modelLoading ||
+ threadLoading ||
+ threadSettingsPending ||
+ !previous ||
+ previous.threadId !== activeThreadId ||
+ previous.checkpoint !== checkpoint ||
+ (previous.settings === reasoningSettings && ((!previous.pending && !previous.loading) || reasoningSettings === undefined)) ||
+ !checkpoint ||
+ isExternalModelId(checkpoint)
+ ) {
+ return;
+ }
+ const store = useChatRuntimeStore.getState();
+ const shown = store.contextUsage;
+ if (shown != null && !shown.estimated && shown.reasoningKey === reasoningSettings) return;
+ try {
+ if (activeThreadId && aui.threads().getState().mainThreadId !== activeThreadId) return;
+ const thread = activeThreadId ? aui.thread().getState() : null;
+ if (thread?.isLoading) return;
+ const messages = thread?.messages ?? [];
+ const branch = threadBranchRecords(messages);
+ let usage = branchContextUsage(
+ branch,
+ checkpoint,
+ loadedContextLength,
+ store.loadedIsGguf ? loadedContextLength : null,
+ currentContextUsageReasoningKey(),
+ );
+ if (
+ usage != null &&
+ !usage.estimated &&
+ (stale ||
+ (usage.reasoningKey === undefined &&
+ !previous.pending &&
+ previous.settings !== reasoningSettings))
+ ) usage = null;
+ store.setContextUsage(usage ?? (shown ? { ...shown, estimated: true } : estimateBranch(branch)));
+ if (usage != null && !usage.estimated) return;
+ if (
+ loadedContextLength == null ||
+ (!store.loadedIsGguf && !store.loadedIsMlx) ||
+ messagesContainImage(messages) ||
+ findLatestUserAudioBase64(messages) ||
+ findLatestUserVideoBase64(messages) ||
+ (!store.loadedIsGguf && messages.some((message) => message.metadata?.custom?.contextTruncation))
+ ) return;
+ } catch {
+ return;
+ }
+ void refreshContextUsage();
+ }, [activeThreadId, aui, checkpoint, enabled, loadedContextLength, modelLoading, promptSettings, reasoningSettings, savedUsage, threadLoading, threadSettingsPending]);
useEffect(() => {
if (
@@ -3915,32 +4056,132 @@ function ThreadContextUsageRecount({
) {
return;
}
- // Only the tip's count describes the branch: a later turn whose run never answered is unpriced.
- const tip = messages.at(-1);
- const saved = (tip?.role === "assistant" ? tip.metadata?.custom?.contextUsage : undefined) as
- | (NonNullable<typeof shown> & { modelId?: string })
- | undefined;
// The history loader's rules, keyed on the SELECTED model (a resident GGUF stays loaded under a
// cloud pick): llama.cpp stops at its window (MLX runs past it), and legacy usage without a
// modelId is trusted only under a local window.
const store = useChatRuntimeStore.getState();
- const ggufWindow = store.loadedIsGguf ? localWindow : null;
- const withinWindow =
- !ggufWindow || (saved?.contextTokens ?? saved?.totalTokens ?? 0) <= ggufWindow;
- const modelMatches = saved?.modelId
- ? saved.modelId === checkpoint
- : localWindow != null && localWindow > 0;
+ const branch = threadBranchRecords(messages);
const usage =
- saved && withinWindow && modelMatches
- ? saved
- : estimateContextUsage(
- messages.map((m, index) => ({ ...m, createdAt: index })) as unknown as MessageRecord[],
- );
+ branchContextUsage(
+ branch,
+ checkpoint,
+ localWindow,
+ store.loadedIsGguf ? localWindow : null,
+ currentContextUsageReasoningKey(),
+ ) ??
+ estimateBranch(branch);
if (!usage) return;
store.setThreadContextUsage(activeThreadId, usage);
store.setContextUsage(usage);
}, [activeThreadId, aui, checkpoint, enabled, loadedContextLength, modelLoading, runActive]);
+ useEffect(() => {
+ if (!enabled || !activeThreadId || modelLoading) return;
+ let messages: readonly ThreadMessage[];
+ try {
+ if (aui.threads().getState().mainThreadId !== activeThreadId) return;
+ messages = aui.thread().getState().messages;
+ } catch {
+ return;
+ }
+ const store = useChatRuntimeStore.getState();
+ const tip = messages.at(-1);
+ const priced = pricedTipRef.current;
+ const current = {
+ threadId: activeThreadId,
+ tipId: tip?.id,
+ length: messages.length,
+ revision: branchRevision,
+ ran: false,
+ };
+ if (priced?.threadId !== activeThreadId || priced.tipId == null) {
+ pricedTipRef.current = { ...current, ran: ownRunActive };
+ return;
+ }
+ if (ownRunActive) {
+ if (!priced.ran) pricedTipRef.current = { ...priced, ran: true };
+ return;
+ }
+ if (
+ priced.tipId === tip?.id &&
+ priced.length === messages.length &&
+ priced.revision === branchRevision
+ ) {
+ if (priced.ran) pricedTipRef.current = current;
+ return;
+ }
+ if (tipRunning) return;
+ if (!tip) {
+ pricedTipRef.current = current;
+ store.setContextUsage(null);
+ if (!runActive && !queuedPromptPending) {
+ void refreshContextUsage({ threadId: activeThreadId });
+ }
+ return;
+ }
+ const localWindow =
+ checkpoint && !isExternalModelId(checkpoint) ? loadedContextLength : null;
+ const branch = threadBranchRecords(messages);
+ let usage = branchContextUsage(
+ branch,
+ checkpoint,
+ localWindow,
+ store.loadedIsGguf ? localWindow : null,
+ currentContextUsageReasoningKey(),
+ );
+ const promptChangedAt = promptChangedAtRef.current.get(JSON.stringify([activeThreadId, checkpoint]));
+ if (
+ usage != null &&
+ !usage.estimated &&
+ promptChangedAt !== undefined &&
+ (tip.createdAt instanceof Date ? tip.createdAt.getTime() : 0) < promptChangedAt
+ ) usage = { ...usage, estimated: true };
+ const exact = usage != null && !usage.estimated;
+ const recount =
+ !exact &&
+ localWindow != null &&
+ !messagesContainImage(messages) &&
+ !findLatestUserAudioBase64(messages) &&
+ !findLatestUserVideoBase64(messages);
+ if (recount && (runActive || queuedPromptPending)) {
+ store.setThreadContextUsage(activeThreadId, null);
+ return;
+ }
+ pricedTipRef.current = current;
+ const shown = store.contextUsage;
+ if (priced.ran && (shown == null || shown.estimated)) return;
+ if (exact && usage != null) {
+ if (!sameContextUsage(shown, usage, checkpoint)) {
+ store.setThreadContextUsage(activeThreadId, usage);
+ store.setContextUsage(usage);
+ }
+ return;
+ }
+ if (recount) {
+ store.setContextUsage(usage ?? estimateBranch(branch));
+ void refreshContextUsage({ threadId: activeThreadId });
+ return;
+ }
+ if (usage == null && priced.ran && shown != null && !shown.estimated) return;
+ const next = usage ?? estimateBranch(branch);
+ store.setThreadContextUsage(activeThreadId, next);
+ store.setContextUsage(next);
+ }, [
+ activeThreadId,
+ aui,
+ branchLength,
+ branchRevision,
+ checkpoint,
+ enabled,
+ loadedContextLength,
+ modelLoading,
+ ownRunActive,
+ queuedPromptPending,
+ runActive,
+ tipId,
+ tipRunning,
+ ]);
+
return null;
}
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 4f4bb56cd1d..0c67f771d6c 100644
--- a/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
+++ b/studio/frontend/src/features/chat/stores/chat-runtime-store.ts
@@ -2431,6 +2431,8 @@ type ContextUsageSnapshot = {
cachedTokens: number;
// Anthropic-only; optional so pre-cache-stats persisted entries load.
cacheWriteTokens?: number;
+ modelId?: string;
+ reasoningKey?: string;
// a text-length guess from storage, replaced by any count
estimated?: boolean;
};
@@ -2945,7 +2947,7 @@ type ChatRuntimeStore = {
/** A finished run's usage, kept per thread so switching back re-applies it. */
setThreadContextUsage: (
threadId: string,
- usage: ContextUsageSnapshot,
+ usage: ContextUsageSnapshot | null,
) => void;
};
@@ -5194,13 +5196,17 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
// Re-apply the incoming thread's own usage rather than blanking the bar: a run that finished in the
// background never wrote the visible value, and a still-mounted runtime skips the history loader.
setActiveThreadId: (activeThreadId) =>
- set((state) => ({
- activeThreadId,
- activeThreadEpoch: state.activeThreadEpoch + 1,
- contextUsage: activeThreadId
+ set((state) => {
+ const own = activeThreadId
? (state.contextUsageByThreadId[activeThreadId] ?? null)
- : null,
- })),
+ : null;
+ return {
+ activeThreadId,
+ activeThreadEpoch: state.activeThreadEpoch + 1,
+ contextUsage:
+ own && own.modelId && own.modelId !== state.params.checkpoint ? null : own,
+ };
+ }),
applyThreadScopedSettings: (threadId, settings) =>
set((state) => {
settings = normalizeSavedThreadScopedSettings(settings);
@@ -6274,22 +6280,37 @@ export const useChatRuntimeStore = create<ChatRuntimeStore>((set, get) => ({
// survives a switch: that loader runs once per mount and setActiveThreadId reads the map.
setContextUsage: (contextUsage) =>
set((state) => {
- if (!state.activeThreadId) return { contextUsage };
+ const stamped =
+ contextUsage && !contextUsage.modelId && state.params.checkpoint
+ ? { ...contextUsage, modelId: state.params.checkpoint }
+ : contextUsage;
+ if (!state.activeThreadId) return { contextUsage: stamped };
const next = { ...state.contextUsageByThreadId };
- if (contextUsage) {
- next[state.activeThreadId] = contextUsage;
+ if (stamped) {
+ next[state.activeThreadId] = stamped;
} else {
delete next[state.activeThreadId];
}
- return { contextUsage, contextUsageByThreadId: next };
+ return { contextUsage: stamped, contextUsageByThreadId: next };
}),
setThreadContextUsage: (threadId, usage) =>
- set((state) => ({
- contextUsageByThreadId: {
- ...state.contextUsageByThreadId,
- [threadId]: usage,
- },
- })),
+ set((state) => {
+ if (!usage) {
+ if (!Object.hasOwn(state.contextUsageByThreadId, threadId)) return state;
+ const next = { ...state.contextUsageByThreadId };
+ delete next[threadId];
+ return { contextUsageByThreadId: next };
+ }
+ return {
+ contextUsageByThreadId: {
+ ...state.contextUsageByThreadId,
+ [threadId]:
+ usage.modelId || !state.params.checkpoint
+ ? usage
+ : { ...usage, modelId: state.params.checkpoint },
+ },
+ };
+ }),
}));
// Mirror token edits made through the shared store (e.g. Unsloth's field).
diff --git a/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts b/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts
index 3a9a69ffd2d..3503317f0ff 100644
--- a/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts
+++ b/studio/frontend/src/features/chat/utils/chat-generation-recovery.ts
@@ -7,6 +7,9 @@ import {
extractDeltaText,
parseAssistantContent,
} from "./parse-assistant-content";
+import type { ContextUsageMarkers } from "./saved-context-usage";
+import { isExternalModelId } from "../external-providers";
+import { contextUsageReasoningKey, type ContextUsageReasoningFields } from "./context-usage-reasoning";
export type StoredGenerationStatus = ChatGenerationStatus;
@@ -182,7 +185,7 @@ export function recoveredGenerationFinalMetadata(options: {
current: Record<string, unknown>;
run: {
id: string;
- requestPayload: { model?: unknown };
+ requestPayload: { model?: unknown } & ContextUsageReasoningFields;
createdAt: number;
startedAt: number | null;
completedAt: number | null;
@@ -192,8 +195,9 @@ export function recoveredGenerationFinalMetadata(options: {
firstChunkAt?: number;
totalChunks: number;
toolCalls?: string[];
+ markers: ContextUsageMarkers;
}): Record<string, unknown> {
- const { current, run, usage, timings, firstChunkAt, totalChunks } = options;
+ const { current, run, usage, timings, firstChunkAt, totalChunks, markers } = options;
const modelId =
typeof run.requestPayload.model === "string"
? run.requestPayload.model
@@ -238,6 +242,14 @@ export function recoveredGenerationFinalMetadata(options: {
: 0),
cacheWriteTokens: usageCacheWriteTokens(usage),
modelId,
+ ...(!isExternalModelId(modelId)
+ ? { reasoningKey: contextUsageReasoningKey(run.requestPayload) }
+ : {}),
+ // Revisions only grow: above zero, an edit may have landed after this run started, so the
+ // count calibrates an estimate instead of being certified for the revised branch.
+ ...(markers.revision > 0
+ ? { messageCount: markers.messageCount, estimatedTokens: markers.estimatedTokens }
+ : markers),
};
}
if (next.responseDetails === undefined) {
diff --git a/studio/frontend/src/features/chat/utils/context-usage-reasoning.ts b/studio/frontend/src/features/chat/utils/context-usage-reasoning.ts
new file mode 100644
index 00000000000..0df63df81c6
--- /dev/null
+++ b/studio/frontend/src/features/chat/utils/context-usage-reasoning.ts
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+export type ContextUsageReasoningFields = {
+ enable_thinking?: unknown;
+ thinking?: unknown;
+ reasoning_effort?: unknown;
+ preserve_thinking?: unknown;
+};
+
+export function contextUsageReasoningKey(fields: ContextUsageReasoningFields): string {
+ const thinking = fields.thinking as { type?: unknown } | undefined;
+ const enabled = typeof fields.enable_thinking === "boolean"
+ ? fields.enable_thinking
+ : thinking?.type === "enabled"
+ ? true
+ : thinking?.type === "disabled"
+ ? false
+ : null;
+ return JSON.stringify([
+ enabled,
+ typeof fields.reasoning_effort === "string" ? fields.reasoning_effort : null,
+ typeof fields.preserve_thinking === "boolean" ? fields.preserve_thinking : null,
+ ]);
+}
diff --git a/studio/frontend/src/features/chat/utils/delete-thread-message.ts b/studio/frontend/src/features/chat/utils/delete-thread-message.ts
index 10c2663030b..a727d856de0 100644
--- a/studio/frontend/src/features/chat/utils/delete-thread-message.ts
+++ b/studio/frontend/src/features/chat/utils/delete-thread-message.ts
@@ -22,6 +22,7 @@ import {
hasResearchMetadata,
reconcileServerManagedMessages,
} from "./research-message-sync";
+import { withMessageRevision } from "./saved-context-usage";
// A copy of the list, not of what is in it. assistant-ui replaces parts and attachments rather
// than mutating them, and the records built from these are serialized straight into the PUT
@@ -162,6 +163,19 @@ export async function deleteThreadMessage(args: {
}
const next = repo.export();
+ const previousParents = new Map(
+ exported.messages.map(({ message, parentId }) => [message.id, parentId]),
+ );
+ next.messages = next.messages.map((item) => {
+ if (previousParents.get(item.message.id) === item.parentId) return item;
+ return {
+ ...item,
+ message: {
+ ...item.message,
+ metadata: withMessageRevision(item.message.metadata),
+ } as ThreadMessage,
+ };
+ });
if (remoteId) {
await syncExportedRepositoryToBackend(remoteId, next, {
pruneMissing: true,
diff --git a/studio/frontend/src/features/chat/utils/estimate-chat-tokens.ts b/studio/frontend/src/features/chat/utils/estimate-chat-tokens.ts
index 4faf02870f2..a7438d93c17 100644
--- a/studio/frontend/src/features/chat/utils/estimate-chat-tokens.ts
+++ b/studio/frontend/src/features/chat/utils/estimate-chat-tokens.ts
@@ -4,6 +4,32 @@
import type { MessageRecord } from "../types";
import { orderBySelectedBranch } from "./message-order.ts";
+const RAG_SOURCES_TAIL = "\n__RAG_SOURCES__:";
+
+function modelVisibleLength(resultText: string): number {
+ const tail = resultText.lastIndexOf(RAG_SOURCES_TAIL);
+ return tail < 0 ? resultText.length : tail;
+}
+
+function partChars(part: unknown): number {
+ const value = part as {
+ type?: unknown;
+ text?: unknown;
+ argsText?: unknown;
+ result?: unknown;
+ provenance?: { source?: unknown };
+ } | null;
+ if (typeof value?.text === "string") return value.text.length;
+ if (value?.type !== "tool-call" || value.provenance?.source !== "local") return 0;
+ const result = value.result as { text?: unknown } | string | null | undefined;
+ const resultText =
+ typeof result === "string" ? result : typeof result?.text === "string" ? result.text : "";
+ return (
+ (typeof value.argsText === "string" ? value.argsText.length : 0) +
+ modelVisibleLength(resultText)
+ );
+}
+
export function estimateMessagesTokenCount(
records: readonly MessageRecord[] | null | undefined,
): number | null {
@@ -16,15 +42,12 @@ export function estimateMessagesTokenCount(
continue;
}
if (!Array.isArray(content)) continue;
- for (const part of content) {
- const text = (part as { text?: unknown } | null)?.text;
- if (typeof text === "string") chars += text.length;
- }
+ for (const part of content) chars += partChars(part);
}
return chars > 0 ? Math.max(1, Math.round(chars / 4)) : null;
}
-// Text only, about 4 characters a token: shown until a loaded model counts the chat exactly.
+// Text and Studio-run tool payloads, about 4 characters a token: shown until a loaded model counts the chat exactly.
export function estimateContextUsage(
records: readonly MessageRecord[] | null | undefined,
) {
diff --git a/studio/frontend/src/features/chat/utils/refresh-context-usage.ts b/studio/frontend/src/features/chat/utils/refresh-context-usage.ts
index 128c17fd75f..91439861375 100644
--- a/studio/frontend/src/features/chat/utils/refresh-context-usage.ts
+++ b/studio/frontend/src/features/chat/utils/refresh-context-usage.ts
@@ -11,35 +11,33 @@ import {
messagesContainImage,
} from "../api/chat-adapter";
import { countChatInputTokens } from "../api/chat-api";
+import { getMcpServerMutationEpoch } from "../api/mcp-server-mutation-tracker";
import { isExternalModelId } from "../external-providers";
-import { useChatRuntimeStore } from "../stores/chat-runtime-store";
+import { localMediaToolsForTurn, useChatRuntimeStore } from "../stores/chat-runtime-store";
import type { MessageRecord } from "../types";
import { savedBranchHead } from "./branch-head";
+import { contextUsageReasoningKey } from "./context-usage-reasoning";
import { listStoredChatMessages } from "./chat-history-storage";
import { orderBySelectedBranch } from "./message-order";
-// key generations by thread so a hidden compare pane cannot invalidate the visible count.
-const refreshGenerations = new Map<string | null, number>();
-
-function nextGeneration(threadKey: string | null): number {
- const generation = (refreshGenerations.get(threadKey) ?? 0) + 1;
- refreshGenerations.set(threadKey, generation);
- return generation;
-}
-
-function superseded(threadKey: string | null, generation: number): boolean {
- return refreshGenerations.get(threadKey) !== generation;
-}
-
// Threads with a count on the wire. A model load fires two triggers, and both would otherwise
// render the template and tokenize.
-const countsInFlight = new Set<string | null>();
+const countsInFlight = new Map<string | null, {
+ afterModelLoad: boolean;
+ loadId: string | null;
+}>();
-// A trigger deferred behind an in-flight count, replayed once that count settles WITHOUT
-// publishing. Dropping it loses the bar: a run stopped before it emits usage fires the
-// retry this depends on. Replaying it unconditionally would restore the doubled model-load
-// count, where the first trigger publishes.
+// Replay a deferred trigger only when the in-flight count did not publish.
const retryAfterInFlight = new Map<string | null, RefreshOptions>();
+let countEpoch = 0;
+
+let completedCount: {
+ key: string;
+ payload: string;
+ usage: NonNullable<
+ ReturnType<typeof useChatRuntimeStore.getState>["contextUsage"]
+ >;
+} | null = null;
function storedMessageToRunMessage(record: MessageRecord): ThreadMessage {
const content =
@@ -56,7 +54,7 @@ function storedMessageToRunMessage(record: MessageRecord): ThreadMessage {
attachments: record.attachments
? structuredClone(record.attachments)
: [],
- metadata: { custom: {} },
+ metadata: { custom: (record.metadata as Record<string, unknown>) ?? {} },
};
}
@@ -68,7 +66,10 @@ function storedMessageToRunMessage(record: MessageRecord): ThreadMessage {
id: record.id,
createdAt: new Date(record.createdAt),
role: "assistant",
- content: content as Extract<ThreadMessage, { role: "assistant" }>["content"],
+ content: content as Extract<
+ ThreadMessage,
+ { role: "assistant" }
+ >["content"],
status: { type: "complete", reason: "unknown" },
metadata: {
custom,
@@ -108,6 +109,15 @@ function branchSignature(messages: readonly ThreadMessage[]): string {
let hash = 0;
let parts = 0;
for (const message of messages) {
+ hash = foldPart(
+ [
+ message.id,
+ message.role,
+ message.metadata?.custom?.revision,
+ message.metadata?.custom?.contextTruncation,
+ ],
+ hash,
+ );
for (const part of message.content as readonly unknown[]) {
parts += 1;
hash = foldPart(part, hash);
@@ -133,6 +143,51 @@ export function setActiveBranchReader(reader: ActiveBranchReader | null): void {
readActiveBranch = reader;
}
+export function currentContextUsageReasoningKey(
+ state = useChatRuntimeStore.getState(),
+): string | undefined {
+ if (
+ !state.params.checkpoint ||
+ isExternalModelId(state.params.checkpoint) ||
+ !(state.supportsReasoning || state.supportsPreserveThinking)
+ ) {
+ return undefined;
+ }
+ return contextUsageReasoningKey(buildLocalTokenCountReasoning(state));
+}
+
+export function currentContextUsageSettingsKey(
+ state: ReturnType<typeof useChatRuntimeStore.getState>,
+ browserTools: string,
+): string {
+ return JSON.stringify([
+ state.params.systemPrompt,
+ state.params.systemVariables,
+ state.params.maxTokens,
+ state.loadedChatTemplateOverride,
+ state.autoCompactEnabled,
+ state.supportsTools,
+ state.toolsEnabled,
+ state.codeToolsEnabled,
+ state.codeToolsDeclinedUnderFullAccess,
+ state.mcpEnabledForChat,
+ state.ragEnabled,
+ state.ragSource,
+ state.ragMode,
+ state.ragTopK,
+ state.ragAutoInject,
+ state.ragAutoInjectMinScore,
+ state.autoHealToolCalls,
+ state.bypassPermissions,
+ state.deepResearchEnabled,
+ state.permissionMode,
+ state.sandboxLevel,
+ state.maxToolCallsPerMessage,
+ browserTools,
+ localMediaToolsForTurn(state),
+ ]);
+}
+
type RefreshOptions =
| {
threadId?: string;
@@ -150,6 +205,16 @@ export async function refreshContextUsage(
const threadId = options?.threadId ?? store.activeThreadId;
const checkpoint = store.params.checkpoint;
+ const pendingCount = countsInFlight.get(threadId ?? null);
+ if (
+ options?.invalidate ||
+ (options?.afterModelLoad &&
+ (!pendingCount?.afterModelLoad || pendingCount.loadId !== store.activeLoadId))
+ ) {
+ completedCount = null;
+ countEpoch += 1;
+ }
+
if (
!checkpoint ||
isExternalModelId(checkpoint) ||
@@ -166,7 +231,10 @@ export async function refreshContextUsage(
);
if (activeModel?.isAudio && !activeModel?.hasAudioInput) return;
- if (options?.invalidate) store.setContextUsage(null);
+ if (options?.invalidate) {
+ store.setContextUsage(null);
+ }
+ const usageBeforeCount = useChatRuntimeStore.getState().contextUsage;
// Never count while anything is generating: the endpoint refuses, and the recount effect
// depends on this so the last run finishing re-fires it. runningByThreadId, not the
@@ -176,23 +244,33 @@ export async function refreshContextUsage(
const capturedThreadId = threadId ?? null;
const capturedCheckpoint = checkpoint;
+ const capturedReasoning = JSON.stringify(buildLocalTokenCountReasoning());
+ const capturedSettingsEpoch = store.queuedSettingsEpoch;
+ const capturedCountEpoch = countEpoch;
+ const capturedMcpEpoch = getMcpServerMutationEpoch();
if (countsInFlight.has(capturedThreadId)) {
retryAfterInFlight.set(capturedThreadId, options);
return;
}
- // Bump only once this call will do work, so a bail-out cannot cancel an in-flight recount.
- const generation = nextGeneration(capturedThreadId);
-
- // The checkpoint can move under any await below, and a later recount for the same thread
- // supersedes this one; publishing after either puts another model's number on the bar.
+ const promptChanged = (): boolean =>
+ countEpoch !== capturedCountEpoch ||
+ (store.mcpEnabledForChat && getMcpServerMutationEpoch() !== capturedMcpEpoch) ||
+ useChatRuntimeStore.getState().params.checkpoint !== capturedCheckpoint ||
+ JSON.stringify(buildLocalTokenCountReasoning()) !== capturedReasoning;
const stale = (): boolean =>
- superseded(capturedThreadId, generation) ||
- useChatRuntimeStore.getState().params.checkpoint !== capturedCheckpoint;
-
- countsInFlight.add(capturedThreadId);
+ promptChanged() ||
+ useChatRuntimeStore.getState().queuedSettingsEpoch !==
+ capturedSettingsEpoch;
+
+ countsInFlight.set(capturedThreadId, {
+ afterModelLoad: options?.afterModelLoad === true,
+ loadId: store.activeLoadId,
+ });
let published = false;
+ let replayQueued = true;
+ let inputsChanged = stale;
try {
// Prefer the mounted runtime: it is what the next request reads from. A captured null is
// EXCLUDED, not matched: New Chat leaves the outgoing conversation mounted until
@@ -210,7 +288,7 @@ export async function refreshContextUsage(
let countedBranch: string | null = null;
// only the last id witnesses the fallback because stored and runtime records hash differently.
let countedLastId: string | null = null;
- const fromLiveBranch = Boolean(liveBranch && liveBranch.length > 0);
+ const fromLiveBranch = liveBranch != null;
if (fromLiveBranch) {
runMessages = liveBranch as readonly ThreadMessage[];
} else {
@@ -231,15 +309,35 @@ export async function refreshContextUsage(
// video counting misses server-side frames; declining also avoids hashing up to 85 MB.
if (findLatestUserVideoBase64(runMessages)) return;
+ if (
+ !store.loadedIsGguf &&
+ runMessages.some((message) => message.metadata?.custom?.contextTruncation)
+ ) return;
+
if (fromLiveBranch) {
countedBranch = branchSignature(runMessages);
} else {
countedLastId = runMessages.at(-1)?.id ?? "";
}
-
- // A completion finishing mid-count writes exact usage for a turn this count predates, so drop
- // the recount rather than roll the bar backwards.
- const usageBeforeCount = useChatRuntimeStore.getState().contextUsage;
+ const branchChanged = (): boolean => {
+ const current = readOwnBranch();
+ if (current == null) return false;
+ return countedBranch != null
+ ? branchSignature(current) !== countedBranch
+ : current.length > 0 && (current.at(-1)?.id ?? "") !== countedLastId;
+ };
+ inputsChanged = () => stale() || branchChanged();
+
+ const canPublish = (): boolean => {
+ const currentStore = useChatRuntimeStore.getState();
+ return (
+ !stale() &&
+ // Compared even when null: a count started with no thread must not land on one since opened.
+ currentStore.activeThreadId === capturedThreadId &&
+ currentStore.contextUsage === usageBeforeCount &&
+ !Object.values(currentStore.runningByThreadId ?? {}).some(Boolean)
+ );
+ };
// undefined, not null: a chat with no persisted thread has no project to resolve from.
const payloadThreadId = threadId ?? undefined;
@@ -252,75 +350,92 @@ export async function refreshContextUsage(
payloadThreadId,
countHistory.messages,
);
- if (stale()) return;
+ if (!canPublish()) return;
+
+ const payload = {
+ model: capturedCheckpoint,
+ ...countHistory,
+ ...buildLocalTokenCountReasoning(),
+ ...countExtras,
+ };
+ const serializedPayload = JSON.stringify(payload);
+ const countKey = JSON.stringify([
+ capturedThreadId,
+ countedBranch ?? branchSignature(runMessages),
+ store.activeLoadId,
+ store.activeGgufVariant,
+ store.loadedContextLength,
+ store.loadedChatTemplateOverride,
+ countExtras.mcp_enabled ? capturedMcpEpoch : null,
+ ]);
+ if (completedCount?.key === countKey && completedCount.payload === serializedPayload) {
+ if (branchChanged()) return;
+ if (usageBeforeCount == null || usageBeforeCount.estimated) {
+ useChatRuntimeStore.getState().setContextUsage(completedCount.usage);
+ }
+ published = true;
+ return;
+ }
- // Always ask the server: the template itself has tokens, and `unsloth run --enable-tools`
- // injects schemas the client cannot see.
const { input_tokens: inputTokens, model: countedModel } =
- await countChatInputTokens({
- model: capturedCheckpoint,
- ...countHistory,
- ...buildLocalTokenCountReasoning(),
- ...countExtras,
- });
+ await countChatInputTokens(payload, serializedPayload);
- if (stale()) return;
// The response type is a compile-time assertion only: anything else answering 200 would put
// undefined on the bar and throw from toLocaleString.
- if (typeof inputTokens !== "number" || !Number.isFinite(inputTokens)) return;
+ if (
+ typeof inputTokens !== "number" ||
+ !Number.isFinite(inputTokens) ||
+ inputTokens < 0
+ ) {
+ replayQueued = false;
+ return;
+ }
// The endpoint counts with whatever is resident, never the model asked for: a load from
// another tab returns another tokenizer's total, which the checkpoint guards cannot see.
if (countedModel != null && countedModel !== capturedCheckpoint) {
+ replayQueued = false;
return;
}
- // Compared even when null: a count started with no thread must not land on one since opened.
- if (useChatRuntimeStore.getState().activeThreadId !== capturedThreadId) {
- return;
- }
- if (useChatRuntimeStore.getState().contextUsage !== usageBeforeCount) {
- return;
- }
- // A run writes its own usage when it lands, so declining while one is live never loses a
- // number. A first turn has no thread id yet and files under "__default".
- if (useChatRuntimeStore.getState().runningByThreadId[capturedThreadId ?? "__default"]) {
- return;
- }
- // The usage snapshot only sees a completion that WROTE usage, so a run stopped before
- // emitting any leaves it equal and the branch is the only witness. An empty current branch
- // is a mismatch: deleting the sole exchange mid-count would leave the old total.
- if (countedBranch != null) {
- const current = readActiveBranch?.();
- if (current != null && branchSignature(current) !== countedBranch) {
- return;
- }
- } else if (countedLastId != null) {
- // The count priced storage because the runtime had not mounted this thread yet; if it has
- // since, the last id is the one witness the two shapes share. readOwnBranch, not
- // readActiveBranch: an empty New Chat still sees the conversation it is leaving.
- const current = readOwnBranch();
- if (
- current != null &&
- current.length > 0 &&
- (current.at(-1)?.id ?? "") !== countedLastId
- ) {
- return;
- }
- }
-
- useChatRuntimeStore.getState().setContextUsage({
+ const usage = {
promptTokens: inputTokens,
completionTokens: 0,
totalTokens: inputTokens,
cachedTokens: 0,
cacheWriteTokens: 0,
- });
+ reasoningKey: contextUsageReasoningKey(JSON.parse(capturedReasoning)),
+ };
+ if (promptChanged() || branchChanged()) return;
+ completedCount = { key: countKey, payload: serializedPayload, usage };
+ if (!canPublish()) return;
+
+ useChatRuntimeStore.getState().setContextUsage(usage);
published = true;
} catch {
+ replayQueued = false;
// Background recount should not interrupt chat; saved usage stays visible.
} finally {
countsInFlight.delete(capturedThreadId);
const queued = retryAfterInFlight.get(capturedThreadId);
const hadQueued = retryAfterInFlight.delete(capturedThreadId);
- if (hadQueued && !published) void refreshContextUsage(queued);
+ const current = useChatRuntimeStore.getState();
+ const newerExactUsage =
+ current.contextUsage !== usageBeforeCount &&
+ current.contextUsage != null &&
+ !current.contextUsage.estimated;
+ const settingsChanged =
+ current.activeThreadId === capturedThreadId &&
+ current.params.checkpoint === capturedCheckpoint &&
+ (current.queuedSettingsEpoch !== capturedSettingsEpoch ||
+ (store.mcpEnabledForChat && getMcpServerMutationEpoch() !== capturedMcpEpoch));
+ if (
+ !published &&
+ !newerExactUsage &&
+ (settingsChanged ||
+ (hadQueued && (replayQueued || inputsChanged() || queued?.invalidate)))
+ ) {
+ void refreshContextUsage(
+ hadQueued ? queued : { ...options, invalidate: false },
+ );
+ }
}
}
diff --git a/studio/frontend/src/features/chat/utils/saved-context-usage.ts b/studio/frontend/src/features/chat/utils/saved-context-usage.ts
new file mode 100644
index 00000000000..79262e66aaf
--- /dev/null
+++ b/studio/frontend/src/features/chat/utils/saved-context-usage.ts
@@ -0,0 +1,190 @@
+// 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 type { ThreadMessage } from "@assistant-ui/react";
+import type { MessageRecord } from "../types";
+import { codexLocalToolRoundId } from "../codex-reasoning.ts";
+import { isExternalModelId } from "../external-providers.ts";
+import { estimateContextUsage, estimateMessagesTokenCount } from "./estimate-chat-tokens.ts";
+
+type BranchRecord = Pick<MessageRecord, "id" | "role" | "content" | "metadata">;
+
+export type BranchContextUsage = {
+ promptTokens: number;
+ completionTokens: number;
+ totalTokens: number;
+ contextTokens?: number;
+ cachedTokens: number;
+ cacheWriteTokens?: number;
+ modelId?: string;
+ reasoningKey?: string;
+ messageCount?: number;
+ estimatedTokens?: number;
+ revision?: number;
+ estimated?: boolean;
+};
+
+export function isSummedToolLoopUsage(
+ usage: { modelId?: unknown; contextTokens?: unknown } | null | undefined,
+ content: unknown,
+): boolean {
+ return (
+ typeof usage?.modelId === "string" &&
+ isExternalModelId(usage.modelId) &&
+ usage.contextTokens === undefined &&
+ Array.isArray(content) &&
+ content.some((part) => {
+ const toolCall = part as { type?: unknown; provenance?: unknown } | null;
+ return toolCall?.type === "tool-call" && codexLocalToolRoundId(toolCall.provenance) !== null;
+ })
+ );
+}
+
+function revisionOf(custom: Record<string, unknown> | undefined): number {
+ const revision = custom?.revision;
+ return typeof revision === "number" && Number.isInteger(revision) && revision > 0 ? revision : 0;
+}
+
+export function withMessageRevision<T extends ThreadMessage["metadata"]>(metadata: T): T {
+ return {
+ ...metadata,
+ custom: { ...metadata?.custom, revision: revisionOf(metadata?.custom) + 1 },
+ };
+}
+
+function researchRevisionOf(custom: Record<string, unknown> | undefined): number {
+ if (typeof custom?.researchRunId !== "string") return 0;
+ const run = custom.researchRun as { lastEventSeq?: unknown } | undefined;
+ const revision = custom.serverRevision ?? run?.lastEventSeq;
+ return typeof revision === "number" && Number.isInteger(revision) && revision > 0 ? revision : 0;
+}
+
+export function revisionTotal(
+ messages: readonly { metadata?: { custom?: Record<string, unknown> } }[],
+): number {
+ let total = 0;
+ for (const message of messages) {
+ total += revisionOf(message.metadata?.custom) + researchRevisionOf(message.metadata?.custom);
+ }
+ return total;
+}
+
+export function threadBranchRecords(messages: readonly ThreadMessage[]): BranchRecord[] {
+ return messages.map((message) => ({
+ id: message.id,
+ role: message.role,
+ content: message.content,
+ metadata: message.metadata?.custom,
+ }));
+}
+
+function inBranchOrder(branch: readonly Pick<BranchRecord, "id" | "role" | "content">[]) {
+ return branch.map(
+ ({ id, role, content }, index) => ({ id, role, content, createdAt: index }) as MessageRecord,
+ );
+}
+
+function branchTokenEstimate(
+ branch: readonly Pick<BranchRecord, "id" | "role" | "content">[],
+): number | null {
+ return estimateMessagesTokenCount(inBranchOrder(branch));
+}
+
+export type ContextUsageMarkers = {
+ messageCount: number;
+ revision: number;
+ estimatedTokens: number;
+};
+
+export function contextUsageMarkers(branch: readonly BranchRecord[]): ContextUsageMarkers {
+ let revision = 0;
+ for (const message of branch) revision += revisionOf(message.metadata);
+ return {
+ messageCount: branch.length,
+ revision,
+ estimatedTokens: branchTokenEstimate(branch) ?? 0,
+ };
+}
+
+export function estimateBranch(branch: readonly BranchRecord[]) {
+ return estimateContextUsage(inBranchOrder(branch));
+}
+
+function estimatedUsage(total: number): BranchContextUsage {
+ return {
+ promptTokens: total,
+ completionTokens: 0,
+ totalTokens: total,
+ cachedTokens: 0,
+ estimated: true,
+ };
+}
+
+export function branchContextUsage(
+ branch: readonly BranchRecord[],
+ checkpoint: string | null | undefined,
+ localWindow: number | null | undefined,
+ ggufWindow: number | null | undefined,
+ reasoningKey?: string,
+): BranchContextUsage | null {
+ let revisions = 0;
+ let anchor = -1;
+ let calibration: { context: number; estimated: number } | null = null;
+ branch.forEach((message, index) => {
+ revisions += revisionOf(message.metadata);
+ if (message.role !== "assistant") return;
+ if (typeof message.metadata?.researchRunId === "string") return;
+ const usage = message.metadata?.contextUsage as BranchContextUsage | undefined;
+ const context = usage?.contextTokens ?? usage?.totalTokens;
+ if (!usage || typeof context !== "number" || !Number.isFinite(context) || context < 0) return;
+ if (ggufWindow && context > ggufWindow) return;
+ const modelMatches = usage.modelId
+ ? usage.modelId === checkpoint
+ : localWindow != null && localWindow > 0;
+ if (!modelMatches || isSummedToolLoopUsage(usage, message.content)) return;
+ const current =
+ (reasoningKey === undefined || usage.reasoningKey === undefined || isExternalModelId(checkpoint) || usage.reasoningKey === reasoningKey) &&
+ (usage.revision ?? 0) === revisions &&
+ (usage.messageCount === undefined || usage.messageCount === index + 1);
+ if (current) {
+ anchor = index;
+ calibration = null;
+ } else if (
+ typeof usage.estimatedTokens === "number" &&
+ Number.isFinite(usage.estimatedTokens) &&
+ usage.estimatedTokens > 0
+ ) {
+ calibration = { context, estimated: usage.estimatedTokens };
+ }
+ });
+ if (calibration) {
+ const { context, estimated } = calibration;
+ const now = branchTokenEstimate(branch) ?? 0;
+ const total = context >= estimated ? now + context - estimated : (now * context) / estimated;
+ return estimatedUsage(Math.max(1, Math.round(total)));
+ }
+ if (anchor < 0) return null;
+ const saved = branch[anchor].metadata?.contextUsage as BranchContextUsage;
+ if (anchor === branch.length - 1) return saved;
+ const base = saved.contextTokens ?? saved.totalTokens;
+ return estimatedUsage(base + (branchTokenEstimate(branch.slice(anchor + 1)) ?? 0));
+}
+
+export function sameContextUsage(
+ shown: BranchContextUsage | null | undefined,
+ usage: BranchContextUsage,
+ checkpoint: string | null | undefined,
+): boolean {
+ return (
+ shown != null &&
+ !shown.estimated === !usage.estimated &&
+ shown.promptTokens === usage.promptTokens &&
+ shown.completionTokens === usage.completionTokens &&
+ shown.totalTokens === usage.totalTokens &&
+ shown.contextTokens === usage.contextTokens &&
+ shown.cachedTokens === usage.cachedTokens &&
+ shown.cacheWriteTokens === usage.cacheWriteTokens &&
+ shown.reasoningKey === usage.reasoningKey &&
+ shown.modelId === (usage.modelId ?? checkpoint)
+ );
+}
diff --git a/studio/frontend/src/features/chat/utils/update-thread-message.ts b/studio/frontend/src/features/chat/utils/update-thread-message.ts
index 6238f3e7784..55cfcec8a37 100644
--- a/studio/frontend/src/features/chat/utils/update-thread-message.ts
+++ b/studio/frontend/src/features/chat/utils/update-thread-message.ts
@@ -1,8 +1,9 @@
-import type { ExportedMessageRepository, ThreadMessage } from "@assistant-ui/react";
+import type { ExportedMessageRepository } from "@assistant-ui/react";
import { saveChatMessage } from "../api/chat-api";
import type { MessageRecord } from "../types";
import { exportedItemToRecord } from "./delete-thread-message";
import { RESEARCH_METADATA_KEYS } from "./research-message-sync";
+import { withMessageRevision } from "./saved-context-usage";
// Mirrors studio_db._SERVER_MANAGED_LINK_KEYS. The backend detaches a finished run only when
// the edit drops its ownership claim; display fields such as timing and incomplete stay.
@@ -229,12 +230,22 @@ export async function updateThreadMessage(args: {
}
}
+ const previousContent = m.message.content;
+ const previousTools = Array.isArray(previousContent)
+ ? previousContent.filter((part) => !isEditablePart(part))
+ : [];
+ const unchanged = extractTaggedText(finalContent) === extractTaggedText(previousContent)
+ && JSON.stringify(finalContent.filter((part) => !isEditablePart(part))) === JSON.stringify(previousTools);
+
return {
...m,
message: {
...m.message,
content: finalContent,
- metadata: withoutGeminiContinuationReplay(m.message.metadata),
+ metadata:
+ unchanged
+ ? withoutGeminiContinuationReplay(m.message.metadata)
+ : withMessageRevision(withoutGeminiContinuationReplay(m.message.metadata)),
},
};
}) as typeof currentExport.messages;
diff --git a/studio/frontend/tests/chat-runtime-reload-status.test.ts b/studio/frontend/tests/chat-runtime-reload-status.test.ts
index 5fa69760706..0292cff5d64 100644
--- a/studio/frontend/tests/chat-runtime-reload-status.test.ts
+++ b/studio/frontend/tests/chat-runtime-reload-status.test.ts
@@ -24,6 +24,8 @@ const {
subscribeGenerationRecoveryTriggers,
} = await import("../src/features/chat/utils/chat-generation-recovery.ts");
+const MARKERS = { messageCount: 2, revision: 0, estimatedTokens: 5 };
+
test("first-token recovery ignores role and control chunks", () => {
assert.equal(
generationChunkHasSubstantiveDelta({
@@ -159,6 +161,7 @@ test("terminal recovery reads cache writes from either provider's usage shape",
},
usage: { prompt_tokens: 8, completion_tokens: 2, total_tokens: 10, ...usage },
totalChunks: 1,
+ markers: MARKERS,
}).contextUsage as { cacheWriteTokens: number }
).cacheWriteTokens;
assert.equal(writesFor({ prompt_tokens_details: { cache_write_tokens: 5 } }), 5);
@@ -187,6 +190,7 @@ test("terminal recovery prices a tool loop's context, not its summed completions
timings: {},
firstChunkAt: 220,
totalChunks: 4,
+ markers: MARKERS,
});
const usage = metadata.contextUsage as {
totalTokens: number;
@@ -196,6 +200,51 @@ test("terminal recovery prices a tool loop's context, not its summed completions
assert.equal(usage.totalTokens, 180);
});
+test("recovered usage keeps the reasoning mode from its persisted request", () => {
+ const metadata = recoveredGenerationFinalMetadata({
+ current: { generationSettled: true },
+ run: {
+ id: "run-reasoning",
+ requestPayload: {
+ model: "local/model",
+ thinking: { type: "enabled" },
+ reasoning_effort: "high",
+ preserve_thinking: false,
+ },
+ createdAt: 100,
+ startedAt: 120,
+ completedAt: 1120,
+ },
+ usage: { prompt_tokens: 140, completion_tokens: 40, total_tokens: 180 },
+ totalChunks: 4,
+ markers: MARKERS,
+ });
+ assert.equal(
+ (metadata.contextUsage as { reasoningKey: string }).reasoningKey,
+ '[true,"high",false]',
+ );
+});
+
+test("recovery certifies usage only for a branch no edit could have revised", () => {
+ const usageFor = (revision: number) =>
+ recoveredGenerationFinalMetadata({
+ current: { generationSettled: true },
+ run: {
+ id: "run-revised",
+ requestPayload: { model: "local/model" },
+ createdAt: 100,
+ startedAt: 120,
+ completedAt: 1120,
+ },
+ usage: { prompt_tokens: 140, completion_tokens: 40, total_tokens: 180 },
+ totalChunks: 4,
+ markers: { ...MARKERS, revision },
+ }).contextUsage as { revision?: number; estimatedTokens?: number };
+ assert.equal(usageFor(0).revision, 0);
+ assert.equal(usageFor(1).revision, undefined);
+ assert.equal(usageFor(1).estimatedTokens, MARKERS.estimatedTokens);
+});
+
test("terminal recovery restores final local usage and timing metadata", () => {
const metadata = recoveredGenerationFinalMetadata({
current: { generationSettled: true },
@@ -215,6 +264,7 @@ test("terminal recovery restores final local usage and timing metadata", () => {
timings: { predicted_per_second: 12, prompt_ms: 25 },
firstChunkAt: 220,
totalChunks: 4,
+ markers: MARKERS,
});
assert.deepEqual(metadata.contextUsage, {
promptTokens: 8,
@@ -223,6 +273,8 @@ test("terminal recovery restores final local usage and timing metadata", () => {
cachedTokens: 3,
cacheWriteTokens: 0,
modelId: "local/model",
+ reasoningKey: "[null,null,null]",
+ ...MARKERS,
});
assert.deepEqual(metadata.serverTimings, {
predicted_per_second: 12,
@@ -391,6 +443,7 @@ test("recovery persists recovered usage alongside the cursor it advanced", () =>
timings: midStream.generationRecoveryTimings as typeof timings,
firstChunkAt: 220,
totalChunks: 4,
+ markers: MARKERS,
});
assert.deepEqual(settled.contextUsage, {
promptTokens: 8,
@@ -399,6 +452,8 @@ test("recovery persists recovered usage alongside the cursor it advanced", () =>
cachedTokens: 0,
cacheWriteTokens: 0,
modelId: "local/model",
+ reasoningKey: "[null,null,null]",
+ ...MARKERS,
});
assert.deepEqual(settled.serverTimings, timings);
});
diff --git a/studio/frontend/tests/context-usage-accuracy.test.ts b/studio/frontend/tests/context-usage-accuracy.test.ts
new file mode 100644
index 00000000000..495e6a70916
--- /dev/null
+++ b/studio/frontend/tests/context-usage-accuracy.test.ts
@@ -0,0 +1,425 @@
+// 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 {
+ contextUsageRows,
+ deriveContextUsageBar,
+ formatTokenCount,
+} from "../src/features/chat/lib/context-usage-bar-state.ts";
+import {
+ branchContextUsage,
+ contextUsageMarkers,
+ estimateBranch,
+ isSummedToolLoopUsage,
+ revisionTotal,
+ sameContextUsage,
+ threadBranchRecords,
+} from "../src/features/chat/utils/saved-context-usage.ts";
+import { contextUsageReasoningKey } from "../src/features/chat/utils/context-usage-reasoning.ts";
+
+test("counts below a million keep the k suffix", () => {
+ assert.equal(formatTokenCount(999), "999");
+ assert.equal(formatTokenCount(1000), "1.0k");
+ assert.equal(formatTokenCount(322_800), "322.8k");
+ assert.equal(formatTokenCount(999_949), "999.9k");
+});
+
+test("counts from a million up switch to an M suffix instead of a four-digit k", () => {
+ assert.equal(formatTokenCount(999_950), "1.0M");
+ assert.equal(formatTokenCount(1_000_000), "1.0M");
+ assert.equal(formatTokenCount(2_500_000), "2.5M");
+ assert.equal(formatTokenCount(12_345_678), "12.3M");
+});
+
+test("a negative or non-finite used count reads as not counted", () => {
+ for (const used of [-5, Number.NaN, Number.POSITIVE_INFINITY]) {
+ const state = deriveContextUsageBar({ used, total: 32768 });
+ assert.ok(state);
+ assert.equal(state.percent, null);
+ assert.equal(state.face, "— / 32.8k");
+ }
+});
+
+test("a count past the window still caps the ratio at one hundred percent", () => {
+ const state = deriveContextUsageBar({ used: 2_345_678, total: 32768 });
+ assert.ok(state);
+ assert.equal(state.face, "2.3M / 32.8k");
+ assert.equal(state.percent, 100);
+});
+
+test("a single request's rows describe the context it reports", () => {
+ assert.deepEqual(
+ contextUsageRows({ promptTokens: 900, completionTokens: 100, cachedTokens: 800 }, true),
+ { promptTokens: 900, completionTokens: 100, cached: 800, cacheWrites: undefined },
+ );
+ assert.deepEqual(
+ contextUsageRows(
+ { promptTokens: 900, completionTokens: 100, contextTokens: 1000, cachedTokens: 800, cacheWriteTokens: 50 },
+ true,
+ ),
+ { promptTokens: 900, completionTokens: 100, cached: 800, cacheWrites: 50 },
+ );
+ assert.deepEqual(contextUsageRows(null, false), {});
+});
+
+test("a local tool loop's rows show the last pass's reply, not every pass's", () => {
+ assert.deepEqual(
+ contextUsageRows({ promptTokens: 625, completionTokens: 150, contextTokens: 654, cachedTokens: 500 }, false),
+ { promptTokens: 625, completionTokens: 29, cached: 500, cacheWrites: undefined },
+ );
+});
+
+test("a managed engine's summed prompt hides the rows unless the turn was one pass", () => {
+ assert.deepEqual(
+ contextUsageRows({ promptTokens: 3000, completionTokens: 1550, contextTokens: 3500 }, true),
+ {},
+ );
+ assert.deepEqual(
+ contextUsageRows({ promptTokens: 2000, completionTokens: 1500, contextTokens: 3500 }, true),
+ { promptTokens: 2000, completionTokens: 1500, cached: undefined, cacheWrites: undefined },
+ );
+});
+
+test("a cloud tool loop's summed billing rows are hidden, even when the last reply is long", () => {
+ assert.deepEqual(
+ contextUsageRows({ promptTokens: 322000, completionTokens: 760, contextTokens: 52400, cachedTokens: 306600 }, true),
+ {},
+ );
+ assert.deepEqual(
+ contextUsageRows({ promptTokens: 6900, completionTokens: 4260, contextTokens: 8100 }, true),
+ {},
+ );
+ assert.deepEqual(
+ contextUsageRows({ promptTokens: 203000, completionTokens: 4500, contextTokens: 24000 }, true),
+ {},
+ );
+});
+
+const localToolCall = {
+ type: "tool-call",
+ toolCallId: "c1",
+ toolName: "web_search",
+ args: {},
+ provenance: { source: "local", round_id: 1 },
+};
+const hostedToolCall = { type: "tool-call", toolCallId: "c1", toolName: "web_search", args: {} };
+const cloudUsage = { modelId: "external::openai::gpt-x" };
+
+test("a cloud turn whose tools Studio ran, saved without its context, is a summed loop figure", () => {
+ assert.equal(isSummedToolLoopUsage(cloudUsage, [localToolCall, { type: "text", text: "a" }]), true);
+});
+
+test("a saved figure that names its context, a hosted tool, a local model or a plain reply is trusted", () => {
+ assert.equal(isSummedToolLoopUsage({ ...cloudUsage, contextTokens: 52400 }, [localToolCall]), false);
+ assert.equal(isSummedToolLoopUsage(cloudUsage, [hostedToolCall]), false);
+ assert.equal(isSummedToolLoopUsage({ modelId: "unsloth/gguf-model" }, [localToolCall]), false);
+ assert.equal(isSummedToolLoopUsage(cloudUsage, [{ type: "text", text: "a" }]), false);
+ assert.equal(isSummedToolLoopUsage(cloudUsage, undefined), false);
+ assert.equal(isSummedToolLoopUsage(undefined, [localToolCall]), false);
+});
+
+const LOCAL = "unsloth/gguf-model";
+const CLOUD = "external::openai::gpt-x";
+
+function usage(context: number, modelId?: string) {
+ return {
+ promptTokens: context - 10,
+ completionTokens: 10,
+ totalTokens: context,
+ cachedTokens: 0,
+ ...(modelId ? { modelId } : {}),
+ };
+}
+
+function row(
+ id: string,
+ role: "user" | "assistant",
+ createdAt: number,
+ text: string,
+ metadata?: Record<string, unknown>,
+) {
+ return { id, role, createdAt, content: [{ type: "text" as const, text }], ...(metadata ? { metadata } : {}) };
+}
+
+test("a retrieval card added before the first pass leaves a one-pass cloud figure trusted", () => {
+ const retrievalCard = {
+ type: "tool-call",
+ toolCallId: "rag_auto_0123456789ab",
+ toolName: "search_knowledge_base",
+ args: { query: "q" },
+ provenance: { source: "local" },
+ };
+ assert.equal(isSummedToolLoopUsage(cloudUsage, [retrievalCard, { type: "text", text: "a" }]), false);
+ assert.equal(isSummedToolLoopUsage(cloudUsage, [retrievalCard, localToolCall]), true);
+ const saved = usage(6330, CLOUD);
+ const reply = row("a2", "assistant", 2, "y".repeat(40), { contextUsage: saved });
+ const branch = [
+ row("u1", "user", 1, "x".repeat(40)),
+ { ...reply, content: [retrievalCard, ...reply.content] },
+ ] as never;
+ assert.equal(branchContextUsage(branch, CLOUD, null, null), saved);
+});
+
+const conversation = () => [
+ row("u1", "user", 1, "x".repeat(40)),
+ row("a1", "assistant", 2, "y".repeat(40), { contextUsage: usage(300, LOCAL) }),
+ row("u2", "user", 3, "x".repeat(40)),
+ row("a2", "assistant", 4, "y".repeat(40), { contextUsage: usage(500, LOCAL) }),
+];
+
+test("a reply's markers record its position, the edits before it and the branch's estimate", () => {
+ const branch = conversation();
+ branch[2] = { ...branch[2], metadata: { revision: 2 } };
+ assert.deepEqual(contextUsageMarkers(branch), { messageCount: 4, revision: 2, estimatedTokens: 40 });
+ assert.deepEqual(contextUsageMarkers([]), { messageCount: 0, revision: 0, estimatedTokens: 0 });
+});
+
+test("the tip's own count prices its branch", () => {
+ const branch = conversation();
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, 8192), branch[3].metadata?.contextUsage);
+});
+
+test("tagged local counts retain their exactness only under the reasoning mode that produced them", () => {
+ const on = contextUsageReasoningKey({ enable_thinking: true, preserve_thinking: true });
+ const off = contextUsageReasoningKey({ enable_thinking: false, preserve_thinking: true });
+ const branch = conversation();
+ branch[3] = {
+ ...branch[3],
+ metadata: {
+ contextUsage: { ...usage(500, LOCAL), reasoningKey: on, estimatedTokens: 40 },
+ },
+ };
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, 8192, on), branch[3].metadata?.contextUsage);
+ const changed = branchContextUsage(branch, LOCAL, 8192, 8192, off);
+ assert.equal(changed?.estimated, true);
+ assert.equal(changed?.totalTokens, 500);
+});
+
+test("legacy saved counts remain exact on a local reasoning model", () => {
+ const branch = conversation();
+ const saved = branch[3].metadata?.contextUsage;
+ for (const enabled of [true, false]) {
+ const reasoningKey = contextUsageReasoningKey({ enable_thinking: enabled });
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, 8192, reasoningKey), saved);
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, null, reasoningKey), saved);
+ assert.equal(branchContextUsage(branch, LOCAL, null, null, reasoningKey), saved);
+ }
+ branch[2] = { ...branch[2], metadata: { revision: 1 } };
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, 8192, "[true,null,null]")?.estimated, true);
+});
+
+test("local reasoning defaults do not invalidate cloud context counts", () => {
+ const branch = conversation().map((message) => message.role === "assistant"
+ ? { ...message, metadata: { contextUsage: usage(500, CLOUD) } }
+ : message);
+ assert.equal(
+ branchContextUsage(branch, CLOUD, null, null, contextUsageReasoningKey({ enable_thinking: true })),
+ branch[3].metadata?.contextUsage,
+ );
+});
+
+test("a reply edited after it was written is estimated on top of the last count before it", () => {
+ const branch = conversation();
+ branch[3] = { ...branch[3], content: [{ type: "text", text: "y".repeat(400) }], metadata: { ...branch[3].metadata, revision: 1 } };
+ assert.deepEqual(branchContextUsage(branch, LOCAL, 8192, 8192), {
+ promptTokens: 410,
+ completionTokens: 0,
+ totalTokens: 410,
+ cachedTokens: 0,
+ estimated: true,
+ });
+});
+
+test("a research report replaces the handoff's count with an estimate from the preceding turn", () => {
+ const branch = conversation();
+ branch[3] = {
+ ...branch[3],
+ content: [{ type: "text", text: "r".repeat(400) }],
+ metadata: { ...branch[3].metadata, researchRunId: "research-1", researchStatus: "completed" },
+ };
+ const priced = branchContextUsage(branch, LOCAL, 8192, 8192);
+ assert.equal(priced?.estimated, true);
+ assert.equal(priced?.totalTokens, 410);
+ assert.equal(branchContextUsage(branch.slice(2), LOCAL, 8192, 8192), null);
+});
+
+test("a turn after a research report keeps its own exact context count", () => {
+ const branch = conversation();
+ branch[1] = {
+ ...branch[1],
+ metadata: { ...branch[1].metadata, researchRunId: "research-1", researchStatus: "completed" },
+ };
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, 8192), branch[3].metadata?.contextUsage);
+});
+
+test("a revision invalidates every count after it and none before it", () => {
+ const branch = conversation();
+ branch[2] = { ...branch[2], metadata: { revision: 1 } };
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, 8192)?.totalTokens, 320);
+ branch[0] = { ...branch[0], metadata: { revision: 1 } };
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, 8192), null);
+});
+
+test("a reply written after the revision is trusted again", () => {
+ const branch = conversation();
+ branch[2] = { ...branch[2], metadata: { revision: 1 } };
+ branch[3] = { ...branch[3], metadata: { contextUsage: { ...usage(500, LOCAL), revision: 1 } } };
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, 8192), branch[3].metadata?.contextUsage);
+});
+
+test("an unanswered prompt is estimated on top of the reply before it", () => {
+ const branch = conversation().slice(0, 3);
+ const priced = branchContextUsage(branch, LOCAL, 8192, 8192);
+ assert.equal(priced?.totalTokens, 310);
+ assert.equal(priced?.estimated, true);
+});
+
+test("counts another model, a summed cloud loop or a past-window figure produced are skipped", () => {
+ const branch = conversation();
+ branch[3] = { ...branch[3], metadata: { contextUsage: usage(500, "unsloth/other-gguf") } };
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, 8192)?.totalTokens, 320);
+ branch[3] = { ...branch[3], metadata: { contextUsage: usage(9000, LOCAL) } };
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, 8192)?.totalTokens, 320);
+ assert.equal(branchContextUsage(branch, LOCAL, 16384, null), branch[3].metadata?.contextUsage);
+ const cloud = conversation().map((message) =>
+ message.role === "assistant"
+ ? { ...message, content: [localToolCall, ...message.content], metadata: { contextUsage: usage(322_000, CLOUD) } }
+ : message,
+ ) as never;
+ assert.equal(branchContextUsage(cloud, CLOUD, null, null), null);
+});
+
+test("a count saved without a model is trusted only under a local window", () => {
+ const branch = conversation().map((message) =>
+ message.role === "assistant" ? { ...message, metadata: { contextUsage: usage(500) } } : message,
+ );
+ assert.equal(branchContextUsage(branch, LOCAL, 8192, 8192)?.totalTokens, 500);
+ assert.equal(branchContextUsage(branch, CLOUD, null, null), null);
+});
+
+test("the plain estimate follows the branch order, not the timestamps", () => {
+ const branch = [row("u1", "user", 50, "x".repeat(40)), row("a1", "assistant", 1, "y".repeat(40))];
+ assert.equal(estimateBranch(branch)?.totalTokens, 20);
+ assert.equal(estimateBranch([]), null);
+});
+
+test("runtime messages read as branch records carrying their own metadata", () => {
+ const messages = [
+ { id: "u1", role: "user", createdAt: new Date(5), content: [], metadata: { custom: { revision: 2 } } },
+ { id: "a1", role: "assistant", createdAt: new Date(6), content: [], metadata: { custom: { revision: 1 } } },
+ ] as never;
+ assert.deepEqual(threadBranchRecords(messages), [
+ { id: "u1", role: "user", content: [], metadata: { revision: 2 } },
+ { id: "a1", role: "assistant", content: [], metadata: { revision: 1 } },
+ ]);
+ assert.equal(revisionTotal(messages), 3);
+ assert.equal(revisionTotal([] as never), 0);
+});
+
+test("a research report replacing the same message changes the runtime branch revision", () => {
+ const message = {
+ metadata: { custom: { researchRunId: "research-1", serverRevision: 2, revision: 1 } },
+ };
+ assert.equal(revisionTotal([message]), 3);
+ message.metadata.custom.serverRevision = 20;
+ assert.equal(revisionTotal([message]), 21);
+ assert.equal(revisionTotal([{
+ metadata: { custom: { researchRunId: "research-1", researchRun: { lastEventSeq: 30 } } },
+ }]), 30);
+});
+
+test("generation stream revisions and malformed research revisions do not invalidate the meter", () => {
+ assert.equal(revisionTotal([{
+ metadata: { custom: { generationRunId: "generation-1", serverRevision: 100, revision: 2 } },
+ }]), 2);
+ for (const serverRevision of [undefined, null, "2", -1, Number.NaN, Infinity, 1.5]) {
+ assert.equal(revisionTotal([{
+ metadata: { custom: { researchRunId: "research-1", serverRevision } },
+ }]), 0);
+ }
+});
+
+test("a shown count matches only the same figure under the same model", () => {
+ const saved = usage(500);
+ assert.equal(sameContextUsage({ ...saved, modelId: LOCAL }, saved, LOCAL), true);
+ assert.equal(sameContextUsage({ ...saved, modelId: "unsloth/other-gguf" }, saved, LOCAL), false);
+ assert.equal(sameContextUsage({ ...saved, modelId: LOCAL, estimated: true }, saved, LOCAL), false);
+ assert.equal(sameContextUsage(null, saved, LOCAL), false);
+});
+
+test("a count stops describing its branch once a turn above it is deleted", () => {
+ const withPositions = conversation().map((message, index) =>
+ message.role === "assistant"
+ ? { ...message, metadata: { contextUsage: { ...usage(index === 1 ? 300 : 500, LOCAL), messageCount: index + 1 } } }
+ : message,
+ );
+ assert.equal(branchContextUsage(withPositions, LOCAL, 8192, 8192)?.totalTokens, 500);
+ const afterDelete = [withPositions[0], withPositions[1], withPositions[3]];
+ const priced = branchContextUsage(afterDelete, LOCAL, 8192, 8192);
+ assert.equal(priced?.totalTokens, 310);
+ assert.equal(priced?.estimated, true);
+ assert.equal(branchContextUsage(conversation().slice(0, 1).concat(conversation()[3]), LOCAL, 8192, 8192)?.totalTokens, 500);
+});
+
+function calibrated() {
+ const branch = conversation();
+ branch[1] = { ...branch[1], metadata: { contextUsage: { ...usage(300, CLOUD), messageCount: 2, estimatedTokens: 20 } } };
+ branch[3] = { ...branch[3], metadata: { contextUsage: { ...usage(500, CLOUD), messageCount: 4, estimatedTokens: 40 } } };
+ return branch;
+}
+
+test("a count left stale by a delete above it still calibrates the estimate", () => {
+ const branch = calibrated();
+ const afterDelete = [branch[2], branch[3]];
+ assert.deepEqual(branchContextUsage(afterDelete, CLOUD, null, null), {
+ promptTokens: 480,
+ completionTokens: 0,
+ totalTokens: 480,
+ cachedTokens: 0,
+ estimated: true,
+ });
+});
+
+test("a reply edited in place is its own count plus the edit", () => {
+ const branch = calibrated();
+ branch[3] = {
+ ...branch[3],
+ content: [{ type: "text", text: "y".repeat(400) }],
+ metadata: { ...branch[3].metadata, revision: 1 },
+ };
+ assert.equal(branchContextUsage(branch, CLOUD, null, null)?.totalTokens, 590);
+});
+
+test("the newest count wins, whether it is still exact or only calibrates", () => {
+ const staleTip = calibrated();
+ staleTip[3] = { ...staleTip[3], metadata: { contextUsage: { ...usage(500, CLOUD), messageCount: 9, estimatedTokens: 40 } } };
+ assert.deepEqual(branchContextUsage(staleTip, CLOUD, null, null), {
+ promptTokens: 500,
+ completionTokens: 0,
+ totalTokens: 500,
+ cachedTokens: 0,
+ estimated: true,
+ });
+ const staleWithoutEstimate = calibrated();
+ staleWithoutEstimate[3] = { ...staleWithoutEstimate[3], metadata: { contextUsage: { ...usage(500, CLOUD), messageCount: 9 } } };
+ assert.equal(branchContextUsage(staleWithoutEstimate, CLOUD, null, null)?.totalTokens, 320);
+ const staleFirst = calibrated();
+ staleFirst[1] = { ...staleFirst[1], metadata: { contextUsage: { ...usage(300, CLOUD), messageCount: 9, estimatedTokens: 20 } } };
+ assert.equal(branchContextUsage(staleFirst, CLOUD, null, null), staleFirst[3].metadata?.contextUsage);
+});
+
+test("a count smaller than its own estimate scales the estimate down instead of going negative", () => {
+ const branch = calibrated();
+ branch[3] = { ...branch[3], metadata: { contextUsage: { ...usage(19_000, CLOUD), messageCount: 20, estimatedTokens: 51_000 } } };
+ const priced = branchContextUsage(branch, CLOUD, null, null);
+ assert.equal(priced?.estimated, true);
+ assert.equal(priced?.totalTokens, 15);
+ const empty = calibrated().map((message) =>
+ message.role === "assistant" ? { ...message, content: [] } : { ...message, content: [] },
+ );
+ empty[3] = { ...empty[3], metadata: { contextUsage: { ...usage(19_000, CLOUD), messageCount: 20, estimatedTokens: 51_000 } } };
+ assert.equal(branchContextUsage(empty, CLOUD, null, null)?.totalTokens, 1);
+});
diff --git a/studio/frontend/tests/context-usage-reasoning.test.ts b/studio/frontend/tests/context-usage-reasoning.test.ts
new file mode 100644
index 00000000000..26c4217a47d
--- /dev/null
+++ b/studio/frontend/tests/context-usage-reasoning.test.ts
@@ -0,0 +1,39 @@
+// 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 { contextUsageReasoningKey } from "../src/features/chat/utils/context-usage-reasoning.ts";
+
+test("completion and recount reasoning fields identify the same effective mode", () => {
+ for (const enabled of [true, false]) {
+ assert.equal(
+ contextUsageReasoningKey({ thinking: { type: enabled ? "enabled" : "disabled" } }),
+ contextUsageReasoningKey({ enable_thinking: enabled }),
+ );
+ }
+ assert.equal(
+ contextUsageReasoningKey({ thinking: { type: "enabled" }, enable_thinking: false }),
+ contextUsageReasoningKey({ enable_thinking: false }),
+ );
+});
+
+test("reasoning effort, explicit omission, and preserved thinking keep separate count identities", () => {
+ const modes = [
+ {},
+ { enable_thinking: false },
+ { enable_thinking: true },
+ { enable_thinking: true, reasoning_effort: "low" },
+ { enable_thinking: true, reasoning_effort: "high" },
+ { enable_thinking: true, reasoning_effort: "high", preserve_thinking: false },
+ { enable_thinking: true, reasoning_effort: "high", preserve_thinking: true },
+ ];
+ assert.equal(new Set(modes.map(contextUsageReasoningKey)).size, modes.length);
+});
+
+test("omitted reasoning fields and serialized null defaults match", () => {
+ assert.equal(
+ contextUsageReasoningKey({ enable_thinking: null, reasoning_effort: null, preserve_thinking: null, thinking: null }),
+ contextUsageReasoningKey({}),
+ );
+});
diff --git a/studio/frontend/tests/context-usage-recount.test.ts b/studio/frontend/tests/context-usage-recount.test.ts
new file mode 100644
index 00000000000..8ceaed54356
--- /dev/null
+++ b/studio/frontend/tests/context-usage-recount.test.ts
@@ -0,0 +1,549 @@
+// 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 { setImmediate } from "node:timers/promises";
+import test from "node:test";
+import type { ThreadMessage } from "@assistant-ui/react";
+
+import * as messageOrder from "../src/features/chat/utils/message-order.ts";
+import * as contextUsageReasoning from "../src/features/chat/utils/context-usage-reasoning.ts";
+import { loadWithStubs } from "./helpers/module-stubs.ts";
+
+type Usage = {
+ promptTokens: number;
+ completionTokens: number;
+ totalTokens: number;
+ cachedTokens: number;
+ estimated?: boolean;
+};
+
+type RefreshModule = {
+ refreshContextUsage: (options?: { invalidate?: boolean; afterModelLoad?: boolean }) => Promise<void>;
+ setActiveBranchReader: (
+ reader: () => readonly ThreadMessage[] | null,
+ ) => void;
+};
+
+function message(
+ id: string,
+ role: "user" | "assistant",
+ text: string,
+): ThreadMessage {
+ const base = { id, createdAt: new Date(0), content: [{ type: "text" as const, text }] };
+ return role === "user"
+ ? { ...base, role, attachments: [], metadata: { custom: {} } }
+ : {
+ ...base,
+ role,
+ status: { type: "complete", reason: "stop" },
+ metadata: {
+ custom: {},
+ steps: [],
+ unstable_annotations: [],
+ unstable_data: [],
+ unstable_state: null,
+ },
+ };
+}
+
+function deferred() {
+ let release = () => {};
+ const promise = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ return { promise, release };
+}
+
+function harness() {
+ const state = {
+ activeThreadId: "thread-1",
+ activeLoadId: "load-1",
+ contextUsage: null as Usage | null,
+ loadedContextLength: 4096,
+ loadedIsGguf: true,
+ modelLoading: false,
+ mcpEnabledForChat: false,
+ models: [],
+ params: { checkpoint: "local-model", temperature: 0.7 },
+ queuedSettingsEpoch: 0,
+ runningByThreadId: {} as Record<string, boolean>,
+ setContextUsage: (usage: Usage | null) => {
+ state.contextUsage = usage;
+ },
+ };
+ const world = {
+ branch: [
+ message("u1", "user", "question"),
+ message("a1", "assistant", "answer"),
+ ],
+ stored: [message("deleted", "assistant", "deleted reply")],
+ extras: {} as Record<string, unknown>,
+ historyFields: {} as Record<string, unknown>,
+ reasoning: { enable_thinking: false },
+ requests: [] as Record<string, unknown>[],
+ gate: null as Promise<void> | null,
+ historyGate: null as Promise<void> | null,
+ storedGate: null as Promise<void> | null,
+ mcpEpoch: 0,
+ failure: false,
+ tokens: 42,
+ };
+ const refresh = loadWithStubs<RefreshModule>(
+ new URL(
+ "../src/features/chat/utils/refresh-context-usage.ts",
+ import.meta.url,
+ ),
+ {
+ "../api/chat-adapter": {
+ buildLocalTokenCountHistory: async (
+ messages: readonly ThreadMessage[],
+ ) => {
+ await world.historyGate;
+ return {
+ messages: messages.map(({ role, content }) => ({ role, content })),
+ ...world.historyFields,
+ };
+ },
+ buildLocalTokenCountExtras: async () => world.extras,
+ buildLocalTokenCountReasoning: () => world.reasoning,
+ findLatestUserAudioBase64: () => null,
+ findLatestUserVideoBase64: () => null,
+ messagesContainImage: () => false,
+ },
+ "../api/chat-api": {
+ countChatInputTokens: async (payload: Record<string, unknown>, serializedPayload: string) => {
+ assert.deepEqual(JSON.parse(serializedPayload), payload);
+ world.requests.push(payload);
+ const input_tokens = world.tokens;
+ const failure = world.failure;
+ await world.gate;
+ if (failure) throw new Error("count unavailable");
+ return { input_tokens, model: "local-model" };
+ },
+ },
+ "../external-providers": { isExternalModelId: (id: string) => id.startsWith("external:") },
+ "../api/mcp-server-mutation-tracker": { getMcpServerMutationEpoch: () => world.mcpEpoch },
+ "../stores/chat-runtime-store": {
+ useChatRuntimeStore: { getState: () => state },
+ },
+ "./branch-head": { savedBranchHead: () => undefined },
+ "./context-usage-reasoning": contextUsageReasoning,
+ "./chat-history-storage": {
+ listStoredChatMessages: async () => {
+ await world.storedGate;
+ return world.stored;
+ },
+ },
+ "./message-order": messageOrder,
+ },
+ );
+ refresh.setActiveBranchReader(() => world.branch);
+ return { state, world, ...refresh };
+}
+
+test("the context recount forwards the tool-history request fields", async () => {
+ const h = harness();
+ h.world.historyFields = { studio_tool_history: true };
+ await h.refreshContextUsage();
+ assert.equal(h.world.requests[0]?.studio_tool_history, true);
+ assert.deepEqual(
+ h.world.requests[0]?.messages,
+ h.world.branch.map(({ role, content }) => ({ role, content })),
+ );
+});
+
+test("changed message identity and compaction boundaries invalidate identical wire history", async () => {
+ const h = harness();
+ await h.refreshContextUsage();
+ h.world.branch[1] = message("a2", "assistant", "answer");
+ await h.refreshContextUsage();
+ h.world.branch[1] = {
+ ...h.world.branch[1],
+ metadata: { custom: { contextTruncation: { boundary_messages: 2 } } },
+ } as ThreadMessage;
+ await h.refreshContextUsage();
+ assert.equal(h.world.requests.length, 3);
+ assert.deepEqual(h.world.requests[0], h.world.requests[2]);
+});
+
+test("successive triggers share a completed count after an estimate is restored", async () => {
+ const h = harness();
+ await h.refreshContextUsage();
+ await h.refreshContextUsage();
+ h.state.contextUsage = {
+ ...h.state.contextUsage!,
+ totalTokens: 20,
+ estimated: true,
+ };
+ await h.refreshContextUsage();
+ assert.equal(h.world.requests.length, 1);
+ assert.equal(h.state.contextUsage?.totalTokens, 42);
+ assert.equal(h.state.contextUsage?.estimated, undefined);
+});
+
+test("request changes and explicit invalidation each obtain a new count", async () => {
+ const h = harness();
+ await h.refreshContextUsage();
+ h.world.reasoning = { enable_thinking: true };
+ await h.refreshContextUsage();
+ h.world.extras = { enable_tools: true, context_overflow: "compact" };
+ await h.refreshContextUsage();
+ await h.refreshContextUsage({ invalidate: true });
+ assert.equal(h.world.requests.length, 4);
+});
+
+test("an old reasoning count is discarded and the queued setting is counted", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.world.gate = gate.promise;
+ const first = h.refreshContextUsage();
+ await setImmediate();
+ h.world.reasoning = { enable_thinking: true };
+ h.world.tokens = 52;
+ await h.refreshContextUsage({ invalidate: true });
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.deepEqual(
+ h.world.requests.map((request) => request.enable_thinking),
+ [false, true],
+ );
+ assert.equal((h.state.contextUsage as Usage | null)?.totalTokens, 52);
+});
+
+for (const invalidate of [false, true]) {
+ test(`a sampling change during a count reuses the result (invalidate=${invalidate})`, async () => {
+ const h = harness();
+ const gate = deferred();
+ h.world.gate = gate.promise;
+ const first = h.refreshContextUsage({ invalidate });
+ await setImmediate();
+ h.state.params.temperature = 0.9;
+ h.state.queuedSettingsEpoch += 1;
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 1);
+ assert.equal(h.state.contextUsage?.totalTokens, 42);
+ });
+}
+
+test("a prompt setting change during a count obtains a replacement without another trigger", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.world.gate = gate.promise;
+ const first = h.refreshContextUsage();
+ await setImmediate();
+ h.world.extras = { enable_tools: true };
+ h.world.tokens = 52;
+ h.state.queuedSettingsEpoch += 1;
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 2);
+ assert.equal(h.world.requests[1]?.enable_tools, true);
+ assert.equal((h.state.contextUsage as Usage | null)?.totalTokens, 52);
+});
+
+test("a settings change while building history restarts the count before sending", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.world.historyGate = gate.promise;
+ const first = h.refreshContextUsage();
+ h.state.queuedSettingsEpoch += 1;
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 1);
+ assert.equal(h.state.contextUsage?.totalTokens, 42);
+});
+
+test("a failed count does not replay an identical overlapping trigger", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.world.gate = gate.promise;
+ h.world.failure = true;
+ const first = h.refreshContextUsage();
+ await setImmediate();
+ await h.refreshContextUsage();
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 1);
+ assert.equal(h.state.contextUsage, null);
+});
+
+test("a failed count still retries queued reasoning changes", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.world.gate = gate.promise;
+ h.world.failure = true;
+ const first = h.refreshContextUsage();
+ await setImmediate();
+ h.world.reasoning = { enable_thinking: true };
+ h.world.failure = false;
+ await h.refreshContextUsage({ invalidate: true });
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 2);
+ assert.equal(h.state.contextUsage?.totalTokens, 42);
+});
+
+test("a failed count still retries a changed branch without explicit invalidation", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.world.gate = gate.promise;
+ h.world.failure = true;
+ const first = h.refreshContextUsage();
+ await setImmediate();
+ h.world.branch = [message("u2", "user", "new question")];
+ h.world.failure = false;
+ await h.refreshContextUsage();
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 2);
+ assert.equal(h.state.contextUsage?.totalTokens, 42);
+});
+
+test("a completed request cache preserves a newer exact generation count", async () => {
+ const h = harness();
+ await h.refreshContextUsage();
+ const completed = { ...h.state.contextUsage!, totalTokens: 80 };
+ h.state.contextUsage = completed;
+ await h.refreshContextUsage();
+ assert.equal(h.world.requests.length, 1);
+ assert.equal(h.state.contextUsage, completed);
+});
+
+test("a cached count cannot replace usage published while history was resolving", async () => {
+ const h = harness();
+ await h.refreshContextUsage();
+ const gate = deferred();
+ h.world.historyGate = gate.promise;
+ const second = h.refreshContextUsage();
+ const completed = { ...h.state.contextUsage!, totalTokens: 80 };
+ h.state.contextUsage = completed;
+ gate.release();
+ await second;
+ assert.equal(h.world.requests.length, 1);
+ assert.equal(h.state.contextUsage, completed);
+});
+
+test("an emptied mounted branch never falls back to deleted stored messages", async () => {
+ const h = harness();
+ h.world.branch = [];
+ await h.refreshContextUsage();
+ assert.deepEqual(h.world.requests[0]?.messages, []);
+});
+
+for (const loadedIsGguf of [true, false]) {
+ test(`tool-enabled counts use the server's result (GGUF=${loadedIsGguf})`, async () => {
+ const h = harness();
+ h.state.loadedIsGguf = loadedIsGguf;
+ h.world.branch = [message("tip", "user", "text")];
+ h.world.extras = {
+ enable_tools: true,
+ permission_mode: "auto",
+ rag_scope: { project_id: "project-1", autoinject: true },
+ };
+ await h.refreshContextUsage();
+ assert.equal(h.world.requests.length, 1);
+ assert.equal(h.state.contextUsage?.totalTokens, 42);
+ });
+
+ test(`a refused tool-enabled count preserves the estimate (GGUF=${loadedIsGguf})`, async () => {
+ const h = harness();
+ h.state.loadedIsGguf = loadedIsGguf;
+ h.world.branch = [message("tip", "user", "text")];
+ h.world.extras = {
+ enable_tools: true,
+ permission_mode: "auto",
+ rag_scope: { project_id: "project-1", autoinject: true },
+ };
+ h.world.failure = true;
+ const estimate = {
+ promptTokens: 10,
+ completionTokens: 0,
+ totalTokens: 10,
+ cachedTokens: 0,
+ estimated: true,
+ };
+ h.state.contextUsage = estimate;
+ await h.refreshContextUsage();
+ assert.equal(h.world.requests.length, 1);
+ assert.equal(h.state.contextUsage, estimate);
+ });
+}
+
+test("negative count responses never become exact usage", async () => {
+ const h = harness();
+ h.world.tokens = -1;
+ await h.refreshContextUsage();
+ assert.equal(h.state.contextUsage, null);
+});
+
+for (const guard of ["external", "loading"]) {
+ test(`explicit invalidation clears the cache during ${guard}`, async () => {
+ const h = harness();
+ await h.refreshContextUsage();
+ h.state.params.checkpoint = guard === "external" ? "external:model" : "local-model";
+ h.state.modelLoading = guard === "loading";
+ await h.refreshContextUsage({ invalidate: true });
+ h.state.params.checkpoint = "local-model";
+ h.state.modelLoading = false;
+ h.state.contextUsage = null;
+ h.world.tokens = 52;
+ await h.refreshContextUsage();
+ assert.equal(h.world.requests.length, 2);
+ assert.equal((h.state.contextUsage as Usage | null)?.totalTokens, 52);
+ });
+}
+
+test("a model reload counts again even with identical configuration", async () => {
+ const h = harness();
+ await h.refreshContextUsage();
+ h.world.tokens = 52;
+ await h.refreshContextUsage({ afterModelLoad: true });
+ await h.refreshContextUsage();
+ assert.equal(h.world.requests.length, 2);
+ assert.equal((h.state.contextUsage as Usage | null)?.totalTokens, 52);
+});
+
+test("overlapping post-load triggers share the same count", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.world.gate = gate.promise;
+ const first = h.refreshContextUsage({ afterModelLoad: true });
+ await setImmediate();
+ await h.refreshContextUsage({ afterModelLoad: true });
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 1);
+ assert.equal(h.state.contextUsage?.totalTokens, 42);
+});
+
+test("a new load supersedes an in-flight post-load count", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.world.gate = gate.promise;
+ const first = h.refreshContextUsage({ afterModelLoad: true });
+ await setImmediate();
+ h.state.activeLoadId = "load-2";
+ h.world.tokens = 52;
+ await h.refreshContextUsage({ afterModelLoad: true });
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 2);
+ assert.equal(h.state.contextUsage?.totalTokens, 52);
+});
+
+test("MCP server mutations invalidate counts with otherwise identical requests", async () => {
+ const h = harness();
+ h.state.mcpEnabledForChat = true;
+ h.world.extras = { enable_tools: true, mcp_enabled: true };
+ h.world.branch = [];
+ await h.refreshContextUsage();
+ h.world.mcpEpoch += 1;
+ h.state.contextUsage = null;
+ h.world.tokens = 52;
+ await h.refreshContextUsage();
+ assert.equal(h.world.requests.length, 2);
+ assert.deepEqual(h.world.requests[0], h.world.requests[1]);
+ assert.equal((h.state.contextUsage as Usage | null)?.totalTokens, 52);
+});
+
+test("MCP changes during an in-flight count cannot publish the old schemas", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.state.mcpEnabledForChat = true;
+ h.world.extras = { enable_tools: true, mcp_enabled: true };
+ h.world.gate = gate.promise;
+ const first = h.refreshContextUsage();
+ await setImmediate();
+ h.world.mcpEpoch += 1;
+ h.world.tokens = 52;
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 2);
+ assert.equal((h.state.contextUsage as Usage | null)?.totalTokens, 52);
+});
+
+test("a plain queued trigger cannot undo an in-flight explicit invalidation", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.world.gate = gate.promise;
+ const first = h.refreshContextUsage();
+ await setImmediate();
+ h.world.tokens = 52;
+ await h.refreshContextUsage({ invalidate: true });
+ await h.refreshContextUsage();
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 2);
+ assert.equal((h.state.contextUsage as Usage | null)?.totalTokens, 52);
+});
+
+test("exact usage restored during storage loading prevents a count and settings replay", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.setActiveBranchReader(() => null);
+ h.world.storedGate = gate.promise;
+ const first = h.refreshContextUsage();
+ await setImmediate();
+ h.state.queuedSettingsEpoch += 1;
+ const restored = { promptTokens: 40, completionTokens: 2, totalTokens: 42, cachedTokens: 0 };
+ h.state.contextUsage = restored;
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 0);
+ assert.equal(h.state.contextUsage, restored);
+});
+
+test("an overlapping trigger cannot replace a restored exact count", async () => {
+ const h = harness();
+ const gate = deferred();
+ h.world.gate = gate.promise;
+ const first = h.refreshContextUsage();
+ await setImmediate();
+ await h.refreshContextUsage();
+ h.state.queuedSettingsEpoch += 1;
+ const restored = { promptTokens: 40, completionTokens: 2, totalTokens: 42, cachedTokens: 0 };
+ h.state.contextUsage = restored;
+ gate.release();
+ await first;
+ await setImmediate();
+ assert.equal(h.world.requests.length, 1);
+ assert.equal(h.state.contextUsage, restored);
+});
+
+test("compacted chats are not recounted by backends without boundary replay", async () => {
+ const h = harness();
+ h.state.loadedIsGguf = false;
+ h.world.branch[1] = {
+ ...h.world.branch[1],
+ metadata: { custom: { contextTruncation: { boundary_messages: 2 } } },
+ } as ThreadMessage;
+ const restored = { promptTokens: 40, completionTokens: 2, totalTokens: 42, cachedTokens: 0 };
+ h.state.contextUsage = restored;
+ await h.refreshContextUsage();
+ assert.equal(h.world.requests.length, 0);
+ assert.equal(h.state.contextUsage, restored);
+});
+
+test("a changed injected system prompt invalidates identical branch content", async () => {
+ const h = harness();
+ await h.refreshContextUsage();
+ h.world.historyFields = {
+ messages: [{ role: "system", content: "updated project instructions" }],
+ };
+ await h.refreshContextUsage();
+ assert.equal(h.world.requests.length, 2);
+});
diff --git a/studio/frontend/tests/context-usage-runtime.test.ts b/studio/frontend/tests/context-usage-runtime.test.ts
new file mode 100644
index 00000000000..6de20c5cdc6
--- /dev/null
+++ b/studio/frontend/tests/context-usage-runtime.test.ts
@@ -0,0 +1,655 @@
+// 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 { readFileSync } from "node:fs";
+import test from "node:test";
+import type { ThreadMessage } from "@assistant-ui/react";
+import ts from "@typescript/typescript6";
+
+import { isExternalModelId } from "../src/features/chat/external-providers.ts";
+import { contextUsageReasoningKey } from "../src/features/chat/utils/context-usage-reasoning.ts";
+import * as savedUsage from "../src/features/chat/utils/saved-context-usage.ts";
+
+const source = ts.createSourceFile(
+ "runtime-provider.tsx",
+ readFileSync(new URL("../src/features/chat/runtime-provider.tsx", import.meta.url), "utf8"),
+ ts.ScriptTarget.ES2022,
+ true,
+ ts.ScriptKind.TSX,
+);
+
+const refreshSource = ts.createSourceFile(
+ "refresh-context-usage.ts",
+ readFileSync(new URL("../src/features/chat/utils/refresh-context-usage.ts", import.meta.url), "utf8"),
+ ts.ScriptTarget.ES2022,
+ true,
+ ts.ScriptKind.TS,
+);
+
+const browserToolsSource = ts.createSourceFile(
+ "tools.ts",
+ readFileSync(new URL("../src/features/browser/agent/tools.ts", import.meta.url), "utf8"),
+ ts.ScriptTarget.ES2022,
+ true,
+ ts.ScriptKind.TS,
+);
+
+const storeSource = ts.createSourceFile(
+ "chat-runtime-store.ts",
+ readFileSync(new URL("../src/features/chat/stores/chat-runtime-store.ts", import.meta.url), "utf8"),
+ ts.ScriptTarget.ES2022,
+ true,
+ ts.ScriptKind.TS,
+);
+
+function runtimeFunction(name: string, dependencies: Record<string, unknown>, sourceFile = source) {
+ const declaration = sourceFile.statements.find(
+ (statement): statement is ts.FunctionDeclaration =>
+ ts.isFunctionDeclaration(statement) && statement.name?.text === name,
+ );
+ assert.ok(declaration, `${name} exists`);
+ const { outputText } = ts.transpileModule(declaration.getText(sourceFile).replace(/^export /, ""), {
+ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS },
+ });
+ return new Function(...Object.keys(dependencies), `${outputText}\nreturn ${name};`)(
+ ...Object.values(dependencies),
+ ) as (props: { enabled: boolean }) => null;
+}
+
+function usage(totalTokens: number, reasoningKey?: string): savedUsage.BranchContextUsage {
+ return { promptTokens: totalTokens, completionTokens: 0, totalTokens, cachedTokens: 0, reasoningKey };
+}
+
+function reply(count: savedUsage.BranchContextUsage): ThreadMessage {
+ return {
+ id: "reply",
+ role: "assistant",
+ createdAt: new Date(0),
+ content: [{ type: "text", text: "Saved reply" }],
+ status: { type: "complete", reason: "stop" },
+ metadata: {
+ custom: { contextUsage: { ...count, modelId: "local-model" } },
+ steps: [],
+ unstable_annotations: [],
+ unstable_data: [],
+ unstable_state: null,
+ },
+ };
+}
+
+function harness(name = "ThreadContextUsageRecount") {
+ const state = {
+ activeThreadId: "thread-a" as string | null,
+ params: { checkpoint: "local-model", systemPrompt: "Original instructions", temperature: 0.6 },
+ loadedChatTemplateOverride: "original-template",
+ loadedContextLength: 4096 as number | null,
+ loadedIsGguf: true,
+ loadedIsMlx: false,
+ modelLoading: false,
+ threadScopedSettingsPending: false,
+ queuedSettingsEpoch: 0,
+ toolsEnabled: false,
+ supportsTools: true,
+ supportsBuiltinImageGeneration: false,
+ imageToolsEnabled: false,
+ localImageModelReady: false,
+ localVideoModelReady: false,
+ reasoningKey: undefined as string | undefined,
+ runningByThreadId: {} as Record<string, boolean>,
+ contextUsage: usage(42, contextUsageReasoningKey({})) as savedUsage.BranchContextUsage | null,
+ setContextUsage: (next: savedUsage.BranchContextUsage | null) => {
+ state.contextUsage = next;
+ writes.push(next);
+ },
+ setThreadContextUsage: () => {},
+ };
+ const world = {
+ mainThreadId: "thread-a",
+ thread: { isLoading: false, messages: [reply(state.contextUsage!)] },
+ recounts: [] as unknown[],
+ image: false,
+ audio: false,
+ video: false,
+ browser: {
+ enabled: false,
+ historyAccess: "ask" as "ask" | "allow" | "never",
+ scriptAccess: "ask" as "ask" | "never",
+ },
+ browserToolSelections: [] as unknown[],
+ };
+ const writes: Array<savedUsage.BranchContextUsage | null> = [];
+ const refs: Array<{ current: unknown }> = [];
+ const previousDeps: Array<readonly unknown[]> = [];
+ let pendingEffects: Array<() => unknown> = [];
+ let hookIndex = 0;
+ let branchReader: (() => readonly ThreadMessage[] | null) | null = null;
+ const aui = {
+ thread: () => ({ getState: () => world.thread }),
+ threads: () => ({ getState: () => ({ mainThreadId: world.mainThreadId }) }),
+ };
+ const useChatRuntimeStore = Object.assign(
+ (selector: (snapshot: typeof state) => unknown) => selector(state),
+ { getState: () => state },
+ );
+ const useBrowserAgentStore = Object.assign(
+ (selector: (snapshot: typeof world.browser) => unknown) => {
+ const selected = selector(world.browser);
+ world.browserToolSelections.push(selected);
+ return selected;
+ },
+ { getState: () => world.browser },
+ );
+ const browserAgentToolsForTurn = runtimeFunction("browserAgentToolsForTurn", {
+ useBrowserAgentStore,
+ browserPanelAvailable: () => true,
+ isTauri: false,
+ BROWSER_AGENT_TOOLS: ["browser_navigate"],
+ BROWSER_SCREENSHOT_TOOL: "browser_screenshot",
+ BROWSER_HISTORY_TOOL: "browser_history",
+ BROWSER_SCRIPT_TOOL: "browser_evaluate",
+ }, browserToolsSource);
+ const component = runtimeFunction(name, {
+ ...savedUsage,
+ isExternalModelId,
+ currentContextUsageReasoningKey: (snapshot = state) => snapshot.reasoningKey,
+ currentContextUsageSettingsKey: runtimeFunction("currentContextUsageSettingsKey", {
+ localMediaToolsForTurn: runtimeFunction("localMediaToolsForTurn", {
+ localImageTool: runtimeFunction("localImageTool", {}, storeSource),
+ }, storeSource),
+ }, refreshSource),
+ useChatRuntimeStore,
+ useBrowserAgentStore,
+ browserAgentToolsForTurn,
+ useAui: () => aui,
+ useAuiState: (selector: (snapshot: { thread: typeof world.thread }) => unknown) => selector(world),
+ usePromptQueueUI: (selector: (snapshot: { byThreadId: Record<string, never> }) => unknown) =>
+ selector({ byThreadId: {} }),
+ useRef: (initial: unknown) => refs[hookIndex++] ??= { current: initial },
+ useEffect: (effect: () => unknown, dependencies: readonly unknown[]) => {
+ const index = hookIndex++;
+ const previous = previousDeps[index];
+ previousDeps[index] = dependencies;
+ if (!previous || dependencies.some((value, i) => !Object.is(value, previous[i]))) {
+ pendingEffects.push(effect);
+ }
+ },
+ refreshContextUsage: (options: unknown) => {
+ world.recounts.push(options);
+ return Promise.resolve();
+ },
+ setActiveBranchReader: (reader: typeof branchReader) => { branchReader = reader; },
+ messagesContainImage: () => world.image,
+ findLatestUserAudioBase64: () => world.audio ? "audio" : null,
+ findLatestUserVideoBase64: () => world.video ? "video" : null,
+ });
+ const render = () => {
+ hookIndex = 0;
+ pendingEffects = [];
+ component({ enabled: true });
+ for (const effect of pendingEffects) effect();
+ };
+ return { state, world, writes, render, readBranch: () => branchReader?.() };
+}
+
+test("pairing a non-reasoning chat preserves its recounted usage", () => {
+ const h = harness();
+ h.world.thread.messages = [reply(usage(114))];
+ h.state.contextUsage = usage(101, contextUsageReasoningKey({}));
+ const recounted = h.state.contextUsage;
+ h.state.threadScopedSettingsPending = true;
+ h.render();
+ h.state.threadScopedSettingsPending = false;
+ h.render();
+ assert.equal(h.state.contextUsage, recounted);
+ assert.deepEqual(h.writes, []);
+ assert.deepEqual(h.world.recounts, []);
+});
+
+test("pairing the first send for a non-reasoning model does not clear the exact prompt count", () => {
+ const h = harness();
+ h.world.thread.messages = [];
+ const counted = h.state.contextUsage;
+ h.state.threadScopedSettingsPending = true;
+ h.render();
+ h.state.threadScopedSettingsPending = false;
+ h.render();
+ assert.equal(h.state.contextUsage, counted);
+ assert.deepEqual(h.writes, []);
+ assert.deepEqual(h.world.recounts, []);
+});
+
+test("an effective reasoning change still estimates and recounts the loaded branch", () => {
+ const h = harness();
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: true });
+ h.state.contextUsage = usage(42, h.state.reasoningKey);
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.render();
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: false });
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ assert.equal(h.state.contextUsage?.totalTokens, 42);
+ assert.equal(h.world.recounts.length, 1);
+});
+
+for (const unavailable of [null, "image", "audio", "video", "unloaded", "compacted", "unsupported"] as const) {
+ test(`returning to the reply's reasoning mode restores its exact count (${unavailable ?? "text"})`, () => {
+ const h = harness();
+ const enabled = contextUsageReasoningKey({ enable_thinking: true });
+ const disabled = contextUsageReasoningKey({ enable_thinking: false });
+ h.state.reasoningKey = enabled;
+ const saved = { ...usage(643, enabled), estimatedTokens: 6 };
+ h.state.contextUsage = saved;
+ h.world.thread.messages = [reply(saved)];
+ if (unavailable === "image" || unavailable === "audio" || unavailable === "video") {
+ h.world[unavailable] = true;
+ } else if (unavailable === "unloaded") {
+ h.state.loadedContextLength = null;
+ } else if (unavailable === "compacted") {
+ h.state.loadedIsGguf = false;
+ h.state.loadedIsMlx = true;
+ h.world.thread.messages[0].metadata.custom.contextTruncation = { summary: "Earlier turns" };
+ } else if (unavailable === "unsupported") {
+ h.state.loadedIsGguf = false;
+ }
+ h.render();
+ h.state.reasoningKey = disabled;
+ h.state.queuedSettingsEpoch += 1;
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ assert.ok((h.state.contextUsage?.totalTokens ?? 0) > 600);
+ assert.equal(h.world.recounts.length, unavailable === null ? 1 : 0);
+ h.state.reasoningKey = enabled;
+ h.state.queuedSettingsEpoch += 1;
+ h.render();
+ assert.equal(h.state.contextUsage, h.world.thread.messages[0].metadata.custom.contextUsage);
+ assert.equal(h.world.recounts.length, unavailable === null ? 1 : 0);
+ });
+}
+
+test("changing reasoning after opening a legacy chat estimates and recounts its saved usage", () => {
+ const h = harness();
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: true });
+ h.state.contextUsage = usage(84);
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.render();
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: false });
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ assert.equal(h.world.recounts.length, 1);
+});
+
+for (const setting of ["systemPrompt", "chatTemplate", "tools"] as const) {
+ test(`returning to a saved reasoning mode recounts after ${setting} changes`, () => {
+ const h = harness();
+ const enabled = contextUsageReasoningKey({ enable_thinking: true });
+ const disabled = contextUsageReasoningKey({ enable_thinking: false });
+ h.state.reasoningKey = enabled;
+ h.state.contextUsage = usage(84, enabled);
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.render();
+ h.state.reasoningKey = disabled;
+ h.state.queuedSettingsEpoch += 1;
+ h.render();
+ if (setting === "systemPrompt") {
+ h.state.params.systemPrompt = "Updated instructions";
+ } else if (setting === "chatTemplate") {
+ h.state.loadedChatTemplateOverride = "updated-template";
+ } else {
+ h.state.toolsEnabled = true;
+ h.state.queuedSettingsEpoch += 1;
+ }
+ h.state.contextUsage = usage(1990, disabled);
+ h.render();
+ const recounts = h.world.recounts.length;
+ h.state.reasoningKey = enabled;
+ h.state.queuedSettingsEpoch += 1;
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ assert.equal(h.state.contextUsage?.totalTokens, 1990);
+ assert.equal(h.world.recounts.length, recounts + 1);
+ });
+}
+
+for (const setting of ["enabled", "historyAccess", "scriptAccess"] as const) {
+ test(`browser ${setting} changes are recounted only after thinking changes`, () => {
+ const h = harness();
+ const enabled = contextUsageReasoningKey({ enable_thinking: true });
+ const disabled = contextUsageReasoningKey({ enable_thinking: false });
+ h.state.reasoningKey = enabled;
+ h.state.contextUsage = usage(84, enabled);
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.world.browser.enabled = setting !== "enabled";
+ h.render();
+ const initialTools = h.world.browserToolSelections.at(-1);
+ assert.equal(typeof initialTools, "string");
+ const saved = h.state.contextUsage;
+ if (setting === "enabled") {
+ h.world.browser.enabled = true;
+ } else {
+ h.world.browser[setting] = "never";
+ }
+ h.render();
+ assert.notEqual(h.world.browserToolSelections.at(-1), initialTools);
+ assert.equal(h.state.contextUsage, saved);
+ assert.deepEqual(h.world.recounts, []);
+ h.state.reasoningKey = disabled;
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ assert.equal(h.world.recounts.length, 1);
+ h.state.contextUsage = usage(1990, disabled);
+ h.state.reasoningKey = enabled;
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ assert.equal(h.state.contextUsage?.totalTokens, 1990);
+ assert.equal(h.world.recounts.length, 2);
+ });
+}
+
+for (const browserEnabled of [false, true]) {
+ test(`unchanged browser tools preserve saved usage when browser is ${browserEnabled ? "enabled" : "disabled"}`, () => {
+ const h = harness();
+ const enabled = contextUsageReasoningKey({ enable_thinking: true });
+ const disabled = contextUsageReasoningKey({ enable_thinking: false });
+ h.state.reasoningKey = enabled;
+ h.state.contextUsage = usage(84, enabled);
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.world.browser.enabled = browserEnabled;
+ h.render();
+ const initialTools = h.world.browserToolSelections.at(-1);
+ assert.equal(typeof initialTools, "string");
+ h.world.browser.historyAccess = browserEnabled ? "allow" : "never";
+ if (!browserEnabled) h.world.browser.scriptAccess = "never";
+ h.render();
+ assert.equal(h.world.browserToolSelections.at(-1), initialTools);
+ assert.deepEqual(h.world.recounts, []);
+ h.state.reasoningKey = disabled;
+ h.render();
+ h.state.contextUsage = usage(1990, disabled);
+ h.state.reasoningKey = enabled;
+ h.render();
+ assert.equal(h.state.contextUsage, h.world.thread.messages[0].metadata.custom.contextUsage);
+ assert.equal(h.world.recounts.length, 1);
+ });
+}
+
+for (const setting of ["imageToolsEnabled", "localImageModelReady", "localVideoModelReady"] as const) {
+ test(`local media ${setting} changes invalidate saved usage when thinking changes`, () => {
+ const h = harness();
+ const enabled = contextUsageReasoningKey({ enable_thinking: true });
+ const disabled = contextUsageReasoningKey({ enable_thinking: false });
+ h.state.reasoningKey = enabled;
+ h.state.contextUsage = usage(84, enabled);
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.state.imageToolsEnabled = true;
+ h.state.localImageModelReady = true;
+ h.render();
+ const saved = h.state.contextUsage;
+ h.state[setting] = !h.state[setting];
+ h.render();
+ assert.equal(h.state.contextUsage, saved);
+ assert.deepEqual(h.world.recounts, []);
+ h.state.reasoningKey = disabled;
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ assert.equal(h.world.recounts.length, 1);
+ h.state.contextUsage = usage(1990, disabled);
+ h.state.reasoningKey = enabled;
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ assert.equal(h.state.contextUsage?.totalTokens, 1990);
+ assert.equal(h.world.recounts.length, 2);
+ });
+}
+
+for (const unavailable of ["disabled", "unsupported", "hosted"] as const) {
+ test(`media readiness preserves saved usage when local tools are ${unavailable}`, () => {
+ const h = harness();
+ const enabled = contextUsageReasoningKey({ enable_thinking: true });
+ const disabled = contextUsageReasoningKey({ enable_thinking: false });
+ h.state.reasoningKey = enabled;
+ h.state.contextUsage = usage(84, enabled);
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.state.imageToolsEnabled = unavailable !== "disabled";
+ h.state.supportsTools = unavailable !== "unsupported";
+ h.state.supportsBuiltinImageGeneration = unavailable === "hosted";
+ h.render();
+ h.state.localImageModelReady = true;
+ h.state.localVideoModelReady = true;
+ h.render();
+ assert.deepEqual(h.world.recounts, []);
+ h.state.reasoningKey = disabled;
+ h.render();
+ h.state.contextUsage = usage(1990, disabled);
+ h.state.reasoningKey = enabled;
+ h.render();
+ assert.equal(h.state.contextUsage, h.world.thread.messages[0].metadata.custom.contextUsage);
+ assert.equal(h.world.recounts.length, 1);
+ });
+}
+
+test("a reply generated after prompt changes can be restored without another count", () => {
+ const h = harness();
+ const enabled = contextUsageReasoningKey({ enable_thinking: true });
+ const disabled = contextUsageReasoningKey({ enable_thinking: false });
+ h.state.reasoningKey = enabled;
+ h.state.contextUsage = usage(84, enabled);
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.render();
+ h.state.params.systemPrompt = "Updated instructions";
+ h.state.queuedSettingsEpoch += 1;
+ h.render();
+ const next = { ...usage(1990, enabled), estimatedTokens: 6 };
+ h.world.thread.messages = [reply(next)];
+ h.state.contextUsage = next;
+ h.render();
+ h.state.reasoningKey = disabled;
+ h.state.queuedSettingsEpoch += 1;
+ h.render();
+ const recounts = h.world.recounts.length;
+ h.state.reasoningKey = enabled;
+ h.state.queuedSettingsEpoch += 1;
+ h.render();
+ assert.equal(h.state.contextUsage, h.world.thread.messages[0].metadata.custom.contextUsage);
+ assert.equal(h.world.recounts.length, recounts);
+});
+
+test("switching to a reply priced before a prompt change recounts it", () => {
+ const h = harness();
+ h.state.contextUsage = usage(84, contextUsageReasoningKey({}));
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.render();
+ h.state.params.systemPrompt = "Updated instructions";
+ h.state.queuedSettingsEpoch += 1;
+ h.render();
+ h.state.contextUsage = usage(1990, contextUsageReasoningKey({}));
+ const recounts = h.world.recounts.length;
+ h.world.thread.messages = [{ ...reply(usage(84, contextUsageReasoningKey({}))), id: "sibling" }];
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ assert.equal(h.world.recounts.length, recounts + 1);
+});
+
+test("batched tool and reasoning changes cannot restore the old prompt count", () => {
+ const h = harness();
+ const enabled = contextUsageReasoningKey({ enable_thinking: true });
+ const disabled = contextUsageReasoningKey({ enable_thinking: false });
+ h.state.reasoningKey = enabled;
+ h.state.contextUsage = usage(84, enabled);
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.render();
+ h.state.reasoningKey = disabled;
+ h.state.toolsEnabled = true;
+ h.state.queuedSettingsEpoch += 2;
+ h.render();
+ h.state.contextUsage = usage(1990, disabled);
+ const recounts = h.world.recounts.length;
+ h.state.reasoningKey = enabled;
+ h.state.queuedSettingsEpoch += 1;
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ assert.equal(h.state.contextUsage?.totalTokens, 1990);
+ assert.equal(h.world.recounts.length, recounts + 1);
+});
+
+test("thinking toggles that also update sampling and preserve settings retain the saved count", () => {
+ const h = harness();
+ const enabled = contextUsageReasoningKey({ enable_thinking: true, preserve_thinking: false });
+ const disabled = contextUsageReasoningKey({ enable_thinking: false, preserve_thinking: false });
+ h.state.reasoningKey = enabled;
+ h.state.contextUsage = usage(643, enabled);
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.world.image = true;
+ h.render();
+ h.state.reasoningKey = disabled;
+ h.state.params.temperature = 0.7;
+ h.state.queuedSettingsEpoch += 3;
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ h.state.reasoningKey = enabled;
+ h.state.params.temperature = 0.6;
+ h.state.queuedSettingsEpoch += 2;
+ h.render();
+ assert.equal(h.state.contextUsage, h.world.thread.messages[0].metadata.custom.contextUsage);
+ assert.deepEqual(h.world.recounts, []);
+});
+
+test("deleting an attachment invalidates exact usage when only the branch revision changes", () => {
+ const h = harness();
+ const user: ThreadMessage = {
+ id: "user",
+ role: "user",
+ createdAt: new Date(0),
+ content: [
+ { type: "text", text: "Read my file" },
+ { type: "image", image: "data:image/png;base64,aW1hZ2U=" },
+ ],
+ attachments: [],
+ metadata: { custom: {} },
+ };
+ const messages = [user, reply(usage(643))];
+ const saved = {
+ ...usage(643),
+ ...savedUsage.contextUsageMarkers(savedUsage.threadBranchRecords(messages)),
+ };
+ h.world.thread.messages = [user, reply(saved)];
+ h.world.image = true;
+ h.state.contextUsage = saved;
+ h.render();
+ assert.deepEqual(h.world.recounts, []);
+ const tip = h.world.thread.messages.at(-1);
+ const length = h.world.thread.messages.length;
+ h.world.thread.messages = [
+ {
+ ...user,
+ content: user.content.slice(0, 1),
+ metadata: { custom: { revision: 1 } },
+ },
+ h.world.thread.messages[1],
+ ];
+ h.world.image = false;
+ h.render();
+ assert.equal(h.world.thread.messages.at(-1), tip);
+ assert.equal(h.world.thread.messages.length, length);
+ assert.equal(h.state.contextUsage?.estimated, true);
+ assert.deepEqual(h.world.recounts, [{ threadId: "thread-a" }]);
+ h.render();
+ assert.equal(h.world.recounts.length, 1);
+});
+
+test("hydrating the saved reasoning settings keeps legacy usage exact", () => {
+ const h = harness();
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: true });
+ h.state.contextUsage = usage(84);
+ h.world.thread.messages = [reply(h.state.contextUsage)];
+ h.state.threadScopedSettingsPending = true;
+ h.render();
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: false });
+ h.state.threadScopedSettingsPending = false;
+ h.render();
+ assert.equal(h.state.contextUsage?.totalTokens, 84);
+ assert.equal(h.state.contextUsage?.estimated, undefined);
+ assert.deepEqual(h.world.recounts, []);
+});
+
+test("settings pairing preserves an exact count already priced for the restored reasoning mode", () => {
+ const h = harness();
+ const enabled = contextUsageReasoningKey({ enable_thinking: true });
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: false });
+ h.state.threadScopedSettingsPending = true;
+ h.world.thread.messages = [reply(usage(84, enabled))];
+ h.render();
+ const counted = usage(101, enabled);
+ h.state.contextUsage = counted;
+ h.state.reasoningKey = enabled;
+ h.state.threadScopedSettingsPending = false;
+ h.render();
+ assert.equal(h.state.contextUsage, counted);
+ assert.deepEqual(h.writes, []);
+ assert.deepEqual(h.world.recounts, []);
+});
+
+test("settings pairing cannot replace restored usage with the still-loading empty branch", () => {
+ const h = harness();
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: true });
+ h.state.threadScopedSettingsPending = true;
+ h.world.thread = { isLoading: true, messages: [] };
+ h.render();
+ const restored = usage(84);
+ h.state.contextUsage = restored;
+ h.state.threadScopedSettingsPending = false;
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: false });
+ h.render();
+ assert.equal(h.state.contextUsage, restored);
+ assert.deepEqual(h.writes, []);
+ assert.deepEqual(h.world.recounts, []);
+});
+
+test("finishing history loading restores exact usage after reasoning settings settle", () => {
+ const h = harness();
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: false });
+ h.state.threadScopedSettingsPending = true;
+ h.state.contextUsage = { ...usage(10), estimated: true };
+ h.world.thread = { isLoading: true, messages: [] };
+ h.render();
+ h.world.recounts.length = 0;
+ h.state.threadScopedSettingsPending = false;
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: true });
+ h.render();
+ assert.equal(h.state.contextUsage?.estimated, true);
+ h.world.thread = {
+ isLoading: false,
+ messages: [reply(usage(84, h.state.reasoningKey))],
+ };
+ h.render();
+ assert.equal(h.state.contextUsage?.totalTokens, 84);
+ assert.equal(h.state.contextUsage?.estimated, undefined);
+ assert.deepEqual(h.world.recounts, []);
+});
+
+test("reasoning repricing ignores an outgoing runtime during a thread switch", () => {
+ const h = harness();
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: true });
+ h.state.contextUsage = usage(42, h.state.reasoningKey);
+ h.render();
+ const shown = h.state.contextUsage;
+ h.world.mainThreadId = "outgoing-thread";
+ h.state.reasoningKey = contextUsageReasoningKey({ enable_thinking: false });
+ h.render();
+ assert.equal(h.state.contextUsage, shown);
+ assert.deepEqual(h.writes, []);
+ assert.deepEqual(h.world.recounts, []);
+});
+
+test("the registered branch belongs to the active thread and must finish loading", () => {
+ const h = harness("ActiveBranchRegistrar");
+ h.render();
+ assert.equal(h.readBranch(), h.world.thread.messages);
+ h.world.mainThreadId = "blank-thread";
+ h.world.thread.messages = [];
+ assert.equal(h.readBranch(), null);
+ h.world.mainThreadId = h.state.activeThreadId!;
+ assert.equal(h.readBranch(), h.world.thread.messages);
+ h.world.thread.isLoading = true;
+ assert.equal(h.readBranch(), null);
+});
diff --git a/studio/frontend/tests/current-date-context-usage.test.ts b/studio/frontend/tests/current-date-context-usage.test.ts
index 3e18e0b7fbd..ec165e8d27c 100644
--- a/studio/frontend/tests/current-date-context-usage.test.ts
+++ b/studio/frontend/tests/current-date-context-usage.test.ts
@@ -5,6 +5,7 @@ import assert from "node:assert/strict";
import test from "node:test";
import * as messageOrder from "../src/features/chat/utils/message-order.ts";
+import * as contextUsageReasoning from "../src/features/chat/utils/context-usage-reasoning.ts";
import { loadWithStubs } from "./helpers/module-stubs.ts";
type RefreshModule = {
@@ -31,6 +32,7 @@ test("a prompt setting change clears local usage even during a run", async () =>
{
"../api/chat-adapter": {},
"../api/chat-api": {},
+ "../api/mcp-server-mutation-tracker": {},
"../external-providers": {
isExternalModelId: () => false,
},
@@ -39,6 +41,7 @@ test("a prompt setting change clears local usage even during a run", async () =>
},
"./message-order": messageOrder,
"./branch-head": { savedBranchHead: () => undefined },
+ "./context-usage-reasoning": contextUsageReasoning,
"./chat-history-storage": {},
},
);
@@ -68,6 +71,7 @@ test("a prompt setting change leaves external usage intact", async () => {
{
"../api/chat-adapter": {},
"../api/chat-api": {},
+ "../api/mcp-server-mutation-tracker": {},
"../external-providers": {
isExternalModelId: () => true,
},
@@ -76,6 +80,7 @@ test("a prompt setting change leaves external usage intact", async () => {
},
"./message-order": messageOrder,
"./branch-head": { savedBranchHead: () => undefined },
+ "./context-usage-reasoning": contextUsageReasoning,
"./chat-history-storage": {},
},
);
diff --git a/studio/frontend/tests/delete-context-usage.test.ts b/studio/frontend/tests/delete-context-usage.test.ts
new file mode 100644
index 00000000000..f9946108d86
--- /dev/null
+++ b/studio/frontend/tests/delete-context-usage.test.ts
@@ -0,0 +1,168 @@
+// 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 { MessageRepository } from "@assistant-ui/core/internal";
+import type { ExportedMessageRepository, ThreadMessage } from "@assistant-ui/react";
+import type { MessageRecord } from "../src/features/chat/types.ts";
+import * as researchSync from "../src/features/chat/utils/research-message-sync.ts";
+import * as savedUsage from "../src/features/chat/utils/saved-context-usage.ts";
+import { orderBySelectedBranch } from "../src/features/chat/utils/message-order.ts";
+import { loadWithStubs } from "./helpers/module-stubs.ts";
+
+type DeleteModule = typeof import("../src/features/chat/utils/delete-thread-message.ts");
+
+const legacyUsage = {
+ modelId: "local-model", promptTokens: 28_902, completionTokens: 0,
+ totalTokens: 28_902, cachedTokens: 0,
+};
+
+function message(id: string, role: "user" | "assistant", count?: typeof legacyUsage): ThreadMessage {
+ return {
+ id, role, createdAt: new Date(0), content: [{ type: "text", text: `Text of ${id}` }],
+ metadata: { custom: count ? { contextUsage: count } : {} },
+ ...(role === "assistant" ? { status: { type: "complete", reason: "stop" } } : { attachments: [] }),
+ } as ThreadMessage;
+}
+
+function conversation(): ExportedMessageRepository {
+ return {
+ headId: "a2",
+ messages: [
+ { parentId: null, message: message("u1", "user") },
+ { parentId: "u1", message: message("a1", "assistant") },
+ { parentId: "a1", message: message("u2", "user") },
+ { parentId: "u2", message: message("a2", "assistant", legacyUsage) },
+ ],
+ };
+}
+
+function harness(exported: ExportedMessageRepository, failure?: Error) {
+ let imported: ExportedMessageRepository | undefined;
+ let persisted: MessageRecord[] | undefined;
+ let reads = 0;
+ const module = loadWithStubs<DeleteModule>(
+ new URL("../src/features/chat/utils/delete-thread-message.ts", import.meta.url),
+ {
+ "@assistant-ui/core/internal": { MessageRepository },
+ "../api/chat-api": { listChatMessages: async () => { reads++; return []; } },
+ "./chat-history-storage": {
+ ensureStoredChatThread: async () => { reads++; },
+ syncStoredChatMessages: async (_id: string, records: MessageRecord[]) => {
+ if (failure) throw failure;
+ persisted = JSON.parse(JSON.stringify(records));
+ return persisted;
+ },
+ },
+ "./research-message-sync": researchSync,
+ "./saved-context-usage": savedUsage,
+ },
+ );
+ const records = (repo: ExportedMessageRepository) => repo.messages.map(({ message, parentId }) =>
+ module.exportedItemToRecord("thread", parentId, message),
+ );
+ return {
+ remove: (messageId: string, remoteId: string | undefined = "thread") => module.deleteThreadMessage({
+ thread: { export: () => exported, import: (next) => { imported = next; } },
+ messageId, remoteId,
+ }),
+ imported: () => imported,
+ live: () => records(imported ?? exported),
+ persisted: () => persisted,
+ reads: () => reads,
+ };
+}
+
+function priced(records: MessageRecord[], head = "a2", model = "local-model") {
+ return savedUsage.branchContextUsage(
+ orderBySelectedBranch(records, head), model, model.startsWith("external::") ? null : 65_536,
+ model.startsWith("external::") ? null : 65_536,
+ );
+}
+
+for (const deleted of ["a1", "u1"]) {
+ test(`deleting ${deleted} invalidates a later legacy count in memory and after reload`, async () => {
+ const original = conversation();
+ const before = structuredClone(original);
+ const h = harness(original);
+ assert.equal(priced(h.live()), legacyUsage);
+ await h.remove(deleted);
+ assert.deepEqual(original, before, "deletion mutated the original repository");
+ assert.ok(h.live().some((m) => m.id === "a2"), "the later answer must survive");
+ for (const records of [h.live(), h.persisted()!]) {
+ const count = priced(records);
+ assert.ok(!count || count.estimated, "the deleted history's count was restored as exact");
+ }
+ assert.equal(h.reads(), 0, "ordinary deletion added a storage read");
+ });
+}
+
+test("deleting an older answer invalidates cloud legacy usage without requiring a local tokenizer", async () => {
+ const original = conversation();
+ const cloud = "external::provider::model";
+ original.messages[3].message.metadata.custom.contextUsage = { ...legacyUsage, modelId: cloud };
+ const h = harness(original);
+ await h.remove("a1");
+ const records = h.persisted()!;
+ const count = priced(records, "a2", cloud);
+ assert.ok(!count || count.estimated);
+ assert.equal(savedUsage.estimateBranch(orderBySelectedBranch(records, "a2"))?.estimated, true);
+});
+
+test("deleting a hidden sibling preserves the untouched branch's exact legacy usage", async () => {
+ const original = conversation();
+ original.messages.push({ parentId: "u1", message: message("alternative", "assistant") });
+ const h = harness(original);
+ await h.remove("alternative");
+ assert.equal(priced(h.live()), legacyUsage);
+ assert.deepEqual(priced(h.persisted()!), legacyUsage);
+ assert.equal(h.live().some((m) => m.metadata?.revision), false);
+});
+
+test("deleting a leaf leaves earlier completed usage valid", async () => {
+ const original = conversation();
+ original.messages[1].message.metadata.custom.contextUsage = legacyUsage;
+ const h = harness(original);
+ await h.remove("u2");
+ assert.equal(priced(h.live(), "a1"), legacyUsage);
+ assert.deepEqual(priced(h.persisted()!, "a1"), legacyUsage);
+});
+
+test("deleting a prompt with sibling replies invalidates every surviving descendant branch", async () => {
+ const original = conversation();
+ original.messages.push(
+ { parentId: "u1", message: message("a1b", "assistant") },
+ { parentId: "a1b", message: message("u2b", "user") },
+ { parentId: "u2b", message: message("a2b", "assistant", legacyUsage) },
+ );
+ const h = harness(original);
+ await h.remove("u1");
+ assert.deepEqual(h.live().map((m) => m.id).sort(), ["a2", "a2b", "u2", "u2b"]);
+ for (const head of ["a2", "a2b"]) {
+ const count = priced(h.persisted()!, head);
+ assert.ok(!count || count.estimated, `${head} retained a pre-delete exact count`);
+ }
+});
+
+test("a failed deletion changes neither live revisions nor saved usage", async () => {
+ const original = conversation();
+ const before = structuredClone(original);
+ const failure = new Error("sync failed");
+ const h = harness(original, failure);
+ await assert.rejects(h.remove("a1"), failure);
+ assert.equal(h.imported(), undefined);
+ assert.equal(h.persisted(), undefined);
+ assert.deepEqual(original, before);
+ assert.equal(priced(h.live()), legacyUsage);
+});
+
+test("a missing deletion target does not change revisions or usage", async () => {
+ const original = conversation();
+ const before = structuredClone(original);
+ const h = harness(original);
+ await assert.rejects(h.remove("missing"));
+ assert.equal(h.imported(), undefined);
+ assert.equal(h.persisted(), undefined);
+ assert.deepEqual(original, before);
+});
diff --git a/studio/frontend/tests/delete-thread-message-cost.test.ts b/studio/frontend/tests/delete-thread-message-cost.test.ts
index 6872fe1bd95..d317a944170 100644
--- a/studio/frontend/tests/delete-thread-message-cost.test.ts
+++ b/studio/frontend/tests/delete-thread-message-cost.test.ts
@@ -12,6 +12,7 @@ import test from "node:test";
import { MessageRepository } from "@assistant-ui/core/internal";
import * as researchSync from "../src/features/chat/utils/research-message-sync.ts";
+import * as savedUsage from "../src/features/chat/utils/saved-context-usage.ts";
import { loadWithStubs } from "./helpers/module-stubs.ts";
type Exported = {
@@ -71,6 +72,7 @@ function harness(): Harness {
},
},
"./research-message-sync": researchSync,
+ "./saved-context-usage": savedUsage,
},
);
return { module, calls, synced, stored };
diff --git a/studio/frontend/tests/edit-message-keeps-metadata.test.ts b/studio/frontend/tests/edit-message-keeps-metadata.test.ts
index e9e395b6c6a..a26afda7024 100644
--- a/studio/frontend/tests/edit-message-keeps-metadata.test.ts
+++ b/studio/frontend/tests/edit-message-keeps-metadata.test.ts
@@ -5,7 +5,18 @@ import assert from "node:assert/strict";
import test from "node:test";
import { MessageRepository } from "@assistant-ui/core/internal";
+import type { ThreadMessage } from "@assistant-ui/react";
+import ts from "@typescript/typescript6";
+import type { MessageRecord } from "../src/features/chat/types.ts";
import * as researchSync from "../src/features/chat/utils/research-message-sync.ts";
+import * as savedUsage from "../src/features/chat/utils/saved-context-usage.ts";
+import {
+ branchContextUsage,
+ contextUsageMarkers,
+ revisionTotal,
+ threadBranchRecords,
+} from "../src/features/chat/utils/saved-context-usage.ts";
+import { readSrc } from "./helpers/kit.ts";
import { loadWithStubs } from "./helpers/module-stubs.ts";
type Exported = {
@@ -29,7 +40,7 @@ const RECORD_MODULE = loadWithStubs<{
threadId: string,
parentId: string | null,
message: unknown,
- ) => Record<string, unknown>;
+ ) => MessageRecord;
}>(
new URL(
"../src/features/chat/utils/delete-thread-message.ts",
@@ -46,6 +57,7 @@ const RECORD_MODULE = loadWithStubs<{
) => records,
},
"./research-message-sync": researchSync,
+ "./saved-context-usage": savedUsage,
},
);
@@ -56,7 +68,26 @@ const CUSTOM = {
contextTruncation: { dropped: 4, fits: true },
};
-function harness() {
+const provider = ts.createSourceFile(
+ "runtime-provider.tsx",
+ readSrc("features/chat/runtime-provider.tsx"),
+ ts.ScriptTarget.Latest,
+ true,
+ ts.ScriptKind.TSX,
+);
+const hydration = ["cloneContent", "cloneAttachments", "toThreadMessage"].map((name) => {
+ const declaration = provider.statements.find(
+ (node) => ts.isFunctionDeclaration(node) && node.name?.text === name,
+ );
+ assert.ok(declaration, `${name} was not found`);
+ return declaration.getText(provider);
+}).join("\n");
+const hydrateUser = new Function(ts.transpileModule(
+ `${hydration}\nreturn toThreadMessage;`,
+ { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None } },
+).outputText)() as (record: MessageRecord) => ThreadMessage;
+
+function harness(persist?: () => Promise<void>) {
const saved: Record<string, unknown>[] = [];
const module = loadWithStubs<Module>(
new URL(
@@ -67,16 +98,28 @@ function harness() {
"../api/chat-api": {
saveChatMessage: async (record: Record<string, unknown>) => {
saved.push(record);
+ await persist?.();
return record;
},
},
"./delete-thread-message": RECORD_MODULE,
"./research-message-sync": researchSync,
+ "./saved-context-usage": savedUsage,
},
);
return { module, saved };
}
+function deferred() {
+ let resolve!: () => void;
+ let reject!: (reason: unknown) => void;
+ const promise = new Promise<void>((accept, refuse) => {
+ resolve = accept;
+ reject = refuse;
+ });
+ return { promise, resolve, reject };
+}
+
function thread(content: unknown[]): Exported {
return {
headId: "a0",
@@ -121,10 +164,109 @@ async function save(
return h.saved[0];
}
+function withoutRevision(metadata: unknown): Record<string, unknown> {
+ const { revision, ...kept } = metadata as Record<string, unknown>;
+ assert.equal(revision, 1);
+ return kept;
+}
+
test("an edited reply keeps the metadata the turn was stored with", async () => {
const record = await save("an edited reply");
- assert.deepEqual(record.metadata, CUSTOM);
+ assert.deepEqual(withoutRevision(record.metadata), CUSTOM);
+});
+
+test("saving a reply unchanged does not mark it revised, however the parts were stored", async () => {
+ for (const content of [
+ [{ type: "text", text: "the original reply" }],
+ [{ type: "reasoning", text: "\nlet me think\n" }, { type: "text", text: "the reply" }],
+ [{ type: "text", text: "one" }, { type: "text", text: "two" }],
+ [{ type: "text", text: "the reply", status: { type: "complete" } }],
+ ]) {
+ const h = harness();
+ const exported = thread(content);
+ await h.module.updateThreadMessage({
+ thread: { export: () => exported, import: () => {} },
+ messageId: "a0",
+ remoteId: "remote-1",
+ newText: h.module.extractTaggedText(content),
+ isIncognito: false,
+ });
+ assert.equal("revision" in ((h.saved[0].metadata ?? {}) as Record<string, unknown>), false);
+ }
+});
+
+for (const modelId of ["unsloth/gguf-model", "external::openai::gpt-x"]) {
+ for (const editedId of ["u0", "a0"]) {
+ test(`an edit after reloading a revised user invalidates saved context: ${modelId}, ${editedId}`, async () => {
+ const h = harness();
+ const user: MessageRecord = {
+ id: "u0",
+ threadId: "remote-1",
+ role: "user",
+ content: [{ type: "text", text: "edited prompt" }],
+ createdAt: 1000,
+ metadata: { revision: 1, createdAtEstimated: true },
+ };
+ const assistant: MessageRecord = {
+ id: "a0",
+ threadId: "remote-1",
+ parentId: "u0",
+ role: "assistant",
+ content: [{ type: "text", text: "the original reply" }],
+ createdAt: 2000,
+ };
+ const usage = {
+ promptTokens: 90,
+ completionTokens: 10,
+ totalTokens: 100,
+ cachedTokens: 0,
+ modelId,
+ ...contextUsageMarkers([user, assistant]),
+ };
+ let exported = thread(assistant.content as unknown[]);
+ exported.messages[0].message = hydrateUser(user) as unknown as Record<string, unknown>;
+ exported.messages[1].message.metadata = { custom: { contextUsage: usage } };
+
+ await h.module.updateThreadMessage({
+ thread: { export: () => exported, import: (data) => { exported = data; } },
+ messageId: editedId,
+ remoteId: "remote-1",
+ newText: "a much longer edited message ".repeat(10),
+ isIncognito: false,
+ });
+
+ const messages = exported.messages.map(({ message }) => message) as unknown as ThreadMessage[];
+ const window = modelId.startsWith("external::") ? null : 4096;
+ const shown = branchContextUsage(threadBranchRecords(messages), modelId, window, window);
+ assert.ok(shown);
+ assert.equal(shown.estimated, true, "the pre-edit count was accepted as exact");
+ assert.equal(revisionTotal(messages), 2);
+ assert.equal(h.saved.length, 1);
+ });
+ }
+}
+
+test("reloading and saving a user unchanged preserves its revision and timestamp flag", async () => {
+ const h = harness();
+ const user: MessageRecord = {
+ id: "u0",
+ threadId: "remote-1",
+ role: "user",
+ content: [{ type: "text", text: "edited prompt" }],
+ createdAt: 1000,
+ metadata: { revision: 3, createdAtEstimated: true },
+ };
+ const exported = thread([]);
+ exported.messages[0].message = hydrateUser(user) as unknown as Record<string, unknown>;
+ await h.module.updateThreadMessage({
+ thread: { export: () => exported, import: () => {} },
+ messageId: "u0",
+ remoteId: "remote-1",
+ newText: h.module.extractTaggedText(user.content),
+ isIncognito: false,
+ });
+ assert.deepEqual(h.saved[0].metadata, user.metadata);
});
test("editing clears stale Gemini continuation replay and keeps other metadata", async () => {
@@ -137,7 +279,7 @@ test("editing clears stale Gemini continuation replay and keeps other metadata",
},
});
- assert.deepEqual(record.metadata, CUSTOM);
+ assert.deepEqual(withoutRevision(record.metadata), CUSTOM);
assert.equal(
"geminiContinuationReplay" in (record.metadata as Record<string, unknown>),
false,
@@ -155,7 +297,7 @@ test("the edit still rewrites the content and its identity", async () => {
assert.deepEqual(record.content, [{ type: "text", text: "an edited reply" }]);
});
-test("a reply with no metadata of its own writes none", async () => {
+test("a reply with no metadata of its own writes only its revision", async () => {
const h = harness();
const exported = thread([{ type: "text", text: "hi" }]);
const target = exported.messages[1].message;
@@ -169,7 +311,7 @@ test("a reply with no metadata of its own writes none", async () => {
isIncognito: false,
});
- assert.equal("metadata" in h.saved[0], false);
+ assert.deepEqual(withoutRevision(h.saved[0].metadata), {});
});
test("an incognito edit is never written at all", async () => {
@@ -187,6 +329,149 @@ test("an incognito edit is never written at all", async () => {
assert.equal(h.saved.length, 0);
});
+test("an edit updates content and token-count revision before the existing save settles", async () => {
+ let accept!: () => void;
+ const pending = new Promise<void>((resolve) => { accept = resolve; });
+ const h = harness(() => pending);
+ let exported = thread([{ type: "text", text: "original" }]);
+ const published: Exported[] = [];
+ const save = h.module.updateThreadMessage({
+ thread: {
+ export: () => exported,
+ import: (next) => { exported = next; published.push(next); },
+ },
+ messageId: "a0",
+ remoteId: "remote-1",
+ newText: "edited",
+ isIncognito: false,
+ });
+
+ assert.equal(h.saved.length, 1);
+ assert.equal(published.length, 1);
+ assert.deepEqual(exported.messages[1].message.content, [{ type: "text", text: "edited" }]);
+ assert.equal(revisionTotal(exported.messages.map(({ message }) => message) as unknown as ThreadMessage[]), 1);
+ accept();
+ await save;
+ assert.equal(published.length, 1);
+ const messages = exported.messages.map(({ message }) => message) as unknown as ThreadMessage[];
+ assert.equal(revisionTotal(messages), 1);
+ assert.deepEqual(messages[1].content, [{ type: "text", text: "edited" }]);
+});
+
+test("upstream edit rollback restores content, revision and the saved token count", async () => {
+ const error = new Error("server-managed generation messages cannot be edited");
+ const write = deferred();
+ const h = harness(() => write.promise);
+ let exported = thread([{ type: "text", text: "original" }]);
+ const saved = { modelId: "local-model", promptTokens: 42, completionTokens: 8, totalTokens: 50, cachedTokens: 0 };
+ exported.messages[1].message.metadata = { custom: { contextUsage: saved } };
+ const shownUsage = () => branchContextUsage(
+ threadBranchRecords(exported.messages.map(({ message }) => message) as unknown as ThreadMessage[]),
+ "local-model", 4096, 4096,
+ );
+
+ const saving = h.module.updateThreadMessage({
+ thread: {
+ export: () => exported,
+ import: (next) => { exported = next; },
+ },
+ messageId: "a0",
+ remoteId: "remote-1",
+ newText: "edited",
+ isIncognito: false,
+ });
+ assert.notEqual(shownUsage(), saved);
+ const rejected = assert.rejects(saving, error);
+ write.reject(error);
+ await rejected;
+
+ assert.equal(shownUsage(), saved);
+ const messages = exported.messages.map(({ message }) => message) as unknown as ThreadMessage[];
+ assert.equal(revisionTotal(messages), 0);
+ assert.deepEqual(messages[1].content, [{ type: "text", text: "original" }]);
+});
+
+test("token-count revisions do not serialize or delay upstream edit requests", async () => {
+ const firstWrite = deferred();
+ const secondWrite = deferred();
+ const writes = [firstWrite, secondWrite];
+ let writeIndex = 0;
+ const h = harness(() => writes[writeIndex++].promise);
+ let exported = thread([{ type: "text", text: "original" }]);
+ const edit = (newText: string) => h.module.updateThreadMessage({
+ thread: {
+ export: () => exported,
+ import: (next) => { exported = next; },
+ },
+ messageId: "a0",
+ remoteId: "remote-1",
+ newText,
+ isIncognito: false,
+ });
+ const first = edit("edit A");
+ const second = edit("edit B");
+ assert.equal(h.saved.length, 2);
+ assert.deepEqual(exported.messages[1].message.content, [{ type: "text", text: "edit B" }]);
+
+ secondWrite.resolve();
+ await second;
+ firstWrite.resolve();
+ await Promise.all([first, second]);
+
+ assert.deepEqual(h.saved.map((record) => record.content), [
+ [{ type: "text", text: "edit A" }],
+ [{ type: "text", text: "edit B" }],
+ ]);
+ assert.deepEqual(h.saved.map((record) => (record.metadata as Record<string, unknown>).revision), [1, 2]);
+ assert.deepEqual(exported.messages[1].message.content, [{ type: "text", text: "edit B" }]);
+ assert.equal(revisionTotal(exported.messages.map(({ message }) => message) as unknown as ThreadMessage[]), 2);
+});
+
+test("submitting the same edit twice does not add a second revision", async () => {
+ const firstWrite = deferred();
+ const h = harness(() => firstWrite.promise);
+ let exported = thread([{ type: "text", text: "original" }]);
+ const edit = () => h.module.updateThreadMessage({
+ thread: {
+ export: () => exported,
+ import: (next) => { exported = next; },
+ },
+ messageId: "a0",
+ remoteId: "remote-1",
+ newText: "edit A",
+ isIncognito: false,
+ });
+ const first = edit();
+ const second = edit();
+ firstWrite.resolve();
+ await Promise.all([first, second]);
+
+ assert.deepEqual(h.saved.map((record) => (record.metadata as Record<string, unknown>).revision), [1, 1]);
+});
+
+test("repeating an edit with THINK whitespace keeps the accepted revision", async () => {
+ const firstWrite = deferred();
+ const h = harness(() => firstWrite.promise);
+ let exported = thread([{ type: "text", text: "original" }]);
+ const edit = () => h.module.updateThreadMessage({
+ thread: { export: () => exported, import: (next) => { exported = next; } },
+ messageId: "a0", remoteId: "remote-1", isIncognito: false,
+ newText: "<THINK>\n reasoning \n</THINK>\n\nreply",
+ });
+ const first = edit();
+ const second = edit();
+ firstWrite.resolve();
+ await Promise.all([first, second]);
+ assert.deepEqual(h.saved.map((record) => (record.metadata as Record<string, unknown>).revision), [1, 1]);
+});
+
+test("reordering distinct calls to the same tool increments the revision", async () => {
+ const tool = (toolCallId: string) => ({ type: "tool-call", toolCallId, toolName: "search", args: {}, result: toolCallId });
+ const record = await save("<TOOL 2: search>\n\n<TOOL 1: search>", [tool("a"), tool("b")]);
+ assert.equal((record.metadata as Record<string, unknown>).revision, 1);
+ assert.deepEqual(record.content, [tool("b"), tool("a")]);
+});
+
test("the turn keeps its timestamp when the export carries epoch millis", async () => {
// Re-dating the turn to now would reorder the thread.
const h = harness();
@@ -234,7 +519,7 @@ async function saveOwnedReply(custom: Record<string, unknown>) {
test("editing a generated reply drops the run's claim on the turn", async () => {
const record = await saveOwnedReply({ ...CUSTOM, ...GENERATION_OWNERSHIP });
- assert.deepEqual(record.metadata, CUSTOM);
+ assert.deepEqual(withoutRevision(record.metadata), CUSTOM);
assert.deepEqual(
Object.keys(GENERATION_OWNERSHIP).filter(
(key) => key in (record.metadata as Record<string, unknown>),
@@ -255,11 +540,109 @@ test("the strip covers the backend's whole server-managed key set", async () =>
researchRun: { id: "research-1" },
});
- assert.deepEqual(record.metadata, CUSTOM);
+ assert.deepEqual(withoutRevision(record.metadata), CUSTOM);
});
-test("a reply whose only metadata was the run's claim writes none", async () => {
+test("a reply whose only metadata was the run's claim writes only its revision", async () => {
const record = await saveOwnedReply({ ...GENERATION_OWNERSHIP });
- assert.equal("metadata" in record, false);
+ assert.deepEqual(withoutRevision(record.metadata), {});
+});
+
+const adapters = provider.statements.find(
+ (node): node is ts.FunctionDeclaration =>
+ ts.isFunctionDeclaration(node) && node.name?.text === "useStudioRuntimeAdapters",
+);
+assert.ok(adapters?.body);
+const deletionEffect = adapters.body.statements.find((node) => {
+ let subscribes = false;
+ const visit = (child: ts.Node) => {
+ if (ts.isCallExpression(child) && child.expression.getText(provider) === "onChatAttachmentDeleted") {
+ subscribes = true;
+ }
+ ts.forEachChild(child, visit);
+ };
+ visit(node);
+ return subscribes;
+});
+assert.ok(deletionEffect);
+const deletionEffectCode = ts.transpileModule(deletionEffect.getText(provider), {
+ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None },
+}).outputText;
+
+function attachmentDeletionHarness(exported: Exported, hash: () => Promise<string>) {
+ let listener!: (event: { messageId: string; attachmentId: string }) => Promise<void>;
+ let imports = 0;
+ new Function("useEffect", "aui", "onChatAttachmentDeleted",
+ "chatContentPartAttachmentSignature", "chatContentPartAttachmentIdFromSignature",
+ ...Object.keys(savedUsage), deletionEffectCode)(
+ (effect: () => unknown) => effect(),
+ { thread: () => ({
+ export: () => exported,
+ import: (next: Exported) => { Object.assign(exported, next); imports += 1; },
+ }) },
+ (callback: typeof listener) => { listener = callback; return () => {}; },
+ (part: { type: string }) => part.type === "image" ? "image-signature" : null,
+ hash,
+ ...Object.values(savedUsage),
+ );
+ return {
+ remove: (attachmentId: string) => listener({ messageId: "u0", attachmentId }),
+ imports: () => imports,
+ };
+}
+
+for (const kind of ["attachment", "content-hash", "legacy-content"] as const) {
+ test(`deleting an upload (${kind}) invalidates saved usage in the loaded branch and after hydration`, async () => {
+ const exported = thread([{ type: "text", text: "reply" }]);
+ const user = exported.messages[0].message;
+ user.metadata = { custom: { revision: 3, keep: "metadata" } };
+ const attachmentId = kind === "attachment" ? "file-1"
+ : kind === "content-hash" ? "content-part-sha256-test" : "content-part-1";
+ if (kind === "attachment") {
+ user.attachments = [{ id: attachmentId, content: [{ type: "text", text: "large file" }] }];
+ } else {
+ (user.content as unknown[]).push({ type: "image", image: "data:image/png;base64,AA==" });
+ }
+ const records = () => exported.messages.map((item) =>
+ RECORD_MODULE.exportedItemToRecord("thread-1", item.parentId, item.message),
+ );
+ const usage = {
+ promptTokens: 4000, completionTokens: 10, totalTokens: 4010, cachedTokens: 0,
+ modelId: "model", ...contextUsageMarkers(records()),
+ };
+ exported.messages[1].message.metadata = { custom: { contextUsage: usage } };
+ assert.equal(branchContextUsage(records(), "model", 8192, 8192), usage);
+ const h = attachmentDeletionHarness(exported, async () => attachmentId);
+ await h.remove(attachmentId);
+ assert.equal(h.imports(), 1);
+ const saved = records();
+ assert.deepEqual(saved[0].metadata, { revision: 4, keep: "metadata" });
+ assert.equal(branchContextUsage(saved, "model", 8192, 8192)?.estimated, true);
+ const reloaded = [hydrateUser(saved[0]), exported.messages[1].message as unknown as ThreadMessage];
+ assert.equal(revisionTotal(reloaded), 4);
+ assert.equal(branchContextUsage(threadBranchRecords(reloaded), "model", 8192, 8192)?.estimated, true);
+ await h.remove(attachmentId);
+ assert.equal(h.imports(), 1);
+ assert.equal(records()[0].metadata?.revision, 4);
+ });
+}
+
+test("an attachment removed during hashing does not revise the message again", async () => {
+ const exported = thread([{ type: "text", text: "reply" }]);
+ const attachmentId = "content-part-sha256-document";
+ const user = exported.messages[0].message;
+ user.attachments = [{ id: attachmentId }];
+ user.content = [{ type: "image", image: "data:image/png;base64,AA==" }];
+ user.metadata = { custom: { revision: 3 } };
+ const h = attachmentDeletionHarness(exported, async () => {
+ exported.messages[0].message = {
+ ...user, attachments: [], metadata: { custom: { revision: 4, keep: "new metadata" } },
+ };
+ return "content-part-sha256-image";
+ });
+ await h.remove(attachmentId);
+ assert.deepEqual(exported.messages[0].message.metadata, {
+ custom: { revision: 4, keep: "new metadata" },
+ });
});
diff --git a/studio/frontend/tests/estimate-chat-tokens.test.ts b/studio/frontend/tests/estimate-chat-tokens.test.ts
index 2de2a7d3b4c..521cf4c3b0a 100644
--- a/studio/frontend/tests/estimate-chat-tokens.test.ts
+++ b/studio/frontend/tests/estimate-chat-tokens.test.ts
@@ -86,3 +86,37 @@ test("an estimate against a window gives a ratio but no limit advice", () => {
const exact = deriveContextUsageBar({ used: 30000, total: 32768 });
assert.equal(exact?.advice, "stops-at-limit");
});
+
+test("a tool call Studio ran counts its arguments and the result the model read", () => {
+ const reply = (part: Record<string, unknown>): MessageRecord => ({
+ id: "2",
+ threadId: "t",
+ role: "assistant",
+ content: [part, { type: "text", text: "a".repeat(40) }] as unknown as MessageRecord["content"],
+ createdAt: 2,
+ });
+ const call = { type: "tool-call", toolCallId: "c1", toolName: "python", argsText: "b".repeat(40) };
+ const local = { source: "local", round_id: 1 };
+ assert.equal(estimateMessagesTokenCount([reply({ ...call, provenance: local, result: "c".repeat(320) })]), 100);
+ assert.equal(
+ estimateMessagesTokenCount([reply({ ...call, provenance: local, result: { text: "c".repeat(320), images: [{ data: "d".repeat(4000) }] } })]),
+ 100,
+ );
+ assert.equal(estimateMessagesTokenCount([reply({ ...call, provenance: local, result: { rows: ["d".repeat(4000)] } })]), 20);
+ assert.equal(estimateMessagesTokenCount([reply({ ...call, result: "c".repeat(320) })]), 10);
+});
+
+test("a retrieval result's source map feeds the citation card, not the model, so it is not counted", () => {
+ const map = JSON.stringify([{ chunkId: "c1", filename: "a.pdf", text: "c".repeat(400) }]);
+ const reply = (result: string): MessageRecord => ({
+ id: "2",
+ threadId: "t",
+ role: "assistant",
+ content: [
+ { type: "tool-call", toolCallId: "c1", toolName: "search_knowledge_base", args: {}, argsText: "", result, provenance: { source: "local", round_id: 1 } },
+ ] as unknown as MessageRecord["content"],
+ createdAt: 2,
+ });
+ assert.equal(estimateMessagesTokenCount([reply(`${"c".repeat(400)}\n__RAG_SOURCES__:${map}`)]), 100);
+ assert.equal(estimateMessagesTokenCount([reply("c".repeat(400))]), 100);
+});
diff --git a/studio/frontend/tests/generation-tool-recovery.test.ts b/studio/frontend/tests/generation-tool-recovery.test.ts
index 27c488b2002..7eeeb61cf4f 100644
--- a/studio/frontend/tests/generation-tool-recovery.test.ts
+++ b/studio/frontend/tests/generation-tool-recovery.test.ts
@@ -27,6 +27,12 @@ const {
providerCompactionConnectionKey,
providerCompactionReplayToolCallCount,
} = await import("../src/features/chat/utils/provider-compaction.ts");
+const { orderBySelectedBranch } = await import(
+ "../src/features/chat/utils/message-order.ts"
+);
+const { contextUsageMarkers } = await import(
+ "../src/features/chat/utils/saved-context-usage.ts"
+);
const start = (id = "call_0") => ({
type: "tool_start",
@@ -515,6 +521,8 @@ async function recoverRun(
...recovery,
...parser,
createGenerationToolRecovery,
+ contextUsageMarkers,
+ orderBySelectedBranch,
providerCompactionConnectionKey,
RUN_CHECKPOINT_INTERVAL_MS,
generationRecoveries,
@@ -550,20 +558,23 @@ async function recoverRun(
ChatGenerationStalledError,
});
vm.runInContext(executable, context);
+ const stored = {
+ id: "msg",
+ role: "assistant",
+ content,
+ createdAt: 1,
+ metadata: {
+ generationRunId: "run",
+ generationSeq: options.cursor ?? 0,
+ generationStatus: "running",
+ generationSettled: false,
+ ...options.metadata,
+ },
+ };
context.scheduleGenerationRecovery(
"thread",
- {
- id: "msg",
- content,
- createdAt: 1,
- metadata: {
- generationRunId: "run",
- generationSeq: options.cursor ?? 0,
- generationStatus: "running",
- generationSettled: false,
- ...options.metadata,
- },
- },
+ stored,
+ [stored],
{
threadListItem: () => ({ getState: () => ({ remoteId: "thread" }) }),
thread: () => ({
diff --git a/studio/frontend/tests/studio-tool-history.test.ts b/studio/frontend/tests/studio-tool-history.test.ts
index 0e4f65da689..7acd454a784 100644
--- a/studio/frontend/tests/studio-tool-history.test.ts
+++ b/studio/frontend/tests/studio-tool-history.test.ts
@@ -132,14 +132,3 @@ test("the live request and token-count paths mark ownership after replay", () =>
-1,
);
});
-
-test("the context recount forwards the tool-history request fields", () => {
- const source = readSrc("features/chat/utils/refresh-context-usage.ts");
- const historyBuild = source.indexOf(
- "const countHistory = await buildLocalTokenCountHistory(",
- );
- const countRequest = source.indexOf("await countChatInputTokens({", historyBuild);
- const historySpread = source.indexOf("...countHistory", countRequest);
- assert.ok(historyBuild >= 0 && countRequest > historyBuild);
- assert.ok(historySpread > countRequest);
-});
diff --git a/tests/studio/test_chat_title_generation.py b/tests/studio/test_chat_title_generation.py
index 5eda36698e2..3e8c68ce2c9 100644
--- a/tests/studio/test_chat_title_generation.py
+++ b/tests/studio/test_chat_title_generation.py
@@ -103,7 +103,7 @@ def test_auto_title_disabled_uses_deterministic_user_text_fallback():
def test_reply_stamps_the_checkpoint_its_request_captured():
source = " ".join(CHAT_ADAPTER_TS.read_text(encoding = "utf-8").split())
- assert source.count("modelId: params.checkpoint,") == 2
+ assert source.count("modelId: params.checkpoint,") == 3
assert "modelId: useChatRuntimeStore.getState().params" not in source
assert "const externalRouting = resolveExternalRouting(params.checkpoint);" in source
assert (
diff --git a/tests/studio/test_new_chat_context_recount.py b/tests/studio/test_new_chat_context_recount.py
index c66a9fa4ab4..c1c716e5141 100644
--- a/tests/studio/test_new_chat_context_recount.py
+++ b/tests/studio/test_new_chat_context_recount.py
@@ -39,10 +39,21 @@ BOUND_NAMES = {
"isLoading",
"mainThreadId",
"runActive",
+ "ownRunActive",
+ "queuedPromptPending",
"modelLoading",
"newThreadSwitchStateRef",
"nonce",
"paused",
+ "tipId",
+ "tipRunning",
+ "branchLength",
+ "branchRevision",
+ "reasoningSettings",
+ "promptSettings",
+ "savedUsage",
+ "threadSettingsPending",
+ "threadLoading",
}
@@ -135,7 +146,7 @@ def _history_usage_restore() -> str:
"""The history loader's saved-usage restore and its recount call, verbatim."""
return slice_between(
read(PROVIDER),
- " const localLimit = store.loadedIsGguf ? store.loadedContextLength : null;",
+ " const checkpoint = store.params.checkpoint;",
" const hasParentIds = msgs.some((m) => m.parentId != null);",
)
@@ -147,6 +158,9 @@ HARNESS_PRELUDE = """
export const world: any = {
storedMessages: {} as Record<string, any[]>,
countedMessages: [] as any[][],
+ countedPayloads: [] as any[],
+ countExtras: {},
+ countError: null,
countedModel: undefined as string | undefined,
switchedToNewThread: 0,
clearedAttachments: 0,
@@ -157,6 +171,7 @@ export const world: any = {
countedTokensOverride: undefined as { value: any } | undefined,
// Holds the count in flight, so a test can move the world while it is awaiting.
countGate: null as Promise<void> | null,
+ promptQueueByThreadId: {} as Record<string, { paused: boolean }>,
};
const state: any = {
@@ -168,6 +183,7 @@ const state: any = {
loadedContextLength: null,
modelLoading: false,
runningByThreadId: {},
+ queuedSettingsEpoch: 0,
// The subset decoding on the local llama-server: the recount must not share it with a decode.
localRunByThreadId: {},
// What the recount reads to tell an output-only audio GGUF from a chat one.
@@ -263,6 +279,8 @@ export const useChatRuntimeStore: any = {
Object.assign(state, typeof patch === "function" ? patch(state) : patch),
};
+const getMcpServerMutationEpoch = () => 0;
+
export function seed(patch: any): void {
Object.assign(state, patch);
}
@@ -327,13 +345,13 @@ async function buildLocalTokenCountHistory(messages: any): Promise<{ messages: a
}
async function buildLocalTokenCountExtras(): Promise<Record<string, unknown>> {
- return {};
+ return world.countExtras;
}
// The reasoning kwargs are pinned by test_token_count_prompt_parity.py; here the count
// only has to reach the server with the right branch of messages.
function buildLocalTokenCountReasoning(): Record<string, unknown> {
- return {};
+ return state.supportsReasoning ? { enable_thinking: state.reasoningEnabled } : {};
}
// 12 tokens for the bare template, 25 more per message actually sent. The endpoint also
@@ -341,7 +359,9 @@ function buildLocalTokenCountReasoning(): Record<string, unknown> {
// omits it, as an older backend would.
async function countChatInputTokens(payload: any): Promise<any> {
world.countedMessages.push(payload.messages);
+ world.countedPayloads.push(payload);
if (world.countGate) await world.countGate;
+ if (world.countError) throw world.countError;
return {
input_tokens:
world.countedTokensOverride === undefined
@@ -369,6 +389,7 @@ const auiFixture: any = {
// The mounted main thread's visible branch: a test's live messages, else its stored records.
thread: () => ({
getState: () => ({
+ isLoading: world.threadLoading ?? false,
messages:
world.liveMessages?.[state.activeThreadId] ??
(world.storedMessages[state.activeThreadId] ?? []).map(storedMessageToRunMessage),
@@ -408,6 +429,18 @@ function replayEffects(effects: any[], scope: any, memo: any[]): void {
}
const recountDeps: any[] = [];
+const pricedTipRef: any = { current: null };
+const pricedReasoningRef: any = { current: null };
+const promptChangedAtRef: any = { current: new Map() };
+
+export function registerActiveBranchReader(): void {
+ const enabled = true;
+ const aui = auiFixture;
+ const effects: any[] = [
+__ACTIVE_BRANCH_EFFECTS__
+ ];
+ for (const effect of effects) effect.run();
+}
export function renderThreadContextUsageRecount(props: any = {}): void {
const enabled = props.enabled ?? true;
@@ -418,6 +451,22 @@ export function renderThreadContextUsageRecount(props: any = {}): void {
const loadedContextLength = state.loadedContextLength;
const modelLoading = state.modelLoading;
const runActive = Object.values(state.runningByThreadId ?? {}).some(Boolean);
+ const ownRunActive = Boolean(
+ state.activeThreadId && state.runningByThreadId?.[state.activeThreadId],
+ );
+ const queueEntry = activeThreadId ? world.promptQueueByThreadId[activeThreadId] : undefined;
+ const queuedPromptPending = Boolean(queueEntry && !queueEntry.paused);
+ const messages = aui.thread().getState().messages;
+ const tip = messages.at(-1);
+ const tipId = tip?.id;
+ const tipRunning = tip?.role === "assistant" && tip.status?.type === "running";
+ const branchLength = messages.length;
+ const branchRevision = revisionTotal(messages);
+ const reasoningSettings = currentContextUsageReasoningKey(state);
+ const promptSettings = currentContextUsageSettingsKey(state, "");
+ const savedUsage = tip?.metadata?.custom?.contextUsage;
+ const threadSettingsPending = Boolean(state.threadScopedSettingsPending);
+ const threadLoading = aui.thread().getState().isLoading;
const scope: any = {
activeThreadId,
aui,
@@ -426,6 +475,17 @@ export function renderThreadContextUsageRecount(props: any = {}): void {
loadedContextLength,
modelLoading,
runActive,
+ ownRunActive,
+ queuedPromptPending,
+ tipId,
+ tipRunning,
+ branchLength,
+ branchRevision,
+ reasoningSettings,
+ promptSettings,
+ savedUsage,
+ threadSettingsPending,
+ threadLoading,
};
const effects: any[] = [
__RECOUNT_EFFECTS__
@@ -586,14 +646,24 @@ HARNESS_HISTORY = """
// the repository, so the live branch is empty and only the stored path can answer.
export async function hydrateThreadUsage(props: any): Promise<void> {
const remoteId: string = props.remoteId;
- const savedUsage = props.savedUsage;
// The loader is created per pane; a compare pane carries a pairId and never owns the bar.
const modelType: string = props.modelType ?? "base";
const pairId = props.pairId ?? undefined;
// The thread's stored messages, which the loader reads into `msgs` and sorts above the sliced
// block (createdAt, then role, then id), so `msgs.at(-1)` below is the newest stored row.
const roleOrder: Record<string, number> = { system: 0, user: 1, assistant: 2 };
- const msgs: any[] = [...(props.messages ?? [])].sort((a: any, b: any) => {
+ const stored: any[] = props.messages ?? [];
+ const savedReply = props.savedUsage
+ ? [{
+ id: "a-saved",
+ parentId: stored.at(-1)?.id ?? null,
+ role: "assistant",
+ createdAt: (stored.at(-1)?.createdAt ?? 0) + 1,
+ content: [{ type: "text", text: "" }],
+ metadata: { contextUsage: props.savedUsage },
+ }]
+ : [];
+ const msgs: any[] = [...stored, ...savedReply].sort((a: any, b: any) => {
if (a.createdAt !== b.createdAt) return a.createdAt - b.createdAt;
const aOrder = roleOrder[a.role] ?? 99;
const bOrder = roleOrder[b.role] ?? 99;
@@ -616,14 +686,24 @@ __RESTORE__
def _harness_source() -> str:
prelude = HARNESS_PRELUDE.replace("__STORE_REDUCERS__", _store_reducers())
- render = HARNESS_RENDER.replace("__EFFECTS__", _rendered_effects(_new_chat_effects())).replace(
- "__RECOUNT_EFFECTS__", _rendered_effects(_thread_recount_effects())
+ render = (
+ HARNESS_RENDER.replace("__EFFECTS__", _rendered_effects(_new_chat_effects()))
+ .replace("__RECOUNT_EFFECTS__", _rendered_effects(_thread_recount_effects()))
+ .replace(
+ "__ACTIVE_BRANCH_EFFECTS__",
+ _rendered_effects(
+ _component_effects(
+ "function ActiveBranchRegistrar(", "function CompactionCountRegistrar("
+ )
+ ),
+ )
)
resident = HARNESS_RESIDENT.replace("__FAST_PATH__", _resident_fast_path())
status_poll = HARNESS_STATUS_POLL.replace("__STATUS_POLL_TAIL__", _status_poll_adoption_tail())
history = HARNESS_HISTORY.replace("__RESTORE__", _history_usage_restore())
return (
prelude
+ + slice_between(read(STORE), "type LocalMediaToolState =", "type MediaStatusSnapshot =")
+ _message_order_body()
+ _refresh_module_body()
+ render
@@ -642,6 +722,7 @@ LOADED_MODEL = """
seed({
params: { checkpoint: "unsloth/gguf-model", systemPrompt: "", systemVariables: "" },
loadedContextLength: 8192,
+ loadedIsGguf: true,
modelLoading: false,
});
"""
@@ -1520,7 +1601,7 @@ def test_the_harness_binds_every_loader_local_the_history_restore_reads() -> Non
above = provider[loader:start]
declared_above = _declared_names(above)
assert (
- {"msgs", "savedUsage", "store"} <= declared_above
+ {"msgs", "branch", "store"} <= declared_above
), "could not read the loader's locals above the restore; this guard would check nothing"
# Code only: the block's comments name words that are locals elsewhere.
code = re.sub(r"//[^\n]*", "", restore)
@@ -2238,7 +2319,7 @@ def test_a_media_chat_keeps_a_value_after_a_local_model_switch(parts, saved, ggu
"unsloth/other-gguf",
', { id: "m3", role: "user", createdAt: 3, content: [{ type: "image", image: "data:image/png;base64,iVBORw0KGgo=" }], metadata: {} }',
'{ promptTokens: 700, completionTokens: 9, totalTokens: 709, cachedTokens: 0, modelId: "unsloth/other-gguf" }',
- {"total": 9, "estimated": True},
+ {"total": 709, "estimated": True},
),
],
ids = ["cloud_count_past_a_resident_gguf_window", "unanswered_media_turn_after_the_count"],
@@ -2491,8 +2572,1121 @@ def test_the_count_is_retried_once_the_run_finishes():
"\nfunction CancelRegistrar(",
)
assert "runningByThreadId" in recount, "the effect must observe decoding"
- deps = re.search(r"\}, \[([^\]]*)\]\);", recount, re.S)
- assert deps and "runActive" in deps.group(1), (
+ deps = re.findall(r"\}, \[([^\]]*)\]\);", recount, re.S)
+ assert any("runActive" in effect for effect in deps), (
"runActive must be a DEPENDENCY, not just a guard: nothing else in this array "
"changes when a run ends, so without it a skipped count would never be retried"
)
+
+
+_U1_ROW = (
+ '{ id: "u1", parentId: null, role: "user", createdAt: 1, '
+ 'content: [{ type: "text", text: "hello there" }], metadata: {} }'
+)
+
+_TOOL_CALL_PARTS = {
+ "local": (
+ '{ type: "tool-call", toolCallId: "c1", toolName: "web_search", args: {}, '
+ 'argsText: "{}", result: "found", provenance: { source: "local", round_id: 1 } }, '
+ ),
+ "hosted": (
+ '{ type: "tool-call", toolCallId: "c1", toolName: "web_search", args: {}, '
+ 'argsText: "{}", result: "found" }, '
+ ),
+ "retrieval": (
+ '{ type: "tool-call", toolCallId: "rag_auto_0123456789ab", '
+ 'toolName: "search_knowledge_base", args: {}, argsText: "{}", result: "found", '
+ 'provenance: { source: "local" } }, '
+ ),
+}
+
+_CLOUD_SUM = (
+ "{ promptTokens: 322000, completionTokens: 760, totalTokens: 322760, cachedTokens: 0, "
+ 'modelId: "external::openai::gpt-x" }'
+)
+
+_CLOUD_SUM_WITH_CONTEXT = (
+ "{ promptTokens: 322000, completionTokens: 760, totalTokens: 322760, contextTokens: 52000, "
+ 'cachedTokens: 0, modelId: "external::openai::gpt-x" }'
+)
+
+_SAVED_300_LOCAL = (
+ "{ promptTokens: 250, completionTokens: 50, totalTokens: 300, cachedTokens: 0, "
+ 'modelId: "unsloth/gguf-model" }'
+)
+
+_SAVED_300_CLOUD = (
+ "{ promptTokens: 250, completionTokens: 50, totalTokens: 300, cachedTokens: 0, "
+ 'modelId: "external::openai::gpt-x" }'
+)
+
+
+def _assistant_row(
+ id_,
+ created_at,
+ text,
+ saved,
+ parent = "u1",
+ tool_call = None,
+):
+ parts = _TOOL_CALL_PARTS[tool_call] if tool_call else ""
+ return (
+ f'{{ id: "{id_}", parentId: "{parent}", role: "assistant", createdAt: {created_at}, '
+ f'content: [{parts}{{ type: "text", text: "{text}" }}], '
+ f"metadata: {{ contextUsage: {saved} }} }}"
+ )
+
+
+def _script(template: str, **rows: str) -> str:
+ script = textwrap.dedent(template).replace("__LOADED_MODEL__", LOADED_MODEL)
+ for name, row in rows.items():
+ script = script.replace(f"__{name}__", row)
+ return script
+
+
+_BRANCH_SWITCH = """
+ // @ts-nocheck
+ import { renderThreadContextUsageRecount, seed, snapshot, useChatRuntimeStore, world } from "./harness.ts";
+ __LOADED_MODEL__
+ seed({ loadedIsGguf: true });
+ __PICK__
+ const u1 = __U1__;
+ world.storedMessages["thread-a"] = [u1, __A2__];
+ seed({ activeThreadId: "thread-a" });
+ useChatRuntimeStore.getState().setContextUsage(__FIRST__);
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const before = snapshot().contextUsage;
+ const countsBefore = world.countedMessages.length;
+ world.storedMessages["thread-a"] = [u1, __A3__];
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ console.log(JSON.stringify({
+ before,
+ after: snapshot().contextUsage,
+ counts: world.countedMessages.length - countsBefore,
+ }));
+"""
+
+
+ ("picked", "first_saved", "switched_saved", "expected"),
+ [
+ (
+ "",
+ _SAVED_300_LOCAL,
+ "{ promptTokens: 400, completionTokens: 100, totalTokens: 500, cachedTokens: 0, "
+ 'modelId: "unsloth/gguf-model" }',
+ {"counts": 0, "total": 500, "estimated": None},
+ ),
+ ("", _SAVED_300_LOCAL, "undefined", {"counts": 1, "total": 62, "estimated": None}),
+ (
+ "external::openai::gpt-x",
+ _SAVED_300_CLOUD,
+ "{ promptTokens: 400, completionTokens: 100, totalTokens: 500, cachedTokens: 0, "
+ 'modelId: "external::openai::gpt-x" }',
+ {"counts": 0, "total": 500, "estimated": None},
+ ),
+ (
+ "external::openai::gpt-x",
+ _SAVED_300_CLOUD,
+ "undefined",
+ {"counts": 0, "total": 6, "estimated": True},
+ ),
+ ],
+ ids = [
+ "local_tip_with_its_own_count",
+ "local_tip_is_recounted",
+ "cloud_tip_with_its_own_count",
+ "cloud_tip_without_a_count_is_estimated_as_reopening_shows",
+ ],
+)
+def test_a_branch_switch_reprices_the_meter_from_the_new_tip(
+ picked, first_saved, switched_saved, expected
+):
+ pick = f"useChatRuntimeStore.getState().setCheckpoint({json.dumps(picked)});" if picked else ""
+ out = _run(
+ _script(
+ _BRANCH_SWITCH,
+ PICK = pick,
+ FIRST = first_saved,
+ U1 = _U1_ROW,
+ A2 = _assistant_row("a2", 2, "general kenobi", first_saved),
+ A3 = _assistant_row("a3", 3, "hello to you", switched_saved),
+ )
+ )
+ assert (out["before"] or {}).get("totalTokens") == 300
+ after = out["after"] or {}
+ assert out["counts"] == expected["counts"]
+ assert after.get("totalTokens") == expected["total"]
+ assert after.get("estimated") == expected["estimated"]
+
+
+_SEND_WINDOW = """
+ // @ts-nocheck
+ import { renderThreadContextUsageRecount, seed, snapshot, useChatRuntimeStore, world } from "./harness.ts";
+ __LOADED_MODEL__
+ seed({ loadedIsGguf: true });
+ __PICK__
+ const msg = (id, role, text, extra = {}) => ({
+ id, role, createdAt: new Date(), content: [{ type: "text", text }],
+ metadata: { custom: extra.usage ? { contextUsage: extra.usage } : {} },
+ ...(role === "assistant" ? { status: { type: extra.status ?? "complete" } } : {}),
+ });
+ const saved = { promptTokens: 250, completionTokens: 50, totalTokens: 300, cachedTokens: 0, modelId: useChatRuntimeStore.getState().params.checkpoint };
+ const live = [msg("u1", "user", "hello there"), msg("a2", "assistant", "general kenobi", { usage: saved })];
+ world.liveMessages = { "thread-a": live };
+ world.storedMessages["thread-a"] = live;
+ seed({ activeThreadId: "thread-a" });
+ useChatRuntimeStore.getState().setContextUsage(saved);
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const countsBefore = world.countedMessages.length;
+ const steps = [];
+ const step = async (name) => {
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const usage = snapshot().contextUsage;
+ steps.push({ name, counts: world.countedMessages.length - countsBefore, total: usage?.totalTokens ?? null, estimated: usage?.estimated ?? null });
+ };
+ live.push(msg("u3", "user", "and the moon"), msg("a4", "assistant", "", { status: "running" }));
+ await step("running_placeholder");
+ seed({ runningByThreadId: { "thread-a": true } });
+ await step("running");
+ __END__
+ console.log(JSON.stringify({ steps }));
+"""
+
+_RUN_COMPLETES = """
+ const published = { promptTokens: 700, completionTokens: 9, totalTokens: 709, cachedTokens: 0 };
+ useChatRuntimeStore.getState().setContextUsage(published);
+ seed({ runningByThreadId: {} });
+ await step("run_flag_cleared");
+ live[3] = msg("a4", "assistant", "is far away", { usage: { ...published, modelId: useChatRuntimeStore.getState().params.checkpoint } });
+ await step("tip_settled");
+"""
+
+_RUN_CANCELLED = """
+ seed({ runningByThreadId: {} });
+ await step("run_flag_cleared");
+ live[3] = { ...live[3], status: { type: "incomplete", reason: "cancelled" } };
+ await step("tip_settled");
+"""
+
+
+ "picked", ["", "external::openai::gpt-x"], ids = ["local_model", "cloud_model"]
+)
+def test_sending_a_message_costs_no_count_and_never_flickers(picked):
+ pick = f"useChatRuntimeStore.getState().setCheckpoint({json.dumps(picked)});" if picked else ""
+ out = _run(_script(_SEND_WINDOW, PICK = pick, END = textwrap.dedent(_RUN_COMPLETES)))
+ steps = {step["name"]: step for step in out["steps"]}
+ for name, step in steps.items():
+ assert step["counts"] == 0, name
+ assert step["estimated"] is None, name
+ for name in ("running_placeholder", "running"):
+ assert steps[name]["total"] == 300, name
+ assert steps["run_flag_cleared"]["total"] == 709
+ assert steps["tip_settled"]["total"] == 709
+
+
+ ("picked", "expected"),
+ [
+ ("", {"counts": 1, "total": None, "estimated": None}),
+ ("external::openai::gpt-x", {"counts": 0, "total": 303, "estimated": True}),
+ ],
+ ids = ["local_model_recounts", "cloud_model_builds_on_the_last_count"],
+)
+def test_a_run_cancelled_before_any_output_reprices_only_what_it_can_count(picked, expected):
+ pick = f"useChatRuntimeStore.getState().setCheckpoint({json.dumps(picked)});" if picked else ""
+ out = _run(_script(_SEND_WINDOW, PICK = pick, END = textwrap.dedent(_RUN_CANCELLED)))
+ steps = {step["name"]: step for step in out["steps"]}
+ for name in ("running_placeholder", "running"):
+ assert steps[name]["counts"] == 0, name
+ assert steps[name]["total"] == 300, name
+ settled = steps["tip_settled"]
+ assert settled["counts"] == expected["counts"]
+ assert settled["estimated"] == expected["estimated"]
+ if expected["total"] is not None:
+ assert settled["total"] == expected["total"]
+ else:
+ assert settled["total"] not in (None, 300)
+
+
+_RUN_CANCELLED_FOR_A_QUEUED_PROMPT = """
+ world.promptQueueByThreadId = { "thread-a": { paused: false } };
+ seed({ runningByThreadId: {} });
+ await step("run_flag_cleared");
+ live[3] = { ...live[3], status: { type: "incomplete", reason: "cancelled" } };
+ await step("tip_settled");
+"""
+
+_QUEUED_PROMPT_RUNS = """
+ live.push(msg("u5", "user", "steer it"), msg("a6", "assistant", "", { status: "running" }));
+ await step("queued_placeholder");
+ seed({ runningByThreadId: { "thread-a": true } });
+ await step("queued_running");
+ const published = { promptTokens: 800, completionTokens: 9, totalTokens: 809, cachedTokens: 0 };
+ useChatRuntimeStore.getState().setContextUsage(published);
+ world.promptQueueByThreadId = {};
+ seed({ runningByThreadId: {} });
+ await step("queued_flag_cleared");
+ live[5] = msg("a6", "assistant", "steered", { usage: { ...published, modelId: useChatRuntimeStore.getState().params.checkpoint } });
+ await step("queued_settled");
+"""
+
+_QUEUE_PAUSED = """
+ world.promptQueueByThreadId = { "thread-a": { paused: true } };
+ await step("queue_paused");
+"""
+
+
+def test_a_run_cancelled_for_a_queued_prompt_leaves_the_count_to_that_prompt():
+ out = _run(
+ _script(
+ _SEND_WINDOW,
+ PICK = "",
+ END = textwrap.dedent(_RUN_CANCELLED_FOR_A_QUEUED_PROMPT + _QUEUED_PROMPT_RUNS),
+ )
+ )
+ steps = {step["name"]: step for step in out["steps"]}
+ for name, step in steps.items():
+ assert step["counts"] == 0, name
+ assert steps["tip_settled"]["total"] == 300
+ assert steps["queued_settled"]["total"] == 809
+ assert steps["queued_settled"]["estimated"] is None
+
+
+def test_a_queue_paused_after_the_cancel_gets_the_recount_it_waited_for():
+ out = _run(
+ _script(
+ _SEND_WINDOW,
+ PICK = "",
+ END = textwrap.dedent(_RUN_CANCELLED_FOR_A_QUEUED_PROMPT + _QUEUE_PAUSED),
+ )
+ )
+ steps = {step["name"]: step for step in out["steps"]}
+ assert steps["tip_settled"]["counts"] == 0
+ assert steps["queue_paused"]["counts"] == 1
+ assert steps["queue_paused"]["total"] == 112
+
+
+def test_a_runtime_still_showing_another_thread_never_prices_this_one():
+ out = _run(
+ _script(
+ """
+ // @ts-nocheck
+ import { renderThreadContextUsageRecount, seed, snapshot, useChatRuntimeStore, world } from "./harness.ts";
+ __LOADED_MODEL__
+ seed({ loadedIsGguf: true });
+ const u1 = __U1__;
+ world.storedMessages["thread-a"] = [u1, __A2__];
+ seed({ activeThreadId: "thread-a" });
+ useChatRuntimeStore.getState().setContextUsage(__FIRST__);
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const before = snapshot().contextUsage;
+ const countsBefore = world.countedMessages.length;
+ world.mainThreadId = "thread-b";
+ world.storedMessages["thread-a"] = [u1, __A3__];
+ renderThreadContextUsageRecount();
+ world.storedMessages["thread-a"] = [];
+ renderThreadContextUsageRecount();
+ world.mainThreadId = undefined;
+ renderThreadContextUsageRecount();
+ world.storedMessages["thread-a"] = [u1, __A2__];
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ console.log(JSON.stringify({
+ before,
+ after: snapshot().contextUsage,
+ counts: world.countedMessages.length - countsBefore,
+ }));
+ """,
+ FIRST = _SAVED_300_LOCAL,
+ U1 = _U1_ROW,
+ A2 = _assistant_row("a2", 2, "general kenobi", _SAVED_300_LOCAL),
+ A3 = _assistant_row("a3", 3, "another chat", "undefined"),
+ )
+ )
+ assert (out["before"] or {}).get("totalTokens") == 300
+ assert out["counts"] == 0
+ assert (out["after"] or {}).get("totalTokens") == 300
+
+
+ ("saved", "tool_call", "expected"),
+ [
+ (_CLOUD_SUM, "local", {"estimated": True, "total": 7, "context": None}),
+ (_CLOUD_SUM_WITH_CONTEXT, "local", {"estimated": None, "total": 322760, "context": 52000}),
+ (_CLOUD_SUM, "hosted", {"estimated": None, "total": 322760, "context": None}),
+ ],
+ ids = ["loop_sum_without_context", "loop_turn_with_context", "provider_hosted_tool_turn"],
+)
+def test_a_cloud_tool_loop_sum_on_the_tip_is_estimated(saved, tool_call, expected):
+ out = _run(
+ _script(
+ """
+ // @ts-nocheck
+ import { renderThreadContextUsageRecount, seed, snapshot, useChatRuntimeStore, world } from "./harness.ts";
+ __LOADED_MODEL__
+ world.storedMessages["thread-a"] = [__U1__, __A2__];
+ seed({ activeThreadId: "thread-a" });
+ useChatRuntimeStore.getState().setCheckpoint("external::openai::gpt-x");
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ console.log(JSON.stringify({ usage: snapshot().contextUsage }));
+ """,
+ U1 = _U1_ROW,
+ A2 = _assistant_row("a2", 2, "found it", saved, tool_call = tool_call),
+ )
+ )
+ usage = out["usage"] or {}
+ assert usage.get("estimated") == expected["estimated"]
+ assert usage.get("totalTokens") == expected["total"]
+ assert usage.get("contextTokens") == expected["context"]
+
+
+ ("saved", "tool_call", "expected"),
+ [
+ (_CLOUD_SUM, "local", {"estimated": True, "total": 104}),
+ (_CLOUD_SUM_WITH_CONTEXT, "local", {"estimated": None, "total": 322760}),
+ (_CLOUD_SUM, "hosted", {"estimated": None, "total": 322760}),
+ (_CLOUD_SUM, "retrieval", {"estimated": None, "total": 322760}),
+ ],
+ ids = [
+ "loop_sum_without_context",
+ "loop_turn_with_context",
+ "provider_hosted_tool_turn",
+ "retrieval_card_before_one_pass",
+ ],
+)
+def test_history_hydration_estimates_a_cloud_tool_loop_sum(saved, tool_call, expected):
+ out = _run(
+ _script(
+ """
+ // @ts-nocheck
+ import { hydrateThreadUsage, seed, snapshot, world } from "./harness.ts";
+ seed({
+ params: { checkpoint: "external::openai::gpt-x", systemPrompt: "", systemVariables: "" },
+ loadedContextLength: null,
+ loadedIsGguf: false,
+ modelLoading: false,
+ activeThreadId: "thread-a",
+ contextUsage: null,
+ contextUsageByThreadId: {},
+ });
+ const messages = [
+ { id: "u1", parentId: null, role: "user", createdAt: 1, content: [{ type: "text", text: "x".repeat(400) }], metadata: {} },
+ __A2__,
+ ];
+ await hydrateThreadUsage({ remoteId: "thread-a", messages });
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ console.log(JSON.stringify({
+ counts: world.countedMessages.length,
+ contextUsage: snapshot().contextUsage,
+ }));
+ """,
+ A2 = _assistant_row("a2", 2, "found it", saved, tool_call = tool_call),
+ )
+ )
+ assert out["counts"] == 0
+ usage = out["contextUsage"] or {}
+ assert usage.get("estimated") == expected["estimated"]
+ assert usage.get("totalTokens") == expected["total"]
+
+
+def test_switching_to_a_thread_priced_under_another_model_blanks_the_bar():
+ out = _run(
+ _script(
+ """
+ // @ts-nocheck
+ import { seed, useChatRuntimeStore } from "./harness.ts";
+ __LOADED_MODEL__
+ const store = useChatRuntimeStore.getState();
+ store.setThreadContextUsage("thread-a", { promptTokens: 1, completionTokens: 1, totalTokens: 2, cachedTokens: 0, modelId: "unsloth/other-gguf" });
+ store.setThreadContextUsage("thread-b", { promptTokens: 2, completionTokens: 2, totalTokens: 4, cachedTokens: 0 });
+ store.setActiveThreadId("thread-a");
+ const a = useChatRuntimeStore.getState().contextUsage;
+ store.setActiveThreadId("thread-b");
+ const b = useChatRuntimeStore.getState().contextUsage;
+ console.log(JSON.stringify({
+ a,
+ b,
+ stamped: useChatRuntimeStore.getState().contextUsageByThreadId["thread-b"].modelId,
+ }));
+ """
+ )
+ )
+ assert out["a"] is None
+ assert (out["b"] or {}).get("totalTokens") == 4
+ assert out["stamped"] == "unsloth/gguf-model"
+
+
+_LIVE_CHANGE = """
+ // @ts-nocheck
+ import { renderThreadContextUsageRecount, seed, snapshot, useChatRuntimeStore, world } from "./harness.ts";
+ __LOADED_MODEL__
+ seed({ loadedIsGguf: true });
+ __PICK__
+ const model = useChatRuntimeStore.getState().params.checkpoint;
+ const text = (role, size) => (role === "user" ? "x" : "y").repeat(size);
+ const msg = (id, role, at, custom = {}, size = 40) => ({
+ id, role, createdAt: new Date(at), content: [{ type: "text", text: text(role, size) }],
+ metadata: { custom },
+ ...(role === "assistant" ? { status: { type: "complete" } } : {}),
+ });
+ const saved = (total, messageCount) => ({ promptTokens: total - 10, completionTokens: 10, totalTokens: total, cachedTokens: 0, modelId: model, messageCount });
+ const live = [
+ msg("u1", "user", 1), msg("a2", "assistant", 2, { contextUsage: saved(300, 2) }),
+ msg("u3", "user", 3), msg("a4", "assistant", 4, { contextUsage: saved(500, 4) }),
+ msg("u5", "user", 5), msg("a6", "assistant", 6, { contextUsage: saved(700, 6) }),
+ ];
+ world.liveMessages = { "thread-a": live };
+ world.storedMessages["thread-a"] = [];
+ seed({ activeThreadId: "thread-a" });
+ useChatRuntimeStore.getState().setContextUsage(saved(700, 6));
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const countsBefore = world.countedMessages.length;
+ const after = (__AFTER__)(live);
+ world.liveMessages["thread-a"] = after;
+ world.storedMessages["thread-a"] = after.map((m) => ({ ...m, createdAt: m.createdAt.getTime() }));
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const usage = snapshot().contextUsage;
+ console.log(JSON.stringify({
+ counts: world.countedMessages.length - countsBefore,
+ total: usage?.totalTokens ?? null,
+ estimated: usage?.estimated ?? null,
+ }));
+"""
+
+_LIVE_CHANGES = {
+ "in_place_edit_of_the_tip": (
+ "(live) => [...live.slice(0, 5), msg('a6', 'assistant', 6, "
+ "{ contextUsage: saved(700, 6), revision: 1 }, 400)]"
+ ),
+ "metadata_only_change": (
+ "(live) => [...live.slice(0, 5), { ...live[5], metadata: { custom: "
+ "{ ...live[5].metadata.custom, feedback: 'up' } } }]"
+ ),
+ "research_report_replaces_the_tip": (
+ "(live) => [...live.slice(0, 5), msg('a6', 'assistant', 6, "
+ "{ contextUsage: saved(700, 6), researchRunId: 'research-1', serverRevision: 9 }, 400)]"
+ ),
+ "delete_the_last_reply": "(live) => live.slice(0, 5)",
+ "delete_the_last_turn": "(live) => live.slice(0, 4)",
+ "delete_a_middle_turn": "(live) => [live[0], live[1], live[4], live[5]]",
+ "delete_every_reply": "(live) => live.slice(0, 1)",
+ "delete_everything": "(live) => []",
+ "delete_every_reply_to_an_image_only_prompt": (
+ "(live) => [{ ...live[0], content: [{ type: 'image', image: 'data:image/png;base64,AAAA' }] }]"
+ ),
+}
+
+
+ ("change", "picked", "expected"),
+ [
+ ("in_place_edit_of_the_tip", "", {"counts": 1, "total": 162, "estimated": None}),
+ (
+ "in_place_edit_of_the_tip",
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 610, "estimated": True},
+ ),
+ ("metadata_only_change", "", {"counts": 0, "total": 700, "estimated": None}),
+ ("research_report_replaces_the_tip", "", {"counts": 1, "total": 162, "estimated": None}),
+ (
+ "research_report_replaces_the_tip",
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 610, "estimated": True},
+ ),
+ (
+ "metadata_only_change",
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 700, "estimated": None},
+ ),
+ ("delete_the_last_reply", "", {"counts": 1, "total": 137, "estimated": None}),
+ (
+ "delete_the_last_reply",
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 510, "estimated": True},
+ ),
+ ("delete_the_last_turn", "", {"counts": 0, "total": 500, "estimated": None}),
+ (
+ "delete_the_last_turn",
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 500, "estimated": None},
+ ),
+ ("delete_a_middle_turn", "", {"counts": 1, "total": 112, "estimated": None}),
+ (
+ "delete_a_middle_turn",
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 320, "estimated": True},
+ ),
+ ("delete_every_reply", "", {"counts": 1, "total": 37, "estimated": None}),
+ (
+ "delete_every_reply",
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 10, "estimated": True},
+ ),
+ ("delete_everything", "", {"counts": 1, "total": 12, "estimated": None}),
+ (
+ "delete_everything",
+ "external::openai::gpt-x",
+ {"counts": 0, "total": None, "estimated": None},
+ ),
+ (
+ "delete_every_reply_to_an_image_only_prompt",
+ "",
+ {"counts": 0, "total": None, "estimated": None},
+ ),
+ (
+ "delete_every_reply_to_an_image_only_prompt",
+ "external::openai::gpt-x",
+ {"counts": 0, "total": None, "estimated": None},
+ ),
+ ],
+ ids = lambda value: value if isinstance(value, str) and not value.startswith("{") else None,
+)
+def test_a_change_to_the_shown_branch_reprices_the_meter(change, picked, expected):
+ pick = f"useChatRuntimeStore.getState().setCheckpoint({json.dumps(picked)});" if picked else ""
+ out = _run(_script(_LIVE_CHANGE, PICK = pick, AFTER = _LIVE_CHANGES[change]))
+ assert out == expected
+
+
+_HYDRATE_REVISED = """
+ // @ts-nocheck
+ import { hydrateThreadUsage, seed, snapshot, useChatRuntimeStore, world } from "./harness.ts";
+ __LOADED_MODEL__
+ seed({ loadedIsGguf: true, activeThreadId: "thread-a", contextUsage: null, contextUsageByThreadId: {} });
+ __PICK__
+ const model = useChatRuntimeStore.getState().params.checkpoint;
+ const saved = (total, messageCount, revision) => ({ promptTokens: total - 10, completionTokens: 10, totalTokens: total, cachedTokens: 0, modelId: model, messageCount, ...(revision ? { revision } : {}) });
+ const row = (id, parentId, role, createdAt, metadata = {}) => ({
+ id, parentId, role, createdAt, content: [{ type: "text", text: (role === "user" ? "x" : "y").repeat(40) }], metadata,
+ });
+ const messages = __MESSAGES__;
+ let release;
+ world.countGate = new Promise((resolve) => { release = resolve; });
+ await hydrateThreadUsage({ remoteId: "thread-a", messages });
+ const shown = snapshot().contextUsage;
+ release();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ console.log(JSON.stringify({
+ counts: world.countedMessages.length,
+ shown: { total: shown?.totalTokens ?? null, estimated: shown?.estimated ?? null },
+ }));
+"""
+
+_FIRST_TURN = 'row("u1", null, "user", 1), row("a2", "u1", "assistant", 2, __A2__)'
+
+
+ ("a2", "tail", "picked", "expected"),
+ [
+ (
+ "{ contextUsage: saved(300, 2) }",
+ ', row("u3", "a2", "user", 3), '
+ 'row("a4", "u3", "assistant", 4, { contextUsage: saved(500, 4), revision: 1 })',
+ "",
+ {"counts": 1, "shown": {"total": 320, "estimated": True}},
+ ),
+ (
+ "{ contextUsage: saved(300, 2) }",
+ ', row("u3", "a2", "user", 3), '
+ 'row("a4", "u3", "assistant", 4, { contextUsage: saved(500, 4), revision: 1 })',
+ "external::openai::gpt-x",
+ {"counts": 0, "shown": {"total": 320, "estimated": True}},
+ ),
+ (
+ "{ contextUsage: saved(300, 2), revision: 1 }",
+ ', row("u3", "a2", "user", 3), '
+ 'row("a4", "u3", "assistant", 10, { contextUsage: saved(500, 4, 1) })',
+ "",
+ {"counts": 0, "shown": {"total": 500, "estimated": None}},
+ ),
+ (
+ "{ contextUsage: saved(300, 2) }",
+ ', row("u5", "a2", "user", 5), row("a6", "u5", "assistant", 6, { contextUsage: saved(700, 6) })',
+ "",
+ {"counts": 1, "shown": {"total": 320, "estimated": True}},
+ ),
+ (
+ "{ contextUsage: saved(300, 2) }",
+ ', row("u3", "a2", "user", 3)',
+ "",
+ {"counts": 1, "shown": {"total": 310, "estimated": True}},
+ ),
+ ],
+ ids = [
+ "local_reply_edited_after_its_count",
+ "cloud_reply_edited_after_its_count",
+ "reply_written_after_an_earlier_edit",
+ "turn_deleted_above_the_reply",
+ "unanswered_last_prompt",
+ ],
+)
+def test_history_hydration_trusts_only_a_count_that_still_describes_its_branch(
+ a2, tail, picked, expected
+):
+ pick = f"useChatRuntimeStore.getState().setCheckpoint({json.dumps(picked)});" if picked else ""
+ messages = "[" + _FIRST_TURN.replace("__A2__", a2) + tail + "]"
+ out = _run(_script(_HYDRATE_REVISED, PICK = pick, MESSAGES = messages))
+ assert out == expected
+
+
+_OVERLAPPING_RUNS = """
+ // @ts-nocheck
+ import { renderThreadContextUsageRecount, seed, snapshot, useChatRuntimeStore, world } from "./harness.ts";
+ __LOADED_MODEL__
+ seed({ loadedIsGguf: true });
+ __PICK__
+ const model = useChatRuntimeStore.getState().params.checkpoint;
+ const msg = (id, role, at, custom = {}) => ({
+ id, role, createdAt: new Date(at), content: [{ type: "text", text: (role === "user" ? "x" : "y").repeat(40) }],
+ metadata: { custom },
+ ...(role === "assistant" ? { status: { type: "complete" } } : {}),
+ });
+ const saved = (total, messageCount) => ({ promptTokens: total - 10, completionTokens: 10, totalTokens: total, cachedTokens: 0, modelId: model, messageCount });
+ const live = [
+ msg("u1", "user", 1), msg("a2", "assistant", 2, { contextUsage: saved(300, 2) }),
+ msg("u3", "user", 3), msg("a4", "assistant", 4, { contextUsage: saved(500, 4) }),
+ ];
+ const show = (messages) => {
+ world.liveMessages = { "thread-a": messages };
+ world.storedMessages["thread-a"] = messages.map((m) => ({ ...m, createdAt: m.createdAt.getTime() }));
+ };
+ const render = async () => {
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ };
+ show(live);
+ seed({ activeThreadId: "thread-a", runningByThreadId: { "thread-y": true } });
+ useChatRuntimeStore.getState().setContextUsage(saved(500, 4));
+ await render();
+ const countsBefore = world.countedMessages.length;
+ __STEPS__
+ const usage = snapshot().contextUsage;
+ console.log(JSON.stringify({
+ counts: world.countedMessages.length - countsBefore,
+ total: usage?.totalTokens ?? null,
+ estimated: usage?.estimated ?? null,
+ }));
+"""
+
+_SWITCH_WHILE_ANOTHER_CHAT_RUNS = """
+ show([...live.slice(0, 3), msg("a4b", "assistant", 5, { contextUsage: saved(800, 4) })]);
+ await render();
+ seed({ runningByThreadId: {} });
+ await render();
+"""
+
+_SWITCH_TO_AN_UNCOUNTED_REPLY_WHILE_ANOTHER_CHAT_RUNS = """
+ show([live[0], msg("a2b", "assistant", 5)]);
+ await render();
+ seed({ runningByThreadId: {} });
+ await render();
+"""
+
+_DELETE_EVERY_REPLY_WHILE_ANOTHER_CHAT_RUNS = """
+ show([live[0]]);
+ await render();
+ seed({ runningByThreadId: {} });
+ await render();
+"""
+
+_OWN_RUN_ENDS_WHILE_ANOTHER_CHAT_RUNS = """
+ seed({ runningByThreadId: { "thread-y": true, "thread-a": true } });
+ await render();
+ const finished = [...live, msg("u5", "user", 6), msg("a6", "assistant", 7, { contextUsage: saved(700, 6) })];
+ show(finished);
+ useChatRuntimeStore.getState().setContextUsage(saved(700, 6));
+ seed({ runningByThreadId: { "thread-y": true } });
+ await render();
+"""
+
+_DELETE_AFTER_OWN_RUN_WHILE_ANOTHER_CHAT_RUNS = (
+ _OWN_RUN_ENDS_WHILE_ANOTHER_CHAT_RUNS
+ + """
+ show([finished[0], finished[1], finished[4], finished[5]]);
+ await render();
+ seed({ runningByThreadId: {} });
+ await render();
+"""
+)
+
+_DELETE_EVERY_REPLY_AFTER_OWN_RUN_WHILE_ANOTHER_CHAT_RUNS = (
+ _OWN_RUN_ENDS_WHILE_ANOTHER_CHAT_RUNS
+ + """
+ show([finished[0]]);
+ await render();
+ seed({ runningByThreadId: {} });
+ await render();
+"""
+)
+
+_DELETE_THEN_LEAVE_WHILE_ANOTHER_CHAT_RUNS = """
+ show(live.slice(0, 3));
+ await render();
+ world.liveMessages["thread-b"] = [msg("b1", "user", 1), msg("b2", "assistant", 2, { contextUsage: saved(900, 2) })];
+ useChatRuntimeStore.getState().setThreadContextUsage("thread-b", saved(900, 2));
+ useChatRuntimeStore.getState().setActiveThreadId("thread-b");
+ await render();
+"""
+
+_RETURN_AFTER_ANOTHER_CHAT_ENDS = (
+ _DELETE_THEN_LEAVE_WHILE_ANOTHER_CHAT_RUNS
+ + """
+ seed({ runningByThreadId: {} });
+ await render();
+ useChatRuntimeStore.getState().setActiveThreadId("thread-a");
+ await render();
+"""
+)
+
+_RETURN_BEFORE_ANOTHER_CHAT_ENDS = (
+ _DELETE_THEN_LEAVE_WHILE_ANOTHER_CHAT_RUNS
+ + """
+ useChatRuntimeStore.getState().setActiveThreadId("thread-a");
+ await render();
+ seed({ runningByThreadId: {} });
+ await render();
+"""
+)
+
+
+ ("steps", "picked", "expected"),
+ [
+ (_SWITCH_WHILE_ANOTHER_CHAT_RUNS, "", {"counts": 0, "total": 800, "estimated": None}),
+ (
+ _SWITCH_WHILE_ANOTHER_CHAT_RUNS,
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 800, "estimated": None},
+ ),
+ (
+ _DELETE_AFTER_OWN_RUN_WHILE_ANOTHER_CHAT_RUNS,
+ "",
+ {"counts": 1, "total": 112, "estimated": None},
+ ),
+ (
+ _DELETE_AFTER_OWN_RUN_WHILE_ANOTHER_CHAT_RUNS,
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 320, "estimated": True},
+ ),
+ (
+ _SWITCH_TO_AN_UNCOUNTED_REPLY_WHILE_ANOTHER_CHAT_RUNS,
+ "",
+ {"counts": 1, "total": 62, "estimated": None},
+ ),
+ (
+ _SWITCH_TO_AN_UNCOUNTED_REPLY_WHILE_ANOTHER_CHAT_RUNS,
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 20, "estimated": True},
+ ),
+ (
+ _DELETE_EVERY_REPLY_WHILE_ANOTHER_CHAT_RUNS,
+ "",
+ {"counts": 1, "total": 37, "estimated": None},
+ ),
+ (
+ _DELETE_EVERY_REPLY_WHILE_ANOTHER_CHAT_RUNS,
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 10, "estimated": True},
+ ),
+ (
+ _DELETE_EVERY_REPLY_AFTER_OWN_RUN_WHILE_ANOTHER_CHAT_RUNS,
+ "",
+ {"counts": 1, "total": 37, "estimated": None},
+ ),
+ (
+ _DELETE_EVERY_REPLY_AFTER_OWN_RUN_WHILE_ANOTHER_CHAT_RUNS,
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 10, "estimated": True},
+ ),
+ (_RETURN_AFTER_ANOTHER_CHAT_ENDS, "", {"counts": 1, "total": 87, "estimated": None}),
+ (
+ _RETURN_AFTER_ANOTHER_CHAT_ENDS,
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 310, "estimated": True},
+ ),
+ (_RETURN_BEFORE_ANOTHER_CHAT_ENDS, "", {"counts": 1, "total": 87, "estimated": None}),
+ (
+ _RETURN_BEFORE_ANOTHER_CHAT_ENDS,
+ "external::openai::gpt-x",
+ {"counts": 0, "total": 310, "estimated": True},
+ ),
+ ],
+ ids = [
+ "local_branch_switch",
+ "cloud_branch_switch",
+ "local_delete_after_its_own_run",
+ "cloud_delete_after_its_own_run",
+ "local_switch_to_an_uncounted_reply",
+ "cloud_switch_to_an_uncounted_reply",
+ "local_delete_every_reply",
+ "cloud_delete_every_reply",
+ "local_delete_every_reply_after_its_own_run",
+ "cloud_delete_every_reply_after_its_own_run",
+ "local_delete_then_return_after_it_ends",
+ "cloud_delete_then_return_after_it_ends",
+ "local_delete_then_return_before_it_ends",
+ "cloud_delete_then_return_before_it_ends",
+ ],
+)
+def test_a_change_made_while_another_chat_generates_is_priced_when_it_ends(steps, picked, expected):
+ pick = f"useChatRuntimeStore.getState().setCheckpoint({json.dumps(picked)});" if picked else ""
+ out = _run(_script(_OVERLAPPING_RUNS, PICK = pick, STEPS = textwrap.dedent(steps)))
+ assert out == expected
+
+
+ ("setup", "expected_counts"),
+ [
+ ('world.countError = new Error("count refused");', 1),
+ ("world.countedTokensOverride = { value: Number.NaN };", 1),
+ (
+ 'world.countExtras = { rag_scope: { kb_id: "kb-1", autoinject: "always" } };'
+ 'world.countError = new Error("count refused");',
+ 1,
+ ),
+ ],
+ ids = ["refused", "invalid_response", "pending_retrieval"],
+)
+def test_a_declined_recount_cannot_leave_the_deleted_reply_count_exact(setup, expected_counts):
+ script = _script(_LIVE_CHANGE, PICK = "", AFTER = _LIVE_CHANGES["delete_the_last_reply"])
+ script = script.replace("const after =", setup + "\nconst after =")
+ out = _run(script)
+ assert out == {"counts": expected_counts, "total": 510, "estimated": True}
+
+
[email protected]("thread_id", ["thread-a", None], ids = ["saved_chat", "new_chat"])
+def test_changing_thinking_recounts_the_current_prompt(thread_id):
+ out = _run(
+ _script(
+ """
+ import { renderThreadContextUsageRecount, seed, snapshot, useChatRuntimeStore, world } from "./harness.ts";
+ __LOADED_MODEL__
+ seed({ activeThreadId: __THREAD_ID__, supportsReasoning: true, reasoningEnabled: true });
+ useChatRuntimeStore.getState().setContextUsage({ promptTokens: 351, completionTokens: 0, totalTokens: 351, cachedTokens: 0 });
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const before = world.countedMessages.length;
+ world.countedTokensOverride = { value: 341 };
+ seed({ reasoningEnabled: false });
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ renderThreadContextUsageRecount();
+ console.log(JSON.stringify({
+ counts: world.countedMessages.length - before,
+ thinking: world.countedPayloads.at(-1)?.enable_thinking,
+ total: snapshot().contextUsage?.totalTokens,
+ }));
+ """,
+ THREAD_ID = json.dumps(thread_id),
+ )
+ )
+ assert out == {"counts": 1, "thinking": False, "total": 341}
+
+
+def test_reopening_after_thinking_changes_does_not_restore_the_previous_count():
+ out = _run(
+ _script(
+ """
+ import { hydrateThreadUsage, renderThreadContextUsageRecount, seed, snapshot, useChatRuntimeStore, world } from "./harness.ts";
+ __LOADED_MODEL__
+ seed({ activeThreadId: "thread-a", supportsReasoning: true, reasoningEnabled: true });
+ const saved = {
+ promptTokens: 351, completionTokens: 0, totalTokens: 351, cachedTokens: 0,
+ modelId: "unsloth/gguf-model", reasoningKey: "[true,null,null]",
+ };
+ const messages = [
+ { id: "u1", parentId: null, role: "user", createdAt: 1, content: [{ type: "text", text: "hello" }], metadata: {} },
+ { id: "a2", parentId: "u1", role: "assistant", createdAt: 2, content: [{ type: "text", text: "hello back" }], metadata: { contextUsage: saved } },
+ ];
+ world.storedMessages["thread-a"] = messages;
+ useChatRuntimeStore.getState().setContextUsage(saved);
+ renderThreadContextUsageRecount();
+ world.countedTokensOverride = { value: 341 };
+ seed({ reasoningEnabled: false });
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const live = snapshot().contextUsage;
+ await hydrateThreadUsage({ remoteId: "thread-a", messages });
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ console.log(JSON.stringify({
+ live: live?.totalTokens,
+ reopened: snapshot().contextUsage?.totalTokens,
+ reasoningKey: snapshot().contextUsage?.reasoningKey,
+ counts: world.countedMessages.length,
+ }));
+ """
+ )
+ )
+ assert out == {"live": 341, "reopened": 341, "reasoningKey": "[false,null,null]", "counts": 1}
+
+
+def test_a_refused_empty_prompt_clears_the_deleted_turn_count():
+ script = _script(_LIVE_CHANGE, PICK = "", AFTER = _LIVE_CHANGES["delete_everything"])
+ script = script.replace(
+ "const after =", 'world.countError = new Error("empty prompt");\nconst after ='
+ )
+ out = _run(script)
+ assert out == {"counts": 1, "total": None, "estimated": None}
+
+
[email protected]("loading", [True, False], ids = ["loading_history", "loaded_empty_branch"])
+def test_the_registered_branch_reader_distinguishes_loading_from_an_emptied_chat(loading):
+ out = _run(
+ _script(
+ """
+ import { hydrateThreadUsage, refreshContextUsage, registerActiveBranchReader, seed, snapshot, world } from "./harness.ts";
+ __LOADED_MODEL__
+ seed({ activeThreadId: "thread-a" });
+ const messages = [
+ { id: "u1", parentId: null, role: "user", createdAt: 1, content: [{ type: "text", text: "hello" }] },
+ { id: "a1", parentId: "u1", role: "assistant", createdAt: 2, content: [{ type: "text", text: "hello back" }], metadata: { contextTruncation: { boundary_messages: 1 } } },
+ ];
+ world.storedMessages["thread-a"] = messages;
+ world.liveMessages = { "thread-a": [] };
+ world.threadLoading = __LOADING__;
+ registerActiveBranchReader();
+ if (world.threadLoading) {
+ await hydrateThreadUsage({ remoteId: "thread-a", messages });
+ delete world.liveMessages["thread-a"];
+ world.threadLoading = false;
+ } else {
+ await refreshContextUsage();
+ }
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ console.log(JSON.stringify({
+ counts: world.countedMessages.length,
+ messages: world.countedMessages.at(-1)?.length,
+ total: snapshot().contextUsage?.totalTokens,
+ estimated: snapshot().contextUsage?.estimated ?? false,
+ }));
+ """,
+ LOADING = json.dumps(loading),
+ )
+ )
+ assert out == {
+ "counts": 1,
+ "messages": 2 if loading else 0,
+ "total": 62 if loading else 12,
+ "estimated": False,
+ }
+
+
+def test_restoring_chat_thinking_settings_keeps_its_exact_snapshot_until_the_user_changes_them():
+ out = _run(
+ _script(
+ """
+ import { renderThreadContextUsageRecount, seed, snapshot, useChatRuntimeStore, world } from "./harness.ts";
+ __LOADED_MODEL__
+ const saved = {
+ promptTokens: 600, completionTokens: 19, totalTokens: 619, cachedTokens: 0,
+ modelId: "unsloth/gguf-model", reasoningKey: "[true,null,null]",
+ };
+ const messages = [
+ { id: "u1", parentId: null, role: "user", createdAt: 1, content: [{ type: "text", text: "hello" }] },
+ { id: "a1", parentId: "u1", role: "assistant", createdAt: 2, content: [{ type: "text", text: "hello back" }], metadata: { contextUsage: saved } },
+ ];
+ world.storedMessages["thread-a"] = messages;
+ world.storedMessages["thread-b"] = messages;
+ seed({
+ activeThreadId: "thread-a", supportsReasoning: true, reasoningEnabled: true,
+ contextUsage: saved, contextUsageByThreadId: { "thread-a": saved, "thread-b": saved },
+ });
+ renderThreadContextUsageRecount();
+ useChatRuntimeStore.getState().setActiveThreadId("thread-b");
+ seed({ threadScopedSettingsPending: true });
+ renderThreadContextUsageRecount();
+ seed({ reasoningEnabled: false });
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const duringPairing = { counts: world.countedMessages.length, usage: snapshot().contextUsage };
+ seed({ reasoningEnabled: true, threadScopedSettingsPending: false });
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const restored = { counts: world.countedMessages.length, usage: snapshot().contextUsage };
+ seed({ reasoningEnabled: false });
+ world.countedTokensOverride = { value: 618 };
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ console.log(JSON.stringify({
+ duringPairing: { counts: duringPairing.counts, preserved: duringPairing.usage === saved },
+ restored: { counts: restored.counts, preserved: restored.usage === saved },
+ changed: { counts: world.countedMessages.length, total: snapshot().contextUsage?.totalTokens },
+ }));
+ """
+ )
+ )
+ assert out == {
+ "duringPairing": {"counts": 0, "preserved": True},
+ "restored": {"counts": 0, "preserved": True},
+ "changed": {"counts": 1, "total": 618},
+ }
+
+
+def test_finishing_settings_pairing_recounts_a_thinking_change_already_shown_while_loading():
+ out = _run(
+ _script(
+ """
+ import { renderThreadContextUsageRecount, seed, snapshot, world } from "./harness.ts";
+ __LOADED_MODEL__
+ seed({
+ activeThreadId: "thread-a", supportsReasoning: true, reasoningEnabled: true,
+ contextUsage: { promptTokens: 351, completionTokens: 0, totalTokens: 351, cachedTokens: 0, reasoningKey: "[true,null,null]" },
+ });
+ renderThreadContextUsageRecount();
+ seed({ threadScopedSettingsPending: true, reasoningEnabled: false });
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ const before = world.countedMessages.length;
+ world.countedTokensOverride = { value: 341 };
+ seed({ threadScopedSettingsPending: false });
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ console.log(JSON.stringify({ before, counts: world.countedMessages.length, total: snapshot().contextUsage?.totalTokens }));
+ """
+ )
+ )
+ assert out == {"before": 0, "counts": 1, "total": 341}
+
+
+def test_returning_to_the_saved_thinking_mode_restores_its_completion_count():
+ out = _run(
+ _script(
+ """
+ import { renderThreadContextUsageRecount, seed, snapshot, world } from "./harness.ts";
+ __LOADED_MODEL__
+ const saved = {
+ promptTokens: 600, completionTokens: 19, totalTokens: 619, cachedTokens: 0,
+ modelId: "unsloth/gguf-model", reasoningKey: "[true,null,null]",
+ };
+ world.storedMessages["thread-a"] = [
+ { id: "u1", parentId: null, role: "user", createdAt: 1, content: [{ type: "text", text: "hello" }] },
+ { id: "a1", parentId: "u1", role: "assistant", createdAt: 2, content: [{ type: "text", text: "hello back" }], metadata: { contextUsage: saved } },
+ ];
+ seed({
+ activeThreadId: "thread-a", supportsReasoning: true, reasoningEnabled: false,
+ contextUsage: { ...saved, reasoningKey: "[false,null,null]", totalTokens: 618 },
+ });
+ renderThreadContextUsageRecount();
+ seed({ reasoningEnabled: true });
+ renderThreadContextUsageRecount();
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ console.log(JSON.stringify({
+ counts: world.countedMessages.length,
+ preserved: snapshot().contextUsage === saved,
+ total: snapshot().contextUsage?.totalTokens,
+ estimated: snapshot().contextUsage?.estimated ?? false,
+ }));
+ """
+ )
+ )
+ assert out == {"counts": 0, "preserved": True, "total": 619, "estimated": False}
diff --git a/tests/studio/test_token_count_prompt_parity.py b/tests/studio/test_token_count_prompt_parity.py
index b77b9fefb72..079692ffddf 100644
--- a/tests/studio/test_token_count_prompt_parity.py
+++ b/tests/studio/test_token_count_prompt_parity.py
@@ -22,10 +22,15 @@ from _node_harness import (
ADAPTER = source_path("studio/frontend/src/features/chat/api/chat-adapter.ts")
CAPABILITIES = source_path("studio/frontend/src/features/chat/provider-capabilities.ts")
MODEL_SIZE = source_path("studio/frontend/src/lib/model-size.ts")
+COMPACTION = source_path("studio/frontend/src/features/chat/utils/auto-compaction.ts")
+MODEL_CONFIG = source_path(
+ "studio/frontend/src/features/model-picker/model-config/per-model-config.ts"
+)
+EXTERNAL_PROVIDERS = source_path("studio/frontend/src/features/chat/external-providers.ts")
TEMP = WORKDIR / "temp" / "token_count_prompt_parity"
-SOURCES = (ADAPTER, CAPABILITIES, MODEL_SIZE)
+SOURCES = (ADAPTER, CAPABILITIES, MODEL_SIZE, COMPACTION, MODEL_CONFIG, EXTERNAL_PROVIDERS)
def _prune_helpers() -> str:
@@ -48,6 +53,12 @@ def _outbound_builder() -> str:
def _extras_builder() -> str:
"""Joined on blank lines, else a slice starting on the previous brace is not seen as a declaration."""
parts = [
+ read(COMPACTION),
+ slice_between(
+ read(MODEL_CONFIG),
+ "export function isServedByLlamaCpp(",
+ "export function resumesThought(",
+ ),
read(MODEL_SIZE).split("\n", 2)[2],
slice_between(
read(ADAPTER),
@@ -207,6 +218,26 @@ def _count_script(seed_patch: str) -> str:
)
[email protected]("auto_compact", [True, False])
[email protected]("supports_tools", [True, False])
+def test_count_carries_the_completion_compaction_policy(auto_compact, supports_tools):
+ out = _run(
+ f"""
+ import {{ buildLocalTokenCountExtras, seed }} from "./harness.ts";
+ seed({{
+ loadedIsGguf: true,
+ autoCompactEnabled: {str(auto_compact).lower()},
+ supportsTools: {str(supports_tools).lower()},
+ params: {{ checkpoint: "local-model", maxTokens: 512 }},
+ }});
+ console.log(JSON.stringify(await buildLocalTokenCountExtras("thread-a")));
+ """
+ )
+ assert out["context_overflow"] == ("truncate_oldest" if auto_compact else "error")
+ assert out["max_tokens"] == 512
+ assert out["thread_id"] == "thread-a"
+
+
USER_TURN = "draw me a bar chart"
SYSTEM_PROMPT = "You are a helpful assistant."
WITH_PROMPT = (