@bitkyc08/opencodex 2.94.0 → 2.96.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 (289) hide show
  1. package/gui/dist/assets/App-B4j-Txmb.css +1 -0
  2. package/gui/dist/assets/App-bwugJRKn.js +58 -0
  3. package/gui/dist/assets/{Tray-CZatQdoF.js → Tray-BOPQyDbH.js} +1 -1
  4. package/gui/dist/assets/index-ebuVUtB7.js +94 -0
  5. package/gui/dist/assets/quota-summary-6HsrHQAy.js +1 -0
  6. package/gui/dist/index.html +1 -1
  7. package/package.json +1 -1
  8. package/src/adapters/anthropic.ts +10 -3
  9. package/src/adapters/command-code-project-context.ts +3 -3
  10. package/src/adapters/kiro/adapter.ts +4 -1
  11. package/src/adapters/kiro/stream.ts +1 -0
  12. package/src/adapters/kiro-events.ts +1 -0
  13. package/src/adapters/ollama-native.ts +10 -6
  14. package/src/adapters/openai-chat/messages.ts +20 -7
  15. package/src/adapters/openai-chat/passthrough.ts +4 -2
  16. package/src/adapters/openai-chat/single-system.ts +24 -0
  17. package/src/adapters/openai-responses/canonical-forward.ts +50 -13
  18. package/src/adapters/openai-responses/muse-tool-catalog.ts +87 -0
  19. package/src/adapters/openai-responses/passthrough.ts +45 -11
  20. package/src/adapters/openai-responses/prompt-cache.ts +3 -1
  21. package/src/adapters/openai-responses/request-strips.ts +23 -4
  22. package/src/adapters/openai-responses/tool-output-recovery.ts +21 -8
  23. package/src/adapters/openai-responses/tool-schema.ts +20 -8
  24. package/src/adapters/openai-responses/transport.ts +17 -26
  25. package/src/adapters/opencode-go-additional-tools.ts +39 -6
  26. package/src/adapters/qoder/adapter.ts +1 -1
  27. package/src/adapters/qoder/image-input.ts +5 -2
  28. package/src/adapters/routed-agent-messages.ts +3 -0
  29. package/src/adapters/xai-web-search.ts +6 -3
  30. package/src/adapters/zed-additional-tools.ts +7 -7
  31. package/src/adapters/zed.ts +1 -6
  32. package/src/advisor/consult.ts +22 -10
  33. package/src/advisor/runtime.ts +10 -4
  34. package/src/bridge/max-output-completion.ts +41 -0
  35. package/src/bridge/response-json.ts +9 -4
  36. package/src/bridge/sse.ts +13 -2
  37. package/src/chatgpt/app-server-shim/bundle-trust-policy.ts +5 -0
  38. package/src/chatgpt/app-server-shim/launcher.ts +40 -6
  39. package/src/claude/compatibility.ts +5 -1
  40. package/src/claude/desktop-3p.ts +27 -6
  41. package/src/claude/desktop-model-visibility.ts +141 -0
  42. package/src/claude/desktop-remote-store-io.ts +8 -8
  43. package/src/claude/inbound-content-options.ts +11 -1
  44. package/src/claude/intercept/proxy-auth.ts +16 -16
  45. package/src/cli/account-main.ts +3 -2
  46. package/src/cli/agent.ts +44 -15
  47. package/src/cli/capabilities-agents-routing.ts +9 -0
  48. package/src/cli/capabilities-provider-models.ts +7 -0
  49. package/src/cli/chatgpt-command.ts +1 -1
  50. package/src/cli/codex-launcher.ts +20 -8
  51. package/src/cli/codex-shim-autorestore.ts +19 -1
  52. package/src/cli/dispatch.ts +6 -6
  53. package/src/cli/doctor.ts +5 -5
  54. package/src/cli/ensure-desired-integrations.ts +4 -3
  55. package/src/cli/index.ts +8 -10
  56. package/src/cli/init.ts +2 -2
  57. package/src/cli/internal-command.ts +29 -2
  58. package/src/cli/message-command.ts +2 -1
  59. package/src/cli/message-mcp-command.ts +29 -0
  60. package/src/cli/provider-cc-switch-import.ts +185 -0
  61. package/src/cli/provider.ts +5 -0
  62. package/src/cli/registry.ts +2 -1
  63. package/src/cli/restart-scope.ts +2 -2
  64. package/src/cli/root.ts +6 -6
  65. package/src/cli/stop-approval.ts +15 -1
  66. package/src/cli/v2.ts +69 -19
  67. package/src/client/machine-api.ts +2 -2
  68. package/src/clients/config-export.ts +14 -3
  69. package/src/codex/account-cooldown-response.ts +18 -0
  70. package/src/codex/account-store.ts +1 -0
  71. package/src/codex/app-server-restart-service.ts +2 -2
  72. package/src/codex/auth-api/login-flow.ts +2 -2
  73. package/src/codex/auth-api/pool-quota-probe.ts +2 -2
  74. package/src/codex/auth-context-provider.ts +15 -0
  75. package/src/codex/auth-context.ts +14 -23
  76. package/src/codex/catalog/bundled.ts +10 -3
  77. package/src/codex/catalog/metadata.ts +4 -5
  78. package/src/codex/catalog/native-omission.ts +28 -0
  79. package/src/codex/catalog/native-template.ts +58 -0
  80. package/src/codex/catalog/parsing.ts +3 -29
  81. package/src/codex/catalog/provider-models.ts +2 -1
  82. package/src/codex/catalog/retained-sync.ts +15 -5
  83. package/src/codex/catalog/write-audit.ts +18 -18
  84. package/src/codex/cli-install-provenance.ts +46 -45
  85. package/src/codex/cli-update-artifacts.ts +8 -6
  86. package/src/codex/cli-update-job-store-windows.ts +99 -27
  87. package/src/codex/cli-update-job-worker.ts +3 -11
  88. package/src/codex/cli-update-recovery.ts +54 -8
  89. package/src/codex/cli-update-selection-gate.ts +74 -4
  90. package/src/codex/cli-windows-plan-provenance.ts +37 -19
  91. package/src/codex/codex-home-owner.ts +9 -8
  92. package/src/codex/convergence.ts +13 -3
  93. package/src/codex/coordinator-doctor.ts +14 -8
  94. package/src/codex/desktop-app/handoff.ts +1 -1
  95. package/src/codex/desktop-app-restart.ts +11 -1
  96. package/src/codex/exec-boundary.ts +26 -5
  97. package/src/codex/forward-transport-headers.ts +33 -33
  98. package/src/codex/history-provider.ts +11 -10
  99. package/src/codex/inject/bounded-config-reader.ts +14 -13
  100. package/src/codex/installation-id.ts +72 -0
  101. package/src/codex/main-account.ts +6 -6
  102. package/src/codex/native-main-lock-file.ts +13 -12
  103. package/src/codex/native-residue.ts +18 -14
  104. package/src/codex/orca-auth-source.ts +8 -7
  105. package/src/codex/quota-types.ts +5 -0
  106. package/src/codex/quota.ts +49 -7
  107. package/src/codex/role-auto-assign.ts +50 -26
  108. package/src/codex/role-capability.ts +160 -0
  109. package/src/codex/role-sizing.ts +24 -6
  110. package/src/codex/routing/cooldown-math.ts +3 -2
  111. package/src/codex/runtime.ts +43 -3
  112. package/src/codex/shim-fingerprint.ts +11 -9
  113. package/src/codex/shim-inspect.ts +3 -2
  114. package/src/codex/shim-restore-lock.ts +11 -14
  115. package/src/codex/shim-state-file.ts +22 -14
  116. package/src/codex/shim-wrapper-write.ts +7 -6
  117. package/src/codex/shim.ts +25 -2
  118. package/src/codex/transition-state.ts +5 -4
  119. package/src/combos/failover.ts +43 -4
  120. package/src/config/atomic-write.ts +38 -7
  121. package/src/config/diagnostics.ts +3 -0
  122. package/src/config/initialize.ts +6 -6
  123. package/src/config/load-degrade.ts +2 -0
  124. package/src/config/process-state.ts +23 -5
  125. package/src/config/salvage.ts +1 -0
  126. package/src/config/schema/config-schema.ts +29 -1
  127. package/src/config/schema/leaf-validators.ts +7 -0
  128. package/src/config/schema/workflow-budget.ts +43 -0
  129. package/src/config/tracked-runtime-inspection.ts +188 -0
  130. package/src/generated/compatibility-version.json +380 -244
  131. package/src/grok/sync.ts +10 -1
  132. package/src/integrations/aside-profile-context.ts +1 -1
  133. package/src/integrations/droid-settings.ts +4 -4
  134. package/src/integrations/hermes-profiles.ts +3 -3
  135. package/src/lib/bounded-file-read.ts +3 -9
  136. package/src/lib/bun-runtime-preflight.ts +9 -8
  137. package/src/lib/file-identity.ts +15 -0
  138. package/src/lib/local-chat-completion.ts +8 -6
  139. package/src/lib/local-destinations.ts +32 -3
  140. package/src/lib/redact-folding.ts +9 -0
  141. package/src/lib/spend-pool-alias-validation.ts +6 -6
  142. package/src/lib/state-store-registrations.ts +2 -0
  143. package/src/lib/test-home-guard.ts +37 -9
  144. package/src/lib/windows-secret-acl.ts +49 -31
  145. package/src/lib/workflow-budget-config.ts +39 -0
  146. package/src/local-credentials/generation.ts +6 -2
  147. package/src/local-credentials/inventory.ts +132 -8
  148. package/src/messaging/mcp-queue-rpc.ts +102 -0
  149. package/src/messaging/mcp-queue-server.ts +42 -0
  150. package/src/messaging/mcp-queue-tools.ts +39 -0
  151. package/src/messaging/remote-forward-failure.ts +35 -0
  152. package/src/messaging/remote-ports.ts +27 -5
  153. package/src/messaging/remote-process.ts +30 -4
  154. package/src/messaging/remote-tunnels.ts +80 -15
  155. package/src/oauth/anthropic-identity.ts +2 -6
  156. package/src/oauth/antigravity-discovery-diagnostics.ts +65 -0
  157. package/src/oauth/google-antigravity.ts +23 -10
  158. package/src/oauth/index.ts +7 -0
  159. package/src/oauth/kiro-device-login.ts +87 -30
  160. package/src/oauth/kiro-iam-identity-center.ts +107 -0
  161. package/src/oauth/store.ts +9 -7
  162. package/src/providers/account-quota-disk.ts +18 -4
  163. package/src/providers/anthropic-subscription.ts +25 -0
  164. package/src/providers/cc-switch-import-bytes.ts +991 -0
  165. package/src/providers/cc-switch-import-source.ts +1083 -0
  166. package/src/providers/cc-switch-import.ts +942 -0
  167. package/src/providers/claude-cli-usage.ts +15 -67
  168. package/src/providers/derive.ts +6 -0
  169. package/src/providers/model-discovery-diagnostic.ts +4 -0
  170. package/src/providers/model-discovery.ts +4 -4
  171. package/src/providers/model-rename-fields.ts +2 -0
  172. package/src/providers/model-rename-migration.ts +41 -6
  173. package/src/providers/quota/anthropic-decision-windows.ts +5 -19
  174. package/src/providers/quota/anthropic-usage-windows.ts +55 -0
  175. package/src/providers/quota/report-cache.ts +17 -4
  176. package/src/providers/quota.ts +2 -2
  177. package/src/providers/reasoning-metadata.ts +31 -15
  178. package/src/providers/registry/entries-core.ts +1 -1
  179. package/src/providers/registry/entries-extended.ts +1 -1
  180. package/src/providers/registry/model-ids.ts +1 -0
  181. package/src/providers/registry/model-seeds.ts +2 -1
  182. package/src/providers/registry/types.ts +3 -1
  183. package/src/providers/resolved-model-policy.ts +2 -2
  184. package/src/providers/routed-config.ts +2 -0
  185. package/src/providers/stale-model-roster-migration.ts +5 -1
  186. package/src/providers/xai-transport.ts +4 -4
  187. package/src/remote-control/workspace-codex-runtime.ts +2 -2
  188. package/src/remote-control/workspace-executor.ts +14 -13
  189. package/src/responses/custom-tool-compat.ts +2 -1
  190. package/src/responses/item-provenance.ts +79 -0
  191. package/src/responses/namespace-tool-compat.ts +21 -3
  192. package/src/responses/parser-tools.ts +1 -0
  193. package/src/responses/schema.ts +5 -2
  194. package/src/router.ts +21 -2
  195. package/src/routing/analytics.ts +3 -5
  196. package/src/routing/fallback-policy.ts +7 -3
  197. package/src/routing/health.ts +5 -3
  198. package/src/routing/history/build-file.ts +6 -0
  199. package/src/routing/history/indexer.ts +158 -31
  200. package/src/routing/history/ingest.ts +1 -1
  201. package/src/routing/history/integrity.ts +208 -0
  202. package/src/routing/history/rebuild-job.ts +20 -6
  203. package/src/routing/history/rebuild-protocol.ts +2 -1
  204. package/src/routing/history/rebuild-worker.ts +18 -0
  205. package/src/routing/history/sqlite-errors.ts +9 -0
  206. package/src/server/admission-model-scope.ts +21 -4
  207. package/src/server/advisor-admission-scope.ts +29 -0
  208. package/src/server/audio-dictation.ts +3 -0
  209. package/src/server/audio-live.ts +8 -2
  210. package/src/server/audio-upstream.ts +2 -0
  211. package/src/server/auth-cors.ts +5 -2
  212. package/src/server/capture-admission-policy.ts +91 -0
  213. package/src/server/chat-completions.ts +33 -7
  214. package/src/server/claude-messages.ts +93 -2
  215. package/src/server/codex-model-list.ts +2 -1
  216. package/src/server/configured-credentials.ts +5 -0
  217. package/src/server/context-history.ts +9 -6
  218. package/src/server/images.ts +14 -5
  219. package/src/server/index/live-sideband.ts +16 -2
  220. package/src/server/index/serve-options.ts +13 -2
  221. package/src/server/index/websocket-handler.ts +4 -1
  222. package/src/server/live.ts +30 -5
  223. package/src/server/management/agent-settings-routes.ts +34 -9
  224. package/src/server/management/codex-agent-role-routes.ts +1 -0
  225. package/src/server/management/codex-role-auto-assign.ts +50 -6
  226. package/src/server/management/config-routes.ts +15 -4
  227. package/src/server/management/context.ts +3 -0
  228. package/src/server/management/native-integration-routes.ts +22 -0
  229. package/src/server/management/oauth-account-routes.ts +20 -4
  230. package/src/server/management/provider-overwrite-carry.ts +21 -0
  231. package/src/server/management/provider-routes.ts +3 -1
  232. package/src/server/management/route-registry.ts +2 -0
  233. package/src/server/management/shared.ts +17 -1
  234. package/src/server/management/workflow-budget-routes.ts +70 -3
  235. package/src/server/messages-native-eligibility.ts +6 -5
  236. package/src/server/messages-native-scope.ts +6 -1
  237. package/src/server/messages-shared.ts +57 -17
  238. package/src/server/persistent-gui-session.ts +4 -4
  239. package/src/server/request-log.ts +30 -2
  240. package/src/server/responses/adapter-delivery.ts +4 -0
  241. package/src/server/responses/adapter-dispatch.ts +5 -0
  242. package/src/server/responses/advisor-plan-slot.ts +4 -0
  243. package/src/server/responses/advisor-slot.ts +73 -46
  244. package/src/server/responses/codex-auth-error.ts +9 -1
  245. package/src/server/responses/combo-required-tool-contract.ts +39 -0
  246. package/src/server/responses/compact.ts +41 -20
  247. package/src/server/responses/core-codex-account.ts +17 -6
  248. package/src/server/responses/core-combo-failure.ts +2 -0
  249. package/src/server/responses/core-combo.ts +70 -5
  250. package/src/server/responses/core-lifetime.ts +4 -0
  251. package/src/server/responses/core-normalize.ts +7 -1
  252. package/src/server/responses/core-options.ts +9 -0
  253. package/src/server/responses/core.ts +2 -0
  254. package/src/server/responses/jev-model-invoke.ts +4 -2
  255. package/src/server/responses/passthrough-build-error.ts +36 -0
  256. package/src/server/responses/passthrough-dispatch.ts +10 -24
  257. package/src/server/responses/policy-fallback.ts +2 -0
  258. package/src/server/responses/request-prepare.ts +4 -5
  259. package/src/server/responses/request-send-budget.ts +2 -1
  260. package/src/server/responses/request-sidecar-auth.ts +4 -1
  261. package/src/server/responses/request-transport.ts +2 -3
  262. package/src/server/responses/run-turn-execution.ts +4 -0
  263. package/src/server/responses/sidecar-execution.ts +21 -5
  264. package/src/server/responses-snapshot-repair.ts +18 -4
  265. package/src/server/responses-undeclared-tool-guard.ts +4 -0
  266. package/src/server/search.ts +13 -4
  267. package/src/server/workflow-refusal.ts +2 -1
  268. package/src/server/ws-bridge.ts +5 -2
  269. package/src/service/state-lock.ts +12 -11
  270. package/src/service-manager-probe.ts +53 -8
  271. package/src/types/admission-policy.ts +13 -0
  272. package/src/types/config.ts +25 -0
  273. package/src/types/provider.ts +10 -0
  274. package/src/types/request.ts +7 -0
  275. package/src/types.ts +1 -0
  276. package/src/update/job.ts +20 -0
  277. package/src/update/worker-launch.ts +71 -1
  278. package/src/usage/log.ts +25 -4
  279. package/src/vision/describe.ts +4 -7
  280. package/src/vision/index.ts +2 -0
  281. package/src/vision/plan.ts +4 -0
  282. package/src/vision/routed-describe.ts +38 -23
  283. package/src/web-search/executor.ts +4 -7
  284. package/src/web-search/loop.ts +104 -9
  285. package/src/web-search/parse.ts +10 -2
  286. package/gui/dist/assets/App-DMps4OLk.css +0 -1
  287. package/gui/dist/assets/App-DmvwlhQq.js +0 -58
  288. package/gui/dist/assets/index-3EPO_KbX.js +0 -94
  289. package/gui/dist/assets/quota-summary-BHvWg9mi.js +0 -1
