graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Merge fix/tui-lifecycle-lane: engine-routed background ops, crash-proof restore, quit ordering
Rach Pradhana6ab56a56ed0
diff --cc TUI/run.zigindex 9cb37d62,ff05322a..37c8de6c--- a/TUI/run.zig+++ b/TUI/run.zig@@@ -138,36 -148,16 +150,36 @@@ pub fn run// Short wait while an unfinished escape sequence is pending: if// nothing follows, the lone ESC was a real Escape keypress (#94).- const wait: i32 = if (pending_len > 0) 25 else if (m.pending != null) 50 else 200;+ const wait: i32 = if (pending_len > 0) 25 else if (m.pending != null or m.bg != null) 50 else 200;if (!tty.poll(wait)) {+ if (key_mod.inPaste() and m.now_ms -| last_input_ms >= paste_idle_ms) {+ // A `CSI 200~` whose `CSI 201~` never arrives latches the+ // composer into literal mode for the rest of the session:+ // Enter only inserts a newline, and Escape, Tab and every+ // slash command are swallowed. Close it out once the terminal+ // has been quiet far longer than any paste keeps streaming+ // (#532/#536/#548).+ closePaste(&m);+ last_input_ms = m.now_ms;+ }if (pending_len > 0) {- esc_stall += 1;- switch (stallVerdict(inbuf[0..pending_len], esc_stall)) {+ esc_stall +|= 1;+ switch (stallVerdict(inbuf[0..pending_len], esc_stall, .{+ .turn_live = m.pending != null,+ .in_paste = key_mod.inPaste(),+ })) {.wait => {},.escape_key => {- if (keys.handle(&m, .escape) == .quit) m.running = false;+ // Keep the ESC. If the next read is this sequence's+ // body after all, the two rejoin and it parses as the+ // arrow / mouse report / OSC reply it always was+ // instead of spraying `[<35;80;24M` into the composer+ // (#530).+ key_mod.stashOrphanHead(inbuf[0..pending_len]);+ stash_ms = m.now_ms;pending_len = 0;esc_stall = 0;+ if (keys.handle(&m, .escape) == .quit) m.running = false;},.drop => {// A sequence the terminal never finished, waited out.@@@ -245,45 -271,15 +302,49 @@@/// delivering Escape cancelled live turns and typing the late tail sprayed/// "2;39M"-style debris into the transcript — wait for the tail instead, and/// only silently drop once it is clearly never coming.+ fn nowMs(io: Io) u64 {+ return @intCast(@divTrunc(@max(@as(i128, 0), Io.Timestamp.now(io, .real).nanoseconds), 1_000_000));+ }+pub const StallVerdict = enum { wait, escape_key, drop };-pub fn stallVerdict(pending: []const u8, stalls: u8) StallVerdict {+pub const StallCtx = struct {+ /// A turn is streaming. A phantom Escape here CANCELS it, and this is+ /// precisely when the 1003 motion flood makes a split sequence likely, so+ /// the lone-ESC grace stretches. Idle, #94's snappy Escape is untouched.+ turn_live: bool = false,+ /// Inside a bracketed paste: a lone ESC is far more likely to be the head+ /// of the closing `CSI 201~` than the Escape key, and giving up on that+ /// marker wedges the composer.+ in_paste: bool = false,+};++/// ~25ms a stall: 2 polls idle, 8 (~200ms) while a turn streams.+const esc_grace_idle: u8 = 2;+const esc_grace_live: u8 = 8;+/// A paste marker is worth ~2s before we conclude it is never coming.+const paste_marker_stalls: u8 = 80;+/// Input silence that ends a bracketed paste nothing else can close.+const paste_idle_ms: u64 = 2000;+/// How long a given-up head stays eligible to rejoin its tail. A sequence cut+/// by ssh/tmux jitter finishes within a few hundred ms of the give-up; past+/// that the next bytes are a human typing, and `\x1b[` + `Hello` would eat the+/// H (`CSI H` is a legal Home).+const carry_window_ms: u64 = 400;++fn carryExpired(now_ms: u64, stash_ms: u64) bool {+ return now_ms -| stash_ms > carry_window_ms;+}++pub fn stallVerdict(pending: []const u8, stalls: u8, ctx: StallCtx) StallVerdict {if (pending.len == 0) return .wait;- if (pending.len == 1 and pending[0] == 0x1b) return if (stalls >= 2) .escape_key else .wait;+ if (ctx.in_paste and isPasteMarkerPrefix(pending)) {+ return if (stalls >= paste_marker_stalls) .drop else .wait;+ }+ if (pending.len == 1 and pending[0] == 0x1b) {+ const grace = if (ctx.turn_live) esc_grace_live else esc_grace_idle;+ return if (stalls >= grace) .escape_key else .wait;+ }return if (stalls >= 20) .drop else .wait;}