graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(workflow): gate retry on the harness's own retry-safety verdict
Rach Pradhanf9259ed2a5cb
diff --git a/src/subagent_run.zig b/src/subagent_run.zigindex baa50b77..1e63280c 100644--- a/src/subagent_run.zig+++ b/src/subagent_run.zig@@ -82,13 +82,19 @@ pub fn classifyFailure(err: anyerror, detail: ?[]const u8) FailKind {return .unknown;}+/// Advisory text appended to a subagentFailure message when the classified+/// cause is transient (quota/transport/unknown) — the model sees this exact+/// string, and failureAllowsRetry below keys off it, so keep them in sync.+pub const retry_ok_note = "retry is likely safe — re-run the same task (after a short backoff for quota/transport)";+/// Advisory text appended when the cause is NOT transient (model/invalid/+/// auth) — retrying would just double the cost of a broken run for zero+/// chance of success. failureAllowsRetry keys off this exact string.+pub const retry_unsafe_note = "retry is NOT safe as-is — fix the cause first (switch model, correct arguments, or re-auth)";+fn subagentFailure(gpa: Allocator, sub_id: []const u8, err: anyerror, detail: ?[]const u8) ToolOutput {const kind = classifyFailure(err, detail);const cause = detail orelse @errorName(err);- const retry = if (kind.retrySafe())- "retry is likely safe — re-run the same task (after a short backoff for quota/transport)"- else- "retry is NOT safe as-is — fix the cause first (switch model, correct arguments, or re-auth)";+ const retry = if (kind.retrySafe()) retry_ok_note else retry_unsafe_note;const text = std.fmt.allocPrint(gpa,"subagent {s} failed before producing a report: {s} [{s} failure]. {s}",@@ -97,6 +103,18 @@ fn subagentFailure(gpa: Allocator, sub_id: []const u8, err: anyerror, detail: ?[return .{ .text = text, .is_error = true };}+/// Whether a workflow retry site should re-run a failed task. Failures that+/// went through subagentFailure carry retry_unsafe_note when the harness's+/// own classification (FailKind.retrySafe) already ruled retrying out — auth+/// or an invalid/unavailable model — so retrying the whole phase would only+/// double the cost of a broken run for zero chance of success. Any failure+/// text that did NOT come from subagentFailure (an empty report, an+/// isolation-setup failure) carries no marker and keeps retrying, since those+/// are exactly the transient cases retry exists for.+pub fn failureAllowsRetry(text: []const u8) bool {+ return std.mem.indexOf(u8, text, retry_unsafe_note) == null;+}+pub fn childProvider(root: Provider, pinned: ?Provider, allow_cross_provider: bool) Provider {const child = pinned orelse return root;return if (std.mem.eql(u8, child.id, root.id) or allow_cross_provider) child else root;@@ -327,3 +345,24 @@ test "agentUsageEvent maps AgentUsage fields onto the wire event" {try std.testing.expect(!failed.ok);try std.testing.expectEqual(@as(u64, 0), failed.duration_ms);}++test "failureAllowsRetry keys off the harness's own retry-safety classification" {+ const gpa = std.testing.allocator;++ // A real auth failure: subagentFailure classifies it .auth (retrySafe:+ // false) and appends retry_unsafe_note — the gate must block a retry.+ const auth = subagentFailure(gpa, "sa-001", error.Unexpected, "401 unauthorized: invalid_api_key");+ defer gpa.free(auth.text);+ try std.testing.expect(!failureAllowsRetry(auth.text));++ // A real transport/transient failure: classifies .transport (retrySafe:+ // true) and appends retry_ok_note — the gate must keep allowing retries.+ const transient = subagentFailure(gpa, "sa-002", error.StreamStalled, null);+ defer gpa.free(transient.text);+ try std.testing.expect(failureAllowsRetry(transient.text));++ // A failure text with no subagentFailure marker at all (e.g. the+ // empty-report path, or an isolation-setup failure) — no marker means no+ // classification was made, so it must keep retrying like before this fix.+ try std.testing.expect(failureAllowsRetry("subagent finished without a report"));+}diff --git a/src/workflow.zig b/src/workflow.zigindex e24056a9..1a2446a0 100644--- a/src/workflow.zig+++ b/src/workflow.zig@@ -27,6 +27,14 @@ const runSub = subagent.runSub;const scoreVariants = subagent.scoreVariants;const max_workflow_tasks = subagent.max_workflow_tasks;+// Sibling-imported directly (not through subagent.zig, which sits at the+// 600-line cap): the harness's own retry-safety verdict from subagentFailure,+// consumed at both retry sites below so a task the harness already knows+// can't be helped by a retry (auth, invalid args, an unavailable model)+// doesn't get retried anyway.+const subagent_run = @import("subagent_run.zig");+const failureAllowsRetry = subagent_run.failureAllowsRetry;+const fleet = @import("fleet.zig");const Isolation = fleet.Isolation; // #276 P0-1const telemetry = @import("telemetry.zig");@@ -121,8 +129,14 @@ fn pipelineChain(ctx: ToolCtx, item: []const u8, stages: []const StageSpec) Toolconst prompt = pipelinePrompt(arena, st.prompt, item, prev, stage_no) catch |e| return failure(gpa, e);var out = if (runSub(ctx, "workflow_task", st.label, prompt, st.override, st.niche, st.isolation, st.isolation_fallback)) |r| r.output else |e| failure(gpa, e);if (out.is_error) {- gpa.free(out.text);- out = if (runSub(ctx, "workflow_retry", st.label, prompt, st.override, st.niche, st.isolation, st.isolation_fallback)) |r| r.output else |e| failure(gpa, e);+ // Only spend the one retry (#2) when the harness's own+ // classification hasn't already ruled it out (auth, invalid+ // args, an unavailable model) — those stay failed below without+ // wasting a second attempt.+ if (failureAllowsRetry(out.text)) {+ gpa.free(out.text);+ out = if (runSub(ctx, "workflow_retry", st.label, prompt, st.override, st.niche, st.isolation, st.isolation_fallback)) |r| r.output else |e| failure(gpa, e);+ }if (out.is_error) {gpa.free(out.text);return .{@@ -322,9 +336,13 @@ pub fn execWorkflow(ctx: ToolCtx, input: Value) !ToolOutput {// for the next one. We can't reliably tell a transient failure from a// deterministic one, so we retry every failure exactly once: a permanent// failure just costs one more attempt and stays failed.+ // Failures the harness already classified as not retry-safe (auth, an+ // invalid argument, an unavailable model — see failureAllowsRetry) are+ // excluded here: retrying the whole phase would only double the cost+ // of a broken run for zero chance of success.var fidx: [max_workflow_tasks]usize = undefined;var nf: usize = 0;- for (outputs, 0..) |out, i| if (out.is_error) {+ for (outputs, 0..) |out, i| if (out.is_error and failureAllowsRetry(out.text)) {fidx[nf] = i;nf += 1;};