Skip to content

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

Python

Add native Jev judgments for logged-in GPT-6 and MiMo sessions

justrach9adf21f872f3
diff --git a/docs/experiments/jev-use-harness.md b/docs/experiments/jev-use-harness.md
new file mode 100644
index 00000000..e80e800a
--- /dev/null
+++ b/docs/experiments/jev-use-harness.md
@@ -0,0 +1,23 @@
+# Native Jev judgment tool (experiment)
+
+Graff offers `jev_judge` as a **native tool**, not an MCP server, when the
+active model is a GPT-6-series model on Codex/OpenAI or a Xiaomi MiMo model
+(including matching Codegraff gateway aliases), **and** the user has a
+persisted Codegraff login from `graff login`. A Codex login or
+`CODEGRAFF_API_KEY` alone does not satisfy that gate. Graff checks the
+recognized local login store; it does not probe the account online. Direct
+dispatch enforces the same rules. Model switches and an in-session
+`/login codegraff` refresh the root tool catalog.
+
+Set `TYPESAFE_API_KEY` in Graff's environment to opt in as well. The tool sends only
+its explicitly supplied short `state` and one typed `question` to TypeSafe's
+`/v1/systemone` endpoint; it never automatically uploads repository context.
+Do not put code, paths, secrets, or customer data in a tool call. Use
+`JEV_BACKEND=mock` for a no-network wiring check.
+
+Jev answers `noul` (yes/no probability), `choice`, or ordered `score`
+questions. Low-confidence answers tell the main model to decide. Any failed
+Jev request or malformed Jev response opens a one-strike, process-session
+circuit: later calls skip the network and return control to the main model.
+The tool disappears from the root catalog after that first failure. There is
+no retry, automatic routing, MCP permission, or claim of a measured speedup.
diff --git a/src/agent_catalog.zig b/src/agent_catalog.zig
index 1edb241c..bab6d80f 100644
--- a/src/agent_catalog.zig
+++ b/src/agent_catalog.zig
@@ -11,15 +11,18 @@ const Provider = @import("provider.zig").Provider;
const Agent = @import("agent.zig").Agent;
const local_tools = @import("local_tools.zig");
const schedule = @import("schedule.zig");
+const jev_tool = @import("jev_tool.zig");
-fn withExtras(arena: Allocator, base: []const schema.ToolSpec) ![]const schema.ToolSpec {
+fn withExtras(arena: Allocator, base: []const schema.ToolSpec, provider: Provider) ![]const schema.ToolSpec {
const a = local_tools.catalogExtras(arena);
const b = schedule.catalogExtras(arena);
- if (a.len == 0 and b.len == 0) return base;
- const out = try arena.alloc(schema.ToolSpec, base.len + a.len + b.len);
+ const c = jev_tool.catalogExtras(provider);
+ if (a.len == 0 and b.len == 0 and c.len == 0) return base;
+ const out = try arena.alloc(schema.ToolSpec, base.len + a.len + b.len + c.len);
@memcpy(out[0..base.len], base);
@memcpy(out[base.len..][0..a.len], a);
- @memcpy(out[base.len + a.len ..], b);
+ @memcpy(out[base.len + a.len ..][0..b.len], b);
+ @memcpy(out[base.len + a.len + b.len ..], c);
return out;
}
@@ -63,7 +66,7 @@ pub fn ensureRootTools(self: *Agent, kind: Provider.Kind) !void {
const specs = if (self.sub)
try surface.filterSpecs(@TypeOf(schema.base_specs[0]), self.arena, schema.base_specs[0..])
else
- try withExtras(self.arena, try schema.effectiveRootSpecs(self.arena));
+ try withExtras(self.arena, try schema.effectiveRootSpecs(self.arena), self.provider);
const connected: []const mcp.Tool = if (self.registry) |registry|
(if (self.sub) try surface.filterWorkerMcp(self.arena, registry.tools) else registry.tools)
else
diff --git a/src/agent_tests.zig b/src/agent_tests.zig
index 51dc0bec..3fb48d98 100644
--- a/src/agent_tests.zig
+++ b/src/agent_tests.zig
@@ -62,6 +62,7 @@ pub fn lazyRootTools(comptime Agent: type) !void {
var agent: Agent = undefined;
agent.arena = arena;
agent.sub = false;
+ agent.provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "gpt-6-sol", .context = 100_000 };
agent.registry = &registry;
agent.tools_anthropic = "";
agent.tools_openai = "";
diff --git a/src/agent_tools.zig b/src/agent_tools.zig
index f72a7d14..fa3d0012 100644
--- a/src/agent_tools.zig
+++ b/src/agent_tools.zig
@@ -46,6 +46,7 @@ const mcp_schema_gate = @import("mcp_schema_gate.zig"); // #416: the load_tool_s
const native_fold = @import("native_fold.zig"); // folded native power tools: load_tool_schemas's native half
const local_tools = @import("local_tools.zig");
const schedule = @import("schedule.zig");
+const jev_tool = @import("jev_tool.zig");
const util = @import("util.zig"); // #225: unixMs, for the clock_sleep interrupted-elapsed measurement
const cite_markup = @import("cite_markup.zig");
@@ -147,6 +148,10 @@ pub fn runTools(self: *Agent, calls: []const ToolCall) ![]ExecResult {
brief_diversity.noteSiblingBatch(self.arena, self.tracer, calls, wave2.items, results);
}
}
+ if (!self.sub and jev_tool.takeCatalogRefresh()) {
+ self.invalidateRootTools();
+ try self.ensureRootTools(self.provider.kind);
+ }
if (defer_completion) if (eval_control.completionIndex(calls)) |i| {
var verify_failed = ext_idx.items.len == 0;
for (ext_idx.items) |j| {
diff --git a/src/exec.zig b/src/exec.zig
index 6e37a5f2..ff51bc2b 100644
--- a/src/exec.zig
+++ b/src/exec.zig
@@ -38,6 +38,7 @@ const workflow = @import("workflow.zig");
const execWorkflow = workflow.execWorkflow;
const mcp = @import("mcp.zig");
+const jev_tool = @import("jev_tool.zig");
const approvals_mod = @import("approvals.zig");
const Approvals = approvals_mod.Approvals;
const confinedPath = approvals_mod.confinedPath;
@@ -197,6 +198,7 @@ fn execToolInner(ctx: ToolCtx, call: ToolCall) !ToolOutput {
}
const input = call.input;
+ if (std.mem.eql(u8, call.name, jev_tool.name)) return jev_tool.execute(ctx, input);
if (std.mem.eql(u8, call.name, "learn_candidate")) {
if (ctx.from_sub) return .{
.text = try gpa.dupe(u8, "learning is root-only — subagents cannot run mutators, evaluators, or publish grades"),
@@ -407,6 +409,48 @@ test "preserveMode restores bits via handle chmod so Windows cannot panic (#179/
}
}
+test "native Jev dispatch rejects other models and latches off after one failed attempt" {
+ const jev = @import("jev_tool.zig");
+ jev.configure(struct {
+ pub fn get(_: @This(), key: []const u8) ?[]const u8 {
+ return if (std.mem.eql(u8, key, "JEV_BACKEND")) "mock-fail" else null;
+ }
+ }{});
+ defer jev.configure(struct {
+ pub fn get(_: @This(), _: []const u8) ?[]const u8 {
+ return null;
+ }
+ }{});
+ const base: @import("provider.zig").Provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "gpt-6-sol", .context = 100_000 };
+ var ctx: ToolCtx = .{ .gpa = std.testing.allocator, .io = std.testing.io, .client = undefined, .provider = base, .registry = null, .from_sub = false, .approvals = null, .tracer = null };
+ const parsed = try std.json.parseFromSlice(Value, std.testing.allocator, "{\"state\":\"10 tests passed\",\"question\":\"Did CI pass?\",\"type\":\"noul\"}", .{});
+ defer parsed.deinit();
+ const call: ToolCall = .{ .id = "1", .name = jev.name, .input = parsed.value };
+ const no_login = try execToolInner(ctx, call);
+ defer std.testing.allocator.free(no_login.text);
+ try std.testing.expect(no_login.is_error);
+ _ = jev.setCodegraffLogin(true);
+ try std.testing.expect(jev.available(ctx.provider)); // no upstream attempt before login
+ ctx.provider.id = "xai";
+ ctx.provider.model = "grok-4.7";
+ const other_provider = try execToolInner(ctx, call);
+ defer std.testing.allocator.free(other_provider.text);
+ try std.testing.expect(other_provider.is_error);
+ ctx.provider.id = "codex";
+ ctx.provider.model = "gpt-5.6";
+ const older_model = try execToolInner(ctx, call);
+ defer std.testing.allocator.free(older_model.text);
+ try std.testing.expect(older_model.is_error);
+ ctx.provider.model = "gpt-6-sol";
+ try std.testing.expect(jev.available(ctx.provider)); // ineligible attempts did not trip the circuit
+ const first = try execToolInner(ctx, call);
+ defer std.testing.allocator.free(first.text);
+ const second = try execToolInner(ctx, call);
+ defer std.testing.allocator.free(second.text);
+ try std.testing.expect(!first.is_error and !second.is_error);
+ try std.testing.expectEqualStrings(first.text, second.text);
+}
+
test "internal learning respects the parent privacy ceiling" {
var argv: [10][]const u8 = undefined;
var len = learningArgv(&argv, "graff", false);
diff --git a/src/jev_model_scope.zig b/src/jev_model_scope.zig
new file mode 100644
index 00000000..8552b4df
--- /dev/null
+++ b/src/jev_model_scope.zig
@@ -0,0 +1,51 @@
+//! Native Jev judgments are limited to GPT-6 on Codex/OpenAI and Xiaomi
+//! MiMo, including those model ids served through the Codegraff gateway.
+const std = @import("std");
+const Provider = @import("provider.zig").Provider;
+
+fn gptSix(model: []const u8) bool {
+ return std.mem.eql(u8, model, "gpt-6") or
+ std.mem.startsWith(u8, model, "gpt-6-") or
+ std.mem.startsWith(u8, model, "gpt-6.");
+}
+
+pub fn eligible(provider: Provider) bool {
+ const id = provider.id;
+ if (std.mem.eql(u8, id, "codex") or std.mem.eql(u8, id, "openai")) return gptSix(provider.model);
+ const mimo = std.mem.startsWith(u8, provider.model, "mimo-");
+ if (std.mem.eql(u8, id, "xiaomi")) return mimo;
+ return std.mem.eql(u8, id, "codegraff") and (mimo or gptSix(provider.model));
+}
+
+test "native Jev eligibility requires GPT-6 on Codex/OpenAI or Xiaomi MiMo" {
+ const base: Provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "gpt-6-sol", .context = 100_000 };
+ var p = base;
+ for ([_][]const u8{ "codex", "openai" }) |id| {
+ p.id = id;
+ for ([_][]const u8{ "gpt-6", "gpt-6-sol", "gpt-6.1", "gpt-6.1-mini" }) |model| {
+ p.model = model;
+ try std.testing.expect(eligible(p));
+ }
+ for ([_][]const u8{ "gpt-5.6", "gpt-60", "gpt-6x", "o3", "mimo-v2.6-pro" }) |model| {
+ p.model = model;
+ try std.testing.expect(!eligible(p));
+ }
+ }
+ p.id = "xiaomi";
+ p.model = "mimo-v2.6-pro";
+ try std.testing.expect(eligible(p));
+ p.model = "not-mimo";
+ try std.testing.expect(!eligible(p));
+ p.id = "codegraff";
+ for ([_][]const u8{ "mimo-v2.6-flash", "gpt-6-sol" }) |model| {
+ p.model = model;
+ try std.testing.expect(eligible(p));
+ }
+ for ([_][]const u8{ "gpt-5.6", "gpt-60", "o3", "claude-opus-4.8", "gemini-3.8-flash" }) |model| {
+ p.model = model;
+ try std.testing.expect(!eligible(p));
+ }
+ p.id = "openrouter";
+ p.model = "openai/gpt-6-sol";
+ try std.testing.expect(!eligible(p));
+}
diff --git a/src/jev_tool.zig b/src/jev_tool.zig
new file mode 100644
index 00000000..39d2ff2a
--- /dev/null
+++ b/src/jev_tool.zig
@@ -0,0 +1,288 @@
+//! Native, opt-in Jev judgment tool. One failed upstream attempt opens a
+//! session-long circuit: later calls skip the network and return control to
+//! the main model. No MCP process and no automatic context transfer.
+const std = @import("std");
+const Io = std.Io;
+const Allocator = std.mem.Allocator;
+const Value = std.json.Value;
+const Provider = @import("provider.zig").Provider;
+const ToolCtx = @import("tools.zig").ToolCtx;
+const ToolOutput = @import("tools.zig").ToolOutput;
+const ToolSpec = @import("schema.zig").ToolSpec;
+const scope = @import("jev_model_scope.zig");
+
+pub const name = "jev_judge";
+pub const description = "Ask Jev one closed-form judgment about a SHORT, NON-SENSITIVE state. Requires a Codegraff login and an eligible GPT-6 or MiMo model. Use for yes/no (noul), a choice, or an ordered score, not writing or open-ended reasoning. Never send source code, secrets, customer data, paths, or unrelated context. A low-confidence verdict escalates to you. If Jev fails once, this tool skips all later Jev calls for this session; decide yourself instead.";
+pub const input_schema =
+ \\{"type":"object","properties":{"state":{"type":"string","description":"Short non-sensitive facts needed for this judgment only; no code, paths or secrets"},"question":{"type":"string","description":"One closed-form question about state"},"type":{"type":"string","enum":["noul","choice","score"],"description":"noul=yes/no probability; choice=one option; score=ordered level"},"options":{"type":"array","items":{"type":"string"},"description":"Required for choice: 2-16 distinct labels"},"levels":{"type":"array","items":{"type":"string"},"description":"Required for score: 2-16 ordered descriptions"}},"required":["state","question","type"]}
+;
+const spec = ToolSpec{ .name = name, .desc = description, .schema = input_schema };
+const endpoint = "https://api.typesafe.ai/v1/systemone";
+const skip_text = "Jev unavailable: skipped. No more Jev requests will be sent this session; judge this step with the main model instead.";
+const Backend = enum { typesafe, mock, mock_fail };
+const State = struct {
+ mu: Io.Mutex = .init,
+ key: []const u8 = "",
+ backend: Backend = .typesafe,
+ codegraff_login: std.atomic.Value(bool) = .init(false),
+ down: std.atomic.Value(bool) = .init(false),
+ refresh: std.atomic.Value(bool) = .init(false),
+ attempts: std.atomic.Value(usize) = .init(0),
+};
+var state: State = .{};
+
+pub fn configure(env: anytype) void {
+ // Called once at session startup, before any tool work begins. Retain the
+ // environment's key in memory; it is never copied into a catalog/result.
+ state.key = env.get("TYPESAFE_API_KEY") orelse "";
+ const mode = env.get("JEV_BACKEND") orelse "";
+ state.backend = if (std.mem.eql(u8, mode, "mock")) .mock else if (std.mem.eql(u8, mode, "mock-fail")) .mock_fail else .typesafe;
+ state.codegraff_login.store(false, .release);
+ state.down.store(false, .release);
+ state.refresh.store(false, .release);
+ state.attempts.store(0, .release);
+}
+
+/// Returns true when a login transition changes the live tool catalog.
+pub fn setCodegraffLogin(logged_in: bool) bool {
+ return state.codegraff_login.swap(logged_in, .acq_rel) != logged_in;
+}
+
+pub fn available(provider: Provider) bool {
+ return state.codegraff_login.load(.acquire) and
+ (state.backend != .typesafe or state.key.len > 0) and
+ !state.down.load(.acquire) and scope.eligible(provider);
+}
+
+pub fn catalogExtras(provider: Provider) []const ToolSpec {
+ return if (available(provider)) &.{spec} else &.{};
+}
+
+pub fn takeCatalogRefresh() bool {
+ return state.refresh.swap(false, .acq_rel);
+}
+
+fn skipped(gpa: Allocator) !ToolOutput {
+ return .{ .text = try gpa.dupe(u8, skip_text) };
+}
+
+fn invalid(gpa: Allocator, text: []const u8) !ToolOutput {
+ return .{ .text = try gpa.dupe(u8, text), .is_error = true };
+}
+
+fn string(v: Value) ?[]const u8 {
+ return if (v == .string) v.string else null;
+}
+
+fn number(v: Value) ?f64 {
+ return switch (v) {
+ .float => |n| if (std.math.isFinite(n)) n else null,
+ .integer => |n| @floatFromInt(n),
+ else => null,
+ };
+}
+
+fn listValid(v: Value) bool {
+ if (v != .array or v.array.items.len < 2 or v.array.items.len > 16) return false;
+ for (v.array.items, 0..) |item, i| {
+ const label = string(item) orelse return false;
+ if (label.len == 0 or label.len > 120) return false;
+ for (v.array.items[0..i]) |old| if (std.mem.eql(u8, old.string, label)) return false;
+ }
+ return true;
+}
+
+fn makeBody(arena: Allocator, input: Value) ![]const u8 {
+ const obj = input.object;
+ const state_text = (obj.get("state") orelse return error.InvalidInput);
+ const facts = string(state_text) orelse return error.InvalidInput;
+ const question = string(obj.get("question") orelse return error.InvalidInput) orelse return error.InvalidInput;
+ const kind = string(obj.get("type") orelse return error.InvalidInput) orelse return error.InvalidInput;
+ if (facts.len == 0 or facts.len > 8192 or question.len == 0 or question.len > 512) return error.InvalidInput;
+ var q = std.json.ObjectMap.empty;
+ try q.put(arena, "type", .{ .string = kind });
+ try q.put(arena, "instructions", .{ .string = question });
+ if (std.mem.eql(u8, kind, "choice")) {
+ const options = obj.get("options") orelse return error.InvalidInput;
+ if (!listValid(options)) return error.InvalidInput;
+ var labels = std.json.ObjectMap.empty;
+ for (options.array.items) |item| try labels.put(arena, item.string, item);
+ try q.put(arena, "criteria", .{ .object = labels });
+ } else if (std.mem.eql(u8, kind, "score")) {
+ const levels = obj.get("levels") orelse return error.InvalidInput;
+ if (!listValid(levels)) return error.InvalidInput;
+ try q.put(arena, "criteria", levels);
+ } else if (!std.mem.eql(u8, kind, "noul")) return error.InvalidInput;
+ var questions = std.json.ObjectMap.empty;
+ try questions.put(arena, "q1", .{ .object = q });
+ var root = std.json.ObjectMap.empty;
+ try root.put(arena, "model", .{ .string = "jev-latest" });
+ try root.put(arena, "state", state_text);
+ try root.put(arena, "questions", .{ .object = questions });
+ var aw: Io.Writer.Allocating = .init(arena);
+ var serializer: std.json.Stringify = .{ .writer = &aw.writer };
+ try serializer.write(Value{ .object = root });
+ return aw.writer.buffered();
+}
+
+fn mockResponse(arena: Allocator, input: Value) ![]const u8 {
+ const kind = input.object.get("type").?.string;
+ if (std.mem.eql(u8, kind, "noul")) return "{\"answers\":{\"q1\":{\"noul\":0.98}}}";
+ if (std.mem.eql(u8, kind, "score")) return "{\"answers\":{\"q1\":{\"score\":0.9,\"confidence\":0.95}}}";
+ const label = input.object.get("options").?.array.items[0].string;
+ var aw: Io.Writer.Allocating = .init(arena);
+ var s: std.json.Stringify = .{ .writer = &aw.writer };
+ try s.write(.{ .answers = .{ .q1 = .{ .choice = label, .confidence = 0.95 } } });
+ return aw.writer.buffered();
+}
+
+fn fetch(ctx: ToolCtx, arena: Allocator, body: []const u8) ![]const u8 {
+ const bearer = try std.fmt.allocPrint(arena, "Bearer {s}", .{state.key});
+ var aw: Io.Writer.Allocating = .init(arena);
+ const res = try ctx.client.fetch(.{
+ .location = .{ .url = endpoint },
+ .method = .POST,
+ .payload = body,
+ .response_writer = &aw.writer,
+ .redirect_behavior = .unhandled,
+ .headers = .{
+ .content_type = .{ .override = "application/json" },
+ .authorization = .{ .override = bearer },
+ },
+ });
+ if (@intFromEnum(res.status) != 200 or aw.writer.buffered().len > 64 * 1024) return error.JevUnavailable;
+ return aw.writer.buffered();
+}
+
+fn verdict(arena: Allocator, input: Value, raw: []const u8) ![]const u8 {
+ const parsed = try std.json.parseFromSliceLeaky(Value, arena, raw, .{ .allocate = .alloc_always });
+ if (parsed != .object) return error.InvalidResponse;
+ const answers = parsed.object.get("answers") orelse return error.InvalidResponse;
+ if (answers != .object) return error.InvalidResponse;
+ const answer = answers.object.get("q1") orelse return error.InvalidResponse;
+ if (answer != .object) return error.InvalidResponse;
+ const kind = input.object.get("type").?.string;
+ const value: Value = if (std.mem.eql(u8, kind, "noul")) answer.object.get("noul") orelse return error.InvalidResponse else if (std.mem.eql(u8, kind, "choice")) answer.object.get("choice") orelse return error.InvalidResponse else answer.object.get("score") orelse return error.InvalidResponse;
+ if (std.mem.eql(u8, kind, "choice")) {
+ const label = string(value) orelse return error.InvalidResponse;
+ const options = input.object.get("options").?.array.items;
+ var found = false;
+ for (options) |opt| if (std.mem.eql(u8, opt.string, label)) {
+ found = true;
+ break;
+ };
+ if (!found) return error.InvalidResponse;
+ } else {
+ const n = number(value) orelse return error.InvalidResponse;
+ if (n < 0 or (std.mem.eql(u8, kind, "noul") and n > 1)) return error.InvalidResponse;
+ }
+ const reported = if (answer.object.get("confidence")) |v| number(v) else null;
+ if (reported) |c| if (c < 0 or c > 1) return error.InvalidResponse;
+ const estimated: f64 = if (std.mem.eql(u8, kind, "noul")) 2 * @abs(number(value).? - 0.5) else 0;
+ const confidence = reported orelse estimated;
+ const threshold: f64 = if (reported != null) 0.5 else 0.4;
+ var aw: Io.Writer.Allocating = .init(arena);
+ var s: std.json.Stringify = .{ .writer = &aw.writer };
+ try s.write(.{ .answer = value, .confidence = confidence, .confidence_source = if (reported != null) "reported" else "estimated", .escalate = confidence < threshold });
+ return aw.writer.buffered();
+}
+
+pub fn execute(ctx: ToolCtx, input: Value) !ToolOutput {
+ if (!state.codegraff_login.load(.acquire)) return invalid(ctx.gpa, "jev_judge requires a Codegraff login (`graff login`)");
+ if (!scope.eligible(ctx.provider)) return invalid(ctx.gpa, "jev_judge is available only with Codex/OpenAI GPT-6 or Xiaomi MiMo models");
+ if (state.backend == .typesafe and state.key.len == 0) return skipped(ctx.gpa);
+ if (state.down.load(.acquire)) return skipped(ctx.gpa);
+ if (input != .object) return invalid(ctx.gpa, "jev_judge needs state, question and type");
+ var temp = std.heap.ArenaAllocator.init(ctx.gpa);
+ defer temp.deinit();
+ const arena = temp.allocator();
+ const body = makeBody(arena, input) catch |err| switch (err) {
+ error.InvalidInput => return invalid(ctx.gpa, "jev_judge needs a short state, one question, and valid noul/choice/score options"),
+ else => return err,
+ };
+ state.mu.lockUncancelable(ctx.io);
+ defer state.mu.unlock(ctx.io);
+ if (state.down.load(.acquire)) return skipped(ctx.gpa);
+ _ = state.attempts.fetchAdd(1, .acq_rel);
+ const raw = switch (state.backend) {
+ .mock => try mockResponse(arena, input),
+ .mock_fail => error.JevUnavailable,
+ .typesafe => fetch(ctx, arena, body),
+ } catch {
+ state.down.store(true, .release);
+ state.refresh.store(true, .release);
+ return skipped(ctx.gpa);
+ };
+ const result = verdict(arena, input, raw) catch {
+ state.down.store(true, .release);
+ state.refresh.store(true, .release);
+ return skipped(ctx.gpa);
+ };
+ return .{ .text = try ctx.gpa.dupe(u8, result) };
+}
+
+test "native Jev failure makes exactly one attempt then skips without retry" {
+ const fake = struct {
+ fn get(_: @This(), key: []const u8) ?[]const u8 {
+ return if (std.mem.eql(u8, key, "JEV_BACKEND")) "mock-fail" else null;
+ }
+ }{};
+ configure(fake);
+ defer configure(struct {
+ fn get(_: @This(), _: []const u8) ?[]const u8 {
+ return null;
+ }
+ }{});
+ _ = setCodegraffLogin(true);
+ const p: Provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "gpt-6-sol", .context = 100_000 };
+ var client: std.http.Client = undefined;
+ const ctx: ToolCtx = .{ .gpa = std.testing.allocator, .io = std.testing.io, .client = &client, .provider = p, .registry = null, .from_sub = false, .approvals = null, .tracer = null };
+ const parsed = try std.json.parseFromSlice(Value, std.testing.allocator, "{\"state\":\"10 tests passed\",\"question\":\"Did CI pass?\",\"type\":\"noul\"}", .{});
+ defer parsed.deinit();
+ const first = try execute(ctx, parsed.value);
+ defer std.testing.allocator.free(first.text);
+ const second = try execute(ctx, parsed.value);
+ defer std.testing.allocator.free(second.text);
+ try std.testing.expect(!first.is_error and !second.is_error);
+ try std.testing.expectEqualStrings(skip_text, first.text);
+ try std.testing.expectEqualStrings(skip_text, second.text);
+ try std.testing.expectEqual(@as(usize, 1), state.attempts.load(.acquire));
+ try std.testing.expect(!available(p));
+ try std.testing.expect(takeCatalogRefresh());
+ try std.testing.expect(!takeCatalogRefresh());
+}
+
+test "native Jev mock uses TypeSafe wire and returns a typed verdict" {
+ configure(struct {
+ fn get(_: @This(), key: []const u8) ?[]const u8 {
+ return if (std.mem.eql(u8, key, "JEV_BACKEND")) "mock" else null;
+ }
+ }{});
+ defer configure(struct {
+ fn get(_: @This(), _: []const u8) ?[]const u8 {
+ return null;
+ }
+ }{});
+ _ = setCodegraffLogin(true);
+ const parsed = try std.json.parseFromSlice(Value, std.testing.allocator, "{\"state\":\"10 passed, 0 failed\",\"question\":\"Did CI pass?\",\"type\":\"noul\"}", .{});
+ defer parsed.deinit();
+ var temp = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer temp.deinit();
+ const arena = temp.allocator();
+ const body = try makeBody(arena, parsed.value);
+ const wire = try std.json.parseFromSliceLeaky(Value, arena, body, .{});
+ try std.testing.expectEqualStrings("jev-latest", wire.object.get("model").?.string);
+ try std.testing.expectEqualStrings("10 passed, 0 failed", wire.object.get("state").?.string);
+ const q1 = wire.object.get("questions").?.object.get("q1").?.object;
+ try std.testing.expectEqualStrings("Did CI pass?", q1.get("instructions").?.string);
+ const p: Provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "gpt-6-sol", .context = 100_000 };
+ var client: std.http.Client = undefined;
+ const ctx: ToolCtx = .{ .gpa = std.testing.allocator, .io = std.testing.io, .client = &client, .provider = p, .registry = null, .from_sub = false, .approvals = null, .tracer = null };
+ const out = try execute(ctx, parsed.value);
+ defer std.testing.allocator.free(out.text);
+ try std.testing.expect(!out.is_error);
+ const result = try std.json.parseFromSliceLeaky(Value, arena, out.text, .{});
+ try std.testing.expect(@abs(number(result.object.get("answer").?).? - 0.98) < 0.0001);
+ try std.testing.expect(!result.object.get("escalate").?.bool);
+ try std.testing.expectEqual(@as(usize, 1), state.attempts.load(.acquire));
+}
diff --git a/src/main.zig b/src/main.zig
index 88fa96d7..d83b22c4 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -558,6 +558,7 @@ const agentJobsReap = subagent.agentJobsReap; // #276 P0-3: background subagents
const workflow = @import("workflow_test.zig"); // the engine's tests live here (workflow.zig hit the 600-line cap)
const exec = @import("exec.zig");
test { // ── Unit tests (`zig build test`): pull in tests from imported modules (mcp.zig)
+ _ = .{ @import("jev_tool.zig"), @import("jev_model_scope.zig") };
_ = .{ @import("mcp_apps.zig"), @import("html_view.zig"), @import("providers_confirmation_tests.zig"), @import("repl_model_confirmation_tests.zig"), @import("repl_model_pick_tests.zig"), @import("engine_sink_citation_tests.zig"), @import("cite_markup_stream_tests.zig"), @import("oneshot_citation_tests.zig"), @import("tui_acp.zig") };
_ = .{ @import("mcp_server_tests.zig"), @import("main_test.zig"), @import("artifact_claim_command.zig"), @import("cli_path_hint.zig"), @import("run_budget.zig"), @import("pr_review_input.zig"), @import("pr_claim_review.zig"), @import("tui_paste_transfer.zig") };
_ = .{ @import("session_catalog.zig"), @import("session_prompt.zig"), @import("session_discovery.zig") };
diff --git a/src/picker_auth.zig b/src/picker_auth.zig
index b9b03936..cbab7956 100644
--- a/src/picker_auth.zig
+++ b/src/picker_auth.zig
@@ -11,6 +11,7 @@ const style = &ansi.style;
const term = @import("term.zig");
const tty = term.tty;
const oauth = @import("oauth.zig");
+const jev_tool = @import("jev_tool.zig");
const oauth_helpers = @import("oauth_helpers.zig"); // the test pins g_codex_home, the resolver startup sets
const policy = @import("agent_request_policy.zig");
const providers = @import("providers.zig");
@@ -42,6 +43,7 @@ pub const PickerFn = *const fn (
/// the startup loaders.
pub fn reloadLoginKey(root: *Agent, keys: *Keys, arena: Allocator, provider_id: []const u8) void {
const home = root.home;
+ var codegraff_reloaded = false;
for (provider_specs, &keys.values, &keys.sources) |spec, *value, *source| {
if (!std.mem.eql(u8, spec.id, provider_id)) continue;
var reloaded = false;
@@ -50,6 +52,7 @@ pub fn reloadLoginKey(root: *Agent, keys: *Keys, arena: Allocator, provider_id:
value.* = k;
source.* = .login;
reloaded = true;
+ codegraff_reloaded = true;
}
} else if (std.mem.eql(u8, provider_id, "kimi")) {
if (oauth.loadKimiOAuth(root.io, root.gpa, arena, home, false, null)) |k| {
@@ -96,6 +99,8 @@ pub fn reloadLoginKey(root: *Agent, keys: *Keys, arena: Allocator, provider_id:
root.closeCodexWs(); // the held socket was dialed with the stale bearer
};
}
+ if (std.mem.eql(u8, provider_id, "codegraff") and jev_tool.setCodegraffLogin(codegraff_reloaded))
+ root.invalidateRootTools();
if (std.mem.eql(u8, provider_id, "codex") and keys.get("codex") != null)
root.reloadModelCatalog(keys.*);
}
@@ -343,3 +348,53 @@ pub fn offerProviderAuth(
};
try switchProvider(root, arena, provider, out);
}
+
+test "in-session Codegraff login refreshes native Jev visibility on a GPT-6 session" {
+ const io = std.testing.io;
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var real_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const home = real_buf[0..try tmp.dir.realPath(io, &real_buf)];
+ jev_tool.configure(struct {
+ pub fn get(_: @This(), key: []const u8) ?[]const u8 {
+ return if (std.mem.eql(u8, key, "JEV_BACKEND")) "mock" else null;
+ }
+ }{});
+ defer jev_tool.configure(struct {
+ pub fn get(_: @This(), _: []const u8) ?[]const u8 {
+ return null;
+ }
+ }{});
+ var root: Agent = undefined;
+ root.io = io;
+ root.gpa = std.testing.allocator;
+ root.arena = arena;
+ root.home = home;
+ root.sub = false;
+ root.registry = null;
+ root.provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "gpt-6-sol", .context = 100_000 };
+ root.tools_anthropic = "";
+ root.tools_openai = "";
+ root.tools_responses = "";
+ root.tools_interactions = "";
+ var keys: Keys = .{ .values = @splat(null) };
+ try root.ensureRootTools(.responses);
+ try std.testing.expect(std.mem.indexOf(u8, root.tools_responses, jev_tool.name) == null);
+ try Io.Dir.cwd().writeFile(io, .{
+ .sub_path = try std.fmt.allocPrint(arena, "{s}/.simple-harness-codegraff.json", .{home}),
+ .data = "{\"api_key\":\"synthetic-login\"}",
+ });
+ reloadLoginKey(&root, &keys, arena, "codegraff");
+ try std.testing.expectEqual(Keys.CredentialSource.login, keys.source("codegraff"));
+ try std.testing.expectEqual(@as(usize, 0), root.tools_responses.len);
+ try root.ensureRootTools(.responses);
+ try std.testing.expect(std.mem.indexOf(u8, root.tools_responses, jev_tool.name) != null);
+ try tmp.dir.deleteFile(io, ".simple-harness-codegraff.json");
+ reloadLoginKey(&root, &keys, arena, "codegraff");
+ try std.testing.expectEqual(@as(usize, 0), root.tools_responses.len);
+ try root.ensureRootTools(.responses);
+ try std.testing.expect(std.mem.indexOf(u8, root.tools_responses, jev_tool.name) == null);
+}
diff --git a/src/providers.zig b/src/providers.zig
index 220f13b7..6cd7ff68 100644
--- a/src/providers.zig
+++ b/src/providers.zig
@@ -26,6 +26,7 @@ const fallback_config = @import("fallback_config.zig");
const util = @import("util.zig");
const trace = @import("trace.zig");
const orch_rows = @import("orchestration_rows.zig");
+const jev_tool = @import("jev_tool.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]");
@@ -75,6 +76,7 @@ fn translateHistory(arena: Allocator, msgs: *std.json.Array, to_kind: Provider.K
pub fn applyProviderInner(root: *Agent, arena: Allocator, p: Provider, persist: bool) ![]const u8 {
const same_format = root.provider.kind == p.kind;
+ const same_jev_scope = jev_tool.available(root.provider) == jev_tool.available(p);
const same_selection = same_format and
root.provider.context == p.context and
std.mem.eql(u8, root.provider.id, p.id) and
@@ -101,6 +103,8 @@ pub fn applyProviderInner(root: *Agent, arena: Allocator, p: Provider, persist:
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);
};
+ // A same-format model switch can change native Jev visibility.
+ if (!same_jev_scope) root.invalidateRootTools();
// Keep the context estimate and first request exact without eagerly
// materializing formats this session has never used.
try root.ensureRootTools(p.kind);
@@ -547,6 +551,31 @@ test "applyProviderInner preserves the server meter on an exact model re-selecti
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);
+
+ const mock = struct {
+ pub fn get(_: @This(), key: []const u8) ?[]const u8 {
+ return if (std.mem.eql(u8, key, "JEV_BACKEND")) "mock" else null;
+ }
+ }{};
+ jev_tool.configure(mock);
+ defer jev_tool.configure(struct {
+ pub fn get(_: @This(), _: []const u8) ?[]const u8 {
+ return null;
+ }
+ }{});
+ _ = jev_tool.setCodegraffLogin(true);
+ // Same-format GPT-6 → GPT-5 switches must also rebuild the catalog.
+ var gpt6 = changed;
+ gpt6.model = "gpt-6-sol";
+ root.invalidateRootTools();
+ _ = try applyProviderInner(&root, a, gpt6, false);
+ try std.testing.expect(std.mem.indexOf(u8, root.tools_responses, jev_tool.name) != null);
+ _ = try applyProviderInner(&root, a, changed, false);
+ try std.testing.expect(std.mem.indexOf(u8, root.tools_responses, jev_tool.name) == null);
+ _ = try applyProviderInner(&root, a, changed_format, false);
+ try std.testing.expect(std.mem.indexOf(u8, root.tools_openai, jev_tool.name) == null);
+ _ = try applyProviderInner(&root, a, gpt6, false);
+ try std.testing.expect(std.mem.indexOf(u8, root.tools_responses, jev_tool.name) != null);
}
test "set_model control resolves explicit provider/model fields" {
diff --git a/src/session_settings.zig b/src/session_settings.zig
index 32e274a7..79748046 100644
--- a/src/session_settings.zig
+++ b/src/session_settings.zig
@@ -19,6 +19,9 @@ const agent_mod = @import("agent.zig");
const http = @import("http.zig");
const http_stall = @import("http_stall.zig");
const plugins = @import("plugins.zig");
+const jev_tool = @import("jev_tool.zig");
+const oauth = @import("oauth.zig");
+const keys_cli = @import("keys_cli.zig");
const job_idle = @import("job_idle.zig"); // #199: GRAFF_JOB_IDLE_WARN_MINS / GRAFF_JOB_IDLE_STOP_MINS
const ws = @import("ws.zig");
const agent_ws = @import("agent_ws.zig"); // codex_ws_idle_ms override (#codex-ws)
@@ -66,6 +69,7 @@ pub fn applyEnvKnobs(arena: Allocator, environ_map: anytype) !void {
// auto-detection above it, NOT arbitrary workspace config.
main_mod.g_path_env = try arena.dupe(u8, environ_map.get("PATH") orelse "");
plugins.applyEnv(environ_map);
+ jev_tool.configure(environ_map);
main_mod.g_codedb_guard = environ_map.get("GRAFF_NO_CODEDB_GUARD") == null; // issue #626 guard, opt-out via env
job_idle.applyEnv(environ_map); // #199: background-job idle warn/stop, minutes (0 = off)
main_mod.g_force_stall_once = environ_map.get("GRAFF_FORCE_STALL_ONCE") != null; // #134 test seam
@@ -262,8 +266,14 @@ pub fn applyEnvKnobs(arena: Allocator, environ_map: anytype) !void {
}
}
+fn codegraffLoginPresent(io: Io, arena: Allocator, environ_map: anytype) bool {
+ const home = keys_cli.homeEnv(environ_map) orelse return false;
+ return oauth.loadCodegraffKey(io, arena, home) != null;
+}
+
pub fn setupSkillsAndTheme(io: Io, arena: Allocator, environ_map: anytype, out: *Io.Writer, flags: args.Flags, use_color: bool, json_mode: bool, cwd_display: []const u8) !ThemeSetup {
try applyEnvKnobs(arena, environ_map);
+ _ = jev_tool.setCodegraffLogin(codegraffLoginPresent(io, arena, environ_map));
skills.loadSkillSettings(io, arena); // per-skill opt-outs, also gates the auto-connect
anim.loadAnimationSetting(io, arena); // {"animation": "..."} → thinking spinner choice
anim.loadThemeSetting(io, arena); // {"theme": "<name>"} → opt-in terminal color theme
@@ -343,3 +353,30 @@ pub fn setupSkillsAndTheme(io: Io, arena: Allocator, environ_map: anytype, out:
anim.loadDevSpinnerOptOut(io, arena, environ_map);
return .{ .theme_on = theme_on, .limyuxi_glam = limyuxi_glam, .should_exit = false };
}
+
+test "Jev startup gate requires a persisted Codegraff login, not an env API key" {
+ const io = std.testing.io;
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var real_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const home = real_buf[0..try tmp.dir.realPath(io, &real_buf)];
+ const env = struct {
+ home: []const u8,
+ pub fn get(self: @This(), key: []const u8) ?[]const u8 {
+ if (std.mem.eql(u8, key, "HOME")) return self.home;
+ if (std.mem.eql(u8, key, "CODEGRAFF_API_KEY")) return "env-only-key";
+ return null;
+ }
+ }{ .home = home };
+ try std.testing.expect(!codegraffLoginPresent(io, arena, env));
+ try Io.Dir.cwd().writeFile(io, .{
+ .sub_path = try std.fmt.allocPrint(arena, "{s}/.simple-harness-codegraff.json", .{home}),
+ .data = "{\"api_key\":\"synthetic-login\"}",
+ });
+ try std.testing.expect(codegraffLoginPresent(io, arena, env));
+ try tmp.dir.deleteFile(io, ".simple-harness-codegraff.json");
+ try std.testing.expect(!codegraffLoginPresent(io, arena, env));
+}