graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(compact): empty summaries escalate instead of re-shipping the over-cap history forever (#379)
Rach Pradhane7240d6d4dcc
diff --git a/src/agent.zig b/src/agent.zigindex f70b60a4..648f1ad4 100644--- a/src/agent.zig+++ b/src/agent.zig@@ -181,6 +181,7 @@ pub const Agent = struct {last_request_context_overflow: bool = false, // explicit provider rejection, not an inferred meter thresholdlast_request_write_failed: bool = false, // transport gave up specifically with WriteFailed this requestcompact_transport_failures: u8 = 0, // bounded escape for repeated opaque over-cap WriteFailed/network failures+ compact_summary_failures: u8 = 0, // #379: consecutive complete-but-unusable (empty/truncated) summariesws_off: bool = false, // codex ws transport disabled for this session after a handshake/transport fallback to SSE (#codex-ws)ws_transport_failures: u8 = 0, // consecutive WS failures; retry once before latching persistent SSEstreamed_text: bool = false, // the last request printed its text live@@ -244,9 +245,8 @@ pub const Agent = struct {return fmt.len > 0 and fmt[fmt.len - 1] == '\n';}- /// 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).+ /// Remember the formatted message for the --json `error` event, then print+ /// like say() + a #398 duration hint; last_api_error keeps provider words.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] });diff --git a/src/agent_compact.zig b/src/agent_compact.zigindex 90bafad1..3125f4d5 100644--- a/src/agent_compact.zig+++ b/src/agent_compact.zig@@ -194,6 +194,7 @@ pub fn compact(self: *Agent) anyerror!usize {// including its user boundary and paired tool calls/results.for (recent_messages) |message| try fresh.append(message);self.messages = fresh;+ self.compact_summary_failures = 0; // #379: a usable summary ends the streakself.last_context_tokens = 0;self.context_local_tokens = 0;self.goal_note_fp = 0; // the injected goal note died with the old history - re-state in full (#318)@@ -541,6 +542,20 @@ pub fn repeatedOpaqueCompactionFailure(self: *Agent, err: anyerror) bool {(self.provider.nearContextLimit(effective) or locally_over_window);}+/// #379: two consecutive COMPLETED-but-unusable summaries (empty or truncated)+/// are provably not transport noise — the model, at this context size, is not+/// going to produce one, and without escalation the over-cap history is+/// re-shipped forever. Unlike compact_transport_failures this counter survives+/// a complete response; only a usable summary (or a trim) resets it.+pub fn repeatedEmptySummaryFailure(self: *Agent, err: anyerror) bool {+ const unusable = err == error.EmptySummary or err == error.IncompleteSummary;+ if (unusable) self.compact_summary_failures +|= 1 else self.compact_summary_failures = 0;+ const threshold = self.provider.compactAt();+ return unusable and threshold > 0 and+ self.compact_summary_failures >= 2 and+ self.effectiveContextTokens() >= threshold;+}+pub fn compactOrRecover(self: *Agent, trim_on_fail: bool) void {if (self.compact()) |_| {self.compact_transport_failures = 0;@@ -560,15 +575,18 @@ pub fn compactOrRecover(self: *Agent, trim_on_fail: bool) void {},}const repeated_opaque_overflow = repeatedOpaqueCompactionFailure(self, err);+ const repeated_empty_summary = repeatedEmptySummaryFailure(self, err);// The caller's policy is computed before compact() makes its summary// request. Override it only for a concrete provider overflow rejection,- // or after two consecutive WriteFailed compaction attempts when the- // effective meter is near 95% (or local bytes prove over-window). The- // first failure and ordinary transport outages always preserve history.- if (!trim_on_fail and !self.last_request_context_overflow and !repeated_opaque_overflow) return;+ // after two consecutive WriteFailed compaction attempts when the+ // effective meter is near 95% (or local bytes prove over-window), or+ // after two complete-but-unusable summaries while over compact@ (#379).+ // The first failure and ordinary transport outages preserve history.+ if (!trim_on_fail and !self.last_request_context_overflow and !repeated_opaque_overflow and !repeated_empty_summary) return;const dropped = self.emergencyTrim();if (dropped > 0) {self.compact_transport_failures = 0;+ self.compact_summary_failures = 0;if (main_mod.json_mode)self.emit(.{ .type = "compact", .ok = true, .trimmed = dropped })elsediff --git a/src/agent_compact_summary_test.zig b/src/agent_compact_summary_test.zignew file mode 100644index 00000000..f63e40ee--- /dev/null+++ b/src/agent_compact_summary_test.zig@@ -0,0 +1,51 @@+//! #379: escalation evidence for complete-but-unusable compaction summaries.+//! Sibling of agent_compact_test.zig (which is at the 600-line ceiling).++const std = @import("std");+const Agent = @import("agent.zig").Agent;+const compact = @import("agent_compact.zig");+const repeatedEmptySummaryFailure = compact.repeatedEmptySummaryFailure;++test "repeated empty summaries unlock bounded recovery (#379)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var agent: Agent = undefined;+ agent.provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "gpt-5", .context = 100_000 };+ agent.messages = std.json.Array.init(a);+ agent.sub = false;+ agent.strict = false;+ agent.sys_normal = "";+ agent.sys_strict = "";+ agent.tools_responses = "";+ agent.last_context_tokens = 85_000; // over compact@ (80%), well under 95%+ agent.context_local_tokens = agent.fullRequestEstimateTokens();+ agent.compact_summary_failures = 0;++ // One empty summary is not evidence; two consecutive are.+ try std.testing.expect(!repeatedEmptySummaryFailure(&agent, error.EmptySummary));+ try std.testing.expectEqual(@as(u8, 1), agent.compact_summary_failures);+ try std.testing.expect(repeatedEmptySummaryFailure(&agent, error.EmptySummary));++ // Truncated summaries count toward the same streak.+ agent.compact_summary_failures = 1;+ try std.testing.expect(repeatedEmptySummaryFailure(&agent, error.IncompleteSummary));++ // Any other failure kind breaks the streak — transport noise is not proof+ // the model can't summarize.+ agent.compact_summary_failures = 1;+ try std.testing.expect(!repeatedEmptySummaryFailure(&agent, error.ApiError));+ try std.testing.expectEqual(@as(u8, 0), agent.compact_summary_failures);++ // Under the compaction threshold there is nothing to escape from.+ agent.last_context_tokens = 10_000;+ agent.context_local_tokens = 0;+ agent.compact_summary_failures = 1;+ try std.testing.expect(!repeatedEmptySummaryFailure(&agent, error.EmptySummary));++ // Unknown context windows never authorize destructive recovery.+ agent.provider.context = 0;+ agent.last_context_tokens = 85_000;+ agent.compact_summary_failures = 1;+ try std.testing.expect(!repeatedEmptySummaryFailure(&agent, error.EmptySummary));+}diff --git a/src/agent_request_body.zig b/src/agent_request_body.zigindex 649cfde8..9b9a1c8f 100644--- a/src/agent_request_body.zig+++ b/src/agent_request_body.zig@@ -230,9 +230,10 @@ pub fn buildBody(self: *Agent, tools: ?[]const u8, force_tool: bool, stream: bootry s.objectField("reasoning");try s.beginObject();try s.objectField("effort");- // Ultra is a Graff/Codex client preset: maximum server reasoning- // plus automatic delegation. The backend wire value is `max`.- try s.write(if (self.reasoning == .ultra) "max" else @tagName(self.reasoning));+ // Ultra preset → wire value `max`. #379: compaction summaries run+ // at low effort — a high-effort reasoner can complete with only+ // reasoning items and zero output text, i.e. an empty summary.+ try s.write(if (self.compaction_request) "low" else if (self.reasoning == .ultra) "max" else @tagName(self.reasoning));try s.endObject();try s.objectField("include");try s.beginArray();diff --git a/src/test_hooks.zig b/src/test_hooks.zigindex 18c59004..7ac85936 100644--- a/src/test_hooks.zig+++ b/src/test_hooks.zig@@ -109,6 +109,7 @@ const workflow_pipeline = @import("workflow_pipeline.zig");// 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");+const agent_compact_summary_test = @import("agent_compact_summary_test.zig");// #364: shutdown-phase timings. Production reaches it from readline.zig,// session.zig, mcp.zig, telemetry.zig and startup_timing.zig, so its coverage@@ -173,4 +174,5 @@ test {_ = snapshots_tests;_ = pipeline_score;_ = retry_hint;+ _ = agent_compact_summary_test;}