Skip to content

Commit 695a46e

Browse files
committed
fix(codex): handle review edge cases without rereads
Parse malformed headless Codex usage rows through a fallback JSON value path so unexpected non-critical timestamp or model types do not drop valid usage. Keep the common typed deserialization path fast and narrow whitespace type scanning so large metadata lines are not rescanned unnecessarily. Propagate SimpleTable stdout write errors through CLI table renderers instead of panicking, which preserves broken-pipe behavior for commands that pipe output. Use the sharded global Codex dedupe path for parallel table aggregation so duplicate events are handled in one file pass instead of detecting duplicates after local aggregation and rereading every session file.
1 parent e183f1d commit 695a46e

20 files changed

Lines changed: 277 additions & 109 deletions

File tree

‎rust/crates/ccusage-terminal/src/lib.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ impl SimpleTable {
7878
self.headers.len()
7979
}
8080

81-
pub fn print(&self) {
81+
pub fn print(&self) -> io::Result<()> {
8282
let stdout = io::stdout();
8383
let mut stdout = stdout.lock();
8484
for line in self.render_lines() {
85-
writeln!(stdout, "{line}").expect("failed to write table line");
85+
writeln!(stdout, "{line}")?;
8686
}
87+
Ok(())
8788
}
8889

8990
fn render_lines(&self) -> Vec<String> {

‎rust/crates/ccusage/src/adapter/all.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ pub(crate) fn run(args: AgentCommandArgs) -> Result<()> {
6464
if wants_json(&shared) {
6565
return print_json_or_jq(report_json(&result.rows, args.kind), shared.jq.as_deref());
6666
}
67-
print_table(&result.rows, args.kind, &shared, &result.detected_agents);
68-
Ok(())
67+
print_table(&result.rows, args.kind, &shared, &result.detected_agents)
6968
}
7069

7170
fn load_rows(kind: AgentReportKind, shared: &SharedArgs) -> Result<AllLoadResult> {
@@ -851,11 +850,11 @@ fn print_table(
851850
kind: AgentReportKind,
852851
shared: &SharedArgs,
853852
detected_agents: &[&'static str],
854-
) {
853+
) -> Result<()> {
855854
print_box_title(&all_report_title(kind, rows, detected_agents), shared);
856855
if rows.is_empty() {
857856
eprintln!("No usage data found.");
858-
return;
857+
return Ok(());
859858
}
860859
let terminal_width = crate::terminal_width();
861860
let compact = shared.compact || terminal_width < crate::USAGE_COMPACT_WIDTH_THRESHOLD;
@@ -949,11 +948,12 @@ fn print_table(
949948
),
950949
]);
951950
}
952-
table.print();
951+
table.print()?;
953952
if compact {
954953
eprintln!("\nRunning in Compact Mode");
955954
eprintln!("Expand terminal width to see cache metrics and total tokens");
956955
}
956+
Ok(())
957957
}
958958

959959
fn all_report_title(

‎rust/crates/ccusage/src/adapter/amp.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ pub(crate) fn run(args: AgentCommandArgs) -> Result<()> {
3030
if wants_json(&shared) {
3131
return print_json_or_jq(report_from_rows(&rows, args.kind), shared.jq.as_deref());
3232
}
33-
print_table(args.kind, &rows, &shared);
34-
Ok(())
33+
print_table(args.kind, &rows, &shared)
3534
}
3635

3736
pub(crate) fn report_from_rows(rows: &[crate::UsageSummary], kind: AgentReportKind) -> Value {
@@ -285,19 +284,19 @@ pub(crate) fn print_table(
285284
kind: AgentReportKind,
286285
rows: &[crate::UsageSummary],
287286
shared: &crate::cli::SharedArgs,
288-
) {
289-
print_table_for_agent("Amp", kind, rows, shared);
287+
) -> Result<()> {
288+
print_table_for_agent("Amp", kind, rows, shared)
290289
}
291290

292291
pub(crate) fn print_table_for_agent(
293292
agent_name: &str,
294293
kind: AgentReportKind,
295294
rows: &[crate::UsageSummary],
296295
shared: &crate::cli::SharedArgs,
297-
) {
296+
) -> Result<()> {
298297
if rows.is_empty() {
299298
eprintln!("No {agent_name} usage data found.");
300-
return;
299+
return Ok(());
301300
}
302301
let terminal_width = crate::terminal_width();
303302
let compact = shared.compact || terminal_width < crate::USAGE_COMPACT_WIDTH_THRESHOLD;
@@ -458,7 +457,8 @@ pub(crate) fn print_table_for_agent(
458457
),
459458
]);
460459
}
461-
table.print();
460+
table.print()?;
461+
Ok(())
462462
}
463463

464464
fn agent_report_label(kind: AgentReportKind) -> &'static str {

‎rust/crates/ccusage/src/adapter/codebuff.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub(crate) fn run(args: AgentCommandArgs) -> Result<()> {
5959
if wants_json(&shared) {
6060
return print_json_or_jq(report_from_rows(&rows, args.kind), shared.jq.as_deref());
6161
}
62-
crate::adapter::amp::print_table_for_agent("Codebuff", args.kind, &rows, &shared);
62+
crate::adapter::amp::print_table_for_agent("Codebuff", args.kind, &rows, &shared)?;
6363
Ok(())
6464
}
6565

