Skip to content

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

Python

Skip an expired Console sign-in that ant cannot renew

justrach32f1de763113
diff --git a/docs/adr/0310-auth-failures-name-the-rejected-credential.md b/docs/adr/0310-auth-failures-name-the-rejected-credential.md
index ad5ee79b..56f871d2 100644
--- a/docs/adr/0310-auth-failures-name-the-rejected-credential.md
+++ b/docs/adr/0310-auth-failures-name-the-rejected-credential.md
@@ -52,3 +52,19 @@ as signed out. `reauth_required` and its wire positions are unchanged.
A client can tell "replace the key" from "sign in" without parsing provider
words, and an old client no longer offers a sign-in for a rejected key. The
generic message text is gone; anything matching it must match the new phrases.
+
+## Addendum: an expired Console sign-in
+
+An Anthropic Console token past `expires_at` with no refresh token stored is
+not renewable: `ant auth print-credentials --access-token` warns, exits 0 and
+prints the same expired token, which graff used to accept as a refresh and
+then send, getting a 401 on every request (also when it was parked behind a
+Claude plan as the fallback). `anthropic_profile` now treats a renewal that
+returns the old token as failed: at startup such a token is neither seated nor
+parked, and mid-session `fresh` offers no token instead of the dead one. The
+texts say what happened: "Anthropic Console sign-in has expired: run `graff
+login console` again. Your sign-in is not affected." (or "...the fallback
+Console sign-in has expired..." after a plan handoff), and a spent plan with no
+fallback for that reason says "Anthropic plan is out of quota, and the Console
+sign-in that would take over has expired". The `api_key_rejected` data is
+unchanged.
diff --git a/src/agent_request_quota.zig b/src/agent_request_quota.zig
index 03b6aa34..5ed04a18 100644
--- a/src/agent_request_quota.zig
+++ b/src/agent_request_quota.zig
@@ -124,7 +124,12 @@ pub fn handle(self: *@import("agent.zig").Agent, etype: []const u8, code: ?[]con
const auth = @import("auth_recovery.zig");
if (fault == .authentication) self.last_api_reauth = auth.forAuthFault(self.provider, code orelse "authentication_error");
self.last_api_error = switch (fault) {
- .quota => "provider quota/billing cap: credit or allowance exhausted; retry requires account action",
+ // The Console sign-in that would have taken over was skipped at startup
+ // because it expired beyond renewal (anthropic_profile.load).
+ .quota => if (std.mem.eql(u8, self.provider.id, "anthropic") and self.provider.source == .login and @import("anthropic_profile.zig").expired())
+ "Anthropic plan is out of quota, and the Console sign-in that would take over has expired: run `graff login console` again, or wait for the plan to reset"
+ else
+ "provider quota/billing cap: credit or allowance exhausted; retry requires account action",
.authentication => auth.failureText(self.arena, self.provider, @import("credential_failover.zig").promoted(self.provider.id)),
.access_denied => "provider access denied: check account permissions or route eligibility",
};
diff --git a/src/anthropic_profile.zig b/src/anthropic_profile.zig
index 2cc87d4d..b8de2552 100644
--- a/src/anthropic_profile.zig
+++ b/src/anthropic_profile.zig
@@ -52,6 +52,19 @@ var remembered: ?Located = null;
/// Anthropic's CLI; tests point it at a stand-in.
pub var ant_bin: []const u8 = "ant";
var last_failed_ms: i64 = 0;
+/// The profile's token is past its expiry and `ant` could not renew it: with
+/// no refresh token stored, `ant auth print-credentials` still exits 0 and
+/// prints the same expired token. Such a token is never seated or sent.
+var expired_unrenewable: bool = false;
+
+/// Did the Console sign-in expire beyond renewal (`graff login console` fixes it)?
+pub fn expired() bool {
+ return expired_unrenewable;
+}
+
+fn pastExpiry(p: Profile, now_ms: i64) bool {
+ return if (p.expires_at) |exp| exp * 1000 <= now_ms else false;
+}
pub fn locate(arena: Allocator, environ_map: anytype, home: []const u8) ?Located {
const dir: []const u8 = blk: {
@@ -123,9 +136,25 @@ pub fn load(io: Io, arena: Allocator, environ_map: anytype, home: []const u8, na
if (named_only and !loc.named) return null;
const p = read(io, arena, loc) orelse return null;
remember(loc, p);
+ expired_unrenewable = false;
+ // An expired token is only worth seating (or parking behind a plan) if
+ // `ant` can renew it; otherwise every request it carries is a 401.
+ if (pastExpiry(p, @import("util.zig").unixMs(io))) {
+ const t = renewed(io, arena, loc, p.token) orelse {
+ expired_unrenewable = true;
+ return null;
+ };
+ return std.heap.page_allocator.dupe(u8, t) catch null;
+ }
return std.heap.page_allocator.dupe(u8, p.token) catch null;
}
+/// A token `ant` renewed, or null when it failed or handed back `old`.
+fn renewed(io: Io, arena: Allocator, loc: Located, old: []const u8) ?[]const u8 {
+ const t = refreshViaAnt(io, arena, profileName(io, arena, loc)) orelse return null;
+ return if (std.mem.eql(u8, t, old)) null else t;
+}
+
fn remember(loc: Located, p: Profile) void {
const a = std.heap.page_allocator;
_ = p;
@@ -140,17 +169,26 @@ pub fn fresh(io: Io, gpa: Allocator, arena: Allocator, force: bool) ?[]const u8
const loc = remembered orelse return null;
const p = read(io, arena, loc) orelse return null;
const now_ms = @import("util.zig").unixMs(io);
+ _ = gpa;
const near = if (p.expires_at) |exp| exp * 1000 - now_ms <= refresh_margin_s * 1000 else false;
if (!force and !near) return p.token;
- if (now_ms - last_failed_ms < retry_after_ms) return p.token;
- return refreshViaAnt(io, gpa, arena, profileName(io, arena, loc)) orelse {
+ // Past expiry, the old token is a sure 401: offer nothing instead.
+ const fallback: ?[]const u8 = if (pastExpiry(p, now_ms)) null else p.token;
+ if (now_ms - last_failed_ms < retry_after_ms) return fallback;
+ const t = refreshViaAnt(io, arena, profileName(io, arena, loc)) orelse {
last_failed_ms = now_ms;
- return p.token;
+ return fallback;
};
+ if (fallback == null and std.mem.eql(u8, t, p.token)) {
+ last_failed_ms = now_ms;
+ expired_unrenewable = true;
+ return null;
+ }
+ expired_unrenewable = false;
+ return t;
}
-fn refreshViaAnt(io: Io, gpa: Allocator, arena: Allocator, profile: []const u8) ?[]const u8 {
- _ = gpa;
+fn refreshViaAnt(io: Io, arena: Allocator, profile: []const u8) ?[]const u8 {
const pr = @import("process_runner.zig");
const run = pr.runCappedWithOptions(arena, io, &.{ ant_bin, "auth", "print-credentials", "--access-token", "--profile", profile }, 8192, 8192, 20_000, .{}) catch return null;
if (!pr.ranOk(run)) return null;
@@ -323,7 +361,9 @@ test "an expiring Console token is refreshed through ant, and sent as a bearer t
last_failed_ms = 0;
const env = Env{ .map = &.{.{ "ANTHROPIC_CONFIG_DIR", dir }} };
- try testing.expectEqualStrings("sk-ant-oat01-old", load(io, a, env, "", false).?);
+ // Already expired at startup: renewed before it is seated.
+ try testing.expectEqualStrings("sk-ant-oat01-new", load(io, a, env, "", false).?);
+ try testing.expect(!expired());
try testing.expectEqualStrings("sk-ant-oat01-new", fresh(io, testing.allocator, a, false).?);
// The provider built from it authenticates with Bearer, not x-api-key.
@@ -367,6 +407,45 @@ test "an expiring Console token is refreshed through ant, and sent as a bearer t
try testing.expect(!policy.retryAfterAuthRefresh(&agent, "Unauthorized", &attempted));
}
+test "an expired Console token ant cannot renew is never seated, parked or sent" {
+ if (builtin.os.tag == .windows) return error.SkipZigTest; // /bin/sh stand-in
+ const io = testing.io;
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var buf: [std.fs.max_path_bytes]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(io, &buf)];
+ try writeFile(io, tmp.dir, "configs/default.json",
+ \\{"authentication":{"type":"user_oauth","client_id":"c"}}
+ );
+ try writeFile(io, tmp.dir, "credentials/default.json",
+ \\{"access_token":"sk-ant-oat01-dead","expires_at":1}
+ );
+ // With no refresh token stored, `ant` warns, exits 0 and prints the same token.
+ try writeFile(io, tmp.dir, "ant", "#!/bin/sh\necho 'warning: no refresh_token is stored' >&2\necho sk-ant-oat01-dead\n");
+ const script = try std.fs.path.join(a, &.{ dir, "ant" });
+ try Io.Dir.cwd().setFilePermissions(io, script, .fromMode(0o755), .{});
+ const saved = ant_bin;
+ defer ant_bin = saved;
+ ant_bin = script;
+ last_failed_ms = 0;
+ defer expired_unrenewable = false;
+
+ const env = Env{ .map = &.{.{ "ANTHROPIC_CONFIG_DIR", dir }} };
+ try testing.expect(load(io, a, env, "", false) == null);
+ try testing.expect(expired());
+ try testing.expect(fresh(io, testing.allocator, a, true) == null);
+
+ var p: @import("provider.zig").Provider = .{ .id = "anthropic", .kind = .anthropic, .auth = .bearer, .url = "", .api_key = "k", .model = "m", .context = 1000, .source = .profile };
+ const text = @import("auth_recovery.zig").failureText(a, p, false);
+ try testing.expectEqualStrings("Anthropic Console sign-in has expired: run `graff login console` again. Your sign-in is not affected.", text);
+ try testing.expect(std.mem.indexOf(u8, @import("auth_recovery.zig").failureText(a, p, true), "fallback Console sign-in has expired") != null);
+ p.source = .environment; // an ordinary key keeps the key wording
+ try testing.expect(std.mem.indexOf(u8, @import("auth_recovery.zig").failureText(a, p, false), "API key was rejected") != null);
+}
+
test "sign-in pastes a code whenever no local browser can finish it" {
var env: std.process.Environ.Map = .init(std.testing.allocator);
defer env.deinit();
diff --git a/src/auth_recovery.zig b/src/auth_recovery.zig
index 48b4bc45..d5edf9a8 100644
--- a/src/auth_recovery.zig
+++ b/src/auth_recovery.zig
@@ -97,6 +97,11 @@ pub fn failureText(a: std.mem.Allocator, p: provider.Provider, promoted: bool) [
return std.fmt.allocPrint(a, "{s} sign-in was rejected: sign in again ({s} {s})", .{ name, login.command, args }) catch
"provider sign-in was rejected: sign in again";
}
+ // A Console token past expiry that `ant` cannot renew: name that, not "key".
+ if (std.mem.eql(u8, p.id, "anthropic") and p.source == .profile and @import("anthropic_profile.zig").expired()) return if (promoted)
+ "Anthropic plan is out of usage, and the fallback Console sign-in has expired: run `graff login console` again, or wait for the plan to reset. Your sign-in is not affected."
+ else
+ "Anthropic Console sign-in has expired: run `graff login console` again. Your sign-in is not affected.";
if (promoted) return std.fmt.allocPrint(
a,
"{s} plan is out of usage, and the fallback " ++ api_key_rejected_phrase ++ " ({s}): replace or remove the key, or wait for the plan to reset.",
diff --git a/src/providers.zig b/src/providers.zig
index cedf97ba..1ad90fe9 100644
--- a/src/providers.zig
+++ b/src/providers.zig
@@ -162,6 +162,7 @@ pub fn failoverEligible(provider_id: []const u8, detail: []const u8) bool {
"authentication failed",
"sign-in was rejected", // auth_recovery.failureText (ADR 0310)
"api key was rejected",
+ "console sign-in has expired",
"invalid api key",
"invalid_api_key",
"invalid x-api-key",
@@ -399,6 +400,7 @@ test "failoverEligible: accepts unavailable credentials/models, rejects transien
try std.testing.expect(failoverEligible("codex", "model_not_found: rollout ended"));
try std.testing.expect(failoverEligible("kimi", "Kimi sign-in was rejected: sign in again (graff login kimi)"));
try std.testing.expect(failoverEligible("anthropic", "Anthropic plan is out of usage, and the fallback API key was rejected (Console profile): wait"));
+ try std.testing.expect(failoverEligible("anthropic", "Anthropic Console sign-in has expired: run `graff login console` again. Your sign-in is not affected."));
try std.testing.expect(failoverEligible("codegraff", "insufficient_quota"));
try std.testing.expect(!failoverEligible("codex", "network error: HttpConnectionClosing"));
try std.testing.expect(!failoverEligible("codex", "rate limited (429)"));