graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(windows): switch console to UTF-8 so TUI/REPL isn't CP437 (#607)
Rach Pradhanbb22a1504337
diff --git a/TUI/tty.zig b/TUI/tty.zigindex 995143f7..eed0b08f 100644--- a/TUI/tty.zig+++ b/TUI/tty.zig@@ -1,13 +1,69 @@//! Minimal raw-mode + size + stdin poll for the fullscreen TUI.//! Kept inside TUI/ so we do not share src/term.zig across modules.+//! Windows console APIs are duplicated here (not `@import("../src/win_api.zig")`)+//! because a file may belong to only one Zig 0.17 module.const std = @import("std");const builtin = @import("builtin");-pub const RawState = if (builtin.os.tag == .windows) struct { dummy: u8 = 0 } else std.posix.termios;+const is_windows = builtin.os.tag == .windows;++/// Windows console leaf used only on that OS. Mirrors src/win_api.zig's UTF-8+/// + VT bits so the fullscreen TUI can switch CP 65001 (#607) without sharing+/// the file with the harness module.+const w = if (is_windows) struct {+ const HANDLE = *anyopaque;+ const BOOL = i32;+ const DWORD = u32;+ const UINT = u32;+ const WORD = u16;+ const SHORT = i16;+ const STD_OUTPUT_HANDLE: DWORD = 0xFFFFFFF5;+ const ENABLE_PROCESSED_OUTPUT: DWORD = 0x0001;+ const ENABLE_VIRTUAL_TERMINAL_PROCESSING: DWORD = 0x0004;+ const CP_UTF8: UINT = 65001;+ const COORD = extern struct { X: SHORT, Y: SHORT };+ const SMALL_RECT = extern struct { Left: SHORT, Top: SHORT, Right: SHORT, Bottom: SHORT };+ const CONSOLE_SCREEN_BUFFER_INFO = extern struct {+ dwSize: COORD,+ dwCursorPosition: COORD,+ wAttributes: WORD,+ srWindow: SMALL_RECT,+ dwMaximumWindowSize: COORD,+ };+ extern "kernel32" fn GetStdHandle(nStdHandle: DWORD) callconv(.winapi) HANDLE;+ extern "kernel32" fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: *DWORD) callconv(.winapi) BOOL;+ extern "kernel32" fn SetConsoleMode(hConsoleHandle: HANDLE, dwMode: DWORD) callconv(.winapi) BOOL;+ extern "kernel32" fn GetConsoleScreenBufferInfo(hConsoleOutput: HANDLE, lpInfo: *CONSOLE_SCREEN_BUFFER_INFO) callconv(.winapi) BOOL;+ extern "kernel32" fn GetConsoleOutputCP() callconv(.winapi) UINT;+ extern "kernel32" fn SetConsoleOutputCP(wCodePageID: UINT) callconv(.winapi) BOOL;+ extern "kernel32" fn GetConsoleCP() callconv(.winapi) UINT;+ extern "kernel32" fn SetConsoleCP(wCodePageID: UINT) callconv(.winapi) BOOL;+} else struct {};++pub const RawState = if (is_windows) struct {+ output_cp: u32 = 0,+ input_cp: u32 = 0,+} else std.posix.termios;pub fn enterRaw() ?RawState {- if (builtin.os.tag == .windows) return .{};+ if (is_windows) {+ const h = w.GetStdHandle(w.STD_OUTPUT_HANDLE);+ var mode: u32 = 0;+ if (w.GetConsoleMode(h, &mode) != 0) {+ _ = w.SetConsoleMode(h, mode | w.ENABLE_VIRTUAL_TERMINAL_PROCESSING | w.ENABLE_PROCESSED_OUTPUT);+ }+ // #607: zigzag UTF-8s its Windows platform layer; this path never did,+ // so PowerShell 5.1 decoded box-drawing as CP437. Restore the prior CPs+ // on the way out so the parent shell keeps its OEM page.+ const orig: RawState = .{+ .output_cp = w.GetConsoleOutputCP(),+ .input_cp = w.GetConsoleCP(),+ };+ _ = w.SetConsoleOutputCP(w.CP_UTF8);+ _ = w.SetConsoleCP(w.CP_UTF8);+ return orig;+ }const fd = std.posix.STDIN_FILENO;const orig = std.posix.tcgetattr(fd) catch return null;var raw = orig;@@ -23,19 +79,23 @@ pub fn enterRaw() ?RawState {}pub fn restore(state: RawState) void {- if (builtin.os.tag == .windows) return;+ if (is_windows) {+ if (state.output_cp != 0) _ = w.SetConsoleOutputCP(state.output_cp);+ if (state.input_cp != 0) _ = w.SetConsoleCP(state.input_cp);+ return;+ }std.posix.tcsetattr(std.posix.STDIN_FILENO, .NOW, state) catch {};}pub fn poll(timeout_ms: i32) bool {- if (builtin.os.tag == .windows) return false;+ if (is_windows) return false;var fds = [_]std.posix.pollfd{.{ .fd = std.posix.STDIN_FILENO, .events = std.posix.POLL.IN, .revents = 0 }};const n = std.posix.poll(&fds, timeout_ms) catch return false;return n > 0;}pub fn readStdin(buf: []u8) usize {- if (builtin.os.tag == .windows) return 0;+ if (is_windows) return 0;return std.posix.read(std.posix.STDIN_FILENO, buf) catch 0;}@@ -45,8 +105,40 @@ test "raw mode surrenders ^V and ^S to the app, not the line discipline (#523)"try std.testing.expect(std.mem.indexOf(u8, src, "IXON = false") != null);}+test "Windows enterRaw switches the console to UTF-8 and restore puts the CPs back (#607)" {+ const src = @embedFile("tty.zig");+ try std.testing.expect(std.mem.indexOf(u8, src, "CP_UTF8: UINT = 65001") != null);+ const enter_at = std.mem.indexOf(u8, src, "pub fn enterRaw() ?RawState {").?;+ const enter = src[enter_at..std.mem.indexOf(u8, src, "pub fn restore(").?];+ try std.testing.expect(std.mem.indexOf(u8, enter, "SetConsoleOutputCP(w.CP_UTF8)") != null);+ try std.testing.expect(std.mem.indexOf(u8, enter, "SetConsoleCP(w.CP_UTF8)") != null);+ const restore_at = std.mem.indexOf(u8, src, "pub fn restore(state: RawState) void {").?;+ const rest = src[restore_at..std.mem.indexOf(u8, src, "pub fn poll(").?];+ try std.testing.expect(std.mem.indexOf(u8, rest, "SetConsoleOutputCP(state.output_cp)") != null);+ try std.testing.expect(std.mem.indexOf(u8, rest, "SetConsoleCP(state.input_cp)") != null);+}++fn consoleSize() ?struct { cols: usize, rows: usize } {+ if (is_windows) {+ var info: w.CONSOLE_SCREEN_BUFFER_INFO = undefined;+ if (w.GetConsoleScreenBufferInfo(w.GetStdHandle(w.STD_OUTPUT_HANDLE), &info) == 0) return null;+ const width = @as(i32, info.srWindow.Right) - info.srWindow.Left + 1;+ const height = @as(i32, info.srWindow.Bottom) - info.srWindow.Top + 1;+ return .{+ .cols = if (width <= 0) 80 else @intCast(width),+ .rows = if (height <= 0) 24 else @intCast(height),+ };+ }+ return null;+}+pub fn cols() usize {- if (builtin.os.tag == .windows) return 80;+ if (is_windows) {+ const c = (consoleSize() orelse return 80).cols;+ if (c == 0) return 80;+ if (c > 400) return 400;+ return c;+ }var wsz: std.posix.winsize = undefined;const rc = std.posix.system.ioctl(std.posix.STDOUT_FILENO, std.posix.T.IOCGWINSZ, @intFromPtr(&wsz));if (std.posix.errno(rc) != .SUCCESS) return 80;@@ -57,7 +149,12 @@ pub fn cols() usize {}pub fn rows() usize {- if (builtin.os.tag == .windows) return 24;+ if (is_windows) {+ const r = (consoleSize() orelse return 24).rows;+ if (r == 0) return 24;+ if (r > 200) return 200;+ return r;+ }var wsz: std.posix.winsize = undefined;const rc = std.posix.system.ioctl(std.posix.STDOUT_FILENO, std.posix.T.IOCGWINSZ, @intFromPtr(&wsz));if (std.posix.errno(rc) != .SUCCESS) return 24;diff --git a/src/main.zig b/src/main.zigindex 7298b74c..daa7e1f9 100644--- a/src/main.zig+++ b/src/main.zig@@ -260,7 +260,7 @@ pub fn main(init: std.process.Init) !void {g_session_arena = init.arena;g_mem_debug = init.environ_map.get("GRAFF_MEM_DEBUG") != null;g_no_browser = init.environ_map.get("GRAFF_NO_BROWSER") != null;- // Windows: let the console interpret ANSI/VT escapes so the harness's color and cursor sequences render instead of literal text.+ // Windows: VT + UTF-8 (CP 65001) so color/cursor work and box-drawing is not CP437 (#607).if (builtin.os.tag == .windows) tty.enableVtOutput();// Environment supplies the default; an explicit CLI flag parsed below has// the usual higher precedence for this invocation.diff --git a/src/term.zig b/src/term.zigindex fdc7049e..6256d1d1 100644--- a/src/term.zig+++ b/src/term.zig@@ -139,14 +139,19 @@ pub const tty = struct {/// from anywhere (including a path that never reached the editor's defer).var raw_outer: ?RawState = null;- /// One-time: let the Windows console interpret ANSI/VT escapes. No-op- /// elsewhere. Call once from main before any styled output.+ /// One-time: let the Windows console interpret ANSI/VT escapes and decode+ /// stdout as UTF-8 (CP 65001). No-op elsewhere. Call once from main before+ /// any styled output. PowerShell 5.1/conhost otherwise treat box-drawing+ /// as CP437 (#607); zigzag already did this in its raw-mode path.pub fn enableVtOutput() void {if (!is_windows) return;const h = win.GetStdHandle(win.STD_OUTPUT_HANDLE);var mode: u32 = 0;- if (win.GetConsoleMode(h, &mode) == 0) return;- _ = win.SetConsoleMode(h, mode | win.ENABLE_VIRTUAL_TERMINAL_PROCESSING | win.ENABLE_PROCESSED_OUTPUT);+ if (win.GetConsoleMode(h, &mode) != 0) {+ _ = win.SetConsoleMode(h, mode | win.ENABLE_VIRTUAL_TERMINAL_PROCESSING | win.ENABLE_PROCESSED_OUTPUT);+ }+ _ = win.SetConsoleOutputCP(win.CP_UTF8);+ _ = win.SetConsoleCP(win.CP_UTF8);}/// Terminal width in columns; 80 on any failure.@@ -517,3 +522,14 @@ test "#396 firewall: both completion paths release the terminal" {const loop_release_at = std.mem.indexOf(u8, loop, "defer terminal.tty.releaseTerminal();").?;try std.testing.expect(flush_at < loop_release_at);}++test "Windows VT enable also switches the console to UTF-8 (#607)" {+ try std.testing.expectEqual(@as(u32, 65001), win.CP_UTF8);+ const src = @embedFile("term.zig");+ const fn_at = std.mem.indexOf(u8, src, "pub fn enableVtOutput() void {").?;+ const window = src[fn_at..std.mem.indexOfPos(u8, src, fn_at, "fn cols()").?];+ try std.testing.expect(std.mem.indexOf(u8, window, "SetConsoleOutputCP(win.CP_UTF8)") != null);+ try std.testing.expect(std.mem.indexOf(u8, window, "SetConsoleCP(win.CP_UTF8)") != null);+ const main_src = @embedFile("main.zig");+ try std.testing.expect(std.mem.indexOf(u8, main_src, "tty.enableVtOutput()") != null);+}diff --git a/src/win_api.zig b/src/win_api.zigindex 98e88fd2..940f32df 100644--- a/src/win_api.zig+++ b/src/win_api.zig@@ -13,10 +13,15 @@pub const HANDLE = *anyopaque;pub const BOOL = i32;pub const DWORD = u32;+pub const UINT = u32;pub const WORD = u16;pub const WCHAR = u16;pub const SHORT = i16;+/// UTF-8. PowerShell 5.1/conhost default to CP437/1252, which turns box-drawing+/// and icons into mojibake (#607). zigzag already switches; the line REPL did not.+pub const CP_UTF8: UINT = 65001;+pub const STD_INPUT_HANDLE: DWORD = 0xFFFFFFF6; // (DWORD)-10pub const STD_OUTPUT_HANDLE: DWORD = 0xFFFFFFF5; // (DWORD)-11@@ -63,3 +68,7 @@ pub extern "kernel32" fn GetNumberOfConsoleInputEvents(hConsoleInput: HANDLE, lppub extern "kernel32" fn ReadConsoleInputW(hConsoleInput: HANDLE, lpBuffer: [*]INPUT_RECORD, nLength: DWORD, lpRead: *DWORD) callconv(.winapi) BOOL;pub extern "kernel32" fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) callconv(.winapi) DWORD;pub extern "kernel32" fn PeekNamedPipe(hNamedPipe: HANDLE, lpBuffer: ?*anyopaque, nBufferSize: DWORD, lpBytesRead: ?*DWORD, lpTotalBytesAvail: ?*DWORD, lpBytesLeftThisMessage: ?*DWORD) callconv(.winapi) BOOL;+pub extern "kernel32" fn GetConsoleOutputCP() callconv(.winapi) UINT;+pub extern "kernel32" fn SetConsoleOutputCP(wCodePageID: UINT) callconv(.winapi) BOOL;+pub extern "kernel32" fn GetConsoleCP() callconv(.winapi) UINT;+pub extern "kernel32" fn SetConsoleCP(wCodePageID: UINT) callconv(.winapi) BOOL;