Skip to content

Commit 78130a5

Browse files
jay-tauCopilot
andcommitted
refactor(adapter/copilot): trim overlong comments and helper code
Remove redundant explanatory prose from the Copilot session-state implementation and shrink a few equivalent helper expressions without changing behavior. Co-authored-by: Copilot <[email protected]>
1 parent cb5bf12 commit 78130a5

5 files changed

Lines changed: 68 additions & 453 deletions

File tree

‎rust/crates/ccusage-test-support/src/lib.rs‎

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@ pub struct EnvVarGuard {
2323

2424
impl EnvVarGuard {
2525
pub fn set(key: &'static str, value: impl AsRef<OsStr>) -> Self {
26-
let guard = env_lock();
27-
let previous = vec![(key, std::env::var_os(key))];
28-
unsafe { std::env::set_var(key, value) };
29-
Self {
30-
previous,
31-
_guard: guard,
32-
}
26+
Self::set_many(&[(key, Some(value.as_ref()))])
3327
}
3428

3529
pub fn set_many(overrides: &[(&'static str, Option<&OsStr>)]) -> Self {
@@ -60,39 +54,6 @@ impl Drop for EnvVarGuard {
6054
}
6155
}
6256

63-
pub fn acquire_env_test_lock() -> MutexGuard<'static, ()> {
64-
env_lock()
65-
}
66-
67-
pub struct EnvScope {
68-
previous: Vec<(&'static str, Option<OsString>)>,
69-
}
70-
71-
impl EnvScope {
72-
pub fn new(overrides: &[(&'static str, Option<&str>)]) -> Self {
73-
let mut previous = Vec::with_capacity(overrides.len());
74-
for (key, value) in overrides {
75-
previous.push((*key, std::env::var_os(key)));
76-
match value {
77-
Some(value) => unsafe { std::env::set_var(key, value) },
78-
None => unsafe { std::env::remove_var(key) },
79-
}
80-
}
81-
Self { previous }
82-
}
83-
}
84-
85-
impl Drop for EnvScope {
86-
fn drop(&mut self) {
87-
for (key, value) in self.previous.drain(..).rev() {
88-
match value {
89-
Some(value) => unsafe { std::env::set_var(key, value) },
90-
None => unsafe { std::env::remove_var(key) },
91-
}
92-
}
93-
}
94-
}
95-
9657
pub struct EnvVarsGuard {
9758
previous: Vec<(&'static str, Option<OsString>)>,
9859
_guard: MutexGuard<'static, ()>,

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,36 +104,23 @@ shutdown means** — because real-data shapes differ between the two paths:
104104
`treats_resumed_session_shutdowns_as_distinct_entries`.
105105
- **Credit-only zero-token rows** key on
106106
`credit-shutdown:{session_id}:{model}:{naiu}` — **deliberately omitting
107-
`event_id`**. Real session-state data ships credit-only rows in
108-
duplicate pairs with distinct event ids but identical `totalNanoAiu`
109-
values (observed pattern in `1135875b-…` and `76dc438a-…`; the local
110-
200-file corpus shows roughly $2.65 of credits would be double-counted
111-
by event-id-based keying). Two such rows are treated as duplicates of
112-
the same snapshot, not distinct charges. Pinned by
107+
`event_id`**. The loader's `HashMap::insert` collapse is last-wins, so
108+
duplicate AIU snapshots with identical `totalNanoAiu` collapse instead of
109+
double-counting. Pinned by
113110
`auto_mode_dedupes_duplicate_credit_only_rows_within_same_session` and
114111
`credit_only_rows_with_identical_naiu_but_distinct_event_ids_are_treated_as_duplicates`.
115112

116113
**The asymmetry is intentional.** Token rows trust event ids because their
117114
data shape (per-process snapshots) makes coincidence-collision a far more
118115
likely failure mode than missed-duplicate. Credit-only rows distrust event
119-
ids because their data shape (paired snapshots) makes the opposite true.
120-
Both choices accept a small known under/over-count risk in the unlikely
121-
forward-compat case (token counts coincidentally identical across resumes;
122-
or distinct genuine credit charges that happen to share the same naiu) in
123-
exchange for correctness on the observed real-data shape.
116+
ids because paired snapshots make the opposite true.
124117

125118
The aggregate-credit synthetic entry (emitted only when no per-model row
126119
carries a priced billing signal — see `parse_session_state_file`'s
127120
suppression guard) uses the same content-based key family
128121
(`credit-aggregate:{session_id}:{nano_aiu}`) to mirror the credit-only
129122
"paired snapshot" semantics.
130123

131-
If a future Copilot CLI ships genuine non-coincidental duplicate event ids
132-
on credit-only rows, the over-suppression would surface as a visible
133-
under-bill in `credits` JSON output (which is always populated regardless
134-
of mode) — a soft signal that surfaces the schema change rather than
135-
silently doubling the bill.
136-
137124
### Date filter must run before dedup
138125

139126
The credit-only key shape above (which deliberately omits `event_id`) makes

0 commit comments

Comments
 (0)