Skip to content

Commit fb272c0

Browse files
committed
feat(runner): detect the agent's usage-limit message and expose it as limit on the session (TM-977 B1)
1 parent d060047 commit fb272c0

8 files changed

Lines changed: 233 additions & 0 deletions

File tree

‎crates/tmgr-cli/src/cmd/runner/attach.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5389,6 +5389,7 @@ mod tests {
53895389
fn info(id: &str) -> SessionInfo {
53905390
SessionInfo {
53915391
agent_session_id: None,
5392+
limit: None,
53925393
team: None,
53935394
id: id.into(),
53945395
name: id.into(),

‎crates/tmgr-ipc/src/api/types.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,15 @@ pub struct SessionInfo {
318318
pub team: Option<TeamLink>,
319319
#[serde(default, skip_serializing_if = "Option::is_none")]
320320
pub agent_session_id: Option<String>,
321+
#[serde(default, skip_serializing_if = "Option::is_none")]
322+
pub limit: Option<LimitHit>,
323+
}
324+
325+
/// The agent printed its usage-limit message and the message stayed on screen.
326+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
327+
pub struct LimitHit {
328+
pub line: String,
329+
pub at_ms: u64,
321330
}
322331

323332
/// Params of `runs.lookup`.

‎crates/tmgr-ipc/tests/contract.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ fn session_info() -> SessionInfo {
139139
SessionInfo {
140140
team: None,
141141
agent_session_id: None,
142+
limit: None,
142143
id: SID.into(),
143144
name: "fix login".into(),
144145
argv: vec![
@@ -468,6 +469,16 @@ fn method_params_and_results() {
468469
..session_info()
469470
},
470471
);
472+
golden(
473+
"session_info_limit",
474+
&SessionInfo {
475+
limit: Some(LimitHit {
476+
line: "5-hour limit reached resets 3pm".into(),
477+
at_ms: 1_700_000_000_000,
478+
}),
479+
..session_info()
480+
},
481+
);
471482
golden(
472483
"lookup_params",
473484
&LookupParams {
@@ -605,6 +616,7 @@ fn method_params_and_results() {
605616
&SessionInfo {
606617
team: None,
607618
agent_session_id: None,
619+
limit: None,
608620
id: SID.into(),
609621
name: "sh".into(),
610622
argv: vec!["/bin/sh".into()],
@@ -1551,6 +1563,7 @@ fn full_info() -> SessionInfo {
15511563
SessionInfo {
15521564
team: None,
15531565
agent_session_id: None,
1566+
limit: None,
15541567
permission_cards: false,
15551568
id: "s_abcdefghij".into(),
15561569
name: "n".into(),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"id": "s_abcdefghij", "name": "fix login", "argv": ["/usr/local/bin/codex", "--model", "gpt-5"], "cwd": "/work/project", "profile": "codex", "state": {"kind": "working", "since_ms": 1760000000000, "detail": null, "exit_code": null}, "created_ms": 1759999990000, "cols": 120, "rows": 40, "shim_pid": 501, "child_pid": 502, "task": {"workspace_id": -3, "task_id": 412}, "limit": {"line": "5-hour limit reached resets 3pm", "at_ms": 1700000000000}}

‎crates/tmgr-link/tests/runs.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ fn info(id: &str, req: &RunCreateReq) -> SessionInfo {
7878
SessionInfo {
7979
team: None,
8080
agent_session_id: None,
81+
limit: None,
8182
id: id.into(),
8283
name: req.name.clone().unwrap_or_default(),
8384
argv: vec!["/usr/bin/true".into(), "--secret-flag".into()],
@@ -1565,6 +1566,7 @@ fn local(id: &str, edit: impl FnOnce(&mut SessionInfo)) -> SessionInfo {
15651566
let mut s = SessionInfo {
15661567
team: None,
15671568
agent_session_id: None,
1569+
limit: None,
15681570
id: id.into(),
15691571
name: "hand-started".into(),
15701572
argv: vec!["/usr/bin/true".into()],

‎crates/tmgr-runner/src/daemon.rs‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::clone;
66
use crate::exe::{resolve_exe, verify_pin};
77
use crate::folders;
88
use crate::humantime;
9+
use crate::limit;
910
use crate::login;
1011
use crate::objectmodes;
1112
use crate::orchestrator;
@@ -69,6 +70,7 @@ const SHIM_QUEUE: usize = 1024;
6970
const SHIM_INPUT_CAP: usize = 1 << 20;
7071
const SHIM_INPUT_FRAMES: usize = 512;
7172
const SHIM_CREDIT_FRAMES: u64 = 1024;
73+
const LIMIT_CONFIRM: Duration = Duration::from_secs(3);
7274
const SHIM_LINK_SILENT: Duration = Duration::from_secs(5);
7375
const ATTACH_INPUT_WAIT: Duration = Duration::from_secs(2);
7476
// A note typed into the agent only shows on screen after its next redraw; wait that long before the next one.
@@ -421,6 +423,8 @@ struct Meta {
421423
team: Option<TeamLink>,
422424
#[serde(default, skip_serializing_if = "Option::is_none")]
423425
agent_session_id: Option<String>,
426+
#[serde(default, skip_serializing_if = "Option::is_none")]
427+
limit: Option<tmgr_ipc::api::LimitHit>,
424428
}
425429

426430
#[derive(Serialize, Deserialize, Clone)]
@@ -495,6 +499,7 @@ struct Inner {
495499
fixed: Option<State>,
496500
size_owner: Option<u64>,
497501
input_sent_at: Option<Instant>,
502+
limit_first: Option<Instant>,
498503
}
499504

500505
#[derive(Default)]
@@ -731,6 +736,7 @@ impl Session {
731736
SessionInfo {
732737
team: m.team.clone(),
733738
agent_session_id: m.agent_session_id.clone(),
739+
limit: m.limit.clone(),
734740
id: m.id.clone(),
735741
name: m.name.clone(),
736742
argv: m.argv.clone(),
@@ -992,13 +998,50 @@ impl Session {
992998
}
993999
});
9941000
self.version.fetch_add(1, Ordering::SeqCst);
1001+
if live {
1002+
self.watch_limit(None);
1003+
}
9951004
for reply in replies {
9961005
if self.reply_allowed() {
9971006
let _ = self.shim_input(&reply);
9981007
}
9991008
}
10001009
}
10011010

1011+
fn watch_limit(&self, exit: Option<Option<i32>>) {
1012+
let mut hit = None;
1013+
{
1014+
let mut i = lock(&self.inner);
1015+
if i.meta.limit.is_some() {
1016+
return;
1017+
}
1018+
let Some(profile) = i.meta.profile.clone() else {
1019+
return;
1020+
};
1021+
let found = limit::detect(&profile, &i.screen.bottom_text(12));
1022+
match (found, exit) {
1023+
(Some(line), Some(code)) if code != Some(0) => hit = Some(line),
1024+
(Some(line), None) => {
1025+
let first = *i.limit_first.get_or_insert_with(Instant::now);
1026+
if first.elapsed() >= LIMIT_CONFIRM {
1027+
hit = Some(line);
1028+
}
1029+
}
1030+
(None, _) | (Some(_), Some(_)) => i.limit_first = None,
1031+
}
1032+
if let Some(line) = &hit {
1033+
i.meta.limit = Some(tmgr_ipc::api::LimitHit {
1034+
line: line.clone(),
1035+
at_ms: now_ms(),
1036+
});
1037+
}
1038+
}
1039+
if hit.is_some() {
1040+
self.persist();
1041+
self.version.fetch_add(1, Ordering::SeqCst);
1042+
}
1043+
}
1044+
10021045
fn card_agent(&self) -> Option<permit::Agent> {
10031046
let i = lock(&self.inner);
10041047
i.meta
@@ -1226,6 +1269,7 @@ impl Session {
12261269
}
12271270
out.extend(changed)
12281271
});
1272+
self.watch_limit(Some(e.code));
12291273
self.worklog.exited(at, e.code, e.signal);
12301274
self.learn_trust();
12311275
finish_seeds(&self.dir, &self.id);
@@ -1289,6 +1333,9 @@ impl Session {
12891333

12901334
fn tick(self: &Arc<Self>) {
12911335
self.change(|i, now, out| out.extend(i.det.tick(now)));
1336+
if !self.is_exited() {
1337+
self.watch_limit(None);
1338+
}
12921339
self.cp_tick();
12931340
self.agent_work();
12941341
}
@@ -4602,6 +4649,7 @@ impl Runner {
46024649
permission_cards: extra.permission_cards,
46034650
team: extra.team.clone(),
46044651
agent_session_id: extra.agent_session_id.clone(),
4652+
limit: None,
46054653
};
46064654
write_meta(sdir, &meta);
46074655
let session = self.new_session(meta, matcher, sdir);
@@ -4649,6 +4697,7 @@ impl Runner {
46494697
fixed: None,
46504698
size_owner: None,
46514699
input_sent_at: None,
4700+
limit_first: None,
46524701
}),
46534702
version: AtomicU64::new(0),
46544703
shim: Mutex::new(None),
@@ -5872,6 +5921,62 @@ mod tests {
58725921
Instant::now() + SEND_SETTLE
58735922
}
58745923

5924+
const LIMIT_SCREEN: &[u8] = "\r\n5-hour limit reached \u{2219} resets 3pm\r\n".as_bytes();
5925+
5926+
fn backdate_limit(s: &Session) {
5927+
lock(&s.inner).limit_first = Some(Instant::now() - LIMIT_CONFIRM - Duration::from_secs(1));
5928+
}
5929+
5930+
#[test]
5931+
fn a_limit_message_counts_only_after_it_stayed_on_screen() {
5932+
let (_t, s, _rx) = lone_session();
5933+
s.feed(LIMIT_SCREEN, true);
5934+
assert!(s.info().limit.is_none());
5935+
backdate_limit(&s);
5936+
s.watch_limit(None);
5937+
let hit = s.info().limit.expect("limit hit");
5938+
assert!(hit.line.starts_with("5-hour limit reached"));
5939+
assert!(lock(&s.inner).meta.limit.is_some());
5940+
}
5941+
5942+
#[test]
5943+
fn a_limit_message_that_goes_away_resets_the_wait() {
5944+
let (_t, s, _rx) = lone_session();
5945+
s.feed(LIMIT_SCREEN, true);
5946+
s.feed(b"\x1b[2J\x1b[Hworking again\r\n", true);
5947+
backdate_limit(&s);
5948+
s.watch_limit(None);
5949+
assert!(s.info().limit.is_none());
5950+
}
5951+
5952+
#[test]
5953+
fn prose_that_mentions_a_limit_is_not_a_hit() {
5954+
let (_t, s, _rx) = lone_session();
5955+
s.feed(
5956+
b"\r\nI added a check for 5-hour limit reached in limit.rs\r\n",
5957+
true,
5958+
);
5959+
backdate_limit(&s);
5960+
s.watch_limit(None);
5961+
assert!(s.info().limit.is_none());
5962+
}
5963+
5964+
#[test]
5965+
fn a_failed_exit_with_the_message_on_screen_counts_at_once() {
5966+
let (_t, s, _rx) = lone_session();
5967+
s.feed(LIMIT_SCREEN, true);
5968+
s.watch_limit(Some(Some(1)));
5969+
assert!(s.info().limit.is_some());
5970+
}
5971+
5972+
#[test]
5973+
fn a_clean_exit_never_counts() {
5974+
let (_t, s, _rx) = lone_session();
5975+
s.feed(LIMIT_SCREEN, true);
5976+
s.watch_limit(Some(Some(0)));
5977+
assert!(s.info().limit.is_none());
5978+
}
5979+
58755980
#[test]
58765981
fn a_second_send_right_after_the_first_is_refused_even_after_an_echo() {
58775982
let (_t, s, rx) = lone_session();

‎crates/tmgr-runner/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod attach;
22
pub mod egress;
33
#[cfg(unix)]
44
pub mod exe;
5+
pub mod limit;
56
pub mod login;
67
pub mod orchestrator;
78
pub mod permit;

‎crates/tmgr-runner/src/limit.rs‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
use regex::Regex;
2+
use std::sync::OnceLock;
3+
4+
fn claude() -> &'static Regex {
5+
static R: OnceLock<Regex> = OnceLock::new();
6+
R.get_or_init(|| {
7+
Regex::new(concat!(
8+
r"(?i)^\W{0,4}(?:",
9+
r"(?:\d+-hour|weekly|session|opus weekly|sonnet weekly) limit reached",
10+
r"|claude (?:ai )?usage limit reached",
11+
r"|you(?:'|’)ve hit your (?:\w+ )?limit",
12+
r")",
13+
))
14+
.expect("claude limit")
15+
})
16+
}
17+
18+
fn codex() -> &'static Regex {
19+
static R: OnceLock<Regex> = OnceLock::new();
20+
R.get_or_init(|| {
21+
Regex::new(concat!(
22+
r"(?i)^\W{0,4}(?:",
23+
r"you(?:'|’)ve hit your usage limit",
24+
r"|you exceeded your current quota",
25+
r"|usage limit reached",
26+
r")",
27+
))
28+
.expect("codex limit")
29+
})
30+
}
31+
32+
pub fn detect(profile: &str, lines: &[String]) -> Option<String> {
33+
let re = match profile {
34+
"claude-code" => claude(),
35+
"codex" => codex(),
36+
_ => return None,
37+
};
38+
lines
39+
.iter()
40+
.rev()
41+
.find(|l| re.is_match(l.trim_start()))
42+
.map(|l| l.trim().chars().take(200).collect())
43+
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use super::*;
48+
49+
fn screen(lines: &[&str]) -> Vec<String> {
50+
lines.iter().map(|l| (*l).to_string()).collect()
51+
}
52+
53+
#[test]
54+
fn claude_limit_messages() {
55+
for line in [
56+
"5-hour limit reached ∙ resets 3pm",
57+
" ⎿ 5-hour limit reached ∙ resets 3pm",
58+
"Weekly limit reached ∙ resets Mon 9am",
59+
"Claude usage limit reached. Your limit will reset at 3pm.",
60+
"You've hit your limit · resets 5pm (Europe/Warsaw)",
61+
"You’ve hit your session limit · resets 5pm",
62+
] {
63+
let found = detect("claude-code", &screen(&["> continue", line, ""]));
64+
assert!(found.is_some(), "{line}");
65+
}
66+
}
67+
68+
#[test]
69+
fn codex_limit_messages() {
70+
for line in [
71+
"You've hit your usage limit. Upgrade to Pro or try again at 3:14 PM.",
72+
"■ You've hit your usage limit. Try again later.",
73+
"You exceeded your current quota, please check your plan.",
74+
] {
75+
assert!(detect("codex", &screen(&[line])).is_some(), "{line}");
76+
}
77+
}
78+
79+
#[test]
80+
fn prose_and_quotes_do_not_match() {
81+
for line in [
82+
"I added a detector for \"5-hour limit reached\" in limit.rs",
83+
"The error says: You've hit your limit",
84+
"- handle the usage limit reached case",
85+
"rate limit reached, retrying in 2s",
86+
] {
87+
assert!(detect("claude-code", &screen(&[line])).is_none(), "{line}");
88+
assert!(detect("codex", &screen(&[line])).is_none(), "{line}");
89+
}
90+
}
91+
92+
#[test]
93+
fn other_profiles_never_match() {
94+
assert!(detect("shell", &screen(&["5-hour limit reached"])).is_none());
95+
}
96+
97+
#[test]
98+
fn claude_text_is_not_a_codex_limit() {
99+
assert!(detect("codex", &screen(&["5-hour limit reached ∙ resets 3pm"])).is_none());
100+
}
101+
}

0 commit comments

Comments
 (0)