Skip to content

0.2.31: Report classified model failures as failed delivery outcomes - #53

Merged
alex-w-99 merged 8 commits into
mainfrom
failed-turn-outcomes
Dec 3, 2026
Merged

alex-w-99 merged 8 commits into
mainfrom
failed-turn-outcomes

Conversation

@alex-w-99

@alex-w-99 alex-w-99 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Executive Summary

A hosted delivery whose turn ended because the model provider refused or could not serve it is now reported as { state: "failed", code } instead of uncertain. The code is one of model_quota_exceeded, model_auth_failed or model_unavailable. Anything that cannot be classified keeps today's outcome. Version 0.2.31.

Description

  • src/gateway/model-failure.ts (new): classifyModelFailure() maps a host assistant error (status code, error name, message and response body) to one of the three codes, and ModelTurnError carries the code.
  • src/gateway/sessions.ts: when the finished run carries an assistant error, the turn fails with a ModelTurnError if the error is classified. The failed turn stores failureCode in the gateway journal.
  • src/gateway/delivery-work.ts: a tracked task that rejects with a ModelTurnError records failureCode on the receipt instead of marking it failed (which reads back as uncertain). status() returns DeliveryOutcome: the existing state string, or { state: "failed", code } for a failed turn or receipt that has a valid code. Pending work still reports pending until it settles.
  • eventState and dispatchVerified return DeliveryOutcome.
  • Patch bump to 0.2.31 with a changelog entry.
  • The quota pattern also matches payment_required, so a billing refusal that carries only that code maps to model_quota_exceeded.
  • A classified quota, key or unavailable refusal settles its turn as failed with its code on the first attempt, Companion turns included: a retry only re-read the same errored session. Both failure paths keep the code.
  • A delivery reports a failed turn's code only after its other turns finish, and only when every failed turn has a code. A route that rejected with a classified refusal (email, SMS) settles with its code instead of uncertain.
  • Settled classified failures expire under the receipt retention like completions, so they cannot fill the receipt store.
  • src/host/v2-history.ts: the synthetic failed-run record carries the run's real provider error, so a 401, 402 or 429 on a V2 host still classifies.
  • A failed turn reports only its own code: a code captured elsewhere in the delivery is no longer stamped onto an unclassified failed turn, and a routing failure is marked failed only when it has no code.
  • Before recording a classified refusal on a submitted turn, the gateway checks the terminal-reply journal: a reply the agent sent, or may have sent, decides the outcome (delivered or uncertain) instead of the refusal.
  • An explicit WhatsApp send for the same source also decides a refused turn: a send that returned a message ID counts as answered, an unconfirmed one leaves the turn uncertain, and a rejected one keeps the code.
  • If the turn's lease moves to another owner while a found reply is being settled, the refusal path writes nothing; any other error leaves the turn uncertain without a code.
  • A failed V2 compaction keeps its provider error, and the classifier also reads a V2 error's response.body, so a 429 whose insufficient_quota appears only there maps to model_quota_exceeded.
  • scripts/smoke-loader.sh: before starting the host, the smoke preinstalls the host's own plugin package for the exact running host version, so a cold host has nothing to install at startup; our plugin still goes through the host's real loader. It waits for the host to listen, then gives the plugin its own 240 s load budget, with a 360 s overall cap.

Reason

A turn that ended with a provider refusal (for example, an exhausted key) is over: nothing is still running, and it is never replayed. Reporting it as uncertain makes the host treat the agent as having unknown work, which can keep it awake indefinitely and hides the actual cause. A coded failed outcome lets the host settle the delivery and show why it failed.

Decisions

  • Only one state carries an object. Every other state stays a plain string. A host that does not understand the object handles a classified failure the same way it handles an unknown outcome today, and a host that does understand it still works with older plugin versions.
  • Classification is conservative. Quota and billing refusals, credential or model-access refusals (401, 403, model_not_found), and provider unavailability (429 rate limits, 5xx, overload, connection resets) are classified. Any other error, including errors that are not model errors, stays on its existing path.
  • Quota is checked before rate limits, so a 429 caused by insufficient quota maps to model_quota_exceeded.
  • A plain failed without a code is unchanged. This includes quarantined deliveries and failed hosted calls.
  • A turn that already replied counts as replied. A refusal after an accepted reply settles the delivery as delivered with no code; after an unconfirmed reply it stays uncertain.

