Skip to content

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

Python

fix(oneshot): fatal exits still print the [usage] footer and name completed work (#387, #389)

Rach Pradhanf884614cd77e
diff --git a/src/pricing.zig b/src/pricing.zig
index 3e38ba9b..94032927 100644
--- a/src/pricing.zig
+++ b/src/pricing.zig
@@ -128,6 +128,17 @@ pub const CostTally = struct {
pub var g_cost: CostTally = .{};
+/// The one-shot `[usage]` stderr footer. Shared by the success path and the
+/// fatal paths (#387/#389): the expensive runs — budget exhaustion, a late
+/// gateway refusal — must not exit without cost accounting.
+pub fn printUsageFooter(io: Io) void {
+ var buf: [256]u8 = undefined;
+ var w: Io.Writer = .fixed(&buf);
+ if (CostTally.render(g_cost.snap(io), &w)) {
+ std.debug.print("[usage] {s}\n", .{w.buffered()});
+ } else |_| {}
+}
+
// Known models per provider: context window in tokens. Direct-provider
// numbers from models.dev/api.json, codegraff numbers from the gateway's
// /v1/models endpoint (both snapshot 2026-06-10). The same model name can
diff --git a/src/session_run.zig b/src/session_run.zig
index 7ebbd754..b9996859 100644
--- a/src/session_run.zig
+++ b/src/session_run.zig
@@ -138,15 +138,15 @@ pub fn runOneshotPrompt(gpa: Allocator, io: Io, arena: Allocator, root: *agent_m
try root.messages.append(try messages_mod.textMessage(arena, "user", oneshot_user));
if (telemetry.g_telem) |t| t.countTurn();
const final_text = providers.runTurnWithFallback(root, keys, arena, null) catch |err| {
- // std.process.fatal does not unwind main's defers. Mirror their normal
- // order here: join the fleet worker, reap background jobs and pumps,
- // then emit/upload the terminal behavioral event.
+ // std.process.fatal does not unwind main's defers. Mirror their order:
+ // join the fleet, reap jobs/pumps, then the terminal behavioral event.
fleet.joinElites(io);
jobs.jobsReap(gpa, io);
- // Mirror main's teardown fully: flush buffered OTLP telemetry too, so
- // a fatal one-shot is visible in ordinary telemetry, not only behavioral.
+ // Flush buffered OTLP too: a fatal one-shot stays visible in ordinary telemetry.
if (telemetry.g_telem) |t| t.flush();
if (tracer.behavior) |behavior| behavior.finish(.failed);
+ pricing.printUsageFooter(io); // #387/#389: fatal exits still owe cost accounting
+ if (root.tools_used.count() > 0) std.debug.print("note: {d} tool call(s) completed before the failure; their work was not rolled back\n", .{root.tools_used.count()});
switch (err) {
error.FallbackConsentRequired => std.process.fatal("saved model unavailable; provider '{s}' is not allowlisted — run graff interactively, then /fallback allow {s}", .{ root.provider.id, root.provider.id }),
error.ApiError => std.process.fatal("{s}", .{root.last_api_error orelse "api error"}),
@@ -157,11 +157,7 @@ pub fn runOneshotPrompt(gpa: Allocator, io: Io, arena: Allocator, root: *agent_m
try out.print("{s}\n", .{final_text});
try out.flush();
// Usage summary → stderr, so stdout stays exactly the answer.
- var ubuf: [256]u8 = undefined;
- var uw: Io.Writer = .fixed(&ubuf);
- if (pricing.CostTally.render(pricing.g_cost.snap(io), &uw)) {
- std.debug.print("[usage] {s}\n", .{uw.buffered()});
- } else |_| {}
+ pricing.printUsageFooter(io);
session.saveSession(root, arena, root.session_name) catch |err| {
std.debug.print("⚠ session save failed: {s}\n", .{@errorName(err)});
};