chainlesschain 0.143.0 → 0.152.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 (227) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +364 -27
  4. package/src/commands/agent-network.js +217 -0
  5. package/src/commands/agent.js +587 -103
  6. package/src/commands/audit.js +206 -0
  7. package/src/commands/automation.js +201 -0
  8. package/src/commands/bi.js +355 -27
  9. package/src/commands/bm25.js +111 -27
  10. package/src/commands/browse.js +403 -29
  11. package/src/commands/ccron.js +128 -28
  12. package/src/commands/chat.js +207 -0
  13. package/src/commands/codegen.js +207 -0
  14. package/src/commands/collab.js +211 -0
  15. package/src/commands/compliance.js +824 -34
  16. package/src/commands/compt.js +127 -29
  17. package/src/commands/consol.js +8 -2
  18. package/src/commands/cowork.js +783 -34
  19. package/src/commands/crosschain.js +182 -28
  20. package/src/commands/dao.js +182 -28
  21. package/src/commands/dbevo.js +227 -0
  22. package/src/commands/dev.js +207 -0
  23. package/src/commands/did-v2.js +217 -0
  24. package/src/commands/did.js +221 -0
  25. package/src/commands/dlp.js +385 -27
  26. package/src/commands/economy.js +205 -50
  27. package/src/commands/evolution.js +203 -50
  28. package/src/commands/evomap.js +387 -27
  29. package/src/commands/export.js +213 -0
  30. package/src/commands/federation.js +209 -0
  31. package/src/commands/fflag.js +74 -22
  32. package/src/commands/fusion.js +205 -0
  33. package/src/commands/git.js +177 -37
  34. package/src/commands/governance.js +209 -0
  35. package/src/commands/hardening.js +209 -0
  36. package/src/commands/hmemory.js +204 -50
  37. package/src/commands/hook.js +209 -0
  38. package/src/commands/import.js +209 -0
  39. package/src/commands/incentive.js +209 -0
  40. package/src/commands/inference.js +170 -34
  41. package/src/commands/infra.js +203 -0
  42. package/src/commands/instinct.js +209 -0
  43. package/src/commands/ipfs.js +207 -0
  44. package/src/commands/itbudget.js +149 -33
  45. package/src/commands/kg.js +206 -0
  46. package/src/commands/llm.js +217 -0
  47. package/src/commands/lowcode.js +195 -38
  48. package/src/commands/marketplace.js +206 -0
  49. package/src/commands/matrix.js +386 -27
  50. package/src/commands/mcp.js +217 -0
  51. package/src/commands/mcpscaf.js +145 -33
  52. package/src/commands/meminj.js +145 -33
  53. package/src/commands/memory.js +209 -0
  54. package/src/commands/multimodal.js +203 -0
  55. package/src/commands/nlprog.js +225 -0
  56. package/src/commands/nostr.js +387 -27
  57. package/src/commands/note.js +205 -0
  58. package/src/commands/ops.js +219 -0
  59. package/src/commands/orchestrate.js +217 -0
  60. package/src/commands/orchgov.js +144 -33
  61. package/src/commands/org.js +209 -0
  62. package/src/commands/p2p.js +209 -0
  63. package/src/commands/pdfp.js +109 -27
  64. package/src/commands/perception.js +209 -0
  65. package/src/commands/perf.js +169 -32
  66. package/src/commands/perm.js +144 -33
  67. package/src/commands/permmem.js +203 -0
  68. package/src/commands/pipeline.js +207 -52
  69. package/src/commands/planmode.js +141 -32
  70. package/src/commands/plugin-ecosystem.js +209 -0
  71. package/src/commands/pqc.js +213 -0
  72. package/src/commands/privacy.js +203 -0
  73. package/src/commands/promcomp.js +111 -27
  74. package/src/commands/quantization.js +207 -0
  75. package/src/commands/rcache.js +205 -0
  76. package/src/commands/recommend.js +410 -34
  77. package/src/commands/reputation.js +208 -0
  78. package/src/commands/runtime.js +205 -0
  79. package/src/commands/sandbox.js +206 -0
  80. package/src/commands/scim.js +209 -0
  81. package/src/commands/seshhook.js +145 -33
  82. package/src/commands/seshsearch.js +141 -33
  83. package/src/commands/seshtail.js +144 -33
  84. package/src/commands/seshu.js +152 -33
  85. package/src/commands/session.js +209 -0
  86. package/src/commands/sganal.js +123 -29
  87. package/src/commands/siem.js +201 -34
  88. package/src/commands/skill.js +207 -0
  89. package/src/commands/sla.js +212 -0
  90. package/src/commands/slotfill.js +146 -33
  91. package/src/commands/social.js +358 -32
  92. package/src/commands/sso.js +209 -0
  93. package/src/commands/stress.js +206 -0
  94. package/src/commands/svccont.js +145 -33
  95. package/src/commands/sync.js +209 -0
  96. package/src/commands/tech.js +209 -0
  97. package/src/commands/tenant.js +217 -0
  98. package/src/commands/terraform.js +206 -0
  99. package/src/commands/tms.js +171 -33
  100. package/src/commands/tokens.js +209 -0
  101. package/src/commands/topiccls.js +146 -33
  102. package/src/commands/trust.js +217 -0
  103. package/src/commands/uprof.js +141 -32
  104. package/src/commands/vcheck.js +122 -28
  105. package/src/commands/wallet.js +209 -0
  106. package/src/commands/webfetch.js +141 -32
  107. package/src/commands/workflow.js +203 -0
  108. package/src/commands/zkp.js +184 -28
  109. package/src/index.js +180 -0
  110. package/src/lib/a2a-protocol.js +319 -51
  111. package/src/lib/activitypub-bridge.js +622 -50
  112. package/src/lib/agent-economy.js +304 -51
  113. package/src/lib/agent-network.js +341 -0
  114. package/src/lib/aiops.js +346 -0
  115. package/src/lib/app-builder.js +279 -46
  116. package/src/lib/audit-logger.js +321 -0
  117. package/src/lib/automation-engine.js +335 -0
  118. package/src/lib/autonomous-agent.js +284 -48
  119. package/src/lib/autonomous-developer.js +332 -0
  120. package/src/lib/bi-engine.js +616 -49
  121. package/src/lib/bm25-search.js +301 -49
  122. package/src/lib/browser-automation.js +630 -49
  123. package/src/lib/chat-core.js +336 -0
  124. package/src/lib/claude-code-bridge.js +341 -0
  125. package/src/lib/code-agent.js +339 -0
  126. package/src/lib/collaboration-governance.js +334 -0
  127. package/src/lib/community-governance.js +346 -0
  128. package/src/lib/compliance-framework-reporter.js +305 -51
  129. package/src/lib/compliance-manager.js +330 -0
  130. package/src/lib/compression-telemetry.js +301 -49
  131. package/src/lib/content-recommendation.js +351 -0
  132. package/src/lib/content-recommender.js +317 -52
  133. package/src/lib/cowork-cron.js +298 -49
  134. package/src/lib/cowork-learning.js +333 -0
  135. package/src/lib/cowork-share.js +338 -0
  136. package/src/lib/cowork-task-runner.js +308 -51
  137. package/src/lib/cowork-workflow.js +327 -0
  138. package/src/lib/cross-chain.js +311 -51
  139. package/src/lib/dao-governance.js +280 -46
  140. package/src/lib/dbevo.js +351 -0
  141. package/src/lib/decentral-infra.js +330 -0
  142. package/src/lib/did-manager.js +341 -0
  143. package/src/lib/did-v2-manager.js +341 -0
  144. package/src/lib/dlp-engine.js +626 -49
  145. package/src/lib/evolution-system.js +278 -47
  146. package/src/lib/evomap-governance.js +334 -0
  147. package/src/lib/evomap-manager.js +280 -46
  148. package/src/lib/execution-backend.js +294 -48
  149. package/src/lib/feature-flags.js +294 -49
  150. package/src/lib/federation-hardening.js +340 -0
  151. package/src/lib/git-integration.js +285 -47
  152. package/src/lib/hardening-manager.js +341 -0
  153. package/src/lib/hierarchical-memory.js +284 -48
  154. package/src/lib/hook-manager.js +341 -0
  155. package/src/lib/inference-network.js +308 -51
  156. package/src/lib/instinct-manager.js +346 -0
  157. package/src/lib/ipfs-storage.js +334 -0
  158. package/src/lib/iteration-budget.js +302 -50
  159. package/src/lib/knowledge-exporter.js +341 -0
  160. package/src/lib/knowledge-graph.js +333 -0
  161. package/src/lib/knowledge-importer.js +341 -0
  162. package/src/lib/llm-providers.js +346 -0
  163. package/src/lib/matrix-bridge.js +620 -47
  164. package/src/lib/mcp-registry.js +346 -0
  165. package/src/lib/mcp-scaffold.js +318 -54
  166. package/src/lib/memory-injection.js +288 -49
  167. package/src/lib/memory-manager.js +336 -0
  168. package/src/lib/multimodal.js +330 -0
  169. package/src/lib/nl-programming.js +341 -0
  170. package/src/lib/nostr-bridge.js +622 -49
  171. package/src/lib/note-versioning.js +339 -0
  172. package/src/lib/orchestrator.js +293 -48
  173. package/src/lib/org-manager.js +336 -0
  174. package/src/lib/p2p-manager.js +341 -0
  175. package/src/lib/pdf-parser.js +298 -49
  176. package/src/lib/perception.js +346 -0
  177. package/src/lib/perf-tuning.js +309 -50
  178. package/src/lib/permanent-memory.js +327 -0
  179. package/src/lib/permission-engine.js +287 -49
  180. package/src/lib/pipeline-orchestrator.js +289 -49
  181. package/src/lib/plan-mode.js +298 -51
  182. package/src/lib/plugin-ecosystem.js +346 -0
  183. package/src/lib/pqc-manager.js +346 -0
  184. package/src/lib/privacy-computing.js +335 -0
  185. package/src/lib/protocol-fusion.js +338 -0
  186. package/src/lib/quantization.js +337 -0
  187. package/src/lib/reputation-optimizer.js +340 -0
  188. package/src/lib/response-cache.js +333 -0
  189. package/src/lib/sandbox-v2.js +327 -0
  190. package/src/lib/scim-manager.js +346 -0
  191. package/src/lib/service-container.js +313 -52
  192. package/src/lib/session-consolidator.js +296 -49
  193. package/src/lib/session-hooks.js +312 -53
  194. package/src/lib/session-manager.js +334 -0
  195. package/src/lib/session-search.js +304 -51
  196. package/src/lib/session-tail.js +288 -49
  197. package/src/lib/session-usage.js +298 -52
  198. package/src/lib/siem-exporter.js +298 -51
  199. package/src/lib/skill-loader.js +334 -0
  200. package/src/lib/skill-marketplace.js +345 -0
  201. package/src/lib/sla-manager.js +341 -0
  202. package/src/lib/slot-filler.js +303 -51
  203. package/src/lib/social-graph-analytics.js +295 -49
  204. package/src/lib/social-graph.js +272 -49
  205. package/src/lib/social-manager.js +330 -0
  206. package/src/lib/sso-manager.js +340 -0
  207. package/src/lib/stress-tester.js +342 -0
  208. package/src/lib/sub-agent-registry.js +302 -53
  209. package/src/lib/sync-manager.js +336 -0
  210. package/src/lib/task-model-selector.js +302 -50
  211. package/src/lib/tech-learning-engine.js +341 -0
  212. package/src/lib/tenant-saas.js +341 -0
  213. package/src/lib/terraform-manager.js +333 -0
  214. package/src/lib/threat-intel.js +330 -0
  215. package/src/lib/todo-manager.js +281 -47
  216. package/src/lib/token-incentive.js +341 -0
  217. package/src/lib/token-tracker.js +336 -0
  218. package/src/lib/topic-classifier.js +297 -49
  219. package/src/lib/trust-security.js +343 -0
  220. package/src/lib/ueba.js +340 -0
  221. package/src/lib/universal-runtime.js +330 -0
  222. package/src/lib/user-profile.js +294 -50
  223. package/src/lib/version-checker.js +304 -50
  224. package/src/lib/wallet-manager.js +336 -0
  225. package/src/lib/web-fetch.js +292 -51
  226. package/src/lib/workflow-engine.js +330 -0
  227. package/src/lib/zkp-engine.js +286 -49
