claude-dev-env 8.111.4 → 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 (51) 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 +6 -4
  21. package/docs/codex-accounts.md +57 -16
  22. package/docs/rule-guides/question-presentation.md +4 -0
  23. package/docs/second-claude-account.md +1 -1
  24. package/hooks/blocking/final_reply_gate.py +59 -3
  25. package/hooks/blocking/open_tasks_transcript.py +18 -0
  26. package/hooks/blocking/test_final_reply_gate.py +160 -0
  27. package/hooks/features/conduct-gates.md +1 -4
  28. package/hooks/hooks.json +0 -10
  29. package/hooks/hooks_constants/final_reply_gate_constants.py +16 -0
  30. package/hooks/hooks_constants/open_tasks_stop_gate_constants.py +9 -0
  31. package/package.json +1 -1
  32. package/rules/question-presentation.md +1 -1
  33. package/scripts/account_broker.py +122 -27
  34. package/scripts/account_broker_support.py +54 -3
  35. package/scripts/codex_account_choice.py +12 -8
  36. package/scripts/codex_account_chooser.py +110 -0
  37. package/scripts/codex_account_meters.py +43 -4
  38. package/scripts/codex_plan_rules.py +156 -0
  39. package/scripts/dev_env_scripts_constants/account_broker_constants.py +41 -42
  40. package/scripts/dev_env_scripts_constants/codex_account_constants.py +62 -2
  41. package/scripts/dev_env_scripts_constants/test_account_broker_constants.py +359 -2
  42. package/scripts/dev_env_scripts_constants/test_codex_account_constants.py +213 -14
  43. package/scripts/policy_lint/adapter_configuration.py +0 -5
  44. package/scripts/policy_lint/config/constants.py +0 -1
  45. package/scripts/test_account_broker.py +1913 -526
  46. package/scripts/test_account_broker_support.py +77 -0
  47. package/scripts/test_codex_account_meters.py +95 -0
  48. package/scripts/tests/test_adapter_configuration.py +0 -8
  49. package/hooks/blocking/merge_over_review_offer_gate.py +0 -114
  50. package/hooks/blocking/test_merge_over_review_offer_gate.py +0 -138
  51. package/hooks/hooks_constants/merge_over_review_offer_gate_constants.py +0 -34
@@ -1,4 +1,16 @@
1
- """Ask the account broker for a Codex account before an Astra advisor request."""
1
+ """Ask the account broker for a Codex account before an Astra advisor request.
2
+
3
+ ::
4
+
5
+ no home exported, or a blank one -> choose --product codex --require-tier normal
6
+ paid home exported, whatever its room -> choose --product codex --home <home>, runs on that home
7
+ free, unknown, unlisted or unreadable
8
+ home exported -> declined, nothing launches
9
+
10
+ An exported home keeps its account, in the roster or not, on the ``normal`` or
11
+ ``luna`` tier, once the broker confirms its plan, whatever its room. The broker's
12
+ reason names the account.
13
+ """
2
14
 
3
15
  from __future__ import annotations
4
16
 
@@ -12,15 +24,19 @@ from pathlib import Path
12
24
 
13
25
  from advisor_scripts_constants.astra_advisor_constants import (
14
26
  ACCOUNT_BROKER_CHOOSE_COMMAND,
27
+ ACCOUNT_BROKER_HOME_FLAG,
28
+ ALL_ACCOUNT_BROKER_REQUIRE_TIER_ARGUMENTS,
15
29
  ASTRA_ACCOUNT_PICK_TIMEOUT_REASON,
30
+ ALL_ASTRA_EXPORTED_HOME_TIERS,
31
+ ALL_ASTRA_NORMAL_ONLY_TIERS,
16
32
  ALL_BROKER_ACCEPTED_EXIT_CODES,
17
33
  ALL_BROKER_METER_PERCENT_KEYS,
18
34
  ASTRA_ACCOUNT_PICK_TIMEOUT_SECONDS,
35
+ ASTRA_EXPORTED_HOME_REFUSED_REASON_TEMPLATE,
19
36
  BROKER_WAIT_EXIT_CODE,
20
37
  ASTRA_FALLBACK_KIND_BROKEN,
21
38
  ASTRA_FALLBACK_KIND_DECLINED,
22
39
  ASTRA_PREFLIGHT_FAILURE_REASON,
23
- CODEX_TIER_NORMAL,
24
40
  )
25
41
 
26
42
 
@@ -51,13 +67,30 @@ def _finite_percent(raw_percent: object) -> float | None:
51
67
  return float(raw_percent) if math.isfinite(raw_percent) else None
52
68
 
53
69
 
