chainlesschain 0.81.0 → 0.143.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 (209) hide show
  1. package/bin/chainlesschain.js +0 -0
  2. package/package.json +1 -1
  3. package/src/commands/a2a.js +62 -0
  4. package/src/commands/activitypub.js +61 -0
  5. package/src/commands/agent-network.js +254 -1
  6. package/src/commands/agent.js +117 -0
  7. package/src/commands/audit.js +302 -0
  8. package/src/commands/automation.js +271 -1
  9. package/src/commands/bi.js +61 -0
  10. package/src/commands/bm25.js +78 -0
  11. package/src/commands/browse.js +64 -0
  12. package/src/commands/ccron.js +78 -0
  13. package/src/commands/codegen.js +224 -0
  14. package/src/commands/collab.js +341 -0
  15. package/src/commands/compliance.js +1075 -0
  16. package/src/commands/compt.js +78 -0
  17. package/src/commands/consol.js +231 -0
  18. package/src/commands/cowork.js +263 -0
  19. package/src/commands/crosschain.js +62 -0
  20. package/src/commands/dao.js +62 -0
  21. package/src/commands/dbevo.js +284 -0
  22. package/src/commands/dev.js +252 -0
  23. package/src/commands/did.js +358 -0
  24. package/src/commands/dlp.js +61 -0
  25. package/src/commands/economy.js +56 -0
  26. package/src/commands/encrypt.js +341 -0
  27. package/src/commands/evolution.js +56 -0
  28. package/src/commands/evomap.js +61 -0
  29. package/src/commands/export.js +256 -1
  30. package/src/commands/fflag.js +178 -0
  31. package/src/commands/fusion.js +258 -0
  32. package/src/commands/git.js +45 -0
  33. package/src/commands/governance.js +325 -0
  34. package/src/commands/hardening.js +411 -0
  35. package/src/commands/hmemory.js +56 -0
  36. package/src/commands/hook.js +148 -0
  37. package/src/commands/import.js +252 -0
  38. package/src/commands/incentive.js +322 -0
  39. package/src/commands/inference.js +42 -0
  40. package/src/commands/infra.js +244 -0
  41. package/src/commands/instinct.js +260 -0
  42. package/src/commands/ipfs.js +318 -0
  43. package/src/commands/itbudget.js +45 -0
  44. package/src/commands/kg.js +387 -0
  45. package/src/commands/llm.js +263 -0
  46. package/src/commands/lowcode.js +44 -0
  47. package/src/commands/matrix.js +62 -0
  48. package/src/commands/mcp.js +221 -0
  49. package/src/commands/mcpscaf.js +41 -0
  50. package/src/commands/meminj.js +41 -0
  51. package/src/commands/memory.js +248 -0
  52. package/src/commands/multimodal.js +296 -0
  53. package/src/commands/nlprog.js +356 -0
  54. package/src/commands/nostr.js +62 -0
  55. package/src/commands/note.js +244 -0
  56. package/src/commands/ops.js +354 -0
  57. package/src/commands/orchestrate.js +166 -0
  58. package/src/commands/orchgov.js +45 -0
  59. package/src/commands/org.js +277 -0
  60. package/src/commands/p2p.js +390 -0
  61. package/src/commands/pdfp.js +78 -0
  62. package/src/commands/perception.js +290 -0
  63. package/src/commands/perf.js +39 -0
  64. package/src/commands/perm.js +45 -0
  65. package/src/commands/permmem.js +251 -0
  66. package/src/commands/pipeline.js +57 -1
  67. package/src/commands/planmode.js +45 -0
  68. package/src/commands/plugin-ecosystem.js +273 -0
  69. package/src/commands/pqc.js +393 -0
  70. package/src/commands/promcomp.js +82 -0
  71. package/src/commands/quantization.js +351 -0
  72. package/src/commands/rcache.js +271 -0
  73. package/src/commands/recommend.js +382 -0
  74. package/src/commands/runtime.js +307 -0
  75. package/src/commands/scim.js +262 -0
  76. package/src/commands/seshhook.js +41 -0
  77. package/src/commands/seshsearch.js +41 -0
  78. package/src/commands/seshtail.js +41 -0
  79. package/src/commands/seshu.js +41 -0
  80. package/src/commands/session.js +258 -0
  81. package/src/commands/sganal.js +78 -0
  82. package/src/commands/siem.js +40 -0
  83. package/src/commands/skill.js +267 -1
  84. package/src/commands/slotfill.js +41 -0
  85. package/src/commands/social.js +290 -0
  86. package/src/commands/sso.js +186 -1
  87. package/src/commands/svccont.js +45 -0
  88. package/src/commands/sync.js +256 -0
  89. package/src/commands/tech.js +338 -0
  90. package/src/commands/tenant.js +351 -0
  91. package/src/commands/tms.js +45 -0
  92. package/src/commands/tokens.js +269 -0
  93. package/src/commands/topiccls.js +45 -0
  94. package/src/commands/trust.js +249 -0
  95. package/src/commands/uprof.js +45 -0
  96. package/src/commands/vcheck.js +78 -0
  97. package/src/commands/wallet.js +277 -0
  98. package/src/commands/webfetch.js +41 -0
  99. package/src/commands/workflow.js +171 -0
  100. package/src/commands/zkp.js +62 -0
  101. package/src/harness/prompt-compressor.js +331 -0
  102. package/src/index.js +65 -1
  103. package/src/lib/a2a-protocol.js +105 -0
  104. package/src/lib/activitypub-bridge.js +105 -0
  105. package/src/lib/agent-coordinator.js +325 -0
  106. package/src/lib/agent-economy.js +105 -0
  107. package/src/lib/agent-network.js +387 -0
  108. package/src/lib/agent-router.js +395 -0
  109. package/src/lib/aiops.js +478 -0
  110. package/src/lib/app-builder.js +105 -0
  111. package/src/lib/audit-logger.js +379 -0
  112. package/src/lib/automation-engine.js +330 -0
  113. package/src/lib/autonomous-agent.js +105 -0
  114. package/src/lib/autonomous-developer.js +350 -0
  115. package/src/lib/bi-engine.js +105 -0
  116. package/src/lib/bm25-search.js +81 -0
  117. package/src/lib/browser-automation.js +105 -0
  118. package/src/lib/code-agent.js +323 -0
  119. package/src/lib/collaboration-governance.js +364 -0
  120. package/src/lib/community-governance.js +436 -0
  121. package/src/lib/compliance-framework-reporter.js +105 -0
  122. package/src/lib/compliance-manager.js +434 -0
  123. package/src/lib/compression-telemetry.js +81 -0
  124. package/src/lib/content-recommendation.js +469 -0
  125. package/src/lib/content-recommender.js +105 -0
  126. package/src/lib/cowork-cron.js +81 -0
  127. package/src/lib/cowork-task-runner.js +105 -0
  128. package/src/lib/cross-chain.js +105 -0
  129. package/src/lib/crypto-manager.js +350 -0
  130. package/src/lib/dao-governance.js +105 -0
  131. package/src/lib/dbevo.js +338 -0
  132. package/src/lib/decentral-infra.js +340 -0
  133. package/src/lib/did-manager.js +367 -0
  134. package/src/lib/dlp-engine.js +105 -0
  135. package/src/lib/evolution-system.js +105 -0
  136. package/src/lib/evomap-manager.js +105 -0
  137. package/src/lib/execution-backend.js +105 -0
  138. package/src/lib/feature-flags.js +85 -0
  139. package/src/lib/git-integration.js +105 -0
  140. package/src/lib/hardening-manager.js +348 -0
  141. package/src/lib/hierarchical-memory.js +105 -0
  142. package/src/lib/hook-manager.js +380 -0
  143. package/src/lib/inference-network.js +105 -0
  144. package/src/lib/instinct-manager.js +332 -0
  145. package/src/lib/ipfs-storage.js +334 -0
  146. package/src/lib/iteration-budget.js +105 -0
  147. package/src/lib/knowledge-exporter.js +381 -0
  148. package/src/lib/knowledge-graph.js +432 -0
  149. package/src/lib/knowledge-importer.js +379 -0
  150. package/src/lib/llm-providers.js +391 -0
  151. package/src/lib/matrix-bridge.js +105 -0
  152. package/src/lib/mcp-registry.js +333 -0
  153. package/src/lib/mcp-scaffold.js +81 -0
  154. package/src/lib/memory-injection.js +81 -0
  155. package/src/lib/memory-manager.js +330 -0
  156. package/src/lib/multimodal.js +346 -0
  157. package/src/lib/nl-programming.js +343 -0
  158. package/src/lib/nostr-bridge.js +105 -0
  159. package/src/lib/note-versioning.js +327 -0
  160. package/src/lib/orchestrator.js +105 -0
  161. package/src/lib/org-manager.js +323 -0
  162. package/src/lib/p2p-manager.js +387 -0
  163. package/src/lib/pdf-parser.js +81 -0
  164. package/src/lib/perception.js +346 -0
  165. package/src/lib/perf-tuning.js +109 -1
  166. package/src/lib/permanent-memory.js +320 -0
  167. package/src/lib/permission-engine.js +81 -0
  168. package/src/lib/pipeline-orchestrator.js +105 -0
  169. package/src/lib/plan-mode.js +81 -0
  170. package/src/lib/plugin-ecosystem.js +377 -0
  171. package/src/lib/pqc-manager.js +368 -0
  172. package/src/lib/prompt-compressor.js +1 -10
  173. package/src/lib/protocol-fusion.js +417 -0
  174. package/src/lib/quantization.js +325 -0
  175. package/src/lib/response-cache.js +327 -0
  176. package/src/lib/scim-manager.js +329 -0
  177. package/src/lib/service-container.js +81 -0
  178. package/src/lib/session-consolidator.js +105 -0
  179. package/src/lib/session-hooks.js +81 -0
  180. package/src/lib/session-manager.js +329 -0
  181. package/src/lib/session-search.js +81 -0
  182. package/src/lib/session-tail.js +81 -0
  183. package/src/lib/session-usage.js +83 -0
  184. package/src/lib/siem-exporter.js +105 -0
  185. package/src/lib/skill-loader.js +377 -0
  186. package/src/lib/slot-filler.js +81 -0
  187. package/src/lib/social-graph-analytics.js +81 -0
  188. package/src/lib/social-graph.js +81 -0
  189. package/src/lib/social-manager.js +326 -0
  190. package/src/lib/sso-manager.js +332 -0
  191. package/src/lib/sub-agent-registry.js +110 -0
  192. package/src/lib/sync-manager.js +326 -0
  193. package/src/lib/task-model-selector.js +81 -0
  194. package/src/lib/tech-learning-engine.js +369 -0
  195. package/src/lib/tenant-saas.js +460 -0
  196. package/src/lib/threat-intel.js +335 -0
  197. package/src/lib/todo-manager.js +105 -0
  198. package/src/lib/token-incentive.js +293 -0
  199. package/src/lib/token-tracker.js +329 -0
  200. package/src/lib/topic-classifier.js +105 -0
  201. package/src/lib/trust-security.js +390 -0
  202. package/src/lib/ueba.js +389 -0
  203. package/src/lib/universal-runtime.js +325 -0
  204. package/src/lib/user-profile.js +81 -0
  205. package/src/lib/version-checker.js +81 -0
  206. package/src/lib/wallet-manager.js +326 -0
  207. package/src/lib/web-fetch.js +81 -0
  208. package/src/lib/workflow-engine.js +322 -0
  209. package/src/lib/zkp-engine.js +105 -0
