graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
//! Provider-switch core: resolve a /model or set_model request to a
//! Provider, apply it to the running Agent (translating or clearing history
//! across a wire-format change), and announce the switch. Split
//! out of main.zig. Interactive pickers live in pickers.zig. Back-imports
//! main (as main_mod, since several params are named `root`) for Agent,
//! Keys, Provider, provider_specs, and extractText.
const std = @import("std");
const Io = std.Io;
const Allocator = std.mem.Allocator;
const Value = std.json.Value;
const pricing = @import("pricing.zig");
const resolveModelName = pricing.resolveModelName;
const kimi_catalog = @import("kimi_catalog.zig");
const catalog_selection = @import("catalog_selection.zig");
const router_catalog = @import("router_catalog.zig");
const serde = @import("serde.zig");
const saveModel = serde.saveModel;
const messages_mod = @import("messages.zig");
const engine_sink = @import("engine_sink.zig"); // #429: the failover notice is a typed event, not a print
const fallback_config = @import("fallback_config.zig");
const util = @import("util.zig");
const trace = @import("trace.zig");
const orch_rows = @import("orchestration_rows.zig");
fn localProviderUrl(url: []const u8) bool {
return std.mem.startsWith(u8, url, "http://127.0.0.1") or std.mem.startsWith(u8, url, "http://localhost") or std.mem.startsWith(u8, url, "http://[::1]");
}
const main_mod = @import("main.zig");
const agent_mod = @import("agent.zig");
const provider_mod = @import("provider.zig");
const Agent = agent_mod.Agent;
const Keys = provider_mod.Keys;
const Provider = provider_mod.Provider;
const provider_specs = provider_mod.provider_specs;
/// Pull plain text out of a message's content (string, or the text blocks of a
/// content array — anthropic "text", openai "text", responses "input/output_text").
pub fn extractText(arena: Allocator, m: Value) []const u8 {
if (m != .object) return "";
const c = m.object.get("content") orelse return "";
if (c == .string) return c.string;
if (c != .array) return "";
var b: std.ArrayList(u8) = .empty;
for (c.array.items) |blk| {
if (blk != .object) continue;
if (blk.object.get("text")) |t| if (t == .string) {
if (b.items.len > 0) b.append(arena, '\n') catch {};
b.appendSlice(arena, t.string) catch {};
};
}
return b.items;
}
pub fn applyProviderInner(root: *Agent, arena: Allocator, p: Provider, persist: bool) ![]const u8 {
const same_format = root.provider.kind == p.kind;
const same_selection = same_format and
root.provider.context == p.context and
std.mem.eql(u8, root.provider.id, p.id) and
std.mem.eql(u8, root.provider.model, p.model) and
std.mem.eql(u8, root.provider.url, p.url);
var note: []const u8 = "context kept";
if (!same_format) {
if (root.keep_context) {
@import("history_translate.zig").translateHistory(arena, &root.messages, p.kind);
note = "context translated & kept";
} else {
root.messages = std.json.Array.init(arena);
root.last_context_tokens = 0;
root.context_local_tokens = 0;
note = "history cleared — /keepcontext on to carry it across formats";
}
}
// A compaction blob decrypts only on the seat that minted it, so a switch
// must not hand it to a different provider or model. Expand it back into
// real items from the exact checkpoint first; without one the request is
// rejected and the bounded recovery path reports the blocked state.
const seat_moved = !same_format or
!std.mem.eql(u8, root.provider.id, p.id) or
!std.mem.eql(u8, root.provider.model, p.model);
if (seat_moved and @import("agent_encrypted_recovery.zig").expandForSeatChange(root))
note = try std.fmt.allocPrint(arena, "{s}; opaque state expanded from checkpoint", .{note});
// ADR 0256: attempt_completion's description follows the model family.
const gemini = @import("gemini_turns.zig");
if (gemini.family(root.provider.model) != gemini.family(p.model)) root.invalidateRootTools();
@import("jev_tool.zig").updateProvider(root, p);
if (root.reasoning == .none and !@import("effort_route.zig").mimoRoute(p.id, p.model)) {
root.reasoning = .medium;
if (persist) _ = @import("repl_glue.zig").saveThinkingSettings(root.io, root.gpa, root.reasoning, root.fast, root.ultracode_mode, root.show_thinking, root.ai_title);
note = try std.fmt.allocPrint(arena, "{s}; reasoning reset to Medium (Off unsupported by this model)", .{note});
}
// #371: only a derived worker default follows a model switch; explicit pins remain.
if (!root.subagent_provider_explicit) if (@import("bench_priors.zig").g_keys) |k| {
root.subagent_provider = @import("subagent_selection.zig").resolveSubagentProvider(k.*, p, null, null, false, false);
};
// Keep the context estimate and first request exact without eagerly
// materializing formats this session has never used.
try root.ensureRootTools(p.kind);
// #204: an actual provider/model switch changes the window and tokenizer, so
// re-estimate until the next response returns real usage. A no-op set_model is
// common in the GUI prompt-settings path; preserve its authoritative server
// meter instead of replacing it with the structurally-low local estimate.
if (!same_selection) {
root.cap_new = false; // per-provider token-cap quirk; relearn on rejection
root.sox_json_object = false; // #543: json_schema support is per-provider; relearn
root.effort_rejected = false; // new model may accept reasoning_effort; relearn
root.ws_off = false; // a previous Codex WS fallback must not leak across switches
root.ws_transport_failures = 0;
root.compact_transport_failures = 0;
root.last_context_tokens = root.fullRequestEstimateTokens();
root.context_local_tokens = root.last_context_tokens;
root.last_cache_read = 0;
}
root.fallback_active = !persist;
root.fallback_blocked = false;
if (persist) saveModel(root.io, root.home, p.id, p.model);
return note;
}
/// An explicit /model or set_model selection becomes the next-launch default.
pub fn applyProvider(root: *Agent, arena: Allocator, p: Provider) ![]const u8 {
return applyProviderInner(root, arena, p, true);
}
/// Automatic failover is session-local: preserve the user's saved preference
/// so a repaired credential/model is tried again on the next fresh launch.
pub fn applyFallbackProvider(root: *Agent, arena: Allocator, p: Provider) ![]const u8 {
return applyProviderInner(root, arena, p, false);
}
fn triedProvider(ids: []const []const u8, id: []const u8) bool {
for (ids) |candidate| if (std.mem.eql(u8, candidate, id)) return true;
return false;
}
/// Walk configured providers in stable priority order, beginning after the
/// failed provider and wrapping once. Providers without credentials and ids
/// already attempted during this turn are skipped.
pub fn nextFallbackProvider(keys: Keys, after_id: []const u8, tried: []const []const u8, allow: []const []const u8) ?Provider {
const count = provider_mod.specCount();
var start: usize = 0;
for (0..count) |i| if (std.mem.eql(u8, provider_mod.specAt(i).?.id, after_id)) {
start = (i + 1) % count;
break;
};
for (0..count) |offset| {
const spec = provider_mod.specAt((start + offset) % count).?;
const key = keys.get(spec.id) orelse continue;
if (triedProvider(tried, spec.id)) continue;
if (!fallback_config.contains(allow, spec.id)) continue;
return keys.build(spec, key, pricing.providerDefaultModel(spec.id, spec.default_model));
}
return null;
}
/// Only failures that clearly mean "this credential/model cannot serve the
/// request" are safe to fail over. Network flakes, rate limits, malformed
/// replies, context overflow, and interrupts stay on the selected provider.
/// #294: the auth/model needles are universal, but the billing ones only
/// qualify when the codegraff gateway raised them. A billing error from a
/// provider the user keyed themselves is about their own account, and treating
/// it as failover-eligible silently swapped their chosen model for some other
/// provider's default.
pub fn failoverEligible(provider_id: []const u8, detail: []const u8) bool {
const needles = [_][]const u8{
"unauthorized",
"authentication_error",
"authentication token",
"authentication failed",
"sign-in was rejected", // auth_recovery.failureText (ADR 0310)
"api key was rejected",
"console sign-in has expired",
"invalid api key",
"invalid_api_key",
"invalid x-api-key",
"incorrect api key",
"api key not valid",
"invalid token",
"token expired",
"expired token",
"access token has expired",
"401",
"403",
"forbidden",
"access denied",
"permission denied",
"permission_denied",
"model_not_found",
"model not found",
"model is not available",
"model unavailable",
"model does not exist",
"does not exist or you do not have access",
"you do not have access to model",
"you do not have access to this model",
};
for (needles) |needle| if (util.indexOfIgnoreCase(detail, needle) != null) return true;
if (!std.mem.eql(u8, provider_id, "codegraff")) return false;
const gateway_billing = [_][]const u8{
@import("agent_request_quota.zig").quota_cap_marker,
"insufficient_quota",
"quota exceeded",
"credits exhausted",
"insufficient credits",
"credit balance",
"no credits",
"billing limit",
};
for (gateway_billing) |needle| if (util.indexOfIgnoreCase(detail, needle) != null) return true;
return false;
}
/// Run a root turn and, before any text/tools have escaped, rotate through
/// credential-backed providers on a clear auth/model/quota failure. Successful
/// fallback becomes active for this session but does not replace the saved
/// model preference.
pub fn runTurnWithFallback(root: *Agent, keys: *Keys, arena: Allocator, out: ?*Io.Writer) anyerror![]const u8 {
if (!root.sub) @import("presence.zig").noteActivity(root.io, arena, true);
defer if (!root.sub) @import("presence.zig").noteActivity(root.io, arena, false);
const behavior_turn = trace.beginRootTurn(root.tracer);
defer trace.endRootTurn(root.tracer, behavior_turn);
if (root.fallback_blocked) return error.FallbackConsentRequired;
var attempted: [provider_specs.len + 1][]const u8 = undefined;
var attempted_len: usize = 1;
attempted[0] = root.provider.id;
while (true) {
root.last_api_error = null;
root.last_api_reauth = null;
const result = root.runTurn();
if (result) |text| {
// #255: no clean per-segment choke point exists across the
// provider streaming paths, so the root turn's final text is
// recorded once here (opt-in rich capture only; no-op otherwise).
if (root.tracer) |tr| tr.textDelta(text);
// The landed-turn funnel: a run with no eval loop files its parked
// orchestration outcome here, or the learned policy never sees an
// ordinary success — completing the turn within budget IS the
// outcome it optimizes. With an eval configured, agent_eval owns
// the pending row and files the real score instead.
if (root.eval_cmd == null)
orch_rows.flushPending(1.0, if (root.run_budget) |b| b.used() else 0, 0, false, "");
return text;
} else |err| {
// A Codex-shaped review is an isolated one-shot, not a provider switch for its parent.
if (root.review_mode) return err;
const account_failure = err == error.ProviderQuotaExceeded or err == error.ProviderAuthenticationFailed or err == error.ProviderAccessDenied;
if ((err != error.ApiError and !account_failure) or root.partial_text.items.len != 0 or root.tool_calls_this_turn != 0)
return err;
const detail = root.last_api_error orelse return err;
if (!failoverEligible(root.provider.id, detail)) return err;
const failed_id = root.provider.id;
const failed_model = root.provider.model;
root.ensureStoredKeys(keys);
// The failure path is already paying for exhaustive credentials;
// hydrate deferred account catalogs before choosing a fallback.
ensureModelQueryCatalogs(root, keys.*, "");
const fallback = nextFallbackProvider(keys.*, failed_id, attempted[0..attempted_len], root.fallback_allow) orelse return err;
attempted[attempted_len] = fallback.id;
attempted_len += 1;
const note = try applyFallbackProvider(root, arena, fallback);
// One moment, three audiences (#422): the --json wire's `model`
// event, the terminal's warning, and — for a run whose stdout is
// reserved (`-p`, `acp`) — stderr, because a silent model swap is
// a cost and consent surprise. The sink picks; `out` is the
// caller's frontend, which is not always `root.out`.
engine_sink.forSession(root, out).emit(root.io, .{ .provider_fallback = .{
.from_provider = failed_id,
.from_model = failed_model,
.to_provider = fallback.id,
.to_model = fallback.model,
.to_context = fallback.context,
.context_note = note,
} });
if (root.tracer) |tr| {
const trace_note = std.fmt.allocPrint(arena, "{s}/{s} -> {s}/{s}", .{ failed_id, failed_model, fallback.id, fallback.model }) catch "automatic provider fallback";
tr.note("model_fallback", trace_note);
}
}
}
}
pub fn resolveProviderControlRequest(
keys: *Keys,
arena: Allocator,
provider_query: []const u8,
model_query: []const u8,
legacy_name: []const u8,
) !Provider {
const provider_id = std.mem.trim(u8, provider_query, " \t");
const model = std.mem.trim(u8, model_query, " \t");
if (provider_id.len != 0) {
if (provider_mod.specFor(provider_id)) |spec| {
const selected_model = if (model.len == 0) pricing.providerDefaultModel(spec.id, spec.default_model) else try arena.dupe(u8, model);
if (model.len != 0 and !localProviderUrl(spec.url) and !catalog_selection.acceptsExplicitModel(spec, selected_model)) return error.InvalidModel;
return keys.providerById(spec.id, selected_model);
}
return error.InvalidProvider;
}
if (model.len != 0) {
const resolved = resolveModelName(keys.*, model) orelse return error.InvalidModel;
const name = try arena.dupe(u8, resolved);
return keys.providerFor(name);
}
return resolveProviderRequest(keys, arena, legacy_name);
}
/// Whether a human `/model` query can observe account-scoped Codex rows.
/// Explicit non-Codex providers never need the dynamic catalog; bare/fuzzy
/// model queries do because they may name a new account rollout.
pub fn modelQueryMayUseCodex(query: []const u8) bool {
return catalog_selection.queryMayUse("codex", query);
}
fn modelQueryMayUseKimi(query: []const u8) bool {
return catalog_selection.queryMayUse("kimi", query);
}
pub fn ensureModelQueryCatalogs(root: *Agent, keys: Keys, query: []const u8) void {
if (modelQueryMayUseCodex(query)) root.ensureModelCatalog(keys);
if (modelQueryMayUseKimi(query))
kimi_catalog.ensure(root.io, root.gpa, root.arena, keys.get("kimi") orelse "");
router_catalog.ensureForQuery(root.io, root.gpa, root.arena, root.home, keys, query);
}
/// The /models listing's live refresh: the explicit ping is the freshness
/// moment, so the router-catalog providers (codegraff/anthropic/fireworks/
/// workspace router) are re-fetched NOW — concurrently, cache as offline
/// fallback — instead of showing the boot-time snapshot until restart.
/// Codex and Kimi keep their bespoke caches; /models prints their sources.
pub fn refreshModelListing(root: *Agent, keys: Keys) usize {
return router_catalog.refreshForListing(root.io, root.gpa, root.arena, root.home, keys);
}
/// Structured set_model has separate provider/model fields. An explicit
/// provider fully determines routing; a model-only request remains fuzzy.
pub fn controlRequestMayUseCodex(provider_query: []const u8, model_query: []const u8, legacy_name: []const u8) bool {
return modelQueryMayUseCodex(catalog_selection.controlQuery(provider_query, model_query, legacy_name));
}
pub fn ensureControlRequestCatalogs(root: *Agent, keys: Keys, provider_query: []const u8, model_query: []const u8, legacy_name: []const u8) void {
if (controlRequestMayUseCodex(provider_query, model_query, legacy_name)) root.ensureModelCatalog(keys);
const query = catalog_selection.controlQuery(provider_query, model_query, legacy_name);
if (modelQueryMayUseKimi(query))
kimi_catalog.ensure(root.io, root.gpa, root.arena, keys.get("kimi") orelse "");
router_catalog.ensureForQuery(root.io, root.gpa, root.arena, root.home, keys, query);
}
fn resolveProviderRequest(keys: *Keys, arena: Allocator, query: []const u8) !Provider {
const arg = std.mem.trim(u8, query, " \t");
if (arg.len == 0) return error.InvalidModelRequest;
if (std.mem.indexOfAny(u8, arg, " /\t")) |i| {
const pid = arg[0..i];
const mdl = std.mem.trim(u8, arg[i + 1 ..], " \t");
if (provider_mod.specFor(pid)) |spec| {
if (mdl.len == 0) return error.InvalidModel;
if (!localProviderUrl(spec.url) and !catalog_selection.acceptsExplicitModel(spec, mdl)) return error.InvalidModel;
const m = try arena.dupe(u8, mdl);
return keys.providerById(pid, m);
}
}
if (provider_mod.specFor(arg)) |spec| {
return keys.providerById(spec.id, pricing.providerDefaultModel(spec.id, spec.default_model));
}
const resolved = resolveModelName(keys.*, arg) orelse return error.InvalidModel;
const name = try arena.dupe(u8, resolved);
return keys.providerFor(name);
}
/// Switch the active provider/model. Within the same wire format
/// (provider.kind) the conversation is kept verbatim. Across formats
/// (OpenAI↔Anthropic↔Responses) the stored messages don't fit the new shape:
/// with keep_context on (default) the dialogue is translated to a text-only
/// history and carried over; off clears it.
pub fn setModelRequestLabel(arena: Allocator, provider_query: []const u8, model_query: []const u8, legacy_name: []const u8) ![]const u8 {
const provider_id = std.mem.trim(u8, provider_query, " \t");
const model = std.mem.trim(u8, model_query, " \t");
if (provider_id.len != 0 and model.len != 0) return std.fmt.allocPrint(arena, "{s} {s}", .{ provider_id, model });
if (provider_id.len != 0) return arena.dupe(u8, provider_id);
if (model.len != 0) return arena.dupe(u8, model);
return arena.dupe(u8, std.mem.trim(u8, legacy_name, " \t"));
}
pub fn switchProvider(root: *Agent, arena: Allocator, p: Provider, out: *Io.Writer) !void {
const same = std.mem.eql(u8, root.provider.id, p.id) and std.mem.eql(u8, root.provider.model, p.model);
const note = try applyProvider(root, arena, p);
try out.print("{s} {s} via {s} ({t} format, {d}k ctx) — {s} · saved for next session\n", .{
if (same) "already using" else "switched to", p.model, p.id, p.kind, p.context / 1000, note,
});
try out.flush();
}
// Tests moved from main.zig alongside the functions they cover (#123 split).
test {
_ = @import("providers_behavior_tests.zig");
}
test "failoverEligible: accepts unavailable credentials/models, rejects transient or prompt errors" {
try std.testing.expect(failoverEligible("codex", "codex api error: Unauthorized"));
try std.testing.expect(failoverEligible("codex", "access token has expired"));
try std.testing.expect(failoverEligible("codex", "model_not_found: rollout ended"));
try std.testing.expect(failoverEligible("kimi", "Kimi sign-in was rejected: sign in again (graff login kimi)"));
try std.testing.expect(failoverEligible("anthropic", "Anthropic plan is out of usage, and the fallback API key was rejected (Console profile): wait"));
try std.testing.expect(failoverEligible("anthropic", "Anthropic Console sign-in has expired: run `graff login console` again. Your sign-in is not affected."));
try std.testing.expect(failoverEligible("codegraff", "insufficient_quota"));
try std.testing.expect(!failoverEligible("codex", "network error: HttpConnectionClosing"));
try std.testing.expect(!failoverEligible("codex", "rate limited (429)"));
try std.testing.expect(!failoverEligible("codex", "maximum context length exceeded"));
try std.testing.expect(!failoverEligible("codex", "unparseable response"));
}
test "failoverEligible: billing needles only fire for the gateway (#294)" {
// The same detail string decides differently depending on who raised it:
// a low balance on the gateway is a reason to move, the user's own
// anthropic/codex billing is not.
const billing = [_][]const u8{
"your credit balance is too low",
"insufficient credits",
"credits exhausted",
"no credits remaining",
"billing limit reached",
"quota exceeded for this account",
"insufficient_quota",
"provider quota/billing cap: credit or allowance exhausted; retry requires account action",
};
for (billing) |detail| {
try std.testing.expect(failoverEligible("codegraff", detail));
try std.testing.expect(!failoverEligible("anthropic", detail));
try std.testing.expect(!failoverEligible("codex", detail));
try std.testing.expect(!failoverEligible("kimi", detail));
}
// Auth and model failures stay provider-agnostic.
try std.testing.expect(failoverEligible("anthropic", "invalid x-api-key"));
try std.testing.expect(failoverEligible("kimi", "model does not exist"));
try std.testing.expect(failoverEligible("codegraff", "unauthorized"));
}
test "nextFallbackProvider: rotates after the failed provider and skips missing or tried credentials" {
const all = Keys{ .values = @splat("k") };
const tried_codex = [_][]const u8{"codex"};
const allow_all = blk: {
var ids: [provider_specs.len][]const u8 = undefined;
for (provider_specs, 0..) |spec, i| ids[i] = spec.id;
break :blk ids;
};
const last = [_][]const u8{provider_specs[provider_specs.len - 1].id}; // rotation wraps past the table end
try std.testing.expectEqualStrings(provider_specs[0].id, nextFallbackProvider(all, last[0], &last, &allow_all).?.id);
var values: [provider_specs.len]?[]const u8 = @splat(null);
for (provider_specs, 0..) |spec, i| {
if (std.mem.eql(u8, spec.id, "codegraff") or std.mem.eql(u8, spec.id, "openai")) values[i] = "k";
}
const sparse = Keys{ .values = values };
const tried = [_][]const u8{ "codex", "codegraff" };
const next = nextFallbackProvider(sparse, "codex", &tried, &allow_all).?;
try std.testing.expectEqualStrings("openai", next.id);
const exhausted = [_][]const u8{ "codex", "codegraff", "openai" };
try std.testing.expect(nextFallbackProvider(sparse, "openai", &exhausted, &allow_all) == null);
const allow_codegraff = [_][]const u8{"codegraff"};
try std.testing.expectEqualStrings("codegraff", nextFallbackProvider(sparse, "codex", &tried_codex, &allow_codegraff).?.id);
}
test "applyProviderInner preserves the server meter on an exact model re-selection" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const a = arena_state.allocator();
const p: Provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "gpt-5", .context = 270_000 };
var root: Agent = undefined;
root.io = std.testing.io;
root.jev_effort_pending = .{};
root.provider = p;
root.subagent_provider = null;
root.subagent_provider_explicit = true; // this test is about the meter, not #371 re-derivation
root.arena = a;
root.registry = null;
root.messages = std.json.Array.init(a);
root.sub = false;
root.strict = false;
root.sys_normal = "";
root.sys_strict = "";
root.tools_anthropic = "";
root.tools_openai = "";
root.tools_responses = "";
root.keep_context = true;
root.last_context_tokens = 220_000;
root.context_local_tokens = root.fullRequestEstimateTokens();
root.last_cache_read = 12_345;
root.cap_new = true;
root.sox_json_object = true;
root.effort_rejected = true;
root.ws_off = true;
root.ws_transport_failures = 2;
_ = try applyProviderInner(&root, a, p, false);
try std.testing.expect(root.tools_responses.len > 0);
try std.testing.expectEqual(@as(usize, 0), root.tools_anthropic.len);
try std.testing.expectEqual(@as(usize, 0), root.tools_openai.len);
try std.testing.expectEqual(@as(u64, 220_000), root.last_context_tokens);
try std.testing.expectEqual(@as(u64, 12_345), root.last_cache_read);
try std.testing.expect(root.cap_new);
try std.testing.expect(root.sox_json_object);
try std.testing.expect(root.effort_rejected);
try std.testing.expect(root.ws_off);
var changed = p;
changed.model = "gpt-5.1";
_ = try applyProviderInner(&root, a, changed, false);
try std.testing.expectEqual(root.fullRequestEstimateTokens(), root.last_context_tokens);
try std.testing.expectEqual(root.last_context_tokens, root.context_local_tokens);
try std.testing.expectEqual(@as(u64, 0), root.last_cache_read);
try std.testing.expect(!root.cap_new);
try std.testing.expect(!root.sox_json_object); // #543: relearned per provider
try std.testing.expect(!root.effort_rejected);
try std.testing.expect(!root.ws_off);
try std.testing.expectEqual(@as(u8, 0), root.ws_transport_failures);
var changed_format = changed;
changed_format.id = "deepseek";
changed_format.kind = .openai;
changed_format.model = "deepseek-chat";
_ = try applyProviderInner(&root, a, changed_format, false);
try std.testing.expect(root.tools_openai.len > 0);
try std.testing.expect(root.tools_responses.len > 0); // already paid for, remains cached
try std.testing.expectEqual(@as(usize, 0), root.tools_anthropic.len);
}
test "set_model control resolves explicit provider/model fields" {
var all = Keys{ .values = @splat("k") };
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const codex = try resolveProviderControlRequest(&all, arena, "codex", "gpt-5.5", "");
try std.testing.expectEqualStrings("codex", codex.id);
try std.testing.expectEqualStrings("gpt-5.5", codex.model);
const legacy = try resolveProviderControlRequest(&all, arena, "", "", "codegraff gpt-5.5");
try std.testing.expectEqualStrings("codegraff", legacy.id);
try std.testing.expectEqualStrings("gpt-5.5", legacy.model);
try std.testing.expectError(error.InvalidModel, resolveProviderControlRequest(&all, arena, "", "definitely-not-a-model", ""));
try std.testing.expectError(error.InvalidModel, resolveProviderControlRequest(&all, arena, "openai", "definitely-not-a-model", ""));
const local = try resolveProviderControlRequest(&all, arena, "lmstudio", "user-loaded/model", "");
try std.testing.expectEqualStrings("user-loaded/model", local.model);
}
test "Codex catalog demand follows model request routing" {
try std.testing.expect(modelQueryMayUseCodex(""));
try std.testing.expect(!modelQueryMayUseCodex("deepseek"));
try std.testing.expect(!modelQueryMayUseCodex("deepseek/deepseek-chat"));
try std.testing.expect(modelQueryMayUseCodex("codex"));
try std.testing.expect(modelQueryMayUseCodex("codex future-sol"));
try std.testing.expect(modelQueryMayUseCodex("future-account-rollout"));
try std.testing.expect(modelQueryMayUseKimi(""));
try std.testing.expect(!modelQueryMayUseKimi("codegraff"));
try std.testing.expect(modelQueryMayUseKimi("kimi"));
try std.testing.expect(modelQueryMayUseKimi("k3"));
try std.testing.expect(!controlRequestMayUseCodex("codegraff", "deepseek-v4-pro", ""));
try std.testing.expect(controlRequestMayUseCodex("codex", "", ""));
try std.testing.expect(controlRequestMayUseCodex("", "future-account-rollout", ""));
try std.testing.expect(!controlRequestMayUseCodex("", "", "openai"));
}
test "extractText: string content, joined text blocks, and empties" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const a = arena_state.allocator();
const plain = std.json.parseFromSliceLeaky(Value, a, "{\"content\":\"plain\"}", .{}) catch unreachable;
try std.testing.expectEqualStrings("plain", extractText(a, plain));
const blocks = std.json.parseFromSliceLeaky(Value, a, "{\"content\":[{\"type\":\"text\",\"text\":\"a\"},{\"type\":\"text\",\"text\":\"b\"}]}", .{}) catch unreachable;
try std.testing.expectEqualStrings("a\nb", extractText(a, blocks));
const empty = std.json.parseFromSliceLeaky(Value, a, "{}", .{}) catch unreachable;
try std.testing.expectEqualStrings("", extractText(a, empty));
try std.testing.expectEqualStrings("", extractText(a, Value{ .null = {} }));
}