Skip to content

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

Python

fix(vision): drop composer images the prompt no longer names

Cursor Agent7fd6fde0c2a9
diff --git a/TUI/image.zig b/TUI/image.zig
index 26ca0351..8b0c776d 100644
--- a/TUI/image.zig
+++ b/TUI/image.zig
@@ -36,7 +36,7 @@ pub fn render(self: *const Model, a: std.mem.Allocator, width: usize) ![]const u
// Pixels stay out of the cell stream: Kitty images sit above the grid and
// survived scroll as a gray slab. Metadata + open is enough to inspect.
_ = width;
- try out.appendSlice(try theme_mod.paint(a, th.muted, " y copy path · Enter open · Esc"));
+ try out.appendSlice(try theme_mod.paint(a, th.muted, " backspace detach · y copy path · Enter open · Esc"));
try out.append('\n');
return out.toOwnedSlice();
}
@@ -292,9 +292,48 @@ pub fn key(self: *Model, k: @import("key.zig").Key) bool {
self.setToast("path copied");
return true;
}
+ if (k == .backspace or k == .delete or (k == .char and (k.char == 'x' or k.char == 'd'))) {
+ if (dropPreviewed(self)) {
+ self.setToast("image detached");
+ return true;
+ }
+ }
return false;
}
+/// Backspace on an empty composer drops the last chip (#634).
+pub fn dropLast(self: *Model) bool {
+ if (self.images.items.len == 0) return false;
+ const last = self.images.pop() orelse return false;
+ forgetPreview(self, last);
+ self.alloc.free(last);
+ return true;
+}
+
+pub fn clearAll(self: *Model) void {
+ for (self.images.items) |p| {
+ forgetPreview(self, p);
+ self.alloc.free(p);
+ }
+ self.images.clearRetainingCapacity();
+}
+
+pub fn dropPreviewed(self: *Model) bool {
+ if (self.preview_n == 0 or self.preview_n > self.images.items.len) return false;
+ const path = self.images.orderedRemove(self.preview_n - 1);
+ forgetPreview(self, path);
+ self.alloc.free(path);
+ return true;
+}
+
+fn forgetPreview(self: *Model, path: []const u8) void {
+ if (!std.mem.eql(u8, self.preview_path, path)) return;
+ self.preview_path = "";
+ self.preview_n = 0;
+ self.preview_pin = false;
+ if (self.overlay == .image) self.closeOverlay();
+}
+
fn ioHandle() std.Io {
return std.Io.Threaded.global_single_threaded.io();
}
@@ -524,3 +563,18 @@ test "hover on a composer chip opens an unpinned preview" {
try std.testing.expectEqualStrings("/tmp/shot.png", m.preview_path);
try std.testing.expect(!m.preview_pin);
}
+
+test "overlay backspace detaches the previewed composer chip (#634)" {
+ var m: Model = undefined;
+ m.setup(std.testing.allocator);
+ defer m.deinit();
+ m.attachImage("/tmp/a.png");
+ m.attachImage("/tmp/b.png");
+ m.preview_path = m.images.items[0];
+ m.preview_n = 1;
+ m.openOverlay(.image);
+ try std.testing.expect(key(&m, .backspace));
+ try std.testing.expectEqual(@as(usize, 1), m.images.items.len);
+ try std.testing.expectEqualStrings("/tmp/b.png", m.images.items[0]);
+ try std.testing.expectEqual(app.Overlay.none, m.overlay);
+}
diff --git a/TUI/keys.zig b/TUI/keys.zig
index ae91bd54..651fac39 100644
--- a/TUI/keys.zig
+++ b/TUI/keys.zig
@@ -165,6 +165,15 @@ fn promptKey(self: *Model, k: Key) Effect {
return .stay;
}
}
+ if (k == .backspace and std.mem.trim(u8, self.input.getValue(), " \t").len == 0) {
+ if (@import("image.zig").dropLast(self)) {
+ self.setToast("image detached");
+ return .stay;
+ }
+ }
+ if ((k == .delete_to_start or isCtrl(k, 'u')) and self.images.items.len > 0) {
+ @import("image.zig").clearAll(self);
+ }
self.input.handle(k);
return .stay;
}
diff --git a/TUI/keys_tests.zig b/TUI/keys_tests.zig
index b8d2c447..3f25bc80 100644
--- a/TUI/keys_tests.zig
+++ b/TUI/keys_tests.zig
@@ -336,3 +336,40 @@ test "Ctrl+R after an image prompt restores the attachment (#577)" {
try std.testing.expectEqual(@as(usize, 1), m.images.items.len);
try std.testing.expectEqualStrings(live, m.images.items[0]);
}
+
+test "backspace on an empty composer detaches the last chip (#634)" {
+ var m: Model = undefined;
+ m.setup(std.testing.allocator);
+ defer m.deinit();
+ m.attachImage("/tmp/a.png");
+ m.attachImage("/tmp/b.png");
+ _ = handle(&m, .backspace);
+ try std.testing.expectEqual(@as(usize, 1), m.images.items.len);
+ try std.testing.expectEqualStrings("/tmp/a.png", m.images.items[0]);
+ _ = handle(&m, .delete_to_start);
+ try std.testing.expectEqual(@as(usize, 0), m.images.items.len);
+}
+
+test "backspace with typed text does not drop chips (#634)" {
+ var m: Model = undefined;
+ m.setup(std.testing.allocator);
+ defer m.deinit();
+ try m.input.setValue("hi");
+ m.input.cursor = 2;
+ m.attachImage("/tmp/shot.png");
+ _ = handle(&m, .backspace);
+ try std.testing.expectEqualStrings("h", m.input.getValue());
+ try std.testing.expectEqual(@as(usize, 1), m.images.items.len);
+}
+
+test "Ctrl+U clears chips with the draft (#634)" {
+ var m: Model = undefined;
+ m.setup(std.testing.allocator);
+ defer m.deinit();
+ try m.input.setValue("keep");
+ m.input.cursor = m.input.getValue().len;
+ m.attachImage("/tmp/shot.png");
+ _ = handle(&m, .{ .ctrl = 'u' });
+ try std.testing.expectEqualStrings("", m.input.getValue());
+ try std.testing.expectEqual(@as(usize, 0), m.images.items.len);
+}
diff --git a/TUI/overlaypane.zig b/TUI/overlaypane.zig
index 280fc66d..acb4cb14 100644
--- a/TUI/overlaypane.zig
+++ b/TUI/overlaypane.zig
@@ -220,7 +220,7 @@ const help_body =
\\ Ctrl+P command palette
\\ Shift+Tab Normal → Plan → Always-approve
\\ click / ← → collapse / expand tools
- \\ hover [Image] preview · y copy · Enter open
+ \\ hover [Image] preview · backspace detach · y copy · Enter open
\\ Shift+← → prev / next user turn
\\ Ctrl+J / K scroll one line
\\ Ctrl+N N new session
diff --git a/src/ask_user.zig b/src/ask_user.zig
index 0866468f..32c951be 100644
--- a/src/ask_user.zig
+++ b/src/ask_user.zig
@@ -76,6 +76,7 @@ pub fn askUser(self: *Agent, call: ToolCall) !ExecResult {
fn finishAnswer(self: *Agent, raw: []const u8) !ExecResult {
const answer = try self.arena.dupe(u8, raw);
vision.stageGuiImageAttachment(self, answer);
+ vision_queue.retainReferenced(self, answer);
// Pixels stay queued for flushPending after the text tool result. If the
// reply only has `[Image]` markers and nothing staged, say so instead of
// letting the model treat placeholders as screenshots.
diff --git a/src/commands_model.zig b/src/commands_model.zig
index 6b813e96..5a30df8e 100644
--- a/src/commands_model.zig
+++ b/src/commands_model.zig
@@ -538,7 +538,7 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,
return true;
}
if (std.mem.eql(u8, path, "clear")) {
- root.pending_image = null;
+ @import("vision_queue.zig").clear(root);
try out.writeAll("cleared the staged image\n");
try out.flush();
return true;
diff --git a/src/input_util.zig b/src/input_util.zig
index 29d2417f..c71898df 100644
--- a/src/input_util.zig
+++ b/src/input_util.zig
@@ -450,6 +450,31 @@ pub fn addMark(g: Allocator, ms: *std.ArrayList([]const u8), s: []const u8) void
ms.append(g, dup) catch g.free(dup);
}
+/// Insert `[Image #N] ` at the cursor and mark the chip for accent redraw.
+pub fn insertImageChip(g: Allocator, b: *std.ArrayList(u8), cur: *usize, ms: *std.ArrayList([]const u8), n: u8) void {
+ var tmp: [20]u8 = undefined;
+ const marker = std.fmt.bufPrint(&tmp, "[Image #{d}] ", .{n}) catch "[Image] ";
+ b.insertSlice(g, cur.*, marker) catch {};
+ cur.* += marker.len;
+ const chip_len = if (marker.len > 0 and marker[marker.len - 1] == ' ') marker.len - 1 else marker.len;
+ addMark(g, ms, marker[0..chip_len]);
+}
+
+/// Highlight every `[Image]` / `[Image #N]` span already in the buffer.
+pub fn markImageChips(g: Allocator, ms: *std.ArrayList([]const u8), text: []const u8) void {
+ var i: usize = 0;
+ while (i < text.len) {
+ if (std.mem.startsWith(u8, text[i..], "[Image")) {
+ if (std.mem.indexOfScalarPos(u8, text, i, ']')) |close| {
+ addMark(g, ms, text[i .. close + 1]);
+ i = close + 1;
+ continue;
+ }
+ }
+ i += 1;
+ }
+}
+
test "cleanDroppedPath unescapes terminal drops" {
const gpa = std.testing.allocator;
const p = cleanDroppedPath(gpa, "", "/tmp/My\\ File.txt ") orelse return error.TestUnexpectedResult;
@@ -462,6 +487,22 @@ test "cleanDroppedPath unescapes terminal drops" {
try std.testing.expect(cleanDroppedPath(gpa, "", "two\nlines") == null); // multi-line
}
+test "insertImageChip writes a numbered marker" {
+ const gpa = std.testing.allocator;
+ var buf: std.ArrayList(u8) = .empty;
+ defer buf.deinit(gpa);
+ var marks: std.ArrayList([]const u8) = .empty;
+ defer {
+ for (marks.items) |m| gpa.free(m);
+ marks.deinit(gpa);
+ }
+ var cur: usize = 0;
+ insertImageChip(gpa, &buf, &cur, &marks, 2);
+ try std.testing.expectEqualStrings("[Image #2] ", buf.items);
+ try std.testing.expectEqual(@as(usize, 1), marks.items.len);
+ try std.testing.expectEqualStrings("[Image #2]", marks.items[0]);
+}
+
test "binaryFileExt flags binaries, passes text" {
try std.testing.expect(binaryFileExt("paper.pdf"));
try std.testing.expect(binaryFileExt("shot.PNG")); // case-insensitive
diff --git a/src/readline.zig b/src/readline.zig
index 965d59f7..838f6887 100644
--- a/src/readline.zig
+++ b/src/readline.zig
@@ -45,6 +45,8 @@ const delRange = input_util.delRange;
const prevWord = input_util.prevWord;
const nextWord = input_util.nextWord;
const addMark = input_util.addMark;
+const insertImageChip = input_util.insertImageChip;
+const markImageChips = input_util.markImageChips;
const util = @import("util.zig");
const main_mod = @import("main.zig");
@@ -287,10 +289,8 @@ pub fn readLine(
const staged = stageImagePath(root, grab.path);
vision.tracePasteResult(root, grab.flavor, staged); // #350: every paste leaves a receipt
if (staged.isOk()) {
- const marker = "[Image] ";
- buf.insertSlice(gpa, cur, marker) catch {};
- cur += marker.len;
- addMark(gpa, &marks, "[Image]");
+ @import("vision_queue.zig").markLastComposer(root);
+ insertImageChip(gpa, buf, &cur, &marks, root.pending_image_len);
redraw(out, buf.items, cur, marks.items, &rstate, prompt_col);
} else {
// No `.no_vision` arm here: clipboardPasteSource
@@ -486,10 +486,8 @@ pub fn readLine(
}
}
if (staged) {
- const marker = "[Image] ";
- buf.insertSlice(gpa, cur, marker) catch {};
- cur += marker.len;
- addMark(gpa, &marks, "[Image]");
+ @import("vision_queue.zig").markLastComposer(root);
+ insertImageChip(gpa, buf, &cur, &marks, root.pending_image_len);
} else {
buf.insertSlice(gpa, cur, dropped.?) catch {};
cur += dropped.?.len;
@@ -595,5 +593,5 @@ fn replayStep(
setLine(gpa, buf, step.text);
cur.* = buf.items.len;
root.pending_image = step.image;
- if (step.image != null) addMark(gpa, marks, "[Image]"); // re-highlight the marker the replayed text carries
+ if (step.image != null) markImageChips(gpa, marks, buf.items);
}
diff --git a/src/vision.zig b/src/vision.zig
index 22bd2174..bd8383f4 100644
--- a/src/vision.zig
+++ b/src/vision.zig
@@ -31,7 +31,14 @@ pub const grabClipboardImage = clip.grabClipboardImage;
pub const max_staged_image_bytes = clip.max_staged_image_bytes;
pub const fmtBytes = clip.fmtMb;
-pub const PendingImage = struct { media_type: []const u8, b64: []const u8, label: []const u8 };
+pub const PendingImage = struct {
+ media_type: []const u8,
+ b64: []const u8,
+ label: []const u8,
+ /// Ctrl-V / drop inserted a composer chip. Submit keeps the payload only
+ /// while that chip (or an `@[path]`) is still in the prompt (#634).
+ from_composer: bool = false,
+};
/// Conservative vision check: only models we know accept images. Everything
/// else (deepseek, kimi, glm, minimax, mimo, …) is treated as text-only so
@@ -232,7 +239,8 @@ pub fn stageGuiImageAttachment(root: *Agent, msg: []const u8) void {
const rest = msg[open + 2 ..];
const close = std.mem.indexOfScalar(u8, rest, ']') orelse break;
const path = rest[0..close];
- if (isImagePath(path)) _ = stageImagePath(root, path);
+ if (isImagePath(path) and !@import("vision_queue.zig").hasLabel(root, path))
+ _ = stageImagePath(root, path);
search = open + 2 + close + 1;
}
}
diff --git a/src/vision_queue.zig b/src/vision_queue.zig
index 86e169ec..7ac4e992 100644
--- a/src/vision_queue.zig
+++ b/src/vision_queue.zig
@@ -4,6 +4,12 @@
//! so nine ask_user pastes left nine `[Image]` markers and one payload. This
//! queue keeps every successful stage (cap 16) until the next model request
//! consumes them as real vision blocks.
+//!
+//! Composer pastes (#634) are marked `from_composer`. Submit keeps those
+//! payloads only while a chip (`[Image]`, `[Image #N]`, `@[path]`) is still
+//! in the prompt. `/image` and `/paste` stay sticky for the next text-only
+//! line. Identical `b64` payloads collapse so a double-stage cannot emit
+//! four blocks from two pastes.
const std = @import("std");
const Value = std.json.Value;
@@ -37,7 +43,37 @@ pub fn take(root: *Agent) []const PendingImage {
return root.pending_images[0..n];
}
+pub fn clear(root: *Agent) void {
+ root.pending_image_len = 0;
+ root.pending_image = null;
+}
+
+pub fn markLastComposer(root: *Agent) void {
+ if (root.pending_image_len == 0) return;
+ root.pending_images[root.pending_image_len - 1].from_composer = true;
+ if (root.pending_image) |*img| img.from_composer = true;
+}
+
+pub fn hasLabel(root: *const Agent, path: []const u8) bool {
+ for (root.pending_images[0..root.pending_image_len]) |img| {
+ if (std.mem.eql(u8, img.label, path)) return true;
+ }
+ return false;
+}
+
+/// Drop composer-staged images the prompt no longer names. Command-staged
+/// `/image` / `/paste` payloads stay. Dedupes identical `b64`.
+pub fn retainReferenced(root: *Agent, text: []const u8) void {
+ const imgs = root.pending_images[0..root.pending_image_len];
+ var keep_buf: [cap]PendingImage = undefined;
+ const kept = selectForPrompt(imgs, text, &keep_buf);
+ for (kept, 0..) |img, i| root.pending_images[i] = img;
+ root.pending_image_len = @intCast(kept.len);
+ root.pending_image = if (kept.len > 0) kept[kept.len - 1] else null;
+}
+
pub fn consumePromptImages(arena: Allocator, root: *Agent, text: []const u8) !Value {
+ retainReferenced(root, text);
const imgs = take(root);
if (imgs.len == 0) return messages.textMessage(arena, "user", text);
return vision.imageMessages(arena, root.provider.kind, text, imgs);
@@ -53,10 +89,105 @@ pub fn flushPending(root: *Agent) !void {
}
pub fn withUnsupportedNote(arena: Allocator, answer: []const u8) ![]const u8 {
- if (std.mem.indexOf(u8, answer, "[Image]") == null) return answer;
+ if (!hasImageMarker(answer)) return answer;
return std.fmt.allocPrint(arena, "{s}\n\n{s}", .{ answer, unsupported_note });
}
+fn hasImageMarker(text: []const u8) bool {
+ return std.mem.indexOf(u8, text, "[Image]") != null or
+ std.mem.indexOf(u8, text, "[Image #") != null;
+}
+
+fn selectForPrompt(imgs: []const PendingImage, text: []const u8, keep: *[cap]PendingImage) []PendingImage {
+ const refs = parseRefs(text);
+ var n: usize = 0;
+ var bare_left = refs.bare;
+ for (imgs, 0..) |img, i| {
+ var want = !img.from_composer;
+ if (img.from_composer) {
+ if (i < cap and (refs.bits & (@as(u16, 1) << @intCast(i))) != 0) {
+ want = true;
+ } else if (labelInAtPath(text, img.label)) {
+ want = true;
+ } else if (bare_left > 0) {
+ want = true;
+ bare_left -= 1;
+ } else {
+ want = false;
+ }
+ }
+ if (!want) continue;
+ var dup = false;
+ for (keep[0..n]) |k| {
+ if (std.mem.eql(u8, k.b64, img.b64)) {
+ dup = true;
+ break;
+ }
+ }
+ if (dup) continue;
+ keep[n] = img;
+ n += 1;
+ }
+ return keep[0..n];
+}
+
+fn parseRefs(text: []const u8) struct { bits: u16, bare: u8 } {
+ var bits: u16 = 0;
+ var bare: u8 = 0;
+ var i: usize = 0;
+ while (i < text.len) {
+ if (std.mem.startsWith(u8, text[i..], "[Image #")) {
+ var p = i + "[Image #".len;
+ var num: u16 = 0;
+ while (p < text.len and text[p] >= '0' and text[p] <= '9') : (p += 1) {
+ num = num *% 10 + (text[p] - '0');
+ }
+ if (num >= 1 and num <= cap and p < text.len and text[p] == ']') {
+ bits |= @as(u16, 1) << @intCast(num - 1);
+ i = p + 1;
+ continue;
+ }
+ }
+ if (std.mem.startsWith(u8, text[i..], "[Image]")) {
+ bare += 1;
+ i += "[Image]".len;
+ continue;
+ }
+ i += 1;
+ }
+ return .{ .bits = bits, .bare = bare };
+}
+
+fn labelInAtPath(text: []const u8, label: []const u8) bool {
+ if (label.len == 0) return false;
+ var search: usize = 0;
+ while (std.mem.indexOfPos(u8, text, search, "@[")) |open| {
+ const rest = text[open + 2 ..];
+ const close = std.mem.indexOfScalar(u8, rest, ']') orelse break;
+ const path = rest[0..close];
+ if (std.mem.eql(u8, path, label) or
+ std.mem.endsWith(u8, label, path) or
+ std.mem.endsWith(u8, path, label))
+ return true;
+ search = open + 2 + close + 1;
+ }
+ return false;
+}
+
+fn testAgent(arena: Allocator) Agent {
+ return .{
+ .gpa = std.testing.allocator,
+ .arena = arena,
+ .io = undefined,
+ .client = undefined,
+ .provider = .{ .id = "xai", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "grok-4", .context = 100_000 },
+ .messages = std.json.Array.init(arena),
+ .sub = false,
+ .label = "test",
+ .out = null,
+ };
+}
+
test "queue keeps two staged images in order" {
var root: Agent = .{
.gpa = std.testing.allocator,
@@ -85,17 +216,7 @@ test "ask_user images become follow-up vision blocks (#580)" {
var arena_state = std.heap.ArenaAllocator.init(a);
defer arena_state.deinit();
const arena = arena_state.allocator();
- var root: Agent = .{
- .gpa = a,
- .arena = arena,
- .io = undefined,
- .client = undefined,
- .provider = .{ .id = "xai", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "grok-4", .context = 100_000 },
- .messages = std.json.Array.init(arena),
- .sub = false,
- .label = "test",
- .out = null,
- };
+ var root = testAgent(arena);
stage(&root, .{ .media_type = "image/png", .b64 = "AAA", .label = "q1.png" });
stage(&root, .{ .media_type = "image/png", .b64 = "BBB", .label = "q2.png" });
try root.messages.append(try messages.toolResultMessage(arena, .responses, "ask1", "[Image] [Image]", false));
@@ -115,3 +236,76 @@ test "placeholder-only ask_user reply gets an explicit fallback" {
try std.testing.expect(std.mem.indexOf(u8, note, "did not reach the model") != null);
try std.testing.expect(std.mem.indexOf(u8, note, "next normal prompt") != null);
}
+
+test "deleted composer chips are not sent (#634)" {
+ const a = std.testing.allocator;
+ var arena_state = std.heap.ArenaAllocator.init(a);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var root = testAgent(arena);
+ stage(&root, .{ .media_type = "image/png", .b64 = "AAA", .label = "one.png", .from_composer = true });
+ stage(&root, .{ .media_type = "image/png", .b64 = "BBB", .label = "two.png", .from_composer = true });
+ const msg = try consumePromptImages(arena, &root, "just the remaining text");
+ try std.testing.expectEqual(@as(u32, 0), compact_cut.countImageBlocks(msg));
+ try std.testing.expectEqual(@as(u8, 0), root.pending_image_len);
+}
+
+test "numbered chip keeps only that composer slot (#634)" {
+ const a = std.testing.allocator;
+ var arena_state = std.heap.ArenaAllocator.init(a);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var root = testAgent(arena);
+ stage(&root, .{ .media_type = "image/png", .b64 = "AAA", .label = "one.png", .from_composer = true });
+ stage(&root, .{ .media_type = "image/png", .b64 = "BBB", .label = "two.png", .from_composer = true });
+ const msg = try consumePromptImages(arena, &root, "[Image #2] what is this");
+ try std.testing.expectEqual(@as(u32, 1), compact_cut.countImageBlocks(msg));
+ const content = msg.object.get("content").?.array.items;
+ try std.testing.expect(std.mem.indexOf(u8, content[1].object.get("image_url").?.string, "BBB") != null);
+}
+
+test "duplicate b64 collapses to one block (#634)" {
+ const a = std.testing.allocator;
+ var arena_state = std.heap.ArenaAllocator.init(a);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var root = testAgent(arena);
+ stage(&root, .{ .media_type = "image/png", .b64 = "AAA", .label = "one.png" });
+ stage(&root, .{ .media_type = "image/png", .b64 = "AAA", .label = "one.png" });
+ stage(&root, .{ .media_type = "image/png", .b64 = "BBB", .label = "two.png" });
+ const msg = try consumePromptImages(arena, &root, "see these");
+ try std.testing.expectEqual(@as(u32, 2), compact_cut.countImageBlocks(msg));
+}
+
+test "command-staged /image still rides a text-only prompt" {
+ const a = std.testing.allocator;
+ var arena_state = std.heap.ArenaAllocator.init(a);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var root = testAgent(arena);
+ stage(&root, .{ .media_type = "image/png", .b64 = "CMD", .label = "/tmp/shot.png" });
+ const msg = try consumePromptImages(arena, &root, "what is this");
+ try std.testing.expectEqual(@as(u32, 1), compact_cut.countImageBlocks(msg));
+}
+
+test "@[path] keeps the matching composer payload" {
+ const a = std.testing.allocator;
+ var arena_state = std.heap.ArenaAllocator.init(a);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var root = testAgent(arena);
+ stage(&root, .{ .media_type = "image/png", .b64 = "AAA", .label = "/tmp/a.png", .from_composer = true });
+ stage(&root, .{ .media_type = "image/png", .b64 = "BBB", .label = "/tmp/b.png", .from_composer = true });
+ const msg = try consumePromptImages(arena, &root, "@[/tmp/b.png] look");
+ try std.testing.expectEqual(@as(u32, 1), compact_cut.countImageBlocks(msg));
+ const content = msg.object.get("content").?.array.items;
+ try std.testing.expect(std.mem.indexOf(u8, content[1].object.get("image_url").?.string, "BBB") != null);
+}
+
+test "retainReferenced drops leftover composer pastes before ask_user flush" {
+ var root = testAgent(std.testing.allocator);
+ stage(&root, .{ .media_type = "image/png", .b64 = "AAA", .label = "gone.png", .from_composer = true });
+ retainReferenced(&root, "I changed my mind");
+ try std.testing.expectEqual(@as(u8, 0), root.pending_image_len);
+ try std.testing.expect(root.pending_image == null);
+}