Class: AgentsControl::RateLimitWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/agents_control/rate_limit_watcher.rb

Overview

Notices a rate-limit message on an agent's screen and types "continue" itself at the exact moment the limit resets.

Three message formats:

5-hour limit reached - resets 3pm (UTC)
You've hit your session limit · resets 2am (Europe/Zurich)
You've hit your weekly limit · resets Oct 9, 10am

Goes through the full Registry, not just tmux (unlike ScreenWatcher) — a limit doesn't hit every few seconds, so polling once a minute keeps the cost of scanning iTerm2 via AppleScript low.

Constant Summary collapse

PATTERN =

"limit ... resets [at] ".

/\blimit\b.{0,60}?\bresets?\b\s*(?:at\s+)?(.+?)\s*$/i
ZONE =

A timezone in parentheses at the end of the line, if present — for display to the human. Not accounted for programmatically: Time.parse treats the time as local to the machine the daemon runs on.

/\(([\w\/]+)\)\s*\z/
HAS_DATE =

A month name in the rest of the line distinguishes a bare time of day ("3pm", today or tomorrow) from a weekly-limit date ("Oct 9").

/[A-Za-z]{3,}\s+\d{1,2}/
GRACE =
60
LINES =
20

Instance Method Summary collapse

Constructor Details

#initialize(registry:, config:, store:, api:, interval: 60, clock: -> { Time.now }, logger: nil) ⇒ RateLimitWatcher

Returns a new instance of RateLimitWatcher.



34
35
36
37
38
39
40
41
42
43
# File 'lib/agents_control/rate_limit_watcher.rb', line 34

def initialize(registry:, config:, store:, api:, interval: 60, clock: -> { Time.now }, logger: nil)
  @registry = registry
  @config = config
  @store = store
  @api = api
  @interval = interval
  @clock = clock
  @logger = logger
  @running = false
end

Instance Method Details

#startObject



45
46
47
48
49
50
51
# File 'lib/agents_control/rate_limit_watcher.rb', line 45

def start
  return self unless enabled?

  @running = true
  @thread = Thread.new { tick while @running }
  self
end

#stopObject



53
54
55
56
# File 'lib/agents_control/rate_limit_watcher.rb', line 53

def stop
  @running = false
  @thread&.kill
end

#tickObject



58
59
60
61
62
63
64
# File 'lib/agents_control/rate_limit_watcher.rb', line 58

def tick
  candidates.each { |session| check(session) } if enabled?
rescue StandardError => e
  log("failure: #{e.class}: #{e.message}")
ensure
  sleep(@interval) if @running
end