@@ -215,25 +215,62 @@ export async function takeScreenshot(url, outputPath, options = {}) {
215
215
  }
216
216
  }
217
217
 
218
-
219
218
  // ===== V2 Surface: Browser Automation governance overlay (CLI v0.134.0) =====
220
219
  export const BROWSE_TARGET_MATURITY_V2 = Object.freeze({
221
- PENDING: "pending", ACTIVE: "active", DEGRADED: "degraded", RETIRED: "retired",
220
+ PENDING: "pending",
221
+ ACTIVE: "active",
222
+ DEGRADED: "degraded",
223
+ RETIRED: "retired",
222
224
  });
223
225
  export const BROWSE_ACTION_LIFECYCLE_V2 = Object.freeze({
224
- QUEUED: "queued", RUNNING: "running", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
226
+ QUEUED: "queued",
227
+ RUNNING: "running",
228
+ COMPLETED: "completed",
229
+ FAILED: "failed",
230
+ CANCELLED: "cancelled",
225
231
  });
226
232
 
227
233
  const _brTargTrans = new Map([
228
- [BROWSE_TARGET_MATURITY_V2.PENDING, new Set([BROWSE_TARGET_MATURITY_V2.ACTIVE, BROWSE_TARGET_MATURITY_V2.RETIRED])],
229
- [BROWSE_TARGET_MATURITY_V2.ACTIVE, new Set([BROWSE_TARGET_MATURITY_V2.DEGRADED, BROWSE_TARGET_MATURITY_V2.RETIRED])],
230
- [BROWSE_TARGET_MATURITY_V2.DEGRADED, new Set([BROWSE_TARGET_MATURITY_V2.ACTIVE, BROWSE_TARGET_MATURITY_V2.RETIRED])],
234
+ [
235
+ BROWSE_TARGET_MATURITY_V2.PENDING,
236
+ new Set([
237
+ BROWSE_TARGET_MATURITY_V2.ACTIVE,
238
+ BROWSE_TARGET_MATURITY_V2.RETIRED,
239
+ ]),
240
+ ],
241
+ [
242
+ BROWSE_TARGET_MATURITY_V2.ACTIVE,
243
+ new Set([
244
+ BROWSE_TARGET_MATURITY_V2.DEGRADED,
245
+ BROWSE_TARGET_MATURITY_V2.RETIRED,
246
+ ]),
247
+ ],
248
+ [
249
+ BROWSE_TARGET_MATURITY_V2.DEGRADED,
250
+ new Set([
251
+ BROWSE_TARGET_MATURITY_V2.ACTIVE,
252
+ BROWSE_TARGET_MATURITY_V2.RETIRED,
253
+ ]),
254
+ ],
231
255
  [BROWSE_TARGET_MATURITY_V2.RETIRED, new Set()],
232
256
  ]);
