graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
//! 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 "graff refresh is the short catalog pull, not a new command" {
const startup = @import("startup.zig");
try std.testing.expectEqual(startup.CatalogCommand.refresh, startup.catalogCommand("refresh"));
try std.testing.expectEqual(startup.CatalogCommand.list, startup.catalogCommand("models"));
try std.testing.expectEqual(startup.CatalogCommand.route, startup.catalogCommand("route"));
try std.testing.expectEqual(startup.CatalogCommand.none, startup.catalogCommand("update"));
}
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]);
const pricing = @import("pricing.zig");
const saved_models = pricing.active_model_table;
defer pricing.active_model_table = saved_models;
defer @import("http2_pool.zig").shutdown(io);
// This test checks credential scope, so seed a fresh local catalog. It
// must not turn a synthetic key into a real provider GET during tier 1.
const catalog = @import("router_catalog.zig");
const spec = @import("provider.zig").specFor("codegraff") orelse return error.TestUnexpectedResult;
const cached_rows = [_]pricing.ModelInfo{.{ .provider = spec.id, .name = spec.default_model, .context = 128_000 }};
const cached = catalog.cacheDocument(io, arena, spec, &cached_rows) orelse return error.TestUnexpectedResult;
try tmp.dir.writeFile(io, .{ .sub_path = ".codegraff-codegraff-models.json", .data = cached });
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", null);
try std.testing.expect(resolved.keys.get("kimi") == null);
try std.testing.expectEqualStrings("codegraff", resolved.default_provider.id);
// …and the mirror: an explicit --subagent-provider kimi is exactly as
// explicit as --model, so the SAME launch shape (unrelated root provider)
// must now load the kimi login — root sol + kimi workers used to fatal
// "no key/login for subagent model 'k3' via 'kimi'" despite a valid
// on-disk kimi credential, because the selective scope read only the
// root model flag.
const hinted = try resolveKeys(io, gpa, arena, env, "codegraff", "kimi");
try std.testing.expectEqualStrings("kimi-test-token", hinted.keys.get("kimi").?);
try std.testing.expectEqualStrings("codegraff", hinted.default_provider.id);
// Scope units for the same rule (pub-flipped in startup.zig for this).
const startup = @import("startup.zig");
try std.testing.expect(startup.storedKeyMayAffectSelection("kimi", "gpt-5.6-sol", "kimi"));
try std.testing.expect(!startup.storedKeyMayAffectSelection("kimi", "gpt-5.6-sol", null));
// The scope mask is indexed by provider_specs position — look kimi's up
// rather than hardcoding it, so provider insertions (PR #395 shifted it
// from 6 to 9) can't silently break this assertion again.
const specs = @import("provider.zig").provider_specs;
const kimi_idx = for (specs, 0..) |s, i| {
if (std.mem.eql(u8, s.id, "kimi")) break i;
} else unreachable;
try std.testing.expect(startup.startupStoredKeyScope("deepseek", true, "kimi").includes(kimi_idx, "kimi"));
}
test "kimi oauth (#477): a home-less side agent resolves the startup-pinned home — real writer, real loader" {
// The reported bug, end to end without the network: the pre-compaction
// note agent (home="") could neither proactively refresh nor recover a
// 401, because its credential path resolved to "/.kimi/...". Pin the
// process home as startup does, then prove the home-less call shape
// finds what the real writer wrote. The ONLY g_home mutation in the
// suite — the parallel runner makes two mutators a coin flip.
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();
const credential_store = @import("credential_store.zig");
const oauth = @import("oauth.zig");
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]);
const saved = credential_store.g_home;
defer credential_store.g_home = saved;
credential_store.initHome(home);
// Path shapes: the pin fills in for a home-less caller, never overrides one.
try std.testing.expectEqualStrings(try std.fmt.allocPrint(arena, "{s}/.kimi/credentials/graff-oauth.json", .{home}), credential_store.oauthPath(arena, "", ".kimi"));
try std.testing.expectEqualStrings("/caller/.xai/credentials/graff-oauth.json", credential_store.oauthPath(arena, "/caller", ".xai"));
// The fix itself: write through the real writer, load with home="" — the
// note agent's exact call shape. Far-future expiry: no refresh, no network.
try credential_store.writeOAuth(io, arena, home, ".kimi", "kimi-access-477", "kimi-refresh", 4102444800);
try std.testing.expectEqualStrings("kimi-access-477", oauth.loadKimiOAuth(io, gpa, arena, "", false, null).?);
}