graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(http): do not echo 429/5xx bodies on retry
Rach Pradhan9a2a9ea546a7
diff --git a/src/agent_request.zig b/src/agent_request.zigindex c2dc32e8..62e546d1 100644--- a/src/agent_request.zig+++ b/src/agent_request.zig@@ -323,7 +323,7 @@ pub fn request(self: *Agent, tools_in: ?[]const u8) !std.json.ObjectMap {// with a working credential sitting unused. One-way// and announced — see credential_failover.if (policy.handOffExhaustedPlan(self)) continue;- self.last_api_error = std.fmt.allocPrint(self.arena, "rate limited (429): {s} — {s}", .{ policy.quota_cap_marker, main_mod.g_5xx_body_buf[0..main_mod.g_5xx_body_len] }) catch "rate limited (429): quota exceeded";+ self.last_api_error = std.fmt.allocPrint(self.arena, "rate limited (429): {s}", .{policy.quota_cap_marker}) catch "rate limited (429): quota exceeded";if (telemetry.g_telem) |t| t.errorEvent("quota", self.last_api_error orelse "quota exceeded");if (self.tracer) |tr| tr.api(self.label, self.sub, self.provider.model, 0, body.len, 0, 0, 0, true);return error.ApiError;@@ -337,12 +337,11 @@ pub fn request(self: *Agent, tools_in: ?[]const u8) !std.json.ObjectMap {const delay_ms = if (server_ra > 0) server_ra else RetryPlan.delayMs(throttled, attempt);const ra_note: []const u8 = if (server_ra > 0) " (server retry-after)" else "";const what: []const u8 = if (err == error.RateLimited) "rate limited (429)" else "server error (5xx)";- if (main_mod.g_5xx_body_len > 0) {- try self.say("[{s}{s} — retrying in {d}s ({d}/{d})] {s}\n", .{ what, ra_note, delay_ms / 1000, attempt + 1, max_attempts, main_mod.g_5xx_body_buf[0..main_mod.g_5xx_body_len] });- } else {- try self.say("[{s}{s} — retrying in {d}s ({d}/{d})]\n", .{ what, ra_note, delay_ms / 1000, attempt + 1, max_attempts });- }- if (self.tracer) |tr| tr.note("retry", if (main_mod.g_5xx_body_len > 0) main_mod.g_5xx_body_buf[0..main_mod.g_5xx_body_len] else what);+ // Never echo the captured 429/5xx body. Envelopes from+ // OpenRouter and similar include user ids, BYOK/settings+ // URLs, and routing docs. Classifiers still read g_5xx_body_buf.+ try self.say("[{s}{s} — retrying in {d}s ({d}/{d})]\n", .{ what, ra_note, delay_ms / 1000, attempt + 1, max_attempts });+ if (self.tracer) |tr| tr.note("retry", what);self.sleepInterruptible(delay_ms) catch return error.Interrupted;} else {// Transport flake (HttpConnectionClosing, a reset,@@ -361,11 +360,7 @@ pub fn request(self: *Agent, tools_in: ?[]const u8) !std.json.ObjectMap {}continue;}- if (main_mod.g_5xx_body_len > 0) {- try self.say("[request failed: {t} — giving up this turn] {s}\n", .{ err, main_mod.g_5xx_body_buf[0..main_mod.g_5xx_body_len] });- } else {- try self.say("[request failed: {t} — giving up this turn]\n", .{err});- }+ try self.say("[request failed: {t} — giving up this turn]\n", .{err});// Network give-up is its own error kind: the ApiError// handler's last_api_error would otherwise be an API// envelope, stale or null on a pure transport failure —diff --git a/src/agent_stream.zig b/src/agent_stream.zigindex 1f9a4663..a4dc93e0 100644--- a/src/agent_stream.zig+++ b/src/agent_stream.zig@@ -178,8 +178,8 @@ pub fn postStreamWithClient(self: *Agent, client: *std.http.Client, body: []consconst status_code = @intFromEnum(response.head.status);if (status_code == 429 or status_code >= 500) {http.captureRetryAfter(&response); // #retry-after: honor the provider's requested backoff- // Drain a snippet of the error body so the retry message surfaces the- // gateway's diagnostic instead of a bare "server error (5xx)".+ // Drain a snippet for quota classifiers. Do not echo it on the REPL+ // retry line — vendor envelopes include user ids and routing docs.capture5xxBodyStream(self.gpa, &response);if (req.connection) |conn| conn.closing = true;return if (status_code == 429) error.RateLimited else error.ServerError;diff --git a/src/http.zig b/src/http.zigindex 2750749d..2aa9ac13 100644--- a/src/http.zig+++ b/src/http.zig@@ -31,9 +31,8 @@ pub const providerHeaders = headers.providerHeaders;pub const postWithConv = post;/// Copy up to root.g_5xx_body_buf.len bytes of an error response body into the-/// global buffer so request()'s retry message can surface the gateway's-/// diagnostic (e.g. "upstream timeout connecting to anthropic") instead of a-/// bare "server error (5xx)".+/// global buffer so quota/overflow classifiers can inspect it. The REPL retry+/// line must not echo this: vendor envelopes include user ids and routing docs.fn capture5xxBody(src: []const u8) void {const n = @min(src.len, root.g_5xx_body_buf.len);if (n > 0) @memcpy(root.g_5xx_body_buf[0..n], src[0..n]);@@ -156,8 +155,8 @@ fn post(gpa: Allocator, client: *std.http.Client, provider: Provider, body: []coconst code = @intFromEnum(response.head.status);if (code == 429 or code >= 500) {- // Drain a snippet of the error body for the retry message, then- // poison: the connection still holds unread body bytes.+ // Drain a snippet for quota classifiers, then poison: the connection+ // still holds unread body bytes.captureRetryAfter(&response); // #retry-after: honor the provider's requested backoffcapture5xxBodyStream(gpa, &response);if (req.connection) |conn| conn.closing = true;