Token usage: keep cache/reasoning split, count only recorded steps, cost only when positive; captures fixture, docs and notes
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
|
||||
/// Opt-in (`?usage=1`) size and reported token usage for the capture
|
||||
/// fixture's runs, shaped exactly as the server sends them: a run-level
|
||||
/// `usage` total (list and detail) and a per-step `usage`. Without the
|
||||
/// parameter the fixture keeps the old-server shape with neither.
|
||||
bool probeUsage = false;
|
||||
|
||||
/// Per-step usage of run 7, by recorded sequence. The build visit is mostly
|
||||
/// cache reads, as a caching harness reports; plan 1 predates the cache split.
|
||||
const _stepUsage = <int, JsonMap>{
|
||||
5: {
|
||||
'tokens_total': 1200000,
|
||||
'tokens_known': true,
|
||||
'split_known': true,
|
||||
'tokens_in': 300000,
|
||||
'tokens_out': 60000,
|
||||
'tokens_reasoning': 40000,
|
||||
'cache_read': 780000,
|
||||
'cache_write': 20000,
|
||||
'cache_known': true,
|
||||
'cost_known': true,
|
||||
},
|
||||
2: {
|
||||
'tokens_total': 20100000,
|
||||
'tokens_known': true,
|
||||
'split_known': true,
|
||||
'tokens_in': 500000,
|
||||
'tokens_out': 80000,
|
||||
'tokens_reasoning': 20000,
|
||||
'cache_read': 19480000,
|
||||
'cache_write': 20000,
|
||||
'cache_known': true,
|
||||
'cost_usd': 1.42,
|
||||
'cost_known': true,
|
||||
},
|
||||
1: {
|
||||
'tokens_total': 1100000,
|
||||
'tokens_known': true,
|
||||
'split_known': true,
|
||||
'tokens_in': 1000000,
|
||||
'tokens_out': 100000,
|
||||
'cache_known': true,
|
||||
'cache_read': 0,
|
||||
'cost_known': true,
|
||||
},
|
||||
};
|
||||
|
||||
/// Run 7's total over [_stepUsage], as the server's aggregate sums it.
|
||||
const _run7Usage = <String, dynamic>{
|
||||
'stages': 3,
|
||||
'tokens_total': 22400000,
|
||||
'tokens_known_stages': 3,
|
||||
'split_known': true,
|
||||
'tokens_in': 1800000,
|
||||
'tokens_out': 240000,
|
||||
'tokens_reasoning': 60000,
|
||||
'cache_read': 20260000,
|
||||
'cache_write': 40000,
|
||||
'cache_known': true,
|
||||
'cache_known_stages': 3,
|
||||
'cost_usd': 1.42,
|
||||
'cost_known_stages': 3,
|
||||
};
|
||||
|
||||
/// Run 6: one of its two stages reported no token count, so its total is a
|
||||
/// lower bound; no stage reported a positive cost.
|
||||
const _run6Usage = <String, dynamic>{
|
||||
'stages': 2,
|
||||
'tokens_total': 9100000,
|
||||
'tokens_known_stages': 1,
|
||||
'split_known': false,
|
||||
'split_unknown_stages': 1,
|
||||
'cost_known_stages': 1,
|
||||
'cost_unknown_stages': 1,
|
||||
};
|
||||
|
||||
/// Adds the run's size and usage to a list row or detail when enabled.
|
||||
JsonMap withProbeUsage(JsonMap run) {
|
||||
if (!probeUsage) return run;
|
||||
return switch (run['id']) {
|
||||
7 => {...run, 'size_bytes': 340 * 1024 * 1024, 'usage': _run7Usage},
|
||||
6 => {...run, 'size_bytes': 4404019, 'usage': _run6Usage},
|
||||
_ => run,
|
||||
};
|
||||
}
|
||||
|
||||
/// Adds the recorded per-step usage to run 7's steps when enabled.
|
||||
JsonMap withProbeStepUsage(JsonMap step) {
|
||||
final usage = _stepUsage[step['sequence']];
|
||||
return probeUsage && usage != null ? {...step, 'usage': usage} : step;
|
||||
}
|
||||
@@ -31,6 +31,7 @@ import 'perf/chat_search_fixture.dart';
|
||||
import 'perf/chat_scroll_fixture.dart';
|
||||
import 'perf/perf_fixture.dart';
|
||||
import 'probe_folder_fixture.dart';
|
||||
import 'probe_usage_fixture.dart';
|
||||
|
||||
/// The run every run-scoped hit names. Its recorded sequences are deliberately
|
||||
/// not its row indexes (plan 5, build 2, plan 1), exactly like the unit
|
||||
@@ -414,12 +415,28 @@ JsonMap? probeReply(JsonMap request) {
|
||||
'time': '2026-10-20T09:00:00Z',
|
||||
};
|
||||
case 'list_runs':
|
||||
return {'id': id, 'type': 'runs', 'runs': probeRuns(), 'usage': null};
|
||||
return {
|
||||
'id': id,
|
||||
'type': 'runs',
|
||||
'runs': probeRuns().map(withProbeUsage).toList(),
|
||||
'usage': null,
|
||||
};
|
||||
case 'get_run':
|
||||
if (request['run_id'] != 7) {
|
||||
return error('Fixture run ${request['run_id']} is unavailable');
|
||||
}
|
||||
return {'id': id, 'type': 'run', 'run_detail': probeRunDetail()};
|
||||
final detail = withProbeUsage(probeRunDetail());
|
||||
return {
|
||||
'id': id,
|
||||
'type': 'run',
|
||||
'run_detail': {
|
||||
...detail,
|
||||
'steps': [
|
||||
for (final step in detail['steps'] as List)
|
||||
withProbeStepUsage(step as JsonMap),
|
||||
],
|
||||
},
|
||||
};
|
||||
case 'list_pipelines':
|
||||
return {'id': id, 'type': 'pipelines', 'pipelines': probePipelines()};
|
||||
case 'get_pipeline':
|
||||
@@ -606,6 +623,7 @@ void main() {
|
||||
configurePerfFixture(params);
|
||||
_fillerRunCount = int.tryParse(params['runs'] ?? '') ?? 0;
|
||||
_fillerStepCount = int.tryParse(params['steps'] ?? '') ?? 0;
|
||||
probeUsage = params['usage'] == '1';
|
||||
final cupertino = params['platform'] == 'cupertino';
|
||||
final route = params['route'];
|
||||
final opensRoute = route != null && route.startsWith('/');
|
||||
|
||||
Reference in New Issue
Block a user