Skip to content

Commit a9b12c4

Browse files
authored
feat(codex): include archived sessions (#1230)
* feat(codex): include archived sessions Load archived_sessions next to a Codex home sessions directory so archived Codex JSONL files remain visible in focused and unified reports. Keep direct JSONL directory CODEX_HOME entries working by only falling back to the home path when neither sessions nor archived_sessions exists. Document the additional Codex source and cover path discovery with a fixture-backed unit test. * test(codex): cover archived session path cases Add fixture-backed coverage for sessions-only, archived-sessions-only, and direct JSONL directory fallback path discovery. This addresses the CodeRabbit review gap while preserving the archived session discovery behavior added in the prior commit. * test(codex): cover archived path dedupe Clarify Codex home discovery documentation so sessions, archived_sessions, and the direct JSONL fallback match the implementation. Add fixture-backed coverage for duplicate CODEX_HOME entries to pin the seen-set behavior requested in review.
1 parent 9786ab1 commit a9b12c4

3 files changed

Lines changed: 84 additions & 4 deletions

File tree

‎docs/guide/codex/index.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Most users can start with unified reports such as `ccusage daily`. Add the `code
2323

2424
## Data Source
2525

26-
The CLI reads Codex session JSONL files located under `CODEX_HOME` (defaults to `~/.codex`). `CODEX_HOME` can be one directory or a comma-separated list of directories. Entries with a `sessions/` directory are treated as Codex homes; entries without `sessions/` are read directly as JSONL directories, which lets saved `codex exec --json` output live beside normal Codex homes.
26+
The CLI reads Codex session JSONL files located under `CODEX_HOME` (defaults to `~/.codex`). `CODEX_HOME` can be one directory or a comma-separated list of directories. For each entry, ccusage discovers `sessions/` and `archived_sessions/` independently, so an entry with only `archived_sessions/` still contributes archived Codex logs. When neither directory exists, the entry is read directly as a JSONL directory, which lets saved `codex exec --json` output live beside normal Codex homes.
2727

2828
```bash
2929
CODEX_HOME="$HOME/.codex,$HOME/.codex-work,$HOME/codex-exec-logs" ccusage codex daily

‎rust/crates/ccusage/src/adapter/codex/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Data source:
44

55
```text
66
${CODEX_HOME:-~/.codex}/sessions/
7+
${CODEX_HOME:-~/.codex}/archived_sessions/
78
```
89

910
Relevant JSONL event:

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

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@ use std::{env, path::PathBuf};
33
use crate::{cli_error, fast::FxHashSet, home, Result};
44

55
pub(super) fn codex_usage_paths() -> Result<Vec<PathBuf>> {
6+
Ok(codex_usage_paths_from_homes(codex_home_paths()?))
7+
}
8+
9+
fn codex_usage_paths_from_homes(homes: Vec<PathBuf>) -> Vec<PathBuf> {
610
let mut paths = Vec::new();
711
let mut seen = FxHashSet::default();
8-
for path in codex_home_paths()? {
12+
for path in homes {
913
let sessions = path.join("sessions");
14+
let archived_sessions = path.join("archived_sessions");
15+
let mut found_usage_dir = false;
1016
if sessions.is_dir() {
1117
if seen.insert(sessions.clone()) {
1218
paths.push(sessions);
1319
}
14-
} else if seen.insert(path.clone()) {
20+
found_usage_dir = true;
21+
}
22+
if archived_sessions.is_dir() {
23+
if seen.insert(archived_sessions.clone()) {
24+
paths.push(archived_sessions);
25+
}
26+
found_usage_dir = true;
27+
}
28+
if !found_usage_dir && seen.insert(path.clone()) {
1529
paths.push(path);
1630
}
1731
}
18-
Ok(paths)
32+
paths
1933
}
2034

2135
pub(super) fn codex_home_paths() -> Result<Vec<PathBuf>> {
@@ -31,3 +45,68 @@ pub(super) fn codex_home_paths() -> Result<Vec<PathBuf>> {
3145
let home = home::home_dir().ok_or_else(|| cli_error("home directory is not set"))?;
3246
Ok(vec![home.join(".codex")])
3347
}
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
53+
use ccusage_test_support::Fixture;
54+
55+
#[test]
56+
fn includes_archived_sessions_next_to_sessions() {
57+
let fixture = Fixture::new();
58+
let _ = fixture.create_dir_all("codex/sessions");
59+
let _ = fixture.create_dir_all("codex/archived_sessions");
60+
61+
let paths = codex_usage_paths_from_homes(vec![fixture.path("codex")]);
62+
63+
assert_eq!(
64+
paths,
65+
vec![
66+
fixture.path("codex/sessions"),
67+
fixture.path("codex/archived_sessions"),
68+
]
69+
);
70+
}
71+
72+
#[test]
73+
fn uses_sessions_without_missing_archived_sessions_path() {
74+
let fixture = Fixture::new();
75+
let _ = fixture.create_dir_all("codex/sessions");
76+
77+
let paths = codex_usage_paths_from_homes(vec![fixture.path("codex")]);
78+
79+
assert_eq!(paths, vec![fixture.path("codex/sessions")]);
80+
}
81+
82+
#[test]
83+
fn uses_archived_sessions_without_direct_path_fallback() {
84+
let fixture = Fixture::new();
85+
let _ = fixture.create_dir_all("codex/archived_sessions");
86+
87+
let paths = codex_usage_paths_from_homes(vec![fixture.path("codex")]);
88+
89+
assert_eq!(paths, vec![fixture.path("codex/archived_sessions")]);
90+
}
91+
92+
#[test]
93+
fn falls_back_to_direct_path_when_no_session_directories_exist() {
94+
let fixture = Fixture::new();
95+
let home = fixture.create_dir_all("codex");
96+
97+
let paths = codex_usage_paths_from_homes(vec![home.clone()]);
98+
99+
assert_eq!(paths, vec![home]);
100+
}
101+
102+
#[test]
103+
fn deduplicates_usage_paths_across_repeated_homes() {
104+
let fixture = Fixture::new();
105+
let home = fixture.create_dir_all("codex");
106+
let _ = fixture.create_dir_all("codex/sessions");
107+
108+
let paths = codex_usage_paths_from_homes(vec![home.clone(), home]);
109+
110+
assert_eq!(paths, vec![fixture.path("codex/sessions")]);
111+
}
112+
}

0 commit comments

Comments
 (0)