graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
perf(startup): scope Kimi/xAI OAuth refresh to selective provider startup
Rach Pradhan5ede8c71c696
diff --git a/src/main_test.zig b/src/main_test.zigindex 6bff93a3..f3933908 100644--- a/src/main_test.zig+++ b/src/main_test.zig@@ -152,3 +152,11 @@ test "/bash slash command runs the bash tool and frees its gpa-allocated result"const written = aw.writer.buffered();try std.testing.expect(std.mem.indexOf(u8, written, "leak-guard-XYZ") != null);}++// main.zig itself is at the 600-line ceiling (#274 fix), so+// startup_tests.zig's regression coverage is pulled in from here instead —+// same test-root-reference requirement as main.zig's own hook: an+// unreferenced module's test {} blocks silently compile to nothing.+test {+ _ = @import("startup_tests.zig");+}diff --git a/src/startup.zig b/src/startup.zigindex 3b6c3da2..ea14cb91 100644--- a/src/startup.zig+++ b/src/startup.zig@@ -217,15 +217,19 @@ pub fn resolveKeys(io: Io, gpa: Allocator, arena: Allocator, environ_map: anytyp// Kimi "login": OAuth device-flow token from `graff login kimi`// (~/.kimi/credentials/graff-oauth.json), refreshed in place when near// expiry. Same on-disk-credential pattern as codex/codegraff; env wins.+ // #274: refreshing is a synchronous network round-trip, so an explicit+ // provider/exact-model selection that cannot route to kimi/xai must skip+ // it entirely rather than pay for it on every startup — same selective-+ // scope discipline as the stored-key load a few lines below.if (keys_cli.homeEnv(environ_map)) |home| {for (provider_mod.provider_specs, &keys.values, &keys.sources) |spec, *value, *source| {- if (std.mem.eql(u8, spec.id, "kimi") and value.* == null) {+ if (std.mem.eql(u8, spec.id, "kimi") and value.* == null and storedKeyMayAffectSelection(spec.id, model_flag)) {if (oauth.loadKimiOAuth(io, gpa, arena, home, false)) |key| {value.* = key;source.* = .login;}}- if (std.mem.eql(u8, spec.id, "xai") and value.* == null) {+ if (std.mem.eql(u8, spec.id, "xai") and value.* == null and storedKeyMayAffectSelection(spec.id, model_flag)) {if (oauth.loadXaiOAuth(io, gpa, arena, home, false)) |key| {value.* = key;source.* = .login;diff --git a/src/startup_tests.zig b/src/startup_tests.zignew file mode 100644index 00000000..451229d1--- /dev/null+++ b/src/startup_tests.zig@@ -0,0 +1,56 @@+//! Focused regression tests for startup.zig's credential-loading scope+//! decisions. Split out (rather than grown in startup.zig itself) to stay+//! under the 600-line file ceiling; main.zig is itself pinned at 600 lines,+//! so this is pulled into the test root via main_test.zig's own hook+//! instead of main.zig's directly (see main_test.zig's trailing test {}).+const std = @import("std");+const Io = std.Io;+const builtin = @import("builtin");++const resolveKeys = @import("startup.zig").resolveKeys;++test "resolveKeys does not load Kimi/xAI OAuth credentials for an explicit unrelated provider" {+ // #274: loadKimiOAuth/loadXaiOAuth refresh in place (a synchronous+ // network round-trip) when the stored credential is near expiry. Before+ // the startup.zig fix, resolveKeys read them unconditionally, so an+ // unrelated explicit `--model codegraff` still paid for that read/+ // refresh whenever a kimi login file happened to exist. This proves the+ // on-disk file is not even opened once selection cannot route to kimi: a+ // valid, far-from-expiry credential on disk must NOT surface in the+ // resolved keys.+ if (builtin.os.tag == .windows) return error.SkipZigTest;+ const io = std.testing.io;+ const gpa = std.testing.allocator;+ var arena_state = std.heap.ArenaAllocator.init(gpa);+ defer arena_state.deinit();+ const arena = arena_state.allocator();++ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ var real_buf: [std.fs.max_path_bytes]u8 = undefined;+ const real_len = try tmp.dir.realPath(io, &real_buf);+ const home = try arena.dupe(u8, real_buf[0..real_len]);++ try tmp.dir.createDirPath(io, ".kimi/credentials");+ const now_s = @divTrunc(Io.Timestamp.now(io, .real).nanoseconds, 1_000_000_000);+ const kimi_json = try std.fmt.allocPrint(+ arena,+ "{{\"access_token\":\"kimi-test-token\",\"refresh_token\":\"r\",\"expires_at\":{d}}}",+ .{now_s + 3600},+ );+ try tmp.dir.writeFile(io, .{ .sub_path = ".kimi/credentials/graff-oauth.json", .data = kimi_json });++ const TestEnv = struct {+ home: []const u8,+ pub fn get(self: @This(), key: []const u8) ?[]const u8 {+ if (std.mem.eql(u8, key, "HOME")) return self.home;+ if (std.mem.eql(u8, key, "CODEGRAFF_API_KEY")) return "test-codegraff-key";+ return null;+ }+ };+ const env: TestEnv = .{ .home = home };++ const resolved = try resolveKeys(io, gpa, arena, env, "codegraff");+ try std.testing.expect(resolved.keys.get("kimi") == null);+ try std.testing.expectEqualStrings("codegraff", resolved.default_provider.id);+}