Skip to content

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

Python

jev: pick each turn's effort alongside its first request

justrachfcc21761b123
diff --git a/docs/adr/0246-jev-picks-each-turns-effort-alongside-the-first-request.md b/docs/adr/0246-jev-picks-each-turns-effort-alongside-the-first-request.md
new file mode 100644
index 00000000..cd87f453
--- /dev/null
+++ b/docs/adr/0246-jev-picks-each-turns-effort-alongside-the-first-request.md
@@ -0,0 +1,40 @@
+# 0246. Jev picks each turn's effort, alongside the first request
+
+Status: accepted 2026-10-03
+
+## Context
+
+Logged in to Codegraff, graff offers GPT-6 and MiMo v2.6 models the
+`jev_effort` tool, which asks the hosted Jev selector to choose the
+reasoning effort. In eval runs the models never called it: across 132 graff
+runs on two models the tool was offered every time and called none. The
+ChatGPT-plan sign-in, `chatgpt-new` (ADR 0229), was also not eligible, so
+most ChatGPT-plan users were never offered Jev at all.
+
+Asking Jev before a turn's first request worked but cost a call's wait,
+about a third of a second in the median, on every turn.
+
+## Decision
+
+- `chatgpt-new` GPT-6 models are eligible for Jev, like `codex` and `openai`.
+- When a root turn starts on an eligible model at the session's default
+ effort, graff asks Jev itself, concurrently with the turn's first model
+ request. The pick applies from the turn's next request, where pending
+ selections are already applied, so it adds no wait.
+- The pick holds for that turn and is never saved. An effort the user chose
+ is never overridden, an effort changed during the turn is kept, and a pick
+ still in flight when the turn ends is canceled and dropped. A canceled pick
+ does not open Jev's session circuit; a failed one does, as before.
+- Jev sees a summary built locally from the request: its words, with code,
+ paths, file names, URLs, emails, identifiers and number-heavy or long
+ tokens dropped, at most 280 bytes. `GRAFF_JEV_AUTO=0` turns the per-turn
+ pick off; the tool is unchanged.
+
+## Consequences
+
+Every eligible turn makes one gateway call; its charge is negligible. A
+turn's first request always runs at the default effort, so a one-request
+turn gets no benefit. On the eval tasks Jev picked the default effort every
+time, so time and pass rates were unchanged. Revisit if Jev's picks start to
+cost time or accuracy, or if starting the turn at the previous turn's pick
+proves worth it.
diff --git a/docs/adr/README.md b/docs/adr/README.md
index ec639dae..2e0095a7 100644
--- a/docs/adr/README.md
+++ b/docs/adr/README.md
@@ -272,6 +272,7 @@ record only when you need the evidence or the edge cases.
| [0243](0243-repo-context-rides-as-an-input-item-on-responses.md) | On the Responses wire for codex, chatgpt-new and openai, root turns send the project-instructions section and the layout as one developer input item ahead of the conversation, so the instructions are byte-identical across repos and cache on turn 1. |
| [0244](0244-saved-workspaces-have-durable-discovery.md) | Remember saved workspace roots locally; remote picker targets are workspace-qualified, and resume must enter the selected tree before restoring it. |
| [0245](0245-fullscreen-tui-is-an-external-client.md) | The fullscreen UI is maintained separately; `graff tui` launches installed `graff-tui` before engine startup, sibling first then PATH, without downloading. TTY `graff repl` uses the line REPL. Supersedes 0041, 0042 and the fullscreen portion of 0142. |
+| [0246](0246-jev-picks-each-turns-effort-alongside-the-first-request.md) | On an eligible model at the default effort, graff asks Jev for the turn's effort alongside the first request; the pick applies from the next request, holds for that turn, is never saved, and never overrides a user's effort. `chatgpt-new` GPT-6 models are eligible. |
## When to write one
diff --git a/src/agent.zig b/src/agent.zig
index 2316ed89..2dd04781 100644
--- a/src/agent.zig
+++ b/src/agent.zig
@@ -327,6 +327,7 @@ pub const Agent = struct {
pub fn runTurn(self: *Agent) anyerror![]const u8 {
defer @import("jev_effort_state.zig").finishTurn(self);
errdefer self.jev_effort_pending.invalidate(self.io);
+ @import("jev_auto.zig").beginTurn(self); // ADR 0246: Jev picks this turn's effort; finishTurn puts it back
self.async_tools_armed = !self.sub and self.eval_cmd == null;
defer self.async_tools_armed = false;
defer @import("agent_async_tools.zig").reset(self);
diff --git a/src/jev_auto.zig b/src/jev_auto.zig
new file mode 100644
index 00000000..2f65e747
--- /dev/null
+++ b/src/jev_auto.zig
@@ -0,0 +1,259 @@
+//! ADR 0246: Jev in the flow. When a root turn starts on an eligible model,
+//! graff asks Jev for the turn's reasoning effort itself; the model was offered
+//! the jev_effort tool and never called it. The request runs alongside the
+//! turn's first model call, so it adds no wait: its pick applies from the
+//! turn's next request (agent_request.zig applies pending picks there). Jev
+//! sees a summary built here: the request's words with code, paths, file
+//! names, URLs, emails, identifiers and number-heavy or long tokens dropped.
+//! The pick holds for that turn only and is never saved, an effort the user
+//! chose (anything but the session default) is never overridden, and a pick
+//! still in flight when the turn ends is canceled and dropped.
+const std = @import("std");
+const Io = std.Io;
+const Allocator = std.mem.Allocator;
+const ReasoningEffort = @import("main.zig").ReasoningEffort;
+const jev_tool = @import("jev_tool.zig");
+
+/// GRAFF_JEV_AUTO=0 (or off/false/no) turns the per-turn selection off.
+pub var enabled: bool = true;
+/// How long a turn waits for Jev before it goes ahead at the default effort.
+pub const timeout_ms: u32 = 4_000;
+pub const max_summary = 280;
+
+/// jev_tool.configure passes GRAFF_JEV_AUTO's value.
+pub fn setEnabled(value: ?[]const u8) void {
+ const v = value orelse "";
+ enabled = !(std.mem.eql(u8, v, "0") or std.ascii.eqlIgnoreCase(v, "off") or
+ std.ascii.eqlIgnoreCase(v, "false") or std.ascii.eqlIgnoreCase(v, "no"));
+}
+
+/// The words Jev may see: no code, paths, file names, URLs, emails,
+/// identifiers, ids or long tokens. Empty when nothing safe is left.
+pub fn summary(arena: Allocator, prompt: []const u8) ![]const u8 {
+ var out: std.ArrayList(u8) = .empty;
+ var in_fence = false;
+ var lines = std.mem.splitScalar(u8, prompt, '\n');
+ while (lines.next()) |line| {
+ const t = std.mem.trim(u8, line, " \t\r");
+ if (std.mem.startsWith(u8, t, "```")) {
+ in_fence = !in_fence;
+ continue;
+ }
+ if (in_fence) continue;
+ var words = std.mem.tokenizeAny(u8, t, " \t");
+ while (words.next()) |w| {
+ if (!keep(w)) continue;
+ if (out.items.len + w.len + 1 > max_summary) return out.items;
+ if (out.items.len != 0) try out.append(arena, ' ');
+ try out.appendSlice(arena, w);
+ }
+ }
+ return out.items;
+}
+
+fn keep(w: []const u8) bool {
+ if (w.len > 24) return false; // keys, hashes, long identifiers
+ if (std.mem.indexOfAny(u8, w, "/\\`@<>{}[]=$|_#~") != null) return false; // paths, code, emails, markup, identifiers
+ var digits: usize = 0;
+ for (w) |c| digits += @intFromBool(std.ascii.isDigit(c));
+ if (digits >= 4) return false; // ids and numbers that identify something
+ const core = std.mem.trimEnd(u8, w, ".,;:!?)\"'");
+ if (std.mem.lastIndexOfScalar(u8, core, '.')) |dot| if (dot > 0 and dot + 1 < core.len) return false; // file.ext, a.b.c
+ return true;
+}
+
+fn lastUserText(agent: anytype) ?[]const u8 {
+ const items = agent.messages.items;
+ var i = items.len;
+ while (i > 0) {
+ i -= 1;
+ const m = items[i];
+ if (m != .object) continue;
+ const role = m.object.get("role") orelse continue;
+ if (role != .string or !std.mem.eql(u8, role.string, "user")) continue;
+ if (m.object.get(@import("session_wake.zig").origin_key) != null) return null; // a harness message, not a request
+ const content = m.object.get("content") orelse return null;
+ switch (content) {
+ .string => |s| return s,
+ .array => |parts| for (parts.items) |part| {
+ if (part != .object) continue;
+ const text = part.object.get("text") orelse continue;
+ if (text == .string and text.string.len != 0) return text.string;
+ },
+ else => {},
+ }
+ return null;
+ }
+ return null;
+}
+
+/// Start this turn's pick alongside its first request. Root turns at the
+/// session default only; never blocks.
+pub fn beginTurn(agent: anytype) void {
+ const pending = &agent.jev_effort_pending;
+ pending.auto = .{};
+ if (!enabled or agent.sub or agent.reasoning != .medium) return;
+ if (!jev_tool.available(agent.provider)) return;
+ const prompt = lastUserText(agent) orelse return;
+ var scratch = std.heap.ArenaAllocator.init(agent.gpa);
+ defer scratch.deinit();
+ const task = summary(scratch.allocator(), prompt) catch return;
+ if (task.len == 0) return;
+ const owned = agent.gpa.dupe(u8, task) catch return;
+ const token = pending.beginTurn(agent.io, agent.provider) orelse {
+ agent.gpa.free(owned);
+ return;
+ };
+ pending.auto.task = owned;
+ pending.auto.saved = agent.reasoning;
+ pending.auto.future = agent.io.concurrent(pickTask, .{ agent.gpa, agent.io, agent.client, agent.provider, owned, pending, token }) catch {
+ pending.abort(agent.io, token);
+ agent.gpa.free(owned);
+ pending.auto = .{};
+ return;
+ };
+}
+
+fn pickTask(gpa: Allocator, io: Io, client: *std.http.Client, provider: @import("provider.zig").Provider, task: []const u8, pending: *@import("jev_effort_state.zig").Pending, token: u64) void {
+ const t0 = Io.Timestamp.now(io, .awake).nanoseconds;
+ const chosen = jev_tool.chooseForTurn(gpa, io, client, provider, task, timeout_ms);
+ const ms = @divTrunc(Io.Timestamp.now(io, .awake).nanoseconds - t0, std.time.ns_per_ms);
+ pending.auto.ms.store(@intCast(std.math.clamp(ms, 0, std.math.maxInt(u32))), .release);
+ if (chosen) |effort| {
+ pending.auto.picked = effort;
+ _ = pending.commit(io, token, effort);
+ pending.auto.outcome.store(1, .release);
+ } else {
+ pending.abort(io, token);
+ pending.auto.outcome.store(2, .release);
+ }
+}
+
+/// Cancel a pick still in flight, drop one no request applied, and put the
+/// session effort back unless someone changed it during the turn.
+pub fn endTurn(agent: anytype) void {
+ const pending = &agent.jev_effort_pending;
+ if (pending.auto.future == null and pending.auto.saved == null) return;
+ if (pending.auto.future) |*f| f.cancel(agent.io);
+ pending.dropTurn(agent.io);
+ if (pending.auto.applied) |a| if (agent.reasoning == a) {
+ if (pending.auto.saved) |s| agent.reasoning = s;
+ };
+ if (agent.tracer) |tr| {
+ var buf: [96]u8 = undefined;
+ const outcome = pending.auto.outcome.load(.acquire);
+ const line = std.fmt.bufPrint(&buf, "turn effort {s} in {d}ms{s}", .{
+ if (outcome == 1) @tagName(pending.auto.picked) else "unavailable",
+ pending.auto.ms.load(.acquire),
+ if (outcome == 1 and pending.auto.applied == null) " (late, dropped)" else "",
+ }) catch "turn effort";
+ tr.note("jev", line);
+ }
+ if (pending.auto.task.len != 0) agent.gpa.free(pending.auto.task);
+ pending.auto = .{};
+}
+
+test "ADR 0246: the summary keeps the request's words and drops what could identify the work" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ try std.testing.expectEqualStrings("Fix the failing test in and run", try summary(a, "Fix the failing test in src/app/main.py and run `pytest -q`."));
+ try std.testing.expectEqualStrings("Count the errors per service, then write", try summary(a, "Count the errors per service, then write summary.json\n```python\nsecret = 'sk-live-1234'\n```"));
+ try std.testing.expectEqualStrings("Email about the outage", try summary(a, "Email [email protected] about https://status.example.com the outage"));
+ try std.testing.expectEqualStrings("Rename to in every caller", try summary(a, "Rename get_user to fetch_user in every caller"));
+ try std.testing.expectEqualStrings("Close issue as done.", try summary(a, "Close issue 48213 as done."));
+ try std.testing.expectEqualStrings("", try summary(a, "ghp_0123456789abcdefghijklmnopqrstuvwxyz"));
+ var long: std.ArrayList(u8) = .empty;
+ for (0..100) |_| try long.appendSlice(a, "word ");
+ const cut = try summary(a, long.items);
+ try std.testing.expect(cut.len <= max_summary and cut.len > max_summary - 10);
+}
+
+const TestAgent = struct {
+ gpa: Allocator,
+ io: Io,
+ client: *std.http.Client,
+ provider: @import("provider.zig").Provider,
+ reasoning: ReasoningEffort = .medium,
+ sub: bool = false,
+ fast: bool = false,
+ ultracode_mode: bool = false,
+ show_thinking: bool = false,
+ ai_title: bool = false,
+ messages: std.json.Array,
+ tracer: ?*@import("trace.zig").Tracer = null,
+ jev_effort_pending: @import("jev_effort_state.zig").Pending = .{},
+};
+
+fn testAgent(arena: Allocator, client: *std.http.Client, prompt: []const u8) !TestAgent {
+ var messages = std.json.Array.init(arena);
+ var user: std.json.ObjectMap = .empty;
+ try user.put(arena, "role", .{ .string = "user" });
+ try user.put(arena, "content", .{ .string = prompt });
+ try messages.append(.{ .object = user });
+ return .{ .gpa = std.testing.allocator, .io = std.testing.io, .client = client, .messages = messages, .provider = .{ .id = "chatgpt-new", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "gpt-6.1-sol", .context = 272_000 } };
+}
+
+fn waitPick(agent: *TestAgent) void {
+ if (agent.jev_effort_pending.auto.future) |*f| f.await(agent.io);
+}
+
+test "ADR 0246: the pick runs alongside the turn, applies at the next request, and the turn's end puts the default back" {
+ const Env = struct {
+ mode: []const u8,
+ pub fn get(self: @This(), key: []const u8) ?[]const u8 {
+ return if (std.mem.eql(u8, key, "JEV_BACKEND")) self.mode else null;
+ }
+ };
+ jev_tool.configure(Env{ .mode = "mock" });
+ defer jev_tool.configure(Env{ .mode = "" });
+ _ = jev_tool.setCodegraffLoginKey(std.testing.io, "synthetic-login");
+ defer _ = jev_tool.setCodegraffLoginKey(std.testing.io, null);
+ const state = @import("jev_effort_state.zig");
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ var client: std.http.Client = undefined;
+
+ // beginTurn returns before the pick lands: the first request runs at the default.
+ var agent = try testAgent(arena_state.allocator(), &client, "Refactor the session store and keep the tests green");
+ beginTurn(&agent);
+ try std.testing.expectEqual(ReasoningEffort.medium, agent.reasoning);
+ waitPick(&agent);
+ state.apply(&agent); // the next request boundary
+ try std.testing.expectEqual(ReasoningEffort.high, agent.reasoning); // the mock answers high
+ endTurn(&agent);
+ try std.testing.expectEqual(ReasoningEffort.medium, agent.reasoning);
+
+ // A pick no request applied is dropped at the turn's end, not applied or saved.
+ var late = try testAgent(arena_state.allocator(), &client, "Refactor the session store");
+ beginTurn(&late);
+ waitPick(&late);
+ endTurn(&late);
+ try std.testing.expectEqual(ReasoningEffort.medium, late.reasoning);
+ state.apply(&late);
+ try std.testing.expectEqual(ReasoningEffort.medium, late.reasoning);
+
+ // An effort changed during the turn stays.
+ var changed = try testAgent(arena_state.allocator(), &client, "Refactor the session store");
+ beginTurn(&changed);
+ waitPick(&changed);
+ state.apply(&changed);
+ changed.reasoning = .xhigh;
+ endTurn(&changed);
+ try std.testing.expectEqual(ReasoningEffort.xhigh, changed.reasoning);
+
+ // A user-chosen effort, a subagent, and the off switch start no pick.
+ var pinned = try testAgent(arena_state.allocator(), &client, "Refactor the session store");
+ pinned.reasoning = .low;
+ beginTurn(&pinned);
+ try std.testing.expect(pinned.jev_effort_pending.auto.future == null);
+ var child = try testAgent(arena_state.allocator(), &client, "Refactor the session store");
+ child.sub = true;
+ beginTurn(&child);
+ try std.testing.expect(child.jev_effort_pending.auto.future == null);
+ defer enabled = true;
+ setEnabled("0");
+ var off = try testAgent(arena_state.allocator(), &client, "Refactor the session store");
+ beginTurn(&off);
+ try std.testing.expect(off.jev_effort_pending.auto.future == null);
+}
diff --git a/src/jev_effort_state.zig b/src/jev_effort_state.zig
index c3339927..a0f69bbd 100644
--- a/src/jev_effort_state.zig
+++ b/src/jev_effort_state.zig
@@ -16,11 +16,37 @@ pub const Pending = struct {
provider: [64]u8 = undefined,
model: [256]u8 = undefined,
effort: ReasoningEffort = .medium,
+ /// ADR 0246: the selection in flight is jev_auto's turn pick, which applies
+ /// for one turn and is never saved; the jev_effort tool's is the session's.
+ turn_scoped: bool = false,
+ /// ADR 0246: this agent's turn pick, owned by jev_auto.zig.
+ auto: Auto = .{},
+
+ pub const Auto = struct {
+ future: ?Io.Future(void) = null,
+ task: []u8 = &.{}, // gpa-owned summary the pick reads
+ saved: ?ReasoningEffort = null, // the effort the turn started at
+ applied: ?ReasoningEffort = null, // the pick a request boundary applied
+ outcome: std.atomic.Value(u8) = .init(0), // 0 in flight, 1 picked, 2 unavailable
+ picked: ReasoningEffort = .medium,
+ ms: std.atomic.Value(u32) = .init(0),
+ };
+
+ pub const Taken = struct { effort: ReasoningEffort, turn: bool };
pub fn begin(self: *Pending, io: Io, route: Provider) ?u64 {
+ return self.beginScoped(io, route, false);
+ }
+
+ pub fn beginTurn(self: *Pending, io: Io, route: Provider) ?u64 {
+ return self.beginScoped(io, route, true);
+ }
+
+ fn beginScoped(self: *Pending, io: Io, route: Provider, turn: bool) ?u64 {
self.mu.lockUncancelable(io);
defer self.mu.unlock(io);
if (self.phase != .idle or route.id.len > self.provider.len or route.model.len > self.model.len) return null;
+ self.turn_scoped = turn;
self.token +%= 1;
@memcpy(self.provider[0..route.id.len], route.id);
@memcpy(self.model[0..route.model.len], route.model);
@@ -46,13 +72,26 @@ pub const Pending = struct {
}
pub fn take(self: *Pending, io: Io, route: Provider) ?ReasoningEffort {
+ return if (self.takeScoped(io, route)) |t| t.effort else null;
+ }
+
+ pub fn takeScoped(self: *Pending, io: Io, route: Provider) ?Taken {
self.mu.lockUncancelable(io);
defer self.mu.unlock(io);
if (self.phase != .selected) return null;
self.phase = .idle;
if (!std.mem.eql(u8, route.id, self.provider[0..self.provider_len]) or
!std.mem.eql(u8, route.model, self.model[0..self.model_len])) return null;
- return self.effort;
+ return .{ .effort = self.effort, .turn = self.turn_scoped };
+ }
+
+ /// Drop a turn pick no request has applied: it is neither applied nor saved.
+ pub fn dropTurn(self: *Pending, io: Io) void {
+ self.mu.lockUncancelable(io);
+ defer self.mu.unlock(io);
+ if (!self.turn_scoped or self.phase == .idle) return;
+ self.token +%= 1;
+ self.phase = .idle;
}
pub fn invalidate(self: *Pending, io: Io) void {
@@ -64,19 +103,28 @@ pub const Pending = struct {
};
pub fn applyToState(agent: anytype) bool {
- const effort = agent.jev_effort_pending.take(agent.io, agent.provider) orelse return false;
- if (!@import("effort_route.zig").allows(agent.provider.id, agent.provider.model, @tagName(effort))) return false;
- agent.reasoning = effort;
- return true;
+ return applyTaken(agent) != null;
+}
+
+fn applyTaken(agent: anytype) ?Pending.Taken {
+ const t = agent.jev_effort_pending.takeScoped(agent.io, agent.provider) orelse return null;
+ if (!@import("effort_route.zig").allows(agent.provider.id, agent.provider.model, @tagName(t.effort))) return null;
+ agent.reasoning = t.effort;
+ return t;
}
pub fn apply(agent: anytype) void {
- if (!applyToState(agent)) return;
+ const t = applyTaken(agent) orelse return;
+ if (t.turn) {
+ agent.jev_effort_pending.auto.applied = t.effort; // ADR 0246: a turn pick holds for this turn, never saved
+ return;
+ }
if (agent.sub) return; // ADR 0232: a child's selection is its own; the saved effort is the root's
_ = @import("repl_glue.zig").saveThinkingSettings(agent.io, agent.gpa, agent.reasoning, agent.fast, agent.ultracode_mode, agent.show_thinking, agent.ai_title);
}
pub fn finishTurn(agent: anytype) void {
+ @import("jev_auto.zig").endTurn(agent); // ADR 0246: before apply(), so a turn's pick is never saved
if (@TypeOf(agent.*).esc_cancel.load(.acquire) or @import("acp_engine.zig").cancel_flag.load(.acquire))
agent.jev_effort_pending.invalidate(agent.io)
else
diff --git a/src/jev_model_scope.zig b/src/jev_model_scope.zig
index 71f5efd6..f9f2aff5 100644
--- a/src/jev_model_scope.zig
+++ b/src/jev_model_scope.zig
@@ -15,7 +15,8 @@ fn mimoV26(model: []const u8) bool {
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);
+ // chatgpt-new is the ChatGPT-plan sign-in every login name maps to (ADR 0229).
+ if (std.mem.eql(u8, id, "codex") or std.mem.eql(u8, id, "openai") or std.mem.eql(u8, id, "chatgpt-new")) return gptSix(provider.model);
const mimo = mimoV26(provider.model);
if (std.mem.eql(u8, id, "xiaomi")) return mimo;
return std.mem.eql(u8, id, "codegraff") and (mimo or gptSix(provider.model));
@@ -24,7 +25,7 @@ pub fn eligible(provider: Provider) bool {
test "native Jev eligibility requires GPT-6 or MiMo v2.6 on supported routes" {
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| {
+ for ([_][]const u8{ "codex", "openai", "chatgpt-new" }) |id| {
p.id = id;
for ([_][]const u8{ "gpt-6", "gpt-6-sol", "gpt-6.1", "gpt-6.1-mini" }) |model| {
p.model = model;
diff --git a/src/jev_tool.zig b/src/jev_tool.zig
index 6a75b181..713551f1 100644
--- a/src/jev_tool.zig
+++ b/src/jev_tool.zig
@@ -39,6 +39,7 @@ const State = struct {
var state: State = .{};
pub fn configure(env: anytype) void {
+ @import("jev_auto.zig").setEnabled(env.get("GRAFF_JEV_AUTO"));
// Only a persisted Codegraff login may supply the gateway credential.
// The provider's own key and any upstream-specific environment key are ignored.
state.key = "";
@@ -209,10 +210,14 @@ fn noteGatewayUsage(io: Io, tally: *pricing.CostTally, arena: Allocator, raw: []
}
fn fetch(ctx: ToolCtx, arena: Allocator, body: []const u8) ![]const u8 {
+ return fetchWithin(ctx.gpa, ctx.io, ctx.client, arena, body, 10_000);
+}
+
+fn fetchWithin(gpa: Allocator, io: Io, client: *std.http.Client, arena: Allocator, body: []const u8, timeout_ms: u32) ![]const u8 {
const bearer = try gatewayBearer(arena, state.key);
var completed = false;
- defer if (!completed) pricing.g_cost.failedWithoutUsage(ctx.io, 1);
- const res = try buffered_https.post(ctx.gpa, arena, ctx.io, ctx.client, endpoint, bearer, body, 10_000);
+ defer if (!completed) pricing.g_cost.failedWithoutUsage(io, 1);
+ const res = try buffered_https.post(gpa, arena, io, client, endpoint, bearer, body, timeout_ms);
switch (res.status) {
200 => {},
401, 403 => return error.JevUnauthorized,
@@ -220,11 +225,46 @@ fn fetch(ctx: ToolCtx, arena: Allocator, body: []const u8) ![]const u8 {
429 => return error.JevRateLimited,
else => return error.JevUnavailable,
}
- noteGatewayUsage(ctx.io, &pricing.g_cost, arena, res.body);
+ noteGatewayUsage(io, &pricing.g_cost, arena, res.body);
completed = true;
return res.body;
}
+/// ADR 0246: the turn-level selection jev_auto.zig makes before a root turn's
+/// first request. Same login, circuit and billing as the tool; the summary is
+/// built locally and redacted. Null when Jev is unavailable or fails, which
+/// opens the circuit like a failed tool call.
+pub fn chooseForTurn(gpa: Allocator, io: Io, client: *std.http.Client, provider: Provider, task: []const u8, timeout_ms: u32) ?ReasoningEffort {
+ if (!available(provider) or task.len == 0) return null;
+ var temp = std.heap.ArenaAllocator.init(gpa);
+ defer temp.deinit();
+ const arena = temp.allocator();
+ var obj: std.json.ObjectMap = .empty;
+ obj.put(arena, "task", .{ .string = task[0..@min(task.len, 512)] }) catch return null;
+ const body = makeBody(arena, .{ .object = obj }, provider) catch return null;
+ state.mu.lockUncancelable(io);
+ defer state.mu.unlock(io);
+ if (state.key.len == 0 or state.down.load(.acquire)) return null;
+ _ = state.attempts.fetchAdd(1, .acq_rel);
+ const raw = switch (state.backend) {
+ .mock => mockResponse(arena),
+ .mock_fail => error.JevUnavailable,
+ .gateway => fetchWithin(gpa, io, client, arena, body, timeout_ms),
+ } catch |err| {
+ // A turn that ended first cancels its pick; that is not Jev failing.
+ if (!std.mem.eql(u8, @errorName(err), "Canceled")) {
+ state.down.store(true, .release);
+ state.refresh.store(true, .release);
+ }
+ return null;
+ };
+ return selectedEffort(arena, provider, raw) catch {
+ state.down.store(true, .release);
+ state.refresh.store(true, .release);
+ return null;
+ };
+}
+
fn selectedEffort(arena: Allocator, provider: Provider, raw: []const u8) !ReasoningEffort {
const parsed = try std.json.parseFromSliceLeaky(Value, arena, raw, .{ .allocate = .alloc_always });
if (parsed != .object) return error.InvalidResponse;
diff --git a/src/main.zig b/src/main.zig
index 08495108..6600cb72 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -593,7 +593,7 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu
_ = .{ @import("acp_agents.zig"), @import("repo_context_tests.zig") };
_ = .{ @import("subagent_activity.zig"), @import("subagent_interactive.zig"), @import("subagent_recovery.zig"), @import("subagent_retained.zig"), @import("subagent_run_tests.zig") };
_ = .{ @import("acp_agent_activity.zig"), @import("acp_subagent_live.zig"), @import("acp_line_writer.zig") };
- _ = .{ @import("acp_replay.zig"), @import("python_version.zig") };
+ _ = .{ @import("acp_replay.zig"), @import("python_version.zig"), @import("jev_auto.zig") };
_ = @import("read_image.zig");
_ = .{ @import("pr_local_checks.zig"), @import("mcp_names.zig"), @import("workspace_history.zig"), @import("file_worktree.zig"), @import("mcp_turn_context.zig"), @import("review_deadline.zig"), @import("issue_cmd.zig"), @import("shell_tool.zig"), @import("acp_ask.zig"), @import("side_steer.zig"), @import("task_workspace_tests.zig"), @import("workspace_prepare_tests.zig"), @import("worktree_reap.zig") };
}