Skip to content

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

Python

Fix Cmd+Delete in the line REPL

yxlyx6c284de6c0dd
diff --git a/src/readline.zig b/src/readline.zig
index 1664678f..731190de 100644
--- a/src/readline.zig
+++ b/src/readline.zig
@@ -43,6 +43,7 @@ const editByte = input_util.editByte; // #396: job-control-aware continuation re
const addMark = input_util.addMark;
const readline_commit = @import("readline_commit.zig");
const rl_image = @import("readline_image.zig");
+const rl_keys = @import("readline_keys.zig");
const util = @import("util.zig");
const main_mod = @import("main.zig");
@@ -77,8 +78,8 @@ pub fn readLine(
var cur: usize = 0; // cursor index within buf
var nav: HistoryNav = .init(history.items.len); // history + unsent-draft nav (#101)
defer if (nav.draft) |d| gpa.free(d);
- out.writeAll("\x1b[?2004h") catch {}; // terminal wraps pastes in ESC[200~ … ESC[201~
- defer out.writeAll("\x1b[?2004l") catch {};
+ out.writeAll(rl_keys.enable_seq) catch {}; // bracketed paste + distinct Cmd/Option Backspace
+ defer out.writeAll(rl_keys.restore_seq) catch {};
out.flush() catch {};
// Ask the terminal (DSR 6) where input starts; the renderer treats those columns as a fixed prefix
// and wraps the input across rows below it. Cursor moves in redraw are all
@@ -251,10 +252,9 @@ pub fn readLine(
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
}
},
- 0x15 => if (cur > 0) { // Ctrl-U → delete to start of line
- rl_image.deleteAtom(root, gpa, buf, &cur, &pastes, 0, cur);
- cur = 0;
- redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
+ 0x15 => { // Ctrl-U → delete to start of line
+ if (rl_image.deleteModified(root, gpa, buf, &cur, &pastes, .delete_to_start))
+ redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
},
0x0b => if (cur < buf.items.len) { // Ctrl-K → delete to end of line
rl_image.deleteAtom(root, gpa, buf, &cur, &pastes, cur, buf.items.len);
@@ -336,13 +336,9 @@ pub fn readLine(
continue;
}
const b1 = editByte(in) orelse break; // #396: guarded
- if (b1 == 0x7f or b1 == 0x08) { // Option/Alt+Delete → delete previous word
- const s = rl_image.prevWord(buf.items, cur, &pastes);
- if (s < cur) {
- rl_image.deleteAtom(root, gpa, buf, &cur, &pastes, s, cur);
- cur = s;
+ if (rl_keys.legacyDelete(b1)) |action| { // Option/Alt+Delete → previous word
+ if (rl_image.deleteModified(root, gpa, buf, &cur, &pastes, action))
redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
- }
continue;
}
if (b1 == 'b') { // Alt-b → word left
@@ -380,6 +376,17 @@ pub fn readLine(
}
}
const ps = params[0..pn];
+ if (rl_keys.csiDelete(ps, final)) |action| {
+ if (rl_image.deleteModified(root, gpa, buf, &cur, &pastes, action))
+ redraw(out, buf.items, cur, marks.items, &pastes, &rstate, prompt_col);
+ continue;
+ }
+ var replay: [2]u8 = undefined;
+ const replayed = rl_keys.csiReplay(ps, final, &replay);
+ if (replayed.len > 0) {
+ pending.appendSlice(gpa, replayed) catch {};
+ continue;
+ }
const word_mod = std.mem.indexOfScalar(u8, ps, ';') != null; // 1;3 (alt) / 1;5 (ctrl)
switch (final) {
'A' => if (nav.up(gpa, history.items, rl_history.g_history_images.slice(), buf.items, root.pending_image)) |step| { // up → history back; snapshots draft (#101)
diff --git a/src/readline_image.zig b/src/readline_image.zig
index d5d7d716..e2c807fc 100644
--- a/src/readline_image.zig
+++ b/src/readline_image.zig
@@ -12,6 +12,7 @@ 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 readline_keys = @import("readline_keys.zig");
const PasteStore = @import("readline_paste.zig").Store;
pub const Chip = struct { start: usize, end: usize, n: u8 };
@@ -86,6 +87,18 @@ pub fn deleteAtom(root: *Agent, gpa: Allocator, buf: *std.ArrayList(u8), cur: *u
afterEdit(root, gpa, buf, cur);
}
+pub fn deleteModified(root: *Agent, gpa: Allocator, buf: *std.ArrayList(u8), cur: *usize, pastes: *PasteStore, action: readline_keys.ModifiedDelete) bool {
+ const start = switch (action) {
+ .delete_word => prevWord(buf.items, cur.*, pastes),
+ .delete_to_start => 0,
+ };
+ if (start >= cur.*) return false;
+ const old_cur = cur.*;
+ deleteAtom(root, gpa, buf, cur, pastes, start, old_cur);
+ cur.* = start;
+ return true;
+}
+
pub fn clearLine(root: *Agent, gpa: Allocator, buf: *std.ArrayList(u8), cur: *usize, pastes: *PasteStore) void {
buf.clearRetainingCapacity();
pastes.clear(gpa);
@@ -157,6 +170,74 @@ pub fn insertComposerChip(
if (cur.* > at) pastes.edited(gpa, at, at, cur.* - at);
}
+fn testAgent(gpa: Allocator) Agent {
+ return .{
+ .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,
+ };
+}
+
+test "Cmd Delete removes UTF-8 prompt prefixes and is idempotent (#981)" {
+ const gpa = std.testing.allocator;
+ var root = testAgent(gpa);
+ var pastes: PasteStore = .{};
+ defer pastes.deinit(gpa);
+ var buf: std.ArrayList(u8) = .empty;
+ defer buf.deinit(gpa);
+
+ try buf.appendSlice(gpa, "é alpha beta");
+ var cur = buf.items.len;
+ try std.testing.expect(deleteModified(&root, gpa, &buf, &cur, &pastes, .delete_to_start));
+ try std.testing.expectEqualStrings("", buf.items);
+ try std.testing.expectEqual(@as(usize, 0), cur);
+ try std.testing.expect(!deleteModified(&root, gpa, &buf, &cur, &pastes, .delete_to_start));
+
+ try buf.appendSlice(gpa, "é alpha beta");
+ cur = "é alpha".len;
+ try std.testing.expect(deleteModified(&root, gpa, &buf, &cur, &pastes, .delete_to_start));
+ try std.testing.expectEqualStrings(" beta", buf.items);
+ try std.testing.expectEqual(@as(usize, 0), cur);
+}
+
+test "Option Delete still removes only the previous word (#981)" {
+ const gpa = std.testing.allocator;
+ var root = testAgent(gpa);
+ var pastes: PasteStore = .{};
+ defer pastes.deinit(gpa);
+ var buf: std.ArrayList(u8) = .empty;
+ defer buf.deinit(gpa);
+ try buf.appendSlice(gpa, "one two");
+ var cur = buf.items.len;
+
+ try std.testing.expect(deleteModified(&root, gpa, &buf, &cur, &pastes, .delete_word));
+ try std.testing.expectEqualStrings("one ", buf.items);
+ try std.testing.expectEqual(@as(usize, 4), cur);
+}
+
+test "Cmd Delete removes a composer image prefix atomically (#981)" {
+ const gpa = std.testing.allocator;
+ var root = testAgent(gpa);
+ vision_queue.stage(&root, .{ .media_type = "image/png", .b64 = "AAA", .label = "one.png", .from_composer = true });
+ var pastes: PasteStore = .{};
+ defer pastes.deinit(gpa);
+ var buf: std.ArrayList(u8) = .empty;
+ defer buf.deinit(gpa);
+ try buf.appendSlice(gpa, "[Image #1] tail");
+ var cur: usize = "[Image #1] ".len;
+
+ try std.testing.expect(deleteModified(&root, gpa, &buf, &cur, &pastes, .delete_to_start));
+ try std.testing.expectEqualStrings("tail", buf.items);
+ try std.testing.expectEqual(@as(usize, 0), cur);
+ try std.testing.expectEqual(@as(u8, 0), root.pending_image_len);
+}
+
test "imageChipAt parses a numbered marker" {
const chip = imageChipAt("[Image #3] look", 0).?;
try std.testing.expectEqual(@as(usize, 0), chip.start);
diff --git a/src/readline_keys.zig b/src/readline_keys.zig
new file mode 100644
index 00000000..03d20ade
--- /dev/null
+++ b/src/readline_keys.zig
@@ -0,0 +1,164 @@
+//! Modified-Backspace decoding for the line REPL (#981).
+//!
+//! Legacy ESC DEL remains Option/Alt word-delete. While the editor is active,
+//! xterm modifyOtherKeys gives Cmd/Super and Ctrl distinct modifier bits; direct
+//! Kitty CSI-u forms are accepted too when a terminal already emits them.
+
+const std = @import("std");
+
+pub const enable_seq = "\x1b[?2004h\x1b[>4;2m";
+pub const restore_seq = "\x1b[>4;0m\x1b[?2004l";
+
+pub const ModifiedDelete = enum {
+ delete_word,
+ delete_to_start,
+};
+
+pub fn legacyDelete(byte: u8) ?ModifiedDelete {
+ return if (byte == 0x7f or byte == 0x08) .delete_word else null;
+}
+
+pub fn csiDelete(params: []const u8, final: u8) ?ModifiedDelete {
+ return switch (final) {
+ 'u' => kittyDelete(params),
+ '~' => modifyOtherKeysDelete(params),
+ else => null,
+ };
+}
+
+fn kittyDelete(params: []const u8) ?ModifiedDelete {
+ var fields = std.mem.splitScalar(u8, params, ';');
+ const code = fieldNumber(fields.next() orelse return null) orelse return null;
+ const modifier_field = fields.next() orelse return null;
+ const encoded_mods = fieldNumber(modifier_field) orelse return null;
+ const event = fieldEvent(modifier_field) orelse 1;
+ return modifiedDelete(code, encoded_mods, event);
+}
+
+fn modifyOtherKeysDelete(params: []const u8) ?ModifiedDelete {
+ var fields = std.mem.splitScalar(u8, params, ';');
+ if ((fieldNumber(fields.next() orelse return null) orelse return null) != 27) return null;
+ const encoded_mods = fieldNumber(fields.next() orelse return null) orelse return null;
+ const code = fieldNumber(fields.next() orelse return null) orelse return null;
+ return modifiedDelete(code, encoded_mods, 1);
+}
+
+fn modifiedDelete(code: u32, encoded_mods: u32, event: u32) ?ModifiedDelete {
+ if (code != 0x7f and code != 0x08) return null;
+ if (event == 3 or (event != 1 and event != 2)) return null;
+ const mods = encoded_mods -| 1;
+ if (mods & (8 | 4) != 0) return .delete_to_start; // Super/Cmd or Ctrl
+ if (mods & 2 != 0) return .delete_word; // Alt/Option
+ return null;
+}
+
+/// Normalize xterm modifyOtherKeys back into the editor's existing byte paths.
+/// Level 2 reports Ctrl/Alt/Shift chords as CSI 27;mods;code~, not only Cmd+BS.
+pub fn csiReplay(params: []const u8, final: u8, out: *[2]u8) []const u8 {
+ if (final != '~') return "";
+ var fields = std.mem.splitScalar(u8, params, ';');
+ if ((fieldNumber(fields.next() orelse return "") orelse return "") != 27) return "";
+ const encoded_mods = fieldNumber(fields.next() orelse return "") orelse return "";
+ const code = fieldNumber(fields.next() orelse return "") orelse return "";
+ if (code > 0x7f or code == 0x7f or code == 0x08) return "";
+ const mods = encoded_mods -| 1;
+ if (mods & 8 != 0) return ""; // unsupported Super chords stay inert
+ if (mods & 4 != 0) {
+ out[0] = @intCast(code & 0x1f);
+ return out[0..1];
+ }
+ const byte: u8 = @intCast(code);
+ if (mods & 2 != 0) {
+ out[0] = 0x1b;
+ out[1] = if (mods & 1 != 0) shiftedAscii(byte) else byte;
+ return out[0..2];
+ }
+ out[0] = if (mods & 1 != 0) shiftedAscii(byte) else byte;
+ return out[0..1];
+}
+
+fn shiftedAscii(byte: u8) u8 {
+ return switch (byte) {
+ 'a'...'z' => byte - 32,
+ '1' => '!',
+ '2' => '@',
+ '3' => '#',
+ '4' => '$',
+ '5' => '%',
+ '6' => '^',
+ '7' => '&',
+ '8' => '*',
+ '9' => '(',
+ '0' => ')',
+ '-' => '_',
+ '=' => '+',
+ '[' => '{',
+ ']' => '}',
+ '\\' => '|',
+ ';' => ':',
+ '\'' => '"',
+ ',' => '<',
+ '.' => '>',
+ '/' => '?',
+ '`' => '~',
+ else => byte,
+ };
+}
+
+fn fieldNumber(field: []const u8) ?u32 {
+ const end = std.mem.indexOfScalar(u8, field, ':') orelse field.len;
+ if (end == 0) return null;
+ return std.fmt.parseInt(u32, field[0..end], 10) catch null;
+}
+
+fn fieldEvent(field: []const u8) ?u32 {
+ const colon = std.mem.indexOfScalar(u8, field, ':') orelse return null;
+ const tail = field[colon + 1 ..];
+ if (tail.len == 0) return null;
+ return std.fmt.parseInt(u32, tail, 10) catch null;
+}
+
+test "line REPL keyboard protocol setup and restore are balanced (#981)" {
+ try std.testing.expectEqualStrings("\x1b[?2004h\x1b[>4;2m", enable_seq);
+ try std.testing.expectEqualStrings("\x1b[>4;0m\x1b[?2004l", restore_seq);
+}
+
+test "legacy ESC Delete remains Option word-delete (#981)" {
+ try std.testing.expect(legacyDelete(0x7f) == .delete_word);
+ try std.testing.expect(legacyDelete(0x08) == .delete_word);
+ try std.testing.expect(legacyDelete('x') == null);
+}
+
+test "Kitty modified Backspace distinguishes Command Option and releases (#981)" {
+ try std.testing.expect(csiDelete("127;9", 'u') == .delete_to_start);
+ try std.testing.expect(csiDelete("8;5", 'u') == .delete_to_start);
+ try std.testing.expect(csiDelete("127;3", 'u') == .delete_word);
+ try std.testing.expect(csiDelete("127;9:1", 'u') == .delete_to_start);
+ try std.testing.expect(csiDelete("127;9:2", 'u') == .delete_to_start);
+ try std.testing.expect(csiDelete("127;9:3", 'u') == null);
+ try std.testing.expect(csiDelete("127;9:4", 'u') == null);
+ try std.testing.expect(csiDelete("97;9", 'u') == null);
+}
+
+test "modifyOtherKeys distinguishes Command Option and malformed input (#981)" {
+ try std.testing.expect(csiDelete("27;9;127", '~') == .delete_to_start);
+ try std.testing.expect(csiDelete("27;5;8", '~') == .delete_to_start);
+ try std.testing.expect(csiDelete("27;3;127", '~') == .delete_word);
+ try std.testing.expect(csiDelete("27;1;127", '~') == null);
+ try std.testing.expect(csiDelete("27;9", '~') == null);
+ try std.testing.expect(csiDelete("26;9;127", '~') == null);
+ try std.testing.expect(csiDelete("27;9;97", '~') == null);
+ try std.testing.expect(csiDelete("27;9;127", 'R') == null);
+}
+
+test "modifyOtherKeys replay preserves existing Ctrl Alt and Shift editing (#981)" {
+ var out: [2]u8 = undefined;
+ try std.testing.expectEqualStrings("\x15", csiReplay("27;5;117", '~', &out)); // Ctrl-U
+ try std.testing.expectEqualStrings("\x01", csiReplay("27;5;97", '~', &out)); // Ctrl-A
+ try std.testing.expectEqualStrings("\x1bb", csiReplay("27;3;98", '~', &out)); // Alt-b
+ try std.testing.expectEqualStrings("A", csiReplay("27;2;97", '~', &out));
+ try std.testing.expectEqualStrings("!", csiReplay("27;2;49", '~', &out));
+ try std.testing.expectEqualStrings("", csiReplay("27;9;122", '~', &out));
+ try std.testing.expectEqualStrings("", csiReplay("27;9;127", '~', &out)); // handled as delete
+ try std.testing.expectEqualStrings("", csiReplay("127;9", 'u', &out));
+}
diff --git a/src/test_hooks.zig b/src/test_hooks.zig
index fefa7d79..77cd4cac 100644
--- a/src/test_hooks.zig
+++ b/src/test_hooks.zig
@@ -353,6 +353,7 @@ test {
_ = @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("readline_keys.zig"); // #981: Cmd/Option modified-Backspace decoding
_ = @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("session_list.zig"); // #712: device-wide saved-session listing