graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix: treat token-parse 500 as overflow, not a gateway flake (#1019)
Rach153ec6af9d54
diff --git a/src/agent_gateway_retry.zig b/src/agent_gateway_retry.zigindex f6892952..9170f832 100644--- a/src/agent_gateway_retry.zig+++ b/src/agent_gateway_retry.zig@@ -97,6 +97,10 @@ pub fn isShortGatewayFlake(etype: []const u8, code: ?[]const u8, msg: []const u8// Gateway 110-byte follow-up. etype is often invalid_request_error, which// would otherwise hard-fail on the "invalid" needle. Auth/quota still die.if (isBodyParseRejection(msg)) return true;+ // xAI 500s "Internal error during token parsing" on a fat prompt. The+ // "internal" flake needle would resend the same body twice and then kill+ // the turn; overflow recovery trims instead (agent_overflow.zig).+ if (util.indexOfIgnoreCase(msg, "token parsing") != null) return false;const hard = [_][]const u8{ "invalid", "authentication", "unauthorized", "insufficient", "quota", "permission", "tool_choice", "not found" };for (hard) |n| {if (util.indexOfIgnoreCase(etype, n) != null) return false;@@ -194,6 +198,7 @@ test "shouldRetryBodyParseAfterTimeouts (#gateway-artifact): timeout history gattest "isShortGatewayFlake: internal/empty api_error retry; invalid/auth/quota do not" {try std.testing.expect(isShortGatewayFlake("api_error", null, "Internal Server Error"));+ try std.testing.expect(!isShortGatewayFlake("api_error", null, "Internal error during token parsing"));try std.testing.expect(isShortGatewayFlake("api_error", null, ""));try std.testing.expect(isShortGatewayFlake("error", null, " "));try std.testing.expect(isShortGatewayFlake("", null, "unknown error"));@@ -208,6 +213,16 @@ test "isShortGatewayFlake: internal/empty api_error retry; invalid/auth/quota dotry std.testing.expect(!isShortGatewayFlake("invalid_request_error", null, "invalid prompt"));}+test "#1019: token-parse 500 is not a short gateway flake (WS etype empty)" {+ const msg = "Internal error during token parsing";+ // The Responses WS arm calls afterServerErrorOrParseReject with etype "".+ try std.testing.expect(!isShortGatewayFlake("", null, msg));+ try std.testing.expect(!isShortGatewayFlake("api_error", null, msg));+ try std.testing.expect(!isShortGatewayFlake("", null, "xai api error: Internal error during token parsing"));+ try std.testing.expect(!isBodyParseRejection(msg));+ try std.testing.expect(isShortGatewayFlake("api_error", null, "Internal Server Error"));+}+test "#748: error-only SSE is not a truncated gateway body" {const err_sse = "event: error\ndata: {\"error\":{\"type\":\"invalid_request_error\",\"message\":\"bad\"}}\n";try std.testing.expect(sseLooksLikeError(err_sse));diff --git a/src/agent_overflow.zig b/src/agent_overflow.zigindex e047c2e5..e0991d6b 100644--- a/src/agent_overflow.zig+++ b/src/agent_overflow.zig@@ -80,6 +80,7 @@ const overflow_needles = [_][]const u8{"input token count", // google: "The input token count (N) exceeds the maximum ...""prompt token count", // github copilot: "prompt token count of N exceeds the limit of M""maximum prompt length", // xai: "This model's maximum prompt length is N ..."+ "during token parsing", // xai: "Internal error during token parsing" — tokenizer dies on a fat prompt instead of a 413; resending the same body never helps"reduce the length of the messages", // groq"exceeds the available context size", // llama.cpp server"exceeded model token limit", // kimi for coding@@ -408,6 +409,7 @@ test "isContextOverflow (#193/#203/#414): guards beat patterns; every provider p.{ .msg = "The input token count (1196265) exceeds the maximum number of tokens allowed (1048575)", .want = true, .why = "google gemini" },.{ .msg = "prompt token count of 141000 exceeds the limit of 128000", .want = true, .why = "github copilot" },.{ .msg = "This model's maximum prompt length is 131072 but the request contains 537812 tokens", .want = true, .why = "xai/grok" },+ .{ .msg = "Internal error during token parsing", .want = true, .why = "xai tokenizer 500 on a fat prompt; not a gateway flake" },.{ .msg = "Please reduce the length of the messages or completion.", .want = true, .why = "groq" },.{ .msg = "the request exceeds the available context size, try increasing it", .want = true, .why = "llama.cpp" },.{ .msg = "Your request exceeded model token limit: 262144 (requested: 300000)", .want = true, .why = "kimi for coding" },diff --git a/src/agent_overflow_tests.zig b/src/agent_overflow_tests.zigindex d961004d..51c67a9a 100644--- a/src/agent_overflow_tests.zig+++ b/src/agent_overflow_tests.zig@@ -54,6 +54,19 @@ fn trimmableAgent(a: std.mem.Allocator, kind: @import("provider.zig").Provider.Kreturn agent;}+test "#1019: token-parse 500 emergency-trims instead of dying" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var agent = try trimmableAgent(a, .responses, 131072);+ const before = agent.fullRequestEstimateTokens();+ var retried = false;+ try std.testing.expect(recoverContextOverflow(&agent, "Internal error during token parsing", null, &retried));+ try std.testing.expect(retried);+ try std.testing.expect(agent.last_request_context_overflow);+ try std.testing.expect(agent.fullRequestEstimateTokens() < before);+}+test "recoverContextOverflow (#414): a Bedrock throttle rides the retry ladder instead of compacting" {var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);defer arena_state.deinit();diff --git a/src/agent_responses.zig b/src/agent_responses.zigindex 90df207a..680c71f8 100644--- a/src/agent_responses.zig+++ b/src/agent_responses.zig@@ -187,6 +187,25 @@ test "parseResponses: terminal failure beats partial items; incomplete stays mar}}+test "#1019: type:error token-parse frame yields failure.message" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var agent: Agent = undefined;+ agent.arena = a;+ agent.scratch_arena = null;+ agent.message_mutation_arena = null;+ const body =+ "data: {\"type\":\"error\",\"error\":{\"message\":\"Internal error during token parsing\"}}\n";+ switch (try parseResponses(&agent, body)) {+ .err => |failure| {+ try std.testing.expectEqualStrings("Internal error during token parsing", failure.message);+ try std.testing.expect(failure.code == null);+ },+ .ok => return error.TestUnexpectedResult,+ }+}+pub fn errorMessage(obj: std.json.ObjectMap) ?[]const u8 {if (obj.get("error")) |e| {if (e == .object) {diff --git a/src/agent_ws_test.zig b/src/agent_ws_test.zigindex f21299e7..252af8f1 100644--- a/src/agent_ws_test.zig+++ b/src/agent_ws_test.zig@@ -99,6 +99,11 @@ test "the codex .responses arm refreshes auth and re-anchors before resending (#// delegates to the one envelope gate, which runs the shared transient retry// first and then the bounded gateway-artifact retry (#gateway-artifact).try std.testing.expect(std.mem.indexOf(u8, arm, "policy.afterServerErrorOrParseReject(self, \"\", failure.code, msg, &server_retries, &gw_retry)") != null);+ // #1019: overflow trim must run on failure.message before the empty-etype flake retry.+ const overflow_at = std.mem.indexOf(u8, arm, "recoverContextOverflow(self, msg, failure.code, &context_retried)") orelse+ return error.ResponsesPathHasNoOverflowRecovery;+ const flake_at = std.mem.indexOf(u8, arm, "policy.afterServerErrorOrParseReject(self, \"\", failure.code, msg, &server_retries, &gw_retry)").?;+ try std.testing.expect(overflow_at < flake_at);const gate = @embedFile("agent_gateway_retry.zig");try std.testing.expect(std.mem.indexOf(u8, gate, "policy.retryTransientServerError(self, etype, code, msg, server_retries)") != null);diff --git a/src/main.zig b/src/main.zigindex 30b05441..4ec69254 100644--- a/src/main.zig+++ b/src/main.zig@@ -575,6 +575,8 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu_ = @import("list_dir_nearmiss.zig");_ = @import("repo_map.zig");_ = @import("agent_overflow_tests.zig"); // #414: and, through it, agent_overflow.zig's table tests+ _ = @import("agent_gateway_retry.zig"); // #1019: flake vs overflow classification must stay reachable+ _ = @import("agent_responses.zig"); // #1019: type:error frame message extraction_ = @import("agent_server_compact.zig"); // server-side autocompact (codex Responses)_ = @import("compact_status.zig");_ = @import("agent_request_search_tests.zig");