Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(vibey): recover malformed streamed tool calls (#2927)

Call White43e8acf6a998
diff --git a/core/agent_core/agent/loop.py b/core/agent_core/agent/loop.py
index ba3f66873..83f6b70c8 100644
--- a/core/agent_core/agent/loop.py
+++ b/core/agent_core/agent/loop.py
@@ -172,6 +172,8 @@ logger = logging.getLogger(__name__)
#: The result of a call a hook's ``end`` or ``skip_tools`` settled without running it (C-3 section 3).
_SKIPPED = "[skipped by policy]"
+#: Safe model-visible result for a provider tool call whose arguments failed strict JSON validation.
+_MALFORMED_TOOL_CALL = "[tool call skipped: provider returned malformed JSON arguments; no tool was executed]"
class _Ended(Exception):
@@ -1050,7 +1052,12 @@ class Agent:
await self._audit("attempt", payload)
async def _commit_model_message(
- self, message: AssistantMessage, emit: Callable[..., Awaitable[None]], *, replied: bool
+ self,
+ message: AssistantMessage,
+ emit: Callable[..., Awaitable[None]],
+ *,
+ replied: bool,
+ keep_open: bool = False,
) -> tuple[ContextEntry, bool]:
"""Commit the response: its row, and whether it is final. Finality and queued-input admission share one lock
with the commit. ``replied`` is the run's decision whether it replies, which the store takes as given."""
@@ -1068,7 +1075,7 @@ class Agent:
# A completed reply the pending input took the final word from: one cut off at its output limit
# never completed, as ``final_outcome`` holds for a final.
superseded = bool(pending) and replied and message.stop_reason != "length"
- if final or failed:
+ if (final or failed) and not keep_open:
self._open = False
row = await self._response(message, final=final, replied=replied, superseded=superseded)
for item, is_steer in pending:
@@ -1093,61 +1100,85 @@ class Agent:
first_selection: Optional[ModelSelection] = selected
length_tool_retries = 0
while True:
+ malformed_recovery: Optional[tuple[ProviderError, dict[str, Tool], ContextEntry]] = None
async with self._model(system, emit, first_selection) as (terminal, tools):
first_selection = None
if isinstance(terminal, ProviderError):
- if terminal.partial is not None and not terminal.partial.content:
- # A usage-only partial is attempt data, never context; the ledger audits it.
- terminal = replace(terminal, partial=None)
- reason = (
- "context_exhausted"
- if terminal.kind == "overflow"
- else "aborted"
- if terminal.kind == "aborted"
- else "error"
- )
- decided = self._outcome.primary(reason)
- # ``aborted`` is the loop's own cancellation and ``overflow`` says our request was too large;
- # every other provider error is the source's.
- origin = "local" if terminal.kind in {"aborted", "overflow"} else "source"
- await self._error(
- emit, terminal.kind, terminal.message, origin, cause=decided, provider_error=terminal
- )
- if terminal.partial is not None:
- row = await self._response(terminal.partial, final=False)
- await emit(MessageCommitted, message_id=row.row_id, context_seq=row.context_seq, final=False)
- return reason
- message = terminal.message
- failed = message.stop_reason in {"error", "aborted"}
- if not (message.tool_calls and message.stop_reason == "length"):
- length_tool_retries = 0
- if failed:
- aborted = message.stop_reason == "aborted"
- decided = self._outcome.primary("aborted" if aborted else "error")
- await self._error(
- emit,
- message.stop_reason,
- message.error_message or f"Model stopped with {message.stop_reason}.",
- "local" if aborted else "source",
- cause=decided,
- )
- # Decided once: the empty-reply test, the row's type and settlement all take it (``has_reply``).
- replied = has_reply(message, generated_images=bool(self._generated_images))
- response, final = await self._commit_model_message(message, emit, replied=replied)
- empty_reply = final and not replied
- truncated_reply = final and message.stop_reason == "length"
- if empty_reply or truncated_reply:
- decided = self._outcome.primary("error")
- await self._error(
- emit,
- message.stop_reason if message.stop_reason in {"length", "refusal", "safety"} else "empty_response",
- message.error_message or (
- "The model reached its output limit before completing the reply."
- if truncated_reply else f"Model stopped with {message.stop_reason} without a reply."
- ),
- "source",
- cause=decided,
- )
+ malformed = terminal.malformed_tool_calls
+ if terminal.partial is not None and malformed:
+ calls_by_id = {call.id: call for call in terminal.partial.tool_calls}
+ malformed_by_id = {item.id: item for item in malformed}
+ if (
+ terminal.partial.stop_reason == "error"
+ and len(malformed_by_id) == len(malformed)
+ and all(
+ item.id in calls_by_id and calls_by_id[item.id].name == item.name
+ for item in malformed
+ )
+ ):
+ replied = has_reply(terminal.partial, generated_images=bool(self._generated_images))
+ response, _ = await self._commit_model_message(
+ terminal.partial, emit, replied=replied, keep_open=True
+ )
+ malformed_recovery = (terminal, tools, response)
+ if malformed_recovery is None:
+ if terminal.partial is not None and not terminal.partial.content:
+ # A usage-only partial is attempt data, never context; the ledger audits it.
+ terminal = replace(terminal, partial=None)
+ reason = (
+ "context_exhausted"
+ if terminal.kind == "overflow"
+ else "aborted"
+ if terminal.kind == "aborted"
+ else "error"
+ )
+ decided = self._outcome.primary(reason)
+ # ``aborted`` is the loop's own cancellation and ``overflow`` says our request was too large;
+ # every other provider error is the source's.
+ origin = "local" if terminal.kind in {"aborted", "overflow"} else "source"
+ await self._error(
+ emit, terminal.kind, terminal.message, origin, cause=decided, provider_error=terminal
+ )
+ if terminal.partial is not None:
+ row = await self._response(terminal.partial, final=False)
+ await emit(MessageCommitted, message_id=row.row_id, context_seq=row.context_seq, final=False)
+ return reason
+ if malformed_recovery is None:
+ message = terminal.message
+ failed = message.stop_reason in {"error", "aborted"}
+ if not (message.tool_calls and message.stop_reason == "length"):
+ length_tool_retries = 0
+ if failed:
+ aborted = message.stop_reason == "aborted"
+ decided = self._outcome.primary("aborted" if aborted else "error")
+ await self._error(
+ emit,
+ message.stop_reason,
+ message.error_message or f"Model stopped with {message.stop_reason}.",
+ "local" if aborted else "source",
+ cause=decided,
+ )
+ # Decided once: the empty-reply test, the row's type and settlement all take it (``has_reply``).
+ replied = has_reply(message, generated_images=bool(self._generated_images))
+ response, final = await self._commit_model_message(message, emit, replied=replied)
+ empty_reply = final and not replied
+ truncated_reply = final and message.stop_reason == "length"
+ if empty_reply or truncated_reply:
+ decided = self._outcome.primary("error")
+ await self._error(
+ emit,
+ message.stop_reason if message.stop_reason in {"length", "refusal", "safety"} else "empty_response",
+ message.error_message or (
+ "The model reached its output limit before completing the reply."
+ if truncated_reply else f"Model stopped with {message.stop_reason} without a reply."
+ ),
+ "source",
+ cause=decided,
+ )
+ if malformed_recovery is not None:
+ recovery_error, tools, response = malformed_recovery
+ message = recovery_error.partial
+ assert message is not None
# aclose failures are diagnostics, never a reason to discard the
# committed response or skip its tools. Primary errors precede them.
await self._flush_diagnostics(emit)
@@ -1158,6 +1189,50 @@ class Agent:
end = True
break
skip = skip or isinstance(decision, SkipTools)
+ if malformed_recovery is not None:
+ recovery_message = message
+ malformed_ids = {call.id for call in recovery_error.malformed_tool_calls}
+ skip_calls = {call_id: _MALFORMED_TOOL_CALL for call_id in malformed_ids}
+ terminate, step_end = await self._calls(
+ recovery_message.tool_calls,
+ response,
+ tools,
+ emit,
+ skip=_SKIPPED if skip or end else None,
+ skip_calls=skip_calls,
+ )
+ if end or step_end:
+ self._open = False
+ await self._error(
+ emit,
+ recovery_error.kind,
+ recovery_error.message,
+ "source",
+ cause=False,
+ provider_error=recovery_error,
+ )
+ return "ended_by_hook"
+ if terminate:
+ self._open = False
+ await self._error(
+ emit,
+ recovery_error.kind,
+ recovery_error.message,
+ "source",
+ cause=False,
+ provider_error=recovery_error,
+ )
+ return "completed"
+ await self._error(
+ emit,
+ recovery_error.kind,
+ recovery_error.message,
+ "source",
+ cause=True,
+ provider_error=recovery_error,
+ )
+ await self._drain_steers(emit)
+ return "error"
if message.tool_calls and message.stop_reason != "tool_use":
reason = message.stop_reason
skip_reason = (
@@ -1268,9 +1343,11 @@ class Agent:
emit: Callable[..., Awaitable[None]],
*,
skip: Optional[str] = None,
+ skip_calls: Optional[Mapping[str, str]] = None,
) -> tuple[bool, bool]:
"""A response's calls, group after group (C-3 sections 2 and 3): whether a result terminates the run, and
- whether a hook ended it. ``skip`` settles every call with that text instead, one at a time, running none."""
+ whether a hook ended it. ``skip`` settles every call with that text instead, one at a time, running none;
+ ``skip_calls`` settles only the named calls and does not expose them as started work."""
terminate = end = False
for group in [[call] for call in calls] if skip is not None else self._groups(calls, tools):
self._scope.check()
@@ -1278,8 +1355,9 @@ class Agent:
gated: list[tuple[ToolCallBlock, Optional[ToolResult], bool]] = []
for original in group:
call = deepcopy(original)
- if skip is not None or end:
- gated.append((call, ToolResult((text(skip or _SKIPPED),), is_error=True), False))
+ call_skip = (skip_calls or {}).get(call.id)
+ if skip is not None or call_skip is not None or end:
+ gated.append((call, ToolResult((text(call_skip or skip or _SKIPPED),), is_error=True), False))
continue
call, result, ended = await self._gate(call, self.hooks)
if result is None and call.name not in tools:
@@ -1289,12 +1367,13 @@ class Agent:
running: dict[int, asyncio.Task[ToolResult]] = {}
try:
for index, (call, result, _) in enumerate(gated):
- await emit(
- ToolStarted,
- tool_call_id=call.id,
- name=call.name,
- preview=json.dumps(call.arguments, ensure_ascii=False)[:500],
- )
+ if call.id not in (skip_calls or {}):
+ await emit(
+ ToolStarted,
+ tool_call_id=call.id,
+ name=call.name,
+ preview=json.dumps(call.arguments, ensure_ascii=False)[:500],
+ )
if result is None:
instance = CallInstance(response.session_id, response.row_id, response.context_seq, call.id)
running[index] = asyncio.create_task(self._execute(tools[call.name], call, instance, emit))
@@ -1311,7 +1390,12 @@ class Agent:
break
if isinstance(decision, AlterResult):
result = decision.result
- await self._commit_result(call, result, emit)
+ await self._commit_result(
+ call,
+ result,
+ emit,
+ emit_finished=call.id not in (skip_calls or {}),
+ )
terminate = terminate or result.terminate
finally:
await self._join(running.values())
@@ -1332,7 +1416,12 @@ class Agent:
raise
async def _commit_result(
- self, call: ToolCallBlock, result: ToolResult, emit: Callable[..., Awaitable[None]]
+ self,
+ call: ToolCallBlock,
+ result: ToolResult,
+ emit: Callable[..., Awaitable[None]],
+ *,
+ emit_finished: bool = True,
) -> None:
message = ToolResultMessage(call.id, call.name, result.content, result.is_error, hosted=result.hosted)
validate_message_append(self._rows, session_id=self.session_id, kind="tool_result", message=message)
@@ -1347,13 +1436,14 @@ class Agent:
if committed is not None and committed.record.status == "completed":
# Only an image a surface can deliver counts toward the reply, and is carried with it.
self._generated_images.extend(image for image in committed.record.images or () if image_deliverable(image))
- await emit(
- ToolFinished,
- tool_call_id=call.id,
- event_id=row.row_id,
- is_error=result.is_error,
- watch_id=result.details.get("watch_id"),
- )
+ if emit_finished:
+ await emit(
+ ToolFinished,
+ tool_call_id=call.id,
+ event_id=row.row_id,
+ is_error=result.is_error,
+ watch_id=result.details.get("watch_id"),
+ )
async def _execute(
self,
diff --git a/core/agent_core/ai/_common.py b/core/agent_core/ai/_common.py
index a012d7d5d..9a645d396 100644
--- a/core/agent_core/ai/_common.py
+++ b/core/agent_core/ai/_common.py
@@ -53,6 +53,7 @@ from core.agent_core.ai.provider import (
Done,
HostedToolRoute,
MediaLoader,
+ MalformedToolCall,
ModelEndpoint,
ModelRequest,
OpaquePayload,
@@ -1095,6 +1096,7 @@ class StreamAssembler:
for key, state in self._tools.items()
if "content_index" in state
}
+ malformed: list[MalformedToolCall] = []
for index, block in enumerate(self.content):
if not isinstance(block, ToolCallBlock):
final.append(block)
@@ -1107,10 +1109,7 @@ class StreamAssembler:
arguments = parsed_arguments(raw_arguments, tool_name=block.name)
if isinstance(arguments, ProviderError):
if stop_reason == "tool_use":
- return self.error(
- arguments.message,
- kind="invalid_request",
- )
+ malformed.append(MalformedToolCall(id=block.id, name=block.name))
# A length, safety, refusal, or error stop must still carry
# every model-emitted call so the loop can settle it as an
# unsuccessful tool result. The arguments are incomplete and
@@ -1126,6 +1125,26 @@ class StreamAssembler:
signature=state.get("signature") or block.signature,
)
)
+ if malformed:
+ message = "malformed tool arguments: expected a JSON object"
+ partial = assistant_message(
+ final,
+ origin=self.origin,
+ stop_reason="error",
+ usage=self.usage,
+ error_message=message,
+ verified_origin=self.verified_origin,
+ hosted_summary=self._hosted_summary(final),
+ source_identity=self.source_identity,
+ )
+ return ProviderError(
+ kind="invalid_request",
+ message=message,
+ retryable=False,
+ partial=partial,
+ continuable=True,
+ malformed_tool_calls=tuple(malformed),
+ )
return assistant_message(
final,
origin=self.origin,
diff --git a/core/agent_core/ai/provider.py b/core/agent_core/ai/provider.py
index 15b187340..e8e48f3be 100644
--- a/core/agent_core/ai/provider.py
+++ b/core/agent_core/ai/provider.py
@@ -207,6 +207,18 @@ class Done:
message: AssistantMessage
+@dataclass(frozen=True)
+class MalformedToolCall:
+ """A provider tool call whose final JSON arguments were not a JSON object.
+
+ This carries only canonical call identity. The malformed provider bytes never
+ cross the adapter boundary or enter the transcript.
+ """
+
+ id: str
+ name: str
+
+
@dataclass(frozen=True)
class ProviderError:
kind: ErrorKind
@@ -224,6 +236,10 @@ class ProviderError:
#: Permission to continue from the committed transcript, not replay this request. False by default:
#: only a classified transient failure grants it, even when streaming makes ``retryable`` false.
continuable: bool = False
+ #: Malformed final arguments are still strict protocol errors, but this
+ #: narrow metadata grants Vibey one transcript-only recovery. The partial
+ #: contains safe empty arguments for these calls; the loop must not execute them.
+ malformed_tool_calls: tuple[MalformedToolCall, ...] = ()
@dataclass(frozen=True)
diff --git a/docs/plans/agent-core-contracts/conversion-tables.md b/docs/plans/agent-core-contracts/conversion-tables.md
index 754aa9c43..ab653d9cb 100644
--- a/docs/plans/agent-core-contracts/conversion-tables.md
+++ b/docs/plans/agent-core-contracts/conversion-tables.md
@@ -43,7 +43,7 @@ construct partials, mark terminal state, or parse final tool arguments.
| --- | --- | --- |
| Content accumulation and visible-output flag | `StreamAssembler.text_delta`, `thinking_delta`, `tool_start`, `tool_arguments` | `anthropic.py` content-block branches; `openai_chat.py` delta branches; `openai_responses.py` output/reasoning/tool branches |
| Stable tool id and native id map | `StreamAssembler.bind_tool_identity`, `tool_start` → `_set_tool_id` | The stream index owns the slot. A non-empty native ID binds once; a different ID on an open slot raises a terminal `invalid_request` before identity/argument mutation. Missing IDs use the unique monotonic allocator. Identical repeats and the first late ID are accepted; closed slots ignore late data. Explicit canonical IDs use the same ownership map and receive a fresh fallback on collision. Native aliases route only indexless frames, never override an explicit index; Responses argument delta/done use the same bind-once check as item added/done. |
-| Final JSON argument validation | `StreamAssembler.finalize` → `parsed_arguments` | The three adapters call `finalize` once after their protocol terminator; malformed arguments are a C-2 terminal `invalid_request` deviation from Pi's permissive partial parser |
+| Final JSON argument validation | `StreamAssembler.finalize` → `parsed_arguments` | The three adapters keep strict `invalid_request` validation for malformed final arguments, but carry a safe partial with `{}` placeholders and `malformed_tool_calls` identities so Vibey can persist/skip the unsafe calls and continue from transcript; malformed provider metadata remains a terminal `invalid_request` deviation from Pi's permissive partial parser |
| Usage and early usage retention | `set_usage` | Anthropic `message_start`/`message_delta`; Chat usage chunks; Responses terminal response |
| Per-protocol usage field normalization | `_anthropic_usage`, `_openai_usage`, `_responses_usage` | One normalizer per adapter. Anthropic start and delta share the same non-null merge; Chat chunk and choice usage share the same counter-level fallback order. See the field ledger below. |
| Partial on every error and abort | `StreamAssembler.partial`, `error`, `exception`, `aborted`, `incomplete` | `drive_sse_stream` owns HTTP, transport-read, error-body, cancellation, and translator exceptions; protocol translators only request assembler errors |
@@ -488,7 +488,7 @@ adapter was not included because the owner deferred it from v1.
| `responses_empty_slots_ignore_late_deltas` | empty text and reasoning slots close on `output_item.done`; later deltas are ignored | same, with optional usage remaining absent | canonical optional usage |
| `chat_finish_reason_end_maps_to_stop` | compatibility `finish_reason=end` maps to `stop`; no usage frame leaves Pi's initialized usage shape | same stop mapping; usage remains unknown | canonical optional usage |
| `anthropic_null_delta_usage_preserves_message_start` | `message_start.input_tokens=7` survives null fields in `message_delta` | same | none |
-| `chat_malformed_tool_arguments` | Pi's permissive parser completes `read` with `{}` and `tool_use` | terminal `invalid_request` naming malformed arguments; partial has no tool block | C-2 canonical malformed-argument error |
+| `chat_malformed_tool_arguments` | Pi's permissive parser completes `read` with `{}` and `tool_use` | strict `invalid_request` with a safe partial call, `malformed_tool_calls`, and explicit transcript-only recovery permission; the loop records an error result and never executes the malformed call | C-2 strict validation plus Vibey recovery boundary |
| `chat_eof_after_text_is_partial_abort` | missing finish reason becomes an error with text partial and Pi's initialized usage shape | `network` error with text partial and `retryable=false`; optional usage remains absent | C-2 retry boundary and canonical optional usage |
| `anthropic_empty_block_end_then_error` | empty text block is ended before the overloaded error; Pi retains the empty block in partial and does not apply the empty-slot retry boundary | empty placeholder does not count as streamed output, so retry remains allowed and partial is omitted | C-2 retry boundary |
| `anthropic_malformed_delta` | Pi's null delta raises a generic stream error with the empty text block in partial | known malformed metadata is terminal `invalid_request`; empty placeholder is omitted from partial | C-2 malformed-event classification and retry boundary |
diff --git a/docs/plans/agent-core-contracts/loop-control.md b/docs/plans/agent-core-contracts/loop-control.md
index 7bba47c56..6604bc805 100644
--- a/docs/plans/agent-core-contracts/loop-control.md
+++ b/docs/plans/agent-core-contracts/loop-control.md
@@ -82,8 +82,7 @@ with pending steer/follow-up keep continuing normally; a tool-free one that
replies and did not stop at its output limit is a completed reply that the
pending input superseded, which the loop passes to `append_response` as
`superseded`, so it commits as `result`, a reply like a final, in the same
-transaction (released versions committed it as `output`). A truncated one stays narration, as it would fail as a final. Truncated tool calls keep the existing bounded reissue
-path without executing their partial arguments.
+transaction (released versions committed it as `output`). A truncated one stays narration, as it would fail as a final. Truncated tool calls keep the existing bounded reissue path without executing their partial arguments. A strict malformed final tool-argument error is different: the adapter preserves every call with safe `{}` arguments and marks only malformed identities; the loop commits the assistant row, records an explicit error result for each malformed call, executes valid siblings once, and permits Vibey's bounded transcript-only continuation. It never guesses or persists the malformed bytes.
A final response with `stop_reason` `refusal` or `safety` and no text is still committed as the final
response, typed `error` ([`transcript.md`](transcript.md) §1); its commit-time display text is the localized
diff --git a/docs/plans/agent-core-contracts/provider-event.schema.json b/docs/plans/agent-core-contracts/provider-event.schema.json
index fe33e5463..b99fba4be 100644
--- a/docs/plans/agent-core-contracts/provider-event.schema.json
+++ b/docs/plans/agent-core-contracts/provider-event.schema.json
@@ -43,6 +43,7 @@
"kind": { "enum": ["overflow", "rate_limit", "overloaded", "network", "stalled", "server", "interrupted", "auth", "invalid_request", "aborted", "unknown"] },
"retryable": { "type": "boolean" },
"continuable": { "type": "boolean", "default": false, "description": "Explicit permission to continue from committed context, separate from same-request replay. Classified transient failures allow it; a pre-output recovery exhaustion with an explicit server_error or network cause may allow the bounded transcript-only continuation; missing/future causes, refusals, permanent and local failures do not." },
+ "malformed_tool_calls": { "type": "array", "items": { "type": "object", "required": ["id", "name"], "additionalProperties": false, "properties": { "id": { "type": "string" }, "name": { "type": "string" } } }, "description": "Canonical identities of calls whose final JSON arguments were malformed. The matching partial carries safe empty arguments; the loop records an error result and never executes them." },
"retry_after_s": { "type": "number", "minimum": 0 },
"after_thinking": { "type": "boolean", "description": "The response streamed thinking and nothing else before this transient or unknown error; the loop retries it at most once per request (provider.md)." },
"status": { "type": "integer", "description": "HTTP status, when there was one." },
diff --git a/docs/plans/agent-core-contracts/provider.md b/docs/plans/agent-core-contracts/provider.md
index ae53f12d5..13250b5eb 100644
--- a/docs/plans/agent-core-contracts/provider.md
+++ b/docs/plans/agent-core-contracts/provider.md
@@ -143,7 +143,7 @@ own bounded continuation policy (`loop-control.md`).
| --- | --- | --- |
| `overflow` | the request exceeded the model's context (Pi's overflow patterns and Anthropic's refusal of input plus `max_tokens` over the window, HTTP 413, `context_length_exceeded`) | overflow recovery (plan §5.2) |
| `rate_limit`, `overloaded`, `network`, `stalled`, `server`, `interrupted` | transient (`network`: the provider could not be reached; `stalled`: a connected provider sent nothing for the silence bound; `interrupted`: the provider's stream ended after output without a terminal, as Model Hub reports with its own `modelHub.errors.stream_interrupted` frame) | retry with backoff, honoring `retry_after_s`, only when nothing was streamed (no deltas, no `partial`), at most 3 retries within 120 s; then the run ends with that error. After streamed text or a tool call the error is terminal for the request, matching Model Hub's first-byte rule; after thinking alone it is retried once (below) |
-| `auth`, `invalid_request` | not retryable | end the run with the error |
+| `auth`, `invalid_request` | not retryable | end the run with the error. The one narrow exception is a strict final tool-argument parse failure: the adapter keeps a safe partial with every call, marks malformed call identities in `malformed_tool_calls`, and grants only Vibey's transcript-only recovery permission; no malformed call is executed. |
| `aborted` | cancelled by the caller | end the run as aborted |
| `unknown` | anything else | end the run with the error; after thinking alone, retry once (below) |
@@ -161,5 +161,13 @@ A `ProviderError` whose `partial` is committed (a response that failed after out
partial's `error_message`, redacted like the error itself and bounded to 1,000 characters, so the transcript row says
why the response ended. A Stop's partial carries none.
+A strict final tool-argument parse failure is the only `invalid_request` that carries recovery metadata. Its `partial`
+keeps text, thinking, and all canonical tool calls, replacing every malformed argument payload with `{}`. The
+`malformed_tool_calls` identities tell the loop which calls are unsafe: it commits the assistant row, records one
+model-visible error `tool_result` for each such call, executes valid sibling calls once, and then returns a bounded
+transcript-only continuation permission. The malformed provider bytes are never logged or persisted. If the continuation
+fails, ordinary provider-error handling reports the actionable failure; unrelated malformed metadata remains terminal
+`invalid_request` with the normal partial policy.
+
The `ai` lane adds a conversion table per protocol (request fields, stream events, stop reasons, usage fields) to this
directory with its first PR; its tests enumerate the tables.
diff --git a/tests/agent_core/ai/test_pi_stream_differential.py b/tests/agent_core/ai/test_pi_stream_differential.py
index 19070b554..9540596da 100644
--- a/tests/agent_core/ai/test_pi_stream_differential.py
+++ b/tests/agent_core/ai/test_pi_stream_differential.py
@@ -751,7 +751,20 @@ _CASES = (
"outcome": "error",
"kind": "invalid_request",
"retryable": False,
- "partial": {"stop_reason": "error", "content": [], "usage": None},
+ "partial": {
+ "stop_reason": "error",
+ "content": [
+ {
+ "type": "tool",
+ "id": "call_1",
+ "native_id": "call_1",
+ "name": "read",
+ "arguments": {},
+ "signature": None,
+ }
+ ],
+ "usage": None,
+ },
},
},
{
diff --git a/tests/agent_core/ai/test_provider_adapters.py b/tests/agent_core/ai/test_provider_adapters.py
index ebf8aa880..76ae1d641 100644
--- a/tests/agent_core/ai/test_provider_adapters.py
+++ b/tests/agent_core/ai/test_provider_adapters.py
@@ -21,6 +21,7 @@ from core.agent_core.ai.openai_responses import OpenAIResponsesAdapter, build_re
from core.agent_core.ai.provider import (
BlockEnd,
Done,
+ MalformedToolCall,
MediaLoader,
ModelEndpoint,
ModelRequest,
@@ -2381,7 +2382,7 @@ async def test_chat_late_tool_id_matches_tool_call_start_id() -> None:
async def test_chat_malformed_tool_arguments_keep_text_and_usage_in_partial() -> None:
body = (
'data: {"choices":[{"delta":{"content":"before "}}]}\n\n'
- 'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"name":"read","arguments":"not-json"}}]},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}\n\n'
+ 'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_good","function":{"name":"read","arguments":"{\\"path\\":\\"x\\"}"}},{"index":1,"id":"call_bad","function":{"name":"read","arguments":"not-json"}}]},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}\n\n'
"data: [DONE]\n\n"
)
@@ -2396,7 +2397,14 @@ async def test_chat_malformed_tool_arguments_keep_text_and_usage_in_partial() ->
assert error.kind == "invalid_request"
assert error.partial is not None
assert error.partial.usage == Usage(input_tokens=3, output_tokens=2)
- assert error.partial.content == (TextBlock("before "),)
+ assert error.partial.content == (
+ TextBlock("before "),
+ ToolCallBlock(id="call_good", name="read", arguments={"path": "x"}, native_id="call_good"),
+ ToolCallBlock(id="call_bad", name="read", arguments={}, native_id="call_bad"),
+ )
+ assert error.partial.error_message == "malformed tool arguments: expected a JSON object"
+ assert error.continuable is True
+ assert error.malformed_tool_calls == (MalformedToolCall(id="call_bad", name="read"),)
@pytest.mark.asyncio
diff --git a/tests/test_vibey_agent.py b/tests/test_vibey_agent.py
index 51bff40a4..33efe5651 100644
--- a/tests/test_vibey_agent.py
+++ b/tests/test_vibey_agent.py
@@ -37,7 +37,7 @@ from sqlalchemy import select, update
from config.v2_config import ModelHubConfig
from core.agent_core.agent.events import NAMED_ERROR_KINDS, ToolFinished
from core.agent_core.agent.recovery import UNRECORDED_EFFECT
-from core.agent_core.ai.provider import Done
+from core.agent_core.ai.provider import Done, MalformedToolCall
from core.agent_core.harness.projection import project
from core.agent_core.messages import (
AssistantMessage,
@@ -59,6 +59,7 @@ from core.agent_core.messages import (
ThinkingBlock,
ToolCallBlock,
ToolResultMessage,
+ Usage,
UserMessage,
text,
message_from_dict,
@@ -1309,6 +1310,125 @@ async def test_a_transient_failure_continues_from_the_same_transcript_without_a_
assert harness.controller.terminals[-1]["is_error"] is False
+async def test_malformed_tool_arguments_recover_without_executing_the_bad_call(
+ engine, session, tmp_path, published, continuation_policy
+) -> None:
+ from core.agent_core.ai.provider import ProviderError
+
+ continuation_policy(initial_delay_s=0)
+ valid = ToolCallBlock(id="call_valid", name="echo", arguments={"value": "ok"})
+ malformed = ToolCallBlock(id="call_bad", name="echo", arguments={})
+ partial = AssistantMessage(
+ (text("before"), valid, malformed), ORIGIN, "error", usage=Usage(input_tokens=17, output_tokens=23)
+ )
+ failure = ProviderError(
+ "invalid_request",
+ "malformed tool arguments: expected a JSON object",
+ False,
+ partial=partial,
+ continuable=True,
+ malformed_tool_calls=(MalformedToolCall(id=malformed.id, name=malformed.name),),
+ )
+ tool = FakeTool("echo")
+ harness = _Harness(
+ engine,
+ tmp_path,
+ "telegram",
+ [[failure], [Done(assistant("recovered"))]],
+ tools=[tool],
+ )
+
+ await harness.agent.handle_message(harness.request("run the safe call"))
+
+ assert len(tool.calls) == 1
+ assert tool.calls[0][0] == {"value": "ok"}
+ assert len(harness.provider.requests) == 2
+ resumed = harness.provider.requests[1].messages
+ assert any(
+ isinstance(message, ToolResultMessage)
+ and message.tool_call_id == malformed.id
+ and message.is_error
+ for message in resumed
+ )
+ rows = await harness.context_rows()
+ responses = [row for row in rows if row.kind == "response"]
+ assert responses[0].message.stop_reason == "error"
+ assert responses[0].message.tool_calls == (valid, malformed)
+ results = [row.message for row in rows if row.kind == "tool_result"]
+ assert len(results) == 2
+ assert any(result.tool_call_id == malformed.id and result.is_error for result in results)
+ assert any(
+ "malformed JSON arguments" in block.text
+ for result in results
+ for block in result.content
+ if isinstance(block, TextBlock)
+ )
+ assert harness.controller.im_client.sent == ["before", "recovered"]
+ with engine.connect() as conn:
+ assert conn.execute(
+ select(agent_events.c.id).where(
+ agent_events.c.session_id == SESSION, agent_events.c.event_type == "model_attempt"
+ )
+ ).first() is None
+ assert harness.controller.terminals[-1]["is_error"] is False
+
+
+async def test_malformed_tool_recovery_honors_a_terminating_valid_sibling(
+ engine, session, tmp_path, published, continuation_policy
+) -> None:
+ from core.agent_core.ai.provider import ProviderError
+
+ continuation_policy(initial_delay_s=0)
+ valid = ToolCallBlock(id="call_valid", name="echo", arguments={"value": "stop"})
+ malformed = ToolCallBlock(id="call_bad", name="echo", arguments={})
+ failure = ProviderError(
+ "invalid_request",
+ "malformed tool arguments: expected a JSON object",
+ False,
+ partial=AssistantMessage((valid, malformed), ORIGIN, "error"),
+ continuable=True,
+ malformed_tool_calls=(MalformedToolCall(id=malformed.id, name=malformed.name),),
+ )
+ tool = FakeTool("echo", result=ToolResult((text("terminated"),), terminate=True))
+ harness = _Harness(engine, tmp_path, "telegram", [[failure]], tools=[tool])
+
+ await harness.agent.handle_message(harness.request("stop after the safe call"))
+
+ assert len(harness.provider.requests) == 1
+ assert len(tool.calls) == 1
+ assert harness.controller.terminals[-1]["is_error"] is False
+
+
+async def test_malformed_hosted_tool_does_not_emit_tool_activity(
+ engine, session, tmp_path, published, continuation_policy
+) -> None:
+ from core.agent_core.ai.provider import ProviderError
+
+ continuation_policy(initial_delay_s=0)
+ malformed = ToolCallBlock(id="call_hosted", name="web_search", arguments={})
+ failure = ProviderError(
+ "invalid_request",
+ "malformed tool arguments: expected a JSON object",
+ False,
+ partial=AssistantMessage((malformed,), ORIGIN, "error"),
+ continuable=True,
+ malformed_tool_calls=(MalformedToolCall(id=malformed.id, name=malformed.name),),
+ )
+ tool = FakeTool("web_search")
+ harness = _Harness(
+ engine,
+ tmp_path,
+ "telegram",
+ [[failure], [Done(assistant("recovered"))]],
+ tools=[tool],
+ )
+
+ await harness.agent.handle_message(harness.request("search safely"))
+
+ assert tool.calls == []
+ assert not any(item.startswith("🔧 web_search") for item in harness.controller.im_client.sent)
+
+
async def test_a_continuation_reuses_a_committed_tool_result_without_rerunning_the_tool(
engine, session, tmp_path, published
) -> None: