synabun 2.2.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (365) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/README.md +1 -1
  3. package/docs/whatsapp.md +52 -20
  4. package/hooks/claude-code/pre-websearch.mjs +4 -1
  5. package/hooks/claude-code/prompt-submit.mjs +7 -0
  6. package/lib/mcp-build.js +519 -0
  7. package/lib/mcp-dist.js +10 -4
  8. package/lib/release-scan.mjs +23 -10
  9. package/lib/server-supervisor.js +7 -1
  10. package/lib/virustotal.mjs +146 -40
  11. package/mcp-server/dist/http.d.ts +57 -0
  12. package/mcp-server/dist/http.d.ts.map +1 -1
  13. package/mcp-server/dist/http.js +128 -18
  14. package/mcp-server/dist/http.js.map +1 -1
  15. package/mcp-server/dist/index.d.ts.map +1 -1
  16. package/mcp-server/dist/index.js +14 -9
  17. package/mcp-server/dist/index.js.map +1 -1
  18. package/mcp-server/dist/services/browser-route-flow.d.ts +119 -0
  19. package/mcp-server/dist/services/browser-route-flow.d.ts.map +1 -0
  20. package/mcp-server/dist/services/browser-route-flow.js +340 -0
  21. package/mcp-server/dist/services/browser-route-flow.js.map +1 -0
  22. package/mcp-server/dist/services/desktop-client.js +2 -2
  23. package/mcp-server/dist/services/desktop-client.js.map +1 -1
  24. package/mcp-server/dist/services/embedding-service.d.ts.map +1 -1
  25. package/mcp-server/dist/services/embedding-service.js +2 -1
  26. package/mcp-server/dist/services/embedding-service.js.map +1 -1
  27. package/mcp-server/dist/services/identity.d.ts +66 -3
  28. package/mcp-server/dist/services/identity.d.ts.map +1 -1
  29. package/mcp-server/dist/services/identity.js +61 -4
  30. package/mcp-server/dist/services/identity.js.map +1 -1
  31. package/mcp-server/dist/services/neural-interface.d.ts +133 -1
  32. package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
  33. package/mcp-server/dist/services/neural-interface.js +630 -64
  34. package/mcp-server/dist/services/neural-interface.js.map +1 -1
  35. package/mcp-server/dist/services/tool-catalog.js +2 -2
  36. package/mcp-server/dist/services/tool-catalog.js.map +1 -1
  37. package/mcp-server/dist/tools/agents.d.ts +3 -2
  38. package/mcp-server/dist/tools/agents.d.ts.map +1 -1
  39. package/mcp-server/dist/tools/agents.js +6 -4
  40. package/mcp-server/dist/tools/agents.js.map +1 -1
  41. package/mcp-server/dist/tools/bluesky.d.ts +20 -7
  42. package/mcp-server/dist/tools/bluesky.d.ts.map +1 -1
  43. package/mcp-server/dist/tools/bluesky.js +147 -45
  44. package/mcp-server/dist/tools/bluesky.js.map +1 -1
  45. package/mcp-server/dist/tools/browser-advanced.d.ts +13 -2
  46. package/mcp-server/dist/tools/browser-advanced.d.ts.map +1 -1
  47. package/mcp-server/dist/tools/browser-advanced.js +153 -18
  48. package/mcp-server/dist/tools/browser-advanced.js.map +1 -1
  49. package/mcp-server/dist/tools/browser-cheatsheet.d.ts +2 -0
  50. package/mcp-server/dist/tools/browser-cheatsheet.d.ts.map +1 -1
  51. package/mcp-server/dist/tools/browser-cheatsheet.js +3 -0
  52. package/mcp-server/dist/tools/browser-cheatsheet.js.map +1 -1
  53. package/mcp-server/dist/tools/browser-navigate.d.ts +3 -3
  54. package/mcp-server/dist/tools/browser-navigate.d.ts.map +1 -1
  55. package/mcp-server/dist/tools/browser-navigate.js +19 -16
  56. package/mcp-server/dist/tools/browser-navigate.js.map +1 -1
  57. package/mcp-server/dist/tools/browser-observe.d.ts +2 -1
  58. package/mcp-server/dist/tools/browser-observe.d.ts.map +1 -1
  59. package/mcp-server/dist/tools/browser-observe.js +61 -20
  60. package/mcp-server/dist/tools/browser-observe.js.map +1 -1
  61. package/mcp-server/dist/tools/morelogin.d.ts +13 -3
  62. package/mcp-server/dist/tools/morelogin.d.ts.map +1 -1
  63. package/mcp-server/dist/tools/morelogin.js +82 -19
  64. package/mcp-server/dist/tools/morelogin.js.map +1 -1
  65. package/mcp-server/dist/tools/skill-tools.d.ts +4 -4
  66. package/mcp-server/dist/tools/youtube/helpers.d.ts +1 -0
  67. package/mcp-server/dist/tools/youtube/helpers.d.ts.map +1 -1
  68. package/mcp-server/dist/tools/youtube/helpers.js +1 -1
  69. package/mcp-server/dist/tools/youtube/helpers.js.map +1 -1
  70. package/mcp-server/dist/tools/youtube/upload.d.ts.map +1 -1
  71. package/mcp-server/dist/tools/youtube/upload.js +225 -88
  72. package/mcp-server/dist/tools/youtube/upload.js.map +1 -1
  73. package/mcp-server/package-lock.json +2 -2
  74. package/mcp-server/package.json +1 -1
  75. package/mcp-server/src/http.ts +135 -17
  76. package/mcp-server/src/index.ts +15 -9
  77. package/mcp-server/src/services/browser-route-flow.ts +403 -0
  78. package/mcp-server/src/services/desktop-client.ts +2 -2
  79. package/mcp-server/src/services/embedding-service.ts +2 -1
  80. package/mcp-server/src/services/identity.ts +80 -4
  81. package/mcp-server/src/services/neural-interface.ts +648 -73
  82. package/mcp-server/src/services/tool-catalog.ts +2 -2
  83. package/mcp-server/src/tools/agents.ts +8 -4
  84. package/mcp-server/src/tools/bluesky.ts +164 -51
  85. package/mcp-server/src/tools/browser-advanced.ts +142 -17
  86. package/mcp-server/src/tools/browser-cheatsheet.ts +3 -0
  87. package/mcp-server/src/tools/browser-navigate.ts +19 -16
  88. package/mcp-server/src/tools/browser-observe.ts +62 -20
  89. package/mcp-server/src/tools/morelogin.ts +82 -19
  90. package/mcp-server/src/tools/youtube/helpers.ts +2 -2
  91. package/mcp-server/src/tools/youtube/upload.ts +218 -99
  92. package/neural-interface/i18n/en.json +655 -123
  93. package/neural-interface/i18n/pt-BR.json +542 -10
  94. package/neural-interface/lib/assistant-api.js +90 -23
  95. package/neural-interface/lib/assistant-brains/claude.js +5 -0
  96. package/neural-interface/lib/assistant-catalog.js +134 -3
  97. package/neural-interface/lib/assistant-dispatch.js +97 -1
  98. package/neural-interface/lib/assistant-persona.js +15 -7
  99. package/neural-interface/lib/assistant-router.js +6 -4
  100. package/neural-interface/lib/assistant-runtime.js +18 -1
  101. package/neural-interface/lib/assistant-task-prompt.js +5 -0
  102. package/neural-interface/lib/assistant-tools.js +10 -1
  103. package/neural-interface/lib/automation-approval.js +249 -0
  104. package/neural-interface/lib/automation-browser-owner-access.js +82 -0
  105. package/neural-interface/lib/backup-service.js +16 -1
  106. package/neural-interface/lib/browser-automation-routes.js +84 -0
  107. package/neural-interface/lib/browser-background-tab.js +72 -3
  108. package/neural-interface/lib/browser-execution.js +16 -3
  109. package/neural-interface/lib/browser-extraction.js +106 -45
  110. package/neural-interface/lib/browser-owner.js +48 -0
  111. package/neural-interface/lib/browser-profile-choices.js +143 -0
  112. package/neural-interface/lib/browser-profiles.js +142 -0
  113. package/neural-interface/lib/browser-session-close.js +84 -0
  114. package/neural-interface/lib/browser-start-handoff.js +202 -0
  115. package/neural-interface/lib/browser-tab-recovery.js +153 -0
  116. package/neural-interface/lib/browser-tool-policy.js +23 -4
  117. package/neural-interface/lib/browser-wedge-retire.js +64 -0
  118. package/neural-interface/lib/chromium-on-device-model.js +67 -0
  119. package/neural-interface/lib/claude-agent-bridge.js +43 -5
  120. package/neural-interface/lib/claude-background-hold.js +239 -0
  121. package/neural-interface/lib/claude-model-catalog.js +12 -0
  122. package/neural-interface/lib/effort-levels.js +28 -2
  123. package/neural-interface/lib/exec-loop-command.js +8 -0
  124. package/neural-interface/lib/http-guards.js +11 -6
  125. package/neural-interface/lib/local-profile-proxy.js +362 -0
  126. package/neural-interface/lib/local-profiles.js +929 -0
  127. package/neural-interface/lib/loop-logger.js +31 -5
  128. package/neural-interface/lib/maritaca/api.js +179 -0
  129. package/neural-interface/lib/maritaca/attachments.js +44 -0
  130. package/neural-interface/lib/maritaca/client.js +362 -0
  131. package/neural-interface/lib/maritaca/key-store.js +122 -0
  132. package/neural-interface/lib/maritaca/models.js +181 -0
  133. package/neural-interface/lib/maritaca/modes.js +39 -0
  134. package/neural-interface/lib/maritaca/opencode-proxy.js +236 -0
  135. package/neural-interface/lib/maritaca/opencode-stream.js +345 -0
  136. package/neural-interface/lib/maritaca/opencode.js +218 -0
  137. package/neural-interface/lib/maritaca/panel-bridge.js +273 -0
  138. package/neural-interface/lib/maritaca/sessions.js +198 -0
  139. package/neural-interface/lib/mcp-external-servers.js +37 -10
  140. package/neural-interface/lib/morelogin-agent-profiles.js +149 -0
  141. package/neural-interface/lib/morelogin.js +278 -14
  142. package/neural-interface/lib/native-loop-providers.js +124 -11
  143. package/neural-interface/lib/native-loop-runtime.js +112 -11
  144. package/neural-interface/lib/owner-authority.js +137 -43
  145. package/neural-interface/lib/page-session.js +111 -27
  146. package/neural-interface/lib/peer-identity.js +263 -43
  147. package/neural-interface/lib/provider-panel-api-key-auth.js +7 -2
  148. package/neural-interface/lib/pty-host/core.js +36 -0
  149. package/neural-interface/lib/pty-host/protocol.js +2 -0
  150. package/neural-interface/lib/request-gate.js +2 -0
  151. package/neural-interface/lib/skills/api-v2.js +13 -2
  152. package/neural-interface/lib/skills/creation-api.js +3 -1
  153. package/neural-interface/lib/skills/evals-api.js +5 -1
  154. package/neural-interface/lib/social-write-pacing.js +151 -0
  155. package/neural-interface/lib/system-activity.js +1 -1
  156. package/neural-interface/lib/terminal-token-redaction.js +293 -0
  157. package/neural-interface/lib/terminal-tokens.js +436 -0
  158. package/neural-interface/lib/worker-mcp-channel.js +45 -10
  159. package/neural-interface/package-lock.json +3 -2
  160. package/neural-interface/package.json +2 -1
  161. package/neural-interface/public/assets/mascot/README.md +11 -0
  162. package/neural-interface/public/assets/mascot/ask.gif +0 -0
  163. package/neural-interface/public/assets/mascot/ask.png +0 -0
  164. package/neural-interface/public/assets/mascot/blocked.gif +0 -0
  165. package/neural-interface/public/assets/mascot/blocked.png +0 -0
  166. package/neural-interface/public/assets/mascot/budget.gif +0 -0
  167. package/neural-interface/public/assets/mascot/budget.png +0 -0
  168. package/neural-interface/public/assets/mascot/computer.gif +0 -0
  169. package/neural-interface/public/assets/mascot/computer.png +0 -0
  170. package/neural-interface/public/assets/mascot/edit.gif +0 -0
  171. package/neural-interface/public/assets/mascot/edit.png +0 -0
  172. package/neural-interface/public/assets/mascot/error.gif +0 -0
  173. package/neural-interface/public/assets/mascot/error.png +0 -0
  174. package/neural-interface/public/assets/mascot/hover-attention.gif +0 -0
  175. package/neural-interface/public/assets/mascot/hover-attention.png +0 -0
  176. package/neural-interface/public/assets/mascot/idle-hero.gif +0 -0
  177. package/neural-interface/public/assets/mascot/idle-hero.png +0 -0
  178. package/neural-interface/public/assets/mascot/idle.gif +0 -0
  179. package/neural-interface/public/assets/mascot/idle.png +0 -0
  180. package/neural-interface/public/assets/mascot/manifest.json +254 -0
  181. package/neural-interface/public/assets/mascot/mcp.gif +0 -0
  182. package/neural-interface/public/assets/mascot/mcp.png +0 -0
  183. package/neural-interface/public/assets/mascot/memory.gif +0 -0
  184. package/neural-interface/public/assets/mascot/memory.png +0 -0
  185. package/neural-interface/public/assets/mascot/offline.gif +0 -0
  186. package/neural-interface/public/assets/mascot/offline.png +0 -0
  187. package/neural-interface/public/assets/mascot/pick.gif +0 -0
  188. package/neural-interface/public/assets/mascot/pick.png +0 -0
  189. package/neural-interface/public/assets/mascot/plan.gif +0 -0
  190. package/neural-interface/public/assets/mascot/plan.png +0 -0
  191. package/neural-interface/public/assets/mascot/present.gif +0 -0
  192. package/neural-interface/public/assets/mascot/present.png +0 -0
  193. package/neural-interface/public/assets/mascot/project.gif +0 -0
  194. package/neural-interface/public/assets/mascot/project.png +0 -0
  195. package/neural-interface/public/assets/mascot/read.gif +0 -0
  196. package/neural-interface/public/assets/mascot/read.png +0 -0
  197. package/neural-interface/public/assets/mascot/runs.gif +0 -0
  198. package/neural-interface/public/assets/mascot/runs.png +0 -0
  199. package/neural-interface/public/assets/mascot/scenes/automation.gif +0 -0
  200. package/neural-interface/public/assets/mascot/scenes/automation.png +0 -0
  201. package/neural-interface/public/assets/mascot/scenes/browser.gif +0 -0
  202. package/neural-interface/public/assets/mascot/scenes/browser.png +0 -0
  203. package/neural-interface/public/assets/mascot/scenes/code.gif +0 -0
  204. package/neural-interface/public/assets/mascot/scenes/code.png +0 -0
  205. package/neural-interface/public/assets/mascot/scenes/complex.gif +0 -0
  206. package/neural-interface/public/assets/mascot/scenes/complex.png +0 -0
  207. package/neural-interface/public/assets/mascot/scenes/computer.gif +0 -0
  208. package/neural-interface/public/assets/mascot/scenes/computer.png +0 -0
  209. package/neural-interface/public/assets/mascot/scenes/design.gif +0 -0
  210. package/neural-interface/public/assets/mascot/scenes/design.png +0 -0
  211. package/neural-interface/public/assets/mascot/scenes/edit.gif +0 -0
  212. package/neural-interface/public/assets/mascot/scenes/edit.png +0 -0
  213. package/neural-interface/public/assets/mascot/scenes/generic.gif +0 -0
  214. package/neural-interface/public/assets/mascot/scenes/generic.png +0 -0
  215. package/neural-interface/public/assets/mascot/scenes/image_gen.gif +0 -0
  216. package/neural-interface/public/assets/mascot/scenes/image_gen.png +0 -0
  217. package/neural-interface/public/assets/mascot/scenes/manifest.json +173 -0
  218. package/neural-interface/public/assets/mascot/scenes/memory.gif +0 -0
  219. package/neural-interface/public/assets/mascot/scenes/memory.png +0 -0
  220. package/neural-interface/public/assets/mascot/scenes/quick.gif +0 -0
  221. package/neural-interface/public/assets/mascot/scenes/quick.png +0 -0
  222. package/neural-interface/public/assets/mascot/scenes/read.gif +0 -0
  223. package/neural-interface/public/assets/mascot/scenes/read.png +0 -0
  224. package/neural-interface/public/assets/mascot/scenes/research.gif +0 -0
  225. package/neural-interface/public/assets/mascot/scenes/research.png +0 -0
  226. package/neural-interface/public/assets/mascot/scenes/review.gif +0 -0
  227. package/neural-interface/public/assets/mascot/scenes/review.png +0 -0
  228. package/neural-interface/public/assets/mascot/scenes/search.gif +0 -0
  229. package/neural-interface/public/assets/mascot/scenes/search.png +0 -0
  230. package/neural-interface/public/assets/mascot/scenes/shell.gif +0 -0
  231. package/neural-interface/public/assets/mascot/scenes/shell.png +0 -0
  232. package/neural-interface/public/assets/mascot/scenes/thinking.gif +0 -0
  233. package/neural-interface/public/assets/mascot/scenes/thinking.png +0 -0
  234. package/neural-interface/public/assets/mascot/scenes/video_gen.gif +0 -0
  235. package/neural-interface/public/assets/mascot/scenes/video_gen.png +0 -0
  236. package/neural-interface/public/assets/mascot/scenes/web.gif +0 -0
  237. package/neural-interface/public/assets/mascot/scenes/web.png +0 -0
  238. package/neural-interface/public/assets/mascot/search.gif +0 -0
  239. package/neural-interface/public/assets/mascot/search.png +0 -0
  240. package/neural-interface/public/assets/mascot/shell.gif +0 -0
  241. package/neural-interface/public/assets/mascot/shell.png +0 -0
  242. package/neural-interface/public/assets/mascot/skill.gif +0 -0
  243. package/neural-interface/public/assets/mascot/skill.png +0 -0
  244. package/neural-interface/public/assets/mascot/sleep.gif +0 -0
  245. package/neural-interface/public/assets/mascot/sleep.png +0 -0
  246. package/neural-interface/public/assets/mascot/subagent.gif +0 -0
  247. package/neural-interface/public/assets/mascot/subagent.png +0 -0
  248. package/neural-interface/public/assets/mascot/success.gif +0 -0
  249. package/neural-interface/public/assets/mascot/success.png +0 -0
  250. package/neural-interface/public/assets/mascot/think.gif +0 -0
  251. package/neural-interface/public/assets/mascot/think.png +0 -0
  252. package/neural-interface/public/assets/mascot/wait.gif +0 -0
  253. package/neural-interface/public/assets/mascot/wait.png +0 -0
  254. package/neural-interface/public/assets/mascot/watch.gif +0 -0
  255. package/neural-interface/public/assets/mascot/watch.png +0 -0
  256. package/neural-interface/public/assets/mascot/web.gif +0 -0
  257. package/neural-interface/public/assets/mascot/web.png +0 -0
  258. package/neural-interface/public/onboarding.html +14 -1
  259. package/neural-interface/public/shared/agent-runtime-options.js +49 -8
  260. package/neural-interface/public/shared/agy/agy-panel.js +2 -2
  261. package/neural-interface/public/shared/agy/agy-render.js +41 -1
  262. package/neural-interface/public/shared/agy/agy-styles.js +5 -0
  263. package/neural-interface/public/shared/api.js +5 -4
  264. package/neural-interface/public/shared/assistant/asst-ask-styles.js +8 -8
  265. package/neural-interface/public/shared/assistant/asst-ask.js +1 -1
  266. package/neural-interface/public/shared/assistant/asst-brain-picker.js +99 -9
  267. package/neural-interface/public/shared/assistant/asst-computer.js +47 -2
  268. package/neural-interface/public/shared/assistant/asst-control.js +1 -1
  269. package/neural-interface/public/shared/assistant/asst-limit-wait.js +7 -5
  270. package/neural-interface/public/shared/assistant/asst-panel.js +13 -5
  271. package/neural-interface/public/shared/assistant/asst-rack-styles.js +4 -4
  272. package/neural-interface/public/shared/assistant/asst-render.js +16 -10
  273. package/neural-interface/public/shared/assistant/asst-route-models.js +64 -4
  274. package/neural-interface/public/shared/assistant/asst-route.js +24 -7
  275. package/neural-interface/public/shared/assistant/asst-routes-editor.js +154 -39
  276. package/neural-interface/public/shared/assistant/asst-run-stage.js +3 -3
  277. package/neural-interface/public/shared/assistant/asst-scene-styles.js +8 -111
  278. package/neural-interface/public/shared/assistant/asst-scenes.js +46 -318
  279. package/neural-interface/public/shared/assistant/asst-sidepanel.js +2 -2
  280. package/neural-interface/public/shared/assistant/asst-state.js +72 -2
  281. package/neural-interface/public/shared/assistant/asst-styles.js +60 -15
  282. package/neural-interface/public/shared/automation-approval-view.js +75 -0
  283. package/neural-interface/public/shared/cdx/cdx-panel.js +2 -2
  284. package/neural-interface/public/shared/cdx/cdx-render.js +73 -7
  285. package/neural-interface/public/shared/cdx/cdx-tabs.js +3 -3
  286. package/neural-interface/public/shared/context-menu-model.js +2 -0
  287. package/neural-interface/public/shared/cp/cp-agents.js +8 -0
  288. package/neural-interface/public/shared/cp/cp-permissions.js +6 -2
  289. package/neural-interface/public/shared/cp/cp-rehydrate.js +25 -1
  290. package/neural-interface/public/shared/cp/cp-tasks-model.js +33 -0
  291. package/neural-interface/public/shared/editor/editor-panel-logic.js +10 -0
  292. package/neural-interface/public/shared/editor/editor-panel.js +10 -2
  293. package/neural-interface/public/shared/html-shell.js +30 -22
  294. package/neural-interface/public/shared/judgments-log-origins.js +29 -0
  295. package/neural-interface/public/shared/morelogin-profile-choices.js +107 -0
  296. package/neural-interface/public/shared/morelogin-profile-field.js +186 -0
  297. package/neural-interface/public/shared/mtc/mtc-client.js +181 -0
  298. package/neural-interface/public/shared/mtc/mtc-compose.js +40 -0
  299. package/neural-interface/public/shared/mtc/mtc-copy.js +253 -0
  300. package/neural-interface/public/shared/mtc/mtc-logic.js +478 -0
  301. package/neural-interface/public/shared/mtc/mtc-panel.js +1354 -0
  302. package/neural-interface/public/shared/mtc/mtc-render.js +172 -0
  303. package/neural-interface/public/shared/mtc/mtc-styles.js +437 -0
  304. package/neural-interface/public/shared/ocp-v2/ocp-v2-automation-ownership.js +23 -0
  305. package/neural-interface/public/shared/ocp-v2/ocp-v2-events.js +13 -0
  306. package/neural-interface/public/shared/ocp-v2/ocp-v2-icons.js +3 -1
  307. package/neural-interface/public/shared/ocp-v2/ocp-v2-panel.js +10 -4
  308. package/neural-interface/public/shared/ocp-v2/ocp-v2-render.js +6 -86
  309. package/neural-interface/public/shared/ocp-v2/ocp-v2-ws.js +5 -2
  310. package/neural-interface/public/shared/owner-access.js +134 -0
  311. package/neural-interface/public/shared/owner-authority-view.js +92 -0
  312. package/neural-interface/public/shared/page-session.js +143 -14
  313. package/neural-interface/public/shared/poll-backoff.js +59 -0
  314. package/neural-interface/public/shared/provider-icons.js +3 -0
  315. package/neural-interface/public/shared/schedules-view.js +7 -0
  316. package/neural-interface/public/shared/server-back.js +50 -0
  317. package/neural-interface/public/shared/settings/settings-browser-owner-access.js +72 -0
  318. package/neural-interface/public/shared/settings/settings-data.js +7 -1
  319. package/neural-interface/public/shared/settings/settings-ia.js +3 -1
  320. package/neural-interface/public/shared/settings/settings-kit.js +11 -3
  321. package/neural-interface/public/shared/settings/settings-local-profiles.js +153 -0
  322. package/neural-interface/public/shared/settings/settings-maritaca-opencode.js +105 -0
  323. package/neural-interface/public/shared/settings/settings-shell.js +7 -0
  324. package/neural-interface/public/shared/shown-children.js +72 -0
  325. package/neural-interface/public/shared/sidepanel-session-status.js +16 -2
  326. package/neural-interface/public/shared/state.js +1 -1
  327. package/neural-interface/public/shared/static-bg-state.js +22 -0
  328. package/neural-interface/public/shared/styles.css +457 -164
  329. package/neural-interface/public/shared/synabun-mascot-assets.js +67 -0
  330. package/neural-interface/public/shared/synabun-mascot.js +136 -12
  331. package/neural-interface/public/shared/synabun-ticker.js +3 -0
  332. package/neural-interface/public/shared/synabun-tools.js +173 -0
  333. package/neural-interface/public/shared/tool-heartbeat.js +43 -0
  334. package/neural-interface/public/shared/tool-list-picker.js +3 -1
  335. package/neural-interface/public/shared/tool-list-view.js +23 -4
  336. package/neural-interface/public/shared/topright-overflow-logic.js +1 -0
  337. package/neural-interface/public/shared/ui-automation-approval.js +188 -0
  338. package/neural-interface/public/shared/ui-automation-studio.js +138 -91
  339. package/neural-interface/public/shared/ui-claude-panel.js +209 -31
  340. package/neural-interface/public/shared/ui-judgments.js +37 -10
  341. package/neural-interface/public/shared/ui-maritaca-panel.js +15 -0
  342. package/neural-interface/public/shared/ui-navbar.js +23 -0
  343. package/neural-interface/public/shared/ui-owner-access.js +191 -0
  344. package/neural-interface/public/shared/ui-owner-authority.js +162 -0
  345. package/neural-interface/public/shared/ui-panels.js +3 -1
  346. package/neural-interface/public/shared/ui-schedules-studio.js +118 -17
  347. package/neural-interface/public/shared/ui-scroll-follow.js +3 -0
  348. package/neural-interface/public/shared/ui-sessions.js +45 -16
  349. package/neural-interface/public/shared/ui-settings.js +676 -42
  350. package/neural-interface/public/shared/ui-sidepanel-session-entry.js +8 -2
  351. package/neural-interface/public/shared/ui-sidepanel-sessions.js +3 -3
  352. package/neural-interface/public/shared/ui-sidepanel-tray.js +24 -0
  353. package/neural-interface/public/shared/ui-sidepanel-windows.js +1 -1
  354. package/neural-interface/public/shared/ui-terminal.js +15 -7
  355. package/neural-interface/public/shared/ui-tutorial-preview.js +4 -0
  356. package/neural-interface/public/shared/ui-update.js +3 -0
  357. package/neural-interface/public/shared/ui-whiteboard.js +4 -0
  358. package/neural-interface/public/shared/window-keys.js +38 -0
  359. package/neural-interface/scripts/build-mascot-assets.mjs +30 -0
  360. package/neural-interface/server.js +2903 -359
  361. package/npm-shrinkwrap.json +5 -17
  362. package/package.json +5 -6
  363. package/postinstall.js +38 -39
  364. package/setup.js +33 -31
  365. package/skills/synabun/modules/changelog.md +14 -0
