synabun 2.2.2 → 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 (276) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +1 -1
  3. package/docs/whatsapp.md +52 -20
  4. package/lib/mcp-build.js +519 -0
  5. package/lib/mcp-dist.js +10 -4
  6. package/lib/server-supervisor.js +7 -1
  7. package/lib/virustotal.mjs +4 -2
  8. package/mcp-server/dist/http.d.ts +38 -0
  9. package/mcp-server/dist/http.d.ts.map +1 -1
  10. package/mcp-server/dist/http.js +75 -7
  11. package/mcp-server/dist/http.js.map +1 -1
  12. package/mcp-server/dist/services/browser-route-flow.d.ts +3 -2
  13. package/mcp-server/dist/services/browser-route-flow.d.ts.map +1 -1
  14. package/mcp-server/dist/services/browser-route-flow.js +4 -3
  15. package/mcp-server/dist/services/browser-route-flow.js.map +1 -1
  16. package/mcp-server/dist/services/embedding-service.d.ts.map +1 -1
  17. package/mcp-server/dist/services/embedding-service.js +2 -1
  18. package/mcp-server/dist/services/embedding-service.js.map +1 -1
  19. package/mcp-server/dist/services/neural-interface.d.ts +21 -0
  20. package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
  21. package/mcp-server/dist/services/neural-interface.js +76 -4
  22. package/mcp-server/dist/services/neural-interface.js.map +1 -1
  23. package/mcp-server/dist/services/tool-catalog.js +2 -2
  24. package/mcp-server/dist/services/tool-catalog.js.map +1 -1
  25. package/mcp-server/dist/tools/agents.d.ts +3 -2
  26. package/mcp-server/dist/tools/agents.d.ts.map +1 -1
  27. package/mcp-server/dist/tools/agents.js +6 -4
  28. package/mcp-server/dist/tools/agents.js.map +1 -1
  29. package/mcp-server/dist/tools/browser-advanced.d.ts +3 -1
  30. package/mcp-server/dist/tools/browser-advanced.d.ts.map +1 -1
  31. package/mcp-server/dist/tools/browser-advanced.js +30 -8
  32. package/mcp-server/dist/tools/browser-advanced.js.map +1 -1
  33. package/mcp-server/dist/tools/browser-navigate.d.ts +3 -3
  34. package/mcp-server/dist/tools/browser-navigate.d.ts.map +1 -1
  35. package/mcp-server/dist/tools/browser-navigate.js +19 -16
  36. package/mcp-server/dist/tools/browser-navigate.js.map +1 -1
  37. package/mcp-server/dist/tools/browser-observe.d.ts +2 -1
  38. package/mcp-server/dist/tools/browser-observe.d.ts.map +1 -1
  39. package/mcp-server/dist/tools/browser-observe.js +61 -20
  40. package/mcp-server/dist/tools/browser-observe.js.map +1 -1
  41. package/mcp-server/dist/tools/morelogin.d.ts +13 -3
  42. package/mcp-server/dist/tools/morelogin.d.ts.map +1 -1
  43. package/mcp-server/dist/tools/morelogin.js +81 -20
  44. package/mcp-server/dist/tools/morelogin.js.map +1 -1
  45. package/mcp-server/dist/tools/youtube/upload.d.ts.map +1 -1
  46. package/mcp-server/dist/tools/youtube/upload.js +225 -88
  47. package/mcp-server/dist/tools/youtube/upload.js.map +1 -1
  48. package/mcp-server/package-lock.json +2 -2
  49. package/mcp-server/package.json +1 -1
  50. package/mcp-server/src/http.ts +79 -7
  51. package/mcp-server/src/services/browser-route-flow.ts +4 -3
  52. package/mcp-server/src/services/embedding-service.ts +2 -1
  53. package/mcp-server/src/services/neural-interface.ts +77 -4
  54. package/mcp-server/src/services/tool-catalog.ts +2 -2
  55. package/mcp-server/src/tools/agents.ts +8 -4
  56. package/mcp-server/src/tools/browser-advanced.ts +29 -8
  57. package/mcp-server/src/tools/browser-navigate.ts +19 -16
  58. package/mcp-server/src/tools/browser-observe.ts +62 -20
  59. package/mcp-server/src/tools/morelogin.ts +81 -20
  60. package/mcp-server/src/tools/youtube/upload.ts +218 -99
  61. package/neural-interface/i18n/en.json +631 -123
  62. package/neural-interface/i18n/pt-BR.json +518 -10
  63. package/neural-interface/lib/assistant-api.js +26 -0
  64. package/neural-interface/lib/assistant-brains/claude.js +5 -0
  65. package/neural-interface/lib/assistant-catalog.js +114 -1
  66. package/neural-interface/lib/assistant-dispatch.js +71 -1
  67. package/neural-interface/lib/assistant-persona.js +14 -7
  68. package/neural-interface/lib/assistant-router.js +6 -4
  69. package/neural-interface/lib/assistant-runtime.js +3 -0
  70. package/neural-interface/lib/assistant-task-prompt.js +5 -0
  71. package/neural-interface/lib/automation-approval.js +249 -0
  72. package/neural-interface/lib/automation-browser-owner-access.js +82 -0
  73. package/neural-interface/lib/backup-service.js +16 -1
  74. package/neural-interface/lib/browser-background-tab.js +72 -3
  75. package/neural-interface/lib/browser-extraction.js +106 -45
  76. package/neural-interface/lib/browser-profile-choices.js +143 -0
  77. package/neural-interface/lib/browser-profiles.js +142 -0
  78. package/neural-interface/lib/browser-tab-recovery.js +153 -0
  79. package/neural-interface/lib/browser-tool-policy.js +2 -0
  80. package/neural-interface/lib/chromium-on-device-model.js +67 -0
  81. package/neural-interface/lib/claude-agent-bridge.js +7 -4
  82. package/neural-interface/lib/claude-model-catalog.js +12 -0
  83. package/neural-interface/lib/effort-levels.js +28 -2
  84. package/neural-interface/lib/local-profile-proxy.js +362 -0
  85. package/neural-interface/lib/local-profiles.js +929 -0
  86. package/neural-interface/lib/loop-logger.js +25 -4
  87. package/neural-interface/lib/maritaca/api.js +179 -0
  88. package/neural-interface/lib/maritaca/attachments.js +44 -0
  89. package/neural-interface/lib/maritaca/client.js +362 -0
  90. package/neural-interface/lib/maritaca/key-store.js +122 -0
  91. package/neural-interface/lib/maritaca/models.js +181 -0
  92. package/neural-interface/lib/maritaca/modes.js +39 -0
  93. package/neural-interface/lib/maritaca/opencode-proxy.js +236 -0
  94. package/neural-interface/lib/maritaca/opencode-stream.js +345 -0
  95. package/neural-interface/lib/maritaca/opencode.js +218 -0
  96. package/neural-interface/lib/maritaca/panel-bridge.js +273 -0
  97. package/neural-interface/lib/maritaca/sessions.js +198 -0
  98. package/neural-interface/lib/morelogin-agent-profiles.js +149 -0
  99. package/neural-interface/lib/morelogin.js +278 -14
  100. package/neural-interface/lib/native-loop-providers.js +3 -1
  101. package/neural-interface/lib/owner-authority.js +30 -12
  102. package/neural-interface/lib/page-session.js +43 -17
  103. package/neural-interface/lib/request-gate.js +2 -0
  104. package/neural-interface/lib/system-activity.js +1 -1
  105. package/neural-interface/lib/terminal-tokens.js +82 -5
  106. package/neural-interface/package-lock.json +3 -2
  107. package/neural-interface/package.json +2 -1
  108. package/neural-interface/public/assets/mascot/README.md +11 -0
  109. package/neural-interface/public/assets/mascot/ask.gif +0 -0
  110. package/neural-interface/public/assets/mascot/ask.png +0 -0
  111. package/neural-interface/public/assets/mascot/blocked.gif +0 -0
  112. package/neural-interface/public/assets/mascot/blocked.png +0 -0
  113. package/neural-interface/public/assets/mascot/budget.gif +0 -0
  114. package/neural-interface/public/assets/mascot/budget.png +0 -0
  115. package/neural-interface/public/assets/mascot/computer.gif +0 -0
  116. package/neural-interface/public/assets/mascot/computer.png +0 -0
  117. package/neural-interface/public/assets/mascot/edit.gif +0 -0
  118. package/neural-interface/public/assets/mascot/edit.png +0 -0
  119. package/neural-interface/public/assets/mascot/error.gif +0 -0
  120. package/neural-interface/public/assets/mascot/error.png +0 -0
  121. package/neural-interface/public/assets/mascot/hover-attention.gif +0 -0
  122. package/neural-interface/public/assets/mascot/hover-attention.png +0 -0
  123. package/neural-interface/public/assets/mascot/idle-hero.gif +0 -0
  124. package/neural-interface/public/assets/mascot/idle-hero.png +0 -0
  125. package/neural-interface/public/assets/mascot/idle.gif +0 -0
  126. package/neural-interface/public/assets/mascot/idle.png +0 -0
  127. package/neural-interface/public/assets/mascot/manifest.json +254 -0
  128. package/neural-interface/public/assets/mascot/mcp.gif +0 -0
  129. package/neural-interface/public/assets/mascot/mcp.png +0 -0
  130. package/neural-interface/public/assets/mascot/memory.gif +0 -0
  131. package/neural-interface/public/assets/mascot/memory.png +0 -0
  132. package/neural-interface/public/assets/mascot/offline.gif +0 -0
  133. package/neural-interface/public/assets/mascot/offline.png +0 -0
  134. package/neural-interface/public/assets/mascot/pick.gif +0 -0
  135. package/neural-interface/public/assets/mascot/pick.png +0 -0
  136. package/neural-interface/public/assets/mascot/plan.gif +0 -0
  137. package/neural-interface/public/assets/mascot/plan.png +0 -0
  138. package/neural-interface/public/assets/mascot/present.gif +0 -0
  139. package/neural-interface/public/assets/mascot/present.png +0 -0
  140. package/neural-interface/public/assets/mascot/project.gif +0 -0
  141. package/neural-interface/public/assets/mascot/project.png +0 -0
  142. package/neural-interface/public/assets/mascot/read.gif +0 -0
  143. package/neural-interface/public/assets/mascot/read.png +0 -0
  144. package/neural-interface/public/assets/mascot/runs.gif +0 -0
  145. package/neural-interface/public/assets/mascot/runs.png +0 -0
  146. package/neural-interface/public/assets/mascot/scenes/automation.gif +0 -0
  147. package/neural-interface/public/assets/mascot/scenes/automation.png +0 -0
  148. package/neural-interface/public/assets/mascot/scenes/browser.gif +0 -0
  149. package/neural-interface/public/assets/mascot/scenes/browser.png +0 -0
  150. package/neural-interface/public/assets/mascot/scenes/code.gif +0 -0
  151. package/neural-interface/public/assets/mascot/scenes/code.png +0 -0
  152. package/neural-interface/public/assets/mascot/scenes/complex.gif +0 -0
  153. package/neural-interface/public/assets/mascot/scenes/complex.png +0 -0
  154. package/neural-interface/public/assets/mascot/scenes/computer.gif +0 -0
  155. package/neural-interface/public/assets/mascot/scenes/computer.png +0 -0
  156. package/neural-interface/public/assets/mascot/scenes/design.gif +0 -0
  157. package/neural-interface/public/assets/mascot/scenes/design.png +0 -0
  158. package/neural-interface/public/assets/mascot/scenes/edit.gif +0 -0
  159. package/neural-interface/public/assets/mascot/scenes/edit.png +0 -0
  160. package/neural-interface/public/assets/mascot/scenes/generic.gif +0 -0
  161. package/neural-interface/public/assets/mascot/scenes/generic.png +0 -0
  162. package/neural-interface/public/assets/mascot/scenes/image_gen.gif +0 -0
  163. package/neural-interface/public/assets/mascot/scenes/image_gen.png +0 -0
  164. package/neural-interface/public/assets/mascot/scenes/manifest.json +173 -0
  165. package/neural-interface/public/assets/mascot/scenes/memory.gif +0 -0
  166. package/neural-interface/public/assets/mascot/scenes/memory.png +0 -0
  167. package/neural-interface/public/assets/mascot/scenes/quick.gif +0 -0
  168. package/neural-interface/public/assets/mascot/scenes/quick.png +0 -0
  169. package/neural-interface/public/assets/mascot/scenes/read.gif +0 -0
  170. package/neural-interface/public/assets/mascot/scenes/read.png +0 -0
  171. package/neural-interface/public/assets/mascot/scenes/research.gif +0 -0
  172. package/neural-interface/public/assets/mascot/scenes/research.png +0 -0
  173. package/neural-interface/public/assets/mascot/scenes/review.gif +0 -0
  174. package/neural-interface/public/assets/mascot/scenes/review.png +0 -0
  175. package/neural-interface/public/assets/mascot/scenes/search.gif +0 -0
  176. package/neural-interface/public/assets/mascot/scenes/search.png +0 -0
  177. package/neural-interface/public/assets/mascot/scenes/shell.gif +0 -0
  178. package/neural-interface/public/assets/mascot/scenes/shell.png +0 -0
  179. package/neural-interface/public/assets/mascot/scenes/thinking.gif +0 -0
  180. package/neural-interface/public/assets/mascot/scenes/thinking.png +0 -0
  181. package/neural-interface/public/assets/mascot/scenes/video_gen.gif +0 -0
  182. package/neural-interface/public/assets/mascot/scenes/video_gen.png +0 -0
  183. package/neural-interface/public/assets/mascot/scenes/web.gif +0 -0
  184. package/neural-interface/public/assets/mascot/scenes/web.png +0 -0
  185. package/neural-interface/public/assets/mascot/search.gif +0 -0
  186. package/neural-interface/public/assets/mascot/search.png +0 -0
  187. package/neural-interface/public/assets/mascot/shell.gif +0 -0
  188. package/neural-interface/public/assets/mascot/shell.png +0 -0
  189. package/neural-interface/public/assets/mascot/skill.gif +0 -0
  190. package/neural-interface/public/assets/mascot/skill.png +0 -0
  191. package/neural-interface/public/assets/mascot/sleep.gif +0 -0
  192. package/neural-interface/public/assets/mascot/sleep.png +0 -0
  193. package/neural-interface/public/assets/mascot/subagent.gif +0 -0
  194. package/neural-interface/public/assets/mascot/subagent.png +0 -0
  195. package/neural-interface/public/assets/mascot/success.gif +0 -0
  196. package/neural-interface/public/assets/mascot/success.png +0 -0
  197. package/neural-interface/public/assets/mascot/think.gif +0 -0
  198. package/neural-interface/public/assets/mascot/think.png +0 -0
  199. package/neural-interface/public/assets/mascot/wait.gif +0 -0
  200. package/neural-interface/public/assets/mascot/wait.png +0 -0
  201. package/neural-interface/public/assets/mascot/watch.gif +0 -0
  202. package/neural-interface/public/assets/mascot/watch.png +0 -0
  203. package/neural-interface/public/assets/mascot/web.gif +0 -0
  204. package/neural-interface/public/assets/mascot/web.png +0 -0
  205. package/neural-interface/public/onboarding.html +14 -1
  206. package/neural-interface/public/shared/agent-runtime-options.js +49 -8
  207. package/neural-interface/public/shared/agy/agy-panel.js +2 -2
  208. package/neural-interface/public/shared/api.js +4 -4
  209. package/neural-interface/public/shared/assistant/asst-ask-styles.js +8 -8
  210. package/neural-interface/public/shared/assistant/asst-ask.js +1 -1
  211. package/neural-interface/public/shared/assistant/asst-brain-picker.js +99 -9
  212. package/neural-interface/public/shared/assistant/asst-control.js +1 -1
  213. package/neural-interface/public/shared/assistant/asst-limit-wait.js +4 -4
  214. package/neural-interface/public/shared/assistant/asst-panel.js +6 -2
  215. package/neural-interface/public/shared/assistant/asst-rack-styles.js +4 -4
  216. package/neural-interface/public/shared/assistant/asst-render.js +6 -6
  217. package/neural-interface/public/shared/assistant/asst-route-models.js +64 -4
  218. package/neural-interface/public/shared/assistant/asst-route.js +24 -7
  219. package/neural-interface/public/shared/assistant/asst-routes-editor.js +154 -39
  220. package/neural-interface/public/shared/assistant/asst-run-stage.js +3 -3
  221. package/neural-interface/public/shared/assistant/asst-scene-styles.js +8 -111
  222. package/neural-interface/public/shared/assistant/asst-scenes.js +46 -318
  223. package/neural-interface/public/shared/assistant/asst-sidepanel.js +2 -2
  224. package/neural-interface/public/shared/assistant/asst-state.js +72 -2
  225. package/neural-interface/public/shared/assistant/asst-styles.js +58 -14
  226. package/neural-interface/public/shared/automation-approval-view.js +75 -0
  227. package/neural-interface/public/shared/cdx/cdx-panel.js +2 -2
  228. package/neural-interface/public/shared/context-menu-model.js +2 -0
  229. package/neural-interface/public/shared/editor/editor-panel-logic.js +10 -0
  230. package/neural-interface/public/shared/editor/editor-panel.js +10 -2
  231. package/neural-interface/public/shared/html-shell.js +30 -22
  232. package/neural-interface/public/shared/morelogin-profile-choices.js +107 -0
  233. package/neural-interface/public/shared/morelogin-profile-field.js +186 -0
  234. package/neural-interface/public/shared/mtc/mtc-client.js +181 -0
  235. package/neural-interface/public/shared/mtc/mtc-compose.js +40 -0
  236. package/neural-interface/public/shared/mtc/mtc-copy.js +253 -0
  237. package/neural-interface/public/shared/mtc/mtc-logic.js +478 -0
  238. package/neural-interface/public/shared/mtc/mtc-panel.js +1354 -0
  239. package/neural-interface/public/shared/mtc/mtc-render.js +172 -0
  240. package/neural-interface/public/shared/mtc/mtc-styles.js +437 -0
  241. package/neural-interface/public/shared/ocp-v2/ocp-v2-panel.js +6 -2
  242. package/neural-interface/public/shared/provider-icons.js +3 -0
  243. package/neural-interface/public/shared/schedules-view.js +7 -0
  244. package/neural-interface/public/shared/server-back.js +50 -0
  245. package/neural-interface/public/shared/settings/settings-browser-owner-access.js +72 -0
  246. package/neural-interface/public/shared/settings/settings-data.js +1 -0
  247. package/neural-interface/public/shared/settings/settings-ia.js +3 -1
  248. package/neural-interface/public/shared/settings/settings-local-profiles.js +153 -0
  249. package/neural-interface/public/shared/settings/settings-maritaca-opencode.js +105 -0
  250. package/neural-interface/public/shared/sidepanel-session-status.js +16 -2
  251. package/neural-interface/public/shared/state.js +1 -1
  252. package/neural-interface/public/shared/styles.css +356 -157
  253. package/neural-interface/public/shared/synabun-mascot-assets.js +67 -0
  254. package/neural-interface/public/shared/synabun-mascot.js +136 -12
  255. package/neural-interface/public/shared/synabun-ticker.js +3 -0
  256. package/neural-interface/public/shared/topright-overflow-logic.js +1 -0
  257. package/neural-interface/public/shared/ui-automation-approval.js +188 -0
  258. package/neural-interface/public/shared/ui-automation-studio.js +130 -90
  259. package/neural-interface/public/shared/ui-claude-panel.js +50 -2
  260. package/neural-interface/public/shared/ui-maritaca-panel.js +15 -0
  261. package/neural-interface/public/shared/ui-navbar.js +17 -0
  262. package/neural-interface/public/shared/ui-panels.js +3 -1
  263. package/neural-interface/public/shared/ui-schedules-studio.js +118 -17
  264. package/neural-interface/public/shared/ui-scroll-follow.js +3 -0
  265. package/neural-interface/public/shared/ui-settings.js +590 -27
  266. package/neural-interface/public/shared/ui-sidepanel-session-entry.js +8 -2
  267. package/neural-interface/public/shared/ui-sidepanel-sessions.js +3 -3
  268. package/neural-interface/public/shared/ui-sidepanel-tray.js +24 -0
  269. package/neural-interface/public/shared/ui-sidepanel-windows.js +1 -1
  270. package/neural-interface/scripts/build-mascot-assets.mjs +30 -0
  271. package/neural-interface/server.js +1784 -195
  272. package/npm-shrinkwrap.json +5 -17
  273. package/package.json +5 -6
  274. package/postinstall.js +38 -39
  275. package/setup.js +33 -31
  276. package/skills/synabun/modules/changelog.md +14 -0
