graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(catalog): Anthropic serves its live model list, like the OpenAI routers
Rach Pradhan795b0ec4e0b9
diff --git a/src/pricing.zig b/src/pricing.zigindex 94032927..da52c212 100644--- a/src/pricing.zig+++ b/src/pricing.zig@@ -176,6 +176,11 @@ pub const model_table = [_]ModelInfo{// LM Studio serves whatever model is loaded; "lmstudio" is a routing alias —// swap for your loaded model id if LM Studio requires an exact match (GET :1234/v1/models)..{ .provider = "lmstudio", .name = "lmstudio", .context = 200_000 },+ // Anthropic: no-key/offline FALLBACK ONLY. With a key, router_catalog+ // fetches /v1/models and replaces this slice 1:1 with the live list+ // (windows from max_input_tokens) — do not grow or "fix" these rows to+ // track releases; they exist so boot, --schema, and routing work before+ // the first authenticated fetch..{ .provider = "anthropic", .name = "claude-fable-5", .context = 1_000_000 },.{ .provider = "anthropic", .name = "claude-opus-4-8", .context = 1_000_000 },.{ .provider = "anthropic", .name = "claude-opus-4-7", .context = 1_000_000 },diff --git a/src/provider.zig b/src/provider.zigindex a24ad895..98dc196f 100644--- a/src/provider.zig+++ b/src/provider.zig@@ -39,7 +39,7 @@ fn contextWindowFor(provider_id: []const u8, model: []const u8) u64 {/// `.graff/.config.router` at startup.pub const ProviderSpec = struct {pub const LoginKind = enum { api_key, codegraff_device, codex_device, kimi_device };- pub const CatalogKind = enum { baked, codex, kimi, openai };+ pub const CatalogKind = enum { baked, codex, kimi, openai, anthropic };id: []const u8,display_name: []const u8,@@ -55,7 +55,10 @@ pub const ProviderSpec = struct {};pub const provider_specs = [_]ProviderSpec{- .{ .id = "anthropic", .display_name = "Anthropic", .kind = .anthropic, .auth = .x_api_key, .url = "https://api.anthropic.com/v1/messages", .env_key = "ANTHROPIC_API_KEY", .default_model = "claude-opus-4-8" },+ // Anthropic publishes its live model list at /v1/models (same x-api-key ++ // anthropic-version auth as Messages), so new Claude releases appear+ // without a rebuild; the baked pricing.zig rows stay the offline fallback.+ .{ .id = "anthropic", .display_name = "Anthropic", .kind = .anthropic, .auth = .x_api_key, .url = "https://api.anthropic.com/v1/messages", .env_key = "ANTHROPIC_API_KEY", .default_model = "claude-opus-4-8", .catalog = .anthropic, .models_url = "https://api.anthropic.com/v1/models?limit=1000" },.{ .id = "codegraff", .display_name = "Codegraff", .kind = .openai, .auth = .bearer, .url = "https://gateway.codegraff.com/v1/chat/completions", .env_key = "CODEGRAFF_API_KEY", .default_model = "deepseek-v4-pro", .login = .codegraff_device, .catalog = .openai, .models_url = "https://gateway.codegraff.com/v1/models", .takes_effort = true },.{ .id = "deepseek", .display_name = "DeepSeek", .kind = .openai, .auth = .bearer, .url = "https://api.deepseek.com/chat/completions", .env_key = "DEEPSEEK_API_KEY", .default_model = "deepseek-v4-pro", .takes_effort = true },.{ .id = "openai", .display_name = "OpenAI", .kind = .openai, .auth = .bearer, .url = "https://api.openai.com/v1/chat/completions", .env_key = "OPENAI_API_KEY", .default_model = "gpt-5.6" },diff --git a/src/router_catalog.zig b/src/router_catalog.zigindex f7444132..19b1b3e1 100644--- a/src/router_catalog.zig+++ b/src/router_catalog.zig@@ -1,13 +1,22 @@-//! Model discovery for Codegraff and the optional workspace router.+//! Model discovery for Codegraff, Anthropic, and the optional workspace router.//!//! The workspace router is declared by `.graff/.config.router`; its catalog//! cache stays beside that config. Catalog failures never prevent startup.+//! Anthropic's `/v1/models` (catalog kind `.anthropic`) rides the same+//! machinery so new Claude releases appear without a rebuild. It differs in+//! auth (x-api-key + anthropic-version, like Messages) and in pagination:+//! the Models API pages with after_id/has_more/last_id rather than the+//! page/next_page scheme, so fetch follows has_more until the list is+//! complete. Discovery replaces the provider's slice, keeping graff's list+//! 1:1 with the official one; the baked pricing.zig rows remain purely the+//! no-key/offline fallback.const std = @import("std");const Io = std.Io;const Allocator = std.mem.Allocator;const Value = std.json.Value;+const root = @import("main.zig");const catalog_selection = @import("catalog_selection.zig");const credential_store = @import("credential_store.zig");const pricing = @import("pricing.zig");@@ -23,6 +32,10 @@ var additional_attempted = false;const Snapshot = struct {models: []const pricing.ModelInfo,fetched_at_ms: i64 = 0,+ // Models-API cursor pagination (after_id/has_more/last_id). Absent on+ // OpenAI-shaped routers and on this module's cache shape → one page.+ has_more: bool = false,+ last_id: ?[]const u8 = null,};fn isAdditional(spec: provider.ProviderSpec) bool {@@ -48,8 +61,14 @@ fn readSmall(io: Io, arena: Allocator, path: []const u8) ?[]const u8 {return Io.Dir.cwd().readFileAlloc(io, path, arena, .limited(1024 * 1024)) catch null;}+/// Whether this module demand-loads the spec's live model list over HTTP.+/// Codex and Kimi have their own bespoke loaders (models_cache/kimi_catalog).+pub fn dynamic(spec: provider.ProviderSpec) bool {+ return spec.catalog == .openai or spec.catalog == .anthropic;+}+pub fn modelsUrl(spec: provider.ProviderSpec) []const u8 {- return if (spec.catalog == .openai) spec.models_url else "";+ return if (dynamic(spec)) spec.models_url else "";}fn validModelId(id: []const u8) bool {@@ -72,6 +91,26 @@ fn boolField(obj: std.json.ObjectMap, name: []const u8) bool {return value == .bool and value.bool;}+/// Context window from the baked offline table for a live-discovered row+/// whose catalog omits windows (Anthropic's /v1/models has no context field).+/// Exact provider+name first, then the longest baked name that prefixes a+/// dated id at a '-' boundary (claude-opus-4-8-20260115 → claude-opus-4-8).+fn bakedContext(provider_id: []const u8, name: []const u8) ?u64 {+ var best: ?u64 = null;+ var best_len: usize = 0;+ for (pricing.model_table) |m| {+ if (!std.mem.eql(u8, m.provider, provider_id)) continue;+ if (std.mem.eql(u8, m.name, name)) return m.context;+ if (m.name.len > best_len and m.name.len < name.len and+ std.mem.startsWith(u8, name, m.name) and name[m.name.len] == '-')+ {+ best = m.context;+ best_len = m.name.len;+ }+ }+ return best;+}+fn listsReasoning(obj: std.json.ObjectMap) bool {const params = obj.get("supported_parameters") orelse return false;if (params != .array) return false;@@ -100,7 +139,11 @@ pub fn parseModels(arena: Allocator, provider_id: []const u8, data: []const u8)if (duplicate) continue;var context = positiveInt(item.object, "context_length");if (context == 0) context = positiveInt(item.object, "context");- if (context == 0) context = pricing.default_context;+ // Anthropic declares the input window as max_input_tokens. Its sibling+ // max_tokens is the OUTPUT cap (128k on Opus 5) — never read it as a+ // window, or a 1M-context model would compact at ~102k.+ if (context == 0) context = positiveInt(item.object, "max_input_tokens");+ if (context == 0) context = bakedContext(provider_id, id) orelse pricing.default_context;rows.append(arena, .{.provider = provider_id,.name = arena.dupe(u8, id) catch continue,@@ -110,36 +153,88 @@ pub fn parseModels(arena: Allocator, provider_id: []const u8, data: []const u8)}const owned = rows.toOwnedSlice(arena) catch return null;if (owned.len == 0) return null;+ const has_more = value.object.get("has_more") orelse Value{ .null = {} };return .{.models = owned,.fetched_at_ms = switch (value.object.get("fetched_at_ms") orelse Value{ .null = {} }) {.integer => |i| i,else => 0,},+ .has_more = has_more == .bool and has_more.bool,+ .last_id = util.strFieldObj(value.object, "last_id"),};}-fn fetch(io: Io, gpa: Allocator, arena: Allocator, spec: provider.ProviderSpec, key: []const u8) ?Snapshot {- const url = modelsUrl(spec);- if (url.len == 0) return null;+/// Catalog GET auth mirrors the provider's chat auth: OpenAI-style routers+/// take a bearer token; Anthropic's /v1/models wants the same x-api-key ++/// anthropic-version pair as the Messages endpoint itself.+fn catalogHeaders(arena: Allocator, spec: provider.ProviderSpec, key: []const u8, buf: *[3]std.http.Header) ?[]const std.http.Header {+ buf[0] = .{ .name = "Accept", .value = "application/json" };+ if (spec.auth == .x_api_key) {+ buf[1] = .{ .name = "x-api-key", .value = key };+ buf[2] = .{ .name = "anthropic-version", .value = root.anthropic_version };+ return buf[0..3];+ }+ const bearer = std.fmt.allocPrint(arena, "Bearer {s}", .{key}) catch return null;+ buf[1] = .{ .name = "Authorization", .value = bearer };+ return buf[0..2];+}++/// Next page of a Models-API cursor walk: `after_id` rides alongside the+/// base URL's own query (limit=1000).+fn pageUrl(arena: Allocator, base: []const u8, after_id: ?[]const u8) ?[]const u8 {+ const id = after_id orelse return base;+ const sep: u8 = if (std.mem.indexOfScalar(u8, base, '?') != null) '&' else '?';+ return std.fmt.allocPrint(arena, "{s}{c}after_id={s}", .{ base, sep, id }) catch null;+}++fn containsName(rows: []const pricing.ModelInfo, name: []const u8) bool {+ for (rows) |row| if (std.mem.eql(u8, row.name, name)) return true;+ return false;+}++fn fetchPage(io: Io, gpa: Allocator, arena: Allocator, spec: provider.ProviderSpec, key: []const u8, url: []const u8) ?Snapshot {var client: std.http.Client = .{ .allocator = gpa, .io = io };defer client.deinit();var aw: Io.Writer.Allocating = .init(arena);- const bearer = std.fmt.allocPrint(arena, "Bearer {s}", .{key}) catch return null;- const headers = [_]std.http.Header{- .{ .name = "Accept", .value = "application/json" },- .{ .name = "Authorization", .value = bearer },- };+ var headers_buf: [3]std.http.Header = undefined;+ const headers = catalogHeaders(arena, spec, key, &headers_buf) orelse return null;const res = client.fetch(.{.location = .{ .url = url },.method = .GET,.response_writer = &aw.writer,- .extra_headers = &headers,+ .extra_headers = headers,}) catch return null;if (@intFromEnum(res.status) != 200) return null;return parseModels(arena, spec.id, aw.writer.buffered());}+/// Fetch the COMPLETE model list. The Models API pages with after_id ++/// has_more/last_id (not page/next_page), so one GET is not the whole+/// catalog; OpenAI-shaped routers never set has_more and stay one page.+/// A failed later page keeps the rows already gathered rather than+/// discarding a valid prefix; the page cap only guards against a server+/// that always answers has_more.+fn fetch(io: Io, gpa: Allocator, arena: Allocator, spec: provider.ProviderSpec, key: []const u8) ?Snapshot {+ const base = modelsUrl(spec);+ if (base.len == 0) return null;+ var rows: std.ArrayList(pricing.ModelInfo) = .empty;+ var after_id: ?[]const u8 = null;+ var pages: usize = 0;+ while (pages < 16) : (pages += 1) {+ const url = pageUrl(arena, base, after_id) orelse break;+ const page = fetchPage(io, gpa, arena, spec, key, url) orelse break;+ for (page.models) |m| {+ if (containsName(rows.items, m.name)) continue;+ rows.append(arena, m) catch return null;+ }+ if (!page.has_more) break;+ after_id = page.last_id orelse break;+ }+ if (rows.items.len == 0) return null;+ return .{ .models = rows.toOwnedSlice(arena) catch return null };+}+fn cacheDocument(io: Io, arena: Allocator, spec: provider.ProviderSpec, models: []const pricing.ModelInfo) ?[]const u8 {var aw: Io.Writer.Allocating = .init(arena);aw.writer.writeAll("{\"source\":") catch return null;@@ -178,20 +273,27 @@ fn cachedSnapshot(io: Io, arena: Allocator, home: []const u8, spec: provider.Pro/// Activate one router's catalog. Fresh cache short-circuits the network; stale/// cache remains an offline fallback. No failure is fatal.+/// Swap the provider's slice for the discovered rows — the active list stays+/// 1:1 with the provider's own catalog; baked rows return only when no live+/// or cached snapshot exists (no key, offline first run).+fn activate(arena: Allocator, spec: provider.ProviderSpec, discovered: []const pricing.ModelInfo) bool {+ return pricing.activateProviderModels(arena, spec.id, discovered);+}+fn loadSpec(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, spec: provider.ProviderSpec, key: []const u8, force_refresh: bool) bool {- if (spec.catalog != .openai or key.len == 0) return false;+ if (!dynamic(spec) or key.len == 0) return false;const cached = cachedSnapshot(io, arena, home, spec);if (!force_refresh) if (cached) |snapshot| {const age = util.unixMs(io) - snapshot.fetched_at_ms;- if (age >= 0 and age <= cache_ttl_ms and pricing.activateProviderModels(arena, spec.id, snapshot.models)) return true;+ if (age >= 0 and age <= cache_ttl_ms and activate(arena, spec, snapshot.models)) return true;};if (fetch(io, gpa, arena, spec, key)) |snapshot| {- if (pricing.activateProviderModels(arena, spec.id, snapshot.models)) {+ if (activate(arena, spec, snapshot.models)) {if (home.len != 0 or isAdditional(spec)) writeCache(io, arena, home, spec, snapshot.models);return true;}}- if (cached) |snapshot| return pricing.activateProviderModels(arena, spec.id, snapshot.models);+ if (cached) |snapshot| return activate(arena, spec, snapshot.models);return false;}@@ -211,7 +313,7 @@ fn ensureAdditional(io: Io, gpa: Allocator, arena: Allocator, home: []const u8,pub fn ensureForStartup(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, keys: provider.Keys, model_flag: ?[]const u8, saved: ?serde.SavedModel) void {for (provider.provider_specs, 0..) |spec, index| {- if (spec.catalog != .openai or !catalog_selection.startupMayUse(keys, spec.id, model_flag, saved)) continue;+ if (!dynamic(spec) or !catalog_selection.startupMayUse(keys, spec.id, model_flag, saved)) continue;ensureAt(io, gpa, arena, home, index, keys.get(spec.id) orelse "");}if (provider.additional_router) |spec|@@ -221,7 +323,7 @@ pub fn ensureForStartup(io: Io, gpa: Allocator, arena: Allocator, home: []constpub fn ensureForQuery(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, keys: provider.Keys, query: []const u8) void {for (provider.provider_specs, 0..) |spec, index| {- if (spec.catalog != .openai or !catalog_selection.queryMayUse(spec.id, query)) continue;+ if (!dynamic(spec) or !catalog_selection.queryMayUse(spec.id, query)) continue;ensureAt(io, gpa, arena, home, index, keys.get(spec.id) orelse "");}if (provider.additional_router) |spec|@@ -231,7 +333,7 @@ pub fn ensureForQuery(io: Io, gpa: Allocator, arena: Allocator, home: []const u8pub fn loadAll(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, keys: provider.Keys, force_refresh: bool) void {for (provider.provider_specs, 0..) |spec, index| {- if (spec.catalog != .openai) continue;+ if (!dynamic(spec)) continue;attempted[index] = true;_ = loadSpec(io, gpa, arena, home, spec, keys.get(spec.id) orelse "", force_refresh);}@@ -245,13 +347,13 @@ pub fn loadAll(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, keys:/// uses this so GUI model metadata follows router rollouts.pub fn loadCachedAll(io: Io, arena: Allocator, home: []const u8) void {for (provider.provider_specs) |spec| {- if (spec.catalog != .openai) continue;+ if (!dynamic(spec)) continue;const snapshot = cachedSnapshot(io, arena, home, spec) orelse continue;- _ = pricing.activateProviderModels(arena, spec.id, snapshot.models);+ _ = activate(arena, spec, snapshot.models);}if (provider.additional_router) |spec| {const snapshot = cachedSnapshot(io, arena, home, spec) orelse return;- _ = pricing.activateProviderModels(arena, spec.id, snapshot.models);+ _ = activate(arena, spec, snapshot.models);}}@@ -317,6 +419,110 @@ test "cache document serializes every model name as valid JSON" {try std.testing.expect(second.get("supports_reasoning").?.bool);}+test "Anthropic catalog is live and authenticates like Messages" {+ const spec = provider.specFor("anthropic") orelse return error.TestUnexpectedResult;+ try std.testing.expectEqual(provider.ProviderSpec.CatalogKind.anthropic, spec.catalog);+ try std.testing.expect(dynamic(spec));+ try std.testing.expectEqualStrings("https://api.anthropic.com/v1/models?limit=1000", modelsUrl(spec));+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ var buf: [3]std.http.Header = undefined;+ const headers = catalogHeaders(state.allocator(), spec, "sk-test", &buf) orelse+ return error.TestUnexpectedResult;+ try std.testing.expectEqual(@as(usize, 3), headers.len);+ try std.testing.expectEqualStrings("x-api-key", headers[1].name);+ try std.testing.expectEqualStrings("sk-test", headers[1].value);+ try std.testing.expectEqualStrings("anthropic-version", headers[2].name);+ // Bearer routers are unchanged by the x-api-key branch.+ const router = provider.specFor("codegraff") orelse return error.TestUnexpectedResult;+ const bearer = catalogHeaders(state.allocator(), router, "key", &buf) orelse+ return error.TestUnexpectedResult;+ try std.testing.expectEqual(@as(usize, 2), bearer.len);+ try std.testing.expectEqualStrings("Authorization", bearer[1].name);+}++test "Anthropic /v1/models rows inherit baked context windows" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ // A row declaring max_input_tokens uses it as the window — and never its+ // sibling max_tokens, which is the OUTPUT cap (128k on Opus 5). A row+ // without limits takes its alias family's baked window; an unknown family+ // falls back to the conservative default.+ const snapshot = parseModels(state.allocator(), "anthropic",+ \\{"data":[+ \\ {"type":"model","id":"claude-opus-5","display_name":"Claude Opus 5",+ \\ "max_input_tokens":1000000,"max_tokens":128000},+ \\ {"type":"model","id":"claude-opus-4-8-20260115","display_name":"Claude Opus 4.8"},+ \\ {"type":"model","id":"claude-haiku-4-5-20251001","display_name":"Claude Haiku 4.5"},+ \\ {"type":"model","id":"claude-nova-9","display_name":"Claude Nova 9"}+ \\],"has_more":false}+ ) orelse return error.TestUnexpectedResult;+ try std.testing.expectEqual(@as(usize, 4), snapshot.models.len);+ try std.testing.expectEqual(@as(u64, 1_000_000), snapshot.models[0].context);+ try std.testing.expectEqual(@as(u64, 1_000_000), snapshot.models[1].context);+ try std.testing.expectEqual(@as(u64, 200_000), snapshot.models[2].context);+ try std.testing.expectEqual(pricing.default_context, snapshot.models[3].context);+}++test "Anthropic discovery keeps the active slice 1:1 with the live list" {+ const saved = pricing.active_model_table;+ defer pricing.active_model_table = saved;+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const spec = provider.specFor("anthropic") orelse return error.TestUnexpectedResult;+ const discovered = [_]pricing.ModelInfo{+ .{ .provider = "anthropic", .name = "claude-opus-5", .context = 1_000_000 },+ .{ .provider = "anthropic", .name = "claude-opus-4-8", .context = 1_000_000 },+ };+ try std.testing.expect(activate(state.allocator(), spec, &discovered));+ // Exactly the live rows — a baked row the API no longer lists is gone,+ // so the picker shows the official catalog and nothing else.+ var count: usize = 0;+ for (pricing.models()) |m| {+ if (std.mem.eql(u8, m.provider, "anthropic")) count += 1;+ }+ try std.testing.expectEqual(@as(usize, 2), count);+ try std.testing.expect(pricing.providerModelInTable("anthropic", "claude-opus-5"));+ try std.testing.expect(!pricing.providerModelInTable("anthropic", "claude-sonnet-4-5"));+ try std.testing.expectEqual(@as(u64, 1_000_000), pricing.contextFor("anthropic", "claude-opus-5"));+ // Other providers' slices are untouched.+ try std.testing.expect(pricing.providerModelInTable("codegraff", "claude-opus-4.8"));+}++test "Models-API cursor pagination is followed 1:1 with the docs" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const arena = state.allocator();+ // parseModels surfaces the after_id/has_more/last_id scheme (the Models+ // API does NOT use page/next_page — see "API overview → Pagination").+ const page = parseModels(arena, "anthropic",+ \\{"data":[{"type":"model","id":"claude-opus-5","max_input_tokens":1000000,"max_tokens":128000}],+ \\ "has_more":true,"first_id":"claude-opus-5","last_id":"claude-opus-5"}+ ) orelse return error.TestUnexpectedResult;+ try std.testing.expect(page.has_more);+ try std.testing.expectEqualStrings("claude-opus-5", page.last_id orelse return error.TestUnexpectedResult);+ // A final page reports has_more:false; the cache shape carries neither.+ const last = parseModels(arena, "anthropic",+ \\{"data":[{"id":"claude-haiku-4-5"}],"has_more":false,"last_id":"claude-haiku-4-5"}+ ) orelse return error.TestUnexpectedResult;+ try std.testing.expect(!last.has_more);+ const cache = parseModels(arena, "anthropic",+ \\{"fetched_at_ms":1,"models":[{"name":"claude-opus-5","context":1000000}]}+ ) orelse return error.TestUnexpectedResult;+ try std.testing.expect(!cache.has_more);+ try std.testing.expect(cache.last_id == null);+ // after_id joins the base URL's existing query string.+ try std.testing.expectEqualStrings(+ "https://api.anthropic.com/v1/models?limit=1000&after_id=claude-opus-5",+ pageUrl(arena, "https://api.anthropic.com/v1/models?limit=1000", "claude-opus-5") orelse return error.TestUnexpectedResult,+ );+ try std.testing.expectEqualStrings(+ "https://example.com/models?after_id=m1",+ pageUrl(arena, "https://example.com/models", "m1") orelse return error.TestUnexpectedResult,+ );+ try std.testing.expectEqualStrings("base", pageUrl(arena, "base", null) orelse return error.TestUnexpectedResult);+}+test "router discovery replaces only its provider slice" {const saved = pricing.active_model_table;defer pricing.active_model_table = saved;diff --git a/src/startup.zig b/src/startup.zigindex 7ae1b77d..0390742d 100644--- a/src/startup.zig+++ b/src/startup.zig@@ -568,7 +568,7 @@ pub fn runSubcommand(io: Io, gpa: Allocator, arena: Allocator, init: std.process_ = kimi_catalog.load(io, gpa, arena, home, kimi_token);var router_keys: provider_mod.Keys = .{ .values = @splat(null) };for (provider_mod.provider_specs, &router_keys.values) |spec, *value| {- if (spec.catalog != .openai) continue;+ if (!router_catalog.dynamic(spec)) continue;const login_key = if (spec.login == .codegraff_device) oauth.loadCodegraffKey(io, arena, home) else null;value.* = init.environ_map.get(spec.env_key) orelselogin_key orelse keys_cli.loadStoredKey(io, arena, home, spec.id);