@cortex-agent/server 2026.10.15 → 2026.10.21

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 (333) hide show
  1. package/README.md +76 -50
  2. package/defaults/board/board.css +140 -62
  3. package/defaults/board/scaffold.html +36 -13
  4. package/defaults/config/environments/creative.json +0 -1
  5. package/defaults/config/environments/main.json +1 -2
  6. package/defaults/plugins/cortex-board/skills/board/SKILL.md +30 -22
  7. package/defaults/plugins/cortex-system/plugin.json +2 -2
  8. package/defaults/plugins/cortex-system/skills/browser/SKILL.md +56 -0
  9. package/dist/agent-adapter/claude/adapter.d.ts +2 -5
  10. package/dist/agent-adapter/claude/adapter.js +6 -5
  11. package/dist/agent-adapter/claude/assistant-blocks.d.ts +5 -0
  12. package/dist/agent-adapter/claude/assistant-blocks.js +61 -0
  13. package/dist/agent-adapter/claude/cache-refresh.d.ts +58 -0
  14. package/dist/agent-adapter/claude/cache-refresh.js +145 -0
  15. package/dist/agent-adapter/claude/cache-touch.d.ts +12 -0
  16. package/dist/agent-adapter/claude/cache-touch.js +75 -0
  17. package/dist/agent-adapter/claude/event-parser.d.ts +8 -3
  18. package/dist/agent-adapter/claude/event-parser.js +33 -17
  19. package/dist/agent-adapter/claude/event-translator.d.ts +2 -0
  20. package/dist/agent-adapter/claude/event-translator.js +2 -0
  21. package/dist/agent-adapter/claude/session-options.d.ts +6 -13
  22. package/dist/agent-adapter/claude/session-options.js +2 -13
  23. package/dist/agent-adapter/claude/spawn-args.d.ts +4 -21
  24. package/dist/agent-adapter/claude/spawn-args.js +17 -24
  25. package/dist/agent-adapter/claude/transcript-clear.d.ts +42 -0
  26. package/dist/agent-adapter/claude/transcript-clear.js +217 -0
  27. package/dist/agent-adapter/claude/turn-machine.d.ts +3 -2
  28. package/dist/agent-adapter/claude/turn-machine.js +21 -68
  29. package/dist/agent-adapter/claude/window-warmup.d.ts +13 -0
  30. package/dist/agent-adapter/claude/window-warmup.js +38 -0
  31. package/dist/agent-adapter/continuation-phase.js +3 -0
  32. package/dist/agent-adapter/normalize/event-types.d.ts +8 -0
  33. package/dist/agent-adapter/pi/adapter.d.ts +0 -1
  34. package/dist/agent-adapter/pi/adapter.js +5 -13
  35. package/dist/agent-adapter/pi/event-parser.d.ts +1 -1
  36. package/dist/agent-adapter/pi/event-parser.js +43 -27
  37. package/dist/agent-adapter/pi/mcp-bridge.d.ts +1 -1
  38. package/dist/agent-adapter/pi/pi-session.d.ts +2 -0
  39. package/dist/agent-adapter/pi/pi-session.js +15 -0
  40. package/dist/agent-adapter/pi/providers-config.d.ts +2 -2
  41. package/dist/agent-adapter/pi/providers-config.js +1 -1
  42. package/dist/agent-adapter/pi/quota-sink.d.ts +3 -3
  43. package/dist/agent-adapter/pi/quota-sink.js +3 -3
  44. package/dist/agent-adapter/pi/runtime.js +6 -1
  45. package/dist/agent-adapter/pi/session-options.d.ts +9 -13
  46. package/dist/agent-adapter/pi/session-options.js +12 -25
  47. package/dist/agent-adapter/run-events.d.ts +11 -10
  48. package/dist/agent-adapter/run-events.js +2 -0
  49. package/dist/agent-adapter/types.d.ts +5 -4
  50. package/dist/client-bundle/manifest.json +2 -2
  51. package/dist/core/codex-quota.d.ts +1 -1
  52. package/dist/core/codex-quota.js +1 -1
  53. package/dist/core/gateway-generator.d.ts +9 -11
  54. package/dist/core/gateway-generator.js +31 -58
  55. package/dist/core/locales/en.d.ts +51 -77
  56. package/dist/core/locales/slices/commands.d.ts +26 -31
  57. package/dist/core/locales/slices/commands.js +54 -64
  58. package/dist/core/locales/slices/init.d.ts +8 -19
  59. package/dist/core/locales/slices/init.js +17 -53
  60. package/dist/core/locales/slices/interactions.d.ts +3 -3
  61. package/dist/core/locales/slices/interactions.js +6 -6
  62. package/dist/core/locales/slices/notices.d.ts +1 -3
  63. package/dist/core/locales/slices/notices.js +2 -6
  64. package/dist/core/locales/slices/scheduling.d.ts +9 -9
  65. package/dist/core/locales/slices/scheduling.js +18 -18
  66. package/dist/core/locales/slices/status.d.ts +0 -4
  67. package/dist/core/locales/slices/status.js +0 -8
  68. package/dist/core/locales/slices/tui.d.ts +0 -1
  69. package/dist/core/locales/slices/tui.js +0 -2
  70. package/dist/core/locales/slices/uimsg.d.ts +4 -7
  71. package/dist/core/locales/slices/uimsg.js +9 -15
  72. package/dist/core/models-generator.d.ts +29 -0
  73. package/dist/core/models-generator.js +82 -0
  74. package/dist/core/paths.d.ts +1 -1
  75. package/dist/core/paths.js +1 -1
  76. package/dist/core/settings-spec.d.ts +25 -0
  77. package/dist/core/settings-spec.js +34 -0
  78. package/dist/core/status-format.d.ts +3 -2
  79. package/dist/core/status-format.js +2 -2
  80. package/dist/core/types/environment-types.d.ts +0 -2
  81. package/dist/domain/agents/agent-state.d.ts +11 -56
  82. package/dist/domain/agents/agent-state.js +48 -115
  83. package/dist/domain/agents/agent-switch.d.ts +2 -24
  84. package/dist/domain/agents/agent-switch.js +9 -72
  85. package/dist/domain/agents/config.d.ts +11 -61
  86. package/dist/domain/agents/config.js +47 -154
  87. package/dist/domain/agents/engine-selection.d.ts +21 -0
  88. package/dist/domain/agents/engine-selection.js +37 -0
  89. package/dist/domain/agents/environments/registry.js +2 -1
  90. package/dist/domain/agents/environments/resolve.js +0 -1
  91. package/dist/domain/agents/environments/schema.d.ts +0 -1
  92. package/dist/domain/agents/environments/schema.js +0 -1
  93. package/dist/domain/agents/index.d.ts +2 -1
  94. package/dist/domain/agents/index.js +3 -7
  95. package/dist/domain/agents/model-selection.d.ts +18 -70
  96. package/dist/domain/agents/model-selection.js +100 -150
  97. package/dist/domain/agents/models.d.ts +12 -0
  98. package/dist/domain/agents/models.js +94 -0
  99. package/dist/domain/agents/subagent/registry.d.ts +2 -0
  100. package/dist/domain/agents/subagent/registry.js +8 -0
  101. package/dist/domain/agents/subagent/runner.d.ts +5 -5
  102. package/dist/domain/agents/subagent/runner.js +12 -35
  103. package/dist/domain/auth/auth-status.d.ts +4 -4
  104. package/dist/domain/auth/auth-status.js +17 -28
  105. package/dist/domain/auth/gateway-sync.d.ts +6 -26
  106. package/dist/domain/auth/gateway-sync.js +31 -75
  107. package/dist/domain/benchmark/identity.d.ts +3 -5
  108. package/dist/domain/benchmark/identity.js +0 -1
  109. package/dist/domain/benchmark/production-attempt-identity.d.ts +6 -5
  110. package/dist/domain/benchmark/production-attempt-identity.js +32 -35
  111. package/dist/domain/benchmark/session-evidence.d.ts +2 -1
  112. package/dist/domain/benchmark/session-evidence.js +8 -1
  113. package/dist/domain/browser/broker.d.ts +99 -0
  114. package/dist/domain/browser/broker.js +487 -0
  115. package/dist/domain/costs/claude-account.js +21 -0
  116. package/dist/domain/costs/rate-limit-throttle.js +3 -1
  117. package/dist/domain/costs/reset-account.d.ts +2 -0
  118. package/dist/domain/costs/usage-service.d.ts +1 -1
  119. package/dist/domain/costs/usage-service.js +20 -7
  120. package/dist/domain/mcp/tools/context.d.ts +5 -6
  121. package/dist/domain/mcp/tools/context.js +17 -3
  122. package/dist/domain/mcp/tools/subagent.js +1 -1
  123. package/dist/domain/mcp/tools/ui-file.js +1 -1
  124. package/dist/domain/pi-providers/custom-provider-model.d.ts +1 -1
  125. package/dist/domain/pi-providers/custom-provider-model.js +1 -1
  126. package/dist/domain/remote/device-browser.js +1 -1
  127. package/dist/domain/remote/device-chrome-commands.js +2 -0
  128. package/dist/domain/remote/device-port.js +2 -2
  129. package/dist/domain/runs/attempt.d.ts +3 -3
  130. package/dist/domain/runs/attempt.js +2 -3
  131. package/dist/domain/runs/builders.d.ts +4 -4
  132. package/dist/domain/runs/builders.js +4 -5
  133. package/dist/domain/runs/compact.d.ts +4 -17
  134. package/dist/domain/runs/compact.js +14 -56
  135. package/dist/domain/runs/config-resolver.d.ts +15 -83
  136. package/dist/domain/runs/config-resolver.js +24 -150
  137. package/dist/domain/runs/engine-spec.d.ts +8 -8
  138. package/dist/domain/runs/engine-spec.js +15 -31
  139. package/dist/domain/runs/events.d.ts +1 -1
  140. package/dist/domain/runs/fallback.d.ts +6 -38
  141. package/dist/domain/runs/fallback.js +10 -72
  142. package/dist/domain/runs/notices.d.ts +1 -11
  143. package/dist/domain/runs/notices.js +3 -30
  144. package/dist/domain/runs/request.d.ts +4 -3
  145. package/dist/domain/runs/retryable-errors.d.ts +0 -13
  146. package/dist/domain/runs/retryable-errors.js +0 -17
  147. package/dist/domain/runs/run.d.ts +12 -42
  148. package/dist/domain/runs/run.js +40 -147
  149. package/dist/domain/runs/service.d.ts +1 -1
  150. package/dist/domain/runs/service.js +8 -3
  151. package/dist/domain/runs/spec-loader.d.ts +1 -1
  152. package/dist/domain/runs/spend.d.ts +1 -1
  153. package/dist/domain/runs/spend.js +1 -1
  154. package/dist/domain/scheduling/jobs/scheduled-task.d.ts +3 -2
  155. package/dist/domain/scheduling/jobs/scheduled-task.js +25 -19
  156. package/dist/domain/scheduling/schedule-ops.d.ts +0 -1
  157. package/dist/domain/scheduling/schedule-ops.js +2 -5
  158. package/dist/domain/scheduling/schedule-selection.d.ts +3 -0
  159. package/dist/domain/scheduling/schedule-selection.js +16 -0
  160. package/dist/domain/scheduling/schedule-spec.d.ts +6 -5
  161. package/dist/domain/scheduling/schedule-spec.js +6 -6
  162. package/dist/domain/scheduling/scheduler.d.ts +3 -1
  163. package/dist/domain/scheduling/scheduler.js +22 -21
  164. package/dist/domain/sessions/session-hooks.d.ts +5 -4
  165. package/dist/domain/sessions/session-hooks.js +15 -33
  166. package/dist/domain/sessions/session-lifecycle.d.ts +19 -51
  167. package/dist/domain/sessions/session-lifecycle.js +34 -96
  168. package/dist/domain/system/doctor.d.ts +3 -3
  169. package/dist/domain/system/doctor.js +22 -26
  170. package/dist/domain/system/own-bin-path.d.ts +13 -0
  171. package/dist/domain/system/own-bin-path.js +36 -0
  172. package/dist/domain/system/terminal-cli/index.d.ts +58 -0
  173. package/dist/domain/system/terminal-cli/index.js +100 -0
  174. package/dist/domain/system/terminal-cli/posix-profiles.d.ts +23 -0
  175. package/dist/domain/system/terminal-cli/posix-profiles.js +74 -0
  176. package/dist/domain/system/terminal-cli/registrar.d.ts +12 -0
  177. package/dist/domain/system/terminal-cli/registrar.js +1 -0
  178. package/dist/domain/system/terminal-cli/shims.d.ts +27 -0
  179. package/dist/domain/system/terminal-cli/shims.js +108 -0
  180. package/dist/domain/system/terminal-cli/state.d.ts +7 -0
  181. package/dist/domain/system/terminal-cli/state.js +36 -0
  182. package/dist/domain/system/terminal-cli/windows-user-path.d.ts +13 -0
  183. package/dist/domain/system/terminal-cli/windows-user-path.js +67 -0
  184. package/dist/domain/tui-session/tui-session-service.js +1 -1
  185. package/dist/domain/tui-session/types.d.ts +2 -2
  186. package/dist/domain/ui-service/app-router.d.ts +49 -71
  187. package/dist/domain/ui-service/app-router.js +9 -13
  188. package/dist/domain/ui-service/assistant-snapshots.js +2 -0
  189. package/dist/domain/ui-service/contract.parity.d.ts +19 -0
  190. package/dist/domain/ui-service/contract.parity.js +1 -0
  191. package/dist/domain/ui-service/inline-media-types.d.ts +18 -0
  192. package/dist/domain/ui-service/inline-media-types.js +1 -0
  193. package/dist/domain/ui-service/input-schemas.d.ts +52 -115
  194. package/dist/domain/ui-service/input-schemas.js +38 -84
  195. package/dist/domain/ui-service/mutate/auth.d.ts +1 -1
  196. package/dist/domain/ui-service/mutate/auth.js +3 -2
  197. package/dist/domain/ui-service/mutate/config.d.ts +0 -8
  198. package/dist/domain/ui-service/mutate/config.js +0 -22
  199. package/dist/domain/ui-service/mutate/models.d.ts +13 -0
  200. package/dist/domain/ui-service/mutate/models.js +137 -0
  201. package/dist/domain/ui-service/mutate/resolve-media.d.ts +4 -0
  202. package/dist/domain/ui-service/mutate/resolve-media.js +17 -0
  203. package/dist/domain/ui-service/mutate/sessions.d.ts +2 -2
  204. package/dist/domain/ui-service/mutate/sessions.js +29 -126
  205. package/dist/domain/ui-service/query/config.js +22 -42
  206. package/dist/domain/ui-service/query/models.js +2 -2
  207. package/dist/domain/ui-service/query/schedules.js +1 -1
  208. package/dist/domain/ui-service/query/sessions.js +7 -13
  209. package/dist/domain/ui-service/types.d.ts +51 -177
  210. package/dist/domain/ui-service/ui-service.d.ts +23 -49
  211. package/dist/domain/ui-service/ui-service.js +8 -6
  212. package/dist/entry/app.js +42 -19
  213. package/dist/entry/browser-cli.d.ts +39 -0
  214. package/dist/entry/browser-cli.js +317 -0
  215. package/dist/entry/cli-help.d.ts +1 -0
  216. package/dist/entry/cli-help.js +37 -6
  217. package/dist/entry/cli.js +16 -14
  218. package/dist/entry/daemon.js +2 -0
  219. package/dist/entry/init-auth.js +1 -1
  220. package/dist/entry/init.d.ts +7 -8
  221. package/dist/entry/init.js +89 -249
  222. package/dist/entry/path-cli.d.ts +4 -0
  223. package/dist/entry/path-cli.js +65 -0
  224. package/dist/entry/schedule-cli.js +13 -6
  225. package/dist/entry/shutdown.d.ts +1 -1
  226. package/dist/entry/shutdown.js +8 -1
  227. package/dist/entry/start-ui-http.js +8 -0
  228. package/dist/entry/ui-service-wiring.js +8 -10
  229. package/dist/events/event-types.d.ts +3 -2
  230. package/dist/orchestration/agent-runner.d.ts +2 -2
  231. package/dist/orchestration/agent-runner.js +10 -9
  232. package/dist/orchestration/benchmark-session-webhook.js +13 -1
  233. package/dist/orchestration/browser-webhook.d.ts +11 -0
  234. package/dist/orchestration/browser-webhook.js +28 -0
  235. package/dist/orchestration/cache-keep-warm.d.ts +77 -0
  236. package/dist/orchestration/cache-keep-warm.js +213 -0
  237. package/dist/orchestration/claude-window-warmup.d.ts +40 -0
  238. package/dist/orchestration/claude-window-warmup.js +126 -0
  239. package/dist/orchestration/conversation-request.d.ts +6 -10
  240. package/dist/orchestration/conversation-request.js +5 -23
  241. package/dist/orchestration/delta-coalescer.d.ts +4 -2
  242. package/dist/orchestration/delta-coalescer.js +5 -5
  243. package/dist/orchestration/edit-retry.js +9 -11
  244. package/dist/orchestration/inline-media-eager.d.ts +13 -0
  245. package/dist/orchestration/inline-media-eager.js +58 -0
  246. package/dist/orchestration/inline-media.d.ts +22 -0
  247. package/dist/orchestration/inline-media.js +151 -0
  248. package/dist/orchestration/interactions/ask-user-resume.js +3 -7
  249. package/dist/orchestration/interactions/interaction-handlers.js +6 -6
  250. package/dist/orchestration/mid-turn-inject.d.ts +2 -1
  251. package/dist/orchestration/mid-turn-inject.js +1 -1
  252. package/dist/orchestration/pending-injection-recovery.d.ts +2 -1
  253. package/dist/orchestration/pending-injection-recovery.js +1 -1
  254. package/dist/orchestration/routing/commands/index.js +4 -4
  255. package/dist/orchestration/routing/commands/mode.d.ts +5 -44
  256. package/dist/orchestration/routing/commands/mode.js +98 -272
  257. package/dist/orchestration/routing/commands/schedule.js +29 -18
  258. package/dist/orchestration/routing/commands/session.js +9 -9
  259. package/dist/orchestration/routing/commands/status.js +1 -1
  260. package/dist/orchestration/routing/commands/usage-format.js +3 -0
  261. package/dist/orchestration/routing/webhook.js +4 -0
  262. package/dist/orchestration/session-compact.d.ts +0 -1
  263. package/dist/orchestration/session-compact.js +5 -7
  264. package/dist/orchestration/session-events.d.ts +2 -1
  265. package/dist/orchestration/session-events.js +1 -0
  266. package/dist/orchestration/status-helpers.d.ts +0 -4
  267. package/dist/orchestration/status-helpers.js +0 -18
  268. package/dist/orchestration/subagent-webhook.js +9 -34
  269. package/dist/orchestration/team-webhook.d.ts +8 -9
  270. package/dist/orchestration/team-webhook.js +16 -42
  271. package/dist/orchestration/transcript-sink.d.ts +6 -0
  272. package/dist/orchestration/transcript-sink.js +21 -2
  273. package/dist/orchestration/turn/terminal.js +7 -7
  274. package/dist/orchestration/turn/turn-prep.d.ts +0 -8
  275. package/dist/orchestration/turn/turn-prep.js +0 -51
  276. package/dist/orchestration/turn/turn-tracking.d.ts +2 -2
  277. package/dist/orchestration/turn/turn-tracking.js +8 -5
  278. package/dist/orchestration/turn/turn.d.ts +0 -2
  279. package/dist/orchestration/turn/turn.js +13 -15
  280. package/dist/platform/adapters/tui/ports.d.ts +1 -1
  281. package/dist/platform/adapters/tui/tui-transcript.js +2 -0
  282. package/dist/platform/browser/agent-browser.d.ts +50 -0
  283. package/dist/platform/browser/agent-browser.js +165 -0
  284. package/dist/platform/browser/managed-browser.d.ts +1 -11
  285. package/dist/platform/browser/managed-browser.js +4 -12
  286. package/dist/platform/ui-http/browser-stream.d.ts +110 -0
  287. package/dist/platform/ui-http/browser-stream.js +447 -0
  288. package/dist/platform/ui-http/ui-http-server.d.ts +12 -0
  289. package/dist/platform/ui-http/ui-http-server.js +23 -6
  290. package/dist/store/conversation-history-reader.js +8 -0
  291. package/dist/store/conversation-history-repo.d.ts +8 -1
  292. package/dist/store/conversation-history-repo.js +5 -0
  293. package/dist/store/conversation-ledger-repo.d.ts +6 -6
  294. package/dist/store/conversation-ledger-repo.js +4 -4
  295. package/dist/store/models-repo.d.ts +18 -0
  296. package/dist/store/models-repo.js +74 -0
  297. package/dist/store/pending-injection-repo.d.ts +2 -1
  298. package/dist/store/pending-injection-repo.js +3 -1
  299. package/dist/store/retire-profiles-migration.d.ts +28 -0
  300. package/dist/store/retire-profiles-migration.js +335 -0
  301. package/dist/store/schedule-repo.d.ts +2 -1
  302. package/dist/store/session-registry-import.js +3 -2
  303. package/dist/store/session-registry-journal.d.ts +10 -13
  304. package/dist/store/session-registry-journal.js +48 -25
  305. package/dist/store/session-registry-repo.d.ts +4 -4
  306. package/dist/store/session-registry-repo.js +4 -5
  307. package/dist/store/version-migrations.d.ts +2 -3
  308. package/dist/store/version-migrations.js +19 -3
  309. package/dist/tui/slash-commands.js +2 -2
  310. package/dist/tui/turn-status.js +1 -1
  311. package/package.json +6 -4
  312. package/web/dist/assets/board-math-css-CB4GJ6qO.js +2 -0
  313. package/web/dist/assets/index-CaSZPg_H.css +1 -0
  314. package/web/dist/assets/index-CzOR7Edh.js +522 -0
  315. package/web/dist/assets/{index-CndZZ1IQ.js → index-cIGFv27z.js} +1 -1
  316. package/web/dist/index.html +3 -2
  317. package/web/dist/theme.css +6 -0
  318. package/dist/agent-adapter/browser-mcp-server.d.ts +0 -13
  319. package/dist/agent-adapter/browser-mcp-server.js +0 -28
  320. package/dist/agent-adapter/claude/browser-mcp.d.ts +0 -9
  321. package/dist/agent-adapter/claude/browser-mcp.js +0 -10
  322. package/dist/core/profile-generator.d.ts +0 -79
  323. package/dist/core/profile-generator.js +0 -215
  324. package/dist/domain/agents/profile-manager.d.ts +0 -103
  325. package/dist/domain/agents/profile-manager.js +0 -286
  326. package/dist/domain/agents/profile-switch.d.ts +0 -49
  327. package/dist/domain/agents/profile-switch.js +0 -62
  328. package/dist/domain/ui-service/mutate/profiles.d.ts +0 -36
  329. package/dist/domain/ui-service/mutate/profiles.js +0 -151
  330. package/dist/store/profile-repo.d.ts +0 -20
  331. package/dist/store/profile-repo.js +0 -97
  332. package/web/dist/assets/index-B76ueW3q.js +0 -494
  333. package/web/dist/assets/index-C8O0yxg8.css +0 -1
