graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
chatgpt-new: serialize token refreshes across graff processes
justrach389dd01d9ac2
diff --git a/docs/adr/0221-chatgpt-new-sign-in.md b/docs/adr/0221-chatgpt-new-sign-in.mdindex df550f44..f57d10ee 100644--- a/docs/adr/0221-chatgpt-new-sign-in.md+++ b/docs/adr/0221-chatgpt-new-sign-in.md@@ -99,7 +99,12 @@ process rather than restarting anything.issued 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.+ `invalid_grant`, which spends the refresh token. A lock file beside the+ record serializes refreshes across graff processes, as OpenAI asks: the+ loser of a race would present a replaced token (`refresh_token_reused`) and+ force 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.`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 todiff --git a/src/oauth_chatgpt.zig b/src/oauth_chatgpt.zigindex a6ed1fae..8188fe4b 100644--- a/src/oauth_chatgpt.zig+++ b/src/oauth_chatgpt.zig@@ -385,13 +385,50 @@ pub fn login(io: Io, gpa: Allocator, arena: Allocator, home: []const u8) !void {var dead_refresh: u64 = 0;+/// OpenAI rotates the refresh token on every grant and asks apps to serialize+/// refreshes for one session: a process that loses the race presents a+/// replaced token (`refresh_token_reused`) and the person must sign in again.+/// The lock sits beside the record (flock / NtLockFile). A filesystem without+/// working locks degrades to the unlocked refresh.+pub fn lockRecord(io: Io, arena: Allocator, home: []const u8) ?Io.File {+ ensureDirs(io, arena, home);+ const path = std.fmt.allocPrint(arena, "{s}.lock", .{recordPath(arena, home)}) catch return null;+ return Io.Dir.cwd().createFile(io, path, .{ .truncate = false, .lock = .exclusive }) catch null;+}++/// Refresh under the lock, against the record as it is now: another graff+/// may have refreshed while this one waited, and its replacement is then the+/// only live refresh token. `before` is what the caller read before locking.+/// Null when the sign-in is gone.+pub fn refreshLocked(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, before: Record) ?[]const u8 {+ const lock = lockRecord(io, arena, home);+ defer if (lock) |f| f.close(io);+ const current = readRecord(io, arena, home) orelse return null;+ if (current.access.len == 0 or !current.planEnabled()) return null;+ if (!std.mem.eql(u8, current.refresh, before.refresh)) return current.access;+ const next = refreshed(io, gpa, arena, home, current) orelse return current.access;+ return next.access;+}++/// 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 {};+}+fn refreshed(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, r: Record) ?Record {const id = std.hash.Wyhash.hash(0, r.refresh);if (id == dead_refresh) return null; // known dead: no round tripconst 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+ 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;+ if (helpers.permanentRefreshFailure(if (e == .string) e.string else "")) {+ dead_refresh = id;+ dropRefresh(io, arena, home, r);+ }return null;}const now_s = @divTrunc(util.unixMs(io), 1000);@@ -414,8 +451,9 @@ fn refreshed(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, r: Reco}/// The access token for requests, refreshed near expiry or when `force`d by-/// a 401. Null when signed out or when plan usage was not granted. Callers-/// hold oauth.refreshOAuthKey's mutex, which serializes rotating refreshes.+/// a 401. Null when signed out or when plan usage was not granted.+/// oauth.refreshOAuthKey's mutex serializes refreshes within this process and+/// the record lock serializes them across graff processes.pub fn loadChatgptOAuth(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, force: bool, stale: ?[]const u8) ?[]const u8 {const r = readRecord(io, arena, home) orelse return null;if (r.access.len == 0 or !r.planEnabled()) return null;@@ -426,8 +464,7 @@ pub fn loadChatgptOAuth(io: Io, gpa: Allocator, arena: Allocator, home: []constif (now_s < r.earliest_refresh_at) return r.access;const due = r.expires_at != 0 and now_s >= r.expires_at - refresh_margin_s;if (!(force or due) or r.refresh.len == 0) return r.access;- const next = refreshed(io, gpa, arena, home, r) orelse return r.access;- return next.access;+ return refreshLocked(io, gpa, arena, home, r);}/// Sign out: revoke the refresh token, then clear the tokens. The app id,diff --git a/src/oauth_chatgpt_tests.zig b/src/oauth_chatgpt_tests.zigindex 378da677..24715c5a 100644--- a/src/oauth_chatgpt_tests.zig+++ b/src/oauth_chatgpt_tests.zig@@ -152,3 +152,80 @@ test "ChatGPT sign-in: loading needs plan usage and never refreshes a fresh toketry write.record(tmp.dir, a, r);try std.testing.expect(chatgpt.loadChatgptOAuth(io, std.testing.allocator, a, home, false, null) == null);}++fn tmpHome(tmp: *std.testing.TmpDir, buf: *[std.fs.max_path_bytes]u8) ![]const u8 {+ const io = std.testing.io;+ try tmp.dir.createDir(io, ".openai", credential_store.private_dir);+ try tmp.dir.createDir(io, ".openai/credentials", credential_store.private_dir);+ return buf[0..try tmp.dir.realPath(io, buf)];+}++fn saveIn(dir: std.Io.Dir, alloc: std.mem.Allocator, r: chatgpt.Record) !void {+ try credential_store.replaceFile(std.testing.io, dir, ".openai/credentials/graff-oauth.json", try chatgpt.serializeRecord(alloc, r, "now"), credential_store.private_file);+}++test "ChatGPT sign-in: a refresh takes a lock a second graff would block on" {+ const io = std.testing.io;+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ var buf: [std.fs.max_path_bytes]u8 = undefined;+ const home = try tmpHome(&tmp, &buf);+ const held = chatgpt.lockRecord(io, a, home) orelse return error.NoRefreshLock;+ const lock_path = try std.fmt.allocPrint(a, "{s}.lock", .{chatgpt.recordPath(a, home)});+ // flock/NtLockFile are per open file description, so a second open+ // conflicts even inside one process, exactly as another graff would.+ try std.testing.expectError(error.WouldBlock, std.Io.Dir.cwd().createFile(io, lock_path, .{ .truncate = false, .lock = .exclusive, .lock_nonblocking = true }));+ held.close(io);+ const next = try std.Io.Dir.cwd().createFile(io, lock_path, .{ .truncate = false, .lock = .exclusive, .lock_nonblocking = true });+ next.close(io);+}++test "ChatGPT sign-in: a refresh another graff already made is adopted, not repeated" {+ const io = std.testing.io;+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ var buf: [std.fs.max_path_bytes]u8 = undefined;+ const home = try tmpHome(&tmp, &buf);+ const before = chatgpt.parseRecord(a, probe_record).?; // what this process read: tok-1 / ref-1+ var rotated = before;+ rotated.access = "tok-2";+ rotated.refresh = "ref-2";+ try saveIn(tmp.dir, a, rotated);+ // The disk moved on while this process waited for the lock: its token is+ // adopted with no token request (tests have no network), and the+ // replaced ref-1 is never presented.+ try std.testing.expectEqualStrings("tok-2", chatgpt.refreshLocked(io, std.testing.allocator, a, home, before).?);+ // Signed out meanwhile: nothing to send.+ var signed_out = rotated;+ signed_out.access = "";+ signed_out.refresh = "";+ try saveIn(tmp.dir, a, signed_out);+ try std.testing.expect(chatgpt.refreshLocked(io, std.testing.allocator, a, home, before) == null);+}++test "ChatGPT sign-in: an unusable refresh token is cleared and the access token kept" {+ const io = std.testing.io;+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ var buf: [std.fs.max_path_bytes]u8 = undefined;+ const 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))).?;+ try std.testing.expectEqualStrings("", saved.refresh);+ try std.testing.expectEqualStrings("tok-1", saved.access); // usable until it expires+ try std.testing.expectEqualStrings("oaiapp_x", saved.client_id); // the next sign-in skips registration+ try std.testing.expect(@import("oauth_helpers.zig").permanentRefreshFailure("refresh_token_reused"));+ try std.testing.expect(@import("oauth_helpers.zig").permanentRefreshFailure("invalid_refresh_token"));+ try std.testing.expect(!@import("oauth_helpers.zig").permanentRefreshFailure("server_error"));+}diff --git a/src/oauth_helpers.zig b/src/oauth_helpers.zigindex 53b487f9..984a3125 100644--- a/src/oauth_helpers.zig+++ b/src/oauth_helpers.zig@@ -220,7 +220,8 @@ pub fn readCodexAuth(io: Io, arena: Allocator, codex_home: []const u8) ?StoredCo/// retryable. codex-rs caches exactly these for the current auth snapshot so a/// dead credential stops costing a round trip per request.pub fn permanentRefreshFailure(code: []const u8) bool {- const permanent = [_][]const u8{ "invalid_grant", "invalid_client", "refresh_token_expired", "refresh_token_reused", "refresh_token_invalidated" };+ // OpenAI's SIWC list adds invalid_refresh_token and token_expired (ADR 0221).+ const permanent = [_][]const u8{ "invalid_grant", "invalid_client", "invalid_refresh_token", "token_expired", "refresh_token_expired", "refresh_token_reused", "refresh_token_invalidated" };for (permanent) |p| if (std.mem.eql(u8, code, p)) return true;return false;}