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
@@ -0,0 +1,143 @@
1
+ // ── A session's own MoreLogin profile: what it resolves to, and where the choice is kept (DOM-free) ──
2
+ //
3
+ // A run can be launched with a MoreLogin profile (a schedule's, a group's, a timer's, a launch's, a dispatch's:
4
+ // server.js pinRunMoreLoginProfile). A session can also choose one for itself while it runs (the `morelogin` tool's
5
+ // "use" action, POST /api/morelogin/use): the same pin, set for the caller's own owner key and for nobody else. The
6
+ // default of Settings → Browser and every other session stay as they are.
7
+ //
8
+ // The pin map lives in memory, and a run's launch pin is read back from its loop file after a restart. A choice a
9
+ // session made itself has no such file (a side panel, the Assistant's brain, a CLI window), and losing it would send
10
+ // that session's next browser call to the default profile without a word: another account. So choices are kept in one
11
+ // small file (DATA_HOME/data/browser-profile-choices.json) and put back into the pin map when the server starts.
12
+ // `envId: null` is a choice too: "the Browser settings default", made by a run that was launched with a profile.
13
+
14
+ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
15
+ import { dirname } from 'node:path';
16
+
17
+ /** Choices kept at most (the oldest go first). */
18
+ export const PROFILE_CHOICES_MAX = 500;
19
+ /** A choice older than this is not put back after a restart: no session lives that long. */
20
+ export const PROFILE_CHOICE_MAX_AGE_MS = 14 * 24 * 60 * 60 * 1000;
21
+
22
+ const OWNER_RE = /^[A-Za-z0-9_:-]{1,128}$/;
23
+ const cleanId = (value) => {
24
+ const id = value === null || value === undefined ? '' : String(value).trim();
25
+ return id && id.length <= 128 && !/[\u0000-\u001f]/.test(id) ? id : '';
26
+ };
27
+ const cleanName = (value) => (typeof value === 'string' ? value.trim().slice(0, 200) : '');
28
+
29
+ function refusal(status, code, message, extra = {}) {
30
+ return Object.assign(new Error(message), { status, code, ...extra });
31
+ }
32
+
33
+ /**
34
+ * The profile a reference names among MoreLogin's listed profiles.
35
+ * @param {{ envId?: unknown, name?: unknown, profile?: unknown }} ref `envId`: an id; `name`: a name; `profile`:
36
+ * either (an id first, then a name). Names match whole, without regard to case.
37
+ * @param {Array<{ id: unknown, name?: unknown }>} envs what MoreLogin lists
38
+ * @returns {{ envId: string, name: string|null }}
39
+ * @throws 400 PROFILE_REQUIRED (nothing named), 404 ENV_NOT_FOUND, 409 PROFILE_AMBIGUOUS (a name several profiles have)
40
+ */
41
+ export function resolveProfileRef(ref, envs) {
42
+ const rows = (Array.isArray(envs) ? envs : []).filter((env) => env && cleanId(env.id));
43
+ const byId = (id) => rows.find((env) => String(env.id) === id) || null;
44
+ const byName = (name) => rows.filter((env) => cleanName(env.name).toLowerCase() === name.toLowerCase());
45
+ const found = (env) => ({ envId: String(env.id), name: cleanName(env.name) || null });
46
+ const listed = () => (rows.length
47
+ ? ` MoreLogin lists: ${rows.slice(0, 20).map((env) => `${cleanName(env.name) || '(no name)'} (id ${env.id})`).join(', ')}${rows.length > 20 ? ', …' : ''}.`
48
+ : ' MoreLogin lists no profile.');
49
+ const envId = cleanId(ref?.envId);
50
+ const name = cleanName(ref?.name);
51
+ const either = cleanName(ref?.profile);
52
+ if (!envId && !name && !either) throw refusal(400, 'PROFILE_REQUIRED', 'Name the MoreLogin profile: its id (envId) or its name. The "list" action of the morelogin tool gives both.');
53
+ if (envId) {
54
+ const env = byId(envId);
55
+ if (env) return found(env);
56
+ if (!name && !either) throw refusal(404, 'ENV_NOT_FOUND', `MoreLogin has no profile with id ${envId.slice(0, 64)}.${listed()}`);
57
+ }
58
+ const wanted = name || either;
59
+ if (!envId && either) {
60
+ const env = byId(either);
61
+ if (env) return found(env);
62
+ }
63
+ const matches = byName(wanted);
64
+ if (matches.length === 1) return found(matches[0]);
65
+ if (matches.length > 1) {
66
+ throw refusal(409, 'PROFILE_AMBIGUOUS', `${matches.length} MoreLogin profiles are named "${wanted}" (ids ${matches.map((env) => env.id).join(', ')}): name the one you mean by its id.`,
67
+ { ids: matches.map((env) => String(env.id)) });
68
+ }
69
+ throw refusal(404, 'ENV_NOT_FOUND', `MoreLogin has no profile named "${wanted.slice(0, 64)}"${envId ? ` and none with id ${envId.slice(0, 64)}` : ''}.${listed()}`);
70
+ }
71
+
72
+ /**
73
+ * The choices file. Every method is synchronous and never throws: a file that cannot be read is no choices, and a
74
+ * write that fails is logged and leaves the in-memory pin (the session's choice still holds until a restart).
75
+ * @param {{ file: string, now?: () => number, log?: (line: string) => void, max?: number, maxAgeMs?: number }} opts
76
+ */
77
+ export function createProfileChoiceStore({ file, now = Date.now, log = () => {}, max = PROFILE_CHOICES_MAX, maxAgeMs = PROFILE_CHOICE_MAX_AGE_MS } = {}) {
78
+ /** ownerKey → { envId: string|null, name: string|null, at: number }, oldest first. */
79
+ let choices = null;
80
+
81
+ function read() {
82
+ const out = new Map();
83
+ try {
84
+ if (!file || !existsSync(file)) return out;
85
+ const data = JSON.parse(readFileSync(file, 'utf-8'));
86
+ const rows = data && typeof data === 'object' && data.choices && typeof data.choices === 'object' ? Object.entries(data.choices) : [];
87
+ const fresh = rows
88
+ .filter(([owner, row]) => OWNER_RE.test(owner) && row && typeof row === 'object' && Number.isFinite(Number(row.at)) && now() - Number(row.at) <= maxAgeMs)
89
+ .sort((a, b) => Number(a[1].at) - Number(b[1].at));
90
+ for (const [owner, row] of fresh) {
91
+ const envId = cleanId(row.envId);
92
+ out.set(owner, { envId: envId || null, name: envId ? (cleanName(row.name) || null) : null, at: Number(row.at) });
93
+ }
94
+ } catch (error) {
95
+ log(`the sessions' MoreLogin profile choices could not be read (${error?.message || error}): none is put back`);
96
+ }
97
+ return out;
98
+ }
99
+
100
+ function write() {
101
+ if (!file) return;
102
+ try {
103
+ mkdirSync(dirname(file), { recursive: true });
104
+ const temp = `${file}.${process.pid}.tmp`;
105
+ writeFileSync(temp, JSON.stringify({ version: 1, choices: Object.fromEntries(choices) }, null, 2));
106
+ renameSync(temp, file);
107
+ } catch (error) {
108
+ log(`a session's MoreLogin profile choice was not saved (${error?.message || error}): it holds until the server restarts`);
109
+ }
110
+ }
111
+
112
+ const loaded = () => (choices ||= read());
113
+
114
+ return {
115
+ /** Every kept choice: [ownerKey, { envId, name }], oldest first. */
116
+ entries() {
117
+ return [...loaded()].map(([owner, row]) => [owner, { envId: row.envId, name: row.name }]);
118
+ },
119
+ /** The choice of one owner ({ envId, name }; envId null = the default), or undefined when it made none. */
120
+ get(ownerKey) {
121
+ const row = loaded().get(ownerKey);
122
+ return row ? { envId: row.envId, name: row.name } : undefined;
123
+ },
124
+ /** Keep `ownerKey`'s choice: a profile, or null for "the Browser settings default". */
125
+ set(ownerKey, choice) {
126
+ if (!OWNER_RE.test(String(ownerKey || ''))) return false;
127
+ const envId = cleanId(choice?.envId);
128
+ const map = loaded();
129
+ map.delete(ownerKey);
130
+ map.set(ownerKey, { envId: envId || null, name: envId ? (cleanName(choice?.name) || null) : null, at: now() });
131
+ while (map.size > max) map.delete(map.keys().next().value);
132
+ write();
133
+ return true;
134
+ },
135
+ /** Forget `ownerKey`'s choice (it never made one). */
136
+ delete(ownerKey) {
137
+ const map = loaded();
138
+ if (!map.delete(ownerKey)) return false;
139
+ write();
140
+ return true;
141
+ },
142
+ };
143
+ }
@@ -0,0 +1,142 @@
1
+ /**
2
+ * One face for the two kinds of browser profile SynaBun drives over the Chrome DevTools protocol:
3
+ * • MoreLogin profiles (lib/morelogin.js): the MoreLogin app's own, reached through its local API;
4
+ * • local profiles (lib/local-profiles.js): a Chrome SynaBun starts itself with a data folder of its own.
5
+ *
6
+ * Everything server.js does with a profile is keyed by the profile's id (the default AI browser, the owner's list of
7
+ * profiles agents may choose, a run's or a session's pin, the pickers, start / stop). So this module re-exports
8
+ * lib/morelogin.js as it is and routes the calls that take an id: a local id (`lp-` + 12 hex characters) goes to the
9
+ * local launcher, every other id to MoreLogin. The lists hold both kinds; a local row says `kind: 'local'`.
10
+ *
11
+ * A local profile is never created in, listed by or started through MoreLogin, and `createProfile` stays MoreLogin's.
12
+ */
13
+ import * as ml from './morelogin.js';
14
+ import { LOCAL_PROFILE_PORT_RANGE, createLocalProfiles, isLocalProfileId } from './local-profiles.js';
15
+
16
+ export * from './morelogin.js';
17
+ export { isLocalProfileId, LOCAL_PROFILE_PORT_RANGE };
18
+
19
+ let _local = null;
20
+
21
+ /** Sets up the local profiles of this process (server.js, once; a test, with its own root). → the instance. */
22
+ export function configureLocalProfiles(options) {
23
+ _local = options ? createLocalProfiles(options) : null;
24
+ return _local;
25
+ }
26
+ /** The local profiles, or null where none were set up. */
27
+ export function localProfiles() { return _local; }
28
+
29
+ function local() {
30
+ if (!_local) throw Object.assign(new Error('Local profiles are not available in this process.'), { status: 500, code: 'LOCAL_PROFILES_UNAVAILABLE' });
31
+ return _local;
32
+ }
33
+
34
+ /** Rows of the local profiles in the list's shape ([] where none were set up). Throws only for an unreadable store. */
35
+ export async function listLocalProfiles() { return _local ? _local.listLocalProfiles() : []; }
36
+ export async function localProfileDetails() { return local().localProfileDetails(); }
37
+ export async function createLocalProfile(input) { return local().createLocalProfile(input); }
38
+ export async function updateLocalProfile(id, input) { return local().updateLocalProfile(id, input); }
39
+ export async function deleteLocalProfile(id) { return local().deleteLocalProfile(id); }
40
+ export async function checkLocalProfileProxy(id, override) { return local().checkLocalProfileProxy(id, override); }
41
+ export async function adoptLocalProfiles() { return _local ? _local.adoptRunning() : []; }
42
+ /** How many local profiles there are (0 where none were set up; nothing is started or probed). */
43
+ export function localProfileCount() { return _local ? _local.count() : 0; }
44
+ /** A local profile's name, or null. */
45
+ export function localProfileName(id) { return _local && isLocalProfileId(id) ? _local.nameOf(id) : null; }
46
+ /** { name, timezone, language } of a local profile, or null. */
47
+ export function localProfileSettings(id) { return _local && isLocalProfileId(id) ? _local.settingsOf(id) : null; }
48
+ /**
49
+ * The loopback ports SynaBun keeps for local profiles ({ from, to }): every proxy relay and every DevTools port of a
50
+ * local profile is inside it. A guarded worker's sandbox denies the whole range (server.js workerBrowserControlPorts).
51
+ */
52
+ export function localProfilePortRange() { return _local ? _local.portRange : LOCAL_PROFILE_PORT_RANGE; }
53
+ /** The loopback ports of the local profiles' proxy relays. */
54
+ export function localRelayPorts() { try { return _local ? _local.relayPorts() : []; } catch { return []; } }
55
+
56
+ /** A short reason of a failed MoreLogin call, as the lists carry it. */
57
+ function describeError(err) {
58
+ return { message: String(err?.message || err), code: err?.code || null };
59
+ }
60
+
61
+ /**
62
+ * Every profile: MoreLogin's, then the local ones. → { envs, moreloginError, localCount }.
63
+ * `moreloginError` ({ message, code } or null): MoreLogin's list could not be read (the app is closed, its API
64
+ * refused); `envs` then holds the local profiles alone. Never throws for MoreLogin; an unreadable local store does.
65
+ */
66
+ export async function listAllProfiles(opts = {}) {
67
+ const firstPage = (opts.pageNo || 1) === 1;
68
+ const localRows = firstPage ? await listLocalProfiles() : [];
69
+ let remote = [];
70
+ let moreloginError = null;
71
+ try { remote = await ml.listProfiles(opts); } catch (err) { moreloginError = describeError(err); moreloginError.cause = err; }
72
+ return { envs: [...remote, ...localRows], moreloginError, localCount: localRows.length };
73
+ }
74
+
75
+ /**
76
+ * The list server.js reads everywhere: MoreLogin's profiles and the local ones. When MoreLogin cannot be reached the
77
+ * local ones are returned, and the array carries the reason as `moreloginError` (not enumerable). With no local
78
+ * profile at all the answer is MoreLogin's alone, its failure included, as it was before local profiles existed.
79
+ */
80
+ export async function listProfiles(opts = {}) {
81
+ const all = await listAllProfiles(opts);
82
+ if (all.moreloginError && !all.localCount) throw all.moreloginError.cause;
83
+ const rows = all.envs;
84
+ Object.defineProperty(rows, 'moreloginError', { value: all.moreloginError ? { message: all.moreloginError.message, code: all.moreloginError.code } : null, enumerable: false });
85
+ return rows;
86
+ }
87
+
88
+ /**
89
+ * Start a profile → { debugPort, webdriver, kernel }. A local id starts SynaBun's own Chrome. `opts.owner === true`
90
+ * is the owner's own Start: only that may end a running local profile to start it again. Every other caller of a
91
+ * local profile that has to be restarted first (its proxy changed) or that does not answer is refused with
92
+ * LOCAL_PROFILE_RESTART_NEEDED / LOCAL_PROFILE_BUSY, and nothing here ever answers with another profile in its place.
93
+ */
94
+ export async function startProfile(envId, opts = {}) {
95
+ if (isLocalProfileId(String(envId))) return local().startLocalProfile(String(envId), { owner: opts?.owner === true });
96
+ return ml.startProfile(envId, opts);
97
+ }
98
+
99
+ /** Stop a profile. */
100
+ export async function stopProfile(envId, opts = {}) {
101
+ if (isLocalProfileId(String(envId))) return local().stopLocalProfile(String(envId));
102
+ return ml.stopProfile(envId, opts);
103
+ }
104
+
105
+ /** One profile's status. A local one: { status: 'running' | 'stopped', debugPort }. */
106
+ export async function profileStatus(envId, opts = {}) {
107
+ if (isLocalProfileId(String(envId))) return local().localProfileStatus(String(envId));
108
+ return ml.profileStatus(envId, opts);
109
+ }
110
+
111
+ // A local profile is Chrome: its browser is known at once, without a lookup.
112
+ export async function profileKernelOf(envId, opts = {}) {
113
+ if (isLocalProfileId(String(envId))) return 'chromium';
114
+ return ml.profileKernelOf(envId, opts);
115
+ }
116
+ export function heldProfileKernel(envId, now) {
117
+ if (isLocalProfileId(String(envId))) return { known: true, kernel: 'chromium' };
118
+ return ml.heldProfileKernel(envId, now);
119
+ }
120
+ export function lookupProfileKernel(envId, opts = {}) {
121
+ if (isLocalProfileId(String(envId))) return Promise.resolve('chromium');
122
+ return ml.lookupProfileKernel(envId, opts);
123
+ }
124
+ export async function profileKernels(envIds, opts = {}) {
125
+ const ids = [...new Set((envIds || []).map(String))];
126
+ const remoteIds = ids.filter((id) => !isLocalProfileId(id));
127
+ const answer = remoteIds.length ? await ml.profileKernels(remoteIds, opts) : { kernels: {}, pending: 0 };
128
+ const kernels = { ...(answer.kernels || {}) };
129
+ for (const id of ids) if (isLocalProfileId(id)) kernels[id] = 'chromium';
130
+ return { kernels, pending: Number(answer.pending) || 0 };
131
+ }
132
+
133
+ // MoreLogin refuses a new profile when the account's plan holds no more. Its exact words are not documented and vary
134
+ // by language, so the match is loose: an API or HTTP refusal of the create call whose text speaks of a limit.
135
+ const PLAN_LIMIT_RE = /\b(?:limit\w*|quota|maximum|upgrade|plan|package|exceed\w*)\b|上限|套餐|超出|限制|额度/i;
136
+ /** Whether a failed `createProfile` reads as MoreLogin's plan limit. */
137
+ export function isMoreLoginPlanLimit(err) {
138
+ if (!err || (err.code !== 'MORELOGIN_API' && err.code !== 'MORELOGIN_HTTP')) return false;
139
+ // The hint SynaBun itself adds to an auth refusal is not MoreLogin's text.
140
+ const text = String(err.message || '').replace(/\(set the API ID and API Key[^)]*\)/g, '');
141
+ return PLAN_LIMIT_RE.test(text);
142
+ }
@@ -0,0 +1,84 @@
1
+ // ── What an explicit close of a browser session closes (DOM-free) ──
2
+ //
3
+ // Scheduled runs share one browser session (one MoreLogin profile), each in a tab of its own. On 2026-11-04 a
4
+ // curiosity-video run cleaned up with browser_session close on every session it listed, the shared one included,
5
+ // and the Bluesky reply-back run working in that session lost its tab mid-write ("Session not found"). An
6
+ // automation's close therefore never tears down a session another automation still works in: it closes the
7
+ // caller's own tab there and leaves the rest. The owner's own close (the Neural Interface page, which sends no
8
+ // X-Synabun-Terminal) is a supervised action and still closes the whole session.
9
+ //
10
+ // An automation closes or stops only what it works in (2026-11-05, review 972afb74, finding 4): before, an automation
11
+ // that owned nothing in a session nobody else held (the owner's manual session, or a profile only the owner used)
12
+ // destroyed it or stopped the profile, because "no other owner" read as "nobody to protect".
13
+
14
+ /** Whether `caller` works in `session`: a tab of its own there (open or not yet pruned), or a hold on the session. */
15
+ export function holdsSession(session, caller) {
16
+ if (!session || !caller) return false;
17
+ if (session._tabOwners?.has?.(caller)) return true;
18
+ return [session._loopOwned, session._interactiveOwned, session._agentOwnedSet].some((set) => !!set?.has?.(caller));
19
+ }
20
+
21
+ /**
22
+ * The automations other than `caller` that still work in `session`: owners of a tab that is open, and the loops,
23
+ * interactive MCP callers and agents that hold the session. Reads the session's own registries only.
24
+ */
25
+ export function otherSessionOwners(session, caller) {
26
+ const others = new Set();
27
+ const tabs = session?.tabs;
28
+ for (const [owner, tabId] of session?._tabOwners || []) {
29
+ if (owner !== caller && (!tabs || tabs.has(tabId))) others.add(owner);
30
+ }
31
+ for (const set of [session?._loopOwned, session?._interactiveOwned, session?._agentOwnedSet]) {
32
+ for (const owner of set || []) if (owner !== caller) others.add(owner);
33
+ }
34
+ return [...others];
35
+ }
36
+
37
+ /**
38
+ * What DELETE /api/browser/sessions/:id does for this caller.
39
+ * - `{ action: 'destroy' }`: the owner's close (no caller), a session that is gone, or an automation's close of a
40
+ * session it works in and nobody else does.
41
+ * - `{ action: 'release', others, ownTabId }`: an automation's close of a session it works in and others still use:
42
+ * only the caller's own tab (ownTabId, null when it has none open there) is closed and its claims dropped.
43
+ * - `{ action: 'refuse' }`: an automation's close of a session it does not work in (no tab of its own, no hold): the
44
+ * owner's to close.
45
+ */
46
+ export function sessionCloseDecision(session, caller) {
47
+ if (!session || !caller) return { action: 'destroy' };
48
+ if (!holdsSession(session, caller)) return { action: 'refuse', reason: 'not_owned' };
49
+ const others = otherSessionOwners(session, caller);
50
+ if (others.length === 0) return { action: 'destroy' };
51
+ const ownTabId = session._tabOwners?.get?.(caller) || null;
52
+ return { action: 'release', others, ownTabId: ownTabId && session.tabs?.has?.(ownTabId) !== false ? ownTabId : null };
53
+ }
54
+
55
+ /**
56
+ * What POST /api/morelogin/stop does for this caller. Stopping a MoreLogin profile closes its whole browser, and
57
+ * with it the tab of every run in every session on that profile: an agent that called the `morelogin` tool's `stop`
58
+ * on the shared profile ended the other runs' tabs three times between 2026-10-27 and 2026-11-03.
59
+ * - `{ action: 'stop' }`: the owner's stop (no caller), or an automation's stop of a profile it holds (it works in a
60
+ * session on it, or `startedBy` says it started the profile) where no other automation works.
61
+ * - `{ action: 'refuse', others, sessionIds }`: an automation's stop while others work there.
62
+ * - `{ action: 'refuse', reason: 'not_owned', others: [], sessionIds: [] }`: an automation's stop of a profile it does
63
+ * not hold: the owner's to stop.
64
+ * `sessions` is the server's session registry (id → session); a session is on the profile when it is a MoreLogin
65
+ * session with that env id. `startedBy(envId)` lists who started the profile through POST /api/morelogin/start.
66
+ */
67
+ export function profileStopDecision(sessions, envId, caller, { startedBy = () => [] } = {}) {
68
+ if (!caller || !envId) return { action: 'stop' };
69
+ const others = new Set();
70
+ const sessionIds = [];
71
+ let holds = false;
72
+ try { holds = [...(startedBy(String(envId)) || [])].includes(caller); } catch { holds = false; }
73
+ for (const [id, session] of sessions || []) {
74
+ if (session?._profileMode !== 'morelogin' || String(session?._mlEnvId ?? '') !== String(envId)) continue;
75
+ if (holdsSession(session, caller)) holds = true;
76
+ const theirs = otherSessionOwners(session, caller);
77
+ if (theirs.length === 0) continue;
78
+ sessionIds.push(id);
79
+ for (const owner of theirs) others.add(owner);
80
+ }
81
+ if (others.size) return { action: 'refuse', others: [...others], sessionIds };
82
+ if (!holds) return { action: 'refuse', reason: 'not_owned', others: [], sessionIds: [] };
83
+ return { action: 'stop' };
84
+ }
@@ -0,0 +1,202 @@
1
+ // ── A browser start its caller stopped waiting for is kept for that caller (DOM-free) ──
2
+ //
3
+ // A cold MoreLogin start waits up to two minutes for the app (server.js createBrowserSession), and nothing can cancel
4
+ // it once it runs. Before 2026-11-04 the MCP gave up on browser_session create after 70 s while the server went on and
5
+ // registered the session: the agent read "timed out", called create again and started a second browser, and the first
6
+ // stayed open, owned by an agent that never learned its id.
7
+ //
8
+ // Now one start runs per caller (`key`) at a time:
9
+ // - a caller that asks again while its start is still running waits for that same start (`joined`), never a second one;
10
+ // - a caller waits `waitMs` at most and is then answered `pending` (the routes answer 202 BROWSER_STARTING before their
11
+ // deadline, so the agent reads "still starting, call create again" rather than a timeout);
12
+ // - a start that finishes when none of its callers can be answered any more is parked for the key, and the caller's
13
+ // next request takes it (`parked`) instead of starting another;
14
+ // - a start that finishes while one of its callers still waits but another was already answered `pending` is parked
15
+ // too, as `delivered`: the caller that was told "call again" gets that same start on its retry, never a second one.
16
+ // A delivered start is in use, so nobody closes it when it is not taken;
17
+ // - a parked start nobody takes within `ttlMs` is handed to `onUnclaimed` (the route closes what it opened), with the
18
+ // time it was parked (`{ parkedAt }`), so the route can see whether its caller used it meanwhile;
19
+ // - a start that never settles is given up after `startDeadlineMs` (BROWSER_START_STUCK): its waiters are answered,
20
+ // the key is free again, and what the start makes if it ever finishes goes to `onUnclaimed`;
21
+ // - after `clear()` (shutdown) nothing is parked any more: every parked start and every one that finishes later goes
22
+ // to `onUnclaimed` at once.
23
+ // A start that fails is never parked: the next request starts again and gets the real error.
24
+
25
+ export const BROWSER_START_HANDOFF_TTL_MS = 10 * 60_000;
26
+ /** A cold MoreLogin start (two minutes for the app) plus a tab, with room to spare: past this a start is stuck. */
27
+ export const BROWSER_START_DEADLINE_MS = 6 * 60_000;
28
+ export const BROWSER_START_STUCK = 'BROWSER_START_STUCK';
29
+
30
+ /**
31
+ * @param {object} [opts]
32
+ * @param {() => number} [opts.now]
33
+ * @param {number} [opts.ttlMs] how long a parked start waits for its caller
34
+ * @param {number} [opts.startDeadlineMs] how long a start may run before it is given up
35
+ * @param {Function} [opts.setTimer] setTimeout stand-in (tests)
36
+ * @param {Function} [opts.clearTimer]
37
+ * @param {(message: string) => void} [opts.log]
38
+ */
39
+ export function createBrowserStartHandoff({
40
+ now = Date.now, ttlMs = BROWSER_START_HANDOFF_TTL_MS, startDeadlineMs = BROWSER_START_DEADLINE_MS,
41
+ setTimer = setTimeout, clearTimer = clearTimeout, log = () => {},
42
+ } = {}) {
43
+ const flights = new Map(); // key → { promise, waiters: Set<{ gone, left }>, timer, abandoned }
44
+ const parked = new Map(); // key → { value, at, timer, onUnclaimed, delivered }
45
+ let closed = false;
46
+
47
+ function unpark(key) {
48
+ const entry = parked.get(key);
49
+ if (!entry) return null;
50
+ parked.delete(key);
51
+ clearTimer(entry.timer);
52
+ return entry;
53
+ }
54
+
55
+ function park(key, value, onUnclaimed, { delivered = false } = {}) {
56
+ const previous = unpark(key);
57
+ if (previous) expire(key, previous);
58
+ const entry = { value, at: now(), onUnclaimed: delivered ? null : onUnclaimed, delivered, timer: null };
59
+ if (closed) { expire(key, entry); return; }
60
+ entry.timer = setTimer(() => {
61
+ if (parked.get(key) !== entry) return;
62
+ parked.delete(key);
63
+ expire(key, entry);
64
+ }, ttlMs);
65
+ entry.timer?.unref?.();
66
+ parked.set(key, entry);
67
+ log(delivered
68
+ ? `kept a browser start for ${key}: one of its callers had been told it was still starting`
69
+ : `parked a browser start for ${key}: its caller had stopped waiting`);
70
+ }
71
+
72
+ /** Hand a parked (or never-parked) value to its onUnclaimed. Resolves when that is done; never rejects. */
73
+ function expire(key, entry) {
74
+ if (typeof entry.onUnclaimed !== 'function') return Promise.resolve();
75
+ log(`a browser start for ${key} was not taken${closed ? ' (shutting down)' : ` within ${Math.round(ttlMs / 1000)} s`}`);
76
+ return Promise.resolve().then(() => entry.onUnclaimed(entry.value, { parkedAt: entry.at })).catch(() => {});
77
+ }
78
+
79
+ /**
80
+ * Run `start` for `key`, or hand over the one already running or parked.
81
+ * @param {string} key
82
+ * @param {() => Promise<any>} start resolves with the started value; rejects with the reason it failed
83
+ * @param {object} [opts]
84
+ * @param {() => boolean} [opts.gone] true once this caller can no longer be answered (its request ended)
85
+ * @param {number} [opts.waitMs] how long this caller waits before it is answered `pending`
86
+ * @param {(value) => boolean} [opts.alive] whether a parked value is still usable (a closed tab is not)
87
+ * @param {(value, info: { parkedAt: number }) => any} [opts.onUnclaimed] what to do with a value nobody took
88
+ * @returns {Promise<{ value: any, from: 'parked' | 'joined' | 'started' } | { pending: true, from: 'joined' | 'started' }>}
89
+ */
90
+ async function run(key, start, { gone = () => false, waitMs = Infinity, alive = null, onUnclaimed = null } = {}) {
91
+ const kept = unpark(key);
92
+ if (kept) {
93
+ if (!alive || safeAlive(alive, kept.value)) return { value: kept.value, from: 'parked' };
94
+ log(`a parked browser start for ${key} is gone: starting again`);
95
+ // What is left of it (a session whose tab closed) is closed like one nobody took.
96
+ expire(key, kept);
97
+ }
98
+ let flight = flights.get(key);
99
+ const from = flight ? 'joined' : 'started';
100
+ if (!flight) {
101
+ const current = { waiters: new Set(), promise: null, timer: null, abandoned: false };
102
+ const started = Promise.resolve().then(start);
103
+ // A start that never settles would hold the key for good (every later request only joins it).
104
+ const watchdog = new Promise((_, reject) => {
105
+ current.timer = setTimer(() => reject(Object.assign(
106
+ new Error(`The browser start did not finish within ${Math.round(startDeadlineMs / 60_000)} minutes and was given up. Call the browser tool again to start over.`),
107
+ { code: BROWSER_START_STUCK, status: 504 },
108
+ )), startDeadlineMs);
109
+ current.timer?.unref?.();
110
+ });
111
+ current.promise = Promise.race([started, watchdog]).then((value) => {
112
+ clearTimer(current.timer);
113
+ if (flights.get(key) === current) flights.delete(key);
114
+ // Answered by a caller that is still there: it has the value. Otherwise nobody does, so keep it for the key.
115
+ // When one caller has it and another was answered "still starting", keep it too (delivered): that caller's
116
+ // retry gets this start, not a second one.
117
+ const waiters = [...current.waiters];
118
+ const answered = waiters.some((w) => !w.left && !safeGone(w.gone));
119
+ const toldPending = waiters.some((w) => w.left);
120
+ if (!answered) park(key, value, onUnclaimed);
121
+ else if (toldPending) park(key, value, onUnclaimed, { delivered: true });
122
+ return value;
123
+ }, (error) => {
124
+ clearTimer(current.timer);
125
+ if (flights.get(key) === current) flights.delete(key);
126
+ if (error?.code === BROWSER_START_STUCK) {
127
+ current.abandoned = true;
128
+ log(`a browser start for ${key} was given up after ${Math.round(startDeadlineMs / 1000)} s`);
129
+ }
130
+ throw error;
131
+ });
132
+ // What a start given up on makes after all is nobody's: handed to onUnclaimed at once.
133
+ started.then((value) => {
134
+ if (current.abandoned) expire(key, { value, at: now(), onUnclaimed });
135
+ }, () => {});
136
+ // Every caller may have left before a failure: never an unhandled rejection.
137
+ current.promise.catch(() => {});
138
+ flights.set(key, current);
139
+ flight = current;
140
+ }
141
+ const waiter = { gone, left: false };
142
+ flight.waiters.add(waiter);
143
+ let timer = null;
144
+ try {
145
+ if (!(waitMs < Infinity)) return { value: await flight.promise, from };
146
+ const waited = await Promise.race([
147
+ flight.promise.then((value) => ({ value })),
148
+ new Promise((resolve) => { timer = setTimer(() => resolve(null), Math.max(0, waitMs)); }),
149
+ ]);
150
+ if (waited) return { value: waited.value, from };
151
+ // This caller is answered "still starting": it no longer counts as one the start can be answered to.
152
+ waiter.left = true;
153
+ return { pending: true, from };
154
+ } finally {
155
+ if (timer) clearTimer(timer);
156
+ // Once answered (or failed) this caller is done either way; a pending one stays marked as left.
157
+ if (!waiter.left) flight.waiters.delete(waiter);
158
+ }
159
+ }
160
+
161
+ return {
162
+ run,
163
+ /** Whether a start for `key` is running now. */
164
+ running: (key) => flights.has(key),
165
+ /** Whether a finished start is parked for `key`. */
166
+ hasParked: (key) => parked.has(key),
167
+ /** Whether the start parked for `key` is one a caller already has (delivered), not one nobody took. */
168
+ parkedDelivered: (key) => parked.get(key)?.delivered === true,
169
+ /**
170
+ * Shutdown: every parked start goes to its onUnclaimed now, and so does every start still running when it
171
+ * finishes. Resolves once the parked ones are handled (the caller bounds the wait).
172
+ */
173
+ clear() {
174
+ closed = true;
175
+ const done = [];
176
+ for (const key of [...parked.keys()]) {
177
+ const entry = unpark(key);
178
+ if (entry) done.push(expire(key, entry));
179
+ }
180
+ return Promise.allSettled(done).then(() => undefined);
181
+ },
182
+ };
183
+ }
184
+
185
+ function safeGone(gone) {
186
+ try { return gone() === true; } catch { return true; }
187
+ }
188
+
189
+ function safeAlive(alive, value) {
190
+ try { return alive(value) === true; } catch { return false; }
191
+ }
192
+
193
+ /**
194
+ * How long a route waits for a browser start before it answers BROWSER_STARTING: what is left of its request's
195
+ * deadline, less a margin so that answer, not the deadline's 408 (lib/browser-execution.js), is what the caller reads.
196
+ * `deadline` absent (a route without one): Infinity, the route waits for the start.
197
+ */
198
+ export function startWaitMs(deadline, { now = Date.now(), margin = 2_000 } = {}) {
199
+ const at = Number(deadline);
200
+ if (!Number.isFinite(at) || at <= 0) return Infinity;
201
+ return Math.max(0, at - now - margin);
202
+ }