graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Merge #688 into release/v0.0.281: Pareto product (nudge, catalog, TTFT)
Cursor Agentbc2d2fe3f47b
diff --cc src/agent.zigindex 47a5bdb1,dc734828..8f681bc7--- a/src/agent.zig+++ b/src/agent.zig@@@ -261,17 -262,14 +262,15 @@@ pub const Agent = structreturn schema.providerTakesEffort(self.provider.kind, self.provider.id, self.provider.model);}- /// Whether this turn should put reasoning_effort on the wire. Gemini 3.x- /// maps that field to thinking_level; the default medium makes every- /// turn think for seconds (2× wall vs Pi, which omits it). /effort still- /// applies — we only skip the unset default.+ /// Whether this turn should put reasoning_effort on the wire.+ /// Flash / Gemini send `low` for the unset default (ADR 0046) — omitting+ /// the field still bills reasoning_tokens. /effort still applies.pub fn sendReasoningEffort(self: *const Agent) bool {-- if (!self.effortApplies() or self.effort_rejected) return false;- if (std.mem.startsWith(u8, self.provider.model, "gemini") and self.reasoning == .medium) return false;-- return true;++ return self.effortApplies() and !self.effort_rejected;+ }+- /// Every agent streams — root even on `-p`, subagents too (#682): the stall-watched path, never the 5-minute POST. `out`/`stream_quiet` only mute paint.+ pub fn usesLiveTransport(_: *const Agent) bool {- return true;++ return true; // #682: stall-watched path; paint mute is out/stream_quiet}pub fn toolsJson(self: *const Agent) []const u8 {@@@ -330,6 -328,8 +329,7 @@@try self.ensureRootTools(self.provider.kind);// No per-turn teardown: the socket and the chain span user turns, guarded by codex_chain.usable instead.self.completed = null;- self.named_work_nudges = 0;- named_work.rememberFrom(self);++ @import("named_work.zig").beginTurn(self);if (!self.sub and !root_turn_prepared.swap(false, .acq_rel)) esc_cancel.store(false, .release);while (true) {if (try @import("turn_chrome.zig").beforeRequest(self)) |paused| return paused;@@@ -393,6 -393,7 +393,7 @@@// A completion with no text and no tool calls used to end the turn// silently — mid-task that just looks like a dead session.if (try empty_completion.handle(self, final_text, hist_len)) continue;- if (try named_work.handle(self, final_text)) continue;++ if (try @import("named_work.zig").handle(self, final_text)) continue;return final_text;}self.empty_completion_retries = 0;diff --cc src/main.zigindex e6f5c174,e6f5c174..158f7966--- a/src/main.zig+++ b/src/main.zig@@@ -122,7 -122,7 +122,7 @@@ test { // unit_tests' root is main.zig_ = @import("obs_cost_test.zig");_ = @import("tui_launch.zig");_ = provider_mod;-- _ = agent_mod;++ _ = @import("named_work.zig");}const prompts = @import("prompts.zig");const schema = @import("schema.zig");diff --cc src/named_work.zigindex 00000000,3cf65957..762ddaa7mode 000000,100644..100644--- a/src/named_work.zig+++ b/src/named_work.zig@@@ -1,0 -1,220 +1,225 @@@+ //! One bounded nudge when the user named a source file and the model+ //! answered without touching the tree. The in-house 5/6 miss was this shape:+ //! one call, "I'll read SPEC.md…", no edit, tests still fail.+ //!+ //! Shared by `-p` and the REPL (no oneshot-only skip). At most one extra+ //! model call, and only when tools_used is still empty.++ const std = @import("std");+ const Value = std.json.Value;++ const Agent = @import("agent.zig").Agent;+ const messages_mod = @import("messages.zig");++ pub const max_nudges: u8 = 1;++ pub const nudge_text =+ "You named a source file and have not used a tool. Read the named path " +++ "and edit it before answering — do not describe a fix you have not applied.";++ /// The current turn's user text. Responses history often drops `role=user`+ /// after the first step, so `handle` must not depend on walking messages.+ var remembered_task: []const u8 = "";++ const source_needles = [_][]const u8{ ".py", ".zig", ".js", ".ts", "SPEC.md" };++ pub fn remember(text: []const u8) void {+ remembered_task = text;+ }++ fn lastUserText(self: *Agent) []const u8 {+ var i = self.messages.items.len;+ while (i > 0) {+ i -= 1;+ const msg = self.messages.items[i];+ if (msg != .object) continue;+ const role = msg.object.get("role") orelse continue;+ if (role != .string or !std.mem.eql(u8, role.string, "user")) continue;+ const content = msg.object.get("content") orelse continue;+ if (content == .string) return content.string;+ }+ return "";+ }++ /// Snapshot the user prompt at turn start, while `role=user` still exists.+ /// Does not clear a `-p` remember() if history has no user string yet.+ pub fn rememberFrom(self: *Agent) void {+ const t = lastUserText(self);+ if (t.len > 0) remember(t);+ }+++pub fn beginTurn(self: *Agent) void {++ self.named_work_nudges = 0;++ rememberFrom(self);++}+++ /// True when `text` names a source path (not a greeting.txt-style data file).+ pub fn hasNamedSource(text: []const u8) bool {+ for (source_needles) |n| {+ if (std.mem.indexOf(u8, text, n) != null) return true;+ }+ return false;+ }++ pub fn shouldNudge(tools_used: u64, nudges: u8, prompt: []const u8) bool {+ if (nudges >= max_nudges) return false;+ if (tools_used != 0) return false;+ return hasNamedSource(prompt);+ }++ fn valueNamesSource(v: Value) bool {+ switch (v) {+ .string => |s| return hasNamedSource(s),+ .object => |o| {+ var it = o.iterator();+ while (it.next()) |e| {+ if (valueNamesSource(e.value_ptr.*)) return true;+ }+ return false;+ },+ .array => |a| {+ for (a.items) |item| {+ if (valueNamesSource(item)) return true;+ }+ return false;+ },+ else => return false,+ }+ }++ /// Walk every string in history. Responses items often have no `role=user`+ /// / `content` string, so looking at only the last user message misses the+ /// named path and the nudge never fires.+ pub fn conversationNamesSource(messages: []const Value) bool {+ for (messages) |m| {+ if (valueNamesSource(m)) return true;+ }+ return false;+ }++ /// Responses `input` item. A Chat `{role, content: string}` after a+ /// `function_call` / `output_text` item is dropped or 400s on the+ /// `previous_response_id` delta (and on a full resend mixed with those items).+ pub fn responsesUserMessage(arena: std.mem.Allocator, text: []const u8) !Value {+ var block: std.json.ObjectMap = .empty;+ try block.put(arena, "type", .{ .string = "input_text" });+ try block.put(arena, "text", .{ .string = try arena.dupe(u8, text) });+ var content: std.json.Array = .init(arena);+ try content.append(.{ .object = block });+ var msg: std.json.ObjectMap = .empty;+ try msg.put(arena, "type", .{ .string = "message" });+ try msg.put(arena, "role", .{ .string = "user" });+ try msg.put(arena, "content", .{ .array = content });+ return .{ .object = msg };+ }++ pub fn userNudge(arena: std.mem.Allocator, kind: @import("provider.zig").Provider.Kind, text: []const u8) !Value {+ return switch (kind) {+ .responses => responsesUserMessage(arena, text),+ .openai, .anthropic => messages_mod.textMessage(arena, "user", text),+ };+ }++ const isResponsesInputText = messages_mod.isResponsesInputText;++ /// Append the nudge and ask the caller to `continue` the turn loop.+ /// Re-anchor the Responses chain so the new item is not a dropped delta.+ pub fn handle(self: *Agent, _: []const u8) !bool {+ if (self.named_work_nudges >= max_nudges) return false;+ if (self.tools_used.count() != 0) return false;+ const named = hasNamedSource(remembered_task) or conversationNamesSource(self.messages.items);+ if (!named) return false;+ self.named_work_nudges += 1;+ self.closeCodexWs();+ try self.messages.append(try userNudge(self.arena, self.provider.kind, nudge_text));+ return true;+ }++ test "hasNamedSource sees SPEC and .py, ignores greeting.txt and pong" {+ try std.testing.expect(hasNamedSource("Read SPEC.md and affinity.py"));+ try std.testing.expect(hasNamedSource("Fix fib.py"));+ try std.testing.expect(hasNamedSource("edit stall_notice.py"));+ try std.testing.expect(!hasNamedSource("Reply with exactly: pong"));+ try std.testing.expect(!hasNamedSource("Create hello.txt then rename it"));+ }++ test "shouldNudge is once, and only when no tools have run" {+ try std.testing.expect(shouldNudge(0, 0, "Fix fib.py"));+ try std.testing.expect(!shouldNudge(1, 0, "Fix fib.py"));+ try std.testing.expect(!shouldNudge(0, 1, "Fix fib.py"));+ try std.testing.expect(!shouldNudge(0, 0, "Reply with exactly: pong"));+ }++ test "remembered task is enough to shouldNudge even without history walk" {+ remember("Fix stall_notice.py");+ defer remember("");+ try std.testing.expect(hasNamedSource(remembered_task));+ remember("Reply with exactly: pong");+ try std.testing.expect(!hasNamedSource(remembered_task));+ }++ test "conversationNamesSource sees Responses input_text, not role=user" {+ const raw =+ \\{"type":"message","content":[{"type":"input_text","text":"Fix atomic_write.py"}]}+ ;+ const parsed = try std.json.parseFromSlice(Value, std.testing.allocator, raw, .{});+ defer parsed.deinit();+ const msgs = [_]Value{parsed.value};+ try std.testing.expect(conversationNamesSource(&msgs));+ const pong = try std.json.parseFromSlice(Value, std.testing.allocator, "{\"role\":\"user\",\"content\":\"pong\"}", .{});+ defer pong.deinit();+ const pongs = [_]Value{pong.value};+ try std.testing.expect(!conversationNamesSource(&pongs));+ }++ test "userNudge is input_text on Responses and a content string on chat" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ const responses = try userNudge(a, .responses, nudge_text);+ try std.testing.expect(isResponsesInputText(responses, nudge_text));+ const chat = try userNudge(a, .openai, nudge_text);+ try std.testing.expectEqualStrings("user", chat.object.get("role").?.string);+ try std.testing.expectEqualStrings(nudge_text, chat.object.get("content").?.string);+ try std.testing.expect(chat.object.get("type") == null);+ }++ test "handle appends Responses input_text once and re-anchors the chain" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var msgs = std.json.Array.init(a);+ try msgs.append(try messages_mod.textMessage(a, "user", "Fix atomic_write.py"));+ var agent: Agent = .{+ .gpa = std.testing.allocator,+ .arena = a,+ .io = std.testing.io,+ .client = undefined,+ .provider = .{+ .id = "xai",+ .kind = .responses,+ .auth = .bearer,+ .url = "",+ .api_key = "k",+ .model = "grok-4.6",+ .context = 100_000,+ },+ .messages = msgs,+ .sub = false,+ .label = "test",+ .out = null,+ .codex_prev_id = try std.testing.allocator.dupe(u8, "resp_drop_me"),+ .codex_sent_upto = 1,+ };+ defer if (agent.codex_prev_id) |p| std.testing.allocator.free(p);+ remember("Fix atomic_write.py");+ defer remember("");+ try std.testing.expect(try handle(&agent, "I'll read SPEC.md"));+ try std.testing.expect(agent.codex_prev_id == null);+ try std.testing.expectEqual(@as(usize, 0), agent.codex_sent_upto);+ try std.testing.expect(isResponsesInputText(agent.messages.items[agent.messages.items.len - 1], nudge_text));+ messages_mod.normalizeResponsesHistory(a, &agent.messages);+ try std.testing.expect(isResponsesInputText(agent.messages.items[0], "Fix atomic_write.py"));+ try std.testing.expect(isResponsesInputText(agent.messages.items[agent.messages.items.len - 1], nudge_text));+ try std.testing.expect(!try handle(&agent, "I'll read SPEC.md"));+ }diff --cc src/session_start.zigindex 927d51cc,c6c50a39..49de5c9f--- a/src/session_start.zig+++ b/src/session_start.zig@@@ -37,7 -37,9 +37,8 @@@ const main_mod = @import("main.zig")const provider_mod = @import("provider.zig");const keys_cli = @import("keys_cli.zig");const pricing = @import("pricing.zig");-const skills = @import("skills.zig");const mcp = @import("mcp.zig");+ const mcp_boot = @import("mcp_boot.zig");const mcp_cli = @import("mcp_cli.zig");const mcp_config = @import("mcp_config.zig");const plugin_scan = @import("plugin_scan.zig");@@@ -451,17 -448,12 +452,22 @@@ pub fn initRegistryConsent(io: Io, gpasink.emit(io, dimNotice(try std.fmt.allocPrint(arena, "ignoring ~/" ++ mcp_config.unsupported_rel_path ++ ": unsupported path — use ~/" ++ mcp_config.global_rel_path ++ " (global) or {s} (project)", .{mcp_config_path})));}const mcp_count = mcp_cli.countMcpServers(merged);- const defer_join = flags.effectiveYolo() and flags.oneshot_prompt == null and !json_mode;+ const defer_join = mcp_boot.deferMcpJoin(flags.effectiveYolo(), json_mode);+ const skip_imported = mcp_boot.oneshotSkipsImportedMcp(+ flags.oneshot_prompt != null,+ leanMode(flags.effectiveLean(), environ_map),+ merged.project.count(),+ );+ // Name the next phase the way `plugins:` already does. Without this, a+ // handshake that still runs on this thread looks like a hung boot.+ if (!json_mode and flags.oneshot_prompt == null and mcp_count > 0) {+ const mcp_line = if (defer_join)+ try std.fmt.allocPrint(arena, "mcp: {d} server(s) in background", .{mcp_count})+ else+ try std.fmt.allocPrint(arena, "mcp: connecting {d} server(s)...", .{mcp_count});+ sink.emit(io, dimNotice(mcp_line));+ }+ const mcp_t0 = Io.Timestamp.now(io, .awake);// Lean folds schemas (deferAllRuntime); it does not skip connect.// --yolo / -p still connect so `.mcp.json` works on the default one-shot// (ADR 0029). Interactive without --yolo still asks.