use super::*;
use crate::rate_limit_signatures::QUOTA_SIGNATURES;
fn stream(output: &str, agent: AgentKind) -> String {
provider_attributable(output, agent, Channel::CliStream).all()
}
fn stream_diagnostic(output: &str, agent: AgentKind) -> String {
provider_attributable(output, agent, Channel::CliStream).cli_diagnostic
}
fn the_incident_line() -> String {
let quoted = "assert_rate_limit(r#\"{\"type\":\"error\",\"message\":\"quota exceeded for \
this workspace\"}\"#, true);\n====\ncommit 7881e2d";
assistant_envelope(quoted)
}
fn assistant_envelope(text: &str) -> String {
serde_json::json!({
"type": "assistant",
"message": {"content": [{"type": "text", "text": text}]}
})
.to_string()
}
#[test]
fn the_assistant_envelope_that_forged_a_cursor_hold_survives_nothing() {
let kept = stream(&the_incident_line(), AgentKind::Cursor);
assert!(kept.is_empty(), "model-authored text must not survive: {kept}");
}
#[test]
fn no_signature_in_the_table_can_be_forged_from_model_text() {
for signature in QUOTA_SIGNATURES {
let quoted = format!(
"In `src/rate_limit_signatures.rs` the needle is \"{}\", and the CLI prints \
it verbatim when the pool is spent.",
signature.needle
);
let kept = stream(&assistant_envelope("ed), signature.agent);
assert!(
kept.is_empty(),
"needle {:?} survived the split for {:?}: {kept}",
signature.needle,
signature.agent
);
}
}
#[test]
fn copilots_session_error_is_read_as_the_cli_speaking() {
let nested = r#"{"type":"session.error","data":{"message":"{\"error\":{\"message\":\"You have exceeded your monthly quota\",\"code\":\"quota_exceeded\"}}","requestFingerprint":{"messageCount":2}}}"#;
let live = r#"{"type":"session.error","data":{"errorType":"quota","message":"You have exceeded your monthly quota (Request ID: EC26:161163:156CE45:19CFB24:6A75A3A0)","statusCode":402,"errorCode":"quota_exceeded"}}"#;
for event in [nested, live] {
let kept = stream_diagnostic(event, AgentKind::Copilot);
assert!(
kept.to_lowercase().contains("exceeded your monthly quota"),
"copilot's live session.error must survive: {kept}"
);
}
}
#[test]
fn a_cli_error_envelope_survives_as_the_providers_own_sentence() {
let event = r#"{"type":"error","errorCode":"quota_exceeded","message":"You have exceeded your monthly quota","requestFingerprint":{"messageCount":2}}"#;
let kept = stream_diagnostic(event, AgentKind::Copilot);
assert!(
kept.lines().any(|line| line == "You have exceeded your monthly quota"),
"provider sentence must survive whole and unescaped: {kept}"
);
let status = stream_diagnostic(r#"{"type":"error","status":429}"#, AgentKind::Droid);
assert!(status.contains("429"), "numeric 429 in a CLI error must survive: {status}");
}
#[test]
fn a_failing_tool_call_is_not_a_provider_refusal() {
let pattern = "you're out of usage|out of usage|ActionRequired";
for envelope in [
format!(
r#"{{"type":"tool_call","subtype":"completed","tool_call":{{"grepToolCall":{{"args":{{"pattern":"{pattern}"}}}}}}}}"#
),
format!(r#"{{"type":"tool.execution_complete","error":"grep failed: {pattern}"}}"#),
format!(
r#"{{"type":"user","tool_use_id":"toolu_1","is_error":true,"content":"{pattern}: no matches"}}"#
),
] {
let kept = stream(&envelope, AgentKind::Cursor);
assert!(
kept.is_empty(),
"a tool envelope is the model's request, not the provider's answer: {kept}"
);
}
}
#[test]
fn a_nested_opencode_billing_refusal_survives() {
let event = r#"{"type":"error","error":{"name":"APIError","data":{"message":"Insufficient balance. Manage your billing here: https://x"}}}"#;
let kept = stream(event, AgentKind::OpenCode);
assert!(kept.contains("Insufficient balance"), "got {kept}");
}
#[test]
fn grok_is_split_by_its_envelope_not_by_its_words() {
let refusal = r#"{"type":"error","message":"API error (status 402 Payment Required): Grok Build usage balance exhausted"}"#;
assert!(
stream(refusal, AgentKind::Grok).contains("usage balance exhausted"),
"grok's error envelope is provider testimony"
);
let report = r#"{"text":"The needle is \"usage balance exhausted\" per rate_limit_signatures.rs","stopReason":"end_turn"}"#;
assert!(
stream(report, AgentKind::Grok).is_empty(),
"grok's answer text is the model talking"
);
}
#[test]
fn the_terminal_result_slot_is_open_for_qwen_and_shut_for_everyone_else() {
let envelope = crate::quota_channel::test_qwen_result_envelope("Quota exhausted: Your token-plan 5-hour quota has been exhausted.");
assert!(
stream(&envelope, AgentKind::Qwen).contains("Quota exhausted:"),
"qwen's measured refusal channel must stay readable"
);
assert!(
stream(&envelope, AgentKind::Cursor).is_empty(),
"no other agent has been measured refusing here"
);
}
#[test]
fn qwens_result_slot_is_never_strong_enough_for_a_bare_status_token() {
let envelope = crate::quota_channel::test_qwen_result_envelope("The upstream RPC returned 429 twice during the run.");
assert!(stream(&envelope, AgentKind::Qwen).contains("429"));
assert!(
stream_diagnostic(&envelope, AgentKind::Qwen).is_empty(),
"the model's own slot is never CLI-diagnostic evidence"
);
}
#[test]
fn plain_text_lines_stay_admissible() {
let refusal = "Individual quota reached. Please upgrade your subscription. Resets in 59m21s.";
assert_eq!(stream(refusal, AgentKind::Antigravity).trim(), refusal);
assert!(
stream_diagnostic(refusal, AgentKind::Antigravity).is_empty(),
"a line with no envelope around it is never CLI-diagnostic evidence"
);
}
#[test]
fn terminal_escapes_do_not_smuggle_model_text_past_the_split() {
let line = format!("\u{1b}[2K\u{1b}[0G{}", assistant_envelope("insufficient balance"));
assert!(
stream(&line, AgentKind::OpenCode).is_empty(),
"an escaped assistant envelope is still an assistant envelope"
);
}
#[test]
fn a_result_envelope_flagged_is_error_is_read_as_a_diagnostic() {
let envelope = r#"{"type":"result","is_error":true,"result":"You have hit your usage limit. try again at Mar 21st, 2099 2:27 PM."}"#;
assert!(
stream_diagnostic(envelope, AgentKind::Codex).contains("hit your usage limit"),
"an envelope the CLI marked failed is the CLI speaking"
);
}
#[test]
fn no_adapter_writes_a_marker_from_a_shape_the_model_can_author() {
let mut cases: Vec<(AgentKind, String)> = Vec::new();
for signature in QUOTA_SIGNATURES {
for line in model_authored_shapes(signature.needle) {
cases.push((signature.agent, line));
}
}
for agent in adapters_under_test() {
for text in [
"429 Too Many Requests while calling the RPC provider",
"402 Payment Required — the report is about a different service",
"match_quota_signature reads rate_limit_signatures.rs; see rate_limit.rs",
"We must respect the rate limit on the Base sequencer feed",
] {
for line in model_authored_shapes(text) {
cases.push((agent, line));
}
}
}
for (agent_kind, line) in cases {
let temp = tempfile::tempdir().expect("temp dir");
std::fs::create_dir_all(temp.path().join(".aid")).expect("aid dir");
let _aid_home = crate::paths::AidHomeGuard::set(temp.path());
let agent = crate::agent::get_agent(agent_kind);
let _ = agent.parse_event(&crate::types::TaskId("t-forge".to_string()), &line);
let _ = agent.parse_completion(&line);
let written = markers_on_disk();
assert!(
written.is_empty(),
"{agent_kind:?} wrote {written:?} from a line the model could author: {line}"
);
}
}
fn model_authored_shapes(needle: &str) -> Vec<String> {
let report = format!("FAILED: | cursor | `{needle}` | yes | JSON `type:error` |");
vec![
format!("Error: the audit found \"{needle}\" in rate_limit_signatures.rs"),
report.clone(),
format!("completed: grep {needle}"),
assistant_envelope(&report),
assistant_envelope(&format!("The needle is \"{needle}\".")),
format!(r#"{{"type":"message","content":"{report}"}}"#),
format!(
r#"{{"type":"user","message":{{"content":[{{"type":"tool_result","tool_use_id":"toolu_1","is_error":true,"content":"grep: {needle}: no matches"}}]}}}}"#
),
format!(
r#"{{"type":"tool_call","subtype":"completed","tool_call":{{"grepToolCall":{{"args":{{"pattern":"{needle}"}}}}}}}}"#
),
format!(r#"{{"type":"text","part":{{"text":"the needle is {needle}"}}}}"#),
]
}
fn adapters_under_test() -> Vec<AgentKind> {
AgentKind::ALL
.iter()
.copied()
.filter(|kind| *kind != AgentKind::Custom)
.chain(std::iter::once(AgentKind::Claude))
.collect()
}
fn markers_on_disk() -> Vec<String> {
let Ok(entries) = std::fs::read_dir(crate::paths::aid_dir()) else {
return Vec::new();
};
entries
.flatten()
.map(|entry| entry.file_name().to_string_lossy().into_owned())
.filter(|name| name.starts_with("rate-limit-"))
.collect()
}
#[test]
fn stderr_is_the_cursor_premium_channel_and_is_kept_whole() {
let line = "ActionRequiredError: Increase limits for faster responses You're out of usage. \
Switch to Auto, or ask your admin to increase your limit to continue.";
let kept = provider_attributable(line, AgentKind::Cursor, Channel::CliStderr);
assert!(kept.cli_diagnostic.contains("You're out of usage"));
assert!(
kept.unsplit.is_empty(),
"stderr has no unsplit half — the whole channel is the CLI's"
);
}