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): drop instead of stopping once a run's checkpoints are used up, and keep refusal reasons (#2900)

Call White2254e77dab89
diff --git a/core/agent_core/agent/loop.py b/core/agent_core/agent/loop.py
index 0866f4d6c..4b7c1058a 100644
--- a/core/agent_core/agent/loop.py
+++ b/core/agent_core/agent/loop.py
@@ -1451,16 +1451,18 @@ class Agent:
) -> bool:
"""C-9 on the final request (sections 3 and 8); True when it changed the context.
- Whether the run still compacts is read from its live bound at every decision, never kept: a checkpoint
- attempt in this pass can use it up.
+ Whether the run still takes checkpoints is read from its live bound at every decision, never kept: a
+ checkpoint attempt in this pass can use it up.
"""
- if not self._stopped and (plan.est >= plan.threshold or not plan.fits or ladder.refused):
+ # Once the run takes no more checkpoints, only a drop can follow, and only for a request that cannot fit or
+ # that the provider refused; for one that will be sent as it is, nothing is built for a compaction.
+ compacting = not plan.can_fit or ladder.refused
+ if not self._stopped:
+ compacting = compacting or plan.est >= plan.threshold or not plan.fits
+ if compacting:
# A compaction may follow: what a person typed, for its memo, read off the event loop first.
await self._prefetch_typed(view)
- # The bound first: once the run has stopped compacting, nothing is built for a compaction either.
if not plan.can_fit:
- if self._stopped:
- raise self._exhausted(request, view, plan) # (d); the provider never sees it
_, minimal = await self._minimal(system, selected, request, view, plan, carry=False)
if not minimal.can_fit:
# Not even the request the drop would leave (its row and the last unit, without the previous
@@ -1503,22 +1505,10 @@ class Agent:
@property
def _stopped(self) -> bool:
- """The run has made ``UNPRODUCTIVE_CHECKPOINTS`` failed or ineffective checkpoint attempts (section 10)."""
+ """The run has made ``UNPRODUCTIVE_CHECKPOINTS`` failed or ineffective checkpoint attempts (section 10): it
+ takes no more checkpoints. The drop, which calls no model, still makes room."""
return self._unproductive >= UNPRODUCTIVE_CHECKPOINTS
- def _hold(self, request: ModelRequest, view: ContextView, plan: Budget, *, refused: bool) -> bool:
- """The run's bound (section 10), read live before every step of the ladder.
-
- Not stopped: False, and the step may run. Stopped: nothing more is compacted this run; a request that can
- fit and the provider has not refused is sent as it is (True), and any other ends the run
- ``context_exhausted``.
- """
- if not self._stopped:
- return False
- if refused or not plan.can_fit:
- raise self._exhausted(request, view, plan)
- return True
-
async def _minimal(
self,
system: str,
@@ -1624,14 +1614,16 @@ class Agent:
) -> bool:
"""The overflow ladder (section 8), one loop; True when a step changed the context, False to send as it is.
- Every step is preceded by the run's bound, read live (``_hold``), as the stage reads it first: a checkpoint
- attempt that uses the bound up ends the ladder there, never in a drop.
+ Every step reads the run's bound live (section 10), as the stage does, so a checkpoint attempt here can use it
+ up: from then on no checkpoint is tried, a request that can fit and the provider has not refused is sent as it
+ is, and any other moves on to the drop.
"""
units = view.units
while True:
- if self._hold(request, view, plan, refused=refused):
+ stopped = self._stopped
+ if stopped and not refused and plan.can_fit:
return False
- if not ladder.summary_failed and not ladder.compacted:
+ if not stopped and not ladder.summary_failed and not ladder.compacted:
ladder.compacted = True
cut = normal_cut(units, plan.keep, turn_inputs=self._turn_inputs, pinned=view.pinned)
turn = (
@@ -1645,7 +1637,7 @@ class Agent:
return True
ladder.summary_failed = not outcome.overflow
continue
- if not ladder.summary_failed and ladder.rolls < MAX_ROLLS:
+ if not stopped and not ladder.summary_failed and ladder.rolls < MAX_ROLLS:
def rolling(cut: int) -> SideTurn:
return self._checkpoint_turn(request, view, plan, cut, mode="rolling", reason="overflow")
@@ -2156,8 +2148,13 @@ class Agent:
produced.append(follow_up)
continue
if message.stop_reason != "tool_use" or not message.tool_calls:
- # Only a stop with no call is a reply, and only a tool-use stop carries calls.
- error = f"The {turn.purpose} turn stopped with {message.stop_reason} and no usable {turn.purpose}."
+ # Only a stop with no call is a reply, and only a tool-use stop carries calls. The provider's
+ # word on why it stopped (a refusal's category, say) is the audit's to keep.
+ why = f" ({message.error_message})" if message.error_message else ""
+ error = (
+ f"The {turn.purpose} turn stopped with {message.stop_reason}{why}"
+ f" and no usable {turn.purpose}."
+ )
break
if not gate.open:
# Its earlier calls were already answered with the policy's budget text.
diff --git a/core/agent_core/ai/anthropic.py b/core/agent_core/ai/anthropic.py
index 6e9aa3ace..7660f869b 100644
--- a/core/agent_core/ai/anthropic.py
+++ b/core/agent_core/ai/anthropic.py
@@ -29,6 +29,7 @@ from core.agent_core.ai._common import (
dispatch_wire_event,
endpoint_origin,
hosted_pin,
+ PARTIAL_ERROR_MESSAGE_CHARS,
PAUSE_TURN,
PIN_SOURCE_HEADER,
PinUnavailable,
@@ -263,6 +264,8 @@ class AnthropicAdapter(ProviderAdapter):
yield terminal
return
stop_reason: str | None = None
+ # Why the provider refused, from ``message_delta.delta.stop_details``: the response's ``error_message``.
+ refusal_detail: str | None = None
message_started = False
message_stopped = False
# The hosted tools this request declared natively; ``server_tool_use`` blocks of other tools stay unparsed.
@@ -285,7 +288,7 @@ class AnthropicAdapter(ProviderAdapter):
try:
async def translate(events: AsyncIterator[Any]) -> AsyncIterator[Any]:
nonlocal message_started, message_stopped, stop_reason, paused, next_index, response_usage
- nonlocal response_counts
+ nonlocal response_counts, refusal_detail
async for event in events:
if cancel.cancelled:
terminal = assembler.terminal(assembler.aborted(cancel.reason))
@@ -766,6 +769,9 @@ class AnthropicAdapter(ProviderAdapter):
return
paused = delta.get("stop_reason") == "pause_turn"
stop_reason = _normalize_stop_reason(delta.get("stop_reason"))
+ refusal_detail = (
+ _refusal_detail(delta.get("stop_details")) if stop_reason == "refusal" else None
+ )
response_usage = _anthropic_usage(usage, current=response_usage)
assembler.set_usage(response_usage)
if isinstance(usage, Mapping) and "server_tool_use" in usage:
@@ -804,7 +810,8 @@ class AnthropicAdapter(ProviderAdapter):
yield PAUSE_TURN
return
final = assembler.finalize(
- stop_reason or ("tool_use" if assembler.has_tools() else "stop")
+ stop_reason or ("tool_use" if assembler.has_tools() else "stop"),
+ error_message=refusal_detail,
)
terminal = assembler.terminal(final if isinstance(final, ProviderError) else Done(final))
if terminal is not None:
@@ -826,7 +833,7 @@ class AnthropicAdapter(ProviderAdapter):
if paused:
yield PAUSE_TURN
return
- final = assembler.finalize(stop_reason)
+ final = assembler.finalize(stop_reason, error_message=refusal_detail)
terminal = assembler.terminal(
final if isinstance(final, ProviderError) else Done(final)
)
@@ -859,7 +866,7 @@ class AnthropicAdapter(ProviderAdapter):
)
rejected = continued = paused = message_started = message_stopped = False
unavailable: PinUnavailable | None = None
- stop_reason = None
+ stop_reason = refusal_detail = None
try:
async for event in driver:
if isinstance(event, PinUnavailable):
@@ -1290,6 +1297,21 @@ def _normalize_stop_reason(value: Any) -> str:
return normalized or "error"
+def _refusal_detail(details: Any) -> str | None:
+ """A refusal's ``stop_details`` as the response's ``error_message``: the category of the safeguard that stopped it
+ and Anthropic's explanation, each when given as a non-empty string, bounded like a partial's error. A diagnostic
+ never fails the response, so any other shape is ignored."""
+
+ if not isinstance(details, Mapping):
+ return None
+ category, explanation = _string(details.get("category")).strip(), _string(details.get("explanation")).strip()
+ fields = (("category", category), ("explanation", explanation))
+ detail = "; ".join(f"{name}: {value}" for name, value in fields if value)
+ if not detail:
+ return None
+ return detail if len(detail) <= PARTIAL_ERROR_MESSAGE_CHARS else detail[: PARTIAL_ERROR_MESSAGE_CHARS - 1] + "…"
+
+
def sent_effort(model_id: str, effort: str) -> str | None:
"""The token an Anthropic request carries at ``output_config.effort`` for ``effort`` on ``model_id``; None when it
carries none, for an off effort or a model that thinks on a legacy token budget.
diff --git a/core/agent_core/harness/context.py b/core/agent_core/harness/context.py
index d63e602ab..9f6f39954 100644
--- a/core/agent_core/harness/context.py
+++ b/core/agent_core/harness/context.py
@@ -112,7 +112,8 @@ TOOL_TRUNCATED = (
MAX_ROLLS = 2
MAX_OVERFLOWS = 4
#: Unproductive checkpoint attempts (failed, or normal with a result still at least ``INEFFECTIVE_RATIO * T``) a run
-#: makes before it stops compacting for the rest of the run; held in memory, never persisted (section 10).
+#: makes before it takes no more checkpoints for the rest of the run; the drop, which calls no model, still makes room.
+#: Held in memory, never persisted (section 10).
UNPRODUCTIVE_CHECKPOINTS = 2
INEFFECTIVE_RATIO = 0.75
diff --git a/docs/plans/agent-core-contracts/context.md b/docs/plans/agent-core-contracts/context.md
index 63bce93e2..a98c72454 100644
--- a/docs/plans/agent-core-contracts/context.md
+++ b/docs/plans/agent-core-contracts/context.md
@@ -5,7 +5,8 @@ and the anchor's validity, §2; the cache lifetime per route, §3; the checkpoin
amended 2026-10-15 by owner decision (the threshold stays below the route's context price step, §1); amended
2026-10-18 by owner decision (`checkpoint-v5`: the checkpoint is a consolidation into a mind the turn revises with
`edit`, a memo of the user's own messages it chooses by checkbox, and a now it writes; skills are carried back and the
-file lists shortened; §6, §7, §8 c, §9, §10, §11, §12).
+file lists shortened; §6, §7, §8 c, §9, §10, §11, §12); amended 2026-10-21 by owner decision (the run's bound on
+unproductive checkpoints stops checkpoint turns only: the drop, which calls no model, still makes room; §3, §8, §10).
The `harness` owns the rules (`core/agent_core/harness/context.py`, `projection.py`); the loop applies them before
every model request (`core/agent_core/agent/loop.py`); the adapter supplies the Session-specific parts (§9). Row
shapes: [`transcript-rows.schema.json`](transcript-rows.schema.json); events:
@@ -138,14 +139,14 @@ before a retry alike:
2. **Clear**, when enabled (§4), if the provider cache is cold (the request's route has not used it for longer than
its lifetime, below; after a restart, measured from the response rows' `created_at`) or `est >= 0.8 * T`, which
does not depend on the lifetime.
-3. **Checkpoint** when `est >= T`, the run has not stopped compacting (§10), and there is something to summarize,
+3. **Checkpoint** when `est >= T`, the run still takes checkpoints (§10), and there is something to summarize,
at most once per model request: a normal checkpoint (§6, reason `threshold`) when the fork it would send can fit,
otherwise the overflow ladder from (b) (§8, reason `overflow`). A checkpoint request that overflows continues at
(b).
4. **Ladder** (§8) when the request does not fit. With no checkpoint request left to try (one failed for this
- request other than by overflow, or the run has stopped compacting), a request that can fit is sent and the
- provider judges. Once the run has stopped compacting nothing is compacted at all: a request that cannot fit, or
- that the provider refuses as overflow, ends the run `context_exhausted` (§10).
+ request other than by overflow, or the run takes no more checkpoints), a request that can fit is sent and the
+ provider judges. Once the run takes no more checkpoints, a request that cannot fit, or that the provider refuses
+ as overflow, goes straight to the drop (§8 c, §10).
The cache lifetime is the documented lifetime of the cache mode Vibey requests for the conversation, where clearable
results live, resolved on every request from the route's provider, protocol, and whole model id
@@ -329,7 +330,9 @@ applies them:
- Anything else (a response refused at admission, a length stop, a `tool_use` stop without calls, calls under any
other stop, an error, or a reply still rejected after the repair) is a failure: it counts as unproductive (§10),
nothing enters the context, the old context is kept, and the ladder continues; the replies' text is only in the
- audit row. So is a checkpoint whose row the host cannot complete (§10, invariant 4).
+ audit row. So is a checkpoint whose row the host cannot complete (§10, invariant 4). A stop the provider explains
+ (a refusal's `error_message`, such as the category of the safeguard that refused it) names that explanation in the
+ turn's error, and the adapter logs the failure; the user sees none of it.
- Framing: the now, the mind, the memo, and each carried skill are shown later inside the summary's frame (§7),
where the `<` of the frame's own tags in them is escaped; the block's tags never reach it (above).
- A sentence the model writes outside the block (its looking back, or why it rewrote a section of its mind, §11) is
@@ -488,7 +491,8 @@ once the request fits:
fit (§5), so the context becomes checkpoint + the rest verbatim; roll again while the request still does not fit,
at most 2 rolls per request.
- (c) **Dropped**: when no checkpoint request can help (one failed for this request other than by overflow, a rolling
- one failed, no rolling prefix can fit, or the rolls are used up), no model is called:
+ one failed, no rolling prefix can fit, the rolls are used up, or the run takes no more checkpoints, §10), no model
+ is called:
the earliest part (§5) moves out of the context. Built like any checkpoint row (§7: fresh `state`, and the
in-flight Turn's inputs stay), the row keeps the previous checkpoint's now, mind, and carried skills as they were
(a `checkpoint-v4` row names its skills only: the host renders them now, as a checkpoint carries them, §7),
@@ -504,7 +508,7 @@ once the request fits:
- (d) **Stop**: when the request and its minimal request (what the drop would leave of it: the drop's own row, §8 c,
plus the last unit; §3) both cannot fit (checked in the stage, §3,
before provider admission), when nothing more can move out of a request that cannot fit or that the provider
- refused, after 4 provider overflows of one request, or once the run has stopped compacting (§10), the run ends
+ refused, or after 4 provider overflows of one request, the run ends
`context_exhausted` and the `context_exhausted` event says what fills the context. No model is called for a
context that cannot fit. The error's kind names what is too large, decided by measurement when moving the
conversation out cannot help: a part of the newest unit is named only when the request the stop judged (the
@@ -587,13 +591,15 @@ ends the run `context_exhausted`, as in P1. The adapter supplies the following;
serializes them.
- **A per-run bound on unproductive checkpoints.** A checkpoint attempt that fails, or a normal one whose result is
still at least `0.75 * T` (ineffective), is unproductive; an effective one is not counted. After 2 unproductive
- attempts in one run (`UNPRODUCTIVE_CHECKPOINTS`), the run stops compacting: nothing more is compacted in it (no
- checkpoint, no mechanical drop), a request that can fit is still sent, and one that cannot fit or that the
- provider refuses as overflow ends the run `context_exhausted` (§8 d). The bound is read live first on every
- request, so nothing is built for a compaction it forbids (no hypothetical drop, no host call), and before every
- step of the overflow ladder, which is one loop, so an attempt that uses it up ends the ladder there, never in a
- drop. It is held in memory and never persisted: the next run starts at zero, whatever route or restart lies
- between, so wasted checkpoint spend is at most 2 attempts per Turn. Nothing of it is shown to the user.
+ attempts in one run (`UNPRODUCTIVE_CHECKPOINTS`), the run takes no more checkpoints, normal or rolling: a request
+ that can fit is still sent, and one that cannot fit or that the provider refuses as overflow takes the mechanical
+ drop (§8 c), which calls no model and so is not bounded; only when nothing more can move out does the run stop
+ (§8 d). A provider's refusal of a checkpoint is no exception: the next run would drop anyway, so ending this one
+ would only make the user start over. The bound is read live first on every request, so nothing is built for a
+ compaction that cannot follow (a request sent as it is asks the host nothing), and before every step of the
+ overflow ladder, which is one loop, so an attempt that uses it up moves the ladder on to the drop. It is held in
+ memory and never persisted: the next run starts at zero, whatever route or restart lies between, so wasted
+ checkpoint spend is at most 2 attempts per Turn. Nothing of it is shown to the user.
- **The mind and the memo hold, whatever the model wrote.** The turn's files are checked when it gives a final reply
(§6). One repair names every problem; after it, a file still rejected takes its fallback and never fails the turn.
Every check compares with what the harness wrote; nothing parses the model's free text:
diff --git a/docs/plans/agent-core-contracts/conversion-tables.md b/docs/plans/agent-core-contracts/conversion-tables.md
index 4fa3444c2..511472264 100644
--- a/docs/plans/agent-core-contracts/conversion-tables.md
+++ b/docs/plans/agent-core-contracts/conversion-tables.md
@@ -84,7 +84,7 @@ ignored without parsing.
| `content_block_start` | `index: non-negative integer`, `content_block: object`, `content_block.type: string` | text/thinking `text`/`thinking`/`signature: string`; redacted `data: non-empty string`; tool `id`/`name: string`, `input: object` |
| `content_block_delta` | `index: non-negative integer`, `delta: object`, `delta.type: string` | `text_delta.text`, `thinking_delta.thinking`, `signature_delta.signature`, `input_json_delta.partial_json: string`; present fields with a wrong type are terminal |
| `content_block_stop` | `index: non-negative integer` | none |
-| `message_delta` | none | `delta: object`, `delta.stop_reason: string`, `usage: object` |
+| `message_delta` | none | `delta: object`, `delta.stop_reason: string`, `usage: object`; `delta.stop_details` is read only when it is an object, and only its string `category` and `explanation` (a refusal's diagnostic never fails the response) |
| `message_stop` | none | none |
| named SSE `error` | none: plain text or JSON error envelope accepted | classified before data-event dispatch, matching Pi |
| data-only `error` | `error: object` when an envelope is present | `error.type`/`error.message: string`; provider extension fields such as `code` are ignored |
@@ -523,7 +523,7 @@ Anthropic stream events:
| `content_block_delta.signature_delta` | state | Appends to the thinking signature. |
| `content_block_delta.input_json_delta` | delta | `ToolCallDelta`; the complete JSON is parsed at `Done`. |
| `content_block_stop` | delta | `BlockEnd`. |
-| `message_delta.stop_reason` | delta/state | Normalized stop reason; usage fields are merged. |
+| `message_delta.stop_reason` | delta/state | Normalized stop reason; usage fields are merged. A `refusal` keeps `delta.stop_details` as its message's `error_message` (`category: …; explanation: …`, bounded to 1,000 characters). |
| `message_delta.usage` with a non-object value | terminal `ProviderError` | `invalid_request`; malformed usage is never silently discarded. |
| `message_stop` or clean EOF | Done or terminal `ProviderError` | `message_stop` yields `Done` when `message_delta.stop_reason` was present; after `message_start`, clean EOF without `message_stop` yields an incomplete error even when a stop reason was captured. A no-`message_start` stream with a stop reason follows Pi's clean-EOF completion path. |
| `ping` | state | Ignored keepalive. |
diff --git a/modules/agents/vibey/agent.py b/modules/agents/vibey/agent.py
index ab40861c2..11be77aa6 100644
--- a/modules/agents/vibey/agent.py
+++ b/modules/agents/vibey/agent.py
@@ -48,6 +48,7 @@ from core.agent_core.agent.events import (
NAMED_ERROR_KINDS,
AgentError,
AgentEvent,
+ CompactionFailed,
CompactionFinished,
MessageCommitted,
RunEnded,
@@ -902,6 +903,15 @@ class VibeyAgent(BaseAgent):
elif isinstance(event, CompactionFinished):
# Silent (C-9 section 10); the session's occupancy snapshot drops with the context.
self._note_total(run, event.tokens_after_estimate)
+ elif isinstance(event, CompactionFailed):
+ # Silent to the user too (C-9 section 10); the log keeps why, a refusal's category included.
+ logger.warning(
+ "Vibey compaction failed: session=%s turn=%s reason=%s error=%s",
+ run.session_id,
+ run.turn_id,
+ event.reason,
+ _loggable(event.error),
+ )
async def _settle(self, run: _Run) -> None:
"""Settle the Turn from the run's outcome (loop-control.md section 6).
diff --git a/tests/agent_core/agent/test_compaction.py b/tests/agent_core/agent/test_compaction.py
index 2fdb932a3..16eb840e7 100644
--- a/tests/agent_core/agent/test_compaction.py
+++ b/tests/agent_core/agent/test_compaction.py
@@ -808,34 +808,32 @@ async def test_failing_checkpoints_stop_after_two_in_a_run_and_the_next_run_trie
assert [row.kind for row in await store.load("session")].count("compaction") == 1
-class _UnusableHost(Host):
- async def render_state(self, request):
- self.states.append(request)
- raise RuntimeError("the state store is down")
-
-
-async def test_once_a_run_stops_compacting_a_request_that_cannot_fit_ends_it_and_nothing_is_built():
- model, host = Model(reads(6), FAILURES[:2]), _UnusableHost()
- agent = make_agent(
- model, context=ContextConfig(host=host), tools=[sized_reader(6_000, 6_000, 6_000, 2_000, 500, 12_000)]
- )
- events = await run(agent)
- # Two failed checkpoints stop compaction for the run; the request a 12,000-token result leaves cannot fit.
- assert len(model.checkpoint_requests) == 2
- assert not host.states # no checkpoint row, not even the stop check's dry run, is built
- assert events[-1].reason == "context_exhausted" and [e for e in events if isinstance(e, ContextExhausted)]
- assert not [row for row in await agent.store.load("session") if row.kind == "compaction"]
+#: A checkpoint turn the provider's safeguards refuse, saying why (the adapter's ``error_message``).
+REFUSED = [Done(replace(assistant("", stop_reason="refusal"), error_message="category: reasoning_extraction"))]
-async def test_the_second_unproductive_checkpoint_of_a_run_ends_a_request_that_cannot_fit_without_a_drop():
- model = Model(reads(5), FAILURES[:2])
- agent = make_agent(model, tools=[sized_reader(6_000, 6_000, 6_000, 2_000, 9_000)])
+ "sizes",
+ [
+ # Requests 5 and 6 cross T and their checkpoints are refused, which uses the bound up; the request a
+ # 12,000-token result leaves cannot fit.
+ pytest.param((6_000, 6_000, 6_000, 2_000, 500, 12_000), id="bound-used-earlier"),
+ # Request 5's checkpoint is refused (one); request 6 cannot fit, and its own checkpoint is refused (two).
+ pytest.param((6_000, 6_000, 6_000, 2_000, 9_000), id="bound-used-by-this-request"),
+ ],
+)
+async def test_a_run_that_takes_no_more_checkpoints_drops_a_request_that_cannot_fit_and_continues(sizes):
+ model = Model(reads(len(sizes)), [REFUSED, REFUSED])
+ agent = make_agent(model, tools=[sized_reader(*sizes)])
events = await run(agent)
- # Request 5 crosses T and its checkpoint fails (one); request 6 cannot fit, its checkpoint fails too (two),
- # so the run stops compacting: the request ends the run, never in a mechanical drop.
assert len(model.checkpoint_requests) == 2
- assert not [row for row in await agent.store.load("session") if row.kind == "compaction"]
- assert events[-1].reason == "context_exhausted"
+ assert [event.error for event in events if isinstance(event, CompactionFailed)] == [
+ "The checkpoint turn stopped with refusal (category: reasoning_extraction) and no usable checkpoint."
+ ] * 2
+ # The drop calls no model: the earliest part moves out, and the run goes on.
+ drops = [row.payload for row in await agent.store.load("session") if row.kind == "compaction"]
+ assert drops and all((drop["mode"], drop["reason"], drop["summarizer"]) == ("dropped", "overflow", None) for drop in drops)
+ assert events[-1].reason == "completed" and not [e for e in events if isinstance(e, ContextExhausted)]
async def test_a_failed_checkpoint_attempt_keeps_every_partial_in_its_audit():
@@ -945,8 +943,12 @@ async def test_two_ineffective_checkpoints_stop_compaction_for_the_run():
agent = make_agent(model, tools=[sized_reader(6_000, 6_000, 6_000, 6_000, 5_000, 5_000, 5_000)])
events = await run(agent)
finished = [event for event in events if isinstance(event, CompactionFinished)]
- assert len(finished) == 2 and all(event.tokens_after_estimate >= 0.75 * THRESHOLD for event in finished)
+ normal = [event for event in finished if event.mode == "normal"]
+ assert len(normal) == 2 and all(event.tokens_after_estimate >= 0.75 * THRESHOLD for event in normal)
assert len(model.checkpoint_requests) == 2 # the next crossing is not tried in this run
+ # The request that no longer fits is dropped instead, with no model call, and the run completes.
+ assert [(event.reason, event.mode) for event in finished[2:]] == [("overflow", "dropped")]
+ assert events[-1].reason == "completed"
OVERFLOW = ProviderError("overflow", "prompt is too long: 40000 tokens > 32000 maximum", False, status=400)
@@ -1445,11 +1447,12 @@ async def test_a_provider_that_always_overflows_stops_after_two_checkpoint_calls
agent = make_agent(model, tools=[reader(tokens(1_500))])
events = await run(agent)
assert len(model.checkpoint_requests) == 2
- # The reads, then the refused request: its normal and rolling checkpoints are refused too, which stops the
- # run's compaction, so the refusal ends the run; nothing is dropped.
- assert len(model.conversation_requests) == 8 + 1
+ # The reads, then the refused request: its normal and rolling checkpoints are refused too, which uses the run's
+ # bound up, so each later refusal is answered by a drop; the fourth refusal of the request ends the run.
+ assert len(model.conversation_requests) == 8 + 4
assert events[-1].reason == "context_exhausted"
- assert not [row for row in await agent.store.load("session") if row.kind == "compaction"]
+ drops = [row.payload for row in await agent.store.load("session") if row.kind == "compaction"]
+ assert [(drop["mode"], drop["reason"]) for drop in drops] == [("dropped", "overflow")] * 3
async def test_one_compaction_is_in_flight_per_session():
diff --git a/tests/agent_core/ai/test_provider_adapters.py b/tests/agent_core/ai/test_provider_adapters.py
index 98773e74d..ebf8aa880 100644
--- a/tests/agent_core/ai/test_provider_adapters.py
+++ b/tests/agent_core/ai/test_provider_adapters.py
@@ -1012,6 +1012,39 @@ async def test_anthropic_ignores_unknown_delta_variants_like_pi() -> None:
assert events[-1].message.content == (TextBlock(""),)
+ ("stop", "details", "error_message"),
+ [
+ (
+ "refusal",
+ {"type": "refusal", "category": "reasoning_extraction", "explanation": "Claude can't help with this."},
+ "category: reasoning_extraction; explanation: Claude can't help with this.",
+ ),
+ ("refusal", {"category": "cyber", "explanation": None}, "category: cyber"),
+ # A diagnostic never fails the response: a shape it cannot read is left out.
+ ("refusal", [], None),
+ ("refusal", {"category": 7}, None),
+ ("end_turn", {"category": "cyber"}, None),
+ ],
+)
+async def test_anthropic_refusal_keeps_its_stop_details_as_the_error_message(
+ stop: str, details: Any, error_message: str | None
+) -> None:
+ delta = json.dumps({"type": "message_delta", "delta": {"stop_reason": stop, "stop_details": details}})
+ body = f'data: {delta}\n\ndata: {{"type":"message_stop"}}\n\n'
+
+ def handler(_: httpx.Request) -> httpx.Response:
+ return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
+
+ async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
+ events = await _events(AnthropicAdapter(client), _request("anthropic"))
+
+ assert isinstance(events[-1], Done)
+ assert events[-1].message.stop_reason == ("refusal" if stop == "refusal" else "stop")
+ assert events[-1].message.error_message == error_message
+
+
@pytest.mark.asyncio
async def test_anthropic_empty_block_end_does_not_count_as_streamed_output() -> None:
body = (
diff --git a/tests/test_vibey_agent.py b/tests/test_vibey_agent.py
index 336dc13c5..f1ce6ead8 100644
--- a/tests/test_vibey_agent.py
+++ b/tests/test_vibey_agent.py
@@ -1948,6 +1948,24 @@ async def test_a_refused_model_route_fails_before_the_input_is_written(
# --- settlement and run shape -----------------------------------------------------------
+async def test_a_failed_compaction_is_logged_with_why_and_never_shown(engine, session, tmp_path, published, caplog) -> None:
+ """C-9 keeps compaction silent to the user; the service log keeps why a checkpoint failed, the category a
+ provider's refusal named included, so a run that later runs out of room can be traced to it."""
+ from core.agent_core.agent.events import CompactionFailed
+ from modules.agents.vibey.agent import _Run
+
+ harness = _Harness(engine, tmp_path, "telegram", [])
+ run = _Run(request=harness.request("hello"), session_id=SESSION, turn_id="turn", instance="test")
+ error = "The checkpoint turn stopped with refusal (category: reasoning_extraction) and no usable checkpoint."
+
+ with caplog.at_level(logging.WARNING, logger="modules.agents.vibey.agent"):
+ await harness.agent._on_event(run, CompactionFailed("turn", 1, "threshold", error))
+
+ [line] = [record.getMessage() for record in caplog.records if record.getMessage().startswith("Vibey compaction")]
+ assert line == f"Vibey compaction failed: session={SESSION} turn=turn reason=threshold error={error}"
+ assert harness.controller.im_client.sent == []
+
+
async def test_the_footer_reports_the_runs_final_outcome(engine, session, tmp_path, published, caplog) -> None:
class _LeakyJobs(FakeJobHost):
async def kill(self, job_id, *, reason="killed") -> None:
diff --git a/tests/test_vibey_agent_context.py b/tests/test_vibey_agent_context.py
index 739bdb8c0..dcf7d208f 100644
--- a/tests/test_vibey_agent_context.py
+++ b/tests/test_vibey_agent_context.py
@@ -322,7 +322,7 @@ async def test_model_hub_hears_only_of_failures_the_served_source_produced(engin
# --- what the user is told -------------------------------------------------------------------------------
-async def test_failed_checkpoints_are_silent_and_a_context_that_then_cannot_fit_ends_with_the_one_notice(
+async def test_refused_checkpoints_are_silent_and_a_context_that_then_cannot_fit_is_dropped_and_the_turn_answers(
engine, session, tmp_path
):
sizes = iter((11_200, 500, 2_000))
@@ -333,16 +333,30 @@ async def test_failed_checkpoints_are_silent_and_a_context_that_then_cannot_fit_
def call(call_id: str) -> list:
return [Done(assistant(calls=[ToolCallBlock(call_id, "read", {"path": "a.py"})]))]
- failing = [Done(assistant(CHECKPOINT, stop_reason="length"))]
- scripts = [[Done(assistant("hi"))], call("r1"), failing, call("r2"), failing, call("r3")]
+ refused = [Done(dataclasses.replace(assistant("", stop_reason="refusal"), error_message="category: cyber"))]
+ scripts = [[Done(assistant("hi"))], call("r1"), refused, call("r2"), refused, call("r3"), [Done(assistant("done"))]]
harness = _Harness(engine, tmp_path, "avibe", scripts, tools=[FakeTool("read", execute=read)], language="zh")
# An earlier Turn gives the checkpoints something to summarize: a cut inside this Turn keeps its input.
await _turn(harness, "hello")
await _turn(harness, tokens(12_000))
- # Two requests of the Turn cross T and their checkpoints fail, silently; the Turn then stops compacting, and the
- # request the last result leaves cannot fit: it ends the Turn with the one notice, and nothing was compacted.
+ # Two requests of the Turn cross T and the provider refuses their checkpoints, silently: the Turn takes no more.
+ # The request the last result leaves cannot fit, so its earliest part is dropped, with no model call, and the
+ # Turn answers; nothing asks the user to start over.
assert sum(is_checkpoint(request) for request in harness.provider.requests) == 2
- assert not [row for row in await harness.context_rows() if row.kind == "compaction"]
+ modes = [row.payload["mode"] for row in await harness.context_rows() if row.kind == "compaction"]
+ assert modes and set(modes) == {"dropped"}
+ assert not _texts(harness, "notify")
+ assert harness.controller.terminals[-1]["is_error"] is False
+
+
+async def test_a_request_the_provider_keeps_refusing_as_too_long_ends_the_turn_with_the_one_notice(
+ engine, session, tmp_path
+):
+ overflow = [ProviderError("overflow", "prompt is too long: 40000 tokens > 32000 maximum", False, status=400)]
+ harness = _Harness(engine, tmp_path, "avibe", [[Done(assistant("hi"))], *[overflow] * 20], language="zh")
+ await _turn(harness, "hello")
+ await _turn(harness, "and now?")
+ # Checkpoints and drops cannot make room the provider accepts: its fourth refusal ends the Turn.
assert _texts(harness, "notify") == [f"❌ {i18n_t('vibeyAgent.error.contextExhausted', 'zh')}"]