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
@@ -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,236 @@ 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
+ };
553
787
  }