54
- def _preflight_from_answer(field_by_name: dict[str, object]) -> AstraPreflight:
70
+ def _accepted_tiers(exported_home: str | None) -> tuple[str, ...]:
71
+ if exported_home is None:
72
+ return ALL_ASTRA_NORMAL_ONLY_TIERS
73
+ return ALL_ASTRA_EXPORTED_HOME_TIERS
74
+
75
+
76
+ def _declined(broker_reason: object, exported_home: str | None) -> AstraPreflight:
77
+ if exported_home is None:
78
+ reason = f"{ASTRA_PREFLIGHT_FAILURE_REASON}: no Codex account has room ({broker_reason})"
79
+ else:
80
+ reason = ASTRA_EXPORTED_HOME_REFUSED_REASON_TEMPLATE.format(
81
+ home=exported_home, reason=broker_reason
82
+ )
83
+ return _preflight_fallback(reason, None, ASTRA_FALLBACK_KIND_DECLINED)
84
+
85
+
86
+ def _preflight_from_answer(
87
+ field_by_name: dict[str, object], exported_home: str | None
88
+ ) -> AstraPreflight:
55
89
  decision = field_by_name.get("decision")
56
90
  if not isinstance(decision, dict) or not isinstance(decision.get("tier"), str):
57
91
  return _broken("broker answer is malformed")
58
- if decision["tier"] != CODEX_TIER_NORMAL:
59
- reason = f"{ASTRA_PREFLIGHT_FAILURE_REASON}: no Codex account has room ({decision.get('reason')})"
60
- return _preflight_fallback(reason, None, ASTRA_FALLBACK_KIND_DECLINED)
92
+ if decision["tier"] not in _accepted_tiers(exported_home):
93
+ return _declined(decision.get("reason"), exported_home)
61
94
  codex_home = decision.get("home")
62
95
  account = decision.get("account")
63
96
  accounts = field_by_name.get("accounts")
@@ -66,8 +99,6 @@ def _preflight_from_answer(field_by_name: dict[str, object]) -> AstraPreflight:
66
99
  return _broken("broker answer names no Codex home with room")
67
100
  if not isinstance(reason, str):
68
101
  return _broken("broker answer is malformed")
