Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions apps/decodex/src/agent/app_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,13 +1110,22 @@ fn protocol_account_detail(payload_value: Option<&Value>) -> Option<String> {
&[
&["params", "planType"],
&["params", "chatgptPlanType"],
&["params", "rateLimits", "planType"],
&["planType"],
&["chatgptPlanType"],
&["rateLimits", "planType"],
],
);
let status = string_at_paths(
value,
&[&["params", "status"], &["params", "refreshStatus"], &["status"], &["refreshStatus"]],
&[
&["params", "status"],
&["params", "refreshStatus"],
&["params", "rateLimits", "rateLimitReachedType"],
&["status"],
&["refreshStatus"],
&["rateLimits", "rateLimitReachedType"],
],
);

match (plan, status) {
Expand Down Expand Up @@ -1207,17 +1216,15 @@ fn thread_status_waiting_reason(payload_value: Option<&Value>) -> Option<String>
None
}

fn protocol_rate_limit_status(event_type: &str, payload: &str) -> Option<String> {
fn protocol_rate_limit_status(_event_type: &str, payload: &str) -> Option<String> {
let payload_value = serde_json::from_str::<Value>(payload).ok()?;

find_string_field(&payload_value, &["rateLimitReachedType", "rate_limit_reached_type"])
.or_else(|| {
find_string_field(&payload_value, &["rateLimitReachedType", "rate_limit_reached_type"]).or_else(
|| {
find_string_field(&payload_value, &["codexErrorInfo", "codex_error_info"])
.filter(|value| value.to_ascii_lowercase().contains("limit"))
})
.or_else(|| {
event_type.to_ascii_lowercase().contains("ratelimit").then(|| event_type.to_owned())
})
},
)
}

fn child_tool_call_event(
Expand Down Expand Up @@ -1519,9 +1526,9 @@ fn find_string_field(value: &Value, keys: &[&str]) -> Option<String> {
Value::Object(entries) => {
for (key, nested) in entries {
if keys.iter().any(|candidate| *candidate == key)
&& let Some(text) = nested.as_str()
&& let Some(text) = string_like_json_value(nested)
{
return Some(text.to_owned());
return Some(text);
}
}

Expand All @@ -1532,6 +1539,18 @@ fn find_string_field(value: &Value, keys: &[&str]) -> Option<String> {
}
}

fn string_like_json_value(value: &Value) -> Option<String> {
match value {
Value::String(text) if !text.is_empty() => Some(text.clone()),
Value::Number(number) => Some(number.to_string()),
Value::Bool(value) => Some(value.to_string()),
Value::Object(entries) => ["kind", "type"]
.iter()
.find_map(|key| entries.get(*key).and_then(string_like_json_value)),
_ => None,
}
}

fn json_number_to_i64(value: &Value) -> Option<i64> {
value.as_i64().or_else(|| value.as_u64().and_then(|number| i64::try_from(number).ok()))
}
Expand Down
51 changes: 51 additions & 0 deletions apps/decodex/src/agent/app_server/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,57 @@ fn recorder_summarizes_high_value_protocol_activity() {
}));
}

#[test]
fn recorder_summarizes_v2_account_rate_limit_notifications() {
let temp_dir = TempDir::new().expect("tempdir should create");
let state_store = StateStore::open_in_memory().expect("state store should open");
let marker_path = temp_dir.path().to_path_buf();
let mut recorder = RunRecorder::new(&state_store, "run-1", 1, Some(&marker_path));

recorder
.record(
"account/rateLimits/updated",
r#"{"method":"account/rateLimits/updated","params":{"rateLimits":{"planType":"pro","rateLimitReachedType":"workspace_member_usage_limit_reached","primary":{"usedPercent":100}}}}"#,
)
.expect("rate limit protocol event should record");

let marker = state::read_run_activity_marker_snapshot(temp_dir.path())
.expect("marker snapshot should load")
.expect("marker snapshot should exist");
let summary = marker.protocol_activity().expect("protocol activity should be captured");
let event = summary.recent_events.first().expect("recent rate limit event should render");

assert_eq!(summary.rate_limit_status.as_deref(), Some("workspace_member_usage_limit_reached"));
assert_eq!(event.category, "rate_limit");
assert_eq!(event.detail.as_deref(), Some("pro/workspace_member_usage_limit_reached"));
}

#[test]
fn recorder_does_not_treat_rate_limit_update_method_as_limit_status() {
let temp_dir = TempDir::new().expect("tempdir should create");
let state_store = StateStore::open_in_memory().expect("state store should open");
let marker_path = temp_dir.path().to_path_buf();
let mut recorder = RunRecorder::new(&state_store, "run-1", 1, Some(&marker_path));

recorder
.record(
"account/rateLimits/updated",
r#"{"method":"account/rateLimits/updated","params":{"rateLimits":{"planType":"pro","rateLimitReachedType":null,"primary":{"usedPercent":12}}}}"#,
)
.expect("rate limit update event should record");

let marker = state::read_run_activity_marker_snapshot(temp_dir.path())
.expect("marker snapshot should load")
.expect("marker snapshot should exist");
let summary = marker.protocol_activity().expect("protocol activity should be captured");

assert_eq!(summary.rate_limit_status, None);
assert_eq!(
summary.recent_events.first().and_then(|event| event.detail.as_deref()),
Some("pro")
);
}

#[test]
fn recorder_summarizes_wrapped_account_protocol_activity() {
let temp_dir = TempDir::new().expect("tempdir should create");
Expand Down
Loading