Skip to content

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

Python

Show unreported usage consistently across session views

Rach Pradhan8aacc70457e9
diff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.json
index 72b9ada5..acfb0cca 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": 2707,
+ "test_count_baseline": 2709,
"test_count_slack": 25,
"required_invariants": [
{
diff --git a/src/commands_misc.zig b/src/commands_misc.zig
index f3b3733c..c33f24e7 100644
--- a/src/commands_misc.zig
+++ b/src/commands_misc.zig
@@ -198,19 +198,7 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,
}
if (std.mem.eql(u8, line, "/cost") or std.mem.eql(u8, line, "/usage")) {
const c = g_cost.snap(root.io);
- if (c.api_calls == 0) {
- try out.writeAll("no API calls yet this session\n");
- } else {
- try out.print("{s}session usage{s}\n", .{ style.bold, style.reset });
- try out.print(" api calls: {d}", .{c.api_calls});
- if (c.sub_calls > 0) try out.print(" ({d} subscription, flat-rate)", .{c.sub_calls});
- if (c.unpriced_calls > 0) try out.print(" ({d} on unpriced models)", .{c.unpriced_calls});
- try out.print("\n tokens: {d} in ({d} cached) + {d} out\n", .{ c.in_tokens + c.cache_tokens, c.cache_tokens, c.out_tokens });
- try out.print(" cost: {s}${d:.4}{s}{s}\n", .{
- style.green, c.usd, style.reset,
- if (c.sub_calls > 0 or c.unpriced_calls > 0) " (API-key calls with a known price only)" else "",
- });
- }
+ try renderSessionUsage(c, out);
@import("plan_usage.zig").appendTo(root.io, root.gpa, arena, root.home, keys.get("xai"), out);
try out.flush();
return true;
@@ -498,3 +486,25 @@ pub fn handleCommand(root: *Agent, keys: *Keys, arena: Allocator, line: []const
if (try tryHandle(root, keys, arena, line, out)) return;
try handleRest(line, out);
}
+
+/// Render known usage without hiding attempts whose usage was not reported.
+pub fn renderSessionUsage(c: pricing.CostTally, out: *Io.Writer) !void {
+ if (c.missing_usage_calls > 0 or c.unreported_failed_attempts > 0) {
+ try pricing.CostTally.render(c, out);
+ try out.writeByte('\n');
+ return;
+ }
+ if (c.api_calls == 0) {
+ try out.writeAll("no API calls yet this session\n");
+ } else {
+ try out.print("{s}session usage{s}\n", .{ style.bold, style.reset });
+ try out.print(" api calls: {d}", .{c.api_calls});
+ if (c.sub_calls > 0) try out.print(" ({d} subscription, flat-rate)", .{c.sub_calls});
+ if (c.unpriced_calls > 0) try out.print(" ({d} on unpriced models)", .{c.unpriced_calls});
+ try out.print("\n tokens: {d} in ({d} cached) + {d} out\n", .{ c.in_tokens + c.cache_tokens, c.cache_tokens, c.out_tokens });
+ try out.print(" cost: {s}${d:.4}{s}{s}\n", .{
+ style.green, c.usd, style.reset,
+ if (c.sub_calls > 0 or c.unpriced_calls > 0) " (API-key calls with a known price only)" else "",
+ });
+ }
+}
diff --git a/src/obs.zig b/src/obs.zig
index 83c83198..786e8408 100644
--- a/src/obs.zig
+++ b/src/obs.zig
@@ -327,7 +327,7 @@ pub fn renderHud(w: *Io.Writer) !void {
// uses — never from the event ring (which has no output tokens or USD).
if (g_io) |io| {
const c = pricing.g_cost.snap(io);
- if (c.api_calls > 0) {
+ if (c.api_calls > 0 or c.missing_usage_calls > 0 or c.unreported_failed_attempts > 0) {
try w.writeAll(" usage ");
try pricing.CostTally.render(c, w);
try w.writeByte('\n');
@@ -348,7 +348,7 @@ pub fn renderHud(w: *Io.Writer) !void {
pub fn renderUsage(w: *Io.Writer) !void {
if (g_io) |io| {
const c = pricing.g_cost.snap(io);
- if (c.api_calls == 0) {
+ if (c.api_calls == 0 and c.missing_usage_calls == 0 and c.unreported_failed_attempts == 0) {
try w.writeAll("no API calls yet this session\n");
return;
}
diff --git a/src/obs_cost_test.zig b/src/obs_cost_test.zig
index 5607017b..f97de09b 100644
--- a/src/obs_cost_test.zig
+++ b/src/obs_cost_test.zig
@@ -20,6 +20,8 @@ fn resetCost(io: Io) void {
c.api_calls = 0;
c.sub_calls = 0;
c.unpriced_calls = 0;
+ c.missing_usage_calls = 0;
+ c.unreported_failed_attempts = 0;
}
fn renderCost(io: Io, buf: []u8) []const u8 {
@@ -171,3 +173,45 @@ test "live emit path fills HUD and OTLP like a real turn" {
try std.testing.expect(!contains(otlp, "/Users/secret"));
try std.testing.expect(!contains(otlp, "/etc/passwd"));
}
+
+fn expectUncertainSurfaces(with_success: bool) !void {
+ const io = std.testing.io;
+ resetCost(io);
+ defer resetCost(io);
+ obs.reset();
+ defer obs.reset();
+ obs.attach(io);
+ pricing.g_cost.failedWithoutUsage(io, 1);
+ if (with_success) pricing.g_cost.add(io, .priced, "gpt-5.5", 1000, 200, 0, 50);
+
+ var expected_buf: [1024]u8 = undefined;
+ const expected = renderCost(io, &expected_buf);
+ try std.testing.expect(contains(expected, "known subtotal:"));
+ try std.testing.expect(contains(expected, "1 failed request attempt(s) without usage"));
+ const snapshot = pricing.g_cost.snap(io);
+ try std.testing.expectEqual(@as(u64, if (with_success) 1 else 0), snapshot.api_calls);
+ try std.testing.expectEqual(@as(u64, if (with_success) 50 else 0), snapshot.out_tokens);
+ if (with_success) try std.testing.expect(snapshot.usd > 0);
+
+ var repl_buf: [2048]u8 = undefined;
+ var repl: Io.Writer = .fixed(&repl_buf);
+ try @import("commands_misc.zig").renderSessionUsage(snapshot, &repl);
+ var usage_buf: [2048]u8 = undefined;
+ var usage: Io.Writer = .fixed(&usage_buf);
+ try obs.renderUsage(&usage);
+ var debug_buf: [4096]u8 = undefined;
+ var debug: Io.Writer = .fixed(&debug_buf);
+ try obs.renderHud(&debug);
+ for ([_][]const u8{ repl.buffered(), usage.buffered(), debug.buffered() }) |text| {
+ try std.testing.expect(contains(text, expected));
+ try std.testing.expect(!contains(text, "no API calls yet"));
+ }
+}
+
+test "usage surfaces preserve failed-only unknown accounting" {
+ try expectUncertainSurfaces(false);
+}
+
+test "usage surfaces preserve unknown attempts alongside known successful usage" {
+ try expectUncertainSurfaces(true);
+}
diff --git a/src/tui_launch.zig b/src/tui_launch.zig
index 2124c95e..4290fcbe 100644
--- a/src/tui_launch.zig
+++ b/src/tui_launch.zig
@@ -480,6 +480,8 @@ test "hudCb usage/debug use the cost-tally renderer, not chars" {
c.api_calls = 0;
c.sub_calls = 0;
c.unpriced_calls = 0;
+ c.missing_usage_calls = 0;
+ c.unreported_failed_attempts = 0;
c.mutex.unlock(io);
defer {
c.mutex.lockUncancelable(io);
@@ -490,6 +492,8 @@ test "hudCb usage/debug use the cost-tally renderer, not chars" {
c.api_calls = 0;
c.sub_calls = 0;
c.unpriced_calls = 0;
+ c.missing_usage_calls = 0;
+ c.unreported_failed_attempts = 0;
c.mutex.unlock(io);
}
obs.reset();