Skip to content

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

Python

fix(windows): line REPL raw mode sets UTF-8 too (#607)

Rach Pradhan4d93b8d6c388
diff --git a/src/repl_run.zig b/src/repl_run.zig
index bd9c0b13..10c4cbba 100644
--- a/src/repl_run.zig
+++ b/src/repl_run.zig
@@ -4,6 +4,7 @@
//! 600-line goal).
const std = @import("std");
+const builtin = @import("builtin");
const zz = @import("zigzag");
const repl = @import("repl.zig");
@@ -112,5 +113,13 @@ pub fn runScripted(
}
pub fn main(init: std.process.Init) !void {
+ // Standalone `graff-repl` never visits src/main.zig. zigzag enableRawMode
+ // also sets CP 65001; this covers any bytes printed before raw mode (#607).
+ if (builtin.os.tag == .windows) @import("term.zig").tty.enableVtOutput();
return run(init.gpa, init.io, init.environ_map, null, null, null, null, "", "");
}
+
+test "standalone graff-repl enables Windows UTF-8 before zigzag raw mode (#607)" {
+ const src = @embedFile("repl_run.zig");
+ try std.testing.expect(std.mem.indexOf(u8, src, "tty.enableVtOutput()") != null);
+}
diff --git a/src/term.zig b/src/term.zig
index 6256d1d1..061ad6f9 100644
--- a/src/term.zig
+++ b/src/term.zig
@@ -22,9 +22,13 @@ pub const win = @import("win_api.zig");
pub const tty = struct {
const is_windows = builtin.os.tag == .windows;
- /// Saved terminal state for restore(): termios on POSIX, the prior console
- /// input-mode word on Windows.
- pub const RawState = if (is_windows) struct { in_mode: u32 } else std.posix.termios;
+ /// Saved terminal state for restore(): termios on POSIX; on Windows the
+ /// prior console input-mode word plus the code pages enterRaw replaced.
+ pub const RawState = if (is_windows) struct {
+ in_mode: u32,
+ output_cp: u32 = 0,
+ input_cp: u32 = 0,
+ } else std.posix.termios;
/// POSIX job control can move a process group out of the foreground between
/// a readiness check and the actual read/tcsetattr. A background terminal
@@ -145,10 +149,12 @@ pub const tty = struct {
/// 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) {
- _ = win.SetConsoleMode(h, mode | win.ENABLE_VIRTUAL_TERMINAL_PROCESSING | win.ENABLE_PROCESSED_OUTPUT);
+ for ([_]win.DWORD{ win.STD_OUTPUT_HANDLE, win.STD_ERROR_HANDLE }) |id| {
+ const h = win.GetStdHandle(id);
+ var mode: u32 = 0;
+ 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);
@@ -195,8 +201,17 @@ pub const tty = struct {
raw |= win.ENABLE_VIRTUAL_TERMINAL_INPUT;
if (blocking) raw &= ~@as(u32, win.ENABLE_PROCESSED_INPUT) else raw |= win.ENABLE_PROCESSED_INPUT;
if (win.SetConsoleMode(h, raw) == 0) return null;
- if (life.entered()) raw_outer = .{ .in_mode = mode };
- return .{ .in_mode = mode };
+ // Line REPL (#607): readline/pickers go through here, not TUI/tty.
+ // Same CP 65001 switch zigzag uses, so `›` chrome is not CP437.
+ const orig: RawState = .{
+ .in_mode = mode,
+ .output_cp = win.GetConsoleOutputCP(),
+ .input_cp = win.GetConsoleCP(),
+ };
+ _ = win.SetConsoleOutputCP(win.CP_UTF8);
+ _ = win.SetConsoleCP(win.CP_UTF8);
+ if (life.entered()) raw_outer = orig;
+ return orig;
}
const fd = std.posix.STDIN_FILENO;
const orig = std.posix.tcgetattr(fd) catch return null;
@@ -221,6 +236,8 @@ pub const tty = struct {
if (life.restored()) raw_outer = null;
if (is_windows) {
_ = win.SetConsoleMode(win.GetStdHandle(win.STD_INPUT_HANDLE), state.in_mode);
+ if (state.output_cp != 0) _ = win.SetConsoleOutputCP(state.output_cp);
+ if (state.input_cp != 0) _ = win.SetConsoleCP(state.input_cp);
return;
}
var job_control = JobControlGuard.init(.output);
@@ -533,3 +550,17 @@ test "Windows VT enable also switches the console to UTF-8 (#607)" {
const main_src = @embedFile("main.zig");
try std.testing.expect(std.mem.indexOf(u8, main_src, "tty.enableVtOutput()") != null);
}
+
+test "line REPL raw mode switches the Windows console to UTF-8 (#607)" {
+ const src = @embedFile("term.zig");
+ const enter_at = std.mem.indexOf(u8, src, "pub fn enterRaw(blocking: bool) ?RawState {").?;
+ const enter = src[enter_at..std.mem.indexOf(u8, src, "pub fn restore(").?];
+ try std.testing.expect(std.mem.indexOf(u8, enter, "SetConsoleOutputCP(win.CP_UTF8)") != null);
+ try std.testing.expect(std.mem.indexOf(u8, enter, "SetConsoleCP(win.CP_UTF8)") != null);
+ const rest_at = std.mem.indexOf(u8, src, "pub fn restore(state: RawState) void {").?;
+ const rest = src[rest_at..std.mem.indexOfPos(u8, src, rest_at, "pub fn poll(").?];
+ try std.testing.expect(std.mem.indexOf(u8, rest, "SetConsoleOutputCP(state.output_cp)") != null);
+ // readline.zig is the line REPL prompt — not TUI/tty, not zigzag.
+ const rl = @embedFile("readline.zig");
+ try std.testing.expect(std.mem.indexOf(u8, rl, "tty.enterRaw(true)") != null);
+}
diff --git a/src/win_api.zig b/src/win_api.zig
index 940f32df..0542c378 100644
--- a/src/win_api.zig
+++ b/src/win_api.zig
@@ -24,6 +24,7 @@ pub const CP_UTF8: UINT = 65001;
pub const STD_INPUT_HANDLE: DWORD = 0xFFFFFFF6; // (DWORD)-10
pub const STD_OUTPUT_HANDLE: DWORD = 0xFFFFFFF5; // (DWORD)-11
+pub const STD_ERROR_HANDLE: DWORD = 0xFFFFFFF4; // (DWORD)-12
pub const ENABLE_PROCESSED_INPUT: DWORD = 0x0001;
pub const ENABLE_LINE_INPUT: DWORD = 0x0002;