Skip to content

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

Python

Keep empty output-limit replies out of continuation history

justrach34b5a6f6b140
diff --git a/scripts/test-headless-durability.py b/scripts/test-headless-durability.py
index 96decd3f..2c60d7c6 100644
--- a/scripts/test-headless-durability.py
+++ b/scripts/test-headless-durability.py
@@ -60,6 +60,13 @@ def run_case(binary, script, code=0, reason="completed", extra=(), resume=False,
assert (work / "result.txt").read_text() == "saved\n"
assert len(model.requests) == 3, model.requests
assert "increment" in json.dumps(model.requests[1]).lower() or "pieces" in json.dumps(model.requests[1]).lower(), model.requests[1]
+ for request in model.requests[1:]:
+ for message in request.get("messages", []):
+ if message.get("role") == "assistant":
+ assert message.get("content") or message.get("tool_calls"), message
+ if script[0].get("stream_deltas"):
+ assert "Think briefly" in json.dumps(model.requests[1]), model.requests[1]
+ assert "unfinished thought" not in json.dumps(model.requests[1]), model.requests[1]
if resume:
assert (work / "result.txt").read_text() == "saved\n"
resumed = subprocess.run(command + ["--resume", final["session_id"], "-p", "Continue from the saved checkpoint."],
@@ -83,6 +90,9 @@ def main():
print("PASS plain stdout preserved; startup resume handle on stderr", flush=True)
run_case(binary, [{"text": "partial draft", "finish": "length"}, write, {"text": "saved"}])
print("PASS length continues through file write; one terminal JSON result", flush=True)
+ run_case(binary, [{"stream_deltas": [{"role": "assistant", "reasoning_content": "unfinished thought"}],
+ "finish": "length"}, write, {"text": "saved"}])
+ print("PASS reasoning-only length stop sends valid history and continues through file write", flush=True)
run_case(binary, [write, {"text": "saved"}], code=3, reason="budget_exhausted", extra=("--max-model-calls", "1"), resume=True)
print("PASS budget stop saves partial work and resumes by emitted session id", flush=True)
run_case(binary, [{"text": "Partial work remains; continue with more budget."}], code=3, reason="budget_exhausted", extra=("--max-model-calls", "1"))
diff --git a/src/agent_length.zig b/src/agent_length.zig
index 07d7e87d..a254f53c 100644
--- a/src/agent_length.zig
+++ b/src/agent_length.zig
@@ -96,8 +96,9 @@ pub const State = struct {
return error.OutputLengthExceeded;
}
var body: []const u8 = note;
- // Never route a reasoning-only length stop through empty-completion
- // rewind: preserve its history, but break the reasoning-budget loop.
+ // Keep the length allowance separate from empty-completion retries.
+ // Chat's non-replayable empty reply is excluded by stepOpenAI; still
+ // use its reasoning to break the reasoning-budget loop.
if (done) |text| if (std.mem.trim(u8, text, " \t\r\n").len == 0 and reasoningOnly(self.provider.kind, root)) {
body = try std.fmt.allocPrint(self.arena, "{s} {s}", .{ note, @import("agent_empty_completion.zig").reasoning_budget_note });
};
diff --git a/src/agent_length_tests.zig b/src/agent_length_tests.zig
index ff89097d..d03f2b9b 100644
--- a/src/agent_length_tests.zig
+++ b/src/agent_length_tests.zig
@@ -93,20 +93,45 @@ test "length continuation allowance stays bounded across tool progress and reset
try std.testing.expectEqual(@as(u8, 1), next_turn.continuations);
}
-test "length continuation preserves reasoning-only output instead of empty-completion rewind" {
+test "length continuation excludes empty chat replies from the next request without rewinding prior work" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
- var buffer: [4096]u8 = undefined;
+ var buffer: [16384]u8 = undefined;
var writer = std.Io.Writer.fixed(&buffer);
- var agent = fixture(arena.allocator(), &writer, .openai);
- var state: length.State = .{};
- const root = try parse(agent.arena, "{\"choices\":[{\"finish_reason\":\"length\",\"message\":{\"role\":\"assistant\",\"content\":null,\"reasoning_content\":\"unfinished thought\"}}]}");
- try std.testing.expect((try state.step(&agent, root)) == null);
- try std.testing.expectEqual(@as(usize, 2), agent.messages.items.len);
- try std.testing.expectEqualStrings("unfinished thought", agent.messages.items[0].object.get("reasoning_content").?.string);
- const serialized = try std.json.Stringify.valueAlloc(agent.arena, agent.messages.items[1], .{});
- try std.testing.expect(std.mem.indexOf(u8, serialized, "Think briefly") != null);
- try std.testing.expectEqual(@as(u8, 0), agent.empty_completion_retries);
+ for ([_][]const u8{ "deepseek", "codegraff" }) |provider| {
+ for ([_][]const u8{ "null", "\"\"", "\" \\n\"" }) |content| {
+ var agent = fixture(arena.allocator(), &writer, .openai);
+ agent.io = std.testing.io;
+ agent.provider.id = provider;
+ agent.provider.model = "deepseek-flash";
+ var state: length.State = .{};
+ try agent.messages.append(try messages.textMessage(agent.arena, "user", "Write the document"));
+ const prior = try parse(agent.arena, "{\"role\":\"assistant\",\"content\":\"partial document\",\"reasoning_content\":\"prior useful reasoning\"}");
+ try agent.messages.append(.{ .object = prior });
+ const raw = try std.fmt.allocPrint(agent.arena, "{{\"choices\":[{{\"finish_reason\":\"length\",\"message\":{{\"role\":\"assistant\",\"content\":{s},\"reasoning_content\":\"unfinished thought\",\"tool_calls\":[]}}}}]}}", .{content});
+ const root = try parse(agent.arena, raw);
+ // Even the last allowed reply must not poison saved/resumed history.
+ for (0..length.max_continuations) |_| try std.testing.expect((try state.step(&agent, root)) == null);
+ try std.testing.expectError(error.OutputLengthExceeded, state.step(&agent, root));
+ try std.testing.expectEqual(@as(usize, 2 + length.max_continuations), agent.messages.items.len);
+ try std.testing.expectEqual(length.max_continuations, state.continuations);
+ try std.testing.expectEqual(@as(u8, 0), agent.empty_completion_retries);
+ const body = try agent.buildBody(null, false, false, false);
+ defer std.testing.allocator.free(body);
+ try std.testing.expect(std.mem.indexOf(u8, body, "unfinished thought") == null);
+ try std.testing.expect(std.mem.indexOf(u8, body, "prior useful reasoning") != null);
+ try std.testing.expect(std.mem.indexOf(u8, body, "Think briefly") != null);
+ const request = try parse(agent.arena, body);
+ var assistants: usize = 0;
+ for (request.get("messages").?.array.items) |message| {
+ if (!std.mem.eql(u8, message.object.get("role").?.string, "assistant")) continue;
+ assistants += 1;
+ try std.testing.expectEqualStrings("partial document", message.object.get("content").?.string);
+ }
+ try std.testing.expectEqual(@as(usize, 1), assistants);
+ try std.testing.expectEqualStrings("saved the document", (try state.step(&agent, try parse(agent.arena, chat_done))).?);
+ }
+ }
}
test "length continuation respects run budgets and both engine cancellation flags without granting another call" {
@@ -160,7 +185,7 @@ test "length continuation never dispatches truncated tool calls and leaves repla
var buffer: [4096]u8 = undefined;
var writer = std.Io.Writer.fixed(&buffer);
const cases = [_]struct { kind: Kind, raw: []const u8 }{
- .{ .kind = .openai, .raw = "{\"choices\":[{\"finish_reason\":\"length\",\"message\":{\"role\":\"assistant\",\"content\":null,\"tool_calls\":[{\"id\":\"c\",\"type\":\"function\",\"function\":{\"name\":\"read_file\",\"arguments\":\"\"}}]}}]}" },
+ .{ .kind = .openai, .raw = "{\"choices\":[{\"finish_reason\":\"length\",\"message\":{\"role\":\"assistant\",\"content\":null,\"reasoning_content\":\"keep tool reasoning\",\"tool_calls\":[{\"id\":\"c\",\"type\":\"function\",\"function\":{\"name\":\"read_file\",\"arguments\":\"\"}}]}}]}" },
.{ .kind = .responses, .raw = "{\"incomplete_details\":{\"reason\":\"max_output_tokens\"},\"output\":[{\"type\":\"function_call\",\"call_id\":\"c\",\"name\":\"read_file\",\"arguments\":\"\"}]}" },
.{ .kind = .anthropic, .raw = "{\"stop_reason\":\"max_tokens\",\"content\":[{\"type\":\"tool_use\",\"id\":\"c\",\"name\":\"read_file\"}]}" },
};
@@ -174,6 +199,13 @@ test "length continuation never dispatches truncated tool calls and leaves repla
try std.testing.expect(std.mem.indexOf(u8, result, "truncated: the response ended") != null);
const history = try std.json.Stringify.valueAlloc(agent.arena, agent.messages.items[0], .{});
try std.testing.expect(std.mem.indexOf(u8, history, "graff_invalid_input") == null);
+ if (case.kind == .openai) {
+ const body = try agent.buildBody(null, false, false, false);
+ defer std.testing.allocator.free(body);
+ try std.testing.expect(std.mem.indexOf(u8, body, "keep tool reasoning") != null);
+ try std.testing.expect(std.mem.indexOf(u8, body, "\"tool_calls\"") != null);
+ try std.testing.expect(std.mem.indexOf(u8, body, "\"tool_call_id\":\"c\"") != null);
+ }
try std.testing.expectEqual(@as(u8, 1), state.continuations);
}
}
diff --git a/src/agent_steps.zig b/src/agent_steps.zig
index e527bf93..b3d27b5c 100644
--- a/src/agent_steps.zig
+++ b/src/agent_steps.zig
@@ -291,6 +291,20 @@ pub fn stepOpenAI(self: *Agent, root: std.json.ObjectMap) !?[]const u8 {
try self.sayApiError("api error: choice message was not an object", .{});
return error.ApiError;
};
+ // A length stop may spend its entire budget reasoning. Chat rejects
+ // replaying an assistant with neither content nor tool calls (#1646).
+ // Keep real partial answers and tool-call reasoning verbatim; only this
+ // empty reply stays out of history, including on budget/cancel exits.
+ const has_content = if (msg_obj.get("content")) |c| switch (c) {
+ .string => std.mem.trim(u8, c.string, " \t\r\n").len > 0,
+ .array => c.array.items.len > 0,
+ else => false,
+ } else false;
+ const has_calls = if (msg_obj.get("tool_calls")) |t| t == .array and t.array.items.len > 0 else false;
+ if (length.limited(.openai, root) and !has_content and !has_calls) {
+ self.pairContextMeterWithCurrentLocal();
+ return "";
+ }
try self.messages.append(message); // echo verbatim (content may be null)
var final_text: []const u8 = "";