Skip to content

Commit b507429

Browse files
XertroVWibias
authored andcommitted
feat(codex): selection order for the account pool
Rebased onto current upstream/dev, integrating the exact-selector routing that landed after this branch last merged dev. The pool selection-order feature is unchanged: per-account priority tiers via a single choke point in getEligiblePoolAccounts, the manual-selection pin, preemption for unbound requests, and the CLI/GUI/config/docs surfaces. Conflict resolution notes: - src/codex/routing.ts: merged the PR's priority tiering with dev's selectionOptions threading (exact-selector usability options). The P2 scope-isolation fix (quotaScope through applyFailureFailover) is preserved alongside dev's fixedAccount guards. - src/config.ts: kept dev's apiKeys salvage + client-integrations schemas and added the codexAccountPriorities / activeCodexAccountPinned fields, write-path guard, and degraded-load warnings. - src/codex/auth-api.ts: dev's newer listCodexAuthAccountsSnapshot (main-identity generation, plan reconciliation) now threads priority into the DTOs. - gui/src/hooks/useCodexAccountPool.ts: dev's progressive-load base gained the PR's priorityUpdatingId / activePinnedId / setAccountPriority and the pin-lifecycle edges (switch/order cross-gate, pause release, order-write pending-marker retirement). - gui i18n + docs: kept the PR's corrected switch/pin copy and grafted the selection-order docs into the restructured split pages in all five locales. Adds the exact-selector bypass regression: selection order must never bypass an exact account selector. With codexAccountPriorities ordering pool-b ahead of pool-a, an exact accountId: pool-a still resolves to pool-a (fixedAccount), while the unbound pool path honors the order. Verified: bun run typecheck, bun run privacy:scan, GUI tsc -b, GUI eslint + lint:i18n, GUI suite (645 pass), core pool/config/cli tests. Pre-existing Windows symlink-EPERM and load-sensitive auth-api ordering failures reproduce identically on the clean dev base and are not introduced here.
1 parent ef13178 commit b507429

61 files changed

Lines changed: 3763 additions & 132 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ devlog/**/security-advisory-draft*
3636
.omo/
3737
**/.omo/
3838

39+
# Local development worktrees
40+
.worktrees/
41+
3942
# Test-generated artifacts
4043
tests/.tmp-*/
4144
.claude/

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ ocx start # or `ocx service` to run it in the backgro
6060
Open **http://localhost:10100** and configure everything in the web dashboard — add providers
6161
(40+ built-ins, or any OpenAI-compatible endpoint), pick models, manage accounts. `ocx gui`
6262
re-opens the dashboard at any time.
63+
It can also manage a **ChatGPT account pool** for Codex auth. Add multiple ChatGPT / Codex accounts,
64+
refresh their 5h / weekly / 30d quota in the dashboard, and let new sessions auto-route to the
65+
lowest-usage healthy account. Existing Codex threads stay pinned to the account that started them,
66+
so long SSH, tmux, or mobile-connected sessions do not jump accounts mid-conversation. Give the
67+
accounts a selection order when one of them — usually your Codex Desktop login — should only be
68+
reached for once the others are drained.
6369

6470
### For agents
6571

‎assets/pr715-selection-order.png‎

146 KB
Loading

‎docs-site/src/content/docs/guides/web-dashboard.md‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,16 @@ catalog entry.
105105

106106
The **Codex Auth** page manages the native ChatGPT/Codex route:
107107

108-
- Manually choosing an account changes the next new Codex session; an already-bound thread keeps its
109-
current account for that manual switch.
108+
- Manually choosing an account applies immediately: an already-bound thread moves to it on its next
109+
request, and only requests already in flight keep the account they captured. A manual choice is also
110+
pinned: the card shows a **PINNED** badge, and a higher selection order cannot preempt that account
111+
until it is drained, you select another account, or you change any account's selection order.
112+
- Each account card carries a **Selection order** control (First, Earlier, Normal, Later, Last).
113+
Higher order is used first, and the pool drops to a lower order only once every account above it is
114+
drained or unavailable. A changed order applies from the next unbound request and never moves a
115+
thread that is already bound. The Codex Desktop (main) account is ordered like any other, so it can
116+
be set to **Last** and kept as the reserve. An order set from `ocx account priority` outside those
117+
five presets stays visible and selectable on the card.
110118
- Thread affinity prevents per-request flapping. With quota auto-switch enabled, a long-running
111119
thread is periodically re-evaluated and may rebind after its relevant usage reaches the threshold
112120
and a strictly lower-usage eligible account exists.
@@ -170,6 +178,7 @@ The GUI is a thin client over the proxy's JSON management API. Useful endpoints
170178
| `POST /api/oauth/login` · `GET /api/oauth/status` | Start a provider OAuth flow and poll for completion. |
171179
| `GET /api/codex-auth/accounts?refresh=1` | List main and pool accounts, force quota refresh, and report main-account `hasCredential` / terminal `needsReauth` state. |
172180
| `PUT /api/codex-auth/active` · `PUT /api/codex-auth/auto-switch` · `PUT /api/codex-auth/failover` | Select the account for the next request and configure pool routing. |
181+
| `GET /api/codex-auth/active` · `PUT /api/codex-auth/accounts/priority` | Read the effective account (including `pinned` and which account is `pinnedAccountId`) and set one account's selection order. |
173182
| `POST /api/codex-auth/login` · `GET /api/codex-auth/login-status` | Add a pool account through browser login. |
174183
| `GET /api/logs?tail=50&limit=20&offset=0&provider=...&status=5xx` | Read recent request metadata with optional tail, provider, and exact/class status filters. With `limit`/`offset`, paging walks backward from the newest row (`offset=0` returns the latest page). Response shape: `{ timeZone, total, logs }` where `total` is the filtered row count before pagination. |
175184
| `GET` / `PUT /api/subagent-models` | Read or set the five featured `spawn_agent` override models. |

