graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Kimi: report graff as the platform, bill console keys as plan
justrach2e277dc8dbb6
diff --git a/docs/adr/0027-kimi-identity-is-graff.md b/docs/adr/0027-kimi-identity-is-graff.mdindex 80878557..2efcee8a 100644--- a/docs/adr/0027-kimi-identity-is-graff.md+++ b/docs/adr/0027-kimi-identity-is-graff.md@@ -1,6 +1,6 @@# 0027. Kimi Coding identity is graff, device fields follow kimi-code-Status: accepted 2026-08-25+Status: superseded by 0204 (2026-09-26)## Contextdiff --git a/docs/adr/0204-kimi-platform-is-graff.md b/docs/adr/0204-kimi-platform-is-graff.mdnew file mode 100644index 00000000..7d2d2509--- /dev/null+++ b/docs/adr/0204-kimi-platform-is-graff.md@@ -0,0 +1,33 @@+# 0204. Kimi requests identify as graff on every header++Status: accepted 2026-09-26. Supersedes 0027.++## Context++ADR 0027 kept `User-Agent: graff/<version>` but sent+`X-Msh-Platform: kimi_code_cli`, the Kimi CLI's own value. Kimi's community+guidelines forbid altering client identity (#1297).++The kimi-code OAuth package (`packages/oauth/src/identity.ts`, MIT) is built+for several hosts on one OAuth client. Each host must state its own+`X-Msh-Platform` (`kimi_code_cli`, `kimi_code_vscode`, `kimi_code_desktop`),+so the OAuth host can tell client families apart. The Kimi OAuth host issues+device codes to a `graff` platform, and the Kimi Code API serves the model+list to a token used with it.++## Decision++- `X-Msh-Platform` is `graff` on every Kimi request, OAuth and inference.+ The User-Agent stays `graff/<version>`. No Kimi request carries+ `kimi_code_cli` or `kimi-code-cli`.+- `graff login kimi` keeps the device-code flow on the shared kimi-code+ client. Kimi can see that graff is the caller and can decide.+- A Kimi Code console key (`KIMI_API_KEY` or `graff key set kimi`) is+ membership quota on `api.kimi.com/coding`, so it bills as a plan, not per+ token.++## Consequences++If Kimi restricts its OAuth client to its own platforms, `graff login kimi`+fails visibly and the console key remains the supported path. graff never+falls back to reporting another client's identity.diff --git a/docs/adr/README.md b/docs/adr/README.mdindex efe08cbb..33868486 100644--- a/docs/adr/README.md+++ b/docs/adr/README.md@@ -37,7 +37,7 @@ record only when you need the evidence or the edge cases.| [0024](0024-three-harness-compare-prompt-subagent-rss.md) | Steal short child briefs + sidecar spawn; `print(read_file)` returns the file; `-p` skips the shared-tree checkpoint (evals are sibling sandboxes); reject rlm-only catalog, grok heap, Harbor. || [0025](0025-io-uring-is-not-the-process-io.md) | Process Io stays Zig `Threaded`. spec-ptc is already the default loop (ADR 0022). Do not take ublk or switch `main` to `std.Io.Uring`. || [0026](0026-foreground-bash-auto-backgrounds.md) | Root foreground `bash` auto-backgrounds after 120s (a shorter `timeout` may promote earlier); it is not killed. Subagents still kill at 120s (#93). |-| [0027](0027-kimi-identity-is-graff.md) | Kimi Coding User-Agent is `graff/<version>`, not a spoofed `kimi-code-cli` token. X-Msh device fields follow kimi-code's shapes. |+| [0027](0027-kimi-identity-is-graff.md) | Kimi Coding User-Agent is `graff/<version>`, not a spoofed `kimi-code-cli` token. X-Msh device fields follow kimi-code's shapes. Superseded by 0204. || [0028](0028-codex-session-id-is-the-cache-key.md) | Codex HTTP/WS `session_id` is the `prompt_cache_key` (openai/codex ModelClient default), not a per-process random UUID. || [0029](0029-mcp-inside-rlm-and-return-shapes.md) | Loaded MCP tools are rlm host functions; persist return shapes on the load result, never the prefix; fat MCP results auto-slim; default `-p` connects `.mcp.json` (folded, not skipped). || [0030](0030-rlm-late-showcase.md) | Superseded by [0140](0140-rlm-is-listed-when-available.md). Late showcase hid `rlm` on small turns; that gate is gone. |@@ -229,6 +229,7 @@ record only when you need the evidence or the edge cases.| [0200](0200-desktop-page-transitions-preserve-tabs.md) | Desktop restart/reload confirms active-turn interruption and restores tab references from saved sessions in their workspaces. || [0201](0201-cli-beta-channel-is-explicit.md) | CLI beta updates require explicit opt-in, select the newest numeric release branch, verify pinned assets, and activate on next launch. || [0202](0202-acp-sessions-adopt-client-cwd.md) | ACP sessions adopt the client `cwd`, report the worktree as `_meta["graff/worktree"]` (null in a main checkout), and `graff worktree` exits non-zero when it refuses or does nothing. |+| [0204](0204-kimi-platform-is-graff.md) | Kimi requests send `X-Msh-Platform: graff` and `graff/<version>` everywhere (OAuth and inference), never `kimi_code_cli`; a Kimi Code console key bills as plan quota. Supersedes 0027. |## When to write onediff --git a/src/billing.zig b/src/billing.zigindex 4821bc56..743ed277 100644--- a/src/billing.zig+++ b/src/billing.zig@@ -87,9 +87,17 @@ pub const CostClass = enum {/// vendor. A seat with no credential yet falls back to the provider table and/// there advertises what signing in would buy, which is the only useful thing/// to say on a row the user cannot pick yet.+/// A key that spends a membership plan, not a meter. Kimi Code keys are made+/// in the Kimi Code console, only work on api.kimi.com/coding, and draw on the+/// membership quota (#1297).+fn planKey(provider_id: []const u8) bool {+ return std.mem.eql(u8, provider_id, "kimi");+}+pub fn costFor(provider_id: []const u8, source: CredentialSource) CostClass {if (localSeat(provider_id)) return .local;if (std.mem.eql(u8, provider_id, "codegraff")) return .credits;+ if (planKey(provider_id)) return .plan;if (!subscriptionLogin(provider_id)) return .api;return switch (source) {.login, .none => .plan,@@ -100,6 +108,7 @@ pub fn costFor(provider_id: []const u8, source: CredentialSource) CostClass {/// The billing class of one API call on this seat.pub fn forSeat(provider_id: []const u8, model: []const u8, source: CredentialSource) Billing {if (source == .login and subscriptionLogin(provider_id)) return .sub;+ if (planKey(provider_id) and source != .none) return .sub;return if (pricing.priceForProvider(provider_id, model) != null) .priced else .unpriced;}@@ -123,10 +132,11 @@ test "a flat-rate login and a metered key on the same seat bill differently (#47try std.testing.expectEqual(Billing.sub, forSeat("xai", "grok-4.3", .login));try std.testing.expectEqual(Billing.priced, forSeat("xai", "grok-4.3", .environment));- // Same discipline for the two providers that were already classified: the- // subscription is the login, so KIMI_API_KEY is metered per token.+ // Kimi is the exception (#1297): a Kimi Code console key draws on the+ // membership quota, so KIMI_API_KEY bills like the login does.try std.testing.expectEqual(Billing.sub, forSeat("kimi", "kimi-k2.7", .login));- try std.testing.expectEqual(Billing.priced, forSeat("kimi", "kimi-k2.7", .environment));+ try std.testing.expectEqual(Billing.sub, forSeat("kimi", "kimi-k2.7", .environment));+ try std.testing.expectEqual(Billing.sub, forSeat("kimi", "kimi-k2.7", .stored));// codex has no usable env key (env_key is the CODEX_DISABLED sentinel), so// it is only ever reached by login — but the source still gates it.@@ -185,7 +195,8 @@ test "costFor: the picker's badge follows the credential, then the provider" {try std.testing.expectEqual(CostClass.plan, costFor("xai", .login));try std.testing.expectEqual(CostClass.api, costFor("xai", .environment));try std.testing.expectEqual(CostClass.plan, costFor("kimi", .login));- try std.testing.expectEqual(CostClass.api, costFor("kimi", .stored));+ try std.testing.expectEqual(CostClass.plan, costFor("kimi", .stored));+ try std.testing.expectEqual(CostClass.plan, costFor("kimi", .environment));// localhost is nobody's bill.try std.testing.expectEqual(CostClass.local, costFor("lmstudio", .environment));diff --git a/src/http_headers.zig b/src/http_headers.zigindex ea81b002..3b574cf0 100644--- a/src/http_headers.zig+++ b/src/http_headers.zig@@ -557,7 +557,8 @@ test "Kimi chat sends graff identity and kimi-code device headers (#617)" {var buf: [12]std.http.Header = undefined;const p: Provider = .{ .id = "kimi", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "k3", .context = 256_000 };const headers = providerHeaders(io, p, "Bearer k", &buf);- try std.testing.expectEqualStrings("kimi_code_cli", headerValue(headers, "X-Msh-Platform").?);+ try std.testing.expectEqualStrings("graff", headerValue(headers, "X-Msh-Platform").?);+ for (headers) |h| try std.testing.expect(std.mem.indexOf(u8, h.value, "kimi_code_cli") == null and std.mem.indexOf(u8, h.value, "kimi-code-cli") == null);try std.testing.expect(headerValue(headers, "X-Msh-Device-Name").?.len > 0);try std.testing.expect(!std.mem.eql(u8, headerValue(headers, "X-Msh-Device-Name").?, "unknown"));const model = headerValue(headers, "X-Msh-Device-Model").?;diff --git a/src/kimi_identity.zig b/src/kimi_identity.zigindex 4c6cdeb6..c75a8239 100644--- a/src/kimi_identity.zig+++ b/src/kimi_identity.zig@@ -1,7 +1,8 @@//! Kimi Coding request identity. Field *shapes* follow MoonshotAI/kimi-code//! `packages/oauth/src/identity.ts` (hostname, Node-style device model,-//! `os.release()`). The product token stays `graff/<version>`: Moonshot treats-//! a spoofed User-Agent as a violation, and a host must state its own name.+//! `os.release()`). Every host states its own identity there: the product+//! token is `graff/<version>` and `X-Msh-Platform` is `graff`, never the Kimi+//! CLI's `kimi_code_cli` (#1297, ADR 0204).const std = @import("std");const builtin = @import("builtin");@@ -9,7 +10,7 @@ const Io = std.Io;const Allocator = std.mem.Allocator;pub const user_agent = "graff/" ++ @import("build_options").version;-pub const platform = "kimi_code_cli";+pub const platform = "graff";pub const version = @import("build_options").version;pub var device_id: []const u8 = "unknown";@@ -258,7 +259,9 @@ test "identityHeaders use hostname and Node-shaped model, not zig os tags" {const got = identityHeaders(&headers);try std.testing.expectEqual(@as(usize, 6), got.len);try std.testing.expectEqualStrings("X-Msh-Platform", got[0].name);- try std.testing.expectEqualStrings("kimi_code_cli", got[0].value);+ // graff states its own platform; it never reports the Kimi CLI's (#1297).+ try std.testing.expectEqualStrings("graff", got[0].value);+ for (got) |h| try std.testing.expect(std.mem.indexOf(u8, h.value, "kimi_code_cli") == null);try std.testing.expectEqualStrings(version, got[1].value);try std.testing.expectEqualStrings(device_name, got[2].value);try std.testing.expect(device_name.len > 0);diff --git a/src/oauth.zig b/src/oauth.zigindex 255667a5..60cc434a 100644--- a/src/oauth.zig+++ b/src/oauth.zig@@ -197,8 +197,11 @@ pub fn codexLogin(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, retry out.flush();}-// Kimi Code OAuth — device-code flow, same client + endpoints the Kimi CLI-// uses (auth.kimi.com). `graff login kimi` runs the flow and writes the token+// Kimi Code OAuth — device-code flow on the shared kimi-code OAuth client and+// endpoints (auth.kimi.com). Moonshot's own hosts share this client and tell+// themselves apart by `X-Msh-Platform`; graff reports `graff` and+// `graff/<version>`, never the CLI's identity (#1297, ADR 0204).+// `graff login kimi` runs the flow and writes the token// to ~/.kimi/credentials/graff-oauth.json; loadKimiOAuth reads it at startup// and refreshes in place when near expiry.const kimi_oauth_host = "https://auth.kimi.com";diff --git a/src/route_check.zig b/src/route_check.zigindex f3742961..70b77311 100644--- a/src/route_check.zig+++ b/src/route_check.zig@@ -134,11 +134,11 @@ test "seatFor: family-alias spelling seats the sub, exact names hold, unknown mitry std.testing.expectEqual(pricing.Billing.sub, sol.seat.billing);try std.testing.expect(seatFor(all, "totally-unknown-zzz") == .unknown);- // The same seat reached with KIMI_API_KEY is billed, not free. (Whether it- // lands on .priced or .unpriced is the price sheet's business; what must- // never happen is a metered key being written off as a subscription.)+ // A metered key is never written off as a subscription: XAI_API_KEY bills.+ // KIMI_API_KEY is a Kimi Code console key on the membership quota (#1297).const keyed = provider_mod.Keys{ .values = @splat("k"), .sources = @splat(.environment) };- try std.testing.expect(seatFor(keyed, "kimi-k3").seat.billing != .sub);+ try std.testing.expect(seatFor(keyed, "grok-4.3").seat.billing != .sub);+ try std.testing.expectEqual(pricing.Billing.sub, seatFor(keyed, "kimi-k3").seat.billing);}test "seatFor: catalogued model with no serving credential reports no_credential, not a gateway seat" {