@@ -5,6 +5,7 @@ import { forgetMainAccountUsage, observeMainAccountUsage, type FreshWindow } fro
5
5
  import { closeSync, constants as fsConstants, existsSync, fstatSync, openSync, readSync, unlinkSync } from "node:fs";
6
6
  import { join } from "node:path";
7
7
  import { atomicWriteFile, getConfigDir } from "../config";
8
+ import { withConfigDirHardeningSettled } from "../config/paths";
8
9
  import { captureConfigGeneration, type GenerationContext } from "../lib/state-store-sweeper";
9
10
  import { isThirtyDayOnlyCodexPlan } from "./plan";
10
11
  import { stampCodexQuotaUsageObservation } from "./quota-observation-freshness";
@@ -42,6 +43,12 @@ let mainPolicyQuota: MainPolicyQuota | null = null;
42
43
  let diskHydrated = false;
43
44
  let persistTimer: ReturnType<typeof setTimeout> | null = null;
44
45
  let persistStartedAt: number | null = null;
46
+ /** The latest admitted snapshot write; settles even when admission times out. */
47
+ let persistWrite: Promise<void> = Promise.resolve();
48
+ /** Bumped by a full clear so a fired callback still waiting for ACL admission writes nothing. */
49
+ let persistGeneration = 0;
50
+ /** Test seam: wait for a fired persistence callback's admitted write. */
51
+ export function settleQuotaPersistenceForTests(): Promise<void> { return persistWrite; }
45
52
 
