Skip to content

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

Python

fix(tui): Alt+Left as ESC ESC CSI does not cancel (#524)

Cursor Agent96b6d4f823da
diff --git a/TUI/key.zig b/TUI/key.zig
index 6b3bf48c..0352d63c 100644
--- a/TUI/key.zig
+++ b/TUI/key.zig
@@ -187,7 +187,15 @@ fn escapeSeq(bytes: []const u8, i: *usize) ?Key {
i.* += 1;
return .shift_enter;
}
- if (c0 == 0x1b) return .escape; // ESC ESC — deliver one, reparse the rest
+ if (c0 == 0x1b) {
+ // Meta-prefixed CSI/SS3 (Alt+Left = ESC ESC [ D). Emitting Escape
+ // here cancelled the live turn (#524). Bare ESC ESC is still Escape.
+ if (i.* + 1 < bytes.len and orphan.isSeqIntroducer(bytes[i.* + 1])) {
+ i.* += 1;
+ return escapeSeq(bytes, i);
+ }
+ return .escape;
+ }
if (c0 == 'O') return ss3(bytes, i);
if (c0 == ']' or c0 == 'P' or c0 == '_' or c0 == '^' or c0 == 'X') return stringSeq(bytes, i);
if (c0 != '[') {
diff --git a/TUI/key_loop_tests.zig b/TUI/key_loop_tests.zig
index 472be03f..e6d5b02f 100644
--- a/TUI/key_loop_tests.zig
+++ b/TUI/key_loop_tests.zig
@@ -489,3 +489,18 @@ test "an armed sweeper still never swallows a word the user typed" {
}
key.armOrphan(false);
}
+
+test "Alt+Left as ESC ESC CSI does not cancel (#524)" {
+ var same = Loop.init(std.testing.allocator);
+ defer same.deinit();
+ try same.read("\x1b\x1b[D");
+ try std.testing.expectEqualStrings("", same.typed.items);
+ try std.testing.expectEqual(@as(usize, 1), same.arrows);
+
+ var split = Loop.init(std.testing.allocator);
+ defer split.deinit();
+ try split.read("\x1b");
+ try split.read("\x1b[D");
+ try std.testing.expectEqualStrings("", split.typed.items);
+ try std.testing.expectEqual(@as(usize, 1), split.arrows);
+}
diff --git a/TUI/key_orphan.zig b/TUI/key_orphan.zig
index caa6e0de..7fa06bca 100644
--- a/TUI/key_orphan.zig
+++ b/TUI/key_orphan.zig
@@ -216,6 +216,13 @@ fn framedDroppedSuffix(h: []const u8, t: []const u8, final_at: usize, final: u8)
(at(h, t, 4) == '0' or at(h, t, 4) == '1');
}
+pub fn isSeqIntroducer(c: u8) bool {
+ return switch (c) {
+ '[', 'O', ']', 'P', '_', '^', 'X' => true,
+ else => false,
+ };
+}
+
/// Does `t` finish the truncated escape `h`? Only a join that yields a
/// COMPLETE sequence is worth making: after a give-up stall the next bytes are
/// just as likely to be a human resuming typing, and gluing a stale head onto
@@ -228,7 +235,22 @@ fn completesWithEvidence(h: []const u8, t: []const u8, dropped: bool) bool {
const n = h.len + t.len;
if (h.len == 0 or t.len == 0 or h[0] != 0x1b or n < 2) return false;
switch (at(h, t, 1)) {
- 0x1b => return true, // ESC ESC — a real Escape either way
+ 0x1b => {
+ // Bare ESC ESC is a real Escape. ESC ESC [ / O / OSC is Alt+seq (#524).
+ if (n == 2) return true;
+ const intro = at(h, t, 2);
+ if (!isSeqIntroducer(intro)) return true;
+ var buf: [max_head]u8 = undefined;
+ if (n - 1 > buf.len) return false;
+ var o: usize = 0;
+ var k: usize = 0;
+ while (k < n) : (k += 1) {
+ if (k == 1) continue;
+ buf[o] = at(h, t, k);
+ o += 1;
+ }
+ return completesWithEvidence(buf[0..1], buf[1..o], dropped);
+ },
'O' => return n >= 3 and isSs3Final(at(h, t, 2)) and
(n == 3 or at(h, t, 3) < 0x20 or at(h, t, 3) == 0x7f),
'[' => {
@@ -531,6 +553,9 @@ test "a join only happens when the tail really completes the head" {
try std.testing.expect(completes("\x1b", "OA"));
try std.testing.expect(completes("\x1b", "]11;rgb:14/14/14\x07"));
try std.testing.expect(completes("\x1b", "[200~payload"));
+ try std.testing.expect(completes("\x1b\x1b", "[D"));
+ try std.testing.expect(completes("\x1b", "\x1b[D"));
+ try std.testing.expect(completes("\x1b\x1b", "OA"));
try std.testing.expect(completes("\x1b[<35;80", ";24M"));
try std.testing.expect(completes("\x1b[201", "~"));
try std.testing.expect(completes("\x1b]11;rgb:1c", "1c/1c1c/1c1c\x07"));
diff --git a/TUI/key_recover.zig b/TUI/key_recover.zig
index fc85259e..147af09c 100644
--- a/TUI/key_recover.zig
+++ b/TUI/key_recover.zig
@@ -23,7 +23,22 @@ fn combinedSlice(head: []const u8, tail: []const u8, start: usize, end: usize, o
pub fn completed(head: []const u8, tail: []const u8) ?Event {
const n = head.len + tail.len;
switch (at(head, tail, 1)) {
- 0x1b => return .{ .event = .escape, .tail_len = 0 },
+ 0x1b => {
+ const total = head.len + tail.len;
+ if (total <= 2) return .{ .event = .escape, .tail_len = 0 };
+ if (head.len >= 2) {
+ var buf: [max_body]u8 = undefined;
+ const rest = head[2..];
+ if (1 + rest.len > buf.len) return null;
+ buf[0] = head[0];
+ @memcpy(buf[1..][0..rest.len], rest);
+ return completed(buf[0 .. 1 + rest.len], tail);
+ }
+ return if (completed(head, tail[1..])) |ev|
+ .{ .event = ev.event, .tail_len = ev.tail_len + 1 }
+ else
+ null;
+ },
'O' => {
if (head.len > 3) return null;
return .{ .event = key.decodeSs3(at(head, tail, 2)), .tail_len = 3 - head.len };
diff --git a/TUI/key_tests.zig b/TUI/key_tests.zig
index 2148b447..2b60b3a7 100644
--- a/TUI/key_tests.zig
+++ b/TUI/key_tests.zig
@@ -78,6 +78,20 @@ test "ESC ESC delivers an Escape and leaves the second pending" {
try std.testing.expectEqual(@as(usize, 1), i);
}
+test "ESC ESC CSI is Alt+arrow, not Escape (#524)" {
+ var i: usize = 0;
+ try std.testing.expectEqual(Key.left, next("\x1b\x1b[D", &i).?);
+ try std.testing.expectEqual(@as(usize, 4), i);
+ i = 0;
+ try std.testing.expectEqual(Key.right, next("\x1b\x1b[C", &i).?);
+ i = 0;
+ try std.testing.expectEqual(Key.up, next("\x1b\x1b[A", &i).?);
+ i = 0;
+ try std.testing.expectEqual(Key.down, next("\x1b\x1b[B", &i).?);
+ i = 0;
+ try std.testing.expectEqual(Key.up, next("\x1b\x1bOA", &i).?);
+}
+
test "kitty release events never fire keys" {
var i: usize = 0;
try std.testing.expectEqual(Key.ignore, next("\x1b[1;1:3A", &i).?);