Testing

  • npx vitest run: 102 files, 1768 passed, 1 skipped.
  • npx biome check src tests scripts bin and npm run build pass. Biome reports warnings only.
  • New tests:
    • tests/gateway/model-failure.test.ts: the classifier mapping, plus cases that stay unclassified.
    • tests/gateway/delivery-work.test.ts:
      • Quota and auth failures report { state: "failed", code }, including after a reopen.
      • An unclassified failure stays uncertain.
      • A classified failure stays pending while other work is open.
    • tests/gateway/sessions.test.ts: a failed run with a quota or auth error stores the code on the failed turn. An unclassified error stores no code.
  • The new delivery-work and sessions tests fail on main. I checked this by reverting the source changes and rerunning them.
  • New classification case for a {"error":{"code":"payment_required"}} message; it fails without the change. typecheck, lint and tests (1769 passed) pass. scripts/smoke-loader.sh passes locally on hosts 1.17.18 and 1.19.0.
  • New delivery-work cases: a running sibling turn, mixed coded and uncoded failures in both orders, a route rejected with a classified refusal, and expiry of 20,000 settled failures. The session test asserts a single attempt for a quota or key refusal. Each fails on the previous code. typecheck, lint and tests (1775 passed) pass.
  • New cases: a V2 history whose failed run carries a 402 (classifies as model_quota_exceeded) and a following run without an error (no inherited code); a captured refusal plus an unclassified failed turn (plain failed); an accepted and an unconfirmed reply before a refusal. Each fails on the previous code. typecheck, lint and tests (102 files, 1780 passed, 1 skipped) pass.
  • New cases: the lease moving to another owner during reply settlement (no write), a confirmed and an unconfirmed explicit WhatsApp send before a refusal, a failed compaction keeping its error, and V2 response.body classification. Each fails on the previous code. Full suite: 102 files, 1784 passed, 1 skipped; typecheck and biome clean.
  • scripts/smoke-loader.sh from a cold state: host 1.17.18 loaded the plugin 1 s after listening (smoke 38 s); host 1.19.6 loaded it 1 s after listening (smoke 33 s).

Related PRs

None.


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

A hosted delivery whose turn ended because the model provider refused or
could not serve it now reports { state: "failed", code } from eventState,
with code model_quota_exceeded, model_auth_failed or model_unavailable.
The code is stored on the failed turn and on the delivery receipt, so it
survives a restart. Unclassified failures keep their existing outcome.

