Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

refactor: extract DGM score signing + fingerprinting into scoring.zig (600-line goal)

justrache914820d4f73
diff --git a/src/main.zig b/src/main.zig
index 613ca027..3cae33a5 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -107,6 +107,7 @@ test {
_ = messages_mod;
_ = http;
_ = terminal;
+ _ = scoring;
}
/// Wire format + auth style + endpoint per provider. Base URLs and env-var
@@ -530,50 +531,15 @@ const resolveNiche = fleet.resolveNiche;
/// Set by main(); runSub and the REPL loop record nodes through this.
var g_traj: ?*Trajectory = null;
-/// Short stable fingerprint of a system prompt (first 8 bytes of SHA-256,
-/// hex): equal hashes along a trajectory edge mean the prompt didn't change.
-fn promptFingerprint(prompt: []const u8) [16]u8 {
- var d: [32]u8 = undefined;
- std.crypto.hash.sha2.Sha256.hash(prompt, &d, .{});
- return std.fmt.bytesToHex(d[0..8].*, .lower);
-}
-
-/// Capability tier for the running model — the MAP-Elites grid's provider-class
-/// axis (docs/hyperagents.md §9). A curated table maps known model families to a
-/// tier (price alone is a poor capability proxy: a small model can be premium-
-/// priced and a frontier model cheap), then falls back to the models.dev output
-/// price graff already carries, then to "mid". Returns "frontier" | "mid" | "small".
-fn providerClass(model: []const u8) []const u8 {
- const Tier = struct { needle: []const u8, tier: []const u8 };
- // Most-specific first; small markers precede the families they sit in so
- // e.g. "...-flash" beats "gemini-3" and "deepseek-v4-flash" beats "deepseek-v4".
- const known = [_]Tier{
- .{ .needle = "haiku", .tier = "small" },
- .{ .needle = "flash", .tier = "small" },
- .{ .needle = "-mini", .tier = "small" }, // -mini suffix, not "minimax"/"gemini"
- .{ .needle = "lite", .tier = "small" },
- .{ .needle = "nano", .tier = "small" },
- .{ .needle = "opus", .tier = "frontier" },
- .{ .needle = "gpt-5", .tier = "frontier" },
- .{ .needle = "deepseek-v4", .tier = "frontier" },
- .{ .needle = "grok-4", .tier = "frontier" },
- .{ .needle = "glm-5", .tier = "frontier" },
- .{ .needle = "kimi-k2", .tier = "frontier" },
- .{ .needle = "minimax-m", .tier = "frontier" },
- .{ .needle = "mimo-v2.5-pro", .tier = "frontier" },
- .{ .needle = "fugu", .tier = "frontier" },
- .{ .needle = "gemini-3", .tier = "frontier" },
- .{ .needle = "sonnet", .tier = "mid" },
- };
- for (known) |k| if (std.ascii.indexOfIgnoreCase(model, k.needle) != null) return k.tier;
- // Unknown family: bucket by models.dev output price ($/1M output tokens).
- if (priceFor(model)) |p| {
- if (p.out >= 10) return "frontier";
- if (p.out >= 2) return "mid";
- return "small";
- }
- return "mid";
-}
+// Prompt/provider-class fingerprinting + DGM score signing live in scoring.zig
+// (600-line goal). Pure fns aliased back; the signing globals are
+// scoring.-qualified at their call sites.
+const scoring = @import("scoring.zig");
+const promptFingerprint = scoring.promptFingerprint;
+const providerClass = scoring.providerClass;
+const scoreSigMessage = scoring.scoreSigMessage;
+const signScore = scoring.signScore;
+const loadScoreKey = scoring.loadScoreKey;
// ── Subagent cards (#51) ───────────────────────────────────────────────────
//
@@ -772,73 +738,8 @@ fn writeSubagentDetail(
return path;
}
-// ── Score-channel signing (DGM fitness integrity, Step 0) ──────────────────
-//
-// The trajectory archive is the DGM's fitness ledger, and it lives in cwd —
-// so a forged `{"kind":"score",...}` row appended via write_file (cwd is
-// inside confinedPath) or `echo >>` manufactures fitness AND parent_sha
-// lineage that parent selection then rewards. Signing makes forged rows
-// detectable: every score record the harness writes carries an HMAC over its
-// fields, keyed by a secret the evolving agent's tools can't reach.
-//
-// Key custody: GRAFF_SCORE_KEY_FILE names a key file OUTSIDE cwd. The env var
-// holds only the path (not the secret), and the file is unreadable by a
-// subagent — file tools are confined to cwd, and subagent bash is cwd-locked
-// with no redirection. The root agent / --yolo is the trusted orchestrator
-// boundary (it could read any file anyway). Readers (dgm_loop.py load_archive,
-// the harness-telemetry worker) recompute the HMAC and reject unsigned rows.
-
-/// Loaded once in main() from GRAFF_SCORE_KEY_FILE; null → unsigned (signing
-/// off, every reader accepts rows as before — backward compatible).
-var g_score_key: ?[]const u8 = null;
-
-/// Per-session run id (random hex), the run_id field of signed score records.
-var g_run_id: [16]u8 = @splat('0');
-
-/// Canonical signing message — newline-delimited, version-prefixed, with the
-/// score formatted to a fixed 6 decimals so Zig/Python/JS agree byte-for-byte.
-/// `buf` must hold the message; returns the slice written.
-fn scoreSigMessage(
- buf: []u8,
- prompt_sha: []const u8,
- parent_sha: []const u8,
- score: f64,
- run_id: []const u8,
- judge_id: []const u8,
- artifact_sha: []const u8,
- eval_set_hash: []const u8,
-) ?[]const u8 {
- return std.fmt.bufPrint(buf, "v1\n{s}\n{s}\n{d:.6}\n{s}\n{s}\n{s}\n{s}", .{
- prompt_sha, parent_sha, score, run_id, judge_id, artifact_sha, eval_set_hash,
- }) catch null;
-}
-
-/// HMAC-SHA256 of the canonical message, hex. "" when no key is configured.
-fn signScore(
- prompt_sha: []const u8,
- parent_sha: []const u8,
- score: f64,
- run_id: []const u8,
- judge_id: []const u8,
- artifact_sha: []const u8,
- eval_set_hash: []const u8,
-) [64]u8 {
- const key = g_score_key orelse return @splat(0);
- var msgbuf: [512]u8 = undefined;
- const msg = scoreSigMessage(&msgbuf, prompt_sha, parent_sha, score, run_id, judge_id, artifact_sha, eval_set_hash) orelse return @splat(0);
- var mac: [std.crypto.auth.hmac.sha2.HmacSha256.mac_length]u8 = undefined;
- std.crypto.auth.hmac.sha2.HmacSha256.create(&mac, msg, key);
- return std.fmt.bytesToHex(mac, .lower);
-}
-
-/// Read the signing key from GRAFF_SCORE_KEY_FILE (a path outside cwd) into
-/// `arena`. Whitespace-trimmed; null when unset, unreadable, or empty.
-fn loadScoreKey(io: Io, arena: Allocator, environ: anytype) ?[]const u8 {
- const path = environ.get("GRAFF_SCORE_KEY_FILE") orelse return null;
- const data = Io.Dir.cwd().readFileAlloc(io, path, arena, .limited(64 * 1024)) catch return null;
- const trimmed = std.mem.trim(u8, data, " \t\r\n");
- return if (trimmed.len == 0) null else trimmed;
-}
+// Score-channel signing (DGM fitness integrity) + the session signing globals
+// live in scoring.zig (600-line goal); reached scoring.-qualified.
/// Per-agent record of external tool calls (name + error flag, in call
/// order): the process signal behind "which tool combinations work" —
@@ -4153,9 +4054,9 @@ pub fn main(init: std.process.Init) !void {
{
var raw: [8]u8 = undefined;
io.random(&raw);
- g_run_id = std.fmt.bytesToHex(raw, .lower);
+ scoring.g_run_id = std.fmt.bytesToHex(raw, .lower);
}
- g_score_key = loadScoreKey(io, arena, init.environ_map);
+ scoring.g_score_key = loadScoreKey(io, arena, init.environ_map);
// Telemetry endpoint precedence: opt-out always wins → else an
// env-configured endpoint (dev / override) → else the release build's
@@ -4732,9 +4633,9 @@ pub fn main(init: std.process.Init) !void {
break :eshblk "";
};
const req_run = reqStr.s(parsed.object, "run_id");
- const run_id: []const u8 = if (req_run.len > 0) utf8Prefix(req_run, 64) else &g_run_id;
+ const run_id: []const u8 = if (req_run.len > 0) utf8Prefix(req_run, 64) else &scoring.g_run_id;
const sig = signScore(sha, parent, sc, run_id, judge_id, artifact_sha, eval_set_hash);
- const signed = g_score_key != null;
+ const signed = scoring.g_score_key != null;
if (g_traj) |tj| tj.node(.{
.kind = "score",
.prompt_sha = sha,
@@ -8618,7 +8519,7 @@ pub const Agent = struct {
const esh_fp = promptFingerprint(cmd);
const genome: []const u8 = &genome_fp;
const esh: []const u8 = &esh_fp;
- const run_id: []const u8 = &g_run_id;
+ const run_id: []const u8 = &scoring.g_run_id;
const pclass = providerClass(self.provider.model);
// --niche tags this score's cell. Without it the score lands in the
// anonymous "" niche, which pullElites can never match to a builtin —
@@ -8632,7 +8533,7 @@ pub const Agent = struct {
// has nothing to serve. Gated on a niche: a "" cell is unpromotable.
if (niche.len > 0) t.fleetEvent("propose", niche, genome, "", pclass, "", 0, sys);
const sig = signScore(genome, "", s, run_id, "", "", esh);
- const sig_s: []const u8 = if (g_score_key != null) &sig else "";
+ const sig_s: []const u8 = if (scoring.g_score_key != null) &sig else "";
var provbuf: [512]u8 = undefined;
const prov = std.fmt.bufPrint(&provbuf, "{s}\t{s}\t{s}\t{s}\t{s}", .{ "", "", esh, pclass, niche }) catch "";
t.scoreEvent(genome, "", s, run_id, sig_s, prov);
@@ -12387,7 +12288,7 @@ fn scoreVariants(
for (jfuts, jprompts) |*jf, jp| jf.* = ctx.io.async(judgeTask, .{ ctx, jp });
const pclass = providerClass(ctx.provider.model);
- const run_id: []const u8 = &g_run_id;
+ const run_id: []const u8 = &scoring.g_run_id;
for (jfuts, 0..) |*jf, k| {
const i = vidx[k];
const jout = jf.await(ctx.io);
@@ -12400,7 +12301,7 @@ fn scoreVariants(
const genome: []const u8 = &genome_fp;
const esh: []const u8 = &esh_fp;
const sig = signScore(genome, "", s, run_id, "", "", esh);
- const sig_s: []const u8 = if (g_score_key != null) &sig else "";
+ const sig_s: []const u8 = if (scoring.g_score_key != null) &sig else "";
var provbuf: [512]u8 = undefined;
const prov = std.fmt.bufPrint(&provbuf, "{s}\t{s}\t{s}\t{s}\t{s}", .{ "", "", esh, pclass, "" }) catch "";
t.scoreEvent(genome, "", s, run_id, sig_s, prov);
@@ -13075,33 +12976,6 @@ test "assembleOpenAI preserves streamed reasoning history" {
try std.testing.expectEqualStrings("alt path", message.get("reasoning").?.string);
}
-test "promptFingerprint is stable, short, and collision-visible" {
- const a = promptFingerprint("you are a careful reviewer");
- const b = promptFingerprint("you are a careful reviewer");
- const c = promptFingerprint("you are a careless reviewer");
- try std.testing.expectEqualStrings(&a, &b);
- try std.testing.expect(!std.mem.eql(u8, &a, &c));
-}
-
-test "score signing message is canonical and cross-language stable" {
- // This exact byte layout is the contract the Python/JS verifiers
- // reimplement — a change here breaks signature verification, so it is
- // pinned. Score is fixed to 6 decimals; fields newline-joined; "v1" tag.
- var buf: [512]u8 = undefined;
- const msg = scoreSigMessage(&buf, "aaaaaaaaaaaaaaaa", "", 0.5, "run123", "j1", "", "").?;
- try std.testing.expectEqualStrings("v1\naaaaaaaaaaaaaaaa\n\n0.500000\nrun123\nj1\n\n", msg);
-
- // With a key, signScore is a stable, key-dependent HMAC; without one
- // (default global) it is all-zero hex and callers omit it.
- g_score_key = "testkey";
- defer g_score_key = null;
- const sig = signScore("aaaaaaaaaaaaaaaa", "", 0.5, "run123", "j1", "", "");
- const sig2 = signScore("aaaaaaaaaaaaaaaa", "", 0.5, "run123", "j1", "", "");
- try std.testing.expectEqualStrings(&sig, &sig2); // deterministic
- const sig_changed = signScore("aaaaaaaaaaaaaaaa", "", 0.6, "run123", "j1", "", "");
- try std.testing.expect(!std.mem.eql(u8, &sig, &sig_changed)); // score is signed
-}
-
test "utf8Prefix truncates without splitting codepoints" {
try std.testing.expectEqualStrings("abc", utf8Prefix("abc", 10));
const s = [_]u8{ 'a', 'b', 0xC3, 0xA9, 'c' }; // "abéc"
@@ -13162,21 +13036,6 @@ test "telemetry dupDetail never yields invalid UTF-8" {
try std.testing.expect(std.mem.startsWith(u8, garbage, "ok??"));
}
-test "providerClass buckets models by capability tier" {
- try std.testing.expectEqualStrings("frontier", providerClass("claude-opus-4-8"));
- try std.testing.expectEqualStrings("frontier", providerClass("gpt-5.5"));
- try std.testing.expectEqualStrings("frontier", providerClass("deepseek-v4-pro"));
- try std.testing.expectEqualStrings("frontier", providerClass("grok-4.3"));
- try std.testing.expectEqualStrings("small", providerClass("claude-haiku-4-5"));
- try std.testing.expectEqualStrings("small", providerClass("gemini-3-flash")); // flash wins over gemini-3
- try std.testing.expectEqualStrings("mid", providerClass("claude-sonnet-4-6"));
- try std.testing.expectEqualStrings("mid", providerClass("some-unknown-model"));
- try std.testing.expectEqualStrings("frontier", providerClass("MiniMax-M3")); // minimax-m needle (case-insensitive)
- // price-table fallback (no name needle): bucket by models.dev output $/1M
- try std.testing.expectEqualStrings("mid", providerClass("grok-build")); // out=2 → mid
- try std.testing.expectEqualStrings("small", providerClass("mimo-v2.5")); // out=0.28 → small
-}
-
test "telemetry writeOtlp emits a fleet record with kind + split prov attrs" {
var t: Telemetry = .{
.io = undefined, // writeOtlp(.., include_summary=false) never touches io
@@ -13307,21 +13166,6 @@ test "scoreSigMessage: canonical bytes are cross-language stable" {
try std.testing.expectEqualStrings("v1\na1b2\n\n1.000000\n\n\n\n", empty);
}
-test "signScore: matches the Python SDK HMAC for a known key" {
- // hmac.new(b"test-key", msg, sha256) over the canonical tuple above.
- const saved = g_score_key;
- defer g_score_key = saved;
- g_score_key = "test-key";
- const sig = signScore("a1b2", "c3d4", 0.5, "run1", "replay-v1", "art", "evalhash");
- try std.testing.expectEqualStrings(
- "32023137ff08de5962f81a0d0f7229bc5c0fb26b8c3399b8af54138fb47c6138",
- &sig,
- );
- g_score_key = null;
- const unsigned = signScore("a1b2", "c3d4", 0.5, "run1", "replay-v1", "art", "evalhash");
- try std.testing.expectEqual(@as(u8, 0), unsigned[0]); // no key -> zeroed sig
-}
-
test "blankText: webfetch empty-output heuristic" {
try std.testing.expect(blankText(""));
try std.testing.expect(blankText(" \n\n\t\r\n \n")); // SPA: pages of blank lines
diff --git a/src/scoring.zig b/src/scoring.zig
new file mode 100644
index 00000000..35a93a6e
--- /dev/null
+++ b/src/scoring.zig
@@ -0,0 +1,178 @@
+//! DGM score-channel signing + prompt/model fingerprinting: the system-prompt
+//! fingerprint, the MAP-Elites provider-class capability tier, and the HMAC
+//! signing of score records (canonical message + key custody). Split out of
+//! main.zig (600-line goal). Self-contained (std + pricing.priceFor); owns the
+//! g_score_key / g_run_id session globals. main aliases the pure fns back and
+//! mod-qualifies the two globals.
+
+const std = @import("std");
+const Io = std.Io;
+const Allocator = std.mem.Allocator;
+
+const pricing = @import("pricing.zig");
+const priceFor = pricing.priceFor;
+
+/// Short stable fingerprint of a system prompt (first 8 bytes of SHA-256,
+/// hex): equal hashes along a trajectory edge mean the prompt didn't change.
+pub fn promptFingerprint(prompt: []const u8) [16]u8 {
+ var d: [32]u8 = undefined;
+ std.crypto.hash.sha2.Sha256.hash(prompt, &d, .{});
+ return std.fmt.bytesToHex(d[0..8].*, .lower);
+}
+
+/// Capability tier for the running model — the MAP-Elites grid's provider-class
+/// axis (docs/hyperagents.md §9). A curated table maps known model families to a
+/// tier (price alone is a poor capability proxy: a small model can be premium-
+/// priced and a frontier model cheap), then falls back to the models.dev output
+/// price graff already carries, then to "mid". Returns "frontier" | "mid" | "small".
+pub fn providerClass(model: []const u8) []const u8 {
+ const Tier = struct { needle: []const u8, tier: []const u8 };
+ // Most-specific first; small markers precede the families they sit in so
+ // e.g. "...-flash" beats "gemini-3" and "deepseek-v4-flash" beats "deepseek-v4".
+ const known = [_]Tier{
+ .{ .needle = "haiku", .tier = "small" },
+ .{ .needle = "flash", .tier = "small" },
+ .{ .needle = "-mini", .tier = "small" }, // -mini suffix, not "minimax"/"gemini"
+ .{ .needle = "lite", .tier = "small" },
+ .{ .needle = "nano", .tier = "small" },
+ .{ .needle = "opus", .tier = "frontier" },
+ .{ .needle = "gpt-5", .tier = "frontier" },
+ .{ .needle = "deepseek-v4", .tier = "frontier" },
+ .{ .needle = "grok-4", .tier = "frontier" },
+ .{ .needle = "glm-5", .tier = "frontier" },
+ .{ .needle = "kimi-k2", .tier = "frontier" },
+ .{ .needle = "minimax-m", .tier = "frontier" },
+ .{ .needle = "mimo-v2.5-pro", .tier = "frontier" },
+ .{ .needle = "fugu", .tier = "frontier" },
+ .{ .needle = "gemini-3", .tier = "frontier" },
+ .{ .needle = "sonnet", .tier = "mid" },
+ };
+ for (known) |k| if (std.ascii.indexOfIgnoreCase(model, k.needle) != null) return k.tier;
+ // Unknown family: bucket by models.dev output price ($/1M output tokens).
+ if (priceFor(model)) |p| {
+ if (p.out >= 10) return "frontier";
+ if (p.out >= 2) return "mid";
+ return "small";
+ }
+ return "mid";
+}
+// ── Score-channel signing (DGM fitness integrity, Step 0) ──────────────────
+//
+// The trajectory archive is the DGM's fitness ledger, and it lives in cwd —
+// so a forged `{"kind":"score",...}` row appended via write_file (cwd is
+// inside confinedPath) or `echo >>` manufactures fitness AND parent_sha
+// lineage that parent selection then rewards. Signing makes forged rows
+// detectable: every score record the harness writes carries an HMAC over its
+// fields, keyed by a secret the evolving agent's tools can't reach.
+//
+// Key custody: GRAFF_SCORE_KEY_FILE names a key file OUTSIDE cwd. The env var
+// holds only the path (not the secret), and the file is unreadable by a
+// subagent — file tools are confined to cwd, and subagent bash is cwd-locked
+// with no redirection. The root agent / --yolo is the trusted orchestrator
+// boundary (it could read any file anyway). Readers (dgm_loop.py load_archive,
+// the harness-telemetry worker) recompute the HMAC and reject unsigned rows.
+
+/// Loaded once in main() from GRAFF_SCORE_KEY_FILE; null → unsigned (signing
+/// off, every reader accepts rows as before — backward compatible).
+pub var g_score_key: ?[]const u8 = null;
+
+/// Per-session run id (random hex), the run_id field of signed score records.
+pub var g_run_id: [16]u8 = @splat('0');
+
+/// Canonical signing message — newline-delimited, version-prefixed, with the
+/// score formatted to a fixed 6 decimals so Zig/Python/JS agree byte-for-byte.
+/// `buf` must hold the message; returns the slice written.
+pub fn scoreSigMessage(
+ buf: []u8,
+ prompt_sha: []const u8,
+ parent_sha: []const u8,
+ score: f64,
+ run_id: []const u8,
+ judge_id: []const u8,
+ artifact_sha: []const u8,
+ eval_set_hash: []const u8,
+) ?[]const u8 {
+ return std.fmt.bufPrint(buf, "v1\n{s}\n{s}\n{d:.6}\n{s}\n{s}\n{s}\n{s}", .{
+ prompt_sha, parent_sha, score, run_id, judge_id, artifact_sha, eval_set_hash,
+ }) catch null;
+}
+
+/// HMAC-SHA256 of the canonical message, hex. "" when no key is configured.
+pub fn signScore(
+ prompt_sha: []const u8,
+ parent_sha: []const u8,
+ score: f64,
+ run_id: []const u8,
+ judge_id: []const u8,
+ artifact_sha: []const u8,
+ eval_set_hash: []const u8,
+) [64]u8 {
+ const key = g_score_key orelse return @splat(0);
+ var msgbuf: [512]u8 = undefined;
+ const msg = scoreSigMessage(&msgbuf, prompt_sha, parent_sha, score, run_id, judge_id, artifact_sha, eval_set_hash) orelse return @splat(0);
+ var mac: [std.crypto.auth.hmac.sha2.HmacSha256.mac_length]u8 = undefined;
+ std.crypto.auth.hmac.sha2.HmacSha256.create(&mac, msg, key);
+ return std.fmt.bytesToHex(mac, .lower);
+}
+
+/// Read the signing key from GRAFF_SCORE_KEY_FILE (a path outside cwd) into
+/// `arena`. Whitespace-trimmed; null when unset, unreadable, or empty.
+pub fn loadScoreKey(io: Io, arena: Allocator, environ: anytype) ?[]const u8 {
+ const path = environ.get("GRAFF_SCORE_KEY_FILE") orelse return null;
+ const data = Io.Dir.cwd().readFileAlloc(io, path, arena, .limited(64 * 1024)) catch return null;
+ const trimmed = std.mem.trim(u8, data, " \t\r\n");
+ return if (trimmed.len == 0) null else trimmed;
+}
+test "promptFingerprint is stable, short, and collision-visible" {
+ const a = promptFingerprint("you are a careful reviewer");
+ const b = promptFingerprint("you are a careful reviewer");
+ const c = promptFingerprint("you are a careless reviewer");
+ try std.testing.expectEqualStrings(&a, &b);
+ try std.testing.expect(!std.mem.eql(u8, &a, &c));
+}
+test "score signing message is canonical and cross-language stable" {
+ // This exact byte layout is the contract the Python/JS verifiers
+ // reimplement — a change here breaks signature verification, so it is
+ // pinned. Score is fixed to 6 decimals; fields newline-joined; "v1" tag.
+ var buf: [512]u8 = undefined;
+ const msg = scoreSigMessage(&buf, "aaaaaaaaaaaaaaaa", "", 0.5, "run123", "j1", "", "").?;
+ try std.testing.expectEqualStrings("v1\naaaaaaaaaaaaaaaa\n\n0.500000\nrun123\nj1\n\n", msg);
+
+ // With a key, signScore is a stable, key-dependent HMAC; without one
+ // (default global) it is all-zero hex and callers omit it.
+ g_score_key = "testkey";
+ defer g_score_key = null;
+ const sig = signScore("aaaaaaaaaaaaaaaa", "", 0.5, "run123", "j1", "", "");
+ const sig2 = signScore("aaaaaaaaaaaaaaaa", "", 0.5, "run123", "j1", "", "");
+ try std.testing.expectEqualStrings(&sig, &sig2); // deterministic
+ const sig_changed = signScore("aaaaaaaaaaaaaaaa", "", 0.6, "run123", "j1", "", "");
+ try std.testing.expect(!std.mem.eql(u8, &sig, &sig_changed)); // score is signed
+}
+test "providerClass buckets models by capability tier" {
+ try std.testing.expectEqualStrings("frontier", providerClass("claude-opus-4-8"));
+ try std.testing.expectEqualStrings("frontier", providerClass("gpt-5.5"));
+ try std.testing.expectEqualStrings("frontier", providerClass("deepseek-v4-pro"));
+ try std.testing.expectEqualStrings("frontier", providerClass("grok-4.3"));
+ try std.testing.expectEqualStrings("small", providerClass("claude-haiku-4-5"));
+ try std.testing.expectEqualStrings("small", providerClass("gemini-3-flash")); // flash wins over gemini-3
+ try std.testing.expectEqualStrings("mid", providerClass("claude-sonnet-4-6"));
+ try std.testing.expectEqualStrings("mid", providerClass("some-unknown-model"));
+ try std.testing.expectEqualStrings("frontier", providerClass("MiniMax-M3")); // minimax-m needle (case-insensitive)
+ // price-table fallback (no name needle): bucket by models.dev output $/1M
+ try std.testing.expectEqualStrings("mid", providerClass("grok-build")); // out=2 → mid
+ try std.testing.expectEqualStrings("small", providerClass("mimo-v2.5")); // out=0.28 → small
+}
+test "signScore: matches the Python SDK HMAC for a known key" {
+ // hmac.new(b"test-key", msg, sha256) over the canonical tuple above.
+ const saved = g_score_key;
+ defer g_score_key = saved;
+ g_score_key = "test-key";
+ const sig = signScore("a1b2", "c3d4", 0.5, "run1", "replay-v1", "art", "evalhash");
+ try std.testing.expectEqualStrings(
+ "32023137ff08de5962f81a0d0f7229bc5c0fb26b8c3399b8af54138fb47c6138",
+ &sig,
+ );
+ g_score_key = null;
+ const unsigned = signScore("a1b2", "c3d4", 0.5, "run1", "replay-v1", "art", "evalhash");
+ try std.testing.expectEqual(@as(u8, 0), unsigned[0]); // no key -> zeroed sig
+}