graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
chatgpt-new: plan limits stop at once; an expired token refreshes
justrach615dda5de0a3
diff --git a/src/agent_gateway_retry.zig b/src/agent_gateway_retry.zigindex e5364fce..39e99d03 100644--- a/src/agent_gateway_retry.zig+++ b/src/agent_gateway_retry.zig@@ -91,8 +91,9 @@ pub fn noteFlake(self: *Agent, state: *GatewayRetryState, err: anyerror) void {pub fn afterServerErrorOrParseReject(self: *Agent, etype: []const u8, code: ?[]const u8, msg: []const u8, server_retries: *usize, state: *GatewayRetryState) !bool {// A structured terminal code outranks transient wording in the message.// In particular, do not retry a model the gateway cannot serve even if- // its diagnostic also mentions capacity or a request-body parse error.- if (isModelUnavailable(code) or isStructuredOverflow(code)) return false;+ // its diagnostic also mentions capacity or a request-body parse error,+ // or a spent ChatGPT plan allowance whose message says "try again later".+ if (isModelUnavailable(code) or isStructuredOverflow(code) or policy.isPlanUsageLimit(code)) return false;if (try retryTransientServerError(self, etype, code, msg, server_retries)) return true;if (try retryShortGatewayFlake(self, etype, code, msg, server_retries)) return true;return retryBodyParseAfterTimeouts(self, msg, state);@@ -119,7 +120,7 @@ fn isModelUnavailable(code: ?[]const u8) bool {pub fn isShortGatewayFlake(etype: []const u8, code: ?[]const u8, msg: []const u8) bool {// Gateway 110-byte follow-up. etype is often invalid_request_error, which// would otherwise hard-fail on the "invalid" needle. Auth/quota still die.- if (isModelUnavailable(code) or isStructuredOverflow(code)) return false;+ if (isModelUnavailable(code) or isStructuredOverflow(code) or policy.isPlanUsageLimit(code)) return false;if (isBodyParseRejection(msg)) return true;const hard = [_][]const u8{ "invalid", "authentication", "unauthorized", "insufficient", "quota", "permission", "tool_choice", "not found" };for (hard) |n| {@@ -312,6 +313,9 @@ test "isShortGatewayFlake: internal/empty api_error retry; invalid/auth/quota dotry std.testing.expect(!isShortGatewayFlake("api_error", null, "model not found"));try std.testing.expect(!isShortGatewayFlake("service_unavailable", "model_unavailable", "Codegraff cannot serve this model right now"));try std.testing.expect(!isShortGatewayFlake("service_unavailable", "model_unavailable", "Malformed JSON in request body"));+ // A spent or unavailable ChatGPT plan allowance says "try again later"; it is not a flake.+ try std.testing.expect(!isShortGatewayFlake("", "subscription_sharing_usage_unavailable", "Usage is temporarily unavailable"));+ try std.testing.expect(!isShortGatewayFlake("", "subscription_sharing_usage_limit_exceeded", "Usage limit reached. Try again later."));try std.testing.expect(isShortGatewayFlake("invalid_request_error", null, "Body must be valid JSON"));try std.testing.expect(isShortGatewayFlake("api_error", null, "Malformed JSON in request body"));try std.testing.expect(!isShortGatewayFlake("invalid_request_error", null, "invalid prompt"));diff --git a/src/agent_request.zig b/src/agent_request.zigindex f51af1e0..69064cc9 100644--- a/src/agent_request.zig+++ b/src/agent_request.zig@@ -334,7 +334,8 @@ pub fn request(self: *Agent, tools_in: ?[]const u8) !std.json.ObjectMap {// with a working credential sitting unused. One-way// and announced — see credential_failover.if (policy.handOffExhaustedPlan(self)) continue;- self.last_api_error = std.fmt.allocPrint(self.arena, "rate limited (429): {s}", .{policy.quota_cap_marker}) catch "rate limited (429): quota exceeded";+ const plan_hint = if (std.mem.eql(u8, self.provider.id, "chatgpt-new")) " \xe2\x80\x94 " ++ @import("oauth_chatgpt.zig").usage_hint else "";+ self.last_api_error = std.fmt.allocPrint(self.arena, "rate limited (429): {s}{s}", .{ policy.quota_cap_marker, plan_hint }) catch "rate limited (429): quota exceeded";if (telemetry.g_telem) |t| t.errorEvent("quota", self.last_api_error orelse "quota exceeded");if (self.tracer) |tr| tr.api(self.label, self.sub, self.provider.model, 0, body.len, 0, 0, 0, true);return error.ApiError;diff --git a/src/agent_request_policy.zig b/src/agent_request_policy.zigindex 6016dc47..b7da3148 100644--- a/src/agent_request_policy.zig+++ b/src/agent_request_policy.zig@@ -17,7 +17,8 @@ pub fn isAuthError(msg: []const u8) bool {util.indexOfIgnoreCase(msg, "unauthorized") != null orutil.indexOfIgnoreCase(msg, "expired") != null orutil.indexOfIgnoreCase(msg, "authentication") != null or- util.indexOfIgnoreCase(msg, "invalid_api_key") != null;+ util.indexOfIgnoreCase(msg, "invalid_api_key") != null or+ util.indexOfIgnoreCase(msg, "invalid_token") != null; // OAuth bearer error (RFC 6750), the ChatGPT plan route's 401}test "isAuthError (#148): auth failures only, not credits/rate/other" {@@ -26,6 +27,7 @@ test "isAuthError (#148): auth failures only, not credits/rate/other" {try std.testing.expect(isAuthError("Unauthorized"));try std.testing.expect(isAuthError("authentication_error"));try std.testing.expect(isAuthError("invalid_api_key"));+ try std.testing.expect(isAuthError("invalid_token"));// NOT auth — must never trigger a refresh looptry std.testing.expect(!isAuthError("You have run out of credits or need a Grok subscription."));try std.testing.expect(!isAuthError("rate limit exceeded"));@@ -63,7 +65,7 @@ pub fn adoptFreshAuth(self: *Agent, fresh: oauth.FreshKey) void {fn warnUnpersistedRefresh(self: *Agent) void {const name = oauth.takePersistError() orelse return;if (self.tracer) |tr| tr.note("oauth_refresh", "refreshed token could not be written to auth.json");- self.say("[⚠ refreshed the Codex token but could not save it ({s}) — run `graff login codex` before your next session]\n", .{name}) catch {};+ self.say("[⚠ refreshed the {s} token but could not save it ({s}) — run `graff login {s}` before your next session]\n", .{ self.provider.id, name, self.provider.id }) catch {};}/// #148/#402: adopt a credential another writer has already produced BEFORE the@@ -383,6 +385,14 @@ pub fn errorCode(root: std.json.ObjectMap) ?[]const u8 {if (root.get("code")) |cv| {if (cv == .string) return cv.string;}+ // The ChatGPT plan route's 401: {"detail":{"error_code":"invalid_token"}}.+ if (root.get("detail")) |dv| {+ if (dv == .object) {+ if (dv.object.get("error_code")) |cv| {+ if (cv == .string) return cv.string;+ }+ }+ }// Responses failure events wrap the same error object under `response`.if (root.get("response")) |rv| {if (rv == .object) {@@ -432,31 +442,15 @@ test "sseKeepAliveOnly: comment-only bodies true, data/error/JSON bodies false"try std.testing.expect(!sseKeepAliveOnly("{\"error\":{\"message\":\"x\"}}"));}-/// #opencode-parity: a 429 body naming a billing/quota cap (OpenAI insufficient_quota,-/// "exceeded your current quota", "quota exceeded") — a usage limit a retry can't-/// clear, unlike transient rate-limit throttling — so we fail fast + fail over rather-/// than burning retry attempts.-/// The phrase agent_request stamps into `last_api_error` when a 429 body named-/// a billing/credit cap rather than transient throttling. It is a const, not a-/// literal spelled twice, because a second reader now depends on it: a worker's-/// in-turn retry ladder (subagent_retry.hardQuotaCap) reads this marker back-/// out of the message to tell "the account is capped" — where re-asking is-/// pure waste — from "slow down", where re-asking is the whole point.-pub const quota_cap_marker = "quota/billing cap";--pub fn isQuotaExceeded(body: []const u8) bool {- return util.indexOfIgnoreCase(body, "insufficient_quota") != null or- util.indexOfIgnoreCase(body, "insufficient quota") != null or- util.indexOfIgnoreCase(body, "exceeded your current quota") != null or- util.indexOfIgnoreCase(body, "quota exceeded") != null;-}+// Quota caps and ChatGPT plan usage limits live in agent_request_quota.zig+// (600-line cap); re-exported so call sites keep saying `policy.`.+const quota = @import("agent_request_quota.zig");+pub const quota_cap_marker = quota.quota_cap_marker;+pub const isQuotaExceeded = quota.isQuotaExceeded;+pub const isPlanUsageLimit = quota.isPlanUsageLimit;-test "isQuotaExceeded (#opencode-parity): billing cap detected, transient throttle not" {- try std.testing.expect(isQuotaExceeded("{\"error\":{\"code\":\"insufficient_quota\",\"message\":\"You exceeded your current quota\"}}"));- try std.testing.expect(isQuotaExceeded("Quota Exceeded for this key"));- // transient rate-limit -> NOT a quota cap; must still retry- try std.testing.expect(!isQuotaExceeded("Rate limit reached. Please try again in 20s."));- try std.testing.expect(!isQuotaExceeded("429 too many requests"));+test {+ _ = quota;}test "errorCode (#203): pulls root.error.code (openai/lmstudio), falls back to root.code, else null" {diff --git a/src/agent_request_quota.zig b/src/agent_request_quota.zignew file mode 100644index 00000000..b57a4913--- /dev/null+++ b/src/agent_request_quota.zig@@ -0,0 +1,49 @@+//! Quota caps and ChatGPT plan usage limits: a limit a retry cannot clear,+//! unlike transient throttling. Split out of agent_request_policy.zig for the+//! 600-line cap and re-exported there, so call sites keep saying `policy.`.++const std = @import("std");+const util = @import("util.zig");++/// #opencode-parity: a 429 body naming a billing/quota cap (OpenAI insufficient_quota,+/// "exceeded your current quota", "quota exceeded") — a usage limit a retry can't+/// clear, unlike transient rate-limit throttling — so we fail fast + fail over rather+/// than burning retry attempts.+/// The phrase agent_request stamps into `last_api_error` when a 429 body named+/// a billing/credit cap rather than transient throttling. It is a const, not a+/// literal spelled twice, because a second reader now depends on it: a worker's+/// in-turn retry ladder (subagent_retry.hardQuotaCap) reads this marker back+/// out of the message to tell "the account is capped" — where re-asking is+/// pure waste — from "slow down", where re-asking is the whole point.+pub const quota_cap_marker = "quota/billing cap";++pub fn isQuotaExceeded(body: []const u8) bool {+ // ChatGPT plan usage limits (ADR 0221): stop, never retry into them.+ return util.indexOfIgnoreCase(body, plan_usage_code_prefix) != null or+ util.indexOfIgnoreCase(body, "insufficient_quota") != null or+ util.indexOfIgnoreCase(body, "insufficient quota") != null or+ util.indexOfIgnoreCase(body, "exceeded your current quota") != null or+ util.indexOfIgnoreCase(body, "quota exceeded") != null;+}++/// ChatGPT plan usage (ADR 0221): `subscription_sharing_usage_limit_exceeded`+/// (the allowance is spent) and `subscription_sharing_usage_unavailable`. Both+/// can arrive mid-stream as response.failed; neither clears in seconds.+const plan_usage_code_prefix = "subscription_sharing_usage";++pub fn isPlanUsageLimit(code: ?[]const u8) bool {+ const c = code orelse return false;+ return std.mem.startsWith(u8, c, plan_usage_code_prefix);+}++test "isQuotaExceeded (#opencode-parity): billing cap detected, transient throttle not" {+ try std.testing.expect(isQuotaExceeded("{\"error\":{\"code\":\"insufficient_quota\",\"message\":\"You exceeded your current quota\"}}"));+ try std.testing.expect(isQuotaExceeded("Quota Exceeded for this key"));+ try std.testing.expect(isQuotaExceeded("{\"error\":{\"code\":\"subscription_sharing_usage_limit_exceeded\"}}"));+ try std.testing.expect(isQuotaExceeded("{\"error\":{\"code\":\"subscription_sharing_usage_unavailable\"}}"));+ try std.testing.expect(isPlanUsageLimit("subscription_sharing_usage_unavailable"));+ try std.testing.expect(!isPlanUsageLimit("rate_limit_exceeded") and !isPlanUsageLimit(null));+ // transient rate-limit -> NOT a quota cap; must still retry+ try std.testing.expect(!isQuotaExceeded("Rate limit reached. Please try again in 20s."));+ try std.testing.expect(!isQuotaExceeded("429 too many requests"));+}diff --git a/src/agent_responses.zig b/src/agent_responses.zigindex 26d6b5f9..47be07bf 100644--- a/src/agent_responses.zig+++ b/src/agent_responses.zig@@ -111,7 +111,7 @@ pub fn parseResponses(self: *Agent, body: []const u8) !ResponsesResult {/// Each field is single-lined and bounded before it reaches last_api_error or/// the default-on trace.pub fn failureDiagnostic(allocator: std.mem.Allocator, provider: []const u8, failure: ResponsesFailure) ![]u8 {- var buf: [672]u8 = undefined;+ var buf: [768]u8 = undefined;var w: std.Io.Writer = .fixed(&buf);try writeDiagnosticField(&w, provider, 40);try w.writeAll(" api error");@@ -127,9 +127,21 @@ pub fn failureDiagnostic(allocator: std.mem.Allocator, provider: []const u8, faitry writeDiagnosticField(&w, rid, 96);try w.writeByte(']');}+ // A spent ChatGPT plan allowance, often mid-stream (ADR 0221).+ if (policy.isPlanUsageLimit(failure.code)) try w.writeAll(" \xe2\x80\x94 " ++ @import("oauth_chatgpt.zig").usage_hint);+ // Reached only after the one refresh-and-retry could not help.+ if (std.mem.eql(u8, provider, "chatgpt-new") and policy.isAuthError(failure.message)) try w.writeAll(" \xe2\x80\x94 sign in again with `graff login chatgpt-new`");return allocator.dupe(u8, w.buffered());}+test "failureDiagnostic points a spent ChatGPT plan at its usage page" {+ const a = std.testing.allocator;+ const d = try failureDiagnostic(a, "chatgpt-new", .{ .message = "Usage limit reached.", .code = "subscription_sharing_usage_limit_exceeded" });+ defer a.free(d);+ try std.testing.expect(std.mem.startsWith(u8, d, "chatgpt-new api error [subscription_sharing_usage_limit_exceeded]: Usage limit reached."));+ try std.testing.expect(std.mem.endsWith(u8, d, "https://chatgpt.com/settings/usage"));+}+fn writeDiagnosticField(w: *std.Io.Writer, raw: []const u8, max: usize) !void {const prefix = util.utf8Prefix(raw, max);for (prefix) |b| try w.writeByte(if (b < 0x20 or b == 0x7f) ' ' else b);@@ -260,11 +272,36 @@ pub fn errorMessage(obj: std.json.ObjectMap) ?[]const u8 {if (e.object.get("message")) |m| if (m == .string) return m.string;};};- if (obj.get("detail")) |d| if (d == .string) return d.string;+ if (obj.get("detail")) |d| switch (d) {+ .string => |s| return s,+ // The ChatGPT plan route's 401: {"detail":{"error_code":"invalid_token"}}.+ .object => |o| {+ if (o.get("message")) |m| if (m == .string) return m.string;+ if (o.get("error_code")) |c| if (c == .string) return c.string;+ },+ else => {},+ };if (obj.get("message")) |m| if (m == .string) return m.string;return null;}+test "parseResponses reads the ChatGPT plan route's expired-token envelope" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ var agent: Agent = undefined;+ agent.arena = arena_state.allocator();+ agent.scratch_arena = null;+ agent.message_mutation_arena = null;+ switch (try parseResponses(&agent, "{\"detail\":{\"error_code\":\"invalid_token\"}}\n")) {+ .err => |failure| {+ try std.testing.expectEqualStrings("invalid_token", failure.message);+ try std.testing.expectEqualStrings("invalid_token", failure.code.?);+ try std.testing.expect(policy.isAuthError(failure.message));+ },+ .ok => return error.TestUnexpectedResult,+ }+}+/// Public error envelopes may carry a join id (`error.request_id`, top-level/// `request_id`, or the Responses `response.error` copy). Empty strings are/// treated as absent so a missing field never prints as `[]`.