@@ -346,3 +346,329 @@ export function getWalletSummary(db) {
346
346
  transactionCount: txns?.c || 0,
347
347
  };
348
348
  }
349
+
350
+ /* ═══════════════════════════════════════════════════════════════
351
+ * V2 Surface — In-memory wallet-maturity + transaction-lifecycle
352
+ * layer. Independent of the SQLite tables; tracks wallet
353
+ * provisioning + tx submission/settlement with caps and auto-flip.
354
+ * ═══════════════════════════════════════════════════════════════ */
355
+
356
+ export const WALLET_MATURITY_V2 = Object.freeze({
357
+ PROVISIONAL: "provisional",
358
+ ACTIVE: "active",
359
+ FROZEN: "frozen",
360
+ RETIRED: "retired",
361
+ });
362
+
363
+ export const TX_LIFECYCLE_V2 = Object.freeze({
364
+ PENDING: "pending",
365
+ SUBMITTED: "submitted",
366
+ CONFIRMED: "confirmed",
367
+ FAILED: "failed",
368
+ REJECTED: "rejected",
369
+ });
370
+
371
+ const WALLET_TRANSITIONS_V2 = new Map([
372
+ ["provisional", new Set(["active", "retired"])],
373
+ ["active", new Set(["frozen", "retired"])],
374
+ ["frozen", new Set(["active", "retired"])],
375
+ ["retired", new Set()],
376
+ ]);
377
+ const WALLET_TERMINALS_V2 = new Set(["retired"]);
378
+
379
+ const TX_TRANSITIONS_V2 = new Map([
380
+ ["pending", new Set(["submitted", "rejected"])],
381
+ ["submitted", new Set(["confirmed", "failed"])],
382
+ ["confirmed", new Set()],
383
+ ["failed", new Set()],
384
+ ["rejected", new Set()],
385
+ ]);
386
+ const TX_TERMINALS_V2 = new Set(["confirmed", "failed", "rejected"]);
387
+
388
+ export const WALLET_DEFAULT_MAX_ACTIVE_WALLETS_PER_OWNER = 10;
389
+ export const WALLET_DEFAULT_MAX_PENDING_TX_PER_WALLET = 25;
390
+ export const WALLET_DEFAULT_WALLET_IDLE_MS = 1000 * 60 * 60 * 24 * 180; // 180 days
391
+ export const WALLET_DEFAULT_TX_STUCK_MS = 1000 * 60 * 60 * 24; // 1 day
392
+
393
+ const _walletsV2 = new Map();
394
+ const _txsV2 = new Map();
395
+ let _maxActiveWalletsPerOwnerV2 = WALLET_DEFAULT_MAX_ACTIVE_WALLETS_PER_OWNER;
396
+ let _maxPendingTxPerWalletV2 = WALLET_DEFAULT_MAX_PENDING_TX_PER_WALLET;
397
+ let _walletIdleMsV2 = WALLET_DEFAULT_WALLET_IDLE_MS;
398
+ let _txStuckMsV2 = WALLET_DEFAULT_TX_STUCK_MS;
399
+
400
+ function _posIntWalletV2(n, label) {
401
+ const v = Math.floor(Number(n));
402
+ if (!Number.isFinite(v) || v <= 0)
403
+ throw new Error(`${label} must be a positive integer`);
404
+ return v;
405
+ }
406
+
407
+ export function getMaxActiveWalletsPerOwnerV2() {
408
+ return _maxActiveWalletsPerOwnerV2;
409
+ }
410
+ export function setMaxActiveWalletsPerOwnerV2(n) {
411
+ _maxActiveWalletsPerOwnerV2 = _posIntWalletV2(n, "maxActiveWalletsPerOwner");
412
+ }
413
+ export function getMaxPendingTxPerWalletV2() {
414
+ return _maxPendingTxPerWalletV2;
415
+ }
416
+ export function setMaxPendingTxPerWalletV2(n) {
417
+ _maxPendingTxPerWalletV2 = _posIntWalletV2(n, "maxPendingTxPerWallet");
418
+ }
419
+ export function getWalletIdleMsV2() {
420
+ return _walletIdleMsV2;
421
+ }
422
+ export function setWalletIdleMsV2(n) {
423
+ _walletIdleMsV2 = _posIntWalletV2(n, "walletIdleMs");
424
+ }
425
+ export function getTxStuckMsV2() {
426
+ return _txStuckMsV2;
427
+ }
428
+ export function setTxStuckMsV2(n) {
429
+ _txStuckMsV2 = _posIntWalletV2(n, "txStuckMs");
430
+ }
431
+
432
+ export function getActiveWalletCountV2(owner) {
433
+ let n = 0;
434
+ for (const w of _walletsV2.values()) {
435
+ if (w.owner === owner && w.maturity === "active") n += 1;
436
+ }
437
+ return n;
438
+ }
439
+
440
+ export function getPendingTxCountV2(walletId) {
441
+ let n = 0;
442
+ for (const t of _txsV2.values()) {
443
+ if (
444
+ t.walletId === walletId &&
445
+ (t.status === "pending" || t.status === "submitted")
446
+ )
447
+ n += 1;
448
+ }
449
+ return n;
450
+ }
451
+
452
+ function _copyWalletV2(w) {
453
+ return { ...w, metadata: { ...w.metadata } };
454
+ }
455
+ function _copyTxV2(t) {
456
+ return { ...t, metadata: { ...t.metadata } };
457
+ }
458
+
459
+ export function registerWalletV2(
460
+ id,
461
+ { owner, address, metadata = {}, now = Date.now() } = {},
462
+ ) {
463
+ if (!id || typeof id !== "string") throw new Error("id must be a string");
464
+ if (!owner || typeof owner !== "string")
465
+ throw new Error("owner must be a string");
466
+ if (!address || typeof address !== "string")
467
+ throw new Error("address must be a string");
468
+ if (_walletsV2.has(id)) throw new Error(`wallet ${id} already exists`);
469
+ const w = {
470
+ id,
471
+ owner,
472
+ address,
473
+ maturity: "provisional",
474
+ createdAt: now,
475
+ lastSeenAt: now,
476
+ activatedAt: null,
477
+ metadata: { ...metadata },
478
+ };
479
+ _walletsV2.set(id, w);
480
+ return _copyWalletV2(w);
481
+ }
482
+
483
+ export function getWalletV2(id) {
484
+ const w = _walletsV2.get(id);
485
+ return w ? _copyWalletV2(w) : null;
486
+ }
487
+
488
+ export function listWalletsV2({ owner, maturity } = {}) {
489
+ const out = [];
490
+ for (const w of _walletsV2.values()) {
491
+ if (owner && w.owner !== owner) continue;
492
+ if (maturity && w.maturity !== maturity) continue;
493
+ out.push(_copyWalletV2(w));
494
+ }
495
+ return out;
496
+ }
497
+
498
+ export function setWalletMaturityV2(id, next, { now = Date.now() } = {}) {
499
+ const w = _walletsV2.get(id);
500
+ if (!w) throw new Error(`wallet ${id} not found`);
501
+ if (!WALLET_TRANSITIONS_V2.has(next))
502
+ throw new Error(`unknown wallet maturity: ${next}`);
503
+ if (WALLET_TERMINALS_V2.has(w.maturity))
504
+ throw new Error(`wallet ${id} is in terminal state ${w.maturity}`);
505
+ const allowed = WALLET_TRANSITIONS_V2.get(w.maturity);
506
+ if (!allowed.has(next))
507
+ throw new Error(`cannot transition wallet from ${w.maturity} to ${next}`);
508
+ if (next === "active") {
509
+ if (w.maturity === "provisional") {
510
+ const count = getActiveWalletCountV2(w.owner);
511
+ if (count >= _maxActiveWalletsPerOwnerV2)
512
+ throw new Error(
513
+ `owner ${w.owner} already at active-wallet cap (${_maxActiveWalletsPerOwnerV2})`,
514
+ );
515
+ }
516
+ if (!w.activatedAt) w.activatedAt = now;
517
+ }
518
+ w.maturity = next;
519
+ w.lastSeenAt = now;
520
+ return _copyWalletV2(w);
521
+ }
522
+
523
+ export function activateWalletV2(id, opts) {
524
+ return setWalletMaturityV2(id, "active", opts);
525
+ }
526
+ export function freezeWalletV2(id, opts) {
527
+ return setWalletMaturityV2(id, "frozen", opts);
528
+ }
529
+ export function retireWalletV2(id, opts) {
530
+ return setWalletMaturityV2(id, "retired", opts);
531
+ }
532
+
533
+ export function touchWalletV2(id, { now = Date.now() } = {}) {
534
+ const w = _walletsV2.get(id);
535
+ if (!w) throw new Error(`wallet ${id} not found`);
536
+ w.lastSeenAt = now;
537
+ return _copyWalletV2(w);
538
+ }
539
+
540
+ export function createTxV2(
541
+ id,
542
+ { walletId, kind, amount, metadata = {}, now = Date.now() } = {},
543
+ ) {
544
+ if (!id || typeof id !== "string") throw new Error("id must be a string");
545
+ if (!walletId || typeof walletId !== "string")
546
+ throw new Error("walletId must be a string");
547
+ if (!kind || typeof kind !== "string")
548
+ throw new Error("kind must be a string");
549
+ if (!_walletsV2.has(walletId))
550
+ throw new Error(`wallet ${walletId} not found`);
551
+ if (_txsV2.has(id)) throw new Error(`tx ${id} already exists`);
552
+ const count = getPendingTxCountV2(walletId);
553
+ if (count >= _maxPendingTxPerWalletV2)
554
+ throw new Error(
555
+ `wallet ${walletId} already at pending-tx cap (${_maxPendingTxPerWalletV2})`,
556
+ );
557
+ const t = {
558
+ id,
559
+ walletId,
560
+ kind,
561
+ amount: amount ?? null,
562
+ status: "pending",
563
+ createdAt: now,
564
+ lastSeenAt: now,
565
+ submittedAt: null,
566
+ settledAt: null,
567
+ metadata: { ...metadata },
568
+ };
569
+ _txsV2.set(id, t);
570
+ return _copyTxV2(t);
571
+ }
572
+
573
+ export function getTxV2(id) {
574
+ const t = _txsV2.get(id);
575
+ return t ? _copyTxV2(t) : null;
576
+ }
577
+
578
+ export function listTxsV2({ walletId, status } = {}) {
579
+ const out = [];
580
+ for (const t of _txsV2.values()) {
581
+ if (walletId && t.walletId !== walletId) continue;
582
+ if (status && t.status !== status) continue;
583
+ out.push(_copyTxV2(t));
584
+ }
585
+ return out;
586
+ }
587
+
588
+ export function setTxStatusV2(id, next, { now = Date.now() } = {}) {
589
+ const t = _txsV2.get(id);
590
+ if (!t) throw new Error(`tx ${id} not found`);
591
+ if (!TX_TRANSITIONS_V2.has(next))
592
+ throw new Error(`unknown tx status: ${next}`);
593
+ if (TX_TERMINALS_V2.has(t.status))
594
+ throw new Error(`tx ${id} is in terminal state ${t.status}`);
595
+ const allowed = TX_TRANSITIONS_V2.get(t.status);
596
+ if (!allowed.has(next))
597
+ throw new Error(`cannot transition tx from ${t.status} to ${next}`);
598
+ if (next === "submitted" && !t.submittedAt) t.submittedAt = now;
599
+ if (TX_TERMINALS_V2.has(next) && !t.settledAt) t.settledAt = now;
600
+ t.status = next;
601
+ t.lastSeenAt = now;
602
+ return _copyTxV2(t);
603
+ }
604
+
605
+ export function submitTxV2(id, opts) {
606
+ return setTxStatusV2(id, "submitted", opts);
607
+ }
608
+ export function confirmTxV2(id, opts) {
609
+ return setTxStatusV2(id, "confirmed", opts);
610
+ }
611
+ export function failTxV2(id, opts) {
612
+ return setTxStatusV2(id, "failed", opts);
613
+ }
614
+ export function rejectTxV2(id, opts) {
615
+ return setTxStatusV2(id, "rejected", opts);
616
+ }
617
+
618
+ export function autoRetireIdleWalletsV2({ now = Date.now() } = {}) {
619
+ const flipped = [];
620
+ for (const w of _walletsV2.values()) {
621
+ if (w.maturity === "retired" || w.maturity === "provisional") continue;
622
+ if (now - w.lastSeenAt > _walletIdleMsV2) {
623
+ w.maturity = "retired";
624
+ w.lastSeenAt = now;
625
+ flipped.push(_copyWalletV2(w));
626
+ }
627
+ }
628
+ return flipped;
629
+ }
630
+
631
+ export function autoFailStuckTxV2({ now = Date.now() } = {}) {
632
+ const flipped = [];
633
+ for (const t of _txsV2.values()) {
634
+ if (t.status !== "submitted") continue;
635
+ const ref = t.submittedAt ?? t.lastSeenAt;
636
+ if (now - ref > _txStuckMsV2) {
637
+ t.status = "failed";
638
+ t.lastSeenAt = now;
639
+ if (!t.settledAt) t.settledAt = now;
640
+ flipped.push(_copyTxV2(t));
641
+ }
642
+ }
643
+ return flipped;
644
+ }
645
+
646
+ export function getWalletManagerStatsV2() {
647
+ const walletsByMaturity = {};
648
+ for (const v of Object.values(WALLET_MATURITY_V2)) walletsByMaturity[v] = 0;
649
+ for (const w of _walletsV2.values()) walletsByMaturity[w.maturity] += 1;
650
+
651
+ const txsByStatus = {};
652
+ for (const v of Object.values(TX_LIFECYCLE_V2)) txsByStatus[v] = 0;
653
+ for (const t of _txsV2.values()) txsByStatus[t.status] += 1;
654
+
655
+ return {
656
+ totalWalletsV2: _walletsV2.size,
657
+ totalTxsV2: _txsV2.size,
658
+ maxActiveWalletsPerOwner: _maxActiveWalletsPerOwnerV2,
659
+ maxPendingTxPerWallet: _maxPendingTxPerWalletV2,
660
+ walletIdleMs: _walletIdleMsV2,
661
+ txStuckMs: _txStuckMsV2,
662
+ walletsByMaturity,
663
+ txsByStatus,
664
+ };
665
+ }
666
+
667
+ export function _resetStateWalletManagerV2() {
668
+ _walletsV2.clear();
669
+ _txsV2.clear();
670
+ _maxActiveWalletsPerOwnerV2 = WALLET_DEFAULT_MAX_ACTIVE_WALLETS_PER_OWNER;
671
+ _maxPendingTxPerWalletV2 = WALLET_DEFAULT_MAX_PENDING_TX_PER_WALLET;
672
+ _walletIdleMsV2 = WALLET_DEFAULT_WALLET_IDLE_MS;
673
+ _txStuckMsV2 = WALLET_DEFAULT_TX_STUCK_MS;
674
+ }
@@ -222,3 +222,84 @@ export async function webFetch(url, options = {}) {
222
222
  }