69
- if not accounts:
70
- return AstraPreflight(True, None, reason, codex_home=Path(codex_home))
71
102
  chosen = next(
72
103
  (
73
104
  entry
@@ -91,12 +122,26 @@ def _preflight_from_answer(field_by_name: dict[str, object]) -> AstraPreflight:
91
122
  return AstraPreflight(True, min(all_percent_left), reason, codex_home=Path(codex_home))
92
123
 
93
124
 
125
+ def _broker_selection_arguments(exported_home: str | None) -> tuple[str, ...]:
126
+ if exported_home is None:
127
+ return ALL_ACCOUNT_BROKER_REQUIRE_TIER_ARGUMENTS
128
+ return (ACCOUNT_BROKER_HOME_FLAG, exported_home)
129
+
130
+
94
131
  def _run_broker(
95
132
  broker_path: Path,
96
133
  process_runner: Callable[..., subprocess.CompletedProcess[str]],
134
+ exported_home: str | None,
97
135
  ) -> subprocess.CompletedProcess[str]:
98
136
  return process_runner(
99
- [sys.executable, str(broker_path), ACCOUNT_BROKER_CHOOSE_COMMAND, "--product", "codex"],
137
+ [
138
+ sys.executable,
139
+ str(broker_path),
140
+ ACCOUNT_BROKER_CHOOSE_COMMAND,
141
+ "--product",
142
+ "codex",
143
+ *_broker_selection_arguments(exported_home),
144
+ ],
100
145
  capture_output=True,
101
146
  text=True,
102
147
  check=False,
@@ -105,7 +150,9 @@ def _run_broker(
105
150
  )
106
151
 
107
152
 
108
- def _preflight_from_broker(completed: subprocess.CompletedProcess[str]) -> AstraPreflight:
153
+ def _preflight_from_broker(
154
+ completed: subprocess.CompletedProcess[str], exported_home: str | None
155
+ ) -> AstraPreflight:
109
156
  if completed.returncode not in ALL_BROKER_ACCEPTED_EXIT_CODES:
110
157
  return _broken(f"broker exit {completed.returncode}")
111
158
  try:
@@ -118,26 +165,35 @@ def _preflight_from_broker(completed: subprocess.CompletedProcess[str]) -> Astra
118
165
  decision = answer.get("decision")
119
166
  if not isinstance(decision, dict) or not isinstance(decision.get("tier"), str):
120
167
  return _broken("broker answer is malformed")
121
- reason = f"{ASTRA_PREFLIGHT_FAILURE_REASON}: no Codex account has room ({decision.get('reason')})"
122
- return _preflight_fallback(reason, None, ASTRA_FALLBACK_KIND_DECLINED)
123
- return _preflight_from_answer(answer)
168
+ return _declined(decision.get("reason"), exported_home)
169
+ return _preflight_from_answer(answer, exported_home)
124
170
 
125
171
 
126
172
  def run_astra_preflight(
127
173
  broker_path: Path,
128
174
  process_runner: Callable[..., subprocess.CompletedProcess[str]],
175
+ exported_home: str | None = None,
129
176
  ) -> AstraPreflight:
130
177
  """Run the Codex account broker and decide whether Astra may bind.
131
178
 
179
+ ::
180
+
181
+ exported_home None -> the broker chooses an account that reaches the normal tier
182
+ exported_home "H" -> the broker judges H alone; normal or luna binds on H
183
+
132
184
  Args:
133
185
  broker_path: Path to ``account_broker.py``.
134
186
  process_runner: Callable that runs the broker.
187
+ exported_home: Codex home the caller exported, or None when it exported none.
135
188
 
136
189
  Returns:
137
190
  Eligibility, the chosen account's percent left and Codex home, reason, and fallback kind.
191
+ A pinned home the broker refuses is declined, with the broker's reason, so nothing launches.
138
192
  """
139
193
  try:
140
- return _preflight_from_broker(_run_broker(broker_path, process_runner))
194
+ return _preflight_from_broker(
195
+ _run_broker(broker_path, process_runner, exported_home), exported_home
196
+ )
141
197
  except subprocess.TimeoutExpired as error:
142
198
  return _preflight_fallback(
143
199
  f"{ASTRA_ACCOUNT_PICK_TIMEOUT_REASON}: {error}", None, ASTRA_FALLBACK_KIND_BROKEN
@@ -17,7 +17,12 @@ CODEX_HOME_ENV_VAR: str = "CODEX_HOME"
17
17
  ALL_ACCOUNT_BROKER_RELATIVE_PARTS: tuple[str, ...] = ("scripts", "account_broker.py")
18
18
  SHARED_PACKAGE_ROOT_PARENT_INDEX: int = 2
19
19
  ACCOUNT_BROKER_CHOOSE_COMMAND: str = "choose"
20
+ ALL_ACCOUNT_BROKER_REQUIRE_TIER_ARGUMENTS: tuple[str, ...] = ("--require-tier", "normal")
21
+ ACCOUNT_BROKER_HOME_FLAG: str = "--home"
20
22
  CODEX_TIER_NORMAL: str = "normal"
23
+ CODEX_TIER_LUNA: str = "luna"
24
+ ALL_ASTRA_NORMAL_ONLY_TIERS: tuple[str, ...] = (CODEX_TIER_NORMAL,)
25
+ ALL_ASTRA_EXPORTED_HOME_TIERS: tuple[str, ...] = (CODEX_TIER_NORMAL, CODEX_TIER_LUNA)
21
26
  BROKER_WAIT_EXIT_CODE: int = 3
22
27
  ALL_BROKER_ACCEPTED_EXIT_CODES: tuple[int, ...] = (0, BROKER_WAIT_EXIT_CODE)
23
28
  ALL_BROKER_METER_PERCENT_KEYS: tuple[str, ...] = ("session_percent_left", "weekly_percent_left")
@@ -27,7 +32,9 @@ ASTRA_ACCOUNT_PICK_TIMEOUT_SECONDS: float = 150.0
27
32
  ASTRA_ENV_VAR: str = "ADVISOR_ASTRA"
28
33
  ALL_ASTRA_TRUTHY_VALUES: frozenset[str] = frozenset({"1", "true", "yes", "on"})
29
34
  ASTRA_PREFLIGHT_FAILURE_REASON: str = "astra preflight found no Codex account to bind"
35
+ ASTRA_EXPORTED_HOME_REFUSED_REASON_TEMPLATE: str = "astra preflight refused the exported Codex home ({home}): {reason}"
30
36
  ASTRA_BIND_FAILURE_REASON: str = "Codex Astra bind failed"
37
+ ASTRA_NO_HOME_REASON: str = "astra preflight named no Codex home to bind"
31
38
  ASTRA_REPLY_FAILURE_REASON: str = "Codex Astra returned no advisor guidance"
32
39
  ASTRA_ACCOUNT_PICK_TIMEOUT_REASON: str = "Codex Astra account pick timed out"
33
40
  ASTRA_CODEX_TIMEOUT_REASON: str = "Codex Astra request timed out"
@@ -4,12 +4,19 @@ import io
4
4
  import json
5
5
  import subprocess
6
6
  import sys
7
- from collections.abc import Callable
7
+ from collections.abc import Callable, Mapping, Sequence
8
+ from datetime import datetime, timezone
8
9
  from pathlib import Path
9
10
  from types import ModuleType
11
+ from typing import NamedTuple
10
12
 
11
13
  import pytest
12
14
 
15
+ sys.path.insert(0, str(Path(__file__).parent.parent.parents[2] / "scripts"))
16
+
17
+ from account_broker import choose_from_readings, decision_payload, readings_payload
18
+ from dev_env_scripts_constants.account_broker_constants import Account, Meters, Product, Reading
19
+
13
20
  _ProcessRunner = Callable[..., subprocess.CompletedProcess[str]]
14
21
 
15
22
 
@@ -193,7 +200,9 @@ def test_should_run_broker_choose_command_for_codex_preflight() -> None:
193
200
  return _broker_answer(NORMAL_ANSWER)
194
201
 
195
202
  astra_advisor.run_astra_preflight(BROKER_PATH, runner)
196
- assert all_calls == [[sys.executable, str(BROKER_PATH), "choose", "--product", "codex"]]
203
+ assert all_calls == [
204
+ [sys.executable, str(BROKER_PATH), "choose", "--product", "codex", "--require-tier", "normal"]
205
+ ]
197
206
 
198
207
 
199
208
  def test_should_accept_normal_tier_and_keep_its_codex_home() -> None:
@@ -306,6 +315,31 @@ def test_should_bind_under_the_broker_chosen_home() -> None:
306
315
  assert calls[1] == astra_advisor.build_codex_arguments("codex")
307
316
 
308
317
 
318
+ def test_injected_preflight_without_a_codex_home_fails_closed() -> None:
319
+ all_started_commands: list[list[str]] = []
320
+
321
+ def runner_that_must_not_start(
322
+ all_arguments: list[str], **_keywords: object
323
+ ) -> subprocess.CompletedProcess[str]:
324
+ all_started_commands.append(all_arguments)
325
+ raise AssertionError("no process may start without a chosen Codex home")
326
+
327
+ reply = astra_advisor.run_codex_astra_advisor(
328
+ "consult",
329
+ Path("."),
330
+ astra_advisor.AstraPreflight(True, 90, "ok"),
331
+ BROKER_PATH,
332
+ ENABLED_SETTINGS,
333
+ None,
334
+ runner_that_must_not_start,
335
+ )
336
+
337
+ assert reply.is_fallback
338
+ assert reply.fallback_kind == astra_advisor.ASTRA_FALLBACK_KIND_BROKEN
339
+ assert astra_advisor.ASTRA_NO_HOME_REASON in (reply.reason or "")
340
+ assert all_started_commands == []
341
+
342
+
309
343
  def test_bind_routes_astra_xhigh_to_the_policy_effort() -> None:
310
344
  calls: list[list[str]] = []
311
345
  settings = {**ENABLED_SETTINGS, astra_advisor.ADVISOR_EFFORT_ENV_VAR: "xhigh"}
@@ -416,3 +450,233 @@ def test_astra_docs_name_shared_effort_and_helper() -> None:
416
450
  assert "ADVISOR_ASTRA=1" in protocol
417
451
  assert "codex_astra_advisor.py" in rung
418
452
  assert "--resume <session_id>" in rung
453
+
454
+
455
+ BROKER_WAIT_EXIT_CODE = 3
456
+ BROKER_CLOCK = datetime(2026, 11, 9, 12, 0, tzinfo=timezone.utc)
457
+ ROSTER_TEAM_NAME = "acct-team"
458
+ ROSTER_PLUS_NAME = "acct-plus"
459
+ UNREAD_REASON = "codex app-server did not answer"
460
+ BrokerBehavior = Callable[[list[str]], subprocess.CompletedProcess[str]]
461
+
462
+
463
+ class _Consultation(NamedTuple):
464
+ reply: astra_advisor.CodexAstraAdvisorReply
465
+ all_calls: list[list[str]]
466
+ all_call_keywords: list[dict[str, object]]
467
+
468
+
469
+ def _reading(home: Path, plan_type: str | None, session_left: float, weekly_left: float) -> Reading:
470
+ account = Account(Product.CODEX, home.name, home.resolve())
471
+ return Reading(account, Meters(session_left, None, weekly_left, None, plan_type))
472
+
473
+
474
+ def _unreadable_reading(home: Path) -> Reading:
475
+ return Reading(Account(Product.CODEX, home.name, home.resolve()), None, UNREAD_REASON)
476
+
477
+
478
+ def _roster_readings(tmp_path: Path) -> tuple[Reading, Reading]:
479
+ return (
480
+ _reading(tmp_path / ROSTER_TEAM_NAME, "team", 90.0, 95.0),
481
+ _reading(tmp_path / ROSTER_PLUS_NAME, "plus", 60.0, 70.0),
482
+ )
483
+
484
+
485
+ def _broker_completion(
486
+ all_arguments: Sequence[str],
487
+ all_roster_readings: Sequence[Reading],
488
+ all_outside_readings: Sequence[Reading],
489
+ ) -> subprocess.CompletedProcess[str]:
490
+ reading_by_home: Mapping[Path, Reading] = {
491
+ each_reading.account.home: each_reading
492
+ for each_reading in (*all_roster_readings, *all_outside_readings)
493
+ }
494
+ if "--home" in all_arguments:
495
+ exported_home = Path(all_arguments[all_arguments.index("--home") + 1]).resolve()
496
+ all_readings = [reading_by_home[exported_home]]
497
+ else:
498
+ all_readings = list(all_roster_readings)
499
+ required_tier = (
500
+ all_arguments[all_arguments.index("--require-tier") + 1]
501
+ if "--require-tier" in all_arguments
502
+ else None
503
+ )
504
+ decision = choose_from_readings(
505
+ Product.CODEX, all_readings, now=BROKER_CLOCK, required_tier=required_tier
506
+ )
507
+ answer = {
508
+ "decision": decision_payload(decision),
509
+ "accounts": readings_payload(all_readings),
510
+ }
511
+ exit_code = BROKER_WAIT_EXIT_CODE if decision.action == "wait" else 0
512
+ return subprocess.CompletedProcess(list(all_arguments), exit_code, json.dumps(answer), "")
513
+
514
+
515
+ def _broker_exits_with_an_error(all_arguments: list[str]) -> subprocess.CompletedProcess[str]:
516
+ return subprocess.CompletedProcess(all_arguments, 2, "", "bad roster")
517
+
518
+
519
+ def _broker_prints_text_that_is_not_json(
520
+ all_arguments: list[str],
521
+ ) -> subprocess.CompletedProcess[str]:
522
+ return subprocess.CompletedProcess(all_arguments, 0, "not json", "")
523
+
524
+
525
+ def _broker_times_out(all_arguments: list[str]) -> subprocess.CompletedProcess[str]:
526
+ raise subprocess.TimeoutExpired(all_arguments, 150)
527
+
528
+
529
+ def _consult(
530
+ tmp_path: Path,
531
+ exported_home: str | None,
532
+ all_outside_readings: Sequence[Reading] = (),
533
+ broker_behavior: BrokerBehavior | None = None,
534
+ ) -> _Consultation:
535
+ all_calls: list[list[str]] = []
536
+ all_call_keywords: list[dict[str, object]] = []
537
+ all_roster_readings = _roster_readings(tmp_path)
538
+
539
+ def answer_from_the_roster(all_arguments: list[str]) -> subprocess.CompletedProcess[str]:
540
+ return _broker_completion(all_arguments, all_roster_readings, all_outside_readings)
541
+
542
+ answer_broker = answer_from_the_roster if broker_behavior is None else broker_behavior
543
+
544
+ def runner(all_arguments: list[str], **keywords: object) -> subprocess.CompletedProcess[str]:
545
+ all_calls.append(all_arguments)
546
+ all_call_keywords.append(keywords)
547
+ if all_arguments[1] == str(BROKER_PATH):
548
+ return answer_broker(all_arguments)
549
+ return subprocess.CompletedProcess(all_arguments, 0, _events(), "")
550
+
551
+ settings = (
552
+ ENABLED_SETTINGS
553
+ if exported_home is None
554
+ else {**ENABLED_SETTINGS, "CODEX_HOME": exported_home}
555
+ )
556
+ reply = astra_advisor.run_codex_astra_advisor(
557
+ "consult", Path("."), None, BROKER_PATH, settings, None, runner
558
+ )
559
+ return _Consultation(reply, all_calls, all_call_keywords)
560
+
561
+
562
+ def _assert_ran_on_the_exported_home(consultation: _Consultation, exported_home: Path) -> None:
563
+ assert consultation.reply.successful
564
+ codex_environment = consultation.all_call_keywords[1]["env"]
565
+ assert isinstance(codex_environment, dict)
566
+ assert codex_environment["CODEX_HOME"] == str(exported_home.resolve())
567
+ assert consultation.all_calls[0] == [
568
+ sys.executable, str(BROKER_PATH), "choose", "--product", "codex", "--home", str(exported_home),
569
+ ]
570
+ assert len(consultation.all_calls) == 2
571
+
572
+
573
+ def _assert_refused_without_a_launch(
574
+ consultation: _Consultation, all_reason_parts: Sequence[str]
575
+ ) -> None:
576
+ assert consultation.reply.is_fallback
577
+ assert not consultation.reply.successful
578
+ assert consultation.reply.fallback_kind == astra_advisor.ASTRA_FALLBACK_KIND_DECLINED
579
+ for each_reason_part in all_reason_parts:
580
+ assert each_reason_part in (consultation.reply.reason or "")
581
+ assert len(consultation.all_calls) == 1
582
+ assert consultation.all_calls[0][1] == str(BROKER_PATH)
583
+
584
+
585
+ def test_should_run_on_a_paid_exported_home_that_is_in_the_roster(tmp_path: Path) -> None:
586
+ exported_home = tmp_path / ROSTER_PLUS_NAME
587
+
588
+ consultation = _consult(tmp_path, str(exported_home))
589
+
590
+ _assert_ran_on_the_exported_home(consultation, exported_home)
591
+
592
+
593
+ def test_should_trim_the_padding_around_an_exported_home(tmp_path: Path) -> None:
594
+ exported_home = tmp_path / ROSTER_PLUS_NAME
595
+
596
+ consultation = _consult(tmp_path, f" {exported_home} ")
597
+
598
+ _assert_ran_on_the_exported_home(consultation, exported_home)
599
+
600
+
601
+ def test_should_run_on_a_paid_exported_home_that_is_outside_the_roster(tmp_path: Path) -> None:
602
+ exported_home = tmp_path / "outside-plus"
603
+
604
+ consultation = _consult(tmp_path, str(exported_home), [_reading(exported_home, "plus", 80.0, 85.0)])
605
+
606
+ _assert_ran_on_the_exported_home(consultation, exported_home)
607
+
608
+
609
+ def test_should_run_on_a_pro_exported_home_at_the_luna_tier(tmp_path: Path) -> None:
610
+ exported_home = tmp_path / "acct-pro"
611
+ pro_reading = _reading(exported_home, "pro", 5.0, 80.0)
612
+ home_answer = json.loads(
613
+ _broker_completion(
614
+ ["--home", str(exported_home)], _roster_readings(tmp_path), [pro_reading]
615
+ ).stdout
616
+ )
617
+ assert home_answer["decision"]["tier"] == "luna"
618
+
619
+ consultation = _consult(tmp_path, str(exported_home), [pro_reading])
620
+
621
+ _assert_ran_on_the_exported_home(consultation, exported_home)
622
+
623
+
624
+ def test_should_refuse_a_free_exported_home_when_a_roster_exists(tmp_path: Path) -> None:
625
+ exported_home = tmp_path / "acct-free"
626
+
627
+ consultation = _consult(tmp_path, str(exported_home), [_reading(exported_home, "free", 95.0, 95.0)])
628
+
629
+ _assert_refused_without_a_launch(
630
+ consultation, ["acct-free is on a free plan and is never chosen"]
631
+ )
632
+
633
+
634
+ def test_should_refuse_an_unlisted_plan_exported_home_when_a_roster_exists(tmp_path: Path) -> None:
635
+ exported_home = tmp_path / "acct-mystery"
636
+
637
+ consultation = _consult(
638
+ tmp_path, str(exported_home), [_reading(exported_home, "mystery", 95.0, 95.0)]
639
+ )
640
+
641
+ _assert_refused_without_a_launch(
642
+ consultation, ["acct-mystery reports no known plan and is never chosen"]
643
+ )
644
+
645
+
646
+ def test_should_refuse_an_unreadable_exported_home(tmp_path: Path) -> None:
647
+ exported_home = tmp_path / "acct-unread"
648
+
649
+ consultation = _consult(tmp_path, str(exported_home), [_unreadable_reading(exported_home)])
650
+
651
+ _assert_refused_without_a_launch(consultation, ["acct-unread", UNREAD_REASON])
652
+
653
+
654
+ @pytest.mark.parametrize(
655
+ "broker_behavior",
656
+ [_broker_exits_with_an_error, _broker_prints_text_that_is_not_json, _broker_times_out],
657
+ )
658
+ def test_should_refuse_the_exported_home_when_the_broker_cannot_check_it(
659
+ tmp_path: Path, broker_behavior: BrokerBehavior
660
+ ) -> None:
661
+ consultation = _consult(
662
+ tmp_path, str(tmp_path / ROSTER_PLUS_NAME), broker_behavior=broker_behavior
663
+ )
664
+
665
+ assert consultation.reply.is_fallback
666
+ assert consultation.reply.fallback_kind == astra_advisor.ASTRA_FALLBACK_KIND_BROKEN
667
+ assert len(consultation.all_calls) == 1
668
+
669
+
670
+ @pytest.mark.parametrize("exported_home", [None, "", " "])
671
+ def test_should_choose_from_the_roster_when_no_home_is_exported(
672
+ tmp_path: Path, exported_home: str | None
673
+ ) -> None:
674
+ consultation = _consult(tmp_path, exported_home)
675
+
676
+ assert consultation.reply.successful
677
+ assert consultation.all_calls[0] == [
678
+ sys.executable, str(BROKER_PATH), "choose", "--product", "codex", "--require-tier", "normal",
679
+ ]
680
+ codex_environment = consultation.all_call_keywords[1]["env"]
681
+ assert isinstance(codex_environment, dict)
682
+ assert codex_environment["CODEX_HOME"] == str((tmp_path / ROSTER_TEAM_NAME).resolve())
@@ -6,8 +6,6 @@ import sys
6
6
  from datetime import datetime, timezone
7
7
  from pathlib import Path
8
8
 
9
- import pytest
10
-
11
9
  SCRIPTS_ROOT = Path(__file__).parent.parent
12
10
  sys.path.insert(0, str(SCRIPTS_ROOT / "config"))
13
11
  sys.path.insert(0, str(SCRIPTS_ROOT))
@@ -58,7 +56,9 @@ def test_should_accept_broker_normal_answer_and_use_tighter_meter(tmp_path: Path
58
56
 
59
57
  preflight = run_astra_preflight(broker_path, process_runner)
60
58
 
61
- assert calls == [[sys.executable, str(broker_path), "choose", "--product", "codex"]]
59
+ assert calls == [
60
+ [sys.executable, str(broker_path), "choose", "--product", "codex", "--require-tier", "normal"]
61
+ ]
62
62
  assert preflight.eligible
63
63
  assert preflight.percent_left == 42
64
64
  assert preflight.codex_home == home
@@ -128,13 +128,28 @@ def test_should_accept_the_answer_the_broker_serializes(tmp_path: Path) -> None:
128
128
  assert preflight.codex_home == tmp_path / "codex-2"
129
129
 
130
130
 
131
+ def _answer_for_the_default_home(
132
+ default_home: Path, plan_type: str
133
+ ) -> dict[str, object]:
134
+ account = Account(Product.CODEX, "default", default_home.resolve(), True)
135
+ reading = Reading(account, Meters(72.0, None, 42.0, None, plan_type))
136
+ decision = choose_from_readings(
137
+ Product.CODEX,
138
+ [reading],
139
+ now=datetime.now(timezone.utc),
140
+ required_tier="normal",
141
+ )
142
+ return {
143
+ "decision": decision_payload(decision),
144
+ "accounts": readings_payload([reading]),
145
+ }
146
+
147
+
131
148
  def test_should_bind_the_default_home_when_no_roster_is_configured(
132
- tmp_path: Path, monkeypatch: pytest.MonkeyPatch
149
+ tmp_path: Path,
133
150
  ) -> None:
134
151
  default_home = tmp_path / ".codex"
135
- monkeypatch.setenv("CODEX_HOME", str(default_home))
136
- decision = choose_from_readings(Product.CODEX, [], now=datetime.now(timezone.utc))
137
- answer = {"decision": decision_payload(decision), "accounts": readings_payload([])}
152
+ answer = _answer_for_the_default_home(default_home, "team")
138
153
 
139
154
  def process_runner(arguments: list[str], **kwargs: object) -> subprocess.CompletedProcess[str]:
140
155
  return subprocess.CompletedProcess(arguments, 0, json.dumps(answer), "")
@@ -142,5 +157,96 @@ def test_should_bind_the_default_home_when_no_roster_is_configured(
142
157
  preflight = run_astra_preflight(tmp_path / "account_broker.py", process_runner)
143
158
 
144
159
  assert preflight.eligible
145
- assert preflight.percent_left is None
160
+ assert preflight.percent_left == 42
146
161
  assert preflight.codex_home == default_home.resolve()
162
+
163
+
164
+ def test_should_not_bind_a_normal_answer_that_lists_no_account(tmp_path: Path) -> None:
165
+ answer = {
166
+ "decision": {
167
+ "action": "run",
168
+ "account": "default",
169
+ "home": str(tmp_path / ".codex"),
170
+ "resets_at": None,
171
+ "reason": "default has 90% left",
172
+ "tier": "normal",
173
+ },
174
+ "accounts": [],
175
+ }
176
+
177
+ def process_runner(arguments: list[str], **kwargs: object) -> subprocess.CompletedProcess[str]:
178
+ return subprocess.CompletedProcess(arguments, 0, json.dumps(answer), "")
179
+
180
+ preflight = run_astra_preflight(tmp_path / "account_broker.py", process_runner)
181
+
182
+ assert not preflight.eligible
183
+ assert preflight.fallback_kind == "broken"
184
+ assert preflight.codex_home is None
185
+
186
+
187
+ def test_should_not_bind_a_free_plan_default_home(tmp_path: Path) -> None:
188
+ answer = _answer_for_the_default_home(tmp_path / ".codex", "free")
189
+
190
+ def process_runner(arguments: list[str], **kwargs: object) -> subprocess.CompletedProcess[str]:
191
+ return subprocess.CompletedProcess(arguments, 3, json.dumps(answer), "")
192
+
193
+ preflight = run_astra_preflight(tmp_path / "account_broker.py", process_runner)
194
+
195
+ assert not preflight.eligible
196
+ assert preflight.codex_home is None
197
+
198
+
199
+ def _broker_reply_for_an_exported_home(
200
+ exported_home: Path, plan_type: str, session_left: float, weekly_left: float
201
+ ) -> tuple[int, dict[str, object], str]:
202
+ account = Account(Product.CODEX, exported_home.name, exported_home.resolve())
203
+ reading = Reading(account, Meters(session_left, None, weekly_left, None, plan_type))
204
+ decision = choose_from_readings(
205
+ Product.CODEX, [reading], now=datetime.now(timezone.utc)
206
+ )
207
+ answer = {
208
+ "decision": decision_payload(decision),
209
+ "accounts": readings_payload([reading]),
210
+ }
211
+ return (3 if decision.action == "wait" else 0), answer, decision.tier
212
+
213
+
214
+ def test_should_ask_the_broker_about_the_exported_home_without_a_required_tier(
215
+ tmp_path: Path,
216
+ ) -> None:
217
+ exported_home = tmp_path / "acct-pro"
218
+ exit_code, answer, tier = _broker_reply_for_an_exported_home(exported_home, "pro", 5.0, 80.0)
219
+ broker_path = tmp_path / "account_broker.py"
220
+ calls: list[list[str]] = []
221
+
222
+ def process_runner(arguments: list[str], **kwargs: object) -> subprocess.CompletedProcess[str]:
223
+ calls.append(arguments)
224
+ return subprocess.CompletedProcess(arguments, exit_code, json.dumps(answer), "")
225
+
226
+ preflight = run_astra_preflight(broker_path, process_runner, exported_home=str(exported_home))
227
+
228
+ assert tier == "luna"
229
+ assert calls == [
230
+ [sys.executable, str(broker_path), "choose", "--product", "codex", "--home", str(exported_home)]
231
+ ]
232
+ assert preflight.eligible
233
+ assert preflight.percent_left == 5
234
+ assert preflight.codex_home == exported_home.resolve()
235
+
236
+
237
+ def test_should_decline_an_exported_home_the_broker_refuses_and_name_it(tmp_path: Path) -> None:
238
+ exported_home = tmp_path / "acct-free"
239
+ exit_code, answer, _tier = _broker_reply_for_an_exported_home(exported_home, "free", 95.0, 95.0)
240
+
241
+ def process_runner(arguments: list[str], **kwargs: object) -> subprocess.CompletedProcess[str]:
242
+ return subprocess.CompletedProcess(arguments, exit_code, json.dumps(answer), "")
243
+
244
+ preflight = run_astra_preflight(
245
+ tmp_path / "account_broker.py", process_runner, exported_home=str(exported_home)
246
+ )
247
+
248
+ assert not preflight.eligible
249
+ assert preflight.fallback_kind == "declined"
250
+ assert preflight.codex_home is None
251
+ assert "acct-free is on a free plan and is never chosen" in preflight.reason
252
+ assert str(exported_home) in preflight.reason
@@ -30,7 +30,7 @@ A probe error does not waive the gate; the live GitHub checks still run.
30
30
 
31
31
  The Codex gate is conditional-required: it demands
32
32
  ``codex_clean_at == current_head`` only when the account broker
33
- (``scripts/account_broker.py choose --product codex``) answers the ``normal`` tier, so
33
+ (``scripts/account_broker.py choose --product codex --require-tier normal``) answers the ``normal`` tier, so
34
34
  one signed-in account has room for a review. A ``luna`` or ``wait`` answer, an
35
35
  unreadable broker, the codex token, or ``codex_down`` never blocks ready.
36
36
  """
@@ -63,6 +63,9 @@ from check_convergence_thread_gates import (
63
63
  _check_no_pending_reviews,
64
64
  _count_unresolved_bot_threads,
65
65
  )
66
+ from codex_review_scripts_constants.account_constants import (
67
+ ALL_ACCOUNT_BROKER_REQUIRE_TIER_ARGUMENTS,
68
+ )
66
69
  from pr_converge_scripts_constants.convergence_gate_constants import (
67
70
  ALL_CODEX_ACCOUNT_BROKER_RELATIVE_PARTS,
68
71
  ALL_CODEX_BROKER_ACCEPTED_EXIT_CODES,
@@ -384,6 +387,7 @@ def _read_codex_tier() -> str | None:
384
387
  CODEX_ACCOUNT_BROKER_CHOOSE_COMMAND,
385
388
  "--product",
386
389
  "codex",
390
+ *ALL_ACCOUNT_BROKER_REQUIRE_TIER_ARGUMENTS,
387
391
  ],
388
392
  capture_output=True,
389
393
  text=True,