Skip to content

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

Python
32.6 KB
Raw Blame
//! In-process ACP loop: initialize / session/new / prompt / cancel.
//! No HTTP, no Agent, no `main.zig` — this is what `libgraff` and
//! `graff-core.wasm` compile (fx-shaped same-process embed).

const std = @import("std");
const Io = std.Io;
const Allocator = std.mem.Allocator;

const proto = @import("acp_protocol.zig");
const acp_workspace = @import("acp_workspace.zig");
const util = @import("util.zig");
const acp_auth = @import("acp_auth.zig");
const v2 = @import("acp_v2.zig");
const v2_prompt = @import("acp_v2_prompt.zig");

pub const parseRequest = proto.parseRequest;
pub const negotiateVersion = proto.negotiateVersion;
pub const flattenPrompt = proto.flattenPrompt;
pub const writeResult = proto.writeResult;
pub const writeError = proto.writeError;
pub const writeSessionUpdate = proto.writeSessionUpdate;
pub const err_method_not_found = proto.err_method_not_found;
pub const err_internal = proto.err_internal;
pub const err_auth_required = proto.err_auth_required;

/// Stamped by the CLI (`harness_version`) or the embed create() call.
pub var implementation_version: []const u8 = "0.0.0-embed";

pub var cancel_flag = std.atomic.Value(bool).init(false);
pub var on_cancel: ?*const fn () void = null;
/// CLI live ACP also watches `Agent.esc_cancel` (Esc / session/cancel).
pub var extra_cancelled: ?*const fn () bool = null;
/// #1285: session/prompt message ids this process already ran, by session.
/// Prompts dispatch one at a time, so no lock.
pub var resent: @import("resend.zig").Ledger = .{};

pub const TurnFn = *const fn (ctx: *anyopaque, arena: Allocator, text: []const u8) anyerror![]const u8;
pub const SlashFn = *const fn (ctx: *anyopaque, arena: Allocator, text: []const u8) anyerror!?[]const u8;
pub const AfterUserFn = *const fn (ctx: *anyopaque, arena: Allocator, text: []const u8, prompt: ?std.json.Value) void;
pub const BindSessionFn = *const fn (ctx: *anyopaque, session_id: []const u8) void;
pub const LoadSessionFn = *const fn (ctx: *anyopaque, arena: Allocator, w: *Io.Writer, req: proto.Request) anyerror!void;
/// Optional per-turn context meter: used and window tokens (plus the known
/// session cost) for the standard `usage_update` (acp_context_meter.zig).
/// Null when the embed has no live agent (pure in-process loop, tests).
pub const Meter = struct { used: u64, window: u64, cost_usd: ?f64 = null, savings: @import("context_savings.zig").Snapshot = .{} };
pub const MeterFn = *const fn (ctx: *anyopaque) Meter;
/// Vendor-method escape hatch: gets every request the core loop does not
/// claim; returns true when it answered (false falls through to -32601).
pub const ExtraFn = *const fn (ctx: *anyopaque, arena: Allocator, w: *Io.Writer, req: proto.Request) anyerror!bool;
pub const ConfigValue = struct { value: []const u8, name: []const u8 };
pub const ConfigOption = struct {
    id: []const u8 = "thought_level",
    name: []const u8 = "Thought Level",
    category: []const u8 = "thought_level",
    type: []const u8 = "select",
    currentValue: []const u8,
    options: []const ConfigValue,
};
pub const ConfigFn = *const fn (ctx: *anyopaque, arena: Allocator) anyerror!?ConfigOption;
pub const SetConfigFn = *const fn (ctx: *anyopaque, value: []const u8) anyerror!bool;
/// The request's `mcpServers` on session/new and session/load (acp_mcp_servers.zig).
pub const McpServersFn = *const fn (ctx: *anyopaque, arena: Allocator, params: ?std.json.Value) anyerror!void;

pub const Cancellation = struct { source: []const u8, message: []const u8 };