@@ -1,64 +1,19 @@
1
- import type { Backend } from '../../core/types/agent-types.js';
2
- /** A channel-scoped selection the user made on top of the channel's profile.
3
- *
4
- * The profile stays the base (it owns the gateway route, extraEnv/extraOption and the fallback
5
- * chain); these three fields are what a composer/`!model` can turn without editing profiles.json:
6
- * the model, the PI provider that model belongs to, and the thinking level.
7
- *
8
- * `backend` is deliberately NOT here: it comes from the profile and only the profile (D5), so the
9
- * "a live conversation cannot change backend" rule has exactly one implementation
10
- * (`decideProfileSwitch`). Selecting a model on another backend means switching profile. */
11
- export interface ChannelOverride {
12
- model?: string;
13
- /** PI only — changes both the request protocol and the gateway endpoint, so `mode` is re-derived
14
- * with it (see `effectiveProfile`). Never set for a claude profile. */
15
- provider?: string;
16
- /** Backend-native thinking level, validated against the channel's effective backend when set. */
17
- thinking?: string;
18
- /** Which gateway route of the profile's endpoint to bill — anthropic's `plan` (subscription) vs
19
- * `api` (metered key). Only ever one of the routes gateway.yaml declares for that endpoint; a
20
- * provider change re-derives it and drops this. */
21
- mode?: string;
22
- }
23
- /** The last engine choice made from a composer, profile included — what a brand-new conversation
24
- * starts on, so "the next session picks up where the last one left off" needs no session to copy
25
- * from. Written only by the composer's own write path: a channel-scoped `!model` in some Slack
26
- * thread is a tool for that thread, not a statement about what the next Web session should run. */
27
- export interface SelectionDefault extends ChannelOverride {
28
- profileName?: string;
29
- /** The agent — the environment — the last composer pick ran in. Written by its own pick, never
30
- * by a model/profile one: the two are independent axes and neither erases the other. */
1
+ import { type EngineSelection } from './engine-selection.js';
2
+ export type SelectionDefault = EngineSelection & {
31
3
  agentName?: string;
32
- }
4
+ };
5
+ export type ModelMemory = Record<string, {
6
+ thinking: string | null;
7
+ mode: string;
8
+ }>;
33
9
  export interface AgentState {
34
- /** Globally selected profile name. `'__active__'` is init's placeholder for "use the default". */
35
- activeProfile: string | null;
36
- /** Per-channel profile selection; beats `activeProfile` for that channel. */
37
- channelProfiles: Record<string, string>;
38
- /** Thread agent applied to plain messages when no agent is named. */
10
+ channelSelections: Record<string, EngineSelection>;
11
+ modelMemory: ModelMemory;
39
12
  defaultAgent: string | null;
40
- /** Per-channel agent selection; beats `defaultAgent` for that channel. The agent is the
41
- * conversation's ENVIRONMENT (prompt, tools, skills, rules), the twin of `channelProfiles`
42
- * on the model axis — which is why the two maps are separate and neither writes the other. */
43
13
  channelAgents: Record<string, string>;
44
- /** Per-channel knobs layered on top of the resolved profile. */
45
- channelOverrides: Record<string, ChannelOverride>;
46
- /** Seed for the next conversation's composer — see {@link SelectionDefault}. */
47
14
  selectionDefault?: SelectionDefault;
48
- /** @deprecated Read the resolved profile's backend. */
49
- backend?: Backend;
50
- /** @deprecated Read the resolved profile's mode. */
51
- claudeMode?: string;
52
- /** @deprecated Read the resolved profile's model. */
53
- claudeModel?: string;
54
15
  }
