Skip to content

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

Python

Reject duplicate live session creation without replacing history

Rach Pradhan0df505942245
diff --git a/docs/acp-client-parity.md b/docs/acp-client-parity.md
index 320667d2..6db42087 100644
--- a/docs/acp-client-parity.md
+++ b/docs/acp-client-parity.md
@@ -2,7 +2,7 @@
The live `graff acp` process speaks [ACP v1](https://agentclientprotocol.com/protocol/v1/overview). The desktop is its client: it sends prompts and renders `session/update` notifications. A streamed tool row is identified by `toolCallId`; updates change that row, and a new `content` array replaces its prior content. The turn ends when the original `session/prompt` request responds with a `stopReason`, including `cancelled` after cancellation. [Tool calls](https://agentclientprotocol.com/protocol/v1/tool-calls) and [prompt turns](https://agentclientprotocol.com/protocol/v1/prompt-turn) define those wire rules.
-The desktop restores its own saved chat view and starts a replacement agent with `--resume <name>`. Separately, a v1 client may use `session/load` when the live agent advertises `loadSession`: the session ID is a saved conversation name in the selected workspace, and the agent replays retained user messages, assistant messages, and tool results before responding. Replay is display history; it does not rerun tools or revive shell jobs. The in-process embed has no durable store and does not advertise `loadSession`. [ACP session setup](https://agentclientprotocol.com/protocol/v1/session-setup) specifies the capability and replay order.
+The desktop restores its own saved chat view and starts a replacement agent with `--resume <name>`. Separately, a v1 client may use `session/load` when the live agent advertises `loadSession`: the session ID is a saved conversation name in the selected workspace, and the agent replays retained user messages, assistant messages, and tool results before responding. Replay is display history; it does not rerun tools or revive shell jobs. The live process owns one active session and rejects a second `session/new`; start another process for another session. The in-process embed has no durable store and does not advertise `loadSession`. [ACP session setup](https://agentclientprotocol.com/protocol/v1/session-setup) specifies the capability and replay order.
The desktop defaults to `--yolo`; setting `GRAFF_YOLO=0` opts into client permission decisions. Pending permission requests must receive a response, including a `cancelled` outcome if the turn is cancelled. [ACP permission flow](https://agentclientprotocol.com/protocol/v1/tool-calls#requesting-permission) defines the request and outcomes. A transport event alone does not establish that the visible desktop control was exercised, so permission UI needs end-to-end verification.
diff --git a/scripts/test-acp-session-load.py b/scripts/test-acp-session-load.py
index 5953d2c4..8e4d4c17 100644
--- a/scripts/test-acp-session-load.py
+++ b/scripts/test-acp-session-load.py
@@ -126,6 +126,8 @@ def run(binary):
assert init["result"]["agentCapabilities"]["loadSession"] is True
sid = a.request("session/new", {"cwd": str(cwd), "mcpServers": []})["result"]["sessionId"]
assert "/" not in sid and "\\" not in sid
+ duplicate = a.request("session/new", {"cwd": str(cwd), "mcpServers": []})
+ assert duplicate["error"]["code"] == -32000
turn = a.request("session/prompt", {"sessionId": sid, "prompt": [{"type": "text", "text": "Original human request."}]}, 35)
assert turn["result"]["stopReason"] == "end_turn"
old_result = tool_text(first_model.requests[1])[-1]
@@ -166,6 +168,8 @@ def run(binary):
before = len(b.events)
loaded = b.request("session/load", {"sessionId": sid, "cwd": str(cwd), "mcpServers": []})
assert loaded.get("result") == {}, (created, loaded)
+ duplicate_after_load = b.request("session/new", {"cwd": str(cwd), "mcpServers": []})
+ assert duplicate_after_load["error"]["code"] == -32000
replay = b.events[before:]
updates = [event["params"]["update"] for event in replay if event.get("method") == "session/update"]
assert any(u.get("sessionUpdate") == "user_message_chunk" and u["content"].get("text") == "Original human request." for u in updates)
diff --git a/src/acp_engine.zig b/src/acp_engine.zig
index 4c61c53b..99c43f3d 100644
--- a/src/acp_engine.zig
+++ b/src/acp_engine.zig
@@ -183,6 +183,8 @@ pub fn handleLine(d: *Dispatch, arena: Allocator, w: *Io.Writer, line: []const u
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");
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.cwd.len > 0)
@@ -258,6 +260,20 @@ test "session/new reports an isolated checkout when Dispatch.cwd is set" {
try std.testing.expect(std.mem.indexOf(u8, w.buffered(), "\"cwd\":\"/repo/.graff/worktrees/session-1\"") != 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 "authenticate names the out-of-band terminal login" {
var state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer state.deinit();