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
@@ -447,25 +447,53 @@ export function _resetState() {
447
447
  };
448
448
  }
449
449
 
450
-
451
450
  // ===== V2 Surface: Matrix Bridge governance overlay (CLI v0.134.0) =====
452
451
  export const MX_ROOM_MATURITY_V2 = Object.freeze({
453
- PENDING: "pending", ACTIVE: "active", MUTED: "muted", ARCHIVED: "archived",
452
+ PENDING: "pending",
453
+ ACTIVE: "active",
454
+ MUTED: "muted",
455
+ ARCHIVED: "archived",
454
456
  });
455
457
  export const MX_MESSAGE_LIFECYCLE_V2 = Object.freeze({
456
- QUEUED: "queued", SENDING: "sending", DELIVERED: "delivered", FAILED: "failed", CANCELLED: "cancelled",
458
+ QUEUED: "queued",
459
+ SENDING: "sending",
460
+ DELIVERED: "delivered",
461
+ FAILED: "failed",
462
+ CANCELLED: "cancelled",
457
463
  });
458
464
 
459
465
  const _mxRoomTrans = new Map([
460
- [MX_ROOM_MATURITY_V2.PENDING, new Set([MX_ROOM_MATURITY_V2.ACTIVE, MX_ROOM_MATURITY_V2.ARCHIVED])],
461
- [MX_ROOM_MATURITY_V2.ACTIVE, new Set([MX_ROOM_MATURITY_V2.MUTED, MX_ROOM_MATURITY_V2.ARCHIVED])],
462
- [MX_ROOM_MATURITY_V2.MUTED, new Set([MX_ROOM_MATURITY_V2.ACTIVE, MX_ROOM_MATURITY_V2.ARCHIVED])],
466
+ [
467
+ MX_ROOM_MATURITY_V2.PENDING,
468
+ new Set([MX_ROOM_MATURITY_V2.ACTIVE, MX_ROOM_MATURITY_V2.ARCHIVED]),
469
+ ],
470
+ [
471
+ MX_ROOM_MATURITY_V2.ACTIVE,
472
+ new Set([MX_ROOM_MATURITY_V2.MUTED, MX_ROOM_MATURITY_V2.ARCHIVED]),
473
+ ],
474
+ [
475
+ MX_ROOM_MATURITY_V2.MUTED,
476
+ new Set([MX_ROOM_MATURITY_V2.ACTIVE, MX_ROOM_MATURITY_V2.ARCHIVED]),
477
+ ],
463
478
  [MX_ROOM_MATURITY_V2.ARCHIVED, new Set()],
464
479
  ]);
465
480
  const _mxRoomTerminal = new Set([MX_ROOM_MATURITY_V2.ARCHIVED]);
