claude-dev-env 8.114.7 → 8.114.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
from datetime import timedelta
|
|
2
2
|
|
|
3
|
-
from hooks_constants.usage_pace_constants import
|
|
3
|
+
from hooks_constants.usage_pace_constants import (
|
|
4
|
+
ALL_OPTIONAL_WINDOW_NAMES,
|
|
5
|
+
ALL_RATE_WINDOW_LENGTHS,
|
|
6
|
+
)
|
|
4
7
|
|
|
5
8
|
|
|
6
9
|
def test_usage_pace_windows_have_expected_lengths() -> None:
|
|
@@ -8,3 +11,7 @@ def test_usage_pace_windows_have_expected_lengths() -> None:
|
|
|
8
11
|
"five_hour": timedelta(hours=5),
|
|
9
12
|
"seven_day": timedelta(days=7),
|
|
10
13
|
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_usage_pace_treats_only_the_five_hour_window_as_optional() -> None:
|
|
17
|
+
assert ALL_OPTIONAL_WINDOW_NAMES == {"five_hour"}
|
|
@@ -105,7 +105,9 @@ def test_should_report_under_pace_when_every_window_trails() -> None:
|
|
|
105
105
|
@pytest.mark.parametrize(
|
|
106
106
|
"broken_payload",
|
|
107
107
|
[
|
|
108
|
-
{"
|
|
108
|
+
{"five_hour": {"utilization": 1.0, "resets_at": FIVE_HOUR_RESET}},
|
|
109
|
+
{"five_hour": None, "seven_day": None},
|
|
110
|
+
{},
|
|
109
111
|
{
|
|
110
112
|
"five_hour": {"utilization": None, "resets_at": FIVE_HOUR_RESET},
|
|
111
113
|
"seven_day": {"utilization": 1.0, "resets_at": WEEKLY_RESET},
|
|
@@ -123,6 +125,42 @@ def test_should_treat_a_missing_or_unreadable_window_as_unreadable(
|
|
|
123
125
|
measure_account_pace(broken_payload, NOW)
|
|
124
126
|
|
|
125
127
|
|
|
128
|
+
@pytest.mark.parametrize(
|
|
129
|
+
"weekly_only_payload",
|
|
130
|
+
[
|
|
131
|
+
{"five_hour": None, "seven_day": {"utilization": 93.0, "resets_at": WEEKLY_RESET}},
|
|
132
|
+
{"seven_day": {"utilization": 93.0, "resets_at": WEEKLY_RESET}},
|
|
133
|
+
],
|
|
134
|
+
)
|
|
135
|
+
def test_should_measure_only_the_weekly_window_when_the_account_has_no_five_hour_window(
|
|
136
|
+
weekly_only_payload: dict[str, object],
|
|
137
|
+
) -> None:
|
|
138
|
+
all_window_paces = measure_account_pace(weekly_only_payload, NOW)
|
|
139
|
+
assert [each.name for each in all_window_paces] == ["seven_day"]
|
|
140
|
+
verdict = build_verdict(all_window_paces, NOW)
|
|
141
|
+
assert verdict["over_pace"] is True
|
|
142
|
+
assert [each["window"] for each in verdict["windows"]] == ["seven_day"]
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def test_should_report_under_pace_when_a_weekly_only_account_trails_the_clock() -> None:
|
|
146
|
+
weekly_only_payload = {
|
|
147
|
+
"five_hour": None,
|
|
148
|
+
"seven_day": {"utilization": 20.0, "resets_at": WEEKLY_RESET},
|
|
149
|
+
}
|
|
150
|
+
verdict = build_verdict(measure_account_pace(weekly_only_payload, NOW), NOW)
|
|
151
|
+
assert verdict["over_pace"] is False
|
|
152
|
+
assert [each["window"] for each in verdict["windows"]] == ["seven_day"]
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def test_should_still_reject_a_five_hour_window_that_is_present_but_unreadable() -> None:
|
|
156
|
+
broken_five_hour_payload = {
|
|
157
|
+
"five_hour": {"utilization": 1.0},
|
|
158
|
+
"seven_day": {"utilization": 1.0, "resets_at": WEEKLY_RESET},
|
|
159
|
+
}
|
|
160
|
+
with pytest.raises(UsageUnreadable):
|
|
161
|
+
measure_account_pace(broken_five_hour_payload, NOW)
|
|
162
|
+
|
|
163
|
+
|
|
126
164
|
def test_should_exit_zero_with_the_verdict_when_over_pace(tmp_path: Path) -> None:
|
|
127
165
|
exit_code, verdict = _run_script(tmp_path, _payload(3.0, 93.0))
|
|
128
166
|
assert exit_code == 0
|
|
@@ -135,8 +173,28 @@ def test_should_exit_one_when_under_pace(tmp_path: Path) -> None:
|
|
|
135
173
|
assert verdict["over_pace"] is False
|
|
136
174
|
|
|
137
175
|
|
|
176
|
+
def test_should_exit_zero_for_a_weekly_only_account_running_ahead(tmp_path: Path) -> None:
|
|
177
|
+
exit_code, verdict = _run_script(
|
|
178
|
+
tmp_path,
|
|
179
|
+
{"five_hour": None, "seven_day": {"utilization": 93.0, "resets_at": WEEKLY_RESET}},
|
|
180
|
+
)
|
|
181
|
+
assert exit_code == 0
|
|
182
|
+
assert verdict["over_pace"] is True
|
|
183
|
+
assert [each["window"] for each in verdict["windows"]] == ["seven_day"]
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def test_should_exit_one_for_a_weekly_only_account_under_pace(tmp_path: Path) -> None:
|
|
187
|
+
exit_code, verdict = _run_script(
|
|
188
|
+
tmp_path,
|
|
189
|
+
{"five_hour": None, "seven_day": {"utilization": 20.0, "resets_at": WEEKLY_RESET}},
|
|
190
|
+
)
|
|
191
|
+
assert exit_code == 1
|
|
192
|
+
assert verdict["over_pace"] is False
|
|
193
|
+
assert [each["window"] for each in verdict["windows"]] == ["seven_day"]
|
|
194
|
+
|
|
195
|
+
|
|
138
196
|
def test_should_exit_two_with_a_null_verdict_when_unreadable(tmp_path: Path) -> None:
|
|
139
197
|
exit_code, verdict = _run_script(tmp_path, {"five_hour": None})
|
|
140
198
|
assert exit_code == 2
|
|
141
199
|
assert verdict["over_pace"] is None
|
|
142
|
-
assert verdict["error"] == "usage response has no
|
|
200
|
+
assert verdict["error"] == "usage response has no seven_day window"
|
|
@@ -12,10 +12,12 @@
|
|
|
12
12
|
|
|
13
13
|
A window is over pace when at least 10% of it has elapsed and its used percent
|
|
14
14
|
exceeds the percent already elapsed. The floor keeps a window's first minutes,
|
|
15
|
-
where any spend outruns the clock, from reading as over pace. The account is
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
where any spend outruns the clock, from reading as over pace. The account is
|
|
16
|
+
over pace when any window is. An account with only a weekly window reports
|
|
17
|
+
``five_hour`` as null or leaves it out, and the verdict then covers the weekly
|
|
18
|
+
window alone. The script reads the OAuth usage endpoint with the session
|
|
19
|
+
ingress bearer token, the same probe the usage-pause skill's
|
|
20
|
+
``resolve_usage_window.py`` uses. It never prints or stores the token.
|
|
19
21
|
|
|
20
22
|
Exit codes: 0 over pace, 1 under pace, 2 unreadable. ``--payload-file`` and
|
|
21
23
|
``--now`` replace the live probe and the clock for a deterministic run.
|
|
@@ -39,6 +41,7 @@ if hooks_root_directory not in sys.path:
|
|
|
39
41
|
sys.path.insert(0, hooks_root_directory)
|
|
40
42
|
|
|
41
43
|
from hooks_constants.usage_pace_constants import (
|
|
44
|
+
ALL_OPTIONAL_WINDOW_NAMES,
|
|
42
45
|
ALL_RATE_WINDOW_LENGTHS,
|
|
43
46
|
ANTHROPIC_BETA_HEADER,
|
|
44
47
|
AUTHORIZATION_HEADER,
|
|
@@ -169,22 +172,34 @@ def measure_window_pace(
|
|
|
169
172
|
def measure_account_pace(
|
|
170
173
|
all_usage_buckets: dict[str, object], now: datetime
|
|
171
174
|
) -> list[WindowPace]:
|
|
172
|
-
"""Measure every rate window the
|
|
175
|
+
"""Measure every rate window the account has.
|
|
176
|
+
|
|
177
|
+
::
|
|
178
|
+
|
|
179
|
+
{"five_hour": None, "seven_day": {...}} -> [seven_day]
|
|
180
|
+
{"five_hour": {...}, "seven_day": {...}} -> [five_hour, seven_day]
|
|
181
|
+
|
|
182
|
+
The five-hour window is optional because accounts with only a weekly
|
|
183
|
+
window report it as null or leave it out. The weekly window is required.
|
|
173
184
|
|
|
174
185
|
Args:
|
|
175
186
|
all_usage_buckets: The decoded usage response body.
|
|
176
187
|
now: The time the pace is measured at.
|
|
177
188
|
|
|
178
189
|
Returns:
|
|
179
|
-
One WindowPace per rate window, five-hour first.
|
|
190
|
+
One WindowPace per rate window the account has, five-hour first.
|
|
180
191
|
|
|
181
192
|
Raises:
|
|
182
|
-
UsageUnreadable:
|
|
193
|
+
UsageUnreadable: A required window is missing, or a window that is
|
|
194
|
+
present is unreadable.
|
|
183
195
|
"""
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
196
|
+
all_window_paces: list[WindowPace] = []
|
|
197
|
+
for each_name, each_length in ALL_RATE_WINDOW_LENGTHS:
|
|
198
|
+
each_bucket = all_usage_buckets.get(each_name)
|
|
199
|
+
if each_bucket is None and each_name in ALL_OPTIONAL_WINDOW_NAMES:
|
|
200
|
+
continue
|
|
201
|
+
all_window_paces.append(measure_window_pace(each_name, each_length, each_bucket, now))
|
|
202
|
+
return all_window_paces
|
|
188
203
|
|
|
189
204
|
|
|
190
205
|
def build_verdict(
|