‎docs-site/src/content/docs/ja/guides/web-dashboard.md‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,16 @@ Codex タスクだけに適用され、このオプション自体が委任を
8989

9090
**Codex 認証**ページはネイティブ ChatGPT/Codex ルートを管理します。
9191

92-
- アカウントを直接選ぶと次の新規 Codex セッションから変わります。すでにアカウントが紐付いた thread はこの手動切り替えだけでは
93-
途中で移動しません。
92+
- アカウントを直接選ぶとすぐに反映されます。すでにアカウントが紐付いた thread も次のリクエストで選んだ
93+
アカウントに移り、すでに送信中のリクエストだけが取得済みのアカウントを使い続けます。手動で選んだアカウントは固定もされます。
94+
カードに **固定中** バッジが付き、そのアカウントが使い切られるか、別のアカウントを選ぶか、いずれかの
95+
アカウントの選択順序を変更するまで、より上位の選択順序が割り込むことはありません。
96+
- 各アカウントカードには **選択順序** のコントロール(最初 / 早め / 標準 / 遅め / 最後)があります。
97+
順序が上のものから使われ、その上にあるアカウントがすべて使い切られるか利用できなくなって初めて
98+
下の順序へ下がります。順序を変更すると **次の未バインドリクエスト** から適用され、すでにアカウントに
99+
紐づいた thread を移動させることはありません。Codex Desktop(メイン)アカウントも同じように
100+
並べ替えられるので、**最後** にして予備に回せます。`ocx account priority` でプリセット以外の値を設定した場合も、カード上に
101+
選択肢として残ります。
94102
- Thread affinity がリクエストごとにアカウントが揺れるのを防ぎます。クォータ自動切り替えがオンなら長く
95103
実行される thread も定期的に再評価します。関連使用量がしきい値以上で、使用量が確実により低い
96104
健全アカウントがあればそのアカウントに再紐付けできます。
@@ -124,6 +132,7 @@ GUI はプロキシの JSON 管理 API を使うシンクライアントです
124132
| `POST /api/oauth/login` · `GET /api/oauth/status` | プロバイダー OAuth ログインを開始し完了可否を確認します。 |
125133
| `GET /api/codex-auth/accounts?refresh=1` | メインおよびプールアカウントを参照しクォータを強制更新し、メインの `hasCredential` / terminal `needsReauth` 状態を返します。 |
126134
| `PUT /api/codex-auth/active` · `PUT /api/codex-auth/auto-switch` · `PUT /api/codex-auth/failover` | 次のリクエストで使うアカウントとプールルーティングポリシーを設定します。 |
135+
| `GET /api/codex-auth/active` · `PUT /api/codex-auth/accounts/priority` | 実効アカウント(固定中かどうかを示す `pinned` と、固定されているアカウントを示す `pinnedAccountId` を含む)を読み、アカウント 1 件の選択順序を設定します。 |
127136
| `POST /api/codex-auth/login` · `GET /api/codex-auth/login-status` | ブラウザログインでプールアカウントを追加します。 |
128137
| `GET /api/logs?tail=50&limit=20&offset=0&provider=...&status=5xx` | tail、プロバイダー、正確な状態コードまたは状態等級で最近のリクエストメタデータを参照します。`limit`/`offset` は最新行から過去方向にページングします(`offset=0` が最新ページ)。応答は `{ timeZone, total, logs }` で、`total` はページング前の一致件数です。 |
129138
| `GET` / `PUT /api/subagent-models` | `spawn_agent` に優先公開するモデル 5 つを読むか設定します。 |

‎docs-site/src/content/docs/ja/reference/cli/providers-accounts.md‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ ocx login anthropic
7676
実行中のプロキシを介してプロバイダー アカウントと API キー プールを一覧表示し、切り替えます。出荷されたヘルプ画面は次のとおりです。
7777