‎rust/crates/ccusage/src/adapter/codex.rs‎

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ pub(crate) fn run(args: AgentCommandArgs) -> Result<()> {
3737
let output = report_from_groups(&groups, args.kind, &pricing, speed);
3838
return print_json_or_jq(output, shared.jq.as_deref());
3939
}
40-
print_table_from_groups(&groups, args.kind, &pricing, speed, &shared);
41-
Ok(())
40+
print_table_from_groups(&groups, args.kind, &pricing, speed, &shared)
4241
}
4342

4443
#[cfg(test)]
@@ -139,7 +138,8 @@ fn load_groups_from_directory(
139138
if shared.single_thread {
140139
return aggregate_files_local(sessions_dir, &files, shared, kind);
141140
}
142-
aggregate_files_parallel_local(sessions_dir, &files, shared, kind)
141+
let seen = create_dedupe_shards();
142+
aggregate_files_parallel(sessions_dir, &files, shared, kind, &seen)
143143
}
144144

145145
fn load_groups_from_directory_with_dedupe(
@@ -277,52 +277,6 @@ fn aggregate_files_local_with_seen(
277277
Ok(aggregation)
278278
}
279279

280-
fn aggregate_files_parallel_local(
281-
sessions_dir: &Path,
282-
files: &[PathBuf],
283-
shared: &SharedArgs,
284-
kind: AgentReportKind,
285-
) -> Result<BTreeMap<String, CodexGroup>> {
286-
let worker_count = thread::available_parallelism()
287-
.map(usize::from)
288-
.unwrap_or(1)
289-
.saturating_mul(3)
290-
.min(files.len());
291-
if worker_count <= 1 {
292-
return aggregate_files_local(sessions_dir, files, shared, kind);
293-
}
294-
295-
let chunks = crate::chunk_file_indexes_by_size(files, worker_count);
296-
thread::scope(|scope| {
297-
let mut handles = Vec::with_capacity(chunks.len());
298-
for chunk in chunks {
299-
handles.push(scope.spawn(move || {
300-
let mut chunk_files = Vec::with_capacity(chunk.len());
301-
for index in chunk {
302-
chunk_files.push(files[index].clone());
303-
}
304-
aggregate_files_local_with_seen(sessions_dir, &chunk_files, shared, kind)
305-
}));
306-
}
307-
308-
let mut groups = BTreeMap::new();
309-
let mut seen = FxHashSet::default();
310-
for handle in handles {
311-
let partial = handle
312-
.join()
313-
.map_err(|_| crate::cli_error("codex worker panicked"))??;
314-
for key in partial.seen {
315-
if !seen.insert(key) {
316-
let global_seen = create_dedupe_shards();
317-
return aggregate_files(sessions_dir, files, shared, kind, &global_seen);
318-
}
319-
}
320-
merge_groups(&mut groups, partial.groups);
321-
}
322-
Ok(groups)
323-
})
324-
}
325-
326280
fn aggregate_file_local(
327281
sessions_dir: &Path,
328282
file: &Path,
@@ -707,10 +661,10 @@ fn print_table_from_groups(
707661
pricing: &PricingMap,
708662
speed: CodexSpeed,
709663
shared: &SharedArgs,
710-
) {
664+
) -> Result<()> {
711665
if groups.is_empty() {
712666
eprintln!("No Codex usage data found.");
713-
return;
667+
return Ok(());
714668
}
715669
let first_column = match kind {
716670
AgentReportKind::Daily => "Date",
@@ -792,7 +746,8 @@ fn print_table_from_groups(
792746
color(shared, format_number(total_tokens), Color::Yellow),
793747
color(shared, format_currency(total_cost), Color::Yellow),
794748
]);
795-
table.print();
749+
table.print()?;
750+
Ok(())
796751
}
797752

798753
#[cfg(test)]

‎rust/crates/ccusage/src/adapter/copilot.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(crate) fn run(args: AgentCommandArgs) -> Result<()> {
8181
&shared,
8282
false,
8383
None,
84-
);
84+
)?;
8585
Ok(())
8686
}
8787

‎rust/crates/ccusage/src/adapter/droid.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub(crate) fn run(args: AgentCommandArgs) -> Result<()> {
6161
&shared,
6262
false,
6363
None,
64-
);
64+
)?;
6565
Ok(())
6666
}
6767

‎rust/crates/ccusage/src/adapter/gemini.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub(crate) fn run(args: AgentCommandArgs) -> Result<()> {
6565
&shared,
6666
false,
6767
None,
68-
);
68+
)?;
6969
Ok(())
7070
}
7171

‎rust/crates/ccusage/src/adapter/goose.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub(crate) fn run(args: AgentCommandArgs) -> Result<()> {
4545
if wants_json(&shared) {
4646
return print_json_or_jq(report_from_rows(&rows, args.kind), shared.jq.as_deref());
4747
}
48-
crate::adapter::amp::print_table(args.kind, &rows, &shared);
48+
crate::adapter::amp::print_table(args.kind, &rows, &shared)?;
4949
Ok(())
5050
}
5151

‎rust/crates/ccusage/src/adapter/hermes.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub(crate) fn run(args: AgentCommandArgs) -> Result<()> {
5252
&shared,
5353
false,
5454
None,
55-
);
55+
)?;
5656
Ok(())
5757
}
5858

0 commit comments

Comments
 (0)