Bump to 0.2.31.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alex-w-99
alex-w-99 marked this pull request as ready for review December 2, 2026 01:46
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/gateway/sessions.ts Outdated
@@ -1320,6 +1323,7 @@ export function createSessionManager(
retryCount,
retryAt: undefined,
state: latest.companion ? "paused" : "failed",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Companion turn that fails with ModelTurnError stays stuck reporting "pending" instead of { state: "failed", code }.

First, latest.state === "submitted" retries 5 times even for a terminal ModelTurnError. Then line 1325 sets state: "paused" alongside failureCode for Companion turns. DeliveryWork.stateOf() (src/gateway/delivery-work.ts:181-188) only returns failure(...) when turn.state === "failed", so "paused" falls into pending = true and eventState() returns "pending" indefinitely (and keeps activity() non-zero). Also, the non-retryable fallback at lines 1347-1351 drops failureCode.

Fix: settle ModelTurnError immediately without retrying as state: "failed" with failureCode and executionFenced: true for both companion and non-companion turns (and handle turn.state === "paused" explicitly in stateOf() so it never reports "pending").

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. A quota or key ModelTurnError now settles the turn at once as failed with its code, Companion turns included (no paused); only model_unavailable keeps the retry budget. Both failure paths store failureCode. The provider-failure test now asserts a single attempt and fails on the old code. Fixed in 0feea40

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping this one as is. Settling model_unavailable on the first attempt is intentional (afec3f7): a retried turn re-enters completion(), which re-reads the same session and parent message and hits the same stored error. No new model call is made, and OpenCode has already done its own provider retries before it writes that error. Keeping the retry budget would only delay settlement by the backoff. Retrying for real would mean resubmitting the prompt, which is a separate decision.

for (const id of sources) {
current[id]!.outstanding = Math.max(0, (current[id]!.outstanding ?? 0) - 1);
if (failed) current[id]!.failed = true;
if (code) current[id]!.failureCode = code;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skipping current[id]!.failed = true when code is set breaks two paths in stateOf():

  1. If a tracked task rejects with ModelTurnError while any associated turn is still non-terminal (such as a "paused" companion turn), stateOf() skips the receipt.failed check at line 169 and returns "pending" instead of settling.
  2. receipt.failureCode is never cleared, so the fallback at line 189 (return receipt.failureCode ? failure(receipt.failureCode) : "completed") overrides a receipt whose turns all settled normally (delivered / context_only / interrupted).

Fix: keep setting receipt.failed = true on any rejection alongside failureCode, return failure(code) only when a turn actually failed (or no turn ran and receipt.failed && receipt.failureCode), and drop the unconditional receipt.failureCode fallback when all turns completed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partly addressed, partly not a defect.

  1. The paused case is gone: classified failures no longer leave a Companion turn paused (0feea40). stateOf also waits for a delivery's other turns before reporting a failed one. Setting receipt.failed would instead turn every classified failure into uncertain through the early check.
  2. receipt.failureCode records a tracked part that failed. The retry path schedules and returns without rejecting the tracked task, so a turn that later succeeds never left a code behind. A delivery whose failed part was untracked and whose other turn delivered is meant to report the failure, which keeps a classified failure pending until its other work settles covers. No change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partly agreed, fixed in 1a4deca.

Skipping failed/!routed when a code is set is intended: when a route rejects with a classified refusal, capture() records the code and the delivery should settle with it. The existing route-rejection test pins that, so letting receipt.failed win would break it.

What did hold is ?? receipt.failureCode in the failed-turn loop, which stamped a captured code onto an unclassified failed turn. The loop now uses only each turn's own code. The dispatch catch sets failed only when there is no code, and the line 168 condition is receipt.failed || (!receipt.routed && !receipt.failureCode). A new test covers a classified capture plus an unclassified failed turn, which settles as plain failed.

alex-w-99 and others added 3 commits December 2, 2026 07:03
A quota or key refusal fails the turn on the first attempt, Companion turns included,
and keeps its code on every failure path. A delivery reports its failure only after its
other turns finish. Billing matches need an error or details wording.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A delivery whose route rejected with a classified refusal settles with its code instead of staying uncertain.
- A delivery reports a code only when every failed turn has one, in any order.
- Settled classified failures expire under the receipt retention like completions.
- Every classified refusal settles on the first attempt; a retry only re-read the same errored session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the plugin

The smoke's cleanup escalates to SIGKILL when the host ignores SIGTERM, the first plugin
load gets 180 seconds on a cold runner, and the job times out after 20 minutes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@blacksmith-sh

This comment has been minimized.

@blacksmith-sh

This comment has been minimized.

alex-w-99 and others added 3 commits December 2, 2026 21:33
…turn

- The synthetic record for a failed V2 run carries the run's last provider
  error, so 401, 402 and 429 refusals still classify on V2 hosts.
- A delivery reports a failed turn's own code only; a captured refusal's code
  is no longer stamped onto an unclassified failed turn, and a routing failure
  without a code stays unknown.
- Before recording a classified refusal, the terminal reply receipt is checked:
  an accepted reply settles the turn as delivered, and an unconfirmed one
  leaves it uncertain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…oad from when the host listens

A cold host installs its own plugin package before loading any plugin, which
can take minutes on some runners and made the minimum-host smoke time out
before this plugin ever ran. The smoke now installs that package for the
running host version into the config and project directories first, waits
for the host to listen, and gives the plugin load its own budget within an
overall cap. This plugin still goes through the host's real loader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… classify V2 compaction errors

- A refused turn with a confirmed explicit WhatsApp send settles as answered,
  and one with an unconfirmed send stays uncertain; a rejected send keeps the
  failure code.
- If the turn's lease moves to another owner while a found reply settles, the
  refusal no longer overwrites that owner's state, and other finish errors
  leave the outcome uncertain instead of recording the code.
- A failed V2 compaction keeps its provider error, and the classifier reads
  V2 response bodies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@dimavrem22 dimavrem22 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified a1c680878: refusal branch preserves lease-loss ownership and handles explicit sent/unconfirmed WhatsApp replies, and V2 compaction errors + response.body are classified. All blockers resolved — LGTM!

@alex-w-99
alex-w-99 merged commit 1d19f97 into main Dec 3, 2026
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants