@north-light/crouter 0.3.318 → 0.3.319

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 (1303) hide show
  1. package/README.md +0 -3
  2. package/dist/api/client.js +2 -912
  3. package/dist/api/dto/attach.js +0 -13
  4. package/dist/api/dto/bash-jobs.js +1 -9
  5. package/dist/api/dto/broker-ops.js +0 -6
  6. package/dist/api/dto/broker.js +0 -20
  7. package/dist/api/dto/canvas.js +0 -2
  8. package/dist/api/dto/chat-inventory.js +0 -11
  9. package/dist/api/dto/common.js +1 -15
  10. package/dist/api/dto/config.js +0 -3
  11. package/dist/api/dto/crons.js +1 -10
  12. package/dist/api/dto/files.js +0 -7
  13. package/dist/api/dto/focus.js +0 -10
  14. package/dist/api/dto/health.js +0 -2
  15. package/dist/api/dto/human-requests.js +0 -4
  16. package/dist/api/dto/human.js +0 -4
  17. package/dist/api/dto/inbox.js +0 -4
  18. package/dist/api/dto/lifecycle.js +0 -3
  19. package/dist/api/dto/mail.js +0 -1
  20. package/dist/api/dto/memory.js +0 -6
  21. package/dist/api/dto/messages.js +0 -2
  22. package/dist/api/dto/model-config.js +0 -1
  23. package/dist/api/dto/modelauth.js +0 -4
  24. package/dist/api/dto/node-outcomes.js +0 -2
  25. package/dist/api/dto/nodes.js +0 -3
  26. package/dist/api/dto/profiles.js +1 -4
  27. package/dist/api/dto/recovery.js +0 -1
  28. package/dist/api/dto/reports.js +0 -2
  29. package/dist/api/dto/review-comments.js +0 -5
  30. package/dist/api/dto/reviews.js +0 -5
  31. package/dist/api/dto/subscriptions.js +0 -2
  32. package/dist/api/dto/worktree.js +0 -6
  33. package/dist/api/errors.js +1 -40
  34. package/dist/api/index.js +1 -33
  35. package/dist/api/plugin-manifest-schema.js +0 -23
  36. package/dist/api/routes.js +1 -147
  37. package/dist/build-root.js +1 -198
  38. package/dist/cli.js +3 -99
  39. package/dist/clients/attach/ansi-cells.js +1 -137
  40. package/dist/clients/attach/chrome/bash-jobs.js +1 -31
  41. package/dist/clients/attach/chrome/canvas-panels.js +1 -194
  42. package/dist/clients/attach/chrome/completion-bell.js +1 -89
  43. package/dist/clients/attach/chrome/cron-wait.js +1 -34
  44. package/dist/clients/attach/chrome/git-info.js +2 -44
  45. package/dist/clients/attach/chrome/header.js +1 -30
  46. package/dist/clients/attach/chrome/inbox-strip.js +1 -199
  47. package/dist/clients/attach/chrome/recap.js +1 -19
  48. package/dist/clients/attach/chrome/review-wait.js +1 -22
  49. package/dist/clients/attach/chrome/roster.js +4 -275
  50. package/dist/clients/attach/chrome/status-line.js +1 -117
  51. package/dist/clients/attach/chrome/ticket-panel.js +1 -280
  52. package/dist/clients/attach/chrome/widgets.js +1 -51
  53. package/dist/clients/attach/command.js +1 -107
  54. package/dist/clients/attach/config.js +1 -274
  55. package/dist/clients/attach/input/capabilities.js +1 -104
  56. package/dist/clients/attach/input/clipboard-image.js +3 -220
  57. package/dist/clients/attach/input/completion-frecency.js +1 -141
  58. package/dist/clients/attach/input/controller.js +7 -377
  59. package/dist/clients/attach/input/overlay-owner.js +1 -199
  60. package/dist/clients/attach/input/ref-autocomplete.js +2 -176
  61. package/dist/clients/attach/input/titled-editor.js +4 -859
  62. package/dist/clients/attach/overlays/auth.js +2 -271
  63. package/dist/clients/attach/overlays/dialogs.js +2 -102
  64. package/dist/clients/attach/overlays/file-review.js +2 -128
  65. package/dist/clients/attach/overlays/graph.js +1 -661
  66. package/dist/clients/attach/overlays/help.js +1 -211
  67. package/dist/clients/attach/overlays/mcp.js +1 -302
  68. package/dist/clients/attach/overlays/pickers.js +1 -163
  69. package/dist/clients/attach/render/activity-slot.js +1 -13
  70. package/dist/clients/attach/render/assistant-message.js +1 -56
  71. package/dist/clients/attach/render/card-presentation.js +5 -218
  72. package/dist/clients/attach/render/chat-view.js +5 -1708
  73. package/dist/clients/attach/render/condensed-history.js +1 -60
  74. package/dist/clients/attach/render/context-message.js +1 -63
  75. package/dist/clients/attach/render/crtr-output.js +17 -905
  76. package/dist/clients/attach/render/diagram.js +7 -111
  77. package/dist/clients/attach/render/edit-diff.js +5 -304
  78. package/dist/clients/attach/render/glyph-codepoints.js +1 -42
  79. package/dist/clients/attach/render/group-activity.js +2 -263
  80. package/dist/clients/attach/render/group-recap.js +1 -152
  81. package/dist/clients/attach/render/markdown-source.js +5 -266
  82. package/dist/clients/attach/render/measured-container.js +1 -322
  83. package/dist/clients/attach/render/page-block.js +1 -183
  84. package/dist/clients/attach/render/scroll-animation.js +1 -80
  85. package/dist/clients/attach/render/tool-calls.js +3 -361
  86. package/dist/clients/attach/render/transcript-copy.js +2 -81
  87. package/dist/clients/attach/render/transcript-search.js +1 -152
  88. package/dist/clients/attach/render/viewport.js +1 -184
  89. package/dist/clients/attach/session/banner.js +1 -29
  90. package/dist/clients/attach/session/bindings.js +1 -71
  91. package/dist/clients/attach/session/chrome-refresh.js +1 -94
  92. package/dist/clients/attach/session/connection.js +1 -186
  93. package/dist/clients/attach/session/context.js +0 -16
  94. package/dist/clients/attach/session/editor-frame.js +1 -142
  95. package/dist/clients/attach/session/editor-inventory.js +6 -160
  96. package/dist/clients/attach/session/file-links.js +1 -68
  97. package/dist/clients/attach/session/focus-events.js +1 -12
  98. package/dist/clients/attach/session/frame.js +1 -214
  99. package/dist/clients/attach/session/frames.js +1 -188
  100. package/dist/clients/attach/session/identity.js +1 -26
  101. package/dist/clients/attach/session/input-wiring.js +1 -140
  102. package/dist/clients/attach/session/invariant.js +1 -15
  103. package/dist/clients/attach/session/keys.js +1 -77
  104. package/dist/clients/attach/session/layout.js +1 -59
  105. package/dist/clients/attach/session/mouse.js +1 -78
  106. package/dist/clients/attach/session/pane-focus.js +1 -42
  107. package/dist/clients/attach/session/pane-tag.js +1 -112
  108. package/dist/clients/attach/session/pi-assets.js +1 -24
  109. package/dist/clients/attach/session/pi-oauth-flows.js +1 -21
  110. package/dist/clients/attach/session/profile-files.js +1 -228
  111. package/dist/clients/attach/session/reconnect.js +1 -175
  112. package/dist/clients/attach/session/selection.js +3 -108
  113. package/dist/clients/attach/session/state-sync.js +1 -67
  114. package/dist/clients/attach/session/surface-frame.js +1 -73
  115. package/dist/clients/attach/session/surface-host.js +1 -255
  116. package/dist/clients/attach/session/teardown.js +5 -94
  117. package/dist/clients/attach/session/transcript-search.js +3 -151
  118. package/dist/clients/attach/session/whip-achievements.js +2 -145
  119. package/dist/clients/attach/session/whip.js +1 -362
  120. package/dist/clients/attach/slash/dispatch.js +1 -706
  121. package/dist/clients/attach/slash/prompt-preview.js +1 -121
  122. package/dist/clients/attach/viewer.js +441 -441
  123. package/dist/clients/conversation/projection.js +1 -324
  124. package/dist/clients/conversation/submit.js +1 -51
  125. package/dist/clients/inbox/controller.js +1 -701
  126. package/dist/clients/inbox/dto-adapt.js +1 -93
  127. package/dist/clients/inbox/layout.js +1 -9
  128. package/dist/clients/inbox/page-adapter.js +1 -55
  129. package/dist/clients/inbox/resolve.js +1 -35
  130. package/dist/clients/inbox/review/anchors.js +1 -89
  131. package/dist/clients/inbox/review/comments-client.js +1 -98
  132. package/dist/clients/inbox/review/companion-pane.js +1 -82
  133. package/dist/clients/inbox/review/document-surface.js +2 -1188
  134. package/dist/clients/inbox/review/frame.js +1 -216
  135. package/dist/clients/inbox/review/keys.js +1 -123
  136. package/dist/clients/inbox/review/launch.js +1 -151
  137. package/dist/clients/inbox/review/live-source.js +1 -160
  138. package/dist/clients/inbox/review/nvim.js +1 -44
  139. package/dist/clients/inbox/review/remap.js +1 -157
  140. package/dist/clients/inbox/review/review-client.js +1 -123
  141. package/dist/clients/inbox/review/roundtrip.js +3 -36
  142. package/dist/clients/inbox/review/state.js +4 -315
  143. package/dist/clients/inbox/review-adapter.js +1 -43
  144. package/dist/clients/inbox/surface.js +1 -152
  145. package/dist/clients/inbox/tui/ansi.js +3 -321
  146. package/dist/clients/inbox/tui/input.js +5 -392
  147. package/dist/clients/inbox/tui/keys.js +1 -104
  148. package/dist/clients/inbox/tui/page-body.js +1 -66
  149. package/dist/clients/inbox/tui/panel.js +1 -84
  150. package/dist/clients/inbox/tui/render.js +2 -368
  151. package/dist/clients/inbox/tui/slots.js +3 -134
  152. package/dist/clients/inbox/tui/types.js +0 -1
  153. package/dist/clients/inbox/tui.js +1 -86
  154. package/dist/clients/surfaces/host.js +0 -1
  155. package/dist/commands/api-client.js +4 -290
  156. package/dist/commands/attention.js +1 -215
  157. package/dist/commands/canvas-browse.js +1 -70
  158. package/dist/commands/canvas-config.js +2 -192
  159. package/dist/commands/canvas-history/grep.js +7 -85
  160. package/dist/commands/canvas-history/read.js +3 -50
  161. package/dist/commands/canvas-history/search.js +10 -99
  162. package/dist/commands/canvas-history/shared.js +1 -139
  163. package/dist/commands/canvas-history/stats.js +6 -68
  164. package/dist/commands/canvas-history.js +1 -32
  165. package/dist/commands/canvas-issue.js +14 -149
  166. package/dist/commands/canvas-prune.js +1 -108
  167. package/dist/commands/canvas-roster.js +1 -69
  168. package/dist/commands/canvas-snapshot.js +1 -34
  169. package/dist/commands/canvas-use.js +1 -61
  170. package/dist/commands/canvas.js +1 -49
  171. package/dist/commands/chord.js +1 -241
  172. package/dist/commands/cron.js +11 -534
  173. package/dist/commands/dashboard.js +1 -85
  174. package/dist/commands/human/components.js +3 -77
  175. package/dist/commands/human/feedback.js +1 -112
  176. package/dist/commands/human/prompts.js +1 -331
  177. package/dist/commands/human/queue.js +1 -191
  178. package/dist/commands/human/request.js +1 -266
  179. package/dist/commands/human/review.js +1 -288
  180. package/dist/commands/human/shared.js +1 -67
  181. package/dist/commands/human.js +4 -60
  182. package/dist/commands/memory/delete.js +1 -100
  183. package/dist/commands/memory/edit.js +21 -219
  184. package/dist/commands/memory/find.js +2 -244
  185. package/dist/commands/memory/history.js +5 -199
  186. package/dist/commands/memory/lint.js +1 -107
  187. package/dist/commands/memory/list.js +1 -142
  188. package/dist/commands/memory/move.js +1 -223
  189. package/dist/commands/memory/origin.js +1 -130
  190. package/dist/commands/memory/read.js +6 -232
  191. package/dist/commands/memory/shared.js +12 -802
  192. package/dist/commands/memory/write.js +13 -235
  193. package/dist/commands/memory.js +1 -30
  194. package/dist/commands/node/bash.js +5 -245
  195. package/dist/commands/node/create.js +3 -332
  196. package/dist/commands/node/inspect.js +12 -300
  197. package/dist/commands/node/lifecycle.js +2 -453
  198. package/dist/commands/node/message.js +1 -161
  199. package/dist/commands/node/outcome.js +4 -134
  200. package/dist/commands/node/pyramid.js +2 -79
  201. package/dist/commands/node/subscription.js +1 -78
  202. package/dist/commands/node/wait.js +7 -163
  203. package/dist/commands/node-context.js +2 -159
  204. package/dist/commands/node-inspect-artifacts.js +8 -109
  205. package/dist/commands/node-lifecycle-revive.js +1 -217
  206. package/dist/commands/node-snapshot.js +1 -37
  207. package/dist/commands/node-transcript.js +1 -62
  208. package/dist/commands/node-worktree.js +1 -138
  209. package/dist/commands/node.js +1 -29
  210. package/dist/commands/pkg/browse/actions.js +1 -198
  211. package/dist/commands/pkg/browse/catalog.js +1 -466
  212. package/dist/commands/pkg/browse/command-view.js +1 -191
  213. package/dist/commands/pkg/browse/detail-panel.js +1 -402
  214. package/dist/commands/pkg/browse/doc-view.js +1 -89
  215. package/dist/commands/pkg/browse/list-panel.js +2 -168
  216. package/dist/commands/pkg/browse/model.js +1 -36
  217. package/dist/commands/pkg/browse/shell.js +1 -640
  218. package/dist/commands/pkg/browse.js +1 -27
  219. package/dist/commands/pkg/market-inspect.js +1 -141
  220. package/dist/commands/pkg/market-manage.js +1 -327
  221. package/dist/commands/pkg/market.js +1 -14
  222. package/dist/commands/pkg/plugin-inspect.js +1 -168
  223. package/dist/commands/pkg/plugin-manage.js +17 -1109
  224. package/dist/commands/pkg/plugin.js +1 -14
  225. package/dist/commands/pkg/shared.js +1 -62
  226. package/dist/commands/pkg.js +1 -21
  227. package/dist/commands/profile/default.js +1 -139
  228. package/dist/commands/profile/delete.js +1 -66
  229. package/dist/commands/profile/env.js +3 -188
  230. package/dist/commands/profile/kind.js +2 -35
  231. package/dist/commands/profile/list.js +1 -43
  232. package/dist/commands/profile/meta.js +1 -67
  233. package/dist/commands/profile/new.js +1 -115
  234. package/dist/commands/profile/pause.js +1 -59
  235. package/dist/commands/profile/project.js +1 -113
  236. package/dist/commands/profile/rename.js +1 -38
  237. package/dist/commands/profile/show.js +1 -52
  238. package/dist/commands/profile.js +1 -35
  239. package/dist/commands/push.js +9 -265
  240. package/dist/commands/revive.js +10 -181
  241. package/dist/commands/surface/node/cycle.js +1 -83
  242. package/dist/commands/surface/node/focus.js +1 -81
  243. package/dist/commands/surface/node/id.js +1 -38
  244. package/dist/commands/surface/node/pin.js +1 -42
  245. package/dist/commands/surface/node/placement.js +1 -134
  246. package/dist/commands/surface-edit.js +1 -157
  247. package/dist/commands/surface-inbox.js +1 -98
  248. package/dist/commands/surface-inspect.js +1 -39
  249. package/dist/commands/surface-node.js +1 -19
  250. package/dist/commands/surface-reload.js +2 -134
  251. package/dist/commands/surface-tmux-spread.js +1 -214
  252. package/dist/commands/surface.js +1 -28
  253. package/dist/commands/sys/branch.js +1 -19
  254. package/dist/commands/sys/config.js +1 -353
  255. package/dist/commands/sys/context/admin/actions.js +2 -246
  256. package/dist/commands/sys/context/admin/detail-panel.js +1 -247
  257. package/dist/commands/sys/context/admin/docs-panel.js +1 -191
  258. package/dist/commands/sys/context/admin/list-view.js +1 -183
  259. package/dist/commands/sys/context/admin/model.js +1 -229
  260. package/dist/commands/sys/context/admin/rail-panel.js +1 -227
  261. package/dist/commands/sys/context/admin/read-view.js +2 -194
  262. package/dist/commands/sys/context/admin/shell.js +1 -910
  263. package/dist/commands/sys/context/admin.js +2 -126
  264. package/dist/commands/sys/context/doc.js +3 -197
  265. package/dist/commands/sys/context/prompt-review.js +11 -291
  266. package/dist/commands/sys/context/resolve.js +10 -489
  267. package/dist/commands/sys/context/sysprompt.js +5 -96
  268. package/dist/commands/sys/context.js +1 -20
  269. package/dist/commands/sys/daemon.js +1 -313
  270. package/dist/commands/sys/doctor.js +1 -617
  271. package/dist/commands/sys/feedback.js +7 -228
  272. package/dist/commands/sys/logs.js +3 -129
  273. package/dist/commands/sys/migrate.js +1 -94
  274. package/dist/commands/sys/panels/broker-limits-panel.js +2 -110
  275. package/dist/commands/sys/panels/dependencies-panel.js +1 -80
  276. package/dist/commands/sys/panels/keybindings-panel.js +1 -703
  277. package/dist/commands/sys/panels/models-panel.js +1 -1133
  278. package/dist/commands/sys/panels/plugins-panel.js +1 -80
  279. package/dist/commands/sys/panels/prefs-panel.js +1 -85
  280. package/dist/commands/sys/panels/profiles-panel.js +1 -601
  281. package/dist/commands/sys/panels/provider-panel.js +1 -538
  282. package/dist/commands/sys/settings-shell.js +1 -400
  283. package/dist/commands/sys/settings.js +1 -46
  284. package/dist/commands/sys/setup-core.js +6 -579
  285. package/dist/commands/sys/setup-settings-runner.js +1 -16
  286. package/dist/commands/sys/setup-wizard.js +1 -279
  287. package/dist/commands/sys/setup.js +1 -34
  288. package/dist/commands/sys/shared.js +1 -81
  289. package/dist/commands/sys/support/prepare.js +3 -239
  290. package/dist/commands/sys/support/submit.js +6 -524
  291. package/dist/commands/sys/support.js +1 -18
  292. package/dist/commands/sys/sync-deps.js +15 -498
  293. package/dist/commands/sys/sync-project-guidance.js +4 -481
  294. package/dist/commands/sys/sync-shared.js +1 -49
  295. package/dist/commands/sys/sync-skills.js +2 -311
  296. package/dist/commands/sys/sync.js +1 -18
  297. package/dist/commands/sys/update.js +1 -147
  298. package/dist/commands/sys/worktrees.js +5 -53
  299. package/dist/commands/sys.js +1 -18
  300. package/dist/core/activity-signal.js +2 -38
  301. package/dist/core/asset-root.js +1 -18
  302. package/dist/core/auth-file.js +2 -179
  303. package/dist/core/auth-interaction.js +3 -94
  304. package/dist/core/auto-update.js +4 -94
  305. package/dist/core/bash-guard.js +11 -393
  306. package/dist/core/bash-jobs.js +7 -411
  307. package/dist/core/bootstrap.js +2 -53
  308. package/dist/core/broker-client/client.js +1 -184
  309. package/dist/core/broker-client/index.js +1 -13
  310. package/dist/core/broker-client/transport-relay.js +1 -157
  311. package/dist/core/broker-client/transport-socket.js +1 -179
  312. package/dist/core/broker-client/transport.js +1 -26
  313. package/dist/core/canvas/attention.js +1 -47
  314. package/dist/core/canvas/boot-id.js +1 -26
  315. package/dist/core/canvas/boot.js +1 -186
  316. package/dist/core/canvas/browse/app.js +3 -919
  317. package/dist/core/canvas/browse/model.js +1 -477
  318. package/dist/core/canvas/browse/pins.js +1 -29
  319. package/dist/core/canvas/browse/render.js +3 -1055
  320. package/dist/core/canvas/canvas.js +15 -922
  321. package/dist/core/canvas/crons.js +11 -481
  322. package/dist/core/canvas/daemon-owner.js +2 -146
  323. package/dist/core/canvas/db.js +2 -100
  324. package/dist/core/canvas/deliveries.js +22 -283
  325. package/dist/core/canvas/delivery-contract.js +1 -8
  326. package/dist/core/canvas/executions.js +1 -68
  327. package/dist/core/canvas/extensions.js +1 -83
  328. package/dist/core/canvas/focuses.js +1 -77
  329. package/dist/core/canvas/history.js +24 -860
  330. package/dist/core/canvas/index.js +1 -15
  331. package/dist/core/canvas/install-id.js +1 -84
  332. package/dist/core/canvas/labels.js +1 -43
  333. package/dist/core/canvas/meta-file.js +1 -89
  334. package/dist/core/canvas/migrations.js +41 -1150
  335. package/dist/core/canvas/nav-model.js +1 -231
  336. package/dist/core/canvas/nav-render.js +1 -260
  337. package/dist/core/canvas/node-order.js +1 -37
  338. package/dist/core/canvas/node-recap.js +2 -67
  339. package/dist/core/canvas/node-sql-migration.js +2 -209
  340. package/dist/core/canvas/paths.js +1 -153
  341. package/dist/core/canvas/pid.js +4 -683
  342. package/dist/core/canvas/recovery.js +3 -565
  343. package/dist/core/canvas/remote-canvas-source.js +1 -288
  344. package/dist/core/canvas/remote-transport.js +1 -57
  345. package/dist/core/canvas/render-source.js +10 -635
  346. package/dist/core/canvas/sessions.js +1 -78
  347. package/dist/core/canvas/source.js +1 -47
  348. package/dist/core/canvas/status-glyph.js +1 -132
  349. package/dist/core/canvas/telemetry.js +1 -35
  350. package/dist/core/canvas/types.js +1 -6
  351. package/dist/core/canvas/waits.js +4 -68
  352. package/dist/core/canvas/worktrees.js +1 -93
  353. package/dist/core/clipboard-text.js +1 -57
  354. package/dist/core/command-hooks/artifact.js +1 -145
  355. package/dist/core/command-hooks/catalog.js +1 -38
  356. package/dist/core/command-hooks/compose.js +1 -99
  357. package/dist/core/command-hooks/discovery.js +1 -228
  358. package/dist/core/command-hooks/help.js +1 -18
  359. package/dist/core/command-hooks/index.js +1 -7
  360. package/dist/core/command-hooks/lifecycle-catalog.js +1 -9
  361. package/dist/core/command-hooks/lifecycle-create.js +1 -51
  362. package/dist/core/command-hooks/report.js +1 -28
  363. package/dist/core/command-hooks/schema.js +1 -157
  364. package/dist/core/command-hooks/transport/exec-invoke.js +1 -276
  365. package/dist/core/command-hooks/transport/exec-lifecycle.js +1 -211
  366. package/dist/core/command-manifests/manifest.js +1 -324
  367. package/dist/core/command-manifests/registry.js +1 -118
  368. package/dist/core/command-manifests/schema.js +1 -736
  369. package/dist/core/command-plugins/bundle.js +1 -261
  370. package/dist/core/command-plugins/compose.js +1 -65
  371. package/dist/core/command-plugins/discovery.js +1 -245
  372. package/dist/core/command-plugins/endpoint.js +1 -48
  373. package/dist/core/command-plugins/extensions.js +1 -166
  374. package/dist/core/command-plugins/help-addenda.js +1 -44
  375. package/dist/core/command-plugins/presence.js +1 -17
  376. package/dist/core/command-plugins/revalidate.js +2 -152
  377. package/dist/core/command-plugins/transport/exec-invoke.js +1 -215
  378. package/dist/core/command-plugins/transport/http-fetch.js +1 -68
  379. package/dist/core/command-plugins/transport/http-invoke.js +3 -546
  380. package/dist/core/command.js +10 -739
  381. package/dist/core/config.js +1 -1059
  382. package/dist/core/document-lines.js +3 -6
  383. package/dist/core/env-name.js +1 -9
  384. package/dist/core/errors.js +1 -52
  385. package/dist/core/events/emit.js +1 -17
  386. package/dist/core/events/envelope.js +1 -84
  387. package/dist/core/events/errors.js +1 -69
  388. package/dist/core/events/index.js +1 -4
  389. package/dist/core/events/operation-id.js +1 -24
  390. package/dist/core/events/read.js +1 -623
  391. package/dist/core/events/serialize.js +1 -199
  392. package/dist/core/events/source.js +1 -31
  393. package/dist/core/events/types.js +1 -11
  394. package/dist/core/exclusive-lock.js +1 -197
  395. package/dist/core/fault-classifier.js +1 -96
  396. package/dist/core/feed/feed.js +7 -202
  397. package/dist/core/feed/inbox-migrate.js +3 -135
  398. package/dist/core/feed/inbox.js +10 -122
  399. package/dist/core/feed/messages.js +6 -212
  400. package/dist/core/frontmatter.js +1 -35
  401. package/dist/core/fs-utils.js +3 -202
  402. package/dist/core/git.js +2 -103
  403. package/dist/core/help.js +9 -310
  404. package/dist/core/host-exports/builtins.js +9 -68
  405. package/dist/core/host-exports/export.js +3 -124
  406. package/dist/core/human/action-binding.js +1 -40
  407. package/dist/core/human/answer-text.js +5 -100
  408. package/dist/core/human/answer.js +1 -119
  409. package/dist/core/human/claim.js +3 -67
  410. package/dist/core/human/completion.js +1 -27
  411. package/dist/core/human/component-docs.js +78 -272
  412. package/dist/core/human/convention.js +2 -82
  413. package/dist/core/human/feedback-companion.js +1 -106
  414. package/dist/core/human/feedback.js +1 -88
  415. package/dist/core/human/page-authoring.js +1 -19
  416. package/dist/core/human/page-catalog.js +1 -172
  417. package/dist/core/human/page-errors.js +1 -7
  418. package/dist/core/human/page-eval.js +1 -125
  419. package/dist/core/human/page-html-markdown.js +8 -260
  420. package/dist/core/human/page-markdown.js +17 -295
  421. package/dist/core/human/page-schema.js +1 -373
  422. package/dist/core/human/page-synth.js +8 -27
  423. package/dist/core/human/page.js +1 -198
  424. package/dist/core/human/requests.js +15 -466
  425. package/dist/core/human/review-schema.js +1 -34
  426. package/dist/core/human/root.js +1 -11
  427. package/dist/core/human/scan.js +1 -69
  428. package/dist/core/human/summary.js +1 -5
  429. package/dist/core/human/tickets.js +1 -171
  430. package/dist/core/human/types.js +0 -1
  431. package/dist/core/human/visible-paths.js +1 -77
  432. package/dist/core/human-actions.js +1 -101
  433. package/dist/core/inspector/chrome.js +1 -14
  434. package/dist/core/inspector/contract.js +0 -1
  435. package/dist/core/inspector/core.js +2 -295
  436. package/dist/core/inspector/host.js +4 -538
  437. package/dist/core/inspector/model.js +2 -226
  438. package/dist/core/inspector/text.js +6 -45
  439. package/dist/core/inspector/transport.js +1 -36
  440. package/dist/core/inspector/tui.js +2 -295
  441. package/dist/core/installed-plugins.js +1 -78
  442. package/dist/core/io.js +7 -356
  443. package/dist/core/keybindings/attach-control.js +1 -42
  444. package/dist/core/keybindings/background-bash.js +1 -33
  445. package/dist/core/keybindings/catalog.js +1 -256
  446. package/dist/core/keybindings/inbox.js +1 -25
  447. package/dist/core/keybindings/index.js +1 -7
  448. package/dist/core/keybindings/match.js +1 -127
  449. package/dist/core/keybindings/persistence.js +1 -49
  450. package/dist/core/keybindings/resolve.js +1 -348
  451. package/dist/core/keybindings/types.js +0 -1
  452. package/dist/core/locked-file.js +1 -118
  453. package/dist/core/log.js +3 -27
  454. package/dist/core/manifest-recovery.js +1 -72
  455. package/dist/core/manifest-stale.js +1 -39
  456. package/dist/core/manifest.js +1 -20
  457. package/dist/core/memory/doc-link-grammar.js +2 -109
  458. package/dist/core/memory/extensions.js +1 -221
  459. package/dist/core/memory/history.js +6 -268
  460. package/dist/core/memory/identity.js +1 -181
  461. package/dist/core/memory/inline-ref-grammar.js +1 -98
  462. package/dist/core/memory/inline-ref-guidance.js +2 -35
  463. package/dist/core/memory/inline-ref-inventory.js +1 -59
  464. package/dist/core/memory/lint.js +2 -671
  465. package/dist/core/memory/project-namespace.js +2 -85
  466. package/dist/core/memory/repository-association.js +1 -129
  467. package/dist/core/memory/tree.js +1 -93
  468. package/dist/core/memory-resolver.js +2 -611
  469. package/dist/core/model-config.js +1 -107
  470. package/dist/core/model-routes.js +1 -201
  471. package/dist/core/nested-stores.js +2 -166
  472. package/dist/core/output.js +7 -99
  473. package/dist/core/pagination.js +1 -88
  474. package/dist/core/plugin-swap-lock.js +1 -31
  475. package/dist/core/pool-credential-store.js +1 -116
  476. package/dist/core/predicate.js +1 -191
  477. package/dist/core/preview-registry.js +2 -411
  478. package/dist/core/preview-result-path.js +1 -26
  479. package/dist/core/profiles/default-binding.js +1 -24
  480. package/dist/core/profiles/deletion-reservation.js +1 -42
  481. package/dist/core/profiles/env-store.js +2 -162
  482. package/dist/core/profiles/fuzzy-match.js +1 -92
  483. package/dist/core/profiles/manifest.js +1 -574
  484. package/dist/core/profiles/select.js +35 -741
  485. package/dist/core/profiles/state-block.js +3 -17
  486. package/dist/core/provider-error-recognition.js +1 -74
  487. package/dist/core/provider-failure.js +1 -86
  488. package/dist/core/provider-management.js +1 -22
  489. package/dist/core/remote-canvas-target.js +1 -30
  490. package/dist/core/render.js +18 -138
  491. package/dist/core/resolver.js +1 -111
  492. package/dist/core/review/birth.js +1 -52
  493. package/dist/core/review/comments.js +5 -279
  494. package/dist/core/review/companion.js +1 -215
  495. package/dist/core/review/document.js +2 -138
  496. package/dist/core/review/project.js +1 -36
  497. package/dist/core/review/realize.js +1 -71
  498. package/dist/core/review/remap.js +4 -141
  499. package/dist/core/review/signal.js +1 -12
  500. package/dist/core/review/stage.js +1 -184
  501. package/dist/core/review/store.js +1 -168
  502. package/dist/core/review/types.js +1 -23
  503. package/dist/core/runtime/advertised-command-invocation.js +1 -233
  504. package/dist/core/runtime/auth-reload.js +1 -67
  505. package/dist/core/runtime/banner.js +1 -51
  506. package/dist/core/runtime/bearings-render.js +18 -250
  507. package/dist/core/runtime/bearings.js +7 -194
  508. package/dist/core/runtime/bin-contributions.js +2 -186
  509. package/dist/core/runtime/boot-root.js +1 -107
  510. package/dist/core/runtime/branded-host.js +2 -264
  511. package/dist/core/runtime/broker/auth-reload.js +1 -157
  512. package/dist/core/runtime/broker/client-registry.js +1 -179
  513. package/dist/core/runtime/broker/daemon-ops.js +1 -88
  514. package/dist/core/runtime/broker/engine-drive.js +1 -354
  515. package/dist/core/runtime/broker/engine-routing.js +1 -211
  516. package/dist/core/runtime/broker/event-projection.js +1 -193
  517. package/dist/core/runtime/broker/extension-abort.js +1 -36
  518. package/dist/core/runtime/broker/fault-retry.js +1 -588
  519. package/dist/core/runtime/broker/frame-client.js +1 -132
  520. package/dist/core/runtime/broker/frame-dispatch.js +1 -183
  521. package/dist/core/runtime/broker/frame-engine-control.js +1 -231
  522. package/dist/core/runtime/broker/frame-memory-refs.js +1 -41
  523. package/dist/core/runtime/broker/frame-model-query.js +1 -69
  524. package/dist/core/runtime/broker/frame-read.js +1 -116
  525. package/dist/core/runtime/broker/frame-replies.js +1 -64
  526. package/dist/core/runtime/broker/frame-session.js +4 -203
  527. package/dist/core/runtime/broker/held-deferred-inbox.js +1 -13
  528. package/dist/core/runtime/broker/inbox.js +13 -163
  529. package/dist/core/runtime/broker/node-named.js +1 -27
  530. package/dist/core/runtime/broker/read-ops.js +1 -171
  531. package/dist/core/runtime/broker/rebind.js +1 -318
  532. package/dist/core/runtime/broker/tool-groups.js +1 -246
  533. package/dist/core/runtime/broker/turn-admission.js +1 -31
  534. package/dist/core/runtime/broker/turn-ignition.js +1 -84
  535. package/dist/core/runtime/broker-cli.js +3 -81
  536. package/dist/core/runtime/broker-extension-render.js +5 -135
  537. package/dist/core/runtime/broker-inventory.js +1 -116
  538. package/dist/core/runtime/broker-persona-guidance.js +4 -154
  539. package/dist/core/runtime/broker-protocol.js +3 -152
  540. package/dist/core/runtime/broker-request.js +1 -85
  541. package/dist/core/runtime/broker-sdk.js +1 -129
  542. package/dist/core/runtime/broker.js +1 -1214
  543. package/dist/core/runtime/canvas-extensions.js +1 -96
  544. package/dist/core/runtime/chat-inventory-rows.js +1 -105
  545. package/dist/core/runtime/close.js +1 -265
  546. package/dist/core/runtime/command-expansion.js +9 -104
  547. package/dist/core/runtime/command-surface.js +1 -117
  548. package/dist/core/runtime/connectivity.js +1 -73
  549. package/dist/core/runtime/deliver-live.js +1 -88
  550. package/dist/core/runtime/fault-recovery.js +1 -83
  551. package/dist/core/runtime/fault.js +1 -17
  552. package/dist/core/runtime/fleet.js +1 -58
  553. package/dist/core/runtime/front-door-env.js +1 -16
  554. package/dist/core/runtime/front-door.js +7 -183
  555. package/dist/core/runtime/headless-pi.js +1 -161
  556. package/dist/core/runtime/host.js +1 -457
  557. package/dist/core/runtime/interactive-deliver.js +1 -51
  558. package/dist/core/runtime/invocation.js +0 -10
  559. package/dist/core/runtime/kickoff.js +30 -327
  560. package/dist/core/runtime/launch-config-worker.js +1 -12
  561. package/dist/core/runtime/launch-prompt.js +4 -13
  562. package/dist/core/runtime/launch-target.js +1 -55
  563. package/dist/core/runtime/launch.js +1 -483
  564. package/dist/core/runtime/lifecycle.js +2 -213
  565. package/dist/core/runtime/managed-provider-cooling.js +1 -62
  566. package/dist/core/runtime/memory.js +1 -17
  567. package/dist/core/runtime/model-registry.js +1 -13
  568. package/dist/core/runtime/model-selection.js +1 -135
  569. package/dist/core/runtime/model-swap.js +1 -86
  570. package/dist/core/runtime/naming.js +14 -298
  571. package/dist/core/runtime/nerd-font.js +2 -127
  572. package/dist/core/runtime/node-read.js +2 -386
  573. package/dist/core/runtime/nodes.js +1 -331
  574. package/dist/core/runtime/outcome-document.js +1 -97
  575. package/dist/core/runtime/package-health.js +4 -99
  576. package/dist/core/runtime/persona.js +2 -55
  577. package/dist/core/runtime/pi-cli.js +1 -65
  578. package/dist/core/runtime/pi-vendored.js +1 -66
  579. package/dist/core/runtime/placement-tmux.js +1 -81
  580. package/dist/core/runtime/placement.js +1 -386
  581. package/dist/core/runtime/promote.js +1 -174
  582. package/dist/core/runtime/prospective-inventory-cli.js +3 -61
  583. package/dist/core/runtime/prospective-inventory.js +2 -88
  584. package/dist/core/runtime/recap.js +27 -103
  585. package/dist/core/runtime/recycle.js +1 -190
  586. package/dist/core/runtime/reopen.js +1 -138
  587. package/dist/core/runtime/reset.js +1 -245
  588. package/dist/core/runtime/resume-root.js +10 -290
  589. package/dist/core/runtime/revive-all.js +1 -107
  590. package/dist/core/runtime/revive.js +4 -544
  591. package/dist/core/runtime/roadmap.js +4 -38
  592. package/dist/core/runtime/session-branch.js +1 -46
  593. package/dist/core/runtime/session-cycles.js +1 -110
  594. package/dist/core/runtime/session-list-cache.js +1 -406
  595. package/dist/core/runtime/session-visibility.js +1 -99
  596. package/dist/core/runtime/shell-expansion.js +9 -100
  597. package/dist/core/runtime/situational-context.js +2 -67
  598. package/dist/core/runtime/situational-live.js +1 -41
  599. package/dist/core/runtime/spawn-env.js +1 -255
  600. package/dist/core/runtime/spawn.js +3 -577
  601. package/dist/core/runtime/stamp/channel.js +1 -76
  602. package/dist/core/runtime/stamp/protocol.js +1 -113
  603. package/dist/core/runtime/stop-guard.js +7 -116
  604. package/dist/core/runtime/stop-signals.js +1 -46
  605. package/dist/core/runtime/stream-watchdog.js +1 -134
  606. package/dist/core/runtime/structured-output.js +4 -156
  607. package/dist/core/runtime/surface-bg.js +1 -85
  608. package/dist/core/runtime/tmux-bindings.js +4 -730
  609. package/dist/core/runtime/tmux-chrome.js +1 -5
  610. package/dist/core/runtime/tmux-driver.js +7 -637
  611. package/dist/core/runtime/tool-group-summary.js +6 -85
  612. package/dist/core/runtime/turn-visibility.js +1 -31
  613. package/dist/core/runtime/view-socket.js +1 -83
  614. package/dist/core/runtime/warm-pool.js +1 -395
  615. package/dist/core/scope.js +1 -254
  616. package/dist/core/scoped-state/db.js +1 -135
  617. package/dist/core/scoped-state/migrate.js +3 -181
  618. package/dist/core/scoped-state/paths.js +1 -27
  619. package/dist/core/scoped-state/profiles.js +1 -62
  620. package/dist/core/scoped-state/providers.js +1 -8
  621. package/dist/core/scoped-state/schema.js +3 -16
  622. package/dist/core/scoped-state/settings.js +1 -82
  623. package/dist/core/secrets.js +2 -55
  624. package/dist/core/self-update.js +1 -71
  625. package/dist/core/session-model/session-state.js +1 -180
  626. package/dist/core/shell-segments.js +4 -169
  627. package/dist/core/spawn.js +1 -26
  628. package/dist/core/stdout-errors.js +1 -14
  629. package/dist/core/subscription-state.js +3 -193
  630. package/dist/core/substrate/frontmatter-validation.js +1 -105
  631. package/dist/core/substrate/gate-explain.js +1 -424
  632. package/dist/core/substrate/gate.js +1 -26
  633. package/dist/core/substrate/index.js +1 -30
  634. package/dist/core/substrate/injected-store.js +1 -249
  635. package/dist/core/substrate/listings.js +1 -115
  636. package/dist/core/substrate/on-read-node.js +1 -36
  637. package/dist/core/substrate/on-read.js +9 -315
  638. package/dist/core/substrate/plan.js +1 -266
  639. package/dist/core/substrate/render-node.js +1 -30
  640. package/dist/core/substrate/render.js +65 -689
  641. package/dist/core/substrate/schema.js +1 -186
  642. package/dist/core/substrate/session-cache.js +1 -83
  643. package/dist/core/substrate/subject-fields.js +1 -35
  644. package/dist/core/substrate/subject.js +1 -47
  645. package/dist/core/substrate/surface-match.js +1 -242
  646. package/dist/core/termrender/code-doc.js +5 -92
  647. package/dist/core/termrender/display.js +2 -56
  648. package/dist/core/termrender/termrender.js +14 -719
  649. package/dist/core/termrender/version.js +1 -1
  650. package/dist/core/timing.js +2 -94
  651. package/dist/core/tui/draw.js +2 -378
  652. package/dist/core/tui/fuzzy.js +1 -38
  653. package/dist/core/tui/markdown.js +3 -407
  654. package/dist/core/tui/page-host.js +1 -74
  655. package/dist/core/tui/panel.js +1 -137
  656. package/dist/core/tui/terminal.js +2 -100
  657. package/dist/core/user-settings.js +1 -454
  658. package/dist/core/wake.js +1 -296
  659. package/dist/core/worktree-close.js +6 -228
  660. package/dist/core/worktree-containment.js +3 -48
  661. package/dist/core/worktree-landing.js +4 -56
  662. package/dist/core/worktree-mutation-async.js +10 -280
  663. package/dist/core/worktree-quarantine.js +1 -54
  664. package/dist/core/worktree-sweep.js +6 -453
  665. package/dist/core/worktree.js +18 -899
  666. package/dist/daemon/api/bridge.js +8 -418
  667. package/dist/daemon/api/handlers/attach.js +1 -65
  668. package/dist/daemon/api/handlers/bash-jobs.js +2 -91
  669. package/dist/daemon/api/handlers/broker-ops.js +1 -447
  670. package/dist/daemon/api/handlers/broker-recovery.js +1 -251
  671. package/dist/daemon/api/handlers/canvas.js +6 -490
  672. package/dist/daemon/api/handlers/chat-inventory.js +1 -25
  673. package/dist/daemon/api/handlers/crons.js +1 -373
  674. package/dist/daemon/api/handlers/daemon.js +1 -76
  675. package/dist/daemon/api/handlers/feedback-comments.js +3 -199
  676. package/dist/daemon/api/handlers/files.js +1 -63
  677. package/dist/daemon/api/handlers/focus.js +1 -92
  678. package/dist/daemon/api/handlers/health.js +1 -76
  679. package/dist/daemon/api/handlers/human-requests.js +1 -379
  680. package/dist/daemon/api/handlers/human.js +1 -3
  681. package/dist/daemon/api/handlers/inbox-page.js +1 -89
  682. package/dist/daemon/api/handlers/inbox.js +1 -301
  683. package/dist/daemon/api/handlers/memory.js +1 -50
  684. package/dist/daemon/api/handlers/messages.js +2 -506
  685. package/dist/daemon/api/handlers/model-config.js +1 -44
  686. package/dist/daemon/api/handlers/modelauth.js +1 -165
  687. package/dist/daemon/api/handlers/node-outcomes.js +1 -181
  688. package/dist/daemon/api/handlers/nodes.js +1 -939
  689. package/dist/daemon/api/handlers/profiles.js +1 -140
  690. package/dist/daemon/api/handlers/prospective-chat-inventory.js +1 -59
  691. package/dist/daemon/api/handlers/reports.js +16 -332
  692. package/dist/daemon/api/handlers/review-comments.js +1 -465
  693. package/dist/daemon/api/handlers/reviews.js +1 -200
  694. package/dist/daemon/api/handlers/subscriptions.js +1 -53
  695. package/dist/daemon/api/handlers/validate.js +1 -58
  696. package/dist/daemon/api/handlers/worktree.js +1 -39
  697. package/dist/daemon/api/map.js +2 -735
  698. package/dist/daemon/api/router.js +1 -190
  699. package/dist/daemon/api/server.js +11 -408
  700. package/dist/daemon/boot-migrations.js +1 -31
  701. package/dist/daemon/companion-retire.js +1 -34
  702. package/dist/daemon/control.js +1 -22
  703. package/dist/daemon/cron/armed-context.js +1 -100
  704. package/dist/daemon/cron/capture.js +2 -45
  705. package/dist/daemon/cron/disposition.js +1 -55
  706. package/dist/daemon/cron/escalation.js +3 -124
  707. package/dist/daemon/cron/held.js +1 -20
  708. package/dist/daemon/cron/lease-recovery.js +1 -119
  709. package/dist/daemon/cron/live-runs.js +1 -23
  710. package/dist/daemon/cron/passes.js +1 -58
  711. package/dist/daemon/cron/settle.js +1 -61
  712. package/dist/daemon/cron/sinks.js +2 -59
  713. package/dist/daemon/cron-run.js +1 -405
  714. package/dist/daemon/crtrd-cli.js +1 -23
  715. package/dist/daemon/crtrd.js +8 -854
  716. package/dist/daemon/delivery/run-delivery.js +1 -164
  717. package/dist/daemon/fleet.js +17 -475
  718. package/dist/daemon/human/finish.js +10 -179
  719. package/dist/daemon/human/settle.js +1 -89
  720. package/dist/daemon/human/sweep.js +1 -66
  721. package/dist/daemon/manage.js +2 -595
  722. package/dist/daemon/messaging/node-message.js +1 -99
  723. package/dist/daemon/park-activity.js +1 -30
  724. package/dist/daemon/park-pending.js +1 -52
  725. package/dist/daemon/pidfile.js +1 -37
  726. package/dist/daemon/profile-delete.js +1 -306
  727. package/dist/daemon/reconcilers/bash-deadline.js +1 -80
  728. package/dist/daemon/reconcilers/broker-supervision.js +4 -761
  729. package/dist/daemon/reconcilers/controller-death.js +1 -14
  730. package/dist/daemon/reconcilers/cron-lane.js +1 -29
  731. package/dist/daemon/reconcilers/delivery-lane.js +1 -130
  732. package/dist/daemon/reconcilers/live-obligation.js +1 -87
  733. package/dist/daemon/reconcilers/managed-worktree-sweep.js +1 -202
  734. package/dist/daemon/reconcilers/node-deadline.js +1 -82
  735. package/dist/daemon/reconcilers/node-lifecycle/freeze-lane.js +1 -172
  736. package/dist/daemon/reconcilers/node-lifecycle/respawn-policy.js +1 -59
  737. package/dist/daemon/reconcilers/node-lifecycle/terminating.js +1 -12
  738. package/dist/daemon/reconcilers/node-lifecycle/tick.js +1 -146
  739. package/dist/daemon/reconcilers/node-lifecycle/wants-execution.js +1 -11
  740. package/dist/daemon/reconcilers/pending-review-submit.js +1 -36
  741. package/dist/daemon/reconcilers/storage-maintenance.js +1 -207
  742. package/dist/daemon/review/comment-notify.js +2 -109
  743. package/dist/daemon/review/companion.js +1 -29
  744. package/dist/daemon/review/deliver.js +3 -8
  745. package/dist/daemon/review/finish.js +1 -122
  746. package/dist/daemon/review/sweep.js +1 -18
  747. package/dist/daemon/startup-policy.js +1 -1
  748. package/dist/daemon/supervise-cadence.js +1 -6
  749. package/dist/hook-authoring.js +5 -447
  750. package/dist/hook-process.js +1 -34
  751. package/dist/index.js +1 -52
  752. package/dist/migrations/001-surfaces-frontmatter.js +1 -161
  753. package/dist/migrations/002-profile-project-memory.js +1 -72
  754. package/dist/migrations/003-repository-root-memory-identity/front-door.js +11 -231
  755. package/dist/migrations/003-repository-root-memory-identity/index.js +5 -513
  756. package/dist/migrations/003-repository-root-memory-identity/references.js +5 -469
  757. package/dist/migrations/003-repository-root-memory-identity/repository-facts.js +3 -349
  758. package/dist/migrations/activation.js +2 -89
  759. package/dist/migrations/convergent.js +2 -82
  760. package/dist/migrations/corpus.js +3 -483
  761. package/dist/migrations/frontmatter-splice.js +8 -176
  762. package/dist/migrations/profile-manifests.js +2 -70
  763. package/dist/migrations/registry.js +1 -42
  764. package/dist/migrations/runner.js +1 -85
  765. package/dist/migrations/types.js +0 -24
  766. package/dist/pi-extensions/broker-local.js +2 -106
  767. package/dist/pi-extensions/canvas-bash-valve.js +7 -461
  768. package/dist/pi-extensions/canvas-context-intro.js +2 -113
  769. package/dist/pi-extensions/canvas-doc-substrate.js +9 -178
  770. package/dist/pi-extensions/canvas-goal-capture.js +1 -83
  771. package/dist/pi-extensions/canvas-inbox-watcher.js +1 -1010
  772. package/dist/pi-extensions/canvas-passive-context.js +14 -203
  773. package/dist/pi-extensions/canvas-preview-result.js +2 -72
  774. package/dist/pi-extensions/canvas-prompt-scrub.js +2 -54
  775. package/dist/pi-extensions/canvas-recap.js +11 -405
  776. package/dist/pi-extensions/canvas-review-boundary.js +3 -63
  777. package/dist/pi-extensions/canvas-stamp.js +2 -77
  778. package/dist/pi-extensions/canvas-stophook.js +1 -643
  779. package/dist/pi-extensions/canvas-structured-output.js +13 -35
  780. package/dist/pi-extensions/canvas-tool-guide.js +9 -78
  781. package/dist/pi-extensions/envelope-merge.js +8 -29
  782. package/dist/prompts/review.js +20 -42
  783. package/dist/shared/birth-announcement.js +1 -25
  784. package/dist/shared/crtr-version.js +1 -18
  785. package/dist/shared/env.js +1 -214
  786. package/dist/shared/generated-context.js +11 -226
  787. package/dist/shared/predicates.js +1 -4
  788. package/dist/shared/shell-quote.js +1 -5
  789. package/dist/shared/tool-groups.js +1 -64
  790. package/dist/shared/working-activity.js +1 -27
  791. package/dist/suppress-experimental-warnings.js +1 -15
  792. package/dist/types.js +1 -185
  793. package/docs/public-api.md +10 -9
  794. package/package.json +5 -4
  795. package/runtime.lock.json +5 -5
  796. package/dist/api/__tests__/integration/client.test.d.ts +0 -1
  797. package/dist/api/__tests__/integration/client.test.js +0 -163
  798. package/dist/builtin-pi-packages/pi-crtr-extensions/__tests__/insights-active-init.test.ts +0 -98
  799. package/dist/builtin-pi-packages/pi-crtr-extensions/extensions/__tests__/crtr-commands.test.ts +0 -93
  800. package/dist/builtin-pi-packages/pi-crtr-extensions/extensions/__tests__/integration/provider-rotation.test.ts +0 -2530
  801. package/dist/clients/__tests__/helpers/human-daemon.d.ts +0 -11
  802. package/dist/clients/__tests__/helpers/human-daemon.js +0 -60
  803. package/dist/clients/attach/__tests__/attach-remote-readonly.test.d.ts +0 -1
  804. package/dist/clients/attach/__tests__/attach-remote-readonly.test.js +0 -97
  805. package/dist/clients/attach/__tests__/bash-call-width.test.d.ts +0 -1
  806. package/dist/clients/attach/__tests__/bash-call-width.test.js +0 -38
  807. package/dist/clients/attach/__tests__/chat-view-snapshot-ordering.test.d.ts +0 -1
  808. package/dist/clients/attach/__tests__/chat-view-snapshot-ordering.test.js +0 -175
  809. package/dist/clients/attach/__tests__/completion-frecency.test.d.ts +0 -1
  810. package/dist/clients/attach/__tests__/completion-frecency.test.js +0 -35
  811. package/dist/clients/attach/__tests__/context-message.test.d.ts +0 -1
  812. package/dist/clients/attach/__tests__/context-message.test.js +0 -130
  813. package/dist/clients/attach/__tests__/crtr-output.test.d.ts +0 -1
  814. package/dist/clients/attach/__tests__/crtr-output.test.js +0 -68
  815. package/dist/clients/attach/__tests__/diagram.test.d.ts +0 -1
  816. package/dist/clients/attach/__tests__/diagram.test.js +0 -42
  817. package/dist/clients/attach/__tests__/edit-diff.test.d.ts +0 -1
  818. package/dist/clients/attach/__tests__/edit-diff.test.js +0 -31
  819. package/dist/clients/attach/__tests__/frame-selection.test.d.ts +0 -1
  820. package/dist/clients/attach/__tests__/frame-selection.test.js +0 -84
  821. package/dist/clients/attach/__tests__/group-activity.test.d.ts +0 -1
  822. package/dist/clients/attach/__tests__/group-activity.test.js +0 -165
  823. package/dist/clients/attach/__tests__/oauth-dialog-lifecycle.test.d.ts +0 -1
  824. package/dist/clients/attach/__tests__/oauth-dialog-lifecycle.test.js +0 -83
  825. package/dist/clients/attach/__tests__/pane-tag-successor.test.d.ts +0 -1
  826. package/dist/clients/attach/__tests__/pane-tag-successor.test.js +0 -56
  827. package/dist/clients/attach/__tests__/profile-files.test.d.ts +0 -1
  828. package/dist/clients/attach/__tests__/profile-files.test.js +0 -21
  829. package/dist/clients/attach/__tests__/prompt-preview.test.d.ts +0 -1
  830. package/dist/clients/attach/__tests__/prompt-preview.test.js +0 -21
  831. package/dist/clients/attach/__tests__/ref-autocomplete.test.d.ts +0 -1
  832. package/dist/clients/attach/__tests__/ref-autocomplete.test.js +0 -64
  833. package/dist/clients/attach/__tests__/titled-editor-paste.test.d.ts +0 -1
  834. package/dist/clients/attach/__tests__/titled-editor-paste.test.js +0 -114
  835. package/dist/clients/attach/__tests__/titled-editor-preview.test.d.ts +0 -1
  836. package/dist/clients/attach/__tests__/titled-editor-preview.test.js +0 -83
  837. package/dist/clients/inbox/__tests__/integration/inbox-controller.test.d.ts +0 -1
  838. package/dist/clients/inbox/__tests__/integration/inbox-controller.test.js +0 -81
  839. package/dist/clients/inbox/__tests__/integration/mount-panel.test.d.ts +0 -1
  840. package/dist/clients/inbox/__tests__/integration/mount-panel.test.js +0 -135
  841. package/dist/clients/inbox/__tests__/integration/stale-row-activation.test.d.ts +0 -1
  842. package/dist/clients/inbox/__tests__/integration/stale-row-activation.test.js +0 -66
  843. package/dist/clients/inbox/review/__tests__/editor-roundtrip.test.d.ts +0 -1
  844. package/dist/clients/inbox/review/__tests__/editor-roundtrip.test.js +0 -35
  845. package/dist/clients/inbox/review/__tests__/review-core.test.d.ts +0 -1
  846. package/dist/clients/inbox/review/__tests__/review-core.test.js +0 -52
  847. package/dist/clients/inbox/review/__tests__/teardown-notice.test.d.ts +0 -1
  848. package/dist/clients/inbox/review/__tests__/teardown-notice.test.js +0 -100
  849. package/dist/commands/__tests__/api-client-cold-start-diagnostic.test.d.ts +0 -1
  850. package/dist/commands/__tests__/api-client-cold-start-diagnostic.test.js +0 -69
  851. package/dist/commands/__tests__/human.test.d.ts +0 -1
  852. package/dist/commands/__tests__/human.test.js +0 -172
  853. package/dist/commands/__tests__/integration/dashboard-snapshot.test.d.ts +0 -1
  854. package/dist/commands/__tests__/integration/dashboard-snapshot.test.js +0 -237
  855. package/dist/commands/__tests__/integration/migrate.test.d.ts +0 -1
  856. package/dist/commands/__tests__/integration/migrate.test.js +0 -1330
  857. package/dist/commands/__tests__/node-message.test.d.ts +0 -1
  858. package/dist/commands/__tests__/node-message.test.js +0 -112
  859. package/dist/commands/__tests__/node-new.test.d.ts +0 -1
  860. package/dist/commands/__tests__/node-new.test.js +0 -58
  861. package/dist/commands/__tests__/revive-now-gate.test.d.ts +0 -1
  862. package/dist/commands/__tests__/revive-now-gate.test.js +0 -62
  863. package/dist/commands/__tests__/seam/daemon-status.test.d.ts +0 -1
  864. package/dist/commands/__tests__/seam/daemon-status.test.js +0 -29
  865. package/dist/commands/__tests__/surface-reload-target.test.d.ts +0 -1
  866. package/dist/commands/__tests__/surface-reload-target.test.js +0 -21
  867. package/dist/commands/memory/__tests__/command-selector-and-mutation-guards.test.d.ts +0 -1
  868. package/dist/commands/memory/__tests__/command-selector-and-mutation-guards.test.js +0 -379
  869. package/dist/commands/memory/__tests__/memory-list-and-lint-ignore.test.d.ts +0 -1
  870. package/dist/commands/memory/__tests__/memory-list-and-lint-ignore.test.js +0 -124
  871. package/dist/commands/memory/__tests__/repository-root-lint.test.d.ts +0 -1
  872. package/dist/commands/memory/__tests__/repository-root-lint.test.js +0 -148
  873. package/dist/commands/sys/__tests__/config-model-ladders.test.d.ts +0 -1
  874. package/dist/commands/sys/__tests__/config-model-ladders.test.js +0 -114
  875. package/dist/commands/sys/__tests__/setup-core.test.d.ts +0 -1
  876. package/dist/commands/sys/__tests__/setup-core.test.js +0 -45
  877. package/dist/commands/sys/__tests__/sync-project-guidance.test.d.ts +0 -1
  878. package/dist/commands/sys/__tests__/sync-project-guidance.test.js +0 -244
  879. package/dist/core/__tests__/bash-guard.test.d.ts +0 -1
  880. package/dist/core/__tests__/bash-guard.test.js +0 -190
  881. package/dist/core/__tests__/boot-fault-attempt.test.d.ts +0 -1
  882. package/dist/core/__tests__/boot-fault-attempt.test.js +0 -105
  883. package/dist/core/__tests__/boot.test.d.ts +0 -1
  884. package/dist/core/__tests__/boot.test.js +0 -274
  885. package/dist/core/__tests__/branded-host-concurrency.test.d.ts +0 -1
  886. package/dist/core/__tests__/branded-host-concurrency.test.js +0 -72
  887. package/dist/core/__tests__/broker-extension-canvas-db-boundary.test.d.ts +0 -1
  888. package/dist/core/__tests__/broker-extension-canvas-db-boundary.test.js +0 -75
  889. package/dist/core/__tests__/broker-launch-candidates.test.d.ts +0 -1
  890. package/dist/core/__tests__/broker-launch-candidates.test.js +0 -311
  891. package/dist/core/__tests__/broker-preflight.test.d.ts +0 -1
  892. package/dist/core/__tests__/broker-preflight.test.js +0 -95
  893. package/dist/core/__tests__/broker-stream-watchdog-floor.test.d.ts +0 -1
  894. package/dist/core/__tests__/broker-stream-watchdog-floor.test.js +0 -82
  895. package/dist/core/__tests__/broker-turn-admission.test.d.ts +0 -1
  896. package/dist/core/__tests__/broker-turn-admission.test.js +0 -44
  897. package/dist/core/__tests__/canvas-db-boundary.test.d.ts +0 -1
  898. package/dist/core/__tests__/canvas-db-boundary.test.js +0 -46
  899. package/dist/core/__tests__/canvas-inbox-watcher-hold.test.d.ts +0 -1
  900. package/dist/core/__tests__/canvas-inbox-watcher-hold.test.js +0 -710
  901. package/dist/core/__tests__/canvas-inbox-watcher-naming.test.d.ts +0 -1
  902. package/dist/core/__tests__/canvas-inbox-watcher-naming.test.js +0 -310
  903. package/dist/core/__tests__/canvas-inbox-watcher.test.d.ts +0 -1
  904. package/dist/core/__tests__/canvas-inbox-watcher.test.js +0 -397
  905. package/dist/core/__tests__/canvas.test.d.ts +0 -1
  906. package/dist/core/__tests__/canvas.test.js +0 -138
  907. package/dist/core/__tests__/child-death-wake.test.d.ts +0 -1
  908. package/dist/core/__tests__/child-death-wake.test.js +0 -153
  909. package/dist/core/__tests__/cli-help-contract.test.d.ts +0 -1
  910. package/dist/core/__tests__/cli-help-contract.test.js +0 -152
  911. package/dist/core/__tests__/close.test.d.ts +0 -1
  912. package/dist/core/__tests__/close.test.js +0 -284
  913. package/dist/core/__tests__/collectible.test.d.ts +0 -1
  914. package/dist/core/__tests__/collectible.test.js +0 -89
  915. package/dist/core/__tests__/config-change-delta.test.d.ts +0 -1
  916. package/dist/core/__tests__/config-change-delta.test.js +0 -108
  917. package/dist/core/__tests__/context-intro.test.d.ts +0 -1
  918. package/dist/core/__tests__/context-intro.test.js +0 -123
  919. package/dist/core/__tests__/cron-armed-context-failure.test.d.ts +0 -1
  920. package/dist/core/__tests__/cron-armed-context-failure.test.js +0 -150
  921. package/dist/core/__tests__/cron-held-settlement.test.d.ts +0 -1
  922. package/dist/core/__tests__/cron-held-settlement.test.js +0 -240
  923. package/dist/core/__tests__/cron-node-sink-parked-root.test.d.ts +0 -1
  924. package/dist/core/__tests__/cron-node-sink-parked-root.test.js +0 -166
  925. package/dist/core/__tests__/cron-self-cancel-delivery.test.d.ts +0 -1
  926. package/dist/core/__tests__/cron-self-cancel-delivery.test.js +0 -84
  927. package/dist/core/__tests__/cron-timeout-descendants.test.d.ts +0 -1
  928. package/dist/core/__tests__/cron-timeout-descendants.test.js +0 -114
  929. package/dist/core/__tests__/daemon-boot.test.d.ts +0 -1
  930. package/dist/core/__tests__/daemon-boot.test.js +0 -422
  931. package/dist/core/__tests__/daemon-wedge.test.d.ts +0 -1
  932. package/dist/core/__tests__/daemon-wedge.test.js +0 -106
  933. package/dist/core/__tests__/dead-node-policy-table.test.d.ts +0 -1
  934. package/dist/core/__tests__/dead-node-policy-table.test.js +0 -77
  935. package/dist/core/__tests__/events-contract.test.d.ts +0 -1
  936. package/dist/core/__tests__/events-contract.test.js +0 -76
  937. package/dist/core/__tests__/extension-abort.test.d.ts +0 -1
  938. package/dist/core/__tests__/extension-abort.test.js +0 -21
  939. package/dist/core/__tests__/fault-classifier.test.d.ts +0 -1
  940. package/dist/core/__tests__/fault-classifier.test.js +0 -138
  941. package/dist/core/__tests__/fixtures/api-server-host.d.ts +0 -1
  942. package/dist/core/__tests__/fixtures/api-server-host.js +0 -47
  943. package/dist/core/__tests__/fixtures/c3-custom-provider-ext.d.ts +0 -2
  944. package/dist/core/__tests__/fixtures/c3-custom-provider-ext.js +0 -34
  945. package/dist/core/__tests__/fixtures/c5-command-boundary-ext.d.ts +0 -2
  946. package/dist/core/__tests__/fixtures/c5-command-boundary-ext.js +0 -32
  947. package/dist/core/__tests__/fixtures/fake-engine.d.ts +0 -347
  948. package/dist/core/__tests__/fixtures/fake-engine.js +0 -1509
  949. package/dist/core/__tests__/fixtures/fake-pi-host.d.ts +0 -2
  950. package/dist/core/__tests__/fixtures/fake-pi-host.js +0 -311
  951. package/dist/core/__tests__/fixtures/memory-slash-live-probe.d.ts +0 -1
  952. package/dist/core/__tests__/fixtures/memory-slash-live-probe.js +0 -75
  953. package/dist/core/__tests__/fixtures/metadata-migration-race.d.ts +0 -1
  954. package/dist/core/__tests__/fixtures/metadata-migration-race.js +0 -49
  955. package/dist/core/__tests__/focuses.test.d.ts +0 -1
  956. package/dist/core/__tests__/focuses.test.js +0 -113
  957. package/dist/core/__tests__/fs-utils.test.d.ts +0 -1
  958. package/dist/core/__tests__/fs-utils.test.js +0 -50
  959. package/dist/core/__tests__/helpers/broker-clients.d.ts +0 -43
  960. package/dist/core/__tests__/helpers/broker-clients.js +0 -174
  961. package/dist/core/__tests__/helpers/fleet.d.ts +0 -12
  962. package/dist/core/__tests__/helpers/fleet.js +0 -50
  963. package/dist/core/__tests__/helpers/harness.d.ts +0 -124
  964. package/dist/core/__tests__/helpers/harness.js +0 -806
  965. package/dist/core/__tests__/helpers/viewer-panes.d.ts +0 -12
  966. package/dist/core/__tests__/helpers/viewer-panes.js +0 -133
  967. package/dist/core/__tests__/history-inbox.test.d.ts +0 -1
  968. package/dist/core/__tests__/history-inbox.test.js +0 -121
  969. package/dist/core/__tests__/human-action-delivery.test.d.ts +0 -1
  970. package/dist/core/__tests__/human-action-delivery.test.js +0 -162
  971. package/dist/core/__tests__/human-actions.test.d.ts +0 -1
  972. package/dist/core/__tests__/human-actions.test.js +0 -120
  973. package/dist/core/__tests__/inline-memory-refs.test.d.ts +0 -1
  974. package/dist/core/__tests__/inline-memory-refs.test.js +0 -225
  975. package/dist/core/__tests__/integration/broker-fork-seam.test.d.ts +0 -1
  976. package/dist/core/__tests__/integration/broker-fork-seam.test.js +0 -193
  977. package/dist/core/__tests__/integration/broker-sdk-wiring.test.d.ts +0 -1
  978. package/dist/core/__tests__/integration/broker-sdk-wiring.test.js +0 -750
  979. package/dist/core/__tests__/integration/broker-snapshot-history.test.d.ts +0 -1
  980. package/dist/core/__tests__/integration/broker-snapshot-history.test.js +0 -288
  981. package/dist/core/__tests__/integration/broker-wedge-supervision.test.d.ts +0 -1
  982. package/dist/core/__tests__/integration/broker-wedge-supervision.test.js +0 -103
  983. package/dist/core/__tests__/integration/command-plugins.test.d.ts +0 -1
  984. package/dist/core/__tests__/integration/command-plugins.test.js +0 -1200
  985. package/dist/core/__tests__/integration/deferred-no-wake.test.d.ts +0 -1
  986. package/dist/core/__tests__/integration/deferred-no-wake.test.js +0 -610
  987. package/dist/core/__tests__/integration/flagship-lifecycle.test.d.ts +0 -1
  988. package/dist/core/__tests__/integration/flagship-lifecycle.test.js +0 -264
  989. package/dist/core/__tests__/integration/fresh-home-commands.test.d.ts +0 -1
  990. package/dist/core/__tests__/integration/fresh-home-commands.test.js +0 -49
  991. package/dist/core/__tests__/integration/git-optional-locks.test.d.ts +0 -1
  992. package/dist/core/__tests__/integration/git-optional-locks.test.js +0 -81
  993. package/dist/core/__tests__/integration/host-teardown-process-group.test.d.ts +0 -1
  994. package/dist/core/__tests__/integration/host-teardown-process-group.test.js +0 -480
  995. package/dist/core/__tests__/integration/lifecycle-hooks.test.d.ts +0 -1
  996. package/dist/core/__tests__/integration/lifecycle-hooks.test.js +0 -498
  997. package/dist/core/__tests__/integration/live-mutation-verbs.test.d.ts +0 -1
  998. package/dist/core/__tests__/integration/live-mutation-verbs.test.js +0 -180
  999. package/dist/core/__tests__/integration/live-mutation.test.d.ts +0 -1
  1000. package/dist/core/__tests__/integration/live-mutation.test.js +0 -173
  1001. package/dist/core/__tests__/integration/plugin-revalidate.test.d.ts +0 -1
  1002. package/dist/core/__tests__/integration/plugin-revalidate.test.js +0 -359
  1003. package/dist/core/__tests__/integration/profile-pause.test.d.ts +0 -1
  1004. package/dist/core/__tests__/integration/profile-pause.test.js +0 -115
  1005. package/dist/core/__tests__/integration/refresh-stall-recycle.test.d.ts +0 -1
  1006. package/dist/core/__tests__/integration/refresh-stall-recycle.test.js +0 -151
  1007. package/dist/core/__tests__/integration/revive.test.d.ts +0 -1
  1008. package/dist/core/__tests__/integration/revive.test.js +0 -615
  1009. package/dist/core/__tests__/integration/spawn-root.test.d.ts +0 -1
  1010. package/dist/core/__tests__/integration/spawn-root.test.js +0 -536
  1011. package/dist/core/__tests__/integration/subscription-delivery.test.d.ts +0 -1
  1012. package/dist/core/__tests__/integration/subscription-delivery.test.js +0 -132
  1013. package/dist/core/__tests__/integration/tmux-surface.test.d.ts +0 -1
  1014. package/dist/core/__tests__/integration/tmux-surface.test.js +0 -330
  1015. package/dist/core/__tests__/integration/worktree-land.test.d.ts +0 -1
  1016. package/dist/core/__tests__/integration/worktree-land.test.js +0 -607
  1017. package/dist/core/__tests__/integration/worktree-reap.test.d.ts +0 -1
  1018. package/dist/core/__tests__/integration/worktree-reap.test.js +0 -842
  1019. package/dist/core/__tests__/kickoff.test.d.ts +0 -1
  1020. package/dist/core/__tests__/kickoff.test.js +0 -196
  1021. package/dist/core/__tests__/lifecycle-terminal-reasons.test.d.ts +0 -1
  1022. package/dist/core/__tests__/lifecycle-terminal-reasons.test.js +0 -62
  1023. package/dist/core/__tests__/lifecycle.test.d.ts +0 -1
  1024. package/dist/core/__tests__/lifecycle.test.js +0 -176
  1025. package/dist/core/__tests__/live-lifecycle-prompt.test.d.ts +0 -1
  1026. package/dist/core/__tests__/live-lifecycle-prompt.test.js +0 -63
  1027. package/dist/core/__tests__/memory-resolver-precedence.test.d.ts +0 -1
  1028. package/dist/core/__tests__/memory-resolver-precedence.test.js +0 -228
  1029. package/dist/core/__tests__/migration.test.d.ts +0 -1
  1030. package/dist/core/__tests__/migration.test.js +0 -946
  1031. package/dist/core/__tests__/model-pin-durability.test.d.ts +0 -1
  1032. package/dist/core/__tests__/model-pin-durability.test.js +0 -195
  1033. package/dist/core/__tests__/model-routes-config.test.d.ts +0 -1
  1034. package/dist/core/__tests__/model-routes-config.test.js +0 -150
  1035. package/dist/core/__tests__/nested-store-discovery.test.d.ts +0 -1
  1036. package/dist/core/__tests__/nested-store-discovery.test.js +0 -89
  1037. package/dist/core/__tests__/node-outcome.test.d.ts +0 -1
  1038. package/dist/core/__tests__/node-outcome.test.js +0 -246
  1039. package/dist/core/__tests__/on-read-crouter-home-fence.test.d.ts +0 -1
  1040. package/dist/core/__tests__/on-read-crouter-home-fence.test.js +0 -66
  1041. package/dist/core/__tests__/on-read-dedup-resume.test.d.ts +0 -1
  1042. package/dist/core/__tests__/on-read-dedup-resume.test.js +0 -132
  1043. package/dist/core/__tests__/on-read-nested-store.test.d.ts +0 -1
  1044. package/dist/core/__tests__/on-read-nested-store.test.js +0 -135
  1045. package/dist/core/__tests__/parse-argv-stdin-secret.test.d.ts +0 -1
  1046. package/dist/core/__tests__/parse-argv-stdin-secret.test.js +0 -68
  1047. package/dist/core/__tests__/passive-subscription.test.d.ts +0 -1
  1048. package/dist/core/__tests__/passive-subscription.test.js +0 -120
  1049. package/dist/core/__tests__/pid-identity-match.test.d.ts +0 -1
  1050. package/dist/core/__tests__/pid-identity-match.test.js +0 -145
  1051. package/dist/core/__tests__/pid-zombie.test.d.ts +0 -1
  1052. package/dist/core/__tests__/pid-zombie.test.js +0 -42
  1053. package/dist/core/__tests__/plugin-kinds.test.d.ts +0 -1
  1054. package/dist/core/__tests__/plugin-kinds.test.js +0 -182
  1055. package/dist/core/__tests__/plugin-link-symlinked-scope-root.test.d.ts +0 -1
  1056. package/dist/core/__tests__/plugin-link-symlinked-scope-root.test.js +0 -43
  1057. package/dist/core/__tests__/plugin-page-components.test.d.ts +0 -1
  1058. package/dist/core/__tests__/plugin-page-components.test.js +0 -159
  1059. package/dist/core/__tests__/preview-mirror-cap.test.d.ts +0 -1
  1060. package/dist/core/__tests__/preview-mirror-cap.test.js +0 -53
  1061. package/dist/core/__tests__/preview-registry-sync.test.d.ts +0 -1
  1062. package/dist/core/__tests__/preview-registry-sync.test.js +0 -107
  1063. package/dist/core/__tests__/profile-project-memory-delivery.test.d.ts +0 -1
  1064. package/dist/core/__tests__/profile-project-memory-delivery.test.js +0 -644
  1065. package/dist/core/__tests__/prospective-inventory-capability-parity.test.d.ts +0 -1
  1066. package/dist/core/__tests__/prospective-inventory-capability-parity.test.js +0 -91
  1067. package/dist/core/__tests__/prune.test.d.ts +0 -1
  1068. package/dist/core/__tests__/prune.test.js +0 -243
  1069. package/dist/core/__tests__/push-final-guard.test.d.ts +0 -1
  1070. package/dist/core/__tests__/push-final-guard.test.js +0 -191
  1071. package/dist/core/__tests__/relaunch-root.test.d.ts +0 -1
  1072. package/dist/core/__tests__/relaunch-root.test.js +0 -249
  1073. package/dist/core/__tests__/remote-canvas-target.test.d.ts +0 -1
  1074. package/dist/core/__tests__/remote-canvas-target.test.js +0 -93
  1075. package/dist/core/__tests__/repository-association.test.d.ts +0 -1
  1076. package/dist/core/__tests__/repository-association.test.js +0 -153
  1077. package/dist/core/__tests__/repository-root-identity.test.d.ts +0 -1
  1078. package/dist/core/__tests__/repository-root-identity.test.js +0 -223
  1079. package/dist/core/__tests__/respawn-throttle.test.d.ts +0 -1
  1080. package/dist/core/__tests__/respawn-throttle.test.js +0 -20
  1081. package/dist/core/__tests__/review-model-floor.test.d.ts +0 -1
  1082. package/dist/core/__tests__/review-model-floor.test.js +0 -83
  1083. package/dist/core/__tests__/revive-all.test.d.ts +0 -1
  1084. package/dist/core/__tests__/revive-all.test.js +0 -157
  1085. package/dist/core/__tests__/revive-capacity.test.d.ts +0 -1
  1086. package/dist/core/__tests__/revive-capacity.test.js +0 -184
  1087. package/dist/core/__tests__/revive-parked-fresh.test.d.ts +0 -1
  1088. package/dist/core/__tests__/revive-parked-fresh.test.js +0 -176
  1089. package/dist/core/__tests__/scope-crouter-home-fence.test.d.ts +0 -1
  1090. package/dist/core/__tests__/scope-crouter-home-fence.test.js +0 -55
  1091. package/dist/core/__tests__/seam/broker-attach-multiclient.test.d.ts +0 -1
  1092. package/dist/core/__tests__/seam/broker-attach-multiclient.test.js +0 -262
  1093. package/dist/core/__tests__/seam/broker-attach-stream.test.d.ts +0 -1
  1094. package/dist/core/__tests__/seam/broker-attach-stream.test.js +0 -176
  1095. package/dist/core/__tests__/seam/broker-cap-freeze.test.d.ts +0 -1
  1096. package/dist/core/__tests__/seam/broker-cap-freeze.test.js +0 -155
  1097. package/dist/core/__tests__/seam/broker-crash-teardown.test.d.ts +0 -1
  1098. package/dist/core/__tests__/seam/broker-crash-teardown.test.js +0 -131
  1099. package/dist/core/__tests__/seam/broker-pane-resolution.test.d.ts +0 -1
  1100. package/dist/core/__tests__/seam/broker-pane-resolution.test.js +0 -204
  1101. package/dist/core/__tests__/seam/broker-provider-retry.test.d.ts +0 -1
  1102. package/dist/core/__tests__/seam/broker-provider-retry.test.js +0 -686
  1103. package/dist/core/__tests__/seam/broker-startup-diagnostics.test.d.ts +0 -1
  1104. package/dist/core/__tests__/seam/broker-startup-diagnostics.test.js +0 -82
  1105. package/dist/core/__tests__/seam/broker-whip-interrupt.test.d.ts +0 -1
  1106. package/dist/core/__tests__/seam/broker-whip-interrupt.test.js +0 -50
  1107. package/dist/core/__tests__/seam/deadline-wake-delivery.test.d.ts +0 -1
  1108. package/dist/core/__tests__/seam/deadline-wake-delivery.test.js +0 -138
  1109. package/dist/core/__tests__/seam/dormancy-release.test.d.ts +0 -1
  1110. package/dist/core/__tests__/seam/dormancy-release.test.js +0 -516
  1111. package/dist/core/__tests__/seam/held-deferred-human-prompt.test.d.ts +0 -1
  1112. package/dist/core/__tests__/seam/held-deferred-human-prompt.test.js +0 -65
  1113. package/dist/core/__tests__/seam/inbox-reference-guidance.test.d.ts +0 -1
  1114. package/dist/core/__tests__/seam/inbox-reference-guidance.test.js +0 -84
  1115. package/dist/core/__tests__/seam/memory-slash-node-relative-inventory.test.d.ts +0 -1
  1116. package/dist/core/__tests__/seam/memory-slash-node-relative-inventory.test.js +0 -160
  1117. package/dist/core/__tests__/seam/node-outcome-delivery.test.d.ts +0 -1
  1118. package/dist/core/__tests__/seam/node-outcome-delivery.test.js +0 -177
  1119. package/dist/core/__tests__/seam/prospective-inventory-stdout.test.d.ts +0 -1
  1120. package/dist/core/__tests__/seam/prospective-inventory-stdout.test.js +0 -31
  1121. package/dist/core/__tests__/seam/reopen-invalidated-worktree.test.d.ts +0 -1
  1122. package/dist/core/__tests__/seam/reopen-invalidated-worktree.test.js +0 -60
  1123. package/dist/core/__tests__/seam/self-close-teardown.test.d.ts +0 -1
  1124. package/dist/core/__tests__/seam/self-close-teardown.test.js +0 -49
  1125. package/dist/core/__tests__/seam/yield-refresh-transaction.test.d.ts +0 -1
  1126. package/dist/core/__tests__/seam/yield-refresh-transaction.test.js +0 -100
  1127. package/dist/core/__tests__/secrets.test.d.ts +0 -1
  1128. package/dist/core/__tests__/secrets.test.js +0 -49
  1129. package/dist/core/__tests__/self-update.test.d.ts +0 -1
  1130. package/dist/core/__tests__/self-update.test.js +0 -46
  1131. package/dist/core/__tests__/session-branch.test.d.ts +0 -1
  1132. package/dist/core/__tests__/session-branch.test.js +0 -61
  1133. package/dist/core/__tests__/session-cycles.test.d.ts +0 -1
  1134. package/dist/core/__tests__/session-cycles.test.js +0 -133
  1135. package/dist/core/__tests__/session-model.test.d.ts +0 -1
  1136. package/dist/core/__tests__/session-model.test.js +0 -111
  1137. package/dist/core/__tests__/spawn-worktree-id-conflict.test.d.ts +0 -1
  1138. package/dist/core/__tests__/spawn-worktree-id-conflict.test.js +0 -71
  1139. package/dist/core/__tests__/stdin-body-no-discard.test.d.ts +0 -1
  1140. package/dist/core/__tests__/stdin-body-no-discard.test.js +0 -33
  1141. package/dist/core/__tests__/stdout-errors.test.d.ts +0 -1
  1142. package/dist/core/__tests__/stdout-errors.test.js +0 -16
  1143. package/dist/core/__tests__/stop-guard.test.d.ts +0 -1
  1144. package/dist/core/__tests__/stop-guard.test.js +0 -237
  1145. package/dist/core/__tests__/stream-watchdog.test.d.ts +0 -1
  1146. package/dist/core/__tests__/stream-watchdog.test.js +0 -154
  1147. package/dist/core/__tests__/subscription-pool-credential-guard.test.d.ts +0 -1
  1148. package/dist/core/__tests__/subscription-pool-credential-guard.test.js +0 -95
  1149. package/dist/core/__tests__/tmux-driver-boundary.test.d.ts +0 -1
  1150. package/dist/core/__tests__/tmux-driver-boundary.test.js +0 -50
  1151. package/dist/core/__tests__/warm-claim-preference-snapshot.test.d.ts +0 -1
  1152. package/dist/core/__tests__/warm-claim-preference-snapshot.test.js +0 -54
  1153. package/dist/core/__tests__/worktree-landing.test.d.ts +0 -1
  1154. package/dist/core/__tests__/worktree-landing.test.js +0 -11
  1155. package/dist/core/__tests__/yield-ensures-daemon.test.d.ts +0 -1
  1156. package/dist/core/__tests__/yield-ensures-daemon.test.js +0 -55
  1157. package/dist/core/broker-client/__tests__/transport-relay.test.d.ts +0 -1
  1158. package/dist/core/broker-client/__tests__/transport-relay.test.js +0 -214
  1159. package/dist/core/broker-client/__tests__/transport-socket.test.d.ts +0 -1
  1160. package/dist/core/broker-client/__tests__/transport-socket.test.js +0 -73
  1161. package/dist/core/canvas/__tests__/attention.test.d.ts +0 -1
  1162. package/dist/core/canvas/__tests__/attention.test.js +0 -83
  1163. package/dist/core/canvas/__tests__/history-transcript.test.d.ts +0 -1
  1164. package/dist/core/canvas/__tests__/history-transcript.test.js +0 -102
  1165. package/dist/core/canvas/__tests__/remote-canvas-source.test.d.ts +0 -1
  1166. package/dist/core/canvas/__tests__/remote-canvas-source.test.js +0 -328
  1167. package/dist/core/canvas/__tests__/remote-transport.test.d.ts +0 -1
  1168. package/dist/core/canvas/__tests__/remote-transport.test.js +0 -97
  1169. package/dist/core/canvas/__tests__/render-remote.test.d.ts +0 -1
  1170. package/dist/core/canvas/__tests__/render-remote.test.js +0 -243
  1171. package/dist/core/canvas/__tests__/session-window.test.d.ts +0 -1
  1172. package/dist/core/canvas/__tests__/session-window.test.js +0 -70
  1173. package/dist/core/feed/__tests__/inbox-migrate.test.d.ts +0 -1
  1174. package/dist/core/feed/__tests__/inbox-migrate.test.js +0 -49
  1175. package/dist/core/human/__tests__/page-catalog.test.d.ts +0 -1
  1176. package/dist/core/human/__tests__/page-catalog.test.js +0 -20
  1177. package/dist/core/human/__tests__/page-html-markdown.test.d.ts +0 -1
  1178. package/dist/core/human/__tests__/page-html-markdown.test.js +0 -48
  1179. package/dist/core/human/__tests__/page.test.d.ts +0 -1
  1180. package/dist/core/human/__tests__/page.test.js +0 -92
  1181. package/dist/core/keybindings/__tests__/resolve.test.d.ts +0 -1
  1182. package/dist/core/keybindings/__tests__/resolve.test.js +0 -171
  1183. package/dist/core/profiles/__tests__/env-store.test.d.ts +0 -1
  1184. package/dist/core/profiles/__tests__/env-store.test.js +0 -99
  1185. package/dist/core/profiles/__tests__/fuzzy-match.test.d.ts +0 -1
  1186. package/dist/core/profiles/__tests__/fuzzy-match.test.js +0 -51
  1187. package/dist/core/profiles/__tests__/root-profile.test.d.ts +0 -1
  1188. package/dist/core/profiles/__tests__/root-profile.test.js +0 -59
  1189. package/dist/core/review/__tests__/capture-origin.test.d.ts +0 -1
  1190. package/dist/core/review/__tests__/capture-origin.test.js +0 -150
  1191. package/dist/core/review/__tests__/comments.test.d.ts +0 -1
  1192. package/dist/core/review/__tests__/comments.test.js +0 -116
  1193. package/dist/core/review/__tests__/remap.test.d.ts +0 -1
  1194. package/dist/core/review/__tests__/remap.test.js +0 -177
  1195. package/dist/core/review/__tests__/stage-identity.test.d.ts +0 -1
  1196. package/dist/core/review/__tests__/stage-identity.test.js +0 -150
  1197. package/dist/core/runtime/__tests__/engine-route.test.d.ts +0 -1
  1198. package/dist/core/runtime/__tests__/engine-route.test.js +0 -36
  1199. package/dist/core/runtime/__tests__/install-id-cache-scoping.test.d.ts +0 -1
  1200. package/dist/core/runtime/__tests__/install-id-cache-scoping.test.js +0 -45
  1201. package/dist/core/runtime/__tests__/node-env.test.d.ts +0 -1
  1202. package/dist/core/runtime/__tests__/node-env.test.js +0 -73
  1203. package/dist/core/runtime/__tests__/spawn-env.test.d.ts +0 -1
  1204. package/dist/core/runtime/__tests__/spawn-env.test.js +0 -259
  1205. package/dist/core/substrate/__tests__/surface-match-command.test.d.ts +0 -1
  1206. package/dist/core/substrate/__tests__/surface-match-command.test.js +0 -89
  1207. package/dist/core/substrate/__tests__/surface-match-memory-read.test.d.ts +0 -1
  1208. package/dist/core/substrate/__tests__/surface-match-memory-read.test.js +0 -28
  1209. package/dist/core/substrate/__tests__/surface-match-pre-command.test.d.ts +0 -1
  1210. package/dist/core/substrate/__tests__/surface-match-pre-command.test.js +0 -92
  1211. package/dist/daemon/__tests__/autostart-storm.test.d.ts +0 -1
  1212. package/dist/daemon/__tests__/autostart-storm.test.js +0 -128
  1213. package/dist/daemon/__tests__/broker-ownership.test.d.ts +0 -1
  1214. package/dist/daemon/__tests__/broker-ownership.test.js +0 -25
  1215. package/dist/daemon/__tests__/daemon-ownership.test.d.ts +0 -1
  1216. package/dist/daemon/__tests__/daemon-ownership.test.js +0 -27
  1217. package/dist/daemon/__tests__/helpers/source-daemon.d.ts +0 -30
  1218. package/dist/daemon/__tests__/helpers/source-daemon.js +0 -176
  1219. package/dist/daemon/__tests__/integration/api-startup-readiness.test.d.ts +0 -1
  1220. package/dist/daemon/__tests__/integration/api-startup-readiness.test.js +0 -83
  1221. package/dist/daemon/__tests__/integration/migration-startup.test.d.ts +0 -1
  1222. package/dist/daemon/__tests__/integration/migration-startup.test.js +0 -127
  1223. package/dist/daemon/__tests__/node-deadline-lane.test.d.ts +0 -1
  1224. package/dist/daemon/__tests__/node-deadline-lane.test.js +0 -77
  1225. package/dist/daemon/__tests__/node-outcome-birth-invariants.test.d.ts +0 -1
  1226. package/dist/daemon/__tests__/node-outcome-birth-invariants.test.js +0 -146
  1227. package/dist/daemon/__tests__/node-outcome-delivery.test.d.ts +0 -1
  1228. package/dist/daemon/__tests__/node-outcome-delivery.test.js +0 -116
  1229. package/dist/daemon/__tests__/reconciler-signature.test.d.ts +0 -1
  1230. package/dist/daemon/__tests__/reconciler-signature.test.js +0 -37
  1231. package/dist/daemon/__tests__/subtree-filter-freshness.test.d.ts +0 -1
  1232. package/dist/daemon/__tests__/subtree-filter-freshness.test.js +0 -63
  1233. package/dist/daemon/__tests__/thaw-order.test.d.ts +0 -1
  1234. package/dist/daemon/__tests__/thaw-order.test.js +0 -55
  1235. package/dist/daemon/api/__tests__/bridge-heartbeat.test.d.ts +0 -1
  1236. package/dist/daemon/api/__tests__/bridge-heartbeat.test.js +0 -58
  1237. package/dist/daemon/api/__tests__/broker-settle-park.test.d.ts +0 -1
  1238. package/dist/daemon/api/__tests__/broker-settle-park.test.js +0 -175
  1239. package/dist/daemon/api/__tests__/canvas-snapshot-fields.test.d.ts +0 -1
  1240. package/dist/daemon/api/__tests__/canvas-snapshot-fields.test.js +0 -216
  1241. package/dist/daemon/api/__tests__/error-seam.test.d.ts +0 -1
  1242. package/dist/daemon/api/__tests__/error-seam.test.js +0 -83
  1243. package/dist/daemon/api/__tests__/model-config.test.d.ts +0 -1
  1244. package/dist/daemon/api/__tests__/model-config.test.js +0 -73
  1245. package/dist/daemon/api/__tests__/node-create-description.test.d.ts +0 -1
  1246. package/dist/daemon/api/__tests__/node-create-description.test.js +0 -127
  1247. package/dist/daemon/api/__tests__/node-model-validation.test.d.ts +0 -1
  1248. package/dist/daemon/api/__tests__/node-model-validation.test.js +0 -183
  1249. package/dist/daemon/api/__tests__/nodes-activity-query.test.d.ts +0 -1
  1250. package/dist/daemon/api/__tests__/nodes-activity-query.test.js +0 -99
  1251. package/dist/daemon/api/__tests__/profile-launch-gates.test.d.ts +0 -1
  1252. package/dist/daemon/api/__tests__/profile-launch-gates.test.js +0 -172
  1253. package/dist/daemon/api/__tests__/profile-metadata-route.test.d.ts +0 -1
  1254. package/dist/daemon/api/__tests__/profile-metadata-route.test.js +0 -95
  1255. package/dist/daemon/api/__tests__/reopen-delivery.test.d.ts +0 -1
  1256. package/dist/daemon/api/__tests__/reopen-delivery.test.js +0 -260
  1257. package/dist/daemon/api/__tests__/seam/api-server.test.d.ts +0 -1
  1258. package/dist/daemon/api/__tests__/seam/api-server.test.js +0 -442
  1259. package/dist/daemon/api/__tests__/seam/b10-attach-modelauth.test.d.ts +0 -1
  1260. package/dist/daemon/api/__tests__/seam/b10-attach-modelauth.test.js +0 -310
  1261. package/dist/daemon/api/__tests__/seam/leaf-api-parity.test.d.ts +0 -1
  1262. package/dist/daemon/api/__tests__/seam/leaf-api-parity.test.js +0 -351
  1263. package/dist/daemon/api/__tests__/seam/node-outcome-api.test.d.ts +0 -1
  1264. package/dist/daemon/api/__tests__/seam/node-outcome-api.test.js +0 -159
  1265. package/dist/daemon/api/__tests__/seam/profile-delete.test.d.ts +0 -1
  1266. package/dist/daemon/api/__tests__/seam/profile-delete.test.js +0 -372
  1267. package/dist/daemon/api/__tests__/submit-result.test.d.ts +0 -1
  1268. package/dist/daemon/api/__tests__/submit-result.test.js +0 -215
  1269. package/dist/migrations/__tests__/activation-concurrency.test.d.ts +0 -1
  1270. package/dist/migrations/__tests__/activation-concurrency.test.js +0 -146
  1271. package/dist/migrations/__tests__/activation.test.d.ts +0 -1
  1272. package/dist/migrations/__tests__/activation.test.js +0 -152
  1273. package/dist/migrations/__tests__/deletion-and-root-declaration.test.d.ts +0 -1
  1274. package/dist/migrations/__tests__/deletion-and-root-declaration.test.js +0 -149
  1275. package/dist/pi-extensions/__tests__/canvas-bash-valve.test.d.ts +0 -1
  1276. package/dist/pi-extensions/__tests__/canvas-bash-valve.test.js +0 -86
  1277. package/dist/pi-extensions/__tests__/canvas-context-intro.test.d.ts +0 -1
  1278. package/dist/pi-extensions/__tests__/canvas-context-intro.test.js +0 -364
  1279. package/dist/pi-extensions/__tests__/canvas-goal-capture-envelope.test.d.ts +0 -1
  1280. package/dist/pi-extensions/__tests__/canvas-goal-capture-envelope.test.js +0 -106
  1281. package/dist/pi-extensions/__tests__/canvas-recap.test.d.ts +0 -1
  1282. package/dist/pi-extensions/__tests__/canvas-recap.test.js +0 -62
  1283. package/dist/pi-extensions/__tests__/canvas-stophook-agentend.test.d.ts +0 -1
  1284. package/dist/pi-extensions/__tests__/canvas-stophook-agentend.test.js +0 -432
  1285. package/dist/pi-extensions/__tests__/canvas-stophook-context-nudge.test.d.ts +0 -1
  1286. package/dist/pi-extensions/__tests__/canvas-stophook-context-nudge.test.js +0 -152
  1287. package/dist/pi-extensions/__tests__/canvas-structured-output.test.d.ts +0 -1
  1288. package/dist/pi-extensions/__tests__/canvas-structured-output.test.js +0 -88
  1289. package/dist/pi-extensions/__tests__/canvas-tool-guide.test.d.ts +0 -1
  1290. package/dist/pi-extensions/__tests__/canvas-tool-guide.test.js +0 -96
  1291. package/dist/pi-extensions/__tests__/integration/canvas-bash-valve.test.d.ts +0 -1
  1292. package/dist/pi-extensions/__tests__/integration/canvas-bash-valve.test.js +0 -133
  1293. package/dist/pi-extensions/__tests__/pre-command-gate.test.d.ts +0 -1
  1294. package/dist/pi-extensions/__tests__/pre-command-gate.test.js +0 -232
  1295. package/dist/shared/__tests__/env-boundary.test.d.ts +0 -1
  1296. package/dist/shared/__tests__/env-boundary.test.js +0 -95
  1297. package/dist/shared/__tests__/generated-context-grammar.test.d.ts +0 -1
  1298. package/dist/shared/__tests__/generated-context-grammar.test.js +0 -80
  1299. package/docs/compat/hearth-crtr-v1.md +0 -179
  1300. package/docs/compat/hearth-crtr-v2.md +0 -177
  1301. package/docs/compat/hearth-crtr-v3.md +0 -176
  1302. package/docs/compat/hearth-crtr-v4.md +0 -182
  1303. package/docs/compat/hearth-crtr-v5.md +0 -169
