Skip to content

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

Python

Don't blame the user for a cancel the ACP client marks as its own

justrach3055248b2cf6
diff --git a/src/acp_engine.zig b/src/acp_engine.zig
index 35fe46ce..395d7577 100644
--- a/src/acp_engine.zig
+++ b/src/acp_engine.zig
@@ -337,6 +337,7 @@ pub fn handleLine(d: *Dispatch, arena: Allocator, w: *Io.Writer, line: []const u
}
if (std.mem.eql(u8, req.method, "session/cancel")) {
cancel_flag.store(true, .release);
+ @import("cancel_source.zig").noteAcp(req.params);
if (on_cancel) |hook| hook();
if (req.id != null) return respond(w, req, struct {}{});
return;
diff --git a/src/acp_inbox.zig b/src/acp_inbox.zig
index 50dd1168..7423114f 100644
--- a/src/acp_inbox.zig
+++ b/src/acp_inbox.zig
@@ -22,6 +22,8 @@ pub const Inbox = struct {
eof: bool = false,
session_id: ?[]u8 = null,
cancelled: bool = false,
+ /// Who the pending cancel came from (#1611).
+ cancel_kind: cancel_source.Source = .acp_cancel,
active: bool = false,
tick: bool = false,
future: ?Io.Future(void) = null,
@@ -92,7 +94,7 @@ pub const Inbox = struct {
self.mutex.lockUncancelable(self.io);
defer self.mutex.unlock(self.io);
self.active = true;
- if (self.cancelled) cancel_source.cancel(.acp_cancel);
+ if (self.cancelled) cancel_source.cancel(self.cancel_kind);
}
pub fn end(self: *Inbox) void {
@@ -129,7 +131,8 @@ pub const Inbox = struct {
if (self.session_id) |current| {
if (sid == null or std.mem.eql(u8, sid.?, current)) {
self.cancelled = true;
- if (self.active) cancel_source.cancel(.acp_cancel);
+ self.cancel_kind = cancel_source.fromAcp(r.params);
+ if (self.active) cancel_source.cancel(self.cancel_kind);
acp_ask.cancelIfWaiting();
if (self.permission) |bridge| bridge.cancel();
}
diff --git a/src/acp_live_turn.zig b/src/acp_live_turn.zig
index c7d5d216..b4058f27 100644
--- a/src/acp_live_turn.zig
+++ b/src/acp_live_turn.zig
@@ -66,7 +66,7 @@ pub const LiveTurn = struct {
}
pub fn signalCancel() void {
- cancel_source.cancel(.acp_cancel); // #728
+ cancel_source.cancel(cancel_source.takeAcp()); // #728, #1611
}
pub fn cancelled() bool {
diff --git a/src/cancel_source.zig b/src/cancel_source.zig
index c5b0cd10..cf93ad3c 100644
--- a/src/cancel_source.zig
+++ b/src/cancel_source.zig
@@ -26,6 +26,9 @@ pub const Source = enum(u8) {
json_cancel,
/// ACP `session/cancel`.
acp_cancel,
+ /// ACP `session/cancel` that its client marked as its own lifecycle,
+ /// not the user's (`_meta["graff/cancelSource"] = "client"`, #1611).
+ acp_client,
/// The fullscreen TUI's Esc / Ctrl+C / force / quit.
ui_cancel,
};
@@ -68,12 +71,37 @@ pub fn take(tracer: ?*Tracer) Source {
}
pub fn byUser(s: Source) bool {
- return s != .none and s != .review_deadline;
+ return s != .none and s != .review_deadline and s != .acp_client;
+}
+
+/// The source of an ACP `session/cancel`. A client that marks its own
+/// lifecycle cancels (Harness replacing or shutting down a run) is not
+/// blamed on the user; a bare cancel still reads as the user's (#1611).
+pub fn fromAcp(params: ?std.json.Value) Source {
+ const p = params orelse return .acp_cancel;
+ if (p != .object) return .acp_cancel;
+ const meta = p.object.get("_meta") orelse return .acp_cancel;
+ if (meta != .object) return .acp_cancel;
+ const v = meta.object.get("graff/cancelSource") orelse return .acp_cancel;
+ return if (v == .string and std.mem.eql(u8, v.string, "client")) .acp_client else .acp_cancel;
+}
+
+/// The engine's cancel hook takes no arguments, so the dispatcher notes the
+/// request's source here first and the hook reads it back.
+var acp_pending: std.atomic.Value(u8) = .init(@backingInt(Source.acp_cancel));
+
+pub fn noteAcp(params: ?std.json.Value) void {
+ acp_pending.store(@backingInt(fromAcp(params)), .release);
+}
+
+pub fn takeAcp() Source {
+ return @fromBackingInt(@intCast(acp_pending.swap(@backingInt(Source.acp_cancel), .acq_rel)));
}
/// The transcript marker appended as the (incomplete) assistant turn.
pub fn marker(s: Source) []const u8 {
if (s == .review_deadline) return "[review stopped: wall-time limit reached; findings are incomplete]";
+ if (s == .acp_client) return "[response ended early: stopped by the client, not the user]";
return if (byUser(s)) "[response interrupted by user]" else "[response ended early: cancelled by the harness, not the user]";
}
@@ -84,6 +112,7 @@ pub fn chrome(s: Source) []const u8 {
.esc_key => "✗ interrupted (esc)",
.force_steer => "✗ interrupted (force)",
.json_cancel, .acp_cancel, .ui_cancel => "✗ interrupted (cancel)",
+ .acp_client => "✗ ended early (the client stopped the run)",
.none => "✗ ended early (harness cancel — no user action recorded)",
};
}
@@ -136,3 +165,29 @@ test "#728: an unsourced cancel is labelled a harness cancel, never a user one"
try std.testing.expectEqualStrings("interrupted source=none", note(&buf, .none));
try std.testing.expectEqualStrings("interrupted source=acp_cancel", note(&buf, .acp_cancel));
}
+
+test "an ACP client's own cancel is not blamed on the user (#1611)" {
+ const a = std.testing.allocator;
+ const parse = struct {
+ fn f(alloc: std.mem.Allocator, text: []const u8) !std.json.Parsed(std.json.Value) {
+ return std.json.parseFromSlice(std.json.Value, alloc, text, .{});
+ }
+ }.f;
+ const client = try parse(a, "{\"sessionId\":\"s\",\"_meta\":{\"graff/cancelSource\":\"client\",\"graff/cancelReason\":\"replace: mailbox closed\"}}");
+ defer client.deinit();
+ const user = try parse(a, "{\"sessionId\":\"s\",\"_meta\":{\"graff/cancelSource\":\"user\"}}");
+ defer user.deinit();
+ const bare = try parse(a, "{\"sessionId\":\"s\"}");
+ defer bare.deinit();
+ try std.testing.expectEqual(Source.acp_client, fromAcp(client.value));
+ try std.testing.expectEqual(Source.acp_cancel, fromAcp(user.value));
+ try std.testing.expectEqual(Source.acp_cancel, fromAcp(bare.value));
+ try std.testing.expectEqual(Source.acp_cancel, fromAcp(null));
+ try std.testing.expect(!byUser(.acp_client));
+ try std.testing.expectEqualStrings("[response ended early: stopped by the client, not the user]", marker(.acp_client));
+ try std.testing.expectEqualStrings("[response interrupted by user]", marker(.acp_cancel));
+ // The engine hook reads the noted source once, then falls back to a bare cancel.
+ noteAcp(client.value);
+ try std.testing.expectEqual(Source.acp_client, takeAcp());
+ try std.testing.expectEqual(Source.acp_cancel, takeAcp());
+}