46
53
  const MONTHLY_WINDOW_MIN_SECONDS = 28 * 24 * 60 * 60;
47
54
  /**
@@ -333,7 +340,10 @@ export function setAccountQuotaFromParsed(
333
340
  const mainDecisionGeneration = mainWriter && mainDecisionQuotaWriterGeneration(mainWriter);
334
341
  if (isMain && mainDecisionGeneration !== undefined && decisionRaw)
335
342
  publishCodexDecisionQuota(accountId, mainDecisionGeneration, decisionRaw, updatedAt, "codex-main", decisionObservationPartial);
336
- const displayQuota: MainPolicyQuotaObservation = isMain && mainWriter && policyQuota?.shortWindowAbsent === true
343
+ // Retiring a short tuple releases a routing block, so it needs the same writer proof as
344
+ // history: a live main writer for main, a live captured pool writer for pool requests.
345
+ const mayRetireShort = isMain ? mainWriter !== undefined : livePoolEvidence;
346
+ const displayQuota: MainPolicyQuotaObservation = mayRetireShort && policyQuota?.shortWindowAbsent === true
337
347
  ? { ...quota, shortWindowAbsent: true }
338
348
  : quota;
339
349
  const next = mergeAccountQuota(displayQuota, legacyExisting, updatedAt);
@@ -797,7 +807,12 @@ function schedulePersistAccountQuotas(): void {
797
807
  persistTimer = setTimeout(() => {
798
808
  persistTimer = null;
799
809
  persistStartedAt = null;
800
- try {
810
+ // Admitted after an overlapping optional config-root ACL flight (#7815); the snapshot is
811
+ // taken inside the callback, and a timed-out admission skips this best-effort write.
812
+ const dir = getConfigDir();
813
+ const generation = persistGeneration;
814
+ persistWrite = withConfigDirHardeningSettled(dir, () => {
815
+ if (generation !== persistGeneration) return;
801
816
  const quotas: Record<string, StoredAccountQuota> = {};
802
817
  for (const [accountId, quota] of accountQuota.entries()) {
803
818
  quotas[accountId] = quota;
@@ -808,10 +823,10 @@ function schedulePersistAccountQuotas(): void {
808
823
  ...(mainPolicyQuota ? { mainPolicyQuota } : {}),
809
824
  history: quotaHistory.serialize(),
810
825
  };
811
- atomicWriteFile(join(getConfigDir(), QUOTA_CACHE_FILENAME), `${JSON.stringify(body)}\n`);
812
- } catch {
826
+ atomicWriteFile(join(dir, QUOTA_CACHE_FILENAME), `${JSON.stringify(body)}\n`);
827
+ }).catch(() => {
813
828
  // Best-effort persistence only.
814
- }
829
+ });
815
830
  }, delay);
816
831
  }
817
832
 
@@ -887,6 +902,7 @@ export function clearAccountQuota(accountId?: string): void {
887
902
  persistTimer = null;
888
903
  }
889
904
  persistStartedAt = null;
905
+ persistGeneration += 1;
890
906
  try {
891
907
  const path = join(getConfigDir(), QUOTA_CACHE_FILENAME);
892
908
  if (existsSync(path)) unlinkSync(path);
@@ -934,7 +950,14 @@ export function parseMainPolicyUsageQuota(data: WhamUsageResponse): MainPolicyQu
934
950
  const windows = [data.rate_limit?.primary_window, data.rate_limit?.secondary_window, data.rate_limit?.tertiary_window];
935
951
  if (windows.some(window => isInvalidPolicyUsagePercent(window?.used_percent))) return null;
936
952
  const quota = filterMainPolicyMonthlyQuota(parseUsageQuota(data), isThirtyDayOnlyCodexPlan(data.plan_type));
937
- const [primary, secondary, tertiary] = windows;
953
+ return quota ? withWhamShortWindowAbsence(data, quota) : null;
954
+ }
955
+
956
+ /** Derive absence from complete WHAM topology without changing the display quota. */
957
+ export function withWhamShortWindowAbsence(
958
+ data: WhamUsageResponse, quota: Omit<StoredAccountQuota, "updatedAt">,
959
+ ): MainPolicyQuotaObservation {
960
+ const [primary, secondary, tertiary] = [data.rate_limit?.primary_window, data.rate_limit?.secondary_window, data.rate_limit?.tertiary_window];
938
961
  // Primary and secondary must explicitly report their topology. WHAM also sends
939
962
  // two-window responses without tertiary; an explicit allowed verdict disambiguates those
940
963
  // from partial observations. A present tertiary still requires measured long/null evidence.
@@ -942,7 +965,7 @@ export function parseMainPolicyUsageQuota(data: WhamUsageResponse): MainPolicyQu
942
965
  // carries a valid usage reading: a long window without used_percent leaves that
943
966
  // window's usage unknown, and unknown usage must never release a block.
944
967
  // Headers never supply this proof, and reset time alone still cannot release a block.
945
- if (quota && (isMeasuredLongWindow(primary)
968
+ if ((isMeasuredLongWindow(primary)
946
969
  || (primary === null && isMeasuredLongWindow(secondary) && quota.weeklyPercent !== undefined))
947
970
  && (secondary === null || isMeasuredLongWindow(secondary))
948
971
  && (tertiary === null || isMeasuredLongWindow(tertiary)
@@ -952,6 +975,25 @@ export function parseMainPolicyUsageQuota(data: WhamUsageResponse): MainPolicyQu
952
975
  return quota;
953
976
  }
954
977
 
978
+ /**
979
+ * Policy evidence for a stored pool account's WHAM read (pool probe or re-login): null when any
980
+ * reading is invalid. A complete long/null topology marks short-window absence (#7961) unless
981
+ * upstream explicitly denied the account, since a denial is never evidence that a block ended.
982
+ */
983
+ export function poolWhamPolicyObservation(
984
+ data: WhamUsageResponse, quota: Omit<StoredAccountQuota, "updatedAt"> | null,
985
+ ): MainPolicyQuotaObservation | null {
986
+ if (!quota || !isValidWhamHistoryObservation(data)) return null;
987
+ // Retirement releases a block, so a present reading that does not parse is not "unknown".
988
+ const readings = [data.rate_limit?.primary_window, data.rate_limit?.secondary_window, data.rate_limit?.tertiary_window];
989
+ for (const limit of Array.isArray(data.additional_rate_limits) ? data.additional_rate_limits : []) {
990
+ if (limit && typeof limit === "object") readings.push(limit.rate_limit?.primary_window, limit.rate_limit?.secondary_window);
991
+ }
992
+ const malformed = readings.some(window => window?.used_percent != null
993
+ && normalizeUsagePercent(window.used_percent) === undefined);
994
+ return data.rate_limit?.allowed === false || malformed ? quota : withWhamShortWindowAbsence(data, quota);
995
+ }
996
+
955
997
  function isMeasuredLongWindow(window: WhamUsageWindow | null | undefined): boolean {
956
998
  return isExplicitLongWindow(window) && normalizeUsagePercent(window?.used_percent) !== undefined;
957
999
  }
@@ -4,19 +4,23 @@
4
4
  *
5
5
  * Tier of a candidate model, first rule that applies:
6
6
  * 1. codexRoleTiers in the opencodex config lists it under fast, standard or frontier.
7
- * 2. It has a known price. Priced candidates are ranked by input plus output USD per 1M tokens.
7
+ * 2. At least two candidates have a measured score in the role's task domain. Measured candidates
8
+ * are ranked by that score and split across the tiers the same way price splits them below;
9
+ * unmeasured ones are left out rather than placed among measured ones by price.
10
+ * 3. It has a known price. Priced candidates are ranked by input plus output USD per 1M tokens.
8
11
  * Three or more split evenly across the three tiers by rank, cheapest in fast. Fewer than three
9
12
  * cannot fill every tier, so the ranking is anchored at the top: the dearest is frontier,
10
13
  * because it is the strongest model on offer, and each cheaper one sits one tier lower. One
11
14
  * priced model is frontier; two are standard and frontier. The tier left empty is fast, so a
12
15
  * fast role steps up to the cheapest model and a standard role keeps it instead of jumping to
13
16
  * the dearest, which keeps every role on the cheapest sufficient model on offer.
14
- * 3. Otherwise it is unclassified and never proposed.
17
+ * 4. Otherwise it is unclassified and never proposed.
15
18
  *
16
19
  * A role gets the cheapest sufficient model: the lowest tier at or above what it was sized for,
17
- * then the lowest price, then the model the role already uses when it is one of the tied models,
18
- * then the picker's own order. The picker lists native models first, so without the current-model
19
- * step a role on a routed model moved to a native one priced and tiered the same.
20
+ * then the lowest price, then the higher measured score, then the model the role already uses when
21
+ * it is one of the tied models, then the picker's own order. The picker lists native models first,
22
+ * so without the current-model step a role on a routed model moved to a native one priced and
23
+ * tiered the same.
20
24
  *
21
25
  * Effort binds to positions on the chosen model's ladder: glance is the floor, measured is the
22
26
  * model's default or the middle rung, exhaustive is the ceiling, and thorough is the rung above
@@ -25,7 +29,7 @@
25
29
  * above wins because thorough asks for more than the everyday setting. Nothing resolves outside
26
30
  * the ladder.
27
31
  */
28
- import { SIZING_TIERS, type RoleSizingOutcome, type SizingEffort, type SizingTier } from "./role-sizing";
32
+ import { SIZING_TIERS, type RoleSizingOutcome, type SizingDomain, type SizingEffort, type SizingTier } from "./role-sizing";
29
33
  import { CODEX_REASONING_LEVELS } from "../reasoning-effort";
30
34
 
31
35
  export type RoleTierMapping = Partial<Record<SizingTier, readonly string[]>>;
@@ -40,7 +44,9 @@ export interface RoleModelCandidate {
40
44
 
41
45
  export interface ClassifiedCandidate extends RoleModelCandidate {
42
46
  readonly tier: SizingTier | null;
43
- readonly tierSource: "mapping" | "price" | null;
47
+ readonly tierSource: "mapping" | "measured" | "price" | null;
48
+ /** The measured score the tier came from, or null when it came from elsewhere. */
49
+ readonly score: number | null;
44
50
  }
45
51
 
46
52
  const LADDER_ORDER = ["none", "minimal", ...CODEX_REASONING_LEVELS.map(level => level.effort)];
@@ -66,33 +72,45 @@ export function mapEffortToLevel(intent: SizingEffort, efforts: readonly string[
66
72
  }
67
73
  }
68
74
 
75
+ /** Split items ranked weakest first across the tiers; fewer than three are anchored at the top. */
76
+ function tierByRank<T>(ranked: readonly T[]): Map<T, SizingTier> {
77
+ const top = SIZING_TIERS.length - 1;
78
+ return new Map(ranked.map((item, rank) => [item, SIZING_TIERS[ranked.length < SIZING_TIERS.length
79
+ ? top - (ranked.length - 1 - rank)
80
+ : Math.round((rank * top) / (ranked.length - 1))]!]));
81
+ }
82
+
69
83
  export function classifyRoleModelCandidates(
70
84
  candidates: readonly RoleModelCandidate[],
71
85
  mapping: RoleTierMapping = {},
86
+ measured: ReadonlyMap<string, number> = new Map(),
72
87
  ): ClassifiedCandidate[] {
73
88
  const mapped = new Map<string, SizingTier>();
74
89
  for (const tier of SIZING_TIERS) {
75
90
  for (const model of mapping[tier] ?? []) if (!mapped.has(model)) mapped.set(model, tier);
76
91
  }
77
- const priced = candidates
78
- .map((candidate, order) => ({ candidate, order }))
79
- .filter(({ candidate }) => !mapped.has(candidate.model) && candidate.unitPrice !== null)
80
- .sort((a, b) => a.candidate.unitPrice! - b.candidate.unitPrice! || a.order - b.order);
81
- const byPrice = new Map<string, SizingTier>();
82
- priced.forEach(({ candidate }, rank) => {
83
- const top = SIZING_TIERS.length - 1;
84
- const index = priced.length < SIZING_TIERS.length
85
- ? top - (priced.length - 1 - rank)
86
- : Math.round((rank * top) / (priced.length - 1));
87
- byPrice.set(candidate.model, SIZING_TIERS[index]!);
88
- });
92
+ const unmapped = candidates.filter(candidate => !mapped.has(candidate.model));
93
+ const scored = unmapped.filter(candidate => measured.has(candidate.model));
94
+ const scoreTiers = tierByRank([...new Set(scored.map(candidate => measured.get(candidate.model)!))].sort((a, b) => a - b));
95
+ const byScore = scored.length >= 2
96
+ ? new Map(scored.map(candidate => [candidate.model, scoreTiers.get(measured.get(candidate.model)!)!] as const))
97
+ : new Map<string, SizingTier>();
98
+ const byPrice = byScore.size > 0
99
+ ? new Map<string, SizingTier>()
100
+ : tierByRank(unmapped
101
+ .map((candidate, order) => ({ candidate, order }))
102
+ .filter(({ candidate }) => candidate.unitPrice !== null)
103
+ .sort((a, b) => a.candidate.unitPrice! - b.candidate.unitPrice! || a.order - b.order)
104
+ .map(({ candidate }) => candidate.model));
89
105
  return candidates.map(candidate => {
90
106
  const fromMapping = mapped.get(candidate.model);
91
- if (fromMapping) return { ...candidate, tier: fromMapping, tierSource: "mapping" as const };
107
+ if (fromMapping) return { ...candidate, tier: fromMapping, tierSource: "mapping" as const, score: null };
108
+ const fromScore = byScore.get(candidate.model);
109
+ if (fromScore) return { ...candidate, tier: fromScore, tierSource: "measured" as const, score: measured.get(candidate.model)! };
92
110
  const fromPrice = byPrice.get(candidate.model);
93
111
  return fromPrice
94
- ? { ...candidate, tier: fromPrice, tierSource: "price" as const }
95
- : { ...candidate, tier: null, tierSource: null };
112
+ ? { ...candidate, tier: fromPrice, tierSource: "price" as const, score: null }
113
+ : { ...candidate, tier: null, tierSource: null, score: null };
96
114
  });
97
115
  }
98
116
 
@@ -107,6 +125,7 @@ export function cheapestSufficientCandidate(
107
125
  .filter(entry => entry.rank >= need)
108
126
  .sort((a, b) => a.rank - b.rank
109
127
  || (a.candidate.unitPrice ?? Infinity) - (b.candidate.unitPrice ?? Infinity)
128
+ || (b.candidate.score ?? -Infinity) - (a.candidate.score ?? -Infinity)
110
129
  || Number(b.candidate.model === current) - Number(a.candidate.model === current)
111
130
  || a.order - b.order);
112
131
  return ranked[0]?.candidate ?? null;
@@ -123,10 +142,13 @@ export type RoleProposal = RoleProposalInput & (
123
142
  readonly status: "proposed" | "unassigned";
124
143
  readonly tier: SizingTier;
125
144
  readonly effortIntent: SizingEffort;
145
+ readonly domain: SizingDomain;
126
146
  readonly rationale: string;
127
147
  readonly moveUpIf: string;
128
148
  readonly moveDownIf: string;
129
149
  readonly proposedModel: string | null;
150
+ /** Where the picked model's tier came from: a user mapping, a measured score, or price rank. */
151
+ readonly tierSource: ClassifiedCandidate["tierSource"];
130
152
  /** The sized effort on the picked model's ladder; null only when that model has no ladder. */
131
153
  readonly proposedEffort: string | null;
132
154
  readonly reason: string | null;
@@ -138,23 +160,25 @@ export function buildRoleProposals(
138
160
  roles: readonly RoleProposalInput[],
139
161
  sizing: ReadonlyMap<string, RoleSizingOutcome>,
140
162
  classified: readonly ClassifiedCandidate[],
163
+ classifiedByDomain: ReadonlyMap<SizingDomain, readonly ClassifiedCandidate[]> = new Map(),
141
164
  ): RoleProposal[] {
142
165
  return roles.map(role => {
143
166
  const outcome = sizing.get(role.role) ?? { unsized: "the role was not sized" };
144
167
  if ("unsized" in outcome) return { ...role, status: "unsized", reason: outcome.unsized };
145
- const { tier, effort, rationale, moveUpIf, moveDownIf } = outcome.sizing;
146
- const pick = cheapestSufficientCandidate(classified, tier, role.model);
147
- const sized = { ...role, tier, effortIntent: effort, rationale, moveUpIf, moveDownIf };
168
+ const { tier, effort, domain, rationale, moveUpIf, moveDownIf } = outcome.sizing;
169
+ const pick = cheapestSufficientCandidate(classifiedByDomain.get(domain) ?? classified, tier, role.model);
170
+ const sized = { ...role, tier, effortIntent: effort, domain, rationale, moveUpIf, moveDownIf };
148
171
  if (!pick) {
149
172
  return {
150
173
  ...sized,
151
174
  status: "unassigned",
152
175
  proposedModel: null,
176
+ tierSource: null,
153
177
  proposedEffort: null,
154
178
  reason: `no available model is classified ${tier} or above`,
155
179
  };
156
180
  }
157
181
  const proposedEffort = mapEffortToLevel(effort, pick.efforts, pick.defaultEffort);
158
- return { ...sized, status: "proposed", proposedModel: pick.model, proposedEffort, reason: null };
182
+ return { ...sized, status: "proposed", proposedModel: pick.model, tierSource: pick.tierSource, proposedEffort, reason: null };
159
183
  });
160
184
  }
@@ -0,0 +1,160 @@
1
+ /**
2
+ * Measured capability per task domain, so role auto-assign can tier candidates by what they score
3
+ * instead of what they cost. Artificial Analysis is read live when configured:
4
+ *
5
+ * - Artificial Analysis (https://artificialanalysis.ai/api-reference), when the user configures
6
+ * their own API key. Its terms allow that user's internal use with attribution and forbid
7
+ * redistributing the raw data, so its scores never leave this process.
8
+ *
9
+ * Each domain maps to one published index. The source names a model by display name
10
+ * with one row per reasoning setting ("Claude Opus 5.5 (High)"); capabilityModelKey drops the
11
+ * setting and folds the rest to a slug, which matches a catalog id once its provider prefix is
12
+ * gone ("cpa/claude-opus-5-5"). The best setting's score stands for the model, since a role can
13
+ * be given that setting. A candidate with no row is unmeasured, never guessed.
14
+ */
15
+ import { resolveProviderApiKey } from "../providers/api-key-resolve";
16
+ import type { OcxConfig } from "../types";
17
+ import type { SizingDomain } from "./role-sizing";
18
+
19
+ export interface CapabilitySource {
20
+ readonly name: string;
21
+ readonly url: string;
22
+ readonly license: string;
23
+ /** Whether the terms let a response carry the raw scores. */
24
+ readonly redistributable: boolean;
25
+ }
26
+
27
+ export const ARTIFICIAL_ANALYSIS_SOURCE: CapabilitySource = {
28
+ name: "Artificial Analysis",
29
+ url: "https://artificialanalysis.ai",
30
+ license: "Artificial Analysis API terms: internal use, attribution required, no redistribution",
31
+ redistributable: false,
32
+ };
33
+
34
+ const ARTIFICIAL_ANALYSIS_URL = "https://artificialanalysis.ai/api/v2/data/llms/models";
35
+
36
+ /** Per domain, the indexes to read in order; the first one the response carries is used. */
37
+ const INTELLIGENCE_INDEX = "artificial_analysis_intelligence_index";
38
+ const ARTIFICIAL_ANALYSIS_INDEXES: Record<SizingDomain, readonly string[]> = {
39
+ coding: ["artificial_analysis_coding_index", INTELLIGENCE_INDEX],
40
+ agentic: ["artificial_analysis_agentic_index", INTELLIGENCE_INDEX],
41
+ reasoning: ["artificial_analysis_math_index", INTELLIGENCE_INDEX],
42
+ writing: [INTELLIGENCE_INDEX],
43
+ knowledge: [INTELLIGENCE_INDEX],
44
+ general: [INTELLIGENCE_INDEX],
45
+ };
46
+
47
+ const FETCH_TIMEOUT_MS = 30_000;
48
+ /** Each published table is under 1 MB today; 16 MB leaves room to grow and still bounds memory. */
49
+ export const MAX_TABLE_BYTES = 16 * 1024 * 1024;
50
+
51
+ export interface CapabilityTable {
52
+ readonly source: CapabilitySource | null;
53
+ readonly publishedAt: string | null;
54
+ /** Per domain: model key to its best published score. */
55
+ readonly domains: Partial<Record<SizingDomain, ReadonlyMap<string, number>>>;
56
+ /** Why a configured source was passed over for this one. */
57
+ readonly fallbackReason?: string;
58
+ }
59
+
60
+ export type LoadCapability = (config: OcxConfig) => Promise<CapabilityTable>;
61
+
62
+ export function capabilityModelKey(name: string): string {
63
+ return name.toLowerCase().replace(/\([^)]*\)/g, " ").replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
64
+ }
65
+
66
+ export function candidateModelKey(slug: string): string {
67
+ return capabilityModelKey(slug.slice(slug.lastIndexOf("/") + 1));
68
+ }
69
+
70
+ function keepBest(scores: Map<string, number>, key: string, value: number) {
71
+ if (key) scores.set(key, Math.max(value, scores.get(key) ?? -Infinity));
72
+ }
73
+
74
+ export function capabilityTableFromArtificialAnalysis(body: unknown): CapabilityTable {
75
+ const rows = (body as { data?: unknown } | null)?.data;
76
+ if (!Array.isArray(rows)) throw new Error("Artificial Analysis returned no model list");
77
+ const domains: Partial<Record<SizingDomain, Map<string, number>>> = {};
78
+ for (const [domain, indexes] of Object.entries(ARTIFICIAL_ANALYSIS_INDEXES) as [SizingDomain, readonly string[]][]) {
79
+ for (const index of indexes) {
80
+ const scores = new Map<string, number>();
81
+ for (const row of rows as Record<string, unknown>[]) {
82
+ const value = (row?.evaluations as Record<string, unknown> | undefined)?.[index];
83
+ if (typeof value !== "number" || !Number.isFinite(value)) continue;
84
+ if (typeof row.name === "string") keepBest(scores, capabilityModelKey(row.name), value);
85
+ if (typeof row.slug === "string") keepBest(scores, capabilityModelKey(row.slug), value);
86
+ }
87
+ if (scores.size > 0) {
88
+ domains[domain] = scores;
89
+ break;
90
+ }
91
+ }
92
+ }
93
+ return { source: ARTIFICIAL_ANALYSIS_SOURCE, publishedAt: null, domains };
94
+ }
95
+
96
+ /** The body's bytes, refused once they pass maxBytes, whatever the transport declared. */
97
+ export async function readBoundedBytes(response: Response, maxBytes: number): Promise<ArrayBuffer> {
98
+ const chunks: Uint8Array[] = [];
99
+ let length = 0;
100
+ const reader = response.body?.getReader();
101
+ if (!reader) return new ArrayBuffer(0);
102
+ for (;;) {
103
+ const { done, value } = await reader.read();
104
+ if (done) break;
105
+ if (length + value.byteLength > maxBytes) {
106
+ await reader.cancel();
107
+ throw new Error("response exceeded byte bound");
108
+ }
109
+ chunks.push(value);
110
+ length += value.byteLength;
111
+ }
112
+ const out = new Uint8Array(length);
113
+ let offset = 0;
114
+ for (const chunk of chunks) {
115
+ out.set(chunk, offset);
116
+ offset += chunk.byteLength;
117
+ }
118
+ return out.buffer;
119
+ }
120
+
121
+ export async function fetchArtificialAnalysisTable(apiKey: string, fetchImpl: typeof fetch = fetch): Promise<CapabilityTable> {
122
+ const response = await fetchImpl(ARTIFICIAL_ANALYSIS_URL, {
123
+ redirect: "error",
124
+ headers: { "x-api-key": apiKey },
125
+ signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
126
+ });
127
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
128
+ const bytes = await readBoundedBytes(response, MAX_TABLE_BYTES);
129
+ return capabilityTableFromArtificialAnalysis(JSON.parse(new TextDecoder().decode(bytes)));
130
+ }
131
+
132
+ /** One fetch per period key; a failed fetch is forgotten so the next request retries. */
133
+ function periodCache() {
134
+ let slot: { key: string; table: Promise<CapabilityTable> } | null = null;
135
+ return (key: string, fetchTable: () => Promise<CapabilityTable>): Promise<CapabilityTable> => {
136
+ if (slot?.key === key) return slot.table;
137
+ const table = fetchTable();
138
+ slot = { key, table };
139
+ table.catch(() => { if (slot?.table === table) slot = null; });
140
+ return table;
141
+ };
142
+ }
143
+
144
+ const artificialAnalysisCache = periodCache();
145
+
146
+ /**
147
+ * Artificial Analysis when the user's key resolves, read once per UTC day to stay inside the
148
+ * free tier's daily request quota. Absent or unavailable scores leave price ranking in charge.
149
+ */
150
+ export const loadCapabilityTable: LoadCapability = async config => {
151
+ const now = new Date().toISOString();
152
+ const unavailable = (fallbackReason: string): CapabilityTable => ({ source: null, publishedAt: null, domains: {}, fallbackReason });
153
+ const apiKey = resolveProviderApiKey(config.codexRoleCapability?.artificialAnalysis?.apiKey)?.trim();
154
+ if (!apiKey) return unavailable("benchmark data was unavailable: no Artificial Analysis key is configured");
155
+ try {
156
+ return await artificialAnalysisCache(`${now.slice(0, 10)} ${apiKey}`, () => fetchArtificialAnalysisTable(apiKey));
157
+ } catch {
158
+ return unavailable("Artificial Analysis could not be read");
159
+ }
160
+ };
@@ -43,6 +43,10 @@ export type SizingTier = typeof SIZING_TIERS[number];
43
43
  export const SIZING_EFFORTS = ["glance", "measured", "thorough", "exhaustive"] as const;
44
44
  export type SizingEffort = typeof SIZING_EFFORTS[number];
45
45
 
46
+ /** Task domains, the kind of capability a tier is measured in. A label, never a model or a vendor. */
47
+ export const SIZING_DOMAINS = ["coding", "agentic", "reasoning", "writing", "knowledge", "general"] as const;
48
+ export type SizingDomain = typeof SIZING_DOMAINS[number];
49
+
46
50
  const MAX_FIELD_CHARS = 400;
47
51
 
48
52
  export const ROLE_SIZING_SYSTEM_PROMPT = [
@@ -66,6 +70,15 @@ export const ROLE_SIZING_SYSTEM_PROMPT = [
66
70
  "or a strong model needs only a quick call. Effort buys deliberation, never capability, and more",
67
71
  "effort is not more correct.",
68
72
  "",
73
+ "Task domain names the kind of capability the tier is measured in. It sizes nothing; it tells the",
74
+ "mapping which measured strength matters, because models of one price class differ by domain:",
75
+ "- coding: writing, changing, or reviewing code in a repository.",
76
+ "- agentic: long multi-step tool use, where the work is completing a run of actions without derailing.",
77
+ "- reasoning: math, logic, planning, or analysis whose answer is derived rather than looked up.",
78
+ "- writing: prose meant for a reader: documents, messages, copy.",
79
+ "- knowledge: research and factual recall across sources.",
80
+ "- general: none of the above dominates. Pick it rather than guess.",
81
+ "",
69
82
  "Assess risk and complexity once for both dials:",
70
83
  "- file, module, or ownership boundary crossing;",
71
84
  "- reversibility and blast radius;",
@@ -86,8 +99,8 @@ export const ROLE_SIZING_SYSTEM_PROMPT = [
86
99
  "- Use only the neutral words above. Do not name concrete model products, vendors, versions, or vendor effort levels anywhere in the answer.",
87
100
  "",
88
101
  "Answer with one JSON object and nothing else, no prose and no code fence:",
89
- '{"roles":{"<role name>":{"tier":"fast|standard|frontier","effort":"glance|measured|thorough|exhaustive","rationale":"<one sentence covering both dials>","move_up_if":"<signals that would justify a stronger tier or higher effort>","move_down_if":"<signals that would justify a cheaper tier or lower effort>"}}}',
90
- "Include every role you were given, keyed by its exact name, with exactly those five fields.",
102
+ '{"roles":{"<role name>":{"tier":"fast|standard|frontier","effort":"glance|measured|thorough|exhaustive","domain":"coding|agentic|reasoning|writing|knowledge|general","rationale":"<one sentence covering both dials>","move_up_if":"<signals that would justify a stronger tier or higher effort>","move_down_if":"<signals that would justify a cheaper tier or lower effort>"}}}',
103
+ "Include every role you were given, keyed by its exact name, with exactly those six fields.",
91
104
  ].join("\n");
92
105
 
93
106
  /**
@@ -105,6 +118,7 @@ export const DELEGATED_WORK_SIZING_SYSTEM_PROMPT = [
105
118
  export interface RoleSizing {
106
119
  readonly tier: SizingTier;
107
120
  readonly effort: SizingEffort;
121
+ readonly domain: SizingDomain;
108
122
  readonly rationale: string;
109
123
  readonly moveUpIf: string;
110
124
  readonly moveDownIf: string;
@@ -167,13 +181,16 @@ function validateEntry(entry: unknown): RoleSizingOutcome {
167
181
  const fields = entry as Record<string, unknown>;
168
182
  if (!isMember(SIZING_TIERS, fields.tier)) return { unsized: "the sizing answer has no valid tier" };
169
183
  if (!isMember(SIZING_EFFORTS, fields.effort)) return { unsized: "the sizing answer has no valid effort" };
184
+ // A missing or unknown domain still sizes the role on the overall scale: the domain only picks
185
+ // which measured capability ranks the candidates, and "general" is the rubric's own no-signal answer.
186
+ const domain = isMember(SIZING_DOMAINS, fields.domain) ? fields.domain : "general";
170
187
  const rationale = oneLine(fields.rationale);
171
188
  const moveUpIf = oneLine(fields.move_up_if);
172
189
  const moveDownIf = oneLine(fields.move_down_if);
173
190
  if (rationale === null || moveUpIf === null || moveDownIf === null) {
174
191
  return { unsized: "the sizing answer is missing a rationale or a move trigger" };
175
192
  }
176
- return { sizing: { tier: fields.tier, effort: fields.effort, rationale, moveUpIf, moveDownIf } };
193
+ return { sizing: { tier: fields.tier, effort: fields.effort, domain, rationale, moveUpIf, moveDownIf } };
177
194
  }
178
195
 
179
196
  const CODE_FENCE = /^\x60{3}(?:json)?[ \t]*\r?\n([\s\S]*?)\r?\n[ \t]*\x60{3}$/i;
@@ -183,9 +200,10 @@ const CODE_FENCE = /^\x60{3}(?:json)?[ \t]*\r?\n([\s\S]*?)\r?\n[ \t]*\x60{3}$/i;
183
200
  *
184
201
  * The answer must be one JSON object of the shape the rubric asks for. Two leniencies, both for
185
202
  * content the mapping never reads: a single surrounding code fence, which models add out of habit,
186
- * and keys beyond the five fields in a role's entry, which are ignored. Every one of the five must
187
- * still be present and valid. Anything else wrong marks the affected role, or every role when the
188
- * whole answer is unusable, as unsized with the reason.
203
+ * and keys beyond the six fields in a role's entry, which are ignored. Tier, effort, rationale and
204
+ * both move triggers must still be present and valid; a missing domain falls back to "general".
205
+ * Anything else wrong marks the affected role, or every role when the whole answer is unusable,
206
+ * as unsized with the reason.
189
207
  */
190
208
  export function parseRoleSizingResponse(text: string, roles: readonly string[]): Map<string, RoleSizingOutcome> {
191
209
  const out = new Map<string, RoleSizingOutcome>();
@@ -3,7 +3,7 @@ import {
3
3
  CODEX_UNKNOWN_USAGE_SCORE,
4
4
  } from "../quota";
5
5
  import { isThirtyDayOnlyCodexPlan } from "../plan";
6
- import { hasSpendableCodexCredits, isTerminalShortWindow, type CodexSpendableCredits } from "../quota-types";
6
+ import { hasElapsedShortWindow, hasSpendableCodexCredits, isTerminalShortWindow, type CodexSpendableCredits } from "../quota-types";
7
7
  import type { CodexQuotaScope } from "./health-store";
8
8
  import type { TransientProbeGrant } from "./thread-affinity";
9
9
  import type { MainQuotaDispatch } from "../main-account-cache";
@@ -136,7 +136,8 @@ export function computeCodexUsageScore(quota: {
136
136
  ? (allowCredits && hasSpendableCodexCredits(quota, now)) ? CODEX_EXHAUSTED_USAGE_PERCENT - 1 : CODEX_EXHAUSTED_USAGE_PERCENT
137
137
  : CODEX_UNKNOWN_USAGE_SCORE;
138
138
  }
139
- const values = finite(quota.shortPercent) ? [...knownLong, quota.shortPercent] : knownLong;
139
+ const values = finite(quota.shortPercent) && !hasElapsedShortWindow(quota.shortResetAt, now)
140
+ ? [...knownLong, quota.shortPercent] : knownLong;
140
141
  const score = Math.max(...values);
141
142
  // Keep included-capacity accounts ahead of a paid overage account, but do not
142
143
  // classify the latter as exhausted solely from its subscription percentages.
@@ -13,6 +13,7 @@ import {
13
13
  import { macCodexAppBundleFor, macCodexAppCommands } from "./mac-app-bundle";
14
14
  import {
15
15
  beginCodexCliSelectionParentWrite,
16
+ withCodexCliSelectionParentWriteAsync,
16
17
  CodexCliSelectionMutationBusyError,
17
18
  } from "./cli-update-selection-gate";
18
19
  import { resolveCodexHomeDir } from "./home";
@@ -406,7 +407,7 @@ export function persistCodexRuntime(
406
407
  // window, so a held gate is an immediate busy rather than a wait. Checked
407
408
  // BEFORE the mkdir and the resolve-cache invalidation so a busy refusal
408
409
  // changes nothing at all.
409
- const selectionWrite = beginCodexCliSelectionParentWrite();
410
+ const selectionWrite = beginCodexCliSelectionParentWrite(deps.configDir);
410
411
  if (!selectionWrite.entered) throw new CodexCliSelectionMutationBusyError();
411
412
  try {
412
413
  const configDir = deps.configDir ?? getConfigDir();
@@ -438,7 +439,7 @@ export function clearPersistedCodexRuntime(deps: ResolveCodexRuntimeDeps = {}):
438
439
  // Same gate as persistCodexRuntime, checked before the cache invalidation
439
440
  // and outside the swallowing catch: a busy refusal must stay an error, not
440
441
  // become a silent "already removed".
441
- const selectionWrite = beginCodexCliSelectionParentWrite();
442
+ const selectionWrite = beginCodexCliSelectionParentWrite(deps.configDir);
442
443
  if (!selectionWrite.entered) throw new CodexCliSelectionMutationBusyError();
443
444
  try {
444
445
  const configDir = deps.configDir ?? getConfigDir();
@@ -461,6 +462,14 @@ export function clearPersistedCodexRuntime(deps: ResolveCodexRuntimeDeps = {}):
461
462
  }
462
463
  }
463
464
 
465
+ export async function persistCodexRuntimeAsync(runtime: ResolvedCodexRuntime, deps: ResolveCodexRuntimeDeps = {},
466
+ origin: CodexRuntimePinOrigin = "pinned"): Promise<void> {
467
+ await withCodexCliSelectionParentWriteAsync(() => persistCodexRuntime(runtime, deps, origin), deps);
468
+ }
469
+ export async function clearPersistedCodexRuntimeAsync(deps: ResolveCodexRuntimeDeps = {}): Promise<void> {
470
+ await withCodexCliSelectionParentWriteAsync(() => clearPersistedCodexRuntime(deps), deps);
471
+ }
472
+
464
473
  type VersionProbeResult = { ok: true; version: string | null; macAppBundle?: boolean } | { ok: false; reason: string };
465
474
 
466
475
  /** Checks that need no exec; a non-null answer settles the candidate without running `--version`. */
@@ -1415,7 +1424,38 @@ export async function resolveAndPersistCodexRuntimeAsync(
1415
1424
  if (deps.signal?.aborted || !isCurrent() || observation.epoch === null || observation.epoch !== resolveCacheEpoch
1416
1425
  || observation.inputs !== codexRuntimeSelectionIdentity(deps)) return null;
1417
1426
  if (deps.signal?.aborted) return null;
1418
- return cloneAndDeepFreeze(persistResolvedCodexRuntime(observation.result, deps));
1427
+ if (resolvedRuntimeMutation(observation.result, deps) === null) return cloneAndDeepFreeze(observation.result);
1428
+ try {
1429
+ return await withCodexCliSelectionParentWriteAsync(() => {
1430
+ // Admission can await Windows ACL subprocesses. Re-prove all selection
1431
+ // inputs after that await, in the same turn as the persisted mutation.
1432
+ if (deps.signal?.aborted || !isCurrent() || observation.epoch !== resolveCacheEpoch
1433
+ || observation.inputs !== codexRuntimeSelectionIdentity(deps)) return null;
1434
+ return cloneAndDeepFreeze(persistResolvedCodexRuntime(observation.result, deps));
1435
+ }, deps);
1436
+ } catch (error) {
1437
+ if (deps.signal?.aborted) return null;
1438
+ if (error instanceof CodexCliSelectionMutationBusyError) {
1439
+ // A recovery denial also followed an await. Never return the obsolete
1440
+ // selection as a usable result merely because no commit callback ran.
1441
+ if (!isCurrent() || observation.epoch !== resolveCacheEpoch
1442
+ || observation.inputs !== codexRuntimeSelectionIdentity(deps)) return null;
1443
+ if (observation.result.runtime.source !== "fallback") {
1444
+ return cloneAndDeepFreeze({ ...observation.result, persistError: error.message });
1445
+ }
1446
+ }
1447
+ throw error;
1448
+ }
1449
+ }
1450
+
1451
+ function resolvedRuntimeMutation(result: ResolveCodexRuntimeResult, deps: ResolveCodexRuntimeDeps): "persist" | "clear" | null {
1452
+ const persisted = loadPersistedCodexRuntime(deps);
1453
+ const unchanged = persisted !== null && persisted.command === result.runtime.command
1454
+ && persisted.source === result.runtime.source && (persisted.selectedVersion ?? null) === (result.runtime.version ?? null);
1455
+ if (result.runtime.command && result.runtime.source !== "fallback" && !unchanged) return "persist";
1456
+ if (result.runtime.source === "fallback" && persisted?.command && result.failures.some(failure =>
1457
+ failure.command.trim() === persisted.command.trim() && failure.reason === PATH_MISSING_REASON)) return "clear";
1458
+ return null;
1419
1459
  }
1420
1460
 
1421
1461
  function persistResolvedCodexRuntime(