pub const Dispatch = struct {
    turn: TurnFn,
    ctx: *anyopaque,
    session_id: ?[]const u8 = null,
    seed: u64 = 0,
    created: u32 = 0,
    slash: ?SlashFn = null,
    /// Per-prompt completion intent; also gates goal/review expansion in live turns.
    commands: bool = true,
    after_user: ?AfterUserFn = null,
    bind_session: ?BindSessionFn = null,
    load_session: ?LoadSessionFn = null,
    /// Live CLI saves under this stable name; embeds retain generated IDs.
    durable_session_id: ?[]const u8 = null,
    /// #1529: the save the live session writes now; `/resume` can move it off
    /// the transport ID, and the prompt result names it (`respondStop`).
    durable_name: ?*const fn (ctx: *anyopaque) ?[]const u8 = null,
    meter: ?MeterFn = null,
    extra: ?ExtraFn = null,
    config: ?ConfigFn = null,
    set_config: ?SetConfigFn = null,
    /// The `model` option; only T3 Code's profile offers it (acp_model_option.zig).
    model_config: ?ConfigFn = null,
    set_model: ?SetConfigFn = null,
    mcp_servers: ?McpServersFn = null,
    error_message: ?*const fn (ctx: *anyopaque, err: anyerror) []const u8 = null,
    cancel_info: ?*const fn (ctx: *anyopaque) ?Cancellation = null,
    error_recovery: ?*const fn (ctx: *anyopaque, err: anyerror) ?@import("auth_recovery.zig").Recovery = null,
    /// Live CLI: adopt the client's cwd, report `_meta["graff/worktree"]` (acp_workspace.zig).
    workspace: ?acp_workspace.Env = null,
    /// ACP v1 draft subagent updates are opt-in at initialize.
    subagents: bool = false,
    /// Graff extension: detached children may outlive a prompt. The client
    /// must request it independently of the draft ACP child-session shape.
    background_subagents: bool = false,
    /// The current implementation streams live child activity; historical
    /// child replay is not yet available, so CLI leaves this off by default.
    draft_subagents_enabled: bool = false,
};

fn supportsSubagents(params: ?std.json.Value) bool {
    const root = params orelse return false;
    if (root != .object) return false;
    const caps = root.object.get("clientCapabilities") orelse return false;
    if (caps != .object) return false;
    const subagents = caps.object.get("subagents") orelse return false;
    return subagents == .object;
}

fn supportsBackgroundSubagents(params: ?std.json.Value) bool {
    const root = params orelse return false;
    if (root != .object) return false;
    const caps = root.object.get("clientCapabilities") orelse return false;
    if (caps != .object) return false;
    const meta = caps.object.get("_meta") orelse return false;
    if (meta != .object) return false;
    const value = meta.object.get("graff/backgroundSubagents") orelse return false;
    return value == .bool and value.bool;
}

pub fn configOptions(d: *Dispatch, arena: Allocator) ![]const ConfigOption {
    var items: std.ArrayList(ConfigOption) = .empty;
    if (d.model_config) |model| if (try model(d.ctx, arena)) |option| try items.append(arena, option);
    if (d.config) |config| if (try config(d.ctx, arena)) |option| try items.append(arena, option);
    return items.items;
}

pub fn emitConfigChange(d: *Dispatch, arena: Allocator, w: *Io.Writer, sid: []const u8, before: []const ConfigOption) !void {
    if (d.config == null) return;
    const after = try configOptions(d, arena);
    if (@import("acp_model_option.zig").same(before, after)) return;
    try proto.writeNotification(w, "session/update", .{ .sessionId = sid, .update = .{
        .sessionUpdate = "config_option_update",
        .configOptions = after,
    } });
}

fn respond(w: *Io.Writer, req: proto.Request, result: anytype) !void {
    if (req.id == null) return;
    try writeResult(w, req.id, result);
}

pub fn respondError(w: *Io.Writer, req: proto.Request, code: i32, message: []const u8) !void {
    if (req.id == null) return;
    try writeError(w, req.id, code, message);
}

pub fn stripSgr(arena: Allocator, s: []const u8) ![]const u8 {
    var buf: std.array_list.Managed(u8) = .init(arena);
    var i: usize = 0;
    while (i < s.len) {
        if (s[i] == 0x1b and i + 1 < s.len and s[i + 1] == '[') {
            i += 2;
            while (i < s.len and (s[i] < '@' or s[i] > '~')) i += 1;
            if (i < s.len) i += 1;
            continue;
        }
        try buf.append(s[i]);
        i += 1;
    }
    return buf.items;
}

fn turnError(d: *Dispatch, w: *Io.Writer, req: proto.Request, err: anyerror) !void {
    if (err == error.Interrupted or err == error.Canceled) {
        if (d.cancel_info) |read| if (read(d.ctx)) |info|
            return respond(w, req, .{ .stopReason = "cancelled", ._meta = .{
                .@"graff/cancelSource" = info.source,
                .@"graff/cancelMessage" = info.message,
            } });
        return respond(w, req, .{ .stopReason = "cancelled" });
    }
    if (err == error.RunBudgetExhausted)
        return respond(w, req, .{ .stopReason = "max_turn_requests" });
    const message = if (d.error_message) |describe| describe(d.ctx, err) else @errorName(err);
    if (d.error_recovery) |recover| if (recover(d.ctx, err)) |data| {
        if (req.id != null) try proto.writeErrorData(w, req.id, data.rpcCode(), message, data);
        return;
    };
    return respondError(w, req, err_internal, message);
}

fn promptTurn(d: *Dispatch, arena: Allocator, w: *Io.Writer, req: proto.Request) !void {
    if (v2.on()) return v2_prompt.promptTurn(d, arena, w, req);
    d.commands = @import("acp_command_intent.zig").selected(req.params);
    const obj: ?std.json.ObjectMap = if (req.params) |p| (if (p == .object) p.object else null) else null;
    const sid = blk: {
        if (obj) |o| if (util.strFieldObj(o, "sessionId")) |s| break :blk s;
        break :blk d.session_id orelse "";
    };
    // #1285: a resent prompt this session already ran answers with that turn's
    // stop reason instead of starting another. A failed turn is not recorded.
    const resend = @import("resend.zig");
    const resend_key: ?u64 = if (obj) |o| (if (resend.acpId(o)) |mid| resend.Ledger.keyOf(sid, mid) else null) else null;
    if (resend_key) |key| if (resent.finished(key)) |prior| return respond(w, req, .{ .stopReason = prior.stopReason() });
    if (resend_key) |key| resent.begin(key, 0);
    if (d.bind_session) |bind| bind(d.ctx, sid);
    @import("acp_citations.zig").endTurn(sid); // a failed turn must not leave a marker open
    const config_before = configOptions(d, arena) catch |err| return turnError(d, w, req, err);
    acp_workspace.snapshot(d.workspace);
    // Before slash handling: an agent's room line must not run as a command.
    const flat = try flattenPrompt(arena, if (obj) |o| o.get("prompt") else null);
    const text = try @import("acp_room.zig").frame(arena, req.params, flat);
    if (if (d.commands) d.slash else null) |slash| {
        const reply = slash(d.ctx, arena, @import("acp_t3.zig").slashText(if (obj) |o| o.get("prompt") else null, flat, text)) catch |err| {
            emitConfigChange(d, arena, w, sid, config_before) catch |config_err| return turnError(d, w, req, config_err);
            return turnError(d, w, req, err);
        };
        if (reply) |plain| {
            if (plain.len > 0) try writeSessionUpdate(w, sid, try @import("acp_t3.zig").slashReply(arena, plain));
            emitConfigChange(d, arena, w, sid, config_before) catch |config_err| return turnError(d, w, req, config_err);
            try acp_workspace.emitChange(d.workspace, arena, w, sid);
            try emitMeter(d, w, sid);
            if (resend_key) |key| resent.finish(key, 0, "end_turn");
            return respondStop(d, w, req, sid, "end_turn");
        }
    }
    if (d.after_user) |after| after(d.ctx, arena, text, if (obj) |o| o.get("prompt") else null);
    const final = d.turn(d.ctx, arena, text) catch |err| {
        emitConfigChange(d, arena, w, sid, config_before) catch |config_err| return turnError(d, w, req, config_err);
        return turnError(d, w, req, err);
    };
    if (final.len > 0) try writeSessionUpdate(w, sid, final);
    emitConfigChange(d, arena, w, sid, config_before) catch |config_err| return turnError(d, w, req, config_err);
    try acp_workspace.emitChange(d.workspace, arena, w, sid);
    try emitMeter(d, w, sid);
    const extra = if (extra_cancelled) |f| f() else false;
    const stop: []const u8 = if (cancel_flag.load(.acquire) or extra) "cancelled" else "end_turn";
    if (resend_key) |key| resent.finish(key, 0, stop);
    try respondStop(d, w, req, sid, stop);
}

/// The save the session writes, when it is no longer the transport ID `sid`.
pub fn movedDurableName(d: *const Dispatch, sid: []const u8) ?[]const u8 {
    const current = d.durable_name orelse return null;
    const name = current(d.ctx) orelse return null;
    if (std.mem.eql(u8, name, sid)) return null;
    return name;
}

/// A prompt's result. When a command moved the session onto another save, the
/// result names it and the dispatch's durable identity follows (#1529); the
/// transport ID keeps routing this connection.
fn respondStop(d: *Dispatch, w: *Io.Writer, req: proto.Request, sid: []const u8, stop: []const u8) !void {
    if (movedDurableName(d, sid)) |name| {
        d.durable_session_id = name;
        return respond(w, req, .{ .stopReason = stop, ._meta = .{ .@"graff/durableSessionId" = name } });
    }
    return respond(w, req, .{ .stopReason = stop });
}

pub fn emitMeter(d: *Dispatch, w: *Io.Writer, sid: []const u8) !void {
    if (d.meter) |meter| {
        // Report live occupancy independently of the model catalog.
        const m = meter(d.ctx);
        if (m.window > 0) try @import("acp_context_meter.zig").write(w, sid, m.used, m.window, m.cost_usd, m.savings);
    }
}

fn respondInitialize(w: *Io.Writer, req: proto.Request, can_load: bool) !void {
    if (v2.on()) return respond(w, req, v2.initializeResult(req.params, can_load, implementation_version));
    return respond(w, req, .{
        .protocolVersion = negotiateVersion(req.params),
        .agentCapabilities = .{
            .loadSession = can_load,
            .sessionCapabilities = proto.SessionCapabilities{ .fork = can_load },
            ._meta = .{ .@"codegraff/usage" = can_load, .@"graff/backgroundSubagents" = true },
            .promptCapabilities = proto.PromptCapabilities{ .image = can_load },
            // session/new and session/load connect a client's MCP servers
            // (stdio always; http advertised; sse not run).
            .mcpCapabilities = .{ .http = true, .sse = false },
        },
        .agentInfo = proto.AgentImplementation{ .version = implementation_version },
        .agentImplementation = proto.AgentImplementation{ .version = implementation_version },
        .authMethods = acp_auth.advertised,
    });
}

/// Credential-free ACP bootstrap. Initialize is byte-for-byte the full
/// engine response; every request that needs a live Agent is auth-gated.
pub fn handlePreAuthLine(arena: Allocator, w: *Io.Writer, line: []const u8) !void {
    const req = parseRequest(arena, line) orelse return;
    if (std.mem.eql(u8, req.method, "initialize")) {
        v2.negotiate(req.params, 0);
        @import("acp_compaction.zig").noteInitialize(req.params);
        return respondInitialize(w, req, false);
    }
    return respondError(w, req, err_auth_required, acp_auth.required_message);
}

pub fn handleLine(d: *Dispatch, arena: Allocator, w: *Io.Writer, line: []const u8) !void {
    const req = parseRequest(arena, line) orelse return;
    if (std.mem.eql(u8, req.method, "initialize")) {
        v2.negotiate(req.params, d.seed);
        @import("acp_t3.zig").note(req.params);
        @import("acp_elicit.zig").configure(req.params);
        @import("acp_compaction.zig").noteInitialize(req.params);
        // The draft child-session RFD is a v1 shape; v2 gets tool-call progress.
        d.subagents = !v2.on() and supportsSubagents(req.params);
        d.background_subagents = supportsBackgroundSubagents(req.params);
        return respondInitialize(w, req, d.load_session != null);
    }
    if (std.mem.eql(u8, req.method, "authenticate"))
        return respondError(w, req, err_method_not_found, "terminal auth is out of band: re-spawn graff login");
    if (std.mem.eql(u8, req.method, "session/new")) {
        if (d.load_session != null and d.session_id != null)
            return respondError(w, req, -32000, "This ACP process already owns a session");
        // The client's MCP servers join before the reply: its first prompt
        // must already see their tools. A bad entry never fails the session.
        if (d.workspace) |env| acp_workspace.adopt(env, arena, req.params) catch |err|
            return respondError(w, req, -32602, acp_workspace.errorText(err));
        if (d.mcp_servers) |attach| attach(d.ctx, arena, req.params) catch {};
        d.created += 1;
        d.session_id = d.durable_session_id orelse try std.fmt.allocPrint(arena, "acp-{x}-{d}", .{ d.seed, d.created });
        if (d.config != null) {
            const options = configOptions(d, arena) catch |err| return respondError(w, req, err_internal, @errorName(err));
            if (d.workspace) |env|
                try respond(w, req, .{ .sessionId = d.session_id.?, .configOptions = options, ._meta = acp_workspace.meta(env, arena) })
            else
                try respond(w, req, .{ .sessionId = d.session_id.?, .configOptions = options });
        } else if (d.workspace) |env|
            try respond(w, req, .{ .sessionId = d.session_id.?, ._meta = acp_workspace.meta(env, arena) })
        else
            try respond(w, req, .{ .sessionId = d.session_id.? });
        try proto.writeAvailableCommands(w, d.session_id.?, try @import("acp_t3.zig").commands(arena, proto.slashCommands()));
        return;
    }
    if ((d.load_session != null and (std.mem.eql(u8, req.method, "session/prompt") or std.mem.eql(u8, req.method, "session/cancel") or std.mem.eql(u8, req.method, "session/close"))) or
        (d.set_config != null and std.mem.eql(u8, req.method, "session/set_config_option")))
    {
        const params = req.params orelse return respondError(w, req, -32602, "Invalid session ID");
        if (params != .object) return respondError(w, req, -32602, "Invalid session ID");
        const sid = util.strFieldObj(params.object, "sessionId") orelse return respondError(w, req, -32602, "Invalid session ID");
        if (d.session_id == null or !std.mem.eql(u8, sid, d.session_id.?))
            return respondError(w, req, -32602, "Unknown session ID");
    }
    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();
        return respond(w, req, struct {}{});
    }
    if (v2.on() and std.mem.eql(u8, req.method, "session/close")) return respond(w, req, struct {}{});
    if (std.mem.eql(u8, req.method, if (v2.on()) "session/resume" else "session/load")) {
        if (d.load_session) |load| {
            if (d.mcp_servers) |attach| attach(d.ctx, arena, req.params) catch {};
            return load(d.ctx, arena, w, req);
        }
    }
    if (std.mem.eql(u8, req.method, "session/set_config_option") and d.set_config != null) {
        const params = req.params orelse return respondError(w, req, -32602, "Invalid configuration option");
        if (params != .object) return respondError(w, req, -32602, "Invalid configuration option");
        const id = util.strFieldObj(params.object, "configId") orelse return respondError(w, req, -32602, "Invalid configuration option");
        const value = util.strFieldObj(params.object, "value") orelse return respondError(w, req, -32602, "Invalid configuration value");
        const setter = if (std.mem.eql(u8, id, "thought_level")) d.set_config.? else if (std.mem.eql(u8, id, "model")) d.set_model orelse return respondError(w, req, -32602, "Invalid configuration value") else return respondError(w, req, -32602, "Invalid configuration value");
        const accepted = setter(d.ctx, value) catch |err| return respondError(w, req, err_internal, @errorName(err));
        if (!accepted) return respondError(w, req, -32602, "Invalid configuration value");
        const options = configOptions(d, arena) catch |err| return respondError(w, req, err_internal, @errorName(err));
        return respond(w, req, .{ .configOptions = options });
    }
    if (std.mem.eql(u8, req.method, "session/prompt")) return promptTurn(d, arena, w, req);
    if (d.extra) |extra| if (try extra(d.ctx, arena, w, req)) return;
    if (req.id == null) return;
    try writeError(w, req.id, err_method_not_found, try std.fmt.allocPrint(arena, "method not found: {s}", .{req.method}));
}