55
- /**
56
- * Read the state, migrating `mode.json` on the boot that first finds it.
57
- *
58
- * The migration is a rename, not a delete: `mode.json.bak` is what the user gets back if this
59
- * build is rolled back, and it is also the evidence that the migration happened at all. An
60
- * existing `agent-state.json` always wins — a second migration could only overwrite newer state
61
- * with older.
62
- */
16
+ /** Migration owns legacy data. Runtime reads only concrete selections and never rewrites on read. */
63
17
  export declare function loadAgentState(): AgentState;
18
+ /** Replace atomically so interruption cannot leave a truncated selection or model memory. */
64
19
  export declare function saveAgentState(state: AgentState): void;
@@ -1,140 +1,73 @@
1
- import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
1
+ import { readFileSync, renameSync, writeFileSync } from 'node:fs';
2
2
  import * as path from 'node:path';
3
3
  import { STORE_DIR } from '../../core/paths.js';
4
- import { createLogger } from '../../core/log.js';
5
- const log = createLogger('agent-state');
4
+ import { copySelection, validateEngineSelection } from './engine-selection.js';
6
5
  const STATE_FILE = path.join(STORE_DIR, 'agent-state.json');
7
- const LEGACY_FILE = path.join(STORE_DIR, 'mode.json');
8
- function emptyState() {
9
- return {
10
- activeProfile: null, channelProfiles: {}, defaultAgent: null, channelAgents: {}, channelOverrides: {},
11
- };
12
- }
13
6
  function record(value) {
14
7
  return value && typeof value === 'object' && !Array.isArray(value)
15
- ? value
16
- : {};
8
+ ? value : {};
17
9
  }