@@ -6,12 +6,14 @@
6
6
 
7
7
  import { AsyncLocalStorage } from 'node:async_hooks';
8
8
  import { randomUUID } from 'node:crypto';
9
+ import { BrowserRouteLost, currentRouteFlow, noteRouteReplaced, type RouteLoss, type RouteRef } from './browser-route-flow.js';
9
10
 
10
11
  import {
11
12
  type CallerIdentity,
12
13
  getIdentity,
13
14
  effectivePins,
14
15
  terminalIdFor,
16
+ terminalHeaders,
15
17
  callerBrainCredential,
16
18
  callerRole,
17
19
  setReleaseHook,
@@ -23,7 +25,33 @@ const BASE_URL = process.env.NEURAL_INTERFACE_URL
23
25
  || `http://localhost:${process.env.NEURAL_PORT || '3344'}`;
24
26
  const DEFAULT_TIMEOUT = 10_000;
25
27
  const LONG_TIMEOUT = 30_000;
26
- const SESSION_CREATE_TIMEOUT = 70_000;
28
+ // A browser start (create, acquire, a run's recovery) is answered by the server within its request budget
29
+ // (neural-interface/lib/browser-execution.js BROWSER_REQUEST_MAX_MS, 120 s): the session, or BROWSER_STARTING while a
30
+ // cold MoreLogin start is still running (it can wait two minutes for the app), with the start kept for this caller's
31
+ // next request (lib/browser-start-handoff.js). This waits longer than that budget, so the answer read is the server's,
32
+ // never a timeout of its own that left the start running unseen (70 s before 2026-11-04).
33
+ const SESSION_CREATE_TIMEOUT = 150_000;
34
+
35
+ /**
36
+ * What an agent does when its browser session or tab is gone and no replacement was made for it: the rule of the
37
+ * browser policy (neural-interface/lib/browser-tool-policy.js browserSessionLossRule), in the answer itself.
38
+ */
39
+ export const LOST_ROUTE_HINT = 'Call browser_session with action "create" to open a new browser tab and continue there (pass its sessionId and tabId from then on, if you pass ids); only if that fails too, stop and report it. Never switch to another browser or tool.';
40
+
41
+ /** The server answered that the browser is still starting (lib/browser-start-handoff.js): nothing failed. */
42
+ export function isBrowserStarting(result: NiResponse | null | undefined): boolean {
43
+ return !!result && (result.starting === true || result.code === 'BROWSER_STARTING');
44
+ }
45
+
46
+ /**
47
+ * browser_navigate's own session (auto-create) while the browser is still starting: nothing failed, and the server keeps
48
+ * that start for this caller, so the next call gets it (never a second browser). Before 2026-11-04 this read
49
+ * "Failed to auto-create session: …" and agents gave up or reached for another browser.
50
+ */
51
+ export const AUTO_CREATE_STARTING = 'The browser is still starting (a cold MoreLogin start can take about two minutes); nothing failed. Call browser_navigate again in a moment: it opens the page in that browser as soon as it is ready and never starts a second one.';
52
+
53
+ /** A route, or why there is none (`starting`: the browser is still starting, not a failure). */
54
+ type RouteOrError = { sessionId: string; tabId?: string } | { error: string; starting?: boolean };
27
55
  // Existing engagement spacing can consume 90 seconds before the click begins.
28
56
  const CLICK_TIMEOUT = 120_000;
29
57
 
@@ -126,6 +154,11 @@ export interface NiResponse {
126
154
  error?: string;
127
155
  tabRecovered?: boolean;
128
156
  sessionInvalidated?: boolean;
157
+ /** The server closed this wedged tab (and kept its session, which other runs use): the next call gets a new tab. */
158
+ wedgedTabRetired?: boolean;
159
+ /** The session or tab the request named was gone, and request() sent it to this replacement route instead.
160
+ * A caller that holds a session id across several requests must use this route from now on. */
161
+ rerouted?: { sessionId: string; tabId: string | null };
129
162
  [key: string]: unknown;
130
163
  }
131
164
 
@@ -159,7 +192,9 @@ async function requestOnce(
159
192
  // Per-caller routing identity: the server maps this to the caller's owned
160
193
  // tab (_tabOwners) so tab-less browser calls never land on another
161
194
  // automation's tab. Derived per-request over HTTP, per-process for stdio.
162
- headers['X-Synabun-Terminal'] = terminalIdFor(getIdentity());
195
+ // A run's pin travels with its terminal token (X-Synabun-Terminal-Token),
196
+ // without which the browser routes refuse to act as that run.
197
+ Object.assign(headers, terminalHeaders(getIdentity()));
163
198
  // Assistant runtimes forward their role so /api/assistant/* can tell an
164
199
  // orchestrator apart from a worker run (workers must never dispatch).
165
200
  const role = callerRole();
@@ -225,25 +260,59 @@ async function requestOnce(
225
260
  }
226
261
  }
227
262
 
263
+ /** A browser request the server refused before starting anything, because its session or tab is gone. */
264
+ export function isMissingRoute(result: NiResponse | null | undefined): boolean {
265
+ return !!result && result.actionStarted === false
266
+ && (result.code === 'SESSION_NOT_FOUND' || result.code === 'TAB_NOT_FOUND');
267
+ }
268
+
269
+ /**
270
+ * `recover: false` — a missing session or tab is answered as it is, never sent on to a replacement route: for a
271
+ * caller that must prepare a page before it runs something there (a Bluesky script needs a bsky.app document, a
272
+ * replacement tab is about:blank) and recovers by itself (refreshSession).
273
+ */
228
274
  async function request(
229
275
  method: string,
230
276
  path: string,
231
277
  body?: Record<string, unknown>,
232
278
  timeout = DEFAULT_TIMEOUT,
279
+ opts: { recover?: boolean } = {},
233
280
  ): Promise<NiResponse> {
281
+ const match = /^\/api\/browser\/sessions\/([^/?]+)\/(.+)$/.exec(path);
282
+ // A tool call whose page was lost runs nothing more on a tab (browser-route-flow.ts): a later step would run on
283
+ // the blank replacement and answer for a page that is not there.
284
+ const flow = currentRouteFlow();
285
+ const guarded = !!flow && flow.policy !== 'follow' && !!match;
286
+ if (guarded && flow!.loss) throw new BrowserRouteLost(flow!.loss);
234
287
  const result = await requestOnce(method, path, body, timeout);
235
288
  const context = batchContext.getStore();
236
- const missingRoute = result.actionStarted === false
237
- && (result.code === 'SESSION_NOT_FOUND' || result.code === 'TAB_NOT_FOUND');
238
- const match = /^\/api\/browser\/sessions\/([^/?]+)\/(.+)$/.exec(path);
239
- if (isBrowserV2Enabled() && missingRoute && match) {
289
+ const missingRoute = isMissingRoute(result);
290
+ const routeOf = (): RouteLoss['from'] => {
291
+ const tabId = typeof body?.tabId === 'string' ? body.tabId : new URL(path, BASE_URL).searchParams.get('tabId');
292
+ return { sessionId: match![1], tabId: tabId || null };
293
+ };
294
+ const stepOf = () => match![2].split('?')[0];
295
+ if (guarded && !context && result.error
296
+ && (result.tabRecovered === true || result.wedgedTabRetired === true || result.sessionInvalidated === true)) {
297
+ // The step failed and the server reset the wedged page to about:blank under the same tab id, or closed the tab
298
+ // or the session: the page is gone. (A screenshot that worked can retire its tab too: it keeps its answer, and
299
+ // the next step finds the tab gone.)
300
+ flow!.loss = { reason: result.tabRecovered === true ? 'reset' : 'retired', step: stepOf(), from: routeOf() };
301
+ throw new BrowserRouteLost(flow!.loss);
302
+ }
303
+ if (isBrowserV2Enabled() && missingRoute && match && opts.recover !== false) {
240
304
  const id = getIdentity();
241
305
  const cached = resolvedRoutes.get(id);
242
306
  resolvedRoutes.delete(id);
243
307
  // Batches are pinned to their original tab and never recover midway.
244
308
  const pinnedSession = effectivePins(id).browserSessionId || id.state.ancestorPinnedSession;
309
+ // The run's pinned session, or the one its route moved to (a recovery, its own browser_session create), named by
310
+ // hand: both are the run's own route, recovered the same way.
245
311
  if (!context && cached?.route.sessionId === match[1]
246
- && (!cached.sessionId || cached.sessionId === pinnedSession)) {
312
+ && (!cached.sessionId || cached.sessionId === pinnedSession
313
+ || (!!pinnedSession && cached.sessionId === id.state.recoveredSessionId))) {
314
+ // The route that failed: the replacement's notice names it (announceReplacement).
315
+ id.state.lostRoute = routeOf();
247
316
  // Clear unpinned fast-mode state too: a force-refresh must not reuse it.
248
317
  if (id.state.interactiveSessionId === match[1]) {
249
318
  id.state.interactiveSessionId = null;
@@ -253,19 +322,48 @@ async function request(
253
322
  const refreshed = await resolutionRefresh.run(true, () => resolveSession(
254
323
  cached.sessionId, cached.autoCreate, cached.tabId, { platform: cached.platform === true },
255
324
  ));
325
+ if (!('error' in refreshed) && guarded) {
326
+ // The replacement tab is ready for the next call; this call stops here (or runs again from the start).
327
+ const from = routeOf();
328
+ const replaced = refreshed.sessionId !== from.sessionId || (refreshed.tabId || null) !== from.tabId;
329
+ flow!.loss = { reason: 'gone', step: stepOf(), from, ...(replaced && { to: { sessionId: refreshed.sessionId, tabId: refreshed.tabId || null } }) };
330
+ throw new BrowserRouteLost(flow!.loss);
331
+ }
256
332
  if (!('error' in refreshed)) {
257
333
  const nextPath = path.replace(`/sessions/${match[1]}/`, `/sessions/${refreshed.sessionId}/`);
258
334
  const nextBody = body ? { ...body, tabId: refreshed.tabId } : undefined;
259
335
  const url = new URL(nextPath, BASE_URL);
260
336
  if (refreshed.tabId) url.searchParams.set('tabId', refreshed.tabId);
261
337
  else url.searchParams.delete('tabId');
262
- return requestOnce(method, `${url.pathname}${url.search}`, nextBody, timeout);
338
+ const retried = await requestOnce(method, `${url.pathname}${url.search}`, nextBody, timeout);
339
+ // Say where it went: a tool that keeps the old session id for its next request would get "Session not
340
+ // found" there (bluesky.ts probed the replacement tab, then navigated the closed session, 2026-11-04).
341
+ const rerouted = { sessionId: refreshed.sessionId, tabId: refreshed.tabId || null };
342
+ if (flow) flow.rerouted = rerouted;
343
+ // The call ran on the replacement: its answer starts with a notice (once; resolution may have raised it).
344
+ announceReplacement(id, rerouted, routeOf());
345
+ return { ...retried, rerouted };
263
346
  }
264
347
  // Recovery failed: report why (e.g. "MoreLogin connect failed: …"), not the
265
348
  // bare "Session not found" that triggered it. Nothing was started.
266
- return { ...result, error: refreshed.error, recoveryFailed: true };
349
+ const starting = (refreshed as { starting?: boolean }).starting === true;
350
+ if (guarded) {
351
+ // Every later step of this tool would find the same dead route: the call stops here and says what happened
352
+ // (routeLossMessage: still starting, or create a tab of your own).
353
+ // The loss message says what to do; the hint the recovery's error carries would say it twice.
354
+ const why = refreshed.error.replace(`\n${LOST_ROUTE_HINT}`, '').trim();
355
+ flow!.loss = { reason: 'gone', step: stepOf(), from: routeOf(), recovery: { error: why, ...(starting && { starting: true }) } };
356
+ throw new BrowserRouteLost(flow!.loss);
357
+ }
358
+ return { ...result, error: refreshed.error, recoveryFailed: true, ...(starting && { starting: true }) };
267
359
  }
268
360
  }
361
+ if (guarded && !context && missingRoute && opts.recover !== false) {
362
+ // Gone, and not a route this caller resolved (a session id named by hand, v2 off): no replacement here; the
363
+ // call stops instead of polling a tab that does not exist until its own timeout.
364
+ flow!.loss = { reason: 'gone', step: stepOf(), from: routeOf() };
365
+ throw new BrowserRouteLost(flow!.loss);
366
+ }
269
367
  // The server refused a session that does not serve the default browser (the
270
368
  // settings changed under a cached route): forget it, so the next call resolves
271
369
  // the default browser. Not retried here: the page the call meant is in the other browser.
@@ -278,11 +376,17 @@ async function request(
278
376
  }
279
377
  if (id.state.affinitySessionId === match[1]) id.state.affinitySessionId = null;
280
378
  }
281
- if (context && result.error) context.errors.push(result);
282
- else if (context && typeof result.snapshotError === 'string') {
283
- context.errors.push({ error: result.snapshotError, code: 'SNAPSHOT_FAILED' });
379
+ // Gone, and nothing replaced it here (a batch, a session named by hand, v2 off): the answer says what to do, so the
380
+ // agent opens a new tab instead of giving up or reaching for another browser. A caller that recovers by itself
381
+ // (recover: false) gets the refusal as it is.
382
+ const answered = missingRoute && opts.recover !== false
383
+ ? { ...result, error: `${result.error || 'The browser session or tab is gone'}. ${LOST_ROUTE_HINT}` }
384
+ : result;
385
+ if (context && answered.error) context.errors.push(answered);
386
+ else if (context && typeof answered.snapshotError === 'string') {
387
+ context.errors.push({ error: answered.snapshotError, code: 'SNAPSHOT_FAILED' });
284
388
  }
285
- return result;
389
+ return answered;
286
390
  }
287
391
 
288
392
  /**
@@ -309,7 +413,7 @@ async function postRuntimeReport(path: string, body: Record<string, unknown>): P
309
413
  const controller = new AbortController();
310
414
  const timer = setTimeout(() => controller.abort(), RUNTIME_REPORT_TIMEOUT);
311
415
  try {
312
- const headers: Record<string, string> = { 'Content-Type': 'application/json', 'X-Synabun-Terminal': terminalIdFor(getIdentity()) };
416
+ const headers: Record<string, string> = { 'Content-Type': 'application/json', ...terminalHeaders(getIdentity()) };
313
417
  const role = callerRole();
314
418
  if (role) headers['X-Synabun-Role'] = role;
315
419
  const res = await fetch(`${BASE_URL}${path}`, { method: 'POST', headers, body: JSON.stringify(body), signal: controller.signal });
@@ -380,26 +484,163 @@ export async function reportRuntimeTools(
380
484
 
381
485
  // Join the ONE shared browser with a dedicated, owned tab (instead of launching a
382
486
  // second browser on a persistent profile, which fails with "profile is locked").
383
- async function acquireInteractive(id: CallerIdentity, url?: string): Promise<{ sessionId: string; tabId?: string } | { error: string }> {
487
+ async function acquireInteractive(id: CallerIdentity, url?: string): Promise<RouteOrError> {
488
+ // The tab is owned by the id this caller routes as (X-Synabun-Terminal): the server checks an explicit tab against
489
+ // that header, so a tab filed under the process's client id was refused to a run with a terminal pin and no browser
490
+ // pin (a native run without a browser, a brain's MCP child) as "another owner's".
384
491
  const resp = await request('POST', '/api/browser/acquire', {
385
- clientId: id.clientId,
492
+ clientId: terminalIdFor(id),
386
493
  url: url || 'about:blank',
387
494
  }, SESSION_CREATE_TIMEOUT);
495
+ // Still starting (a cold MoreLogin start): nothing failed, and the tab is kept for this caller's next request.
496
+ if (isBrowserStarting(resp)) return { error: String(resp.error || 'The browser is still starting. Call the browser tool again in a moment.'), starting: true };
388
497
  if (resp.error || !resp.sessionId) {
389
498
  return { error: `Failed to acquire a shared browser tab: ${resp.error || 'no session returned'}` };
390
499
  }
391
500
  id.state.interactiveSessionId = resp.sessionId as string;
392
501
  id.state.interactiveTabId = (resp.tabId as string) || null;
393
502
  id.state.affinitySessionId = id.state.interactiveSessionId;
503
+ // A tab of its own again, after the one it had went away: the call says so.
504
+ if (id.state.lostRoute) announceReplacement(id, { sessionId: id.state.interactiveSessionId, tabId: id.state.interactiveTabId });
394
505
  return { sessionId: id.state.interactiveSessionId, tabId: id.state.interactiveTabId || undefined };
395
506
  }
396
507
 
397
508
  function adoptCreatedSession(id: CallerIdentity, created: NiResponse, tabId?: string): { sessionId: string; tabId?: string } {
398
509
  const sessionId = created.sessionId as string;
399
510
  id.state.affinitySessionId = sessionId;
511
+ // A tab of its own in a shared browser (a caller with a MoreLogin profile of its own): released when it leaves.
512
+ if (created.shared === true && typeof created.tabId === 'string') {
513
+ id.state.interactiveSessionId = sessionId;
514
+ id.state.interactiveTabId = created.tabId;
515
+ tabId = tabId || created.tabId;
516
+ }
517
+ if (id.state.lostRoute) {
518
+ announceReplacement(id, { sessionId, tabId: tabId || (typeof created.tabId === 'string' ? created.tabId : null) });
519
+ }
400
520
  return { sessionId, tabId };
401
521
  }
402
522
 
523
+ /** Old tab ids this caller still names after they were replaced: kept small (the latest ones). */
524
+ const REPLACED_TABS_MAX = 16;
525
+
526
+ /**
527
+ * This call is served by a replacement route (`to`) for one that went away (`from`, else the caller's lostRoute):
528
+ * its answer starts with a notice that names both (browser-route-flow.ts noteRouteReplaced), once per replacement,
529
+ * and a later call that still names the old tab goes to the new one. A replacement that is the lost route itself (a
530
+ * recovery that handed the same tab back) is not one.
531
+ */
532
+ function announceReplacement(
533
+ id: CallerIdentity,
534
+ to: { sessionId: string; tabId?: string | null },
535
+ from?: RouteRef | null,
536
+ kind: 'lost' | 'moved' = 'lost',
537
+ ): void {
538
+ const lost = from || id.state.lostRoute;
539
+ id.state.lostRoute = null;
540
+ const next = { sessionId: to.sessionId, tabId: to.tabId || null };
541
+ if (lost && lost.sessionId === next.sessionId && (lost.tabId || null) === next.tabId) return;
542
+ // A tab that is still open (`moved`) was not lost: status reports the move (routeMoved), not a lost tab.
543
+ if (kind === 'lost' && lost?.tabId && next.tabId) {
544
+ id.state.replacedTabs.delete(lost.tabId);
545
+ id.state.replacedTabs.set(lost.tabId, next);
546
+ while (id.state.replacedTabs.size > REPLACED_TABS_MAX) id.state.replacedTabs.delete(id.state.replacedTabs.keys().next().value as string);
547
+ }
548
+ noteRouteReplaced({ kind, from: lost ? { sessionId: lost.sessionId, tabId: lost.tabId || null } : null, to: next });
549
+ }
550
+
551
+ /**
552
+ * The route this caller knew before a repin: the one it moved to earlier, else the one it last served a call on, else
553
+ * its launch pin. A route without a tab id is known by its session only.
554
+ */
555
+ function knownRoute(id: CallerIdentity): { sessionId: string; tabId: string | null } | null {
556
+ if (id.state.recoveredSessionId) return { sessionId: id.state.recoveredSessionId, tabId: id.state.recoveredTabId };
557
+ if (id.state.servedRoute) return id.state.servedRoute;
558
+ const pins = effectivePins(id);
559
+ const session = pins.browserSessionId || id.state.ancestorPinnedSession;
560
+ return session ? { sessionId: session, tabId: pins.browserTabId || id.state.ancestorPinnedTab || null } : null;
561
+ }
562
+
563
+ /**
564
+ * Whether `to` is a different route than `from`: another session, or another tab when both tab ids are known. A
565
+ * route it learns where it knew only the session (a pin without a tab) is the same route, not a move.
566
+ */
567
+ function isOtherRoute(from: { sessionId: string; tabId: string | null } | null, to: { sessionId: string; tabId: string | null }): boolean {
568
+ if (!from) return false;
569
+ return from.sessionId !== to.sessionId || (!!from.tabId && !!to.tabId && from.tabId !== to.tabId);
570
+ }
571
+
572
+ /** A pinned caller's route moves here (a recovery, its own browser_session create, a session of its own named by id). */
573
+ function repin(id: CallerIdentity, sessionId: string, tabId?: string | null): void {
574
+ const to = { sessionId, tabId: tabId || null };
575
+ const from = knownRoute(id);
576
+ // A move from a tab it knew (status says Replaced while it is current); adopting the route it was launched with, or
577
+ // the first one it learns, is not one.
578
+ if (from && isOtherRoute(from, to)) id.state.routeMoved = { from, to };
579
+ id.state.recoveredSessionId = sessionId;
580
+ id.state.recoveredTabId = tabId || null;
581
+ resolvedRoutes.delete(id);
582
+ }
583
+
584
+ /**
585
+ * The open tab this caller owns in `sessionId` (the server's tabOwners, under its routing id or its client id): the
586
+ * tab it named when that is one of its own and open, else its first open one (a tab it named that closed, or that is
587
+ * another owner's, is never returned). null for a session it has no open tab in, or one that is not the default browser.
588
+ */
589
+ function ownedTabIn(listing: NiResponse, sessionId: string, id: CallerIdentity, tabId?: string): string | null {
590
+ const session = ((listing.sessions || []) as BrowserSessionInfo[]).find(s => s.id === sessionId);
591
+ if (!session || !servesDefault(session)) return null;
592
+ const owners = session.tabOwners || {};
593
+ const mine = [owners[terminalIdFor(id)], owners[id.clientId]].filter((t): t is string => typeof t === 'string' && t.length > 0);
594
+ const open = (t: string) => !session.tabs || session.tabs.some(x => x.id === t);
595
+ if (tabId && mine.includes(tabId) && open(tabId)) return tabId;
596
+ return mine.find(open) || null;
597
+ }
598
+
599
+ /**
600
+ * A pinned run moved by itself to a tab it owns (ids of a session of its own, named by hand): the server records it
601
+ * as the run's route (POST /api/loop/recover-browser `adopt`), so a later MCP process of the run (a Codex turn, a
602
+ * restarted stdio child) follows it through yourRoute instead of the route the server knew before. Only a server
603
+ * that says it records it (`adoptRoute` in its session list): an older one would read the request as a recovery and
604
+ * open one more tab. Best-effort: the move holds in this process either way.
605
+ */
606
+ async function recordOwnRoute(id: CallerIdentity, listing: NiResponse, sessionId: string, tabId: string): Promise<void> {
607
+ if (listing.adoptRoute !== true) return;
608
+ const terminalSessionId = effectivePins(id).terminalSessionId || id.state.ancestorPinnedTerminal || undefined;
609
+ if (!terminalSessionId) return;
610
+ const recorded = await request('POST', '/api/loop/recover-browser', { terminalSessionId, adopt: { sessionId, tabId } });
611
+ if (recorded.error) console.error(`[MCP] the run's move to session ${sessionId} tab ${tabId} was not recorded: ${recorded.error}`);
612
+ }
613
+
614
+ /**
615
+ * A pinned run gets a new tab of its own in SynaBun's shared browser (POST /api/loop/recover-browser), started when
616
+ * none runs. `lost`: the route it found gone (the server hands back the run's current route when that one is still
617
+ * open, e.g. after another MCP process of the run recovered first). `reason: 'create'`: the run's own
618
+ * browser_session create, always a new tab. The server records the move on the run (state file, run record).
619
+ */
620
+ async function recoverRunBrowser(
621
+ id: CallerIdentity,
622
+ lost: { sessionId?: string; tabId?: string } | null,
623
+ reason?: 'create',
624
+ ): Promise<NiResponse> {
625
+ const pins = effectivePins(id);
626
+ const terminalSessionId = pins.terminalSessionId || id.state.ancestorPinnedTerminal || undefined;
627
+ const body: Record<string, unknown> = {};
628
+ if (terminalSessionId) body.terminalSessionId = terminalSessionId;
629
+ if (reason) body.reason = reason;
630
+ if (lost?.sessionId) body.lost = { sessionId: lost.sessionId, tabId: lost.tabId || null };
631
+ const recovered = await request('POST', '/api/loop/recover-browser', body, SESSION_CREATE_TIMEOUT);
632
+ if (!recovered.error && typeof recovered.sessionId === 'string' && !isBrowserStarting(recovered)) {
633
+ const tabId = typeof recovered.tabId === 'string' ? recovered.tabId : null;
634
+ repin(id, recovered.sessionId, tabId);
635
+ // A recovery (a new tab, or the route another process of the run moved to: `current`) serves this call; the
636
+ // run's own create is not a replacement it has to be told about.
637
+ if (reason === 'create') id.state.lostRoute = null;
638
+ else announceReplacement(id, { sessionId: recovered.sessionId, tabId },
639
+ id.state.lostRoute || (lost?.sessionId ? { sessionId: lost.sessionId, tabId: lost.tabId || null } : null));
640
+ }
641
+ return recovered;
642
+ }
643
+
403
644
  // Pages (localhost included) only run where the server says the default browser
404
645
  // is. Absent servesDefault (older server, or a managed / real-Chrome setup) keeps
405
646
  // the previous behavior.
@@ -420,11 +661,15 @@ function describeDefault(list: NiResponse): string {
420
661
  // Also registered as the identity-eviction hook (HTTP DELETE / idle TTL).
421
662
  function releaseTabFor(id: CallerIdentity): void {
422
663
  if (!id.state.interactiveSessionId) return;
664
+ // The id the tab was acquired under (acquireInteractive), as the caller it is: the server releases a caller's own
665
+ // tabs only, and a run's only with its terminal token (a stdio child's own; an HTTP identity evicted outside any
666
+ // request has none, and the run's end releases its tabs on the server anyway).
667
+ const owner = terminalIdFor(id);
423
668
  try {
424
669
  void fetch(`${BASE_URL}/api/browser/release`, {
425
670
  method: 'POST',
426
- headers: { 'Content-Type': 'application/json' },
427
- body: JSON.stringify({ clientId: id.clientId }),
671
+ headers: { 'Content-Type': 'application/json', ...terminalHeaders(id) },
672
+ body: JSON.stringify({ clientId: owner }),
428
673
  }).catch(() => {});
429
674
  } catch { /* best-effort */ }
430
675
  }
@@ -534,7 +779,7 @@ async function resolveSessionUncached(
534
779
  autoCreate?: { url?: string },
535
780
  tabId?: string,
536
781
  platform = false,
537
- ): Promise<{ sessionId: string; tabId?: string } | { error: string }> {
782
+ ): Promise<RouteOrError> {
538
783
  // Per-caller identity: pins come from launch headers (HTTP) or live env
539
784
  // (stdio); all caches below live on the identity, never module-level.
540
785
  const id = getIdentity();
@@ -546,16 +791,24 @@ async function resolveSessionUncached(
546
791
  // tool is refused, since the page it names is in the other browser. The
547
792
  // server refuses them too (NOT_DEFAULT_BROWSER).
548
793
  const wantsDefaultBrowser = platform || !!autoCreate;
794
+ let namedListing: NiResponse | null = null;
549
795
  if (sessionId && sessionId !== (pins.browserSessionId || id.state.ancestorPinnedSession)) {
550
796
  const listed = await request('GET', '/api/browser/sessions');
797
+ namedListing = listed;
551
798
  const named = ((listed.sessions || []) as BrowserSessionInfo[]).find(s => s.id === sessionId);
552
799
  if (named && !servesDefault(named)) {
553
- if (!wantsDefaultBrowser) {
554
- return { error: `Browser session ${sessionId} is not the default browser (${describeSession(named)}; the default is ${describeDefault(listed)}), so SynaBun's browser tools do not act on it. Leave sessionId out: browser_navigate opens the page in the default browser.` };
800
+ // Ids of the browser this caller left when it chose another MoreLogin profile (useSessionProfile): the call
801
+ // goes to its own tab in the profile it works in now, and its answer names that tab.
802
+ const replaced = tabId ? id.state.replacedTabs.get(tabId) : undefined;
803
+ const left = id.state.lostRoute?.sessionId === sessionId || !!replaced;
804
+ if (!wantsDefaultBrowser && !left) {
805
+ const own = listed.yourProfile ? 'this session\'s browser' : 'the default browser';
806
+ return { error: `Browser session ${sessionId} is not ${own} (${describeSession(named)}; ${listed.yourProfile ? 'this session works in' : 'the default is'} ${describeDefault(listed)}), so SynaBun's browser tools do not act on it. Leave sessionId out: browser_navigate opens the page in ${own}.${named.profileMode === 'morelogin' ? ' To work in another MoreLogin profile, call morelogin with action "use" first.' : ''}` };
555
807
  }
556
808
  console.error(`[MCP] not using session ${sessionId} (${describeSession(named)}) — resolving ${describeDefault(listed)}`);
557
809
  sessionId = undefined;
558
- tabId = undefined;
810
+ // A tab that was replaced keeps its name: the call goes to its replacement (below).
811
+ if (!replaced) tabId = undefined;
559
812
  }
560
813
  }
561
814
 
@@ -568,7 +821,7 @@ async function resolveSessionUncached(
568
821
  await resolveFromAncestors(id);
569
822
  }
570
823
  const pinnedTab = pins.browserTabId || id.state.ancestorPinnedTab || undefined;
571
- const resolvedTabId = pinnedTab || tabId || undefined;
824
+ let resolvedTabId = pinnedTab || tabId || undefined;
572
825
  if (pinnedTab && tabId && tabId !== pinnedTab) {
573
826
  console.error(`[MCP] tabId override ignored: pinned=${pinnedTab} requested=${tabId}`);
574
827
  }
@@ -577,25 +830,88 @@ async function resolveSessionUncached(
577
830
  // this caller to a specific browser session (multi-session isolation).
578
831
  // If pinned session died, check recovery cache first, then auto-create.
579
832
  const pinnedSession = pins.browserSessionId || id.state.ancestorPinnedSession || undefined;
833
+ // A pinned run that names a session of its own by hand (the one its browser_session create returned, or one it
834
+ // recovered into in an earlier process): that session and its own open tab there, never the dead pinned tab paired
835
+ // with it. The run's route moves there, here and on the server (recordOwnRoute). A session that is not the run's
836
+ // (another run's, or one that is gone) is ignored like a tab the pin overrides: the run works in its own route.
837
+ // Before 2026-11-04 it was paired with the pinned tab, a pair that exists nowhere (the server refused it, and the
838
+ // call reported the run's tab lost).
839
+ if (pinnedSession && sessionId && sessionId !== pinnedSession && sessionId !== id.state.recoveredSessionId) {
840
+ const listed = namedListing || await request('GET', '/api/browser/sessions');
841
+ const owned = ownedTabIn(listed, sessionId, id, tabId);
842
+ if (owned) {
843
+ repin(id, sessionId, owned);
844
+ await recordOwnRoute(id, listed, sessionId, owned);
845
+ return { sessionId, tabId: owned };
846
+ }
847
+ console.error(`[MCP] session ${sessionId} is not one of this run's: ignored, the run's own route is used`);
848
+ sessionId = undefined;
849
+ tabId = undefined;
850
+ resolvedTabId = pinnedTab;
851
+ }
580
852
  // Loop prompts intentionally pass their injected sessionId/tabId on every
581
853
  // call. Treat an explicit session that matches the pin as pinned traffic too;
582
854
  // otherwise the old fast path skipped liveness checks and returned a stale
583
- // tab forever after a renderer/page closed.
584
- if (pinnedSession && (!sessionId || sessionId === pinnedSession)) {
855
+ // tab forever after a renderer/page closed. The session the run moved to (a
856
+ // recovery, its own browser_session create) is the run's pinned traffic now.
857
+ if (pinnedSession && (!sessionId || sessionId === pinnedSession || sessionId === id.state.recoveredSessionId)) {
585
858
  // Check recovery cache first — avoids re-creating on every call after recovery.
586
859
  // Return the RECOVERED tab, never the stale pinned tab (which 404s).
587
860
  if (id.state.recoveredSessionId) {
588
861
  const recheck = await request('GET', '/api/browser/sessions');
589
862
  const alive = ((recheck.sessions || []) as BrowserSessionInfo[]).find(s => s.id === id.state.recoveredSessionId);
590
- if (alive) return { sessionId: id.state.recoveredSessionId, tabId: id.state.recoveredTabId || undefined };
591
- id.state.recoveredSessionId = null; // recovered session also died — try fresh recovery
863
+ // The recovered TAB must still be there too: one closed while its session lives was returned forever
864
+ // ("Tab … not found in session" on every call, 2026-10-04).
865
+ const recoveredTab = id.state.recoveredTabId;
866
+ const tabAlive = !!alive && (!recoveredTab || !alive.tabs || alive.tabs.some(t => t.id === recoveredTab));
867
+ if (alive && tabAlive) return { sessionId: id.state.recoveredSessionId, tabId: recoveredTab || undefined };
868
+ id.state.recoveredSessionId = null; // recovered session or tab also died — try fresh recovery
592
869
  id.state.recoveredTabId = null;
593
870
  }
594
871
 
595
872
  const check = await request('GET', '/api/browser/sessions');
596
- const active = ((check.sessions || []) as BrowserSessionInfo[]).find(s => s.id === pinnedSession);
873
+ const listed = (check.sessions || []) as BrowserSessionInfo[];
874
+ // The route the server recorded for this run (yourRoute): where a recovery or the run's own browser_session
875
+ // create moved it, also when that happened in an earlier MCP process of the run (a Codex turn, a restarted
876
+ // stdio child) that this one never heard of. Used while its tab is open; the pin otherwise.
877
+ const yours = check.yourRoute as { sessionId?: unknown; tabId?: unknown } | undefined;
878
+ if (yours && typeof yours.sessionId === 'string' && typeof yours.tabId === 'string'
879
+ && (yours.sessionId !== pinnedSession || yours.tabId !== resolvedTabId)) {
880
+ const moved = listed.find(s => s.id === yours.sessionId);
881
+ if (moved && servesDefault(moved) && (!moved.tabs || moved.tabs.some(t => t.id === yours.tabId))) {
882
+ // A route this process already served calls on: a recorded route that differs from it is a move it is told
883
+ // about. The pin alone is not (the launch route, or a route that moved before this process started: status
884
+ // reports that one, from the pin, without a notice).
885
+ const served = id.state.servedRoute;
886
+ const servedElsewhere = !!served?.tabId && isOtherRoute(served, { sessionId: yours.sessionId, tabId: yours.tabId });
887
+ repin(id, yours.sessionId, yours.tabId);
888
+ // The server moved this run's route earlier (a recovery or a create in another MCP process of the run). A call
889
+ // that still names other ids, or that worked in another tab before, is told where it ran; one that names none
890
+ // in a process that never worked elsewhere just works there.
891
+ const namedTab = tabId || null;
892
+ if (id.state.lostRoute || servedElsewhere || (namedTab && namedTab !== yours.tabId) || (sessionId && sessionId !== yours.sessionId)) {
893
+ const pinnedListed = listed.find(s => s.id === pinnedSession);
894
+ const formerTab = namedTab || (servedElsewhere ? served!.tabId : null) || resolvedTabId || null;
895
+ const formerOpen = !!pinnedListed && !!formerTab && (!pinnedListed.tabs || pinnedListed.tabs.some(t => t.id === formerTab));
896
+ announceReplacement(id, { sessionId: yours.sessionId, tabId: yours.tabId },
897
+ id.state.lostRoute || { sessionId: sessionId || pinnedSession, tabId: formerTab }, formerOpen ? 'moved' : 'lost');
898
+ }
899
+ return { sessionId: yours.sessionId, tabId: yours.tabId };
900
+ }
901
+ }
902
+ const active = listed.find(s => s.id === pinnedSession);
597
903
  // A run's pinned browser must be the default one. Hard-fail rather than work
598
904
  // in the wrong browser.
905
+ if (active && !servesDefault(active) && check.profileChosen === true) {
906
+ // The run chose another MoreLogin profile for itself (morelogin "use"): its launch tab is in the browser it
907
+ // left. It gets a tab of its own in the profile it works in now, and the server records that as its route.
908
+ const recovered = await recoverRunBrowser(id, { sessionId: pinnedSession, tabId: resolvedTabId });
909
+ if (isBrowserStarting(recovered)) return { error: String(recovered.error), starting: true };
910
+ if (!recovered.error && recovered.sessionId) {
911
+ return { sessionId: recovered.sessionId as string, tabId: (recovered.tabId as string) || undefined };
912
+ }
913
+ return { error: `No browser tab in ${describeDefault(check)}, the profile this run chose: ${recovered.error || 'no tab returned'}\nThe run was not moved to another profile or browser. Pick a profile that can start (morelogin "use"), or go back with morelogin "use" and envId "default".` };
914
+ }
599
915
  if (active && !servesDefault(active)) {
600
916
  return { error: `Pinned browser session ${pinnedSession} is not the default browser (${describeSession(active)}; the default is ${describeDefault(check)}), so this run will not use it. Start the run again so it acquires the default browser.` };
601
917
  }
@@ -605,38 +921,40 @@ async function resolveSessionUncached(
605
921
  const tabAlive = !resolvedTabId || !tabList || tabList.some(t => t.id === resolvedTabId);
606
922
  if (tabAlive) return { sessionId: pinnedSession, tabId: resolvedTabId };
607
923
  console.error(`[MCP] Pinned tab ${resolvedTabId} not found in active session ${pinnedSession} — recovering`);
608
- const terminalSessionId = pins.terminalSessionId || undefined;
609
- const recovered = await request(
610
- 'POST',
611
- '/api/loop/recover-browser',
612
- terminalSessionId ? { terminalSessionId } : {},
613
- SESSION_CREATE_TIMEOUT
614
- );
924
+ const recovered = await recoverRunBrowser(id, { sessionId: pinnedSession, tabId: resolvedTabId });
925
+ if (isBrowserStarting(recovered)) return { error: String(recovered.error), starting: true };
615
926
  if (!recovered.error && recovered.sessionId) {
616
- id.state.recoveredSessionId = recovered.sessionId as string;
617
- id.state.recoveredTabId = (recovered.tabId as string) || null;
618
- return { sessionId: id.state.recoveredSessionId, tabId: id.state.recoveredTabId || undefined };
927
+ return { sessionId: recovered.sessionId as string, tabId: (recovered.tabId as string) || undefined };
619
928
  }
620
- return { sessionId: pinnedSession, tabId: resolvedTabId }; // fall through to old tab (will fail gracefully)
929
+ // Never hand back the dead tab: every call would answer "Tab … not found" and none would recover. An
930
+ // error is not cached, so the next call tries the recovery again.
931
+ return { error: `Browser tab ${resolvedTabId} of session ${pinnedSession} is gone and recovery failed: ${recovered.error || 'no tab returned'}\n${LOST_ROUTE_HINT}` };
621
932
  }
622
933
 
623
934
  // Pinned session is gone — rejoin the SHARED loop browser with a fresh owned
624
935
  // tab (not an isolated new browser), and adopt the NEW tabId. Forwarding the
625
936
  // stale pinned tab into a different session would 404 every subsequent call.
626
937
  console.error(`[MCP] Pinned browser session ${pinnedSession} is gone — recovering via shared loop browser`);
627
- const terminalSessionId = pins.terminalSessionId || undefined;
628
- const recovered = await request(
629
- 'POST',
630
- '/api/loop/recover-browser',
631
- terminalSessionId ? { terminalSessionId } : {},
632
- SESSION_CREATE_TIMEOUT
633
- );
938
+ const recovered = await recoverRunBrowser(id, { sessionId: pinnedSession, tabId: resolvedTabId });
939
+ if (isBrowserStarting(recovered)) return { error: String(recovered.error), starting: true };
634
940
  if (recovered.error || !recovered.sessionId) {
635
- return { error: `Pinned browser session ${pinnedSession} is no longer available and recovery failed: ${recovered.error || 'no session returned'}` };
941
+ return { error: `Pinned browser session ${pinnedSession} is no longer available and recovery failed: ${recovered.error || 'no session returned'}\n${LOST_ROUTE_HINT}` };
942
+ }
943
+ return { sessionId: recovered.sessionId as string, tabId: (recovered.tabId as string) || undefined };
944
+ }
945
+
946
+ // Ids of a tab of this caller's that went away. One SynaBun already replaced: the call goes to its replacement (the
947
+ // notice was given with the replacement). The one just lost (its own close, say): the call resolves its route as if
948
+ // it named none, which opens a tab of its own and says so, instead of failing on the old ids until it creates one.
949
+ if (tabId && !pinnedSession) {
950
+ const replaced = id.state.replacedTabs.get(tabId);
951
+ if (replaced) return { sessionId: replaced.sessionId, tabId: replaced.tabId || undefined };
952
+ const lost = id.state.lostRoute;
953
+ if (lost && lost.tabId === tabId && (!sessionId || lost.sessionId === sessionId)) {
954
+ sessionId = undefined;
955
+ tabId = undefined;
956
+ resolvedTabId = undefined;
636
957
  }
637
- id.state.recoveredSessionId = recovered.sessionId as string;
638
- id.state.recoveredTabId = (recovered.tabId as string) || null;
639
- return { sessionId: id.state.recoveredSessionId, tabId: id.state.recoveredTabId || undefined };
640
958
  }
641
959
 
642
960
  if (sessionId) {
@@ -658,6 +976,7 @@ async function resolveSessionUncached(
658
976
  const check = await request('GET', '/api/browser/sessions');
659
977
  const alive = ((check.sessions || []) as BrowserSessionInfo[]).find(s => s.id === id.state.interactiveSessionId);
660
978
  if (alive && servesDefault(alive)) return { sessionId: id.state.interactiveSessionId, tabId: resolvedTabId || id.state.interactiveTabId || undefined };
979
+ id.state.lostRoute = { sessionId: id.state.interactiveSessionId, tabId: id.state.interactiveTabId };
661
980
  id.state.interactiveSessionId = null;
662
981
  id.state.interactiveTabId = null; // session gone — fall through to re-acquire
663
982
  }
@@ -670,6 +989,8 @@ async function resolveSessionUncached(
670
989
  const created = await request('POST', '/api/browser/sessions', {
671
990
  url: autoCreate.url || 'about:blank',
672
991
  }, SESSION_CREATE_TIMEOUT);
992
+ // Still starting: not a failure (the server keeps the start for this caller's next call).
993
+ if (isBrowserStarting(created)) return { error: AUTO_CREATE_STARTING, starting: true };
673
994
  if (created.error) {
674
995
  if (/lock/i.test(created.error)) return acquireInteractive(id, autoCreate.url);
675
996
  return { error: `Failed to auto-create session: ${created.error}` };
@@ -702,11 +1023,13 @@ async function resolveSessionUncached(
702
1023
  // with a dedicated, owned tab. For a clean profile with no owners, keep the historical
703
1024
  // separate-browser behavior (no lock, no regression).
704
1025
  const lockRisk = allSessions.some(s => s.persistent || s.loopOwned || s.agentOwned || s.interactiveOwned);
705
- const autoCreateSession = async (): Promise<{ sessionId: string; tabId?: string } | { error: string }> => {
1026
+ const autoCreateSession = async (): Promise<RouteOrError> => {
706
1027
  if (lockRisk) return acquireInteractive(id, autoCreate?.url);
707
1028
  const created = await request('POST', '/api/browser/sessions', {
708
1029
  url: autoCreate?.url || 'about:blank',
709
1030
  }, SESSION_CREATE_TIMEOUT);
1031
+ // Still starting: not a failure (the server keeps the start for this caller's next call).
1032
+ if (isBrowserStarting(created)) return { error: AUTO_CREATE_STARTING, starting: true };
710
1033
  if (created.error) {
711
1034
  if (/lock/i.test(created.error)) return acquireInteractive(id, autoCreate?.url);
712
1035
  return { error: `Failed to auto-create session: ${created.error}` };
@@ -721,20 +1044,26 @@ async function resolveSessionUncached(
721
1044
  return autoCreateSession();
722
1045
  }
723
1046
 
724
- if (sessions.length === 1) {
725
- // No autoCreate — caller wants to interact with an existing page (click, snapshot, etc.).
726
- // Adopting is ONLY safe for a pristine, unowned session (e.g. a page the user opened
727
- // manually in the NI UI). If ANOTHER automation already owns a tab here (it plain-created
728
- // the session or acquired a tab — see _tabOwners), adopting makes us a tab-less freeloader
729
- // that FAILS CLOSED (404) the moment the session goes multi-tab — the sidepanel-vs-CLI
730
- // collision. In that case acquire our OWN dedicated tab in the shared browser instead.
1047
+ if (sessions.length > 0 && !resolvedTabId) {
1048
+ // No autoCreate — the caller wants to interact with a page (click, snapshot, etc.). The server hands an automation
1049
+ // only a tab it owns (server.js getTargetPage, since 2026-11-05): a page the user opened by hand in SynaBun's
1050
+ // browser panel, or another automation's tab, is never adopted. A tab of its own there (a session it created, which
1051
+ // this process forgot) is used; else acquire our OWN dedicated tab in the shared browser (it starts at about:blank:
1052
+ // navigate it where the work is).
731
1053
  const myTerminalId = terminalIdFor(id);
732
- const loneOwners = sessions[0].tabOwners || {};
733
- const ownedByOther = Object.keys(loneOwners).some(k => k !== id.clientId && k !== myTerminalId);
734
- if (ownedByOther) {
735
- return acquireInteractive(id, autoCreate?.url);
1054
+ for (const s of sessions) {
1055
+ const owners = s.tabOwners || {};
1056
+ const mine = owners[myTerminalId] || owners[id.clientId];
1057
+ if (mine && (s.tabs || []).some(t => t.id === mine)) {
1058
+ id.state.affinitySessionId = s.id;
1059
+ return { sessionId: s.id, tabId: mine };
1060
+ }
736
1061
  }
737
- id.state.affinitySessionId = sessions[0].id;
1062
+ return acquireInteractive(id, undefined);
1063
+ }
1064
+
1065
+ if (sessions.length === 1) {
1066
+ // An explicit tab with no session: the server checks that the tab is this caller's own.
738
1067
  return { sessionId: sessions[0].id, tabId: resolvedTabId };
739
1068
  }
740
1069
 
@@ -750,11 +1079,11 @@ async function resolveSessionUncached(
750
1079
  // navigate reuses this same owned tab via the X-Synabun-Terminal routing.
751
1080
  return acquireInteractive(id, undefined);
752
1081
  }
753
- return { error: 'No browser sessions open. Use browser_session to create one first, or use browser_navigate with a URL to auto-create.' };
1082
+ return { error: 'No browser sessions open. Call browser_session with action "create" to open one, or browser_navigate with a URL, which opens one by itself.' };
754
1083
  }
755
1084
 
756
1085
  // Multiple available sessions — require explicit ID
757
- const list = sessions.map(s => ` ${s.id} — ${s.title || s.url}`).join('\n');
1086
+ const list = sessions.map(s => ` ${s.id}${s.title || s.url ? ` — ${s.title || s.url}` : ''}`).join('\n');
758
1087
  return { error: `Multiple browser sessions open. Specify sessionId:\n${list}` };
759
1088
  }
760
1089
 
@@ -768,7 +1097,7 @@ export async function resolveSession(
768
1097
  autoCreate?: { url?: string },
769
1098
  tabId?: string,
770
1099
  opts: { platform?: boolean } = {},
771
- ): Promise<BrowserRoute | { error: string }> {
1100
+ ): Promise<BrowserRoute | { error: string; starting?: boolean }> {
772
1101
  const context = batchContext.getStore();
773
1102
  if (context) return context.route;
774
1103
  const startedAt = performance.now();
@@ -781,11 +1110,31 @@ export async function resolveSession(
781
1110
  return { ...cached.route };
782
1111
  }
783
1112
  const result = await resolveSessionUncached(sessionId, autoCreate, tabId, platform);
784
- if (!('error' in result)) resolvedRoutes.set(id, { key: routeKey(id, sessionId, tabId, platform || !!autoCreate), route: { ...result }, sessionId, tabId, autoCreate, platform });
1113
+ if (!('error' in result)) {
1114
+ resolvedRoutes.set(id, { key: routeKey(id, sessionId, tabId, platform || !!autoCreate), route: { ...result }, sessionId, tabId, autoCreate, platform });
1115
+ id.state.servedRoute = { sessionId: result.sessionId, tabId: result.tabId || null };
1116
+ }
785
1117
  browserMetric({ phase: 'session-resolution', cached: false, durationMs: performance.now() - startedAt });
786
1118
  return result;
787
1119
  }
788
1120
 
1121
+ /**
1122
+ * resolveSession without the route cache: for a caller whose request found its session or tab gone (isMissingRoute)
1123
+ * and that recovers by itself. A pinned run gets its session again or a replacement tab (POST /api/loop/recover-browser),
1124
+ * and the next resolveSession reuses what this found. Batches stay on their route and never recover midway.
1125
+ */
1126
+ export async function refreshSession(
1127
+ sessionId?: string,
1128
+ autoCreate?: { url?: string },
1129
+ tabId?: string,
1130
+ opts: { platform?: boolean } = {},
1131
+ ): Promise<BrowserRoute | { error: string; starting?: boolean }> {
1132
+ const context = batchContext.getStore();
1133
+ if (context) return context.route;
1134
+ resolvedRoutes.delete(getIdentity());
1135
+ return resolutionRefresh.run(true, () => resolveSession(sessionId, autoCreate, tabId, opts));
1136
+ }
1137
+
789
1138
  /** Batches require a concrete tab owned by this caller, verified once up front. */
790
1139
  export async function resolveBatchRoute(sessionId?: string, tabId?: string): Promise<BrowserRoute | { error: string }> {
791
1140
  const resolved = await resolveSession(sessionId, undefined, tabId);
@@ -820,14 +1169,189 @@ export async function listSessions(): Promise<NiResponse> {
820
1169
  return request('GET', '/api/browser/sessions');
821
1170
  }
822
1171
 
1172
+ export interface OwnBrowserTab { id: string; url: string; title: string }
1173
+ export interface OwnBrowserSession {
1174
+ id: string;
1175
+ /** This caller's own open tabs there (never another automation's or the owner's). */
1176
+ tabs: OwnBrowserTab[];
1177
+ /** How many other tabs the session has (other automations', the owner's). */
1178
+ otherTabs: number;
1179
+ servesDefault: boolean;
1180
+ profile: string;
1181
+ }
1182
+ export interface BrowserRouteStatus {
1183
+ error?: string;
1184
+ sessions: OwnBrowserSession[];
1185
+ /** The route this caller's next browser call uses, as far as it is known without opening anything. */
1186
+ current: { sessionId: string; tabId: string | null; open: boolean; url?: string; title?: string; source: 'current' | 'recovered' | 'recorded' | 'pinned' | 'own' } | null;
1187
+ /** Tabs of this caller's that were lost and replaced by `current`. */
1188
+ replacedTabs: string[];
1189
+ /** A route of this caller's that went away and that no replacement was opened for yet. */
1190
+ lost: { sessionId: string; tabId: string | null } | null;
1191
+ /** The run's route moved away from a tab it knew: the tab it was launched with (its pin), or one this process
1192
+ * worked in (a recovery or its own create). Never for the launch route itself or the first route a run learns. */
1193
+ movedFromPin: boolean;
1194
+ /** The tab it moved from, when known. */
1195
+ movedFrom?: { sessionId: string; tabId: string | null } | null;
1196
+ /** The MoreLogin profile this caller works in when it is not the default (its launch's, or its own choice). */
1197
+ profile?: { envId: string; name: string | null } | null;
1198
+ }
1199
+
1200
+ /**
1201
+ * What browser_session list / status show: the sessions this caller can see, each with the caller's own tabs only
1202
+ * (the server lists an automation nothing else, ownTabsOnly; an older server's other tabs are left out here by their
1203
+ * owner), and the route its next call uses. Reads only: never opens, recovers or moves a tab.
1204
+ */
1205
+ export async function browserRouteStatus(): Promise<BrowserRouteStatus> {
1206
+ const id = getIdentity();
1207
+ const pins = effectivePins(id);
1208
+ if (!pins.browserSessionId && !pins.browserTabId) await resolveFromAncestors(id);
1209
+ const listing = await request('GET', '/api/browser/sessions');
1210
+ const empty: BrowserRouteStatus = { sessions: [], current: null, replacedTabs: [], lost: id.state.lostRoute, movedFromPin: false };
1211
+ if (listing.error) return { ...empty, error: listing.error };
1212
+ const raw = (listing.sessions || []) as Array<BrowserSessionInfo & { otherTabs?: number }>;
1213
+ const keys = [terminalIdFor(id), id.clientId];
1214
+ const sessions: OwnBrowserSession[] = raw.map((s) => {
1215
+ const owners = s.tabOwners || {};
1216
+ const mine = new Set(keys.map(k => owners[k]).filter((t): t is string => typeof t === 'string' && t.length > 0));
1217
+ const all = s.tabs || [];
1218
+ const tabs = all.filter(t => mine.has(t.id)).map(t => ({ id: t.id, url: t.url || '', title: t.title || '' }));
1219
+ const otherTabs = typeof s.otherTabs === 'number' ? s.otherTabs : all.length - tabs.length;
1220
+ return { id: s.id, tabs, otherTabs, servesDefault: servesDefault(s), profile: describeSession(s) };
1221
+ });
1222
+ const openTab = (sessionId: string, tabId: string | null | undefined) => {
1223
+ const session = sessions.find(x => x.id === sessionId);
1224
+ if (!session) return null;
1225
+ if (!tabId) return session.tabs[0] || null;
1226
+ return session.tabs.find(t => t.id === tabId) || null;
1227
+ };
1228
+ const pinnedSession = pins.browserSessionId || id.state.ancestorPinnedSession || null;
1229
+ const pinnedTab = pins.browserTabId || id.state.ancestorPinnedTab || null;
1230
+ const yours = listing.yourRoute as { sessionId?: unknown; tabId?: unknown } | undefined;
1231
+ const cached = resolvedRoutes.get(id)?.route;
1232
+ type Source = NonNullable<BrowserRouteStatus['current']>['source'];
1233
+ const candidates: Array<{ sessionId: string; tabId: string | null; source: Source }> = [];
1234
+ if (cached) candidates.push({ sessionId: cached.sessionId, tabId: cached.tabId || null, source: 'current' });
1235
+ if (id.state.recoveredSessionId) candidates.push({ sessionId: id.state.recoveredSessionId, tabId: id.state.recoveredTabId, source: 'recovered' });
1236
+ if (yours && typeof yours.sessionId === 'string' && typeof yours.tabId === 'string') candidates.push({ sessionId: yours.sessionId, tabId: yours.tabId, source: 'recorded' });
1237
+ if (pinnedSession) candidates.push({ sessionId: pinnedSession, tabId: pinnedTab, source: 'pinned' });
1238
+ if (id.state.interactiveSessionId) candidates.push({ sessionId: id.state.interactiveSessionId, tabId: id.state.interactiveTabId, source: 'own' });
1239
+ if (id.state.affinitySessionId) candidates.push({ sessionId: id.state.affinitySessionId, tabId: null, source: 'own' });
1240
+ for (const s of sessions) if (s.tabs[0]) candidates.push({ sessionId: s.id, tabId: s.tabs[0].id, source: 'own' });
1241
+ let current: BrowserRouteStatus['current'] = null;
1242
+ for (const c of candidates) {
1243
+ const tab = openTab(c.sessionId, c.tabId);
1244
+ if (tab) { current = { sessionId: c.sessionId, tabId: tab.id, open: true, url: tab.url, title: tab.title, source: c.source }; break; }
1245
+ }
1246
+ // Nothing open: the route it had (pinned or last used), reported as gone.
1247
+ if (!current && candidates[0] && candidates[0].source !== 'own') current = { ...candidates[0], open: false };
1248
+ const replacedTabs = current?.open
1249
+ ? [...id.state.replacedTabs].filter(([, to]) => to.sessionId === current!.sessionId && to.tabId === current!.tabId).map(([from]) => from)
1250
+ : [];
1251
+ // Moved: the current route is not the launch pin, or it is where this process moved from a tab it knew. The route
1252
+ // the server recorded is not a move by itself: on a fresh run it is the launch route (live runs 188b4ed7, 2ed620e8
1253
+ // said "Replaced: yes" on their first call). A fresh process of a run that another process moved knows the move
1254
+ // from its launch pin only; with no tab pinned it cannot tell, and says no.
1255
+ const here = current?.open ? { sessionId: current.sessionId, tabId: current.tabId } : null;
1256
+ const fromPin = !!here && !!pinnedSession && isOtherRoute({ sessionId: pinnedSession, tabId: pinnedTab }, here);
1257
+ const moved = id.state.routeMoved;
1258
+ const fromHere = !!here && !!moved && moved.to.sessionId === here.sessionId && (!moved.to.tabId || moved.to.tabId === here.tabId);
1259
+ const movedFromPin = fromPin || fromHere;
1260
+ const movedFrom = fromHere ? moved!.from : fromPin ? { sessionId: pinnedSession!, tabId: pinnedTab } : null;
1261
+ const yourProfile = listing.yourProfile as { envId?: unknown; name?: unknown } | undefined;
1262
+ const profile = yourProfile && typeof yourProfile.envId === 'string'
1263
+ ? { envId: yourProfile.envId, name: typeof yourProfile.name === 'string' ? yourProfile.name : null } : null;
1264
+ return { sessions, current, replacedTabs, lost: id.state.lostRoute, movedFromPin, movedFrom, profile };
1265
+ }
1266
+
1267
+ /**
1268
+ * browser_session create: a new browser tab of this caller's own, where it continues.
1269
+ * - A pinned run (a loop, a dispatched worker): a new tab in SynaBun's shared browser (started when none runs). The
1270
+ * run's route moves there (repinned): its next calls go to it with or without ids, and the server records it on the
1271
+ * run (state file, run record, agent_status).
1272
+ * - Any other caller: what browser_navigate's auto-create gives it. Its own tab in the shared browser when that runs
1273
+ * on a persistent profile or other automations hold it (a second browser on that profile fails, or ends the shared
1274
+ * one); else a new browser, which becomes its session.
1275
+ * A url other than about:blank is opened in the new tab. A start the server is still running answers
1276
+ * BROWSER_STARTING (isBrowserStarting): the next create gets that same start, never a second browser.
1277
+ */
823
1278
  export async function createSession(url?: string): Promise<NiResponse> {
824
- return request('POST', '/api/browser/sessions', {
825
- url: url || 'about:blank',
826
- }, SESSION_CREATE_TIMEOUT);
1279
+ const id = getIdentity();
1280
+ const pins = effectivePins(id);
1281
+ const target = url || 'about:blank';
1282
+ // The caller's own create is where it continues: nothing to announce about a tab it lost before.
1283
+ id.state.lostRoute = null;
1284
+ if (!pins.browserSessionId && !pins.browserTabId) await resolveFromAncestors(id);
1285
+ const pinned = pins.browserSessionId || id.state.ancestorPinnedSession;
1286
+ let created: NiResponse;
1287
+ let opened = false; // the new tab is already at `target`
1288
+ if (pinned) {
1289
+ created = await recoverRunBrowser(id, null, 'create');
1290
+ if (created.error || isBrowserStarting(created) || typeof created.sessionId !== 'string') return created;
1291
+ created = { ...created, repinned: true };
1292
+ } else {
1293
+ const listed = await request('GET', '/api/browser/sessions');
1294
+ if (listed.error) return listed;
1295
+ const all = (listed.sessions || []) as BrowserSessionInfo[];
1296
+ const lockRisk = all.some(s => s.persistent || s.loopOwned || s.agentOwned || s.interactiveOwned);
1297
+ let shared = lockRisk;
1298
+ created = {};
1299
+ if (!lockRisk) {
1300
+ created = await request('POST', '/api/browser/sessions', { url: target }, SESSION_CREATE_TIMEOUT);
1301
+ if (isBrowserStarting(created)) return created;
1302
+ if (created.error && /lock/i.test(created.error)) shared = true;
1303
+ else if (created.error || typeof created.sessionId !== 'string') return created;
1304
+ else {
1305
+ id.state.affinitySessionId = created.sessionId as string;
1306
+ if (created.shared === true && typeof created.tabId === 'string') {
1307
+ id.state.interactiveSessionId = created.sessionId as string;
1308
+ id.state.interactiveTabId = created.tabId;
1309
+ }
1310
+ resolvedRoutes.delete(id);
1311
+ // A start handed over from an earlier create may not be at this URL yet.
1312
+ opened = !created.handedOver;
1313
+ }
1314
+ }
1315
+ if (shared) {
1316
+ const acquired = await acquireInteractive(id, target);
1317
+ if ('error' in acquired) return { error: acquired.error, ...(acquired.starting && { starting: true, code: 'BROWSER_STARTING' }) };
1318
+ resolvedRoutes.delete(id);
1319
+ created = { ok: true, sessionId: acquired.sessionId, tabId: acquired.tabId || null, shared: true };
1320
+ }
1321
+ }
1322
+ const sessionId = created.sessionId as string;
1323
+ const tabId = typeof created.tabId === 'string' ? created.tabId : undefined;
1324
+ if (!opened && target !== 'about:blank') {
1325
+ const went = await navigate(sessionId, target, tabId, undefined, 'none');
1326
+ if (went.error) return { ...created, url: 'about:blank', navigateError: went.error };
1327
+ return { ...created, url: typeof went.url === 'string' ? went.url : target };
1328
+ }
1329
+ return { ...created, url: typeof created.url === 'string' ? created.url : target };
827
1330
  }
828
1331
 
1332
+ /**
1333
+ * Close a session. One that other automations still have tabs in is kept by the server, which closes only this
1334
+ * caller's own tab there (`sessionKept`). Either way this caller's cached route to it is forgotten: the next call
1335
+ * resolves again (a pinned run gets a tab back through its recovery).
1336
+ */
829
1337
  export async function closeSession(sessionId: string): Promise<NiResponse> {
830
- return request('DELETE', `/api/browser/sessions/${sessionId}`);
1338
+ const result = await request('DELETE', `/api/browser/sessions/${sessionId}`);
1339
+ if (!result.error) {
1340
+ const id = getIdentity();
1341
+ // The tab that went (the server's releasedTabId, else the one this caller routed to there): the next call that
1342
+ // opens a tab for this caller says it replaced this one.
1343
+ const cachedRoute = resolvedRoutes.get(id)?.route;
1344
+ const closedTab = typeof result.releasedTabId === 'string' ? result.releasedTabId
1345
+ : cachedRoute?.sessionId === sessionId ? cachedRoute.tabId || null
1346
+ : id.state.recoveredSessionId === sessionId ? id.state.recoveredTabId
1347
+ : id.state.interactiveSessionId === sessionId ? id.state.interactiveTabId : null;
1348
+ if (result.sessionKept !== true || closedTab) id.state.lostRoute = { sessionId, tabId: closedTab };
1349
+ if (resolvedRoutes.get(id)?.route.sessionId === sessionId) resolvedRoutes.delete(id);
1350
+ if (id.state.interactiveSessionId === sessionId) { id.state.interactiveSessionId = null; id.state.interactiveTabId = null; }
1351
+ if (id.state.affinitySessionId === sessionId) id.state.affinitySessionId = null;
1352
+ if (id.state.recoveredSessionId === sessionId) { id.state.recoveredSessionId = null; id.state.recoveredTabId = null; }
1353
+ }
1354
+ return result;
831
1355
  }
832
1356
 
833
1357
  // ── Navigation ──
@@ -1036,8 +1560,17 @@ export async function consoleMessages(
1036
1560
 
1037
1561
  // ── Advanced ──
1038
1562
 
1039
- export async function evaluate(sessionId: string, script: string, tabId?: string): Promise<NiResponse> {
1040
- return request('POST', `/api/browser/sessions/${sessionId}/evaluate`, { script, ...(tabId && { tabId }) }, LONG_TIMEOUT);
1563
+ /** A social write a page script makes (Bluesky): the platform whose gap the Neural Interface spaces it by, and the
1564
+ * longest this call may wait for it (neural-interface/lib/social-write-pacing.js). */
1565
+ export interface SocialWritePace {
1566
+ platform: 'bluesky';
1567
+ maxWaitMs: number;
1568
+ }
1569
+
1570
+ /** `recover: false`: a missing session or tab is reported, never retried on a replacement tab (see request()). */
1571
+ export async function evaluate(sessionId: string, script: string, tabId?: string, opts?: { pace?: SocialWritePace; recover?: boolean }): Promise<NiResponse> {
1572
+ const pace = opts?.pace ? { platform: opts.pace.platform, maxWaitMs: Math.max(0, Math.floor(opts.pace.maxWaitMs || 0)) } : null;
1573
+ return request('POST', `/api/browser/sessions/${sessionId}/evaluate`, { script, ...(tabId && { tabId }), ...(pace && { pace }) }, LONG_TIMEOUT + (pace?.maxWaitMs || 0), { recover: opts?.recover });
1041
1574
  }
1042
1575
 
1043
1576
  export interface BrowserExtractOptions {
@@ -1385,6 +1918,46 @@ export async function moreloginStop(envId: string): Promise<NiResponse> {
1385
1918
  return request('POST', '/api/morelogin/stop', { envId }, LONG_TIMEOUT);
1386
1919
  }
1387
1920
 
1921
+ /**
1922
+ * This caller works in a MoreLogin profile of its own from now on (POST /api/morelogin/use): `envId`, `name` or
1923
+ * `profile` (either) of a profile MoreLogin lists, or `default: true` for the default of Settings → Browser. Only this
1924
+ * caller moves: the default AI browser and every other session stay where they are. The server releases what the
1925
+ * caller owned in the browser it leaves; here every route this process knew is forgotten, so its next browser call
1926
+ * resolves again and gets a tab of its own in the new profile (started then). The route it left is kept as its lost
1927
+ * route: a call that still names those ids goes to the new tab and says so.
1928
+ */
1929
+ export async function useSessionProfile(ref: { envId?: string; name?: string; profile?: string; default?: boolean }): Promise<NiResponse> {
1930
+ const id = getIdentity();
1931
+ const pins = effectivePins(id);
1932
+ if (!pins.browserSessionId && !pins.browserTabId) await resolveFromAncestors(id);
1933
+ const result = await request('POST', '/api/morelogin/use', { ...ref }, LONG_TIMEOUT);
1934
+ // A Neural Interface from before per-session profiles has no such route and answers it as the owner's.
1935
+ if (result.code === 'OWNER_ROUTE') {
1936
+ return { ...result, error: 'This SynaBun server is older than per-session MoreLogin profiles (it was started before the update): it needs a restart before a session can pick a profile of its own.' };
1937
+ }
1938
+ if (result.error || result.ok !== true || result.changed !== true) return result;
1939
+ const left = knownRoute(id) || (id.state.interactiveSessionId
1940
+ ? { sessionId: id.state.interactiveSessionId, tabId: id.state.interactiveTabId }
1941
+ : null);
1942
+ id.state.interactiveSessionId = null;
1943
+ id.state.interactiveTabId = null;
1944
+ id.state.affinitySessionId = null;
1945
+ id.state.recoveredSessionId = null;
1946
+ id.state.recoveredTabId = null;
1947
+ id.state.servedRoute = null;
1948
+ id.state.routeMoved = null;
1949
+ id.state.replacedTabs.clear();
1950
+ // Only a route the server released is a lost one (a profile that is the default too keeps its tab). The server
1951
+ // names the tab it closed; this process may have known its route by the session alone (a tab-less route).
1952
+ const released = (Array.isArray(result.released) ? result.released : []) as Array<{ sessionId?: unknown; tabId?: unknown }>;
1953
+ const gone = released.find(r => r && r.sessionId === left?.sessionId) || released[0];
1954
+ id.state.lostRoute = gone && typeof gone.sessionId === 'string'
1955
+ ? { sessionId: gone.sessionId, tabId: typeof gone.tabId === 'string' ? gone.tabId : left?.sessionId === gone.sessionId ? left.tabId : null }
1956
+ : null;
1957
+ resolvedRoutes.delete(id);
1958
+ return result;
1959
+ }
1960
+
1388
1961
  export async function moreloginUseDefault(envId: string | null): Promise<NiResponse> {
1389
1962
  return request('POST', '/api/morelogin/use-default', envId ? { envId } : { clear: true });
1390
1963
  }
@@ -1431,6 +2004,8 @@ export type AssistantDispatchBody = {
1431
2004
  maxMinutes?: number;
1432
2005
  budgetUsd?: number;
1433
2006
  usesBrowser?: boolean;
2007
+ /** The MoreLogin profile (id or name) of the worker's browser tab; none = the default browser. */
2008
+ moreloginProfile?: string;
1434
2009
  focus?: boolean;
1435
2010
  title?: string;
1436
2011
  context?: string;