Files
ai-agent/backend/test_week_target.py
gabrielvidalandClaude Opus 5.5 8d1a46de83 feat(usage): a week target on the home week gauge, and a push when behind
The personal Max is meant to be maxed out every week. Its account declares
`weekTarget: {goal: 100, behindAlert: 20}`: a straight line from 0 % when the
week opens to 100 % at its reset (usage/target.py, mirrored by
lib/weekTarget.ts).

- /api/usage/limits carries the account's `weekTarget`.
- The home usage strip's week gauge gets a tick at the target (ArcGauge
  `target`) and a "target N%" line under the reset countdown, re-read
  every minute.
- The poller pushes "🎯 Personal week behind its target" once each time the
  week falls more than 20 points under the line (crossing between two
  stored points, so a restart never re-pushes; catching up re-arms it),
  with how many nonstop Opus runs would use the rest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:11:25 +01:00

94 lines
3.7 KiB
Python

"""The week target (usage/target.py): the straight line to the goal at the
reset, and the poller's one push per fall more than ``behindAlert`` points
under it. ``python3 -m unittest test_week_target``"""
import unittest
from usage import target
from usage.poller import HEARTBEAT_S, UsagePoller
DAY = 86400
RESET = 1_800_000_000.0 # the personal week's reset
OPEN = RESET - 7 * DAY # when that window opened
class FakeUsageStore:
def __init__(self):
self.rows = []
def last_point(self, account, window):
pts = [r for r in self.rows if r[0] == account and r[1] == window]
return (pts[-1][2], pts[-1][3], pts[-1][4]) if pts else None
def add_points(self, rows):
self.rows += rows
return len(rows)
class Line(unittest.TestCase):
def test_straight_line_to_the_goal(self):
self.assertEqual(target.linear(RESET, OPEN), 0.0)
self.assertAlmostEqual(target.linear(RESET, OPEN + 3.5 * DAY), 50.0)
self.assertAlmostEqual(target.linear(RESET, OPEN + 3.5 * DAY, goal=90), 45.0)
self.assertAlmostEqual(target.gap(40, RESET, OPEN + 3.5 * DAY), 10.0)
self.assertAlmostEqual(target.gap(60, RESET, OPEN + 3.5 * DAY), -10.0)
def test_no_window_no_target(self):
self.assertIsNone(target.linear(None, OPEN))
self.assertIsNone(target.linear(RESET, RESET)) # already reset
self.assertEqual(target.linear(RESET, OPEN - DAY), 0.0) # clamped
def test_declared_on_the_personal_account_only(self):
self.assertEqual(target.of("personal")["goal"], 100)
self.assertIsNone(target.of("work"))
class BehindPush(unittest.TestCase):
def setUp(self):
self.body = {"updated_at": OPEN + DAY, "rate_limits": {
"seven_day": {"used_percentage": 10.0, "resets_at": RESET}}}
self.pushes = []
self.poller = UsagePoller(FakeUsageStore(), dirs={},
remote=lambda: {"personal": self.body})
self.poller.behind = lambda *a: self.pushes.append(a)
def at(self, t, pct):
self.body["updated_at"] = t
self.body["rate_limits"]["seven_day"]["used_percentage"] = pct
self.poller.poll_once()
def test_once_per_fall_behind(self):
self.at(OPEN + DAY, 10.0) # target 14.3: 4 under
self.at(OPEN + 2 * DAY, 10.0) # heartbeat, 28.6: 19 under
self.assertEqual(self.pushes, [])
self.at(OPEN + 2.5 * DAY, 10.0) # 35.7: 26 under → push
self.assertEqual(len(self.pushes), 1)
acct, pct, tgt, resets = self.pushes[0]
self.assertEqual((acct, pct, resets), ("personal", 10.0, RESET))
self.assertAlmostEqual(tgt, 100 * 2.5 / 7)
self.at(OPEN + 3 * DAY, 11.0) # still behind: no second push
self.assertEqual(len(self.pushes), 1)
self.at(OPEN + 3 * DAY + 600, 40.0) # caught up (42.9: 3 under) …
self.at(OPEN + 4.5 * DAY, 40.0) # … and fell behind again (64.3)
self.assertEqual(len(self.pushes), 2)
def test_a_restart_never_re_pushes(self):
self.at(OPEN + DAY, 10.0)
self.at(OPEN + 2.5 * DAY, 10.0)
self.assertEqual(len(self.pushes), 1)
store = self.poller.store
self.poller = UsagePoller(store, dirs={}, remote=lambda: {"personal": self.body})
self.poller.behind = lambda *a: self.pushes.append(a)
self.at(OPEN + 2.5 * DAY + HEARTBEAT_S, 10.0)
self.assertEqual(len(self.pushes), 1)
def test_only_the_declared_week(self):
self.poller.remote = lambda: {"work": self.body}
self.at(OPEN + DAY, 10.0)
self.at(OPEN + 3 * DAY, 10.0)
self.assertEqual(self.pushes, [])
if __name__ == "__main__":
unittest.main()