Skip to content

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

Python

fix(tui): claim alt-screen only; run writes enable_seq once

Cursor Agent6eb6c28ad548
diff --git a/TUI/restore.zig b/TUI/restore.zig
index c1c690c7..bf6a9153 100644
--- a/TUI/restore.zig
+++ b/TUI/restore.zig
@@ -97,9 +97,10 @@ pub fn claimScreen(enable_seq: []const u8) bool {
claim_raw = raw;
arm(raw, enable_seq);
if (builtin.os.tag != .windows) {
- _ = std.posix.system.write(std.posix.STDOUT_FILENO, enable_seq.ptr, enable_seq.len);
- const wipe = "\x1b[H\x1b[2J";
- _ = std.posix.system.write(std.posix.STDOUT_FILENO, wipe.ptr, wipe.len);
+ // Alt-screen only. Kitty/mouse/paste are a stack — run() writes
+ // the full enable_seq once when the loop actually starts.
+ const first = "\x1b[?1049h\x1b[?25l\x1b[H\x1b[2J";
+ _ = std.posix.system.write(std.posix.STDOUT_FILENO, first.ptr, first.len);
}
muteStderr();
claim_owned.store(true, .release);
diff --git a/TUI/run.zig b/TUI/run.zig
index d6b19a0d..2fa161c2 100644
--- a/TUI/run.zig
+++ b/TUI/run.zig
@@ -69,6 +69,7 @@ pub fn run(
const claimed = restore_mod.takeClaim();
var raw = claimed orelse (tty.enterRaw() orelse return error.NotATty);
+ if (claimed != null) _ = tty.enterRaw(); // boot may have put the line discipline back
if (claimed == null) restore_mod.arm(raw, enable_seq);
defer restore_mod.disarm();
defer tty.restore(raw);
@@ -76,9 +77,7 @@ pub fn run(
var out_buf: [64 * 1024]u8 = undefined;
var stdout = Io.File.stdout().writer(io, &out_buf);
const w = &stdout.interface;
- // Claim already wrote enable_seq (kitty is a stack). Writing it again
- // left a leftover push and failed mode-balance on the PTY probes.
- if (claimed == null) w.writeAll(enable_seq) catch {};
+ w.writeAll(enable_seq) catch {};
w.writeAll("\x1b]11;?\x07") catch {}; // background query -> auto light/dark (key.zig bg_report)
w.flush() catch {};
// Registered BEFORE the restore below, so LIFO puts the line on the normal
diff --git a/docs/adr/0042-tui-claims-screen-before-session.md b/docs/adr/0042-tui-claims-screen-before-session.md
index 5b871a80..423857ff 100644
--- a/docs/adr/0042-tui-claims-screen-before-session.md
+++ b/docs/adr/0042-tui-claims-screen-before-session.md
@@ -17,9 +17,12 @@ the steal — leaving the shell is.
## Decision
When the command is `tui` or `repl` (and not `--json` / `-p`),
-`startup.runSubcommand` claims the alt-screen (`tty.enterRaw` + enable
-seq + `restore.arm` + stderr mute) before credentials. `tui.run` takes
-that claim and does not re-arm over already-raw termios. If we never
+`startup.runSubcommand` claims the alt-screen (`tty.enterRaw` +
+`?1049h`/`?25l` wipe + `restore.arm` + stderr mute) before credentials.
+Kitty/mouse/paste stay on `run`'s single `enable_seq` write — they are
+a stack, and a second push fails PTY mode-balance. `tui.run` takes the
+claim, re-asserts raw after boot, and does not re-arm over already-raw
+termios. If we never
reach `run`, `restore.releaseIfOwned` on the shutdown stamps plus the
panic hook / fatal signals restore the shell. No libc `atexit` — Zig
exits via syscall and would skip it.