Skip to content

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

Python

ask_user: a question that comes back unanswered is not a cancel

justrachd1220f000555
diff --git a/src/acp_ask.zig b/src/acp_ask.zig
index 50c2714c..d071a2b1 100644
--- a/src/acp_ask.zig
+++ b/src/acp_ask.zig
@@ -12,12 +12,18 @@ var ready: Io.Condition = .init;
var io_slot: ?Io = null;
var gpa_slot: ?Allocator = null;
var text: ?[]u8 = null;
-var cancelled = false;
+var outcome: Outcome = .answered;
var pending = false;
var waiting = false;
+/// How the question ended (#1322). Only `answered` carries text. A client
+/// that returns the question with no reply is `unanswered`, not a refusal:
+/// the user may never have seen it.
+pub const Outcome = enum { answered, declined, dismissed, unanswered };
+
pub const Reply = struct {
text: []const u8,
+ outcome: Outcome,
cancelled: bool,
};
@@ -26,7 +32,7 @@ pub fn attach(io: Io, gpa: Allocator) void {
gpa_slot = gpa;
clearLocked();
pending = false;
- cancelled = false;
+ outcome = .answered;
waiting = false;
}
@@ -39,7 +45,7 @@ pub fn detach() void {
io_slot = null;
gpa_slot = null;
pending = false;
- cancelled = false;
+ outcome = .answered;
waiting = false;
}
@@ -51,31 +57,42 @@ fn clearLocked() void {
}
pub fn wait(arena: Allocator) !Reply {
- const lock_io = io_slot orelse return .{ .text = "", .cancelled = true };
+ const lock_io = io_slot orelse return .{ .text = "", .outcome = .dismissed, .cancelled = true };
mutex.lockUncancelable(lock_io);
defer mutex.unlock(lock_io);
waiting = true;
defer waiting = false;
while (!pending) ready.waitUncancelable(lock_io, &mutex);
pending = false;
- const was = cancelled;
- cancelled = false;
+ const how = outcome;
+ outcome = .answered;
const src = text;
text = null;
defer if (src) |t| if (gpa_slot) |gpa| gpa.free(t);
- if (was) return .{ .text = "", .cancelled = true };
- return .{ .text = try arena.dupe(u8, src orelse ""), .cancelled = false };
+ if (how != .answered) return .{ .text = "", .outcome = how, .cancelled = true };
+ return .{ .text = try arena.dupe(u8, src orelse ""), .outcome = .answered, .cancelled = false };
}
+/// An answer, or a user cancel. A blank non-cancelled answer is refused.
pub fn reply(answer: []const u8, cancel: bool) bool {
if (!cancel and std.mem.trim(u8, answer, " \t\r\n").len == 0) return false;
+ return post(if (cancel) .dismissed else .answered, answer);
+}
+
+/// The question ended without an answer (`declined`, `dismissed`, `unanswered`).
+pub fn replyWithout(how: Outcome) bool {
+ std.debug.assert(how != .answered);
+ return post(how, "");
+}
+
+fn post(how: Outcome, answer: []const u8) bool {
const io = io_slot orelse return false;
const gpa = gpa_slot orelse return false;
mutex.lockUncancelable(io);
defer mutex.unlock(io);
clearLocked();
- cancelled = cancel;
- if (!cancel) text = gpa.dupe(u8, answer) catch null;
+ outcome = how;
+ if (how == .answered) text = gpa.dupe(u8, answer) catch null;
pending = true;
ready.broadcast(io);
return true;
@@ -88,7 +105,7 @@ pub fn cancelIfWaiting() void {
defer mutex.unlock(io);
if (!waiting) return;
clearLocked();
- cancelled = true;
+ outcome = .dismissed;
pending = true;
ready.broadcast(io);
}
diff --git a/src/acp_elicit.zig b/src/acp_elicit.zig
index 46075dec..2066da99 100644
--- a/src/acp_elicit.zig
+++ b/src/acp_elicit.zig
@@ -115,16 +115,20 @@ pub fn accept(value: Value) bool {
if (!std.mem.startsWith(u8, id, request_prefix)) return false;
const n = std.fmt.parseInt(u64, id[request_prefix.len..], 10) catch return true;
if (n == 0 or pending.cmpxchgStrong(n, 0, .acq_rel, .acquire) != null) return true;
+ // An error response or a malformed result: nothing says the user saw it.
const result = value.object.get("result") orelse {
- _ = acp_ask.reply("", true);
+ _ = acp_ask.replyWithout(.unanswered);
return true;
};
- if (result == .object and std.mem.eql(u8, util.strFieldObj(result.object, "action") orelse "", "accept")) {
+ const action = if (result == .object) util.strFieldObj(result.object, "action") orelse "" else "";
+ if (std.mem.eql(u8, action, "accept")) {
if (result.object.get("content")) |content| if (content == .object) if (util.strFieldObj(content.object, "answer")) |answer|
if (acp_ask.reply(answer, false)) return true;
+ // #1322: accepted with no answer bound to the question.
+ _ = acp_ask.replyWithout(.unanswered);
+ return true;
}
- // decline, cancel, an empty answer, or a malformed result.
- _ = acp_ask.reply("", true);
+ _ = acp_ask.replyWithout(if (std.mem.eql(u8, action, "decline")) .declined else if (std.mem.eql(u8, action, "cancel")) .dismissed else .unanswered);
return true;
}
@@ -200,16 +204,27 @@ test "responses answer only the outstanding request" {
try testing.expect(!accept(try parse(a, "{\"id\":\"graff-permission-1\",\"result\":{}}")));
}
-test "decline and cancel end the question as cancelled" {
+test "decline, cancel, an empty accept and an error each end the question as themselves (#1322)" {
acp_ask.attach(testing.io, testing.allocator);
defer acp_ask.detach();
defer finish();
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const a = arena.allocator();
- var sink: Io.Writer.Allocating = .init(a);
- try writeRequest(&sink.writer, "s", "q", .null);
- const decline = try std.fmt.allocPrint(a, "{{\"id\":\"graff-elicit-{d}\",\"result\":{{\"action\":\"decline\"}}}}", .{pending.load(.acquire)});
- try testing.expect(accept(try parse(a, decline)));
- try testing.expect((try acp_ask.wait(a)).cancelled);
+ const cases = [_]struct { result: []const u8, want: acp_ask.Outcome }{
+ .{ .result = "\"result\":{\"action\":\"decline\"}", .want = .declined },
+ .{ .result = "\"result\":{\"action\":\"cancel\"}", .want = .dismissed },
+ .{ .result = "\"result\":{\"action\":\"accept\",\"content\":{}}", .want = .unanswered },
+ .{ .result = "\"result\":{\"action\":\"accept\",\"content\":{\"answer\":\" \"}}", .want = .unanswered },
+ .{ .result = "\"error\":{\"code\":-32601,\"message\":\"no\"}", .want = .unanswered },
+ };
+ for (cases) |c| {
+ var sink: Io.Writer.Allocating = .init(a);
+ try writeRequest(&sink.writer, "s", "q", .null);
+ const line = try std.fmt.allocPrint(a, "{{\"id\":\"graff-elicit-{d}\",{s}}}", .{ pending.load(.acquire), c.result });
+ try testing.expect(accept(try parse(a, line)));
+ const got = try acp_ask.wait(a);
+ try testing.expect(got.cancelled);
+ try testing.expectEqual(c.want, got.outcome);
+ }
}
diff --git a/src/ask_user.zig b/src/ask_user.zig
index 23fbbd2b..8879656c 100644
--- a/src/ask_user.zig
+++ b/src/ask_user.zig
@@ -50,7 +50,8 @@ pub fn askUser(self: *Agent, call: ToolCall) !ExecResult {
if (self.tracer) |tr| tr.note("ask_user_wait", @tagName(acp_elicit.mode));
const got = try acp_ask.wait(self.arena);
acp_elicit.finish();
- if (got.cancelled) return .{ .text = "user cancelled the follow-up", .is_error = true };
+ if (self.tracer) |tr| tr.note("ask_user_outcome", @tagName(got.outcome));
+ if (got.outcome != .answered) return .{ .text = noAnswerText(got.outcome), .is_error = true };
return finishAnswer(self, std.mem.trim(u8, got.text, " \t\r"));
}
const in = self.in.?;
@@ -96,6 +97,25 @@ pub fn askUser(self: *Agent, call: ToolCall) !ExecResult {
return finishAnswer(self, std.mem.trim(u8, raw, " \t\r"));
}
+/// #1322: only a user's explicit cancel reads as a cancel. A question the
+/// client returned with nothing bound to it may never have been shown, so
+/// the model must not take it as a refusal and end the turn.
+pub fn noAnswerText(outcome: acp_ask.Outcome) []const u8 {
+ return switch (outcome) {
+ .answered => unreachable,
+ .dismissed => "user cancelled the follow-up",
+ .declined => "the user declined to answer this question. Continue without it and do not ask it again.",
+ .unanswered => "no answer came back: the client returned the question without a reply, so the user may not have seen it. This is not a refusal. Continue with a reasonable, reversible assumption and state it, or ask the question in your reply.",
+ };
+}
+
+test "noAnswerText keeps cancel as cancel and never calls a lost question a cancel (#1322)" {
+ try std.testing.expectEqualStrings("user cancelled the follow-up", noAnswerText(.dismissed));
+ try std.testing.expect(std.mem.indexOf(u8, noAnswerText(.unanswered), "cancel") == null);
+ try std.testing.expect(std.mem.indexOf(u8, noAnswerText(.unanswered), "not a refusal") != null);
+ try std.testing.expect(std.mem.indexOf(u8, noAnswerText(.declined), "declined") != null);
+}
+
fn finishAnswer(self: *Agent, raw: []const u8) !ExecResult {
const answer = try self.arena.dupe(u8, raw);
vision.stageGuiImageAttachment(self, answer);