fn echoTurn(_: *anyopaque, arena: Allocator, text: []const u8) anyerror![]const u8 {
    return std.fmt.allocPrint(arena, "echo:{s}", .{text});
}

test "in-process handleLine speaks the same initialize / new / prompt envelopes" {
    implementation_version = "embed-test";
    cancel_flag.store(false, .release);
    var state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer state.deinit();
    const a = state.allocator();
    var buf: [16384]u8 = undefined; // session/new advertises the whole command catalog
    var w: Io.Writer = .fixed(&buf);
    var d: Dispatch = .{ .turn = echoTurn, .ctx = undefined, .seed = 0xabc };

    try handleLine(&d, a, &w, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{\"protocolVersion\":9}}");
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"protocolVersion\":1") != null);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"name\":\"graff\"") != null);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "embed-test") != null);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"authMethods\"") != null);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "graff-login") != null);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"type\":\"terminal\"") != null);

    w = .fixed(&buf);
    try handleLine(&d, a, &w, "{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"session/new\"}");
    try std.testing.expectEqualStrings("acp-abc-1", d.session_id.?);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "available_commands_update") != null);

    w = .fixed(&buf);
    try handleLine(&d, a, &w, "{\"id\":3,\"method\":\"session/prompt\",\"params\":{\"prompt\":[{\"type\":\"text\",\"text\":\"ping\"}]}}");
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"text\":\"echo:ping\"") != null);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"stopReason\":\"end_turn\"") != null);
}

test "subagent draft updates require explicit client capability" {
    var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena.deinit();
    var buf: [8192]u8 = undefined;
    var out: Io.Writer = .fixed(&buf);
    var dispatch: Dispatch = .{ .turn = echoTurn, .ctx = undefined };
    try handleLine(&dispatch, arena.allocator(), &out, "{\"id\":1,\"method\":\"initialize\",\"params\":{\"clientCapabilities\":{}}}");
    try std.testing.expect(!dispatch.subagents);
    out = .fixed(&buf);
    try handleLine(&dispatch, arena.allocator(), &out, "{\"id\":2,\"method\":\"initialize\",\"params\":{\"clientCapabilities\":{\"subagents\":{}}}}");
    try std.testing.expect(dispatch.subagents);
    out = .fixed(&buf);
    try handleLine(&dispatch, arena.allocator(), &out, "{\"id\":3,\"method\":\"initialize\",\"params\":{\"clientCapabilities\":{\"subagents\":null}}}");
    try std.testing.expect(!dispatch.subagents);
}

test "background child extension negotiates independently of draft sessions" {
    var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena.deinit();
    var buf: [2048]u8 = undefined;
    var out: Io.Writer = .fixed(&buf);
    var dispatch: Dispatch = .{ .turn = echoTurn, .ctx = undefined };
    try handleLine(&dispatch, arena.allocator(), &out, "{\"id\":1,\"method\":\"initialize\",\"params\":{\"clientCapabilities\":{\"_meta\":{\"graff/backgroundSubagents\":true}}}}");
    try std.testing.expect(dispatch.background_subagents);
    try std.testing.expect(!dispatch.subagents);
    try std.testing.expect(std.mem.indexOf(u8, out.buffered(), "\"graff/backgroundSubagents\":true") != null);
    out = .fixed(&buf);
    try handleLine(&dispatch, arena.allocator(), &out, "{\"id\":2,\"method\":\"initialize\",\"params\":{\"clientCapabilities\":{\"_meta\":{\"graff/backgroundSubagents\":false}}}}");
    try std.testing.expect(!dispatch.background_subagents);
}

test "session/new: embeds send no root cwd; a relative client cwd is rejected" {
    var state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer state.deinit();
    var buf: [32768]u8 = undefined;
    var w: Io.Writer = .fixed(&buf);
    var d: Dispatch = .{ .turn = echoTurn, .ctx = undefined, .seed = 0x11 };
    try handleLine(&d, state.allocator(), &w, "{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"session/new\"}");
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"sessionId\":\"acp-11-1\"") != null);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"cwd\"") == null);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "graff/worktree") == null);
    w = .fixed(&buf);
    var live: Dispatch = .{ .turn = echoTurn, .ctx = undefined, .seed = 0x12, .workspace = .{ .gpa = std.testing.allocator, .io = std.testing.io } };
    try handleLine(&live, state.allocator(), &w, "{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"session/new\",\"params\":{\"cwd\":\"relative/dir\"}}");
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "Session cwd must be an absolute path") != null);
    try std.testing.expect(live.session_id == null);
}