7878
```text
79-
Usage: ocx account <list|current|use|refresh|auto-switch|login|reauth|code|cancel|remove|add-key|reset-credits> ...
79+
Usage: ocx account <list|current|use|refresh|auto-switch|priority|login|reauth|code|cancel|remove|add-key|reset-credits> ...
8080
8181
list [provider] Codex account pool, OAuth accounts and API keys (identifiers shown masked as the API returns them).
8282
current <provider> Show the active account or key.
8383
use <provider> <id> Switch the active credential; 'main' selects the Codex App login.
8484
refresh <provider> Force-refresh Codex or provider quota reports.
8585
auto-switch <provider> <on|off|status|threshold N> Control the Codex pool threshold.
86+
priority <provider> <id|main> [first|earlier|normal|later|last|-100..100|reset] Selection order; omit the value to read it.
8687
remove <provider> <id> --yes Remove a stored account or key after an existence check.
8788
add-key <provider> [--label <label>] Add a key read only from piped stdin.
8889
login/reauth/code/cancel Run browser or manual-code auth from a headless shell.
@@ -102,6 +103,7 @@ Codex pool selection applies to the next request after clearing existing affinit
102103
"label": "plus",
103104
"email": "m***@example.com",
104105
"plan": "plus",
106+
"priority": 0,
105107
"masked": "sk-ab****wxyz",
106108
"active": true,
107109
"needsReauth": false,
@@ -111,7 +113,7 @@ Codex pool selection applies to the next request after clearing existing affinit
111113

112114
### `ocx account list [provider] [--json] [--all]`
113115

114-
プロバイダーを使用しない場合、Codex プール、OAuth アカウント、および設定された API キー プールが一覧表示されます。 `--all` が存在しない限り、空のプロバイダーはスキップされます。プロバイダーを使用すると、その資格情報ファミリーのみがリストされます。人間の出力では `PROVIDER TYPE ID PLAN/LABEL STATUS` を使用します。手動で選択した Codex 行には `selected` というマークが付けられます。保存された Kiro アカウントが存在する場合、出力には、Kiro には 1 つのログイン スロットがあり、再度サインインすると現在のアカウントが置き換えられることが示されます。結果が空であっても成功です。 `--json` は次を返します:
116+
プロバイダーを使用しない場合、Codex プール、OAuth アカウント、および設定された API キー プールが一覧表示されます。 `--all` が存在しない限り、空のプロバイダーはスキップされます。プロバイダーを使用すると、その資格情報ファミリーのみがリストされます。人間の出力では `PROVIDER TYPE ID PLAN/LABEL PRIORITY STATUS` を使用します。手動で選択した Codex 行には `selected` というマークが付けられます。保存された Kiro アカウントが存在する場合、出力には、Kiro には 1 つのログイン スロットがあり、再度サインインすると現在のアカウントが置き換えられることが示されます。結果が空であっても成功です。 `--json` は次を返します:
115117

116118
```text
117119
{ accounts: AccountRow[], notes: string[] }
@@ -153,6 +155,29 @@ OAuth プロバイダーと API キー プロバイダーの場合、これに
153155
{ provider, autoSwitchThreshold: number, enabled: boolean }
154156
```
155157

158+
### `ocx account priority <provider> <account-id|main> [<-100..100|first|earlier|normal|later|last|reset>] [--json]`
159+
160+
Codex pool のアカウント別選択順を読み書きします。**値が大きいほど先に使われ**、既定は `0`、範囲は
161+
`-100` から `100` です。順序を持つのは `openai` の Codex pool だけなので、他のプロバイダーは終了コード
162+
1 です。`main` は Codex Desktop ログインを指し、他の pool アカウントと同じように並べ替えられます。
163+
`ocx account priority openai main last` とすれば予備として最後に回せます。
164+
165+
プリセット語は小さな整数の別名です。`first` が `+2`、`earlier` が `+1`、`normal` が `0`、`later` が
166+
`-1`、`last` が `-2` です。`reset` は既定に戻し、保存されたエントリを削除します。**値を省略すると
167+
読み取り**になり、現在の順序を書き換えません。
168+
169+
順序が決めるのは「どのアカウントから見るか」であって「どれが使えるか」ではありません。選択は引き続き
170+
適格なアカウントの中で行われ、まだ quota に余裕がある最上位 tier を取り、その中は
171+
`accountPoolStrategy` が選びます。一時停止、cooldown、再認証には影響しません。変更は新しいセッションだけでなく **次の未バインドリクエスト** から適用されます。上位の順序に余裕が戻れば
172+
preemption が未バインドリクエストを直ちに引き上げます。既にアカウントに紐づいた thread は、通常はそのアカウントを
173+
使い切るまで維持します。ただし再認証エラー、quota cooldown、一時的な失敗の連続はそれより早く紐付けを解除します。受理された書き込みは、どのアカウントの手動の「今すぐこのアカウントを使う」固定も解除します。すでに設定済みの順序を書き込んだ場合も同様で、これは現在選択中のアカウントを保ったまま固定を解除する唯一の方法です(管理 API でアクティブアカウントを解除しても固定は解除されますが、その選択自体も失われます)。プロキシに接続できない場合、
174+
不明なアカウント id、受け付けない値はいずれも終了コード 1 です。`--json` は次を返します。
175+
176+
```text
177+
{ ok: true, provider, id, priority: number, preset: string | null }
178+
```
179+
180+
156181
### `ocx account login|reauth|code|cancel ...`
157182

158183
ヘッドレス シェルからブラウザベースまたは手動コードのアカウント認証を実行します。プロバイダー固有のコマンド形式には `ocx account --help` を使用します。

‎docs-site/src/content/docs/ja/reference/configuration/providers.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ description: プロバイダー エントリ、認証、エンドポイント、
1818
| `pausedCodexAccountIds?` | `string[]` | `[]` |再開するまでプールの選択から除外されるアカウント (一時停止時のメイン `__main__` アカウントを含む)。 |
1919
| `codexAccountNamespaces?` | `Record<string, string>` | — | 任意の公開 model selector を保存済み Codex アカウント target に対応付ける任意の map。target が存在する各 selector は Codex picker に個別の `<selector>/<native-openai-model>` row を追加し、各 row はそのアカウントだけを使用します。selector が 1 つでも有効な場合、bare native row は picker で非表示になりますが、明示的に無効化されない限り id は引き続き routing でき、raw `/v1/models` にも表示されます。 |
2020
| `activeCodexAccountId?` | `string` | — |次のリクエスト用に手動で選択されたプール アカウント。選択するとスレッドのアフィニティがクリアされます。実行中のリクエストでは、取得された資格情報が保持されます。 |
21+
| `codexAccountPriorities?` | `Record<string,number>` | — | Codex pool のアカウント別選択順。アカウント ID → `-100` から `100` の整数で、**大きいほど先に使われ**、未設定は `0` です。これは eligibility ではなく順序の境界です。選択は適格なアカウントを、まだ quota に余裕がある最上位 tier に絞り込み、その tier の中を `accountPoolStrategy` が選びます。tier が飛ばされるのは、そのメンバー全員が `autoSwitchThreshold` 超過、cooldown 中、soft-avoid、一時停止、または再認証待ちのときだけで、usage 不明が tier を drain させることはありません。順序付けが不適格なアカウントを選択可能にすることはなく、すでにアカウントが結び付いた thread を再 bind することもありません。メインの `__main__` も同じ条件で参加するため、Codex Desktop ログインを最後に使わせられます。エントリが 1 つもなければ挙動は従来どおりです。map が不正な場合は警告を出して順序付けを無効にします(config の修復処理は走りません)。`ocx account priority` と Codex Auth ページで管理します。 |
2122
| `autoSwitchThreshold?` | `number` | `80` | 使用量ベースのプロアクティブ切り替えしきい値。`quota` は紐付け済み/未紐付けタスクの次のリクエストを再評価でき、`fill-first` は未紐付け割り当ての使い切り基準としてのみ使用し、通常の `round-robin` 選択は使用しません。既知の 5 時間、週次、30 日 quota window の最大スコアを使います。`0` は使用量ベースの切り替えだけを無効にし、未紐付け割り当てや障害回復は無効にしません。 |
2223
| `accountPoolStrategy?` | `"quota" \| "round-robin" \| "fill-first"` | `"quota"` | 新規/未紐付け Codex リクエストの割り当て戦略。live な `(parent thread id, quota scope)` affinity がなければ未紐付けで、プロキシ再起動や affinity リセット後は既存の表示タスクも未紐付けになり得ます。`quota` はアクティブアカウントがなければ既知 usage 最小の適格アカウントを選び、適格なアクティブアカウントが `autoSwitchThreshold` 未満なら維持します。しきい値到達後は、未紐付けリクエストまたは紐付け済みタスクの次のリクエストを usage の低い適格アカウントへ移せます。`round-robin` は未紐付けリクエストを均等分散し、`fill-first` は cooldown、使用不可、または drain threshold までアクティブアカウントへ割り当てます。 |
2324
| `accountPoolStickyLimit?` | `number` | `1` | 1 回の round-robin 選択で次へ進む前に保持する新規/未紐付けタスク割り当て数。カウンターは上流の成功後ではなくタスクの紐付け時に増えます。範囲 1–100。`accountPoolStrategy` が `round-robin` のときのみ。 |

0 commit comments

Comments
 (0)