Skip to content

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

Python

fix(tui): refuse a session reset while the engine holds the history

Rach Pradhan0889e008fdaf
diff --git a/TUI/app.zig b/TUI/app.zig
index 45be06e5..edbd4c6c 100644
--- a/TUI/app.zig
+++ b/TUI/app.zig
@@ -302,9 +302,17 @@ pub const Model = struct {
/// which the compaction replay also uses — that one REPLACES the visible
/// transcript for a conversation the engine has just rewritten and must
/// keep, while this one throws the conversation away too (#551).
- pub fn newSession(self: *Model) void {
+ ///
+ /// Returns false while an engine call is in flight, and does nothing: the
+ /// reset frees the arena that call is allocating its history from, so
+ /// "clear the screen" would be a use-after-free on the turn thread. /new
+ /// already refused mid-turn (#521); Ctrl+N did not, and only became
+ /// dangerous once the reset reached past the transcript.
+ pub fn newSession(self: *Model) bool {
+ if (self.pending != null or self.bg != null) return false;
self.clearHistory();
engine.historyChanged(.reset);
+ return true;
}
pub fn userTurnCount(self: *const Model) usize {
diff --git a/TUI/dispatch.zig b/TUI/dispatch.zig
index 41739111..38a16652 100644
--- a/TUI/dispatch.zig
+++ b/TUI/dispatch.zig
@@ -75,7 +75,7 @@ pub fn runCommand(self: *Model, line: []const u8) Effect {
self.quit_requested = true;
return .quit;
} else if (std.mem.eql(u8, canon, "/new")) {
- self.newSession();
+ _ = self.newSession(); // the `destroys` guard above already refused a live call
self.push(.system, "started a new conversation") catch {};
self.screen = .welcome;
} else if (std.mem.eql(u8, canon, "/home")) {
@@ -253,6 +253,12 @@ fn copyLastReply(self: *Model) void {
}
pub fn rewind(self: *Model) void {
+ // Same reason newSession refuses: the rewind reaches the engine's history,
+ // and the rewind overlay's Enter does not go through runCommand's guard.
+ if (self.pending != null or self.bg != null) {
+ self.push(.system, busy_note) catch {};
+ return;
+ }
var i = self.history.items.len;
while (i > 0) : (i -= 1) {
if (self.history.items[i - 1].kind == .user) break;
diff --git a/TUI/dispatch_tests.zig b/TUI/dispatch_tests.zig
index dc6eae94..86a52ee3 100644
--- a/TUI/dispatch_tests.zig
+++ b/TUI/dispatch_tests.zig
@@ -138,6 +138,48 @@ test "cutting the transcript reaches the engine's conversation (#551)" {
try std.testing.expectEqual(before, Seen.ops.items.len);
}
+test "a live engine call blocks every path that would free its history (#551)" {
+ // The reset/rewind seam frees the arena the running turn allocates its
+ // history from, so "just clear the screen" would be a use-after-free on
+ // the turn thread. Ctrl+N never went through runCommand's #521 guard.
+ const Seen = struct {
+ var n: usize = 0;
+ fn f(_: ?*anyopaque, _: engine.HistoryOp) void {
+ n += 1;
+ }
+ };
+ Seen.n = 0;
+ engine.g_history_fn = Seen.f;
+ defer engine.g_history_fn = null;
+
+ var m: Model = undefined;
+ m.setup(std.testing.allocator);
+ defer m.deinit();
+ try m.push(.user, "alpha");
+ const job = try std.testing.allocator.create(engine.Job);
+ job.* = .{
+ .threaded = false,
+ .gpa = std.testing.allocator,
+ .history = try std.testing.allocator.alloc(engine.Turn, 0),
+ .params = .{},
+ .stream = .{},
+ };
+ m.pending = job;
+
+ try std.testing.expect(!m.newSession());
+ dispatch.rewind(&m);
+ try std.testing.expectEqual(@as(usize, 0), Seen.n);
+ try std.testing.expectEqualStrings("alpha", m.history.items[0].text);
+
+ m.pending = null;
+ std.testing.allocator.free(job.history);
+ std.testing.allocator.destroy(job);
+
+ // With the engine idle both reach it again.
+ try std.testing.expect(m.newSession());
+ try std.testing.expectEqual(@as(usize, 1), Seen.n);
+}
+
test "/compact never crops locally; engine stub rewrites history" {
var m: Model = undefined;
m.setup(std.testing.allocator);
diff --git a/TUI/nav.zig b/TUI/nav.zig
index d8af7145..361deba6 100644
--- a/TUI/nav.zig
+++ b/TUI/nav.zig
@@ -51,9 +51,8 @@ fn isCtrl(k: Key, c: u8) bool {
fn ctrlN(self: *Model) void {
if (self.new_arm_until_ms != 0 and self.now_ms <= self.new_arm_until_ms) {
- self.newSession();
self.new_arm_until_ms = 0;
- self.setToast("new session");
+ self.setToast(if (self.newSession()) "new session" else "an engine call is still running — press Esc first");
return;
}
self.new_arm_until_ms = self.now_ms + 1000;
diff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.json
index aff61763..be57a1f0 100644
--- a/scripts/eval/tier1-manifest.json
+++ b/scripts/eval/tier1-manifest.json
@@ -15,7 +15,7 @@
"src/repl.zig",
"TUI/root.zig"
],
- "test_count_baseline": 1328,
+ "test_count_baseline": 1344,
"test_count_slack": 25,
"required_invariants": [
{