graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Merge branch 'perf/tui-event-pacing' into ui/tui-scrollbar-osc52
Rach Pradhan6b5fb100dd27
diff --cc TUI/root.zigindex 995a24c7,67153438..7252b0a6--- a/TUI/root.zig+++ b/TUI/root.zig@@@ -78,8 -76,8 +78,9 @@@ test_ = @import("overlays.zig");_ = run_mod;_ = @import("paint.zig");+ _ = @import("scrollpaint.zig");_ = @import("run_stall.zig");+ _ = @import("pacing.zig");_ = @import("traj.zig");}diff --cc TUI/run.zigindex 4b0c47cd,de6cd99b..d39ba6e3--- a/TUI/run.zig+++ b/TUI/run.zig@@@ -127,57 -140,75 +140,82 @@@ pub fn run// Background engine ops (/compact, !cmd, @-file list) land here too —// the same poll that keeps a turn from freezing the loop (#533).bgop.finish(&m);- const cols = @max(tty.cols(), @as(usize, 40));- const rows = @max(tty.rows(), @as(usize, 12));- const frame = render_mod.render(&m, gpa, cols, rows, m.now_ms) catch "tui: render error";- defer gpa.free(frame);- const hash = std.hash.Wyhash.hash(0, frame);- // The diff painter trusts `prev` to be what is on screen. Two things- // can make that a lie without changing a single byte of the frame: a- // resize the loop never sees (SIGWINCH between tty.cols() and the- // paint, or one that ends on the dimensions it started from — the- // terminal still reflowed), and any async writer over the same tty (a- // kitty image delete redrawing late). Neither moves the hash, so- // neither would ever be repaired. A resize EVENT forces the full- // clear-and-lay-down; the heartbeat forces a cheaper every-row rewrite- // of the frame we already believe is up, which under ?2026 is- // byte-identical output into a synchronized swap — visually a no-op.- const resized = restore_mod.takeResized();- const heal = resized or m.now_ms -| last_heal_ms >= heal_interval_ms;- if (hash != last_hash or heal) {- const has_gfx = std.mem.indexOf(u8, frame, "\x1b_G") != null;- // The theme bg is painted per ROW, not baked into the frame, and- // blank rows are byte-identical across themes/widths — the diff- // path skips them, stranding old-bg rows after /theme or the- // startup OSC-11 polarity flip, and stale columns after a- // width-only resize. Both must force a full paint.- const full = prev.len == 0 or rows != prev_rows or cols != prev_cols or m.theme_id != prev_theme or saw_gfx or has_gfx or resized;- // Kitty images sit above the cell grid and survive \x1b[K / dirty- // paints — delete before every redraw that might have shown one.- // ?2026 synchronized output: the terminal buffers everything- // between begin/end and swaps atomically, so a diff paint can- // never show a half-updated frame (grok-build does the same).- // Terminals without it ignore the pair — strictly no worse.- // Scrolling the transcript moves CELLS, not the kitty images that- // sit above the cell grid — and the self-heal exists precisely to- // rewrite rows a scroll would skip. Both take the whole-screen- // paths, so neither may reach the scroll fast path; `full` already- // folds in graphics, resize and theme.- const hint = if (full or heal) null else m.paint_hint;- w.writeAll("\x1b[?2026h") catch {};- if (saw_gfx or has_gfx) w.writeAll("\x1b_Ga=d,d=A,q=2\x1b\\") catch {};- paint_mod.paint(w, frame, rows, cols, if (full) &.{} else prev, m.theme().bg, heal, hint) catch {};- w.writeAll("\x1b[?2026l") catch {};- w.flush() catch {};- if (prev.len != 0) gpa.free(prev);- prev = gpa.dupe(u8, frame) catch &.{};- prev_rows = rows;- prev_cols = cols;- prev_theme = m.theme_id;- last_hash = hash;- saw_gfx = has_gfx;- // Only a paint that rewrote EVERY row resets the heartbeat.- if (full or heal) last_heal_ms = m.now_ms;+ pacing.ticks += 1;+ // At most ONE composed frame per tick, and while input keeps arriving,+ // at most one per frame budget (pacing.zig). The storm signal is a+ // backlog OR a stream still landing inside the storm window — a check+ // for queued bytes alone misses momentum entirely, because on a loop+ // this fast every report is read the instant it arrives and finds the+ // queue empty behind it. Quiet, the frame is composed on the spot, so a+ // single flick pays nothing. A skipped tick falls straight through to+ // the drain below, so the frame that does land shows the LATEST scroll+ // position — the intermediate ones are never painted at all.+ const more_pending = tty.poll(0);+ const painted = pacing.shouldPaint(m.now_ms, last_frame_ms, last_input_ms, more_pending);+ if (!painted) {+ pacing.frames_skipped += 1;+ } else {+ last_frame_ms = m.now_ms;+ pacing.frames += 1;+ frame_blk: {+ const cols = @max(tty.cols(), @as(usize, 40));+ const rows = @max(tty.rows(), @as(usize, 12));+ const frame = render_mod.render(&m, gpa, cols, rows, m.now_ms) catch break :frame_blk;+ defer gpa.free(frame);+ const hash = std.hash.Wyhash.hash(0, frame);+ // The diff painter trusts `prev` to be what is on screen. Two+ // things can make that a lie without changing a single byte of+ // the frame: a resize the loop never sees (SIGWINCH between+ // tty.cols() and the paint, or one that ends on the dimensions+ // it started from — the terminal still reflowed), and any async+ // writer over the same tty (a kitty image delete redrawing+ // late). Neither moves the hash, so neither would ever be+ // repaired. A resize EVENT forces the full clear-and-lay-down;+ // the heartbeat forces a cheaper every-row rewrite of the frame+ // we already believe is up, which under ?2026 is byte-identical+ // output into a synchronized swap — visually a no-op.+ const resized = restore_mod.takeResized();+ const heal = resized or m.now_ms -| last_heal_ms >= heal_interval_ms;+ if (hash != last_hash or heal) {+ const has_gfx = std.mem.indexOf(u8, frame, "\x1b_G") != null;+ // The theme bg is painted per ROW, not baked into the+ // frame, and blank rows are byte-identical across+ // themes/widths — the diff path skips them, stranding+ // old-bg rows after /theme or the startup OSC-11 polarity+ // flip, and stale columns after a width-only resize. Both+ // must force a full paint.+ const full = prev.len == 0 or rows != prev_rows or cols != prev_cols or m.theme_id != prev_theme or saw_gfx or has_gfx or resized;+ // Kitty images sit above the cell grid and survive \x1b[K /+ // dirty paints — delete before every redraw that might have+ // shown one. ?2026 synchronized output: the terminal+ // buffers everything between begin/end and swaps+ // atomically, so a diff paint can never show a half-updated+ // frame (grok-build does the same). Terminals without it+ // ignore the pair — strictly no worse.++ // Scrolling the transcript moves CELLS, not the kitty++ // images that sit above the cell grid — and the self-heal++ // exists precisely to rewrite rows a scroll would skip.++ // Both take the whole-screen paths, so neither may reach++ // the scroll fast path; `full` already folds in graphics,++ // resize and theme.++ const hint = if (full or heal) null else m.paint_hint;+ w.writeAll("\x1b[?2026h") catch {};+ if (saw_gfx or has_gfx) w.writeAll("\x1b_Ga=d,d=A,q=2\x1b\\") catch {};- paint_mod.paint(w, frame, rows, cols, if (full) &.{} else prev, m.theme().bg, heal) catch {};++ paint_mod.paint(w, frame, rows, cols, if (full) &.{} else prev, m.theme().bg, heal, hint) catch {};+ w.writeAll("\x1b[?2026l") catch {};+ w.flush() catch {};+ pacing.paints += 1;+ if (prev.len != 0) gpa.free(prev);+ prev = gpa.dupe(u8, frame) catch &.{};+ prev_rows = rows;+ prev_cols = cols;+ prev_theme = m.theme_id;+ last_hash = hash;+ saw_gfx = has_gfx;+ // Only a paint that rewrote EVERY row resets the heartbeat.+ if (full or heal) last_heal_ms = m.now_ms;+ }+ }}// Short wait while an unfinished escape sequence is pending: ifdiff --cc scripts/eval-tier1.shindex 43f3b4b8,07848e1a..b8776e9b--- a/scripts/eval-tier1.sh+++ b/scripts/eval-tier1.sh@@@ -243,13 -242,6 +243,16 @@@ if wanted tuiguard; theif python3 scripts/test-tui-typed-events.py zig-out/bin/graff; then :; elserecord_fail tuiguardfi++<<<<<<< HEAD+ # Same invariants, read off a VIRTUAL SCREEN instead of raw bytes+ # (scripts/ptyharness.py): the VT interpreter's own selftest, the ported+ # mode-balance check, and out-of-band screen corruption fed straight to the+ # tty.+ if python3 scripts/test-tui-screenstate.py zig-out/bin/graff; then :; else+ record_fail tuiguard+ fi++=======++>>>>>>> perf/tui-event-pacing# The frame painter may never show anything but the current frame. Storms# the window size, fills the screen with glyphs terminals measure# differently from us, then reads the SCREEN back and compares it cell for@@@ -257,13 -249,6 +260,16 @@@if python3 scripts/test-tui-painter.py zig-out/bin/graff; then :; elserecord_fail tuiguardfi++<<<<<<< HEAD+ # Scrolling is a viewport SLICE of one cached layout, not a re-layout+ # (TUI/layout_cache.zig). Reads the screen back on every scroll step and+ # after every width change: the markers on screen must be a contiguous+ # ascending run, which a stale block or a mis-offset slice cannot fake.+ if python3 scripts/test-tui-layout-cache.py zig-out/bin/graff; then :; else+ record_fail tuiguard+ fi++=======++>>>>>>> perf/tui-event-pacing# The viewport must not jump when the terminal changes width: drives# TIOCSWINSZ across five widths mid-scroll and reads the screen back. With# the logical scroll anchor removed the marker leaves the screen on the@@@ -271,13 -256,14 +277,21 @@@if python3 scripts/test-tui-resize-anchor.py zig-out/bin/graff; then :; elserecord_fail tuiguardfi+ # A wheel notch must be a scroll region + SU, not a screen rewrite — and a+ # storm of them must still leave the screen equal to a forced full repaint.+ # With the hint refused the probe fails on the FIRST notch (verified), so+ # this is a gate on both halves, not a smoke test.+ if python3 scripts/test-tui-scroll-paint.py zig-out/bin/graff; then :; else+ record_fail tuiguard+ fi+ # Trackpad momentum must be COALESCED and BUDGETED, not queued. Drives 500+ # SGR wheel reports twice — all at once, then spread over ~1s the way+ # momentum really arrives — and reads the loop's own paint counters back.+ # With the frame budget removed the paced storm paints 503 times instead of+ # ~105, so this is a real gate, not a smoke test.+ if python3 scripts/test-tui-event-pacing.py zig-out/bin/graff; then :; else+ record_fail tuiguard+ fififi