Skip to content
TypeScript

fix(opencode): Claude usage limits through OpenCode keep their reset time (#29301)

Julius Marminge6f41364805e3
diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
index b4c319e8e9a..bb52bb92dae 100644
--- a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+++ b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
@@ -563,6 +563,22 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
status: 429,
},
},
+ {
+ name: "a Claude limit without a reset timestamp",
+ failureClass: "usage_limit",
+ error: {
+ type: "provider.unknown",
+ message: "You've hit your session limit · resets 5:20pm (Europe/Berlin)",
+ },
+ },
+ {
+ name: "an unrelated provider.unknown failure",
+ failureClass: "provider_error",
+ error: {
+ type: "provider.unknown",
+ message: "Request failed. Resets at 2026-11-02T20:00:00Z.",
+ },
+ },
] as const)("classifies $name as $failureClass", ({ error, failureClass }) =>
Effect.gen(function* () {
const { runtime, thread } = yield* resumed([
@@ -583,6 +599,35 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
}).pipe(Effect.scoped),
);
+ // regression: #29289 Claude weekly limits through claude-agent lost their usage-limit reset
+ it.effect(
+ "classifies a Claude weekly limit as a usage limit that resets at its reported time",
+ () =>
+ Effect.gen(function* () {
+ const message =
+ "You've hit your weekly limit · resets 9pm (Europe/Paris) Resets at 2026-11-02T20:00:00.000Z.";
+ const { runtime, thread } = yield* resumed([
+ out("session.prompt", { sessionID: SESSION, text: "<any>" }),
+ promptAccepted,
+ event("session.execution.failed", {
+ sessionID: SESSION,
+ error: { type: "provider.unknown", message },
+ }),
+ ]);
+ const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
+ yield* runtime.startTurn(turnInput(thread));
+ const ended = yield* Fiber.join(terminal);
+ assert.equal(ended?.status, "failed");
+ assert.deepEqual(ended?.failure, {
+ class: "usage_limit",
+ code: "provider.unknown",
+ message,
+ retryable: null,
+ resetAt: "2026-11-02T20:00:00.000Z",
+ });
+ }).pipe(Effect.scoped),
+ );
+
it.effect("ends the turn when its terminal event is one this build cannot decode", () =>
Effect.gen(function* () {
const { runtime, thread } = yield* resumed([
diff --git a/packages/provider-opencode/src/server/v2/adapter.ts b/packages/provider-opencode/src/server/v2/adapter.ts
index f49de61b246..ae74eb2908e 100644
--- a/packages/provider-opencode/src/server/v2/adapter.ts
+++ b/packages/provider-opencode/src/server/v2/adapter.ts
@@ -524,6 +524,12 @@ const wakeTurnId = (sessionId: string, attemptId: string) => `${sessionId}:wake:
const isWakeTurn = (turn: OrchestrationV2ProviderTurn) =>
turn.nativeTurnRef?.nativeId?.includes(":wake:") === true;
+// OpenCode's claude-agent reports a spent Claude plan as provider.unknown with Claude's own text.
+const claudeUsageLimit = (message: string | undefined) =>
+ message !== undefined && /^You've hit your [^\n·]*\blimit\b/.test(message)
+ ? { resetAt: /\bResets at (\d{4}-\d\d-\d\dT[\d:.]+Z)\.?\s*$/.exec(message)?.[1] ?? null }
+ : undefined;
+
const INTERRUPT_TIMEOUT = "10 seconds";
const INSTRUCTIONS_KEY = "t3-code";
const RECONNECT_ATTEMPTS = 5;
@@ -2152,14 +2158,18 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
const { type, message, status, response } = event.data.error;
// OpenCode types an empty balance as provider.quota too, but it answers
// that with HTTP 402 and it never resets. Usage windows and budgets are 429.
+ const claudeLimit = claudeUsageLimit(message);
const usageLimit =
- (type === "provider.quota" && status !== 402) || type === "provider.rate-limit";
+ (type === "provider.quota" && status !== 402) ||
+ type === "provider.rate-limit" ||
+ claudeLimit !== undefined;
return yield* endExecution(state, turn, {
status: "failed",
failure: makeProviderFailure({
message: readableProviderErrorMessage(message, status, response?.body),
code: type,
class: usageLimit ? "usage_limit" : "provider_error",
+ resetAt: claudeLimit?.resetAt ?? null,
}),
});
}