Skip to content

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

Python

fix(workflow): a 5xx outage is retryable again, and pipeline honors top-level context

Rach Pradhan8d89d94f94c6
diff --git a/src/subagent_run.zig b/src/subagent_run.zig
index 8b7e417c..64abe93e 100644
--- a/src/subagent_run.zig
+++ b/src/subagent_run.zig
@@ -74,10 +74,17 @@ pub fn classifyFailure(err: anyerror, detail: ?[]const u8) FailKind {
else => {},
}
const msg = detail orelse return .unknown;
+ // 5xx before everything: a server-side outage is the most common transient
+ // failure there is, and "503 Service Unavailable" used to fall through to
+ // the .model arm on the bare word "unavailable". That was harmless while
+ // this classifier was advisory prose; once failureAllowsRetry made it
+ // control flow it meant a whole fan-out stopped retrying an outage.
+ if (containsAnyCI(msg, &.{ "500", "502", "503", "504", "service unavailable", "temporarily unavailable", "bad gateway", "gateway timeout", "internal server error", "overloaded_error" })) return .transport;
if (containsAnyCI(msg, &.{ "network error", "stream stalled", "stream dropped", "timed out", "connection reset", "connection closed" })) return .transport;
if (containsAnyCI(msg, &.{ "rate limit", "rate_limit", "quota", "429", "overloaded", "out of credits", "credits", "billing", "too many requests" })) return .quota;
if (containsAnyCI(msg, &.{ "api key", "unauthorized", "expired", "authentication", "invalid_api_key", "401" })) return .auth;
- if (containsAnyCI(msg, &.{ "does not exist", "not found", "no such model", "decommission", "no longer", "model_not_found", "unavailable", "404" })) return .model;
+ // "unavailable" must stay qualified: bare, it swallows every 5xx above.
+ if (containsAnyCI(msg, &.{ "does not exist", "not found", "no such model", "decommission", "no longer", "model_not_found", "model unavailable", "model is unavailable", "404" })) return .model;
if (containsAnyCI(msg, &.{ "context length", "context window", "invalid", "unsupported", "not supported", "must be", "too long", "400", "parameter", "max_tokens", "bad request" })) return .invalid;
return .unknown;
}
diff --git a/src/workflow.zig b/src/workflow.zig
index 93eed100..27462252 100644
--- a/src/workflow.zig
+++ b/src/workflow.zig
@@ -217,7 +217,7 @@ pub fn pipelineIsolationError(stage_index: usize, iso: Isolation) ?[]const u8 {
/// Pipeline mode entry (#3): validate {items, stages}, then run one independent
/// chain per item concurrently (no barrier) and return the labeled final-stage
/// result for each item.
-fn runPipeline(ctx: ToolCtx, pv: Value) !ToolOutput {
+fn runPipeline(ctx: ToolCtx, pv: Value, outer_context: []const u8) !ToolOutput {
const gpa = ctx.gpa;
if (pv != .object) return .{ .text = try gpa.dupe(u8, "pipeline must be an object with items + stages"), .is_error = true };
const items_val = pv.object.get("items") orelse return .{ .text = try gpa.dupe(u8, "pipeline needs an items array"), .is_error = true };
@@ -227,9 +227,12 @@ fn runPipeline(ctx: ToolCtx, pv: Value) !ToolOutput {
const stage_vals = stages_val.array.items;
if (items.len == 0 or items.len > max_pipeline_items) return .{ .text = try std.fmt.allocPrint(gpa, "pipeline needs 1-{d} items", .{max_pipeline_items}), .is_error = true };
if (stage_vals.len == 0 or stage_vals.len > max_pipeline_stages) return .{ .text = try std.fmt.allocPrint(gpa, "pipeline needs 1-{d} stages", .{max_pipeline_stages}), .is_error = true };
- // U5 — shared context slot: one string, set once on the pipeline object,
- // prepended to every stage prompt below (before {{item}}/{{prev}}).
- const context_str: []const u8 = if (pv.object.get("context")) |cv| (if (cv == .string) cv.string else "") else "";
+ // Shared context slot: one string prepended to every stage prompt below
+ // (before {{item}}/{{prev}}). Readable in BOTH places the tool schema says
+ // it is - on the pipeline object, or top-level alongside it - because the
+ // dispatch to runPipeline happens before the top-level read, so a pipeline
+ // call silently ignored the documented top-level field.
+ const context_str: []const u8 = if (pv.object.get("context")) |cv| (if (cv == .string) cv.string else outer_context) else outer_context;
var arena_state = std.heap.ArenaAllocator.init(gpa);
defer arena_state.deinit();
@@ -350,7 +353,8 @@ pub fn execWorkflow(ctx: ToolCtx, input: Value) !ToolOutput {
.text = try gpa.dupe(u8, "workflow: arguments must be a JSON object with a \"phases\" array (or a \"pipeline\" object)"),
.is_error = true,
};
- if (obj.get("pipeline")) |pv| return runPipeline(ctx, pv);
+ const outer_context: []const u8 = if (obj.get("context")) |cv| (if (cv == .string) cv.string else "") else "";
+ if (obj.get("pipeline")) |pv| return runPipeline(ctx, pv, outer_context);
const phases_val = obj.get("phases") orelse return .{
.text = try gpa.dupe(u8, "workflow needs a \"phases\" array (or a \"pipeline\" object)"),
.is_error = true,
@@ -366,7 +370,7 @@ pub fn execWorkflow(ctx: ToolCtx, input: Value) !ToolOutput {
// U5 — shared context slot: one string, set once on the workflow object,
// prepended to every task prompt below (before {{prev}} substitution) so
// repo/task boilerplate is paid for once instead of per task.
- const context_str: []const u8 = if (obj.get("context")) |cv| (if (cv == .string) cv.string else "") else "";
+ const context_str = outer_context; // read once above, shared with the pipeline branch
// All intermediate strings live in this arena; the final result is
// duped into gpa for the caller.
diff --git a/src/workflow_test.zig b/src/workflow_test.zig
index 7c3da3c2..bff016b6 100644
--- a/src/workflow_test.zig
+++ b/src/workflow_test.zig
@@ -10,6 +10,7 @@ const Io = std.Io;
const Value = std.json.Value;
const workflow = @import("workflow.zig");
const subagent = @import("subagent.zig");
+const subagent_run = @import("subagent_run.zig");
const tools = @import("tools.zig");
const util = @import("util.zig");
const ToolCtx = tools.ToolCtx;
@@ -196,3 +197,15 @@ test "pipelineIsolationError: stage 0 may isolate, stage 1+ worktree is refused
try std.testing.expect(pipelineIsolationError(1, .shared_cwd) == null);
try std.testing.expect(pipelineIsolationError(4, .shared_cwd) == null);
}
+
+test "a 5xx outage is transient, so a fan-out keeps retrying it (U1 regression guard)" {
+ // The classifier was advisory prose until failureAllowsRetry made it control
+ // flow. Its .model arm matched the bare word "unavailable", so "503 Service
+ // Unavailable" - the single most common transient failure - classified as a
+ // dead model and stopped the whole phase retrying.
+ for ([_][]const u8{ "503 Service Unavailable", "502 Bad Gateway", "500 internal server error", "upstream temporarily unavailable" }) |detail|
+ try std.testing.expectEqual(subagent_run.FailKind.transport, subagent_run.classifyFailure(error.Unexpected, detail));
+ // A genuinely dead model still classifies as .model and still stops retrying.
+ for ([_][]const u8{ "model unavailable", "no such model", "model_not_found" }) |detail|
+ try std.testing.expectEqual(subagent_run.FailKind.model, subagent_run.classifyFailure(error.Unexpected, detail));
+}