Skip to content

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

Python

compaction: stop re-compacting a blob-anchored Responses history

justrachf63f7636de29
diff --git a/src/additional_tests.zig b/src/additional_tests.zig
index 367a820b..ec60ec8e 100644
--- a/src/additional_tests.zig
+++ b/src/additional_tests.zig
@@ -7,6 +7,7 @@ test {
_ = @import("codedbpro_paths.zig");
_ = @import("agent_request_body_responses.zig");
_ = @import("agent_server_compact_tests.zig");
+ _ = @import("agent_server_compact_route_tests.zig");
_ = @import("provider_codegraff_tests.zig");
_ = @import("agent_responses.zig");
_ = @import("session_branch.zig");
diff --git a/src/agent_compact.zig b/src/agent_compact.zig
index 8f2ec42a..676c5447 100644
--- a/src/agent_compact.zig
+++ b/src/agent_compact.zig
@@ -550,7 +550,7 @@ pub fn compactOrRecover(self: *Agent, trim_on_fail: bool) void {
}
if (self.compact_pin_degraded and !trim_on_fail) return;
const has_opaque = @import("compaction_window.zig").latestBlob(self.messages.items) != null;
- const result = if (has_opaque) server_compact.manualCompact(self) else self.compact();
+ const result = if (has_opaque or server_compact.serverOnly(self.provider)) server_compact.manualCompact(self) else self.compact();
if (result) |_| {
self.compact_transport_failures = 0;
return;
diff --git a/src/agent_server_compact.zig b/src/agent_server_compact.zig
index 2cf08379..58a4840d 100644
--- a/src/agent_server_compact.zig
+++ b/src/agent_server_compact.zig
@@ -12,9 +12,11 @@
//!
//! Policy split vs the client-side path (agent_compact.zig):
//! - .responses providers: autocompact prunes local history to the newest
-//! server blob (zero model calls, near-lossless); the client-side summary
-//! survives only as the near-the-wall (95%) fallback. Manual /compact uses
-//! OpenAI's standalone endpoint or Codex's forced in-stream directive.
+//! server blob (zero model calls, near-lossless); near the wall (95%) a
+//! first-party route forces a server pass. Manual /compact uses OpenAI's
+//! standalone endpoint or Codex's forced in-stream directive. With the
+//! server arm on, those routes never fall back to a client summary
+//! (serverOnly); GRAFF_SERVER_COMPACT=0 is the opt-out.
//! - every other provider kind: unchanged legacy policy.
//!
//! The opaque blob's server-side lifetime under store:false is unknown; if a
@@ -147,6 +149,12 @@ pub fn manualServerEligible(p: Provider) bool {
return manualRoute(p) != .local;
}
+/// A first-party OpenAI route on the server arm compacts only into the
+/// provider's own encrypted state: no client summary, automatic or manual.
+pub fn serverOnly(p: Provider) bool {
+ return enabled(p) and manualServerEligible(p);
+}
+
pub fn compactEndpoint(arena: std.mem.Allocator, responses_url: []const u8) ![]const u8 {
if (!std.mem.endsWith(u8, responses_url, "/responses")) return error.UnsupportedServerCompaction;
return std.fmt.allocPrint(arena, "{s}/compact", .{responses_url});
@@ -278,6 +286,10 @@ fn fallbackLocal(self: *Agent, err: anyerror) anyerror!usize {
if (self.tracer) |tr| tr.note("server_compact_failed", @errorName(err));
return err; // local summaries cannot replace opaque state; caller reports the failure
}
+ if (serverOnly(self.provider)) {
+ if (self.tracer) |tr| tr.note("server_compact_failed", @errorName(err));
+ return err; // the provider's own compaction or none; caller reports the failure
+ }
if (self.tracer) |tr| tr.note("server_compact_fallback", @errorName(err));
if (!main_mod.json_mode) try self.say("[OpenAI server compaction unavailable; falling back to local summary]\n", .{});
return self.compact();
@@ -436,9 +448,10 @@ pub fn autocompactIf(self: *Agent, recovery_meter: u64, server_arm: bool) void {
}
if (pruneIf(self, true)) return;
// No blob yet: the server compacts in-stream at the same threshold, so
- // trust it at the ordinary compactAt line; the client-side summary (and
- // its destructive fallback) is reserved for genuinely near the wall, where
- // shipping the request itself would risk an over-cap hard failure (#163).
+ // trust it at the ordinary compactAt line; a forced pass (server-side on
+ // first-party routes, see serverOnly) is reserved for genuinely near the
+ // wall, where shipping the request itself would risk an over-cap hard
+ // failure (#163).
if (self.provider.nearContextLimit(recovery_meter))
self.compactOrRecover(true);
}
diff --git a/src/agent_server_compact_route_tests.zig b/src/agent_server_compact_route_tests.zig
new file mode 100644
index 00000000..e985394b
--- /dev/null
+++ b/src/agent_server_compact_route_tests.zig
@@ -0,0 +1,143 @@
+//! First-party OpenAI routes compact only into the provider's own encrypted
+//! state, and a blob-anchored history is metered by what it costs, not by its
+//! byte length. Split from agent_server_compact_tests.zig (600-line ceiling).
+
+const std = @import("std");
+const Io = std.Io;
+const Value = std.json.Value;
+const main_mod = @import("main.zig");
+const Agent = @import("agent.zig").Agent;
+const Provider = @import("provider.zig").Provider;
+const asc = @import("agent_server_compact.zig");
+const test_support = @import("agent_compact_test_support.zig");
+
+fn testAgent(a: std.mem.Allocator, id: []const u8, model: []const u8, context: u64) Agent {
+ var agent = test_support.subAgent(a, false);
+ agent.provider = .{ .id = id, .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = model, .context = context };
+ agent.gpa = a;
+ agent.out = null;
+ agent.label = "t";
+ agent.goal_note_fp = 0;
+ agent.history_rewrites = 0;
+ agent.messages = std.json.Array.init(a);
+ agent.compaction_window = .{};
+ agent.call_kind = .root;
+ agent.stream_quiet = true;
+ agent.tracer = null;
+ agent.compact_pin_degraded = false;
+ agent.compact_stall = .{};
+ agent.compact_transport_failures = 0;
+ agent.compact_summary_failures = 0;
+ agent.last_request_write_failed = false;
+ agent.last_request_context_overflow = false;
+ return agent;
+}
+
+test "serverOnly: every first-party OpenAI route, including the ChatGPT plan" {
+ var p: Provider = .{ .id = "chatgpt-new", .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = "gpt-6.1-sol", .context = 270_000 };
+ try std.testing.expect(asc.serverOnly(p));
+ p.id = "codex";
+ try std.testing.expect(asc.serverOnly(p));
+ p.id = "openai";
+ try std.testing.expect(asc.serverOnly(p));
+ p.id = "codegraff";
+ p.model = "gpt-6-astra";
+ try std.testing.expect(asc.serverOnly(p));
+ p.id = "xai"; // client summary still wins there
+ try std.testing.expect(!asc.serverOnly(p));
+ p.id = "kilo";
+ try std.testing.expect(!asc.serverOnly(p));
+ p.id = "codex";
+ p.kind = .anthropic;
+ try std.testing.expect(!asc.serverOnly(p));
+ p.kind = .responses;
+ asc.g_server_compact_override = false; // GRAFF_SERVER_COMPACT=0 opts back into the summary
+ defer asc.g_server_compact_override = null;
+ try std.testing.expect(!asc.serverOnly(p));
+}
+
+test "a 1.4 MB compaction blob does not hold a ChatGPT-plan session over the window" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var agent = testAgent(a, "chatgpt-new", "gpt-6.1-sol", 270_000);
+ const state = try a.alloc(u8, 1_400_000);
+ @memset(state, 'g');
+ var blob: std.json.ObjectMap = .empty;
+ try blob.put(a, "type", .{ .string = "compaction" });
+ try blob.put(a, "encrypted_content", .{ .string = state });
+ try agent.messages.append(.{ .object = blob });
+ try agent.messages.append(try std.json.parseFromSliceLeaky(Value, a, "{\"type\":\"function_call\",\"call_id\":\"c1\",\"name\":\"todo_read\",\"arguments\":\"{}\"}", .{}));
+ try agent.messages.append(try std.json.parseFromSliceLeaky(Value, a, "{\"type\":\"function_call_output\",\"call_id\":\"c1\",\"output\":\"[x] done\"}", .{}));
+ // The server bills this window at ~79k; /4 over the blob read ~350k and
+ // forced a compaction before every tool call.
+ const local = agent.fullRequestEstimateTokens();
+ try std.testing.expect(local < agent.provider.compactAt());
+ @import("agent_context.zig").replaceContextTokens(&agent, 79_000);
+ try std.testing.expect(agent.last_context_tokens < agent.provider.compactAt());
+ // No network is wired: a forced pass here would fail and say so.
+ var aw: Io.Writer.Allocating = .init(a);
+ agent.out = &aw.writer;
+ asc.autocompactIf(&agent, agent.effectiveContextTokens(), true);
+ try std.testing.expectEqual(@as(usize, 3), agent.messages.items.len);
+ try std.testing.expectEqual(@as(u32, 0), agent.history_rewrites);
+ try std.testing.expectEqualStrings("", aw.written());
+}
+
+test "a failed first-party compaction never falls back to a client summary" {
+ const io = std.testing.io;
+ const Server = struct {
+ fn run(io_: Io, server: *Io.net.Server, hits: *std.atomic.Value(u32)) void {
+ while (true) {
+ const conn = server.accept(io_) catch return;
+ defer conn.close(io_);
+ _ = hits.fetchAdd(1, .acq_rel);
+ var rbuf: [8192]u8 = undefined;
+ var reader = Io.net.Stream.Reader.init(conn, io_, &rbuf);
+ var length: usize = 0;
+ _ = (reader.interface.takeDelimiter('\n') catch return) orelse return;
+ while (true) {
+ const header = (reader.interface.takeDelimiter('\n') catch return) orelse return;
+ if (std.mem.eql(u8, header, "\r")) break;
+ if (std.ascii.startsWithIgnoreCase(header, "content-length:"))
+ length = std.fmt.parseInt(usize, std.mem.trim(u8, header[15..], " \r"), 10) catch return;
+ }
+ _ = reader.interface.take(length) catch return;
+ const payload = "{\"output\":[]}";
+ var wbuf: [1024]u8 = undefined;
+ var writer = Io.net.Stream.Writer.init(conn, io_, &wbuf);
+ writer.interface.print("HTTP/1.1 200 OK\r\ncontent-length: {d}\r\nconnection: close\r\n\r\n{s}", .{ payload.len, payload }) catch return;
+ writer.interface.flush() catch {};
+ }
+ }
+ };
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var addr = try Io.net.IpAddress.parseLiteral("127.0.0.1:0");
+ var server = try Io.net.IpAddress.listen(&addr, io, .{});
+ defer server.deinit(io);
+ var hits: std.atomic.Value(u32) = .init(0);
+ var group: Io.Group = .init;
+ defer group.cancel(io);
+ try group.concurrent(io, Server.run, .{ io, &server, &hits });
+ var client: std.http.Client = .{ .allocator = a, .io = io };
+ defer client.deinit();
+ var agent = testAgent(a, "openai", "gpt-6-astra", 100_000);
+ agent.io = io;
+ agent.client = &client;
+ agent.provider.url = try std.fmt.allocPrint(a, "http://127.0.0.1:{d}/responses", .{server.socket.address.getPort()});
+ var aw: Io.Writer.Allocating = .init(a);
+ agent.out = &aw.writer;
+ const was_json = main_mod.json_mode;
+ main_mod.json_mode = false;
+ defer main_mod.json_mode = was_json;
+ for (0..4) |_| try agent.messages.append(try std.json.parseFromSliceLeaky(Value, a, "{\"type\":\"message\",\"role\":\"user\",\"content\":\"hi\"}", .{}));
+ // No blob yet, so the old policy ran compact() — a client summary.
+ agent.compactOrRecover(false);
+ try std.testing.expectEqual(@as(u32, 1), hits.load(.acquire)); // the /responses/compact call only
+ try std.testing.expectEqual(@as(usize, 4), agent.messages.items.len);
+ try std.testing.expectEqual(@as(u32, 0), agent.history_rewrites);
+ try std.testing.expect(std.mem.indexOf(u8, aw.written(), "auto-compaction failed") != null);
+ try std.testing.expect(std.mem.indexOf(u8, aw.written(), "falling back to local") == null);
+}
diff --git a/src/context_tokens.zig b/src/context_tokens.zig
index 0b5e920a..111bb0ae 100644
--- a/src/context_tokens.zig
+++ b/src/context_tokens.zig
@@ -1,6 +1,7 @@
//! Conservative JSON context estimates that treat inline image bytes as media,
//! not ordinary text. Providers tokenize images from decoded dimensions/content;
-//! the Base64 transport size is not part of the language-model context.
+//! the Base64 transport size is not part of the language-model context. Server
+//! compaction state gets the same treatment at its own ratio.
const std = @import("std");
const Io = std.Io;
@@ -11,13 +12,22 @@ const Value = std.json.Value;
/// compaction's 8k recent-turn preservation budget.
pub const image_tokens: u64 = 4_096;
+/// A server compaction item's `encrypted_content` is compressed, encrypted
+/// state, not text: a 1.4 MB blob reports ~70k input tokens. Counting it at
+/// /4 put a blob-anchored history over the window, so every step re-ran a
+/// compaction that could not shrink it. /16 stays above the observed ratio;
+/// the provider's reported usage remains the authority above this floor.
+pub const compaction_state_bytes_per_token: usize = 16;
+
const MediaStats = struct {
payload_bytes: usize = 0,
images: u64 = 0,
+ compaction_state_bytes: usize = 0,
fn add(self: *MediaStats, other: MediaStats) void {
self.payload_bytes +|= other.payload_bytes;
self.images +|= other.images;
+ self.compaction_state_bytes +|= other.compaction_state_bytes;
}
};
@@ -56,6 +66,12 @@ fn imageBlockPayloadLen(obj: std.json.ObjectMap) ?usize {
return null;
}
+fn compactionStateLen(obj: std.json.ObjectMap) ?usize {
+ const kind = stringValue(obj.get("type")) orelse return null;
+ if (!std.mem.eql(u8, kind, "compaction") and !std.mem.eql(u8, kind, "compaction_summary")) return null;
+ return (stringValue(obj.get("encrypted_content")) orelse return null).len;
+}
+
fn mediaStats(value: Value) MediaStats {
var stats: MediaStats = .{};
switch (value) {
@@ -65,6 +81,7 @@ fn mediaStats(value: Value) MediaStats {
stats.payload_bytes +|= payload_bytes;
stats.images +|= 1;
}
+ if (compactionStateLen(obj)) |state_bytes| stats.compaction_state_bytes +|= state_bytes;
var it = obj.iterator();
while (it.next()) |entry| stats.add(mediaStats(entry.value_ptr.*));
},
@@ -157,12 +174,15 @@ fn escapeExtra(byte: u8) usize {
/// Estimate a JSON container when its serialized byte count is already known.
/// Inline image payloads are removed from the text estimate and replaced by a
-/// bounded per-image vision estimate; every other byte retains the prior /4
+/// bounded per-image vision estimate, and server compaction state counts at
+/// compaction_state_bytes_per_token; every other byte retains the prior /4
/// policy so tool output and reasoning overflow protection stays conservative.
pub fn estimatedTokensFromLen(value: Value, total_bytes: usize) u64 {
const media = mediaStats(value);
- const text_bytes = total_bytes -| media.payload_bytes;
- return @as(u64, @intCast(text_bytes / 4)) +| media.images *| image_tokens;
+ const text_bytes = total_bytes -| media.payload_bytes -| media.compaction_state_bytes;
+ return @as(u64, @intCast(text_bytes / 4)) +|
+ media.images *| image_tokens +|
+ @as(u64, @intCast(media.compaction_state_bytes / compaction_state_bytes_per_token));
}
pub fn estimatedTokens(value: Value) u64 {
@@ -189,6 +209,34 @@ test "inline image payload size does not masquerade as text context" {
try std.testing.expect(estimatedTokens(value) < 5_000);
}
+test "server compaction state does not masquerade as text context" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+
+ const state = try arena.alloc(u8, 1_400_000);
+ @memset(state, 'g');
+ var items: std.json.Array = .init(arena);
+ for ([_][]const u8{ "compaction", "compaction_summary" }) |kind| {
+ var blob: std.json.ObjectMap = .empty;
+ try blob.put(arena, "type", .{ .string = kind });
+ try blob.put(arena, "encrypted_content", .{ .string = state });
+ items.clearRetainingCapacity();
+ try items.append(.{ .object = blob });
+ const value: Value = .{ .array = items };
+ // At /4 this one item alone read ~350k tokens, over a 270k window.
+ try std.testing.expect(serializedLen(value) / 4 > 340_000);
+ try std.testing.expect(estimatedTokens(value) >= 1_400_000 / compaction_state_bytes_per_token);
+ try std.testing.expect(estimatedTokens(value) < 100_000);
+ }
+
+ // Reasoning keeps its encrypted_content at /4: a resend pays for it again.
+ var reasoning: std.json.ObjectMap = .empty;
+ try reasoning.put(arena, "type", .{ .string = "reasoning" });
+ try reasoning.put(arena, "encrypted_content", .{ .string = state[0..40_000] });
+ try std.testing.expect(estimatedTokens(.{ .object = reasoning }) >= 10_000);
+}
+
test "ordinary large strings remain fully counted" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();