graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(route): graff route <model> — instant dry-run of provider seating + billing (#377 follow-up)
justrach1d7678bbd73a
diff --git a/src/cli.zig b/src/cli.zigindex ac9a2099..8f240b99 100644--- a/src/cli.zig+++ b/src/cli.zig@@ -66,6 +66,7 @@ pub const usage_text =\\ graff key set <provider> <key> store a key (macOS Keychain, else 0600 file)\\ graff key list show which providers have keys\\ graff models [refresh] list the live catalog; refresh Codex + models.dev metadata+ \\ graff route <model>… dry-run which provider/billing a model lands on (no API call)\\ graff mcp add <name> -- <cmd> add a stdio MCP server to .mcp.json\\ graff mcp add <name> --url <url> add a Streamable HTTP MCP server\\ graff mcp login <name> OAuth login for a remote MCP serverdiff --git a/src/pricing.zig b/src/pricing.zigindex 3ce0547b..3e38ba9b 100644--- a/src/pricing.zig+++ b/src/pricing.zig@@ -432,11 +432,27 @@ pub fn modelAliasEquals(name: []const u8, query: []const u8) bool {return std.mem.eql(u8, normalizeModelAlias(&nb, name), normalizeModelAlias(&qb, query));}+/// #377: `<provider><sep><name>` is the same model as the provider's own+/// `<name>` row under a family-prefixed spelling (gateway catalogs do this).+/// Alias-normalized so `kimi-k3` == kimi + `k3` regardless of separators.+pub fn familyAliasEquals(provider_id: []const u8, name: []const u8, query: []const u8) bool {+ var qb: [128]u8 = undefined;+ var pb: [128]u8 = undefined;+ var nb: [128]u8 = undefined;+ const q = normalizeModelAlias(&qb, query);+ const p = normalizeModelAlias(&pb, provider_id);+ const n = normalizeModelAlias(&nb, name);+ return q.len == p.len + n.len and std.mem.startsWith(u8, q, p) and std.mem.endsWith(u8, q, n);+}+/// `keys` is main.zig's Keys (anytype to keep provider wiring out of this/// module) — anything with `get(provider_id) ?[]const u8`.pub fn resolveModelName(keys: anytype, query: []const u8) ?[]const u8 {for (models()) |m| if (std.mem.eql(u8, m.name, query)) return m.name;for (models()) |m| if (modelAliasEquals(m.name, query)) return m.name;+ // #377: family-prefixed spelling of a provider's own row (`kimi-k3` → kimi's+ // `k3`) — resolves even when no gateway catalog supplies the prefixed name.+ for (models()) |m| if (familyAliasEquals(m.provider, m.name, query)) return m.name;var qbuf: [128]u8 = undefined;const qnorm = normalizeModelAlias(&qbuf, query);var fallback: ?[]const u8 = null;@@ -561,39 +577,5 @@ test "usdFor: per-million math and negative clamping" {try std.testing.expectApproxEqAbs(@as(f64, 0.0), usdFor(p, -42, -1, 0), 1e-9); // clamped}-test "CostTally token and call counters saturate" {- var tally: CostTally = .{- .in_tokens = std.math.maxInt(u64) - 1,- .cache_tokens = std.math.maxInt(u64),- .out_tokens = std.math.maxInt(u64) - 2,- .api_calls = std.math.maxInt(u64),- .sub_calls = std.math.maxInt(u64),- };- tally.add(std.testing.io, "codex", "gpt-5.5", 10, 10, 10);- try std.testing.expectEqual(std.math.maxInt(u64), tally.in_tokens);- try std.testing.expectEqual(std.math.maxInt(u64), tally.cache_tokens);- try std.testing.expectEqual(std.math.maxInt(u64), tally.out_tokens);- try std.testing.expectEqual(std.math.maxInt(u64), tally.api_calls);- try std.testing.expectEqual(std.math.maxInt(u64), tally.sub_calls);-}--test "modelInTable: known models present, unknown absent" {- try std.testing.expect(modelInTable("gpt-5.5"));- try std.testing.expect(modelInTable("claude-opus-4-8"));- try std.testing.expect(!modelInTable("not-a-real-model"));-}--test "priceFor: known model priced, unknown is null" {- try std.testing.expect(priceFor("gpt-5.5") != null);- try std.testing.expect(priceFor("claude-opus-4-8") != null);- try std.testing.expect(priceFor("no-such-model") == null);-}--test "resolveModelName exact aliases and miss" {- const provider_mod = @import("provider.zig");- const Keys = provider_mod.Keys;- const keys = Keys{ .values = @splat(null) };- try std.testing.expect(resolveModelName(keys, "gpt-5.5") != null); // exact name- try std.testing.expectEqualStrings("glm-5.2", resolveModelName(keys, "glm5.2").?); // natural alias- try std.testing.expect(resolveModelName(keys, "totally-unknown-zzz") == null);-}+// CostTally/modelInTable/priceFor/resolveModelName tests live in+// pricing_tests.zig (this file sits against the 600-line cap).diff --git a/src/pricing_tests.zig b/src/pricing_tests.zignew file mode 100644index 00000000..fb1bdc41--- /dev/null+++ b/src/pricing_tests.zig@@ -0,0 +1,51 @@+//! Tests carved out of pricing.zig, which sits against the 600-line cap.++const std = @import("std");+const pricing = @import("pricing.zig");+const provider_mod = @import("provider.zig");++test "CostTally token and call counters saturate" {+ var tally: pricing.CostTally = .{+ .in_tokens = std.math.maxInt(u64) - 1,+ .cache_tokens = std.math.maxInt(u64),+ .out_tokens = std.math.maxInt(u64) - 2,+ .api_calls = std.math.maxInt(u64),+ .sub_calls = std.math.maxInt(u64),+ };+ tally.add(std.testing.io, "codex", "gpt-5.5", 10, 10, 10);+ try std.testing.expectEqual(std.math.maxInt(u64), tally.in_tokens);+ try std.testing.expectEqual(std.math.maxInt(u64), tally.cache_tokens);+ try std.testing.expectEqual(std.math.maxInt(u64), tally.out_tokens);+ try std.testing.expectEqual(std.math.maxInt(u64), tally.api_calls);+ try std.testing.expectEqual(std.math.maxInt(u64), tally.sub_calls);+}++test "modelInTable: known models present, unknown absent" {+ try std.testing.expect(pricing.modelInTable("gpt-5.5"));+ try std.testing.expect(pricing.modelInTable("claude-opus-4-8"));+ try std.testing.expect(!pricing.modelInTable("not-a-real-model"));+}++test "priceFor: known model priced, unknown is null" {+ try std.testing.expect(pricing.priceFor("gpt-5.5") != null);+ try std.testing.expect(pricing.priceFor("claude-opus-4-8") != null);+ try std.testing.expect(pricing.priceFor("no-such-model") == null);+}++test "resolveModelName exact aliases and miss" {+ const keys = provider_mod.Keys{ .values = @splat(null) };+ try std.testing.expect(pricing.resolveModelName(keys, "gpt-5.5") != null); // exact name+ try std.testing.expectEqualStrings("glm-5.2", pricing.resolveModelName(keys, "glm5.2").?); // natural alias+ try std.testing.expect(pricing.resolveModelName(keys, "totally-unknown-zzz") == null);+}++test "resolveModelName (#377): family-prefixed spelling resolves to the provider's native row" {+ const keys = provider_mod.Keys{ .values = @splat(null) };+ // Works from the COMPILED table alone — no gateway catalog cache needed,+ // so a fresh machine with only a kimi login can type `--model kimi-k3`.+ try std.testing.expectEqualStrings("k3", pricing.resolveModelName(keys, "kimi-k3").?);+ try std.testing.expect(pricing.familyAliasEquals("kimi", "k3", "kimi-k3"));+ try std.testing.expect(pricing.familyAliasEquals("kimi", "k3", "kimi_k3"));+ try std.testing.expect(!pricing.familyAliasEquals("kimi", "k3", "kimi-k30"));+ try std.testing.expect(!pricing.familyAliasEquals("codex", "k3", "kimi-k3"));+}diff --git a/src/provider.zig b/src/provider.zigindex b470e425..a24ad895 100644--- a/src/provider.zig+++ b/src/provider.zig@@ -313,7 +313,7 @@ pub const Keys = struct {if ((phase == 2) != std.mem.eql(u8, spec.id, "codegraff")) continue;for (pricing.models()) |m| {if (!std.mem.eql(u8, m.provider, spec.id)) continue;- if (if (phase == 1) familyAliasEquals(spec.id, m.name, model) else std.mem.eql(u8, m.name, model))+ if (if (phase == 1) pricing.familyAliasEquals(spec.id, m.name, model) else std.mem.eql(u8, m.name, model))return keys.build(spec, key, m.name);}}@@ -336,19 +336,6 @@ pub const Keys = struct {return keys.build(spec, key, model);}- /// #377: `<provider><sep><name>` is the same model as the provider's own- /// `<name>` row under a family-prefixed spelling (gateway catalogs do this).- /// Alias-normalized so `kimi-k3` == kimi + `k3` regardless of separators.- fn familyAliasEquals(provider_id: []const u8, name: []const u8, query: []const u8) bool {- var qb: [128]u8 = undefined;- var pb: [128]u8 = undefined;- var nb: [128]u8 = undefined;- const q = pricing.normalizeModelAlias(&qb, query);- const p = pricing.normalizeModelAlias(&pb, provider_id);- const n = pricing.normalizeModelAlias(&nb, name);- return q.len == p.len + n.len and std.mem.startsWith(u8, q, p) and std.mem.endsWith(u8, q, n);- }-/// The startup default: the first provider (in spec order) with a key,/// on its default model.pub fn defaultProvider(keys: Keys) error{MissingKey}!Provider {diff --git a/src/route_check.zig b/src/route_check.zignew file mode 100644index 00000000..fa701481--- /dev/null+++ b/src/route_check.zig@@ -0,0 +1,103 @@+//! `graff route <model>…` — dry-run provider seating (#377 follow-up).+//!+//! Answers "which provider, which native spelling, and which billing class+//! would this model land on?" WITHOUT making an API call: the same key+//! resolution as a real session (startup.resolveKeys), the same seat chooser+//! (Keys.providerFor — including the #377 family-alias phase that seats+//! `kimi-k3` on a logged-in kimi instead of the gateway), and the usage+//! footer's own billing classifier (pricing.billingFor). Dispatched from the+//! `graff models`/`route` block in startup.runSubcommand, so the live catalog+//! caches are already loaded and gateway spellings resolve exactly as they+//! would in-session.++const std = @import("std");+const Io = std.Io;+const Allocator = std.mem.Allocator;+const pricing = @import("pricing.zig");+const provider_mod = @import("provider.zig");++pub const Seat = struct {+ pid: []const u8,+ model: []const u8,+ billing: pricing.Billing,+ source: provider_mod.Keys.CredentialSource,+};++pub const Answer = union(enum) {+ unknown,+ no_credential: []const u8, // the resolved catalog name nobody keyed serves+ seat: Seat,+};++/// Pure resolution shared by the command and its tests: catalog-name+/// resolution, then the real provider seat.+pub fn seatFor(keys: provider_mod.Keys, query: []const u8) Answer {+ const resolved = pricing.resolveModelName(keys, query) orelse return .unknown;+ const p = keys.providerFor(resolved) catch return .{ .no_credential = resolved };+ return .{ .seat = .{+ .pid = p.id,+ .model = p.model,+ .billing = pricing.billingFor(p.id, p.model),+ .source = p.source,+ } };+}++pub fn billingLabel(b: pricing.Billing) []const u8 {+ return switch (b) {+ .sub => "subscription, flat-rate",+ .priced => "metered",+ .unpriced => "unpriced (gateway/license)",+ };+}++pub fn command(io: Io, gpa: Allocator, arena: Allocator, environ_map: anytype, sub_args: []const []const u8) !void {+ var obuf: [8 * 1024]u8 = undefined;+ var ow = Io.File.stdout().writer(io, &obuf);+ const out = &ow.interface;+ const rk = try @import("startup.zig").resolveKeys(io, gpa, arena, environ_map, null, null);+ if (sub_args.len == 0) {+ const d = rk.default_provider;+ try out.print("session default: {s}/{s} · auth: {s} · billing: {s}\n", .{ d.id, d.model, d.source.label(), billingLabel(pricing.billingFor(d.id, d.model)) });+ try out.writeAll("\nusage: graff route <model> [<model>…] — dry-run which provider a model would land on (no API call)\n");+ try out.flush();+ return;+ }+ for (sub_args) |q| {+ switch (seatFor(rk.keys, q)) {+ .unknown => try out.print("{s} → ✗ unknown model — run `graff models refresh` or see `graff models`\n", .{q}),+ .no_credential => |m| try out.print("{s} → ✗ '{s}' is catalogued, but no logged-in/keyed provider serves it — `graff login <provider>` or `graff key set`\n", .{ q, m }),+ .seat => |s| {+ try out.print("{s} → {s}/{s} · auth: {s} · billing: {s}", .{ q, s.pid, s.model, s.source.label(), billingLabel(s.billing) });+ if (s.billing == .priced) {+ if (pricing.priceFor(s.model)) |pr| try out.print(" (${d}/{d} per 1M)", .{ pr.in, pr.out });+ }+ try out.writeAll("\n");+ },+ }+ }+ try out.flush();+}++test "seatFor: family-alias spelling seats the sub, exact names hold, unknown misses" {+ const all = provider_mod.Keys{ .values = @splat("k") };+ const kk = seatFor(all, "kimi-k3");+ try std.testing.expectEqualStrings("kimi", kk.seat.pid);+ try std.testing.expectEqualStrings("k3", kk.seat.model);+ try std.testing.expectEqual(pricing.Billing.sub, kk.seat.billing);+ const sol = seatFor(all, "gpt-5.6-sol");+ try std.testing.expectEqualStrings("codex", sol.seat.pid);+ try std.testing.expectEqual(pricing.Billing.sub, sol.seat.billing);+ try std.testing.expect(seatFor(all, "totally-unknown-zzz") == .unknown);+}++test "seatFor: catalogued model with no serving credential reports no_credential, not a gateway seat" {+ var values: [provider_mod.provider_specs.len]?[]const u8 = @splat(null);+ for (provider_mod.provider_specs, 0..) |spec, i| {+ if (std.mem.eql(u8, spec.id, "codegraff")) values[i] = "k";+ }+ const only_gateway = provider_mod.Keys{ .values = values };+ // gpt-5.6-sol is catalogued only under codex (#294): with just a gateway+ // key it must surface as a credential problem, not silently seat there.+ const a = seatFor(only_gateway, "gpt-5.6-sol");+ try std.testing.expectEqualStrings("gpt-5.6-sol", a.no_credential);+}diff --git a/src/startup.zig b/src/startup.zigindex 0da58655..be39eb3a 100644--- a/src/startup.zig+++ b/src/startup.zig@@ -542,9 +542,9 @@ pub fn runSubcommand(io: Io, gpa: Allocator, arena: Allocator, init: std.processreturn true;}- // `graff models [refresh]`: print the effective model catalog, or pull- // fresh window/price metadata from models.dev into the local cache.- if (flags.positionals.items.len > 0 and std.mem.eql(u8, flags.positionals.items[0], "models")) {+ // `graff models [refresh]` / `graff route <model>…`: print the catalog or+ // pull fresh metadata; route dry-runs provider seating on the same caches.+ if (flags.positionals.items.len > 0 and (std.mem.eql(u8, flags.positionals.items[0], "models") or std.mem.eql(u8, flags.positionals.items[0], "route"))) {const home = keys_cli.homeEnv(init.environ_map) orelse std.process.fatal("no HOME/USERPROFILE", .{});const codex_home = init.environ_map.get("CODEX_HOME") orelse(std.fmt.allocPrint(arena, "{s}/.codex", .{home}) catch "");@@ -579,7 +579,7 @@ pub fn runSubcommand(io: Io, gpa: Allocator, arena: Allocator, init: std.processrouter_keys.router_source = if (router_keys.router_value != null) .stored else .none;}router_catalog.loadAll(io, gpa, arena, home, router_keys, refreshing);- try models_cache.command(io, gpa, arena, home, flags.positionals.items[1..]);+ if (std.mem.eql(u8, flags.positionals.items[0], "route")) try @import("route_check.zig").command(io, gpa, arena, init.environ_map, flags.positionals.items[1..]) else try models_cache.command(io, gpa, arena, home, flags.positionals.items[1..]);return true;}diff --git a/src/test_hooks.zig b/src/test_hooks.zigindex 8cf34930..648a69e2 100644--- a/src/test_hooks.zig+++ b/src/test_hooks.zig@@ -52,6 +52,8 @@ const route_policy_tests = @import("route_policy_tests.zig");const route_trace = @import("route_trace.zig");// #376 phase-uniform learned routing + the rung-stratified fitness fold.const route_phase = @import("route_phase.zig");+const route_check = @import("route_check.zig");+const pricing_tests = @import("pricing_tests.zig");const fitness_strata = @import("fitness_strata.zig");// `graff serve` resumability (#330): serve.zig imports these, but nothing in@@ -95,6 +97,8 @@ test {_ = route_policy_tests;_ = route_trace;_ = route_phase;+ _ = route_check;+ _ = pricing_tests;_ = fitness_strata;_ = serve_events;_ = serve_create;