claude-dev-env 8.111.5 → 8.112.0

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.
Files changed (50) hide show
  1. package/.agents/skills/build-eval/evals/review/README.md +2 -2
  2. package/.agents/skills/build-eval/evals/review/review_eval_support/account.py +166 -0
  3. package/.agents/skills/build-eval/evals/review/review_eval_support/config/constants.py +26 -0
  4. package/.agents/skills/build-eval/evals/review/review_eval_support/execution.py +8 -0
  5. package/.agents/skills/build-eval/evals/review/test_run.py +261 -0
  6. package/_shared/advisor/reference/astra-rung.md +13 -5
  7. package/_shared/advisor/scripts/codex_astra_advisor.py +31 -8
  8. package/_shared/advisor/scripts/codex_astra_preflight.py +70 -14
  9. package/_shared/advisor/scripts/config/advisor_scripts_constants/astra_advisor_constants.py +7 -0
  10. package/_shared/advisor/scripts/tests/test_codex_astra_advisor.py +266 -2
  11. package/_shared/advisor/scripts/tests/test_codex_astra_preflight.py +114 -8
  12. package/_shared/pr-loop/scripts/check_convergence.py +5 -1
  13. package/_shared/pr-loop/scripts/codex_review_account.py +180 -0
  14. package/_shared/pr-loop/scripts/codex_review_scripts_constants/account_constants.py +28 -0
  15. package/_shared/pr-loop/scripts/codex_review_scripts_constants/run_constants.py +3 -1
  16. package/_shared/pr-loop/scripts/run_codex_review.py +144 -15
  17. package/_shared/pr-loop/scripts/test_check_convergence_codex.py +3 -3
  18. package/_shared/pr-loop/scripts/test_codex_review_account.py +355 -0
  19. package/_shared/pr-loop/scripts/test_run_codex_review.py +507 -6
  20. package/docs/account-broker.md +5 -3
  21. package/docs/codex-accounts.md +57 -16
  22. package/docs/rule-guides/question-presentation.md +4 -0
  23. package/hooks/blocking/final_reply_gate.py +59 -3
  24. package/hooks/blocking/open_tasks_transcript.py +18 -0
  25. package/hooks/blocking/test_final_reply_gate.py +160 -0
  26. package/hooks/features/conduct-gates.md +1 -4
  27. package/hooks/hooks.json +0 -10
  28. package/hooks/hooks_constants/final_reply_gate_constants.py +16 -0
  29. package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +9 -0
  30. package/package.json +1 -1
  31. package/rules/question-presentation.md +1 -1
  32. package/scripts/account_broker.py +50 -25
  33. package/scripts/account_broker_support.py +54 -3
  34. package/scripts/codex_account_choice.py +12 -8
  35. package/scripts/codex_account_chooser.py +110 -0
  36. package/scripts/codex_account_meters.py +43 -4
  37. package/scripts/codex_plan_rules.py +156 -0
  38. package/scripts/dev_env_scripts_constants/account_broker_constants.py +29 -42
  39. package/scripts/dev_env_scripts_constants/codex_account_constants.py +62 -2
  40. package/scripts/dev_env_scripts_constants/test_account_broker_constants.py +359 -2
  41. package/scripts/dev_env_scripts_constants/test_codex_account_constants.py +213 -14
  42. package/scripts/policy_lint/adapter_configuration.py +0 -5
  43. package/scripts/policy_lint/config/constants.py +0 -1
  44. package/scripts/test_account_broker.py +1756 -523
  45. package/scripts/test_account_broker_support.py +77 -0
  46. package/scripts/test_codex_account_meters.py +95 -0
  47. package/scripts/tests/test_adapter_configuration.py +0 -8
  48. package/hooks/blocking/merge_over_review_offer_gate.py +0 -114
  49. package/hooks/blocking/test_merge_over_review_offer_gate.py +0 -138
  50. package/hooks/hooks_constants/merge_over_review_offer_gate_constants.py +0 -34
@@ -14,6 +14,7 @@ from pathlib import Path
14
14
 
15
15
  import pytest
16
16
 
17
+ import codex_plan_rules
17
18
  from dev_env_scripts_constants import account_broker_constants as broker_constants
18
19
  from dev_env_scripts_constants import claude_account_constants, codex_account_constants, shared_tree_constants
