graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
chatgpt-new: keep OpenAI's answer to a refused renewal
justrach6efedf917bc7
diff --git a/docs/adr/0221-chatgpt-new-sign-in.md b/docs/adr/0221-chatgpt-new-sign-in.mdindex fbd77c65..6fa4c8d9 100644--- a/docs/adr/0221-chatgpt-new-sign-in.md+++ b/docs/adr/0221-chatgpt-new-sign-in.md@@ -60,16 +60,23 @@ live on 2026-09-30, and all 45 checks behaved as documented:again). Any of them can also arrive mid-stream as `response.failed`, andOpenAI never moves the request to another billing path. Some responsesstill 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.+ 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``https://api.openai.com/v1`, `iss` `https://auth.openai.com`, the issued`client_id`, the granted scopes, and opaque OpenAI metadata graff never- reads). A refresh returns a new access token and a replacement refresh- token; refresh tokens last 30 days, renewed by every refresh.- `earliest_refresh_at` falls six minutes before expiry, and a refresh- before it answers `invalid_grant` and spends the refresh token.+ reads). OpenAI documents that a refresh returns a new access token and a+ replacement refresh token, and that refresh tokens last 30 days, renewed+ by every refresh. `earliest_refresh_at` falls six minutes before expiry.+- **Refresh does not work yet.** Every refresh tried so far was refused with+ HTTP 400 `{"error":"invalid_grant"}` and no description. That includes an+ unused refresh token sent 63 seconds after `earliest_refresh_at` with+ exactly the documented request (the issued `client_id`, the refresh token+ and `resource`, no `scope`) from a clock in step with OpenAI's. Until that+ changes a sign-in lasts one hour: a turn still running when the access+ token expires fails with `invalid_token`, and the person signs in again.OpenAI also documents running Codex app-server on the same token (stdio toapp-server, then HTTP/SSE, with `supports_websockets=false`). Graff calls@@ -108,13 +115,15 @@ process rather than restarting anything.split and the stale one spends the sign-in. Refresh sends `resource` and theissued client id five minutes before expiry (inside the six-minute window)or after a 401, under the existing refresh mutex, and never before- `earliest_refresh_at`: OpenAI answers an early refresh with- `invalid_grant`, which spends the refresh token. A lock file beside the+ `earliest_refresh_at`, which OpenAI names as the earliest renewal. A lock+ file beside therecord serializes refreshes across graff processes, as OpenAI asks: theloser of a race would present a replaced token (`refresh_token_reused`) andforce a new sign-in, so it re-reads the record under the lock and adopts- the winner's token. An unusable refresh token is cleared; a network- failure keeps everything.+ the winner's token. OpenAI's answer to a refused renewal (time, code and+ description) is kept in the record as `last_refresh_error`; a code OpenAI+ calls unusable also clears the refresh token, and a network failure keeps+ everything.`graff logout chatgpt-new` revokes the refresh token and keeps the client id andhost id.- **Request shape**: no prompt-cache options or anchor (those stay keyed to@@ -153,7 +162,36 @@ lists both. Hosted use (the gateway, hosted sandboxes) is out of scope;OpenAI requires a separate agreement for it. The live account catalog is afollow-up, so the offline model rows can lag a rollout. HTTP requests re-sendthe whole input and caching is intermittent, so one-shot runs pay the most;-live turns chain on the socket instead. A refresh before-`earliest_refresh_at` spends the refresh token, so that gate must hold. A-sign-in lives as long as graff refreshes it within 30 days; after a longer-gap the error says to run `graff login chatgpt-new` again.+live turns chain on the socket instead. While OpenAI refuses refreshes, a+sign-in lasts an hour and a longer task stops at the hour with a sign-in+error; once refresh works, a sign-in lives as long as graff refreshes it+within 30 days.++## Compared with the Codex route++The same probes and the same graff build ran against both ChatGPT-plan+routes on 2026-09-30, with gpt-6.1-sol:++- **Same on both.** The model list and context windows (272k by default,+ 872k at most). The request rules: `store: false`, `stream: true`, array+ input, no system-role items, and the same refused fields (`temperature`,+ `max_output_tokens`, `metadata`, `user`, `previous_response_id` over+ HTTP and the others listed above). Function and custom tools (flat, in a+ namespace, or as `additional_tools`), parallel tool calls, `web_search`,+ structured output, reasoning summaries, image and PDF input,+ `service_tier`, verbosity and in-stream compaction. On the socket:+ chaining on the same connection only, a `generate: false` prewarm, and+ steering a GPT-6 answer. In graff, a tool call's follow-up rides the+ socket as a delta on both routes, and a forced compaction kept every fact+ a later turn asked for.+- **Only on Codex.** Hosted image generation, hosted `tool_search`, the+ `codex.rate_limits` stream events and the plan windows `/usage` shows, and+ working token refresh.+- **On neither.** File search, Code Interpreter, computer use, hosted MCP,+ programmatic tool calling and audio input.++For the harness, moving to the new route means deferred tools load through+graff's own `load_tool_schemas` rather than hosted tool search (graff+already does this on every provider except codex), image generation comes+from another provider, `/usage` can only link to ChatGPT's usage settings,+and a sign-in lasts an hour until OpenAI's refresh works.diff --git a/src/oauth_chatgpt.zig b/src/oauth_chatgpt.zigindex 7d955b85..40255217 100644--- a/src/oauth_chatgpt.zig+++ b/src/oauth_chatgpt.zig@@ -94,6 +94,8 @@ pub const Record = struct {expires_at: i64 = 0,earliest_refresh_at: i64 = 0,scopes: []const []const u8 = &.{},+ /// The last refused renewal: when, OpenAI's code and its description.+ refresh_error: []const u8 = "",pub fn planEnabled(r: Record) bool {for (r.scopes) |s| if (std.mem.eql(u8, s, plan_scope)) return true;@@ -122,6 +124,7 @@ pub fn parseRecord(arena: Allocator, bytes: []const u8) ?Record {.refresh = strFieldObj(o, "refresh_token") orelse "",.expires_at = util.intFieldObj(o, "expires_at", 0),.earliest_refresh_at = util.intFieldObj(o, "earliest_refresh_at", 0),+ .refresh_error = strFieldObj(o, "last_refresh_error") orelse "",};if (o.get("scopes")) |s| if (s == .array) {var list: std.ArrayList([]const u8) = .empty;@@ -148,6 +151,7 @@ pub fn serializeRecord(arena: Allocator, r: Record, saved_at: []const u8) ![]confor (r.scopes) |s| try list.append(.{ .string = s });try obj.put(arena, "scopes", .{ .array = list });try obj.put(arena, "saved_at", .{ .string = saved_at });+ if (r.refresh_error.len > 0) try obj.put(arena, "last_refresh_error", .{ .string = r.refresh_error });var aw: Io.Writer.Allocating = .init(arena);var json: std.json.Stringify = .{ .writer = &aw.writer };try json.write(Value{ .object = obj });@@ -452,10 +456,16 @@ pub fn refreshLocked(io: Io, gpa: Allocator, arena: Allocator, home: []const u8,/// OpenAI: clear an unusable refresh token so no process replays it. The/// access token stays until it expires; after that the request error says to/// sign in again. Called with the record lock held.-pub fn dropRefresh(io: Io, arena: Allocator, home: []const u8, r: Record) void {- var cleared = r;- cleared.refresh = "";- writeRecord(io, arena, home, cleared) catch {};+/// Keep OpenAI's answer to a refused renewal in the record: otherwise the+/// refresh fails silently and only the later 401 shows. A code OpenAI calls+/// unusable also clears the refresh token; the access token stays until it+/// expires, and the issued client id lets the next sign-in skip registration.+pub fn recordRefreshFailure(io: Io, arena: Allocator, home: []const u8, r: Record, code: []const u8, description: []const u8) void {+ var next = r;+ var stamp: [24]u8 = undefined;+ next.refresh_error = std.fmt.allocPrint(arena, "{s} {s}: {s}", .{ helpers.rfc3339Utc(&stamp, util.unixMs(io)), code, description }) catch code;+ if (helpers.permanentRefreshFailure(code)) next.refresh = "";+ writeRecord(io, arena, home, next) catch {};}fn refreshed(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, r: Record) ?Record {@@ -464,10 +474,9 @@ fn refreshed(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, r: Recoconst body = form(arena, &.{ .{ "grant_type", "refresh_token" }, .{ "client_id", r.client_id }, .{ "refresh_token", r.refresh }, .{ "resource", resource } }) catch return null;const tok = helpers.oauthFormPost(io, gpa, arena, token_url, body) catch return null; // transient: keep everythingif (tok.get("error")) |e| {- if (helpers.permanentRefreshFailure(if (e == .string) e.string else "")) {- dead_refresh = id;- dropRefresh(io, arena, home, r);- }+ const code = if (e == .string) e.string else "";+ if (helpers.permanentRefreshFailure(code)) dead_refresh = id;+ recordRefreshFailure(io, arena, home, r, code, strFieldObj(tok, "error_description") orelse "");return null;}const now_s = @divTrunc(util.unixMs(io), 1000);diff --git a/src/oauth_chatgpt_tests.zig b/src/oauth_chatgpt_tests.zigindex 4134de28..fffc428e 100644--- a/src/oauth_chatgpt_tests.zig+++ b/src/oauth_chatgpt_tests.zig@@ -237,7 +237,7 @@ test "ChatGPT sign-in: a sign-in saved before the move is adopted, never copied"try std.testing.expectEqual(@as(u32, 0o600), (try dir.statFile(io, "chatgpt-new.json", .{})).permissions.toMode() & 0o777);}-test "ChatGPT sign-in: an unusable refresh token is cleared and the access token kept" {+test "ChatGPT sign-in: a refused renewal is recorded; an unusable refresh token is cleared, the access token kept" {const io = std.testing.io;var arena = std.heap.ArenaAllocator.init(std.testing.allocator);defer arena.deinit();@@ -248,9 +248,15 @@ test "ChatGPT sign-in: an unusable refresh token is cleared and the access tokenconst home = try tmpHome(&tmp, &buf);const r = chatgpt.parseRecord(a, probe_record).?;try saveIn(tmp.dir, a, r);- chatgpt.dropRefresh(io, a, home, r);- const saved = chatgpt.parseRecord(a, try std.Io.Dir.cwd().readFileAlloc(io, chatgpt.recordPath(a, home), a, .limited(64 * 1024))).?;+ // A transient code keeps the refresh token for a later try, but says why.+ chatgpt.recordRefreshFailure(io, a, home, r, "temporarily_unavailable", "try later");+ var saved = chatgpt.parseRecord(a, try std.Io.Dir.cwd().readFileAlloc(io, chatgpt.recordPath(a, home), a, .limited(64 * 1024))).?;+ try std.testing.expectEqualStrings("ref-1", saved.refresh);+ try std.testing.expect(std.mem.endsWith(u8, saved.refresh_error, " temporarily_unavailable: try later"));+ chatgpt.recordRefreshFailure(io, a, home, r, "invalid_grant", "");+ saved = chatgpt.parseRecord(a, try std.Io.Dir.cwd().readFileAlloc(io, chatgpt.recordPath(a, home), a, .limited(64 * 1024))).?;try std.testing.expectEqualStrings("", saved.refresh);+ try std.testing.expect(std.mem.endsWith(u8, saved.refresh_error, " invalid_grant: "));try std.testing.expectEqualStrings("tok-1", saved.access); // usable until it expirestry std.testing.expectEqualStrings("oaiapp_x", saved.client_id); // the next sign-in skips registrationtry std.testing.expect(@import("oauth_helpers.zig").permanentRefreshFailure("refresh_token_reused"));