Skip to content

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

Python

feat(routing,learning): search roles ride the small rung; landed turns feed the fold

justrach7ff89a9384a8
diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonl
index 62364c1d..7ff000f7 100644
--- a/evals/harness_behavior.jsonl
+++ b/evals/harness_behavior.jsonl
@@ -35,3 +35,4 @@
{"id": "orch-two-obs-cell-stays-bootstrap", "why": "min_arm_obs: a candidate arm with min_arm_obs-1 (=2) observations must NOT override - the base R0 arm is folded (n=3) but the winning R2 arm is one row short, so the decision stays source:\"bootstrap\" R0. Adding one more R2 row flips it to a learned R2 fleet (verified), so this is exactly the threshold. A scripted seed-presence probe keeps this case loudly RED until the workspace_seed runner extension exists - without it an empty archive declines identically and the case would otherwise pass while guarding nothing; once the seed lands it locks the n-gate.", "args": [], "env": {"GRAFF_FLEET": "on"}, "workspace_seed": {".graff/trajectories/seed.jsonl": "{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"lmstudio\", \"arm\": \"R0\", \"source\": \"bootstrap\", \"score\": 0.5, \"calls_used\": 6, \"exhausted\": false, \"landed\": true, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"lmstudio\", \"arm\": \"R0\", \"source\": \"bootstrap\", \"score\": 0.5, \"calls_used\": 7, \"exhausted\": false, \"landed\": true, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"lmstudio\", \"arm\": \"R0\", \"source\": \"bootstrap\", \"score\": 0.5, \"calls_used\": 8, \"exhausted\": false, \"landed\": true, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"lmstudio\", \"arm\": \"R2\", \"source\": \"bootstrap\", \"score\": 1.0, \"calls_used\": 12, \"exhausted\": false, \"landed\": true, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"lmstudio\", \"arm\": \"R2\", \"source\": \"bootstrap\", \"score\": 1.0, \"calls_used\": 13, \"exhausted\": false, \"landed\": true, \"variant_free\": true}\n"}, "timeout_s": 90, "prompt": "fix the regression in mathutil.py", "script": [{"tool": "workflow", "arguments": {"phases": [{"title": "find the defect", "tasks": [{"description": "find", "prompt": "read mathutil.py and locate the regression"}]}, {"title": "implement the fix", "tasks": [{"description": "fix", "prompt": "fix the regression in mathutil.py"}]}]}}, {"tool": "bash", "arguments": {"command": "test -s .graff/trajectories/seed.jsonl && echo seed-present"}}, {"tool": "bash", "arguments": {"command": "grep -h '\"kind\":\"orch\"' .graff/trajectories/*.jsonl | grep '\"task_class\":\"bugfix\"' | grep '\"source\":\"bootstrap\"' | grep -q '\"arm\":\"R0\"' && echo sparse-cell-stayed-bootstrap"}}, {"text": "two observations are not evidence; the hand ladder decided"}], "assert": [{"event": {"type": "tool_result", "name": "workflow", "is_error": false}}, {"request_contains": {"index": 1, "text": "(escalation R0):"}}, {"event": {"type": "tool_result", "name": "bash", "text": "seed-present\n"}}, {"event": {"type": "tool_result", "name": "bash", "text": "sparse-cell-stayed-bootstrap\n"}}, {"final_text_contains": "the hand ladder decided"}]}
{"id": "orch-explicit-and-foreign-stratum-never-fold", "why": "#290 fold side, both firewalls at once: seeded R2 rows that would win the cell outright are marked source:\"explicit\" (user intent) or carry stratum gpt-5.6-sol (different model), so neither may fold into the (bugfix, unlimited, lmstudio) ask - the decision must stay bootstrap R0. Relabeling the explicit rows bootstrap flips it to a learned R2 fleet (verified), so each firewall is individually load-bearing. A scripted seed-presence probe keeps this case loudly RED until the workspace_seed runner extension exists - without it an empty archive declines identically and the case would otherwise pass while guarding nothing; once the seed lands it locks both firewalls.", "args": [], "env": {"GRAFF_FLEET": "on"}, "workspace_seed": {".graff/trajectories/seed.jsonl": "{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"lmstudio\", \"arm\": \"R0\", \"source\": \"bootstrap\", \"score\": 0.0, \"calls_used\": 6, \"exhausted\": false, \"landed\": false, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"lmstudio\", \"arm\": \"R0\", \"source\": \"bootstrap\", \"score\": 0.0, \"calls_used\": 7, \"exhausted\": false, \"landed\": false, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"lmstudio\", \"arm\": \"R0\", \"source\": \"bootstrap\", \"score\": 0.0, \"calls_used\": 8, \"exhausted\": false, \"landed\": false, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"lmstudio\", \"arm\": \"R2\", \"source\": \"explicit\", \"score\": 1.0, \"calls_used\": 12, \"exhausted\": false, \"landed\": true, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"lmstudio\", \"arm\": \"R2\", \"source\": \"explicit\", \"score\": 1.0, \"calls_used\": 13, \"exhausted\": false, \"landed\": true, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"lmstudio\", \"arm\": \"R2\", \"source\": \"explicit\", \"score\": 1.0, \"calls_used\": 14, \"exhausted\": false, \"landed\": true, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"gpt-5.6-sol\", \"arm\": \"R2\", \"source\": \"bootstrap\", \"score\": 1.0, \"calls_used\": 12, \"exhausted\": false, \"landed\": true, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"gpt-5.6-sol\", \"arm\": \"R2\", \"source\": \"bootstrap\", \"score\": 1.0, \"calls_used\": 13, \"exhausted\": false, \"landed\": true, \"variant_free\": true}\n{\"kind\": \"orch_outcome\", \"task_class\": \"bugfix\", \"budget_band\": \"unlimited\", \"stratum\": \"gpt-5.6-sol\", \"arm\": \"R2\", \"source\": \"bootstrap\", \"score\": 1.0, \"calls_used\": 14, \"exhausted\": false, \"landed\": true, \"variant_free\": true}\n"}, "timeout_s": 90, "prompt": "fix the crash in payments.py", "script": [{"tool": "workflow", "arguments": {"phases": [{"title": "find the defect", "tasks": [{"description": "find", "prompt": "read payments.py and locate the crash"}]}, {"title": "implement the fix", "tasks": [{"description": "fix", "prompt": "fix the crash in payments.py"}]}]}}, {"tool": "bash", "arguments": {"command": "test -s .graff/trajectories/seed.jsonl && echo seed-present"}}, {"tool": "bash", "arguments": {"command": "grep -h '\"kind\":\"orch\"' .graff/trajectories/*.jsonl | grep '\"task_class\":\"bugfix\"' | grep '\"source\":\"bootstrap\"' | grep -q '\"arm\":\"R0\"' && echo firewalled-rows-did-not-fold"}}, {"text": "user intent and foreign strata stayed out of the cell"}], "assert": [{"event": {"type": "tool_result", "name": "workflow", "is_error": false}}, {"request_contains": {"index": 1, "text": "(escalation R0):"}}, {"event": {"type": "tool_result", "name": "bash", "text": "seed-present\n"}}, {"event": {"type": "tool_result", "name": "bash", "text": "firewalled-rows-did-not-fold\n"}}, {"final_text_contains": "stayed out of the cell"}]}
{"id": "citation-free-report-not-rejected", "why": "The false-positive axis of anchorCheck: a report-slot worker that honestly cites nothing must pass in one call - no anchor rejection, no burnt phase retry. The scan once counted the harness's own appended inspect link (.graff/subagents/<id>.md) as a fabricated citation, auto-rejecting every citation-free report; this locks the fix (report_anchors.scan skips .graff/ paths).", "args": [], "prompt": "investigate whether any throttle wiring exists in this project", "script": [{"tool": "workflow", "arguments": {"phases": [{"title": "sweep the project for throttle wiring", "tasks": [{"description": "scout", "prompt": "search for throttle wiring and report what exists"}]}]}}, {"text": "No throttle wiring exists anywhere in this project; there is nothing to cite."}, {"text": "confirmed: no throttle wiring exists"}], "timeout_s": 90, "assert": [{"event": {"type": "tool_result", "name": "workflow", "is_error": false}}, {"events_at_most": {"match": {"type": "agent_usage"}, "count": 1}}, {"request_lacks": {"index": 2, "text": "Anchor every finding to real code"}}, {"request_contains": {"index": -1, "text": "1/1 ok, 0 retried"}}, {"final_text_contains": "confirmed: no throttle wiring exists"}]}
+{"id": "orch-landed-turn-files-outcome-row", "why": "The landed-turn funnel: before it, flushPending fired only from the eval funnel and exhaustedFatal, so the learned rung policy never saw an ordinary success - blind to exactly the outcome it optimizes (a rung that lands within budget). A no-eval turn that admits a workflow decision must file its kind:\"orch_outcome\" row when the turn lands, so the next boot's fold has evidence from everyday runs, not just scored ones and deaths.", "env": {"GRAFF_FLEET": "on"}, "prompt": "fix the bug in stats.py", "script": [{"tool": "workflow", "arguments": {"phases": [{"title": "find the defect", "tasks": [{"description": "find", "prompt": "read stats.py and locate the defect"}]}, {"title": "implement the fix", "tasks": [{"description": "fix", "prompt": "fix the defect in stats.py"}]}]}}, {"text": "declined solo - fixed it myself"}, {"tool": "bash", "arguments": {"command": "grep -h '\"kind\":\"orch_outcome\"' .graff/trajectories/*.jsonl | grep '\"arm\":\"R0\"' | grep -q '\"source\":\"bootstrap\"' && echo outcome-row-filed"}}, {"text": "the learning layer recorded the landing"}], "second_prompt": "did the learning layer record that?", "assert": [{"event": {"type": "tool_result", "name": "workflow", "is_error": false}}, {"request_contains": {"index": 1, "text": "(escalation R0):"}}, {"event": {"type": "tool_result", "name": "bash", "text": "outcome-row-filed\n"}}, {"final_text_contains": "recorded the landing"}]}
diff --git a/src/providers.zig b/src/providers.zig
index a25097ed..b2588f49 100644
--- a/src/providers.zig
+++ b/src/providers.zig
@@ -27,6 +27,7 @@ const ansi = @import("ansi.zig");
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]");
@@ -233,6 +234,13 @@ pub fn runTurnWithFallback(root: *Agent, keys: *Keys, arena: Allocator, out: ?*I
// 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.
diff --git a/src/route_phase.zig b/src/route_phase.zig
index 4a1e6e57..0f51c487 100644
--- a/src/route_phase.zig
+++ b/src/route_phase.zig
@@ -76,6 +76,9 @@ pub const Seat = struct {
/// the seat could not see one — a capability requirement, not a measured
/// preference, so it must not be reported as `learned-policy`.
vision: bool = false,
+ /// `pin` was set by the search-role prior (a hand rule, not evidence),
+ /// so the source must stay the ladder's, not claim `learned-policy`.
+ role_prior: bool = false,
/// The (shape, role) cell one worker files its score under. Identical for
/// every worker of a routed phase by construction: `uniformRole` only
@@ -90,11 +93,19 @@ pub const Seat = struct {
/// is what keeps the trace honest about a phase-level choice.
pub fn sourceFor(self: Seat, has_override: bool) Source {
if (self.vision) return .vision_ask; // #380 — a capability, not a preference
- if (self.pin != null) return .learned_policy;
+ if (self.pin != null) return if (self.role_prior) self.base_source else .learned_policy;
return if (has_override) .workflow_override else self.base_source;
}
};
+/// The roles whose work is reading and reporting — the search half of every
+/// shape. Their whole benefit is a summary, so the role prior seats them at
+/// the ladder's SMALL rung; verify/review/implement/synthesize keep the
+/// session rung (their work is judged or lands edits).
+fn searchRole(role: []const u8) bool {
+ return std.mem.eql(u8, role, "find") or std.mem.eql(u8, role, "sweep");
+}
+
/// The one role a whole phase can be routed under: the phase title's own
/// canonical slot when it names one (policy.roleOf then reports it for every
/// task regardless of niche), else the slot every task's niche agrees on,
@@ -135,7 +146,22 @@ pub fn forPhase(
// The baseline a challenger must dominate is the model this phase would
// otherwise have run — so the comparison is against reality, not against
// a rung nobody was going to use.
- const learned = policy.learnedRung(base.id, .{ .shape = shape, .role = seat.role }, base.model) orelse return seat;
+ const learned = policy.learnedRung(base.id, .{ .shape = shape, .role = seat.role }, base.model) orelse {
+ // No lived evidence for this cell. Search roles then ride the small
+ // rung by default ("different things get different things") — their
+ // whole product is a summary. resolveIn re-runs the tier chain
+ // (catalog check, cost ceiling), so the worst case is today's answer;
+ // once real evidence lands in the cell, it decides instead.
+ if (searchRole(seat.role)) {
+ const r = pin_mod.resolveIn(base, .{ .tier = .small }, .{ .shape = shape, .role = seat.role });
+ if (r.provider) |p| if (!std.mem.eql(u8, p.model, seat.provider.model)) {
+ seat.pin = p;
+ seat.provider = p;
+ seat.role_prior = true;
+ };
+ }
+ return seat;
+ };
// Re-clear the cost ceiling for the swapped model: learning may descend
// price, never escalate it, and a swap that somehow fails the ceiling is
// dropped rather than failing the phase.
diff --git a/src/route_phase_tests.zig b/src/route_phase_tests.zig
index 28c6c4d5..06341d0c 100644
--- a/src/route_phase_tests.zig
+++ b/src/route_phase_tests.zig
@@ -74,6 +74,36 @@ const Env = struct {
/// niche is the persona name (or the phase title for an inline variant).
const sweep_niches = [_][]const u8{ "researcher", "skeptic" };
+test "role prior: search roles ride the small rung; judged roles keep the session rung" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ // NO lived evidence: an empty archive, so any move is the prior alone.
+ const saved = Env.install(a, "", true);
+ defer saved.restore();
+
+ const find = phase.forPhase(codex("gpt-5.6-terra"), .review, "find the defects", &.{}, true);
+ try std.testing.expectEqualStrings("gpt-5.6-luna", find.provider.model);
+ try std.testing.expect(find.pin != null);
+ try std.testing.expect(find.role_prior);
+ // The source stays the ladder's — a hand rule must not claim evidence.
+ try std.testing.expectEqual(policy.Source.ladder, find.sourceFor(false));
+
+ const sweep = phase.forPhase(codex("gpt-5.6-terra"), .research, "sweep the sources", &.{}, true);
+ try std.testing.expectEqualStrings("gpt-5.6-luna", sweep.provider.model);
+
+ const verify = phase.forPhase(codex("gpt-5.6-terra"), .review, "verify each finding", &.{}, true);
+ try std.testing.expect(verify.pin == null);
+ try std.testing.expectEqualStrings("gpt-5.6-terra", verify.provider.model);
+
+ // A user-chosen seat is never moved by the prior, same as #372's rule.
+ const pinned = Env.install(a, "", false);
+ defer pinned.restore();
+ const kept = phase.forPhase(codex("gpt-5.6-terra"), .review, "find the defects", &.{}, true);
+ try std.testing.expect(kept.pin == null);
+ try std.testing.expectEqualStrings("gpt-5.6-terra", kept.provider.model);
+}
+
test "#376 uniformRole: a phase routes only when all of its workers agree on one role" {
// The phase title's own canonical slot is the role every worker reports
// (policy.roleOf reads the title first), whatever their niches are.
@@ -242,11 +272,16 @@ test "#376: an uncelled phase, and a bootstrap install, keep today's behavior ex
try std.testing.expectEqualStrings("gpt-5.6-terra", seat.provider.model);
}
{
- // An install with no archive at all: zero cells, nothing to say.
+ // An install with no archive at all: zero cells — the learned layer
+ // says nothing, so the search-role PRIOR seats sweep at the small
+ // rung. Still reported as the ladder's answer: a hand rule, not
+ // evidence, and never a claim of `learned-policy`.
const env = Env.install(a, "", true);
defer env.restore();
const seat = phase.forPhase(base, .research, "sweep the repo", &sweep_niches, true);
- try std.testing.expect(seat.pin == null);
+ try std.testing.expect(seat.pin != null);
+ try std.testing.expectEqualStrings("gpt-5.6-luna", seat.provider.model);
+ try std.testing.expect(seat.role_prior);
try std.testing.expectEqual(policy.Source.ladder, seat.sourceFor(false));
}
}