Skip to content

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

Python

fix(compact): manual /compact uses the xAI explicit-compact endpoint (#503)

Rach Pradhanc119d62a5204
diff --git a/src/agent_server_compact.zig b/src/agent_server_compact.zig
index f83c425a..2b7b1a3e 100644
--- a/src/agent_server_compact.zig
+++ b/src/agent_server_compact.zig
@@ -282,6 +282,30 @@ fn fallbackLocal(self: *Agent, err: anyerror) anyerror!usize {
/// exists. A failed or malformed server result never touches live history and
/// falls back to the existing inspectable local summary.
pub fn manualCompact(self: *Agent) anyerror!usize {
+ // #503: an explicit-compact provider (xAI, #502) serves manual /compact
+ // from its first-party endpoint too — the same one-POST blob as the
+ // autocompaction path, with the client summary as the fallback.
+ if (self.provider.serverCompactUrl() != null) {
+ self.closeCodexWs();
+ if (self.messages.items.len == 0) {
+ if (!main_mod.json_mode) try self.say("nothing to compact\n", .{});
+ return 0;
+ }
+ if (explicitCompact(self)) return self.messages.items.len;
+ // Refused (anti-thrash: already blob-anchored) or failed. Never
+ // client-summarize a blob-anchored history — the summary model can't
+ // see inside the opaque item, so everything in it would be dropped.
+ const items = self.messages.items;
+ if (items[0] == .object) {
+ if (items[0].object.get("type")) |t| {
+ if (t == .string and std.mem.eql(u8, t.string, "compaction")) {
+ if (!main_mod.json_mode) try self.say("[history already anchored on a server compaction blob]\n", .{});
+ return 0;
+ }
+ }
+ }
+ return self.compact();
+ }
const route = manualRoute(self.provider);
if (route == .local) return self.compact();
self.closeCodexWs();
diff --git a/src/agent_server_compact_tests.zig b/src/agent_server_compact_tests.zig
index 2db43e22..e59971b6 100644
--- a/src/agent_server_compact_tests.zig
+++ b/src/agent_server_compact_tests.zig
@@ -7,6 +7,7 @@ const Value = std.json.Value;
const main_mod = @import("main.zig");
const Agent = @import("agent.zig").Agent;
const Provider = @import("provider.zig").Provider;
+const provider = @import("provider.zig");
const asc = @import("agent_server_compact.zig");
const enabled = asc.enabled;
const noteExposure = asc.noteExposure;
@@ -302,3 +303,24 @@ test "explicitCompact anti-thrash: a fresh blob head with little growth refuses
try std.testing.expectEqual(@as(usize, 2), agent.messages.items.len);
try std.testing.expectEqual(@as(u32, 0), agent.history_rewrites);
}
+
+test "manualCompact (#503): a blob-anchored history on an explicit-compact provider no-ops instead of client-summarizing the blob" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var agent = testAgent(a, .responses);
+ provider.g_xai_responses = true;
+ defer provider.g_xai_responses = false;
+ agent.provider = .{ .id = "xai", .kind = .responses, .auth = .bearer, .url = provider.xai_responses_url, .api_key = "k", .model = "grok-4.6", .context = 100_000 };
+ // History = one opaque compaction item + one turn: small and blob-anchored,
+ // so the anti-thrash guard refuses the endpoint before any POST — and the
+ // fallback must NOT be a client summary (it cannot see inside the blob).
+ var blob: std.json.ObjectMap = .empty;
+ try blob.put(a, "type", .{ .string = "compaction" });
+ try blob.put(a, "encrypted_content", .{ .string = "opaque" });
+ try agent.messages.append(.{ .object = blob });
+ try agent.messages.append(try item(a, "message", null));
+ const n = try manualCompact(&agent);
+ try std.testing.expectEqual(@as(usize, 0), n);
+ try std.testing.expectEqual(@as(usize, 2), agent.messages.items.len); // untouched
+}
diff --git a/src/agent_ws_fallback_test.zig b/src/agent_ws_fallback_test.zig
index 225e19d4..95f61de3 100644
--- a/src/agent_ws_fallback_test.zig
+++ b/src/agent_ws_fallback_test.zig
@@ -196,3 +196,34 @@ test "#427: a non-426 handshake failure keeps the ladder's free retry, then latc
try std.testing.expect(!traced(&tw, "\"detail\":\"426 upgrade required"));
try std.testing.expect(!sawTransportAbort(rec.items));
}
+
+// #502 regression: the codex-only gate (correct for Platform OpenAI, which has
+// no WS server to probe) must not swallow xAI — both serve a real WS endpoint,
+// and the v0.0.25x release line's gate silently killed xAI WS turns once.
+test "wsEligible: codex and xai qualify; Platform OpenAI and chat wires never do" {
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+
+ const saved_ws = main_mod.g_codex_ws;
+ main_mod.g_codex_ws = true;
+ defer main_mod.g_codex_ws = saved_ws;
+
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ var aw: Io.Writer.Allocating = .init(gpa);
+ defer aw.deinit();
+
+ var agent = mockAgent(gpa, arena_state.allocator(), io, "https://chatgpt.com/x");
+ agent.out = &aw.writer;
+ try std.testing.expect(agent_ws.wsEligible(&agent)); // codex baseline
+
+ agent.provider.id = "xai";
+ try std.testing.expect(agent_ws.wsEligible(&agent)); // #502: xai rides WS
+
+ agent.provider.id = "openai";
+ try std.testing.expect(!agent_ws.wsEligible(&agent)); // Platform: no WS server
+
+ agent.provider.id = "xai";
+ agent.provider.kind = .openai;
+ try std.testing.expect(!agent_ws.wsEligible(&agent)); // chat wire: never WS
+}