18
- function stringMap(value) {
19
- const out = {};
20
- for (const [key, entry] of Object.entries(record(value))) {
21
- if (typeof entry === 'string' && entry)
22
- out[key] = entry;
10
+ function parseSelection(value) {
11
+ try {
12
+ validateEngineSelection(value);
13
+ return copySelection(value);
23
14
  }
24
- return out;
25
- }
26
- const OVERRIDE_FIELDS = ['model', 'provider', 'thinking', 'mode'];
27
- function parseOverride(value) {
28
- const raw = record(value);
29
- const override = {};
30
- for (const field of OVERRIDE_FIELDS) {
31
- const candidate = raw[field];
32
- if (typeof candidate === 'string' && candidate)
33
- override[field] = candidate;
15
+ catch {
16
+ return null;
34
17
  }
35
- return override;
36
18
  }
37
- function overrideMap(value) {
38
- const out = {};
39
- for (const [key, entry] of Object.entries(record(value))) {
40
- const override = parseOverride(entry);
41
- // An entry with no usable field is not an override — dropping it keeps the map's presence
42
- // meaningful ("this channel selected something") for every reader.
43
- if (Object.keys(override).length > 0)
44
- out[key] = override;
45
- }
46
- return out;
19
+ function parseMap(value, parse) {
20
+ const entries = Object.entries(record(value)).map(([key, entry]) => [key, parse(entry)]);
21
+ return Object.fromEntries(entries.filter(([, entry]) => entry !== null));
47
22
  }
48
- function parseSelectionDefault(value) {
49
- if (!value)
50
- return undefined;
51
- const seed = parseOverride(value);
52
- const profileName = record(value).profileName;
53
- if (typeof profileName === 'string' && profileName)
54
- seed.profileName = profileName;
55
- const agentName = record(value).agentName;
56
- if (typeof agentName === 'string' && agentName)
57
- seed.agentName = agentName;
58
- return Object.keys(seed).length > 0 ? seed : undefined;
23
+ function parseMemory(value) {
24
+ const raw = record(value);
25
+ const validThinking = raw.thinking === null || (typeof raw.thinking === 'string' && !!raw.thinking);
26
+ if (!validThinking || typeof raw.mode !== 'string' || !raw.mode)
27
+ return null;
28
+ return { thinking: raw.thinking, mode: raw.mode };
59
29
  }
60
- /** Parse either file shape into one state. Unknown and malformed fields collapse to the empty
61
- * value rather than throwing: a corrupt selection must not stop the daemon from starting, and
62
- * every field has a defensible default. */
63
- function parseState(raw) {
64
- const data = record(raw);
65
- const state = emptyState();
66
- if (typeof data.activeProfile === 'string' && data.activeProfile)
67
- state.activeProfile = data.activeProfile;
68
- state.channelProfiles = stringMap(data.channelProfiles);
69
- if (typeof data.defaultAgent === 'string' && data.defaultAgent)
70
- state.defaultAgent = data.defaultAgent;
71
- state.channelAgents = stringMap(data.channelAgents);
72
- state.channelOverrides = overrideMap(data.channelOverrides);
73
- // Assigned only when there is one: an explicit `selectionDefault: undefined` key is still a key,
74
- // and every caller here compares whole states.
75
- const seed = parseSelectionDefault(data.selectionDefault);
76
- if (seed)
77
- state.selectionDefault = seed;
78
- if (data.backend === 'claude' || data.backend === 'pi')
79
- state.backend = data.backend;
80
- // mode.json wrote the Claude mode twice — `claudeMode`, and `mode` when the backend was claude.
81
- const legacyMode = typeof data.claudeMode === 'string' && data.claudeMode
82
- ? data.claudeMode
83
- : (data.backend === 'claude' && typeof data.mode === 'string' ? data.mode : undefined);
84
- if (legacyMode)
85
- state.claudeMode = legacyMode === 'plan' ? 'plan' : 'api';
86
- if (typeof data.claudeModel === 'string' && data.claudeModel)
87
- state.claudeModel = data.claudeModel;
88
- return state;
30
+ function parseString(value) {
31
+ return typeof value === 'string' && value ? value : null;
89
32
  }
90
- function readJson(file) {
91
- try {
92
- return JSON.parse(readFileSync(file, 'utf8'));
93
- }
94
- catch {
95
- return null;
33
+ function parseState(value) {
34
+ const raw = record(value);
35
+ const state = {
36
+ channelSelections: parseMap(raw.channelSelections, parseSelection),
37
+ modelMemory: parseMap(raw.modelMemory, parseMemory),
38
+ defaultAgent: parseString(raw.defaultAgent),
39
+ channelAgents: parseMap(raw.channelAgents, parseString),
40
+ };
41
+ const seed = parseSelection(raw.selectionDefault);
42
+ if (seed) {
43
+ state.selectionDefault = seed;
44
+ const agentName = parseString(record(raw.selectionDefault).agentName);
45
+ if (agentName)
46
+ state.selectionDefault.agentName = agentName;
96
47
  }
48
+ return state;
97
49
  }
98
- /**
99
- * Read the state, migrating `mode.json` on the boot that first finds it.
100
- *
101
- * The migration is a rename, not a delete: `mode.json.bak` is what the user gets back if this
102
- * build is rolled back, and it is also the evidence that the migration happened at all. An
103
- * existing `agent-state.json` always wins — a second migration could only overwrite newer state
104
- * with older.
105
- */
50
+ /** Migration owns legacy data. Runtime reads only concrete selections and never rewrites on read. */
106
51
  export function loadAgentState() {
107
- if (existsSync(STATE_FILE))
108
- return parseState(readJson(STATE_FILE));
109
- if (!existsSync(LEGACY_FILE))
110
- return emptyState();
111
- const migrated = parseState(readJson(LEGACY_FILE));
112
52
  try {
113
- writeFileSync(STATE_FILE, JSON.stringify(migrated));
114
- renameSync(LEGACY_FILE, `${LEGACY_FILE}.bak`);
115
- log.info(`Migrated mode.json → agent-state.json (previous file kept as ${path.basename(LEGACY_FILE)}.bak)`);
53
+ return parseState(JSON.parse(readFileSync(STATE_FILE, 'utf8')));
116
54
  }
117
- catch (error) {
118
- // A read-only or full disk must not cost the user their selection for this run; the next boot
119
- // tries again, and until then the in-memory state is the migrated one either way.
120
- log.warn(`Could not persist the agent-state migration: ${error.message}`);
55
+ catch {
56
+ return parseState(null);
121
57
  }
122
- return migrated;
123
58
  }
59
+ /** Replace atomically so interruption cannot leave a truncated selection or model memory. */
124
60
  export function saveAgentState(state) {
125
61
  const data = {};
126
- if (state.activeProfile)
127
- data.activeProfile = state.activeProfile;
128
- if (Object.keys(state.channelProfiles).length > 0)
129
- data.channelProfiles = state.channelProfiles;
62
+ for (const field of ['channelSelections', 'modelMemory', 'channelAgents']) {
63
+ if (Object.keys(state[field]).length)
64
+ data[field] = state[field];
65
+ }
130
66
  if (state.defaultAgent)
131
67
  data.defaultAgent = state.defaultAgent;
132
- if (Object.keys(state.channelAgents).length > 0)
133
- data.channelAgents = state.channelAgents;
134
- if (Object.keys(state.channelOverrides).length > 0)
135
- data.channelOverrides = state.channelOverrides;
136
- if (state.selectionDefault && Object.keys(state.selectionDefault).length > 0) {
68
+ if (state.selectionDefault)
137
69
  data.selectionDefault = state.selectionDefault;
138
- }
139
- writeFileSync(STATE_FILE, JSON.stringify(data));
70
+ const temporary = `${STATE_FILE}.tmp`;
71
+ writeFileSync(temporary, JSON.stringify(data));
72
+ renameSync(temporary, STATE_FILE);
140
73
  }
@@ -1,38 +1,16 @@
1
- export type SwitchAgentFailure = 'unknown-agent' | 'cross-backend-live-session';
1
+ export type SwitchAgentFailure = 'unknown-agent';
2
2
  export interface SwitchAgentSuccess {
3
3
  ok: true;
4
- /** The agent the channel now runs. */
5
4
  agentName: string;
6
- /** The profile that agent will actually run under — its own when it pins one, otherwise the
7
- * channel's. FOR DISPLAY ONLY: the run path re-resolves it every turn, and this rule never
8
- * writes `channelProfiles` (a pinned profile is already an override there). */
9
- effectiveProfile: string;
10
- /** True when the switch moved to a different backend (only possible on a fresh session). */
11
- backendChanged: boolean;
12
5
  }
13
6
  export interface SwitchAgentRefusal {
14
7
  ok: false;
15
8
  reason: SwitchAgentFailure;
16
- /** The channel's effective backend before the switch; absent when the agent was unknown. */
17
- currentBackend?: string;
18
- /** The backend the agent would have run on; absent when the agent was unknown. */
19
- targetBackend?: string;
20
9
  }
21
10
  export type SwitchAgentResult = SwitchAgentSuccess | SwitchAgentRefusal;
22
- /**
23
- * Switch the agent for a channel/session under the shared switch rule.
24
- *
25
- * Takes effect on the NEXT turn, like `!model`: the EngineSpec changes identity, so `SessionEngines`
26
- * restarts the process with `--resume` and the conversation keeps its history under the new prompt
27
- * and tool surface. Nothing is reset.
28
- *
29
- * The cross-backend refusal is {@link decideProfileSwitch}, unchanged — an agent that pins a
30
- * profile on the other backend is a backend move wearing a different hat, and a live conversation
31
- * cannot be resumed there.
32
- */
11
+ /** An environment pick changes prompt/tools, never the engine or its conversation history. */
33
12
  export declare function switchChannelAgent(opts: {
34
13
  channel: string;
35
14
  name: string;
36
15
  }): Promise<SwitchAgentResult>;
37
- /** Drop the channel's agent selection: it follows the global default again, from the next turn. */
38
16
  export declare function clearChannelAgentSelection(channel: string): Promise<void>;
@@ -1,93 +1,30 @@
1
1
  import { sessionStore } from '../../store/session-registry-repo.js';
2
- import { clearChannelAgent, getActiveProfile, resolveBackendForChannel, setDefaultAgent, setSelectionDefaultAgent, } from './config.js';
3
- import { resolveProfileConfig } from './profile-manager.js';
4
- import { channelHasHistory, decideProfileSwitch } from './profile-switch.js';
2
+ import { clearChannelAgent, setDefaultAgent, setSelectionDefaultAgent } from './config.js';
5
3
  import { getEnvironment } from './environments/registry.js';
6
- /**
7
- * The backend an agent runs on for a given channel: its own profile's when it pins one, else the
8
- * channel's — an `__active__` agent follows whatever the channel already resolves to, so it can
9
- * never move the backend by itself.
10
- */
11
- function targetBackendOf(agentProfile, currentBackend) {
12
- if (agentProfile === '__active__')
13
- return currentBackend;
14
- try {
15
- return resolveProfileConfig(agentProfile).backend;
16
- }
17
- catch {
18
- // A pinned profile that no longer exists cannot state a backend; the channel's own answer is
19
- // the honest one, and the run path reports the bad name when the turn opens.
20
- return currentBackend;
21
- }
22
- }
23
- /**
24
- * Switch the agent for a channel/session under the shared switch rule.
25
- *
26
- * Takes effect on the NEXT turn, like `!model`: the EngineSpec changes identity, so `SessionEngines`
27
- * restarts the process with `--resume` and the conversation keeps its history under the new prompt
28
- * and tool surface. Nothing is reset.
29
- *
30
- * The cross-backend refusal is {@link decideProfileSwitch}, unchanged — an agent that pins a
31
- * profile on the other backend is a backend move wearing a different hat, and a live conversation
32
- * cannot be resumed there.
33
- */
4
+ /** An environment pick changes prompt/tools, never the engine or its conversation history. */
34
5
  export async function switchChannelAgent(opts) {
35
- const { channel, name } = opts;
36
- const agent = getEnvironment(name);
37
- if (!agent)
6
+ if (!getEnvironment(opts.name))
38
7
  return { ok: false, reason: 'unknown-agent' };
39
- const currentBackend = resolveBackendForChannel(channel);
40
- const targetBackend = targetBackendOf(agent.profile, currentBackend);
41
- const decision = decideProfileSwitch({
42
- currentBackend, targetBackend, hasHistory: await channelHasHistory(channel),
43
- });
44
- if (!decision.allowed) {
45
- return { ok: false, reason: 'cross-backend-live-session', currentBackend, targetBackend };
46
- }
47
- setDefaultAgent(name, channel);
48
- seedNextConversation(channel, name);
49
- await syncSessionAgent(channel, name);
50
- return {
51
- ok: true,
52
- agentName: name,
53
- effectiveProfile: agent.profile === '__active__' ? resolveChannelProfileName(channel) : agent.profile,
54
- backendChanged: decision.backendChanged,
55
- };
8
+ setDefaultAgent(opts.name, opts.channel);
9
+ seedNextConversation(opts.channel, opts.name);
10
+ await syncSessionAgent(opts.channel, opts.name);
11
+ return { ok: true, agentName: opts.name };
56
12
  }
57
- /** Drop the channel's agent selection: it follows the global default again, from the next turn. */
58
13
  export async function clearChannelAgentSelection(channel) {
59
14
  clearChannelAgent(channel);
60
15
  seedNextConversation(channel, null);
61
16
  await syncSessionAgent(channel, null);
62
17
  }
63
- /** A pick made in the app says how the user wants to work, so the NEXT new conversation opens in
64
- * the same environment. Direct (`web:`) channels only, for the reason `applyChannelSelection`
65
- * gives: an `!agent` in some Slack thread is a tool for that thread. */
66
18
  function seedNextConversation(channel, agentName) {
67
19
  if (channel.startsWith('web:'))
68
20
  setSelectionDefaultAgent(agentName);
69
21
  }
70
- /** Keep the session-registry record in lock-step (best-effort), so the Web session list and
71
- * `!resume` report the agent the channel is on. The channel map is the runtime source of truth;
72
- * a failure here costs a label, not the switch. */
22
+ /** The engine is not touched; a failed label sync does not discard the environment pick. */
73
23
  async function syncSessionAgent(channel, agentName) {
74
24
  try {
75
25
  const session = await sessionStore.getById((await sessionStore.getBoundSessionId(channel)) ?? '');
76
26
  if (session)
77
27
  await sessionStore.updateSession(session.sessionId, { agentName });
78
28
  }
79
- catch {
80
- // non-fatal — see above
81
- }
82
- }
83
- /** The profile name an `__active__` agent displays: whatever the channel itself resolves to. */
84
- function resolveChannelProfileName(channel) {
85
- try {
86
- return resolveProfileConfig(getActiveProfile(channel)).name;
87
- }
88
- catch {
89
- // No profile resolvable at all (profiles.json unreadable) — say what the agent declares rather
90
- // than inventing a name the run would not use.
91
- return '__active__';
92
- }
29
+ catch { /* The channel's environment selection is already persisted. */ }
93
30
  }
@@ -1,70 +1,20 @@
1
- import { type ChannelOverride, type SelectionDefault } from './agent-state.js';
2
- import type { Backend } from '../../agent-adapter/types.js';
3
- export declare function getActiveProfile(channel?: string): string | null;
4
- /** The Claude model a channel with no selection of its own runs — the default profile's (D5) —
5
- * else the stock alias when that profile is not a Claude one or cannot be read. */
1
+ import { type ModelMemory, type SelectionDefault } from './agent-state.js';
2
+ import { type EngineSelection } from './engine-selection.js';
3
+ import type { Backend } from '../../core/types/agent-types.js';
4
+ /** Startup migrations run after static imports and may replace the persisted state. */
5
+ export declare function reloadAgentState(): void;
6
+ export declare function getChannelSelection(channel?: string): EngineSelection | null;
7
+ export declare function setChannelSelection(channel: string, selection: EngineSelection | null): void;
8
+ export declare function clearChannelSelection(channel: string): void;
9
+ export declare function getModelMemory(): ModelMemory;
10
+ export declare function rememberModelSelection(selection: EngineSelection): void;
6
11
  export declare function defaultClaudeModel(): string;
7
- /**
8
- * The backend a channel runs: its profile's, falling back to the default profile's.
9
- *
10
- * DEVIATION from plan D5, which has this "degenerate into reading the session record". That was
11
- * the right move while the session store keyed on `backend:channel` and the caller had to know the
12
- * backend to find the session at all — but the backend is no longer part of the key, so no session
13
- * lookup needs this any more. What is left are callers asking which backend a channel runs
14
- * (conversation ledger, rollback, compaction support), and for them the profile is the source of
15
- * truth, not a record of what some earlier session happened to use. Reading the record would also
16
- * make this async and ripple through fifteen synchronous call sites for a worse answer.
17
- *
18
- * Equivalent to `resolveRunBackend({ channel })` minus the session/override layers. The two cannot
19
- * be folded together here: `config-resolver` imports this module, so doing so would be an import
20
- * cycle.
21
- */
22
12
  export declare function resolveBackendForChannel(channel?: string): Backend;
23
- export declare function setActiveProfile(profileName: string | null, channel?: string): void;
24
- export declare function clearChannelProfile(channel: string): void;
25
- /** The channel's whole selection on top of its profile (model / provider / thinking). null when the
26
- * channel runs its profile as declared, which is the normal case. */
27
- export declare function getChannelOverride(channel?: string | null): ChannelOverride | null;
28
- /**
29
- * Patch the channel's selection. A field set to `null` is cleared, a field left out is untouched,
30
- * and an override with nothing left in it is removed entirely (so "has a selection" stays a
31
- * truthful question to ask of the map).
32
- *
33
- * Takes effect on the next turn — a live turn already has its EngineSpec.
34
- */
35
- export declare function setChannelOverride(channel: string, patch: Partial<Record<keyof ChannelOverride, string | null>>): void;
36
- /**
37
- * Remember a composer's pick as the seed a brand-new conversation starts on.
38
- *
39
- * Written for direct (`web:`) channels only, by `applyChannelSelection` — never for a platform
40
- * channel, and never for `!model` / `!thinking`, which pass `seedDefault: false`. Those are
41
- * channel-scoped tools; letting one of them decide what the next Web session runs would be action
42
- * at a distance. Clearing back to the profile IS a choice and is remembered as one — the
43
- * seed then carries a profile and no overrides.
44
- */
13
+ /** Engine and agent picks are independent; a model pick preserves the environment seed. */
45
14
  export declare function setSelectionDefault(seed: SelectionDefault | null): void;
46
- /** Remember the composer's AGENT pick in the same seed, leaving the engine half alone. A null name
47
- * records "the next conversation opens on the global default", which is a choice like any other. */
48
15
  export declare function setSelectionDefaultAgent(agentName: string | null): void;
49
- /** The seed for the next conversation, or null when nothing has been picked on this host yet. */
50
16
  export declare function getSelectionDefault(): SelectionDefault | null;
51
- /** Drop every override the channel carries — what an explicit "run this profile as declared"
52
- * (a profile switch) means. */
53
- export declare function clearChannelOverride(channel: string): void;
54
- /**
55
- * The agent a channel runs: its own selection, else the global default. Null when neither is set
56
- * and the caller's own fallback applies.
57
- *
58
- * The channel layer is what makes an agent a per-conversation choice rather than a daemon-wide
59
- * one — the same shape `getActiveProfile` has, and deliberately so: profile and agent are two
60
- * independent axes (which model vs which environment) and each has its own channel map.
61
- */
62
17
  export declare function getDefaultAgent(channel?: string): string | null;
63
- /** Set the agent for one channel (`channel` given) or the global default (omitted). A null name
64
- * clears: for a channel that means "follow the global default again", globally it means "no
65
- * default agent at all". */
66
18
  export declare function setDefaultAgent(name: string | null, channel?: string): void;
67
- /** Drop the channel's agent selection — it follows the global default again. */
68
19
  export declare function clearChannelAgent(channel: string): void;
69
- /** Every channel that has selected an agent of its own. */
70
20
  export declare function getChannelAgents(): Record<string, string>;