chainlesschain 0.143.0 → 0.145.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 (133) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +157 -27
  4. package/src/commands/agent.js +587 -103
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +152 -27
  7. package/src/commands/bm25.js +111 -27
  8. package/src/commands/browse.js +190 -29
  9. package/src/commands/ccron.js +128 -28
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +414 -34
  12. package/src/commands/compt.js +127 -29
  13. package/src/commands/consol.js +8 -2
  14. package/src/commands/cowork.js +580 -34
  15. package/src/commands/crosschain.js +182 -28
  16. package/src/commands/dao.js +182 -28
  17. package/src/commands/dlp.js +172 -27
  18. package/src/commands/economy.js +205 -50
  19. package/src/commands/evolution.js +203 -50
  20. package/src/commands/evomap.js +182 -27
  21. package/src/commands/fflag.js +74 -22
  22. package/src/commands/git.js +177 -37
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +204 -50
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +170 -34
  27. package/src/commands/itbudget.js +149 -33
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +195 -38
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +179 -27
  32. package/src/commands/mcpscaf.js +145 -33
  33. package/src/commands/meminj.js +145 -33
  34. package/src/commands/nostr.js +178 -27
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +144 -33
  37. package/src/commands/pdfp.js +109 -27
  38. package/src/commands/perf.js +169 -32
  39. package/src/commands/perm.js +144 -33
  40. package/src/commands/pipeline.js +207 -52
  41. package/src/commands/planmode.js +141 -32
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +111 -27
  44. package/src/commands/recommend.js +177 -34
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +145 -33
  48. package/src/commands/seshsearch.js +141 -33
  49. package/src/commands/seshtail.js +144 -33
  50. package/src/commands/seshu.js +152 -33
  51. package/src/commands/sganal.js +123 -29
  52. package/src/commands/siem.js +201 -34
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +146 -33
  55. package/src/commands/social.js +157 -32
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +145 -33
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +171 -33
  60. package/src/commands/topiccls.js +146 -33
  61. package/src/commands/uprof.js +141 -32
  62. package/src/commands/vcheck.js +122 -28
  63. package/src/commands/webfetch.js +141 -32
  64. package/src/commands/zkp.js +184 -28
  65. package/src/index.js +40 -0
  66. package/src/lib/a2a-protocol.js +319 -51
  67. package/src/lib/activitypub-bridge.js +288 -50
  68. package/src/lib/agent-economy.js +304 -51
  69. package/src/lib/app-builder.js +279 -46
  70. package/src/lib/audit-logger.js +321 -0
  71. package/src/lib/autonomous-agent.js +284 -48
  72. package/src/lib/bi-engine.js +283 -49
  73. package/src/lib/bm25-search.js +301 -49
  74. package/src/lib/browser-automation.js +296 -49
  75. package/src/lib/chat-core.js +336 -0
  76. package/src/lib/claude-code-bridge.js +341 -0
  77. package/src/lib/compliance-framework-reporter.js +305 -51
  78. package/src/lib/compliance-manager.js +330 -0
  79. package/src/lib/compression-telemetry.js +301 -49
  80. package/src/lib/content-recommender.js +317 -52
  81. package/src/lib/cowork-cron.js +298 -49
  82. package/src/lib/cowork-learning.js +333 -0
  83. package/src/lib/cowork-task-runner.js +308 -51
  84. package/src/lib/cowork-workflow.js +327 -0
  85. package/src/lib/cross-chain.js +311 -51
  86. package/src/lib/dao-governance.js +280 -46
  87. package/src/lib/dlp-engine.js +287 -49
  88. package/src/lib/evolution-system.js +278 -47
  89. package/src/lib/evomap-manager.js +280 -46
  90. package/src/lib/execution-backend.js +294 -48
  91. package/src/lib/feature-flags.js +294 -49
  92. package/src/lib/git-integration.js +285 -47
  93. package/src/lib/hardening-manager.js +341 -0
  94. package/src/lib/hierarchical-memory.js +284 -48
  95. package/src/lib/inference-network.js +308 -51
  96. package/src/lib/iteration-budget.js +302 -50
  97. package/src/lib/knowledge-graph.js +333 -0
  98. package/src/lib/matrix-bridge.js +281 -47
  99. package/src/lib/mcp-scaffold.js +318 -54
  100. package/src/lib/memory-injection.js +288 -49
  101. package/src/lib/nostr-bridge.js +286 -49
  102. package/src/lib/orchestrator.js +293 -48
  103. package/src/lib/pdf-parser.js +298 -49
  104. package/src/lib/perf-tuning.js +309 -50
  105. package/src/lib/permission-engine.js +287 -49
  106. package/src/lib/pipeline-orchestrator.js +289 -49
  107. package/src/lib/plan-mode.js +298 -51
  108. package/src/lib/privacy-computing.js +335 -0
  109. package/src/lib/reputation-optimizer.js +340 -0
  110. package/src/lib/sandbox-v2.js +327 -0
  111. package/src/lib/service-container.js +313 -52
  112. package/src/lib/session-consolidator.js +296 -49
  113. package/src/lib/session-hooks.js +312 -53
  114. package/src/lib/session-search.js +304 -51
  115. package/src/lib/session-tail.js +288 -49
  116. package/src/lib/session-usage.js +298 -52
  117. package/src/lib/siem-exporter.js +298 -51
  118. package/src/lib/skill-marketplace.js +345 -0
  119. package/src/lib/sla-manager.js +341 -0
  120. package/src/lib/slot-filler.js +303 -51
  121. package/src/lib/social-graph-analytics.js +295 -49
  122. package/src/lib/social-graph.js +272 -49
  123. package/src/lib/stress-tester.js +342 -0
  124. package/src/lib/sub-agent-registry.js +302 -53
  125. package/src/lib/task-model-selector.js +302 -50
  126. package/src/lib/terraform-manager.js +333 -0
  127. package/src/lib/todo-manager.js +281 -47
  128. package/src/lib/token-incentive.js +341 -0
  129. package/src/lib/topic-classifier.js +297 -49
  130. package/src/lib/user-profile.js +294 -50
  131. package/src/lib/version-checker.js +304 -50
  132. package/src/lib/web-fetch.js +292 -51
  133. 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,286 @@ 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
+ };
321
568
  }