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
@@ -5,12 +5,33 @@
5
5
  */
6
6
  import { AsyncLocalStorage } from 'node:async_hooks';
7
7
  import { randomUUID } from 'node:crypto';
8
- import { getIdentity, effectivePins, terminalIdFor, callerBrainCredential, callerRole, setReleaseHook, peekStdioIdentity, isHttpMode, } from './identity.js';
8
+ import { BrowserRouteLost, currentRouteFlow, noteRouteReplaced } from './browser-route-flow.js';
9
+ import { getIdentity, effectivePins, terminalIdFor, terminalHeaders, callerBrainCredential, callerRole, setReleaseHook, peekStdioIdentity, isHttpMode, } from './identity.js';
9
10
  const BASE_URL = process.env.NEURAL_INTERFACE_URL
10
11
  || `http://localhost:${process.env.NEURAL_PORT || '3344'}`;
11
12
  const DEFAULT_TIMEOUT = 10_000;
12
13
  const LONG_TIMEOUT = 30_000;
13
- const SESSION_CREATE_TIMEOUT = 70_000;
14
+ // A browser start (create, acquire, a run's recovery) is answered by the server within its request budget
15
+ // (neural-interface/lib/browser-execution.js BROWSER_REQUEST_MAX_MS, 120 s): the session, or BROWSER_STARTING while a
16
+ // cold MoreLogin start is still running (it can wait two minutes for the app), with the start kept for this caller's
17
+ // next request (lib/browser-start-handoff.js). This waits longer than that budget, so the answer read is the server's,
18
+ // never a timeout of its own that left the start running unseen (70 s before 2026-11-04).
19
+ const SESSION_CREATE_TIMEOUT = 150_000;
20
+ /**
21
+ * What an agent does when its browser session or tab is gone and no replacement was made for it: the rule of the
22
+ * browser policy (neural-interface/lib/browser-tool-policy.js browserSessionLossRule), in the answer itself.
23
+ */
24
+ 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.';
25
+ /** The server answered that the browser is still starting (lib/browser-start-handoff.js): nothing failed. */
26
+ export function isBrowserStarting(result) {
27
+ return !!result && (result.starting === true || result.code === 'BROWSER_STARTING');
28
+ }
29
+ /**
30
+ * browser_navigate's own session (auto-create) while the browser is still starting: nothing failed, and the server keeps
31
+ * that start for this caller, so the next call gets it (never a second browser). Before 2026-11-04 this read
32
+ * "Failed to auto-create session: …" and agents gave up or reached for another browser.
33
+ */
34
+ 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.';
14
35
  // Existing engagement spacing can consume 90 seconds before the click begins.
15
36
  const CLICK_TIMEOUT = 120_000;
16
37
  export function isBrowserV2Enabled() {
@@ -97,7 +118,9 @@ async function requestOnce(method, path, body, timeout = DEFAULT_TIMEOUT, extraH
97
118
  // Per-caller routing identity: the server maps this to the caller's owned
98
119
  // tab (_tabOwners) so tab-less browser calls never land on another
99
120
  // automation's tab. Derived per-request over HTTP, per-process for stdio.
100
- headers['X-Synabun-Terminal'] = terminalIdFor(getIdentity());
121
+ // A run's pin travels with its terminal token (X-Synabun-Terminal-Token),
122
+ // without which the browser routes refuse to act as that run.
123
+ Object.assign(headers, terminalHeaders(getIdentity()));
101
124
  // Assistant runtimes forward their role so /api/assistant/* can tell an
102
125
  // orchestrator apart from a worker run (workers must never dispatch).
103
126
  const role = callerRole();
@@ -170,20 +193,53 @@ async function requestOnce(method, path, body, timeout = DEFAULT_TIMEOUT, extraH
170
193
  }
171
194
  }
172
195
  }