19
20
  from dev_env_scripts_constants.account_broker_constants import (
@@ -40,6 +41,7 @@ from dev_env_scripts_constants.codex_account_constants import (
40
41
  )
41
42
 
42
43
  TEMP_TREE_MARKER = "marker-only-in-the-temp-install-tree"
44
+ FLOOR_TEST_PLAN_TYPE = "team"
43
45
 
44
46
 
45
47
  def test_should_keep_claude_floors_tied_to_the_account_ceilings() -> None:
@@ -226,10 +228,14 @@ def test_should_let_a_codex_account_at_the_short_window_floor_take_luna() -> Non
226
228
  weekly_reset = datetime(2026, 10, 4, tzinfo=timezone.utc)
227
229
  session_reset = weekly_reset + timedelta(hours=4)
228
230
  meters = broker_constants.Meters(
229
- LUNA_TIER_SHORT_WINDOW_MINIMUM_PERCENT_LEFT, session_reset, LUNA_TIER_STOP_PERCENT_LEFT / 2, weekly_reset
231
+ LUNA_TIER_SHORT_WINDOW_MINIMUM_PERCENT_LEFT,
232
+ session_reset,
233
+ LUNA_TIER_STOP_PERCENT_LEFT / 2,
234
+ weekly_reset,
235
+ FLOOR_TEST_PLAN_TYPE,
230
236
  )
231
237
 
232
- assert broker_constants._codex_tier(
238
+ assert codex_plan_rules.codex_tier(
233
239
  dataclasses.replace(meters, weekly_percent_left=NORMAL_TIER_MINIMUM_PERCENT_LEFT)
234
240
  ) == "luna"
235
241
  assert broker_constants._account_reset(_codex_reading(meters)) == weekly_reset
@@ -254,6 +260,357 @@ def test_should_name_the_latest_codex_reset_when_every_blocking_window_has_one()
254
260
  assert broker_constants._account_reset(_codex_reading(meters)) == weekly_reset
255
261
 
256
262
 
263
+ WEEKLY_FLOOR_SESSION_RESET = datetime(2026, 10, 3, 2, tzinfo=timezone.utc)
264
+ WEEKLY_FLOOR_WEEKLY_RESET = datetime(2026, 10, 4, tzinfo=timezone.utc)
265
+ ROOMY_SESSION_PERCENT_LEFT = LUNA_TIER_SHORT_WINDOW_MINIMUM_PERCENT_LEFT * 2
266
+
267
+
268
+ def _weekly_floor_meters(session_percent_left: float | None) -> broker_constants.Meters:
269
+ return broker_constants.Meters(
270
+ session_percent_left,
271
+ WEEKLY_FLOOR_SESSION_RESET if session_percent_left is not None else None,
272
+ LUNA_TIER_STOP_PERCENT_LEFT / 2,
273
+ WEEKLY_FLOOR_WEEKLY_RESET,
274
+ FLOOR_TEST_PLAN_TYPE,
275
+ )
276
+
277
+
278
+ def _disable_codex_weekly_floor(monkeypatch: pytest.MonkeyPatch, setting: str = "1") -> None:
279
+ monkeypatch.setenv(codex_account_constants.CODEX_WEEKLY_FLOOR_DISABLED_ENVIRONMENT_VARIABLE, setting)
280
+
281
+
282
+ def test_should_block_a_codex_account_at_the_weekly_floor_by_default() -> None:
283
+ meters = _weekly_floor_meters(ROOMY_SESSION_PERCENT_LEFT)
284
+
285
+ assert codex_plan_rules.codex_tier(meters) is None
286
+ assert broker_constants._account_reset(_codex_reading(meters)) == WEEKLY_FLOOR_WEEKLY_RESET
287
+
288
+
289
+ def test_should_let_a_codex_account_at_the_weekly_floor_take_luna_when_the_floor_is_disabled(
290
+ monkeypatch: pytest.MonkeyPatch,
291
+ ) -> None:
292
+ _disable_codex_weekly_floor(monkeypatch)
293
+ meters = _weekly_floor_meters(ROOMY_SESSION_PERCENT_LEFT)
294
+
295
+ assert codex_plan_rules.codex_tier(meters) == "luna"
296
+ assert broker_constants._account_reset(_codex_reading(meters)) is None
297
+
298
+
299
+ def test_should_let_a_codex_account_with_only_a_weekly_window_take_luna_when_the_floor_is_disabled(
300
+ monkeypatch: pytest.MonkeyPatch,
301
+ ) -> None:
302
+ _disable_codex_weekly_floor(monkeypatch)
303
+ meters = _weekly_floor_meters(None)
304
+
305
+ assert codex_plan_rules.codex_tier(meters) == "luna"
306
+ assert broker_constants._account_reset(_codex_reading(meters)) is None
307
+
308
+
309
+ def test_should_keep_the_session_floor_when_the_weekly_floor_is_disabled(monkeypatch: pytest.MonkeyPatch) -> None:
310
+ _disable_codex_weekly_floor(monkeypatch)
311
+ meters = _weekly_floor_meters(LUNA_TIER_SHORT_WINDOW_MINIMUM_PERCENT_LEFT / 2)
312
+
313
+ assert codex_plan_rules.codex_tier(meters) is None
314
+ assert broker_constants._account_reset(_codex_reading(meters)) == WEEKLY_FLOOR_SESSION_RESET
315
+
316
+
317
+ @pytest.mark.parametrize("setting", ("1", "true", "TRUE", " yes ", "on"))
318
+ def test_should_disable_the_codex_weekly_floor_for_every_truthy_setting(
319
+ monkeypatch: pytest.MonkeyPatch, setting: str
320
+ ) -> None:
321
+ _disable_codex_weekly_floor(monkeypatch, setting)
322
+
323
+ assert codex_plan_rules.codex_tier(_weekly_floor_meters(ROOMY_SESSION_PERCENT_LEFT)) == "luna"
324
+
325
+
326
+ @pytest.mark.parametrize("setting", ("", "0", "false", "off", "no", "disabled"))
327
+ def test_should_keep_the_codex_weekly_floor_for_every_other_setting(
328
+ monkeypatch: pytest.MonkeyPatch, setting: str
329
+ ) -> None:
330
+ _disable_codex_weekly_floor(monkeypatch, setting)
331
+
332
+ assert codex_plan_rules.codex_tier(_weekly_floor_meters(ROOMY_SESSION_PERCENT_LEFT)) is None
333
+
334
+
335
+ CREDIT_BALANCE = 12.5
336
+ PRO_PLAN_TYPE = "prolite"
337
+ FREE_PLAN_TYPE = "free"
338
+ TEAM_PLAN_TYPE = "team"
339
+ UNKNOWN_PLAN_TYPE = "unknown"
340
+ WAIT_NOW = datetime(2026, 10, 3, tzinfo=timezone.utc)
341
+
342
+
343
+ def _credit_meters(
344
+ session_percent_left: float | None,
345
+ *,
346
+ plan_type: str | None = PRO_PLAN_TYPE,
347
+ weekly_percent_left: float = LUNA_TIER_STOP_PERCENT_LEFT / 2,
348
+ has_credits: bool = True,
349
+ is_credits_unlimited: bool = False,
350
+ credit_balance: float | None = CREDIT_BALANCE,
351
+ ) -> broker_constants.Meters:
352
+ return dataclasses.replace(
353
+ _weekly_floor_meters(session_percent_left),
354
+ weekly_percent_left=weekly_percent_left,
355
+ plan_type=plan_type,
356
+ has_credits=has_credits,
357
+ is_credits_unlimited=is_credits_unlimited,
358
+ credit_balance=credit_balance,
359
+ )
360
+
361
+
362
+ @pytest.mark.parametrize(
363
+ ("has_credits", "is_unlimited", "balance", "is_spendable"),
364
+ (
365
+ (True, False, CREDIT_BALANCE, True),
366
+ (True, False, 0.0, False),
367
+ (True, False, -1.0, False),
368
+ (True, False, None, False),
369
+ (False, False, CREDIT_BALANCE, False),
370
+ (False, False, None, False),
371
+ (False, True, None, True),
372
+ (True, True, 0.0, True),
373
+ ),
374
+ )
375
+ def test_should_count_credits_as_spendable_only_with_a_balance_or_unlimited_use(
376
+ has_credits: bool, is_unlimited: bool, balance: float | None, is_spendable: bool
377
+ ) -> None:
378
+ meters = _credit_meters(None, has_credits=has_credits, is_credits_unlimited=is_unlimited, credit_balance=balance)
379
+
380
+ assert meters.has_spendable_credits is is_spendable
381
+
382
+
383
+ def test_should_report_no_credits_for_meters_built_without_them() -> None:
384
+ meters = broker_constants.Meters(80.0, None, 70.0, None, PRO_PLAN_TYPE)
385
+
386
+ assert meters.has_credits is False
387
+ assert meters.is_credits_unlimited is False
388
+ assert meters.credit_balance is None
389
+ assert meters.has_spendable_credits is False
390
+
391
+
392
+ @pytest.mark.parametrize(
393
+ "session_percent_left", (ROOMY_SESSION_PERCENT_LEFT, LUNA_TIER_SHORT_WINDOW_MINIMUM_PERCENT_LEFT / 2, 0.0, None)
394
+ )
395
+ def test_should_let_a_codex_account_with_credits_run_under_both_floors(session_percent_left: float | None) -> None:
396
+ meters = _credit_meters(session_percent_left)
397
+
398
+ assert codex_plan_rules.codex_tier(meters) == codex_account_constants.TIER_NORMAL
399
+ assert broker_constants._account_reset(_codex_reading(meters)) is None
400
+
401
+
402
+ def test_should_let_a_codex_account_with_unlimited_credits_run_under_both_floors() -> None:
403
+ meters = _credit_meters(0.0, has_credits=False, is_credits_unlimited=True, credit_balance=None)
404
+
405
+ assert codex_plan_rules.codex_tier(meters) == codex_account_constants.TIER_NORMAL
406
+ assert broker_constants._account_reset(_codex_reading(meters)) is None
407
+
408
+
409
+ def test_should_let_a_team_account_with_credits_run_under_both_floors() -> None:
410
+ meters = _credit_meters(0.0, plan_type=TEAM_PLAN_TYPE)
411
+
412
+ assert codex_plan_rules.codex_tier(meters) == codex_account_constants.TIER_NORMAL
413
+
414
+
415
+ def test_should_keep_both_floors_for_a_non_pro_codex_account_with_a_zero_credit_balance() -> None:
416
+ meters = _credit_meters(ROOMY_SESSION_PERCENT_LEFT, plan_type=TEAM_PLAN_TYPE, credit_balance=0.0)
417
+
418
+ assert codex_plan_rules.codex_tier(meters) is None
419
+ assert broker_constants._account_reset(_codex_reading(meters)) == WEEKLY_FLOOR_WEEKLY_RESET
420
+
421
+
422
+ def test_should_let_a_pro_codex_account_with_a_zero_credit_balance_and_plan_room_take_luna() -> None:
423
+ meters = _credit_meters(ROOMY_SESSION_PERCENT_LEFT, credit_balance=0.0)
424
+
425
+ assert codex_plan_rules.codex_tier(meters) == codex_account_constants.TIER_LUNA
426
+ assert broker_constants._account_reset(_codex_reading(meters)) is None
427
+
428
+
429
+ @pytest.mark.parametrize("plan_type", (FREE_PLAN_TYPE, "Free", " FREE "))
430
+ def test_should_never_give_a_free_codex_account_a_tier(plan_type: str) -> None:
431
+ roomy_meters = _credit_meters(
432
+ ROOMY_SESSION_PERCENT_LEFT, plan_type=plan_type, weekly_percent_left=codex_account_constants.FULL_PERCENT, has_credits=False, credit_balance=None
433
+ )
434
+
435
+ assert codex_plan_rules.codex_tier(roomy_meters) is None
436
+ assert codex_plan_rules.codex_tier(dataclasses.replace(roomy_meters, has_credits=True, credit_balance=CREDIT_BALANCE)) is None
437
+ assert codex_plan_rules.codex_tier(dataclasses.replace(roomy_meters, is_credits_unlimited=True)) is None
438
+
439
+
440
+ def test_should_never_give_a_free_codex_account_a_tier_whatever_the_floors(monkeypatch: pytest.MonkeyPatch) -> None:
441
+ _disable_codex_weekly_floor(monkeypatch)
442
+ meters = _credit_meters(ROOMY_SESSION_PERCENT_LEFT, plan_type=FREE_PLAN_TYPE, has_credits=False, credit_balance=None)
443
+
444
+ assert codex_plan_rules.codex_tier(meters) is None
445
+
446
+
447
+ @pytest.mark.parametrize("plan_type", (None, "unknown", "Unknown", " UNKNOWN ", ""))
448
+ def test_should_never_give_a_codex_account_with_no_known_plan_a_tier(plan_type: str | None) -> None:
449
+ roomy_meters = _credit_meters(
450
+ ROOMY_SESSION_PERCENT_LEFT, plan_type=plan_type, weekly_percent_left=codex_account_constants.FULL_PERCENT, has_credits=False, credit_balance=None
451
+ )
452
+
453
+ assert codex_plan_rules.codex_tier(roomy_meters) is None
454
+ assert codex_plan_rules.codex_tier(dataclasses.replace(roomy_meters, has_credits=True, credit_balance=CREDIT_BALANCE)) is None
455
+ assert codex_plan_rules.codex_tier(dataclasses.replace(roomy_meters, is_credits_unlimited=True)) is None
456
+
457
+
458
+ def test_should_never_give_a_codex_account_with_no_known_plan_a_tier_whatever_the_floors(monkeypatch: pytest.MonkeyPatch) -> None:
459
+ _disable_codex_weekly_floor(monkeypatch)
460
+ meters = _credit_meters(ROOMY_SESSION_PERCENT_LEFT, plan_type=UNKNOWN_PLAN_TYPE, has_credits=False, credit_balance=None)
461
+
462
+ assert codex_plan_rules.codex_tier(meters) is None
463
+
464
+
465
+ UNRECOGNISED_PLAN_TYPES = ("free_works", "Free_Works", " free_works ", "free trial", "pro_trial", "teams", "freemium", "proo")
466
+
467
+
468
+ @pytest.mark.parametrize("plan_type", UNRECOGNISED_PLAN_TYPES)
469
+ def test_should_never_give_a_codex_account_on_an_unrecognised_plan_a_tier(plan_type: str) -> None:
470
+ roomy_meters = _credit_meters(
471
+ ROOMY_SESSION_PERCENT_LEFT, plan_type=plan_type, weekly_percent_left=codex_account_constants.FULL_PERCENT, has_credits=False, credit_balance=None
472
+ )
473
+
474
+ assert codex_plan_rules.codex_tier(roomy_meters) is None
475
+ assert codex_plan_rules.codex_tier(dataclasses.replace(roomy_meters, has_credits=True, credit_balance=CREDIT_BALANCE)) is None
476
+ assert codex_plan_rules.codex_tier(dataclasses.replace(roomy_meters, is_credits_unlimited=True)) is None
477
+
478
+
479
+ @pytest.mark.parametrize("plan_type", UNRECOGNISED_PLAN_TYPES)
480
+ def test_should_never_give_a_codex_account_on_an_unrecognised_plan_a_tier_whatever_the_floors_or_credits(
481
+ monkeypatch: pytest.MonkeyPatch, plan_type: str
482
+ ) -> None:
483
+ _disable_codex_weekly_floor(monkeypatch)
484
+ meters = _credit_meters(0.0, plan_type=plan_type, weekly_percent_left=0.0, is_credits_unlimited=True)
485
+
486
+ assert codex_plan_rules.codex_tier(meters) is None
487
+
488
+
489
+ @pytest.mark.parametrize("plan_type", UNRECOGNISED_PLAN_TYPES)
490
+ def test_should_name_the_missing_plan_when_a_codex_account_is_on_an_unrecognised_plan(plan_type: str) -> None:
491
+ meters = _credit_meters(ROOMY_SESSION_PERCENT_LEFT, plan_type=plan_type)
492
+
493
+ assert codex_plan_rules.is_refused_plan(meters) is True
494
+ assert codex_plan_rules.refusal_text("editor", meters) == "editor reports no known plan and is never chosen"
495
+
496
+
497
+ @pytest.mark.parametrize(
498
+ "plan_type", ("pro", "prolite", "promax", "team", "plus", "business", "enterprise", "edu", "Team", " PRO ")
499
+ )
500
+ def test_should_not_refuse_a_codex_account_on_a_listed_paid_plan(plan_type: str) -> None:
501
+ meters = _credit_meters(ROOMY_SESSION_PERCENT_LEFT, plan_type=plan_type)
502
+
503
+ assert codex_plan_rules.is_refused_plan(meters) is False
504
+ assert codex_plan_rules.refusal_text("editor", meters) is None
505
+ assert codex_plan_rules.codex_tier(meters) == codex_account_constants.TIER_NORMAL
506
+
507
+
508
+ def test_should_not_refuse_a_codex_account_whose_meters_could_not_be_read() -> None:
509
+ assert codex_plan_rules.refusal_text("unread", None) is None
510
+
511
+
512
+ @pytest.mark.parametrize(
513
+ ("plan_type", "expected_text"),
514
+ (
515
+ (FREE_PLAN_TYPE, "editor is on a free plan and is never chosen"),
516
+ (None, "editor reports no known plan and is never chosen"),
517
+ (UNKNOWN_PLAN_TYPE, "editor reports no known plan and is never chosen"),
518
+ (TEAM_PLAN_TYPE, None),
519
+ (PRO_PLAN_TYPE, None),
520
+ ),
521
+ )
522
+ def test_should_name_why_a_codex_plan_is_refused(plan_type: str | None, expected_text: str | None) -> None:
523
+ meters = _credit_meters(ROOMY_SESSION_PERCENT_LEFT, plan_type=plan_type)
524
+
525
+ assert codex_plan_rules.refusal_text("editor", meters) == expected_text
526
+
527
+
528
+ def test_should_name_the_free_plan_when_a_free_codex_account_is_the_only_account() -> None:
529
+ meters = _credit_meters(
530
+ ROOMY_SESSION_PERCENT_LEFT, plan_type=FREE_PLAN_TYPE, weekly_percent_left=codex_account_constants.FULL_PERCENT, has_credits=False, credit_balance=None
531
+ )
532
+ reading = broker_constants.Reading(Account(Product.CODEX, "editor", Path("/profiles/editor")), meters)
533
+
534
+ decision = broker_constants._wait_decision([reading], WAIT_NOW, frozenset(), {})
535
+
536
+ assert decision.action == "wait"
537
+ assert decision.reason == (
538
+ "no account has room (editor is on a free plan and is never chosen); "
539
+ "next check at 2026-10-03T01:00:00+00:00"
540
+ )
541
+
542
+
543
+ def test_should_not_wait_for_a_free_codex_account_to_reset() -> None:
544
+ free_meters = _credit_meters(
545
+ ROOMY_SESSION_PERCENT_LEFT, plan_type=FREE_PLAN_TYPE, weekly_percent_left=codex_account_constants.FULL_PERCENT, has_credits=False, credit_balance=None
546
+ )
547
+ team_meters = _credit_meters(None, plan_type=TEAM_PLAN_TYPE, has_credits=False, credit_balance=None)
548
+ all_readings = [
549
+ broker_constants.Reading(Account(Product.CODEX, "editor", Path("/profiles/editor")), free_meters),
550
+ broker_constants.Reading(Account(Product.CODEX, "team", Path("/profiles/team")), team_meters),
551
+ ]
552
+
553
+ decision = broker_constants._wait_decision(all_readings, WAIT_NOW, frozenset(), {})
554
+
555
+ assert decision.resets_at == WEEKLY_FLOOR_WEEKLY_RESET
556
+ assert decision.reason == (
557
+ "no account has room (editor is on a free plan and is never chosen, team 0.5% left); "
558
+ "next reset at 2026-10-04T00:00:00+00:00"
559
+ )
560
+
561
+
562
+ @pytest.mark.parametrize(
563
+ ("session_percent_left", "weekly_percent_left"),
564
+ (
565
+ (LUNA_TIER_SHORT_WINDOW_MINIMUM_PERCENT_LEFT / 4, ROOMY_SESSION_PERCENT_LEFT * 2),
566
+ (None, LUNA_TIER_STOP_PERCENT_LEFT / 2),
567
+ (LUNA_TIER_STOP_PERCENT_LEFT, LUNA_TIER_STOP_PERCENT_LEFT),
568
+ ),
569
+ )
570
+ def test_should_let_a_pro_codex_account_with_plan_room_in_both_windows_take_luna(
571
+ session_percent_left: float | None, weekly_percent_left: float
572
+ ) -> None:
573
+ meters = _credit_meters(
574
+ session_percent_left, weekly_percent_left=weekly_percent_left, has_credits=False, credit_balance=None
575
+ )
576
+
577
+ assert codex_plan_rules.codex_blocking_flags(meters) == (False, False)
578
+ assert codex_plan_rules.codex_tier(meters) == codex_account_constants.TIER_LUNA
579
+ assert broker_constants._account_reset(_codex_reading(meters)) is None
580
+
581
+
582
+ @pytest.mark.parametrize(
583
+ ("session_percent_left", "weekly_percent_left", "expected_flags"),
584
+ (
585
+ (codex_account_constants.PRO_PLAN_STOP_PERCENT_LEFT, ROOMY_SESSION_PERCENT_LEFT, (True, False)),
586
+ (ROOMY_SESSION_PERCENT_LEFT, codex_account_constants.PRO_PLAN_STOP_PERCENT_LEFT, (False, True)),
587
+ (codex_account_constants.PRO_PLAN_STOP_PERCENT_LEFT, codex_account_constants.PRO_PLAN_STOP_PERCENT_LEFT, (True, True)),
588
+ ),
589
+ )
590
+ def test_should_block_a_pro_codex_account_only_when_a_window_has_no_room_left(
591
+ session_percent_left: float, weekly_percent_left: float, expected_flags: tuple[bool, bool]
592
+ ) -> None:
593
+ meters = _credit_meters(
594
+ session_percent_left, weekly_percent_left=weekly_percent_left, has_credits=False, credit_balance=None
595
+ )
596
+
597
+ assert codex_plan_rules.codex_blocking_flags(meters) == expected_flags
598
+ assert codex_plan_rules.codex_tier(meters) is None
599
+
600
+
601
+ def test_should_keep_the_short_window_and_weekly_floors_for_a_team_codex_account_with_pro_room() -> None:
602
+ meters = _credit_meters(
603
+ LUNA_TIER_SHORT_WINDOW_MINIMUM_PERCENT_LEFT / 4,
604
+ plan_type=TEAM_PLAN_TYPE,
605
+ weekly_percent_left=ROOMY_SESSION_PERCENT_LEFT * 2,
606
+ has_credits=False,
607
+ credit_balance=None,
608
+ )
609
+
610
+ assert codex_plan_rules.codex_blocking_flags(meters) == (True, False)
611
+ assert codex_plan_rules.codex_tier(meters) is None
612
+
613
+
257
614
  def test_should_double_the_meter_read_delay_up_to_its_cap() -> None:
258
615
  all_delays = [broker_constants.meter_read_delay_seconds(each_attempt) for each_attempt in range(1, 6)]
259
616
 
@@ -53,9 +53,14 @@ def documented_percent(percent: float) -> str:
53
53
 
54
54
  def test_fallback_roster_matches_the_documented_accounts() -> None:
55
55
  expected_sentence = (
56
- "When neither exists, the accounts are "
56
+ "When neither exists, the roster is empty. "
57
+ "The broker's `choose` and `check` then read the default Codex home "
58
+ f"as the account `{constants.NO_ROSTER_ACCOUNT_NAME}`, and `install` has no accounts. "
59
+ "`sync` falls back to "
57
60
  + documented_list(list(constants.ALL_CODEX_ACCOUNT_NAMES))
58
- + "."
61
+ + " when no list names an account. "
62
+ f"`choose` and `check` fall back to them only when `{constants.CODEX_ACCOUNT_PROFILES_ENVIRONMENT_VARIABLE}` "
63
+ f"holds only separators, such as `{constants.CODEX_ACCOUNT_NAME_SEPARATOR}`, and no saved list names an account."
59
64
  )
60
65
  assert expected_sentence in documented_section("Names and order")
61
66
 
@@ -87,29 +92,223 @@ def test_tier_bars_match_the_documented_table() -> None:
87
92
  luna_stop = documented_percent(constants.LUNA_TIER_STOP_PERCENT_LEFT)
88
93
  luna_short_window_floor = documented_percent(constants.LUNA_TIER_SHORT_WINDOW_MINIMUM_PERCENT_LEFT)
89
94
  pro_plans = documented_list(sorted(constants.ALL_PRO_PLAN_TYPES))
90
- pro_preference = (
91
- f"on a Pro plan account ({pro_plans}) before any other account in this tier; "
92
- "among accounts of the same kind, the one with the most room"
95
+ other_paid_account = "an account on another paid plan"
96
+ no_pro_account = f"No Pro plan account reaches `{constants.TIER_NORMAL}` or `{constants.TIER_LUNA}`"
97
+ most_room_pro_account = "on the Pro account with the most room, before any account on another plan"
98
+ free_plans = documented_list(sorted(constants.ALL_FREE_PLAN_TYPES))
99
+ paid_plans = documented_list(sorted(constants.ALL_PAID_PLAN_TYPES))
100
+ pro_stop = documented_percent(constants.PRO_PLAN_STOP_PERCENT_LEFT)
101
+ credits_condition = (
102
+ f"`{constants.HAS_CREDITS_KEY}` is `true` with a balance above zero, "
103
+ f"or `{constants.UNLIMITED_CREDITS_KEY}` is `true`"
104
+ )
105
+ other_paid_luna_condition = (
106
+ f"with more than {luna_stop} of its week left and at least {luna_short_window_floor} left "
107
+ "in any window shorter than a week"
108
+ )
109
+ credits_answer = (
110
+ f"`{constants.TIER_NORMAL}`, even at or under {luna_stop} of its week left or under {luna_short_window_floor} "
111
+ f"of a short window; the rows above that give `{constants.TIER_LUNA}` count only accounts without credits; "
112
+ "a Pro plan account with credits comes before every account on another plan; "
113
+ "among accounts of the same kind, the one with the most room, so plan usage comes before credits alone"
114
+ )
115
+ same_group_condition = (
116
+ "Within one plan group (the Pro plan accounts, or the accounts on another paid plan), "
117
+ f"one account reaches `{constants.TIER_NORMAL}` and another reaches only `{constants.TIER_LUNA}`"
93
118
  )
94
119
  all_expected_rows = [
95
- f"| One or more accounts with more than {normal_bar} left | `{constants.TIER_NORMAL}` {pro_preference} |",
96
- f"| No account over {normal_bar}, one or more over {luna_stop} | `{constants.TIER_LUNA}` {pro_preference} |",
97
- f"| An account that reports a window shorter than a week, such as the 5-hour window, with under {luna_short_window_floor} of that window left | never takes `{constants.TIER_LUNA}` |",
98
- f"| No account over {luna_stop} | `{constants.TIER_WAIT}`, naming the next reset when known |",
120
+ f"| A Pro plan account ({pro_plans}) with more than {normal_bar} left | `{constants.TIER_NORMAL}` {most_room_pro_account} |",
121
+ (
122
+ f"| No such Pro account, and a Pro plan account with more than {pro_stop} left in each window | `{constants.TIER_LUNA}` "
123
+ f"{most_room_pro_account}, even one with more than {normal_bar} left |"
124
+ ),
125
+ f"| {no_pro_account}, and {other_paid_account} with more than {normal_bar} left | `{constants.TIER_NORMAL}` on the account with the most room |",
126
+ (
127
+ f"| {no_pro_account}, no other account over {normal_bar}, and {other_paid_account} "
128
+ f"{other_paid_luna_condition} | `{constants.TIER_LUNA}` on the account with the most room |"
129
+ ),
130
+ f"| {same_group_condition} | the `{constants.TIER_NORMAL}` account, whatever the room of each |",
131
+ f"| An account on a free plan ({free_plans}) | never chosen, whatever its room or credits; a `{constants.TIER_WAIT}` reason names it |",
132
+ f"| An account whose plan is missing, blank or not one of the paid plans, which are {paid_plans} | never chosen, whatever its room or credits; a `{constants.TIER_WAIT}` reason names it |",
133
+ (
134
+ f"| An account with credits ({credits_condition}) and {normal_bar} or less left "
135
+ f"| {credits_answer} |"
136
+ ),
137
+ f"| An account on another paid plan that reports a window shorter than a week, such as the 5-hour window, with under {luna_short_window_floor} of that window left | never takes `{constants.TIER_LUNA}` |",
138
+ (
139
+ f"| No account can run: no Pro plan account with more than {pro_stop} left in each window, "
140
+ f"no account on another paid plan with over {normal_bar} left or {other_paid_luna_condition}, "
141
+ f"and no account with credits | `{constants.TIER_WAIT}`, naming the next reset when known |"
142
+ ),
99
143
  ]
100
144
  section_text = documented_section("Which account a job uses")
101
145
  for each_row in all_expected_rows:
102
146
  assert each_row in section_text
103
147
 
104
148
 
149
+ def test_tier_requirement_matches_the_documented_paragraph() -> None:
150
+ required_tier = f"`{constants.REQUIRE_TIER_ARGUMENT} {constants.TIER_NORMAL}`"
151
+ all_expected_sentences = [
152
+ f"A caller that needs the `{constants.TIER_NORMAL}` tier passes {required_tier}.",
153
+ (
154
+ f"The broker then chooses a Pro plan account only when it reaches `{constants.TIER_NORMAL}`, "
155
+ "and otherwise the account on another paid plan with the most room that does."
156
+ ),
157
+ (
158
+ f"A caller that passes no tier takes a Pro plan account first, even on `{constants.TIER_LUNA}`, "
159
+ "before an account on another plan with more room."
160
+ ),
161
+ ]
162
+ section_text = documented_section("Which account a job uses")
163
+ for each_sentence in all_expected_sentences:
164
+ assert each_sentence in section_text
165
+
166
+
167
+ def test_default_home_rule_matches_the_documented_paragraph() -> None:
168
+ default_home = f"`{constants.CODEX_HOME_ENVIRONMENT_VARIABLE}` or `~/{constants.MAIN_CODEX_HOME_DIRECTORY_NAME}`"
169
+ floor_switch = f"`{constants.CODEX_WEEKLY_FLOOR_DISABLED_ENVIRONMENT_VARIABLE}`"
170
+ all_expected_sentences = [
171
+ (
172
+ f"With no roster configured, `choose` and `check` read the default Codex home, {default_home}, "
173
+ f"as the account `{constants.NO_ROSTER_ACCOUNT_NAME}` and apply the same rules."
174
+ ),
175
+ f"A `{constants.CODEX_HOME_ENVIRONMENT_VARIABLE}` that is empty or only whitespace counts as unset.",
176
+ f"A failed read gives `{constants.TIER_WAIT}` and exit 3, and the reason names the failure.",
177
+ (
178
+ "`--home <path>` judges that Codex home alone. A home on a paid plan with readable meters runs, "
179
+ f"whatever its room, with or without credits, and whatever {floor_switch} says."
180
+ ),
181
+ f"It reports the tier its meters allow, or `{constants.ALL_TIERS_BEST_FIRST[-1]}` when no tier allows it.",
182
+ (
183
+ "A free, missing, blank, unknown or unlisted plan, or a home whose meters are unread, "
184
+ f"still gives `{constants.TIER_WAIT}`."
185
+ ),
186
+ (
187
+ f"`{constants.REQUIRE_TIER_ARGUMENT} {constants.TIER_NORMAL}` still applies, "
188
+ f"so a home that does not reach `{constants.TIER_NORMAL}` gives `{constants.TIER_WAIT}`."
189
+ ),
190
+ "`check --home` answers as `choose --home` does.",
191
+ (
192
+ "A spent mark saved for the roster account that has this home, by `run` or by `choose --spent`, "
193
+ f"still gives `{constants.TIER_WAIT}` until its reset on a paid plan, unless that account has spendable credits."
194
+ ),
195
+ (
196
+ "A mark is keyed by product, account name and home, and `--home` reads a home the roster does not list "
197
+ "as an account named for its folder. "
198
+ f"So a mark saved for the no-roster default account, named `{constants.NO_ROSTER_ACCOUNT_NAME}`, "
199
+ "does not hold `--home <that home>` unless the folder of that home is also named "
200
+ f"`{constants.NO_ROSTER_ACCOUNT_NAME}`, and a mark saved for an account outside the roster never holds a `--home` answer."
201
+ ),
202
+ (
203
+ "After an account leaves the roster, a mark saved while it was in the roster still holds "
204
+ "`--home <its home>` only when the account's name equals the name of its resolved home folder and the account has no spendable credits. "
205
+ "A roster account `Alpha` whose home folder is `alpha`, or whose home folder is a junction "
206
+ "to a folder with another name, stops holding `--home <its home>` once it leaves the roster."
207
+ ),
208
+ "The broker refuses `--home` for the Claude product and with `--spent`.",
209
+ ]
210
+ section_text = documented_section("Which account a job uses")
211
+ for each_sentence in all_expected_sentences:
212
+ assert each_sentence in section_text
213
+
214
+
215
+ def test_shared_callers_match_the_documented_answers() -> None:
216
+ all_expected_sentences = [
217
+ (
218
+ "Runs each live `codex exec` with `CODEX_HOME` set to `decision.home`. "
219
+ f"With `CODEX_HOME` unset or blank, it accepts only `{constants.TIER_NORMAL}`. "
220
+ "With a non-blank `CODEX_HOME`, it asks the broker to judge that home "
221
+ f"and accepts `{constants.TIER_NORMAL}` or `{constants.TIER_LUNA}`, "
222
+ "and the run uses `decision.home`, the trimmed absolute folder the broker judged. "
223
+ f"`{constants.TIER_WAIT}`, a tier it does not accept, or a failed broker records `infra_error` "
224
+ "with the failure `no_codex_account` and starts no model call"
225
+ ),
226
+ (
227
+ f"A `{constants.TIER_WAIT}` answer or a tier it does not accept returns a `declined` fallback "
228
+ "with the broker's reason and starts no Codex process."
229
+ ),
230
+ (
231
+ "A broker that times out, cannot start, exits with a code other than 0 or 3, prints malformed JSON, "
232
+ f"or answers `{constants.TIER_NORMAL}` or `{constants.TIER_LUNA}` with no home or no meters "
233
+ "returns a `broken` fallback and starts no Codex process"
234
+ ),
235
+ ]
236
+ section_text = documented_section("Shared callers")
237
+ for each_sentence in all_expected_sentences:
238
+ assert each_sentence in section_text
239
+
240
+
241
+ def test_free_plan_types_stay_apart_from_paid_plan_types() -> None:
242
+ assert constants.ALL_FREE_PLAN_TYPES
243
+ assert constants.ALL_FREE_PLAN_TYPES.isdisjoint(constants.ALL_PAID_PLAN_TYPES)
244
+ assert constants.ALL_FREE_PLAN_TYPES.isdisjoint(constants.ALL_PRO_PLAN_TYPES)
245
+
246
+
247
+ def test_paid_plan_types_hold_every_pro_plan_and_the_other_paid_plans() -> None:
248
+ all_other_paid_plans = {"team", "plus", "business", "enterprise", "edu"}
249
+
250
+ assert constants.ALL_PRO_PLAN_TYPES <= constants.ALL_PAID_PLAN_TYPES
251
+ assert constants.ALL_PAID_PLAN_TYPES == constants.ALL_PRO_PLAN_TYPES | all_other_paid_plans
252
+
253
+
254
+ def test_paid_plan_types_are_written_as_the_broker_compares_them() -> None:
255
+ assert all(each_plan == each_plan.strip().casefold() and each_plan for each_plan in constants.ALL_PAID_PLAN_TYPES)
256
+
257
+
258
+ def test_weekly_floor_switch_matches_the_documented_paragraph() -> None:
259
+ switch_name = constants.CODEX_WEEKLY_FLOOR_DISABLED_ENVIRONMENT_VARIABLE
260
+ truthy_values = documented_list(sorted(constants.ALL_CODEX_WEEKLY_FLOOR_DISABLED_TRUTHY_VALUES))
261
+ luna_stop = documented_percent(constants.LUNA_TIER_STOP_PERCENT_LEFT)
262
+ luna_short_window_floor = documented_percent(constants.LUNA_TIER_SHORT_WINDOW_MINIMUM_PERCENT_LEFT)
263
+ pro_stop = documented_percent(constants.PRO_PLAN_STOP_PERCENT_LEFT)
264
+ all_expected_sentences = [
265
+ (
266
+ f"Set `{switch_name}` to lift the {luna_stop} weekly stop for an account on a paid plan other than a Pro plan. "
267
+ f"The values that turn it on are {truthy_values}."
268
+ ),
269
+ (
270
+ f"While it is on, such an account without credits that has {luna_stop} or less of its week left "
271
+ f"can take `{constants.TIER_LUNA}`, and `choose` and `check` do not wait for its weekly reset."
272
+ ),
273
+ f"The {luna_short_window_floor} short-window floor still applies to it.",
274
+ (
275
+ "The switch changes nothing for a Pro plan account. "
276
+ f"Without credits, a Pro plan account takes `{constants.TIER_LUNA}` with more than {pro_stop} left "
277
+ f"in each window and waits at {pro_stop} in either window, whether the switch is on or off."
278
+ ),
279
+ (
280
+ "An account with credits already runs under both floors, "
281
+ "so the switch matters only for an account on another paid plan without credits."
282
+ ),
283
+ ]
284
+ section_text = documented_section("Which account a job uses")
285
+ for each_sentence in all_expected_sentences:
286
+ assert each_sentence in section_text
287
+
288
+
105
289
  def test_meter_retries_match_the_documented_table() -> None:
106
290
  all_delays = [meter_read_delay_seconds(each_attempt) for each_attempt in range(1, METER_READ_ATTEMPTS)]
107
291
  all_delay_texts = [f"{each_delay:g} second" + ("" if each_delay == 1 else "s") for each_delay in all_delays]
108
- expected_row = (
109
- f"| A meter read fails | The broker reads it again, up to {METER_READ_ATTEMPTS} reads, "
110
- f"waiting {' and then '.join(all_delay_texts)} |"
111
- )
112
- assert expected_row in documented_section("Which account a job uses")
292
+ all_expected_rows = [
293
+ (
294
+ "| A meter read finds no Codex executable, or fails with text that holds `HTTP Error 429` "
295
+ "| The broker makes that 1 read and does not retry. "
296
+ "A 429 text sets no rate-limit cooldown for a Codex account |"
297
+ ),
298
+ (
299
+ "| A meter read fails with a `CodexMeterUnreadError`, `OSError`, `ValueError`, `RuntimeError` "
300
+ "or `subprocess.SubprocessError` that the row above does not cover "
301
+ f"| The broker reads it again, up to {METER_READ_ATTEMPTS} reads, "
302
+ f"waiting {' and then '.join(all_delay_texts)} |"
303
+ ),
304
+ (
305
+ "| A meter read raises any other exception type "
306
+ "| The broker does not catch it, so the command ends with that exception after 1 read |"
307
+ ),
308
+ ]
309
+ section_text = documented_section("Which account a job uses")
310
+ for each_row in all_expected_rows:
311
+ assert each_row in section_text
113
312
 
114
313
 
115
314
  def test_documented_choose_sample_runs_normal_on_the_tightest_window() -> None:
@@ -139,7 +139,6 @@ def _names_exempt_registration_path(registered_string: str) -> bool:
139
139
  hooks/blocking/artifact_dark_mode_gate.py -> exempt
140
140
  hooks/blocking/send_user_file_worktree_root_gate.py -> exempt
141
141
  hooks/blocking/build_eval_before_eval_write_gate.py -> exempt
142
- hooks/blocking/merge_over_review_offer_gate.py -> exempt
143
142
  hooks/blocking/final_reply_gate.py -> exempt
144
143
  hooks/blocking/open_tasks_stop_gate.py -> exempt
145
144
  hooks/blocking/owned_pr_merge_stop_gate.py -> exempt
@@ -192,10 +191,6 @@ def _names_exempt_registration_path(registered_string: str) -> bool:
192
191
  invokes ``/claude-api build-eval``. It holds eval writes until a skill
193
192
  loads, and it judges no code or safety content.
194
193
 
195
- The merge offer gate denies a decision card whose text offers a merge and
196
- names open review findings. It reads only the wording of a chat card and
197
- decides no code or safety policy; the merge review gate holds the merge.
198
-
199
194
  The open tasks Stop gate holds the end of a turn once when the turn worked
200
195
  on an ask and made no task list call. It decides when a turn may end and
201
196
  no code or safety policy.
@@ -78,7 +78,6 @@ ALL_ACTION_BOUNDARY_EXEMPT_REGISTRATION_PATHS = (
78
78
  "blocking/send_user_file_worktree_root_gate.py",
79
79
  "blocking/haiku_spawn_slim_gate.py",
80
80
  "blocking/build_eval_before_eval_write_gate.py",
81
- "blocking/merge_over_review_offer_gate.py",
82
81
  "blocking/final_reply_gate.py",
83
82
  "blocking/open_tasks_stop_gate.py",
84
83
  "blocking/owned_pr_merge_stop_gate.py",