223
223
 
224
224
  export const _deps = { http, https };
225
+
226
+ // ===== V2 Surface: Web Fetch governance overlay (CLI v0.142.0) =====
227
+ export const WFET_TARGET_MATURITY_V2 = Object.freeze({
228
+ PENDING: "pending", ACTIVE: "active", DEGRADED: "degraded", RETIRED: "retired",
229
+ });
230
+ export const WFET_JOB_LIFECYCLE_V2 = Object.freeze({
231
+ QUEUED: "queued", FETCHING: "fetching", SUCCEEDED: "succeeded", FAILED: "failed", CANCELLED: "cancelled",
232
+ });
233
+ const _wfetTTrans = new Map([
234
+ [WFET_TARGET_MATURITY_V2.PENDING, new Set([WFET_TARGET_MATURITY_V2.ACTIVE, WFET_TARGET_MATURITY_V2.RETIRED])],
235
+ [WFET_TARGET_MATURITY_V2.ACTIVE, new Set([WFET_TARGET_MATURITY_V2.DEGRADED, WFET_TARGET_MATURITY_V2.RETIRED])],
236
+ [WFET_TARGET_MATURITY_V2.DEGRADED, new Set([WFET_TARGET_MATURITY_V2.ACTIVE, WFET_TARGET_MATURITY_V2.RETIRED])],
237
+ [WFET_TARGET_MATURITY_V2.RETIRED, new Set()],
238
+ ]);
239
+ const _wfetTTerminal = new Set([WFET_TARGET_MATURITY_V2.RETIRED]);
240
+ const _wfetJTrans = new Map([
241
+ [WFET_JOB_LIFECYCLE_V2.QUEUED, new Set([WFET_JOB_LIFECYCLE_V2.FETCHING, WFET_JOB_LIFECYCLE_V2.CANCELLED])],
242
+ [WFET_JOB_LIFECYCLE_V2.FETCHING, new Set([WFET_JOB_LIFECYCLE_V2.SUCCEEDED, WFET_JOB_LIFECYCLE_V2.FAILED, WFET_JOB_LIFECYCLE_V2.CANCELLED])],
243
+ [WFET_JOB_LIFECYCLE_V2.SUCCEEDED, new Set()],
244
+ [WFET_JOB_LIFECYCLE_V2.FAILED, new Set()],
245
+ [WFET_JOB_LIFECYCLE_V2.CANCELLED, new Set()],
246
+ ]);
247
+ const _wfetTsV2 = new Map();
248
+ const _wfetJsV2 = new Map();
249
+ let _wfetMaxActive = 12, _wfetMaxPending = 30, _wfetIdleMs = 7 * 24 * 60 * 60 * 1000, _wfetStuckMs = 60 * 1000;
250
+ function _wfetPos(n, label) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${label} must be positive integer`); return v; }
251
+ function _wfetCheckT(from, to) { const a = _wfetTTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid wfet target transition ${from} → ${to}`); }
252
+ function _wfetCheckJ(from, to) { const a = _wfetJTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid wfet job transition ${from} → ${to}`); }
253
+ export function setMaxActiveWfetTargetsPerOwnerV2(n) { _wfetMaxActive = _wfetPos(n, "maxActiveWfetTargetsPerOwner"); }
254
+ export function getMaxActiveWfetTargetsPerOwnerV2() { return _wfetMaxActive; }
255
+ export function setMaxPendingWfetJobsPerTargetV2(n) { _wfetMaxPending = _wfetPos(n, "maxPendingWfetJobsPerTarget"); }
256
+ export function getMaxPendingWfetJobsPerTargetV2() { return _wfetMaxPending; }
257
+ export function setWfetTargetIdleMsV2(n) { _wfetIdleMs = _wfetPos(n, "wfetTargetIdleMs"); }
258
+ export function getWfetTargetIdleMsV2() { return _wfetIdleMs; }
259
+ export function setWfetJobStuckMsV2(n) { _wfetStuckMs = _wfetPos(n, "wfetJobStuckMs"); }
260
+ export function getWfetJobStuckMsV2() { return _wfetStuckMs; }
261
+ export function _resetStateWebFetchV2() { _wfetTsV2.clear(); _wfetJsV2.clear(); _wfetMaxActive = 12; _wfetMaxPending = 30; _wfetIdleMs = 7 * 24 * 60 * 60 * 1000; _wfetStuckMs = 60 * 1000; }
262
+ export function registerWfetTargetV2({ id, owner, baseUrl, metadata } = {}) {
263
+ if (!id) throw new Error("wfet target id required"); if (!owner) throw new Error("wfet target owner required");
264
+ if (_wfetTsV2.has(id)) throw new Error(`wfet target ${id} already registered`);
265
+ const now = Date.now();
266
+ const t = { id, owner, baseUrl: baseUrl || "", status: WFET_TARGET_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, retiredAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
267
+ _wfetTsV2.set(id, t); return { ...t, metadata: { ...t.metadata } };
268
+ }
269
+ function _wfetCountActive(owner) { let n = 0; for (const t of _wfetTsV2.values()) if (t.owner === owner && t.status === WFET_TARGET_MATURITY_V2.ACTIVE) n++; return n; }
270
+ export function activateWfetTargetV2(id) {
271
+ const t = _wfetTsV2.get(id); if (!t) throw new Error(`wfet target ${id} not found`);
272
+ _wfetCheckT(t.status, WFET_TARGET_MATURITY_V2.ACTIVE);
273
+ const recovery = t.status === WFET_TARGET_MATURITY_V2.DEGRADED;
274
+ if (!recovery && _wfetCountActive(t.owner) >= _wfetMaxActive) throw new Error(`max active wfet targets for owner ${t.owner} reached`);
275
+ const now = Date.now(); t.status = WFET_TARGET_MATURITY_V2.ACTIVE; t.updatedAt = now; t.lastTouchedAt = now; if (!t.activatedAt) t.activatedAt = now;
276
+ return { ...t, metadata: { ...t.metadata } };
277
+ }
278
+ export function degradeWfetTargetV2(id) { const t = _wfetTsV2.get(id); if (!t) throw new Error(`wfet target ${id} not found`); _wfetCheckT(t.status, WFET_TARGET_MATURITY_V2.DEGRADED); t.status = WFET_TARGET_MATURITY_V2.DEGRADED; t.updatedAt = Date.now(); return { ...t, metadata: { ...t.metadata } }; }
279
+ export function retireWfetTargetV2(id) { const t = _wfetTsV2.get(id); if (!t) throw new Error(`wfet target ${id} not found`); _wfetCheckT(t.status, WFET_TARGET_MATURITY_V2.RETIRED); const now = Date.now(); t.status = WFET_TARGET_MATURITY_V2.RETIRED; t.updatedAt = now; if (!t.retiredAt) t.retiredAt = now; return { ...t, metadata: { ...t.metadata } }; }
280
+ export function touchWfetTargetV2(id) { const t = _wfetTsV2.get(id); if (!t) throw new Error(`wfet target ${id} not found`); if (_wfetTTerminal.has(t.status)) throw new Error(`cannot touch terminal wfet target ${id}`); const now = Date.now(); t.lastTouchedAt = now; t.updatedAt = now; return { ...t, metadata: { ...t.metadata } }; }
281
+ export function getWfetTargetV2(id) { const t = _wfetTsV2.get(id); if (!t) return null; return { ...t, metadata: { ...t.metadata } }; }
282
+ export function listWfetTargetsV2() { return [..._wfetTsV2.values()].map((t) => ({ ...t, metadata: { ...t.metadata } })); }
283
+ function _wfetCountPending(targetId) { let n = 0; for (const j of _wfetJsV2.values()) if (j.targetId === targetId && (j.status === WFET_JOB_LIFECYCLE_V2.QUEUED || j.status === WFET_JOB_LIFECYCLE_V2.FETCHING)) n++; return n; }
284
+ export function createWfetJobV2({ id, targetId, kind, metadata } = {}) {
285
+ if (!id) throw new Error("wfet job id required"); if (!targetId) throw new Error("wfet job targetId required");
286
+ if (_wfetJsV2.has(id)) throw new Error(`wfet job ${id} already exists`);
287
+ if (!_wfetTsV2.has(targetId)) throw new Error(`wfet target ${targetId} not found`);
288
+ if (_wfetCountPending(targetId) >= _wfetMaxPending) throw new Error(`max pending wfet jobs for target ${targetId} reached`);
289
+ const now = Date.now();
290
+ const j = { id, targetId, kind: kind || "GET", status: WFET_JOB_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
291
+ _wfetJsV2.set(id, j); return { ...j, metadata: { ...j.metadata } };
292
+ }
293
+ export function fetchingWfetJobV2(id) { const j = _wfetJsV2.get(id); if (!j) throw new Error(`wfet job ${id} not found`); _wfetCheckJ(j.status, WFET_JOB_LIFECYCLE_V2.FETCHING); const now = Date.now(); j.status = WFET_JOB_LIFECYCLE_V2.FETCHING; j.updatedAt = now; if (!j.startedAt) j.startedAt = now; return { ...j, metadata: { ...j.metadata } }; }
294
+ export function succeedWfetJobV2(id) { const j = _wfetJsV2.get(id); if (!j) throw new Error(`wfet job ${id} not found`); _wfetCheckJ(j.status, WFET_JOB_LIFECYCLE_V2.SUCCEEDED); const now = Date.now(); j.status = WFET_JOB_LIFECYCLE_V2.SUCCEEDED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; return { ...j, metadata: { ...j.metadata } }; }
295
+ export function failWfetJobV2(id, reason) { const j = _wfetJsV2.get(id); if (!j) throw new Error(`wfet job ${id} not found`); _wfetCheckJ(j.status, WFET_JOB_LIFECYCLE_V2.FAILED); const now = Date.now(); j.status = WFET_JOB_LIFECYCLE_V2.FAILED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; if (reason) j.metadata.failReason = String(reason); return { ...j, metadata: { ...j.metadata } }; }
296
+ export function cancelWfetJobV2(id, reason) { const j = _wfetJsV2.get(id); if (!j) throw new Error(`wfet job ${id} not found`); _wfetCheckJ(j.status, WFET_JOB_LIFECYCLE_V2.CANCELLED); const now = Date.now(); j.status = WFET_JOB_LIFECYCLE_V2.CANCELLED; j.updatedAt = now; if (!j.settledAt) j.settledAt = now; if (reason) j.metadata.cancelReason = String(reason); return { ...j, metadata: { ...j.metadata } }; }
297
+ export function getWfetJobV2(id) { const j = _wfetJsV2.get(id); if (!j) return null; return { ...j, metadata: { ...j.metadata } }; }
298
+ export function listWfetJobsV2() { return [..._wfetJsV2.values()].map((j) => ({ ...j, metadata: { ...j.metadata } })); }
299
+ export function autoDegradeIdleWfetTargetsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const x of _wfetTsV2.values()) if (x.status === WFET_TARGET_MATURITY_V2.ACTIVE && (t - x.lastTouchedAt) >= _wfetIdleMs) { x.status = WFET_TARGET_MATURITY_V2.DEGRADED; x.updatedAt = t; flipped.push(x.id); } return { flipped, count: flipped.length }; }
300
+ export function autoFailStuckWfetJobsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const j of _wfetJsV2.values()) if (j.status === WFET_JOB_LIFECYCLE_V2.FETCHING && j.startedAt != null && (t - j.startedAt) >= _wfetStuckMs) { j.status = WFET_JOB_LIFECYCLE_V2.FAILED; j.updatedAt = t; if (!j.settledAt) j.settledAt = t; j.metadata.failReason = "auto-fail-stuck"; flipped.push(j.id); } return { flipped, count: flipped.length }; }
301
+ export function getWebFetchGovStatsV2() {
302
+ const targetsByStatus = {}; for (const v of Object.values(WFET_TARGET_MATURITY_V2)) targetsByStatus[v] = 0; for (const t of _wfetTsV2.values()) targetsByStatus[t.status]++;
303
+ const jobsByStatus = {}; for (const v of Object.values(WFET_JOB_LIFECYCLE_V2)) jobsByStatus[v] = 0; for (const j of _wfetJsV2.values()) jobsByStatus[j.status]++;
304
+ return { totalWfetTargetsV2: _wfetTsV2.size, totalWfetJobsV2: _wfetJsV2.size, maxActiveWfetTargetsPerOwner: _wfetMaxActive, maxPendingWfetJobsPerTarget: _wfetMaxPending, wfetTargetIdleMs: _wfetIdleMs, wfetJobStuckMs: _wfetStuckMs, targetsByStatus, jobsByStatus };
305
+ }