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): a Session whose relay rotated accounts heals: refused opaque payloads go portable and stay so (#2729)

Call Whitee61f73d9ea30
diff --git a/core/agent_core/agent/loop.py b/core/agent_core/agent/loop.py
index 591f9d2ff..d88b4e105 100644
--- a/core/agent_core/agent/loop.py
+++ b/core/agent_core/agent/loop.py
@@ -199,20 +199,26 @@ class _Refusals:
A refusal is answered by a fresh hop, rendered normally: at most once per (Source, tool, aspect) a narrow hosted
rule names, and at most ``MAX_REFUSAL_RESOLVES`` times in the run. Past a bound, or when a gateway hand-back's
- fresh hop is the refused one again, the request goes portable instead. A Source identity whose provider refused
- an opaque payload sends none for the rest of the run; that refusal is answered once per identity.
+ fresh hop is the refused one again, the request goes portable instead.
+
+ A Source identity whose provider refused an opaque payload of a kind sends none of that kind again in this Session:
+ ``opaque`` starts with the Session's earlier refusals (``replay_refusal`` rows), and each new one is answered once,
+ then kept (``unsaved`` until the loop persists it). A refusal of a kind already demoted stands as the error.
"""
resolves: int = 0
rules: set[tuple[Optional[str], str, Optional[str]]] = field(default_factory=set)
- identities: set[Optional[str]] = field(default_factory=set)
+ opaque: set[tuple[Optional[str], str]] = field(default_factory=set)
+ unsaved: dict[Optional[str], list[str]] = field(default_factory=dict)
def answer(self, refusal: Refusal) -> Optional[Literal["resolve", "portable"]]:
"""How the next attempt answers ``refusal``, counted; None when the refusal stands as the error."""
if refusal.kind == "opaque_refused":
- if refusal.source_identity in self.identities:
+ fresh = [kind for kind in refusal.payloads if (refusal.source_identity, kind) not in self.opaque]
+ if not fresh:
return None
- self.identities.add(refusal.source_identity)
+ self.opaque.update((refusal.source_identity, kind) for kind in fresh)
+ self.unsaved.setdefault(refusal.source_identity, []).extend(fresh)
rule = (refusal.source_id, refusal.tool, refusal.aspect) if refusal.tool is not None else None
if self.resolves >= MAX_REFUSAL_RESOLVES or rule in self.rules:
return "portable"
@@ -225,8 +231,8 @@ class _Refusals:
"""``selection`` as the next request goes out: without the refused identities' opaque payloads, and portable
when the refusal it answers asked for that."""
endpoint = selection.endpoint
- if self.identities:
- endpoint = replace(endpoint, opaque_refused=frozenset(self.identities))
+ if self.opaque:
+ endpoint = replace(endpoint, opaque_refused=frozenset(self.opaque))
if portable:
endpoint = portable_endpoint(endpoint)
return selection if endpoint is selection.endpoint else replace(selection, endpoint=endpoint)
@@ -513,6 +519,17 @@ class Agent:
self._running = self._open = False
self._run_tools = None
+ async def _save_refusals(self) -> None:
+ """Keep the run's new opaque refusals in the Session, so its later turns never send what was refused."""
+ unsaved, self._refusals.unsaved = self._refusals.unsaved, {}
+ for identity, payloads in unsaved.items():
+ entry = {"version": 1, "source_identity": identity, "payloads": payloads}
+ row = await self._scope.call(
+ lambda entry=entry: self.store.append_payload(self.session_id, "replay_refusal", entry),
+ interruptible=False,
+ )
+ self._rows.append(row)
+
async def _save_state(self, *, cleanup: bool = False) -> None:
"""Hook state at a commit point; a C-9 transition writes it with its own rows (``_commit_context``)."""
representation = state_representation(self._ctx.state)
@@ -611,7 +628,8 @@ class Agent:
if consume:
self._turn_inputs = set()
self._attempts = []
- self._refusals = _Refusals()
+ # The Session's earlier opaque refusals hold from this run's first request.
+ self._refusals = _Refusals(opaque=set(projection.opaque_refused))
if consume:
self._generated_images = []
loaded = True
@@ -930,6 +948,8 @@ class Agent:
# ladder step (section 8).
await self._settle_attempts(first)
terminal = replace(terminal, partial=None)
+ if rerender is not None:
+ await self._save_refusals()
if rerender == "portable":
selected, portable = route, True
continue
diff --git a/core/agent_core/ai/_common.py b/core/agent_core/ai/_common.py
index f9f666d2d..69674d1c1 100644
--- a/core/agent_core/ai/_common.py
+++ b/core/agent_core/ai/_common.py
@@ -44,6 +44,7 @@ from core.agent_core.messages import (
UserContent,
)
from core.agent_core.ai.provider import (
+ OPAQUE_PAYLOADS,
RETRYABLE_ERROR_KINDS,
THINKING_DROP_RETRY_KINDS,
BlockEnd,
@@ -52,6 +53,7 @@ from core.agent_core.ai.provider import (
MediaLoader,
ModelEndpoint,
ModelRequest,
+ OpaquePayload,
ProviderError,
Refusal,
TextDelta,
@@ -1678,6 +1680,23 @@ def _pin_unavailable(body: str) -> PinUnavailable | None:
#: xAI's plain-text refusal of a replayed ciphertext it cannot read (§3.2); it does not say which one.
_DECRYPT_REFUSAL = re.compile(r"could not decrypt the provided encrypted_content", re.IGNORECASE)
+#: Anthropic's refusal of a replayed opaque payload, by kind: an encrypted search result or citation index another
+#: account minted, or a thinking signature it does not accept. Each names exactly what it refused.
+_OPAQUE_REFUSALS: tuple[tuple[re.Pattern[str], tuple[OpaquePayload, ...]], ...] = (
+ (_DECRYPT_REFUSAL, OPAQUE_PAYLOADS),
+ (re.compile(r"invalid `?encrypted_(?:content|index)`?", re.IGNORECASE), ("hosted",)),
+ (re.compile(r"invalid `?signature`? in `?thinking`? block", re.IGNORECASE), ("signature",)),
+)
+
+
+def opaque_refusal(body: str) -> tuple[OpaquePayload, ...]:
+ """The kinds of opaque payload a 400 body says the provider refused; none when it names no such refusal."""
+
+ kinds: list[OpaquePayload] = []
+ for pattern, payloads in _OPAQUE_REFUSALS:
+ if pattern.search(body):
+ kinds.extend(kind for kind in payloads if kind not in kinds)
+ return tuple(kinds)
def pin_refusal(assembler: StreamAssembler, endpoint: ModelEndpoint, handed_back: PinUnavailable) -> ProviderError:
@@ -1877,10 +1896,16 @@ async def drive_sse_stream(
if refused is not None and refused(candidate):
yield REFUSED
return
- if response.status_code == 400 and _DECRYPT_REFUSAL.search(body or ""):
- # A replayed ciphertext this account cannot read, refused before any output: the
- # loop renders the request again without that identity's opaque payloads (§3.5).
- refusal = Refusal("opaque_refused", endpoint.source_id, endpoint.source_identity)
+ refused_payloads = opaque_refusal(body or "") if response.status_code == 400 else ()
+ if refused_payloads:
+ # A replayed opaque payload this account cannot read, refused before any output: the
+ # loop renders the request again without that identity's payloads of that kind (§3.5).
+ refusal = Refusal(
+ "opaque_refused",
+ endpoint.source_id,
+ endpoint.source_identity,
+ payloads=refused_payloads,
+ )
candidate = replace(
candidate, retryable=False, continuable=False, after_thinking=False, refusal=refusal
)
diff --git a/core/agent_core/ai/provider.py b/core/agent_core/ai/provider.py
index c83dab814..15b187340 100644
--- a/core/agent_core/ai/provider.py
+++ b/core/agent_core/ai/provider.py
@@ -113,9 +113,9 @@ class ModelEndpoint:
#: A portable request (§3.5, ``portable_endpoint``): never pinned, and no native hosted content. Its Source and
#: identity stay, so a response its own Source serves is still attributed to them.
portable: bool = False
- #: The Source identities whose opaque payloads go nowhere for the rest of the turn: their provider refused one
- #: (§3.5). A message they served is sent as a different origin's.
- opaque_refused: frozenset[str | None] = frozenset()
+ #: The opaque payloads, by (Source identity, kind), that go nowhere: their provider refused one in this Session
+ #: (§3.5). A message that identity served is sent without them, as a different origin's would be.
+ opaque_refused: frozenset[tuple[str | None, str]] = frozenset()
#: Each enabled search tool's route on this hop, in Model Hub's order; empty when none is enabled.
hosted_tools: Mapping[str, HostedToolRoute] = field(default_factory=dict)
#: The models ``image_generate`` may call; None when the tool is off or no model is available.
@@ -231,9 +231,11 @@ class Refusal:
"""Why a request was refused before any output, so the next attempt renders it differently (§3.5).
``pin_unavailable``: the gateway handed a pinned request back; ``tool`` and ``aspect`` name the narrow hosted rule
- that fired, when one did. ``opaque_refused``: the provider refused a replayed ciphertext (xAI's "Could not decrypt
- the provided encrypted_content"), which does not say which one. ``source_id`` and ``source_identity`` are the
- refused request's hop.
+ that fired, when one did. ``opaque_refused``: the provider refused an opaque payload a response of this Source
+ identity minted, replayed under an account that cannot read it (a key change, or a relay serving from another
+ account); ``payloads`` says which kind (``OPAQUE_PAYLOADS``): Anthropic names it, xAI's "Could not decrypt the
+ provided encrypted_content" does not, so it is both. ``source_id`` and ``source_identity`` are the refused
+ request's hop.
"""
kind: Literal["pin_unavailable", "opaque_refused"]
@@ -241,6 +243,13 @@ class Refusal:
source_identity: Optional[str] = None
tool: Optional[str] = None
aspect: Optional[str] = None
+ payloads: tuple["OpaquePayload", ...] = ()
+
+
+#: The kinds of opaque payload a response carries for its own account: ``hosted``, a hosted call's native payload
+#: (Anthropic's encrypted search results); ``signature``, signed or redacted thinking and other per-block signatures.
+OpaquePayload = Literal["hosted", "signature"]
+OPAQUE_PAYLOADS: tuple[OpaquePayload, ...] = ("hosted", "signature")
ProviderEvent = Union[TextDelta, ThinkingDelta, ToolCallStart, ToolCallDelta, BlockEnd, Done, ProviderError]
diff --git a/core/agent_core/ai/transform.py b/core/agent_core/ai/transform.py
index 145127b03..6f8dbc7fd 100644
--- a/core/agent_core/ai/transform.py
+++ b/core/agent_core/ai/transform.py
@@ -13,7 +13,7 @@ from dataclasses import dataclass
from typing import Any, Mapping
from core.agent_core.ai.hosted import portable_hosted
-from core.agent_core.ai.provider import ModelEndpoint
+from core.agent_core.ai.provider import ModelEndpoint, OpaquePayload
from core.agent_core.messages import (
AssistantMessage,
AssistantContent,
@@ -128,7 +128,7 @@ def transform_messages(
# Opaque payloads are bound to the account that issued them, the Source identity: another account's ciphertext
# is refused (xAI, OpenAI). A message another identity served, one no known identity did, or one whose
# identity's provider refused an opaque payload this turn is sent as a different origin's.
- same_origin = message.origin == target and _opaque_admitted(message, endpoint)
+ same_origin = message.origin == target and _opaque_admitted(message, endpoint, "signature")
content: list[AssistantContent] = []
turn_tool_ids: dict[str, list[str]] = {}
in_flight = position == live
@@ -253,12 +253,13 @@ def in_flight_position(messages: list[Message] | tuple[Message, ...]) -> int | N
return last
-def _opaque_admitted(message: AssistantMessage, endpoint: ModelEndpoint | None) -> bool:
- """Whether ``message``'s opaque payloads may go to ``endpoint``: a hop of the Source identity that served it, whose
- provider has not refused one this turn (§3.5)."""
+def _opaque_admitted(message: AssistantMessage, endpoint: ModelEndpoint | None, payload: OpaquePayload) -> bool:
+ """Whether ``message``'s opaque payloads of kind ``payload`` may go to ``endpoint``: a hop of the Source identity
+ that served it, whose provider has not refused that kind in this Session (§3.5)."""
return endpoint is None or (
- message.source_identity == endpoint.source_identity and message.source_identity not in endpoint.opaque_refused
+ message.source_identity == endpoint.source_identity
+ and (message.source_identity, payload) not in endpoint.opaque_refused
)
@@ -281,7 +282,7 @@ def native_replay(
if not blocks or endpoint is None or not endpoint.source_id or endpoint.portable or message.origin != target:
return False
# A native payload is opaque provider content too: it goes back only to the account that issued it.
- if not _opaque_admitted(message, endpoint):
+ if not _opaque_admitted(message, endpoint, "hosted"):
return False
if protocol not in ("anthropic", "openai_responses"):
return False
diff --git a/core/agent_core/harness/projection.py b/core/agent_core/harness/projection.py
index f80d804bc..fa5f6218a 100644
--- a/core/agent_core/harness/projection.py
+++ b/core/agent_core/harness/projection.py
@@ -16,6 +16,7 @@ from copy import deepcopy
from dataclasses import dataclass, field, replace
from typing import Any, Callable, Literal, Mapping, Optional, Sequence
+from core.agent_core.ai.provider import OPAQUE_PAYLOADS
from core.agent_core.harness.store import ContextEntry
from core.agent_core.messages import (
AssistantMessage,
@@ -44,6 +45,8 @@ class Projection:
messages: tuple[Message, ...]
context_seq: int
state: Mapping[str, Any] = field(default_factory=dict)
+ #: The opaque payloads, by (Source identity, kind), a provider refused in this Session (``replay_refusal`` rows).
+ opaque_refused: frozenset[tuple[Optional[str], str]] = frozenset()
@dataclass(frozen=True)
@@ -81,6 +84,8 @@ class ContextView:
state: Mapping[str, Any]
#: How many of ``units``, first, are inputs the latest checkpoint keeps (``kept_inputs``, C-9 context.md §5).
pinned: int = 0
+ #: The opaque payloads, by (Source identity, kind), a provider refused in this Session (``replay_refusal`` rows).
+ opaque_refused: frozenset[tuple[Optional[str], str]] = frozenset()
@property
def messages(self) -> tuple[Message, ...]:
@@ -238,6 +243,13 @@ _PAYLOAD_SHAPES: dict[str, Check] = {
}
),
"agent_state": _object({"version": _one_of(1), "state": lambda value: isinstance(value, dict)}),
+ "replay_refusal": _object(
+ {
+ "version": _one_of(1),
+ "source_identity": _nullable(_string),
+ "payloads": lambda value: isinstance(value, list) and bool(value) and set(value) <= set(OPAQUE_PAYLOADS),
+ }
+ ),
}
@@ -291,9 +303,14 @@ def context_view(entries: Sequence[ContextEntry], *, fork_point: Optional[int] =
compaction: Optional[ContextEntry] = None
edits: dict[str, str] = {}
reasons: dict[str, str] = {}
+ refused: set[tuple[Optional[str], str]] = set()
for row in rows:
if row.kind == "agent_state":
state = row.payload["state"]
+ elif row.kind == "replay_refusal":
+ # A refusal stands for the whole Session, whatever a later checkpoint cut: what the account could not read
+ # still cannot be read.
+ refused.update((row.payload["source_identity"], payload) for payload in row.payload["payloads"])
elif row.kind == "compaction":
compaction = row
elif row.kind == "context_edit":
@@ -342,6 +359,7 @@ def context_view(entries: Sequence[ContextEntry], *, fork_point: Optional[int] =
context_seq=rows[-1].context_seq if rows else 0,
state=deepcopy(state),
pinned=len(kept),
+ opaque_refused=frozenset(refused),
)
@@ -360,7 +378,7 @@ def project(
"""
view = context_view(entries, fork_point=fork_point)
messages = (*deepcopy(tuple(rehydrated)), *view.messages)
- return Projection(system, messages, view.context_seq, view.state)
+ return Projection(system, messages, view.context_seq, view.state, view.opaque_refused)
def validate_message_append(
diff --git a/core/agent_core/harness/store.py b/core/agent_core/harness/store.py
index c98455eff..19624fc7e 100644
--- a/core/agent_core/harness/store.py
+++ b/core/agent_core/harness/store.py
@@ -15,7 +15,7 @@ from typing import Any, Literal, Mapping, Optional, Protocol, Sequence
from core.agent_core.messages import AssistantMessage, HostedImage, Message, ToolResultMessage, UserMessage
-EntryKind = Literal["input", "response", "tool_result", "compaction", "context_edit", "agent_state"]
+EntryKind = Literal["input", "response", "tool_result", "compaction", "context_edit", "agent_state", "replay_refusal"]
@dataclass(frozen=True)
@@ -72,11 +72,11 @@ class TranscriptStore(Protocol):
) -> ContextEntry: ...
async def append_payload(
- self, session_id: str, kind: Literal["compaction", "context_edit", "agent_state"], payload: Mapping[str, Any]
+ self, session_id: str, kind: Literal["compaction", "context_edit", "agent_state", "replay_refusal"], payload: Mapping[str, Any]
) -> ContextEntry: ...
async def append_payloads(
- self, session_id: str, entries: Sequence[tuple[Literal["compaction", "context_edit", "agent_state"], Mapping[str, Any]]]
+ self, session_id: str, entries: Sequence[tuple[Literal["compaction", "context_edit", "agent_state", "replay_refusal"], Mapping[str, Any]]]
) -> Sequence[ContextEntry]:
"""Commit several payload entries in order, in one transaction: all of them or none (C-9 section 10)."""
...
diff --git a/docs/plans/agent-core-contracts/cross-provider.md b/docs/plans/agent-core-contracts/cross-provider.md
index a4a839c43..f31430551 100644
--- a/docs/plans/agent-core-contracts/cross-provider.md
+++ b/docs/plans/agent-core-contracts/cross-provider.md
@@ -27,8 +27,11 @@ Invariants:
takes the "same origin" column only if its origin equals the target's **and** its `source_identity` equals the
hop's. A message another identity served (another Source, or the same Source after its key or relay changed), or
one no known identity served (after a failover to another Source, or a row written before the field), takes the
- "different origin" column at a hop that names an identity. So does a message whose identity's provider refused
- an opaque payload earlier in the run (`ModelEndpoint.opaque_refused`): the refusal does not say which one failed.
+ "different origin" column at a hop that names an identity. A message whose identity's provider refused an opaque
+ payload of a kind in this Session (`ModelEndpoint.opaque_refused`) goes without every payload of that kind: its
+ signatures take the "different origin" column (kind `signature`), or its hosted calls their portable form (kind
+ `hosted`). A relay pooling upstream accounts behind one key changes account without changing identity, so the
+ refusal, not the identity, is what reveals it.
The gateway applies the same binding at failover (C-6 item 6).
- Tool-call id normalization is deterministic: the stored id when the target accepts it; otherwise
`call_` + the first 24 hex characters of SHA-256 of the stored id. Anthropic accepts `^[a-zA-Z0-9_-]{1,64}$`; the
diff --git a/docs/plans/agent-core-contracts/provider.md b/docs/plans/agent-core-contracts/provider.md
index b969a7e85..1bcc05a08 100644
--- a/docs/plans/agent-core-contracts/provider.md
+++ b/docs/plans/agent-core-contracts/provider.md
@@ -22,8 +22,8 @@ class ModelEndpoint: # built by the adapter layer from C-6 HopResolut
source_id: str | None # HopResolution.source_id: the pin of a request with native hosted content
source_identity: str | None # HopResolution.source_identity: the account its opaque payloads are bound to
portable: bool # never pinned, and no native hosted content (vibey-hosted-tools.md §3.5)
- opaque_refused: frozenset[str | None] # identities whose provider refused an opaque payload in this run; the
- # messages they served are sent as a different origin's (cross-provider.md)
+ opaque_refused: frozenset[tuple[str | None, str]] # (Source identity, payload kind) pairs whose provider
+ # refused that kind in this Session; its messages go without them (cross-provider.md)
hosted_tools: Mapping[str, HostedToolRoute] # HopResolution.hosted_tools, in Model Hub's order: per enabled
# search tool its mode and replay, and for native its declaration and Responses
# include values (vibey-hosted-tools.md §3.4)
@@ -74,19 +74,25 @@ continuation, later with the hop's current `replay`, and pinned to that Source e
**Refusals before any output** (`vibey-hosted-tools.md` §3.5). The gateway answers a pinned request it cannot serve
from that Source with `409 pin_unavailable`, matched on its `type` and `code`, and names the narrow hosted rule that
-fired in `error.reason` when one did. xAI answers a replayed ciphertext it cannot read with a plain-text 400, `Could
-not decrypt the provided encrypted_content`, on any request. The adapter ends either send with a `ProviderError` that
-has no output, is not `retryable`, and carries a `refusal`: `pin_unavailable` with the hop's Source and the rule's
-tool and aspect, or `opaque_refused` with the hop's Source identity. The loop answers it by rendering the request
-again, never by the generic retry:
+fired in `error.reason` when one did. A provider refuses an opaque payload replayed under an account that cannot read
+it (a key change, or a relay serving a later turn from another pooled account, which no Source identity can see) with
+a 400 naming it: xAI's plain-text `Could not decrypt the provided encrypted_content`, which does not say which kind;
+Anthropic's `Invalid \`encrypted_content\`` (or `encrypted_index`) for a search result (kind `hosted`) and
+`Invalid \`signature\` in \`thinking\` block` (kind `signature`), on any request. The adapter ends either send with a
+`ProviderError` that has no output, is not `retryable`, and carries a `refusal`: `pin_unavailable` with the hop's
+Source and the rule's tool and aspect, or `opaque_refused` with the hop's Source identity and the kinds refused (both
+for xAI's). The loop answers it by rendering the request again, never by the generic retry:
- by a fresh hop, rendered normally, at most once per (Source, tool, aspect) a narrow rule names and at most twice
per run, whatever the reason;
- in portable form on the hop it has, past either bound, or at once when a hand-back's fresh hop is the refused one
again (the same Source, identity, model and hosted-tool facts). A portable request is never pinned, so the gateway
never hands it back;
-- after `opaque_refused`, with that identity in `opaque_refused` for the rest of the run, so none of its opaque
- payloads is sent again. That is answered once per identity: refused again, the error ends the run.
+- after `opaque_refused`, with each refused (identity, kind) in `opaque_refused`, so none of that identity's payloads
+ of that kind is sent again: its earlier search calls take their portable form (the query and sources, readable;
+ no encrypted data), and its thinking goes without signatures. The loop keeps each new pair as a `replay_refusal`
+ row before resending, so the Session's later turns, and its forks, start without them (`transcript.md`). Each
+ pair is answered once; refused again, the error ends the run.
A `pause_turn` continuation's `pin_unavailable` comes after output, so it is no refusal (`conversion-tables.md`).
diff --git a/docs/plans/agent-core-contracts/transcript.md b/docs/plans/agent-core-contracts/transcript.md
index 94e5d39f1..07a9ac00c 100644
--- a/docs/plans/agent-core-contracts/transcript.md
+++ b/docs/plans/agent-core-contracts/transcript.md
@@ -17,6 +17,7 @@ unique per Session across both tables (partial unique index on each table, plus
| checkpoint | `agent_events` | `context_compaction` | `context` | `content_json` = `Compaction` |
| cleared result | `agent_events` | `context_edit` | `context` | `content_json` = `ContextEdit` |
| hook state | `agent_events` | `agent_state` | `context` | `content_json` = `AgentState` |
+| replay refusal | `agent_events` | `replay_refusal` | `context` | `content_json` = `{version, source_identity, payloads}` |
A final response is `error` when it failed by itself (no text of its own: an empty answer, or a refusal or safety
stop without an explanation), as the other backends' failed terminal rows are. A successful final with nothing to show
@@ -73,8 +74,9 @@ and usage) or
2. Order by `context_seq`.
3. If a `context_compaction` row exists, take the latest; the context becomes its checkpoint message (its `summary`,
then each `state` text as its own block) followed by the rows with `context_seq >= first_kept_seq`, excluding every
- `context_compaction` row (including the selected one) and `agent_state` rows. A tool result whose call was
- summarized leaves with it.
+ `context_compaction` row (including the selected one), `agent_state` and `replay_refusal` rows. A tool result
+ whose call was summarized leaves with it. Every `replay_refusal` row of the ancestry holds, whatever a checkpoint
+ cut: the (Source identity, payload kind) pairs it names are never sent again in this Session (`provider.md`).
4. Apply `context_edit` rows: the latest edit per target replaces that tool result's content with its placeholder;
the `context_edit` rows themselves are then removed, so the result is only messages.
5. Answer any tool call that still has no committed result with the synthetic interrupted result from
diff --git a/modules/agents/vibey/store.py b/modules/agents/vibey/store.py
index b9081f2b3..371a22a5b 100644
--- a/modules/agents/vibey/store.py
+++ b/modules/agents/vibey/store.py
@@ -163,7 +163,7 @@ class AdapterTranscriptStore:
)
async def append_payload(
- self, session_id: str, kind: Literal["compaction", "context_edit", "agent_state"], payload: Mapping[str, Any]
+ self, session_id: str, kind: Literal["compaction", "context_edit", "agent_state", "replay_refusal"], payload: Mapping[str, Any]
) -> ContextEntry:
return await self._store.append_payload(
session_id, kind, payload, agent_name=self._agents.get(session_id), turn_id=self._turns.get(session_id)
@@ -172,7 +172,7 @@ class AdapterTranscriptStore:
async def append_payloads(
self,
session_id: str,
- entries: Sequence[tuple[Literal["compaction", "context_edit", "agent_state"], Mapping[str, Any]]],
+ entries: Sequence[tuple[Literal["compaction", "context_edit", "agent_state", "replay_refusal"], Mapping[str, Any]]],
) -> Sequence[ContextEntry]:
committed = await self._store.append_payloads(
session_id, entries, agent_name=self._agents.get(session_id), turn_id=self._turns.get(session_id)
diff --git a/storage/agent_transcript.py b/storage/agent_transcript.py
index b506acc4a..d933b5314 100644
--- a/storage/agent_transcript.py
+++ b/storage/agent_transcript.py
@@ -108,12 +108,13 @@ _AUDIT_EVENT_TYPE: dict[str, str] = {"attempt": "model_attempt", "fork_turn": "f
CONTEXT_WRITER = "vibey"
PAYLOAD_VERSION = 1
-PayloadKind = Literal["compaction", "context_edit", "agent_state"]
+PayloadKind = Literal["compaction", "context_edit", "agent_state", "replay_refusal"]
_EVENT_TYPE_BY_KIND: dict[str, str] = {
"tool_result": "tool_result",
"compaction": "context_compaction",
"context_edit": "context_edit",
"agent_state": "agent_state",
+ "replay_refusal": "replay_refusal",
}
_KIND_BY_EVENT_TYPE = {event_type: kind for kind, event_type in _EVENT_TYPE_BY_KIND.items()}
@@ -889,7 +890,7 @@ def _canonical(value: dict[str, Any], what: str) -> dict[str, Any]:
def _payload(kind: str, payload: Mapping[str, Any]) -> dict[str, Any]:
- if kind not in ("compaction", "context_edit", "agent_state"):
+ if kind not in ("compaction", "context_edit", "agent_state", "replay_refusal"):
raise ValueError(f"not a payload entry kind: {kind!r}")
data = _canonical(dict(payload), f"{kind} payload")
if not _is_current_version(data):
diff --git a/tests/agent_core/ai/test_hosted_replay.py b/tests/agent_core/ai/test_hosted_replay.py
index 5187bc2ee..2d2eb3a2b 100644
--- a/tests/agent_core/ai/test_hosted_replay.py
+++ b/tests/agent_core/ai/test_hosted_replay.py
@@ -312,30 +312,49 @@ async def test_pin_unavailable_ends_the_pinned_send_as_a_refusal_with_no_output(
_DECRYPT_REFUSAL = "Could not decrypt the provided encrypted_content"
+# Anthropic's, which name what they refused.
+_SEARCH_REFUSAL = "messages.9.content.0: Invalid `encrypted_content` in `search_result` block"
+_SIGNATURE_REFUSAL = "messages.1.content.0: Invalid `signature` in `thinking` block"
@pytest.mark.parametrize("native", [True, False], ids=["pinned", "unpinned"])
@pytest.mark.parametrize(
- ("body", "refused"),
+ ("protocol", "body", "payloads"),
[
- (_DECRYPT_REFUSAL, True),
+ # xAI's says neither which ciphertext nor which kind.
+ ("openai_responses", _DECRYPT_REFUSAL, ("hosted", "signature")),
# A relay may wrap the provider's text in its own error object.
- (json.dumps({"error": {"message": _DECRYPT_REFUSAL, "type": "invalid_request_error"}}), True),
- ("Invalid request: unknown parameter", False),
+ (
+ "openai_responses",
+ json.dumps({"error": {"message": _DECRYPT_REFUSAL, "type": "invalid_request_error"}}),
+ ("hosted", "signature"),
+ ),
+ # Anthropic's names what it refused.
+ ("anthropic", json.dumps({"type": "error", "error": {"type": "invalid_request_error", "message": _SEARCH_REFUSAL}}),
+ ("hosted",)),
+ ("anthropic", json.dumps({"type": "error", "error": {"type": "invalid_request_error",
+ "message": "messages.3.content.1: Invalid `encrypted_index` in citation"}}),
+ ("hosted",)),
+ ("anthropic", json.dumps({"type": "error", "error": {"type": "invalid_request_error", "message": _SIGNATURE_REFUSAL}}),
+ ("signature",)),
+ ("openai_responses", "Invalid request: unknown parameter", ()),
],
- ids=["plain-text", "wrapped", "another-400"],
+ ids=["xai-plain-text", "xai-wrapped", "anthropic-search-result", "anthropic-citation-index",
+ "anthropic-thinking-signature", "another-400"],
)
-async def test_a_decrypt_refusal_ends_the_send_as_an_opaque_refusal_of_its_source_identity(native, body, refused):
- """xAI's plain-text 400 for a replayed ciphertext it cannot read says neither which one nor why. The adapter, its
- one classifier, ends the send with an opaque refusal of the request's Source identity, pinned or not (§3.5)."""
+async def test_an_opaque_payload_refusal_ends_the_send_as_a_refusal_of_its_source_identity(native, protocol, body, payloads):
+ """A provider's 400 for a replayed opaque payload its account cannot read: the adapter, the one classifier, ends
+ the send with an opaque refusal of the request's Source identity and the kinds the provider named, pinned or not
+ (§3.5). Any other 400 is the ordinary error."""
- request = _request(_endpoint("openai_responses", replay=True, native=native))
+ request = _request(_endpoint(protocol, replay=True, native=native))
sent, events = await _send(request, lambda _: httpx.Response(400, text=body))
(error,) = events
assert len(sent) == 1 and isinstance(error, ProviderError) and error.status == 400 and not error.retryable
- assert error.refusal == (Refusal("opaque_refused", SOURCE, IDENTITY) if refused else None)
+ expected = Refusal("opaque_refused", SOURCE, IDENTITY, payloads=payloads) if payloads else None
+ assert error.refusal == expected
# The loop answers a refusal (§3.5). These run a turn through the Agent loop and the real adapters against a stub
@@ -496,3 +515,113 @@ async def test_a_decrypt_refusal_drops_every_opaque_payload_of_that_identity_for
assert len(sent) == 3 and ended.reason == "error"
else:
assert len(sent) == 4 and ended.reason == "completed"
+
+
+# A relay pooling upstream accounts behind one API key can serve a later turn from another account, which cannot read
+# what the first minted. Anthropic's real refusals, as the provider writes them and as the Model Hub gateway forwards
+# them (its own code, the upstream detail inside its copy).
+
+
+def _anthropic_refusal(detail: str, *, via_gateway: bool) -> dict[str, Any]:
+ from tests.agent_core.fakes import model_hub_error
+
+ if via_gateway:
+ return model_hub_error(detail)
+ return {"type": "error", "error": {"type": "invalid_request_error", "message": detail}}
+
+
+def _searched_and_answered() -> str:
+ """An Anthropic turn that thinks (signed), searches natively, and answers."""
+
+ events = (
+ {"type": "message_start", "message": {"usage": {"input_tokens": 5}}},
+ {"type": "content_block_start", "index": 0, "content_block": {"type": "thinking", "thinking": ""}},
+ {"type": "content_block_delta", "index": 0, "delta": {"type": "thinking_delta", "thinking": "look it up"}},
+ {"type": "content_block_delta", "index": 0, "delta": {"type": "signature_delta", "signature": "sig-account-a"}},
+ {"type": "content_block_stop", "index": 0},
+ {"type": "content_block_start", "index": 1, "content_block": USE},
+ {"type": "content_block_stop", "index": 1},
+ {"type": "content_block_start", "index": 2, "content_block": RESULT},
+ {"type": "content_block_stop", "index": 2},
+ {"type": "content_block_start", "index": 3, "content_block": {"type": "text", "text": ""}},
+ {"type": "content_block_delta", "index": 3, "delta": {"type": "text_delta", "text": "v8 is out"}},
+ {"type": "content_block_stop", "index": 3},
+ {"type": "message_delta", "delta": {"stop_reason": "end_turn"}, "usage": {"output_tokens": 3}},
+ {"type": "message_stop"},
+ )
+ return _sse("anthropic", events)
+
+
+async def _session(answer: Any, endpoint: ModelEndpoint, turns: int) -> tuple[list[list[httpx.Request]], list[Any]]:
+ """``turns`` turns of one Session (one store): the requests each turn sent, and how each ended."""
+
+ sent: list[list[httpx.Request]] = []
+
+ def gateway(wire: httpx.Request) -> httpx.Response:
+ sent[-1].append(wire)
+ return answer(len(sent), len(sent[-1]))
+
+ store = InMemoryTranscriptStore()
+ ended = []
+ async with httpx.AsyncClient(transport=httpx.MockTransport(gateway)) as client:
+ for turn in range(1, turns + 1):
+ sent.append([])
+ router = _Hops(client, (endpoint,))
+ agent = Agent(
+ session_id="session", models=router, tools=(_hosted_tool("web_search"),), hooks=(), store=store,
+ jobs=FakeJobHost(), cwd="/t",
+ )
+ events = [event async for event in agent.run(input_row(f"input-{turn}", "and then?"), turn_id=f"turn-{turn}")]
+ ended.append(events[-1])
+ return sent, ended
+
+
[email protected]("via_gateway", [True, False], ids=["through-model-hub", "upstream-body"])
+ ("refusal", "demoted", "kept"),
+ [(_SEARCH_REFUSAL, '"opaque"', "sig-account-a"), (_SIGNATURE_REFUSAL, "sig-account-a", '"opaque"')],
+ ids=["search-result", "thinking-signature"],
+)
+async def test_an_opaque_payload_another_account_refuses_is_sent_portably_from_then_on_in_the_session(
+ refusal, demoted, kept, via_gateway
+):
+ """A relay can serve a later turn from another upstream account, which cannot read the search results or thinking
+ signatures the first minted; Anthropic then refuses the request before any output, naming what it refused. The
+ Session's native search replays verbatim while that is accepted; once refused, the turn is sent again once without
+ that kind of payload from that Source identity, in its portable form, and succeeds. The Session keeps the
+ refusal: its next turn is portable from its first request, with no failed attempt. Only the kind refused goes."""
+
+ def answer(turn: int, attempt: int) -> httpx.Response:
+ if turn == 1:
+ return httpx.Response(200, headers=_served("anthropic"), text=_searched_and_answered())
+ if turn == 3 and attempt == 1:
+ return httpx.Response(400, json=_anthropic_refusal(refusal, via_gateway=via_gateway))
+ return httpx.Response(200, headers=_served("anthropic"), text=_sse("anthropic", _ANSWERED["anthropic"]))
+
+ sent, ended = await _session(answer, _endpoint("anthropic", replay=True), turns=4)
+
+ assert [event.reason for event in ended] == ["completed"] * 4
+ bodies = [[wire.content.decode() for wire in turn] for turn in sent]
+ # Accepted: the earlier turn's search and signed thinking replay verbatim.
+ assert len(bodies[1]) == 1 and demoted in bodies[1][0] and kept in bodies[1][0]
+ # Refused once, then sent again without what was refused, and nothing else lost.
+ assert len(bodies[2]) == 2 and demoted in bodies[2][0]
+ assert demoted not in bodies[2][1] and kept in bodies[2][1]
+ if demoted == '"opaque"':
+ # The search in its portable form: the query and the source, readable, with no encrypted data.
+ assert "avibe release" in bodies[2][1] and "https://avibe.bot/r" in bodies[2][1]
+ # The next turn: portable from its first request.
+ assert len(bodies[3]) == 1 and demoted not in bodies[3][0] and kept in bodies[3][0]
+
+
+async def test_a_refusal_of_what_the_session_already_demoted_stands_as_the_error():
+ """A refusal is answered once: the same refusal again, after the payload it names was demoted, is the error."""
+
+ def answer(turn: int, attempt: int) -> httpx.Response:
+ if turn == 1:
+ return httpx.Response(200, headers=_served("anthropic"), text=_searched_and_answered())
+ return httpx.Response(400, json=_anthropic_refusal(_SEARCH_REFUSAL, via_gateway=True))
+
+ sent, ended = await _session(answer, _endpoint("anthropic", replay=True), turns=2)
+
+ assert len(sent[1]) == 2 and ended[1].reason == "error"
diff --git a/tests/agent_core/fakes.py b/tests/agent_core/fakes.py
index 34dc3b45b..a022b5bb1 100644
--- a/tests/agent_core/fakes.py
+++ b/tests/agent_core/fakes.py
@@ -200,7 +200,7 @@ class InMemoryTranscriptStore:
def _payload(kind, payload: Mapping) -> dict:
"""A payload row's content, as the contract admits it: a payload kind, at the current version."""
- if kind not in ("compaction", "context_edit", "agent_state"):
+ if kind not in ("compaction", "context_edit", "agent_state", "replay_refusal"):
raise ValueError(f"not a payload entry kind: {kind!r}")
if payload.get("version") != 1:
raise ValueError(f"a {kind} payload needs version 1")
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index f2f2edd48..24aed1722 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -399,7 +399,9 @@ scenarios:
related_tests:
- tests/agent_core/ai/test_hosted_replay.py::test_pin_unavailable_ends_the_pinned_send_as_a_refusal_with_no_output
- tests/agent_core/ai/test_hosted_replay.py::test_refusals_are_answered_within_their_bounds_and_portable_rendering_is_the_last_resort
- - tests/agent_core/ai/test_hosted_replay.py::test_a_decrypt_refusal_ends_the_send_as_an_opaque_refusal_of_its_source_identity
+ - tests/agent_core/ai/test_hosted_replay.py::test_an_opaque_payload_refusal_ends_the_send_as_a_refusal_of_its_source_identity
+ - tests/agent_core/ai/test_hosted_replay.py::test_an_opaque_payload_another_account_refuses_is_sent_portably_from_then_on_in_the_session
+ - tests/agent_core/ai/test_hosted_replay.py::test_a_refusal_of_what_the_session_already_demoted_stands_as_the_error
- tests/agent_core/ai/test_hosted_replay.py::test_a_decrypt_refusal_drops_every_opaque_payload_of_that_identity_for_the_rest_of_the_turn
- tests/agent_core/ai/test_hosted_replay.py::test_a_hosted_call_goes_back_verbatim_and_pinned_only_to_its_own_source_with_replay_evidence
- tests/agent_core/ai/test_hosted_replay.py::test_a_continuation_goes_back_verbatim_only_to_the_hop_that_served_it
diff --git a/tests/test_transcript_store_contract.py b/tests/test_transcript_store_contract.py
index 92ec65f63..3536d072d 100644
--- a/tests/test_transcript_store_contract.py
+++ b/tests/test_transcript_store_contract.py
@@ -196,13 +196,18 @@ async def test_each_append_returns_the_entry_load_reads_back_in_commit_order(bac
await store.append_response(SESSION, assistant(calls=[call]), final=False, request={"tokens": 120}),
await store.append_tool_result(SESSION, _result("call-1", "body"), details={"path": "f"}),
await store.append_payload(SESSION, "agent_state", {"version": 1, "state": {}}),
+ await store.append_payload(
+ SESSION, "replay_refusal", {"version": 1, "source_identity": "sha256:relay", "payloads": ["hosted"]}
+ ),
await store.append_response(SESSION, assistant("done"), final=True),
]
after = time.time()
loaded = list(await store.load(SESSION))
assert loaded == written
- assert [entry.kind for entry in loaded] == ["input", "response", "tool_result", "agent_state", "response"]
- assert [entry.context_seq for entry in loaded] == list(range(1, 6))
+ assert [entry.kind for entry in loaded] == [
+ "input", "response", "tool_result", "agent_state", "replay_refusal", "response"
+ ]
+ assert [entry.context_seq for entry in loaded] == list(range(1, 7))
assert [entry.message for entry in loaded if entry.message is not None] == [
user("hello"),
assistant(calls=[call]),