Skip to content

Commit aad0c6e

Browse files
unhappychoiceclaude
andcommitted
fix(fetcher/codex): address coderabbit review findings
- `read_jsonl_files` now also filters on the `rollout-` filename prefix, so a future stray file under `~/.codex/sessions/YYYY/MM/DD/` can't quietly shadow the lex-sorted "newest rollup" lookup `codex_subscription` does. - Module doc on `src/fetcher/codex/mod.rs` previously claimed "no network" for both fetchers — corrected: `codex_usage` does make one host-fixed HTTP GET for the LLM pricing snapshot. `codex_subscription` stays local-only. - `llm_pricing::price_for` prefix lookup now lowercases the key it compares against. `data/llm_pricing.json` ships lowercase keys today, but the lookup shouldn't silently miss if a future LiteLLM sync ships a mixed-case id. - Workflow's `actions/checkout` step gains `persist-credentials: false` — the sync step uses `peter-evans/create-pull-request`'s own token, so checkout doesn't need to leave credentials in the workspace. - Documented why `fetch_reads_jsonl_from_env_override_root` holds `TEST_ENV_LOCK` across an `.await`; the future doesn't re-take the lock, so the `#[allow(clippy::await_holding_lock)]` is safe. Skipped from the review: - SHA-pinning Actions: existing workflows in the repo use floating `@v6` / `@v7` tags; matching the precedent. Worth a separate hardening pass. - Returning `embedded_floor() -> &'static PriceMap`: it's only hit on the HTTP-failure cold path, so the clone cost is negligible vs the API ergonomics win of returning an owned map. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 6d09272 commit aad0c6e

5 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎.github/workflows/sync-llm-pricing.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ jobs:
2020
runs-on: ubuntu-latest
2121
steps:
2222
- uses: actions/checkout@v6
23+
with:
24+
# `peter-evans/create-pull-request` supplies its own token below, so
25+
# checkout doesn't need to persist credentials in the workspace.
26+
persist-credentials: false
2327

2428
- name: Fetch upstream LiteLLM table
2529
run: |

‎src/fetcher/claude/code_usage.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,10 @@ mod tests {
950950
}
951951

952952
#[tokio::test]
953+
// TEST_ENV_LOCK serialises tests that mutate process env (`CLAUDE_CONFIG_DIR` here). The
954+
// lock is held across `.fetch().await` deliberately so a sibling test can't swap the env
955+
// out mid-flight; the fetcher only reads env at the synchronous entry to the call, so
956+
// there's no path where the awaiting future re-takes the same lock and deadlocks.
953957
#[allow(clippy::await_holding_lock)]
954958
async fn fetch_reads_jsonl_from_env_override_root() {
955959
let _lock = crate::paths::TEST_ENV_LOCK

‎src/fetcher/codex/common.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,16 @@ fn read_jsonl_files(dir: &PathBuf) -> Vec<PathBuf> {
7777
.into_iter()
7878
.flat_map(|it| it.flatten())
7979
.map(|e| e.path())
80-
.filter(|p| p.extension().is_some_and(|x| x == "jsonl"))
80+
// Only `rollout-*.jsonl` — Codex writes session rollups with this prefix; other
81+
// *.jsonl files that may appear under `~/.codex/sessions/` (history backups, future
82+
// file kinds) are unrelated and would confuse the lex-sorted "newest session" lookup
83+
// codex_subscription does.
84+
.filter(|p| {
85+
p.extension().is_some_and(|x| x == "jsonl")
86+
&& p.file_name()
87+
.and_then(|n| n.to_str())
88+
.is_some_and(|n| n.starts_with("rollout-"))
89+
})
8190
.collect()
8291
}
8392

‎src/fetcher/codex/mod.rs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
//! Codex (OpenAI Codex CLI) fetchers.
22
//!
33
//! - [`usage::CodexUsage`] aggregates the local `rollout-*.jsonl` files Codex CLI writes for
4-
//! every session, with zero configuration and no network.
4+
//! every session, then HTTP-GETs the shared LLM pricing snapshot from splashboard's own
5+
//! GitHub repo (see [`crate::fetcher::llm_pricing`]) to convert tokens into a USD cost.
56
//! - [`subscription::CodexSubscription`] extracts rate-limit utilisation (5h / 7d windows +
6-
//! plan type) from the most recent session's last `token_count` event.
7+
//! plan type) from the most recent session's last `token_count` event. No network — the
8+
//! payload Codex CLI already wrote is what we read.
79
//!
8-
//! Both are `Safety::Safe` — all reads are rooted at `~/.codex/sessions/`. No network, no
9-
//! token leaves the host (the rate-limit data is already cached in the JSONL by Codex CLI;
10-
//! re-fetching it would mean making an inference call, which costs money).
10+
//! Both are `Safety::Safe`. `codex_subscription` is local-only. `codex_usage` makes one
11+
//! host-fixed HTTP GET (the URL never accepts user input) — same model as `nasa_apod` or any
12+
//! other Safe + outbound fetcher: config can't redirect the traffic, no token leaves the
13+
//! host.
1114
1215
mod common;
1316
mod subscription;

‎src/fetcher/llm_pricing.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ pub fn price_for<'a>(prices: &'a PriceMap, model: &str) -> Option<&'a Price> {
8787
if let Some(p) = prices.get(&lower) {
8888
return Some(p);
8989
}
90+
// Prefix match against lowercased keys too — `data/llm_pricing.json` keys happen to be
91+
// lowercase today but the lookup shouldn't quietly miss if a future LiteLLM sync ships a
92+
// mixed-case identifier.
9093
prices
9194
.iter()
92-
.filter(|(k, _)| lower.starts_with(k.as_str()) && !k.is_empty())
95+
.filter(|(k, _)| !k.is_empty() && lower.starts_with(&k.to_lowercase()))
9396
.max_by_key(|(k, _)| k.len())
9497
.map(|(_, p)| p)
9598
}

0 commit comments

Comments
 (0)