173
- async function request(method, path, body, timeout = DEFAULT_TIMEOUT) {
174
- const result = await requestOnce(method, path, body, timeout);
175
- const context = batchContext.getStore();
176
- const missingRoute = result.actionStarted === false
196
+ /** A browser request the server refused before starting anything, because its session or tab is gone. */
197
+ export function isMissingRoute(result) {
198
+ return !!result && result.actionStarted === false
177
199
  && (result.code === 'SESSION_NOT_FOUND' || result.code === 'TAB_NOT_FOUND');
200
+ }
201
+ /**
202
+ * `recover: false` — a missing session or tab is answered as it is, never sent on to a replacement route: for a
203
+ * caller that must prepare a page before it runs something there (a Bluesky script needs a bsky.app document, a
204
+ * replacement tab is about:blank) and recovers by itself (refreshSession).
205
+ */
206
+ async function request(method, path, body, timeout = DEFAULT_TIMEOUT, opts = {}) {
178
207
  const match = /^\/api\/browser\/sessions\/([^/?]+)\/(.+)$/.exec(path);
179
- if (isBrowserV2Enabled() && missingRoute && match) {
208
+ // A tool call whose page was lost runs nothing more on a tab (browser-route-flow.ts): a later step would run on
209
+ // the blank replacement and answer for a page that is not there.
210
+ const flow = currentRouteFlow();
211
+ const guarded = !!flow && flow.policy !== 'follow' && !!match;
212
+ if (guarded && flow.loss)
213
+ throw new BrowserRouteLost(flow.loss);
214
+ const result = await requestOnce(method, path, body, timeout);
215
+ const context = batchContext.getStore();
216
+ const missingRoute = isMissingRoute(result);
217
+ const routeOf = () => {
218
+ const tabId = typeof body?.tabId === 'string' ? body.tabId : new URL(path, BASE_URL).searchParams.get('tabId');
219
+ return { sessionId: match[1], tabId: tabId || null };
220
+ };
221
+ const stepOf = () => match[2].split('?')[0];
222
+ if (guarded && !context && result.error
223
+ && (result.tabRecovered === true || result.wedgedTabRetired === true || result.sessionInvalidated === true)) {
224
+ // The step failed and the server reset the wedged page to about:blank under the same tab id, or closed the tab
225
+ // or the session: the page is gone. (A screenshot that worked can retire its tab too: it keeps its answer, and
226
+ // the next step finds the tab gone.)
227
+ flow.loss = { reason: result.tabRecovered === true ? 'reset' : 'retired', step: stepOf(), from: routeOf() };
228
+ throw new BrowserRouteLost(flow.loss);
229
+ }
230
+ if (isBrowserV2Enabled() && missingRoute && match && opts.recover !== false) {
180
231
  const id = getIdentity();
181
232
  const cached = resolvedRoutes.get(id);
182
233
  resolvedRoutes.delete(id);
183
234
  // Batches are pinned to their original tab and never recover midway.
184
235
  const pinnedSession = effectivePins(id).browserSessionId || id.state.ancestorPinnedSession;
236
+ // The run's pinned session, or the one its route moved to (a recovery, its own browser_session create), named by
237
+ // hand: both are the run's own route, recovered the same way.
185
238
  if (!context && cached?.route.sessionId === match[1]
186
- && (!cached.sessionId || cached.sessionId === pinnedSession)) {
239
+ && (!cached.sessionId || cached.sessionId === pinnedSession
240
+ || (!!pinnedSession && cached.sessionId === id.state.recoveredSessionId))) {
241
+ // The route that failed: the replacement's notice names it (announceReplacement).
242
+ id.state.lostRoute = routeOf();
187
243
  // Clear unpinned fast-mode state too: a force-refresh must not reuse it.
188
244
  if (id.state.interactiveSessionId === match[1]) {
189
245
  id.state.interactiveSessionId = null;
@@ -192,6 +248,13 @@ async function request(method, path, body, timeout = DEFAULT_TIMEOUT) {
192
248
  if (id.state.affinitySessionId === match[1])
193
249
  id.state.affinitySessionId = null;
194
250
  const refreshed = await resolutionRefresh.run(true, () => resolveSession(cached.sessionId, cached.autoCreate, cached.tabId, { platform: cached.platform === true }));
251
+ if (!('error' in refreshed) && guarded) {
252
+ // The replacement tab is ready for the next call; this call stops here (or runs again from the start).
253
+ const from = routeOf();
254
+ const replaced = refreshed.sessionId !== from.sessionId || (refreshed.tabId || null) !== from.tabId;
255
+ flow.loss = { reason: 'gone', step: stepOf(), from, ...(replaced && { to: { sessionId: refreshed.sessionId, tabId: refreshed.tabId || null } }) };
256
+ throw new BrowserRouteLost(flow.loss);
257
+ }
195
258
  if (!('error' in refreshed)) {
196
259
  const nextPath = path.replace(`/sessions/${match[1]}/`, `/sessions/${refreshed.sessionId}/`);
197
260
  const nextBody = body ? { ...body, tabId: refreshed.tabId } : undefined;
@@ -200,13 +263,36 @@ async function request(method, path, body, timeout = DEFAULT_TIMEOUT) {
200
263
  url.searchParams.set('tabId', refreshed.tabId);
201
264
  else
202
265
  url.searchParams.delete('tabId');
203
- return requestOnce(method, `${url.pathname}${url.search}`, nextBody, timeout);
266
+ const retried = await requestOnce(method, `${url.pathname}${url.search}`, nextBody, timeout);
267
+ // Say where it went: a tool that keeps the old session id for its next request would get "Session not
268
+ // found" there (bluesky.ts probed the replacement tab, then navigated the closed session, 2026-11-04).
269
+ const rerouted = { sessionId: refreshed.sessionId, tabId: refreshed.tabId || null };
270
+ if (flow)
271
+ flow.rerouted = rerouted;
272
+ // The call ran on the replacement: its answer starts with a notice (once; resolution may have raised it).
273
+ announceReplacement(id, rerouted, routeOf());
274
+ return { ...retried, rerouted };
204
275
  }
205
276
  // Recovery failed: report why (e.g. "MoreLogin connect failed: …"), not the
206
277
  // bare "Session not found" that triggered it. Nothing was started.
207
- return { ...result, error: refreshed.error, recoveryFailed: true };
278
+ const starting = refreshed.starting === true;
279
+ if (guarded) {
280
+ // Every later step of this tool would find the same dead route: the call stops here and says what happened
281
+ // (routeLossMessage: still starting, or create a tab of your own).
282
+ // The loss message says what to do; the hint the recovery's error carries would say it twice.
283
+ const why = refreshed.error.replace(`\n${LOST_ROUTE_HINT}`, '').trim();
284
+ flow.loss = { reason: 'gone', step: stepOf(), from: routeOf(), recovery: { error: why, ...(starting && { starting: true }) } };
285
+ throw new BrowserRouteLost(flow.loss);
286
+ }
287
+ return { ...result, error: refreshed.error, recoveryFailed: true, ...(starting && { starting: true }) };
208
288
  }
209
289
  }
290
+ if (guarded && !context && missingRoute && opts.recover !== false) {
291
+ // Gone, and not a route this caller resolved (a session id named by hand, v2 off): no replacement here; the
292
+ // call stops instead of polling a tab that does not exist until its own timeout.
293
+ flow.loss = { reason: 'gone', step: stepOf(), from: routeOf() };
294
+ throw new BrowserRouteLost(flow.loss);
295
+ }
210
296
  // The server refused a session that does not serve the default browser (the
211
297
  // settings changed under a cached route): forget it, so the next call resolves
212
298
  // the default browser. Not retried here: the page the call meant is in the other browser.
@@ -220,12 +306,18 @@ async function request(method, path, body, timeout = DEFAULT_TIMEOUT) {
220
306
  if (id.state.affinitySessionId === match[1])
221
307
  id.state.affinitySessionId = null;
222
308
  }
223
- if (context && result.error)
224
- context.errors.push(result);
225
- else if (context && typeof result.snapshotError === 'string') {
226
- context.errors.push({ error: result.snapshotError, code: 'SNAPSHOT_FAILED' });
309
+ // Gone, and nothing replaced it here (a batch, a session named by hand, v2 off): the answer says what to do, so the
310
+ // agent opens a new tab instead of giving up or reaching for another browser. A caller that recovers by itself
311
+ // (recover: false) gets the refusal as it is.
312
+ const answered = missingRoute && opts.recover !== false
313
+ ? { ...result, error: `${result.error || 'The browser session or tab is gone'}. ${LOST_ROUTE_HINT}` }
314
+ : result;
315
+ if (context && answered.error)
316
+ context.errors.push(answered);
317
+ else if (context && typeof answered.snapshotError === 'string') {
318
+ context.errors.push({ error: answered.snapshotError, code: 'SNAPSHOT_FAILED' });
227
319
  }
228
- return result;
320
+ return answered;
229
321
  }
230
322
  const RUNTIME_REPORT_TIMEOUT = 2_000;
231
323
  /** One report: its status and its JSON body, or null for a body that is not JSON (a route the server does not have). */
@@ -233,7 +325,7 @@ async function postRuntimeReport(path, body) {
233
325
  const controller = new AbortController();
234
326
  const timer = setTimeout(() => controller.abort(), RUNTIME_REPORT_TIMEOUT);
235
327
  try {
236
- const headers = { 'Content-Type': 'application/json', 'X-Synabun-Terminal': terminalIdFor(getIdentity()) };
328
+ const headers = { 'Content-Type': 'application/json', ...terminalHeaders(getIdentity()) };
237
329
  const role = callerRole();
238
330
  if (role)
239
331
  headers['X-Synabun-Role'] = role;
@@ -303,23 +395,160 @@ export async function reportRuntimeTools(groups, options) {
303
395
  // Join the ONE shared browser with a dedicated, owned tab (instead of launching a
304
396
  // second browser on a persistent profile, which fails with "profile is locked").
305
397
  async function acquireInteractive(id, url) {
398
+ // The tab is owned by the id this caller routes as (X-Synabun-Terminal): the server checks an explicit tab against
399
+ // that header, so a tab filed under the process's client id was refused to a run with a terminal pin and no browser
400
+ // pin (a native run without a browser, a brain's MCP child) as "another owner's".
306
401
  const resp = await request('POST', '/api/browser/acquire', {
307
- clientId: id.clientId,
402
+ clientId: terminalIdFor(id),
308
403
  url: url || 'about:blank',
309
404
  }, SESSION_CREATE_TIMEOUT);
405
+ // Still starting (a cold MoreLogin start): nothing failed, and the tab is kept for this caller's next request.
406
+ if (isBrowserStarting(resp))
407
+ return { error: String(resp.error || 'The browser is still starting. Call the browser tool again in a moment.'), starting: true };
310
408
  if (resp.error || !resp.sessionId) {
311
409
  return { error: `Failed to acquire a shared browser tab: ${resp.error || 'no session returned'}` };
312
410
  }
313
411
  id.state.interactiveSessionId = resp.sessionId;
314
412
  id.state.interactiveTabId = resp.tabId || null;
315
413
  id.state.affinitySessionId = id.state.interactiveSessionId;
414
+ // A tab of its own again, after the one it had went away: the call says so.
415
+ if (id.state.lostRoute)
416
+ announceReplacement(id, { sessionId: id.state.interactiveSessionId, tabId: id.state.interactiveTabId });
316
417
  return { sessionId: id.state.interactiveSessionId, tabId: id.state.interactiveTabId || undefined };
317
418
  }
318
419
  function adoptCreatedSession(id, created, tabId) {
319
420
  const sessionId = created.sessionId;
320
421
  id.state.affinitySessionId = sessionId;
422
+ // A tab of its own in a shared browser (a caller with a MoreLogin profile of its own): released when it leaves.
423
+ if (created.shared === true && typeof created.tabId === 'string') {
424
+ id.state.interactiveSessionId = sessionId;
425
+ id.state.interactiveTabId = created.tabId;
426
+ tabId = tabId || created.tabId;
427
+ }
428
+ if (id.state.lostRoute) {
429
+ announceReplacement(id, { sessionId, tabId: tabId || (typeof created.tabId === 'string' ? created.tabId : null) });
430
+ }
321
431
  return { sessionId, tabId };
322
432
  }
433
+ /** Old tab ids this caller still names after they were replaced: kept small (the latest ones). */
434
+ const REPLACED_TABS_MAX = 16;
435
+ /**
436
+ * This call is served by a replacement route (`to`) for one that went away (`from`, else the caller's lostRoute):
437
+ * its answer starts with a notice that names both (browser-route-flow.ts noteRouteReplaced), once per replacement,
438
+ * and a later call that still names the old tab goes to the new one. A replacement that is the lost route itself (a
439
+ * recovery that handed the same tab back) is not one.
440
+ */
441
+ function announceReplacement(id, to, from, kind = 'lost') {
442
+ const lost = from || id.state.lostRoute;
443
+ id.state.lostRoute = null;
444
+ const next = { sessionId: to.sessionId, tabId: to.tabId || null };
445
+ if (lost && lost.sessionId === next.sessionId && (lost.tabId || null) === next.tabId)
446
+ return;
447
+ // A tab that is still open (`moved`) was not lost: status reports the move (routeMoved), not a lost tab.
448
+ if (kind === 'lost' && lost?.tabId && next.tabId) {
449
+ id.state.replacedTabs.delete(lost.tabId);
450
+ id.state.replacedTabs.set(lost.tabId, next);
451
+ while (id.state.replacedTabs.size > REPLACED_TABS_MAX)
452
+ id.state.replacedTabs.delete(id.state.replacedTabs.keys().next().value);
453
+ }
454
+ noteRouteReplaced({ kind, from: lost ? { sessionId: lost.sessionId, tabId: lost.tabId || null } : null, to: next });
455
+ }
456
+ /**
457
+ * The route this caller knew before a repin: the one it moved to earlier, else the one it last served a call on, else
458
+ * its launch pin. A route without a tab id is known by its session only.
459
+ */
460
+ function knownRoute(id) {
461
+ if (id.state.recoveredSessionId)
462
+ return { sessionId: id.state.recoveredSessionId, tabId: id.state.recoveredTabId };
463
+ if (id.state.servedRoute)
464
+ return id.state.servedRoute;
465
+ const pins = effectivePins(id);
466
+ const session = pins.browserSessionId || id.state.ancestorPinnedSession;
467
+ return session ? { sessionId: session, tabId: pins.browserTabId || id.state.ancestorPinnedTab || null } : null;
468
+ }
469
+ /**
470
+ * Whether `to` is a different route than `from`: another session, or another tab when both tab ids are known. A
471
+ * route it learns where it knew only the session (a pin without a tab) is the same route, not a move.
472
+ */
473
+ function isOtherRoute(from, to) {
474
+ if (!from)
475
+ return false;
476
+ return from.sessionId !== to.sessionId || (!!from.tabId && !!to.tabId && from.tabId !== to.tabId);
477
+ }
478
+ /** A pinned caller's route moves here (a recovery, its own browser_session create, a session of its own named by id). */
479
+ function repin(id, sessionId, tabId) {
480
+ const to = { sessionId, tabId: tabId || null };
481
+ const from = knownRoute(id);
482
+ // A move from a tab it knew (status says Replaced while it is current); adopting the route it was launched with, or
483
+ // the first one it learns, is not one.
484
+ if (from && isOtherRoute(from, to))
485
+ id.state.routeMoved = { from, to };
486
+ id.state.recoveredSessionId = sessionId;
487
+ id.state.recoveredTabId = tabId || null;
488
+ resolvedRoutes.delete(id);
489
+ }
490
+ /**
491
+ * The open tab this caller owns in `sessionId` (the server's tabOwners, under its routing id or its client id): the
492
+ * 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
493
+ * another owner's, is never returned). null for a session it has no open tab in, or one that is not the default browser.
494
+ */
495
+ function ownedTabIn(listing, sessionId, id, tabId) {
496
+ const session = (listing.sessions || []).find(s => s.id === sessionId);
497
+ if (!session || !servesDefault(session))
498
+ return null;
499
+ const owners = session.tabOwners || {};
500
+ const mine = [owners[terminalIdFor(id)], owners[id.clientId]].filter((t) => typeof t === 'string' && t.length > 0);
501
+ const open = (t) => !session.tabs || session.tabs.some(x => x.id === t);
502
+ if (tabId && mine.includes(tabId) && open(tabId))
503
+ return tabId;
504
+ return mine.find(open) || null;
505
+ }
506
+ /**
507
+ * 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
508
+ * as the run's route (POST /api/loop/recover-browser `adopt`), so a later MCP process of the run (a Codex turn, a
509
+ * restarted stdio child) follows it through yourRoute instead of the route the server knew before. Only a server
510
+ * that says it records it (`adoptRoute` in its session list): an older one would read the request as a recovery and
511
+ * open one more tab. Best-effort: the move holds in this process either way.
512
+ */
513
+ async function recordOwnRoute(id, listing, sessionId, tabId) {
514
+ if (listing.adoptRoute !== true)
515
+ return;
516
+ const terminalSessionId = effectivePins(id).terminalSessionId || id.state.ancestorPinnedTerminal || undefined;
517
+ if (!terminalSessionId)
518
+ return;
519
+ const recorded = await request('POST', '/api/loop/recover-browser', { terminalSessionId, adopt: { sessionId, tabId } });
520
+ if (recorded.error)
521
+ console.error(`[MCP] the run's move to session ${sessionId} tab ${tabId} was not recorded: ${recorded.error}`);
522
+ }
523
+ /**
524
+ * A pinned run gets a new tab of its own in SynaBun's shared browser (POST /api/loop/recover-browser), started when
525
+ * none runs. `lost`: the route it found gone (the server hands back the run's current route when that one is still
526
+ * open, e.g. after another MCP process of the run recovered first). `reason: 'create'`: the run's own
527
+ * browser_session create, always a new tab. The server records the move on the run (state file, run record).
528
+ */
529
+ async function recoverRunBrowser(id, lost, reason) {
530
+ const pins = effectivePins(id);
531
+ const terminalSessionId = pins.terminalSessionId || id.state.ancestorPinnedTerminal || undefined;
532
+ const body = {};
533
+ if (terminalSessionId)
534
+ body.terminalSessionId = terminalSessionId;
535
+ if (reason)
536
+ body.reason = reason;
537
+ if (lost?.sessionId)
538
+ body.lost = { sessionId: lost.sessionId, tabId: lost.tabId || null };
539
+ const recovered = await request('POST', '/api/loop/recover-browser', body, SESSION_CREATE_TIMEOUT);
540
+ if (!recovered.error && typeof recovered.sessionId === 'string' && !isBrowserStarting(recovered)) {
541
+ const tabId = typeof recovered.tabId === 'string' ? recovered.tabId : null;
542
+ repin(id, recovered.sessionId, tabId);
543
+ // A recovery (a new tab, or the route another process of the run moved to: `current`) serves this call; the
544
+ // run's own create is not a replacement it has to be told about.
545
+ if (reason === 'create')
546
+ id.state.lostRoute = null;
547
+ else
548
+ announceReplacement(id, { sessionId: recovered.sessionId, tabId }, id.state.lostRoute || (lost?.sessionId ? { sessionId: lost.sessionId, tabId: lost.tabId || null } : null));
549
+ }
550
+ return recovered;
551
+ }
323
552
  // Pages (localhost included) only run where the server says the default browser
324
553
  // is. Absent servesDefault (older server, or a managed / real-Chrome setup) keeps
325
554
  // the previous behavior.
@@ -341,11 +570,15 @@ function describeDefault(list) {
341
570
  function releaseTabFor(id) {
342
571
  if (!id.state.interactiveSessionId)
343
572
  return;
573
+ // The id the tab was acquired under (acquireInteractive), as the caller it is: the server releases a caller's own
574
+ // tabs only, and a run's only with its terminal token (a stdio child's own; an HTTP identity evicted outside any
575
+ // request has none, and the run's end releases its tabs on the server anyway).
576
+ const owner = terminalIdFor(id);
344
577
  try {
345
578
  void fetch(`${BASE_URL}/api/browser/release`, {
346
579
  method: 'POST',
347
- headers: { 'Content-Type': 'application/json' },
348
- body: JSON.stringify({ clientId: id.clientId }),
580
+ headers: { 'Content-Type': 'application/json', ...terminalHeaders(id) },
581
+ body: JSON.stringify({ clientId: owner }),
349
582
  }).catch(() => { });
350
583
  }
351
584
  catch { /* best-effort */ }
@@ -472,16 +705,25 @@ async function resolveSessionUncached(sessionId, autoCreate, tabId, platform = f
472
705
  // tool is refused, since the page it names is in the other browser. The
473
706
  // server refuses them too (NOT_DEFAULT_BROWSER).
474
707
  const wantsDefaultBrowser = platform || !!autoCreate;
708
+ let namedListing = null;
475
709
  if (sessionId && sessionId !== (pins.browserSessionId || id.state.ancestorPinnedSession)) {
476
710
  const listed = await request('GET', '/api/browser/sessions');
711
+ namedListing = listed;
477
712
  const named = (listed.sessions || []).find(s => s.id === sessionId);
478
713
  if (named && !servesDefault(named)) {
479
- if (!wantsDefaultBrowser) {
480
- 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.` };
714
+ // Ids of the browser this caller left when it chose another MoreLogin profile (useSessionProfile): the call
715
+ // goes to its own tab in the profile it works in now, and its answer names that tab.
716
+ const replaced = tabId ? id.state.replacedTabs.get(tabId) : undefined;
717
+ const left = id.state.lostRoute?.sessionId === sessionId || !!replaced;
718
+ if (!wantsDefaultBrowser && !left) {
719
+ const own = listed.yourProfile ? 'this session\'s browser' : 'the default browser';
720
+ 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.' : ''}` };
481
721
  }
482
722
  console.error(`[MCP] not using session ${sessionId} (${describeSession(named)}) — resolving ${describeDefault(listed)}`);
483
723
  sessionId = undefined;
484
- tabId = undefined;
724
+ // A tab that was replaced keeps its name: the call goes to its replacement (below).
725
+ if (!replaced)
726
+ tabId = undefined;
485
727
  }
486
728
  }
487
729
  // Resolve tab ID from explicit param or pin.
@@ -493,7 +735,7 @@ async function resolveSessionUncached(sessionId, autoCreate, tabId, platform = f
493
735
  await resolveFromAncestors(id);
494
736
  }
495
737
  const pinnedTab = pins.browserTabId || id.state.ancestorPinnedTab || undefined;
496
- const resolvedTabId = pinnedTab || tabId || undefined;
738
+ let resolvedTabId = pinnedTab || tabId || undefined;
497
739
  if (pinnedTab && tabId && tabId !== pinnedTab) {
498
740
  console.error(`[MCP] tabId override ignored: pinned=${pinnedTab} requested=${tabId}`);
499
741
  }
@@ -501,25 +743,88 @@ async function resolveSessionUncached(sessionId, autoCreate, tabId, platform = f
501
743
  // this caller to a specific browser session (multi-session isolation).
502
744
  // If pinned session died, check recovery cache first, then auto-create.
503
745
  const pinnedSession = pins.browserSessionId || id.state.ancestorPinnedSession || undefined;
746
+ // A pinned run that names a session of its own by hand (the one its browser_session create returned, or one it
747
+ // recovered into in an earlier process): that session and its own open tab there, never the dead pinned tab paired
748
+ // with it. The run's route moves there, here and on the server (recordOwnRoute). A session that is not the run's
749
+ // (another run's, or one that is gone) is ignored like a tab the pin overrides: the run works in its own route.
750
+ // Before 2026-11-04 it was paired with the pinned tab, a pair that exists nowhere (the server refused it, and the
751
+ // call reported the run's tab lost).
752
+ if (pinnedSession && sessionId && sessionId !== pinnedSession && sessionId !== id.state.recoveredSessionId) {
753
+ const listed = namedListing || await request('GET', '/api/browser/sessions');
754
+ const owned = ownedTabIn(listed, sessionId, id, tabId);
755
+ if (owned) {
756
+ repin(id, sessionId, owned);
757
+ await recordOwnRoute(id, listed, sessionId, owned);
758
+ return { sessionId, tabId: owned };
759
+ }
760
+ console.error(`[MCP] session ${sessionId} is not one of this run's: ignored, the run's own route is used`);
761
+ sessionId = undefined;
762
+ tabId = undefined;
763
+ resolvedTabId = pinnedTab;
764
+ }
504
765
  // Loop prompts intentionally pass their injected sessionId/tabId on every
505
766
  // call. Treat an explicit session that matches the pin as pinned traffic too;
506
767
  // otherwise the old fast path skipped liveness checks and returned a stale
507
- // tab forever after a renderer/page closed.
508
- if (pinnedSession && (!sessionId || sessionId === pinnedSession)) {
768
+ // tab forever after a renderer/page closed. The session the run moved to (a
769
+ // recovery, its own browser_session create) is the run's pinned traffic now.
770
+ if (pinnedSession && (!sessionId || sessionId === pinnedSession || sessionId === id.state.recoveredSessionId)) {
509
771
  // Check recovery cache first — avoids re-creating on every call after recovery.
510
772
  // Return the RECOVERED tab, never the stale pinned tab (which 404s).
511
773
  if (id.state.recoveredSessionId) {
512
774
  const recheck = await request('GET', '/api/browser/sessions');
513
775
  const alive = (recheck.sessions || []).find(s => s.id === id.state.recoveredSessionId);
514
- if (alive)
515
- return { sessionId: id.state.recoveredSessionId, tabId: id.state.recoveredTabId || undefined };
516
- id.state.recoveredSessionId = null; // recovered session also died — try fresh recovery
776
+ // The recovered TAB must still be there too: one closed while its session lives was returned forever
777
+ // ("Tab … not found in session" on every call, 2026-10-04).
778
+ const recoveredTab = id.state.recoveredTabId;
779
+ const tabAlive = !!alive && (!recoveredTab || !alive.tabs || alive.tabs.some(t => t.id === recoveredTab));
780
+ if (alive && tabAlive)
781
+ return { sessionId: id.state.recoveredSessionId, tabId: recoveredTab || undefined };
782
+ id.state.recoveredSessionId = null; // recovered session or tab also died — try fresh recovery
517
783
  id.state.recoveredTabId = null;
518
784
  }
519
785
  const check = await request('GET', '/api/browser/sessions');
520
- const active = (check.sessions || []).find(s => s.id === pinnedSession);
786
+ const listed = (check.sessions || []);
787
+ // The route the server recorded for this run (yourRoute): where a recovery or the run's own browser_session
788
+ // create moved it, also when that happened in an earlier MCP process of the run (a Codex turn, a restarted
789
+ // stdio child) that this one never heard of. Used while its tab is open; the pin otherwise.
790
+ const yours = check.yourRoute;
791
+ if (yours && typeof yours.sessionId === 'string' && typeof yours.tabId === 'string'
792
+ && (yours.sessionId !== pinnedSession || yours.tabId !== resolvedTabId)) {
793
+ const moved = listed.find(s => s.id === yours.sessionId);
794
+ if (moved && servesDefault(moved) && (!moved.tabs || moved.tabs.some(t => t.id === yours.tabId))) {
795
+ // A route this process already served calls on: a recorded route that differs from it is a move it is told
796
+ // about. The pin alone is not (the launch route, or a route that moved before this process started: status
797
+ // reports that one, from the pin, without a notice).
798
+ const served = id.state.servedRoute;
799
+ const servedElsewhere = !!served?.tabId && isOtherRoute(served, { sessionId: yours.sessionId, tabId: yours.tabId });
800
+ repin(id, yours.sessionId, yours.tabId);
801
+ // The server moved this run's route earlier (a recovery or a create in another MCP process of the run). A call
802
+ // that still names other ids, or that worked in another tab before, is told where it ran; one that names none
803
+ // in a process that never worked elsewhere just works there.
804
+ const namedTab = tabId || null;
805
+ if (id.state.lostRoute || servedElsewhere || (namedTab && namedTab !== yours.tabId) || (sessionId && sessionId !== yours.sessionId)) {
806
+ const pinnedListed = listed.find(s => s.id === pinnedSession);
807
+ const formerTab = namedTab || (servedElsewhere ? served.tabId : null) || resolvedTabId || null;
808
+ const formerOpen = !!pinnedListed && !!formerTab && (!pinnedListed.tabs || pinnedListed.tabs.some(t => t.id === formerTab));
809
+ announceReplacement(id, { sessionId: yours.sessionId, tabId: yours.tabId }, id.state.lostRoute || { sessionId: sessionId || pinnedSession, tabId: formerTab }, formerOpen ? 'moved' : 'lost');
810
+ }
811
+ return { sessionId: yours.sessionId, tabId: yours.tabId };
812
+ }
813
+ }
814
+ const active = listed.find(s => s.id === pinnedSession);
521
815
  // A run's pinned browser must be the default one. Hard-fail rather than work
522
816
  // in the wrong browser.
817
+ if (active && !servesDefault(active) && check.profileChosen === true) {
818
+ // The run chose another MoreLogin profile for itself (morelogin "use"): its launch tab is in the browser it
819
+ // left. It gets a tab of its own in the profile it works in now, and the server records that as its route.
820
+ const recovered = await recoverRunBrowser(id, { sessionId: pinnedSession, tabId: resolvedTabId });
821
+ if (isBrowserStarting(recovered))
822
+ return { error: String(recovered.error), starting: true };
823
+ if (!recovered.error && recovered.sessionId) {
824
+ return { sessionId: recovered.sessionId, tabId: recovered.tabId || undefined };
825
+ }
826
+ 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".` };
827
+ }
523
828
  if (active && !servesDefault(active)) {
524
829
  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.` };
525
830
  }
@@ -530,27 +835,41 @@ async function resolveSessionUncached(sessionId, autoCreate, tabId, platform = f
530
835
  if (tabAlive)
531
836
  return { sessionId: pinnedSession, tabId: resolvedTabId };
532
837
  console.error(`[MCP] Pinned tab ${resolvedTabId} not found in active session ${pinnedSession} — recovering`);
533
- const terminalSessionId = pins.terminalSessionId || undefined;
534
- const recovered = await request('POST', '/api/loop/recover-browser', terminalSessionId ? { terminalSessionId } : {}, SESSION_CREATE_TIMEOUT);
838
+ const recovered = await recoverRunBrowser(id, { sessionId: pinnedSession, tabId: resolvedTabId });
839
+ if (isBrowserStarting(recovered))
840
+ return { error: String(recovered.error), starting: true };
535
841
  if (!recovered.error && recovered.sessionId) {
536
- id.state.recoveredSessionId = recovered.sessionId;
537
- id.state.recoveredTabId = recovered.tabId || null;
538
- return { sessionId: id.state.recoveredSessionId, tabId: id.state.recoveredTabId || undefined };
842
+ return { sessionId: recovered.sessionId, tabId: recovered.tabId || undefined };
539
843
  }
540
- return { sessionId: pinnedSession, tabId: resolvedTabId }; // fall through to old tab (will fail gracefully)
844
+ // Never hand back the dead tab: every call would answer "Tab … not found" and none would recover. An
845
+ // error is not cached, so the next call tries the recovery again.
846
+ return { error: `Browser tab ${resolvedTabId} of session ${pinnedSession} is gone and recovery failed: ${recovered.error || 'no tab returned'}\n${LOST_ROUTE_HINT}` };
541
847
  }
542
848
  // Pinned session is gone — rejoin the SHARED loop browser with a fresh owned
543
849
  // tab (not an isolated new browser), and adopt the NEW tabId. Forwarding the
544
850
  // stale pinned tab into a different session would 404 every subsequent call.
545
851
  console.error(`[MCP] Pinned browser session ${pinnedSession} is gone — recovering via shared loop browser`);
546
- const terminalSessionId = pins.terminalSessionId || undefined;
547
- const recovered = await request('POST', '/api/loop/recover-browser', terminalSessionId ? { terminalSessionId } : {}, SESSION_CREATE_TIMEOUT);
852
+ const recovered = await recoverRunBrowser(id, { sessionId: pinnedSession, tabId: resolvedTabId });
853
+ if (isBrowserStarting(recovered))
854
+ return { error: String(recovered.error), starting: true };
548
855
  if (recovered.error || !recovered.sessionId) {
549
- return { error: `Pinned browser session ${pinnedSession} is no longer available and recovery failed: ${recovered.error || 'no session returned'}` };
856
+ return { error: `Pinned browser session ${pinnedSession} is no longer available and recovery failed: ${recovered.error || 'no session returned'}\n${LOST_ROUTE_HINT}` };
857
+ }
858
+ return { sessionId: recovered.sessionId, tabId: recovered.tabId || undefined };
859
+ }
860
+ // Ids of a tab of this caller's that went away. One SynaBun already replaced: the call goes to its replacement (the
861
+ // notice was given with the replacement). The one just lost (its own close, say): the call resolves its route as if
862
+ // it named none, which opens a tab of its own and says so, instead of failing on the old ids until it creates one.
863
+ if (tabId && !pinnedSession) {
864
+ const replaced = id.state.replacedTabs.get(tabId);
865
+ if (replaced)
866
+ return { sessionId: replaced.sessionId, tabId: replaced.tabId || undefined };
867
+ const lost = id.state.lostRoute;
868
+ if (lost && lost.tabId === tabId && (!sessionId || lost.sessionId === sessionId)) {
869
+ sessionId = undefined;
870
+ tabId = undefined;
871
+ resolvedTabId = undefined;
550
872
  }
551
- id.state.recoveredSessionId = recovered.sessionId;
552
- id.state.recoveredTabId = recovered.tabId || null;
553
- return { sessionId: id.state.recoveredSessionId, tabId: id.state.recoveredTabId || undefined };
554
873
  }
555
874
  if (sessionId) {
556
875
  // Checked above against the default browser; the server 404s a session that
@@ -571,6 +890,7 @@ async function resolveSessionUncached(sessionId, autoCreate, tabId, platform = f
571
890
  const alive = (check.sessions || []).find(s => s.id === id.state.interactiveSessionId);
572
891
  if (alive && servesDefault(alive))
573
892
  return { sessionId: id.state.interactiveSessionId, tabId: resolvedTabId || id.state.interactiveTabId || undefined };
893
+ id.state.lostRoute = { sessionId: id.state.interactiveSessionId, tabId: id.state.interactiveTabId };
574
894
  id.state.interactiveSessionId = null;
575
895
  id.state.interactiveTabId = null; // session gone — fall through to re-acquire
576
896
  }
@@ -582,6 +902,9 @@ async function resolveSessionUncached(sessionId, autoCreate, tabId, platform = f
582
902
  const created = await request('POST', '/api/browser/sessions', {
583
903
  url: autoCreate.url || 'about:blank',
584
904
  }, SESSION_CREATE_TIMEOUT);
905
+ // Still starting: not a failure (the server keeps the start for this caller's next call).
906
+ if (isBrowserStarting(created))
907
+ return { error: AUTO_CREATE_STARTING, starting: true };
585
908
  if (created.error) {
586
909
  if (/lock/i.test(created.error))
587
910
  return acquireInteractive(id, autoCreate.url);
@@ -618,6 +941,9 @@ async function resolveSessionUncached(sessionId, autoCreate, tabId, platform = f
618
941
  const created = await request('POST', '/api/browser/sessions', {
619
942
  url: autoCreate?.url || 'about:blank',
620
943
  }, SESSION_CREATE_TIMEOUT);
944
+ // Still starting: not a failure (the server keeps the start for this caller's next call).
945
+ if (isBrowserStarting(created))
946
+ return { error: AUTO_CREATE_STARTING, starting: true };
621
947
  if (created.error) {
622
948
  if (/lock/i.test(created.error))
623
949
  return acquireInteractive(id, autoCreate?.url);
@@ -631,20 +957,25 @@ async function resolveSessionUncached(sessionId, autoCreate, tabId, platform = f
631
957
  if (sessions.length > 0 && autoCreate) {
632
958
  return autoCreateSession();
633
959
  }
634
- if (sessions.length === 1) {
635
- // No autoCreate — caller wants to interact with an existing page (click, snapshot, etc.).
636
- // Adopting is ONLY safe for a pristine, unowned session (e.g. a page the user opened
637
- // manually in the NI UI). If ANOTHER automation already owns a tab here (it plain-created
638
- // the session or acquired a tab — see _tabOwners), adopting makes us a tab-less freeloader
639
- // that FAILS CLOSED (404) the moment the session goes multi-tab — the sidepanel-vs-CLI
640
- // collision. In that case acquire our OWN dedicated tab in the shared browser instead.
960
+ if (sessions.length > 0 && !resolvedTabId) {
961
+ // No autoCreate — the caller wants to interact with a page (click, snapshot, etc.). The server hands an automation
962
+ // only a tab it owns (server.js getTargetPage, since 2026-11-05): a page the user opened by hand in SynaBun's
963
+ // browser panel, or another automation's tab, is never adopted. A tab of its own there (a session it created, which
964
+ // this process forgot) is used; else acquire our OWN dedicated tab in the shared browser (it starts at about:blank:
965
+ // navigate it where the work is).
641
966
  const myTerminalId = terminalIdFor(id);
642
- const loneOwners = sessions[0].tabOwners || {};
643
- const ownedByOther = Object.keys(loneOwners).some(k => k !== id.clientId && k !== myTerminalId);
644
- if (ownedByOther) {
645
- return acquireInteractive(id, autoCreate?.url);
967
+ for (const s of sessions) {
968
+ const owners = s.tabOwners || {};
969
+ const mine = owners[myTerminalId] || owners[id.clientId];
970
+ if (mine && (s.tabs || []).some(t => t.id === mine)) {
971
+ id.state.affinitySessionId = s.id;
972
+ return { sessionId: s.id, tabId: mine };
973
+ }
646
974
  }
647
- id.state.affinitySessionId = sessions[0].id;
975
+ return acquireInteractive(id, undefined);
976
+ }
977
+ if (sessions.length === 1) {
978
+ // An explicit tab with no session: the server checks that the tab is this caller's own.
648
979
  return { sessionId: sessions[0].id, tabId: resolvedTabId };
649
980
  }
650
981
  if (sessions.length === 0) {
@@ -659,10 +990,10 @@ async function resolveSessionUncached(sessionId, autoCreate, tabId, platform = f
659
990
  // navigate reuses this same owned tab via the X-Synabun-Terminal routing.
660
991
  return acquireInteractive(id, undefined);
661
992
  }
662
- return { error: 'No browser sessions open. Use browser_session to create one first, or use browser_navigate with a URL to auto-create.' };
993
+ 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.' };
663
994
  }
664
995
  // Multiple available sessions — require explicit ID
665
- const list = sessions.map(s => ` ${s.id} — ${s.title || s.url}`).join('\n');
996
+ const list = sessions.map(s => ` ${s.id}${s.title || s.url ? ` — ${s.title || s.url}` : ''}`).join('\n');
666
997
  return { error: `Multiple browser sessions open. Specify sessionId:\n${list}` };
667
998
  }
668
999
  /**
@@ -684,11 +1015,25 @@ export async function resolveSession(sessionId, autoCreate, tabId, opts = {}) {
684
1015
  return { ...cached.route };
685
1016
  }
686
1017
  const result = await resolveSessionUncached(sessionId, autoCreate, tabId, platform);
687
- if (!('error' in result))
1018
+ if (!('error' in result)) {
688
1019
  resolvedRoutes.set(id, { key: routeKey(id, sessionId, tabId, platform || !!autoCreate), route: { ...result }, sessionId, tabId, autoCreate, platform });
1020
+ id.state.servedRoute = { sessionId: result.sessionId, tabId: result.tabId || null };
1021
+ }
689
1022
  browserMetric({ phase: 'session-resolution', cached: false, durationMs: performance.now() - startedAt });
690
1023
  return result;
691
1024
  }
1025
+ /**
1026
+ * resolveSession without the route cache: for a caller whose request found its session or tab gone (isMissingRoute)
1027
+ * and that recovers by itself. A pinned run gets its session again or a replacement tab (POST /api/loop/recover-browser),
1028
+ * and the next resolveSession reuses what this found. Batches stay on their route and never recover midway.
1029
+ */
1030
+ export async function refreshSession(sessionId, autoCreate, tabId, opts = {}) {
1031
+ const context = batchContext.getStore();
1032
+ if (context)
1033
+ return context.route;
1034
+ resolvedRoutes.delete(getIdentity());
1035
+ return resolutionRefresh.run(true, () => resolveSession(sessionId, autoCreate, tabId, opts));
1036
+ }
692
1037
  /** Batches require a concrete tab owned by this caller, verified once up front. */
693
1038
  export async function resolveBatchRoute(sessionId, tabId) {
694
1039
  const resolved = await resolveSession(sessionId, undefined, tabId);
@@ -721,13 +1066,192 @@ export async function invalidateCache(reason, id) {
721
1066
  export async function listSessions() {
722
1067
  return request('GET', '/api/browser/sessions');
723
1068
  }
1069
+ /**
1070
+ * What browser_session list / status show: the sessions this caller can see, each with the caller's own tabs only
1071
+ * (the server lists an automation nothing else, ownTabsOnly; an older server's other tabs are left out here by their
1072
+ * owner), and the route its next call uses. Reads only: never opens, recovers or moves a tab.
1073
+ */
1074
+ export async function browserRouteStatus() {
1075
+ const id = getIdentity();
1076
+ const pins = effectivePins(id);
1077
+ if (!pins.browserSessionId && !pins.browserTabId)
1078
+ await resolveFromAncestors(id);
1079
+ const listing = await request('GET', '/api/browser/sessions');
1080
+ const empty = { sessions: [], current: null, replacedTabs: [], lost: id.state.lostRoute, movedFromPin: false };
1081
+ if (listing.error)
1082
+ return { ...empty, error: listing.error };
1083
+ const raw = (listing.sessions || []);
1084
+ const keys = [terminalIdFor(id), id.clientId];
1085
+ const sessions = raw.map((s) => {
1086
+ const owners = s.tabOwners || {};
1087
+ const mine = new Set(keys.map(k => owners[k]).filter((t) => typeof t === 'string' && t.length > 0));
1088
+ const all = s.tabs || [];
1089
+ const tabs = all.filter(t => mine.has(t.id)).map(t => ({ id: t.id, url: t.url || '', title: t.title || '' }));
1090
+ const otherTabs = typeof s.otherTabs === 'number' ? s.otherTabs : all.length - tabs.length;
1091
+ return { id: s.id, tabs, otherTabs, servesDefault: servesDefault(s), profile: describeSession(s) };
1092
+ });
1093
+ const openTab = (sessionId, tabId) => {
1094
+ const session = sessions.find(x => x.id === sessionId);
1095
+ if (!session)
1096
+ return null;
1097
+ if (!tabId)
1098
+ return session.tabs[0] || null;
1099
+ return session.tabs.find(t => t.id === tabId) || null;
1100
+ };
1101
+ const pinnedSession = pins.browserSessionId || id.state.ancestorPinnedSession || null;
1102
+ const pinnedTab = pins.browserTabId || id.state.ancestorPinnedTab || null;
1103
+ const yours = listing.yourRoute;
1104
+ const cached = resolvedRoutes.get(id)?.route;
1105
+ const candidates = [];
1106
+ if (cached)
1107
+ candidates.push({ sessionId: cached.sessionId, tabId: cached.tabId || null, source: 'current' });
1108
+ if (id.state.recoveredSessionId)
1109
+ candidates.push({ sessionId: id.state.recoveredSessionId, tabId: id.state.recoveredTabId, source: 'recovered' });
1110
+ if (yours && typeof yours.sessionId === 'string' && typeof yours.tabId === 'string')
1111
+ candidates.push({ sessionId: yours.sessionId, tabId: yours.tabId, source: 'recorded' });
1112
+ if (pinnedSession)
1113
+ candidates.push({ sessionId: pinnedSession, tabId: pinnedTab, source: 'pinned' });
1114
+ if (id.state.interactiveSessionId)
1115
+ candidates.push({ sessionId: id.state.interactiveSessionId, tabId: id.state.interactiveTabId, source: 'own' });
1116
+ if (id.state.affinitySessionId)
1117
+ candidates.push({ sessionId: id.state.affinitySessionId, tabId: null, source: 'own' });
1118
+ for (const s of sessions)
1119
+ if (s.tabs[0])
1120
+ candidates.push({ sessionId: s.id, tabId: s.tabs[0].id, source: 'own' });
1121
+ let current = null;
1122
+ for (const c of candidates) {
1123
+ const tab = openTab(c.sessionId, c.tabId);
1124
+ if (tab) {
1125
+ current = { sessionId: c.sessionId, tabId: tab.id, open: true, url: tab.url, title: tab.title, source: c.source };
1126
+ break;
1127
+ }
1128
+ }
1129
+ // Nothing open: the route it had (pinned or last used), reported as gone.
1130
+ if (!current && candidates[0] && candidates[0].source !== 'own')
1131
+ current = { ...candidates[0], open: false };
1132
+ const replacedTabs = current?.open
1133
+ ? [...id.state.replacedTabs].filter(([, to]) => to.sessionId === current.sessionId && to.tabId === current.tabId).map(([from]) => from)
1134
+ : [];
1135
+ // Moved: the current route is not the launch pin, or it is where this process moved from a tab it knew. The route
1136
+ // the server recorded is not a move by itself: on a fresh run it is the launch route (live runs 188b4ed7, 2ed620e8
1137
+ // said "Replaced: yes" on their first call). A fresh process of a run that another process moved knows the move
1138
+ // from its launch pin only; with no tab pinned it cannot tell, and says no.
1139
+ const here = current?.open ? { sessionId: current.sessionId, tabId: current.tabId } : null;
1140
+ const fromPin = !!here && !!pinnedSession && isOtherRoute({ sessionId: pinnedSession, tabId: pinnedTab }, here);
1141
+ const moved = id.state.routeMoved;
1142
+ const fromHere = !!here && !!moved && moved.to.sessionId === here.sessionId && (!moved.to.tabId || moved.to.tabId === here.tabId);
1143
+ const movedFromPin = fromPin || fromHere;
1144
+ const movedFrom = fromHere ? moved.from : fromPin ? { sessionId: pinnedSession, tabId: pinnedTab } : null;
1145
+ const yourProfile = listing.yourProfile;
1146
+ const profile = yourProfile && typeof yourProfile.envId === 'string'
1147
+ ? { envId: yourProfile.envId, name: typeof yourProfile.name === 'string' ? yourProfile.name : null } : null;
1148
+ return { sessions, current, replacedTabs, lost: id.state.lostRoute, movedFromPin, movedFrom, profile };
1149
+ }
1150
+ /**
1151
+ * browser_session create: a new browser tab of this caller's own, where it continues.
1152
+ * - A pinned run (a loop, a dispatched worker): a new tab in SynaBun's shared browser (started when none runs). The
1153
+ * run's route moves there (repinned): its next calls go to it with or without ids, and the server records it on the
1154
+ * run (state file, run record, agent_status).
1155
+ * - Any other caller: what browser_navigate's auto-create gives it. Its own tab in the shared browser when that runs
1156
+ * on a persistent profile or other automations hold it (a second browser on that profile fails, or ends the shared
1157
+ * one); else a new browser, which becomes its session.
1158
+ * A url other than about:blank is opened in the new tab. A start the server is still running answers
1159
+ * BROWSER_STARTING (isBrowserStarting): the next create gets that same start, never a second browser.
1160
+ */
724
1161
  export async function createSession(url) {
725
- return request('POST', '/api/browser/sessions', {
726
- url: url || 'about:blank',
727
- }, SESSION_CREATE_TIMEOUT);
1162
+ const id = getIdentity();
1163
+ const pins = effectivePins(id);
1164
+ const target = url || 'about:blank';
1165
+ // The caller's own create is where it continues: nothing to announce about a tab it lost before.
1166
+ id.state.lostRoute = null;
1167
+ if (!pins.browserSessionId && !pins.browserTabId)
1168
+ await resolveFromAncestors(id);
1169
+ const pinned = pins.browserSessionId || id.state.ancestorPinnedSession;
1170
+ let created;
1171
+ let opened = false; // the new tab is already at `target`
1172
+ if (pinned) {
1173
+ created = await recoverRunBrowser(id, null, 'create');
1174
+ if (created.error || isBrowserStarting(created) || typeof created.sessionId !== 'string')
1175
+ return created;
1176
+ created = { ...created, repinned: true };
1177
+ }
1178
+ else {
1179
+ const listed = await request('GET', '/api/browser/sessions');
1180
+ if (listed.error)
1181
+ return listed;
1182
+ const all = (listed.sessions || []);
1183
+ const lockRisk = all.some(s => s.persistent || s.loopOwned || s.agentOwned || s.interactiveOwned);
1184
+ let shared = lockRisk;
1185
+ created = {};
1186
+ if (!lockRisk) {
1187
+ created = await request('POST', '/api/browser/sessions', { url: target }, SESSION_CREATE_TIMEOUT);
1188
+ if (isBrowserStarting(created))
1189
+ return created;
1190
+ if (created.error && /lock/i.test(created.error))
1191
+ shared = true;
1192
+ else if (created.error || typeof created.sessionId !== 'string')
1193
+ return created;
1194
+ else {
1195
+ id.state.affinitySessionId = created.sessionId;
1196
+ if (created.shared === true && typeof created.tabId === 'string') {
1197
+ id.state.interactiveSessionId = created.sessionId;
1198
+ id.state.interactiveTabId = created.tabId;
1199
+ }
1200
+ resolvedRoutes.delete(id);
1201
+ // A start handed over from an earlier create may not be at this URL yet.
1202
+ opened = !created.handedOver;
1203
+ }
1204
+ }
1205
+ if (shared) {
1206
+ const acquired = await acquireInteractive(id, target);
1207
+ if ('error' in acquired)
1208
+ return { error: acquired.error, ...(acquired.starting && { starting: true, code: 'BROWSER_STARTING' }) };
1209
+ resolvedRoutes.delete(id);
1210
+ created = { ok: true, sessionId: acquired.sessionId, tabId: acquired.tabId || null, shared: true };
1211
+ }
1212
+ }
1213
+ const sessionId = created.sessionId;
1214
+ const tabId = typeof created.tabId === 'string' ? created.tabId : undefined;
1215
+ if (!opened && target !== 'about:blank') {
1216
+ const went = await navigate(sessionId, target, tabId, undefined, 'none');
1217
+ if (went.error)
1218
+ return { ...created, url: 'about:blank', navigateError: went.error };
1219
+ return { ...created, url: typeof went.url === 'string' ? went.url : target };
1220
+ }
1221
+ return { ...created, url: typeof created.url === 'string' ? created.url : target };
728
1222
  }
1223
+ /**
1224
+ * Close a session. One that other automations still have tabs in is kept by the server, which closes only this
1225
+ * caller's own tab there (`sessionKept`). Either way this caller's cached route to it is forgotten: the next call
1226
+ * resolves again (a pinned run gets a tab back through its recovery).
1227
+ */
729
1228
  export async function closeSession(sessionId) {
730
- return request('DELETE', `/api/browser/sessions/${sessionId}`);
1229
+ const result = await request('DELETE', `/api/browser/sessions/${sessionId}`);
1230
+ if (!result.error) {
1231
+ const id = getIdentity();
1232
+ // The tab that went (the server's releasedTabId, else the one this caller routed to there): the next call that
1233
+ // opens a tab for this caller says it replaced this one.
1234
+ const cachedRoute = resolvedRoutes.get(id)?.route;
1235
+ const closedTab = typeof result.releasedTabId === 'string' ? result.releasedTabId
1236
+ : cachedRoute?.sessionId === sessionId ? cachedRoute.tabId || null
1237
+ : id.state.recoveredSessionId === sessionId ? id.state.recoveredTabId
1238
+ : id.state.interactiveSessionId === sessionId ? id.state.interactiveTabId : null;
1239
+ if (result.sessionKept !== true || closedTab)
1240
+ id.state.lostRoute = { sessionId, tabId: closedTab };
1241
+ if (resolvedRoutes.get(id)?.route.sessionId === sessionId)
1242
+ resolvedRoutes.delete(id);
1243
+ if (id.state.interactiveSessionId === sessionId) {
1244
+ id.state.interactiveSessionId = null;
1245
+ id.state.interactiveTabId = null;
1246
+ }
1247
+ if (id.state.affinitySessionId === sessionId)
1248
+ id.state.affinitySessionId = null;
1249
+ if (id.state.recoveredSessionId === sessionId) {
1250
+ id.state.recoveredSessionId = null;
1251
+ id.state.recoveredTabId = null;
1252
+ }
1253
+ }
1254
+ return result;
731
1255
  }
732
1256
  // ── Navigation ──
733
1257
  export async function navigate(sessionId, url, tabId, returnSnapshot, snapshot, snapshotOptions, assist) {
@@ -881,9 +1405,10 @@ export async function consoleMessages(sessionId, tabId, opts) {
881
1405
  const qs = params.size ? `?${params}` : '';
882
1406
  return request('GET', `/api/browser/sessions/${sessionId}/console${qs}`);
883
1407
  }
884
- // ── Advanced ──
885
- export async function evaluate(sessionId, script, tabId) {
886
- return request('POST', `/api/browser/sessions/${sessionId}/evaluate`, { script, ...(tabId && { tabId }) }, LONG_TIMEOUT);
1408
+ /** `recover: false`: a missing session or tab is reported, never retried on a replacement tab (see request()). */
1409
+ export async function evaluate(sessionId, script, tabId, opts) {
1410
+ const pace = opts?.pace ? { platform: opts.pace.platform, maxWaitMs: Math.max(0, Math.floor(opts.pace.maxWaitMs || 0)) } : null;
1411
+ return request('POST', `/api/browser/sessions/${sessionId}/evaluate`, { script, ...(tabId && { tabId }), ...(pace && { pace }) }, LONG_TIMEOUT + (pace?.maxWaitMs || 0), { recover: opts?.recover });
887
1412
  }
888
1413
  export async function extract(sessionId, script, tabId, opts) {
889
1414
  return request('POST', `/api/browser/sessions/${sessionId}/extract`, {
@@ -1105,6 +1630,47 @@ export async function moreloginStart(envId) {
1105
1630
  export async function moreloginStop(envId) {
1106
1631
  return request('POST', '/api/morelogin/stop', { envId }, LONG_TIMEOUT);
1107
1632
  }
1633
+ /**
1634
+ * This caller works in a MoreLogin profile of its own from now on (POST /api/morelogin/use): `envId`, `name` or
1635
+ * `profile` (either) of a profile MoreLogin lists, or `default: true` for the default of Settings → Browser. Only this
1636
+ * caller moves: the default AI browser and every other session stay where they are. The server releases what the
1637
+ * caller owned in the browser it leaves; here every route this process knew is forgotten, so its next browser call
1638
+ * resolves again and gets a tab of its own in the new profile (started then). The route it left is kept as its lost
1639
+ * route: a call that still names those ids goes to the new tab and says so.
1640
+ */
1641
+ export async function useSessionProfile(ref) {
1642
+ const id = getIdentity();
1643
+ const pins = effectivePins(id);
1644
+ if (!pins.browserSessionId && !pins.browserTabId)
1645
+ await resolveFromAncestors(id);
1646
+ const result = await request('POST', '/api/morelogin/use', { ...ref }, LONG_TIMEOUT);
1647
+ // A Neural Interface from before per-session profiles has no such route and answers it as the owner's.
1648
+ if (result.code === 'OWNER_ROUTE') {
1649
+ 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.' };
1650
+ }
1651
+ if (result.error || result.ok !== true || result.changed !== true)
1652
+ return result;
1653
+ const left = knownRoute(id) || (id.state.interactiveSessionId
1654
+ ? { sessionId: id.state.interactiveSessionId, tabId: id.state.interactiveTabId }
1655
+ : null);
1656
+ id.state.interactiveSessionId = null;
1657
+ id.state.interactiveTabId = null;
1658
+ id.state.affinitySessionId = null;
1659
+ id.state.recoveredSessionId = null;
1660
+ id.state.recoveredTabId = null;
1661
+ id.state.servedRoute = null;
1662
+ id.state.routeMoved = null;
1663
+ id.state.replacedTabs.clear();
1664
+ // Only a route the server released is a lost one (a profile that is the default too keeps its tab). The server
1665
+ // names the tab it closed; this process may have known its route by the session alone (a tab-less route).
1666
+ const released = (Array.isArray(result.released) ? result.released : []);
1667
+ const gone = released.find(r => r && r.sessionId === left?.sessionId) || released[0];
1668
+ id.state.lostRoute = gone && typeof gone.sessionId === 'string'
1669
+ ? { sessionId: gone.sessionId, tabId: typeof gone.tabId === 'string' ? gone.tabId : left?.sessionId === gone.sessionId ? left.tabId : null }
1670
+ : null;
1671
+ resolvedRoutes.delete(id);
1672
+ return result;
1673
+ }
1108
1674
  export async function moreloginUseDefault(envId) {
1109
1675
  return request('POST', '/api/morelogin/use-default', envId ? { envId } : { clear: true });
1110
1676
  }