Skip to content

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

Python

compaction: accept a hosted summary that keeps more than six messages

justrach15d44dd92a03
diff --git a/docs/adr/0252-hosted-compaction-with-a-codegraff-login.md b/docs/adr/0252-hosted-compaction-with-a-codegraff-login.md
index b75cfdf1..ec98f853 100644
--- a/docs/adr/0252-hosted-compaction-with-a-codegraff-login.md
+++ b/docs/adr/0252-hosted-compaction-with-a-codegraff-login.md
@@ -69,6 +69,7 @@ Codegraff gateway each time they compact. Each compaction costs a small
billed request and a round trip, which buys decisions that can be audited and
results that are trimmed rather than paraphrased. A gateway outage or an empty
balance never stops a session; it costs that compaction its quality.
-Revisit when the endpoint accepts graff's own pin (the hybrid summary keeps a
-fixed number of newest messages today), or when live-turn calls can be
-dropped safely on a given wire.
+The endpoint may keep more than its six newest messages when it moves its
+cut back over a call and its result; graff accepts any such tail. Revisit to
+send graff's own working set as `preserveRecentMessages`, or when live-turn
+calls can be dropped safely on a given wire.
diff --git a/src/gateway_compact_tests.zig b/src/gateway_compact_tests.zig
index 002f6709..f1adc21e 100644
--- a/src/gateway_compact_tests.zig
+++ b/src/gateway_compact_tests.zig
@@ -219,7 +219,7 @@ const long_turn =
\\ {"type":"function_call_output","call_id":"a5","output":"five"}
;
-fn summarizedReply(a: Allocator, msgs: []const wire.Msg) !wire.Reply {
+fn summarizedReply(a: Allocator, msgs: []const wire.Msg, kept: usize) !wire.Reply {
var out: std.Io.Writer.Allocating = .init(a);
const w = &out.writer;
try w.writeAll("{\"stats\":{\"summarized\":true},\"decisions\":[],\"messages\":[");
@@ -227,7 +227,7 @@ fn summarizedReply(a: Allocator, msgs: []const wire.Msg) !wire.Reply {
const sent = field(neutral, "messages").array.items;
try std.json.Stringify.value(sent[0], .{}, w);
try w.writeAll(",{\"role\":\"user\",\"text\":\"[compacted summary of 6 earlier messages]\\nRan two checks.\",\"toolUses\":[]}");
- for (sent[sent.len - wire.recent_kept ..]) |m| {
+ for (sent[sent.len - kept ..]) |m| {
try w.writeByte(',');
try std.json.Stringify.value(m, .{}, w);
}
@@ -243,7 +243,7 @@ test "ADR 0252: a summary keeps the unresolved turn's opening prompt and never o
const unresolved = try parse(a, long_turn ++ "]");
const msgs = try wire.transcript(a, unresolved);
try std.testing.expectEqual(@as(usize, 13), msgs.len);
- const reply = try summarizedReply(a, msgs);
+ const reply = try summarizedReply(a, msgs, wire.recent_kept);
try std.testing.expectEqualStrings("Ran two checks.", reply.summary);
const p = try wire.plan(a, msgs, &reply, 6);
const stubber: TestStub = .{};
@@ -261,12 +261,22 @@ test "ADR 0252: a summary keeps the unresolved turn's opening prompt and never o
\\,{"type":"message","role":"assistant","content":[{"type":"output_text","text":"done"}]}]
);
const msgs2 = try wire.transcript(a, resolved);
- const reply2 = try summarizedReply(a, msgs2);
+ const reply2 = try summarizedReply(a, msgs2, wire.recent_kept);
const p2 = try wire.plan(a, msgs2, &reply2, null);
const fresh2 = try wire.apply(a, resolved, msgs2, p2, &reply2.decisions, 6, note, &stubber);
try std.testing.expectEqualStrings("a3", field(fresh2.items[2], "call_id").string);
try std.testing.expectEqualStrings("function_call", field(fresh2.items[2], "type").string);
try wire.checkPairs(a, resolved, fresh2.items);
+
+ // An endpoint that moves its own cut back over the pair keeps seven
+ // newest messages; the same history results.
+ const reply3 = try summarizedReply(a, msgs2, wire.recent_kept + 1);
+ const p3 = try wire.plan(a, msgs2, &reply3, null);
+ const fresh3 = try wire.apply(a, resolved, msgs2, p3, &reply3.decisions, 6, note, &stubber);
+ try std.testing.expectEqual(fresh2.items.len, fresh3.items.len);
+ try std.testing.expectEqualStrings("a3", field(fresh3.items[2], "call_id").string);
+ // Too few left to summarize: refused, not misread.
+ try std.testing.expectError(error.UnexpectedReply, wire.plan(a, msgs2, &(try summarizedReply(a, msgs2, msgs2.len - 2)), null));
}
test "ADR 0252: a history that pairs worse than the one it replaces is refused" {
diff --git a/src/gateway_compact_wire.zig b/src/gateway_compact_wire.zig
index 039c8605..85ec52d7 100644
--- a/src/gateway_compact_wire.zig
+++ b/src/gateway_compact_wire.zig
@@ -336,10 +336,11 @@ pub const Plan = struct {
};
/// Check the reply against what the decisions imply, then decide which
-/// neutral messages stay. A summarized reply is [first, summary, newest
-/// recent_kept]; the cut moves back over any call a kept result answers (a
-/// result without its call is rejected by every wire), and `opening` — an
-/// unresolved turn's opening prompt (#581) — is kept verbatim too.
+/// neutral messages stay. A summarized reply is [first, summary, newest k]:
+/// k is at least recent_kept, more when the endpoint moved its cut back over
+/// a call/result pair. The cut also moves back here over any call a kept
+/// result answers (a result without its call is rejected by every wire), and
+/// `opening` — an unresolved turn's opening prompt (#581) — stays verbatim.
pub fn plan(a: Allocator, msgs: []const Msg, reply: *const Reply, opening: ?usize) !Plan {
const d = &reply.decisions;
var left: std.ArrayList(usize) = .empty;
@@ -351,9 +352,10 @@ pub fn plan(a: Allocator, msgs: []const Msg, reply: *const Reply, opening: ?usiz
for (left.items, reply.messages) |j, r| if (!matches(r, msgs[j], d)) return error.UnexpectedReply;
return .{ .keep = keep };
}
- if (left.items.len < recent_kept + 3 or reply.messages.len != recent_kept + 2) return error.UnexpectedReply;
+ // At least two messages summarized between the first and the kept tail.
+ if (reply.messages.len < 3 or left.items.len < reply.messages.len + 1) return error.UnexpectedReply;
const head = left.items[0];
- const tail = left.items[left.items.len - recent_kept ..];
+ const tail = left.items[left.items.len - (reply.messages.len - 2) ..];
if (!matches(reply.messages[0], msgs[head], d)) return error.UnexpectedReply;
for (tail, reply.messages[2..]) |j, r| if (!matches(r, msgs[j], d)) return error.UnexpectedReply;
var cut = tail[0];