From d3a2369d1f04d012312d570ddc74b7c6f8453565 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Wed, 4 Nov 2026 17:26:31 +0000 Subject: [PATCH] Stage rows show cached share and cost; Changes explain no_patch_reason MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Timeline stage rows add the cached share (when cache_known) and a positive cost: 20.1M tokens · 97% cached · $1.42. The file viewer words each server no_patch_reason, keeping the old truncated/binary text as the fallback. --- .../changes/domain/change_wording.dart | 23 +++++ .../features/changes/domain/file_change.dart | 11 +++ .../changes/widgets/file_diff_viewer.dart | 6 +- app/lib/features/runs/domain/run_tokens.dart | 15 ++++ .../features/runs/ui/run_detail_content.dart | 2 +- app/notes/no-patch-reason.md | 9 ++ app/notes/run-tokens.md | 7 +- .../changes/no_patch_reason_test.dart | 87 +++++++++++++++++++ .../runs/ui/run_stage_tokens_test.dart | 47 +++++++++- 9 files changed, 202 insertions(+), 5 deletions(-) create mode 100644 app/notes/no-patch-reason.md create mode 100644 app/test/features/changes/no_patch_reason_test.dart diff --git a/app/lib/features/changes/domain/change_wording.dart b/app/lib/features/changes/domain/change_wording.dart index cf81814d..e1896e60 100644 --- a/app/lib/features/changes/domain/change_wording.dart +++ b/app/lib/features/changes/domain/change_wording.dart @@ -100,8 +100,31 @@ String noChangeDetail(RunChanges changes) { return emptyCaptureDetail; } +/// The message for the server's `no_patch_reason`, or null when it sent none +/// or one this client does not know (the generic wording then applies). +String? noPatchReasonMessage(String? reason) => switch (reason) { + 'binary' => 'Binary file; only its metadata was recorded.', + 'file_size' => 'This file is over 1 MiB, so no diff text was recorded.', + 'budget' => + 'The workspace snapshot ran out of text budget, so no diff text was ' + 'recorded.', + 'lines' => 'The change is over 5,000 lines, so no diff text was recorded.', + 'evidence_size' => + "This visit's recorded changes reached their size limit, so this patch " + 'was dropped.', + 'git_status' => + "This change was seen only in Git status, so the file's contents weren't " + 'compared.', + 'baseline' => + "The file's content at stage start couldn't be recovered from Git, so no " + 'diff was recorded.', + _ => null, +}; + /// Why a file has no patch to render, in the entry's own recorded terms. String patchUnavailableReason(FileChange file) { + final reason = noPatchReasonMessage(file.noPatchReason); + if (reason != null) return reason; if (file.truncated) { return "The server truncated this file's patch at its capture limit; no " 'diff text was recorded.'; diff --git a/app/lib/features/changes/domain/file_change.dart b/app/lib/features/changes/domain/file_change.dart index 0f8ccdcf..6068d6f2 100644 --- a/app/lib/features/changes/domain/file_change.dart +++ b/app/lib/features/changes/domain/file_change.dart @@ -12,12 +12,14 @@ class FileChange { String? patch, this.binary = false, this.truncated = false, + String? noPatchReason, String? beforeMode, String? afterMode, this.steps = const [], ParsedPatch? parsedPatch, }) : patch = _nonBlank(patch), beforeMode = _nonBlank(beforeMode), + noPatchReason = _nonBlank(noPatchReason), afterMode = _nonBlank(afterMode), // Aggregate copies pass their already-parsed patch through instead of // re-parsing the same recorded bytes for every stage that touched a path. @@ -32,6 +34,11 @@ class FileChange { final String? patch; final bool binary; final bool truncated; + + /// Why the server recorded no (complete) patch: `binary`, `file_size`, + /// `budget`, `lines`, `evidence_size`, `git_status` or `baseline`. Null from + /// an older server or when a patch was recorded. + final String? noPatchReason; final String? beforeMode; final String? afterMode; @@ -68,6 +75,7 @@ class FileChange { patch: patch, binary: binary, truncated: truncated, + noPatchReason: noPatchReason, beforeMode: beforeMode, afterMode: afterMode, steps: List.unmodifiable(value), @@ -88,6 +96,9 @@ class FileChange { patch: json['patch'] is String ? json['patch'] as String : null, binary: json['binary'] == true, truncated: json['truncated'] == true, + noPatchReason: json['no_patch_reason'] is String + ? json['no_patch_reason'] as String + : null, beforeMode: _textOrNull(json['before_mode']), afterMode: _textOrNull(json['after_mode']), ); diff --git a/app/lib/features/changes/widgets/file_diff_viewer.dart b/app/lib/features/changes/widgets/file_diff_viewer.dart index 8d5db5bd..2f049ea8 100644 --- a/app/lib/features/changes/widgets/file_diff_viewer.dart +++ b/app/lib/features/changes/widgets/file_diff_viewer.dart @@ -211,7 +211,11 @@ class _FileDiffViewerState extends State { Widget _body(BuildContext context, TextStyle mono) { final file = widget.file; - if (file.binary) return _message(context, binaryDiffMessage); + // A recorded reason is more specific than the generic binary message. + final reason = noPatchReasonMessage(file.noPatchReason); + if (file.binary && reason == null) { + return _message(context, binaryDiffMessage); + } if (!file.hasPatch) { return _message(context, patchUnavailableReason(file)); } diff --git a/app/lib/features/runs/domain/run_tokens.dart b/app/lib/features/runs/domain/run_tokens.dart index 4189c7d7..a683fc22 100644 --- a/app/lib/features/runs/domain/run_tokens.dart +++ b/app/lib/features/runs/domain/run_tokens.dart @@ -122,6 +122,21 @@ String formatTokenCount(int tokens) { String tokenUsageLabel(TokenUsage usage) => '${usage.partial ? '≥' : ''}${formatTokenCount(usage.total)} tokens'; +/// A stage row's compact label: the total, the cached share when the stage +/// recorded its cache split, and the cost when a positive one was reported. +/// `20.1M tokens · 95% cached · $0.42`. A missing figure is omitted, never +/// shown as zero. +String stageTokenRowLabel(TokenUsage usage) { + final cacheRead = usage.cacheRead; + final cost = usage.cost; + return [ + tokenUsageLabel(usage), + if (cacheRead != null && usage.total > 0) + '${(cacheRead * 100 / usage.total).round()}% cached', + if (cost != null) cost < 0.005 ? '<\$0.01' : '\$${cost.toStringAsFixed(2)}', + ].join(' · '); +} + /// The detail's token value: the compact total, then fresh versus cached /// when every stage reported its cache components, the in/out split when /// reported, and how many stages reported when the total is partial. diff --git a/app/lib/features/runs/ui/run_detail_content.dart b/app/lib/features/runs/ui/run_detail_content.dart index bb296eb4..236fb0ff 100644 --- a/app/lib/features/runs/ui/run_detail_content.dart +++ b/app/lib/features/runs/ui/run_detail_content.dart @@ -153,7 +153,7 @@ List detailTimeline(dynamic steps) { target: '${step['target'] ?? ''}'.trim(), duration: _visitDuration(step), tokens: switch (stageTokenUsage(JsonMap.from(step))) { - final usage? => tokenUsageLabel(usage), + final usage? => stageTokenRowLabel(usage), null => null, }, ); diff --git a/app/notes/no-patch-reason.md b/app/notes/no-patch-reason.md new file mode 100644 index 00000000..4e06852b --- /dev/null +++ b/app/notes/no-patch-reason.md @@ -0,0 +1,9 @@ +# Changes: why a file has no patch + +The server's `FileChange.no_patch_reason` (`binary`, `file_size`, `budget`, +`lines`, `evidence_size`, `git_status`, `baseline`) is parsed into +`FileChange.noPatchReason` and worded by `noPatchReasonMessage` in +`lib/features/changes/domain/change_wording.dart`. The file viewer shows it in +place of the generic truncated/binary message. A missing, blank or unknown +reason (older server) keeps the old wording. Tests: +`test/features/changes/no_patch_reason_test.dart`. diff --git a/app/notes/run-tokens.md b/app/notes/run-tokens.md index 96041afb..8c37944a 100644 --- a/app/notes/run-tokens.md +++ b/app/notes/run-tokens.md @@ -31,8 +31,11 @@ Runs that spend 20M+ tokens now show it the way they show disk size. - Summary: `Size on disk`, `Tokens` (`22.4M · 2.1M fresh, 20.3M cached · 1.8M in, 240K out`; fresh is total minus cache reads, shown only when `cache_known`) and `Cost`. -- Stage rows: the Timeline row subtitle adds `· 20.1M tokens`. The visit facts - add `Tokens` and `Cost`. +- Stage rows: the Timeline row subtitle adds `· 20.1M tokens · 97% cached · + $1.42` (`stageTokenRowLabel`). The cached share (cache reads over the total, + rounded) appears only when the stage recorded `cache_known`; the cost only + when positive, to two places, `<$0.01` below half a cent. The visit facts + add `Tokens` and `Cost` (`Not reported` stays there). - Cost is shown only when a positive `cost_usd` was reported. Subscription models report `cost_known` with no amount, which shows as `Not reported`. The stats page's `Reported cost` uses the same rule. diff --git a/app/test/features/changes/no_patch_reason_test.dart b/app/test/features/changes/no_patch_reason_test.dart new file mode 100644 index 00000000..d7e737a9 --- /dev/null +++ b/app/test/features/changes/no_patch_reason_test.dart @@ -0,0 +1,87 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/changes/changes.dart'; + +import 'widget_hosts.dart'; + +const _legacy = + "The server truncated this file's patch at its capture limit; no diff " + 'text was recorded.'; + +const _expected = { + 'file_size': 'This file is over 1 MiB, so no diff text was recorded.', + 'budget': + 'The workspace snapshot ran out of text budget, so no diff text was ' + 'recorded.', + 'lines': 'The change is over 5,000 lines, so no diff text was recorded.', + 'evidence_size': + "This visit's recorded changes reached their size limit, so this patch " + 'was dropped.', + 'git_status': + "This change was seen only in Git status, so the file's contents weren't " + 'compared.', + 'baseline': + "The file's content at stage start couldn't be recovered from Git, so no " + 'diff was recorded.', + 'binary': 'Binary file; only its metadata was recorded.', +}; + +FileChange _noPatch(Object? reason, {bool binary = false}) => + FileChange.fromJson({ + 'path': 'big.txt', + 'kind': 'modified', + 'truncated': true, + 'no_patch': true, + 'binary': binary, + 'no_patch_reason': ?reason, + }); + +void main() { + test('each recorded no-patch reason has its own message', () { + for (final entry in _expected.entries) { + final file = _noPatch(entry.key, binary: entry.key == 'binary'); + expect(file.noPatchReason, entry.key); + expect(patchUnavailableReason(file), entry.value, reason: entry.key); + } + }); + + test('no reason, a blank one or an unknown one keeps the old wording', () { + expect(patchUnavailableReason(_noPatch(null)), _legacy); + expect(patchUnavailableReason(_noPatch('')), _legacy); + expect(patchUnavailableReason(_noPatch('future_reason')), _legacy); + expect(patchUnavailableReason(_noPatch(42)), _legacy); + }); + + test('the reason survives aggregation across stages', () { + final file = _noPatch('budget').withSteps(const ['build']); + expect(file.noPatchReason, 'budget'); + }); + + testWidgets('the viewer shows the reason, binary included', (tester) async { + await tester.pumpWidget( + materialHost( + Column( + children: [ + Expanded(child: FileDiffViewer(file: _noPatch('lines'))), + Expanded( + child: FileDiffViewer(file: _noPatch('binary', binary: true)), + ), + Expanded( + child: FileDiffViewer( + file: FileChange( + path: 'logo.bin', + kind: ChangeKind.modified, + binary: true, + ), + ), + ), + ], + ), + ), + ); + expect(find.text(_expected['lines']!), findsOneWidget); + expect(find.text(_expected['binary']!), findsOneWidget); + expect(find.text(binaryDiffMessage), findsOneWidget); + expect(find.text(_legacy), findsNothing); + }); +} diff --git a/app/test/features/runs/ui/run_stage_tokens_test.dart b/app/test/features/runs/ui/run_stage_tokens_test.dart index ae6e12ac..f0706ef9 100644 --- a/app/test/features/runs/ui/run_stage_tokens_test.dart +++ b/app/test/features/runs/ui/run_stage_tokens_test.dart @@ -40,7 +40,52 @@ void main() { test('a stage row carries its compact token total', () { final visits = detailTimeline([_reported, _unreported]); - expect(visits[0].tokens, '1.5M tokens'); + expect(visits[0].tokens, '1.5M tokens · \$0.42'); expect(visits[1].tokens, isNull); }); + + String? rowLabel(Map usage) => detailTimeline([ + {..._unreported, 'usage': usage}, + ])[0].tokens; + + test('a stage row adds its cached share and cost when reported', () { + const base = {'tokens_known': true, 'tokens_total': 20100000}; + expect( + rowLabel({ + ...base, + 'cache_known': true, + 'cache_read': 19100000, + 'cost_known': true, + 'cost_usd': 0.4249, + }), + '20.1M tokens · 95% cached · \$0.42', + ); + // A known split with no reads is a real 0%, not an absence. + expect( + rowLabel({...base, 'cache_known': true}), + '20.1M tokens · 0% cached', + ); + // An older row without cache_known, or a cache read alone, shows no share. + expect(rowLabel({...base, 'cache_read': 19100000}), '20.1M tokens'); + // Tiny costs stay visible; zero or unknown costs are omitted on the row. + expect( + rowLabel({...base, 'cost_known': true, 'cost_usd': 0.0042}), + '20.1M tokens · <\$0.01', + ); + expect( + rowLabel({...base, 'cost_known': true, 'cost_usd': 0.005}), + '20.1M tokens · \$0.01', + ); + expect( + rowLabel({...base, 'cost_known': true, 'cost_usd': 0}), + '20.1M tokens', + ); + expect(rowLabel({...base, 'cost_usd': 1.5}), '20.1M tokens'); + // The stage detail still says the cost was not reported. + final facts = detailVisitFacts({ + ..._unreported, + 'usage': {...base, 'cost_known': true}, + }); + expect(facts, contains((label: 'Cost', value: 'Not reported'))); + }); }