Skip to content

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

Python

fix(budget,shapes): the landing reserve lands the answer; audits cell as review (#390)

justrachb8f17745c4ad
diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonl
index 4605ea35..f18ac3a6 100644
--- a/evals/harness_behavior.jsonl
+++ b/evals/harness_behavior.jsonl
@@ -11,3 +11,4 @@
{"id": "escalation-r0-declines-then-r0d-revision", "why": "escalation R0d: a 1-file bugfix must be DECLINED solo (the 0.0.237 study's 30-calls-no-edit regression), and the SECOND ask of the same class must get the R0d revision prescription — evidence-carrying sequential revision before any fleet. The regression is a fleet on first ask, or a flat second decline that never prescribes the revision.", "args": [], "prompt": "ultracode 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 by the ladder — I made the edit solo but the tests still fail"}, {"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": "revising with deeper analysis as prescribed"}], "second_prompt": "the fix did not take — pytest still fails in stats.py", "assert": [{"event": {"type": "tool_result", "name": "workflow", "is_error": false}}, {"request_contains": {"index": 1, "text": "(escalation R0):"}}, {"request_contains": {"index": 1, "text": "Do it yourself"}}, {"request_lacks": {"index": 1, "text": "(escalation R0d):"}}, {"request_contains": {"index": -1, "text": "(escalation R0d):"}}, {"request_contains": {"index": -1, "text": "revision case"}}, {"request_contains": {"index": -1, "text": "second failure earns the fleet"}}, {"final_text_contains": "revising with deeper analysis"}]}
{"id": "escalation-no-verifier-no-revision-loop", "why": "Weng's revision gate (failure_evidence.verifierFor): an `other`-class ask has no verifier — no diff contract, no eval, no citable anchors — so a repeat failure must NOT be prescribed a solo revision; R0d stays shut and with the fleet unaffordable the decline is the plain R0 advisory. The regression is a revision loop spun without external feedback.", "args": ["--max-model-calls", "8"], "prompt": "polish the onboarding copy in welcome.txt", "script": [{"tool": "workflow", "arguments": {"phases": [{"title": "find problems", "tasks": [{"description": "find", "prompt": "read welcome.txt and list the problems"}]}, {"title": "verify each finding", "tasks": [{"description": "verify", "prompt": "verify the problems in welcome.txt"}]}]}}, {"text": "polished it solo as told"}, {"tool": "workflow", "arguments": {"phases": [{"title": "find problems", "tasks": [{"description": "find", "prompt": "read welcome.txt and list the problems"}]}, {"title": "verify each finding", "tasks": [{"description": "verify", "prompt": "verify the problems in welcome.txt"}]}]}}, {"text": "continuing the polish myself"}], "second_prompt": "the onboarding copy in welcome.txt still reads badly — go again", "assert": [{"request_contains": {"index": 1, "text": "(escalation R0):"}}, {"request_contains": {"index": -1, "text": "(escalation R0):"}}, {"request_lacks": {"index": -1, "text": "(escalation R0d):"}}, {"request_lacks": {"index": -1, "text": "revision case"}}, {"final_text_contains": "continuing the polish myself"}]}
{"id": "review-repeat-failure-gets-anchor-revision", "why": "report_anchors gives review/research a real verifier (cited paths must resolve against the tree), so a review-class repeat failure now earns the R0d revision prescription instead of jumping to the judge fleet — the anchors are the external feedback Weng's gate demands.", "args": [], "prompt": "review the findings in report.py", "script": [{"tool": "workflow", "arguments": {"phases": [{"title": "find problems", "tasks": [{"description": "find", "prompt": "read report.py and list the problems"}]}, {"title": "verify each finding", "tasks": [{"description": "verify", "prompt": "verify the problems in report.py"}]}]}}, {"text": "reviewed solo as told"}, {"tool": "workflow", "arguments": {"phases": [{"title": "find problems", "tasks": [{"description": "find", "prompt": "read report.py and list the problems"}]}, {"title": "verify each finding", "tasks": [{"description": "verify", "prompt": "verify the problems in report.py"}]}]}}, {"text": "re-reviewing against the cited code as prescribed"}], "second_prompt": "that review missed real problems in report.py — go again", "assert": [{"request_contains": {"index": 1, "text": "(escalation R0):"}}, {"request_lacks": {"index": 1, "text": "(escalation R0d):"}}, {"request_contains": {"index": -1, "text": "(escalation R0d):"}}, {"request_contains": {"index": -1, "text": "revision case"}}, {"final_text_contains": "re-reviewing against the cited code"}]}
+{"id": "budget-final-call-lands-the-answer", "why": "#390: the pool's FINAL admitted model call belongs to the answer. The run's last request must offer no tools and carry the landing note, so the model answers from gathered evidence instead of asking for a tool the budget can never pay for — the audit-smoke run died exactly this way, exhausted mid-tool-loop with the work done and nothing narrated.", "args": ["--max-model-calls", "2"], "prompt": "keep reading the todo list forever", "script": [{"tool": "todo_read", "arguments": {}}, {"text": "landing: the todo list is empty; nothing else was verified"}], "assert": [{"request_contains": {"index": 0, "text": "\"todo_write\""}}, {"request_contains": {"index": -1, "text": "budget landing"}}, {"request_lacks": {"index": -1, "text": "\"todo_write\""}}, {"final_text_contains": "landing: the todo list is empty"}]}
diff --git a/src/agent_request.zig b/src/agent_request.zig
index 3f5de815..5fc7f3bf 100644
--- a/src/agent_request.zig
+++ b/src/agent_request.zig
@@ -26,6 +26,7 @@ const apiErrorMessage = tools_mod.apiErrorMessage;
const mentionsReasoningEffort = tools_mod.mentionsReasoningEffort;
const telemetry = @import("telemetry.zig");
const run_budget_mod = @import("run_budget.zig");
+const wire_messages = @import("messages.zig");
const policy = @import("agent_request_policy.zig");
const errorCode = policy.errorCode;
@@ -71,7 +72,15 @@ fn resetRequestScratch(scratch: *std.heap.ArenaAllocator) void {
}
}
-pub fn request(self: *Agent, tools: ?[]const u8) !std.json.ObjectMap {
+/// #390 — appended once, on the run's final admitted model call, right where
+/// the tools disappear, so the model knows WHY and lands instead of retrying.
+pub const landing_note =
+ "[budget landing] This is the final model call this run's --max-model-calls budget admits, so " ++
+ "no tools are offered. Land the answer NOW from the evidence already gathered: state what was " ++
+ "completed and what was verified, then name what remains unchecked. Honestly-labeled partial " ++
+ "results beat dying mid-tool-call.";
+
+pub fn request(self: *Agent, tools_in: ?[]const u8) !std.json.ObjectMap {
// Startup paints the prompt while CA loading continues. The root turn and
// title task rendezvous here, then issue their requests concurrently.
http.waitForClientReady(self.io);
@@ -89,6 +98,19 @@ pub fn request(self: *Agent, tools: ?[]const u8) !std.json.ObjectMap {
};
}
defer if (budget_permit) |*permit| permit.release();
+ // #390 — the landing reserve's root half: the pool's FINAL admitted call
+ // belongs to the answer. Offer no tools (compaction's trick) and say why,
+ // so the model lands a text answer now instead of asking for a tool the
+ // budget can never pay for — which is how the audit smoke died narrating.
+ // compaction/title requests pass tools=null already and skip this whole.
+ var tools = tools_in;
+ if (self.run_budget) |b| if (budget_permit) |p| {
+ if (b.max_model_calls != 0 and p.call_number == b.max_model_calls and tools != null) {
+ tools = null;
+ try self.messages.append(try wire_messages.textMessage(self.arena, "user", landing_note));
+ if (self.tracer) |tr| tr.note("budget", "final call: tools withheld so the run lands its answer (#390)");
+ }
+ };
self.last_request_context_overflow = false;
self.last_request_write_failed = false;
self.last_usage_includes_output = false;
diff --git a/src/escalation_tests.zig b/src/escalation_tests.zig
index c0af4841..3d77d78f 100644
--- a/src/escalation_tests.zig
+++ b/src/escalation_tests.zig
@@ -457,6 +457,12 @@ test "shapes.classOf / isAuditClass: the study's five prompts land where the lad
// The qualified forms still read as defects.
try std.testing.expectEqual(shapes.TaskClass.bugfix, shapes.classOf("the parser is broken on empty input"));
try std.testing.expectEqual(shapes.TaskClass.bugfix, shapes.classOf("two broken tests in the suite"));
+ // Breadth language outranks the noun it audits FOR: "for bugs" must not
+ // file an audit fleet's outcome under the bugfix learning cell (the
+ // mis-cell the branch's first real-model smoke caught).
+ try std.testing.expectEqual(shapes.TaskClass.review, shapes.classOf(
+ "thoroughly audit every file in this repo for bugs and robustness problems",
+ ));
// Audit language is narrow on purpose: a fleet takes an explicit word.
try std.testing.expect(shapes.isAuditClass("thoroughly audit the auth layer"));
try std.testing.expect(shapes.isAuditClass("check every file for this pattern"));
diff --git a/src/run_budget.zig b/src/run_budget.zig
index c6a4fbc2..6b3f913a 100644
--- a/src/run_budget.zig
+++ b/src/run_budget.zig
@@ -88,6 +88,14 @@ pub const RunBudget = struct {
/// a new call from the same shared ceiling.
pub fn acquire(self: *RunBudget, io: Io, depth: u8, kind: CallKind) !Permit {
if (depth > self.max_depth) return error.AgentDepthExceeded;
+ // #390 — the landing reserve's hard half: a CHILD may not take the
+ // pool's last slot. The final call is the root's landing answer; a
+ // worker that consumed it would leave the run to die narrating.
+ // Racy against a concurrent sibling (remaining() is a plain load),
+ // so it is a strong bias rather than a lock — the root's own final
+ // call cannot race itself, which is the case that matters.
+ if (depth > 0 and self.max_model_calls != 0 and self.remaining() <= 1)
+ return error.RunBudgetExhausted;
try self.acquireConcurrency(io);
errdefer {
const before = self.active.fetchSub(1, .release);
@@ -132,8 +140,8 @@ pub const RunBudget = struct {
}
};
-test "RunBudget enforces depth, call ceiling, and releases concurrency" {
- var budget: RunBudget = .{ .max_model_calls = 2, .max_concurrency = 1, .max_depth = 1 };
+test "RunBudget enforces depth, call ceiling, and reserves the last call for the root" {
+ var budget: RunBudget = .{ .max_model_calls = 3, .max_concurrency = 1, .max_depth = 1 };
var first = try budget.acquire(std.testing.io, 0, .root);
try std.testing.expectEqual(@as(u64, 1), first.call_number);
try std.testing.expectEqual(@as(u32, 1), budget.active.load(.acquire));
@@ -142,6 +150,11 @@ test "RunBudget enforces depth, call ceiling, and releases concurrency" {
var second = try budget.acquire(std.testing.io, 1, .child);
second.release();
+ // #390 — one slot left: a child is refused it, the root's landing takes it.
+ try std.testing.expectError(error.RunBudgetExhausted, budget.acquire(std.testing.io, 1, .child));
+ var last = try budget.acquire(std.testing.io, 0, .root);
+ try std.testing.expectEqual(@as(u64, 3), last.call_number);
+ last.release();
try std.testing.expectError(error.RunBudgetExhausted, budget.acquire(std.testing.io, 0, .root));
try std.testing.expectError(error.AgentDepthExceeded, budget.acquire(std.testing.io, 2, .child));
try std.testing.expectEqual(@as(u64, 0), budget.remaining());
diff --git a/src/shapes.zig b/src/shapes.zig
index d2d2f498..e7c073c3 100644
--- a/src/shapes.zig
+++ b/src/shapes.zig
@@ -326,6 +326,13 @@ fn anyOfCI(hay: []const u8, needles: []const []const u8) bool {
/// changed no files. Unrecognized asks are `other` rather than a guess: an
/// uncelled observation is honest, a mis-celled one poisons a policy cell.
pub fn classOf(raw: []const u8) TaskClass {
+ // Breadth language first: an audit-class ask is REVIEW work whatever
+ // nouns it mentions. "thoroughly audit every file … for bugs" matched
+ // the bare bugfix needle below and filed a fleet-sized audit under the
+ // bugfix learning cell — a mis-celled observation, caught by this
+ // branch's first real-model smoke. "fix the bug the review found" has no
+ // breadth word, so repair asks are untouched.
+ if (isAuditClass(raw)) return .review;
// "broken" is qualified rather than bare: an ordinary feature spec says
// "ties broken by ascending order", and the bare word classified the eval
// study's wordfreq FEATURE task as a bugfix — a mis-celled observation,