graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(tui): a mid-turn failover moves the provider label with the model
justrach27c19c6e6402
diff --git a/TUI/app.zig b/TUI/app.zigindex 5993f3cb..dbbda914 100644--- a/TUI/app.zig+++ b/TUI/app.zig@@ -356,6 +356,24 @@ pub const Model = struct {engine.g_model_provider = picked.provider;}+ /// A mid-turn failover changed the seat behind the frontend's back (#551).+ /// Unlike `adoptModel` the model string dies with the drain that delivered+ /// the event, so it is copied here; the provider id is a static spec+ /// literal and is not owned, exactly as above.+ ///+ /// Both globals must move TOGETHER. The failover path used to set only the+ /// name, which left the new model paired with the OLD provider id — a seat+ /// that serves nothing — and `models.isCurrent` compares the provider too,+ /// so the picker marked the wrong row, or none at all.+ pub fn adoptFailover(self: *Model, changed: engine.ModelChanged) void {+ const owned = self.alloc.dupe(u8, changed.model) catch return;+ if (self.model_override) |old| self.alloc.free(old);+ self.model_override = owned;+ engine.g_model_name = owned;+ engine.g_model_provider = changed.provider;+ self.pushFmt(.system, "model \u{2192} {s}", .{owned}) catch {};+ }+pub fn setToast(self: *Model, text: []const u8) void {self.toast = text;self.toast_until_ms = self.now_ms + 1500;diff --git a/TUI/engine.zig b/TUI/engine.zigindex 312871d4..962efd57 100644--- a/TUI/engine.zig+++ b/TUI/engine.zig@@ -10,6 +10,7 @@ pub const Event = events_mod.Event;pub const EventQueue = events_mod.Queue;pub const ToolEvent = events_mod.Tool;pub const Status = events_mod.Status;+pub const ModelChanged = events_mod.ModelChanged;pub const Cost = events_mod.Cost;pub const Effort = enum { low, medium, high, xhigh, max, ultra };diff --git a/TUI/events.zig b/TUI/events.zigindex 912711e2..2add4e71 100644--- a/TUI/events.zig+++ b/TUI/events.zig@@ -83,6 +83,11 @@ pub const Status = struct {}};+/// What a mid-turn failover landed on. The provider travels with the model for+/// the same reason it travels with `Picked`: the picker's current-row marker+/// compares BOTH, so a name alone cannot tell two same-named seats apart.+pub const ModelChanged = struct { model: []const u8, provider: []const u8 = "" };+pub const Event = union(enum) {/// A tool call cleared the gates and is running (ToolInvocation).tool_started: Tool,@@ -94,7 +99,7 @@ pub const Event = union(enum) {/// One operational line about the session (session_notice) -> system row.notice: []const u8,/// The active provider failed over mid-turn (provider_fallback).- model_changed: []const u8,+ model_changed: ModelChanged,/// The engine's own context/cost meters (prompt_ready). The TUI renders/// /context, /session-info and the footer from THIS, instead of counting/// characters and calling the result a context meter.@@ -192,7 +197,9 @@ fn dupeEvent(gpa: std.mem.Allocator, ev: Event) ?Event {.tool_finished => |t| .{ .tool_finished = dupeTool(gpa, t) orelse return null },.tool_rejected => |t| .{ .tool_rejected = dupeTool(gpa, t) orelse return null },.notice => |s| .{ .notice = gpa.dupe(u8, s) catch return null },- .model_changed => |s| .{ .model_changed = gpa.dupe(u8, s) catch return null },+ // Only the model name is copied: a provider id is a static spec literal+ // (see app.Model.adoptModel), so it outlives the drain on its own.+ .model_changed => |m| .{ .model_changed = .{ .model = gpa.dupe(u8, m.model) catch return null, .provider = m.provider } },.status => |st| .{ .status = dupeStatus(gpa, st) orelse return null },};}@@ -213,7 +220,8 @@ fn freeEvent(gpa: std.mem.Allocator, ev: Event) void {gpa.free(t.name);gpa.free(t.detail);},- .notice, .model_changed => |s| gpa.free(s),+ .notice => |s| gpa.free(s),+ .model_changed => |m| gpa.free(m.model),.status => |st| {gpa.free(st.model);gpa.free(st.provider_id);diff --git a/TUI/turn.zig b/TUI/turn.zigindex 76b03598..5298af63 100644--- a/TUI/turn.zig+++ b/TUI/turn.zig@@ -241,12 +241,7 @@ fn applyEvent(self: *Model, ev: engine.Event) void {},.notice => |s| self.push(.system, s) catch {},.status => |st| self.setStatus(st),- .model_changed => |s| if (self.alloc.dupe(u8, s)) |owned| {- if (self.model_override) |old| self.alloc.free(old);- self.model_override = owned;- engine.g_model_name = owned;- self.pushFmt(.system, "model → {s}", .{owned}) catch {};- } else |_| {},+ .model_changed => |m| self.adoptFailover(m),}}@@ -381,7 +376,7 @@ test "a refusal and a failover reach the transcript instead of vanishing" {.denied = true,} });job.events.push(.{ .notice = "loaded 2 saved approval(s)" });- job.events.push(.{ .model_changed = "sonnet" });+ job.events.push(.{ .model_changed = .{ .model = "sonnet", .provider = "anthropic" } });job.done.store(true, .release);try m.push(.pending, "");m.pending = job;@@ -394,6 +389,10 @@ test "a refusal and a failover reach the transcript instead of vanishing" {try std.testing.expectEqualStrings("loaded 2 saved approval(s)", m.history.items[1].text);// The failover updates what the status bar reads, not just the transcript.try std.testing.expectEqualStrings("sonnet", engine.g_model_name);+ // ...and the PROVIDER moves with it. Updating only the name left the new+ // model paired with the old provider id, so models.isCurrent marked a seat+ // that no longer served the turn.+ try std.testing.expectEqualStrings("anthropic", engine.g_model_provider);try std.testing.expect(std.mem.indexOf(u8, m.history.items[2].text, "sonnet") != null);}diff --git a/src/tui_sink.zig b/src/tui_sink.zigindex 31563497..e6726a78 100644--- a/src/tui_sink.zig+++ b/src/tui_sink.zig@@ -186,7 +186,7 @@ fn emit(ctx: *anyopaque, ev: engine_sink.Stamped) void {// The status bar's model name is a TUI-owned global; a mid-turn// failover is the one thing that can change it behind the frontend's// back, so the event carries the substitute and the TUI adopts it.- .provider_fallback => |f| b.queue.push(.{ .model_changed = f.to_model }),+ .provider_fallback => |f| b.queue.push(.{ .model_changed = .{ .model = f.to_model, .provider = f.to_provider } }),// ── consciously ignored, with the reason ─────────────────────────// Live-stream bookkeeping: the TUI's pending row IS the spinner, and@@ -337,7 +337,8 @@ test "a refusal and a failover reach the frontend instead of being dropped" {try std.testing.expect(evs[0].tool_rejected.denied);try std.testing.expectEqualStrings("tool-call budget spent", evs[0].tool_rejected.detail);try std.testing.expectEqualStrings("loaded 2 saved approval(s)", evs[1].notice);- try std.testing.expectEqualStrings("sonnet", evs[2].model_changed);+ try std.testing.expectEqualStrings("sonnet", evs[2].model_changed.model);+ try std.testing.expectEqualStrings("anthropic", evs[2].model_changed.provider);}test "the engine's meters reach the frontend instead of being dropped (#551)" {