Skip to content

Commit 1f53a3f

Browse files
committed
fix: triage account usage in capability ledger (fixes #1950)
1 parent cc88693 commit 1f53a3f

1 file changed

Lines changed: 101 additions & 26 deletions

File tree

‎crates/jcode-harness-api/src/harness_api_tests/capability_coverage.rs‎

Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ enum Disposition {
2828
ClientInternal,
2929
/// A real gap. Worth exposing, not yet done. Every entry needs a reason
3030
/// that says what a client cannot build without it.
31-
#[allow(dead_code)] // Empty today; the ledger keeps the slot for future gaps.
3231
Gap(&'static str),
3332
}
3433

@@ -38,6 +37,14 @@ use Disposition::{ClientInternal, Covered, Gap};
3837
///
3938
/// Sorted by name so additions produce clean diffs.
4039
const LEDGER: &[(&str, Disposition)] = &[
40+
(
41+
"AccountUsage",
42+
Gap(
43+
"clients cannot read daemon-owned subscription quota ratios, reset times, \
44+
or exhaustion status to warn before a turn is refused, even though \
45+
InvalidateUsage already lets them invalidate that quota cache",
46+
),
47+
),
4148
("BackgroundTool", Covered),
4249
("Cancel", Covered),
4350
("CancelSoftInterrupts", Covered),
@@ -52,8 +59,10 @@ const LEDGER: &[(&str, Disposition)] = &[
5259
("GetState", Covered),
5360
("InputShell", ClientInternal),
5461
("InvalidateAnthropicUsage", Covered),
62+
("InvalidateOpenAiUsage", Covered),
5563
("Message", Covered),
5664
("NotifyAuthChanged", Covered),
65+
("PersistSession", ClientInternal),
5766
("RefreshModels", ClientInternal),
5867
("Reload", ClientInternal),
5968
("RenameSession", Covered),
@@ -62,16 +71,15 @@ const LEDGER: &[(&str, Disposition)] = &[
6271
("Rewind", Covered),
6372
("RewindUndo", Covered),
6473
("RunSubagent", ClientInternal),
65-
("SetCompactionMode", ClientInternal),
6674
("SetAgentModel", Covered),
75+
("SetCompactionMode", ClientInternal),
6776
("SetFeature", ClientInternal),
6877
("SetModel", Covered),
6978
("SetPremiumMode", ClientInternal),
7079
("SetReasoningEffort", Covered),
7180
("SetRoute", ClientInternal),
7281
("SetServiceTier", ClientInternal),
7382
("SetSessionSaved", Covered),
74-
("PersistSession", ClientInternal),
7583
("SetSubagentModel", ClientInternal),
7684
("SetTransport", ClientInternal),
7785
("SoftInterrupt", Covered),
@@ -80,21 +88,34 @@ const LEDGER: &[(&str, Disposition)] = &[
8088
("Subscribe", Covered),
8189
("SwitchAnthropicAccount", ClientInternal),
8290
("SwitchOpenAiAccount", ClientInternal),
83-
("InvalidateOpenAiUsage", Covered),
84-
("InvalidateAnthropicUsage", Covered),
8591
("Transcript", ClientInternal),
8692
("Transfer", ClientInternal),
8793
("TriggerMemoryExtraction", ClientInternal),
8894
];
8995

9096
/// Requests the reference clients (TUI) send to the daemon.
91-
fn reference_client_requests() -> BTreeSet<String> {
92-
let mut found = BTreeSet::new();
93-
{
94-
let dir = "../jcode-tui/src";
95-
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(dir);
96-
collect_requests(&root, &mut found);
97+
fn reference_client_requests() -> Option<BTreeSet<String>> {
98+
let manifest = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
99+
// Standalone vendored/packaged copies may omit the reference client. In
100+
// the repository, the workspace manifest marks that omission as an error.
101+
let in_workspace = manifest.join("../../Cargo.toml").is_file();
102+
reference_client_requests_at(&manifest.join("../jcode-tui/src"), in_workspace)
103+
.expect("failed to scrape reference client requests")
104+
}
105+
106+
fn reference_client_requests_at(
107+
root: &std::path::Path,
108+
in_workspace: bool,
109+
) -> std::io::Result<Option<BTreeSet<String>>> {
110+
match std::fs::metadata(root) {
111+
Err(error) if error.kind() == std::io::ErrorKind::NotFound && !in_workspace => {
112+
return Ok(None);
113+
}
114+
Err(error) => return Err(error),
115+
Ok(_) => {}
97116
}
117+
let mut found = BTreeSet::new();
118+
collect_requests(root, &mut found)?;
98119
// covered by construction and would otherwise pollute the diff.
99120
for api_only in [
100121
"CreateSession",
@@ -105,21 +126,22 @@ fn reference_client_requests() -> BTreeSet<String> {
105126
] {
106127
found.remove(api_only);
107128
}
108-
found
129+
if found.is_empty() {
130+
return Err(std::io::Error::new(
131+
std::io::ErrorKind::InvalidData,
132+
format!("no daemon requests scraped from {}", root.display()),
133+
));
134+
}
135+
Ok(Some(found))
109136
}
110137

111-
fn collect_requests(dir: &std::path::Path, found: &mut BTreeSet<String>) {
112-
let Ok(entries) = std::fs::read_dir(dir) else {
113-
return;
114-
};
115-
for entry in entries.flatten() {
116-
let path = entry.path();
138+
fn collect_requests(dir: &std::path::Path, found: &mut BTreeSet<String>) -> std::io::Result<()> {
139+
for entry in std::fs::read_dir(dir)? {
140+
let path = entry?.path();
117141
if path.is_dir() {
118-
collect_requests(&path, found);
142+
collect_requests(&path, found)?;
119143
} else if path.extension().is_some_and(|ext| ext == "rs") {
120-
let Ok(source) = std::fs::read_to_string(&path) else {
121-
continue;
122-
};
144+
let source = std::fs::read_to_string(&path)?;
123145
for (index, _) in source.match_indices("Request::") {
124146
// `Request::` also matches unrelated local enums whose names
125147
// merely end in "Request" (the TUI's own
@@ -146,6 +168,60 @@ fn collect_requests(dir: &std::path::Path, found: &mut BTreeSet<String>) {
146168
}
147169
}
148170
}
171+
Ok(())
172+
}
173+
174+
#[test]
175+
fn the_ledger_is_sorted_unique_and_gaps_have_reasons() {
176+
for pair in LEDGER.windows(2) {
177+
assert!(
178+
pair[0].0 < pair[1].0,
179+
"ledger must be strictly sorted with no duplicates: {pair:?}"
180+
);
181+
}
182+
for (name, disposition) in LEDGER {
183+
if let Gap(reason) = disposition {
184+
assert!(!reason.trim().is_empty(), "{name} needs a gap reason");
185+
}
186+
}
187+
}
188+
189+
#[test]
190+
fn reference_scrape_fails_closed_except_for_absent_vendored_sources() {
191+
let root = std::env::temp_dir().join(format!(
192+
"jcode-capability-coverage-{}-{}",
193+
std::process::id(),
194+
std::time::SystemTime::now()
195+
.duration_since(std::time::UNIX_EPOCH)
196+
.unwrap()
197+
.as_nanos()
198+
));
199+
std::fs::create_dir(&root).unwrap();
200+
// Ensure fixtures are removed even when an assertion fails.
201+
struct Fixture(std::path::PathBuf);
202+
impl Drop for Fixture {
203+
fn drop(&mut self) {
204+
let _ = std::fs::remove_dir_all(&self.0);
205+
}
206+
}
207+
let _fixture = Fixture(root.clone());
208+
let source = root.join("src");
209+
assert!(reference_client_requests_at(&source, true).is_err());
210+
assert_eq!(reference_client_requests_at(&source, false).unwrap(), None);
211+
std::fs::create_dir(&source).unwrap();
212+
for in_workspace in [false, true] {
213+
assert!(reference_client_requests_at(&source, in_workspace).is_err());
214+
}
215+
let file = source.join("client.rs");
216+
std::fs::write(&file, "Request::CreateSession").unwrap();
217+
assert!(reference_client_requests_at(&source, true).is_err());
218+
std::fs::write(&file, "Request::AccountUsage").unwrap();
219+
assert_eq!(
220+
reference_client_requests_at(&source, true).unwrap(),
221+
Some(BTreeSet::from(["AccountUsage".to_owned()]))
222+
);
223+
std::fs::write(&file, [0xff]).unwrap();
224+
assert!(reference_client_requests_at(&source, true).is_err());
149225
}
150226

151227
/// Every capability the reference clients use must have a reviewed entry.
@@ -154,11 +230,10 @@ fn collect_requests(dir: &std::path::Path, found: &mut BTreeSet<String>) {
154230
/// that nothing is missing *by accident*.
155231
#[test]
156232
fn every_reference_client_capability_is_triaged() {
157-
let used = reference_client_requests();
158-
if used.is_empty() {
159-
// Vendored/packaged builds without the sibling crates: nothing to do.
233+
let Some(used) = reference_client_requests() else {
234+
// Only standalone vendored/packaged builds may omit sibling sources.
160235
return;
161-
}
236+
};
162237
let ledger: BTreeSet<&str> = LEDGER.iter().map(|(name, _)| *name).collect();
163238

164239
let untriaged: Vec<&String> = used

0 commit comments

Comments
 (0)