233
257
  const _brTargTerminal = new Set([BROWSE_TARGET_MATURITY_V2.RETIRED]);
234
258
  const _brActTrans = new Map([
235
- [BROWSE_ACTION_LIFECYCLE_V2.QUEUED, new Set([BROWSE_ACTION_LIFECYCLE_V2.RUNNING, BROWSE_ACTION_LIFECYCLE_V2.CANCELLED])],
236
- [BROWSE_ACTION_LIFECYCLE_V2.RUNNING, new Set([BROWSE_ACTION_LIFECYCLE_V2.COMPLETED, BROWSE_ACTION_LIFECYCLE_V2.FAILED, BROWSE_ACTION_LIFECYCLE_V2.CANCELLED])],
259
+ [
260
+ BROWSE_ACTION_LIFECYCLE_V2.QUEUED,
261
+ new Set([
262
+ BROWSE_ACTION_LIFECYCLE_V2.RUNNING,
263
+ BROWSE_ACTION_LIFECYCLE_V2.CANCELLED,
264
+ ]),
265
+ ],
266
+ [
267
+ BROWSE_ACTION_LIFECYCLE_V2.RUNNING,
268
+ new Set([
269
+ BROWSE_ACTION_LIFECYCLE_V2.COMPLETED,
270
+ BROWSE_ACTION_LIFECYCLE_V2.FAILED,
271
+ BROWSE_ACTION_LIFECYCLE_V2.CANCELLED,
272
+ ]),
273
+ ],
237
274
  [BROWSE_ACTION_LIFECYCLE_V2.COMPLETED, new Set()],
238
275
  [BROWSE_ACTION_LIFECYCLE_V2.FAILED, new Set()],
239
276
  [BROWSE_ACTION_LIFECYCLE_V2.CANCELLED, new Set()],
@@ -246,76 +283,620 @@ let _brMaxPendingPerTarget = 20;
246
283
  let _brTargetIdleMs = 12 * 60 * 60 * 1000;
247
284
  let _brActionStuckMs = 3 * 60 * 1000;
248
285
 
249
- function _brPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
286
+ function _brPos(n, lbl) {
287
+ const v = Math.floor(Number(n));
288
+ if (!Number.isFinite(v) || v <= 0)
289
+ throw new Error(`${lbl} must be positive integer`);
290
+ return v;
291
+ }
250
292
 
251
- export function setMaxActiveBrowseTargetsPerOwnerV2(n) { _brMaxActivePerOwner = _brPos(n, "maxActiveBrowseTargetsPerOwner"); }
252
- export function getMaxActiveBrowseTargetsPerOwnerV2() { return _brMaxActivePerOwner; }
253
- export function setMaxPendingBrowseActionsPerTargetV2(n) { _brMaxPendingPerTarget = _brPos(n, "maxPendingBrowseActionsPerTarget"); }
254
- export function getMaxPendingBrowseActionsPerTargetV2() { return _brMaxPendingPerTarget; }
255
- export function setBrowseTargetIdleMsV2(n) { _brTargetIdleMs = _brPos(n, "browseTargetIdleMs"); }
256
- export function getBrowseTargetIdleMsV2() { return _brTargetIdleMs; }
257
- export function setBrowseActionStuckMsV2(n) { _brActionStuckMs = _brPos(n, "browseActionStuckMs"); }
258
- export function getBrowseActionStuckMsV2() { return _brActionStuckMs; }
293
+ export function setMaxActiveBrowseTargetsPerOwnerV2(n) {
294
+ _brMaxActivePerOwner = _brPos(n, "maxActiveBrowseTargetsPerOwner");
295
+ }
296
+ export function getMaxActiveBrowseTargetsPerOwnerV2() {
297
+ return _brMaxActivePerOwner;
298
+ }
299
+ export function setMaxPendingBrowseActionsPerTargetV2(n) {
300
+ _brMaxPendingPerTarget = _brPos(n, "maxPendingBrowseActionsPerTarget");
301
+ }
302
+ export function getMaxPendingBrowseActionsPerTargetV2() {
303
+ return _brMaxPendingPerTarget;
304
+ }
305
+ export function setBrowseTargetIdleMsV2(n) {
306
+ _brTargetIdleMs = _brPos(n, "browseTargetIdleMs");
307
+ }
308
+ export function getBrowseTargetIdleMsV2() {
309
+ return _brTargetIdleMs;
310
+ }
311
+ export function setBrowseActionStuckMsV2(n) {
312
+ _brActionStuckMs = _brPos(n, "browseActionStuckMs");
313
+ }
314
+ export function getBrowseActionStuckMsV2() {
315
+ return _brActionStuckMs;
316
+ }
259
317
 
260
318
  export function _resetStateBrowserAutomationV2() {
261
- _brTargets.clear(); _brActions.clear();
262
- _brMaxActivePerOwner = 8; _brMaxPendingPerTarget = 20;
263
- _brTargetIdleMs = 12 * 60 * 60 * 1000; _brActionStuckMs = 3 * 60 * 1000;
319
+ _brTargets.clear();
320
+ _brActions.clear();
321
+ _brMaxActivePerOwner = 8;
322
+ _brMaxPendingPerTarget = 20;
323
+ _brTargetIdleMs = 12 * 60 * 60 * 1000;
324
+ _brActionStuckMs = 3 * 60 * 1000;
264
325
  }
265
326
 
266
327
  export function registerBrowseTargetV2({ id, owner, url, metadata } = {}) {
267
328
  if (!id || typeof id !== "string") throw new Error("id is required");
268
329
  if (!owner || typeof owner !== "string") throw new Error("owner is required");
269
- if (_brTargets.has(id)) throw new Error(`browse target ${id} already registered`);
330
+ if (_brTargets.has(id))
331
+ throw new Error(`browse target ${id} already registered`);
270
332
  const now = Date.now();
271
- const t = { id, owner, url: url || "", status: BROWSE_TARGET_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, retiredAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
333
+ const t = {
334
+ id,
335
+ owner,
336
+ url: url || "",
337
+ status: BROWSE_TARGET_MATURITY_V2.PENDING,
338
+ createdAt: now,
339
+ updatedAt: now,
340
+ activatedAt: null,
341
+ retiredAt: null,
342
+ lastTouchedAt: now,
343
+ metadata: { ...(metadata || {}) },
344
+ };
272
345
  _brTargets.set(id, t);
273
346
  return { ...t, metadata: { ...t.metadata } };
274
347
  }
275
- function _brCheckT(from, to) { const a = _brTargTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid browse target transition ${from} → ${to}`); }
276
- function _brCountActive(owner) { let n = 0; for (const t of _brTargets.values()) if (t.owner === owner && t.status === BROWSE_TARGET_MATURITY_V2.ACTIVE) n++; return n; }
348
+ function _brCheckT(from, to) {
349
+ const a = _brTargTrans.get(from);
350
+ if (!a || !a.has(to))
351
+ throw new Error(`invalid browse target transition ${from} → ${to}`);
352
+ }
353
+ function _brCountActive(owner) {
354
+ let n = 0;
355
+ for (const t of _brTargets.values())
356
+ if (t.owner === owner && t.status === BROWSE_TARGET_MATURITY_V2.ACTIVE) n++;
357
+ return n;
358
+ }
277
359
 
278
360
  export function activateBrowseTargetV2(id) {
279
- const t = _brTargets.get(id); if (!t) throw new Error(`browse target ${id} not found`);
361
+ const t = _brTargets.get(id);
362
+ if (!t) throw new Error(`browse target ${id} not found`);
280
363
  _brCheckT(t.status, BROWSE_TARGET_MATURITY_V2.ACTIVE);
281
364
  const recovery = t.status === BROWSE_TARGET_MATURITY_V2.DEGRADED;
282
- if (!recovery) { const a = _brCountActive(t.owner); if (a >= _brMaxActivePerOwner) throw new Error(`max active browse targets per owner (${_brMaxActivePerOwner}) reached for ${t.owner}`); }
283
- const now = Date.now(); t.status = BROWSE_TARGET_MATURITY_V2.ACTIVE; t.updatedAt = now; t.lastTouchedAt = now; if (!t.activatedAt) t.activatedAt = now;
365
+ if (!recovery) {
366
+ const a = _brCountActive(t.owner);
367
+ if (a >= _brMaxActivePerOwner)
368
+ throw new Error(
369
+ `max active browse targets per owner (${_brMaxActivePerOwner}) reached for ${t.owner}`,
370
+ );
371
+ }
372
+ const now = Date.now();
373
+ t.status = BROWSE_TARGET_MATURITY_V2.ACTIVE;
374
+ t.updatedAt = now;
375
+ t.lastTouchedAt = now;
376
+ if (!t.activatedAt) t.activatedAt = now;
377
+ return { ...t, metadata: { ...t.metadata } };
378
+ }
379
+ export function degradeBrowseTargetV2(id) {
380
+ const t = _brTargets.get(id);
381
+ if (!t) throw new Error(`browse target ${id} not found`);
382
+ _brCheckT(t.status, BROWSE_TARGET_MATURITY_V2.DEGRADED);
383
+ t.status = BROWSE_TARGET_MATURITY_V2.DEGRADED;
384
+ t.updatedAt = Date.now();
284
385
  return { ...t, metadata: { ...t.metadata } };
285
386
  }
286
- export function degradeBrowseTargetV2(id) { const t = _brTargets.get(id); if (!t) throw new Error(`browse target ${id} not found`); _brCheckT(t.status, BROWSE_TARGET_MATURITY_V2.DEGRADED); t.status = BROWSE_TARGET_MATURITY_V2.DEGRADED; t.updatedAt = Date.now(); return { ...t, metadata: { ...t.metadata } }; }
287
- export function retireBrowseTargetV2(id) { const t = _brTargets.get(id); if (!t) throw new Error(`browse target ${id} not found`); _brCheckT(t.status, BROWSE_TARGET_MATURITY_V2.RETIRED); const now = Date.now(); t.status = BROWSE_TARGET_MATURITY_V2.RETIRED; t.updatedAt = now; if (!t.retiredAt) t.retiredAt = now; return { ...t, metadata: { ...t.metadata } }; }
288
- export function touchBrowseTargetV2(id) { const t = _brTargets.get(id); if (!t) throw new Error(`browse target ${id} not found`); if (_brTargTerminal.has(t.status)) throw new Error(`cannot touch terminal browse target ${id}`); const now = Date.now(); t.lastTouchedAt = now; t.updatedAt = now; return { ...t, metadata: { ...t.metadata } }; }
289
- export function getBrowseTargetV2(id) { const t = _brTargets.get(id); if (!t) return null; return { ...t, metadata: { ...t.metadata } }; }
290
- export function listBrowseTargetsV2() { return [..._brTargets.values()].map((t) => ({ ...t, metadata: { ...t.metadata } })); }
387
+ export function retireBrowseTargetV2(id) {
388
+ const t = _brTargets.get(id);
389
+ if (!t) throw new Error(`browse target ${id} not found`);
390
+ _brCheckT(t.status, BROWSE_TARGET_MATURITY_V2.RETIRED);
391
+ const now = Date.now();
392
+ t.status = BROWSE_TARGET_MATURITY_V2.RETIRED;
393
+ t.updatedAt = now;
394
+ if (!t.retiredAt) t.retiredAt = now;
395
+ return { ...t, metadata: { ...t.metadata } };
396
+ }
397
+ export function touchBrowseTargetV2(id) {
398
+ const t = _brTargets.get(id);
399
+ if (!t) throw new Error(`browse target ${id} not found`);
400
+ if (_brTargTerminal.has(t.status))
401
+ throw new Error(`cannot touch terminal browse target ${id}`);
402
+ const now = Date.now();
403
+ t.lastTouchedAt = now;
404
+ t.updatedAt = now;
405
+ return { ...t, metadata: { ...t.metadata } };
406
+ }
407
+ export function getBrowseTargetV2(id) {
408
+ const t = _brTargets.get(id);
409
+ if (!t) return null;
410
+ return { ...t, metadata: { ...t.metadata } };
411
+ }
412
+ export function listBrowseTargetsV2() {
413
+ return [..._brTargets.values()].map((t) => ({
414
+ ...t,
415
+ metadata: { ...t.metadata },
416
+ }));
417
+ }
291
418
 
292
- function _brCountPending(tid) { let n = 0; for (const a of _brActions.values()) if (a.targetId === tid && (a.status === BROWSE_ACTION_LIFECYCLE_V2.QUEUED || a.status === BROWSE_ACTION_LIFECYCLE_V2.RUNNING)) n++; return n; }
419
+ function _brCountPending(tid) {
420
+ let n = 0;
421
+ for (const a of _brActions.values())
422
+ if (
423
+ a.targetId === tid &&
424
+ (a.status === BROWSE_ACTION_LIFECYCLE_V2.QUEUED ||
425
+ a.status === BROWSE_ACTION_LIFECYCLE_V2.RUNNING)
426
+ )
427
+ n++;
428
+ return n;
429
+ }
293
430
 
294
431
  export function createBrowseActionV2({ id, targetId, kind, metadata } = {}) {
295
432
  if (!id || typeof id !== "string") throw new Error("id is required");
296
- if (!targetId || typeof targetId !== "string") throw new Error("targetId is required");
433
+ if (!targetId || typeof targetId !== "string")
434
+ throw new Error("targetId is required");
297
435
  if (_brActions.has(id)) throw new Error(`browse action ${id} already exists`);
298
- if (!_brTargets.has(targetId)) throw new Error(`browse target ${targetId} not found`);
436
+ if (!_brTargets.has(targetId))
437
+ throw new Error(`browse target ${targetId} not found`);
299
438
  const pending = _brCountPending(targetId);
300
- if (pending >= _brMaxPendingPerTarget) throw new Error(`max pending browse actions per target (${_brMaxPendingPerTarget}) reached for ${targetId}`);
439
+ if (pending >= _brMaxPendingPerTarget)
440
+ throw new Error(
441
+ `max pending browse actions per target (${_brMaxPendingPerTarget}) reached for ${targetId}`,
442
+ );
301
443
  const now = Date.now();
302
- const a = { id, targetId, kind: kind || "fetch", status: BROWSE_ACTION_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
444
+ const a = {
445
+ id,
446
+ targetId,
447
+ kind: kind || "fetch",
448
+ status: BROWSE_ACTION_LIFECYCLE_V2.QUEUED,
449
+ createdAt: now,
450
+ updatedAt: now,
451
+ startedAt: null,
452
+ settledAt: null,
453
+ metadata: { ...(metadata || {}) },
454
+ };
303
455
  _brActions.set(id, a);
304
456
  return { ...a, metadata: { ...a.metadata } };
305
457
  }
306
- function _brCheckA(from, to) { const al = _brActTrans.get(from); if (!al || !al.has(to)) throw new Error(`invalid browse action transition ${from} → ${to}`); }
307
- export function startBrowseActionV2(id) { const a = _brActions.get(id); if (!a) throw new Error(`browse action ${id} not found`); _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.RUNNING); const now = Date.now(); a.status = BROWSE_ACTION_LIFECYCLE_V2.RUNNING; a.updatedAt = now; if (!a.startedAt) a.startedAt = now; return { ...a, metadata: { ...a.metadata } }; }
308
- export function completeBrowseActionV2(id) { const a = _brActions.get(id); if (!a) throw new Error(`browse action ${id} not found`); _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.COMPLETED); const now = Date.now(); a.status = BROWSE_ACTION_LIFECYCLE_V2.COMPLETED; a.updatedAt = now; if (!a.settledAt) a.settledAt = now; return { ...a, metadata: { ...a.metadata } }; }
309
- export function failBrowseActionV2(id, reason) { const a = _brActions.get(id); if (!a) throw new Error(`browse action ${id} not found`); _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.FAILED); const now = Date.now(); a.status = BROWSE_ACTION_LIFECYCLE_V2.FAILED; a.updatedAt = now; if (!a.settledAt) a.settledAt = now; if (reason) a.metadata.failReason = String(reason); return { ...a, metadata: { ...a.metadata } }; }
310
- export function cancelBrowseActionV2(id, reason) { const a = _brActions.get(id); if (!a) throw new Error(`browse action ${id} not found`); _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.CANCELLED); const now = Date.now(); a.status = BROWSE_ACTION_LIFECYCLE_V2.CANCELLED; a.updatedAt = now; if (!a.settledAt) a.settledAt = now; if (reason) a.metadata.cancelReason = String(reason); return { ...a, metadata: { ...a.metadata } }; }
311
- export function getBrowseActionV2(id) { const a = _brActions.get(id); if (!a) return null; return { ...a, metadata: { ...a.metadata } }; }
312
- export function listBrowseActionsV2() { return [..._brActions.values()].map((a) => ({ ...a, metadata: { ...a.metadata } })); }
458
+ function _brCheckA(from, to) {
459
+ const al = _brActTrans.get(from);
460
+ if (!al || !al.has(to))
461
+ throw new Error(`invalid browse action transition ${from} ${to}`);
462
+ }
463
+ export function startBrowseActionV2(id) {
464
+ const a = _brActions.get(id);
465
+ if (!a) throw new Error(`browse action ${id} not found`);
466
+ _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.RUNNING);
467
+ const now = Date.now();
468
+ a.status = BROWSE_ACTION_LIFECYCLE_V2.RUNNING;
469
+ a.updatedAt = now;
470
+ if (!a.startedAt) a.startedAt = now;
471
+ return { ...a, metadata: { ...a.metadata } };
472
+ }
473
+ export function completeBrowseActionV2(id) {
474
+ const a = _brActions.get(id);
475
+ if (!a) throw new Error(`browse action ${id} not found`);
476
+ _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.COMPLETED);
477
+ const now = Date.now();
478
+ a.status = BROWSE_ACTION_LIFECYCLE_V2.COMPLETED;
479
+ a.updatedAt = now;
480
+ if (!a.settledAt) a.settledAt = now;
481
+ return { ...a, metadata: { ...a.metadata } };
482
+ }
483
+ export function failBrowseActionV2(id, reason) {
484
+ const a = _brActions.get(id);
485
+ if (!a) throw new Error(`browse action ${id} not found`);
486
+ _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.FAILED);
487
+ const now = Date.now();
488
+ a.status = BROWSE_ACTION_LIFECYCLE_V2.FAILED;
489
+ a.updatedAt = now;
490
+ if (!a.settledAt) a.settledAt = now;
491
+ if (reason) a.metadata.failReason = String(reason);
492
+ return { ...a, metadata: { ...a.metadata } };
493
+ }
494
+ export function cancelBrowseActionV2(id, reason) {
495
+ const a = _brActions.get(id);
496
+ if (!a) throw new Error(`browse action ${id} not found`);
497
+ _brCheckA(a.status, BROWSE_ACTION_LIFECYCLE_V2.CANCELLED);
498
+ const now = Date.now();
499
+ a.status = BROWSE_ACTION_LIFECYCLE_V2.CANCELLED;
500
+ a.updatedAt = now;
501
+ if (!a.settledAt) a.settledAt = now;
502
+ if (reason) a.metadata.cancelReason = String(reason);
503
+ return { ...a, metadata: { ...a.metadata } };
504
+ }
505
+ export function getBrowseActionV2(id) {
506
+ const a = _brActions.get(id);
507
+ if (!a) return null;
508
+ return { ...a, metadata: { ...a.metadata } };
509
+ }
510
+ export function listBrowseActionsV2() {
511
+ return [..._brActions.values()].map((a) => ({
512
+ ...a,
513
+ metadata: { ...a.metadata },
514
+ }));
515
+ }
313
516
 
314
- export function autoDegradeIdleBrowseTargetsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const tg of _brTargets.values()) if (tg.status === BROWSE_TARGET_MATURITY_V2.ACTIVE && (t - tg.lastTouchedAt) >= _brTargetIdleMs) { tg.status = BROWSE_TARGET_MATURITY_V2.DEGRADED; tg.updatedAt = t; flipped.push(tg.id); } return { flipped, count: flipped.length }; }
315
- export function autoFailStuckBrowseActionsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const a of _brActions.values()) if (a.status === BROWSE_ACTION_LIFECYCLE_V2.RUNNING && a.startedAt != null && (t - a.startedAt) >= _brActionStuckMs) { a.status = BROWSE_ACTION_LIFECYCLE_V2.FAILED; a.updatedAt = t; if (!a.settledAt) a.settledAt = t; a.metadata.failReason = "auto-fail-stuck"; flipped.push(a.id); } return { flipped, count: flipped.length }; }
517
+ export function autoDegradeIdleBrowseTargetsV2({ now } = {}) {
518
+ const t = now ?? Date.now();
519
+ const flipped = [];
520
+ for (const tg of _brTargets.values())
521
+ if (
522
+ tg.status === BROWSE_TARGET_MATURITY_V2.ACTIVE &&
523
+ t - tg.lastTouchedAt >= _brTargetIdleMs
524
+ ) {
525
+ tg.status = BROWSE_TARGET_MATURITY_V2.DEGRADED;
526
+ tg.updatedAt = t;
527
+ flipped.push(tg.id);
528
+ }
529
+ return { flipped, count: flipped.length };
530
+ }
531
+ export function autoFailStuckBrowseActionsV2({ now } = {}) {
532
+ const t = now ?? Date.now();
533
+ const flipped = [];
534
+ for (const a of _brActions.values())
535
+ if (
536
+ a.status === BROWSE_ACTION_LIFECYCLE_V2.RUNNING &&
537
+ a.startedAt != null &&
538
+ t - a.startedAt >= _brActionStuckMs
539
+ ) {
540
+ a.status = BROWSE_ACTION_LIFECYCLE_V2.FAILED;
541
+ a.updatedAt = t;
542
+ if (!a.settledAt) a.settledAt = t;
543
+ a.metadata.failReason = "auto-fail-stuck";
544
+ flipped.push(a.id);
545
+ }
546
+ return { flipped, count: flipped.length };
547
+ }
316
548
 
317
549
  export function getBrowserAutomationStatsV2() {
318
- const targetsByStatus = {}; for (const s of Object.values(BROWSE_TARGET_MATURITY_V2)) targetsByStatus[s] = 0; for (const t of _brTargets.values()) targetsByStatus[t.status]++;
319
- const actionsByStatus = {}; for (const s of Object.values(BROWSE_ACTION_LIFECYCLE_V2)) actionsByStatus[s] = 0; for (const a of _brActions.values()) actionsByStatus[a.status]++;
320
- return { totalTargetsV2: _brTargets.size, totalActionsV2: _brActions.size, maxActiveBrowseTargetsPerOwner: _brMaxActivePerOwner, maxPendingBrowseActionsPerTarget: _brMaxPendingPerTarget, browseTargetIdleMs: _brTargetIdleMs, browseActionStuckMs: _brActionStuckMs, targetsByStatus, actionsByStatus };
550
+ const targetsByStatus = {};
551
+ for (const s of Object.values(BROWSE_TARGET_MATURITY_V2))
552
+ targetsByStatus[s] = 0;
553
+ for (const t of _brTargets.values()) targetsByStatus[t.status]++;
554
+ const actionsByStatus = {};
555
+ for (const s of Object.values(BROWSE_ACTION_LIFECYCLE_V2))
556
+ actionsByStatus[s] = 0;
557
+ for (const a of _brActions.values()) actionsByStatus[a.status]++;
558
+ return {
559
+ totalTargetsV2: _brTargets.size,
560
+ totalActionsV2: _brActions.size,
561
+ maxActiveBrowseTargetsPerOwner: _brMaxActivePerOwner,
562
+ maxPendingBrowseActionsPerTarget: _brMaxPendingPerTarget,
563
+ browseTargetIdleMs: _brTargetIdleMs,
564
+ browseActionStuckMs: _brActionStuckMs,
565
+ targetsByStatus,
566
+ actionsByStatus,
567
+ };
568
+ }
569
+
570
+ // =====================================================================
571
+ // browser-automation V2 governance overlay (iter20)
572
+ // =====================================================================
573
+ export const BAGOV_PROFILE_MATURITY_V2 = Object.freeze({
574
+ PENDING: "pending",
575
+ ACTIVE: "active",
576
+ STALE: "stale",
577
+ ARCHIVED: "archived",
578
+ });
579
+ export const BAGOV_NAVIGATION_LIFECYCLE_V2 = Object.freeze({
580
+ QUEUED: "queued",
581
+ NAVIGATING: "navigating",
582
+ NAVIGATED: "navigated",
583
+ FAILED: "failed",
584
+ CANCELLED: "cancelled",
585
+ });
586
+ const _bagovPTrans = new Map([
587
+ [
588
+ BAGOV_PROFILE_MATURITY_V2.PENDING,
589
+ new Set([
590
+ BAGOV_PROFILE_MATURITY_V2.ACTIVE,
591
+ BAGOV_PROFILE_MATURITY_V2.ARCHIVED,
592
+ ]),
593
+ ],
594
+ [
595
+ BAGOV_PROFILE_MATURITY_V2.ACTIVE,
596
+ new Set([
597
+ BAGOV_PROFILE_MATURITY_V2.STALE,
598
+ BAGOV_PROFILE_MATURITY_V2.ARCHIVED,
599
+ ]),
600
+ ],
601
+ [
602
+ BAGOV_PROFILE_MATURITY_V2.STALE,
603
+ new Set([
604
+ BAGOV_PROFILE_MATURITY_V2.ACTIVE,
605
+ BAGOV_PROFILE_MATURITY_V2.ARCHIVED,
606
+ ]),
607
+ ],
608
+ [BAGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
609
+ ]);
610
+ const _bagovPTerminal = new Set([BAGOV_PROFILE_MATURITY_V2.ARCHIVED]);
611
+ const _bagovJTrans = new Map([
612
+ [
613
+ BAGOV_NAVIGATION_LIFECYCLE_V2.QUEUED,
614
+ new Set([
615
+ BAGOV_NAVIGATION_LIFECYCLE_V2.NAVIGATING,
616
+ BAGOV_NAVIGATION_LIFECYCLE_V2.CANCELLED,
617
+ ]),
618
+ ],
619
+ [
620
+ BAGOV_NAVIGATION_LIFECYCLE_V2.NAVIGATING,
621
+ new Set([
622
+ BAGOV_NAVIGATION_LIFECYCLE_V2.NAVIGATED,
623
+ BAGOV_NAVIGATION_LIFECYCLE_V2.FAILED,
624
+ BAGOV_NAVIGATION_LIFECYCLE_V2.CANCELLED,
625
+ ]),
626
+ ],
627
+ [BAGOV_NAVIGATION_LIFECYCLE_V2.NAVIGATED, new Set()],
628
+ [BAGOV_NAVIGATION_LIFECYCLE_V2.FAILED, new Set()],
629
+ [BAGOV_NAVIGATION_LIFECYCLE_V2.CANCELLED, new Set()],
630
+ ]);
631
+ const _bagovPsV2 = new Map();
632
+ const _bagovJsV2 = new Map();
633
+ let _bagovMaxActive = 6,
634
+ _bagovMaxPending = 15,
635
+ _bagovIdleMs = 30 * 24 * 60 * 60 * 1000,
636
+ _bagovStuckMs = 60 * 1000;
637
+ function _bagovPos(n, label) {
638
+ const v = Math.floor(Number(n));
639
+ if (!Number.isFinite(v) || v <= 0)
640
+ throw new Error(`${label} must be positive integer`);
641
+ return v;
642
+ }
643
+ function _bagovCheckP(from, to) {
644
+ const a = _bagovPTrans.get(from);
645
+ if (!a || !a.has(to))
646
+ throw new Error(`invalid bagov profile transition ${from} → ${to}`);
647
+ }
648
+ function _bagovCheckJ(from, to) {
649
+ const a = _bagovJTrans.get(from);
650
+ if (!a || !a.has(to))
651
+ throw new Error(`invalid bagov navigation transition ${from} → ${to}`);
652
+ }
653
+ function _bagovCountActive(owner) {
654
+ let c = 0;
655
+ for (const p of _bagovPsV2.values())
656
+ if (p.owner === owner && p.status === BAGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
657
+ return c;
658
+ }
659
+ function _bagovCountPending(profileId) {
660
+ let c = 0;
661
+ for (const j of _bagovJsV2.values())
662
+ if (
663
+ j.profileId === profileId &&
664
+ (j.status === BAGOV_NAVIGATION_LIFECYCLE_V2.QUEUED ||
665
+ j.status === BAGOV_NAVIGATION_LIFECYCLE_V2.NAVIGATING)
666
+ )
667
+ c++;
668
+ return c;
669
+ }
670
+ export function setMaxActiveBagovProfilesPerOwnerV2(n) {
671
+ _bagovMaxActive = _bagovPos(n, "maxActiveBagovProfilesPerOwner");
672
+ }
673
+ export function getMaxActiveBagovProfilesPerOwnerV2() {
674
+ return _bagovMaxActive;
675
+ }
676
+ export function setMaxPendingBagovNavigationsPerProfileV2(n) {
677
+ _bagovMaxPending = _bagovPos(n, "maxPendingBagovNavigationsPerProfile");
678
+ }
679
+ export function getMaxPendingBagovNavigationsPerProfileV2() {
680
+ return _bagovMaxPending;
681
+ }
682
+ export function setBagovProfileIdleMsV2(n) {
683
+ _bagovIdleMs = _bagovPos(n, "bagovProfileIdleMs");
684
+ }
685
+ export function getBagovProfileIdleMsV2() {
686
+ return _bagovIdleMs;
687
+ }
688
+ export function setBagovNavigationStuckMsV2(n) {
689
+ _bagovStuckMs = _bagovPos(n, "bagovNavigationStuckMs");
690
+ }
691
+ export function getBagovNavigationStuckMsV2() {
692
+ return _bagovStuckMs;
693
+ }
694
+ export function _resetStateBrowserAutomationGovV2() {
695
+ _bagovPsV2.clear();
696
+ _bagovJsV2.clear();
697
+ _bagovMaxActive = 6;
698
+ _bagovMaxPending = 15;
699
+ _bagovIdleMs = 30 * 24 * 60 * 60 * 1000;
700
+ _bagovStuckMs = 60 * 1000;
701
+ }
702
+ export function registerBagovProfileV2({ id, owner, engine, metadata } = {}) {
703
+ if (!id || !owner) throw new Error("id and owner required");
704
+ if (_bagovPsV2.has(id)) throw new Error(`bagov profile ${id} already exists`);
705
+ const now = Date.now();
706
+ const p = {
707
+ id,
708
+ owner,
709
+ engine: engine || "chromium",
710
+ status: BAGOV_PROFILE_MATURITY_V2.PENDING,
711
+ createdAt: now,
712
+ updatedAt: now,
713
+ lastTouchedAt: now,
714
+ activatedAt: null,
715
+ archivedAt: null,
716
+ metadata: { ...(metadata || {}) },
717
+ };
718
+ _bagovPsV2.set(id, p);
719
+ return { ...p, metadata: { ...p.metadata } };
720
+ }
721
+ export function activateBagovProfileV2(id) {
722
+ const p = _bagovPsV2.get(id);
723
+ if (!p) throw new Error(`bagov profile ${id} not found`);
724
+ const isInitial = p.status === BAGOV_PROFILE_MATURITY_V2.PENDING;
725
+ _bagovCheckP(p.status, BAGOV_PROFILE_MATURITY_V2.ACTIVE);
726
+ if (isInitial && _bagovCountActive(p.owner) >= _bagovMaxActive)
727
+ throw new Error(`max active bagov profiles for owner ${p.owner} reached`);
728
+ const now = Date.now();
729
+ p.status = BAGOV_PROFILE_MATURITY_V2.ACTIVE;
730
+ p.updatedAt = now;
731
+ p.lastTouchedAt = now;
732
+ if (!p.activatedAt) p.activatedAt = now;
733
+ return { ...p, metadata: { ...p.metadata } };
734
+ }
735
+ export function staleBagovProfileV2(id) {
736
+ const p = _bagovPsV2.get(id);
737
+ if (!p) throw new Error(`bagov profile ${id} not found`);
738
+ _bagovCheckP(p.status, BAGOV_PROFILE_MATURITY_V2.STALE);
739
+ p.status = BAGOV_PROFILE_MATURITY_V2.STALE;
740
+ p.updatedAt = Date.now();
741
+ return { ...p, metadata: { ...p.metadata } };
742
+ }
743
+ export function archiveBagovProfileV2(id) {
744
+ const p = _bagovPsV2.get(id);
745
+ if (!p) throw new Error(`bagov profile ${id} not found`);
746
+ _bagovCheckP(p.status, BAGOV_PROFILE_MATURITY_V2.ARCHIVED);
747
+ const now = Date.now();
748
+ p.status = BAGOV_PROFILE_MATURITY_V2.ARCHIVED;
749
+ p.updatedAt = now;
750
+ if (!p.archivedAt) p.archivedAt = now;
751
+ return { ...p, metadata: { ...p.metadata } };
752
+ }
753
+ export function touchBagovProfileV2(id) {
754
+ const p = _bagovPsV2.get(id);
755
+ if (!p) throw new Error(`bagov profile ${id} not found`);
756
+ if (_bagovPTerminal.has(p.status))
757
+ throw new Error(`cannot touch terminal bagov profile ${id}`);
758
+ const now = Date.now();
759
+ p.lastTouchedAt = now;
760
+ p.updatedAt = now;
761
+ return { ...p, metadata: { ...p.metadata } };
762
+ }
763
+ export function getBagovProfileV2(id) {
764
+ const p = _bagovPsV2.get(id);
765
+ if (!p) return null;
766
+ return { ...p, metadata: { ...p.metadata } };
767
+ }
768
+ export function listBagovProfilesV2() {
769
+ return [..._bagovPsV2.values()].map((p) => ({
770
+ ...p,
771
+ metadata: { ...p.metadata },
772
+ }));
773
+ }
774
+ export function createBagovNavigationV2({ id, profileId, url, metadata } = {}) {
775
+ if (!id || !profileId) throw new Error("id and profileId required");
776
+ if (_bagovJsV2.has(id))
777
+ throw new Error(`bagov navigation ${id} already exists`);
778
+ if (!_bagovPsV2.has(profileId))
779
+ throw new Error(`bagov profile ${profileId} not found`);
780
+ if (_bagovCountPending(profileId) >= _bagovMaxPending)
781
+ throw new Error(
782
+ `max pending bagov navigations for profile ${profileId} reached`,
783
+ );
784
+ const now = Date.now();
785
+ const j = {
786
+ id,
787
+ profileId,
788
+ url: url || "",
789
+ status: BAGOV_NAVIGATION_LIFECYCLE_V2.QUEUED,
790
+ createdAt: now,
791
+ updatedAt: now,
792
+ startedAt: null,
793
+ settledAt: null,
794
+ metadata: { ...(metadata || {}) },
795
+ };
796
+ _bagovJsV2.set(id, j);
797
+ return { ...j, metadata: { ...j.metadata } };
798
+ }
799
+ export function navigatingBagovNavigationV2(id) {
800
+ const j = _bagovJsV2.get(id);
801
+ if (!j) throw new Error(`bagov navigation ${id} not found`);
802
+ _bagovCheckJ(j.status, BAGOV_NAVIGATION_LIFECYCLE_V2.NAVIGATING);
803
+ const now = Date.now();
804
+ j.status = BAGOV_NAVIGATION_LIFECYCLE_V2.NAVIGATING;
805
+ j.updatedAt = now;
806
+ if (!j.startedAt) j.startedAt = now;
807
+ return { ...j, metadata: { ...j.metadata } };
808
+ }
809
+ export function completeNavigationBagovV2(id) {
810
+ const j = _bagovJsV2.get(id);
811
+ if (!j) throw new Error(`bagov navigation ${id} not found`);
812
+ _bagovCheckJ(j.status, BAGOV_NAVIGATION_LIFECYCLE_V2.NAVIGATED);
813
+ const now = Date.now();
814
+ j.status = BAGOV_NAVIGATION_LIFECYCLE_V2.NAVIGATED;
815
+ j.updatedAt = now;
816
+ if (!j.settledAt) j.settledAt = now;
817
+ return { ...j, metadata: { ...j.metadata } };
818
+ }
819
+ export function failBagovNavigationV2(id, reason) {
820
+ const j = _bagovJsV2.get(id);
821
+ if (!j) throw new Error(`bagov navigation ${id} not found`);
822
+ _bagovCheckJ(j.status, BAGOV_NAVIGATION_LIFECYCLE_V2.FAILED);
823
+ const now = Date.now();
824
+ j.status = BAGOV_NAVIGATION_LIFECYCLE_V2.FAILED;
825
+ j.updatedAt = now;
826
+ if (!j.settledAt) j.settledAt = now;
827
+ if (reason) j.metadata.failReason = String(reason);
828
+ return { ...j, metadata: { ...j.metadata } };
829
+ }
830
+ export function cancelBagovNavigationV2(id, reason) {
831
+ const j = _bagovJsV2.get(id);
832
+ if (!j) throw new Error(`bagov navigation ${id} not found`);
833
+ _bagovCheckJ(j.status, BAGOV_NAVIGATION_LIFECYCLE_V2.CANCELLED);
834
+ const now = Date.now();
835
+ j.status = BAGOV_NAVIGATION_LIFECYCLE_V2.CANCELLED;
836
+ j.updatedAt = now;
837
+ if (!j.settledAt) j.settledAt = now;
838
+ if (reason) j.metadata.cancelReason = String(reason);
839
+ return { ...j, metadata: { ...j.metadata } };
840
+ }
841
+ export function getBagovNavigationV2(id) {
842
+ const j = _bagovJsV2.get(id);
843
+ if (!j) return null;
844
+ return { ...j, metadata: { ...j.metadata } };
845
+ }
846
+ export function listBagovNavigationsV2() {
847
+ return [..._bagovJsV2.values()].map((j) => ({
848
+ ...j,
849
+ metadata: { ...j.metadata },
850
+ }));
851
+ }
852
+ export function autoStaleIdleBagovProfilesV2({ now } = {}) {
853
+ const t = now ?? Date.now();
854
+ const flipped = [];
855
+ for (const p of _bagovPsV2.values())
856
+ if (
857
+ p.status === BAGOV_PROFILE_MATURITY_V2.ACTIVE &&
858
+ t - p.lastTouchedAt >= _bagovIdleMs
859
+ ) {
860
+ p.status = BAGOV_PROFILE_MATURITY_V2.STALE;
861
+ p.updatedAt = t;
862
+ flipped.push(p.id);
863
+ }
864
+ return { flipped, count: flipped.length };
865
+ }
866
+ export function autoFailStuckBagovNavigationsV2({ now } = {}) {
867
+ const t = now ?? Date.now();
868
+ const flipped = [];
869
+ for (const j of _bagovJsV2.values())
870
+ if (
871
+ j.status === BAGOV_NAVIGATION_LIFECYCLE_V2.NAVIGATING &&
872
+ j.startedAt != null &&
873
+ t - j.startedAt >= _bagovStuckMs
874
+ ) {
875
+ j.status = BAGOV_NAVIGATION_LIFECYCLE_V2.FAILED;
876
+ j.updatedAt = t;
877
+ if (!j.settledAt) j.settledAt = t;
878
+ j.metadata.failReason = "auto-fail-stuck";
879
+ flipped.push(j.id);
880
+ }
881
+ return { flipped, count: flipped.length };
882
+ }
883
+ export function getBrowserAutomationGovStatsV2() {
884
+ const profilesByStatus = {};
885
+ for (const v of Object.values(BAGOV_PROFILE_MATURITY_V2))
886
+ profilesByStatus[v] = 0;
887
+ for (const p of _bagovPsV2.values()) profilesByStatus[p.status]++;
888
+ const navigationsByStatus = {};
889
+ for (const v of Object.values(BAGOV_NAVIGATION_LIFECYCLE_V2))
890
+ navigationsByStatus[v] = 0;
891
+ for (const j of _bagovJsV2.values()) navigationsByStatus[j.status]++;
892
+ return {
893
+ totalBagovProfilesV2: _bagovPsV2.size,
894
+ totalBagovNavigationsV2: _bagovJsV2.size,
895
+ maxActiveBagovProfilesPerOwner: _bagovMaxActive,
896
+ maxPendingBagovNavigationsPerProfile: _bagovMaxPending,
897
+ bagovProfileIdleMs: _bagovIdleMs,
898
+ bagovNavigationStuckMs: _bagovStuckMs,
899
+ profilesByStatus,
900
+ navigationsByStatus,
901
+ };
321
902
  }