@@ -1,2530 +0,0 @@
1
- import { before, after, test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { spawn } from "node:child_process";
4
- import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
5
- import { LockedJsonFile } from "../../../../../core/locked-file.ts";
6
- import { initializeFreshScopedStore } from "../../../../../core/scoped-state/migrate.ts";
7
- import { writeRawConfig } from "../../../../../core/config.ts";
8
- import { expandModelCandidates } from "../../../../../core/model-routes.ts";
9
- import type { SubscriptionCredential } from "../../../lib/subscription-state.ts";
10
- import { join } from "node:path";
11
- import { tmpdir } from "node:os";
12
-
13
- // Child processes reach crouter's locked-file primitive by absolute URL: they run under
14
- // tsx with `--input-type=module`, so a bare relative specifier has no base to resolve from.
15
- const lockedFileUrl = new URL("../../../../../core/locked-file.ts", import.meta.url).href;
16
-
17
- let home: string;
18
- let nodeCwd: string;
19
- let helpers: typeof import("../../../lib/subscription-state.ts");
20
- let providerManagement: typeof import("../../../../../core/provider-management.ts");
21
- let rotationModule: typeof import("../../provider-rotation.ts");
22
- let providerRotationExtension: typeof import("../../provider-rotation.ts").default;
23
- const previousHome = process.env.HOME;
24
- const previousAgentDir = process.env.PI_CODING_AGENT_DIR;
25
- const previousModelIntent = process.env.CRTR_MODEL_INTENT;
26
- const previousNodeCwd = process.env.CRTR_NODE_CWD;
27
-
28
- function writeLadderConfig() {
29
- writeRawConfig("user", {
30
- modelLadders: {
31
- anthropic: {
32
- ultra: "anthropic/claude-fable-5:high",
33
- strong: "anthropic/claude-opus-4-8:high",
34
- medium: "anthropic/claude-sonnet-4-6:high",
35
- light: "anthropic/custom-light-x",
36
- },
37
- openai: {
38
- ultra: "openai-codex/gpt-5.6-sol:max",
39
- strong: "openai-codex/gpt-5.6-sol:high",
40
- medium: "openai-codex/gpt-5.6-terra:medium",
41
- light: "openai-codex/gpt-5.6-luna:low",
42
- },
43
- },
44
- });
45
- }
46
-
47
- function writeSameModelRouteConfig() {
48
- writeRawConfig("user", {
49
- modelRoutes: {
50
- primary: { family: "openai", credentialSource: "primary", providerId: "openai-codex", models: { ultra: "openai-codex/gpt-5.6-sol:high", strong: "openai-codex/gpt-5.6-sol:high", medium: "openai-codex/gpt-5.6-sol:high", light: "openai-codex/gpt-5.6-sol:high" } },
51
- alternate: { family: "openai", credentialSource: "alternate", providerId: "anthropic", models: { ultra: "anthropic/gpt-5.6-sol:high", strong: "anthropic/gpt-5.6-sol:high", medium: "anthropic/gpt-5.6-sol:high", light: "anthropic/gpt-5.6-sol:high" } },
52
- },
53
- modelRouting: { credentialSourceOrder: ["primary", "alternate"], familyOrder: ["openai"] },
54
- });
55
- }
56
-
57
- function credential(label: string, overrides: Partial<SubscriptionCredential> = {}): SubscriptionCredential {
58
- return {
59
- label,
60
- refresh: `refresh-${label}`,
61
- access: `access-${label}`,
62
- expires: Date.now() + 60_000,
63
- rateLimitedUntil: 0,
64
- lastAttemptAt: 0,
65
- lastRateLimitedAt: 0,
66
- ...overrides,
67
- };
68
- }
69
-
70
- async function resetRotationState() {
71
- rmSync(join(home, ".pi", "agent", "auth.json"), { force: true });
72
- await helpers.writeSubscriptionPool("anthropic", []);
73
- await helpers.writeSubscriptionPool("openai-codex", []);
74
- rmSync(join(home, ".pi", "agent", "provider-rotation.json"), { force: true });
75
- }
76
-
77
- function observeLines(stream: NodeJS.ReadableStream, onLine: (line: string) => void): void {
78
- let buffer = "";
79
- stream.setEncoding("utf8");
80
- stream.on("data", (chunk: string) => {
81
- buffer += chunk;
82
- let newlineIndex = buffer.indexOf("\n");
83
- while (newlineIndex !== -1) {
84
- const line = buffer.slice(0, newlineIndex);
85
- buffer = buffer.slice(newlineIndex + 1);
86
- onLine(line);
87
- newlineIndex = buffer.indexOf("\n");
88
- }
89
- });
90
- }
91
-
92
- function within<T>(promise: Promise<T>, ms: number, message: string): Promise<T> {
93
- return new Promise<T>((resolve, reject) => {
94
- const timer = setTimeout(() => reject(new Error(message)), ms);
95
- promise.then(
96
- (value) => {
97
- clearTimeout(timer);
98
- resolve(value);
99
- },
100
- (error: unknown) => {
101
- clearTimeout(timer);
102
- reject(error);
103
- },
104
- );
105
- });
106
- }
107
-
108
- before(async () => {
109
- home = mkdtempSync(join(tmpdir(), "pi-provider-rotation-"));
110
- nodeCwd = mkdtempSync(join(tmpdir(), "pi-provider-rotation-cwd-"));
111
- process.env.HOME = home;
112
- process.env.PI_CODING_AGENT_DIR = join(home, ".pi", "agent");
113
- process.env.CRTR_NODE_CWD = nodeCwd;
114
- initializeFreshScopedStore({ kind: "user", root: join(home, ".crouter") });
115
- initializeFreshScopedStore({ kind: "provider", root: process.env.PI_CODING_AGENT_DIR });
116
- helpers = await import("../../../lib/subscription-state.ts");
117
- providerManagement = await import("../../../../../core/provider-management.ts");
118
- rotationModule = await import("../../provider-rotation.ts");
119
- providerRotationExtension = rotationModule.default;
120
- });
121
-
122
- after(() => {
123
- if (previousHome === undefined) delete process.env.HOME;
124
- else process.env.HOME = previousHome;
125
- if (previousAgentDir === undefined) delete process.env.PI_CODING_AGENT_DIR;
126
- else process.env.PI_CODING_AGENT_DIR = previousAgentDir;
127
- if (previousModelIntent === undefined) delete process.env.CRTR_MODEL_INTENT;
128
- else process.env.CRTR_MODEL_INTENT = previousModelIntent;
129
- if (previousNodeCwd === undefined) delete process.env.CRTR_NODE_CWD;
130
- else process.env.CRTR_NODE_CWD = previousNodeCwd;
131
- rmSync(home, { recursive: true, force: true });
132
- rmSync(nodeCwd, { recursive: true, force: true });
133
- });
134
-
135
- // Concurrent refreshes of the same expired pool credential serialize read, token exchange,
136
- // and write under one lock. The injected refresh runs once; the second caller observes the
137
- // winner's fresh token and does not make a second single-use refresh request.
138
- test("brick-repro: concurrent refreshes of the same expired pool token serialize through the lock (exactly one network refresh)", async () => {
139
- await resetRotationState();
140
- await helpers.writeSubscriptionPool("anthropic", [credential("anthropic", { refresh: "shared-refresh", access: "access-stale", expires: 1 })]);
141
- const poolEntry = helpers.readSubscriptionPool("anthropic")[0];
142
- assert.equal(poolEntry.label, "anthropic");
143
-
144
- let refreshCalls = 0;
145
- const requestController = new AbortController();
146
- const refreshSignals: (AbortSignal | undefined)[] = [];
147
- rotationModule.__setRefreshForProviderForTest(async (_providerId, refreshToken, signal) => {
148
- refreshCalls += 1;
149
- refreshSignals.push(signal);
150
- if (refreshCalls === 1) {
151
- return { refresh: "shared-refresh-rotated", access: "access-fresh", expires: Date.now() + 60_000 };
152
- }
153
- // Any refresh call after the first is simulating "this refresh token was already
154
- // consumed by the winner" -- a genuine invalid_grant from the provider.
155
- throw Object.assign(new Error(`HTTP request failed. status=400; body={"error":"invalid_grant"} for token ${refreshToken}`), { status: 400 });
156
- });
157
-
158
- try {
159
- const [a, b] = await Promise.all([
160
- rotationModule.__refreshCredentialIfNeededForTest("anthropic", poolEntry, requestController.signal),
161
- rotationModule.__refreshCredentialIfNeededForTest("anthropic", poolEntry, requestController.signal),
162
- ]);
163
-
164
- assert.equal(refreshCalls, 1);
165
- assert.deepEqual(refreshSignals, [requestController.signal], 'the request abort signal reaches the serialized token exchange');
166
- assert.equal(a.access, "access-fresh");
167
- assert.equal(b.access, "access-fresh");
168
-
169
- // The rotated value landed on the account it came from -- the pool is the only store now.
170
- const [stored] = helpers.readSubscriptionPool("anthropic");
171
- assert.equal(stored.label, "anthropic");
172
- assert.equal(stored.access, "access-fresh");
173
- assert.equal(stored.refresh, "shared-refresh-rotated");
174
- } finally {
175
- rotationModule.__setRefreshForProviderForTest(undefined);
176
- }
177
- });
178
-
179
- test("a fresh default credential bypasses the cross-process refresh lock", async () => {
180
- await resetRotationState();
181
- await helpers.writeSubscriptionPool("openai-codex", [
182
- credential("openai-codex", { refresh: "refresh-codex", access: "access-codex-fresh", expires: Date.now() + 60_000 }),
183
- ]);
184
- const poolEntry = helpers.readSubscriptionPool("openai-codex")[0];
185
- const originalWithLockAsync = LockedJsonFile.prototype.withLockAsync;
186
- LockedJsonFile.prototype.withLockAsync = async () => {
187
- throw new Error("fresh credential unexpectedly joined the auth refresh lock");
188
- };
189
-
190
- try {
191
- const resolved = await rotationModule.__refreshCredentialIfNeededForTest("openai-codex", poolEntry);
192
- assert.equal(resolved.access, "access-codex-fresh");
193
- } finally {
194
- LockedJsonFile.prototype.withLockAsync = originalWithLockAsync;
195
- }
196
- });
197
-
198
- test("subscription bookkeeping retries lock collisions and then degrades without failing the provider turn", async () => {
199
- const waits: number[] = [];
200
- rotationModule.__setSleepForTest(async (ms) => { waits.push(ms); });
201
- try {
202
- let eventualCalls = 0;
203
- const eventual = await rotationModule.__recordSubscriptionMutationForTest("testing eventual acquisition", async () => {
204
- eventualCalls++;
205
- if (eventualCalls < 4) throw Object.assign(new Error("Lock file is already being held"), { code: "ELOCKED" });
206
- });
207
- assert.equal(eventual, true);
208
- assert.equal(eventualCalls, 4);
209
- assert.deepEqual(waits, [25, 75, 150]);
210
-
211
- waits.length = 0;
212
- let exhaustedCalls = 0;
213
- const exhausted = await rotationModule.__recordSubscriptionMutationForTest("testing exhaustion", async () => {
214
- exhaustedCalls++;
215
- throw Object.assign(new Error("Lock file is already being held"), { code: "ELOCKED" });
216
- });
217
- assert.equal(exhausted, false);
218
- assert.equal(exhaustedCalls, 4);
219
- assert.deepEqual(waits, [25, 75, 150]);
220
- } finally {
221
- rotationModule.__setSleepForTest(undefined);
222
- }
223
- });
224
-
225
- // A GENUINE invalid_grant (the refresh token is actually dead, not merely raced) must
226
- // still reach the real cooldown -> reauth path through the new lock-delegated seam --
227
- // the fix must not accidentally swallow or misclassify a real failure. Drives it through
228
- // the actual provider stream (not the raw helper) so `isInvalidRefreshTokenError` +
229
- // reauthentication + the credential-store persist all fire for real.
230
- test("a genuine invalid_grant through the pool account's lock still reauthenticates and persists to the pool", async () => {
231
- await resetRotationState();
232
- writeLadderConfig();
233
- await helpers.writeSubscriptionPool("anthropic", [credential("anthropic", { refresh: "dead-refresh", access: "access-stale", expires: 1 })]);
234
-
235
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
236
- ctx.hasUI = true;
237
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
238
- ctx.ui.notify = () => {};
239
- const loginCalls: string[] = [];
240
- // Plain Error, no typed .status: real pi-ai refresh failures carry the HTTP status
241
- // in the message text, so the refresh path still recognizes an invalid_grant from this
242
- // shape.
243
- rotationModule.__setRefreshForProviderForTest(async (providerId) => {
244
- if (providerId === "anthropic") {
245
- throw new Error('HTTP request failed. status=400; body={"error":"invalid_grant","error_description":"Refresh token not found or invalid"}');
246
- }
247
- throw new Error("unexpected refresh");
248
- });
249
- rotationModule.__setLoginForProviderForTest(async (providerId) => {
250
- loginCalls.push(providerId);
251
- return { refresh: "refresh-reauthed", access: "access-reauthed", expires: Date.now() + 60_000 };
252
- });
253
- rotationModule.__setStreamForProviderForTest((_model, _context, options) => {
254
- return (async function* () {
255
- yield { type: "text_delta", text: `hello with ${options?.apiKey}` };
256
- })() as any;
257
- });
258
-
259
- try {
260
- await providerRotationExtension(pi);
261
- await events.session_start?.({}, ctx);
262
- const stream = providers["anthropic"].streamSimple({ provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" }, ctx);
263
- const emitted = [] as any[];
264
- for await (const event of stream) emitted.push(event);
265
-
266
- assert.deepEqual(loginCalls, ["anthropic"]);
267
- assert.deepEqual(setModelCalls, []);
268
- assert.ok(emitted.some((e) => e.type === "text_delta"));
269
- assert.ok(!emitted.some((e) => e.type === "error"));
270
-
271
- // Reauth persisted onto the pool account's own pool entry -- the single owner -- with the
272
- // cooldown the failed refresh recorded cleared again.
273
- const [entry] = helpers.readSubscriptionPool("anthropic");
274
- assert.equal(entry.label, "anthropic");
275
- assert.equal(entry.refresh, "refresh-reauthed");
276
- assert.equal(entry.access, "access-reauthed");
277
- assert.equal(entry.rateLimitedUntil, 0);
278
- assert.equal(entry.authFailure, undefined);
279
- } finally {
280
- rotationModule.__setRefreshForProviderForTest(undefined);
281
- rotationModule.__setLoginForProviderForTest(undefined);
282
- rotationModule.__setStreamForProviderForTest(undefined);
283
- }
284
- });
285
-
286
- // A genuine invalid_grant through the pool account's lock seam cools the credential down
287
- // and falls back cross-provider when UI is unavailable: an expired default pool entry, an
288
- // authenticated Codex fallback, and the real streamSimple path all participate.
289
- test("a genuine invalid_grant through the pool account's lock with no UI reaches cooldown and falls back cross-provider", async () => {
290
- await resetRotationState();
291
- writeLadderConfig();
292
- await helpers.writeSubscriptionPool("anthropic", [credential("anthropic", { refresh: "dead-refresh", access: "access-stale", expires: 1 })]);
293
- // Authenticated Codex fallback already sitting in its own pool.
294
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
295
-
296
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
297
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
298
- // ctx.hasUI is deliberately left falsy: reauth must be impossible, so the only path
299
- // forward is cooldown + cross-provider fallback.
300
- const streamedModels: any[] = [];
301
- rotationModule.__setRefreshForProviderForTest(async (providerId) => {
302
- if (providerId === "anthropic") {
303
- // Plain Error, no typed .status: the refresh path reads the invalid_grant shape from
304
- // the message text, matching production failures.
305
- throw new Error('HTTP request failed. status=400; body={"error":"invalid_grant","error_description":"Refresh token not found or invalid"}');
306
- }
307
- throw new Error("unexpected refresh");
308
- });
309
- rotationModule.__setStreamForProviderForTest((model, _context, options) => {
310
- streamedModels.push(model);
311
- return (async function* () {
312
- yield { type: "text_delta", text: "hello from codex fallback" };
313
- })() as any;
314
- });
315
-
316
- try {
317
- await providerRotationExtension(pi);
318
- await events.session_start?.({}, ctx);
319
- const stream = providers["anthropic"].streamSimple(
320
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
321
- ctx,
322
- );
323
- const emitted = [] as any[];
324
- for await (const event of stream) emitted.push(event);
325
-
326
- // Fell back cross-provider and actually served the turn on Codex.
327
- assert.deepEqual(setModelCalls.map((m) => m.provider), ["openai-codex"]);
328
- assert.equal(streamedModels.length, 1);
329
- assert.equal(streamedModels[0].provider, "openai-codex");
330
- assert.ok(emitted.some((e) => e.type === "text_delta"));
331
- assert.ok(!emitted.some((e) => e.type === "error"));
332
-
333
- // The pool account's entry got the ~30-day invalid-refresh exclusion, tagged with its auth
334
- // cause through the real locked-refresh seam -- not a bypass.
335
- const [entry] = helpers.readSubscriptionPool("anthropic");
336
- assert.equal(entry.label, "anthropic");
337
- assert.ok(entry.rateLimitedUntil > Date.now() + 29 * 24 * 60 * 60 * 1000);
338
- assert.equal(entry.authFailure, "invalid_grant");
339
-
340
- // No UI -- no reauth happened; the dead credential is still stored unchanged.
341
- assert.equal(entry.refresh, "dead-refresh");
342
- } finally {
343
- rotationModule.__setRefreshForProviderForTest(undefined);
344
- rotationModule.__setStreamForProviderForTest(undefined);
345
- }
346
- });
347
-
348
- test("the synthesized ladder registry preserves family preference before configured strength fallback", async () => {
349
- await resetRotationState();
350
- writeLadderConfig();
351
- helpers.writeRotationConfig({ defaultFallbackStrength: "medium" });
352
-
353
- const candidates = expandModelCandidates({
354
- kind: "logical",
355
- family: "anthropic",
356
- strength: "strong",
357
- spec: "anthropic/claude-opus-4-8:high",
358
- }, nodeCwd, null);
359
-
360
- assert.deepEqual(candidates.map((candidate) => `${candidate.family}/${candidate.strength}`), [
361
- "anthropic/strong", "openai/strong", "anthropic/medium", "openai/medium",
362
- ]);
363
- });
364
-
365
- test("shared launch/general candidate policy preserves a viable request, skips a cooling request, and reports total exhaustion", async () => {
366
- const requested = { providerId: "anthropic", modelId: "claude-opus", registered: true, authenticated: true };
367
- const fallback = { providerId: "openai-codex", modelId: "gpt-5", registered: true, authenticated: true };
368
-
369
- assert.deepEqual(helpers.resolveProviderCandidates([requested, fallback], 100).candidates, [requested, fallback]);
370
- assert.deepEqual(
371
- helpers.resolveProviderCandidates([{ ...requested, coolingUntil: 200 }, fallback], 100).candidates,
372
- [fallback],
373
- "a cooling requested provider must not prevent launch/general selection of a viable fallback",
374
- );
375
- assert.deepEqual(
376
- helpers.resolveProviderCandidates([
377
- { ...requested, registered: false },
378
- { ...fallback, authenticated: false },
379
- { providerId: "anthropic", modelId: "claude-sonnet", registered: true, authenticated: true, coolingUntil: 200 },
380
- ], 100).rejected.map(({ reason }) => reason),
381
- ["unregistered", "unauthenticated", "cooling"],
382
- "callers get one complete, actionable reason set when no candidate is viable",
383
- );
384
- });
385
-
386
- test("parses Retry-After seconds and HTTP dates", async () => {
387
- const now = Date.UTC(2026, 5, 16, 18, 30, 0);
388
- assert.equal(helpers.parseRetryAfterHeader("1.5", now), 1500);
389
-
390
- const retryAt = new Date(now + 15_000).toUTCString();
391
- const parsed = helpers.parseRetryAfterHeader(retryAt, now);
392
- assert.ok(parsed !== undefined && parsed >= 14_000 && parsed <= 15_000);
393
- assert.equal(helpers.parseRetryAfterHeader("", now), undefined);
394
- assert.equal(helpers.parseRetryAfterHeader("not-a-delay", now), undefined);
395
- });
396
-
397
- test("keeps newest Retry-After cooldown across overlapping attempts", async () => {
398
- await resetRotationState();
399
- await helpers.writeSubscriptionPool("anthropic", [credential("personal")]);
400
-
401
- await helpers.markSubscriptionAttempt("anthropic", "personal", 100);
402
- await helpers.markSubscriptionAttempt("anthropic", "personal", 200);
403
- await helpers.markSubscriptionRateLimited("anthropic", "personal", 60_000, 100, 150);
404
- await helpers.markSubscriptionRateLimited("anthropic", "personal", 5_000, 200, 205);
405
- await helpers.markSubscriptionRateLimited("anthropic", "personal", 10_000, 100, 300);
406
-
407
- const [entry] = helpers.readSubscriptionPool("anthropic");
408
- assert.deepEqual(
409
- { lastAttemptAt: entry.lastAttemptAt, lastRateLimitedAt: entry.lastRateLimitedAt, rateLimitedUntil: entry.rateLimitedUntil },
410
- { lastAttemptAt: 200, lastRateLimitedAt: 300, rateLimitedUntil: 10300 },
411
- );
412
-
413
- await helpers.markSubscriptionSuccess("anthropic", "personal", 250);
414
- assert.equal(helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil, 10300);
415
- await helpers.markSubscriptionSuccess("anthropic", "personal", 400);
416
- assert.equal(helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil, 0);
417
- });
418
-
419
- // Storage-hardening (design mrf6n12x): the locked read/modify/write primitive must preserve a
420
- // causally newer cooldown across a stale credential upsert, while still letting an equal-timestamp
421
- // reauth clear it.
422
- test("a stale upsert refreshes tokens but cannot erase a newer cooldown; an equal-timestamp reauth clears it", async () => {
423
- await resetRotationState();
424
- await helpers.writeSubscriptionPool("anthropic", [credential("work", { refresh: "r0", access: "a0", lastRateLimitedAt: 100 })]);
425
- // What a node reads before doing a slow token refresh.
426
- const snapshot = helpers.readSubscriptionPool("anthropic")[0];
427
-
428
- // "Other node" 429s the slot with a strictly newer basis while the refresh is in flight.
429
- await helpers.markSubscriptionRateLimited("anthropic", "work", 60_000, 500, 500);
430
- const cooled = helpers.readSubscriptionPool("anthropic")[0];
431
- assert.equal(cooled.lastRateLimitedAt, 500);
432
- assert.equal(cooled.rateLimitedUntil, 60_500);
433
-
434
- // Stale refresh upsert built from the pre-429 snapshot: fresh tokens, stale cooldown basis.
435
- await helpers.upsertSubscription("anthropic", { ...snapshot, refresh: "r1", access: "a1", expires: snapshot.expires ?? 0 });
436
- const merged = helpers.readSubscriptionPool("anthropic")[0];
437
- assert.equal(merged.refresh, "r1", "token fields take the incoming value");
438
- assert.equal(merged.access, "a1");
439
- assert.equal(merged.rateLimitedUntil, 60_500, "the newer cooldown survives the stale upsert");
440
- assert.equal(merged.lastRateLimitedAt, 500);
441
-
442
- // Reauth clears the cooldown while preserving lastRateLimitedAt (equal timestamp -> incoming wins).
443
- await helpers.upsertSubscription("anthropic", { ...merged, refresh: "r2", access: "a2", rateLimitedUntil: 0, lastRateLimitedAt: merged.lastRateLimitedAt });
444
- const reauthed = helpers.readSubscriptionPool("anthropic")[0];
445
- assert.equal(reauthed.rateLimitedUntil, 0, "an equal-timestamp reauth clears the cooldown");
446
- assert.equal(reauthed.refresh, "r2");
447
- });
448
-
449
- test("a stale success cannot reopen a slot cooled down by a newer 429", async () => {
450
- await resetRotationState();
451
- await helpers.writeSubscriptionPool("anthropic", [credential("personal")]);
452
- await helpers.markSubscriptionRateLimited("anthropic", "personal", 30_000, 500, 500);
453
- // A success whose attempt STARTED before the 429 must not clear the newer cooldown.
454
- await helpers.markSubscriptionSuccess("anthropic", "personal", 200);
455
- assert.equal(helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil, 30_500);
456
- // A success that started after the 429 legitimately clears it.
457
- await helpers.markSubscriptionSuccess("anthropic", "personal", 600);
458
- assert.equal(helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil, 0);
459
- });
460
-
461
- // A mutation must genuinely go through the per-pool-file cross-process lock: while another
462
- // process holds the pool file's lock (the SAME LockedJsonFile lock the backend uses),
463
- // a mutator throws after exhausting its retry budget rather than writing unlocked; once the
464
- // holder releases, the same call succeeds.
465
- test("pool mutations take the cross-process pool-file lock", async () => {
466
- await resetRotationState();
467
- await helpers.writeSubscriptionPool("openai-codex", [credential("work")]);
468
- const poolFile = join(home, ".pi", "agent", "openai-codex-credentials.json");
469
-
470
- const lockHolder = spawn(
471
- process.execPath,
472
- [
473
- "--import",
474
- "tsx/esm",
475
- "--input-type=module",
476
- "-e",
477
- String.raw`const { LockedJsonFile } = await import(${JSON.stringify(lockedFileUrl)});
478
- const backend = new LockedJsonFile(${JSON.stringify(poolFile)});
479
- await backend.withLockAsync(async () => {
480
- process.stdout.write("locked\n");
481
- await new Promise((resolve) => {
482
- process.stdin.setEncoding("utf8");
483
- process.stdin.once("data", resolve);
484
- });
485
- return { result: undefined };
486
- });`,
487
- ],
488
- { env: { ...process.env, HOME: home }, stdio: ["pipe", "pipe", "pipe"] },
489
- );
490
- let lockStderr = "";
491
- lockHolder.stderr!.setEncoding("utf8");
492
- lockHolder.stderr!.on("data", (chunk: string) => {
493
- lockStderr += chunk;
494
- });
495
- let sawLocked = false;
496
- const lockReady = new Promise<void>((resolve, reject) => {
497
- lockHolder.once("error", reject);
498
- lockHolder.once("close", (code) => {
499
- if (!sawLocked) reject(new Error(`lock-holder exited with ${code} before signalling readiness: ${lockStderr.trim()}`));
500
- });
501
- observeLines(lockHolder.stdout!, (line) => {
502
- if (line === "locked") {
503
- sawLocked = true;
504
- resolve();
505
- }
506
- });
507
- });
508
- // Teardown releases the lock and proves the child completed its awaited unlock before the
509
- // post-release mutation runs. A wedged child still gets bounded TERM/KILL cleanup.
510
- const childClosed = new Promise<{ code: number | null; signal: NodeJS.Signals | null }>((resolve) =>
511
- lockHolder.once("close", (code, signal) => resolve({ code, signal })),
512
- );
513
- let readyTimer: NodeJS.Timeout | undefined;
514
- try {
515
- await Promise.race([
516
- lockReady,
517
- new Promise<never>((_, reject) => {
518
- // Boot budget, not a semantics guard: the child is a fresh `node --import tsx/esm`
519
- // process that compiles the TS module graph before it can signal. Under the full
520
- // tier's --test-concurrency=4 on a 4-vCPU CI runner that boot alone can exceed 10s,
521
- // so give it real headroom -- a genuine lock bug shows up as a wrong outcome below,
522
- // never as a slow boot.
523
- readyTimer = setTimeout(() => reject(new Error("lock-holder did not signal readiness within 60s")), 60_000);
524
- }),
525
- ]);
526
- // Health is SQL-only, so the credential-file lock cannot delay it.
527
- const pool = await within(helpers.markSubscriptionAttempt("openai-codex", "work", 123), 1_000, "health write waited on the credential-file lock");
528
- assert.equal(pool[0].lastAttemptAt, 123);
529
- } finally {
530
- if (readyTimer) clearTimeout(readyTimer);
531
- try {
532
- lockHolder.stdin!.write("release\n");
533
- lockHolder.stdin!.end();
534
- } catch {
535
- /* child already gone */
536
- }
537
- // Wait for clean exit first: code 0 proves the async unlock completed. A wedged child gets
538
- // bounded SIGTERM/SIGKILL cleanup, and its test-owned lock is removed only on that fallback.
539
- const closedWithin = (ms: number) =>
540
- Promise.race([
541
- childClosed,
542
- new Promise<null>((resolve) => setTimeout(() => resolve(null), ms)),
543
- ]);
544
- let closed = await closedWithin(2000);
545
- let forced = false;
546
- if (!closed || closed.code !== 0) {
547
- forced = true;
548
- lockHolder.kill("SIGTERM");
549
- closed = await closedWithin(2000);
550
- if (!closed) {
551
- lockHolder.kill("SIGKILL");
552
- closed = await closedWithin(2000);
553
- }
554
- if (!closed) throw new Error("lock-holder child did not exit after SIGKILL");
555
- }
556
- if (forced) {
557
- rmSync(`${poolFile}.lock`, { recursive: true, force: true });
558
- throw new Error(`lock-holder exited without clean unlock (code=${closed.code}, signal=${closed.signal})`);
559
- }
560
- }
561
-
562
- const pool = await helpers.markSubscriptionAttempt("openai-codex", "work", 456);
563
- assert.equal(pool[0].lastAttemptAt, 456);
564
- });
565
-
566
- // Codex captures the login `accountId` and rejects the same account re-added under a different
567
- // label (a sticky browser session otherwise fakes redundancy on one quota); a distinct account
568
- // is stored with its own accountId.
569
- test("rejects a duplicate Codex account under a new label and stores accountId for a distinct one", async () => {
570
- await resetRotationState();
571
- const { ctx, commands, pi } = makeRotationCtx();
572
- ctx.hasUI = true;
573
- const notes: Array<{ message: string; type?: string }> = [];
574
- ctx.ui.notify = (message: string, type?: string) => notes.push({ message, type });
575
- ctx.ui.input = async () => "";
576
- let nextAccountId = "acct-1";
577
- rotationModule.__setLoginForProviderForTest(async () => ({ refresh: "r", access: "a", expires: Date.now() + 60_000, accountId: nextAccountId }));
578
-
579
- try {
580
- await providerRotationExtension(pi);
581
-
582
- await commands["provider-sub"].handler("openai-codex add codex-alt", ctx);
583
- let pool = helpers.readSubscriptionPool("openai-codex");
584
- assert.deepEqual(pool.map((e) => e.label), ["codex-alt"]);
585
- assert.equal(pool[0].accountId, "acct-1");
586
-
587
- // Same account, different label -> rejected, pool unchanged.
588
- notes.length = 0;
589
- await commands["provider-sub"].handler("openai-codex add codex-dup", ctx);
590
- assert.ok(notes.some((n) => n.type === "error" && /already added/i.test(n.message)), "duplicate account is rejected with an error");
591
- pool = helpers.readSubscriptionPool("openai-codex");
592
- assert.deepEqual(pool.map((e) => e.label), ["codex-alt"]);
593
-
594
- // Distinct account -> stored with its own accountId.
595
- nextAccountId = "acct-2";
596
- await commands["provider-sub"].handler("openai-codex add codex-other", ctx);
597
- pool = helpers.readSubscriptionPool("openai-codex");
598
- assert.deepEqual(pool.map((e) => e.label).sort(), ["codex-alt", "codex-other"]);
599
- assert.equal(pool.find((e) => e.label === "codex-other")?.accountId, "acct-2");
600
- } finally {
601
- rotationModule.__setLoginForProviderForTest(undefined);
602
- }
603
- });
604
-
605
- // The default Codex slot carries its account identity, preventing a second label from claiming
606
- // the same quota and making that identity visible in the list.
607
- test("the default Codex slot carries its account identity: blocks re-adding the same account and renders it in list", async () => {
608
- await resetRotationState();
609
- await helpers.writeSubscriptionPool("openai-codex", [
610
- credential("openai-codex", { refresh: "r-def", access: "a-def", accountId: "acct-default" }),
611
- ]);
612
-
613
- let pool = helpers.readSubscriptionPool("openai-codex");
614
- assert.deepEqual(pool.map((e) => e.label), ["openai-codex"]);
615
- assert.equal(pool[0].accountId, "acct-default");
616
-
617
- const { ctx, commands, pi } = makeRotationCtx();
618
- ctx.hasUI = true;
619
- const notes: Array<{ message: string; type?: string }> = [];
620
- ctx.ui.notify = (message: string, type?: string) => notes.push({ message, type });
621
- ctx.ui.input = async () => "";
622
- // A sticky browser re-login yields the SAME ChatGPT account already backing the pool account.
623
- rotationModule.__setLoginForProviderForTest(async () => ({ refresh: "r2", access: "a2", expires: Date.now() + 60_000, accountId: "acct-default" }));
624
- try {
625
- await providerRotationExtension(pi);
626
-
627
- await commands["provider-sub"].handler("openai-codex add codex-dup", ctx);
628
- assert.ok(notes.some((n) => n.type === "error" && /already added/i.test(n.message)), "re-adding the account under a new label is rejected");
629
- pool = helpers.readSubscriptionPool("openai-codex");
630
- assert.deepEqual(pool.map((e) => e.label), ["openai-codex"]);
631
-
632
- notes.length = 0;
633
- await commands["provider-sub"].handler("openai-codex list", ctx);
634
- assert.match(notes.map((n) => n.message).join("\n"), /account=/);
635
- } finally {
636
- rotationModule.__setLoginForProviderForTest(undefined);
637
- }
638
- });
639
-
640
- // Drive the REAL login handler path: a collision with an explicit account must reject
641
- // before commitManagedLogin writes anything, so the failed OAuth result never partially
642
- // switches the default credential or creates a default pool entry with the duplicate identity.
643
- test("login handler rejects a colliding Codex account before switching the pool account", async () => {
644
- await resetRotationState();
645
- await helpers.addSubscription("openai-codex", credential("work", { accountId: "acct-X" }));
646
- const { ctx, commands, pi } = makeRotationCtx();
647
- ctx.hasUI = true;
648
- const notes: Array<{ message: string; type?: string }> = [];
649
- ctx.ui.notify = (message: string, type?: string) => notes.push({ message, type });
650
- ctx.ui.input = async () => "";
651
- rotationModule.__setLoginForProviderForTest(async () => ({ refresh: "r-X", access: "a-X", expires: Date.now() + 60_000, accountId: "acct-X" }));
652
- try {
653
- await providerRotationExtension(pi);
654
- await commands["provider-sub"].handler("openai-codex add openai-codex", ctx);
655
- assert.ok(notes.some((note) => note.type === "error" && /already added/i.test(note.message)));
656
- assert.equal(readAuthRecord("openai-codex"), undefined, "rejected login must not write auth.json");
657
- const pool = helpers.readSubscriptionPool("openai-codex");
658
- assert.equal(pool.filter((entry) => entry.accountId === "acct-X").length, 1);
659
- assert.equal(pool.some((entry) => entry.label === "openai-codex"), false);
660
- } finally {
661
- rotationModule.__setLoginForProviderForTest(undefined);
662
- }
663
- });
664
-
665
- // The handler snapshots the pool account before OAuth. A 429 recorded while OAuth is open is
666
- // causally newer, so completing a same-account login must not reopen that quota.
667
- test("the uniqueness-aware reauth upsert refreshes its slot but rejects another slot's account", async () => {
668
- await resetRotationState();
669
- await helpers.writeSubscriptionPool("openai-codex", [credential("work", { accountId: "acct-old", rateLimitedUntil: 900, lastAttemptAt: 100, lastRateLimitedAt: 50 }), credential("other", { accountId: "acct-held" })]);
670
- await helpers.upsertSubscriptionWithUniqueAccount("openai-codex", credential("work", { accountId: "acct-new", refresh: "r-new", access: "a-new" }));
671
- let pool = helpers.readSubscriptionPool("openai-codex");
672
- const refreshed = pool.find((entry) => entry.label === "work");
673
- assert.equal(refreshed?.accountId, "acct-new");
674
- assert.equal(refreshed?.rateLimitedUntil, 900);
675
- assert.equal(refreshed?.lastAttemptAt, 100);
676
- assert.equal(refreshed?.lastRateLimitedAt, 50);
677
- await assert.rejects(
678
- () => helpers.upsertSubscriptionWithUniqueAccount("openai-codex", credential("work", { accountId: "acct-held", refresh: "r-collision", access: "a-collision" })),
679
- (err: unknown) => err instanceof helpers.DuplicateSubscriptionError && /already added/i.test((err as Error).message),
680
- );
681
- pool = helpers.readSubscriptionPool("openai-codex");
682
- assert.equal(pool.find((entry) => entry.label === "work")?.accountId, "acct-new");
683
- assert.equal(pool.filter((entry) => entry.accountId === "acct-held").length, 1);
684
- });
685
-
686
- // A rejected default identity move must leave the pool untouched — account uniqueness is decided
687
- // inside the pool lock, before anything is written — and must not touch auth.json at all, which
688
- // no managed path writes any more.
689
- test("addSubscription normalizes labels and enforces Codex account uniqueness inside the locked transform", async () => {
690
- await resetRotationState();
691
- await helpers.addSubscription("openai-codex", credential("work", { accountId: "acct-1" }));
692
- await assert.rejects(
693
- () => helpers.addSubscription("openai-codex", credential(" work ", { accountId: "acct-2" })),
694
- (err: unknown) => err instanceof helpers.DuplicateSubscriptionError && /already exists/i.test((err as Error).message),
695
- );
696
- await assert.rejects(
697
- () => helpers.addSubscription("openai-codex", credential(" ", { accountId: "acct-3" })),
698
- (err: unknown) => !(err instanceof helpers.DuplicateSubscriptionError) && /cannot be empty/i.test((err as Error).message),
699
- );
700
- await assert.rejects(
701
- () => helpers.addSubscription("openai-codex", credential("work-2", { accountId: "acct-1" })),
702
- (err: unknown) => err instanceof helpers.DuplicateSubscriptionError && /already added/i.test((err as Error).message),
703
- );
704
- await helpers.addSubscription("openai-codex", credential("work-3", { accountId: "acct-3" }));
705
- assert.deepEqual(helpers.readSubscriptionPool("openai-codex").map((entry) => entry.label).sort(), ["work", "work-3"]);
706
- });
707
-
708
- // The first contender holds the real pool lock inside addSubscription's transform. The second
709
- // contender cannot reach its own transform until it has first attempted that same real lock.
710
- // This makes an outside-lock uniqueness check deterministic: both checks run while the pool is
711
- // empty, then both appends succeed after the holder releases.
712
- // Approved slow-test exception: two fresh TypeScript child processes can take over 10s to boot
713
- // on a hosted runner; the test's bounded coordination budgets distinguish a genuine regression.
714
- test("cross-process explicit adds admit exactly one duplicate Codex account", { timeout: 60_000 }, async () => {
715
- await resetRotationState();
716
- const poolPath = join(home, ".pi", "agent", "openai-codex-subscriptions.json");
717
- const subscriptionStateUrl = new URL("../../../lib/subscription-state.ts", import.meta.url).href;
718
- const spawnContender = (label: string) => {
719
- const child = spawn(
720
- process.execPath,
721
- [
722
- "--import",
723
- "tsx/esm",
724
- "--input-type=module",
725
- "-e",
726
- String.raw`const { addSubscription, DuplicateSubscriptionError } = await import(${JSON.stringify(subscriptionStateUrl)});
727
- try {
728
- await addSubscription("openai-codex", {
729
- label: ${JSON.stringify(label)}, refresh: "refresh-${label}", access: "access-${label}", expires: Date.now() + 60_000,
730
- accountId: "acct-cross-process-race", rateLimitedUntil: 0, lastAttemptAt: 0, lastRateLimitedAt: 0,
731
- });
732
- process.stdout.write("outcome:success\n");
733
- } catch (error) {
734
- const outcome = error instanceof DuplicateSubscriptionError ? "duplicate" : "error";
735
- process.stdout.write("outcome:" + outcome + "\n");
736
- if (outcome === "error") process.stderr.write(String(error));
737
- }`,
738
- ],
739
- { env: { ...process.env, HOME: home }, stdio: ["ignore", "pipe", "pipe"] },
740
- );
741
- return new Promise<{ outcome: string; stderr: string }>((resolve, reject) => {
742
- let stdout = "";
743
- let stderr = "";
744
- child.stdout!.on("data", (chunk: string) => { stdout += chunk; });
745
- child.stderr!.on("data", (chunk: string) => { stderr += chunk; });
746
- child.once("error", reject);
747
- child.once("close", (code) => {
748
- const outcome = /outcome:([^\n]+)/.exec(stdout)?.[1];
749
- if (code !== 0 || outcome === undefined) reject(new Error(`${label} exited with ${code}: ${stderr.trim()}`));
750
- else resolve({ outcome, stderr });
751
- });
752
- });
753
- };
754
-
755
- const results = await Promise.all([spawnContender("race-left"), spawnContender("race-right")]);
756
- assert.deepEqual(results.map((result) => result.outcome).sort(), ["duplicate", "success"], results.map((result) => result.stderr).join("\n"));
757
- const pool = helpers.readSubscriptionPool("openai-codex");
758
- assert.equal(pool.length, 1);
759
- assert.ok(["race-left", "race-right"].includes(pool[0].label));
760
- assert.equal(existsSync(`${poolPath}.lock`), false, "cross-process add race must release the pool lock directory");
761
- });
762
-
763
- test("renames explicit subscriptions without permitting pool or case-insensitive collisions", async () => {
764
- await resetRotationState();
765
- await helpers.writeSubscriptionPool("openai-codex", [credential("openai-codex"), credential("work"), credential("personal")]);
766
- assert.deepEqual((await helpers.renameSubscription("openai-codex", "work", "Lab")).map((entry) => entry.label), ["openai-codex", "Lab", "personal"]);
767
- await assert.rejects(() => helpers.renameSubscription("openai-codex", "Lab", " PERSONAL "), helpers.DuplicateSubscriptionError);
768
- await helpers.renameSubscription("openai-codex", "openai-codex", "anything");
769
- });
770
-
771
- test("treats numeric subscription refs as 1-based indexes", async () => {
772
- await resetRotationState();
773
- const pool = [credential("alpha"), credential("beta")];
774
-
775
- await helpers.writeSubscriptionPool("openai-codex", pool);
776
- assert.deepEqual(
777
- (await helpers.removeSubscription("openai-codex", "1")).map((entry) => entry.label),
778
- ["beta"],
779
- );
780
-
781
- await helpers.writeSubscriptionPool("openai-codex", pool);
782
- assert.deepEqual(
783
- (await helpers.promoteSubscription("openai-codex", "2")).map((entry) => entry.label),
784
- ["beta", "alpha"],
785
- );
786
- });
787
-
788
- function makeRotationCtx(modelIntent?: string) {
789
- const concreteRequest = arguments.length > 0 && modelIntent === undefined;
790
- if (modelIntent === undefined) delete process.env.CRTR_MODEL_INTENT;
791
- else process.env.CRTR_MODEL_INTENT = modelIntent;
792
- const ctx = {
793
- model: { provider: "openai-codex", id: "gpt-5.6-sol", api: "openai-codex-responses" },
794
- thinkingLevel: "high",
795
- cwd: nodeCwd,
796
- ui: { setStatus() {} },
797
- modelRegistry: {
798
- find(provider: string, id: string) {
799
- if (provider === "anthropic" && id === "claude-opus-4-8") return { provider, id, api: "anthropic-messages" };
800
- if (provider === "openai-codex" && id === "gpt-5.6-sol") return { provider, id, api: "openai-codex-responses" };
801
- return undefined;
802
- },
803
- // These rotation scenarios model an account with no hand-written non-pool
804
- // credential — pool state alone drives authentication here.
805
- hasConfiguredAuth() {
806
- return false;
807
- },
808
- },
809
- } as any;
810
- const setModelCalls: any[] = [];
811
- const thinkingLevelCalls: string[] = [];
812
- const providers: Record<string, { streamSimple: (...args: any[]) => AsyncIterable<any> }> = {};
813
- const events: Record<string, (...args: any[]) => unknown> = {};
814
- const commands: Record<string, { handler: (args: string, ctx: any) => unknown }> = {};
815
- const pi = {
816
- registerProvider(name: string, provider: { streamSimple: (...args: any[]) => AsyncIterable<any> }) {
817
- providers[name] = {
818
- ...provider,
819
- streamSimple(...args: any[]) {
820
- if (!concreteRequest) {
821
- const model = args[0] as { provider: string };
822
- process.env.CRTR_MODEL_INTENT = model.provider === "anthropic" ? "anthropic/strong" : "openai/strong";
823
- }
824
- return provider.streamSimple(...args);
825
- },
826
- };
827
- },
828
- registerCommand(name: string, command: { handler: (args: string, ctx: any) => unknown }) {
829
- commands[name] = command;
830
- },
831
- on(eventName: string, handler: (...args: any[]) => unknown) {
832
- events[eventName] = handler;
833
- },
834
- async setModel(model: any) {
835
- setModelCalls.push(model);
836
- ctx.model = model;
837
- return true;
838
- },
839
- setThinkingLevel(level: string) {
840
- thinkingLevelCalls.push(level);
841
- ctx.thinkingLevel = level;
842
- },
843
- } as any;
844
- return { ctx, setModelCalls, thinkingLevelCalls, providers, events, commands, pi };
845
- }
846
-
847
- test("managed Anthropic requests opt into server-side fallback without changing other managed routes", async () => {
848
- await resetRotationState();
849
- writeLadderConfig();
850
- await helpers.writeSubscriptionPool("anthropic", [credential("anthropic")]);
851
-
852
- const anthropicModel = { provider: "anthropic", id: "claude-opus-5", api: "anthropic-messages" };
853
- const { ctx, providers, events, pi } = makeRotationCtx("anthropic/ultra");
854
- ctx.model = anthropicModel;
855
- const streamOptions: any[] = [];
856
- const fetchCalls: Array<[RequestInfo | URL, RequestInit | undefined]> = [];
857
- const callerPayloads: unknown[] = [];
858
- rotationModule.__setStreamForProviderForTest((_model, _context, options) => {
859
- streamOptions.push(options);
860
- return (async function* () {
861
- yield { type: "text_delta", text: "served" };
862
- })() as any;
863
- });
864
-
865
- try {
866
- await providerRotationExtension(pi);
867
- await events.session_start?.({}, ctx);
868
- const stream = providers["anthropic"].streamSimple(anthropicModel, ctx, {
869
- onPayload(payload: unknown) {
870
- callerPayloads.push(payload);
871
- if ((payload as { replaceWithNull?: boolean })?.replaceWithNull) return null;
872
- return { ...(payload as object), callerPayload: true };
873
- },
874
- fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
875
- fetchCalls.push([input, init]);
876
- return new Response();
877
- },
878
- });
879
- for await (const _event of stream) {
880
- // Drain the managed request so the injected stream options are captured.
881
- }
882
-
883
- const [anthropicOptions] = streamOptions;
884
- assert.deepEqual(await anthropicOptions.onPayload({ request: true }, anthropicModel), {
885
- request: true,
886
- callerPayload: true,
887
- fallbacks: "default",
888
- });
889
- assert.deepEqual(await anthropicOptions.onPayload({ replaceWithNull: true }, anthropicModel), { fallbacks: "default" });
890
- assert.deepEqual(callerPayloads, [{ request: true }, { replaceWithNull: true }]);
891
-
892
- for (const headers of [
893
- new Headers({ "anthropic-beta": "claude-code-20250219,oauth-2025-04-20", "x-keep": "headers" }),
894
- { "anthropic-beta": "claude-code-20250219,oauth-2025-04-20", "x-keep": "object" },
895
- [["anthropic-beta", "claude-code-20250219,oauth-2025-04-20"], ["x-keep", "pairs"]],
896
- ]) {
897
- await anthropicOptions.fetch("https://example.test/messages", { headers });
898
- }
899
- for (const [_input, init] of fetchCalls) {
900
- const headers = new Headers(init?.headers);
901
- assert.equal(headers.get("anthropic-beta"), "claude-code-20250219,oauth-2025-04-20,server-side-fallback-2026-07-01");
902
- assert.ok(headers.get("x-keep"));
903
- }
904
-
905
- const haikuModel = { provider: "anthropic", id: "claude-haiku-4-5", api: "anthropic-messages" };
906
- const haikuOnPayload = (payload: unknown) => payload;
907
- const haikuFetch = async () => new Response();
908
- for await (const _event of providers["anthropic"].streamSimple(haikuModel, ctx, {
909
- onPayload: haikuOnPayload,
910
- fetch: haikuFetch,
911
- })) {
912
- // Drain the managed Haiku request.
913
- }
914
- assert.equal(streamOptions[1].onPayload, haikuOnPayload);
915
- assert.equal(streamOptions[1].fetch, haikuFetch);
916
- assert.deepEqual(await streamOptions[1].onPayload({ request: true }, haikuModel), { request: true });
917
-
918
- await resetRotationState();
919
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
920
- const codexModel = { provider: "openai-codex", id: "gpt-5.6-sol", api: "openai-codex-responses" };
921
- const codex = makeRotationCtx("openai/strong");
922
- const codexOptions: any[] = [];
923
- rotationModule.__setStreamForProviderForTest((_model, _context, options) => {
924
- codexOptions.push(options);
925
- return (async function* () {
926
- yield { type: "text_delta", text: "served" };
927
- })() as any;
928
- });
929
- await providerRotationExtension(codex.pi);
930
- await codex.events.session_start?.({}, codex.ctx);
931
- for await (const _event of codex.providers["openai-codex"].streamSimple(codexModel, codex.ctx)) {
932
- // Drain the managed Codex route.
933
- }
934
- assert.equal(codexOptions[0].onPayload, undefined);
935
- assert.equal(codexOptions[0].fetch, undefined);
936
- } finally {
937
- rotationModule.__setStreamForProviderForTest(undefined);
938
- }
939
- });
940
-
941
- // A live route carries the session's exact thinking level through every provider
942
- // request, including the requests that continue a turn after tool results.
943
- test("preserves a manually selected thinking level across provider requests", async () => {
944
- await resetRotationState();
945
- writeLadderConfig();
946
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
947
-
948
- const { ctx, thinkingLevelCalls, providers, events, pi } = makeRotationCtx("openai/strong");
949
- ctx.thinkingLevel = "max";
950
- rotationModule.__setStreamForProviderForTest(() => (async function* () {
951
- yield { type: "text_delta", text: "continued after tool result" };
952
- })() as any);
953
-
954
- try {
955
- await providerRotationExtension(pi);
956
- await events.session_start?.({}, ctx);
957
- const stream = providers["openai-codex"].streamSimple(ctx.model, ctx);
958
- for await (const _event of stream) {
959
- // Drain the managed-provider request to completion.
960
- }
961
-
962
- assert.deepEqual(thinkingLevelCalls, ["max"]);
963
- assert.equal(ctx.thinkingLevel, "max");
964
- } finally {
965
- rotationModule.__setStreamForProviderForTest(undefined);
966
- }
967
- });
968
-
969
- // Codex exhaustion switches the model and serves the same turn on the Anthropic fallback.
970
- test("falls back transparently and serves the turn on Anthropic when Codex is exhausted", async () => {
971
- await resetRotationState();
972
- writeLadderConfig();
973
- await helpers.writeSubscriptionPool("anthropic", [credential("personal")]);
974
-
975
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
976
- const streamedModels: any[] = [];
977
- rotationModule.__setStreamForProviderForTest((model) => {
978
- streamedModels.push(model);
979
- return (async function* () {
980
- yield { type: "text_delta", text: "hello from anthropic" };
981
- })() as any;
982
- });
983
-
984
- try {
985
- await providerRotationExtension(pi);
986
- await events.session_start?.({}, ctx);
987
- const stream = providers["openai-codex"].streamSimple(
988
- { provider: "openai-codex", id: "gpt-5.6-sol", api: "openai-codex-responses" },
989
- ctx,
990
- );
991
- const emitted = [] as any[];
992
- for await (const event of stream) emitted.push(event);
993
-
994
- // Session model was switched to the Anthropic fallback...
995
- assert.deepEqual(setModelCalls.map((m) => m.provider), ["anthropic"]);
996
- // ...and the turn was actually STREAMED on the Anthropic model (not the codex one).
997
- assert.equal(streamedModels.length, 1);
998
- assert.equal(streamedModels[0].provider, "anthropic");
999
- // The turn was served with content, never failed with an error.
1000
- assert.ok(emitted.some((e) => e.type === "text_delta"));
1001
- assert.ok(!emitted.some((e) => e.type === "error"));
1002
- } finally {
1003
- rotationModule.__setStreamForProviderForTest(undefined);
1004
- }
1005
- });
1006
-
1007
- // A concrete request only expands to routes carrying its exact model id. A
1008
- // cooling Codex Sol route must not substitute Anthropic's different model.
1009
- test("a cooling concrete model never substitutes a different-provider model", async () => {
1010
- await resetRotationState();
1011
- writeLadderConfig();
1012
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1013
- await helpers.markSubscriptionRateLimited("openai-codex", "codex", 30_000);
1014
- const expectedDeadline = helpers.readSubscriptionPool("openai-codex")[0].rateLimitedUntil;
1015
- await helpers.writeSubscriptionPool("anthropic", [credential("personal")]);
1016
-
1017
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx(undefined);
1018
- let streamCalls = 0;
1019
- rotationModule.__setStreamForProviderForTest((_model) => {
1020
- streamCalls += 1;
1021
- return (async function* () {
1022
- yield { type: "text_delta", text: "must not call a cooling or fallback provider" };
1023
- })() as any;
1024
- });
1025
-
1026
- try {
1027
- await providerRotationExtension(pi);
1028
- await events.session_start?.({}, ctx);
1029
- const stream = providers["openai-codex"].streamSimple(
1030
- { provider: "openai-codex", id: "gpt-5.6-sol", api: "openai-codex-responses" },
1031
- ctx,
1032
- );
1033
- const emitted = [] as any[];
1034
- for await (const event of stream) emitted.push(event);
1035
-
1036
- assert.deepEqual(setModelCalls, [], "the concrete Sol request never switches to Anthropic");
1037
- assert.equal(streamCalls, 0);
1038
- assert.equal(emitted.length, 1);
1039
- assert.match(emitted[0].error.errorMessage, /All provider routes are rate limited/i);
1040
- assert.equal(emitted[0].error.diagnostics?.[0]?.details?.rateLimitedUntil, expectedDeadline);
1041
- } finally {
1042
- rotationModule.__setStreamForProviderForTest(undefined);
1043
- }
1044
- });
1045
-
1046
- test("an exact override never substitutes another provider sharing its model id", async () => {
1047
- await resetRotationState();
1048
- writeSameModelRouteConfig();
1049
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1050
- await helpers.markSubscriptionRateLimited("openai-codex", "codex", 30_000);
1051
- await helpers.writeSubscriptionPool("anthropic", [credential("anthropic")]);
1052
-
1053
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx(undefined);
1054
- const previousModelExact = process.env.CRTR_MODEL_EXACT;
1055
- process.env.CRTR_MODEL_EXACT = "1";
1056
- ctx.modelRegistry.find = (provider: string, id: string) => {
1057
- if (id !== "gpt-5.6-sol") return undefined;
1058
- return { provider, id, api: provider === "anthropic" ? "anthropic-messages" : "openai-codex-responses" };
1059
- };
1060
- let streamCalls = 0;
1061
- rotationModule.__setStreamForProviderForTest(() => {
1062
- streamCalls += 1;
1063
- return (async function* () {
1064
- yield { type: "text_delta", text: "must not reach an alternate provider" };
1065
- })() as any;
1066
- });
1067
-
1068
- try {
1069
- await providerRotationExtension(pi);
1070
- await events.session_start?.({}, ctx);
1071
- const emitted = [] as any[];
1072
- for await (const event of providers["openai-codex"].streamSimple(ctx.model, ctx)) emitted.push(event);
1073
-
1074
- assert.deepEqual(setModelCalls, [], "the exact provider/model never switches to the same-id alternate");
1075
- assert.equal(streamCalls, 0, "the alternate provider is never called");
1076
- assert.match(emitted[0].error.errorMessage, /All provider routes are rate limited/i);
1077
- } finally {
1078
- rotationModule.__setStreamForProviderForTest(undefined);
1079
- if (previousModelExact === undefined) delete process.env.CRTR_MODEL_EXACT;
1080
- else process.env.CRTR_MODEL_EXACT = previousModelExact;
1081
- }
1082
- });
1083
-
1084
- // Request timeouts and other transient network/5xx blips retry the same provider in place
1085
- // without cooling the credential down or rotating away.
1086
- test("treats a request timeout as transient: retries the SAME provider in place, no cooldown or rotation", async () => {
1087
- await resetRotationState();
1088
- writeLadderConfig();
1089
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
1090
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1091
-
1092
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1093
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
1094
- const streamedModels: any[] = [];
1095
- const sleepCalls: number[] = [];
1096
- let anthropicCalls = 0;
1097
- rotationModule.__setSleepForTest(async (ms) => {
1098
- sleepCalls.push(ms);
1099
- });
1100
- rotationModule.__setStreamForProviderForTest((model) => {
1101
- streamedModels.push(model);
1102
- return (async function* () {
1103
- if (model.provider === "anthropic") {
1104
- anthropicCalls += 1;
1105
- if (anthropicCalls === 1) {
1106
- yield { type: "error", error: Object.assign(new Error("Request timed out."), { errorMessage: "Request timed out." }) };
1107
- return;
1108
- }
1109
- yield { type: "text_delta", text: "hello from anthropic, second try" };
1110
- return;
1111
- }
1112
- yield { type: "text_delta", text: "hello from codex" };
1113
- })() as any;
1114
- });
1115
-
1116
- try {
1117
- await providerRotationExtension(pi);
1118
- await events.session_start?.({}, ctx);
1119
- const stream = providers["anthropic"].streamSimple(
1120
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1121
- ctx,
1122
- );
1123
- const emitted = [] as any[];
1124
- for await (const event of stream) emitted.push(event);
1125
-
1126
- // Never switched providers or models -- both attempts stayed on anthropic.
1127
- assert.deepEqual(setModelCalls, []);
1128
- assert.deepEqual(streamedModels.map((m) => m.provider), ["anthropic", "anthropic"]);
1129
- assert.deepEqual(sleepCalls, [250]);
1130
- assert.ok(emitted.some((e) => e.type === "text_delta"));
1131
- assert.ok(!emitted.some((e) => e.type === "error"));
1132
- // No cooldown -- a transient blip must never cool down a healthy subscription.
1133
- assert.equal(helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil, 0);
1134
- } finally {
1135
- rotationModule.__setStreamForProviderForTest(undefined);
1136
- rotationModule.__setSleepForTest(undefined);
1137
- }
1138
- });
1139
-
1140
- // Once the in-place transient retry budget is exhausted, the credential is rotated away
1141
- // for this turn without persisting a fake rate-limit cooldown.
1142
- test("surfaces the transient error and rotates to the fallback once the in-place retry budget is exhausted", async () => {
1143
- await resetRotationState();
1144
- writeLadderConfig();
1145
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
1146
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1147
-
1148
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1149
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
1150
- const notifications: Array<{ message: string; level: string }> = [];
1151
- ctx.ui.notify = (message: string, level: string) => {
1152
- notifications.push({ message, level });
1153
- };
1154
- const streamedModels: any[] = [];
1155
- const sleepCalls: number[] = [];
1156
- rotationModule.__setSleepForTest(async (ms) => {
1157
- sleepCalls.push(ms);
1158
- });
1159
- rotationModule.__setStreamForProviderForTest((model) => {
1160
- streamedModels.push(model);
1161
- return (async function* () {
1162
- if (model.provider === "anthropic") {
1163
- // 503 with no retry-after header is transient by STATUS, not just by message.
1164
- yield { type: "error", error: Object.assign(new Error("Service Unavailable"), { status: 503 }) };
1165
- return;
1166
- }
1167
- yield { type: "text_delta", text: "hello from codex fallback" };
1168
- })() as any;
1169
- });
1170
-
1171
- try {
1172
- await providerRotationExtension(pi);
1173
- await events.session_start?.({}, ctx);
1174
- const stream = providers["anthropic"].streamSimple(
1175
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1176
- ctx,
1177
- );
1178
- const emitted = [] as any[];
1179
- for await (const event of stream) emitted.push(event);
1180
-
1181
- // Retried the same anthropic credential in place (3 attempts) then rotated to codex.
1182
- assert.equal(streamedModels.filter((m) => m.provider === "anthropic").length, 3);
1183
- assert.deepEqual(sleepCalls, [250, 750]);
1184
- assert.deepEqual(setModelCalls.map((m) => m.provider), ["openai-codex"]);
1185
- // The turn was served on the fallback, never failed with a terminal error.
1186
- assert.ok(emitted.some((e) => e.type === "text_delta"));
1187
- assert.ok(!emitted.some((e) => e.type === "error"));
1188
- // A network failure is not a rate limit and must not poison future turns.
1189
- assert.equal(helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil, 0);
1190
- } finally {
1191
- rotationModule.__setStreamForProviderForTest(undefined);
1192
- rotationModule.__setSleepForTest(undefined);
1193
- }
1194
- });
1195
-
1196
- test("surfaces the underlying transient error without reporting a rate limit when every route fails", async () => {
1197
- await resetRotationState();
1198
- writeLadderConfig();
1199
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1200
-
1201
- const { ctx, providers, events, pi } = makeRotationCtx(undefined);
1202
- const sleepCalls: number[] = [];
1203
- let streamCalls = 0;
1204
- rotationModule.__setSleepForTest(async (ms) => {
1205
- sleepCalls.push(ms);
1206
- });
1207
- rotationModule.__setStreamForProviderForTest(() => {
1208
- streamCalls += 1;
1209
- return (async function* () {
1210
- yield {
1211
- type: "error",
1212
- error: Object.assign(new Error("WebSocket connection closed unexpectedly"), {
1213
- errorMessage: "WebSocket connection closed unexpectedly",
1214
- }),
1215
- };
1216
- })() as any;
1217
- });
1218
-
1219
- try {
1220
- await providerRotationExtension(pi);
1221
- await events.session_start?.({}, ctx);
1222
- const stream = providers["openai-codex"].streamSimple(
1223
- { provider: "openai-codex", id: "gpt-5.6-sol", api: "openai-codex-responses" },
1224
- ctx,
1225
- );
1226
- const emitted = [] as any[];
1227
- for await (const event of stream) emitted.push(event);
1228
-
1229
- assert.equal(streamCalls, 3);
1230
- assert.deepEqual(sleepCalls, [250, 750]);
1231
- assert.equal(emitted.length, 1);
1232
- assert.match(emitted[0].error.errorMessage, /WebSocket connection closed unexpectedly/);
1233
- assert.doesNotMatch(emitted[0].error.errorMessage, /rate limited/i);
1234
- assert.equal(helpers.readSubscriptionPool("openai-codex")[0].rateLimitedUntil, 0);
1235
- } finally {
1236
- rotationModule.__setStreamForProviderForTest(undefined);
1237
- rotationModule.__setSleepForTest(undefined);
1238
- }
1239
- });
1240
-
1241
- test("carries an observed 503 into the terminal provider error after bounded recovery", async () => {
1242
- await resetRotationState();
1243
- writeLadderConfig();
1244
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1245
-
1246
- const { ctx, providers, events, pi } = makeRotationCtx(undefined);
1247
- rotationModule.__setSleepForTest(async () => {});
1248
- rotationModule.__setStreamForProviderForTest((model, _context, options) => (async function* () {
1249
- await options?.onResponse?.({ status: 503, headers: {} }, model);
1250
- yield { type: "error", error: new Error("Request failed", { cause: Object.assign(new Error("socket reset"), { code: "ECONNRESET" }) }) };
1251
- })() as any);
1252
-
1253
- try {
1254
- await providerRotationExtension(pi);
1255
- await events.session_start?.({}, ctx);
1256
- const emitted = [] as any[];
1257
- for await (const event of providers["openai-codex"].streamSimple(ctx.model, ctx)) emitted.push(event);
1258
-
1259
- const terminal = emitted.find((event) => event.type === "error");
1260
- assert.ok(terminal, "the bounded attempts settle a terminal provider error");
1261
- assert.equal(terminal.error.provider, "openai-codex");
1262
- assert.equal(terminal.error.model, "gpt-5.6-sol");
1263
- const details = terminal.error.diagnostics?.find((diagnostic: any) => diagnostic.type === "crtr:provider-failure")?.details;
1264
- assert.equal(details?.status, 503, "the response status survives the stream error boundary");
1265
- assert.equal(details?.causes?.[0]?.code, "ECONNRESET", "the cause chain survives the stream error boundary");
1266
- } finally {
1267
- rotationModule.__setStreamForProviderForTest(undefined);
1268
- rotationModule.__setSleepForTest(undefined);
1269
- }
1270
- });
1271
-
1272
- test("does not replay a partially exposed response, while retaining its 503 facts", async () => {
1273
- await resetRotationState();
1274
- writeLadderConfig();
1275
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
1276
-
1277
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1278
- let attempts = 0;
1279
- rotationModule.__setStreamForProviderForTest((model, _context, options) => (async function* () {
1280
- attempts += 1;
1281
- yield { type: "text_delta", text: "already exposed" };
1282
- await options?.onResponse?.({ status: 503, headers: {} }, model);
1283
- yield { type: "error", error: new Error("Request failed") };
1284
- })() as any);
1285
-
1286
- try {
1287
- await providerRotationExtension(pi);
1288
- await events.session_start?.({}, ctx);
1289
- const emitted = [] as any[];
1290
- for await (const event of providers["anthropic"].streamSimple(ctx.model, ctx)) emitted.push(event);
1291
-
1292
- assert.equal(attempts, 1, "content commits the attempt before its failure");
1293
- assert.ok(setModelCalls.every((selected) => selected.provider === "anthropic"), "a partial response never rotates to another route");
1294
- const terminal = emitted.find((event) => event.type === "error");
1295
- assert.equal(terminal.error.diagnostics?.find((diagnostic: any) => diagnostic.type === "crtr:provider-failure")?.details?.status, 503);
1296
- } finally {
1297
- rotationModule.__setStreamForProviderForTest(undefined);
1298
- }
1299
- });
1300
-
1301
- test("keeps a known authentication rejection fatal", async () => {
1302
- await resetRotationState();
1303
- writeLadderConfig();
1304
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1305
-
1306
- const { ctx, providers, events, pi } = makeRotationCtx(undefined);
1307
- let attempts = 0;
1308
- rotationModule.__setStreamForProviderForTest((model, _context, options) => (async function* () {
1309
- attempts += 1;
1310
- await options?.onResponse?.({ status: 401, headers: {} }, model);
1311
- yield { type: "error", error: new Error("Request failed") };
1312
- })() as any);
1313
-
1314
- try {
1315
- await providerRotationExtension(pi);
1316
- await events.session_start?.({}, ctx);
1317
- const emitted = [] as any[];
1318
- for await (const event of providers["openai-codex"].streamSimple(ctx.model, ctx)) emitted.push(event);
1319
-
1320
- assert.equal(attempts, 1, "a protocol/auth failure never enters transient retries");
1321
- const terminal = emitted.find((event) => event.type === "error");
1322
- assert.equal(terminal.error.diagnostics?.find((diagnostic: any) => diagnostic.type === "crtr:provider-failure")?.details?.status, 401);
1323
- } finally {
1324
- rotationModule.__setStreamForProviderForTest(undefined);
1325
- }
1326
- });
1327
-
1328
- // Regression: a transport-poisoned process (a corrupted TLS upload leaves undici's H2
1329
- // session destroyed, so EVERY request fails with no HTTP response while fresh processes
1330
- // succeed) must self-terminate after consecutive transport-only exhausted passes so the
1331
- // daemon respawns the broker fresh — and any pass that saw a real HTTP status must reset
1332
- // the counter so ordinary provider blips never kill a healthy process.
1333
- test("self-terminates after consecutive transport-only exhausted passes; an HTTP status resets the counter", async () => {
1334
- await resetRotationState();
1335
- writeLadderConfig();
1336
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1337
-
1338
- const { ctx, providers, events, pi } = makeRotationCtx(undefined);
1339
- rotationModule.__setSleepForTest(async () => {});
1340
- let mode: "success" | "transport" | "http503" = "success";
1341
- rotationModule.__setStreamForProviderForTest(() => {
1342
- const current = mode;
1343
- return (async function* () {
1344
- if (current === "success") {
1345
- yield { type: "text_delta", text: "ok" };
1346
- return;
1347
- }
1348
- if (current === "http503") {
1349
- // Transient by status — but an HTTP response nonetheless: proof the
1350
- // process can still reach the provider.
1351
- yield { type: "error", error: Object.assign(new Error("Service Unavailable"), { status: 503 }) };
1352
- return;
1353
- }
1354
- // No status at all — the poisoned-transport signature.
1355
- yield {
1356
- type: "error",
1357
- error: Object.assign(new Error("fetch failed: The session has been destroyed"), {
1358
- errorMessage: "fetch failed: The session has been destroyed",
1359
- }),
1360
- };
1361
- })() as any;
1362
- });
1363
- const terminateCalls: string[] = [];
1364
- let resolveTerminated: (tried: string) => void;
1365
- const terminated = new Promise<string>((resolve) => { resolveTerminated = resolve; });
1366
- rotationModule.__setTerminatePoisonedProcessForTest(async (tried) => {
1367
- terminateCalls.push(tried);
1368
- resolveTerminated(tried);
1369
- return true;
1370
- });
1371
-
1372
- const runPass = async () => {
1373
- const stream = providers["openai-codex"].streamSimple(
1374
- { provider: "openai-codex", id: "gpt-5.6-sol", api: "openai-codex-responses" },
1375
- ctx,
1376
- );
1377
- const emitted: any[] = [];
1378
- for await (const event of stream) emitted.push(event);
1379
- return emitted;
1380
- };
1381
-
1382
- try {
1383
- await providerRotationExtension(pi);
1384
- await events.session_start?.({}, ctx);
1385
-
1386
- // Baseline success pass: the cross-turn counter starts at zero.
1387
- assert.ok((await runPass()).some((e) => e.type === "text_delta"));
1388
-
1389
- // First transport-only exhaustion: ordinary terminal error, no termination.
1390
- mode = "transport";
1391
- assert.ok((await runPass()).some((e) => e.type === "error"));
1392
- assert.equal(terminateCalls.length, 0);
1393
-
1394
- // An exhausted pass WITH an HTTP status resets the counter.
1395
- mode = "http503";
1396
- assert.ok((await runPass()).some((e) => e.type === "error"));
1397
-
1398
- // One transport-only pass after the reset: still below the limit.
1399
- mode = "transport";
1400
- assert.ok((await runPass()).some((e) => e.type === "error"));
1401
- assert.equal(terminateCalls.length, 0);
1402
-
1403
- // Second consecutive transport-only pass: terminate fires and the stream is
1404
- // deliberately left open (no terminal error), so the SIGTERM abort path owns
1405
- // how this turn settles. Don't await the never-ending stream.
1406
- const stream = providers["openai-codex"].streamSimple(
1407
- { provider: "openai-codex", id: "gpt-5.6-sol", api: "openai-codex-responses" },
1408
- ctx,
1409
- );
1410
- const emitted: any[] = [];
1411
- void (async () => { for await (const event of stream) emitted.push(event); })();
1412
- await within(terminated, 5_000, "poisoned-process termination never fired");
1413
- assert.equal(terminateCalls.length, 1);
1414
- assert.equal(emitted.length, 0);
1415
- } finally {
1416
- rotationModule.__setTerminatePoisonedProcessForTest(undefined);
1417
- rotationModule.__setStreamForProviderForTest(undefined);
1418
- rotationModule.__setSleepForTest(undefined);
1419
- }
1420
- });
1421
-
1422
- // Regression: a genuine rate limit (HTTP 429) must never get the transient in-place
1423
- // retry treatment -- it cools the credential down and rotates on the FIRST failure. The
1424
- // cooldown duration must reflect a real Retry-After header (5s here) rather than always
1425
- // falling back to the 5-minute default, so a misclassification is obvious from the numbers.
1426
- test("treats HTTP 429 as a genuine rate limit: rotates immediately using the Retry-After header, no in-place retry", async () => {
1427
- await resetRotationState();
1428
- writeLadderConfig();
1429
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
1430
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1431
-
1432
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1433
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
1434
- const streamedModels: any[] = [];
1435
- const sleepCalls: number[] = [];
1436
- rotationModule.__setSleepForTest(async (ms) => {
1437
- sleepCalls.push(ms);
1438
- });
1439
- rotationModule.__setStreamForProviderForTest((model, _context, options) => {
1440
- streamedModels.push(model);
1441
- return (async function* () {
1442
- if (model.provider === "anthropic") {
1443
- await options?.onResponse?.({ status: 429, headers: { "retry-after": "5" } }, model);
1444
- yield { type: "error", error: Object.assign(new Error("Too Many Requests"), { status: 429 }) };
1445
- return;
1446
- }
1447
- yield { type: "text_delta", text: "hello from codex" };
1448
- })() as any;
1449
- });
1450
-
1451
- try {
1452
- await providerRotationExtension(pi);
1453
- await events.session_start?.({}, ctx);
1454
- const stream = providers["anthropic"].streamSimple(
1455
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1456
- ctx,
1457
- );
1458
- const emitted = [] as any[];
1459
- for await (const event of stream) emitted.push(event);
1460
-
1461
- assert.equal(streamedModels.filter((m) => m.provider === "anthropic").length, 1);
1462
- assert.deepEqual(sleepCalls, []); // a genuine rate limit rotates immediately, it never sleeps in place
1463
- assert.deepEqual(setModelCalls.map((m) => m.provider), ["openai-codex"]);
1464
- assert.ok(emitted.some((e) => e.type === "text_delta"));
1465
- assert.ok(!emitted.some((e) => e.type === "error"));
1466
- const [entry] = helpers.readSubscriptionPool("anthropic");
1467
- // Retry-After: 5 -> ~5s cooldown, not the 5-minute default backoff.
1468
- assert.ok(entry.rateLimitedUntil > Date.now() + 4_000 && entry.rateLimitedUntil <= Date.now() + 5_000);
1469
- } finally {
1470
- rotationModule.__setStreamForProviderForTest(undefined);
1471
- rotationModule.__setSleepForTest(undefined);
1472
- }
1473
- });
1474
-
1475
- test("re-enters an already-attempted provider when a concurrent success makes it ready again", async () => {
1476
- await resetRotationState();
1477
- writeLadderConfig();
1478
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
1479
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1480
-
1481
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1482
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
1483
- const streamedProviders: string[] = [];
1484
- let anthropicAttempts = 0;
1485
- rotationModule.__setStreamForProviderForTest((model, _context, options) => {
1486
- streamedProviders.push(model.provider);
1487
- return (async function* () {
1488
- if (model.provider === "anthropic" && anthropicAttempts++ > 0) {
1489
- yield { type: "text_delta", text: "recovered on Anthropic" };
1490
- return;
1491
- }
1492
- if (model.provider === "openai-codex") {
1493
- // A concurrent turn began after this turn's 429, so its causal success
1494
- // may legitimately clear Anthropic's cooldown before Codex exhausts.
1495
- await helpers.markSubscriptionSuccess("anthropic", "primary", Date.now() + 1);
1496
- }
1497
- await options?.onResponse?.({ status: 429, headers: { "retry-after": "0" } }, model);
1498
- yield { type: "error", error: Object.assign(new Error("Too Many Requests"), { status: 429 }) };
1499
- })() as any;
1500
- });
1501
-
1502
- try {
1503
- await providerRotationExtension(pi);
1504
- await events.session_start?.({}, ctx);
1505
- const stream = providers["anthropic"].streamSimple(
1506
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1507
- ctx,
1508
- );
1509
- const emitted = [] as any[];
1510
- for await (const event of stream) emitted.push(event);
1511
-
1512
- assert.deepEqual(streamedProviders, ["anthropic", "openai-codex", "anthropic"]);
1513
- assert.deepEqual(setModelCalls.map((entry) => entry.provider), ["openai-codex", "anthropic"]);
1514
- assert.ok(emitted.some((event) => event.type === "text_delta"));
1515
- assert.ok(!emitted.some((event) => event.type === "error"), "the ready-again provider must not fall into fatal exhaustion");
1516
- } finally {
1517
- rotationModule.__setStreamForProviderForTest(undefined);
1518
- }
1519
- });
1520
-
1521
- test("does not cool down credentials or switch providers when the user aborts", async () => {
1522
- await resetRotationState();
1523
- writeLadderConfig();
1524
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
1525
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1526
-
1527
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1528
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
1529
- const streamedModels: any[] = [];
1530
- rotationModule.__setStreamForProviderForTest((model) => {
1531
- streamedModels.push(model);
1532
- return (async function* () {
1533
- yield {
1534
- type: "error",
1535
- reason: "aborted",
1536
- error: {
1537
- role: "assistant",
1538
- content: [],
1539
- api: model.api,
1540
- provider: model.provider,
1541
- model: model.id,
1542
- usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
1543
- stopReason: "aborted",
1544
- errorMessage: "Request was aborted",
1545
- timestamp: Date.now(),
1546
- },
1547
- };
1548
- })() as any;
1549
- });
1550
-
1551
- try {
1552
- await providerRotationExtension(pi);
1553
- await events.session_start?.({}, ctx);
1554
- // No AbortSignal is threaded through options here on purpose: the abort must be recognized
1555
- // from the event's own `reason: "aborted"` marker alone. Requiring the signal is exactly the
1556
- // regression that let an ESC cool down every credential and drain both provider pools.
1557
- const stream = providers["anthropic"].streamSimple(
1558
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1559
- ctx,
1560
- );
1561
- const emitted = [] as any[];
1562
- for await (const event of stream) emitted.push(event);
1563
-
1564
- assert.deepEqual(setModelCalls, []);
1565
- assert.deepEqual(streamedModels.map((m) => m.provider), ["anthropic"]);
1566
- assert.equal(emitted.length, 1);
1567
- assert.equal(emitted[0].type, "error");
1568
- assert.equal(emitted[0].reason, "aborted");
1569
- assert.equal(helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil, 0);
1570
- assert.equal(helpers.readSubscriptionPool("openai-codex")[0].rateLimitedUntil, 0);
1571
- } finally {
1572
- rotationModule.__setStreamForProviderForTest(undefined);
1573
- }
1574
- });
1575
-
1576
- test("skips invalid refresh tokens and serves the turn on the fallback provider", async () => {
1577
- await resetRotationState();
1578
- writeLadderConfig();
1579
- await helpers.writeSubscriptionPool("anthropic", [credential("stale", { expires: 1 })]);
1580
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1581
-
1582
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1583
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
1584
- const streamedModels: any[] = [];
1585
- // Plain Error, no typed .status: the pool path still recognizes invalid_grant
1586
- // from the message text.
1587
- rotationModule.__setRefreshForProviderForTest(async (providerId) => {
1588
- if (providerId === "anthropic") {
1589
- throw new Error('HTTP request failed. status=400; body={"error":"invalid_grant","error_description":"Refresh token not found or invalid"}');
1590
- }
1591
- throw new Error("unexpected refresh");
1592
- });
1593
- rotationModule.__setStreamForProviderForTest((model) => {
1594
- streamedModels.push(model);
1595
- return (async function* () {
1596
- yield { type: "text_delta", text: "hello from codex" };
1597
- })() as any;
1598
- });
1599
-
1600
- try {
1601
- await providerRotationExtension(pi);
1602
- await events.session_start?.({}, ctx);
1603
- const stream = providers["anthropic"].streamSimple(
1604
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1605
- ctx,
1606
- );
1607
- const emitted = [] as any[];
1608
- for await (const event of stream) emitted.push(event);
1609
-
1610
- assert.deepEqual(setModelCalls.map((m) => m.provider), ["openai-codex"]);
1611
- assert.equal(streamedModels.length, 1);
1612
- assert.equal(streamedModels[0].provider, "openai-codex");
1613
- assert.ok(emitted.some((e) => e.type === "text_delta"));
1614
- assert.ok(!emitted.some((e) => e.type === "error"));
1615
- assert.ok(helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil > Date.now() + 29 * 24 * 60 * 60 * 1000);
1616
- } finally {
1617
- rotationModule.__setRefreshForProviderForTest(undefined);
1618
- rotationModule.__setStreamForProviderForTest(undefined);
1619
- }
1620
- });
1621
-
1622
- test("all invalid refresh credentials surface an auth terminal instead of a typed cooldown", async () => {
1623
- await resetRotationState();
1624
- writeLadderConfig();
1625
- await helpers.writeSubscriptionPool("anthropic", [credential("anthropic-stale", { expires: 1 })]);
1626
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex-stale", { expires: 1 })]);
1627
-
1628
- const { ctx, providers, events, pi } = makeRotationCtx();
1629
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
1630
- rotationModule.__setRefreshForProviderForTest(async () => {
1631
- throw new Error('HTTP request failed. status=400; body={"error":"invalid_grant"}');
1632
- });
1633
-
1634
- try {
1635
- await providerRotationExtension(pi);
1636
- await events.session_start?.({}, ctx);
1637
- const stream = providers["anthropic"].streamSimple(
1638
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1639
- ctx,
1640
- );
1641
- const emitted = [] as any[];
1642
- for await (const event of stream) emitted.push(event);
1643
-
1644
- const terminal = emitted.find((event) => event.type === "error");
1645
- assert.ok(terminal, "all invalid credentials must terminate the turn");
1646
- assert.match(terminal.error.errorMessage, /authentication failed.*invalid_grant/i);
1647
- assert.equal(terminal.error.diagnostics, undefined, "an auth failure must not carry the auto-cooling diagnostic");
1648
- assert.equal(helpers.readSubscriptionPool("anthropic")[0].authFailure, "invalid_grant");
1649
- assert.equal(helpers.readSubscriptionPool("openai-codex")[0].authFailure, "invalid_grant");
1650
- } finally {
1651
- rotationModule.__setRefreshForProviderForTest(undefined);
1652
- }
1653
- });
1654
-
1655
- test("automatically reauthenticates invalid refresh tokens when UI is available", async () => {
1656
- await resetRotationState();
1657
- writeLadderConfig();
1658
- await helpers.writeSubscriptionPool("anthropic", [credential("stale", { expires: 1 })]);
1659
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1660
-
1661
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1662
- ctx.hasUI = true;
1663
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
1664
- ctx.ui.notify = () => {};
1665
- const loginCalls: string[] = [];
1666
- const streamedOptions: any[] = [];
1667
- // Plain Error, no typed .status: the pool path still recognizes invalid_grant
1668
- // from the message text.
1669
- rotationModule.__setRefreshForProviderForTest(async (providerId) => {
1670
- if (providerId === "anthropic") {
1671
- throw new Error('HTTP request failed. status=400; body={"error":"invalid_grant","error_description":"Refresh token not found or invalid"}');
1672
- }
1673
- throw new Error("unexpected refresh");
1674
- });
1675
- rotationModule.__setLoginForProviderForTest(async (providerId) => {
1676
- loginCalls.push(providerId);
1677
- return { refresh: "refresh-fresh", access: "access-fresh", expires: Date.now() + 60_000 };
1678
- });
1679
- rotationModule.__setStreamForProviderForTest((_model, _context, options) => {
1680
- streamedOptions.push(options);
1681
- return (async function* () {
1682
- yield { type: "text_delta", text: "hello after reauth" };
1683
- })() as any;
1684
- });
1685
-
1686
- try {
1687
- await providerRotationExtension(pi);
1688
- await events.session_start?.({}, ctx);
1689
- const stream = providers["anthropic"].streamSimple(
1690
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1691
- ctx,
1692
- );
1693
- const emitted = [] as any[];
1694
- for await (const event of stream) emitted.push(event);
1695
-
1696
- assert.deepEqual(loginCalls, ["anthropic"]);
1697
- assert.deepEqual(setModelCalls, []);
1698
- assert.equal(streamedOptions[0].apiKey, "access-fresh");
1699
- assert.ok(emitted.some((e) => e.type === "text_delta"));
1700
- const [stored] = helpers.readSubscriptionPool("anthropic");
1701
- assert.equal(stored.label, "stale");
1702
- assert.equal(stored.refresh, "refresh-fresh");
1703
- assert.equal(stored.access, "access-fresh");
1704
- assert.equal(stored.rateLimitedUntil, 0);
1705
- } finally {
1706
- rotationModule.__setRefreshForProviderForTest(undefined);
1707
- rotationModule.__setLoginForProviderForTest(undefined);
1708
- rotationModule.__setStreamForProviderForTest(undefined);
1709
- }
1710
- });
1711
-
1712
- // The real invalid-refresh path must route an explicit Codex reauth through the locked identity
1713
- // gate. A colliding OAuth result is rejected, the old credential remains intact, and the slot is
1714
- // cooled instead of being duplicated or streamed with another slot's quota.
1715
- test("real explicit Codex reauth rejects an account held by another slot", async () => {
1716
- await resetRotationState();
1717
- writeLadderConfig();
1718
- await helpers.writeSubscriptionPool("openai-codex", [
1719
- credential("stale", { expires: 1, accountId: "acct-old" }),
1720
- credential("held", { accountId: "acct-held", rateLimitedUntil: Date.now() + 10 * 60_000 }),
1721
- ]);
1722
-
1723
- const { ctx, providers, events, pi } = makeRotationCtx();
1724
- ctx.hasUI = true;
1725
- const notes: Array<{ message: string; type?: string }> = [];
1726
- ctx.ui.notify = (message: string, type?: string) => notes.push({ message, type });
1727
- rotationModule.__setRefreshForProviderForTest(async (providerId) => {
1728
- if (providerId === "openai-codex") {
1729
- throw new Error('HTTP request failed. status=400; body={"error":"invalid_grant","error_description":"Refresh token not found or invalid"}');
1730
- }
1731
- throw new Error("unexpected refresh");
1732
- });
1733
- rotationModule.__setLoginForProviderForTest(async () => ({
1734
- refresh: "r-collision",
1735
- access: "a-collision",
1736
- expires: Date.now() + 60_000,
1737
- accountId: "acct-held",
1738
- }));
1739
- try {
1740
- await providerRotationExtension(pi);
1741
- await events.session_start?.({}, ctx);
1742
- const stream = providers["openai-codex"].streamSimple(ctx.model, ctx);
1743
- for await (const _event of stream) {
1744
- // Drain the real managed-provider stream to completion.
1745
- }
1746
-
1747
- const pool = helpers.readSubscriptionPool("openai-codex");
1748
- const stale = pool.find((entry) => entry.label === "stale");
1749
- assert.equal(stale?.accountId, "acct-old");
1750
- assert.equal(stale?.refresh, "refresh-stale");
1751
- assert.ok((stale?.rateLimitedUntil ?? 0) > Date.now() + 29 * 24 * 60 * 60 * 1000);
1752
- assert.equal(pool.filter((entry) => entry.accountId === "acct-held").length, 1);
1753
- assert.ok(notes.some((note) => note.type === "error" && /re-authentication failed.*already added/i.test(note.message)));
1754
- } finally {
1755
- rotationModule.__setRefreshForProviderForTest(undefined);
1756
- rotationModule.__setLoginForProviderForTest(undefined);
1757
- }
1758
- });
1759
-
1760
- // The default Codex reauth branch uses the same gate before it commits. A colliding login leaves
1761
- // the default pool entry on the original account and keeps both pool identities unique.
1762
- test("real default Codex reauth rejects an explicit slot's account before switching the default entry", async () => {
1763
- await resetRotationState();
1764
- writeLadderConfig();
1765
- await helpers.writeSubscriptionPool("openai-codex", [
1766
- credential("openai-codex", { refresh: "dead-refresh", access: "stale-access", expires: 1, accountId: "acct-old" }),
1767
- ]);
1768
- await helpers.addSubscription("openai-codex", credential("held", { accountId: "acct-held", rateLimitedUntil: Date.now() + 10 * 60_000 }));
1769
-
1770
- const { ctx, providers, events, pi } = makeRotationCtx();
1771
- ctx.hasUI = true;
1772
- const notes: Array<{ message: string; type?: string }> = [];
1773
- ctx.ui.notify = (message: string, type?: string) => notes.push({ message, type });
1774
- rotationModule.__setRefreshForProviderForTest(async (providerId) => {
1775
- if (providerId === "openai-codex") {
1776
- throw new Error('HTTP request failed. status=400; body={"error":"invalid_grant","error_description":"Refresh token not found or invalid"}');
1777
- }
1778
- throw new Error("unexpected refresh");
1779
- });
1780
- rotationModule.__setLoginForProviderForTest(async () => ({
1781
- refresh: "r-collision",
1782
- access: "a-collision",
1783
- expires: Date.now() + 60_000,
1784
- accountId: "acct-held",
1785
- }));
1786
- try {
1787
- await providerRotationExtension(pi);
1788
- await events.session_start?.({}, ctx);
1789
- const stream = providers["openai-codex"].streamSimple(ctx.model, ctx);
1790
- for await (const _event of stream) {
1791
- // Drain the real managed-provider stream to completion.
1792
- }
1793
-
1794
- const pool = helpers.readSubscriptionPool("openai-codex");
1795
- const slot = pool.find((entry) => entry.label === "openai-codex");
1796
- assert.equal(slot?.accountId, "acct-old");
1797
- assert.equal(slot?.refresh, "dead-refresh");
1798
- assert.equal(pool.filter((entry) => entry.accountId === "acct-held").length, 1);
1799
- assert.ok(notes.some((note) => note.type === "error" && /re-authentication failed.*already added/i.test(note.message)));
1800
- } finally {
1801
- rotationModule.__setRefreshForProviderForTest(undefined);
1802
- rotationModule.__setLoginForProviderForTest(undefined);
1803
- }
1804
- });
1805
-
1806
- // A resolved-but-unauthenticated fallback stays on the current model and returns an
1807
- // actionable "not authenticated" error when no provider is logged in.
1808
- test("emits an actionable 'not authenticated' error without switching models when neither provider has ever logged in", async () => {
1809
- await resetRotationState();
1810
- writeLadderConfig();
1811
-
1812
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1813
-
1814
- await providerRotationExtension(pi);
1815
- await events.session_start?.({}, ctx);
1816
- const stream = providers["openai-codex"].streamSimple(ctx.model, ctx);
1817
- const emitted = [] as any[];
1818
- for await (const event of stream) emitted.push(event);
1819
-
1820
- // Never switches to the unauthenticated fallback -- nothing to try it with.
1821
- assert.deepEqual(setModelCalls, []);
1822
- assert.equal(emitted.length, 1);
1823
- assert.equal(emitted[0].type, "error");
1824
- assert.match(emitted[0].error.errorMessage, /All provider routes exhausted \(tried openai\)/);
1825
- });
1826
-
1827
- // The builtin ladder is available with no user configuration, so a config-less user can still
1828
- // resolve cross-provider fallbacks.
1829
- test("degrades gracefully via the builtin ladder when a config-less user has no cross-provider credentials", async () => {
1830
- await resetRotationState();
1831
- writeRawConfig("user", {});
1832
-
1833
- const ctx = {
1834
- cwd: nodeCwd,
1835
- model: { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1836
- ui: { setStatus() {} },
1837
- modelRegistry: {
1838
- find(provider: string, id: string) {
1839
- if (provider === "anthropic" && id === "claude-opus-4-8") return { provider, id, api: "anthropic-messages" };
1840
- if (provider === "openai-codex" && id === "gpt-5.6-sol") return { provider, id, api: "openai-codex-responses" };
1841
- return undefined;
1842
- },
1843
- // This scenario models a config-less user with no hand-written non-pool
1844
- // credential — pool state alone drives authentication here.
1845
- hasConfiguredAuth() {
1846
- return false;
1847
- },
1848
- },
1849
- } as any;
1850
- const setModelCalls: any[] = [];
1851
- const providers: Record<string, { streamSimple: (...args: any[]) => AsyncIterable<any> }> = {};
1852
- const events: Record<string, (...args: any[]) => unknown> = {};
1853
- const pi = {
1854
- registerProvider(name: string, provider: { streamSimple: (...args: any[]) => AsyncIterable<any> }) {
1855
- providers[name] = provider;
1856
- },
1857
- registerCommand() {},
1858
- on(eventName: string, handler: (...args: any[]) => unknown) {
1859
- events[eventName] = handler;
1860
- },
1861
- async setModel(model: any) {
1862
- setModelCalls.push(model);
1863
- ctx.model = model;
1864
- return true;
1865
- },
1866
- setThinkingLevel() {},
1867
- } as any;
1868
-
1869
- process.env.CRTR_MODEL_INTENT = "anthropic/strong";
1870
- await providerRotationExtension(pi);
1871
- await events.session_start?.({}, ctx);
1872
- // Anthropic's pool is empty, so the credential loop never runs. The builtin ladder resolves
1873
- // a cross-provider target (openai-codex/gpt-5.6-sol), but that provider is also
1874
- // unauthenticated; the session model stays unchanged and the turn returns actionable advice.
1875
- const stream = providers["anthropic"].streamSimple(ctx.model, ctx);
1876
- const emitted = [] as any[];
1877
- for await (const event of stream) emitted.push(event);
1878
-
1879
- assert.deepEqual(setModelCalls, []);
1880
- assert.equal(emitted.length, 1);
1881
- assert.equal(emitted[0].type, "error");
1882
- assert.match(emitted[0].error.errorMessage, /All provider routes exhausted \(tried live:anthropic\/claude-opus-4-8\)/);
1883
- assert.ok(!emitted[0].error.errorMessage.includes("configure a openai ladder"));
1884
- });
1885
-
1886
- // A cooling provider must settle the turn with a typed retry deadline. Holding
1887
- // the stream open here rendered the node as generic `Working...` for five silent
1888
- // minutes and prevented the broker's existing scheduled retry path from owning
1889
- // recovery.
1890
- test("settles a sole provider cooldown with a typed retry deadline instead of sleeping in-stream", async () => {
1891
- await resetRotationState();
1892
- writeLadderConfig();
1893
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
1894
- await helpers.markSubscriptionRateLimited("anthropic", "primary", 30_000);
1895
- const expectedDeadline = helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil;
1896
-
1897
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1898
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
1899
- const sleepCalls: number[] = [];
1900
- rotationModule.__setSleepForTest(async (ms) => { sleepCalls.push(ms); });
1901
- let streamCalls = 0;
1902
- rotationModule.__setStreamForProviderForTest((_model) => {
1903
- streamCalls += 1;
1904
- return (async function* () {
1905
- yield { type: "text_delta", text: "must not stream while cooling" };
1906
- })() as any;
1907
- });
1908
-
1909
- try {
1910
- await providerRotationExtension(pi);
1911
- await events.session_start?.({}, ctx);
1912
- const stream = providers["anthropic"].streamSimple(
1913
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1914
- ctx,
1915
- );
1916
- const emitted = [] as any[];
1917
- for await (const event of stream) emitted.push(event);
1918
-
1919
- assert.deepEqual(setModelCalls, []);
1920
- assert.deepEqual(sleepCalls, []);
1921
- assert.equal(streamCalls, 0);
1922
- assert.equal(emitted.length, 1);
1923
- assert.equal(emitted[0].type, "error");
1924
- assert.equal(emitted[0].error.diagnostics?.[0]?.type, "crtr:managed-provider-cooling");
1925
- assert.equal(emitted[0].error.diagnostics?.[0]?.details?.rateLimitedUntil, expectedDeadline);
1926
- } finally {
1927
- rotationModule.__setStreamForProviderForTest(undefined);
1928
- rotationModule.__setSleepForTest(undefined);
1929
- }
1930
- });
1931
-
1932
- // A fallback that is itself cooling must contribute its earlier deadline without
1933
- // churning the live session model to a provider that cannot serve the turn.
1934
- test("does not switch to a cooling fallback and schedules the earliest provider deadline", async () => {
1935
- await resetRotationState();
1936
- writeLadderConfig();
1937
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
1938
- await helpers.markSubscriptionRateLimited("anthropic", "primary", 60_000);
1939
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
1940
- await helpers.markSubscriptionRateLimited("openai-codex", "codex", 30_000);
1941
- const expectedDeadline = helpers.readSubscriptionPool("openai-codex")[0].rateLimitedUntil;
1942
-
1943
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1944
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
1945
- const sleepCalls: number[] = [];
1946
- let streamCalls = 0;
1947
- rotationModule.__setSleepForTest(async (ms) => { sleepCalls.push(ms); });
1948
- rotationModule.__setStreamForProviderForTest((_model) => {
1949
- streamCalls += 1;
1950
- return (async function* () {
1951
- yield { type: "text_delta", text: "must not stream while cooling" };
1952
- })() as any;
1953
- });
1954
-
1955
- try {
1956
- await providerRotationExtension(pi);
1957
- await events.session_start?.({}, ctx);
1958
- const stream = providers["anthropic"].streamSimple(
1959
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1960
- ctx,
1961
- );
1962
- const emitted = [] as any[];
1963
- for await (const event of stream) emitted.push(event);
1964
-
1965
- assert.deepEqual(setModelCalls, [], "a cooling fallback does not churn the session model");
1966
- assert.deepEqual(sleepCalls, []);
1967
- assert.equal(streamCalls, 0);
1968
- assert.equal(emitted.length, 1);
1969
- assert.equal(emitted[0].type, "error");
1970
- assert.equal(emitted[0].error.diagnostics?.[0]?.details?.rateLimitedUntil, expectedDeadline);
1971
- } finally {
1972
- rotationModule.__setStreamForProviderForTest(undefined);
1973
- rotationModule.__setSleepForTest(undefined);
1974
- }
1975
- });
1976
-
1977
- // Even an unusually long provider deadline uses the same typed scheduled-retry
1978
- // path; invalid-grant remains separately classified before this branch.
1979
- test("emits a typed rate-limit cooling error for a long provider deadline", async () => {
1980
- await resetRotationState();
1981
- writeLadderConfig();
1982
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
1983
- await helpers.markSubscriptionRateLimited("anthropic", "primary", 30 * 24 * 60 * 60 * 1000);
1984
- const expectedDeadline = helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil;
1985
-
1986
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
1987
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
1988
- const sleepCalls: number[] = [];
1989
- rotationModule.__setSleepForTest(async (ms) => {
1990
- sleepCalls.push(ms);
1991
- });
1992
-
1993
- try {
1994
- await providerRotationExtension(pi);
1995
- await events.session_start?.({}, ctx);
1996
- const stream = providers["anthropic"].streamSimple(
1997
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
1998
- ctx,
1999
- );
2000
- const emitted = [] as any[];
2001
- for await (const event of stream) emitted.push(event);
2002
-
2003
- assert.deepEqual(setModelCalls, []);
2004
- assert.deepEqual(sleepCalls, [], "a cooldown past the cap must not sleep in-process");
2005
- assert.equal(emitted.length, 1);
2006
- assert.equal(emitted[0].type, "error");
2007
- // Rate-limit-classifiable so classify('pi→provider', msg) → rate-limit/auto (not fatal other).
2008
- assert.match(emitted[0].error.errorMessage, /rate limit/i);
2009
- // The typed cooling diagnostic carries the exact pool deadline for the stophook.
2010
- assert.equal(emitted[0].error.diagnostics?.[0]?.type, "crtr:managed-provider-cooling");
2011
- assert.equal(emitted[0].error.diagnostics?.[0]?.details?.rateLimitedUntil, expectedDeadline);
2012
- } finally {
2013
- rotationModule.__setSleepForTest(undefined);
2014
- }
2015
- });
2016
-
2017
- test("an excluded invalid_grant credential does not mask a cooling credential", async () => {
2018
- await resetRotationState();
2019
- writeLadderConfig();
2020
- await helpers.writeSubscriptionPool("anthropic", [
2021
- credential("dead", { authFailure: "invalid_grant", rateLimitedUntil: Date.now() + 30 * 24 * 60 * 60 * 1000 }),
2022
- credential("cooling", { rateLimitedUntil: Date.now() + 60_000 }),
2023
- ]);
2024
- const expectedDeadline = helpers.readSubscriptionPool("anthropic").find((entry) => entry.label === "cooling")!.rateLimitedUntil;
2025
-
2026
- const { ctx, providers, events, pi } = makeRotationCtx();
2027
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
2028
-
2029
- await providerRotationExtension(pi);
2030
- await events.session_start?.({}, ctx);
2031
- const stream = providers["anthropic"].streamSimple(ctx.model, ctx);
2032
- const emitted = [] as any[];
2033
- for await (const event of stream) emitted.push(event);
2034
-
2035
- assert.equal(emitted.length, 1);
2036
- assert.equal(emitted[0].type, "error");
2037
- assert.match(emitted[0].error.errorMessage, /rate limited/i);
2038
- assert.doesNotMatch(emitted[0].error.errorMessage, /invalid_grant/);
2039
- assert.equal(emitted[0].error.diagnostics?.[0]?.type, "crtr:managed-provider-cooling");
2040
- assert.equal(emitted[0].error.diagnostics?.[0]?.details?.rateLimitedUntil, expectedDeadline);
2041
- });
2042
-
2043
- test("a same-turn 429 settles immediately instead of starting cooldown continuations", async () => {
2044
- await resetRotationState();
2045
- writeLadderConfig();
2046
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
2047
- const { ctx, providers, events, pi } = makeRotationCtx();
2048
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
2049
- let attempts = 0;
2050
- let waits = 0;
2051
- rotationModule.__setSleepForTest(async () => { waits++; });
2052
- rotationModule.__setStreamForProviderForTest((model, _context, options) => {
2053
- attempts++;
2054
- return (async function* () {
2055
- await options?.onResponse?.({ status: 429, headers: { "retry-after": "1" } }, model);
2056
- yield { type: "error", error: Object.assign(new Error("Too Many Requests"), { status: 429 }) };
2057
- })() as any;
2058
- });
2059
-
2060
- try {
2061
- await providerRotationExtension(pi);
2062
- await events.session_start?.({}, ctx);
2063
- const stream = providers["anthropic"].streamSimple(
2064
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
2065
- ctx,
2066
- );
2067
- const emitted = [] as any[];
2068
- for await (const event of stream) emitted.push(event);
2069
-
2070
- assert.equal(attempts, 1, "the mocked clock keeps the credential cooling after its first 429");
2071
- assert.equal(waits, 0, "cooldown recovery never sleeps inside the active turn");
2072
- assert.equal(emitted.length, 1);
2073
- assert.equal(emitted[0].error.diagnostics?.[0]?.type, "crtr:managed-provider-cooling");
2074
- } finally {
2075
- rotationModule.__setSleepForTest(undefined);
2076
- rotationModule.__setStreamForProviderForTest(undefined);
2077
- }
2078
- });
2079
-
2080
- test("does not requeue a zero Retry-After credential without a later success", async () => {
2081
- await resetRotationState();
2082
- writeLadderConfig();
2083
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
2084
- const { ctx, providers, events, pi } = makeRotationCtx();
2085
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
2086
- let attempts = 0;
2087
- rotationModule.__setStreamForProviderForTest((model, _context, options) => {
2088
- attempts++;
2089
- return (async function* () {
2090
- await options?.onResponse?.({ status: 429, headers: { "retry-after": "0" } }, model);
2091
- yield { type: "error", error: Object.assign(new Error("Too Many Requests"), { status: 429 }) };
2092
- })() as any;
2093
- });
2094
-
2095
- try {
2096
- await providerRotationExtension(pi);
2097
- await events.session_start?.({}, ctx);
2098
- const stream = providers["anthropic"].streamSimple(ctx.model, ctx);
2099
- const emitted = [] as any[];
2100
- for await (const event of stream) emitted.push(event);
2101
-
2102
- assert.equal(attempts, 1, "a zero deadline must not manufacture a fresh candidate");
2103
- assert.equal(emitted.length, 1);
2104
- assert.equal(emitted[0].type, "error");
2105
- } finally {
2106
- rotationModule.__setStreamForProviderForTest(undefined);
2107
- }
2108
- });
2109
-
2110
- // Fresh-availability re-entry (Fix 3a): a credential that becomes ready AFTER the initial
2111
- // `available` snapshot (a concurrent turn's cooldown expiring, or a causally valid success)
2112
- // must be consumed by re-entering the SAME provider, not lost to a stale snapshot that would
2113
- // wrongly declare exhaustion. Start on an unauthenticated Anthropic (empty pool) so the
2114
- // fallback to Codex is already attempted; Codex has A ready + B cooling; A's 429 frees B
2115
- // mid-attempt, so the re-entry must attempt B and stream the turn with no terminal error.
2116
- test("re-enters the same provider to consume a credential that became ready after the initial snapshot", async () => {
2117
- await resetRotationState();
2118
- writeLadderConfig();
2119
- // Anthropic unauthenticated (empty) so the turn falls back to Codex immediately.
2120
- await helpers.writeSubscriptionPool("anthropic", []);
2121
- // Codex: A ready now, B cooling (absent from the initial ready snapshot).
2122
- await helpers.writeSubscriptionPool("openai-codex", [
2123
- credential("A"),
2124
- credential("B", { rateLimitedUntil: Date.now() + 60_000 }),
2125
- ]);
2126
-
2127
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
2128
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
2129
- const streamedModels: any[] = [];
2130
- let codexAttempt = 0;
2131
- rotationModule.__setStreamForProviderForTest((model, _context, options) => {
2132
- streamedModels.push(model.provider);
2133
- return (async function* () {
2134
- if (model.provider === "openai-codex") {
2135
- codexAttempt += 1;
2136
- if (codexAttempt === 1) {
2137
- // Attempt A: 429 (cools A). Free B mid-attempt so it becomes ready AFTER the
2138
- // snapshot that only saw A — the fresh-availability condition under test.
2139
- await options?.onResponse?.({ status: 429, headers: { "retry-after": "5" } }, model);
2140
- const pool = helpers.readSubscriptionPool("openai-codex");
2141
- await helpers.writeSubscriptionPool(
2142
- "openai-codex",
2143
- pool.map((entry) => (entry.label === "B" ? { ...entry, rateLimitedUntil: 0 } : entry)),
2144
- );
2145
- yield { type: "error", error: Object.assign(new Error("Too Many Requests"), { status: 429 }) };
2146
- return;
2147
- }
2148
- // Re-entry attempt: B is now ready — serve the turn.
2149
- yield { type: "text_delta", text: "hello from codex B after re-entry" };
2150
- return;
2151
- }
2152
- yield { type: "text_delta", text: "unexpected" };
2153
- })() as any;
2154
- });
2155
-
2156
- try {
2157
- await providerRotationExtension(pi);
2158
- await events.session_start?.({}, ctx);
2159
- const stream = providers["anthropic"].streamSimple(
2160
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
2161
- ctx,
2162
- );
2163
- const emitted = [] as any[];
2164
- for await (const event of stream) emitted.push(event);
2165
-
2166
- // Fell back to codex, attempted A (429), then re-entered and attempted B to success.
2167
- assert.deepEqual(setModelCalls.map((m) => m.provider), ["openai-codex"]);
2168
- assert.equal(codexAttempt, 2, "re-entered the same provider once to consume the freshly-ready credential");
2169
- assert.ok(emitted.some((e) => e.type === "text_delta"), "the turn was served, not terminally failed");
2170
- assert.ok(!emitted.some((e) => e.type === "error"), "no terminal all-pools error despite the stale initial snapshot");
2171
- } finally {
2172
- rotationModule.__setStreamForProviderForTest(undefined);
2173
- }
2174
- });
2175
-
2176
- // Both-pools-cooling typed diagnostic (Fix 3b): when EVERY managed pool tried this turn is
2177
- // cooling with a finite future deadline, the terminal error must be rate-limit-classifiable
2178
- // AND carry the typed cooling diagnostic whose deadline is the exact MINIMUM across the tried
2179
- // pools (so the stophook stamps retry.nextAt at the soonest pool free-up).
2180
- test("both pools cooling: emits a rate-limit-classifiable terminal carrying the exact earliest deadline as a typed diagnostic", async () => {
2181
- await resetRotationState();
2182
- writeLadderConfig();
2183
- const now = Date.now();
2184
- const anthropicDeadline = now + 10 * 60_000; // earliest
2185
- const codexDeadline = now + 20 * 60_000;
2186
- await helpers.writeSubscriptionPool("anthropic", [credential("a", { rateLimitedUntil: anthropicDeadline })]);
2187
- await helpers.writeSubscriptionPool("openai-codex", [credential("c", { rateLimitedUntil: codexDeadline })]);
2188
- const expectedMin = helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil;
2189
-
2190
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
2191
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
2192
- const sleepCalls: number[] = [];
2193
- rotationModule.__setSleepForTest(async (ms) => {
2194
- sleepCalls.push(ms);
2195
- });
2196
-
2197
- try {
2198
- await providerRotationExtension(pi);
2199
- await events.session_start?.({}, ctx);
2200
- const stream = providers["anthropic"].streamSimple(
2201
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
2202
- ctx,
2203
- );
2204
- const emitted = [] as any[];
2205
- for await (const event of stream) emitted.push(event);
2206
-
2207
- // A cooling fallback contributes its deadline without churning the live model.
2208
- assert.deepEqual(setModelCalls, []);
2209
- assert.deepEqual(sleepCalls, [], "cooldowns are never waited out in-process");
2210
- assert.equal(emitted.length, 1);
2211
- assert.equal(emitted[0].type, "error");
2212
- assert.match(emitted[0].error.errorMessage, /rate limit/i, "classifiable as rate-limit (auto), not fatal other");
2213
- assert.equal(emitted[0].error.diagnostics?.[0]?.type, "crtr:managed-provider-cooling");
2214
- assert.equal(
2215
- emitted[0].error.diagnostics?.[0]?.details?.rateLimitedUntil,
2216
- expectedMin,
2217
- "the diagnostic carries the exact MINIMUM deadline across both tried pools",
2218
- );
2219
- } finally {
2220
- rotationModule.__setSleepForTest(undefined);
2221
- }
2222
- });
2223
-
2224
- // A 429 observed during this provider call must author the deadline from the
2225
- // freshly-mutated pool, without retrying or sleeping inside the active turn.
2226
- test("schedules a sole credential cooldown observed in this turn from fresh pool state", async () => {
2227
- await resetRotationState();
2228
- writeLadderConfig();
2229
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
2230
- // openai-codex pool stays empty -- no authenticated fallback to switch to.
2231
-
2232
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
2233
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
2234
- const sleepCalls: number[] = [];
2235
- let anthropicCalls = 0;
2236
- rotationModule.__setSleepForTest(async (ms) => { sleepCalls.push(ms); });
2237
- rotationModule.__setStreamForProviderForTest((model, _context, options) => {
2238
- return (async function* () {
2239
- anthropicCalls += 1;
2240
- if (anthropicCalls === 1) {
2241
- await options?.onResponse?.({ status: 429, headers: { "retry-after": "1" } }, model);
2242
- yield { type: "error", error: Object.assign(new Error("Too Many Requests"), { status: 429 }) };
2243
- return;
2244
- }
2245
- yield { type: "text_delta", text: "must not retry inside this turn" };
2246
- })() as any;
2247
- });
2248
-
2249
- try {
2250
- await providerRotationExtension(pi);
2251
- await events.session_start?.({}, ctx);
2252
- const stream = providers["anthropic"].streamSimple(
2253
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
2254
- ctx,
2255
- );
2256
- const emitted = [] as any[];
2257
- for await (const event of stream) emitted.push(event);
2258
-
2259
- assert.deepEqual(setModelCalls, []);
2260
- assert.deepEqual(sleepCalls, []);
2261
- assert.equal(anthropicCalls, 1);
2262
- assert.equal(emitted.length, 1);
2263
- assert.equal(emitted[0].type, "error");
2264
- assert.equal(emitted[0].error.diagnostics?.[0]?.details?.rateLimitedUntil, helpers.readSubscriptionPool("anthropic")[0].rateLimitedUntil);
2265
- } finally {
2266
- rotationModule.__setStreamForProviderForTest(undefined);
2267
- rotationModule.__setSleepForTest(undefined);
2268
- }
2269
- });
2270
-
2271
- // pi-ai's Codex adapter can surface a genuine usage-limit exhaustion on a non-429
2272
- // status. Without Retry-After it is local to this turn (never a global gate),
2273
- // while the authenticated fallback still serves the same turn.
2274
- test("treats a headerless Codex usage-limit message as turn-local and falls back", async () => {
2275
- await resetRotationState();
2276
- writeLadderConfig();
2277
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
2278
- await helpers.writeSubscriptionPool("anthropic", [credential("personal")]);
2279
-
2280
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
2281
- ctx.model = { provider: "openai-codex", id: "gpt-5.6-sol", api: "openai-codex-responses" };
2282
- const streamedModels: any[] = [];
2283
- rotationModule.__setStreamForProviderForTest((model) => {
2284
- streamedModels.push(model);
2285
- return (async function* () {
2286
- if (model.provider === "openai-codex") {
2287
- yield {
2288
- type: "error",
2289
- error: Object.assign(new Error("You have hit your ChatGPT usage limit. Please try again later."), {
2290
- status: 403,
2291
- errorMessage: "You have hit your ChatGPT usage limit. usage_limit_reached",
2292
- }),
2293
- };
2294
- return;
2295
- }
2296
- yield { type: "text_delta", text: "hello from anthropic fallback" };
2297
- })() as any;
2298
- });
2299
-
2300
- try {
2301
- await providerRotationExtension(pi);
2302
- await events.session_start?.({}, ctx);
2303
- const stream = providers["openai-codex"].streamSimple(
2304
- { provider: "openai-codex", id: "gpt-5.6-sol", api: "openai-codex-responses" },
2305
- ctx,
2306
- );
2307
- const emitted = [] as any[];
2308
- for await (const event of stream) emitted.push(event);
2309
-
2310
- // A headerless usage-limit signal is turn-local: it proves this request
2311
- // failed, not that unrelated brokers sharing the credential are cooling.
2312
- assert.equal(helpers.readSubscriptionPool("openai-codex")[0].rateLimitedUntil, 0);
2313
- // Fell back to the authenticated Anthropic subscription and served the turn on it.
2314
- assert.deepEqual(setModelCalls.map((m) => m.provider), ["anthropic"]);
2315
- assert.equal(streamedModels.filter((m) => m.provider === "anthropic").length, 1);
2316
- assert.ok(emitted.some((e) => e.type === "text_delta"));
2317
- assert.ok(!emitted.some((e) => e.type === "error"));
2318
- } finally {
2319
- rotationModule.__setStreamForProviderForTest(undefined);
2320
- }
2321
- });
2322
-
2323
- test("a concrete model's headerless usage-limit stays turn-local and carries its retry deadline", async () => {
2324
- await resetRotationState();
2325
- writeLadderConfig();
2326
- await helpers.writeSubscriptionPool("openai-codex", [credential("codex")]);
2327
- await helpers.writeSubscriptionPool("anthropic", [credential("personal")]);
2328
-
2329
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx(undefined);
2330
- ctx.model = { provider: "openai-codex", id: "gpt-5.6-sol", api: "openai-codex-responses" };
2331
- rotationModule.__setStreamForProviderForTest(() => {
2332
- return (async function* () {
2333
- yield {
2334
- type: "error",
2335
- error: Object.assign(new Error("You have hit your ChatGPT usage limit."), {
2336
- status: 403,
2337
- errorMessage: "usage_limit_reached",
2338
- }),
2339
- };
2340
- })() as any;
2341
- });
2342
-
2343
- try {
2344
- await providerRotationExtension(pi);
2345
- await events.session_start?.({}, ctx);
2346
- const before = Date.now();
2347
- const emitted = [] as any[];
2348
- for await (const event of providers["openai-codex"].streamSimple(ctx.model, ctx)) emitted.push(event);
2349
-
2350
- assert.deepEqual(setModelCalls, []);
2351
- assert.equal(helpers.readSubscriptionPool("openai-codex")[0].rateLimitedUntil, 0);
2352
- assert.equal(emitted.length, 1);
2353
- assert.equal(emitted[0].error.diagnostics?.[0]?.type, "crtr:managed-provider-cooling");
2354
- const deadline = emitted[0].error.diagnostics?.[0]?.details?.rateLimitedUntil;
2355
- assert.ok(deadline >= before + 14_000 && deadline <= Date.now() + 15_000);
2356
- } finally {
2357
- rotationModule.__setStreamForProviderForTest(undefined);
2358
- }
2359
- });
2360
-
2361
- // Provider-management removal routing and login coordination
2362
-
2363
- function readAuthRecord(providerId: string): unknown {
2364
- const authPath = join(home, ".pi", "agent", "auth.json");
2365
- if (!existsSync(authPath)) return undefined;
2366
- return (JSON.parse(readFileSync(authPath, "utf8")) as Record<string, unknown>)[providerId];
2367
- }
2368
-
2369
- // Removing a managed provider from its HEADER row (no label -- the default cursor position) tears
2370
- // down the WHOLE pool, not just the account: a surviving explicit account would stay
2371
- // configured and keep serving turns while the panel said "Removed".
2372
- test("removeProviderRow on a managed HEADER row clears the whole pool", async () => {
2373
- await resetRotationState();
2374
- await helpers.upsertSubscription("anthropic", credential("anthropic"));
2375
- await helpers.upsertSubscription("anthropic", credential("work"));
2376
- assert.ok(helpers.readSubscriptionPool("anthropic").some((e) => e.label === "anthropic"), "pool account present");
2377
- assert.ok(helpers.readSubscriptionPool("anthropic").some((e) => e.label === "work"), "explicit account present");
2378
-
2379
- const notice = await providerManagement.removeProviderRow(join(home, ".pi", "agent"), {
2380
- providerId: "anthropic",
2381
- name: "Claude",
2382
- managed: true,
2383
- configured: true,
2384
- // no label -> the provider header row
2385
- });
2386
-
2387
- assert.equal(notice, "Removed Claude");
2388
- assert.deepEqual(helpers.readSubscriptionPool("anthropic"), [], "pool fully torn down");
2389
- });
2390
-
2391
- // An account ROW (has a label) removes just that account and leaves the rest of the pool intact.
2392
- test("removeProviderRow on a managed account row removes only that account", async () => {
2393
- await resetRotationState();
2394
- await helpers.upsertSubscription("anthropic", credential("anthropic"));
2395
- await helpers.upsertSubscription("anthropic", credential("work"));
2396
-
2397
- const notice = await providerManagement.removeProviderRow(join(home, ".pi", "agent"), {
2398
- providerId: "anthropic",
2399
- name: "Claude",
2400
- managed: true,
2401
- label: "work",
2402
- });
2403
-
2404
- assert.equal(notice, "Removed Claude account");
2405
- const remaining = helpers.readSubscriptionPool("anthropic");
2406
- assert.deepEqual(
2407
- remaining.map((e) => e.label),
2408
- ["anthropic"],
2409
- "only the pool account remains",
2410
- );
2411
- assert.equal(remaining[0].refresh, "refresh-anthropic", "the pool account keeps its own token");
2412
- });
2413
-
2414
- // Managed login commits the pool account through commitManagedLogin; the default pool entry
2415
- // carries its tokens and auth.json has no managed OAuth record.
2416
- test("commitManagedLogin pool account writes tokens into the default pool entry, not auth.json", async () => {
2417
- await resetRotationState();
2418
- await helpers.commitManagedLogin("anthropic", "anthropic", { refresh: "r1", access: "a1", expires: Date.now() + 60_000, accountId: "acct-1", lastRateLimitedAt: 0 });
2419
-
2420
- const slot = helpers.readSubscriptionPool("anthropic").find((e) => e.label === "anthropic");
2421
- assert.ok(slot, "pool account present");
2422
- assert.equal(slot?.accountId, "acct-1", "pool pool account carries the identity");
2423
- assert.equal(slot?.refresh, "r1", "pool pool account carries its own token");
2424
- assert.equal(readAuthRecord("anthropic"), undefined, "no auth.json record written for a managed login");
2425
- });
2426
-
2427
- // A managed login that identifies an existing account refreshes that row rather than creating a
2428
- // second quota slot under the requested label.
2429
- test("commitManagedLogin updates an existing account identity", async () => {
2430
- await resetRotationState();
2431
- await helpers.commitManagedLogin("openai-codex", "first", { refresh: "r", access: "a", expires: Date.now() + 60_000, accountId: "dup", lastRateLimitedAt: 0 });
2432
- await helpers.commitManagedLogin("openai-codex", "second", { refresh: "r2", access: "a2", expires: Date.now() + 60_000, accountId: "dup", lastRateLimitedAt: 0 });
2433
- const pool = helpers.readSubscriptionPool("openai-codex");
2434
- assert.equal(pool.length, 1);
2435
- assert.equal(pool[0].label, "first");
2436
- assert.equal(pool[0].refresh, "r2");
2437
- });
2438
-
2439
- // A provider with NO account must never be switched onto: rotation would move the session model
2440
- // onto a provider pi cannot serve, and the next turn dies with the non-retryable
2441
- // `Provider is not configured: openai-codex`, which no agent can clear from inside a turn that
2442
- // fails before it starts.
2443
- test("a provider with no account: rotation never switches onto it", async () => {
2444
- await resetRotationState();
2445
- writeLadderConfig();
2446
- await helpers.writeSubscriptionPool("anthropic", [credential("primary")]);
2447
- await helpers.markSubscriptionRateLimited("anthropic", "primary", 30 * 24 * 60 * 60 * 1000);
2448
- await helpers.writeSubscriptionPool("openai-codex", []);
2449
- assert.equal(helpers.readSubscriptionPool("openai-codex").length, 0, "no account stored");
2450
-
2451
- const { ctx, setModelCalls, providers, events, pi } = makeRotationCtx();
2452
- ctx.model = { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" };
2453
-
2454
- await providerRotationExtension(pi);
2455
- await events.session_start?.({}, ctx);
2456
- const stream = providers["anthropic"].streamSimple(
2457
- { provider: "anthropic", id: "claude-opus-4-8", api: "anthropic-messages" },
2458
- ctx,
2459
- );
2460
- const emitted = [] as any[];
2461
- for await (const event of stream) emitted.push(event);
2462
-
2463
- assert.deepEqual(setModelCalls, [], "never switches the session model onto an unconfigured provider");
2464
- assert.equal(emitted.length, 1);
2465
- assert.equal(emitted[0].type, "error");
2466
- // An unauthenticated fallback never receives a model switch, while the live route's cooldown remains actionable.
2467
- assert.match(emitted[0].error.errorMessage, /All provider routes are rate limited/);
2468
- });
2469
-
2470
-
2471
- test("Anthropic profile enrichment reads the real account identity", async () => {
2472
- const originalFetch = globalThis.fetch;
2473
- globalThis.fetch = async () => new Response(JSON.stringify({ account: { uuid: "account-a", email: "a@example.test" } }), { status: 200 });
2474
- try {
2475
- assert.deepEqual(
2476
- await helpers.enrichManagedLoginIdentity("anthropic", { access: "test-access" }),
2477
- { accountId: "account-a", email: "a@example.test" },
2478
- );
2479
- } finally {
2480
- globalThis.fetch = originalFetch;
2481
- }
2482
- });
2483
-
2484
- test("a failed Anthropic profile lookup leaves identity unknown", async () => {
2485
- const originalFetch = globalThis.fetch;
2486
- globalThis.fetch = async () => { throw new Error("profile unavailable"); };
2487
- try {
2488
- assert.deepEqual(await helpers.enrichManagedLoginIdentity("anthropic", { access: "test-access" }), {});
2489
- } finally {
2490
- globalThis.fetch = originalFetch;
2491
- }
2492
- });
2493
-
2494
- test("Anthropic managed login auto-labels new accounts and updates the matching account", async () => {
2495
- await resetRotationState();
2496
- const originalFetch = globalThis.fetch;
2497
- globalThis.fetch = async (_url, options) => {
2498
- const access = (options as RequestInit).headers as Record<string, string>;
2499
- const account = access.Authorization === "Bearer a1" || access.Authorization === "Bearer a2"
2500
- ? { uuid: "account-a", email: "a@example.test" }
2501
- : { uuid: "account-b", email: "b@example.test" };
2502
- return new Response(JSON.stringify({ account }), { status: 200 });
2503
- };
2504
- try {
2505
- const expires = Date.now() + 60_000;
2506
- await helpers.commitManagedLogin("anthropic", "anthropic", { refresh: "refresh-a1", access: "a1", expires, lastRateLimitedAt: 0, autoLabel: true });
2507
- await helpers.commitManagedLogin("anthropic", "anthropic", { refresh: "refresh-a2", access: "a2", expires, lastRateLimitedAt: 0, autoLabel: true });
2508
- await helpers.commitManagedLogin("anthropic", "anthropic", { refresh: "refresh-b", access: "b", expires, lastRateLimitedAt: 0, autoLabel: true });
2509
- const pool = helpers.readSubscriptionPool("anthropic");
2510
- assert.deepEqual(pool.map((entry) => entry.label), ["a@example.test", "b@example.test"]);
2511
- assert.equal(pool[0]?.refresh, "refresh-a2");
2512
- } finally {
2513
- globalThis.fetch = originalFetch;
2514
- }
2515
- });
2516
-
2517
- test("managed login preserves a cooldown newer than its login snapshot", async () => {
2518
- await resetRotationState();
2519
- await helpers.writeSubscriptionPool("openai-codex", [credential("personal", { accountId: "account-a", lastRateLimitedAt: 100, rateLimitedUntil: 60_100 })]);
2520
- await helpers.commitManagedLogin("openai-codex", "personal", {
2521
- refresh: "refresh-new",
2522
- access: "access-new",
2523
- expires: Date.now() + 60_000,
2524
- accountId: "account-a",
2525
- lastRateLimitedAt: 0,
2526
- });
2527
- const [entry] = helpers.readSubscriptionPool("openai-codex");
2528
- assert.equal(entry.rateLimitedUntil, 60_100);
2529
- assert.equal(entry.lastRateLimitedAt, 100);
2530
- });