Skip to content

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

Python
8.6 KB
Raw Blame
//! Who cancelled the turn (#728). `Agent.esc_cancel` is one process-wide
//! bool with six setters — a lone Esc on raw stdin, a double-Enter force
//! steer, a `--json` cancel line, an ACP session/cancel, the TUI's cancel —
//! and the turn-ending marker always read "[response interrupted by user]".
//! When the flag flips for any other reason, that label is wrong twice: it
//! blames the user, and it leaves no trace of the real source. Every setter
//! now goes through `cancel(source)`, and mainloop reads the source back: a
//! turn that ends cancelled with no recorded source is labelled a harness
//! cancel, not a user interrupt, and the source lands in the trace.

const std = @import("std");
const Agent = @import("agent.zig").Agent;
const main_mod = @import("main.zig");
const Tracer = @import("trace.zig").Tracer;

pub const Source = enum(u8) {
    /// Nobody recorded one: the harness ended the turn, not the user.
    none,
    /// An explicit review wall-time limit, never a user cancel.
    review_deadline,
    /// A lone Esc on the line REPL's raw stdin.
    esc_key,
    /// Double Enter with a queued steer line (escPressed marks it force).
    force_steer,
    /// --json `{"type":"cancel"}`.
    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,
};

var source: std.atomic.Value(u8) = .init(0);

/// Raise the flag and remember who did.
pub fn cancel(s: Source) void {
    source.store(@backingInt(s), .release);
    Agent.esc_cancel.store(true, .release);
}

/// A deadline must not replace an already recorded user cancellation.
pub fn cancelIfIdle(s: Source) void {
    if (source.cmpxchgStrong(0, @backingInt(s), .acq_rel, .acquire) == null)
        Agent.esc_cancel.store(true, .release);
}

/// A cancel that came off raw stdin: a double Enter with a queued steer
/// line is the force path, anything else is a real Esc.
pub fn cancelFromStdin() void {
    cancel(if (main_mod.g_force_interrupt) .force_steer else .esc_key);
}

/// Fresh turn: the flag and the source clear together.
pub fn clear() void {
    Agent.esc_cancel.store(false, .release);
    source.store(0, .release);
}

/// The source of the cancel that just ended a turn; consumed, and traced
/// (`interrupted source=…`) so the next false interrupt can be attributed.
pub fn take(tracer: ?*Tracer) Source {
    const s: Source = @fromBackingInt(@intCast(source.swap(0, .acq_rel)));
    if (tracer) |tr| {
        var buf: [48]u8 = undefined;
        tr.note("turn", note(&buf, s));
    }
    return s;
}

pub fn byUser(s: Source) bool {
    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]";
}

/// The yellow chrome line the line REPL prints.
pub fn chrome(s: Source) []const u8 {
    return switch (s) {
        .review_deadline => "✗ review stopped (wall-time limit; findings incomplete)",
        .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)",
    };
}

/// The --json error message.
pub fn jsonMessage(s: Source) []const u8 {
    if (s == .review_deadline) return "review wall-time limit reached; findings are incomplete";
    return if (byUser(s)) "turn cancelled" else "turn cancelled by the harness (no user cancel recorded)";
}

/// The trace note: `interrupted source=esc_key`.
pub fn note(buf: []u8, s: Source) []const u8 {
    return std.fmt.bufPrint(buf, "interrupted source={t}", .{s}) catch "interrupted";
}

test "cancel raises the flag with its source; take consumes; clear drops both" {
    clear();
    try std.testing.expectEqual(Source.none, take(null));
    cancel(.json_cancel);
    try std.testing.expect(Agent.esc_cancel.load(.acquire));
    try std.testing.expectEqual(Source.json_cancel, take(null));
    try std.testing.expectEqual(Source.none, take(null)); // consumed
    cancel(.acp_cancel);
    clear();
    try std.testing.expect(!Agent.esc_cancel.load(.acquire));
    try std.testing.expectEqual(Source.none, take(null));
}

test "cancelFromStdin is force after a double Enter, else Esc" {
    const saved = main_mod.g_force_interrupt;
    defer main_mod.g_force_interrupt = saved;
    main_mod.g_force_interrupt = true;
    cancelFromStdin();
    try std.testing.expectEqual(Source.force_steer, take(null));
    main_mod.g_force_interrupt = false;
    cancelFromStdin();
    try std.testing.expectEqual(Source.esc_key, take(null));
    clear();
}

test "#728: an unsourced cancel is labelled a harness cancel, never a user one" {
    try std.testing.expectEqualStrings("[response interrupted by user]", marker(.esc_key));
    try std.testing.expectEqualStrings("[response interrupted by user]", marker(.ui_cancel));
    try std.testing.expectEqualStrings("[response ended early: cancelled by the harness, not the user]", marker(.none));
    try std.testing.expect(std.mem.indexOf(u8, chrome(.none), "harness") != null);
    try std.testing.expect(std.mem.indexOf(u8, chrome(.force_steer), "force") != null);
    try std.testing.expect(std.mem.indexOf(u8, jsonMessage(.none), "harness") != null);
    try std.testing.expectEqualStrings("turn cancelled", jsonMessage(.json_cancel));
    var buf: [48]u8 = undefined;
    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());
}