Skip to content

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

Python

fix(errors): rate-limit retry advice gets a human duration hint (#398)

Rach Pradhan75817a8a1255
diff --git a/src/agent.zig b/src/agent.zig
index 9b9f96ec..f70b60a4 100644
--- a/src/agent.zig
+++ b/src/agent.zig
@@ -244,10 +244,12 @@ pub const Agent = struct {
return fmt.len > 0 and fmt[fmt.len - 1] == '\n';
}
- /// Report an API error: remember the formatted message so the --json
- /// `error` event can carry the detail, then print it like say().
+ /// Report an API error: remember the formatted message for the --json
+ /// `error` event, then print it like say() plus a #398 duration hint;
+ /// last_api_error keeps the provider's words (classifiers match them).
pub fn sayApiError(self: *Agent, comptime fmt: []const u8, args: anytype) !void {
self.last_api_error = std.fmt.allocPrint(self.arena, fmt, args) catch null;
+ if (self.last_api_error) |m| if (@import("retry_hint.zig").humanizeRetrySeconds(m)) |h| return self.say("{s} (~{s})\n", .{ m, h.buf[0..h.len] });
try self.say(fmt ++ "\n", args);
}
diff --git a/src/retry_hint.zig b/src/retry_hint.zig
new file mode 100644
index 00000000..67b7ddea
--- /dev/null
+++ b/src/retry_hint.zig
@@ -0,0 +1,58 @@
+//! #398: annotate provider retry advice with a human duration. "Please try
+//! again in 350000 seconds" reads like a bug; "(~4d 1h)" reads like advice.
+//! Display-only by contract — callers append the hint and never rewrite the
+//! provider's words, because failure classifiers keyword-match the original
+//! text (see subagent classifyFailure).
+
+const std = @import("std");
+
+pub const Human = struct { buf: [24]u8, len: u8 };
+
+/// A compact human duration ("4d 1h", "5m 50s") when `msg` names a wait of at
+/// least a minute in seconds; null otherwise (sub-minute waits already read).
+pub fn humanizeRetrySeconds(msg: []const u8) ?Human {
+ const secs = findSeconds(msg) orelse return null;
+ if (secs < 60) return null;
+ var h: Human = .{ .buf = undefined, .len = 0 };
+ var w: std.Io.Writer = .fixed(&h.buf);
+ const d = secs / 86400;
+ const hr = (secs / 3600) % 24;
+ const m = (secs / 60) % 60;
+ const s = secs % 60;
+ (if (d > 0) w.print("{d}d {d}h", .{ d, hr }) else if (hr > 0) w.print("{d}h {d}m", .{ hr, m }) else w.print("{d}m {d}s", .{ m, s })) catch return null;
+ h.len = @intCast(w.buffered().len);
+ return h;
+}
+
+/// Finds the first "<number> second(s)" in `msg` and returns the whole-second
+/// count. Tolerates a fractional part ("3599.548 seconds"), which providers
+/// emit; the fraction is dropped, not rounded.
+fn findSeconds(msg: []const u8) ?u64 {
+ var i: usize = 0;
+ while (std.mem.indexOfPos(u8, msg, i, " second")) |pos| : (i = pos + 7) {
+ var start = pos;
+ var saw_dot = false;
+ while (start > 0 and (std.ascii.isDigit(msg[start - 1]) or (!saw_dot and msg[start - 1] == '.'))) : (start -= 1) {
+ if (msg[start - 1] == '.') saw_dot = true;
+ }
+ var end = pos;
+ if (saw_dot) end = std.mem.indexOfScalarPos(u8, msg, start, '.').?;
+ if (end > start and std.ascii.isDigit(msg[start]))
+ return std.fmt.parseInt(u64, msg[start..end], 10) catch null;
+ }
+ return null;
+}
+
+test "humanizeRetrySeconds (#398): big waits annotate, small and absent do not" {
+ const big = humanizeRetrySeconds("Please try again in 350000 seconds.").?;
+ try std.testing.expectEqualStrings("4d 1h", big.buf[0..big.len]);
+ const mid = humanizeRetrySeconds("rate limited; retry in 350 seconds").?;
+ try std.testing.expectEqualStrings("5m 50s", mid.buf[0..mid.len]);
+ const hrs = humanizeRetrySeconds("available again in 7250 seconds").?;
+ try std.testing.expectEqualStrings("2h 0m", hrs.buf[0..hrs.len]);
+ const frac = humanizeRetrySeconds("Expected available in 3599.548 seconds").?;
+ try std.testing.expectEqualStrings("59m 59s", frac.buf[0..frac.len]);
+ try std.testing.expect(humanizeRetrySeconds("try again in 30 seconds") == null);
+ try std.testing.expect(humanizeRetrySeconds("quota exceeded, no numbers") == null);
+ try std.testing.expect(humanizeRetrySeconds("in a second or two") == null);
+}
diff --git a/src/test_hooks.zig b/src/test_hooks.zig
index bd911f91..18c59004 100644
--- a/src/test_hooks.zig
+++ b/src/test_hooks.zig
@@ -108,6 +108,7 @@ const workflow_pipeline = @import("workflow_pipeline.zig");
// #296: the pipeline stage-level fitness capture. Reached in production only
// through workflow_pipeline.run's CALL, so its tests need the hook.
const pipeline_score = @import("pipeline_score.zig");
+const retry_hint = @import("retry_hint.zig");
// #364: shutdown-phase timings. Production reaches it from readline.zig,
// session.zig, mcp.zig, telemetry.zig and startup_timing.zig, so its coverage
@@ -171,4 +172,5 @@ test {
_ = snapshots;
_ = snapshots_tests;
_ = pipeline_score;
+ _ = retry_hint;
}