Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

chatgpt-new: older plan code names and grant errors

justrachddf58a5f4d96
diff --git a/docs/adr/0221-chatgpt-new-sign-in.md b/docs/adr/0221-chatgpt-new-sign-in.md
index c0ab6fed..7777f0b3 100644
--- a/docs/adr/0221-chatgpt-new-sign-in.md
+++ b/docs/adr/0221-chatgpt-new-sign-in.md
@@ -58,7 +58,9 @@ live on 2026-09-30, and all 45 checks behaved as documented:
with bounded backoff), `…_user_not_eligible` and `…_route_not_supported`
(403), `…_unsupported_capability` (400), and `…_invalid_user` (401; sign in
again). Any of them can also arrive mid-stream as `response.failed`, and
- OpenAI never moves the request to another billing path. An expired access
+ OpenAI never moves the request to another billing path. Some responses
+ still carry older names for the same codes (`subscription_sharing_v2_…`),
+ and a grant that does not cover the request fails with `chatpass_v2_…`. An expired access
token is a 401 with `{"detail":{"error_code":"invalid_token"}}`. A rejected
field is a 400 whose `detail` names it.
- **Tokens.** The access token is a one-hour RS256 JWT (`aud`
@@ -126,9 +128,9 @@ process rather than restarting anything.
- **Errors** follow that table. A spent allowance fails fast, on a 429 or
mid-stream, with a link to ChatGPT's usage settings, and never falls back
to another credential. The two 503s take the overload ladder (three
- retries, 1·2·4 s) and keep the credentials. Every other plan code, including
- one graff does not know yet, stops the turn; no plan code is retried as a
- gateway flake. `invalid_token` and `subscription_sharing_invalid_user` are
+ retries, 1·2·4 s) and keep the credentials. Every other plan code, under
+ either name, a `chatpass_` grant error, or one graff does not know yet,
+ stops the turn; no plan code is retried as a gateway flake. `invalid_token` and `subscription_sharing_invalid_user` are
auth errors: one refresh and retry (never before `earliest_refresh_at`),
then the error says to sign in again.
- **Compaction**: the in-stream blob carries the prompts it pruned, so a
diff --git a/src/agent_request_policy.zig b/src/agent_request_policy.zig
index 0a51d373..900e491e 100644
--- a/src/agent_request_policy.zig
+++ b/src/agent_request_policy.zig
@@ -19,7 +19,8 @@ pub fn isAuthError(msg: []const u8) bool {
util.indexOfIgnoreCase(msg, "authentication") != null or
util.indexOfIgnoreCase(msg, "invalid_api_key") != null or
util.indexOfIgnoreCase(msg, "invalid_token") != null or // OAuth bearer error (RFC 6750), the ChatGPT plan route's 401
- util.indexOfIgnoreCase(msg, "subscription_sharing_invalid_user") != null; // the plan route's other 401: sign in again
+ util.indexOfIgnoreCase(msg, "subscription_sharing_invalid_user") != null or // the plan route's other 401: sign in again
+ util.indexOfIgnoreCase(msg, "subscription_sharing_v2_invalid_user") != null; // its older name
}
test "isAuthError (#148): auth failures only, not credits/rate/other" {
@@ -30,6 +31,7 @@ test "isAuthError (#148): auth failures only, not credits/rate/other" {
try std.testing.expect(isAuthError("invalid_api_key"));
try std.testing.expect(isAuthError("invalid_token"));
try std.testing.expect(isAuthError("subscription_sharing_invalid_user"));
+ try std.testing.expect(isAuthError("subscription_sharing_v2_invalid_user"));
// NOT auth — must never trigger a refresh loop
try std.testing.expect(!isAuthError("You have run out of credits or need a Grok subscription."));
try std.testing.expect(!isAuthError("rate limit exceeded"));
diff --git a/src/agent_request_quota.zig b/src/agent_request_quota.zig
index a46c20ae..53805814 100644
--- a/src/agent_request_quota.zig
+++ b/src/agent_request_quota.zig
@@ -30,23 +30,33 @@ pub fn isQuotaExceeded(body: []const u8) bool {
// ChatGPT plan errors, as OpenAI's errors-and-recovery page classifies them
// (ADR 0221). Only a spent allowance is a limit (429, resets after hours).
const plan_limit_code = "subscription_sharing_usage_limit_exceeded";
+const plan_prefix = "subscription_sharing_";
+
+/// A plan code's reason, in either naming: some responses still carry the
+/// older `subscription_sharing_v2_` form of the same code.
+fn planReason(code: ?[]const u8) ?[]const u8 {
+ const c = code orelse return null;
+ if (!std.mem.startsWith(u8, c, plan_prefix)) return null;
+ const reason = c[plan_prefix.len..];
+ return if (std.mem.startsWith(u8, reason, "v2_")) reason["v2_".len..] else reason;
+}
pub fn isPlanUsageLimit(code: ?[]const u8) bool {
- return std.mem.eql(u8, code orelse return false, plan_limit_code);
+ return std.mem.eql(u8, planReason(code) orelse return false, "usage_limit_exceeded");
}
-/// Any ChatGPT plan code. None of them is a gateway flake: each is either a
-/// temporary outage with its own ladder or a request to stop.
+/// Any ChatGPT plan code, including the `chatpass_` grant errors. None of them
+/// is a gateway flake: each is either a temporary outage with its own ladder
+/// or a request to stop.
pub fn isPlanCode(code: ?[]const u8) bool {
- return std.mem.startsWith(u8, code orelse return false, "subscription_sharing_");
+ return planReason(code) != null or std.mem.startsWith(u8, code orelse return false, "chatpass_");
}
/// Usage or user data temporarily unavailable (503): keep the credentials and
/// retry with bounded backoff, like an overload.
pub fn isPlanTransient(code: ?[]const u8) bool {
- const c = code orelse return false;
- return std.mem.eql(u8, c, "subscription_sharing_usage_unavailable") or
- std.mem.eql(u8, c, "subscription_sharing_user_unavailable");
+ const reason = planReason(code) orelse return false;
+ return std.mem.eql(u8, reason, "usage_unavailable") or std.mem.eql(u8, reason, "user_unavailable");
}
/// Every other plan code stops the turn: a spent allowance (429), an account,
@@ -74,6 +84,7 @@ test "ChatGPT plan codes follow OpenAI's recovery table" {
try std.testing.expect(!isQuotaExceeded("{\"error\":{\"code\":\"subscription_sharing_usage_unavailable\"}}"));
try std.testing.expect(isPlanTransient("subscription_sharing_usage_unavailable"));
try std.testing.expect(isPlanTransient("subscription_sharing_user_unavailable"));
+ try std.testing.expect(isPlanTransient("subscription_sharing_v2_user_unavailable"));
try std.testing.expect(!isPlanTransient("subscription_sharing_usage_limit_exceeded"));
try std.testing.expect(!isPlanStop("subscription_sharing_usage_unavailable"));
for ([_][]const u8{
@@ -82,6 +93,9 @@ test "ChatGPT plan codes follow OpenAI's recovery table" {
"subscription_sharing_route_not_supported",
"subscription_sharing_unsupported_capability",
"subscription_sharing_invalid_user",
+ "subscription_sharing_v2_user_not_eligible",
+ "subscription_sharing_v2_client_not_enabled",
+ "chatpass_v2_scope_not_authorized",
}) |c| try std.testing.expect(isPlanStop(c));
try std.testing.expect(!isPlanCode("rate_limit_exceeded") and !isPlanCode(null) and !isPlanStop(null));
}