466
481
  const _mxMsgTrans = new Map([
467
- [MX_MESSAGE_LIFECYCLE_V2.QUEUED, new Set([MX_MESSAGE_LIFECYCLE_V2.SENDING, MX_MESSAGE_LIFECYCLE_V2.CANCELLED])],
468
- [MX_MESSAGE_LIFECYCLE_V2.SENDING, new Set([MX_MESSAGE_LIFECYCLE_V2.DELIVERED, MX_MESSAGE_LIFECYCLE_V2.FAILED, MX_MESSAGE_LIFECYCLE_V2.CANCELLED])],
482
+ [
483
+ MX_MESSAGE_LIFECYCLE_V2.QUEUED,
484
+ new Set([
485
+ MX_MESSAGE_LIFECYCLE_V2.SENDING,
486
+ MX_MESSAGE_LIFECYCLE_V2.CANCELLED,
487
+ ]),
488
+ ],
489
+ [
490
+ MX_MESSAGE_LIFECYCLE_V2.SENDING,
491
+ new Set([
492
+ MX_MESSAGE_LIFECYCLE_V2.DELIVERED,
493
+ MX_MESSAGE_LIFECYCLE_V2.FAILED,
494
+ MX_MESSAGE_LIFECYCLE_V2.CANCELLED,
495
+ ]),
496
+ ],
469
497
  [MX_MESSAGE_LIFECYCLE_V2.DELIVERED, new Set()],
470
498
  [MX_MESSAGE_LIFECYCLE_V2.FAILED, new Set()],
471
499
  [MX_MESSAGE_LIFECYCLE_V2.CANCELLED, new Set()],
@@ -478,21 +506,45 @@ let _mxMaxPendingPerRoom = 40;
478
506
  let _mxRoomIdleMs = 24 * 60 * 60 * 1000;
479
507
  let _mxMsgStuckMs = 3 * 60 * 1000;
480
508
 
481
- function _mxPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
509
+ function _mxPos(n, lbl) {
510
+ const v = Math.floor(Number(n));
511
+ if (!Number.isFinite(v) || v <= 0)
512
+ throw new Error(`${lbl} must be positive integer`);
513
+ return v;
514
+ }
482
515
 
483
- export function setMaxActiveMatrixRoomsPerOwnerV2(n) { _mxMaxActivePerOwner = _mxPos(n, "maxActiveMatrixRoomsPerOwner"); }
484
- export function getMaxActiveMatrixRoomsPerOwnerV2() { return _mxMaxActivePerOwner; }
485
- export function setMaxPendingMatrixMessagesPerRoomV2(n) { _mxMaxPendingPerRoom = _mxPos(n, "maxPendingMatrixMessagesPerRoom"); }
486
- export function getMaxPendingMatrixMessagesPerRoomV2() { return _mxMaxPendingPerRoom; }
487
- export function setMatrixRoomIdleMsV2(n) { _mxRoomIdleMs = _mxPos(n, "matrixRoomIdleMs"); }
488
- export function getMatrixRoomIdleMsV2() { return _mxRoomIdleMs; }
489
- export function setMatrixMessageStuckMsV2(n) { _mxMsgStuckMs = _mxPos(n, "matrixMessageStuckMs"); }
490
- export function getMatrixMessageStuckMsV2() { return _mxMsgStuckMs; }
516
+ export function setMaxActiveMatrixRoomsPerOwnerV2(n) {
517
+ _mxMaxActivePerOwner = _mxPos(n, "maxActiveMatrixRoomsPerOwner");
518
+ }
519
+ export function getMaxActiveMatrixRoomsPerOwnerV2() {
520
+ return _mxMaxActivePerOwner;
521
+ }
522
+ export function setMaxPendingMatrixMessagesPerRoomV2(n) {
523
+ _mxMaxPendingPerRoom = _mxPos(n, "maxPendingMatrixMessagesPerRoom");
524
+ }
525
+ export function getMaxPendingMatrixMessagesPerRoomV2() {
526
+ return _mxMaxPendingPerRoom;
527
+ }
528
+ export function setMatrixRoomIdleMsV2(n) {
529
+ _mxRoomIdleMs = _mxPos(n, "matrixRoomIdleMs");
530
+ }
531
+ export function getMatrixRoomIdleMsV2() {
532
+ return _mxRoomIdleMs;
533
+ }
534
+ export function setMatrixMessageStuckMsV2(n) {
535
+ _mxMsgStuckMs = _mxPos(n, "matrixMessageStuckMs");
536
+ }
537
+ export function getMatrixMessageStuckMsV2() {
538
+ return _mxMsgStuckMs;
539
+ }
491
540
 
492
541
  export function _resetStateMatrixBridgeV2() {
493
- _mxRooms.clear(); _mxMsgs.clear();
494
- _mxMaxActivePerOwner = 20; _mxMaxPendingPerRoom = 40;
495
- _mxRoomIdleMs = 24 * 60 * 60 * 1000; _mxMsgStuckMs = 3 * 60 * 1000;
542
+ _mxRooms.clear();
543
+ _mxMsgs.clear();
544
+ _mxMaxActivePerOwner = 20;
545
+ _mxMaxPendingPerRoom = 40;
546
+ _mxRoomIdleMs = 24 * 60 * 60 * 1000;
547
+ _mxMsgStuckMs = 3 * 60 * 1000;
496
548
  }
497
549
 
498
550
  export function registerMatrixRoomV2({ id, owner, alias, metadata } = {}) {
@@ -500,54 +552,575 @@ export function registerMatrixRoomV2({ id, owner, alias, metadata } = {}) {
500
552
  if (!owner || typeof owner !== "string") throw new Error("owner is required");
501
553
  if (_mxRooms.has(id)) throw new Error(`matrix room ${id} already registered`);
502
554
  const now = Date.now();
503
- const r = { id, owner, alias: alias || id, status: MX_ROOM_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
555
+ const r = {
556
+ id,
557
+ owner,
558
+ alias: alias || id,
559
+ status: MX_ROOM_MATURITY_V2.PENDING,
560
+ createdAt: now,
561
+ updatedAt: now,
562
+ activatedAt: null,
563
+ archivedAt: null,
564
+ lastTouchedAt: now,
565
+ metadata: { ...(metadata || {}) },
566
+ };
504
567
  _mxRooms.set(id, r);
505
568
  return { ...r, metadata: { ...r.metadata } };
506
569
  }
507
- function _mxCheckR(from, to) { const a = _mxRoomTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid matrix room transition ${from} → ${to}`); }
508
- function _mxCountActive(owner) { let n = 0; for (const r of _mxRooms.values()) if (r.owner === owner && r.status === MX_ROOM_MATURITY_V2.ACTIVE) n++; return n; }
570
+ function _mxCheckR(from, to) {
571
+ const a = _mxRoomTrans.get(from);
572
+ if (!a || !a.has(to))
573
+ throw new Error(`invalid matrix room transition ${from} → ${to}`);
574
+ }
575
+ function _mxCountActive(owner) {
576
+ let n = 0;
577
+ for (const r of _mxRooms.values())
578
+ if (r.owner === owner && r.status === MX_ROOM_MATURITY_V2.ACTIVE) n++;
579
+ return n;
580
+ }
509
581
 
510
582
  export function activateMatrixRoomV2(id) {
511
- const r = _mxRooms.get(id); if (!r) throw new Error(`matrix room ${id} not found`);
583
+ const r = _mxRooms.get(id);
584
+ if (!r) throw new Error(`matrix room ${id} not found`);
512
585
  _mxCheckR(r.status, MX_ROOM_MATURITY_V2.ACTIVE);
513
586
  const recovery = r.status === MX_ROOM_MATURITY_V2.MUTED;
514
- if (!recovery) { const a = _mxCountActive(r.owner); if (a >= _mxMaxActivePerOwner) throw new Error(`max active matrix rooms per owner (${_mxMaxActivePerOwner}) reached for ${r.owner}`); }
515
- const now = Date.now(); r.status = MX_ROOM_MATURITY_V2.ACTIVE; r.updatedAt = now; r.lastTouchedAt = now; if (!r.activatedAt) r.activatedAt = now;
587
+ if (!recovery) {
588
+ const a = _mxCountActive(r.owner);
589
+ if (a >= _mxMaxActivePerOwner)
590
+ throw new Error(
591
+ `max active matrix rooms per owner (${_mxMaxActivePerOwner}) reached for ${r.owner}`,
592
+ );
593
+ }
594
+ const now = Date.now();
595
+ r.status = MX_ROOM_MATURITY_V2.ACTIVE;
596
+ r.updatedAt = now;
597
+ r.lastTouchedAt = now;
598
+ if (!r.activatedAt) r.activatedAt = now;
599
+ return { ...r, metadata: { ...r.metadata } };
600
+ }
601
+ export function muteMatrixRoomV2(id) {
602
+ const r = _mxRooms.get(id);
603
+ if (!r) throw new Error(`matrix room ${id} not found`);
604
+ _mxCheckR(r.status, MX_ROOM_MATURITY_V2.MUTED);
605
+ r.status = MX_ROOM_MATURITY_V2.MUTED;
606
+ r.updatedAt = Date.now();
516
607
  return { ...r, metadata: { ...r.metadata } };
517
608
  }
518
- export function muteMatrixRoomV2(id) { const r = _mxRooms.get(id); if (!r) throw new Error(`matrix room ${id} not found`); _mxCheckR(r.status, MX_ROOM_MATURITY_V2.MUTED); r.status = MX_ROOM_MATURITY_V2.MUTED; r.updatedAt = Date.now(); return { ...r, metadata: { ...r.metadata } }; }
519
- export function archiveMatrixRoomV2(id) { const r = _mxRooms.get(id); if (!r) throw new Error(`matrix room ${id} not found`); _mxCheckR(r.status, MX_ROOM_MATURITY_V2.ARCHIVED); const now = Date.now(); r.status = MX_ROOM_MATURITY_V2.ARCHIVED; r.updatedAt = now; if (!r.archivedAt) r.archivedAt = now; return { ...r, metadata: { ...r.metadata } }; }
520
- export function touchMatrixRoomV2(id) { const r = _mxRooms.get(id); if (!r) throw new Error(`matrix room ${id} not found`); if (_mxRoomTerminal.has(r.status)) throw new Error(`cannot touch terminal matrix room ${id}`); const now = Date.now(); r.lastTouchedAt = now; r.updatedAt = now; return { ...r, metadata: { ...r.metadata } }; }
521
- export function getMatrixRoomV2(id) { const r = _mxRooms.get(id); if (!r) return null; return { ...r, metadata: { ...r.metadata } }; }
522
- export function listMatrixRoomsV2() { return [..._mxRooms.values()].map((r) => ({ ...r, metadata: { ...r.metadata } })); }
609
+ export function archiveMatrixRoomV2(id) {
610
+ const r = _mxRooms.get(id);
611
+ if (!r) throw new Error(`matrix room ${id} not found`);
612
+ _mxCheckR(r.status, MX_ROOM_MATURITY_V2.ARCHIVED);
613
+ const now = Date.now();
614
+ r.status = MX_ROOM_MATURITY_V2.ARCHIVED;
615
+ r.updatedAt = now;
616
+ if (!r.archivedAt) r.archivedAt = now;
617
+ return { ...r, metadata: { ...r.metadata } };
618
+ }
619
+ export function touchMatrixRoomV2(id) {
620
+ const r = _mxRooms.get(id);
621
+ if (!r) throw new Error(`matrix room ${id} not found`);
622
+ if (_mxRoomTerminal.has(r.status))
623
+ throw new Error(`cannot touch terminal matrix room ${id}`);
624
+ const now = Date.now();
625
+ r.lastTouchedAt = now;
626
+ r.updatedAt = now;
627
+ return { ...r, metadata: { ...r.metadata } };
628
+ }
629
+ export function getMatrixRoomV2(id) {
630
+ const r = _mxRooms.get(id);
631
+ if (!r) return null;
632
+ return { ...r, metadata: { ...r.metadata } };
633
+ }
634
+ export function listMatrixRoomsV2() {
635
+ return [..._mxRooms.values()].map((r) => ({
636
+ ...r,
637
+ metadata: { ...r.metadata },
638
+ }));
639
+ }
523
640
 
524
- function _mxCountPending(rid) { let n = 0; for (const m of _mxMsgs.values()) if (m.roomId === rid && (m.status === MX_MESSAGE_LIFECYCLE_V2.QUEUED || m.status === MX_MESSAGE_LIFECYCLE_V2.SENDING)) n++; return n; }
641
+ function _mxCountPending(rid) {
642
+ let n = 0;
643
+ for (const m of _mxMsgs.values())
644
+ if (
645
+ m.roomId === rid &&
646
+ (m.status === MX_MESSAGE_LIFECYCLE_V2.QUEUED ||
647
+ m.status === MX_MESSAGE_LIFECYCLE_V2.SENDING)
648
+ )
649
+ n++;
650
+ return n;
651
+ }
525
652
 
526
653
  export function createMatrixMessageV2({ id, roomId, body, metadata } = {}) {
527
654
  if (!id || typeof id !== "string") throw new Error("id is required");
528
- if (!roomId || typeof roomId !== "string") throw new Error("roomId is required");
655
+ if (!roomId || typeof roomId !== "string")
656
+ throw new Error("roomId is required");
529
657
  if (_mxMsgs.has(id)) throw new Error(`matrix message ${id} already exists`);
530
658
  if (!_mxRooms.has(roomId)) throw new Error(`matrix room ${roomId} not found`);
531
659
  const pending = _mxCountPending(roomId);
532
- if (pending >= _mxMaxPendingPerRoom) throw new Error(`max pending matrix messages per room (${_mxMaxPendingPerRoom}) reached for ${roomId}`);
660
+ if (pending >= _mxMaxPendingPerRoom)
661
+ throw new Error(
662
+ `max pending matrix messages per room (${_mxMaxPendingPerRoom}) reached for ${roomId}`,
663
+ );
533
664
  const now = Date.now();
534
- const m = { id, roomId, body: body || "", status: MX_MESSAGE_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
665
+ const m = {
666
+ id,
667
+ roomId,
668
+ body: body || "",
669
+ status: MX_MESSAGE_LIFECYCLE_V2.QUEUED,
670
+ createdAt: now,
671
+ updatedAt: now,
672
+ startedAt: null,
673
+ settledAt: null,
674
+ metadata: { ...(metadata || {}) },
675
+ };
535
676
  _mxMsgs.set(id, m);
536
677
  return { ...m, metadata: { ...m.metadata } };
537
678
  }
538
- function _mxCheckM(from, to) { const a = _mxMsgTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid matrix message transition ${from} → ${to}`); }
539
- export function startMatrixMessageV2(id) { const m = _mxMsgs.get(id); if (!m) throw new Error(`matrix message ${id} not found`); _mxCheckM(m.status, MX_MESSAGE_LIFECYCLE_V2.SENDING); const now = Date.now(); m.status = MX_MESSAGE_LIFECYCLE_V2.SENDING; m.updatedAt = now; if (!m.startedAt) m.startedAt = now; return { ...m, metadata: { ...m.metadata } }; }
540
- export function deliverMatrixMessageV2(id) { const m = _mxMsgs.get(id); if (!m) throw new Error(`matrix message ${id} not found`); _mxCheckM(m.status, MX_MESSAGE_LIFECYCLE_V2.DELIVERED); const now = Date.now(); m.status = MX_MESSAGE_LIFECYCLE_V2.DELIVERED; m.updatedAt = now; if (!m.settledAt) m.settledAt = now; return { ...m, metadata: { ...m.metadata } }; }
541
- export function failMatrixMessageV2(id, reason) { const m = _mxMsgs.get(id); if (!m) throw new Error(`matrix message ${id} not found`); _mxCheckM(m.status, MX_MESSAGE_LIFECYCLE_V2.FAILED); const now = Date.now(); m.status = MX_MESSAGE_LIFECYCLE_V2.FAILED; m.updatedAt = now; if (!m.settledAt) m.settledAt = now; if (reason) m.metadata.failReason = String(reason); return { ...m, metadata: { ...m.metadata } }; }
542
- export function cancelMatrixMessageV2(id, reason) { const m = _mxMsgs.get(id); if (!m) throw new Error(`matrix message ${id} not found`); _mxCheckM(m.status, MX_MESSAGE_LIFECYCLE_V2.CANCELLED); const now = Date.now(); m.status = MX_MESSAGE_LIFECYCLE_V2.CANCELLED; m.updatedAt = now; if (!m.settledAt) m.settledAt = now; if (reason) m.metadata.cancelReason = String(reason); return { ...m, metadata: { ...m.metadata } }; }
543
- export function getMatrixMessageV2(id) { const m = _mxMsgs.get(id); if (!m) return null; return { ...m, metadata: { ...m.metadata } }; }
544
- export function listMatrixMessagesV2() { return [..._mxMsgs.values()].map((m) => ({ ...m, metadata: { ...m.metadata } })); }
679
+ function _mxCheckM(from, to) {
680
+ const a = _mxMsgTrans.get(from);
681
+ if (!a || !a.has(to))
682
+ throw new Error(`invalid matrix message transition ${from} ${to}`);
683
+ }
684
+ export function startMatrixMessageV2(id) {
685
+ const m = _mxMsgs.get(id);
686
+ if (!m) throw new Error(`matrix message ${id} not found`);
687
+ _mxCheckM(m.status, MX_MESSAGE_LIFECYCLE_V2.SENDING);
688
+ const now = Date.now();
689
+ m.status = MX_MESSAGE_LIFECYCLE_V2.SENDING;
690
+ m.updatedAt = now;
691
+ if (!m.startedAt) m.startedAt = now;
692
+ return { ...m, metadata: { ...m.metadata } };
693
+ }
694
+ export function deliverMatrixMessageV2(id) {
695
+ const m = _mxMsgs.get(id);
696
+ if (!m) throw new Error(`matrix message ${id} not found`);
697
+ _mxCheckM(m.status, MX_MESSAGE_LIFECYCLE_V2.DELIVERED);
698
+ const now = Date.now();
699
+ m.status = MX_MESSAGE_LIFECYCLE_V2.DELIVERED;
700
+ m.updatedAt = now;
701
+ if (!m.settledAt) m.settledAt = now;
702
+ return { ...m, metadata: { ...m.metadata } };
703
+ }
704
+ export function failMatrixMessageV2(id, reason) {
705
+ const m = _mxMsgs.get(id);
706
+ if (!m) throw new Error(`matrix message ${id} not found`);
707
+ _mxCheckM(m.status, MX_MESSAGE_LIFECYCLE_V2.FAILED);
708
+ const now = Date.now();
709
+ m.status = MX_MESSAGE_LIFECYCLE_V2.FAILED;
710
+ m.updatedAt = now;
711
+ if (!m.settledAt) m.settledAt = now;
712
+ if (reason) m.metadata.failReason = String(reason);
713
+ return { ...m, metadata: { ...m.metadata } };
714
+ }
715
+ export function cancelMatrixMessageV2(id, reason) {
716
+ const m = _mxMsgs.get(id);
717
+ if (!m) throw new Error(`matrix message ${id} not found`);
718
+ _mxCheckM(m.status, MX_MESSAGE_LIFECYCLE_V2.CANCELLED);
719
+ const now = Date.now();
720
+ m.status = MX_MESSAGE_LIFECYCLE_V2.CANCELLED;
721
+ m.updatedAt = now;
722
+ if (!m.settledAt) m.settledAt = now;
723
+ if (reason) m.metadata.cancelReason = String(reason);
724
+ return { ...m, metadata: { ...m.metadata } };
725
+ }
726
+ export function getMatrixMessageV2(id) {
727
+ const m = _mxMsgs.get(id);
728
+ if (!m) return null;
729
+ return { ...m, metadata: { ...m.metadata } };
730
+ }
731
+ export function listMatrixMessagesV2() {
732
+ return [..._mxMsgs.values()].map((m) => ({
733
+ ...m,
734
+ metadata: { ...m.metadata },
735
+ }));
736
+ }
545
737
 
546
- export function autoMuteIdleMatrixRoomsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const r of _mxRooms.values()) if (r.status === MX_ROOM_MATURITY_V2.ACTIVE && (t - r.lastTouchedAt) >= _mxRoomIdleMs) { r.status = MX_ROOM_MATURITY_V2.MUTED; r.updatedAt = t; flipped.push(r.id); } return { flipped, count: flipped.length }; }
547
- export function autoFailStuckMatrixMessagesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const m of _mxMsgs.values()) if (m.status === MX_MESSAGE_LIFECYCLE_V2.SENDING && m.startedAt != null && (t - m.startedAt) >= _mxMsgStuckMs) { m.status = MX_MESSAGE_LIFECYCLE_V2.FAILED; m.updatedAt = t; if (!m.settledAt) m.settledAt = t; m.metadata.failReason = "auto-fail-stuck"; flipped.push(m.id); } return { flipped, count: flipped.length }; }
738
+ export function autoMuteIdleMatrixRoomsV2({ now } = {}) {
739
+ const t = now ?? Date.now();
740
+ const flipped = [];
741
+ for (const r of _mxRooms.values())
742
+ if (
743
+ r.status === MX_ROOM_MATURITY_V2.ACTIVE &&
744
+ t - r.lastTouchedAt >= _mxRoomIdleMs
745
+ ) {
746
+ r.status = MX_ROOM_MATURITY_V2.MUTED;
747
+ r.updatedAt = t;
748
+ flipped.push(r.id);
749
+ }
750
+ return { flipped, count: flipped.length };
751
+ }
752
+ export function autoFailStuckMatrixMessagesV2({ now } = {}) {
753
+ const t = now ?? Date.now();
754
+ const flipped = [];
755
+ for (const m of _mxMsgs.values())
756
+ if (
757
+ m.status === MX_MESSAGE_LIFECYCLE_V2.SENDING &&
758
+ m.startedAt != null &&
759
+ t - m.startedAt >= _mxMsgStuckMs
760
+ ) {
761
+ m.status = MX_MESSAGE_LIFECYCLE_V2.FAILED;
762
+ m.updatedAt = t;
763
+ if (!m.settledAt) m.settledAt = t;
764
+ m.metadata.failReason = "auto-fail-stuck";
765
+ flipped.push(m.id);
766
+ }
767
+ return { flipped, count: flipped.length };
768
+ }
548
769
 