test "in-process embed can still create a second session" {
    var state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer state.deinit();
    const a = state.allocator();
    var buf: [32768]u8 = undefined;
    var w: Io.Writer = .fixed(&buf);
    var d: Dispatch = .{ .turn = echoTurn, .ctx = undefined, .seed = 0x22 };
    try handleLine(&d, a, &w, "{\"id\":1,\"method\":\"session/new\"}");
    try std.testing.expectEqualStrings("acp-22-1", d.session_id.?);
    w = .fixed(&buf);
    try handleLine(&d, a, &w, "{\"id\":2,\"method\":\"session/new\"}");
    try std.testing.expectEqualStrings("acp-22-2", d.session_id.?);
}

test "ACP effort config validates sessions and values and notifies slash changes" {
    const Fixture = struct {
        level: []const u8 = "medium",
        narrow: bool = false,
        calls: usize = 0,
        fn config(ctx: *anyopaque, _: Allocator) anyerror!?ConfigOption {
            const self: *@This() = @ptrCast(@alignCast(ctx));
            const broad = &[_]ConfigValue{ .{ .value = "low", .name = "Low" }, .{ .value = "medium", .name = "Medium" }, .{ .value = "high", .name = "High" } };
            const narrow = &[_]ConfigValue{ .{ .value = "low", .name = "Low" }, .{ .value = "medium", .name = "Medium" } };
            return .{ .currentValue = self.level, .options = if (self.narrow) narrow else broad };
        }
        fn set(ctx: *anyopaque, value: []const u8) anyerror!bool {
            const self: *@This() = @ptrCast(@alignCast(ctx));
            for (if (self.narrow) &[_][]const u8{ "low", "medium" } else &[_][]const u8{ "low", "medium", "high" }) |allowed| {
                if (std.mem.eql(u8, value, allowed)) {
                    self.level = allowed;
                    return true;
                }
            }
            return false;
        }
        fn slash(ctx: *anyopaque, _: Allocator, text: []const u8) anyerror!?[]const u8 {
            const self: *@This() = @ptrCast(@alignCast(ctx));
            if (std.mem.eql(u8, text, "/effort low")) {
                self.level = "low";
                return "effort changed";
            }
            if (std.mem.eql(u8, text, "/model narrow")) {
                self.narrow = true;
                return "model changed";
            }
            return null;
        }
        fn turn(ctx: *anyopaque, _: Allocator, _: []const u8) anyerror![]const u8 {
            const self: *@This() = @ptrCast(@alignCast(ctx));
            self.calls += 1;
            return "done";
        }
    };
    var state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer state.deinit();
    const a = state.allocator();
    var fixture: Fixture = .{};
    var d: Dispatch = .{ .turn = Fixture.turn, .ctx = &fixture, .seed = 1, .config = Fixture.config, .set_config = Fixture.set, .slash = Fixture.slash };
    var buf: [32768]u8 = undefined;
    var w: Io.Writer = .fixed(&buf);
    try handleLine(&d, a, &w, "{\"id\":1,\"method\":\"session/set_config_option\",\"params\":{\"sessionId\":\"acp-1-1\",\"configId\":\"thought_level\",\"value\":\"high\"}}");
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "Unknown session ID") != null);
    w = .fixed(&buf);
    try handleLine(&d, a, &w, "{\"id\":2,\"method\":\"session/new\"}");
    const setup = try std.json.parseFromSliceLeaky(std.json.Value, a, std.mem.sliceTo(w.buffered(), '\n'), .{});
    const option = setup.object.get("result").?.object.get("configOptions").?.array.items[0].object;
    try std.testing.expectEqualStrings("thought_level", option.get("category").?.string);
    try std.testing.expectEqualStrings("medium", option.get("currentValue").?.string);
    try std.testing.expectEqual(@as(usize, 3), option.get("options").?.array.items.len);
    for ([_][]const u8{
        "{\"id\":3,\"method\":\"session/set_config_option\",\"params\":{\"sessionId\":\"wrong\",\"configId\":\"thought_level\",\"value\":\"high\"}}",
        "{\"id\":4,\"method\":\"session/set_config_option\",\"params\":{\"sessionId\":\"acp-1-1\",\"configId\":\"other\",\"value\":\"high\"}}",
        "{\"id\":5,\"method\":\"session/set_config_option\",\"params\":{\"sessionId\":\"acp-1-1\",\"configId\":\"thought_level\",\"value\":\"ultra\"}}",
        "{\"id\":6,\"method\":\"session/set_config_option\",\"params\":{\"sessionId\":\"acp-1-1\",\"configId\":\"thought_level\",\"value\":true}}",
    }) |request| {
        w = .fixed(&buf);
        try handleLine(&d, a, &w, request);
        try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"code\":-32602") != null);
        try std.testing.expectEqualStrings("medium", fixture.level);
    }
    w = .fixed(&buf);
    try handleLine(&d, a, &w, "{\"id\":7,\"method\":\"session/set_config_option\",\"params\":{\"sessionId\":\"acp-1-1\",\"configId\":\"thought_level\",\"value\":\"high\"}}");
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"currentValue\":\"high\"") != null);
    try std.testing.expectEqual(@as(usize, 0), fixture.calls);
    w = .fixed(&buf);
    try handleLine(&d, a, &w, "{\"id\":8,\"method\":\"session/prompt\",\"params\":{\"prompt\":[{\"type\":\"text\",\"text\":\"/effort low\"}]}}");
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "config_option_update") != null);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"currentValue\":\"low\"") != null);
    try std.testing.expectEqual(@as(usize, 0), fixture.calls);
    w = .fixed(&buf);
    try handleLine(&d, a, &w, "{\"id\":9,\"method\":\"session/prompt\",\"params\":{\"prompt\":[{\"type\":\"text\",\"text\":\"/model narrow\"}]}}");
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "config_option_update") != null);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"value\":\"high\"") == null);
}

