Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

feat(core): a provider 429 is classified apart from a transport drop (#1114)

lmlihengde709bd09fc2
diff --git a/changelog/unreleased/2026-10-15-rate-limited-error-code.md b/changelog/unreleased/2026-10-15-rate-limited-error-code.md
new file mode 100644
index 00000000..080ab532
--- /dev/null
+++ b/changelog/unreleased/2026-10-15-rate-limited-error-code.md
@@ -0,0 +1,36 @@
+# A provider 429 is classified apart from a transport drop
+
+- **Date:** 2026-10-15
+- **Type:** feature
+- **Scope:** `core`
+
+[中文版](2026-10-15-rate-limited-error-code.zh.md)
+
+Ported from upstream PenguinHarness (#1114, commit `77036d31`).
+
+A 429 and a dropped connection were the same `error_code` — `network` — so nothing downstream could
+tell "the wire blipped, try again in a moment" from "this model has no quota left, and the same
+request will 429 again after every backoff rung".
+
+## What changes
+
+- **`ErrorCode` gains `rate_limited`**: the provider answered 429, which is its rate limit or a
+ quota that is exhausted for now. `isRateLimitError` (exported beside `isFatalProviderRejection`)
+ reads it off any level of the error's `cause` chain, the way the fatal check does.
+- **The outcome stays `retryable`.** A short burst is exactly what the reconnect ladder is for, and
+ the classification does not change when the run gives up or what it retries — it only names the
+ cause.
+- Everything else about the vocabulary is unchanged: a transport drop is still `network`, a 5xx is
+ still `network`, and the `request_end` event carries the code as `error_code` exactly as before.
+
+## Why a host wants it
+
+The two failures call for different actions. One is waited out; the other is a Session that has
+somewhere else to run — given the harness now owns an in-session model switch
+(`Session.switchModel`, the `model_switch` record, and the server's `updateModel` write), telling
+them apart in the outcome is what makes that decision possible at all, and it is the first step of
+moving a rate-limited Session onto another model rather than spending the ~60 s ladder on a quota
+that is gone.
+
+It also reaches observability unchanged: the Cost center's errors panel groups on `error_code`, so
+rate limits stop hiding inside the network bucket.
diff --git a/changelog/unreleased/2026-10-15-rate-limited-error-code.zh.md b/changelog/unreleased/2026-10-15-rate-limited-error-code.zh.md
new file mode 100644
index 00000000..6e1a3308
--- /dev/null
+++ b/changelog/unreleased/2026-10-15-rate-limited-error-code.zh.md
@@ -0,0 +1,31 @@
+# 把 provider 的 429 与传输中断分开归类
+
+- **Date:** 2026-10-15
+- **Type:** feature
+- **Scope:** `core`
+
+[English](2026-10-15-rate-limited-error-code.md)
+
+移植自上游 PenguinHarness(#1114,提交 `77036d31`)。
+
+429 与连接中断此前是同一个 `error_code`——`network`——于是下游无从区分「线路抖了一下,稍等再试」与
+「这个模型已经没有配额了,同一请求在每一级退避之后仍会 429」。
+
+## 改了什么
+
+- **`ErrorCode` 增加 `rate_limited`**:provider 回答了 429,即它的速率限制,或一份暂时耗尽的配额。
+ `isRateLimitError`(与 `isFatalProviderRejection` 并列导出)沿错误 `cause` 链的任一层读出它,读法与
+ fatal 判定一致。
+- **结果状态仍是 `retryable`。** 短暂的突发正是重连阶梯要处理的情形;这次分类不改变该轮何时放弃、重试
+ 什么,只是把原因命名出来。
+- 词汇表其余部分不变:传输中断仍是 `network`,5xx 仍是 `network`,`request_end` 事件照旧以 `error_code`
+ 携带该码。
+
+## 宿主为什么需要它
+
+两种故障需要不同的动作。一种等一等就过去了;另一种是「这段会话在别处还有地方可跑」——既然 harness 现在
+自带了会话内的模型切换(`Session.switchModel`、`model_switch` 记录,以及服务端的 `updateModel` 落库),
+在结果里把两者分开,正是做出这个判断的前提,也是「把被限流的会话迁到另一个模型、而不是把约 60 秒的阶梯
+耗在一份已经没有的配额上」的第一步。
+
+它对可观测性同样直接生效:Cost 中心的错误面板按 `error_code` 分组,限流因此不再藏在 network 桶里。
diff --git a/packages/core/src/llm/generative-model.ts b/packages/core/src/llm/generative-model.ts
index abd3a8ca..99229c0a 100644
--- a/packages/core/src/llm/generative-model.ts
+++ b/packages/core/src/llm/generative-model.ts
@@ -901,6 +901,21 @@ export function isFastModeUnsupportedError(error: unknown): boolean {
export const FAST_MODE_UNSUPPORTED_GUIDANCE =
"Fast mode is enabled for this model; turn it off in the model settings to use this model.";
+/**
+ * Whether any level of the error chain is an HTTP **429** — the provider's rate limit, or a
+ * quota that is exhausted for now. The outcome stays `retryable` (a short burst is exactly
+ * what the reconnect ladder is for), but it is classified apart from a transport drop: the
+ * code is what lets a host tell "the wire blipped" from "this model has no quota left", and
+ * act on the second.
+ */
+export function isRateLimitError(error: unknown): boolean {
+ return anyInCauseChain(error, (level) => {
+ const err = level as { status?: unknown; statusCode?: unknown };
+ const status = typeof err.status === "number" ? err.status : err.statusCode;
+ return status === 429;
+ });
+}
+
/**
* Determines whether an error is a definitive provider rejection of the request itself —
* the `fatal` detector for errored requests. Only an explicit HTTP client-error status
@@ -1281,8 +1296,18 @@ export class GenerativeModel implements LLMInterface {
outcome = { status: "fatal", errorCode: "rejected", errorMessage: describeError(error) };
} else if ((error as { name?: string })?.name === "AbortError") {
outcome = { status: "aborted" }; // Fallback: an unexpected abort (neither timeout nor user)
+ } else if (isRateLimitError(error)) {
+ // A 429: the provider's rate limit, or a quota with nothing left in it. Retryable
+ // like a transport drop — the ladder may well outlast a burst — but classified
+ // apart, because a host that has somewhere else to run the Session can tell the two
+ // apart and move it.
+ outcome = {
+ status: "retryable",
+ errorCode: "rate_limited",
+ errorMessage: describeError(error),
+ };
} else {
- // Everything else — network/transport drops, 429/5xx, and anything unclassifiable —
+ // Everything else — network/transport drops, 5xx, and anything unclassifiable —
// retries on the engine's ladder. The detail rides on the outcome so observability
// (request_end -> the Cost center's errors panel) shows the real reason behind a
// retried request.
diff --git a/packages/core/src/llm/index.ts b/packages/core/src/llm/index.ts
index 7dcfa7d0..a6467edf 100644
--- a/packages/core/src/llm/index.ts
+++ b/packages/core/src/llm/index.ts
@@ -15,6 +15,7 @@ export {
isMalformedJsonParseError,
isIncompleteStreamError,
isFatalProviderRejection,
+ isRateLimitError,
isAuthenticationError,
isFastModeUnsupportedError,
FAST_MODE_UNSUPPORTED_GUIDANCE,
diff --git a/packages/core/src/omnimessage/types.ts b/packages/core/src/omnimessage/types.ts
index 42fd8d78..abe6b635 100644
--- a/packages/core/src/omnimessage/types.ts
+++ b/packages/core/src/omnimessage/types.ts
@@ -343,7 +343,8 @@ export type ErrorCode =
// LLM request failures (request_end / compaction_end; the status carries the retry
// decision, the code carries the classified cause).
| "timeout" // idle timeout / connection went silent
- | "network" // transport drops, provider 429/5xx, and anything unclassifiable
+ | "network" // transport drops, provider 4xx/5xx and anything unclassifiable
+ | "rate_limited" // the provider answered 429: its rate limit or an exhausted quota
| "malformed" // response failed parsing/validation, or the stream was truncated
| "auth" // the provider rejected the credentials
| "rejected" // a definitive provider 4xx rejection (params, quota; 408/429 excluded)
diff --git a/packages/core/test/llm.test.ts b/packages/core/test/llm.test.ts
index 9707d5e3..7b3d159d 100644
--- a/packages/core/test/llm.test.ts
+++ b/packages/core/test/llm.test.ts
@@ -30,6 +30,7 @@ import {
isFastModeUnsupportedError,
isMalformedJsonParseError,
isFatalProviderRejection,
+ isRateLimitError,
FAST_MODE_UNSUPPORTED_GUIDANCE,
mapThinkingLevel,
mergeOmniToUniMessage,
@@ -1742,6 +1743,11 @@ describe("GenerativeModel.streamGenerate outcome classification (PRN-013)", () =
throw Object.assign(new Error("invalid api key"), { status: 401 });
}
+ // The provider answered 429: its rate limit, or a quota with nothing left in it.
+ async function* rateLimited(): AsyncGenerator<UniEvent> {
+ throw Object.assign(new Error("rate limit exceeded"), { status: 429 });
+ }
+
// AgentHub's response body is not valid JSON (e.g. the gateway returns HTML / a truncated response).
async function* malformedJsonAfterText(): AsyncGenerator<UniEvent> {
yield ev({ content_items: [{ type: "text", text: "hi" }] });
@@ -1849,6 +1855,17 @@ describe("GenerativeModel.streamGenerate outcome classification (PRN-013)", () =
expect(messages.map(typeOf)).not.toContain("token_usage");
});
+ it("classifies a 429 apart from a transport drop, so a host can move the Session off it", async () => {
+ const model = new SeamModel(() => rateLimited());
+ const { outcome } = await drain(model.streamGenerate({ newMessages: [userText("go")] }));
+ // Still retryable — a short burst is exactly what the ladder is for — but named, so a
+ // host with somewhere else to run can tell "the wire blipped" from "no quota left".
+ expect(outcome.status).toBe("retryable");
+ expect(outcome.errorCode).toBe("rate_limited");
+ expect(isRateLimitError(Object.assign(new Error("x"), { status: 429 }))).toBe(true);
+ expect(isRateLimitError(new Error("socket hang up"))).toBe(false);
+ });
+
it("classifies an AgentHub JSON parse exception as retryable and closes partial output", async () => {
const model = new SeamModel(() => malformedJsonAfterText());
const { messages, outcome } = await drain(