@@ -0,0 +1,249 @@
1
+ // ── The owner's approval of what an agent changed (re-review of 2026-11-11, finding 3) ──
2
+ //
3
+ // A schedule an agent created or last changed (`taskBy: 'agent'`) and a template an agent created or changed
4
+ // (`editedBy: 'agent'`) are held to the owner's list of profiles agents may choose (lib/morelogin-agent-profiles.js
5
+ // scheduleProfileAuthority). Until 2026-11-11 an owner's save took the mark away when it carried certain fields, and the
6
+ // Schedules editor sent them by itself, so opening a marked schedule to rename it cleared the mark although the owner
7
+ // reviewed nothing. The owner's decision: no save ever clears a mark; an explicit approve action does, from the owner's
8
+ // page only, tied to the exact content the owner was shown.
9
+ //
10
+ // How:
11
+ // • The server builds the review (what will actually run and where) from the stored records: scheduleReview /
12
+ // templateReview. It shows the effective instructions (the template's task and context, every day theme's text,
13
+ // the two numbers a launch reads from `overrides`) and the account (the MoreLogin profile the run uses, by name,
14
+ // or the default browser of Settings → Browser), who chose that profile, and whether it is on the owner's list.
15
+ // • Its digest (reviewDigest) is a SHA-256 over the stored fields a launch reads for that content, never over the
16
+ // display text: the schedule's template, day themes, overrides, group, own profile, the marks; the template's task
17
+ // and context; the group's profile; the default browser. Anything an agent (or anybody) changes there after the
18
+ // review was served changes the digest, and the approve request, which carries the digest it was shown, is refused
19
+ // (409 REVIEW_CHANGED). Nothing about the request's own words is trusted: the server recomputes.
20
+ // • Approving a schedule clears its mark and, in the same act, the mark of its template (one flow for the owner).
21
+ // When the profile is the schedule's own, the owner approving "runs in <profile>" is the owner choosing that
22
+ // profile, so `moreloginEnvBy` becomes 'owner'. A group's profile chosen by an agent is the group's: approving one
23
+ // schedule never makes it the owner's for the group's other schedules (the review says so).
24
+ // • Any later change by an agent marks the record again (server.js markScheduleTaskAuthor / markTemplateAuthor).
25
+ // • Restored and imported records come in marked (`unreviewedFrom`: 'restore' / 'import'), because a file is data: no
26
+ // field in it can say "the owner approved this". markRecordsUnreviewed only adds marks, never removes one.
27
+ //
28
+ // DOM-free, no imports but node:crypto. Unit-tested in tests/automation-approval.test.mjs.
29
+
30
+ import { createHash } from 'node:crypto';
31
+
32
+ /** The version of the digest's input; bump it when the fields change, so an old review can never match. */
33
+ export const APPROVAL_DIGEST_VERSION = 'ap1';
34
+ /** The refusal codes of the approve routes. */
35
+ export const REVIEW_CHANGED = 'REVIEW_CHANGED';
36
+ export const NOTHING_TO_APPROVE = 'NOTHING_TO_APPROVE';
37
+ /** Where a mark came from when it was not an agent's own change (display only; authority reads `taskBy` / `editedBy`). */
38
+ export const UNREVIEWED_SOURCES = Object.freeze(['import', 'restore']);
39
+
40
+ const str = (value) => (value === null || value === undefined ? null : String(value));
41
+ const weekdayOrder = ['1', '2', '3', '4', '5', '6', '0'];
42
+
43
+ /** JSON with object keys sorted at every level, so equal content always hashes equally. */
44
+ export function canonicalJson(value) {
45
+ if (Array.isArray(value)) return `[${value.map((item) => canonicalJson(item === undefined ? null : item)).join(',')}]`;
46
+ if (value && typeof value === 'object') {
47
+ const keys = Object.keys(value).filter((key) => value[key] !== undefined).sort();
48
+ return `{${keys.map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key])}`).join(',')}}`;
49
+ }
50
+ return JSON.stringify(value === undefined ? null : value);
51
+ }
52
+
53
+ /** The day themes as a launch reads them: weekday → contextOverride text (empty entries left out). */
54
+ function themeTexts(dayThemes) {
55
+ const out = [];
56
+ if (!dayThemes || typeof dayThemes !== 'object') return out;
57
+ for (const day of weekdayOrder) {
58
+ const entry = dayThemes[day];
59
+ if (!entry) continue;
60
+ const text = typeof entry === 'string' ? entry : (typeof entry.contextOverride === 'string' ? entry.contextOverride : '');
61
+ out.push({ day: Number(day), text, label: typeof entry === 'object' && typeof entry.theme === 'string' ? entry.theme : null });
62
+ }
63
+ return out;
64
+ }
65
+
66
+ /** The stored template fields that decide its instructions (and its mark). */
67
+ function templateCore(template) {
68
+ if (!template) return null;
69
+ return {
70
+ id: str(template.id), name: str(template.name), task: str(template.task), context: str(template.context),
71
+ usesBrowser: template.usesBrowser === undefined ? null : !!template.usesBrowser,
72
+ editedBy: template.editedBy === 'agent' ? 'agent' : null, unreviewedFrom: str(template.unreviewedFrom),
73
+ };
74
+ }
75
+
76
+ /** The stored schedule fields that decide what it runs and where (and its mark). */
77
+ function scheduleCore(schedule) {
78
+ return {
79
+ id: str(schedule.id), templateId: str(schedule.templateId), dayThemes: schedule.dayThemes ?? null,
80
+ overrides: schedule.overrides ?? null, groupId: str(schedule.groupId),
81
+ moreloginEnvId: str(schedule.moreloginEnvId), moreloginEnvBy: str(schedule.moreloginEnvBy),
82
+ usesBrowser: schedule.usesBrowser === undefined ? null : !!schedule.usesBrowser,
83
+ taskBy: schedule.taskBy === 'agent' ? 'agent' : null, unreviewedFrom: str(schedule.unreviewedFrom),
84
+ };
85
+ }
86
+
87
+ /** The group fields a schedule's run reads for its account. */
88
+ function groupCore(group) {
89
+ if (!group) return null;
90
+ return {
91
+ id: str(group.id), moreloginEnvId: str(group.moreloginEnvId), moreloginEnvBy: str(group.moreloginEnvBy),
92
+ usesBrowser: group.usesBrowser === undefined ? null : !!group.usesBrowser,
93
+ };
94
+ }
95
+
96
+ /** The default browser of Settings → Browser, as far as the account goes. */
97
+ function defaultCore(browserDefault) {
98
+ const mode = ['attach', 'morelogin'].includes(browserDefault?.connectMode) ? browserDefault.connectMode : 'mirror';
99
+ return { connectMode: mode, moreloginEnvId: mode === 'morelogin' ? str(browserDefault?.moreloginEnvId) : null };
100
+ }
101
+
102
+ /** The digest of a review's input (hex SHA-256 of the canonical JSON, prefixed by APPROVAL_DIGEST_VERSION). */
103
+ export function reviewDigest(input) {
104
+ return `${APPROVAL_DIGEST_VERSION}:${createHash('sha256').update(canonicalJson({ v: APPROVAL_DIGEST_VERSION, ...input })).digest('hex')}`;
105
+ }
106
+
107
+ /** Whether `a` and `b` are the same digest (a string compare that takes the same time whatever matches). */
108
+ export function sameDigest(a, b) {
109
+ if (typeof a !== 'string' || typeof b !== 'string') return false;
110
+ const x = Buffer.from(String(a ?? ''));
111
+ const y = Buffer.from(String(b ?? ''));
112
+ if (x.length !== y.length || !x.length) return false;
113
+ let diff = 0;
114
+ for (let i = 0; i < x.length; i++) diff |= x[i] ^ y[i];
115
+ return diff === 0;
116
+ }
117
+
118
+ /** Whether a record carries a mark the approve action is for. */
119
+ export const scheduleMarked = (schedule) => schedule?.taskBy === 'agent';
120
+ export const templateMarked = (template) => template?.editedBy === 'agent';
121
+
122
+ /**
123
+ * The account a schedule's run uses: its own profile, else its group's, else the default browser.
124
+ * @param {{ allowed?: (envId: string) => boolean, profileName?: (envId: string) => string|null }} o
125
+ */
126
+ function accountOf(schedule, group, browserDefault, { allowed = () => false, profileName = () => null } = {}) {
127
+ const holder = schedule.moreloginEnvId ? schedule : (group?.moreloginEnvId ? group : null);
128
+ if (holder) {
129
+ const envId = String(holder.moreloginEnvId);
130
+ return {
131
+ kind: 'morelogin', envId, name: holder.moreloginEnvName || profileName(envId) || null,
132
+ source: holder === schedule ? 'schedule' : 'group', groupName: holder === schedule ? null : (group?.name || null),
133
+ chosenBy: holder.moreloginEnvBy === 'owner' ? 'owner' : 'agent', onList: allowed(envId) === true,
134
+ };
135
+ }
136
+ const def = defaultCore(browserDefault);
137
+ return {
138
+ kind: 'default', connectMode: def.connectMode, envId: def.moreloginEnvId,
139
+ name: def.moreloginEnvId ? (profileName(def.moreloginEnvId) || null) : null, source: 'default', chosenBy: 'owner', onList: null,
140
+ };
141
+ }
142
+
143
+ /**
144
+ * Where a marked schedule stands now, for the flag in its list and editor:
145
+ * 'no-profile' — it runs in the default browser (the owner's choice), the mark changes nothing today;
146
+ * 'on-list' — it runs in a profile on the owner's list, as today, mark or not;
147
+ * 'blocked' — it runs in a profile off the list, and its runs are refused until the owner approves it;
148
+ * 'clear' — nothing is marked.
149
+ */
150
+ export function approvalCase({ marked, account }) {
151
+ if (!marked) return 'clear';
152
+ if (!account || account.kind !== 'morelogin') return 'no-profile';
153
+ return account.onList ? 'on-list' : 'blocked';
154
+ }
155
+
156
+ /**
157
+ * The review of a schedule: what will run and where, and the digest the approve request must carry.
158
+ * @param {{ schedule: object, template?: object|null, group?: object|null, browserDefault?: object,
159
+ * allowed?: (envId: string) => boolean, profileName?: (envId: string) => string|null }} o
160
+ */
161
+ export function scheduleReview({ schedule, template = null, group = null, browserDefault = {}, allowed, profileName } = {}) {
162
+ if (!schedule) return null;
163
+ const account = accountOf(schedule, group, browserDefault, { allowed, profileName });
164
+ const marks = {
165
+ schedule: scheduleMarked(schedule), template: templateMarked(template),
166
+ scheduleFrom: scheduleMarked(schedule) ? (UNREVIEWED_SOURCES.includes(schedule.unreviewedFrom) ? schedule.unreviewedFrom : 'agent') : null,
167
+ templateFrom: templateMarked(template) ? (UNREVIEWED_SOURCES.includes(template.unreviewedFrom) ? template.unreviewedFrom : 'agent') : null,
168
+ };
169
+ const overrides = schedule.overrides && typeof schedule.overrides === 'object' ? schedule.overrides : {};
170
+ const digest = reviewDigest({
171
+ kind: 'schedule', schedule: scheduleCore(schedule), template: templateCore(template), group: groupCore(group),
172
+ browserDefault: account.kind === 'default' ? defaultCore(browserDefault) : null,
173
+ });
174
+ // After approval: the schedule's own profile becomes the owner's choice; a group's stays whoever chose it.
175
+ const ownerAfter = account.kind !== 'morelogin' || account.source === 'schedule' || account.chosenBy === 'owner';
176
+ return {
177
+ kind: 'schedule', id: str(schedule.id), name: str(schedule.name), digest,
178
+ marks, needsApproval: marks.schedule || marks.template,
179
+ case: approvalCase({ marked: marks.schedule || marks.template, account }),
180
+ instructions: {
181
+ templateId: str(schedule.templateId), templateName: template ? str(template.name) : null, templateMissing: !template,
182
+ task: template ? str(template.task) : null, context: template ? str(template.context) : null,
183
+ dayThemes: themeTexts(schedule.dayThemes),
184
+ iterations: overrides.iterations ?? null, maxMinutes: overrides.maxMinutes ?? null,
185
+ },
186
+ account,
187
+ afterApproval: account.kind !== 'morelogin' ? 'default' : (ownerAfter ? 'owner' : 'list'),
188
+ };
189
+ }
190
+
191
+ /**
192
+ * The review of a template: its instructions, and which schedules run it (with their accounts, for display).
193
+ * @param {{ template: object, schedules?: object[], groups?: object[], browserDefault?: object, allowed?, profileName? }} o
194
+ */
195
+ export function templateReview({ template, schedules = [], groups = [], browserDefault = {}, allowed, profileName } = {}) {
196
+ if (!template) return null;
197
+ const marked = templateMarked(template);
198
+ const usedBy = schedules.filter((s) => s && s.templateId === template.id).map((s) => {
199
+ const group = s.groupId ? groups.find((g) => g.id === s.groupId) || null : null;
200
+ return { id: str(s.id), name: str(s.name), marked: scheduleMarked(s), account: accountOf(s, group, browserDefault, { allowed, profileName }) };
201
+ });
202
+ return {
203
+ kind: 'template', id: str(template.id), name: str(template.name),
204
+ digest: reviewDigest({ kind: 'template', template: templateCore(template) }),
205
+ marks: { template: marked, templateFrom: marked ? (UNREVIEWED_SOURCES.includes(template.unreviewedFrom) ? template.unreviewedFrom : 'agent') : null },
206
+ needsApproval: marked,
207
+ instructions: { task: str(template.task), context: str(template.context) },
208
+ usedBy,
209
+ };
210
+ }
211
+
212
+ /** Takes the marks away (the approve action's write). Returns the record. */
213
+ export function clearScheduleMark(schedule, { ownProfileToOwner = false } = {}) {
214
+ delete schedule.taskBy;
215
+ delete schedule.unreviewedFrom;
216
+ if (ownProfileToOwner && schedule.moreloginEnvId) schedule.moreloginEnvBy = 'owner';
217
+ return schedule;
218
+ }
219
+ export function clearTemplateMark(template) {
220
+ delete template.editedBy;
221
+ delete template.unreviewedFrom;
222
+ return template;
223
+ }
224
+
225
+ /**
226
+ * Marks every record of a restored or imported file unreviewed (finding 2: an archive is data, nothing in it may say
227
+ * "the owner approved this"). Only adds marks: a record already marked keeps its mark. A schedule's own profile is
228
+ * kept, but who chose it is not believed: `moreloginEnvBy: 'owner'` from a file becomes 'agent' until approved.
229
+ * @param {unknown} data the parsed file: an array of records, or { templates } / { schedules, groups }
230
+ * @param {'templates'|'schedules'} kind
231
+ * @param {'import'|'restore'} source
232
+ * @returns {{ data: unknown, marked: number }}
233
+ */
234
+ export function markRecordsUnreviewed(data, kind, source = 'restore') {
235
+ let marked = 0;
236
+ const field = kind === 'templates' ? 'editedBy' : 'taskBy';
237
+ const mark = (record) => {
238
+ if (!record || typeof record !== 'object' || Array.isArray(record)) return;
239
+ if (record[field] !== 'agent') { record[field] = 'agent'; record.unreviewedFrom = source; marked++; }
240
+ if (kind === 'schedules' && record.moreloginEnvId && record.moreloginEnvBy === 'owner') record.moreloginEnvBy = 'agent';
241
+ };
242
+ const rows = Array.isArray(data) ? data : (data && typeof data === 'object' ? data[kind] : null);
243
+ if (Array.isArray(rows)) rows.forEach(mark);
244
+ if (kind === 'schedules' && data && typeof data === 'object' && !Array.isArray(data) && Array.isArray(data.groups)) {
245
+ // A group's profile in a file was not chosen in front of this server either.
246
+ for (const group of data.groups) if (group && typeof group === 'object' && group.moreloginEnvId && group.moreloginEnvBy === 'owner') group.moreloginEnvBy = 'agent';
247
+ }
248
+ return { data, marked };
249
+ }
@@ -0,0 +1,82 @@
1
+ // ═══════════════════════════════════════════
2
+ // SynaBun — Let the automation browser act as owner: the one global value
3
+ // ═══════════════════════════════════════════
4
+ //
5
+ // DATA_HOME/data/automation-browser-owner-access.json ({ enabled, updatedAt }), read live: whether SynaBun's own
6
+ // automation browser (the Playwright / MoreLogin browser the browser_* tools drive) may use the Neural Interface
7
+ // on this server as the owner. Off by default, and off whenever the file is missing or cannot be read.
8
+ //
9
+ // What it changes (lib/page-session.js, lib/owner-authority.js read it through `enabled()` on every request):
10
+ // while on, a request that carries the automation browser's marker (cookie or host-proof header), or whose peer
11
+ // process the lookup names an automation browser, is handed the page secret, verifies as the owner and passes the
12
+ // owner gate (its writes, its reads, its sockets: /ws/assistant, the panels, the memory map's data), so a
13
+ // screenshot or visual-QA run sees the real UI. Every other refusal stays: an agent's process tree, a relay,
14
+ // another SynaBun server, a remote login, a guest, a failed lookup. The automation browser still gets no service
15
+ // worker (nothing it does is answered out of the server's sight).
16
+ //
17
+ // Who may change it: only the owner's own page (PUT /api/assistant/automation-browser-owner-access: this boot's
18
+ // page secret, no agent header, and never from the automation browser itself, whatever the setting says). No
19
+ // agent tool, mailbox, worker or hook writes it; nothing reads it from a request. A change is broadcast to every
20
+ // window and said in the server log; a server that starts with it on says so too.
21
+
22
+ import { mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
23
+ import { dirname } from 'node:path';
24
+
25
+ export const AUTOMATION_BROWSER_OWNER_ACCESS_EVENT = 'browser:owner-access-changed';
26
+ export const AUTOMATION_BROWSER_OWNER_ACCESS_FILE = 'automation-browser-owner-access.json';
27
+ export const AUTOMATION_BROWSER_OWNER_ACCESS_DEFAULT = false;
28
+ /** The one-line notice the server log carries while the setting is on (at start, and on every change to on). */
29
+ export const AUTOMATION_BROWSER_OWNER_ACCESS_NOTICE = 'the automation browser acts as the owner on this server: agents driving the SynaBun browser can read and act on the Neural Interface as the owner (Settings → Browser → Automation browser access)';
30
+
31
+ /** Only `true` / `false` (and the strings "true" / "false") are a value; anything else is refused. */
32
+ export function normalizeOwnerAccessValue(value) {
33
+ if (value === true || value === 'true') return true;
34
+ if (value === false || value === 'false') return false;
35
+ return null;
36
+ }
37
+
38
+ export function createAutomationBrowserOwnerAccess({ path, broadcast = () => {}, log = () => {}, now = Date.now } = {}) {
39
+ if (!path) throw new Error('createAutomationBrowserOwnerAccess requires a path');
40
+ const listeners = new Set();
41
+ let cache = null; // { mtimeMs, value }
42
+
43
+ /** Whether the setting is on: the file's `enabled` when it is exactly true, else off. Never throws. */
44
+ function read() {
45
+ try {
46
+ const stat = statSync(path);
47
+ if (cache && cache.mtimeMs === stat.mtimeMs) return cache.value;
48
+ const data = JSON.parse(readFileSync(path, 'utf8'));
49
+ const value = data?.enabled === true;
50
+ cache = { mtimeMs: stat.mtimeMs, value };
51
+ return value;
52
+ } catch {
53
+ return AUTOMATION_BROWSER_OWNER_ACCESS_DEFAULT;
54
+ }
55
+ }
56
+ /** What the UI shows: the value in effect and the default. */
57
+ function view() {
58
+ return { enabled: read(), defaultEnabled: AUTOMATION_BROWSER_OWNER_ACCESS_DEFAULT };
59
+ }
60
+ function set(value) {
61
+ const next = normalizeOwnerAccessValue(value);
62
+ if (next === null) throw Object.assign(new Error('enabled must be true or false.'), { code: 'INVALID_VALUE', status: 400 });
63
+ const before = read();
64
+ mkdirSync(dirname(path), { recursive: true });
65
+ const tmp = `${path}.${process.pid}.tmp`;
66
+ writeFileSync(tmp, JSON.stringify({ enabled: next, updatedAt: new Date(now()).toISOString() }, null, 2) + '\n', 'utf8');
67
+ renameSync(tmp, path);
68
+ cache = null;
69
+ const changed = next !== before;
70
+ if (changed) log('automation-browser', next ? `owner access ON: ${AUTOMATION_BROWSER_OWNER_ACCESS_NOTICE}` : 'owner access OFF: the automation browser is refused the owner\'s page secret and authority again');
71
+ try { broadcast({ type: AUTOMATION_BROWSER_OWNER_ACCESS_EVENT, ...view() }); } catch { /* nobody listening */ }
72
+ if (changed) for (const listener of [...listeners]) { try { listener(next); } catch { /* the listener's own failure */ } }
73
+ return view();
74
+ }
75
+ /** The notice a server that starts with the setting on writes to its log (null when off). */
76
+ function startupNotice() {
77
+ return read() ? `[automation-browser] owner access is ON: ${AUTOMATION_BROWSER_OWNER_ACCESS_NOTICE}` : null;
78
+ }
79
+ function subscribe(listener) { listeners.add(listener); return () => listeners.delete(listener); }
80
+
81
+ return { enabled: read, view, set, subscribe, startupNotice, path };
82
+ }
@@ -228,10 +228,24 @@ function createConsistentSqliteSnapshot(sourcePath, snapshotPath) {
228
228
  // they belonged to, and that file is snapshotted consistently or not at all.
229
229
  const SQLITE_JOURNAL_RE = /\.(?:db|bak)-journal$/i;
230
230
 
231
+ /**
232
+ * True for what lies inside a local browser profile's folder (data/local-profiles/<id>/…, lib/local-profiles.js): a
233
+ * whole Chrome profile, which Chrome locks and rewrites while it runs, as large as a browser gets, and holding the
234
+ * profile's cookies and logins. No archive of any kind holds it (a copy taken under a running Chrome would not be a
235
+ * profile anyway). The profiles' definitions, data/local-profiles/profiles.json, are a plain file of that folder and
236
+ * stay in every backup and upgrade snapshot, so a rollback brings the list back; that file holds the proxy passwords
237
+ * as typed (docs/browser-v2.md).
238
+ */
239
+ export function isLocalProfileFolderPath(relativePath) {
240
+ const parts = String(relativePath).replace(/\\/g, '/').split('/');
241
+ return parts[0] === 'local-profiles' && parts.length > 2;
242
+ }
243
+
231
244
  function shouldSkipDataPath(relativePath) {
232
245
  const normalized = relativePath.replace(/\\/g, '/');
233
246
  const parts = normalized.split('/');
234
247
  const name = parts.at(-1);
248
+ if (isLocalProfileFolderPath(normalized)) return true;
235
249
  if (parts[0] === 'updater') return true;
236
250
  if (parts[0] === 'logs') return true;
237
251
  if (parts.length > 1 && EXCLUDED_DATA_DIRS.has(parts[0])) return true;
@@ -930,7 +944,8 @@ export function sameBackupFile(path, stamp) {
930
944
  // and transcript captures, the MCP tool counters; the caches the server rebuilds from the memories (the map's layout,
931
945
  // the links); and the automation browser's Chromium profiles, which Chromium locks and rewrites while it runs (the
932
946
  // "attach" one even when SynaBun does not), and which no SynaBun update touches. Upgrade snapshots leave them out;
933
- // the full backups keep everything.
947
+ // the full backups keep everything. (The folders of the local browser profiles, data/local-profiles/<id>/, are in no
948
+ // archive at all: shouldSkipDataPath. Their definitions, data/local-profiles/profiles.json, are in every one.)
934
949
  const UPGRADE_RUNTIME_FILES = Object.freeze({
935
950
  data: new Set(['supervisor.json', 'start-launcher.lock', 'update-handoff.json', 'last-session.json', 'memory-map.json', 'links-cache.json']),
936
951
  'mcp-data': new Set(['tool-usage.json']),
@@ -28,6 +28,17 @@ const closeTarget = (session, targetId) => within(Promise.resolve().then(() => s
28
28
  // How long the browser session stays attached for a tab asked for and not yet answered, to close it when it comes.
29
29
  const LATE_TARGET_MS = 60_000;
30
30
 
31
+ // The Browser that can open a browser-level CDP session: the one given, else the context's own (a persistent
32
+ // context is passed where a browser is expected and has none of its own methods for it).
33
+ function browserOf(browser, context) {
34
+ if (typeof browser?.newBrowserCDPSession === 'function') return browser;
35
+ try {
36
+ const owner = context?.browser?.();
37
+ if (typeof owner?.newBrowserCDPSession === 'function') return owner;
38
+ } catch { /* no owner */ }
39
+ return null;
40
+ }
41
+
31
42
  // The pages this module opened behind the selected tab.
32
43
  const backgroundPages = new WeakSet();
33
44
 
@@ -54,8 +65,9 @@ async function targetIdOf(context, page, until) {
54
65
  * the answer, so the caller is not kept waiting, and also when the browser only
55
66
  * creates it after the wait was given up — and the caller opens a normal one.
56
67
  */
57
- export async function openBackgroundPage(browser, context, { timeoutMs = 5000 } = {}) {
58
- if (typeof browser?.newBrowserCDPSession !== 'function') return null;
68
+ export async function openBackgroundPage(browser, context, { timeoutMs = 5000, browserContextId = null } = {}) {
69
+ browser = browserOf(browser, context);
70
+ if (!browser) return null;
59
71
  const until = Date.now() + timeoutMs;
60
72
  const left = () => Math.max(1, until - Date.now());
61
73
  const appeared = [];
@@ -70,7 +82,7 @@ export async function openBackgroundPage(browser, context, { timeoutMs = 5000 }
70
82
  cdp = await within(Promise.resolve().then(() => browser.newBrowserCDPSession()), left(), 'opening a browser CDP session', (late) => late?.detach?.());
71
83
  const session = cdp;
72
84
  let answered = false;
73
- const creating = Promise.resolve().then(() => session.send('Target.createTarget', { url: 'about:blank', background: true }));
85
+ const creating = Promise.resolve().then(() => session.send('Target.createTarget', { url: 'about:blank', background: true, ...(browserContextId && { browserContextId }) }));
74
86
  creating.then(() => { answered = true; }, () => { answered = true; });
75
87
  try {
76
88
  ({ targetId } = await within(creating, left(), 'creating the tab'));
@@ -132,3 +144,60 @@ export function selectedTabReading(page) {
132
144
  return { reading: 'unknown', basis: 'unreadable' };
133
145
  }
134
146
  }
147
+
148
+ /**
149
+ * Whether tabs open in the background: on unless browser-config.json says
150
+ * `openTabsInBackground: false` (only the boolean false turns it off; not set,
151
+ * or anything else, is on). Default on since 2026-11-08 so an automation on
152
+ * macOS never brings its window forward or switches the user's Space.
153
+ */
154
+ export function backgroundTabsEnabled(config) {
155
+ return config?.openTabsInBackground !== false;
156
+ }
157
+
158
+ /** The error of a tab that could not be opened in the background: never replaced by a foreground tab. */
159
+ export class BackgroundTabError extends Error {
160
+ constructor(attempts) {
161
+ super(`Could not open the browser tab in the background (${attempts} attempts). SynaBun does not open it in the foreground, which would bring the browser window forward; set openTabsInBackground to false in browser-config.json to allow that`);
162
+ this.name = 'BackgroundTabError';
163
+ this.code = 'BACKGROUND_TAB_FAILED';
164
+ }
165
+ }
166
+
167
+ /**
168
+ * openBackgroundPage, strictly: one retry, then BackgroundTabError. It never
169
+ * falls back to context.newPage(), which selects the tab and brings the
170
+ * window (and on macOS its Space) to the front. `timeoutMs` is per attempt.
171
+ */
172
+ export async function openBackgroundPageStrict(browser, context, { timeoutMs = 5000, attempts = 2, browserContextId = null, open = openBackgroundPage } = {}) {
173
+ for (let attempt = 1; attempt <= attempts; attempt += 1) {
174
+ const page = await Promise.resolve().then(() => open(browser, context, { timeoutMs, browserContextId })).catch(() => null);
175
+ if (page) return page;
176
+ if (attempt < attempts) console.warn(`[browser] background tab attempt ${attempt} failed; trying again in the background`);
177
+ }
178
+ throw new BackgroundTabError(attempts);
179
+ }
180
+
181
+ /**
182
+ * browser.newContext(options) plus the CDP id of the context it made, so a
183
+ * background tab can be created in it (Target.createTarget without an id
184
+ * opens in the default context). The id is the one that was not there before;
185
+ * null when that cannot be told (no CDP, or more than one new context).
186
+ */
187
+ export async function newContextWithId(browser, options) {
188
+ const ids = async () => {
189
+ const session = await browser.newBrowserCDPSession();
190
+ try { return (await session.send('Target.getBrowserContexts')).browserContextIds || []; } finally { detach(session); }
191
+ };
192
+ let before = null;
193
+ try { before = new Set(await ids()); } catch { /* unknown */ }
194
+ const context = await browser.newContext(options);
195
+ let browserContextId = null;
196
+ if (before) {
197
+ try {
198
+ const added = (await ids()).filter((id) => !before.has(id));
199
+ if (added.length === 1) [browserContextId] = added;
200
+ } catch { /* unknown */ }
201
+ }
202
+ return { context, browserContextId };
203
+ }