549
770
  export function getMatrixBridgeStatsV2() {
550
- const roomsByStatus = {}; for (const s of Object.values(MX_ROOM_MATURITY_V2)) roomsByStatus[s] = 0; for (const r of _mxRooms.values()) roomsByStatus[r.status]++;
551
- const msgsByStatus = {}; for (const s of Object.values(MX_MESSAGE_LIFECYCLE_V2)) msgsByStatus[s] = 0; for (const m of _mxMsgs.values()) msgsByStatus[m.status]++;
552
- return { totalRoomsV2: _mxRooms.size, totalMessagesV2: _mxMsgs.size, maxActiveMatrixRoomsPerOwner: _mxMaxActivePerOwner, maxPendingMatrixMessagesPerRoom: _mxMaxPendingPerRoom, matrixRoomIdleMs: _mxRoomIdleMs, matrixMessageStuckMs: _mxMsgStuckMs, roomsByStatus, msgsByStatus };
771
+ const roomsByStatus = {};
772
+ for (const s of Object.values(MX_ROOM_MATURITY_V2)) roomsByStatus[s] = 0;
773
+ for (const r of _mxRooms.values()) roomsByStatus[r.status]++;
774
+ const msgsByStatus = {};
775
+ for (const s of Object.values(MX_MESSAGE_LIFECYCLE_V2)) msgsByStatus[s] = 0;
776
+ for (const m of _mxMsgs.values()) msgsByStatus[m.status]++;
777
+ return {
778
+ totalRoomsV2: _mxRooms.size,
779
+ totalMessagesV2: _mxMsgs.size,
780
+ maxActiveMatrixRoomsPerOwner: _mxMaxActivePerOwner,
781
+ maxPendingMatrixMessagesPerRoom: _mxMaxPendingPerRoom,
782
+ matrixRoomIdleMs: _mxRoomIdleMs,
783
+ matrixMessageStuckMs: _mxMsgStuckMs,
784
+ roomsByStatus,
785
+ msgsByStatus,
786
+ };
787
+ }
788
+
789
+ // =====================================================================
790
+ // matrix-bridge V2 governance overlay (iter21)
791
+ // =====================================================================
792
+ export const MATGOV_PROFILE_MATURITY_V2 = Object.freeze({
793
+ PENDING: "pending",
794
+ ACTIVE: "active",
795
+ SUSPENDED: "suspended",
796
+ ARCHIVED: "archived",
797
+ });
798
+ export const MATGOV_SEND_LIFECYCLE_V2 = Object.freeze({
799
+ QUEUED: "queued",
800
+ SENDING: "sending",
801
+ SENT: "sent",
802
+ FAILED: "failed",
803
+ CANCELLED: "cancelled",
804
+ });
805
+ const _matgovPTrans = new Map([
806
+ [
807
+ MATGOV_PROFILE_MATURITY_V2.PENDING,
808
+ new Set([
809
+ MATGOV_PROFILE_MATURITY_V2.ACTIVE,
810
+ MATGOV_PROFILE_MATURITY_V2.ARCHIVED,
811
+ ]),
812
+ ],
813
+ [
814
+ MATGOV_PROFILE_MATURITY_V2.ACTIVE,
815
+ new Set([
816
+ MATGOV_PROFILE_MATURITY_V2.SUSPENDED,
817
+ MATGOV_PROFILE_MATURITY_V2.ARCHIVED,
818
+ ]),
819
+ ],
820
+ [
821
+ MATGOV_PROFILE_MATURITY_V2.SUSPENDED,
822
+ new Set([
823
+ MATGOV_PROFILE_MATURITY_V2.ACTIVE,
824
+ MATGOV_PROFILE_MATURITY_V2.ARCHIVED,
825
+ ]),
826
+ ],
827
+ [MATGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
828
+ ]);
829
+ const _matgovPTerminal = new Set([MATGOV_PROFILE_MATURITY_V2.ARCHIVED]);
830
+ const _matgovJTrans = new Map([
831
+ [
832
+ MATGOV_SEND_LIFECYCLE_V2.QUEUED,
833
+ new Set([
834
+ MATGOV_SEND_LIFECYCLE_V2.SENDING,
835
+ MATGOV_SEND_LIFECYCLE_V2.CANCELLED,
836
+ ]),
837
+ ],
838
+ [
839
+ MATGOV_SEND_LIFECYCLE_V2.SENDING,
840
+ new Set([
841
+ MATGOV_SEND_LIFECYCLE_V2.SENT,
842
+ MATGOV_SEND_LIFECYCLE_V2.FAILED,
843
+ MATGOV_SEND_LIFECYCLE_V2.CANCELLED,
844
+ ]),
845
+ ],
846
+ [MATGOV_SEND_LIFECYCLE_V2.SENT, new Set()],
847
+ [MATGOV_SEND_LIFECYCLE_V2.FAILED, new Set()],
848
+ [MATGOV_SEND_LIFECYCLE_V2.CANCELLED, new Set()],
849
+ ]);
850
+ const _matgovPsV2 = new Map();
851
+ const _matgovJsV2 = new Map();
852
+ let _matgovMaxActive = 6,
853
+ _matgovMaxPending = 20,
854
+ _matgovIdleMs = 30 * 24 * 60 * 60 * 1000,
855
+ _matgovStuckMs = 60 * 1000;
856
+ function _matgovPos(n, label) {
857
+ const v = Math.floor(Number(n));
858
+ if (!Number.isFinite(v) || v <= 0)
859
+ throw new Error(`${label} must be positive integer`);
860
+ return v;
861
+ }
862
+ function _matgovCheckP(from, to) {
863
+ const a = _matgovPTrans.get(from);
864
+ if (!a || !a.has(to))
865
+ throw new Error(`invalid matgov profile transition ${from} → ${to}`);
866
+ }
867
+ function _matgovCheckJ(from, to) {
868
+ const a = _matgovJTrans.get(from);
869
+ if (!a || !a.has(to))
870
+ throw new Error(`invalid matgov send transition ${from} → ${to}`);
871
+ }
872
+ function _matgovCountActive(owner) {
873
+ let c = 0;
874
+ for (const p of _matgovPsV2.values())
875
+ if (p.owner === owner && p.status === MATGOV_PROFILE_MATURITY_V2.ACTIVE)
876
+ c++;
877
+ return c;
878
+ }
879
+ function _matgovCountPending(profileId) {
880
+ let c = 0;
881
+ for (const j of _matgovJsV2.values())
882
+ if (
883
+ j.profileId === profileId &&
884
+ (j.status === MATGOV_SEND_LIFECYCLE_V2.QUEUED ||
885
+ j.status === MATGOV_SEND_LIFECYCLE_V2.SENDING)
886
+ )
887
+ c++;
888
+ return c;
889
+ }
890
+ export function setMaxActiveMatgovProfilesPerOwnerV2(n) {
891
+ _matgovMaxActive = _matgovPos(n, "maxActiveMatgovProfilesPerOwner");
892
+ }
893
+ export function getMaxActiveMatgovProfilesPerOwnerV2() {
894
+ return _matgovMaxActive;
895
+ }
896
+ export function setMaxPendingMatgovSendsPerProfileV2(n) {
897
+ _matgovMaxPending = _matgovPos(n, "maxPendingMatgovSendsPerProfile");
898
+ }
899
+ export function getMaxPendingMatgovSendsPerProfileV2() {
900
+ return _matgovMaxPending;
901
+ }
902
+ export function setMatgovProfileIdleMsV2(n) {
903
+ _matgovIdleMs = _matgovPos(n, "matgovProfileIdleMs");
904
+ }
905
+ export function getMatgovProfileIdleMsV2() {
906
+ return _matgovIdleMs;
907
+ }
908
+ export function setMatgovSendStuckMsV2(n) {
909
+ _matgovStuckMs = _matgovPos(n, "matgovSendStuckMs");
910
+ }
911
+ export function getMatgovSendStuckMsV2() {
912
+ return _matgovStuckMs;
913
+ }
914
+ export function _resetStateMatrixBridgeGovV2() {
915
+ _matgovPsV2.clear();
916
+ _matgovJsV2.clear();
917
+ _matgovMaxActive = 6;
918
+ _matgovMaxPending = 20;
919
+ _matgovIdleMs = 30 * 24 * 60 * 60 * 1000;
920
+ _matgovStuckMs = 60 * 1000;
921
+ }
922
+ export function registerMatgovProfileV2({
923
+ id,
924
+ owner,
925
+ homeserver,
926
+ metadata,
927
+ } = {}) {
928
+ if (!id || !owner) throw new Error("id and owner required");
929
+ if (_matgovPsV2.has(id))
930
+ throw new Error(`matgov profile ${id} already exists`);
931
+ const now = Date.now();
932
+ const p = {
933
+ id,
934
+ owner,
935
+ homeserver: homeserver || "matrix.org",
936
+ status: MATGOV_PROFILE_MATURITY_V2.PENDING,
937
+ createdAt: now,
938
+ updatedAt: now,
939
+ lastTouchedAt: now,
940
+ activatedAt: null,
941
+ archivedAt: null,
942
+ metadata: { ...(metadata || {}) },
943
+ };
944
+ _matgovPsV2.set(id, p);
945
+ return { ...p, metadata: { ...p.metadata } };
946
+ }
947
+ export function activateMatgovProfileV2(id) {
948
+ const p = _matgovPsV2.get(id);
949
+ if (!p) throw new Error(`matgov profile ${id} not found`);
950
+ const isInitial = p.status === MATGOV_PROFILE_MATURITY_V2.PENDING;
951
+ _matgovCheckP(p.status, MATGOV_PROFILE_MATURITY_V2.ACTIVE);
952
+ if (isInitial && _matgovCountActive(p.owner) >= _matgovMaxActive)
953
+ throw new Error(`max active matgov profiles for owner ${p.owner} reached`);
954
+ const now = Date.now();
955
+ p.status = MATGOV_PROFILE_MATURITY_V2.ACTIVE;
956
+ p.updatedAt = now;
957
+ p.lastTouchedAt = now;
958
+ if (!p.activatedAt) p.activatedAt = now;
959
+ return { ...p, metadata: { ...p.metadata } };
960
+ }
961
+ export function suspendMatgovProfileV2(id) {
962
+ const p = _matgovPsV2.get(id);
963
+ if (!p) throw new Error(`matgov profile ${id} not found`);
964
+ _matgovCheckP(p.status, MATGOV_PROFILE_MATURITY_V2.SUSPENDED);
965
+ p.status = MATGOV_PROFILE_MATURITY_V2.SUSPENDED;
966
+ p.updatedAt = Date.now();
967
+ return { ...p, metadata: { ...p.metadata } };
968
+ }
969
+ export function archiveMatgovProfileV2(id) {
970
+ const p = _matgovPsV2.get(id);
971
+ if (!p) throw new Error(`matgov profile ${id} not found`);
972
+ _matgovCheckP(p.status, MATGOV_PROFILE_MATURITY_V2.ARCHIVED);
973
+ const now = Date.now();
974
+ p.status = MATGOV_PROFILE_MATURITY_V2.ARCHIVED;
975
+ p.updatedAt = now;
976
+ if (!p.archivedAt) p.archivedAt = now;
977
+ return { ...p, metadata: { ...p.metadata } };
978
+ }
979
+ export function touchMatgovProfileV2(id) {
980
+ const p = _matgovPsV2.get(id);
981
+ if (!p) throw new Error(`matgov profile ${id} not found`);
982
+ if (_matgovPTerminal.has(p.status))
983
+ throw new Error(`cannot touch terminal matgov profile ${id}`);
984
+ const now = Date.now();
985
+ p.lastTouchedAt = now;
986
+ p.updatedAt = now;
987
+ return { ...p, metadata: { ...p.metadata } };
988
+ }
989
+ export function getMatgovProfileV2(id) {
990
+ const p = _matgovPsV2.get(id);
991
+ if (!p) return null;
992
+ return { ...p, metadata: { ...p.metadata } };
993
+ }
994
+ export function listMatgovProfilesV2() {
995
+ return [..._matgovPsV2.values()].map((p) => ({
996
+ ...p,
997
+ metadata: { ...p.metadata },
998
+ }));
999
+ }
1000
+ export function createMatgovSendV2({ id, profileId, room, metadata } = {}) {
1001
+ if (!id || !profileId) throw new Error("id and profileId required");
1002
+ if (_matgovJsV2.has(id)) throw new Error(`matgov send ${id} already exists`);
1003
+ if (!_matgovPsV2.has(profileId))
1004
+ throw new Error(`matgov profile ${profileId} not found`);
1005
+ if (_matgovCountPending(profileId) >= _matgovMaxPending)
1006
+ throw new Error(
1007
+ `max pending matgov sends for profile ${profileId} reached`,
1008
+ );
1009
+ const now = Date.now();
1010
+ const j = {
1011
+ id,
1012
+ profileId,
1013
+ room: room || "",
1014
+ status: MATGOV_SEND_LIFECYCLE_V2.QUEUED,
1015
+ createdAt: now,
1016
+ updatedAt: now,
1017
+ startedAt: null,
1018
+ settledAt: null,
1019
+ metadata: { ...(metadata || {}) },
1020
+ };
1021
+ _matgovJsV2.set(id, j);
1022
+ return { ...j, metadata: { ...j.metadata } };
1023
+ }
1024
+ export function sendingMatgovSendV2(id) {
1025
+ const j = _matgovJsV2.get(id);
1026
+ if (!j) throw new Error(`matgov send ${id} not found`);
1027
+ _matgovCheckJ(j.status, MATGOV_SEND_LIFECYCLE_V2.SENDING);
1028
+ const now = Date.now();
1029
+ j.status = MATGOV_SEND_LIFECYCLE_V2.SENDING;
1030
+ j.updatedAt = now;
1031
+ if (!j.startedAt) j.startedAt = now;
1032
+ return { ...j, metadata: { ...j.metadata } };
1033
+ }
1034
+ export function completeSendMatgovV2(id) {
1035
+ const j = _matgovJsV2.get(id);
1036
+ if (!j) throw new Error(`matgov send ${id} not found`);
1037
+ _matgovCheckJ(j.status, MATGOV_SEND_LIFECYCLE_V2.SENT);
1038
+ const now = Date.now();
1039
+ j.status = MATGOV_SEND_LIFECYCLE_V2.SENT;
1040
+ j.updatedAt = now;
1041
+ if (!j.settledAt) j.settledAt = now;
1042
+ return { ...j, metadata: { ...j.metadata } };
1043
+ }
1044
+ export function failMatgovSendV2(id, reason) {
1045
+ const j = _matgovJsV2.get(id);
1046
+ if (!j) throw new Error(`matgov send ${id} not found`);
1047
+ _matgovCheckJ(j.status, MATGOV_SEND_LIFECYCLE_V2.FAILED);
1048
+ const now = Date.now();
1049
+ j.status = MATGOV_SEND_LIFECYCLE_V2.FAILED;
1050
+ j.updatedAt = now;
1051
+ if (!j.settledAt) j.settledAt = now;
1052
+ if (reason) j.metadata.failReason = String(reason);
1053
+ return { ...j, metadata: { ...j.metadata } };
1054
+ }
1055
+ export function cancelMatgovSendV2(id, reason) {
1056
+ const j = _matgovJsV2.get(id);
1057
+ if (!j) throw new Error(`matgov send ${id} not found`);
1058
+ _matgovCheckJ(j.status, MATGOV_SEND_LIFECYCLE_V2.CANCELLED);
1059
+ const now = Date.now();
1060
+ j.status = MATGOV_SEND_LIFECYCLE_V2.CANCELLED;
1061
+ j.updatedAt = now;
1062
+ if (!j.settledAt) j.settledAt = now;
1063
+ if (reason) j.metadata.cancelReason = String(reason);
1064
+ return { ...j, metadata: { ...j.metadata } };
1065
+ }
1066
+ export function getMatgovSendV2(id) {
1067
+ const j = _matgovJsV2.get(id);
1068
+ if (!j) return null;
1069
+ return { ...j, metadata: { ...j.metadata } };
1070
+ }
1071
+ export function listMatgovSendsV2() {
1072
+ return [..._matgovJsV2.values()].map((j) => ({
1073
+ ...j,
1074
+ metadata: { ...j.metadata },
1075
+ }));
1076
+ }
1077
+ export function autoSuspendIdleMatgovProfilesV2({ now } = {}) {
1078
+ const t = now ?? Date.now();
1079
+ const flipped = [];
1080
+ for (const p of _matgovPsV2.values())
1081
+ if (
1082
+ p.status === MATGOV_PROFILE_MATURITY_V2.ACTIVE &&
1083
+ t - p.lastTouchedAt >= _matgovIdleMs
1084
+ ) {
1085
+ p.status = MATGOV_PROFILE_MATURITY_V2.SUSPENDED;
1086
+ p.updatedAt = t;
1087
+ flipped.push(p.id);
1088
+ }
1089
+ return { flipped, count: flipped.length };
1090
+ }
1091
+ export function autoFailStuckMatgovSendsV2({ now } = {}) {
1092
+ const t = now ?? Date.now();
1093
+ const flipped = [];
1094
+ for (const j of _matgovJsV2.values())
1095
+ if (
1096
+ j.status === MATGOV_SEND_LIFECYCLE_V2.SENDING &&
1097
+ j.startedAt != null &&
1098
+ t - j.startedAt >= _matgovStuckMs
1099
+ ) {
1100
+ j.status = MATGOV_SEND_LIFECYCLE_V2.FAILED;
1101
+ j.updatedAt = t;
1102
+ if (!j.settledAt) j.settledAt = t;
1103
+ j.metadata.failReason = "auto-fail-stuck";
1104
+ flipped.push(j.id);
1105
+ }
1106
+ return { flipped, count: flipped.length };
1107
+ }
1108
+ export function getMatrixBridgeGovStatsV2() {
1109
+ const profilesByStatus = {};
1110
+ for (const v of Object.values(MATGOV_PROFILE_MATURITY_V2))
1111
+ profilesByStatus[v] = 0;
1112
+ for (const p of _matgovPsV2.values()) profilesByStatus[p.status]++;
1113
+ const sendsByStatus = {};
1114
+ for (const v of Object.values(MATGOV_SEND_LIFECYCLE_V2)) sendsByStatus[v] = 0;
1115
+ for (const j of _matgovJsV2.values()) sendsByStatus[j.status]++;
1116
+ return {
1117
+ totalMatgovProfilesV2: _matgovPsV2.size,
1118
+ totalMatgovSendsV2: _matgovJsV2.size,
1119
+ maxActiveMatgovProfilesPerOwner: _matgovMaxActive,
1120
+ maxPendingMatgovSendsPerProfile: _matgovMaxPending,
1121
+ matgovProfileIdleMs: _matgovIdleMs,
1122
+ matgovSendStuckMs: _matgovStuckMs,
1123
+ profilesByStatus,
1124
+ sendsByStatus,
1125
+ };
553
1126
  }