Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: split parallel tool-call arguments at JSON object boundaries (#10059)
Daniel Hanfa6f06e56b30
diff --git a/studio/backend/core/inference/studio_tool_loop.py b/studio/backend/core/inference/studio_tool_loop.pyindex 41b7759f742..d47d5df5f41 100644--- a/studio/backend/core/inference/studio_tool_loop.py+++ b/studio/backend/core/inference/studio_tool_loop.py@@ -226,6 +226,24 @@ def _chunk_payload(line: str) -> dict[str, Any] | None:return value if isinstance(value, dict) else None+def _mint_streamed_card_id(taken: set[str], index: Any) -> str:+ """The id the client painted an id-less call's card under.++ ``tool_call_<delta index>``, else the lowest free ``tool_call_<n>``: the+ client's rule, reproduced so tool_start and tool_end reach the card the+ stream drew. A card id only; the replayed id stays ``call_<round>_<pos>``.+ """+ preferred = (+ f"tool_call_{index}" if isinstance(index, int) and not isinstance(index, bool) else ""+ )+ if preferred and preferred not in taken:+ return preferred+ position = 0+ while f"tool_call_{position}" in taken:+ position += 1+ return f"tool_call_{position}"++def _normalized_call(call: dict[str, Any], fallback_id: str = "") -> dict[str, Any] | None:call_id = call.get("id")function = call.get("function")@@ -351,6 +369,141 @@ class ToolLoopPolicy:nudge_tool_calls: bool | None = None+def _reject_json_constant(name: str) -> Any:+ """Refuse ``NaN`` / ``Infinity``: ``json.loads`` takes them, ``JSON.parse`` does not."""+ raise ValueError(f"{name} is not JSON")+++def _split_top_level_json_objects(text: str) -> tuple[list[str], str]:+ """The top-level JSON objects in ``text``, and any object still unfinished.++ A second top-level ``{`` means one slot took a second parallel call. Text+ that is not a run of whole objects comes back whole, so a stream this was+ never meant for is left alone. Must agree with+ ``splitTopLevelJsonObjects`` in ``chat/tool-call-arguments.ts``.+ """+ unsplit: tuple[list[str], str] = ([], text)+ complete: list[str] = []+ depth = 0+ start = -1+ in_string = False+ escaped = False++ for i, ch in enumerate(text):+ if in_string:+ # A backslash escapes one character, so a run of them toggles.+ if escaped:+ escaped = False+ elif ch == "\\":+ escaped = True+ elif ch == '"':+ in_string = False+ continue+ if depth == 0:+ # Between objects only whitespace, "\r\n" as readily as "\n".+ if ch == "{":+ depth = 1+ start = i+ continue+ if ch in " \t\n\r":+ continue+ return unsplit+ if ch == '"':+ in_string = True+ continue+ if ch == "{":+ depth += 1+ elif ch == "}":+ depth -= 1+ if depth == 0:+ segment = text[start : i + 1]+ try:+ json.loads(+ segment,+ parse_constant = _reject_json_constant,+ # As text: JSON.parse has no 4300-digit int cap.+ parse_int = str,+ )+ except (ValueError, TypeError):+ # Balanced but invalid: cutting here would invent a call.+ return unsplit+ except RecursionError:+ # Valid but too deep to decode, and not a ValueError.+ return unsplit+ complete.append(segment)+ start = -1++ return complete, ("" if start == -1 else text[start:])+++@dataclass+class _BoundaryScan:+ """``_split_top_level_json_objects`` over a string that only ever grows.++ Rescanning per fragment is O(N^2); resuming is one pass. ``feed`` takes the+ same string extended, never a rewritten one, so ``_Turn`` drops the scan+ for any key a fork rewrites.+ """++ depth: int = 0+ start: int = -1+ in_string: bool = False+ escaped: bool = False+ scanned: int = 0+ complete: list[str] = field(default_factory = list)+ # Once unsplittable, appending can never make it splittable again.+ unsplittable: bool = False++ def feed(self, text: str) -> tuple[list[str], str]:+ if self.unsplittable:+ return [], text+ i = self.scanned+ while i < len(text):+ ch = text[i]+ if self.in_string:+ if self.escaped:+ self.escaped = False+ elif ch == "\\":+ self.escaped = True+ elif ch == '"':+ self.in_string = False+ i += 1+ continue+ if self.depth == 0:+ if ch == "{":+ self.depth = 1+ self.start = i+ i += 1+ continue+ if ch in " \t\n\r":+ i += 1+ continue+ self.unsplittable = True+ return [], text+ if ch == '"':+ self.in_string = True+ elif ch == "{":+ self.depth += 1+ elif ch == "}":+ self.depth -= 1+ if self.depth == 0:+ segment = text[self.start : i + 1]+ try:+ json.loads(+ segment,+ parse_constant = _reject_json_constant,+ parse_int = str,+ )+ except (ValueError, TypeError, RecursionError):+ self.unsplittable = True+ return [], text+ self.complete.append(segment)+ self.start = -1+ i += 1+ self.scanned = len(text)+ return list(self.complete), ("" if self.start == -1 else text[self.start :])++@dataclassclass _Turn:"""Accumulated state for one provider turn."""@@ -358,9 +511,24 @@ class _Turn:by_index: dict[Any, dict[str, Any]] = field(default_factory = dict)order: list[Any] = field(default_factory = list)# call key each delta index maps to: the index itself until a second call- # forks off it, then (index, call_id).+ # forks off it, then (index, call_id), or (index, "_split", n) for a call+ # that had no id to fork on and was found on a JSON object boundary.open_key_by_index: dict[int, Any] = field(default_factory = dict)last_index: int | None = None+ split_seq: int = 0+ seq_by_key: dict[Any, int] = field(default_factory = dict)+ seq_counter: int = 0+ # Which call each id names: a fragment repeating one returns to its call.+ key_by_call_id: dict[str, Any] = field(default_factory = dict)+ # Resumable boundary scan per call, keyed the same as ``by_index``.+ scan_by_key: dict[Any, _BoundaryScan] = field(default_factory = dict)+ # Forks whose object never closed: reported only once it does, or a stream+ # cut short after '{"a":1}{' runs the tool on half an argument.+ open_tail_keys: set[Any] = field(default_factory = set)+ # Metadata from a delta repeating the slot's name. That name is either the+ # call's, resent, or the next call to the same tool announcing itself, and+ # only the object that follows tells them apart.+ pending_extra: dict[Any, dict[str, Any]] = field(default_factory = dict)round: int = 0healed: list[dict[str, Any]] = field(default_factory = list)text: list[str] = field(default_factory = list)@@ -479,32 +647,163 @@ class _Turn:# belong to the newer call.key: Any = self.open_key_by_index.get(index, index)if isinstance(call_id, str) and call_id:- open_id = self.by_index.get(key, {}).get("id")- if open_id and open_id != call_id:+ # An id beats the latest-index mapping: a fragment repeating+ # one returns to its own call wherever that call now sits.+ owner = self.key_by_call_id.get(call_id)+ if owner is not None:+ key = owner+ elif self.by_index.get(key, {}).get("id"):# Two distinct calls reported at the same index. Merging them# concatenates their argument JSON into one unparseable blob# and loses an intent, so key the second on its own id.- # A fragment that names the call this index opened first goes- # back to it: an id beats the latest-index mapping, which only- # exists to place the fragments that carry no id.- first_id = self.by_index.get(index, {}).get("id")- key = index if first_id == call_id else (index, call_id)+ key = (index, call_id)+ # A closed object takes no more content, so the next arguments to reach the+ # slot belong to the next parallel call. Forking on the accumulated text+ # alone catches this only once they glue on, and a delta carrying an id+ # would claim the finished call and append to it (issue #9807).+ held = self.by_index.get(key)+ new_function = raw_call.get("function")+ new_arguments = (+ new_function.get("arguments") if isinstance(new_function, dict) else None+ )+ new_name = new_function.get("name") if isinstance(new_function, dict) else None+ # A next call opens with the "{" of its own arguments object, so a+ # fragment starting with anything else is not one: whitespace after+ # a closing brace belongs to the object just closed, and forking on+ # a stray scalar suffix would run the tool twice.+ # An id names a call outright, so one naming a DIFFERENT call opens+ # the next even before its arguments arrive. Alone, or repeating+ # the slot's name, it is that call's id stamped late.+ held_name_now = held["function"]["name"] if held is not None else ""+ id_names_another_call = bool(+ isinstance(call_id, str)+ and call_id+ and isinstance(new_name, str)+ and new_name+ and held_name_now+ # Not a prefix test: a catalog holds both "web" and+ # "web_search", so only the same name reads as the same call.+ and new_name != held_name_now+ )+ # A snapshot provider repeats the finished call verbatim once it+ # has an id. Exact repeats only, so a second call still opens.+ resends_this_call = bool(+ held is not None+ and isinstance(call_id, str)+ and call_id+ and not held.get("id")+ and isinstance(new_name, str)+ and new_name == held_name_now+ and isinstance(new_arguments, str)+ and new_arguments == held["function"]["arguments"]+ )+ # A name at a closed slot announces the next call: names grow+ # before the arguments, so nothing is left to extend. A shared+ # prefix is no proof ("web" after "web_search" is a second call).+ names_next_call = bool(+ isinstance(new_name, str)+ and new_name+ and held_name_now+ # The same name is that call's, resent: llama-server and vLLM+ # both repeat it, and forking runs one request twice.+ and new_name != held_name_now+ )+ opens_next_call = (+ bool(isinstance(new_arguments, str) and new_arguments.strip().startswith("{"))+ or id_names_another_call+ or names_next_call+ ) and not resends_this_call+ # An announcement has no object to close, so the rule above cannot reach+ # it. A different name bringing an object is the next call; gluing gave+ # "A_longB", which matches no tool.+ announces_over_announcement = (+ held is not None+ and (held.get("announced_only") is True or held.get("resend_suspect") is True)+ and not held["function"]["arguments"]+ and isinstance(new_name, str)+ and bool(new_name)+ and new_name != held_name_now+ and isinstance(new_arguments, str)+ and new_arguments.strip().startswith("{")+ )+ # A fragment repeating the id this slot holds continues it however+ # complete the arguments look, or two calls end up with one id.+ names_this_call = (+ held is not None+ and isinstance(call_id, str)+ and bool(call_id)+ and held.get("id") == call_id+ )+ slot_is_closed = False+ if held is not None and not names_this_call:+ closed, unfinished = self._scan(key, held["function"]["arguments"])+ slot_is_closed = bool(closed) and not unfinished+ extra = raw_call.get("extra_content")+ # A name repeating a closed slot's says nothing new about that call, so+ # metadata riding it belongs to whichever call the next object opens.+ # Merged here it overwrites that call's signature and leaves the next+ # unsigned.+ extra_is_ambiguous = bool(+ slot_is_closed+ and isinstance(new_name, str)+ and new_name+ and new_name == held_name_now+ and isinstance(extra, dict)+ and extra+ )+ if (slot_is_closed and opens_next_call) or announces_over_announcement:+ if announces_over_announcement and held is not None:+ # Taken over while it held only its name.+ held["superseded"] = True+ self.split_seq += 1+ waiting = self.pending_extra.pop(key, None)+ key = (index, "_split", self.split_seq)+ if waiting:+ # The object proved the repeated name announced this call.+ extra = {**waiting, **extra} if isinstance(extra, dict) else waitingself.last_index = indexself.open_key_by_index[index] = keyif key not in self.by_index:+ # A second call to the same tool can arrive with no name, the+ # first delta having given it. Nameless is dropped, so inherit.+ opening_name = new_name if isinstance(new_name, str) and new_name else held_name_nowself.by_index[key] = {"id": "","type": "function",- "function": {"name": "", "arguments": ""},+ "function": {"name": opening_name, "arguments": ""},+ # A name with no `arguments` field is an announcement; a+ # zero-parameter tool sends "" rather than no field.+ "announced_only": new_arguments is None and bool(opening_name),+ # A fork's guess, or a slot the provider opened itself.+ # Only the provider's own announcement runs unfilled.+ "from_fork": held is not None,+ # A name extending the one this fork left behind is most likely it, resent.+ # It still opens a slot, but gives way rather than gluing "alpha_longbeta".+ "resend_suspect": bool(+ opening_name+ and held_name_now+ and opening_name != held_name_now+ and opening_name.startswith(held_name_now)+ ),}+ self.seq_by_key[key] = self._next_seq()# First-seen order, so a negative or out-of-order index cannot# reorder parallel calls against what the model actually sent.self.order.append(key)current = self.by_index[key]+ if new_arguments is not None:+ current["announced_only"] = Falseif isinstance(call_id, str) and call_id:current["id"] = call_id- extra = raw_call.get("extra_content")- if isinstance(extra, dict) and extra:+ self.key_by_call_id.setdefault(call_id, key)+ # So a fork below can hand this delta's metadata to its own call.+ extra_before = current.get("extra_content")+ if extra_is_ambiguous:+ self.pending_extra[key] = {+ **self.pending_extra.get(key, {}),+ **extra,+ }+ elif isinstance(extra, dict) and extra:# Gemini 3 stows this call's thoughtSignature here, and the# native translator rejects a replayed functionCall without it.# Per call, so it cannot ride along on the delta-level slot.@@ -520,31 +819,193 @@ class _Turn:# starts with what we have is the whole name resent; anything# else continues it.fragment = function.get("name")- if isinstance(fragment, str) and fragment:- accumulated = current["function"]["name"]- if fragment.startswith(accumulated):+ name_before = current["function"]["name"]+ # Never once the object has closed AND a name is set: that puts one tool's+ # arguments under another's name. Naming a call that has none is not a+ # rename, and servers do send the arguments first.+ if isinstance(fragment, str) and fragment and not (slot_is_closed and name_before):+ if fragment.startswith(name_before):current["function"]["name"] = fragmentelse:- current["function"]["name"] = accumulated + fragment- if isinstance(function.get("arguments"), str):+ current["function"]["name"] = name_before + fragment+ if isinstance(function.get("arguments"), str) and not resends_this_call:current["function"]["arguments"] += function["arguments"]+ if not (isinstance(call_id, str) and call_id):+ # The id fork cannot see this: an id-less stream has no ids to differ on,+ # so appending glued two calls into one blob (issue #9807).+ self._fork_glued_arguments(+ index,+ key,+ current,+ name_before,+ fragment if isinstance(fragment, str) else "",+ extra_before,+ extra if isinstance(extra, dict) and extra else None,+ )++ def _scan(self, key: Any, text: str) -> tuple[list[str], str]:+ """``_split_top_level_json_objects(text)``, resuming the scan for ``key``.++ The same answer as scanning from byte zero, at the cost of the bytes+ this call added rather than of the whole accumulation.+ """+ scan = self.scan_by_key.get(key)+ if scan is None:+ scan = _BoundaryScan()+ self.scan_by_key[key] = scan+ return scan.feed(text)++ def _fork_glued_arguments(+ self,+ index: int,+ key: Any,+ current: dict[str, Any],+ name_before: str,+ incoming_name: str,+ extra_before: dict[str, Any] | None,+ incoming_extra: dict[str, Any] | None,+ ) -> None:+ """Give every call after the first in one slot a call of its own."""+ complete, tail = self._scan(key, current["function"]["arguments"])+ segments = complete + ([tail] if tail else [])+ if len(segments) < 2:+ return+ # Below rewrites this slot's arguments rather than extending them, so+ # the resumable scan no longer describes the string it was reading.+ self.scan_by_key.pop(key, None)+ # The slot keeps its first object, name and id. Nothing per-call rides+ # along: two calls claiming one thoughtSignature is a rejected turn.+ current["function"]["arguments"] = segments[0]+ # A name on this delta names the calls it opened, so the slot keeps its+ # own: merging gave "alphagamma", which matches no enabled tool.+ born_name = incoming_name or name_before+ current["function"]["name"] = name_before or born_name+ # This delta's metadata belongs to the call it closes, the last one:+ # Gemini checks the signature against the call it is replayed on.+ if incoming_extra is not None:+ if extra_before:+ current["extra_content"] = extra_before+ else:+ current.pop("extra_content", None)+ open_key: Any = key+ for segment in segments[1:]:+ self.split_seq += 1+ born_key = (index, "_split", self.split_seq)+ self.by_index[born_key] = {+ "id": "",+ "type": "function",+ "function": {"name": born_name, "arguments": segment},+ }+ self.seq_by_key[born_key] = self._next_seq()+ if tail and segment is segments[-1]:+ self.open_tail_keys.add(born_key)+ self.order.append(born_key)+ open_key = born_key+ if incoming_extra is not None:+ self.by_index[open_key]["extra_content"] = dict(incoming_extra)+ # Later id-less fragments continue the last call.+ self.open_key_by_index[index] = open_key++ def _call_is_finished(self, key: Any) -> bool:+ """Whether a call forked off an unfinished object has since closed it.++ Only ``length`` and ``content_filter`` mark a turn truncated, so a+ stream that stops after ``{"a":1}{`` looks complete; running the tool a+ second time on that lone brace is worse than dropping a call the model+ never finished writing.+ """+ if key not in self.open_tail_keys:+ return True+ closed, unfinished = _split_top_level_json_objects(+ self.by_index[key]["function"]["arguments"]+ )+ return bool(closed) and not unfinished- def calls(self, taken: set[str] | None = None) -> list[dict[str, Any]]:+ def _next_seq(self) -> int:+ self.seq_counter += 1+ return self.seq_counter++ def calls(+ self,+ taken: set[str] | None = None,+ cards: set[str] | None = None,+ ) -> list[dict[str, Any]]:"""Every call this turn produced, with ids unique across the whole run.``taken`` carries the ids already used by earlier turns. A provider thatrestarts its numbering each turn, and the healer (which always mintscall_0 first), would otherwise put two different results under one id inthe conversation replayed upstream.++ ``cards`` carries the card ids already handed out. The client keeps one+ list of cards for the whole response, so a second round has to keep+ counting rather than start again at ``tool_call_0`` and reopen the first+ round's cards."""seen: set[str] = taken if taken is not None else set()+ painted: set[str] = cards if cards is not None else set()out: list[dict[str, Any]] = []- for position, call in enumerate(- [self.by_index[key] for key in self.order] + list(self.healed)- ):+ # Announcement order, so an index opening in between cannot reorder+ # them. On the sequence number alone: comparing calls raises.+ numbered = [+ (+ self.seq_by_key.get(key, position),+ key[0] if isinstance(key, tuple) else key,+ self.by_index[key],+ )+ for position, key in enumerate(self.order)+ if self._call_is_finished(key)+ ]+ ordered = [+ (index, call) for _, index, call in sorted(numbered, key = lambda triple: triple[0])+ ]+ # An announcement another call took over, and a name that only looked+ # like the closed call's resent, were never calls. A lone announcement+ # the provider opened is kept (a zero-parameter tool looks like that).+ # Its metadata goes to the call it was mistaken for: Gemini stows a+ # thought signature there and rejects a replay without one.+ for key, waiting in self.pending_extra.items():+ # No object ever came, so the repeated name was that call's after+ # all and so is the metadata that rode it.+ held = self.by_index.get(key)+ if held is not None and waiting:+ held["extra_content"] = {**held.get("extra_content", {}), **waiting}+ self.pending_extra.clear()+ kept: list[tuple[Any, dict[str, Any]]] = []+ for index, call in ordered:+ never_ran = not call["function"]["arguments"]+ if never_ran and (+ call.get("superseded") is True+ or call.get("resend_suspect") is True+ or (call.get("announced_only") is True and call.get("from_fork") is True)+ ):+ extra = call.get("extra_content")+ if isinstance(extra, dict) and extra and kept:+ previous = kept[-1][1]+ previous["extra_content"] = {**previous.get("extra_content", {}), **extra}+ continue+ kept.append((index, call))+ ordered = kept+ # Provider ids reserved first, so a minted card id never collides. Only+ # the ones _normalized_call keeps: a call it rejects draws no card, the+ # client releases the id it had minted for it, and an id held here that+ # the client has given back puts the two out of step from the next round.+ painted.update(+ call["id"]+ for _, call in ordered+ if isinstance(call.get("id"), str) and call["id"] and _normalized_call(call) is not None+ )+ for position, (index, call) in enumerate(ordered + [(None, call) for call in self.healed]):+ streamed_id = call.get("id")normalized = _normalized_call(call, fallback_id = f"call_{self.round}_{position}")if normalized is None:continue+ if not (isinstance(streamed_id, str) and streamed_id):+ # No id on the wire: mint the client's spelling for the card+ # events. The conversation id above is untouched.+ card_id = _mint_streamed_card_id(painted, index)+ painted.add(card_id)+ normalized["card_id"] = card_idif normalized["id"] in seen:# The client keyed the card it painted on the id the provider# streamed, so keep that one for the events aimed at the card.@@ -814,6 +1275,9 @@ async def stream_with_studio_tools(last_reprompt_text = ""provider_turns = 0used_call_ids: set[str] = _replayed_call_ids(conversation)+ # Card ids handed out this response. The client keeps one list across all+ # rounds, so the numbering carries rather than reopening an earlier card.+ painted_card_ids: set[str] = set()spent_budget_passes = 0fruitless_turns = 0# One provider call per possible execution, plus headroom for the no-op,@@ -1011,16 +1475,17 @@ async def stream_with_studio_tools(# the rest of the response. Close it the way every other unrun call# is closed. Structured only: a healed call was never streamed, and# the span released just above is what tells the user about that one.- for raw_call in turn.by_index.values():- truncated_id = raw_call.get("id")- function = raw_call.get("function")- name = function.get("name") if isinstance(function, dict) else None- if not isinstance(truncated_id, str) or not truncated_id:- continue- if not isinstance(name, str) or not name:- # Not enough of the call arrived to name a tool, so there is- # no card of ours to close.- continue+ # Through `calls`, which never executes anything: it is what mints+ # the card id for a call the provider gave none, and the client drew+ # its card under that same spelling. Reading the slots directly left+ # every id-less call out, so the card the deltas painted spun for the+ # rest of the response. Calls it drops -- nameless, or a fork whose+ # object never closed -- have no card of ours to close either.+ for raw_call in turn.calls(used_call_ids, painted_card_ids):+ truncated_id = (+ raw_call.get("card_id") or raw_call.get("stream_id") or raw_call["id"]+ )+ name = raw_call["function"]["name"]for card_line in _unrun_call_card(tool_name = name,tool_call_id = truncated_id,@@ -1037,8 +1502,22 @@ async def stream_with_studio_tools(# so the scraped web text in its prompts cannot reach python or terminal,# so a naive or compromised endpoint echoing a call back must not be able# to execute it here.- calls = [] if (truncated or tool_choice == "none") else turn.calls(used_call_ids)+ calls = (+ []+ if (truncated or tool_choice == "none")+ else turn.calls(used_call_ids, painted_card_ids)+ )if not calls:+ # The badge clears between iterations, and this turn is over too.+ # Without it a turn whose only call was refused never reaches the+ # empty status below, so the client keeps the card it drew for that+ # call open and a reprompted call merges into it: an upstream ending+ # on [DONE] sends no finish_reason either, so there is no other+ # boundary on this path. Only when a call was streamed: a turn that+ # said nothing at all left the client nothing to close, and a+ # provider that sends no lines still ends the response silently.+ if turn.by_index:+ yield _status_sse("")# No tool this turn. A model that only said what it was about to do# gets one nudge to actually do it, the same recovery the local loops# give a stalled small model, then the answer stands as written.@@ -1098,7 +1577,7 @@ async def stream_with_studio_tools(# execute: the cap is a safety limit, not a hint to the provider.for card_line in _unrun_call_card(tool_name = call["function"]["name"],- tool_call_id = call.get("stream_id") or call["id"],+ tool_call_id = call.get("card_id") or call.get("stream_id") or call["id"],arguments = call.get("arguments"),result = _TOOL_BUDGET_EXHAUSTED,provenance = _unrun_provenance(call["function"]["name"], round_id),@@ -1153,7 +1632,9 @@ async def stream_with_studio_tools(continuefor card_line in _unrun_call_card(tool_name = decision.tool_name,- tool_call_id = call.get("stream_id") or decision.tool_call_id,+ tool_call_id = (+ call.get("card_id") or call.get("stream_id") or decision.tool_call_id+ ),arguments = decision.arguments,result = _TOOL_SKIPPED.get(decision.action, "Unsloth did not run this call."),provenance = decision.provenance,@@ -1171,6 +1652,9 @@ async def stream_with_studio_tools(name = decision.tool_namearguments = decision.argumentscall_id = decision.tool_call_id+ # Same id for a call the provider named; for one it did not, the+ # card answers to the id the client minted.+ card_id = decision.card_idneeds_confirmation = (confirm_tool_calls and not bypass_permissions and permission_mode != "off")@@ -1230,7 +1714,7 @@ async def stream_with_studio_tools({"type": "tool_end","tool_name": name,- "tool_call_id": call_id,+ "tool_call_id": card_id,"result": TOOL_REJECTED_MESSAGE,"provenance": decision.provenance,}@@ -1289,7 +1773,7 @@ async def stream_with_studio_tools(tool_stream = stream_tool_execution(_invoke,tool_name = name,- tool_call_id = call_id,+ tool_call_id = card_id,cancel_event = cancel_event,)outcome: dict[str, Any] = {}diff --git a/studio/backend/core/inference/tool_loop_controller.py b/studio/backend/core/inference/tool_loop_controller.pyindex 665d9659a8d..83c84402482 100644--- a/studio/backend/core/inference/tool_loop_controller.py+++ b/studio/backend/core/inference/tool_loop_controller.py@@ -193,6 +193,10 @@ class ToolCallDecision:tool_name: strarguments: dict[str, Any]tool_call_id: str = ""+ # The id the card carries on screen. For an id-less call that is the+ # spelling the client minted, not the id the conversation replays;+ # otherwise the two are the same.+ card_call_id: str = ""key: str = ""provenance: dict[str, Any] = field(default_factory = dict)status_text: str = ""@@ -202,6 +206,11 @@ class ToolCallDecision:def should_execute(self) -> bool:return self.action == "execute"+ @property+ def card_id(self) -> str:+ """The id every frontend-visible event for this call is addressed to."""+ return self.card_call_id or self.tool_call_id+@propertydef emit_visible_events(self) -> bool:"""Only real executions should become frontend-visible tool cards."""@@ -235,7 +244,7 @@ class ToolCallDecision:arguments = {"raw": fragment} if fragment is not None else self.argumentsreturn {"tool_name": self.tool_name,- "tool_call_id": self.tool_call_id,+ "tool_call_id": self.card_id,"arguments": arguments,"provenance": self.provenance,}@@ -285,7 +294,7 @@ class ToolCallCompletion:"""Build the payload fields for a real tool_end event."""return {"tool_name": self.decision.tool_name,- "tool_call_id": self.decision.tool_call_id,+ "tool_call_id": self.decision.card_id,"result": self.result,"provenance": self.decision.provenance,}@@ -1033,6 +1042,7 @@ class ToolLoopController:tool_name = tool_name,arguments = coerced.arguments,tool_call_id = str(tool_call.get("id") or ""),+ card_call_id = str(tool_call.get("card_id") or ""),key = key,provenance = provenance,status_text = status_for_tool(tool_name, coerced.arguments),diff --git a/studio/backend/tests/test_external_tool_stream_abuse.py b/studio/backend/tests/test_external_tool_stream_abuse.pyindex f8fea64a183..627b38ebc71 100644--- a/studio/backend/tests/test_external_tool_stream_abuse.py+++ b/studio/backend/tests/test_external_tool_stream_abuse.py@@ -329,7 +329,14 @@ def test_a_provider_cannot_forge_a_studio_control_frame(executed, forged):lines = _run(transport)assert not executed- assert _events(lines, forged["type"]) == []+ relayed = [+ event+ for event in _events(lines, forged["type"])+ # The loop clears the badge with an empty status of its own once a turn+ # is over, so it is a status carrying text that would be the provider's.+ if event.get("content") != ""+ ]+ assert relayed == []# The forged frame must not survive under any encoding either.assert not any("forged-1" in line for line in lines)@@ -1081,3 +1088,56 @@ 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) == 1assert usage_chunks[0]["usage"]["prompt_tokens"] == 50+++def test_a_truncated_turn_closes_the_card_an_id_less_call_painted(executed):+ # The client draws a card the moment the delta arrives, keyed on an id it+ # mints because the provider sent none. Reading the slots directly here left+ # every id-less call out, so refusing to run it left that card spinning for+ # the rest of the response.+ transport = FakeTransport(+ [+ [+ _sse(+ delta = {+ "tool_calls": [+ {+ "index": 0,+ "function": {+ "name": "web_search",+ "arguments": '{"query":"a"}',+ },+ }+ ]+ }+ ),+ _sse(finish = "length"),+ _DONE,+ ]+ ]+ )+ lines = _run(transport, tools = [WEB])++ assert not executed+ ends = _events(lines, "tool_end")+ assert [event.get("tool_call_id") for event in ends] == ["tool_call_0"]+++def test_a_turn_whose_only_call_is_refused_still_ends(executed):+ # A nameless call is dropped before it runs, and an upstream ending on+ # [DONE] sends no finish_reason, so without this the client never reaches a+ # turn boundary and keeps the card it drew for that call.+ transport = FakeTransport(+ [+ [+ _sse(+ delta = {"tool_calls": [{"index": 0, "function": {"arguments": '{"query":"a"}'}}]}+ ),+ _DONE,+ ]+ ]+ )+ lines = _run(transport, tools = [WEB])++ assert not executed+ assert any(event.get("content") == "" for event in _events(lines, "tool_status"))diff --git a/studio/backend/tests/test_parallel_tool_call_arguments.py b/studio/backend/tests/test_parallel_tool_call_arguments.pynew file mode 100644index 00000000000..9d746462d59--- /dev/null+++ b/studio/backend/tests/test_parallel_tool_call_arguments.py@@ -0,0 +1,892 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Parallel tool calls that share one delta index (issue #9807).++A server that streams parallel calls as id-less, index-based deltas reuses one+slot for several calls. Appending their arguments glues them into one+unparseable string, which then rides into the next request verbatim and the+provider answers 400. The fork on a differing call id cannot catch this one:+there are no ids to differ.++The boundary between two calls is the end of a top-level JSON object, not a+change of function name -- the reported stream calls the same tool three times.+"""++from __future__ import annotations++import json++import pytest++from core.inference.studio_tool_loop import (+ _Turn,+ _split_top_level_json_objects,+)+++# --------------------------------------------------------------------- scanner+++ "text,complete,tail",+ [+ ("", [], ""),+ ("{}", ["{}"], ""),+ ('{"a":1}', ['{"a":1}'], ""),+ ('{"a":1}{"b":2}', ['{"a":1}', '{"b":2}'], ""),+ (+ '{"a":1} {"b":2}\n{"c":3}\r\n{"d":4}',+ ['{"a":1}', '{"b":2}', '{"c":3}', '{"d":4}'],+ "",+ ),+ ('{"a":', [], '{"a":'),+ ('{"a":1}{"b":', ['{"a":1}'], '{"b":'),+ # Braces that are data, not structure.+ ('{"a":"}{"}{"b":2}', ['{"a":"}{"}', '{"b":2}'], ""),+ ('{"a":"say \\"}{\\" ok"}{"b":2}', ['{"a":"say \\"}{\\" ok"}', '{"b":2}'], ""),+ ('{"p":"C:\\\\Users\\\\me"}{"b":2}', ['{"p":"C:\\\\Users\\\\me"}', '{"b":2}'], ""),+ ('{"a":{"b":{"c":1}}}', ['{"a":{"b":{"c":1}}}'], ""),+ ('{"a":[{"b":1},{"c":2}]}', ['{"a":[{"b":1},{"c":2}]}'], ""),+ (+ '{"q":"caf\u00e9 \u65e5\u672c\u8a9e \U0001f680"}',+ ['{"q":"caf\u00e9 \u65e5\u672c\u8a9e \U0001f680"}'],+ "",+ ),+ # Not a run of objects: left alone rather than cut somewhere meaningless.+ ('[{"a":1}]', [], '[{"a":1}]'),+ ('"hello"', [], '"hello"'),+ ("42", [], "42"),+ ("null", [], "null"),+ ('{"a":1}junk{"b":2}', [], '{"a":1}junk{"b":2}'),+ ('{"a":1}}', [], '{"a":1}}'),+ ('{"a":1,}{"b":2}', [], '{"a":1,}{"b":2}'),+ ('{"a":"unterminated', [], '{"a":"unterminated'),+ ],+)+def test_split_top_level_json_objects(text, complete, tail):+ assert _split_top_level_json_objects(text) == (complete, tail)+++# ----------------------------------------------------------------- accumulator+++def _delta(+ index,+ name = None,+ arguments = "",+ call_id = None,+):+ function: dict = {"arguments": arguments}+ if name is not None:+ function["name"] = name+ call: dict = {"index": index, "function": function}+ if call_id is not None:+ call["id"] = call_id+ return call+++def _reported(turn: _Turn):+ """The calls the turn ends up making, which is what the loop executes.++ Not every slot becomes one: a name that arrived with no arguments of its+ own opened a slot mid-stream, and it is dropped here rather than run.+ """+ return [(call["function"]["name"], call["function"]["arguments"]) for call in turn.calls()]+++def _shape(turn: _Turn):+ return [+ (turn.by_index[key]["function"]["name"], turn.by_index[key]["function"]["arguments"])+ for key in turn.order+ ]+++def test_the_stream_from_9807_becomes_one_call_per_object():+ # Three fetches and a search, all id-less at index 0. The same tool repeats,+ # so there is no name change to fork on.+ turn = _Turn()+ turn.merge_structured([_delta(0, "url", '{"url":"a"}')])+ turn.merge_structured([_delta(0, "url", '{"url":"b"}')])+ turn.merge_structured([_delta(0, "query", '{"q":"c"}')])+ turn.merge_structured([_delta(0, "url", '{"url":"d"}')])++ assert _shape(turn) == [+ ("url", '{"url":"a"}'),+ ("url", '{"url":"b"}'),+ ("query", '{"q":"c"}'),+ ("url", '{"url":"d"}'),+ ]+ for _name, arguments in _shape(turn):+ assert isinstance(json.loads(arguments), dict)+++def test_every_forked_call_gets_an_id_of_its_own():+ turn = _Turn()+ turn.merge_structured([_delta(0, "url", '{"url":"a"}{"url":"b"}')])+ ids = [call["id"] for call in turn.calls()]+ assert len(ids) == 2+ assert len(set(ids)) == 2+ assert all(ids)+++def test_a_call_at_another_index_survives_a_fork():+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(1, "beta", '{"b":2}')])+ turn.merge_structured([_delta(0, "gamma", '{"c":3}')])++ assert _shape(turn) == [("alpha", '{"a":1}'), ("beta", '{"b":2}'), ("gamma", '{"c":3}')]+++def test_an_ordinary_fragmented_call_is_still_one_call():+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":')])+ turn.merge_structured([_delta(0, None, "1")])+ turn.merge_structured([_delta(0, None, "}")])++ assert _shape(turn) == [("alpha", '{"a":1}')]+++def test_a_fragment_continues_the_call_still_being_written():+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}{"b":')])+ turn.merge_structured([_delta(0, None, "2}")])++ assert _shape(turn) == [("alpha", '{"a":1}'), ("alpha", '{"b":2}')]+++def test_a_stream_that_carries_ids_forks_on_the_id_as_before():+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":', call_id = "call_a")])+ turn.merge_structured([_delta(1, "beta", '{"b":', call_id = "call_b")])+ turn.merge_structured([_delta(0, None, "1}", call_id = "call_a")])+ turn.merge_structured([_delta(1, None, "2}", call_id = "call_b")])++ assert _shape(turn) == [("alpha", '{"a":1}'), ("beta", '{"b":2}')]+ assert [call["id"] for call in turn.calls()] == ["call_a", "call_b"]+++def test_a_name_arriving_in_fragments_is_unaffected():+ # llama-server resends the whole name as it grows; OpenAI streams it in+ # pieces. Both still land on one call.+ turn = _Turn()+ turn.merge_structured([_delta(0, "web", '{"q":')])+ turn.merge_structured([_delta(0, "web_search", '"x"}')])++ assert _shape(turn) == [("web_search", '{"q":"x"}')]+++def test_a_name_only_delta_does_not_rename_the_finished_call():+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, "beta", "")])+ turn.merge_structured([_delta(0, None, '{"b":2}')])++ assert _shape(turn) == [("alpha", '{"a":1}'), ("beta", '{"b":2}')]+++def test_an_id_after_one_closed_object_opens_a_call_not_a_claim():+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, "beta", '{"b":2}', call_id = "call_b")])++ assert _shape(turn) == [("alpha", '{"a":1}'), ("beta", '{"b":2}')]+ assert [call["id"] for call in turn.calls()] == ["call_0_0", "call_b"]+++def test_an_id_after_a_closed_fork_opens_a_third_call():+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}{"b":2}')])+ turn.merge_structured([_delta(0, "gamma", '{"c":3}', call_id = "call_c")])++ assert _shape(turn) == [("alpha", '{"a":1}'), ("alpha", '{"b":2}'), ("gamma", '{"c":3}')]+ ids = [call["id"] for call in turn.calls()]+ assert len(set(ids)) == len(ids)+++def test_nesting_deep_enough_to_exhaust_the_stack_is_unsplittable():+ payload = '{"x":' * 20000 + "null" + "}" * 20000+ assert _split_top_level_json_objects(payload) == ([], payload)++ turn = _Turn()+ turn.merge_structured([_delta(0, "deep", payload)])+ assert _shape(turn) == [("deep", payload)]+++def test_a_fragment_repeating_the_slots_id_continues_that_call():+ # llama-server grows the name across deltas; forking gives two calls one id.+ turn = _Turn()+ turn.merge_structured([_delta(0, "web", '{"q":"x"}', call_id = "call_a")])+ turn.merge_structured([_delta(0, "web_search", "", call_id = "call_a")])++ assert _shape(turn) == [("web_search", '{"q":"x"}')]+ assert [call["id"] for call in turn.calls()] == ["call_a"]+++def test_whitespace_chunked_after_a_closing_brace_is_not_a_new_call():+ # Trailing whitespace is legal JSON and says nothing about another call.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, "alpha", " ")])++ assert _shape(turn) == [("alpha", '{"a":1} ')]++ # ... and the call that really does follow it still forks.+ turn.merge_structured([_delta(0, "alpha", '{"b":2}')])+ assert _shape(turn) == [("alpha", '{"a":1} '), ("alpha", '{"b":2}')]+++def test_the_metadata_a_delta_carries_goes_to_the_call_it_closes():+ # It belongs to the last object this delta closed, which is what Gemini+ # validates the signature against.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":')])+ turn.merge_structured(+ [_delta(0, None, '1}{"b":2}') | {"extra_content": {"google": {"thought_signature": "SIG"}}}]+ )++ entries = [turn.by_index[key] for key in turn.order]+ assert [e["function"]["arguments"] for e in entries] == ['{"a":1}', '{"b":2}']+ assert entries[0].get("extra_content") is None+ assert entries[1]["extra_content"] == {"google": {"thought_signature": "SIG"}}+++def test_nonstandard_numeric_constants_are_not_object_boundaries():+ # json.loads takes NaN and Infinity; JSON.parse does not.+ assert _split_top_level_json_objects('{"a":NaN}{"b":2}') == ([], '{"a":NaN}{"b":2}')+ assert _split_top_level_json_objects('{"a":Infinity}{"b":2}') == (+ [],+ '{"a":Infinity}{"b":2}',+ )+ # Ordinary numbers, exponents included, still split.+ assert _split_top_level_json_objects('{"a":1.5e3}{"b":2}') == (+ ['{"a":1.5e3}', '{"b":2}'],+ "",+ )+++def test_an_opening_delta_after_a_closed_call_does_not_claim_it():+ # Landing the opening delta on the finished call glues what follows.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, "beta", "", call_id = "call_b")])+ turn.merge_structured([_delta(0, None, '{"b":2}', call_id = "call_b")])++ assert _shape(turn) == [("alpha", '{"a":1}'), ("beta", '{"b":2}')]+ ids = [call["id"] for call in turn.calls()]+ assert ids[1] == "call_b"+ assert len(set(ids)) == len(ids)+++def test_a_name_held_for_the_next_call_grows_across_deltas():+ # OpenAI streams "web" then "_search"; llama-server resends the whole.+ for fragments in (("web", "_search"), ("web", "web_search")):+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ for fragment in fragments:+ turn.merge_structured([_delta(0, fragment, "")])+ turn.merge_structured([_delta(0, None, '{"q":"x"}')])++ assert _shape(turn) == [("alpha", '{"a":1}'), ("web_search", '{"q":"x"}')]+++def test_whitespace_carrying_the_repeated_name_is_not_the_next_call():+ # The name is that call's, resent; parking it gave "alphabeta".+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, "alpha", " ")])+ turn.merge_structured([_delta(0, "beta", '{"b":2}')])++ assert _shape(turn) == [("alpha", '{"a":1} '), ("beta", '{"b":2}')]+++def test_a_repeated_id_reaches_its_own_call_across_a_later_split():+ # The index points at the newer call, so matching a repeated id there+ # renamed it and gave it a second copy of the id.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}', call_id = "call_a")])+ turn.merge_structured([_delta(0, "beta", '{"b":')])+ turn.merge_structured([_delta(0, "alpha_long", "", call_id = "call_a")])++ assert _shape(turn) == [("alpha_long", '{"a":1}'), ("beta", '{"b":')]+ ids = [call["id"] for call in turn.calls()]+ assert ids[0] == "call_a"+ assert len(set(ids)) == len(ids)+++def test_metadata_announced_with_a_name_waits_for_that_call():+ # The signature is for the call being announced, and native replay rejects+ # a call wearing another's.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured(+ [_delta(0, "beta", None) | {"extra_content": {"google": {"thought_signature": "SIG"}}}]+ )+ turn.merge_structured([_delta(0, None, '{"b":2}')])++ entries = [turn.by_index[key] for key in turn.order]+ assert _shape(turn) == [("alpha", '{"a":1}'), ("beta", '{"b":2}')]+ assert entries[0].get("extra_content") is None+ assert entries[1]["extra_content"] == {"google": {"thought_signature": "SIG"}}+++def test_the_resumable_scan_agrees_with_scanning_from_the_start():+ # Only safe while it answers as restarting does, for every chunking.+ import random++ from core.inference.studio_tool_loop import _BoundaryScan++ pieces = list('{}"\\ abc:,1[]') + ['\\"', '"a"', "NaN", "\n", "\r\n", "\t", '{"a":1}', "}{"]+ rng = random.Random(20260827)+ for _ in range(4000):+ text = "".join(rng.choice(pieces) for _ in range(rng.randint(0, 16)))+ scan = _BoundaryScan()+ cut = 0+ result = scan.feed("")+ while cut < len(text):+ cut = min(len(text), cut + rng.randint(1, 4))+ result = scan.feed(text[:cut])+ assert result == _split_top_level_json_objects(text), text+++def test_one_argument_streamed_a_character_at_a_time_stays_linear():+ # Rescanning per fragment made a 10 KB argument cost seconds. The bound is+ # generous: the quadratic term is asserted, not a machine's speed.+ import time++ payload = '{"code":"' + "x" * 20000 + '"}'+ turn = _Turn()+ turn.merge_structured([_delta(0, "write", "")])+ started = time.perf_counter()+ for ch in payload:+ turn.merge_structured([_delta(0, None, ch)])+ assert time.perf_counter() - started < 2.0+ assert _shape(turn) == [("write", payload)]+++def test_metadata_arriving_alone_stays_on_the_call_that_closed():+ # No name, so nothing announces another call: the signature is this call's.+ # Parking it lost it outright when no further call followed.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([{"index": 0, "extra_content": {"google": {"thought_signature": "SIG"}}}])++ assert _shape(turn) == [("alpha", '{"a":1}')]+ entries = [turn.by_index[key] for key in turn.order]+ assert entries[0]["extra_content"] == {"google": {"thought_signature": "SIG"}}+++def test_a_call_announced_by_name_only_is_still_reported():+ # A tool that takes no parameters can be announced and then simply end, and+ # _normalized_call already reads empty arguments as {}.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, "beta", "")])++ reported = [(call["function"]["name"], call["function"]["arguments"]) for call in turn.calls()]+ assert reported == [("alpha", '{"a":1}'), ("beta", "{}")]+ ids = [call["id"] for call in turn.calls()]+ assert len(set(ids)) == len(ids)++ # And a later fragment that does open it opens it, rather than adding a+ # second call beside the one this reported.+ turn.merge_structured([_delta(0, None, '{"b":2}')])+ assert _shape(turn) == [("alpha", '{"a":1}'), ("beta", '{"b":2}')]+++def test_a_name_resent_or_grown_after_a_call_closed_invents_nothing():+ # Indistinguishable from a second no-argument call to the same tool, so+ # take the reading that does not run a tool twice. A grown name opens a+ # slot (the prefix is no proof) but `calls` drops it unfilled.+ for resent in ("alpha", "alpha_long"):+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, resent, "")])++ reported = [+ (call["function"]["name"], call["function"]["arguments"]) for call in turn.calls()+ ]+ assert reported == [("alpha", '{"a":1}')]+++def test_a_pending_call_runs_where_the_stream_announced_it():+ # The budget is spent down this list in order.+ turn = _Turn()+ turn.merge_structured([_delta(0, "A", '{"a":1}')])+ turn.merge_structured([_delta(0, "B", "")])+ turn.merge_structured([_delta(1, "C", '{"c":3}')])++ reported = [(call["function"]["name"], call["function"]["arguments"]) for call in turn.calls()]+ assert reported == [("A", '{"a":1}'), ("B", "{}"), ("C", '{"c":3}')]+++def test_a_fragment_that_does_not_open_an_object_does_not_open_a_call():+ # A next call begins with its own "{"; forking on anything else made a+ # stray scalar suffix run the tool twice.+ turn = _Turn()+ turn.merge_structured([_delta(0, "q", '{"query":"a"}')])+ turn.merge_structured([_delta(0, "q", '"b"')])++ assert _shape(turn) == [("q", '{"query":"a"}"b"')]+ assert _split_top_level_json_objects('{"query":"a"}"b"') == ([], '{"query":"a"}"b"')+++def test_an_integer_too_long_to_convert_is_still_a_boundary():+ # json.loads raises past the 4300-digit int cap where JSON.parse does not,+ # and 4301 digits is 4 KB. Validation never reads them, so keep them text.+ big = '{"a":' + "1" * 4301 + "}"+ assert _split_top_level_json_objects(big + '{"b":2}') == ([big, '{"b":2}'], "")++ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", big)])+ turn.merge_structured([_delta(0, "beta", '{"b":2}')])+ assert _shape(turn) == [("alpha", big), ("beta", '{"b":2}')]+++def test_two_calls_announced_at_once_do_not_break_the_sort():+ # Two announcements can share a moment. Breaking the tie by comparing the+ # calls themselves raises TypeError and takes the whole turn with it.+ turn = _Turn()+ turn.merge_structured([_delta(0, "A", '{"a":1}')])+ turn.merge_structured([_delta(1, "B", '{"b":1}')])+ turn.merge_structured([_delta(0, "C", "")])+ turn.merge_structured([_delta(1, "D", "")])++ reported = [(call["function"]["name"], call["function"]["arguments"]) for call in turn.calls()]+ assert reported == [("A", '{"a":1}'), ("B", '{"b":1}'), ("C", "{}"), ("D", "{}")]+++def test_a_pending_call_keeps_its_place_when_it_later_opens():+ # A call takes the moment it was announced at.+ turn = _Turn()+ turn.merge_structured([_delta(0, "A", '{"a":1}')])+ turn.merge_structured([_delta(0, "B", "")])+ turn.merge_structured([_delta(1, "C", '{"c":3}')])+ turn.merge_structured([_delta(0, None, '{"b":2}')])++ # calls() is the list the loop spends its budget down.+ reported = [(call["function"]["name"], call["function"]["arguments"]) for call in turn.calls()]+ assert reported == [("A", '{"a":1}'), ("B", '{"b":2}'), ("C", '{"c":3}')]+++def test_a_resent_name_does_not_rename_the_call_it_closed():+ # A name extending the closed call's is most likely it, resent, so a delta+ # naming its own call wins: merging gave "alpha_longbeta".+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, "alpha_long", "")])+ turn.merge_structured([_delta(0, "beta", '{"b":2}')])++ assert _reported(turn) == [("alpha", '{"a":1}'), ("beta", '{"b":2}')]++ # The second no-argument call to the same tool still works, and so does a+ # name streamed in fragments: neither has an opening name that disagrees.+ same = _Turn()+ same.merge_structured([_delta(0, "no_args", "{}")])+ same.merge_structured([_delta(0, "no_args", "")])+ same.merge_structured([_delta(0, None, "{}")])+ assert _reported(same) == [("no_args", "{}"), ("no_args", "{}")]++ grown = _Turn()+ grown.merge_structured([_delta(0, "alpha", '{"a":1}')])+ grown.merge_structured([_delta(0, "web", "")])+ grown.merge_structured([_delta(0, "_search", '{"q":1}')])+ assert _reported(grown) == [("alpha", '{"a":1}'), ("web_search", '{"q":1}')]+++def test_metadata_on_a_resent_name_stays_with_the_closed_call():+ # Once the name is read as the closed call's, so is the metadata.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}') | {"extra_content": {"own": "A"}}])+ turn.merge_structured([_delta(0, "alpha_long", None) | {"extra_content": {"resent": 1}}])+ turn.merge_structured([_delta(0, "beta", '{"b":2}')])++ reported = turn.calls()+ assert _reported(turn) == [("alpha", '{"a":1}'), ("beta", '{"b":2}')]+ assert reported[0]["extra_content"] == {"own": "A", "resent": 1}+ assert reported[1].get("extra_content") is None+++def test_a_discarded_resend_does_not_lend_its_place_to_the_next_call():+ # The moment belongs to the announcement, and a name read as a resent+ # announced nothing, so the call that opens takes its own arrival.+ turn = _Turn()+ turn.merge_structured([_delta(0, "A", '{"a":1}')])+ turn.merge_structured([_delta(0, "A_long", None)])+ turn.merge_structured([_delta(1, "C", '{"c":3}')])+ turn.merge_structured([_delta(0, "B", '{"b":2}')])++ assert [call["function"]["name"] for call in turn.calls()] == ["A", "C", "B"]++ # An announcement that is accepted still keeps its place.+ kept = _Turn()+ kept.merge_structured([_delta(0, "A", '{"a":1}')])+ kept.merge_structured([_delta(0, "B", None)])+ kept.merge_structured([_delta(1, "C", '{"c":3}')])+ kept.merge_structured([_delta(0, None, '{"b":2}')])+ assert [call["function"]["name"] for call in kept.calls()] == ["A", "B", "C"]+++def test_an_id_stamped_after_the_object_closed_claims_that_call():+ # Reading the late id as another call left the finished one minted.+ for late in ({}, {"name": "alpha"}):+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([{"index": 0, "id": "call_a", "function": late}])++ reported = [+ (call["id"], call["function"]["name"], call["function"]["arguments"])+ for call in turn.calls()+ ]+ assert reported == [("call_a", "alpha", '{"a":1}')]++ # An id that names a different call is still the next call opening.+ opened = _Turn()+ opened.merge_structured([_delta(0, "alpha", '{"a":1}')])+ opened.merge_structured([_delta(0, "beta", "", call_id = "call_b")])+ opened.merge_structured([_delta(0, None, '{"b":2}', call_id = "call_b")])+ assert _shape(opened) == [("alpha", '{"a":1}'), ("beta", '{"b":2}')]+++def test_a_new_name_arriving_with_whitespace_opens_its_own_call():+ # A different name opens the next call rather than renaming the finished+ # one, which gave "alphabeta" and an unnamed second call. The whitespace+ # rides the announcing delta and is valid JSON, so both still parse.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, "beta", " ")])+ turn.merge_structured([_delta(0, None, '{"b":2}')])++ assert _shape(turn) == [("alpha", '{"a":1}'), ("beta", ' {"b":2}')]++ # And a name repeated with the whitespace is still that call's, resent, so+ # the call that really opens next takes its own name.+ resent = _Turn()+ resent.merge_structured([_delta(0, "alpha", '{"a":1}')])+ resent.merge_structured([_delta(0, "alpha", " ")])+ resent.merge_structured([_delta(0, "beta", '{"b":2}')])+ assert _shape(resent) == [("alpha", '{"a":1} '), ("beta", '{"b":2}')]+++def test_metadata_on_a_resent_name_reaches_the_closed_call():+ # A name read as a resent invents no call, so its metadata has nowhere+ # else to go.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}') | {"extra_content": {"own": "A"}}])+ turn.merge_structured([_delta(0, "alpha_long", None) | {"extra_content": {"sig": "S"}}])++ reported = turn.calls()+ assert [(c["function"]["name"], c["function"]["arguments"]) for c in reported] == [+ ("alpha", '{"a":1}')+ ]+ assert reported[0]["extra_content"] == {"own": "A", "sig": "S"}+ # Read twice, so a second read cannot double anything or lose it.+ assert turn.calls()[0]["extra_content"] == {"own": "A", "sig": "S"}+++def test_a_catalog_holding_both_web_and_web_search_splits_either_way_round():+ # Both are in Studio's own catalog, so a shared prefix is no evidence+ # either way; reading it as evidence swallowed the second announcement.+ for first, second in (("web_search", "web"), ("web", "web_search")):+ turn = _Turn()+ turn.merge_structured([_delta(0, first, '{"a":1}')])+ turn.merge_structured([_delta(0, second)])+ turn.merge_structured([_delta(0, None, '{"b":2}')])++ assert _reported(turn) == [(first, '{"a":1}'), (second, '{"b":2}')]+++def test_a_name_bringing_an_object_over_an_announcement_is_the_next_call():+ # An announcement has no object to close, so the next name grew into it.+ for announced in ("alpha_long", "zeta"):+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ # No `arguments` field: an empty string means the call has started,+ # and a name after one is the fragment dialect.+ turn.merge_structured([_delta(0, announced, None)])+ turn.merge_structured([_delta(0, "beta", '{"b":2}')])++ assert _reported(turn) == [("alpha", '{"a":1}'), ("beta", '{"b":2}')]+++def test_an_unfinished_fork_does_not_reserve_a_card_id():+ # The client releases the card it drew for the fork this turn filters out.+ taken: set[str] = set()+ cards: set[str] = set()+ first = _Turn()+ first.merge_structured([_delta(0, "alpha", '{"a":1}{')])+ assert [call.get("card_id") for call in first.calls(taken, cards)] == ["tool_call_0"]++ second = _Turn()+ second.round = 1+ second.merge_structured([_delta(0, "beta", '{"b":2}')])+ assert [call.get("card_id") for call in second.calls(taken, cards)] == ["tool_call_1"]+++def test_a_provider_claiming_a_minted_id_displaces_the_id_less_call():+ # Provider ids are reserved before any card id is minted, so the id-less+ # call moves aside. The client displaces once the claim lands.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(1, "beta", '{"b":2}', call_id = "tool_call_0")])++ reported = [+ (call.get("card_id") or call["id"], call["function"]["name"]) for call in turn.calls()+ ]+ assert reported == [("tool_call_1", "alpha"), ("tool_call_0", "beta")]+++def test_a_claim_on_a_split_born_card_leaves_every_call_its_own():+ # The client renumbers its minted cards when a provider claims one of their+ # spellings, so the numbering here is what it has to land on: the claim+ # keeps tool_call_1 and the three id-less calls take 0, 2 and 3 in order.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}{"b":2}{"c":3}')])+ turn.merge_structured([_delta(1, "beta", '{"d":4}', call_id = "tool_call_1")])++ reported = [+ (call.get("card_id") or call["id"], call["function"]["arguments"]) for call in turn.calls()+ ]+ assert reported == [+ ("tool_call_0", '{"a":1}'),+ ("tool_call_2", '{"b":2}'),+ ("tool_call_3", '{"c":3}'),+ ("tool_call_1", '{"d":4}'),+ ]+++def test_a_nameless_call_reserves_no_card_id():+ # The client drops the card it drew for a call that never got a name, so+ # the next round has to land on the number this one leaves free.+ taken: set[str] = set()+ cards: set[str] = set()+ first = _Turn()+ first.merge_structured([_delta(0, None, '{"a":1}')])+ assert first.calls(taken, cards) == []++ second = _Turn()+ second.round = 1+ second.merge_structured([_delta(0, "beta", '{"b":2}')])+ assert [call.get("card_id") for call in second.calls(taken, cards)] == ["tool_call_0"]+++def test_a_card_ledger_is_append_only_across_rounds():+ # A round that carries tool_call_0 as a provider id does not take the card+ # an earlier round already drew under that spelling, so the third round+ # numbers from what both are holding.+ taken: set[str] = set()+ cards: set[str] = set()+ first = _Turn()+ first.merge_structured([_delta(0, "alpha", '{"a":1}')])+ assert [call.get("card_id") for call in first.calls(taken, cards)] == ["tool_call_0"]++ second = _Turn()+ second.round = 1+ second.merge_structured([_delta(0, "beta", '{"b":2}', call_id = "tool_call_0")])+ assert [call.get("card_id") for call in second.calls(taken, cards)] == [None]++ third = _Turn()+ third.round = 2+ third.merge_structured([_delta(0, "gamma", '{"c":3}')])+ assert [call.get("card_id") for call in third.calls(taken, cards)] == ["tool_call_1"]+++def test_a_rejected_call_reserves_no_card_id():+ # A provider id on a call that never gets a name draws no card: the client+ # releases the id it had minted, so holding it here would put the two out of+ # step from the next round on.+ taken: set[str] = set()+ cards: set[str] = set()+ first = _Turn()+ first.merge_structured([_delta(0, None, '{"a":1}', call_id = "tool_call_0")])+ first.merge_structured([_delta(1, "beta", '{"b":2}')])+ assert [call.get("card_id") for call in first.calls(taken, cards)] == ["tool_call_1"]++ second = _Turn()+ second.round = 1+ second.merge_structured([_delta(0, "gamma", '{"c":3}')])+ assert [call.get("card_id") for call in second.calls(taken, cards)] == ["tool_call_0"]+++def test_a_nameless_claim_does_not_take_a_valid_call_s_card():+ # The client displaces the card holding a spelling the moment a provider+ # claims it, so a claim that turns out not to be a call has to give the+ # number back. Nothing is reserved for it here, so the valid call keeps it.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(1, None, '{"b":2}', call_id = "tool_call_0")])++ reported = [(call.get("card_id"), call["function"]["name"]) for call in turn.calls()]+ assert reported == [("tool_call_0", "alpha")]+++def test_a_repeated_names_metadata_waits_for_the_call_it_announced():+ # The same tool twice on one slot, the second announced by a name-only+ # delta carrying its own signature. Merging it where it landed overwrote+ # the closed call's and left the new one unsigned.+ turn = _Turn()+ turn.merge_structured([_delta(0, "lookup", '{"q":"a"}') | {"extra_content": {"sig": "A"}}])+ turn.merge_structured([_delta(0, "lookup", None) | {"extra_content": {"sig": "B"}}])+ turn.merge_structured([_delta(0, None, '{"q":"b"}')])++ reported = turn.calls()+ assert _reported(turn) == [("lookup", '{"q":"a"}'), ("lookup", '{"q":"b"}')]+ assert reported[0]["extra_content"] == {"sig": "A"}+ assert reported[1]["extra_content"] == {"sig": "B"}++ # No object followed, so the repeated name really was that call's resent.+ kept = _Turn()+ kept.merge_structured([_delta(0, "lookup", '{"q":"a"}') | {"extra_content": {"own": 1}}])+ kept.merge_structured([_delta(0, "lookup", None) | {"extra_content": {"sig": "B"}}])+ assert kept.calls()[0]["extra_content"] == {"own": 1, "sig": "B"}+++def test_parked_metadata_survives_a_late_id_landing_on_the_call():+ # The slot key does not change when an id lands, so the parked signature is+ # still found. Pinned because the frontend keys the same wait by card id,+ # which a late id does rename.+ turn = _Turn()+ turn.merge_structured([_delta(0, "lookup", '{"q":"a"}') | {"extra_content": {"sig": "A"}}])+ turn.merge_structured([_delta(0, "lookup", None) | {"extra_content": {"sig": "B"}}])+ turn.merge_structured([_delta(0, None, "") | {"id": "call_x"}])++ reported = turn.calls()+ assert _reported(turn) == [("lookup", '{"q":"a"}')]+ assert reported[0]["extra_content"] == {"sig": "B"}+++def test_a_stable_id_naming_a_longer_tool_opens_its_own_call():+ # Reading "web_search" as "web" grown gave the id to the completed call.+ turn = _Turn()+ turn.merge_structured([_delta(0, "web", '{"a":1}')])+ turn.merge_structured([_delta(0, "web_search", "", call_id = "call_b")])+ turn.merge_structured([_delta(0, None, '{"b":2}', call_id = "call_b")])++ reported = [+ (call["id"], call["function"]["name"], call["function"]["arguments"])+ for call in turn.calls()+ ]+ assert reported == [("call_0_0", "web", '{"a":1}'), ("call_b", "web_search", '{"b":2}')]+++def test_an_unfinished_split_tail_is_not_reported_as_a_call():+ # Nothing marks this truncated, and running the tool on half an argument+ # is the worse failure.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}{')])++ assert [(call["function"]["name"], call["function"]["arguments"]) for call in turn.calls()] == [+ ("alpha", '{"a":1}')+ ]++ # It is held, not discarded: the rest of the object still opens the call.+ turn.merge_structured([_delta(0, None, '"b":2}')])+ assert _shape(turn) == [("alpha", '{"a":1}'), ("alpha", '{"b":2}')]+++def test_a_second_call_to_the_same_tool_keeps_that_tools_name():+ # The index is reused with arguments alone, and nameless calls are dropped.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, None, '{"a":2}')])++ assert _shape(turn) == [("alpha", '{"a":1}'), ("alpha", '{"a":2}')]+++def test_a_snapshot_repeated_to_carry_the_id_claims_the_call():+ # The id arrives on a verbatim repeat; a second call runs the tool twice.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, "alpha", '{"a":1}', call_id = "call_a")])++ reported = [+ (call["id"], call["function"]["name"], call["function"]["arguments"])+ for call in turn.calls()+ ]+ assert reported == [("call_a", "alpha", '{"a":1}')]+++def test_a_second_call_that_differs_anywhere_still_opens_its_own():+ # The claim above is an exact repeat only: a call whose arguments differ is+ # a parallel call of its own however alike the two look.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])+ turn.merge_structured([_delta(0, "alpha", '{"a":2}', call_id = "call_b")])++ reported = [(call["id"], call["function"]["arguments"]) for call in turn.calls()]+ assert reported == [("call_0_0", '{"a":1}'), ("call_b", '{"a":2}')]+++# ------------------------------------------------------------------- card ids+++def test_an_id_less_call_carries_the_card_id_the_client_painted():+ # Without the id the client minted, tool_start finds no card: eight cards+ # for the four calls in #9807.+ turn = _Turn()+ for url in ("a", "b", "c", "d"):+ turn.merge_structured([_delta(0, "fetch", '{"url":"%s"}' % url)])++ reported = [(call["id"], call.get("card_id")) for call in turn.calls()]+ assert reported == [+ ("call_0_0", "tool_call_0"),+ ("call_0_1", "tool_call_1"),+ ("call_0_2", "tool_call_2"),+ ("call_0_3", "tool_call_3"),+ ]+++def test_the_conversation_id_is_not_the_card_id():+ # What is stored and replayed stays call_<round>_<position>, so a thread+ # written before this still resolves.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}')])++ call = turn.calls()[0]+ assert call["id"] == "call_0_0"+ assert call["card_id"] == "tool_call_0"+++def test_a_call_the_provider_named_gets_no_card_id():+ # An id on the wire is already the card's id on both sides, so minting a+ # second spelling for it would be the very mismatch this exists to close.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}', call_id = "call_a")])++ call = turn.calls()[0]+ assert call["id"] == "call_a"+ assert "card_id" not in call+++def test_a_second_round_keeps_numbering_where_the_first_stopped():+ # One list of cards spans the whole response, so restarting at tool_call_0+ # would address the second round's events to the first round's cards.+ first = _Turn()+ first.merge_structured([_delta(0, "alpha", '{"a":1}')])+ painted: set[str] = set()+ taken: set[str] = set()+ assert [call["card_id"] for call in first.calls(taken, painted)] == ["tool_call_0"]++ second = _Turn()+ second.round = 1+ second.merge_structured([_delta(0, "beta", '{"b":2}')])+ assert [call["card_id"] for call in second.calls(taken, painted)] == ["tool_call_1"]+++def test_a_provider_id_in_the_minted_namespace_is_not_handed_out_twice():+ # tool_call_<n> is not reserved to Unsloth.+ turn = _Turn()+ turn.merge_structured([_delta(0, "alpha", '{"a":1}', call_id = "tool_call_0")])+ turn.merge_structured([_delta(1, "beta", '{"b":2}')])++ reported = [(call["id"], call.get("card_id")) for call in turn.calls()]+ assert reported == [("tool_call_0", None), ("call_0_1", "tool_call_1")]+++def test_a_slot_that_opens_late_keeps_its_own_index():+ # The slot names the card before any split has happened.+ turn = _Turn()+ turn.merge_structured([_delta(3, "alpha", '{"a":1}')])++ assert [call.get("card_id") for call in turn.calls()] == ["tool_call_3"]diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex c79fabdb44d..c56d1cf508c 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -106,9 +106,15 @@ import {type CodexReasoningLedger,} from "../codex-reasoning";-import { toolCallReplayArguments } from "../tool-call-arguments";import {+ createBoundaryScan,+ splitTopLevelJsonObjects,+ toolCallReplayArguments,+} from "../tool-call-arguments";+import {+ bindStreamedToolCallCard,findStreamedToolCallPartIndex,+ mintStreamedToolCallId,resolveToolCallPartId,} from "../tool-call-id";@@ -5272,15 +5278,267 @@ export function createOpenAIStreamAdapter(textCursor?: number;_delta_index?: number;_has_stable_id?: boolean;+ // No `arguments` field at all; a zero-parameter tool sends """".+ _announced_only?: boolean;+ _resend_suspect?: boolean;+ _superseded?: boolean;+ _from_fork?: boolean;extra_content?: unknown;provenance?: ToolCallProvenance;};// Tool call parts, cumulative; result lands on tool_end.const toolCallParts: PositionedToolCallPart[] = [];+ // Ids for calls the stream gave none (issue #9807). A card is minted+ // before its part joins `toolCallParts`, so without this a batch opening+ // three calls mints one id three times.+ const reservedToolCallIds = new Set<string>();+ // Of those, the ones the provider sent. `_has_stable_id` cannot say which:+ // a split marks every card but the last with it, to keep a late id off the+ // calls already spoken for.+ const providerSentToolCallIds = new Set<string>();+ const boundaryScans = new Map<+ string,+ ReturnType<typeof createBoundaryScan>+ >();+ const isPlainRecord = (v: unknown): v is Record<string, unknown> =>+ typeof v === "object" && v !== null && !Array.isArray(v);+ // Forks whose last object never closed, the backend's open_tail_keys.+ // `{"a":1}{` is not marked truncated, so the lone brace would persist as a+ // card nothing completes.+ const openTailIds = new Set<string>();+ // Metadata from a delta repeating a closed card's name. That name is+ // either that call's, resent, or the next call to the same tool announcing+ // itself, and only the object that follows tells them apart.+ const pendingExtraByPartId = new Map<string, Record<string, unknown>>();+ const endProviderTurn = (): boolean => {+ let changed = false;+ // _call_is_finished, on the arguments alone: the backend holds the+ // fork back whatever id reached it.+ for (const tailId of openTailIds) {+ const at = toolCallParts.findIndex((p) => p.toolCallId === tailId);+ if (at === -1) continue;+ const part = toolCallParts[at];+ const scanned = splitTopLevelJsonObjects(part.argsText ?? "");+ if (scanned.complete.length > 0 && !scanned.tail) continue;+ toolCallParts.splice(at, 1);+ releaseStreamedCard(part.toolCallId);+ changed = true;+ }+ // An announcement another call took over, and a name that only looked like+ // a resent, were never calls. A lone announcement stays: a zero-parameter+ // tool looks like that.+ for (let at = toolCallParts.length - 1; at >= 0; at -= 1) {+ const part = toolCallParts[at] as PositionedToolCallPart;+ if (+ (!part._superseded &&+ !part._resend_suspect &&+ !(part._announced_only && part._from_fork)) ||+ part.argsText+ )+ continue;+ // Its metadata goes to the call it was mistaken for: Gemini stows a+ // thought signature there and rejects a replay without one.+ const previous = toolCallParts[at - 1] as+ | PositionedToolCallPart+ | undefined;+ if (part.extra_content !== undefined && previous) {+ previous.extra_content = {+ ...(previous.extra_content ?? {}),+ ...part.extra_content,+ };+ }+ toolCallParts.splice(at, 1);+ releaseStreamedCard(part.toolCallId);+ changed = true;+ }+ // _normalized_call drops a nameless call before reserving a card id, so a+ // card kept here holds a number the backend gives the next round, whose+ // events then land on this blank card.+ for (let at = toolCallParts.length - 1; at >= 0; at -= 1) {+ const part = toolCallParts[at] as PositionedToolCallPart;+ if (part.toolName || part._delta_index === undefined) continue;+ toolCallParts.splice(at, 1);+ releaseStreamedCard(part.toolCallId);+ changed = true;+ }+ for (const [partId, waiting] of pendingExtraByPartId) {+ // No object ever came, so the repeated name was that call's after all+ // and so is the metadata that rode it.+ const at = toolCallParts.findIndex((part) => part.toolCallId === partId);+ if (at === -1) continue;+ const part = toolCallParts[at] as PositionedToolCallPart;+ toolCallParts[at] = {+ ...part,+ extra_content: {+ ...(isPlainRecord(part.extra_content) ? part.extra_content : {}),+ ...waiting,+ },+ };+ changed = true;+ }+ pendingExtraByPartId.clear();+ // The backend numbers the calls that survive: a card dropped above leaves+ // a gap, and a claim displaced one for what turned out not to be a call.+ // Both are settled here, where the backend settles them.+ if (changed) renumberMintedCards();+ openTailIds.clear();+ // The next round opens at index 0 again, and without this "B" then+ // "C" across the boundary named one call "BC".+ for (let at = 0; at < toolCallParts.length; at += 1) {+ const part = toolCallParts[at] as PositionedToolCallPart;+ if (part._delta_index === undefined) continue;+ const closed = { ...part };+ delete closed._delta_index;+ toolCallParts[at] = closed;+ }+ return changed;+ };+ const scanArgsText = (partId: string, text: string) => {+ let scan = boundaryScans.get(partId);+ if (!scan) {+ scan = createBoundaryScan();+ boundaryScans.set(partId, scan);+ }+ return scan.feed(text);+ };+ const mintStreamedCardId = (deltaIndex: number | undefined): string =>+ mintStreamedToolCallId(toolCallParts, deltaIndex, reservedToolCallIds);+ const paintStreamedCard = (partId: string): void => {+ reservedToolCallIds.add(partId);+ bindStreamedToolCallCard(toolPartIdByBackendId, partId);+ };// Raw tool_args accumulator per card: the backend forwards arguments while// the model is still WRITING them, and the partial parse below feeds the// card's args so the code renders live.const liveArgsTextById = new Map<string, string>();+ // A dropped card gives its id back: the backend never reserved it, so+ // holding it makes the next round's mint skip a number it then reuses.+ const releaseStreamedCard = (partId: string): void => {+ reservedToolCallIds.delete(partId);+ toolPartIdByBackendId.delete(partId);+ // A card that took a late id answers to a run-unique part id, so the id+ // the provider sent is a separate key pointing at it. Left behind, it makes+ // the next round's mint skip a number the backend reuses.+ for (const [backendId, mapped] of [...toolPartIdByBackendId]) {+ if (mapped !== partId) continue;+ toolPartIdByBackendId.delete(backendId);+ reservedToolCallIds.delete(backendId);+ providerSentToolCallIds.delete(backendId);+ }+ boundaryScans.delete(partId);+ openTailIds.delete(partId);+ liveArgsTextById.delete(partId);+ pendingExtraByPartId.delete(partId);+ };+ // The backend reserves provider ids before it mints; the client can+ // only move a card aside once the claim lands.+ const renameStreamedCard = (from: string, to: string): void => {+ reservedToolCallIds.delete(from);+ toolPartIdByBackendId.delete(from);+ const scan = boundaryScans.get(from);+ if (scan) boundaryScans.set(to, scan);+ boundaryScans.delete(from);+ if (openTailIds.delete(from)) openTailIds.add(to);+ const live = liveArgsTextById.get(from);+ if (live !== undefined) liveArgsTextById.set(to, live);+ liveArgsTextById.delete(from);+ // The turn-end sweep claims parked metadata by the part id the card holds+ // then, so it has to travel with the rename or the sweep finds nothing.+ const pending = pendingExtraByPartId.get(from);+ if (pending) pendingExtraByPartId.set(to, pending);+ pendingExtraByPartId.delete(from);+ const at = codexRoundToolCallIds.indexOf(from);+ if (at !== -1) codexRoundToolCallIds[at] = to;+ paintStreamedCard(to);+ };+ // The backend reserves provider ids before minting any card id, so a claim+ // mid-response moves every card already numbered. Renumber in the order the+ // backend walks them, or tool_start reaches the wrong card: three calls in+ // one delta and a later `tool_call_1` cross-wire the second and third.+ const renumberMintedCards = (claimed?: string): void => {+ const minted = toolCallParts.filter(+ (part) =>+ !providerSentToolCallIds.has(part.toolCallId) &&+ // This round's cards only. Earlier ones may carry a result, and the+ // backend's ledger is append-only: it never renumbers, so neither may this.+ (part as PositionedToolCallPart)._delta_index !== undefined,+ ) as PositionedToolCallPart[];+ const carried = minted.map((part) => ({+ part,+ from: part.toolCallId,+ scan: boundaryScans.get(part.toolCallId),+ live: liveArgsTextById.get(part.toolCallId),+ openTail: openTailIds.has(part.toolCallId),+ pending: pendingExtraByPartId.get(part.toolCallId),+ }));+ for (const held of carried) {+ reservedToolCallIds.delete(held.from);+ toolPartIdByBackendId.delete(held.from);+ boundaryScans.delete(held.from);+ liveArgsTextById.delete(held.from);+ openTailIds.delete(held.from);+ pendingExtraByPartId.delete(held.from);+ // Cleared before any is minted again: the mint reads the ids the parts+ // hold, and a stale one makes the first card skip the backend's number.+ const at = toolCallParts.indexOf(held.part);+ if (at !== -1) toolCallParts[at] = { ...held.part, toolCallId: "" };+ }+ // Held back only now: the loop above frees every minted id, and one of+ // them is the spelling being claimed.+ if (claimed !== undefined) reservedToolCallIds.add(claimed);+ for (const held of carried) {+ const to = mintStreamedCardId(held.part._delta_index);+ paintStreamedCard(to);+ if (held.scan) boundaryScans.set(to, held.scan);+ if (held.live !== undefined) liveArgsTextById.set(to, held.live);+ if (held.openTail) openTailIds.add(to);+ if (held.pending) pendingExtraByPartId.set(to, held.pending);+ const at = codexRoundToolCallIds.indexOf(held.from);+ if (at !== -1) codexRoundToolCallIds[at] = to;+ const index = toolCallParts.findIndex((part) => part.toolCallId === "");+ if (index !== -1) {+ toolCallParts[index] = { ...held.part, toolCallId: to };+ }+ }+ };+ /**+ * Parts for the calls after the first in a slot holding several. Nothing+ * per-call is copied: the thought signature goes only to the last.+ */+ const bornSplitToolCalls = (+ extraSegments: string[],+ toolName: string,+ deltaIndex: number | undefined,+ extraContent: unknown,+ ): PositionedToolCallPart[] =>+ extraSegments.map((segment, n) => {+ const isLast = n === extraSegments.length - 1;+ let segmentArgs: ToolCallMessagePart["args"] = {};+ try {+ segmentArgs = JSON.parse(segment) as ToolCallMessagePart["args"];+ } catch {+ segmentArgs = { _raw: segment } as ToolCallMessagePart["args"];+ }+ const bornId = mintStreamedCardId(deltaIndex);+ paintStreamedCard(bornId);+ if (!codexRoundToolCallIds.includes(bornId)) {+ codexRoundToolCallIds.push(bornId);+ }+ return {+ type: "tool-call" as const,+ toolCallId: bornId,+ toolName,+ argsText: segment,+ args: segmentArgs,+ textCursor: cumulativeText.length,+ ...(isLast && extraContent !== undefined+ ? { extra_content: extraContent }+ : {}),+ // All but the last are spoken for; the last stays claimable.+ ...(isLast ? {} : { _has_stable_id: true }),+ ...(deltaIndex !== undefined ? { _delta_index: deltaIndex } : {}),+ };+ });// Backend tool ids ("call_0", ...) restart every response, so a bare id as// store key lets a later turn's stream overwrite the preserved output an// earlier still-mounted finished card reads (the tool_start stale-clear@@ -6260,6 +6518,11 @@ export function createOpenAIStreamAdapter(chunk as unknown as { _toolStatus?: string })._toolStatus;if (toolStatusText !== undefined) {+ // The one boundary every round has: only-disabled rounds emit+ // no card and a [DONE] upstream sends no finish_reason.+ if (!toolStatusText) {+ endProviderTurn();+ }runtime.setToolStatus(liveThreadKey(serverCancel),toolStatusText || null,@@ -6344,6 +6607,12 @@ export function createOpenAIStreamAdapter(chunk as unknown as { _toolEvent?: Record<string, unknown> })._toolEvent;if (toolEvent !== undefined) {+ // Unsloth's own tool events end the turn that asked for them; finish_reason+ // alone is not enough. A hosted tool runs INSIDE the turn and rides a whole+ // chunk, where Unsloth's are bare {"type": "tool_start"} frames.+ if (!chunk.choices) {+ endProviderTurn();+ }// Deep Research is an ordinary tool to every loop that runs it, so the handoff// is read off the events they all publish rather than a bespoke frame. An// ungated pair is not rendered: the research card is the reply, a tool pill@@ -6950,13 +7219,40 @@ export function createOpenAIStreamAdapter(const idx =typeof call.index === "number" ? call.index : undefined;const stableId = call.id;+ // The chunk is cast, not validated, and llama-server has+ // shipped `arguments` as a decoded object.+ const deltaArgs =+ typeof call.function?.arguments === "string"+ ? call.function.arguments+ : "";// Unsloth's local Codex loop follows the OpenAI tool-call delta with// tool_start/tool_end events. Resolve the backend id now so all three// event shapes update one run-unique card instead of leaving the raw// provisional card beside a second execution card.+ // Before resolving: a provider claiming a minted spelling+ // would resolve onto that card and merge the two calls. The+ // backend's reserve-then-mint order lands on the same pair.+ if (+ stableId &&+ !providerSentToolCallIds.has(stableId) &&+ toolCallParts.some((part) => part.toolCallId === stableId)+ ) {+ // Renumber first: a minted card holds the claimed spelling until this runs,+ // and marking it provider-sent earlier would exempt it from the move.+ renumberMintedCards(stableId);+ providerSentToolCallIds.add(stableId);+ reservedToolCallIds.add(stableId);+ }const stablePartId = stableId? resolveToolPartId(stableId): undefined;+ // Reserved before any card id is minted, so a provider that+ // happens to spell an id `tool_call_0` keeps it to itself.+ if (stableId) {+ reservedToolCallIds.add(stableId);+ providerSentToolCallIds.add(stableId);+ }+ if (stablePartId) providerSentToolCallIds.add(stablePartId);// match by resolved id when the fragment carries one, else by// index slot; streams that send neither get a minted// tool_call_<n> id.@@ -6965,10 +7261,88 @@ export function createOpenAIStreamAdapter(stablePartId,idx,);- const existing =+ const matched =existingIndex === -1? undefined: toolCallParts[existingIndex];+ // A closed object takes no more content, so a name or arguments reaching it+ // open the next call. Splitting the text alone is too late.+ const slotIsClosed = (() => {+ if (!matched?.argsText) return false;+ const held = scanArgsText(+ matched.toolCallId,+ matched.argsText,+ );+ return held.complete.length > 0 && !held.tail;+ })();+ // A fragment repeating the id this part holds continues it+ // however complete the arguments look.+ const namesThisCall =+ !!stablePartId && matched?.toolCallId === stablePartId;+ // A next call opens with its own "{"; cutting on anything+ // else runs the tool twice on a stray scalar suffix.+ const bringsArgs = deltaArgs.trim().startsWith("{");+ const closedSlot = slotIsClosed && !namesThisCall;+ // An id naming a DIFFERENT call opens the next even before+ // its arguments arrive; alone it is that call's, stamped late.+ const idNamesAnotherCall =+ !!stablePartId &&+ !!call.function?.name &&+ !!matched?.toolName &&+ // Not a prefix test: a catalog holds both "web" and+ // "web_search".+ call.function.name !== matched.toolName;+ // A snapshot provider repeats the finished call verbatim+ // once it has an id. Exact repeats only, so parallel calls+ // differing anywhere still open separately.+ const resendsThisCall =+ !!stablePartId &&+ !!matched &&+ !matched._has_stable_id &&+ call.function?.name === matched.toolName &&+ deltaArgs === matched.argsText;+ // A name at a closed slot announces the next call: names+ // grow before the arguments, so nothing is left to extend. A+ // shared prefix is no proof ("web" after "web_search" is a+ // second call), but the SAME name is that call's, resent, and+ // llama-server and vLLM both resend it.+ const namesNextCall =+ !!call.function?.name &&+ !!matched?.toolName &&+ call.function.name !== matched.toolName;+ // An announcement has no object to close, so the rule above+ // cannot reach it. A different name bringing an object is the+ // next call; gluing gave "A_longB", which matches no tool.+ const announcesOverAnnouncement =+ ((matched as PositionedToolCallPart | undefined)+ ?._announced_only === true ||+ (matched as PositionedToolCallPart | undefined)+ ?._resend_suspect === true) &&+ !matched?.argsText &&+ !!call.function?.name &&+ !!matched?.toolName &&+ call.function.name !== matched.toolName &&+ bringsArgs;+ if (announcesOverAnnouncement && matched) {+ (matched as PositionedToolCallPart)._superseded = true;+ }+ // A name repeating the one a closed card holds says nothing+ // new about that call, so metadata riding it belongs to+ // whichever call the next object opens; merged now it+ // overwrites the signature the closed call arrived with and+ // leaves the next call unsigned.+ const extraIsAmbiguous =+ closedSlot &&+ !!call.function?.name &&+ !!matched?.toolName &&+ call.function.name === matched.toolName &&+ isPlainRecord(call.extra_content);+ const opensNextCall =+ (closedSlot &&+ (bringsArgs || idNamesAnotherCall || namesNextCall) &&+ !resendsThisCall) ||+ announcesOverAnnouncement;+ const existing = opensNextCall ? undefined : matched;if (stablePartId &&@@ -6976,32 +7350,80 @@ export function createOpenAIStreamAdapter() {codexRoundToolCallIds.push(stablePartId);}- const argsFragment = call.function?.arguments ?? "";+ const argsFragment = deltaArgs;streamedChars +=argsFragment.length + (call.function?.name?.length ?? 0);if (existing) {const prevName = existing.toolName ?? "";- const nextName = call.function?.name ?? prevName;- const merged = (existing.argsText ?? "") + argsFragment;+ // Two dialects, and either alone breaks the other:+ // llama-server resends the whole name as it grows, OpenAI+ // streams it in fragments. Same rule as the backend.+ const nameFragment = call.function?.name ?? "";+ // Never once the object has closed AND a name is set: that+ // would put one tool's arguments under another's name.+ // Naming a card that has none is not a rename, and servers+ // do send the arguments first.+ const nextName =+ !nameFragment || (closedSlot && prevName)+ ? prevName+ : nameFragment.startsWith(prevName)+ ? nameFragment+ : prevName + nameFragment;+ // A snapshot repeated to carry the id adds nothing;+ // appending gives `{"a":1}{"a":1}` and splits it in two.+ const merged = resendsThisCall+ ? (existing.argsText ?? "")+ : (existing.argsText ?? "") + argsFragment;+ // A slot holding two objects is holding two calls: this is+ // how vLLM's id-less deltas glue `{"url":"a"}` and+ // `{"url":"b"}` into one unparsable string (issue #9807).+ // Cut on the object boundary, since the same tool twice has+ // no name to cut on; a delta with an id addresses its own.+ const split = stablePartId+ ? { complete: [], tail: "" }+ : scanArgsText(existing.toolCallId, merged);+ // Whether the last segment is still open decides who may+ // go on writing to it.+ const splitTailIsOpen = split.tail.length > 0;+ const segments = splitTailIsOpen+ ? [...split.complete, split.tail]+ : split.complete;+ const isSplit = segments.length > 1;+ // The slot keeps the object it opened with, under the name+ // and id it had; the rest are calls this delta introduced.+ const slotText = isSplit ? segments[0] : merged;let parsedArgs: ToolCallMessagePart["args"] =existing.args ?? {};- if (merged) {+ if (slotText) {try {parsedArgs = JSON.parse(- merged,+ slotText,) as ToolCallMessagePart["args"];} catch {parsedArgs = {- _raw: merged,+ _raw: slotText,} as ToolCallMessagePart["args"];}}const prevExtra = (existing as PositionedToolCallPart).extra_content;+ // Merged, not replaced: a signature announced with the+ // name and one arriving with the arguments are different+ // fields of one call.+ if (extraIsAmbiguous && isPlainRecord(call.extra_content)) {+ pendingExtraByPartId.set(existing.toolCallId, {+ ...(pendingExtraByPartId.get(existing.toolCallId) ?? {}),+ ...call.extra_content,+ });+ }+ const incomingExtra = extraIsAmbiguous+ ? prevExtra+ : isPlainRecord(prevExtra) && isPlainRecord(call.extra_content)+ ? { ...prevExtra, ...call.extra_content }+ : call.extra_content;if (- call.extra_content !== undefined &&- JSON.stringify(call.extra_content) !==- JSON.stringify(prevExtra)+ incomingExtra !== undefined &&+ JSON.stringify(incomingExtra) !== JSON.stringify(prevExtra)) {// Gemini puts the thought signature on the call, and// the next turn is rejected without it.@@ -7014,26 +7436,108 @@ export function createOpenAIStreamAdapter(...(stablePartId? { toolCallId: stablePartId, _has_stable_id: true }: {}),- toolName: nextName,- argsText: merged,+ toolName: isSplit ? prevName || nextName : nextName,+ argsText: slotText,args: parsedArgs,- ...(call.extra_content !== undefined- ? { extra_content: call.extra_content }+ ...(incomingExtra !== undefined && !isSplit+ ? { extra_content: incomingExtra }: prevExtra !== undefined? { extra_content: prevExtra }: {}),...(idx !== undefined ? { _delta_index: idx } : {}),+ // Any arguments field, "" included, means the call has+ // started, so the card survives the boundary sweep.+ _announced_only:+ (existing as PositionedToolCallPart)._announced_only ===+ true && call.function?.arguments === undefined,+ // A name that brought its own arguments was no resend.+ _resend_suspect:+ (existing as PositionedToolCallPart)._resend_suspect ===+ true && !slotText,};toolCallParts[existingIndex] = updated;+ // The card answers to its late id from here on, so what was+ // keyed on the provisional one moves and the id itself goes+ // back: the backend never reserved it for a call the+ // provider went on to name, and holding it makes the next+ // mint skip the number the backend hands out.+ if (stablePartId && stablePartId !== existing.toolCallId) {+ renameStreamedCard(existing.toolCallId, stablePartId);+ }+ if (isSplit) {+ // The slot keeps one segment, not the whole string, so+ // the resumable scan no longer describes it.+ boundaryScans.delete(existing.toolCallId);+ boundaryScans.delete(updated.toolCallId);+ // Appended, not inserted beside the slot, so a call+ // opened third reads third whichever index it reused.+ // This delta's own metadata, not the merge: the merged+ // fields belong to the call the slot was holding, and+ // Gemini validates a signature against the functionCall+ // part it was returned on.+ const born = bornSplitToolCalls(+ segments.slice(1),+ nextName,+ idx,+ call.extra_content,+ );+ // The last is the object still being written, if one is:+ // kept for later fragments, dropped if it never closes.+ if (splitTailIsOpen && born.length > 0) {+ openTailIds.add(born[born.length - 1].toolCallId);+ }+ toolCallParts.push(...born);+ // New calls are state, so they never wait on the gate.+ addedToolCall = true;+ }} else {const callId =stablePartId ||- `tool_call_${idx ?? toolCallParts.length}`;+ mintStreamedCardId(idx ?? toolCallParts.length);+ if (!stablePartId) paintStreamedCard(callId);if (!codexRoundToolCallIds.includes(callId)) {codexRoundToolCallIds.push(callId);}- const argsText = argsFragment;+ // vLLM bundles several calls into one delta when the model+ // writes them in one pass. Same boundary as above.+ const freshSplit = stablePartId+ ? { complete: [], tail: "" }+ : splitTopLevelJsonObjects(argsFragment);+ const freshTailIsOpen = freshSplit.tail.length > 0;+ const freshSegments = freshTailIsOpen+ ? [...freshSplit.complete, freshSplit.tail]+ : freshSplit.complete;+ const freshIsSplit = freshSegments.length > 1;+ const nameFragment = call.function?.name ?? "";+ const heldName = matched?.toolName ?? "";+ // A second call to the same tool can arrive with no name,+ // the first delta having given it; blank names nothing.+ const freshName = nameFragment || heldName;+ // Across several calls the metadata belongs to the one+ // this delta closes, the last. Same as the backend.+ // The object proved the repeated name announced this call,+ // so what was waiting on the card it reached is this one's.+ const waiting = matched+ ? pendingExtraByPartId.get(matched.toolCallId)+ : undefined;+ if (waiting && matched) {+ pendingExtraByPartId.delete(matched.toolCallId);+ }+ const withWaiting =+ waiting === undefined+ ? call.extra_content+ : {+ ...waiting,+ ...(isPlainRecord(call.extra_content)+ ? call.extra_content+ : {}),+ };+ const freshOwnExtra = freshIsSplit ? undefined : withWaiting;+ const bornExtra = freshIsSplit ? withWaiting : undefined;+ const argsText = freshIsSplit+ ? freshSegments[0]+ : argsFragment;let parsedArgs: ToolCallMessagePart["args"] = {};if (argsText) {try {@@ -7049,20 +7553,56 @@ export function createOpenAIStreamAdapter(const fresh: PositionedToolCallPart = {type: "tool-call" as const,toolCallId: callId,- toolName: call.function?.name ?? "",+ toolName: freshName,argsText,args: parsedArgs,textCursor: cumulativeText.length,- ...(call.extra_content !== undefined- ? { extra_content: call.extra_content }+ ...(freshOwnExtra !== undefined+ ? { extra_content: freshOwnExtra }: {}),...(stablePartId ? { _has_stable_id: true } : {}),...(idx !== undefined ? { _delta_index: idx } : {}),+ ...(call.function?.arguments === undefined && freshName+ ? { _announced_only: true }+ : {}),+ // A fork's guess, or a slot the provider opened itself.+ // Only the provider's own announcement runs unfilled.+ ...(matched ? { _from_fork: true } : {}),+ // A name extending the one this fork left behind is most+ // likely it, resent. It still opens a card (the prefix is+ // no proof) but gives way rather than gluing+ // "alpha_longbeta".+ ...(freshName &&+ heldName &&+ freshName !== heldName &&+ freshName.startsWith(heldName)+ ? { _resend_suspect: true }+ : {}),};- toolCallParts.push(fresh);+ const born = freshIsSplit+ ? bornSplitToolCalls(+ freshSegments.slice(1),+ freshName,+ idx,+ bornExtra,+ )+ : [];+ // As above: the fork whose object is still open is held+ // only for the rest of the turn.+ if (freshTailIsOpen && born.length > 0) {+ openTailIds.add(born[born.length - 1].toolCallId);+ }+ toolCallParts.push(fresh, ...born);addedToolCall = true;}}+ // After this chunk's deltas: a provider can put finish_reason+ // on the same chunk as the turn's last name-only delta.+ if (chunk.choices?.[0]?.finish_reason) {+ // Ending the turn drops cards, so the publish below has to+ // see it rather than wait for the pacing gate.+ replayStateChanged ||= endProviderTurn();+ }if (addedToolCall ||replayStateChanged ||@@ -7082,6 +7622,9 @@ export function createOpenAIStreamAdapter(}continue;}+ if (chunk.choices?.[0]?.finish_reason) {+ replayStateChanged ||= endProviderTurn();+ }// extra_content can arrive with no content at all: a Gemini// thoughtSignature fragment, or the codex reasoning ledger on a// terminal delta. The skip below would drop both.diff --git a/studio/frontend/src/features/chat/tool-call-arguments.ts b/studio/frontend/src/features/chat/tool-call-arguments.tsindex 7f0076fd3b0..9339557d039 100644--- a/studio/frontend/src/features/chat/tool-call-arguments.ts+++ b/studio/frontend/src/features/chat/tool-call-arguments.ts@@ -1,6 +1,151 @@// SPDX-License-Identifier: AGPL-3.0-only// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0+/** `true` when `text` is exactly one JSON object and nothing else. */+function isSingleJsonObject(text: string): boolean {+ try {+ const value = JSON.parse(text);+ return typeof value === "object" && value !== null && !Array.isArray(value);+ } catch {+ return false;+ }+}++/**+ * One slot's accumulated argument text, cut into the top-level JSON objects it+ * holds: `complete` closed, `tail` is still being written.+ *+ * A second top-level `{` means the stream reused the slot for another parallel+ * call, which is what turns two calls into `{"url":"a"}{"url":"b"}`. Text that+ * is not a run of whole objects comes back whole in `tail`, leaving streams+ * this was never meant for alone.+ */+export function splitTopLevelJsonObjects(text: string): {+ complete: string[];+ tail: string;+} {+ const unsplit = { complete: [] as string[], tail: text };+ const complete: string[] = [];+ let depth = 0;+ let start = -1;+ let inString = false;+ let escaped = false;++ for (let i = 0; i < text.length; i += 1) {+ const ch = text[i];+ if (inString) {+ // A backslash escapes one character, so a run of them toggles.+ if (escaped) escaped = false;+ else if (ch === "\\") escaped = true;+ else if (ch === '"') inString = false;+ continue;+ }+ if (depth === 0) {+ // Between objects only whitespace, "\r\n" as readily as "\n".+ if (ch === "{") {+ depth = 1;+ start = i;+ continue;+ }+ if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") continue;+ return unsplit;+ }+ if (ch === '"') {+ inString = true;+ continue;+ }+ if (ch === "{") depth += 1;+ else if (ch === "}") {+ depth -= 1;+ if (depth === 0) {+ const segment = text.slice(start, i + 1);+ try {+ JSON.parse(segment);+ } catch {+ // Balanced but invalid: cutting here would invent a call.+ return unsplit;+ }+ complete.push(segment);+ start = -1;+ }+ }+ }++ return {+ complete,+ tail: start === -1 ? "" : text.slice(start),+ };+}++/**+ * `splitTopLevelJsonObjects` over a string that only ever grows. Rescanning per+ * fragment is O(N^2): a 20 KB argument sent a character at a time took about a+ * second and a half on the thread that paints the stream. `feed` takes the same+ * string extended, never a rewritten one, so a caller that splits a slot drops+ * its scan.+ */+export function createBoundaryScan(): {+ feed: (text: string) => { complete: string[]; tail: string };+} {+ let depth = 0;+ let start = -1;+ let inString = false;+ let escaped = false;+ let scanned = 0;+ // Once unsplittable, appending can never make it splittable again.+ let unsplittable = false;+ const complete: string[] = [];++ return {+ feed(text: string) {+ if (unsplittable) return { complete: [], tail: text };+ for (let i = scanned; i < text.length; i += 1) {+ const ch = text[i];+ if (inString) {+ if (escaped) escaped = false;+ else if (ch === "\\") escaped = true;+ else if (ch === '"') inString = false;+ continue;+ }+ if (depth === 0) {+ if (ch === "{") {+ depth = 1;+ start = i;+ continue;+ }+ if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") continue;+ unsplittable = true;+ return { complete: [], tail: text };+ }+ if (ch === '"') {+ inString = true;+ continue;+ }+ if (ch === "{") depth += 1;+ else if (ch === "}") {+ depth -= 1;+ if (depth === 0) {+ const segment = text.slice(start, i + 1);+ try {+ JSON.parse(segment);+ } catch {+ unsplittable = true;+ return { complete: [], tail: text };+ }+ complete.push(segment);+ start = -1;+ }+ }+ }+ scanned = text.length;+ return {+ complete: [...complete],+ tail: start === -1 ? "" : text.slice(start),+ };+ },+ };+}+/*** The `function.arguments` string to replay for a stored tool call.*@@ -8,18 +153,49 @@* byte-exact. Text that does not parse would be replayed on every later request* in the thread, and strict chat templates reject the whole request rather than* one call, so it falls back to the structured args the part already carries.+ *+ * `{ _raw }` is the adapter's marker for text it could not parse, so a thread+ * carrying one replays as `{}` rather than as the blob.*/export function toolCallReplayArguments(argsText: string | undefined,args: unknown,): string {- if (typeof argsText === "string" && argsText.length > 0) {- try {- JSON.parse(argsText);- return argsText;- } catch {- // unparsable, so the structured args below stand in for it- }+ if (+ typeof argsText === "string" &&+ argsText.length > 0 &&+ // One object, because that is what `function.arguments` is. A run of them,+ // an array, a scalar or a half-written object gets the whole request+ // rejected, not just the one call.+ isSingleJsonObject(argsText)+ ) {+ return argsText;+ }+ const serialized = JSON.stringify(args ?? {});+ // Not a set of named parameters, whatever else it might be.+ if (serialized === undefined || !isSingleJsonObject(serialized)) {+ return "{}";+ }+ const parsed = JSON.parse(serialized) as Record<string, unknown>;+ const keys = Object.keys(parsed);+ // The adapter writes `{ _raw }` holding the exact text it could not parse,+ // so a lone `_raw` whose value IS that text is the marker, and replaying it+ // would send a parameter no tool declares. `_raw` is not reserved and an+ // MCP server's schema is its own, so recognise it only when the surviving+ // text proves it: a thread stored before `argsText` was kept has nothing to+ // compare against, and guessing from the value's shape would discard the+ // argument of a tool that really takes one. Guessing buys little anyway,+ // since the wrapped form is one JSON object and raises no `Extra data`.+ if (+ keys.length === 1 &&+ keys[0] === "_raw" &&+ typeof parsed._raw === "string" &&+ // Non-empty, or the equality proves nothing: the adapter only writes the+ // marker for parsed text, so `{ _raw: "" }` is a real argument.+ parsed._raw.length > 0 &&+ parsed._raw === argsText+ ) {+ return "{}";}- return JSON.stringify(args ?? {});+ return serialized;}diff --git a/studio/frontend/src/features/chat/tool-call-id.ts b/studio/frontend/src/features/chat/tool-call-id.tsindex 61a79caf678..7028bfebfa4 100644--- a/studio/frontend/src/features/chat/tool-call-id.ts+++ b/studio/frontend/src/features/chat/tool-call-id.ts@@ -23,6 +23,41 @@ export interface StreamedToolCallPart {_has_stable_id?: boolean;}+/**+ * The id for a card drawn from a delta the provider gave no id to: the slot's+ * own `tool_call_<index>` when free, else the lowest `tool_call_<n>` that is.+ * The backend's `_mint_streamed_card_id` walks the same deltas under the same+ * rule, so its `tool_start` reaches this card instead of opening a second one.+ * `reserved` holds ids the provider sent, so one spelled `tool_call_0` keeps+ * it. No colon: a replayed id must satisfy `^[a-zA-Z0-9_-]+$`.+ */+export function mintStreamedToolCallId(+ parts: StreamedToolCallPart[],+ deltaIndex: number | undefined,+ reserved: Set<string>,+): string {+ const isTaken = (candidate: string) =>+ reserved.has(candidate) || parts.some((part) => part.toolCallId === candidate);+ const preferred = deltaIndex === undefined ? "" : `tool_call_${deltaIndex}`;+ if (preferred && !isTaken(preferred)) return preferred;+ let position = 0;+ while (isTaken(`tool_call_${position}`)) position += 1;+ return `tool_call_${position}`;+}++/**+ * Let a card drawn by the deltas answer to its own id. Without it,+ * `resolveToolCallPartId` mints `<backend id>:<uuid>` when the backend first+ * names an id-less call, `tool_start` finds no card and pushes one, and the+ * turn persists two parts per call.+ */+export function bindStreamedToolCallCard(+ ids: Map<string, string>,+ partId: string,+): void {+ if (!ids.has(partId)) ids.set(partId, partId);+}+/*** Newest part holding `deltaIndex`, or -1. `unownedOnly` restricts the match to* a slot no provider id has claimed yet.diff --git a/studio/frontend/tests/chat-adapter-scan-cost.test.ts b/studio/frontend/tests/chat-adapter-scan-cost.test.tsindex 312bf3b3665..173345df90d 100644--- a/studio/frontend/tests/chat-adapter-scan-cost.test.ts+++ b/studio/frontend/tests/chat-adapter-scan-cost.test.ts@@ -452,7 +452,7 @@ const FLAG_NEXT_TO_APPEND =/streamedChars \+= reasoning\.length \+ delta\.length;\s*producedReplyText = true;/;/** The adapter between two anchors, without its comments. */-function regionOf(from: string, to: string, maxChars = 60_000): string {+function regionOf(from: string, to: string, maxChars = 75_000): string {const start = ADAPTER.indexOf(from);assert.notEqual(start, -1, `"${from}" is gone; this test needs rewriting`);const end = ADAPTER.indexOf(to, start);diff --git a/studio/frontend/tests/chat-stream-publish-gate.test.ts b/studio/frontend/tests/chat-stream-publish-gate.test.tsindex 82a280a5923..c49ef19c72c 100644--- a/studio/frontend/tests/chat-stream-publish-gate.test.ts+++ b/studio/frontend/tests/chat-stream-publish-gate.test.ts@@ -267,14 +267,16 @@ function withoutComments(source: string): string {}/** The adapter between two anchors, without its comments. */-function regionOf(from: string, to: string, maxChars = 60_000): string {+function regionOf(from: string, to: string, maxChars = 75_000): string {const start = ADAPTER.indexOf(from);assert.notEqual(start, -1, `"${from}" is gone; this test needs rewriting`);const end = ADAPTER.indexOf(to, start);assert.notEqual(end, -1, `"${to}" is gone; this test needs rewriting`);// Without this, editing the end anchor's line (even adding a space) silently// slides the region to the next match hundreds of lines away, and the- // ordering assertions below go on passing against the wrong slice.+ // ordering assertions below go on passing against the wrong slice. A ceiling+ // on drift, not a budget: raise it when the loop legitimately grows, after+ // checking the anchors still land where they should.assert.ok(end - start < maxChars,`the region from "${from}" to "${to}" is ${end - start} chars; ` +@@ -637,13 +639,20 @@ test("a per-call thought signature forces a publish", () => {// cannot be replayed.const update = source.indexOf("const prevExtra =");assert.notEqual(update, -1, "the existing-call update path is gone");- const window = source.slice(update, update + 700);+ // Wide enough for the parking branch that now sits between the anchor and+ // the latch: the ambiguous metadata of a repeated name waits rather than+ // landing on the closed call.+ const window = source.slice(update, update + 1400);assert.ok(window.includes("replayStateChanged = true"),"a changed per-call extra_content does not force a publish",);+ // Read off `incomingExtra`, which merges what the delta carried into what+ // the card already held rather than replacing it: a signature announced with+ // the name and metadata arriving with the arguments are different fields of+ // one call, and dropping either gets the replayed turn rejected.assert.ok(- window.includes("call.extra_content !== undefined"),+ window.includes("incomingExtra !== undefined"),"the latch fires on calls that carry no extra_content at all",);diff --git a/studio/frontend/tests/parallel-tool-call-arguments.test.ts b/studio/frontend/tests/parallel-tool-call-arguments.test.tsnew file mode 100644index 00000000000..0affa355b01--- /dev/null+++ b/studio/frontend/tests/parallel-tool-call-arguments.test.ts@@ -0,0 +1,1573 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// Issue #9807: a backend streaming id-less, index-based deltas reuses one slot+// for several calls, so the adapter glued their arguments into an unparsable+// `{"url":"a"}{"url":"b"}`. The boundary is the end of a top-level JSON object,+// not a change of name: the reported stream calls one tool three times.++import assert from "node:assert/strict";+import { readFileSync } from "node:fs";+import test from "node:test";+import { fileURLToPath } from "node:url";++import ts from "typescript";++import {+ createBoundaryScan,+ splitTopLevelJsonObjects,+ toolCallReplayArguments,+} from "../src/features/chat/tool-call-arguments.ts";+import {+ bindStreamedToolCallCard,+ findStreamedToolCallPartIndex,+ mintStreamedToolCallId,+ resolveToolCallPartId,+} from "../src/features/chat/tool-call-id.ts";++test("a slot holding one object is left as one object", () => {+ assert.deepEqual(splitTopLevelJsonObjects('{"url":"a"}'), {+ complete: ['{"url":"a"}'],+ tail: "",+ });+ assert.deepEqual(splitTopLevelJsonObjects("{}"), {+ complete: ["{}"],+ tail: "",+ });+ assert.deepEqual(splitTopLevelJsonObjects(""), { complete: [], tail: "" });+});++test("adjacent objects are cut apart, however they are spaced", () => {+ assert.deepEqual(+ splitTopLevelJsonObjects('{"a":1}{"b":2} {"c":3}\n{"d":4}\r\n{"e":5}'),+ {+ complete: ['{"a":1}', '{"b":2}', '{"c":3}', '{"d":4}', '{"e":5}'],+ tail: "",+ },+ );+});++test("the object still being written is the tail, not a call", () => {+ assert.deepEqual(splitTopLevelJsonObjects('{"a":1}{"b":'), {+ complete: ['{"a":1}'],+ tail: '{"b":',+ });+ assert.deepEqual(splitTopLevelJsonObjects('{"a":'), {+ complete: [],+ tail: '{"a":',+ });+});++test("braces that are data, not structure, are not boundaries", () => {+ assert.deepEqual(splitTopLevelJsonObjects('{"a":"}{"}{"b":2}'), {+ complete: ['{"a":"}{"}', '{"b":2}'],+ tail: "",+ });+ assert.deepEqual(splitTopLevelJsonObjects('{"a":"say \\"}{\\" ok"}{"b":2}'), {+ complete: ['{"a":"say \\"}{\\" ok"}', '{"b":2}'],+ tail: "",+ });+ assert.deepEqual(+ splitTopLevelJsonObjects('{"p":"C:\\\\Users\\\\me"}{"b":2}'),+ {+ complete: ['{"p":"C:\\\\Users\\\\me"}', '{"b":2}'],+ tail: "",+ },+ );+ assert.deepEqual(splitTopLevelJsonObjects('{"a":{"b":{"c":1}}}'), {+ complete: ['{"a":{"b":{"c":1}}}'],+ tail: "",+ });+ assert.deepEqual(splitTopLevelJsonObjects('{"a":[{"b":1},{"c":2}]}'), {+ complete: ['{"a":[{"b":1},{"c":2}]}'],+ tail: "",+ });+});++test("text that is not a run of objects is handed back untouched", () => {+ for (const text of [+ '[{"a":1}]',+ '"hello"',+ "42",+ "null",+ '{"a":1}junk{"b":2}',+ '{"a":1}}',+ '{"a":1,}{"b":2}',+ '{"a":"unterminated',+ ]) {+ assert.deepEqual(+ splitTopLevelJsonObjects(text),+ { complete: [], tail: text },+ text,+ );+ }+});++test("a healthy call replays byte for byte", () => {+ assert.equal(+ toolCallReplayArguments('{"query":"first"}', { query: "first" }),+ '{"query":"first"}',+ );+ assert.equal(+ toolCallReplayArguments("", { query: "first" }),+ '{"query":"first"}',+ );+});++test("the _raw marker never reaches a backend as a tool parameter", () => {+ // `_raw` is the adapter's marker for text it could not parse.+ assert.equal(+ toolCallReplayArguments('{"query":"a"}{"query":"b"}', {+ _raw: '{"query":"a"}{"query":"b"}',+ }),+ "{}",+ );+ assert.equal(+ toolCallReplayArguments('{"query":', { _raw: '{"query":' }),+ "{}",+ );+});++test("arguments that are not one JSON object fall back rather than replay", () => {+ assert.equal(toolCallReplayArguments("[1,2]", { url: "a" }), '{"url":"a"}');+ assert.equal(toolCallReplayArguments(undefined, [1, 2]), "{}");+ assert.equal(toolCallReplayArguments(undefined, "nope"), "{}");+ assert.equal(toolCallReplayArguments(undefined, null), "{}");+ assert.equal(toolCallReplayArguments(undefined, undefined), "{}");+});++// chat-adapter.ts cannot be imported, so lift the loop as+// tests/pr9057-video-simulation.test.ts does: a re-implementation passes while+// the adapter stays broken, which is how this defect survived+// tool-call-delta-index.test.ts.+const adapterSource = readFileSync(+ fileURLToPath(+ new URL("../src/features/chat/api/chat-adapter.ts", import.meta.url),+ ),+ "utf8",+);++function liftBetween(what: string, from: string, to: string): string {+ const start = adapterSource.indexOf(from);+ assert.ok(start >= 0, `${what}: "${from}" is gone from chat-adapter.ts`);+ const end = adapterSource.indexOf(to, start);+ assert.ok(end > start, `${what}: "${to}" is gone from chat-adapter.ts`);+ return adapterSource.slice(start, end);+}++/** The two helpers the loop calls, declared beside `toolCallParts`. */+function liftSplitHelpers(): string {+ const lifted = liftBetween(+ "split helpers",+ "const reservedToolCallIds = new Set<string>();",+ "const toolPartIdByBackendId = new Map<string, string>();",+ );+ assert.ok(+ lifted.includes("bornSplitToolCalls"),+ "the split helpers moved in chat-adapter.ts",+ );+ return lifted;+}++function liftDeltaLoop(): string {+ const loopStart = adapterSource.indexOf(+ "for (const tc of rawDeltaToolCalls) {",+ );+ assert.ok(+ loopStart >= 0,+ "the delta.tool_calls loop moved in chat-adapter.ts",+ );+ const gate = adapterSource.lastIndexOf(+ "if (",+ adapterSource.indexOf("addedToolCall ||", loopStart),+ );+ assert.ok(gate > loopStart, "the publish gate moved in chat-adapter.ts");+ const lifted = adapterSource.slice(loopStart, gate);+ assert.ok(+ lifted.includes("splitTopLevelJsonObjects"),+ "the loop no longer splits on JSON object boundaries",+ );+ return lifted;+}++interface DeltaCall {+ id?: string;+ index?: number;+ function?: { name?: string; arguments?: string };+ extra_content?: unknown;+}++interface LoopPart {+ toolCallId: string;+ toolName?: string;+ argsText?: string;+ args?: Record<string, unknown>;+ _delta_index?: number;+ _has_stable_id?: boolean;+ extra_content?: unknown;+}++/**+ * The lifted loop, with the locals it closes over supplied by hand.+ *+ * `mintPartIds` picks which `resolveToolPartId` the loop closes over. The+ * accumulation tests want the identity, because they assert which call an id+ * lands on and the spelling is noise. The card tests want the shipped mint,+ * `<backend id>:<uuid>`, because whether the backend's id finds the card the+ * deltas drew is the whole question there, and under the identity every id+ * finds one whether or not the two halves ever agreed.+ */+function makeStream(mintPartIds = false): {+ feed: (batch: DeltaCall[], finished?: boolean) => boolean;+ parts: LoopPart[];+ resolveToolPartId: (backendId: string) => string;+ endRound: () => void;+} {+ const body = `+ const toolCallParts = [];+ let codexRoundToolCallIds = [];+ const toolPartIdByBackendId = new Map();+ const cumulativeText = "";+ let streamedChars = 0;+ ${liftSplitHelpers()}+ let mintedPartIds = 0;+ const resolveToolPartId = (backendId) =>+ resolveToolCallPartId(+ toolPartIdByBackendId,+ backendId,+ undefined,+ toolCallParts[toolCallParts.length - 1]?.toolCallId ?? "",+ () =>+ mintPartIds ? backendId + ":uuid-" + (mintedPartIds += 1) : backendId,+ );+ let addedToolCall = false;+ let replayStateChanged = false;+ // What the lifted loop reads finish_reason off.+ let chunk = { choices: [{}] };+ function feed(rawDeltaToolCalls, finished) {+ chunk = { choices: [finished ? { finish_reason: "tool_calls" } : {}] };+ addedToolCall = false;+ replayStateChanged = false;+ ${liftDeltaLoop()}+ return addedToolCall;+ }+ // Production drops a backend id's binding on tool_end, so an id the+ // provider reuses in a later round reaches a card of its own. That branch+ // is outside the lifted loop, so a test that spans rounds does it here.+ const endRound = () => {+ for (const part of toolCallParts) {+ toolPartIdByBackendId.delete(part.toolCallId);+ }+ };+ return { feed, parts: toolCallParts, resolveToolPartId, endRound };+ `;+ const js = ts.transpileModule(`const mintPartIds = ${mintPartIds};` + body, {+ compilerOptions: { target: ts.ScriptTarget.ES2022 },+ }).outputText;+ return new Function(+ "splitTopLevelJsonObjects",+ "createBoundaryScan",+ "findStreamedToolCallPartIndex",+ "mintStreamedToolCallId",+ "bindStreamedToolCallCard",+ "resolveToolCallPartId",+ js,+ )(+ splitTopLevelJsonObjects,+ createBoundaryScan,+ findStreamedToolCallPartIndex,+ mintStreamedToolCallId,+ bindStreamedToolCallCard,+ resolveToolCallPartId,+ ) as {+ feed: (batch: DeltaCall[], finished?: boolean) => boolean;+ parts: LoopPart[];+ resolveToolPartId: (backendId: string) => string;+ endRound: () => void;+ };+}++function run(batches: DeltaCall[][]): LoopPart[] {+ const stream = makeStream();+ for (const batch of batches) stream.feed(batch);+ return stream.parts;+}++const shape = (parts: LoopPart[]) => parts.map((p) => [p.toolName, p.argsText]);++test("the stream from #9807 becomes one call per JSON object", () => {+ const parts = run([+ [{ index: 0, function: { name: "url", arguments: '{"url":"a"}' } }],+ [{ index: 0, function: { name: "url", arguments: '{"url":"b"}' } }],+ [{ index: 0, function: { name: "query", arguments: '{"q":"c"}' } }],+ [{ index: 0, function: { name: "url", arguments: '{"url":"d"}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["url", '{"url":"a"}'],+ ["url", '{"url":"b"}'],+ ["query", '{"q":"c"}'],+ ["url", '{"url":"d"}'],+ ]);+ const ids = parts.map((p) => p.toolCallId);+ assert.equal(new Set(ids).size, ids.length, `ids collide: ${ids.join(",")}`);+});++test("a call at another index is not overwritten when a slot splits", () => {+ // Writing back through the old position deleted the neighbour.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ index: 1, function: { name: "beta", arguments: '{"b":2}' } }],+ [{ index: 0, function: { name: "gamma", arguments: '{"c":3}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["beta", '{"b":2}'],+ ["gamma", '{"c":3}'],+ ]);+});++test("a call opened third reads third, whichever index it reused", () => {+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ index: 1, function: { name: "beta", arguments: '{"b":2}' } }],+ [{ index: 1, function: { name: "gamma", arguments: '{"c":3}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["beta", '{"b":2}'],+ ["gamma", '{"c":3}'],+ ]);+});++test("several objects inside one fragment split just the same", () => {+ const parts = run([+ [+ {+ index: 0,+ function: { name: "url", arguments: '{"url":"a"}{"url":"b"}' },+ },+ ],+ ]);++ assert.deepEqual(shape(parts), [+ ["url", '{"url":"a"}'],+ ["url", '{"url":"b"}'],+ ]);+});++test("a call born from a split is state, so it does not wait to publish", () => {+ const stream = makeStream();+ assert.equal(+ stream.feed([+ { index: 0, function: { name: "alpha", arguments: '{"a":1}' } },+ ]),+ true,+ );+ // Or the pacing gate holds it back and Stop persists without it.+ assert.equal(+ stream.feed([+ { index: 0, function: { name: "beta", arguments: '{"b":2}' } },+ ]),+ true,+ );+});++test("an ordinary fragmented call is still one call", () => {+ assert.deepEqual(+ shape(+ run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":' } }],+ [{ index: 0, function: { arguments: "1" } }],+ [{ index: 0, function: { arguments: "}" } }],+ ]),+ ),+ [["alpha", '{"a":1}']],+ );+});++test("a stream that carries ids is left exactly as it was", () => {+ const parts = run([+ [+ {+ id: "call_a",+ index: 0,+ function: { name: "alpha", arguments: '{"a":' },+ },+ {+ id: "call_b",+ index: 1,+ function: { name: "beta", arguments: '{"b":' },+ },+ ],+ [+ { id: "call_a", index: 0, function: { arguments: "1}" } },+ { id: "call_b", index: 1, function: { arguments: "2}" } },+ ],+ ]);++ assert.deepEqual(+ parts.map((p) => [p.toolCallId, p.toolName, p.argsText]),+ [+ ["call_a", "alpha", '{"a":1}'],+ ["call_b", "beta", '{"b":2}'],+ ],+ );+});++test("an id stamped on a later fragment still claims its own slot", () => {+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":' } }],+ [{ id: "call_a", index: 0, function: { arguments: "1}" } }],+ ]);++ assert.deepEqual(+ parts.map((p) => [p.toolCallId, p.argsText]),+ [["call_a", '{"a":1}']],+ );+});++test("a fragment repeating the slot's id continues that call", () => {+ // llama-server grows the name across deltas, so opening a call here gives+ // two cards one id.+ const parts = run([+ [{ id: "call_a", index: 0, function: { name: "web", arguments: '{"q":"x"}' } }],+ [{ id: "call_a", index: 0, function: { name: "web_search" } }],+ ]);++ assert.deepEqual(shape(parts), [["web_search", '{"q":"x"}']]);+ assert.equal(parts.length, 1);+});++test("whitespace chunked after a closing brace is not a new call", () => {+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ index: 0, function: { name: "alpha", arguments: " " } }],+ [{ index: 0, function: { name: "alpha", arguments: '{"b":2}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1} '],+ ["alpha", '{"b":2}'],+ ]);+});++test("a late id claims the call still being written, never a closed one", () => {+ // The id has to land on the half-written third call; appending to either of+ // the closed ones is the gluing this change is about.+ const parts = run([+ [+ {+ index: 0,+ function: { name: "alpha", arguments: '{"a":1}{"b":2}{"c":' },+ },+ ],+ [{ id: "call_c", index: 0, function: { arguments: "3}" } }],+ ]);++ assert.deepEqual(+ parts.map((p) => [p.toolCallId, p.argsText]),+ [+ ["tool_call_0", '{"a":1}'],+ ["tool_call_1", '{"b":2}'],+ ["call_c", '{"c":3}'],+ ],+ );+});++test("a name-only delta for the next call does not rename the finished one", () => {+ // The name arrives before its arguments, so there is nothing to split on.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ index: 0, function: { name: "beta" } }],+ [{ index: 0, function: { arguments: '{"b":2}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["beta", '{"b":2}'],+ ]);+ const ids = parts.map((p) => p.toolCallId);+ assert.equal(new Set(ids).size, ids.length, `ids collide: ${ids.join(",")}`);+});++test("an id arriving after one closed object opens a call, not a claim", () => {+ // A late id claims its slot only while that call is still being written.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ id: "call_b", index: 0, function: { name: "beta", arguments: '{"b":2}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["beta", '{"b":2}'],+ ]);+});++test("a late id opens its own call when every call in the slot has closed", () => {+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}{"b":2}' } }],+ [+ {+ id: "call_c",+ index: 0,+ function: { name: "gamma", arguments: '{"c":3}' },+ },+ ],+ ]);++ assert.deepEqual(+ parts.map((p) => [p.toolCallId, p.argsText]),+ [+ ["tool_call_0", '{"a":1}'],+ ["tool_call_1", '{"b":2}'],+ ["call_c", '{"c":3}'],+ ],+ );+});++test("an opening delta after a closed call does not claim it", () => {+ // The conventional opening delta: id and name, empty arguments. Landing it+ // on the finished card glues the arguments that follow onto that one.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ id: "call_b", index: 0, function: { name: "beta", arguments: "" } }],+ [{ id: "call_b", index: 0, function: { arguments: '{"b":2}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["beta", '{"b":2}'],+ ]);+ const ids = parts.map((p) => p.toolCallId);+ assert.equal(ids[1], "call_b");+ assert.equal(new Set(ids).size, ids.length, `ids collide: ${ids.join(",")}`);+});++test("a name held for the next call grows across deltas", () => {+ // OpenAI streams "web" then "_search"; llama-server resends "web" then+ // "web_search". Last-write-wins opens the call as "_search".+ for (const fragments of [+ ["web", "_search"],+ ["web", "web_search"],+ ]) {+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ ...fragments.map((name) => [{ index: 0, function: { name } }]),+ [{ index: 0, function: { arguments: '{"q":"x"}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["web_search", '{"q":"x"}'],+ ]);+ }+});++test("whitespace carrying the repeated name is not the next call", () => {+ // A repeated name with the trailing whitespace chunked separately is still+ // that call's; parking it merged the two into "alphabeta".+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ index: 0, function: { name: "alpha", arguments: " " } }],+ [{ index: 0, function: { name: "beta", arguments: '{"b":2}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1} '],+ ["beta", '{"b":2}'],+ ]);+});++test("metadata announced with a name waits for that call", () => {+ // Gemini stows the thought signature for the call being announced, so a+ // name-only delta carrying one describes the next call, not the closed one.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [+ {+ index: 0,+ function: { name: "beta" },+ extra_content: { google: { thought_signature: "SIG" } },+ },+ ],+ [{ index: 0, function: { arguments: '{"b":2}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["beta", '{"b":2}'],+ ]);+ assert.equal(parts[0].extra_content, undefined);+ assert.deepEqual(parts[1].extra_content, {+ google: { thought_signature: "SIG" },+ });+});++test("the resumable scan agrees with scanning from the start", () => {+ // Only safe while it agrees with restarting, for every chunking.+ const pieces = [+ ..."{}\"\\ abc:,1[]".split(""),+ '\\"',+ '"a"',+ "NaN",+ "\n",+ "\r\n",+ "\t",+ '{"a":1}',+ "}{",+ ];+ let seed = 20260827;+ const next = (n: number) => {+ seed = (seed * 1103515245 + 12345) % 2147483648;+ return seed % n;+ };+ for (let trial = 0; trial < 4000; trial += 1) {+ let text = "";+ for (let i = next(17); i > 0; i -= 1) text += pieces[next(pieces.length)];+ const scan = createBoundaryScan();+ let cut = 0;+ let result = scan.feed("");+ while (cut < text.length) {+ cut = Math.min(text.length, cut + 1 + next(4));+ result = scan.feed(text.slice(0, cut));+ }+ assert.deepEqual(result, splitTopLevelJsonObjects(text), text);+ }+});++test("one argument streamed a character at a time is scanned once", () => {+ // Rescanning per fragment made a 20 KB argument cost over a second on the+ // thread that paints the stream. Counted, not timed: a resumable scan+ // parses once per object however many deltas it arrived in, a restarting+ // one parses every closed object again on every delta. A wall-clock ratio+ // agreed, with too little margin for a loaded runner.+ const parses = (size: number, feed: (text: string) => unknown): number => {+ const real = JSON.parse;+ let calls = 0;+ (JSON as { parse: typeof JSON.parse }).parse = ((+ text: string,+ reviver?: unknown,+ ) => {+ calls += 1;+ return (real as (t: string, r?: unknown) => unknown)(text, reviver);+ }) as typeof JSON.parse;+ try {+ const payload = '{"a":1}{"code":"' + "x".repeat(size) + '"}';+ let text = "";+ for (const ch of payload) {+ text += ch;+ feed(text);+ }+ return calls;+ } finally {+ (JSON as { parse: typeof JSON.parse }).parse = real;+ }+ };++ for (const size of [500, 2000]) {+ const scan = createBoundaryScan();+ assert.equal(+ parses(size, (text) => scan.feed(text)),+ 2,+ "the scan is parsing more than once per object it closes",+ );+ }++ // The shape it replaces, measured the same way, so the test says what it+ // guards against rather than only asserting a number.+ const restarting = parses(2000, splitTopLevelJsonObjects);+ assert.ok(+ restarting > 2000,+ `restarting from the beginning parsed ${restarting} times, so this test is no longer measuring the difference it was written for`,+ );++ // And the loop still reads it as one call, whatever the scan costs.+ const stream = makeStream();+ const payload = '{"code":"' + "x".repeat(400) + '"}';+ stream.feed([{ index: 0, function: { name: "write", arguments: "" } }]);+ for (const ch of payload) {+ stream.feed([{ index: 0, function: { arguments: ch } }]);+ }+ assert.deepEqual(shape(stream.parts), [["write", payload]]);+});++test("metadata arriving alone stays on the call that closed", () => {+ // No name, so nothing announces another call: the signature is this one's.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ index: 0, extra_content: { google: { thought_signature: "SIG" } } }],+ ]);++ assert.deepEqual(shape(parts), [["alpha", '{"a":1}']]);+ assert.deepEqual(parts[0].extra_content, {+ google: { thought_signature: "SIG" },+ });+});++test("a name resent or grown after a call closed invents nothing", () => {+ // Indistinguishable from a second no-argument call to the same tool, so+ // take the reading that does not run a tool twice. A grown name announces+ // the next call, so its provisional card is reaped unfilled at the boundary.+ for (const resent of ["alpha", "alpha_long"]) {+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }]);+ stream.feed([{ index: 0, function: { name: resent } }]);+ stream.feed([], true);+ assert.deepEqual(shape(stream.parts), [["alpha", '{"a":1}']]);+ }+});++test("an argument fragment that is not a string does not abort the stream", () => {+ // llama-server has shipped `arguments` as a decoded object rather than the+ // string the API specifies, and the chunk is cast rather than validated.+ const parts = run([+ [+ {+ index: 0,+ function: {+ name: "alpha",+ arguments: { a: 1 } as unknown as string,+ },+ },+ ],+ [{ index: 0, function: { arguments: '{"a":1}' } }],+ ]);++ assert.deepEqual(shape(parts), [["alpha", '{"a":1}']]);+});++test("a fragment that does not open an object does not open a call", () => {+ // A next call begins with its own "{"; forking on anything else made a stray+ // scalar suffix run the tool twice.+ const parts = run([+ [{ index: 0, function: { name: "q", arguments: '{"query":"a"}' } }],+ [{ index: 0, function: { name: "q", arguments: '"b"' } }],+ ]);++ assert.deepEqual(shape(parts), [["q", '{"query":"a"}"b"']]);+});++test("metadata announced with a name is merged, not replaced", () => {+ // Two fields of one call, and replay needs both.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [+ {+ index: 0,+ function: { name: "beta" },+ extra_content: { google: { thought_signature: "SIG" } },+ },+ ],+ [+ {+ index: 0,+ function: { arguments: '{"b":2}' },+ extra_content: { openai: { x: 1 } },+ },+ ],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["beta", '{"b":2}'],+ ]);+ assert.deepEqual(parts[1].extra_content, {+ google: { thought_signature: "SIG" },+ openai: { x: 1 },+ });+});++test("an MCP tool that really takes _raw keeps it", () => {+ // The marker is the pairing, not the key: `_raw` is not reserved.+ assert.equal(+ toolCallReplayArguments('{"url":"a"}{"url":"b"}', {+ _raw: '{"url":"a"}{"url":"b"}',+ }),+ "{}",+ );+ assert.equal(+ toolCallReplayArguments(undefined, { _raw: "a legitimate value" }),+ '{"_raw":"a legitimate value"}',+ );+ assert.equal(+ toolCallReplayArguments('{"url":"a"}{"url":"b"}', { _raw: 42 }),+ '{"_raw":42}',+ );+});++test("a name waiting for arguments does not cross a turn boundary", () => {+ // The next turn restarts the delta index at 0.+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "A", arguments: '{"a":1}' } }]);+ // The name-only delta rides the same chunk as finish_reason, which is how a+ // clear placed before the deltas let the name through.+ stream.feed([{ index: 0, function: { name: "B" } }], true);+ stream.feed([{ index: 0, function: { name: "C", arguments: '{"c":3}' } }]);++ assert.deepEqual(shape(stream.parts), [+ ["A", '{"a":1}'],+ ["C", '{"c":3}'],+ ]);+});++test("metadata on several name fragments is merged, not replaced", () => {+ // The name accumulates across fragments, so the metadata has to as well.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [+ {+ index: 0,+ function: { name: "web" },+ extra_content: { google: { thought_signature: "SIG" } },+ },+ ],+ [{ index: 0, function: { name: "_search" }, extra_content: { seq: 2 } }],+ [{ index: 0, function: { arguments: '{"q":1}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["web_search", '{"q":1}'],+ ]);+ assert.deepEqual(parts[1].extra_content, {+ google: { thought_signature: "SIG" },+ seq: 2,+ });+});++test("a resent name does not rename the call it closed", () => {+ // Concatenating gave "alphabeta", which the backend never executes. Once the+ // name is read as the closed call's, so is the metadata beside it.+ const stream = makeStream();+ stream.feed([+ {+ index: 0,+ function: { name: "alpha", arguments: '{"a":1}' },+ extra_content: { own: "A" },+ },+ ]);+ stream.feed([+ { index: 0, function: { name: "alpha_long" }, extra_content: { resent: 1 } },+ ]);+ stream.feed([{ index: 0, function: { name: "beta", arguments: '{"b":2}' } }]);+ stream.feed([], true);+ const parts = stream.parts;++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["beta", '{"b":2}'],+ ]);+ assert.deepEqual(parts[0].extra_content, { own: "A", resent: 1 });+ assert.equal(parts[1].extra_content, undefined);+});++test("an id stamped after the object closed claims that call", () => {+ // Reading the late id as another call left the finished one provisional.+ for (const late of [+ { id: "call_a", index: 0 },+ { id: "call_a", index: 0, function: { name: "alpha" } },+ ]) {+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [late],+ ]);+ assert.deepEqual(+ parts.map((p) => [p.toolCallId, p.toolName, p.argsText]),+ [["call_a", "alpha", '{"a":1}']],+ );+ }++ const opened = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ id: "call_b", index: 0, function: { name: "beta", arguments: "" } }],+ [{ id: "call_b", index: 0, function: { arguments: '{"b":2}' } }],+ ]);+ assert.deepEqual(shape(opened), [+ ["alpha", '{"a":1}'],+ ["beta", '{"b":2}'],+ ]);+});++test("a new name arriving with whitespace opens its own call", () => {+ // A different name opens the next call rather than renaming the finished+ // one, which gave "alphabeta" and an unnamed second card. The whitespace+ // rides the announcing delta and is valid JSON, so both still parse.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ index: 0, function: { name: "beta", arguments: " " } }],+ [{ index: 0, function: { arguments: '{"b":2}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["beta", ' {"b":2}'],+ ]);+});++test("a call announced by name is placed where it was announced", () => {+ // The backend orders by when a call was announced, so a card appended where+ // its arguments turned up reads C before B while the backend runs B first.+ const parts = run([+ [{ index: 0, function: { name: "A", arguments: '{"a":1}' } }],+ [{ index: 0, function: { name: "B" } }],+ [{ index: 1, function: { name: "C", arguments: '{"c":3}' } }],+ [{ index: 0, function: { arguments: '{"b":2}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["A", '{"a":1}'],+ ["B", '{"b":2}'],+ ["C", '{"c":3}'],+ ]);+});++test("a late id claims the last call a bundled delta opened", () => {+ // A provider bundling several calls can stamp the last one's real id later;+ // marking that card owned sent the id to a third, empty card.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}{"b":2}' } }],+ [{ id: "call_b", index: 0 }],+ ]);++ assert.deepEqual(+ parts.map((p) => [p.toolCallId, p.toolName, p.argsText]),+ [+ ["tool_call_0", "alpha", '{"a":1}'],+ ["call_b", "alpha", '{"b":2}'],+ ],+ );+});++test("two calls announced at once keep the order they were announced in", () => {+ // Both record the same position, so splicing at it put the later first.+ const parts = run([+ [{ index: 0, function: { name: "A", arguments: '{"a":1}' } }],+ [{ index: 1, function: { name: "B", arguments: '{"b":1}' } }],+ [{ index: 0, function: { name: "C" } }],+ [{ index: 1, function: { name: "D" } }],+ [{ index: 0, function: { arguments: '{"c":1}' } }],+ [{ index: 1, function: { arguments: '{"d":1}' } }],+ ]);++ assert.deepEqual(+ parts.map((p) => p.toolName),+ ["A", "B", "C", "D"],+ );+});++test("a rejected resend gives up its place too", () => {+ // The place goes with the announcement, and a name read as a resent+ // announced nothing, so the call that opens takes its own arrival.+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "A", arguments: '{"a":1}' } }]);+ stream.feed([{ index: 0, function: { name: "A_long" } }]);+ stream.feed([{ index: 1, function: { name: "C", arguments: '{"c":1}' } }]);+ stream.feed([{ index: 0, function: { name: "B", arguments: '{"b":1}' } }]);+ stream.feed([], true);+ const parts = stream.parts;++ assert.deepEqual(+ parts.map((p) => p.toolName),+ ["A", "C", "B"],+ );+});++test("a catalog holding both web and web_search splits either way round", () => {+ // Both are in Studio's own catalog, so a shared prefix is no evidence either+ // way; reading it as evidence swallowed the second announcement.+ for (const [first, second] of [+ ["web_search", "web"],+ ["web", "web_search"],+ ]) {+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: first, arguments: '{"a":1}' } }]);+ stream.feed([{ index: 0, function: { name: second } }]);+ stream.feed([{ index: 0, function: { arguments: '{"b":2}' } }]);+ stream.feed([], true);+ assert.deepEqual(shape(stream.parts), [+ [first, '{"a":1}'],+ [second, '{"b":2}'],+ ]);+ }+});++test("a name bringing an object over an announcement is the next call", () => {+ // An announcement has no object to close, so the next call's name grew into+ // it: "alpha_longbeta" and "zetabeta" match no tool.+ for (const announced of ["alpha_long", "zeta"]) {+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }]);+ stream.feed([{ index: 0, function: { name: announced } }]);+ stream.feed([{ index: 0, function: { name: "beta", arguments: '{"b":2}' } }]);+ stream.feed([], true);+ assert.deepEqual(shape(stream.parts), [+ ["alpha", '{"a":1}'],+ ["beta", '{"b":2}'],+ ]);+ }+});++test("a dropped card gives its id back to the next round", () => {+ // The backend never reserves the filtered fork's card id, so holding it here+ // made the next round mint tool_call_2 against the backend's tool_call_1.+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "alpha", arguments: '{"a":1}{' } }]);+ stream.feed([], true);+ stream.feed([{ index: 0, function: { name: "beta", arguments: '{"b":2}' } }]);++ assert.deepEqual(+ stream.parts.map((p) => [p.toolCallId, p.toolName]),+ [+ ["tool_call_0", "alpha"],+ ["tool_call_1", "beta"],+ ],+ );+});++test("a provider claiming a minted id displaces the card holding it", () => {+ // tool_call_<n> is not reserved to Unsloth. Resolving the claim through the+ // id-less call's binding merged the two into one card, losing a call. The+ // backend reserves provider ids before minting; do the same on the claim.+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }]);+ stream.feed([+ { id: "tool_call_0", index: 1, function: { name: "beta", arguments: '{"b":2}' } },+ ]);++ assert.deepEqual(+ stream.parts.map((p) => [p.toolCallId, p.toolName, p.argsText]),+ [+ ["tool_call_1", "alpha", '{"a":1}'],+ ["tool_call_0", "beta", '{"b":2}'],+ ],+ );+});++test("a card taking a late provider id gives its minted id back", () => {+ // The backend never reserves a minted id for a call the provider went on to+ // name, so holding it made the next call at that index mint tool_call_1+ // against the backend's tool_call_0.+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }]);+ stream.feed([{ id: "call_a", index: 0, function: { arguments: "" } }]);+ stream.feed([{ index: 0, function: { name: "beta", arguments: '{"b":2}' } }]);++ assert.deepEqual(+ stream.parts.map((p) => [p.toolCallId, p.toolName]),+ [+ ["call_a", "alpha"],+ ["tool_call_0", "beta"],+ ],+ );+});++test("a claim on a split-born card renumbers every minted card", () => {+ // A split marks every born card but the last _has_stable_id, so reading+ // that as provider-owned let the claim merge them: "alphabeta", arguments+ // glued. The backend reserves the claim then numbers the id-less calls in+ // order, so matching it means renumbering all of them.+ const stream = makeStream();+ stream.feed([+ { index: 0, function: { name: "alpha", arguments: '{"a":1}{"b":2}{"c":3}' } },+ ]);+ stream.feed([+ { id: "tool_call_1", index: 1, function: { name: "beta", arguments: '{"d":4}' } },+ ]);++ assert.deepEqual(+ stream.parts.map((p) => [p.toolCallId, p.toolName, p.argsText]),+ [+ ["tool_call_0", "alpha", '{"a":1}'],+ ["tool_call_2", "alpha", '{"b":2}'],+ ["tool_call_3", "alpha", '{"c":3}'],+ ["tool_call_1", "beta", '{"d":4}'],+ ],+ );+});++test("a born call carries only the metadata of the delta that opened it", () => {+ // The merged fields belong to the call the slot was holding; carried onto a+ // born call they put one call's signature on another, and Gemini validates a+ // signature against the functionCall part it was returned on.+ const stream = makeStream();+ stream.feed([+ { index: 0, function: { name: "alpha" }, extra_content: { parked: 1 } },+ ]);+ stream.feed([+ {+ index: 0,+ function: { arguments: '{"a":1}{"b":2}' },+ extra_content: { delta: 2 },+ },+ ]);++ assert.deepEqual(shape(stream.parts), [+ ["alpha", '{"a":1}'],+ ["alpha", '{"b":2}'],+ ]);+ assert.deepEqual(stream.parts[0].extra_content, { parked: 1 });+ assert.deepEqual(stream.parts[1].extra_content, { delta: 2 });+});++test("a claim in a later round leaves an earlier round's card alone", () => {+ // The backend's card ledger is append-only, so a card from a finished round+ // keeps its number however a later round spells its ids. Renumbering it put+ // the two sides one apart from the third round on, and the backend's events+ // for the renamed card reached whatever had taken its old id.+ const stream = makeStream(true);+ stream.feed([{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }]);+ stream.feed([], true);+ stream.endRound();+ stream.feed([+ { id: "tool_call_0", index: 0, function: { name: "beta", arguments: '{"b":2}' } },+ ]);+ stream.feed([], true);+ stream.endRound();+ stream.feed([{ index: 0, function: { name: "gamma", arguments: '{"c":3}' } }]);++ assert.deepEqual(+ stream.parts.map((p) => [p.toolCallId, p.toolName]),+ [+ ["tool_call_0", "alpha"],+ ["tool_call_0:uuid-1", "beta"],+ ["tool_call_1", "gamma"],+ ],+ );+});++test("a dropped card gives back the provider id that aliased it", () => {+ // A card that took a late id answers to a run-unique part id, so the id the+ // provider sent is a second key pointing at it. Releasing only the part id+ // left tool_call_1 reserved, and the next round minted tool_call_2 where+ // the backend minted tool_call_1.+ const stream = makeStream(true);+ stream.feed([{ index: 0, function: { name: "alpha", arguments: '{}{"x":' } }]);+ stream.feed([{ id: "tool_call_1", index: 0, function: { arguments: "" } }]);+ stream.feed([], true);+ stream.endRound();+ stream.feed([{ index: 0, function: { name: "beta", arguments: '{"b":2}' } }]);++ assert.deepEqual(+ stream.parts.map((p) => [p.toolCallId, p.toolName]),+ [+ ["tool_call_0", "alpha"],+ ["tool_call_1", "beta"],+ ],+ );+});++test("a card that never got a name is dropped when the turn ends", () => {+ // _normalized_call rejects a nameless call before it reserves a card id, so+ // a card kept for one holds a number the backend gives the next round, and+ // that round's events land on the blank card instead.+ const stream = makeStream();+ stream.feed([{ index: 0, function: { arguments: '{"a":1}' } }]);+ assert.equal(stream.parts.length, 1);+ stream.feed([], true);+ assert.equal(stream.parts.length, 0);++ stream.feed([{ index: 0, function: { name: "beta", arguments: '{"b":2}' } }]);+ assert.deepEqual(+ stream.parts.map((p) => [p.toolCallId, p.toolName]),+ [["tool_call_0", "beta"]],+ );+});++test("a claim that turns out not to be a call gives the number back", () => {+ // The displacement happens as the claim lands, but a nameless call is not a+ // call: the backend reserves nothing for it and keeps tool_call_0 for the+ // valid one, so a card left at tool_call_1 is one its execution events miss.+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }]);+ stream.feed([{ id: "tool_call_0", index: 1, function: { arguments: '{"b":2}' } }]);+ assert.equal(stream.parts.length, 2);+ stream.feed([], true);++ assert.deepEqual(+ stream.parts.map((p) => [p.toolCallId, p.toolName]),+ [["tool_call_0", "alpha"]],+ );+});++test("a repeated name's metadata waits for the call it announced", () => {+ // The same tool twice on one slot, the second announced by a name-only+ // delta carrying its own signature. Merging it where it landed overwrote+ // the closed call's and left the new one unsigned, and Gemini validates a+ // signature against the call it is replayed on.+ const stream = makeStream();+ stream.feed([+ {+ index: 0,+ function: { name: "lookup", arguments: '{"q":"a"}' },+ extra_content: { sig: "A" },+ },+ ]);+ stream.feed([+ { index: 0, function: { name: "lookup" }, extra_content: { sig: "B" } },+ ]);+ stream.feed([{ index: 0, function: { arguments: '{"q":"b"}' } }]);+ stream.feed([], true);++ assert.deepEqual(shape(stream.parts), [+ ["lookup", '{"q":"a"}'],+ ["lookup", '{"q":"b"}'],+ ]);+ assert.deepEqual(stream.parts[0].extra_content, { sig: "A" });+ assert.deepEqual(stream.parts[1].extra_content, { sig: "B" });+});++test("a repeated name that announced nothing keeps its metadata", () => {+ // No object followed, so the repeated name really was that call's resent and+ // the signature riding it is that call's too.+ const stream = makeStream();+ stream.feed([+ {+ index: 0,+ function: { name: "lookup", arguments: '{"q":"a"}' },+ extra_content: { own: 1 },+ },+ ]);+ stream.feed([+ { index: 0, function: { name: "lookup" }, extra_content: { sig: "B" } },+ ]);+ stream.feed([], true);++ assert.deepEqual(shape(stream.parts), [["lookup", '{"q":"a"}']]);+ assert.deepEqual(stream.parts[0].extra_content, { own: 1, sig: "B" });+});++test("parked metadata follows the card a late id renames", () => {+ // The signature waits under the id the card was minted with, so it has to+ // move with the card or the turn-end sweep drops what the backend keeps.+ const stream = makeStream(true);+ stream.feed([+ {+ index: 0,+ function: { name: "lookup", arguments: '{"q":"a"}' },+ extra_content: { sig: "A" },+ },+ ]);+ stream.feed([+ { index: 0, function: { name: "lookup" }, extra_content: { sig: "B" } },+ ]);+ stream.feed([{ index: 0, id: "call_x", function: { arguments: "" } }], true);++ assert.deepEqual(shape(stream.parts), [["lookup", '{"q":"a"}']]);+ assert.deepEqual(stream.parts[0].extra_content, { sig: "B" });+ assert.equal(stream.parts[0].toolCallId, "call_x:uuid-1");+});++test("parked metadata follows the card a claim renumbers", () => {+ // Same entry, other mover: a provider claiming the spelling a minted card+ // holds renumbers every minted card in the round.+ const stream = makeStream(true);+ stream.feed([+ {+ index: 0,+ function: { name: "lookup", arguments: '{"q":"a"}' },+ extra_content: { sig: "A" },+ },+ ]);+ stream.feed([+ { index: 0, function: { name: "lookup" }, extra_content: { sig: "B" } },+ ]);+ stream.feed(+ [{ index: 1, id: "tool_call_0", function: { name: "beta", arguments: '{"b":2}' } }],+ true,+ );++ assert.deepEqual(shape(stream.parts), [+ ["lookup", '{"q":"a"}'],+ ["beta", '{"b":2}'],+ ]);+ assert.deepEqual(stream.parts[0].extra_content, { sig: "B" });+ assert.equal(stream.parts[1].extra_content, undefined);+});++test("a stable id naming a longer tool opens its own call", () => {+ // Reading "web_search" as "web" grown gave the id to the completed card.+ const parts = run([+ [{ index: 0, function: { name: "web", arguments: '{"a":1}' } }],+ [{ id: "call_b", index: 0, function: { name: "web_search", arguments: "" } }],+ [{ id: "call_b", index: 0, function: { arguments: '{"b":2}' } }],+ ]);++ assert.deepEqual(+ parts.map((p) => [p.toolCallId, p.toolName, p.argsText]),+ [+ ["tool_call_0", "web", '{"a":1}'],+ ["call_b", "web_search", '{"b":2}'],+ ],+ );+});++test("a fork whose object never closed is dropped when the turn ends", () => {+ // A stream stopping after `{"a":1}{` is not marked truncated, so the lone+ // brace persists as a card nothing completes. The backend holds it too.+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "a", arguments: '{"a":1}{' } }]);+ assert.deepEqual(shape(stream.parts), [+ ["a", '{"a":1}'],+ ["a", "{"],+ ]);+ stream.feed([], true);+ assert.deepEqual(shape(stream.parts), [["a", '{"a":1}']]);+});++test("a fork that does close its object is kept", () => {+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "a", arguments: '{"a":1}{' } }]);+ stream.feed([{ index: 0, function: { arguments: '"b":2}' } }]);+ stream.feed([], true);+ assert.deepEqual(shape(stream.parts), [+ ["a", '{"a":1}'],+ ["a", '{"b":2}'],+ ]);+});++test("metadata from a resent name goes to the call that runs", () => {+ // No call is invented for a name read as a resent, so its signature has+ // nowhere else to go and the card would replay without it.+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }]);+ stream.feed([+ { index: 0, function: { name: "alpha_long" }, extra_content: { sig: "s" } },+ ]);+ stream.feed([], true);++ assert.deepEqual(shape(stream.parts), [["alpha", '{"a":1}']]);+ assert.deepEqual(stream.parts[0].extra_content, { sig: "s" });+});++test("a provider-hosted tool event does not end the provider turn", () => {+ // Hosted events ride a whole chunk, `choices` and all, and leave the turn+ // open; Unsloth's are bare {"type": "tool_start"}.+ const guarded = liftBetween(+ "the hosted-event guard",+ "const toolEvent = (",+ "// Deep Research is an ordinary tool",+ );+ assert.match(guarded, /if \(!chunk\.choices\) \{\s*endProviderTurn\(\);/);+});++test("a late id does not rescue a fork whose object never closed", () => {+ // _call_is_finished holds the fork back on its arguments alone, so a card+ // kept because an id reached it is one no tool_start or tool_end can reach.+ const stream = makeStream();+ stream.feed([{ index: 0, function: { name: "a", arguments: '{}{"x":' } }]);+ stream.feed([{ id: "call_z", index: 0, function: { arguments: "" } }]);+ assert.deepEqual(+ stream.parts.map((p) => [p.toolCallId, p.argsText]),+ [+ ["tool_call_0", "{}"],+ ["call_z", '{"x":'],+ ],+ );+ stream.feed([], true);+ assert.deepEqual(shape(stream.parts), [["a", "{}"]]);+});++test("only the announced call takes the place reserved for it", () => {+ // B was announced before C opened. The calls B's arguments introduce were+ // never announced, so they are numbered as the arguments arrive.+ const parts = run([+ [{ index: 0, function: { name: "A", arguments: '{"a":1}' } }],+ [{ index: 0, function: { name: "B" } }],+ [{ index: 1, function: { name: "C", arguments: '{"c":1}' } }],+ [{ index: 0, function: { arguments: '{"b":1}{"b2":2}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["A", '{"a":1}'],+ ["B", '{"b":1}'],+ ["C", '{"c":1}'],+ ["B", '{"b2":2}'],+ ]);+});++test("the empty status between rounds ends the provider turn", () => {+ // A round of only disabled calls emits no tool_start, and a [DONE] upstream+ // sends no finish_reason, so this is the only boundary there is.+ const branch = liftBetween(+ "the tool_status branch",+ "const toolStatusText = (",+ "if (chunk.context_truncated) {",+ );+ assert.match(branch, /if \(!toolStatusText\) \{\s*endProviderTurn\(\);/);+});++test("the announced call keeps its own metadata when its delta splits", () => {+ // The parked signature is B's; the one riding the arguments belongs to the+ // call that delta closes, the last. The backend divides them the same way.+ const parts = run([+ [{ index: 0, function: { name: "A", arguments: '{"a":1}' } }],+ [{ index: 0, function: { name: "B" }, extra_content: { sig: "parked" } }],+ [+ {+ index: 0,+ function: { arguments: '{"b":1}{"b2":2}' },+ extra_content: { sig: "incoming" },+ },+ ],+ ]);++ assert.deepEqual(shape(parts), [+ ["A", '{"a":1}'],+ ["B", '{"b":1}'],+ ["B", '{"b2":2}'],+ ]);+ assert.deepEqual(parts[1].extra_content, { sig: "parked" });+ assert.deepEqual(parts[2].extra_content, { sig: "incoming" });+});++test("a second call to the same tool keeps that tool's name", () => {+ // The index is reused with arguments alone, and a blank name is no tool:+ // the backend drops the call and the card here names nothing.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ index: 0, function: { arguments: '{"a":2}' } }],+ ]);++ assert.deepEqual(shape(parts), [+ ["alpha", '{"a":1}'],+ ["alpha", '{"a":2}'],+ ]);+});++test("a snapshot repeated to carry the id claims the call", () => {+ // Snapshot servers resend the whole call, so the id arrives on a verbatim+ // repeat and opening a second call runs the tool twice.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ id: "call_a", index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ ]);++ assert.deepEqual(+ parts.map((p) => [p.toolCallId, p.toolName, p.argsText]),+ [["call_a", "alpha", '{"a":1}']],+ );+});++test("a second call that differs anywhere still opens its own", () => {+ // The claim above is exact repeats only.+ const parts = run([+ [{ index: 0, function: { name: "alpha", arguments: '{"a":1}' } }],+ [{ id: "call_b", index: 0, function: { name: "alpha", arguments: '{"a":2}' } }],+ ]);++ assert.deepEqual(+ parts.map((p) => [p.toolCallId, p.argsText]),+ [+ ["tool_call_0", '{"a":1}'],+ ["call_b", '{"a":2}'],+ ],+ );+});++test("an id-less card answers to the id the backend mints for it", () => {+ // The backend addresses tool_start at tool_call_<n>. Without the binding,+ // resolveToolPartId mints "<id>:<uuid>" and no card is found: #9807's four+ // calls ended the turn holding eight cards.+ const stream = makeStream(true);+ for (const url of ["a", "b", "c", "d"]) {+ stream.feed([{ index: 0, function: { name: "fetch", arguments: `{"url":"${url}"}` } }]);+ }++ assert.deepEqual(+ stream.parts.map((p) => p.toolCallId),+ ["tool_call_0", "tool_call_1", "tool_call_2", "tool_call_3"],+ );++ const painted = stream.parts.length;+ for (const backendId of ["tool_call_0", "tool_call_1", "tool_call_2", "tool_call_3"]) {+ const partId = stream.resolveToolPartId(backendId);+ assert.equal(partId, backendId, `${backendId} did not resolve to its own card`);+ assert.ok(+ stream.parts.some((p) => p.toolCallId === partId),+ `${backendId} found no card to update`,+ );+ }+ assert.equal(stream.parts.length, painted, "a backend event opened a second card");+});++test("a call the provider named still resolves through the minted part id", () => {+ // Unchanged for streams that carry ids: still keyed on "<id>:<uuid>".+ const stream = makeStream(true);+ stream.feed([{ id: "call_a", index: 0, function: { name: "alpha", arguments: '{"a":1}' } }]);++ const partId = stream.resolveToolPartId("call_a");+ assert.match(partId, /^call_a:uuid-\d+$/);+ assert.deepEqual(+ stream.parts.map((p) => p.toolCallId),+ [partId],+ );+});++test("a provider id spelled tool_call_0 keeps its own card", () => {+ // tool_call_<n> is not reserved to Unsloth. A provider using that spelling+ // must not have the card taken from it by the id-less call beside it.+ const stream = makeStream(true);+ stream.feed([+ { id: "tool_call_0", index: 0, function: { name: "alpha", arguments: '{"a":1}' } },+ ]);+ stream.feed([{ index: 1, function: { name: "beta", arguments: '{"b":2}' } }]);++ const ids = stream.parts.map((p) => p.toolCallId);+ assert.equal(new Set(ids).size, ids.length, "two cards share one id");+ assert.ok(!ids.includes("tool_call_0"), "the minted id took the provider's spelling");+});++test("several calls opened by one delta each get their own card id", () => {+ // The cards are minted before any joins the parts array, so the ids have to+ // be reserved as they are handed out or all three collide.+ const stream = makeStream(true);+ stream.feed([+ { index: 0, function: { name: "fetch", arguments: '{"a":1}{"b":2}{"c":3}' } },+ ]);++ const ids = stream.parts.map((p) => p.toolCallId);+ assert.deepEqual(ids, ["tool_call_0", "tool_call_1", "tool_call_2"]);+ assert.equal(new Set(ids).size, 3);+});++test("the marker is only recognised by the text that proves it", () => {+ // Threads written before argsText was kept carry { _raw } with nothing to+ // compare against. Reading the value's shape instead would discard a real+ // _raw argument and gain nothing: the wrapped form raises no Extra data.+ const glued = '{"url":"https://example.com/1"}{"query":"search"}';+ assert.equal(toolCallReplayArguments(glued, { _raw: glued }), "{}");+ assert.equal(+ toolCallReplayArguments(undefined, { _raw: glued }),+ JSON.stringify({ _raw: glued }),+ );+ assert.equal(+ toolCallReplayArguments("", { _raw: glued }),+ JSON.stringify({ _raw: glued }),+ );+});++test("an empty _raw is an argument, not the marker", () => {+ // Both writers are guarded on non-empty text, so `{ _raw: "" }` beside an+ // empty argsText is a real argument. Equality alone read it as the marker.+ assert.equal(toolCallReplayArguments("", { _raw: "" }), '{"_raw":""}');+ assert.equal(toolCallReplayArguments(undefined, { _raw: "" }), '{"_raw":""}');+});++test("a tool that really takes a _raw parameter keeps it either way", () => {+ // _raw is not reserved and an MCP server's schema is its own.+ assert.equal(+ toolCallReplayArguments('{"_raw":"hello"}', { _raw: "hello" }),+ '{"_raw":"hello"}',+ );+ assert.equal(+ toolCallReplayArguments(undefined, { _raw: "hello" }),+ '{"_raw":"hello"}',+ );+ assert.equal(+ toolCallReplayArguments(undefined, { _raw: '{"one":1}' }),+ '{"_raw":"{\\"one\\":1}"}',+ );+});diff --git a/studio/frontend/tests/tool-call-delta-index.test.ts b/studio/frontend/tests/tool-call-delta-index.test.tsindex 53f18be74bd..5ac0802273e 100644--- a/studio/frontend/tests/tool-call-delta-index.test.ts+++ b/studio/frontend/tests/tool-call-delta-index.test.ts@@ -150,11 +150,15 @@ test("replayed arguments keep parsable text and fall back otherwise", () => {toolCallReplayArguments('{"query":"first"}', { query: "first" }),'{"query":"first"}',);+ // Two calls glued into one slot. The stream adapter no longer produces this+ // (see tests/parallel-tool-call-arguments.test.ts), but threads stored before+ // it split them still hold it, and `_raw` is the adapter's own marker rather+ // than a parameter any tool declares, so it must not go back on the wire.assert.equal(toolCallReplayArguments('{"query":"first"}{"query":"second"}', {_raw: '{"query":"first"}{"query":"second"}',}),- '{"_raw":"{\\"query\\":\\"first\\"}{\\"query\\":\\"second\\"}"}',+ "{}",);assert.equal(toolCallReplayArguments("", { query: "first" }),