Skip to content

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

Python

fix: sync composer image chips on delete (#702)

Cursor Agent1f90a2529123
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e4f3a3de..acebe446 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,14 @@ The release workflow uses a tag's section here as its release notes (a
hand-written `docs/releases/<tag>.md` wins if present), so keeping this file
current is part of cutting a release.
+## v0.0.283 (2026-08-31)
+
+- Next cut after v0.0.282 landed on main. First fold: `#702` /
+ ADR 0056 — deleting a composer image chip drops that queue slot and
+ reuses `#N`. `clipboard_paste` no longer stores MIME or byte count
+ for drafts abandoned before submit.
+- Suite floor **1868** (was 1861). No tag until asked.
+
## v0.0.282 (2026-08-31)
- Next cut after v0.0.281 landed on main (`#670`). First fold:
diff --git a/docs/adr/0056-composer-image-chips-sync-on-delete.md b/docs/adr/0056-composer-image-chips-sync-on-delete.md
new file mode 100644
index 00000000..9d2c51a2
--- /dev/null
+++ b/docs/adr/0056-composer-image-chips-sync-on-delete.md
@@ -0,0 +1,25 @@
+# 0056. Composer image chips sync on delete; paste traces wait for send
+
+Status: accepted 2026-08-31
+
+## Context
+
+#634 already drops composer pixels at submit if the chip is gone. The
+line composer still numbered the next `[Image #N]` from the unpruned
+queue, so deleting `#1` and pasting again showed `#2`. Staging also
+wrote `clipboard_paste` with MIME and byte count immediately (#350),
+so an abandoned draft left content-derived metadata in `.graff/traces`.
+
+## Decision
+
+Deleting a composer chip drops that queue slot and remaps remaining
+`#N`s to 1..k. The next paste reuses the next live number. A successful
+stage records `clipboard_paste` without MIME or bytes; those fields
+are written only when the image is actually sent. Failures may still
+carry a size. `/image` and `/paste` stay sticky (not composer chips).
+
+## Consequences
+
+#350 still has a receipt for every paste attempt. Abandoned drafts no
+longer disclose what was staged. TUI already numbered from its live
+array; the line REPL now matches.
diff --git a/docs/adr/README.md b/docs/adr/README.md
index 5bd49c01..1d8d15c7 100644
--- a/docs/adr/README.md
+++ b/docs/adr/README.md
@@ -66,6 +66,7 @@ record only when you need the evidence or the edge cases.
| [0053](0053-codegraff-flake-retry-opencode.md) | Retry short Codegraff follow-up flakes (2); `opencode-codegraff` A/B on the same seat. Auth/quota stay fail-fast. |
| [0054](0054-deepseek-thinking-disabled-at-low.md) | DeepSeek family default low sends `thinking.type=disabled`; `/effort high` still thinks. GLM stays low-only. |
| [0055](0055-lean-oneshot-bash-15s.md) | Lean `-p` bash auto-backgrounds at 15s; interactive / `--no-lean` stay 120s (ADR 0026). |
+| [0056](0056-composer-image-chips-sync-on-delete.md) | Composer image chips sync on delete and reuse `#N`; `clipboard_paste` MIME/bytes wait until send. |
## When to write one
diff --git a/docs/releases/v0.0.283.md b/docs/releases/v0.0.283.md
new file mode 100644
index 00000000..0a6ab26e
--- /dev/null
+++ b/docs/releases/v0.0.283.md
@@ -0,0 +1,23 @@
+# v0.0.283 (2026-08-31)
+
+Next cut after [v0.0.282](v0.0.282.md) merged to main. No GitHub tag
+until asked. Download / notarization ids land after the build.
+
+## Composer image chips sync on delete (ADR 0056 / #702)
+
+#634 already kept deleted composer screenshots off the model. The line
+composer still numbered the next `[Image #N]` from the unpruned queue,
+and every successful Ctrl-V / `/paste` wrote MIME type and byte count
+into `.graff/traces` even when the chip was backspaced away.
+
+Deleting a chip now drops that queue slot and remaps remaining numbers
+to `1..k`. The next paste reuses the next live number. A successful
+stage records `clipboard_paste` without content-derived fields; MIME
+and bytes are written only when the image is actually sent. Failures
+may still carry a size. `/image` and `/paste` stay sticky.
+
+TUI already numbered from its live array. The line REPL matches.
+
+## Tests
+
+This cut raises the release-cut floor to **1868** (slack 25).
diff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.json
index 84dd57a9..4114ecce 100644
--- a/scripts/eval/tier1-manifest.json
+++ b/scripts/eval/tier1-manifest.json
@@ -15,7 +15,7 @@
"src/repl.zig",
"TUI/root.zig"
],
- "test_count_baseline": 1861,
+ "test_count_baseline": 1868,
"test_count_slack": 25,
"required_invariants": [
{
diff --git a/src/cli.zig b/src/cli.zig
index 0dcb2da9..6de82ded 100644
--- a/src/cli.zig
+++ b/src/cli.zig
@@ -21,6 +21,9 @@ const harness_version = root.harness_version;
pub const changelog_text =
\\What's new
\\──────────
+ \\0.0.283
+ \\ • deleted composer image chips reuse #N; paste traces omit MIME/bytes until send (#702)
+ \\
\\0.0.282
\\ • pasted-text chips are atomic spans — a typed lookalike stays literal (#674)
\\ • Codex WS type:error is a terminal API response; last_api_error stays (#693)
diff --git a/src/libgraff.zig b/src/libgraff.zig
index 5108379b..9690c439 100644
--- a/src/libgraff.zig
+++ b/src/libgraff.zig
@@ -76,7 +76,7 @@ export fn graff_acp_create(new_seed: u32) void {
session_id = null;
seed = if (new_seed == 0) 1 else new_seed;
engine.cancel_flag.store(false, .release);
- engine.implementation_version = "0.0.282-core";
+ engine.implementation_version = "0.0.283-core";
}
export fn graff_acp_feed(len: usize) i32 {
diff --git a/src/readline.zig b/src/readline.zig
index 37019316..c8a053e3 100644
--- a/src/readline.zig
+++ b/src/readline.zig
@@ -40,9 +40,8 @@ const collectRepoFiles = input_util.collectRepoFiles;
const isImagePath = input_util.isImagePath;
const redraw = input_util.redraw;
const editByte = input_util.editByte; // #396: job-control-aware continuation reads
-const delRange = input_util.delRange;
const addMark = input_util.addMark;
-const insertImageChip = input_util.insertImageChip;
+const rl_image = @import("readline_image.zig");
const util = @import("util.zig");
const main_mod = @import("main.zig");
@@ -240,31 +239,28 @@ pub fn readLine(
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
},
0x02 => if (cur > 0) { // Ctrl-B → left
- cur = pastes.left(cur);
+ cur = rl_image.left(buf.items, cur) orelse pastes.left(cur);
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
},
0x06 => if (cur < buf.items.len) { // Ctrl-F → right
- cur = pastes.right(cur, buf.items.len);
+ cur = rl_image.right(buf.items, cur) orelse pastes.right(cur, buf.items.len);
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
},
0x17, 0x1f => { // Ctrl-W / Ctrl-_ → delete previous word
const s = pastes.prevWord(buf.items, cur);
if (s < cur) {
- pastes.edited(gpa, s, cur, 0);
- delRange(buf, s, cur);
+ rl_image.deleteAtom(root, gpa, buf, &cur, &pastes, s, cur);
cur = s;
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
}
},
0x15 => if (cur > 0) { // Ctrl-U → delete to start of line
- pastes.edited(gpa, 0, cur, 0);
- delRange(buf, 0, cur);
+ rl_image.deleteAtom(root, gpa, buf, &cur, &pastes, 0, cur);
cur = 0;
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
},
0x0b => if (cur < buf.items.len) { // Ctrl-K → delete to end of line
- pastes.edited(gpa, cur, buf.items.len, 0);
- buf.shrinkRetainingCapacity(cur);
+ rl_image.deleteAtom(root, gpa, buf, &cur, &pastes, cur, buf.items.len);
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
},
0x16 => { // Ctrl-V: attach a clipboard image (macOS) at the cursor
@@ -273,13 +269,11 @@ pub fn readLine(
switch (vision.clipboardPasteSource(root.io, gpa, vision.visionCapable(root.provider), builtin.os.tag == .macos, grabClipboardImage)) {
.image => |grab| {
defer grab.release(root.io, gpa); // temp export never outlives the paste
+ rl_image.afterEdit(root, gpa, buf, &cur);
const staged = stageImagePath(root, grab.path);
vision.tracePasteResult(root, grab.flavor, staged); // #350: every paste leaves a receipt
if (staged.isOk()) {
- @import("vision_queue.zig").markLastComposer(root);
- const at = cur;
- insertImageChip(gpa, buf, &cur, &marks, root.pending_image_len);
- if (cur > at) pastes.edited(gpa, at, at, cur - at);
+ rl_image.insertComposerChip(root, gpa, buf, &cur, &marks, &pastes);
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
} else {
// No `.no_vision` arm here: clipboardPasteSource
@@ -303,9 +297,8 @@ pub fn readLine(
}
},
0x7f, 0x08 => if (cur > 0) { // backspace → delete previous atom
- const start = pastes.left(cur);
- pastes.edited(gpa, start, cur, 0);
- delRange(buf, start, cur);
+ const start = rl_image.left(buf.items, cur) orelse pastes.left(cur);
+ rl_image.deleteAtom(root, gpa, buf, &cur, &pastes, start, cur);
cur = start;
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
},
@@ -315,9 +308,7 @@ pub fn readLine(
out.flush() catch {};
return null;
}
- buf.clearRetainingCapacity();
- pastes.clear(gpa);
- cur = 0;
+ rl_image.clearLine(root, gpa, buf, &cur, &pastes);
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
},
0x1a => { // Ctrl-Z: save the session and quit (like a safe Ctrl-D)
@@ -333,9 +324,8 @@ pub fn readLine(
return null;
}
if (cur < buf.items.len) {
- const end = pastes.right(cur, buf.items.len);
- pastes.edited(gpa, cur, end, 0);
- delRange(buf, cur, end);
+ const end = rl_image.right(buf.items, cur) orelse pastes.right(cur, buf.items.len);
+ rl_image.deleteAtom(root, gpa, buf, &cur, &pastes, cur, end);
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
}
},
@@ -344,9 +334,7 @@ pub fn readLine(
// this the chord read below would block and silently eat
// the next keypress.
if (tty.pendingBytes() == 0 and in.buffered().len == 0 and !inputPending()) {
- buf.clearRetainingCapacity();
- pastes.clear(gpa);
- cur = 0;
+ rl_image.clearLine(root, gpa, buf, &cur, &pastes);
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
continue;
}
@@ -354,8 +342,7 @@ pub fn readLine(
if (b1 == 0x7f or b1 == 0x08) { // Option/Alt+Delete → delete previous word
const s = pastes.prevWord(buf.items, cur);
if (s < cur) {
- pastes.edited(gpa, s, cur, 0);
- delRange(buf, s, cur);
+ rl_image.deleteAtom(root, gpa, buf, &cur, &pastes, s, cur);
cur = s;
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
}
@@ -374,8 +361,7 @@ pub fn readLine(
if (b1 == 'd') { // Alt-d → delete next word
const e = pastes.nextWord(buf.items, cur);
if (e > cur) {
- pastes.edited(gpa, cur, e, 0);
- delRange(buf, cur, e);
+ rl_image.deleteAtom(root, gpa, buf, &cur, &pastes, cur, e);
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
}
continue;
@@ -408,11 +394,11 @@ pub fn readLine(
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
},
'C' => { // right (word-right with a modifier)
- cur = if (word_mod) pastes.nextWord(buf.items, cur) else pastes.right(cur, buf.items.len);
+ cur = if (word_mod) pastes.nextWord(buf.items, cur) else (rl_image.right(buf.items, cur) orelse pastes.right(cur, buf.items.len));
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
},
'D' => { // left (word-left with a modifier)
- cur = if (word_mod) pastes.prevWord(buf.items, cur) else pastes.left(cur);
+ cur = if (word_mod) pastes.prevWord(buf.items, cur) else (rl_image.left(buf.items, cur) orelse pastes.left(cur));
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
},
'H' => {
@@ -472,6 +458,7 @@ pub fn readLine(
var dmsg: ?[]const u8 = null;
var dbuf: [224]u8 = undefined;
if (isImagePath(dropped.?)) {
+ rl_image.afterEdit(root, gpa, buf, &cur);
const r = stageImagePath(root, dropped.?);
if (r.isOk()) {
staged = true;
@@ -483,10 +470,7 @@ pub fn readLine(
}
}
if (staged) {
- @import("vision_queue.zig").markLastComposer(root);
- const at = cur;
- insertImageChip(gpa, buf, &cur, &marks, root.pending_image_len);
- if (cur > at) pastes.edited(gpa, at, at, cur - at);
+ rl_image.insertComposerChip(root, gpa, buf, &cur, &marks, &pastes);
} else {
const at = cur;
const old_len = buf.items.len;
@@ -517,9 +501,8 @@ pub fn readLine(
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
} else if (std.mem.eql(u8, ps, "3")) { // forward delete
if (cur < buf.items.len) {
- const end = pastes.right(cur, buf.items.len);
- pastes.edited(gpa, cur, end, 0);
- delRange(buf, cur, end);
+ const end = rl_image.right(buf.items, cur) orelse pastes.right(cur, buf.items.len);
+ rl_image.deleteAtom(root, gpa, buf, &cur, &pastes, cur, end);
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
}
} else if (std.mem.eql(u8, ps, "1") or std.mem.eql(u8, ps, "7")) {
diff --git a/src/readline_image.zig b/src/readline_image.zig
new file mode 100644
index 00000000..e51bdd95
--- /dev/null
+++ b/src/readline_image.zig
@@ -0,0 +1,187 @@
+//! Line-composer image chips (#702).
+//!
+//! Ctrl-V / drop stage pixels on `Agent.pending_images` and insert `[Image #N]`.
+//! Deleting a chip must drop that queue slot and rewrite remaining numbers so
+//! the next paste is `#1` again — not a ghost `#2`. Content-derived paste
+//! traces wait until submit (ADR 0056).
+
+const std = @import("std");
+const Allocator = std.mem.Allocator;
+
+const Agent = @import("agent.zig").Agent;
+const vision = @import("vision.zig");
+const vision_queue = @import("vision_queue.zig");
+const input_util = @import("input_util.zig");
+const PasteStore = @import("readline_paste.zig").Store;
+
+pub const Chip = struct { start: usize, end: usize, n: u8 };
+
+/// `[Image #N]` starting at `i`, or null.
+pub fn imageChipAt(text: []const u8, i: usize) ?Chip {
+ if (!std.mem.startsWith(u8, text[i..], "[Image #")) return null;
+ 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 or num > vision_queue.cap or p >= text.len or text[p] != ']') return null;
+ return .{ .start = i, .end = p + 1, .n = @intCast(num) };
+}
+
+/// Start of the image chip that `cur` is inside (including a trailing space).
+pub fn left(text: []const u8, cur: usize) ?usize {
+ var i: usize = 0;
+ while (i < text.len) {
+ if (imageChipAt(text, i)) |chip| {
+ var end = chip.end;
+ if (end < text.len and text[end] == ' ') end += 1;
+ if (cur > chip.start and cur <= end) return chip.start;
+ i = chip.end;
+ continue;
+ }
+ i += 1;
+ }
+ return null;
+}
+
+/// End of the image chip that `cur` is inside (including a trailing space).
+pub fn right(text: []const u8, cur: usize) ?usize {
+ var i: usize = 0;
+ while (i < text.len) {
+ if (imageChipAt(text, i)) |chip| {
+ var end = chip.end;
+ if (end < text.len and text[end] == ' ') end += 1;
+ if (cur >= chip.start and cur < end) return end;
+ i = chip.end;
+ continue;
+ }
+ i += 1;
+ }
+ return null;
+}
+
+/// Drop composer slots the buffer no longer names and rewrite remaining `#N`s.
+pub fn afterEdit(root: *Agent, gpa: Allocator, buf: *std.ArrayList(u8), cur: *usize) void {
+ const remap = vision_queue.retainReferenced(root, buf.items);
+ rewriteChips(gpa, buf, cur, remap);
+}
+
+pub fn deleteAtom(root: *Agent, gpa: Allocator, buf: *std.ArrayList(u8), cur: *usize, pastes: *PasteStore, from: usize, to: usize) void {
+ pastes.edited(gpa, from, to, 0);
+ input_util.delRange(buf, from, to);
+ afterEdit(root, gpa, buf, cur);
+}
+
+pub fn clearLine(root: *Agent, gpa: Allocator, buf: *std.ArrayList(u8), cur: *usize, pastes: *PasteStore) void {
+ buf.clearRetainingCapacity();
+ pastes.clear(gpa);
+ cur.* = 0;
+ afterEdit(root, gpa, buf, cur);
+}
+
+/// Rewrite `[Image #old]` to the compacted chip numbers. Identity is a no-op.
+pub fn rewriteChips(gpa: Allocator, buf: *std.ArrayList(u8), cur: *usize, remap: vision_queue.Remap) void {
+ var out: std.ArrayList(u8) = .empty;
+ defer out.deinit(gpa);
+ var i: usize = 0;
+ var new_cur = cur.*;
+ var changed = false;
+ while (i < buf.items.len) {
+ if (imageChipAt(buf.items, i)) |chip| {
+ const new_n: u8 = if (chip.n >= 1 and chip.n <= vision_queue.cap) remap[chip.n - 1] else 0;
+ var tmp: [20]u8 = undefined;
+ const repl = if (new_n == 0 or new_n == chip.n)
+ buf.items[chip.start..chip.end]
+ else blk: {
+ changed = true;
+ break :blk std.fmt.bufPrint(&tmp, "[Image #{d}]", .{new_n}) catch buf.items[chip.start..chip.end];
+ };
+ const old_len = chip.end - chip.start;
+ if (cur.* >= chip.end) {
+ if (repl.len >= old_len) new_cur += repl.len - old_len else new_cur -= old_len - repl.len;
+ } else if (cur.* > chip.start) {
+ new_cur = out.items.len + repl.len;
+ }
+ out.appendSlice(gpa, repl) catch return;
+ i = chip.end;
+ continue;
+ }
+ out.append(gpa, buf.items[i]) catch return;
+ i += 1;
+ }
+ if (!changed) return;
+ buf.clearRetainingCapacity();
+ buf.appendSlice(gpa, out.items) catch return;
+ cur.* = @min(new_cur, buf.items.len);
+}
+
+pub fn insertComposerChip(
+ root: *Agent,
+ gpa: Allocator,
+ buf: *std.ArrayList(u8),
+ cur: *usize,
+ marks: *std.ArrayList([]const u8),
+ pastes: *PasteStore,
+) void {
+ vision_queue.markLastComposer(root);
+ const at = cur.*;
+ input_util.insertImageChip(gpa, buf, cur, marks, root.pending_image_len);
+ if (cur.* > at) pastes.edited(gpa, at, at, cur.* - at);
+}
+
+test "imageChipAt parses a numbered marker" {
+ const chip = imageChipAt("[Image #3] look", 0).?;
+ try std.testing.expectEqual(@as(usize, 0), chip.start);
+ try std.testing.expectEqual(@as(usize, 10), chip.end);
+ try std.testing.expectEqual(@as(u8, 3), chip.n);
+ try std.testing.expect(imageChipAt("see [Image #1]", 4) != null);
+ try std.testing.expect(imageChipAt("[Image]", 0) == null);
+}
+
+test "left/right treat an image chip as one atom (#702)" {
+ const text = "hi [Image #1] there";
+ try std.testing.expectEqual(@as(usize, 3), left(text, 13).?); // on the trailing space
+ try std.testing.expectEqual(@as(usize, 3), left(text, 12).?); // on ]
+ try std.testing.expect(left(text, 3) == null);
+ try std.testing.expectEqual(@as(usize, 13), right(text, 3).?);
+ try std.testing.expect(right(text, 13) == null);
+}
+
+test "afterEdit drops an abandoned chip and remaps the rest (#702)" {
+ const gpa = std.testing.allocator;
+ var root: Agent = .{
+ .gpa = gpa,
+ .arena = gpa,
+ .io = undefined,
+ .client = undefined,
+ .provider = .{ .id = "xai", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "grok-4", .context = 100_000 },
+ .messages = undefined,
+ .sub = false,
+ .label = "test",
+ .out = null,
+ };
+ vision_queue.stage(&root, .{ .media_type = "image/png", .b64 = "AAA", .label = "one.png", .from_composer = true });
+ vision_queue.stage(&root, .{ .media_type = "image/png", .b64 = "BBB", .label = "two.png", .from_composer = true });
+ var buf: std.ArrayList(u8) = .empty;
+ defer buf.deinit(gpa);
+ try buf.appendSlice(gpa, "[Image #2] look");
+ var cur: usize = buf.items.len;
+ afterEdit(&root, gpa, &buf, &cur);
+ try std.testing.expectEqual(@as(u8, 1), root.pending_image_len);
+ try std.testing.expectEqualStrings("BBB", root.pending_images[0].b64);
+ try std.testing.expectEqualStrings("[Image #1] look", buf.items);
+ try std.testing.expectEqual(@as(u8, 2), vision_queue.nextChipNumber(&root));
+}
+
+test "rewriteChips remaps #2 to #1 after the first slot is dropped (#702)" {
+ const gpa = std.testing.allocator;
+ var buf: std.ArrayList(u8) = .empty;
+ defer buf.deinit(gpa);
+ try buf.appendSlice(gpa, "[Image #2] look");
+ var cur: usize = buf.items.len;
+ var remap: vision_queue.Remap = @splat(0);
+ remap[1] = 1;
+ rewriteChips(gpa, &buf, &cur, remap);
+ try std.testing.expectEqualStrings("[Image #1] look", buf.items);
+ try std.testing.expectEqual(@as(usize, buf.items.len), cur);
+}
diff --git a/src/test_hooks.zig b/src/test_hooks.zig
index 987e5196..fb79a9af 100644
--- a/src/test_hooks.zig
+++ b/src/test_hooks.zig
@@ -346,6 +346,7 @@ test {
_ = @import("session_wake.zig");
_ = @import("tui_acp.zig");
_ = @import("readline_paste.zig"); // #674: semantic paste spans (not reached via readline.zig's runtime import)
+ _ = @import("readline_image.zig"); // #702: composer image chips sync on delete
_ = @import("agent_request_scratch.zig"); // #693: split out of agent_request.zig (600-line ceiling)
_ = @import("commands_resume.zig"); // #697: --resume SOURCE --branch DEST parser
_ = @import("net_efficiency_test.zig");
diff --git a/src/vision.zig b/src/vision.zig
index fa5e63f4..f3a8bf05 100644
--- a/src/vision.zig
+++ b/src/vision.zig
@@ -204,9 +204,9 @@ pub fn stageMessage(buf: []u8, r: StageResult, what: []const u8) []const u8 {
/// One `clipboard_paste` receipt per Ctrl-V / `/paste`, whatever the outcome
/// (#350). Before this a dropped paste left ZERO evidence behind: a failing
-/// and a succeeding run produced byte-identical `.graff/traces` JSONL. Carries
-/// the decision, the cascade flavor, the byte count and the mime — never the
-/// pixels.
+/// and a succeeding run produced byte-identical `.graff/traces` JSONL.
+/// Successful stages omit MIME / bytes until the image is actually sent
+/// (#702 / ADR 0056). Failures may still carry a size. Never the pixels.
pub fn tracePaste(root: *Agent, result: []const u8, flavor: []const u8, bytes: u64, mime: []const u8) void {
const tracer = root.tracer orelse return;
tracer.write(.{
@@ -222,7 +222,10 @@ pub fn tracePaste(root: *Agent, result: []const u8, flavor: []const u8, bytes: u
/// `tracePaste` for a paste that got as far as staging.
pub fn tracePasteResult(root: *Agent, flavor: Flavor, r: StageResult) void {
switch (r) {
- .ok => |o| tracePaste(root, "ok", flavor.name(), o.bytes, o.media_type),
+ // #702: a successful stage is not a sent attachment. MIME / bytes
+ // wait for consumePromptImages so an abandoned draft leaves no
+ // content-derived receipt. Failures still record why they died.
+ .ok => tracePaste(root, "ok", flavor.name(), 0, ""),
.too_large => |t| tracePaste(root, "too_large", flavor.name(), t.bytes, ""),
else => tracePaste(root, @tagName(r), flavor.name(), 0, ""),
}
diff --git a/src/vision_queue.zig b/src/vision_queue.zig
index 7b748b07..6fca6083 100644
--- a/src/vision_queue.zig
+++ b/src/vision_queue.zig
@@ -61,20 +61,43 @@ pub fn hasLabel(root: *const Agent, path: []const u8) bool {
return false;
}
+/// old queue index → new 1-based chip number, or 0 if that slot was dropped.
+pub const Remap = [cap]u8;
+
/// 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 {
+/// `/image` / `/paste` payloads stay. Dedupes identical `b64`. Returns the
+/// old-index → new-chip map so the composer can rewrite remaining `#N`s.
+pub fn retainReferenced(root: *Agent, text: []const u8) Remap {
const imgs = root.pending_images[0..root.pending_image_len];
var keep_buf: [cap]PendingImage = undefined;
const kept = selectForPrompt(imgs, text, &keep_buf);
+ var remap: Remap = @splat(0);
+ var k: usize = 0;
+ for (imgs, 0..) |img, i| {
+ if (k >= kept.len) break;
+ if (!samePayload(img, kept[k])) continue;
+ remap[i] = @intCast(k + 1);
+ k += 1;
+ }
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;
+ return remap;
+}
+
+/// Next `[Image #N]` for a new composer paste after the live queue is synced.
+pub fn nextChipNumber(root: *const Agent) u8 {
+ return root.pending_image_len + 1;
}
pub fn consumePromptImages(arena: Allocator, root: *Agent, text: []const u8) !Value {
- retainReferenced(root, text);
+ _ = retainReferenced(root, text);
const imgs = take(root);
+ for (imgs) |img| {
+ if (!img.from_composer or img.media_type.len == 0) continue;
+ const bytes: u64 = if (img.b64.len == 0) 0 else std.base64.standard.Decoder.calcSizeForSlice(img.b64) catch img.b64.len;
+ vision.tracePaste(root, "sent", "submit", bytes, img.media_type);
+ }
if (imgs.len == 0) return messages.textMessage(arena, "user", text);
return vision.imageMessages(arena, root.provider.kind, text, imgs);
}
@@ -98,6 +121,10 @@ fn hasImageMarker(text: []const u8) bool {
std.mem.indexOf(u8, text, "[Image #") != null;
}
+fn samePayload(a: PendingImage, b: PendingImage) bool {
+ return std.mem.eql(u8, a.b64, b.b64) and std.mem.eql(u8, a.url, b.url);
+}
+
fn selectForPrompt(imgs: []const PendingImage, text: []const u8, keep: *[cap]PendingImage) []PendingImage {
const refs = parseRefs(text);
var n: usize = 0;
@@ -359,7 +386,36 @@ test "@[path] keeps the matching composer payload" {
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");
+ _ = 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);
}
+
+test "deleting a composer chip reuses #1 (#702)" {
+ var root = testAgent(std.testing.allocator);
+ stage(&root, .{ .media_type = "image/png", .b64 = "AAA", .label = "one.png", .from_composer = true });
+ try std.testing.expectEqual(@as(u8, 2), nextChipNumber(&root));
+ _ = retainReferenced(&root, "changed my mind");
+ try std.testing.expectEqual(@as(u8, 0), root.pending_image_len);
+ try std.testing.expectEqual(@as(u8, 1), nextChipNumber(&root));
+}
+
+test "remaining composer chip remaps to #1 after an earlier chip is dropped (#702)" {
+ var root = testAgent(std.testing.allocator);
+ 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 remap = retainReferenced(&root, "[Image #2] look");
+ try std.testing.expectEqual(@as(u8, 1), root.pending_image_len);
+ try std.testing.expectEqualStrings("BBB", root.pending_images[0].b64);
+ try std.testing.expectEqual(@as(u8, 0), remap[0]);
+ try std.testing.expectEqual(@as(u8, 1), remap[1]);
+ try std.testing.expectEqual(@as(u8, 2), nextChipNumber(&root));
+}
+
+test "command-staged /image survives an empty composer sync (#702)" {
+ var root = testAgent(std.testing.allocator);
+ stage(&root, .{ .media_type = "image/png", .b64 = "CMD", .label = "/tmp/shot.png" });
+ _ = retainReferenced(&root, "no chips here");
+ try std.testing.expectEqual(@as(u8, 1), root.pending_image_len);
+ try std.testing.expectEqualStrings("CMD", root.pending_images[0].b64);
+}