test "authenticate names the out-of-band terminal login" {
    var state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer state.deinit();
    var buf: [512]u8 = undefined;
    var w: Io.Writer = .fixed(&buf);
    var d: Dispatch = .{ .turn = echoTurn, .ctx = undefined };
    try handleLine(&d, state.allocator(), &w, "{\"jsonrpc\":\"2.0\",\"id\":9,\"method\":\"authenticate\",\"params\":{\"methodId\":\"graff-login\"}}");
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"code\":-32601") != null);
    try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "graff login") != null);
}

test "stripSgr drops CSI sequences" {
    var state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer state.deinit();
    try std.testing.expectEqualStrings("ok", try stripSgr(state.allocator(), "\x1b[2mok\x1b[0m"));
}

test "ACP failure carries its reason without claiming completion and the next prompt still works" {
    const Fixture = struct {
        fn fail(_: *anyopaque, _: Allocator, _: []const u8) anyerror![]const u8 {
            return error.ApiError;
        }
        fn message(_: *anyopaque, _: anyerror) []const u8 {
            return "Connection closed after tool results";
        }
    };
    var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena.deinit();
    var buf: [2048]u8 = undefined;
    var writer: Io.Writer = .fixed(&buf);
    var dispatch: Dispatch = .{ .turn = Fixture.fail, .ctx = undefined, .error_message = Fixture.message };
    const request = "{\"id\":1,\"method\":\"session/prompt\",\"params\":{\"prompt\":[{\"type\":\"text\",\"text\":\"check\"}]}}";
    try handleLine(&dispatch, arena.allocator(), &writer, request);
    try std.testing.expect(std.mem.indexOf(u8, writer.buffered(), "Connection closed after tool results") != null);
    try std.testing.expect(std.mem.indexOf(u8, writer.buffered(), "stopReason") == null);
    writer = .fixed(&buf);
    dispatch.turn = echoTurn;
    try handleLine(&dispatch, arena.allocator(), &writer, request);
    try std.testing.expect(std.mem.indexOf(u8, writer.buffered(), "end_turn") != null);
}