Skip to content

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

Python

fix(tui): a plain kitty keypress clears a stale alt/super latch

Rach Pradhan84d0deccab2a
diff --git a/TUI/key.zig b/TUI/key.zig
index a0ee5461..ecd88c7a 100644
--- a/TUI/key.zig
+++ b/TUI/key.zig
@@ -269,9 +269,14 @@ fn kitty(params: []const u8) Key {
const ev = eventOf(params);
if (code >= 57344 and code <= 57454) return functional(code, mods, ev);
if (ev == 3) return .ignore;
- // A live key with an explicit mods field is ground truth for held
- // modifiers — resync so a missed release can't latch alt/super forever.
- if (has_mods) held = mods & 10;
+ // A live key is ground truth for held modifiers — resync so a missed
+ // release can't latch alt/super forever. The ABSENT mods field is ground
+ // truth too: kitty omits it exactly when no modifiers are down, so a
+ // plain keypress must clear the latch. Before this, Cmd+Tab-ing away
+ // mid-composition latched super (the release went to the other app) and
+ // the next plain Backspace became Cmd+Backspace = delete-to-start,
+ // silently wiping the composer.
+ held = if (has_mods) mods & 10 else 0;
return mapCode(code, mods);
}
diff --git a/TUI/key_tests.zig b/TUI/key_tests.zig
index b4878ca7..b65bc788 100644
--- a/TUI/key_tests.zig
+++ b/TUI/key_tests.zig
@@ -243,3 +243,23 @@ test "Shift+9 is open-paren, Shift+0 is close-paren" {
i = 0;
try std.testing.expectEqual(Key{ .char = '(' }, next("\x1b[27;2;57~", &i).?);
}
+
+test "a plain CSI-u keypress clears a stale super latch — Backspace stays Backspace" {
+ // Cmd+Tab mid-composition: the super PRESS arrives, focus leaves, the
+ // RELEASE goes to the other app. Typing resumes with plain CSI-u keys
+ // (no mods field). Those must resync held to 0, or the next Backspace
+ // reads as Cmd+Backspace = delete_to_start and wipes the composer.
+ key.held = 8; // stale Super
+ var i: usize = 0;
+ try std.testing.expectEqual(Key{ .char = 'h' }, next("\x1b[104u", &i).?);
+ try std.testing.expectEqual(@as(u32, 0), key.held);
+ i = 0;
+ try std.testing.expectEqual(Key.backspace, next("\x7f", &i).?);
+ // Stale alt is the same class of bug (backspace -> delete_word).
+ key.held = 2;
+ i = 0;
+ try std.testing.expectEqual(Key{ .char = 'a' }, next("\x1b[97u", &i).?);
+ i = 0;
+ try std.testing.expectEqual(Key.backspace, next("\x7f", &i).?);
+ key.held = 0;
+}