chainlesschain 0.132.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 (135) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +230 -0
  3. package/src/commands/activitypub.js +191 -0
  4. package/src/commands/agent.js +601 -0
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +186 -0
  7. package/src/commands/bm25.js +162 -0
  8. package/src/commands/browse.js +225 -0
  9. package/src/commands/ccron.js +178 -0
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +420 -0
  12. package/src/commands/compt.js +176 -0
  13. package/src/commands/consol.js +237 -0
  14. package/src/commands/cowork.js +588 -0
  15. package/src/commands/crosschain.js +216 -0
  16. package/src/commands/dao.js +216 -0
  17. package/src/commands/dlp.js +206 -0
  18. package/src/commands/economy.js +211 -0
  19. package/src/commands/evolution.js +209 -0
  20. package/src/commands/evomap.js +216 -0
  21. package/src/commands/fflag.js +230 -0
  22. package/src/commands/git.js +185 -0
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +210 -0
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +178 -0
  27. package/src/commands/itbudget.js +161 -0
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +201 -0
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +214 -0
  32. package/src/commands/mcpscaf.js +153 -0
  33. package/src/commands/meminj.js +153 -0
  34. package/src/commands/nostr.js +213 -0
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +156 -0
  37. package/src/commands/pdfp.js +160 -0
  38. package/src/commands/perf.js +176 -0
  39. package/src/commands/perm.js +156 -0
  40. package/src/commands/pipeline.js +211 -0
  41. package/src/commands/planmode.js +154 -0
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +166 -0
  44. package/src/commands/recommend.js +185 -0
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +153 -0
  48. package/src/commands/seshsearch.js +149 -0
  49. package/src/commands/seshtail.js +152 -0
  50. package/src/commands/seshu.js +160 -0
  51. package/src/commands/sganal.js +172 -0
  52. package/src/commands/siem.js +207 -0
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +154 -0
  55. package/src/commands/social.js +159 -0
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +157 -0
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +183 -0
  60. package/src/commands/topiccls.js +158 -0
  61. package/src/commands/uprof.js +154 -0
  62. package/src/commands/vcheck.js +172 -0
  63. package/src/commands/webfetch.js +150 -0
  64. package/src/commands/zkp.js +218 -0
  65. package/src/harness/prompt-compressor.js +331 -0
  66. package/src/index.js +101 -1
  67. package/src/lib/a2a-protocol.js +373 -0
  68. package/src/lib/activitypub-bridge.js +343 -0
  69. package/src/lib/agent-economy.js +358 -0
  70. package/src/lib/app-builder.js +338 -0
  71. package/src/lib/audit-logger.js +321 -0
  72. package/src/lib/autonomous-agent.js +341 -0
  73. package/src/lib/bi-engine.js +339 -0
  74. package/src/lib/bm25-search.js +333 -0
  75. package/src/lib/browser-automation.js +352 -0
  76. package/src/lib/chat-core.js +336 -0
  77. package/src/lib/claude-code-bridge.js +341 -0
  78. package/src/lib/compliance-framework-reporter.js +359 -0
  79. package/src/lib/compliance-manager.js +330 -0
  80. package/src/lib/compression-telemetry.js +333 -0
  81. package/src/lib/content-recommender.js +370 -0
  82. package/src/lib/cowork-cron.js +330 -0
  83. package/src/lib/cowork-learning.js +333 -0
  84. package/src/lib/cowork-task-runner.js +362 -0
  85. package/src/lib/cowork-workflow.js +327 -0
  86. package/src/lib/cross-chain.js +365 -0
  87. package/src/lib/dao-governance.js +339 -0
  88. package/src/lib/dlp-engine.js +343 -0
  89. package/src/lib/evolution-system.js +336 -0
  90. package/src/lib/evomap-manager.js +339 -0
  91. package/src/lib/execution-backend.js +351 -0
  92. package/src/lib/feature-flags.js +330 -0
  93. package/src/lib/git-integration.js +343 -0
  94. package/src/lib/hardening-manager.js +341 -0
  95. package/src/lib/hierarchical-memory.js +341 -0
  96. package/src/lib/inference-network.js +362 -0
  97. package/src/lib/iteration-budget.js +357 -0
  98. package/src/lib/knowledge-graph.js +333 -0
  99. package/src/lib/matrix-bridge.js +339 -0
  100. package/src/lib/mcp-scaffold.js +345 -0
  101. package/src/lib/memory-injection.js +320 -0
  102. package/src/lib/nostr-bridge.js +342 -0
  103. package/src/lib/orchestrator.js +350 -0
  104. package/src/lib/pdf-parser.js +330 -0
  105. package/src/lib/perf-tuning.js +364 -0
  106. package/src/lib/permission-engine.js +319 -0
  107. package/src/lib/pipeline-orchestrator.js +345 -0
  108. package/src/lib/plan-mode.js +328 -0
  109. package/src/lib/privacy-computing.js +335 -0
  110. package/src/lib/prompt-compressor.js +1 -10
  111. package/src/lib/reputation-optimizer.js +340 -0
  112. package/src/lib/sandbox-v2.js +327 -0
  113. package/src/lib/service-container.js +342 -0
  114. package/src/lib/session-consolidator.js +352 -0
  115. package/src/lib/session-hooks.js +340 -0
  116. package/src/lib/session-search.js +334 -0
  117. package/src/lib/session-tail.js +320 -0
  118. package/src/lib/session-usage.js +329 -0
  119. package/src/lib/siem-exporter.js +352 -0
  120. package/src/lib/skill-marketplace.js +345 -0
  121. package/src/lib/sla-manager.js +341 -0
  122. package/src/lib/slot-filler.js +333 -0
  123. package/src/lib/social-graph-analytics.js +327 -0
  124. package/src/lib/social-graph.js +304 -0
  125. package/src/lib/stress-tester.js +342 -0
  126. package/src/lib/sub-agent-registry.js +359 -0
  127. package/src/lib/task-model-selector.js +333 -0
  128. package/src/lib/terraform-manager.js +333 -0
  129. package/src/lib/todo-manager.js +339 -0
  130. package/src/lib/token-incentive.js +341 -0
  131. package/src/lib/topic-classifier.js +353 -0
  132. package/src/lib/user-profile.js +325 -0
  133. package/src/lib/version-checker.js +335 -0
  134. package/src/lib/web-fetch.js +322 -0
  135. package/src/lib/zkp-engine.js +342 -0
@@ -446,3 +446,342 @@ export function _resetState() {
446
446
  e2eeEnabled: true,
447
447
  };
448
448
  }
449
+
450
+ // ===== V2 Surface: Matrix Bridge governance overlay (CLI v0.134.0) =====
451
+ export const MX_ROOM_MATURITY_V2 = Object.freeze({
452
+ PENDING: "pending",
453
+ ACTIVE: "active",
454
+ MUTED: "muted",
455
+ ARCHIVED: "archived",
456
+ });
457
+ export const MX_MESSAGE_LIFECYCLE_V2 = Object.freeze({
458
+ QUEUED: "queued",
459
+ SENDING: "sending",
460
+ DELIVERED: "delivered",
461
+ FAILED: "failed",
462
+ CANCELLED: "cancelled",
463
+ });
464
+
465
+ const _mxRoomTrans = new Map([
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
+ ],
478
+ [MX_ROOM_MATURITY_V2.ARCHIVED, new Set()],
479
+ ]);
480
+ const _mxRoomTerminal = new Set([MX_ROOM_MATURITY_V2.ARCHIVED]);
481
+ const _mxMsgTrans = new Map([
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
+ ],
497
+ [MX_MESSAGE_LIFECYCLE_V2.DELIVERED, new Set()],
498
+ [MX_MESSAGE_LIFECYCLE_V2.FAILED, new Set()],
499
+ [MX_MESSAGE_LIFECYCLE_V2.CANCELLED, new Set()],
500
+ ]);
501
+
502
+ const _mxRooms = new Map();
503
+ const _mxMsgs = new Map();
504
+ let _mxMaxActivePerOwner = 20;
505
+ let _mxMaxPendingPerRoom = 40;
506
+ let _mxRoomIdleMs = 24 * 60 * 60 * 1000;
507
+ let _mxMsgStuckMs = 3 * 60 * 1000;
508
+
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
+ }
515
+
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
+ }
540
+
541
+ export function _resetStateMatrixBridgeV2() {
542
+ _mxRooms.clear();
543
+ _mxMsgs.clear();
544
+ _mxMaxActivePerOwner = 20;
545
+ _mxMaxPendingPerRoom = 40;
546
+ _mxRoomIdleMs = 24 * 60 * 60 * 1000;
547
+ _mxMsgStuckMs = 3 * 60 * 1000;
548
+ }
549
+
550
+ export function registerMatrixRoomV2({ id, owner, alias, metadata } = {}) {
551
+ if (!id || typeof id !== "string") throw new Error("id is required");
552
+ if (!owner || typeof owner !== "string") throw new Error("owner is required");
553
+ if (_mxRooms.has(id)) throw new Error(`matrix room ${id} already registered`);
554
+ const now = Date.now();
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
+ };
567
+ _mxRooms.set(id, r);
568
+ return { ...r, metadata: { ...r.metadata } };
569
+ }
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
+ }
581
+
582
+ export function activateMatrixRoomV2(id) {
583
+ const r = _mxRooms.get(id);
584
+ if (!r) throw new Error(`matrix room ${id} not found`);
585
+ _mxCheckR(r.status, MX_ROOM_MATURITY_V2.ACTIVE);
586
+ const recovery = r.status === MX_ROOM_MATURITY_V2.MUTED;
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();
607
+ return { ...r, metadata: { ...r.metadata } };
608
+ }
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
+ }
640
+
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
+ }
652
+
653
+ export function createMatrixMessageV2({ id, roomId, body, metadata } = {}) {
654
+ if (!id || typeof id !== "string") throw new Error("id is required");
655
+ if (!roomId || typeof roomId !== "string")
656
+ throw new Error("roomId is required");
657
+ if (_mxMsgs.has(id)) throw new Error(`matrix message ${id} already exists`);
658
+ if (!_mxRooms.has(roomId)) throw new Error(`matrix room ${roomId} not found`);
659
+ const pending = _mxCountPending(roomId);
660
+ if (pending >= _mxMaxPendingPerRoom)
661
+ throw new Error(
662
+ `max pending matrix messages per room (${_mxMaxPendingPerRoom}) reached for ${roomId}`,
663
+ );
664
+ const now = Date.now();
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
+ };
676
+ _mxMsgs.set(id, m);
677
+ return { ...m, metadata: { ...m.metadata } };
678
+ }
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
+ }
737
+
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
+ }
769
+
770
+ export function getMatrixBridgeStatsV2() {
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
+ }
@@ -383,3 +383,348 @@ function renderGitignore() {
383
383
  !.env.example
384
384
  `;
385
385
  }
386
+
387
+ // ===== V2 Surface: MCP Scaffold governance overlay (CLI v0.142.0) =====
388
+ export const MSCAF_PROFILE_MATURITY_V2 = Object.freeze({
389
+ PENDING: "pending",
390
+ ACTIVE: "active",
391
+ STALE: "stale",
392
+ ARCHIVED: "archived",
393
+ });
394
+ export const MSCAF_GENERATION_LIFECYCLE_V2 = Object.freeze({
395
+ QUEUED: "queued",
396
+ GENERATING: "generating",
397
+ GENERATED: "generated",
398
+ FAILED: "failed",
399
+ CANCELLED: "cancelled",
400
+ });
401
+ const _mscafPTrans = new Map([
402
+ [
403
+ MSCAF_PROFILE_MATURITY_V2.PENDING,
404
+ new Set([
405
+ MSCAF_PROFILE_MATURITY_V2.ACTIVE,
406
+ MSCAF_PROFILE_MATURITY_V2.ARCHIVED,
407
+ ]),
408
+ ],
409
+ [
410
+ MSCAF_PROFILE_MATURITY_V2.ACTIVE,
411
+ new Set([
412
+ MSCAF_PROFILE_MATURITY_V2.STALE,
413
+ MSCAF_PROFILE_MATURITY_V2.ARCHIVED,
414
+ ]),
415
+ ],
416
+ [
417
+ MSCAF_PROFILE_MATURITY_V2.STALE,
418
+ new Set([
419
+ MSCAF_PROFILE_MATURITY_V2.ACTIVE,
420
+ MSCAF_PROFILE_MATURITY_V2.ARCHIVED,
421
+ ]),
422
+ ],
423
+ [MSCAF_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
424
+ ]);
425
+ const _mscafPTerminal = new Set([MSCAF_PROFILE_MATURITY_V2.ARCHIVED]);
426
+ const _mscafGTrans = new Map([
427
+ [
428
+ MSCAF_GENERATION_LIFECYCLE_V2.QUEUED,
429
+ new Set([
430
+ MSCAF_GENERATION_LIFECYCLE_V2.GENERATING,
431
+ MSCAF_GENERATION_LIFECYCLE_V2.CANCELLED,
432
+ ]),
433
+ ],
434
+ [
435
+ MSCAF_GENERATION_LIFECYCLE_V2.GENERATING,
436
+ new Set([
437
+ MSCAF_GENERATION_LIFECYCLE_V2.GENERATED,
438
+ MSCAF_GENERATION_LIFECYCLE_V2.FAILED,
439
+ MSCAF_GENERATION_LIFECYCLE_V2.CANCELLED,
440
+ ]),
441
+ ],
442
+ [MSCAF_GENERATION_LIFECYCLE_V2.GENERATED, new Set()],
443
+ [MSCAF_GENERATION_LIFECYCLE_V2.FAILED, new Set()],
444
+ [MSCAF_GENERATION_LIFECYCLE_V2.CANCELLED, new Set()],
445
+ ]);
446
+ const _mscafPsV2 = new Map();
447
+ const _mscafGsV2 = new Map();
448
+ let _mscafMaxActive = 6,
449
+ _mscafMaxPending = 15,
450
+ _mscafIdleMs = 30 * 24 * 60 * 60 * 1000,
451
+ _mscafStuckMs = 60 * 1000;
452
+ function _mscafPos(n, label) {
453
+ const v = Math.floor(Number(n));
454
+ if (!Number.isFinite(v) || v <= 0)
455
+ throw new Error(`${label} must be positive integer`);
456
+ return v;
457
+ }
458
+ function _mscafCheckP(from, to) {
459
+ const a = _mscafPTrans.get(from);
460
+ if (!a || !a.has(to))
461
+ throw new Error(`invalid mscaf profile transition ${from} → ${to}`);
462
+ }
463
+ function _mscafCheckG(from, to) {
464
+ const a = _mscafGTrans.get(from);
465
+ if (!a || !a.has(to))
466
+ throw new Error(`invalid mscaf generation transition ${from} → ${to}`);
467
+ }
468
+ export function setMaxActiveMscafProfilesPerOwnerV2(n) {
469
+ _mscafMaxActive = _mscafPos(n, "maxActiveMscafProfilesPerOwner");
470
+ }
471
+ export function getMaxActiveMscafProfilesPerOwnerV2() {
472
+ return _mscafMaxActive;
473
+ }
474
+ export function setMaxPendingMscafGenerationsPerProfileV2(n) {
475
+ _mscafMaxPending = _mscafPos(n, "maxPendingMscafGenerationsPerProfile");
476
+ }
477
+ export function getMaxPendingMscafGenerationsPerProfileV2() {
478
+ return _mscafMaxPending;
479
+ }
480
+ export function setMscafProfileIdleMsV2(n) {
481
+ _mscafIdleMs = _mscafPos(n, "mscafProfileIdleMs");
482
+ }
483
+ export function getMscafProfileIdleMsV2() {
484
+ return _mscafIdleMs;
485
+ }
486
+ export function setMscafGenerationStuckMsV2(n) {
487
+ _mscafStuckMs = _mscafPos(n, "mscafGenerationStuckMs");
488
+ }
489
+ export function getMscafGenerationStuckMsV2() {
490
+ return _mscafStuckMs;
491
+ }
492
+ export function _resetStateMcpScaffoldV2() {
493
+ _mscafPsV2.clear();
494
+ _mscafGsV2.clear();
495
+ _mscafMaxActive = 6;
496
+ _mscafMaxPending = 15;
497
+ _mscafIdleMs = 30 * 24 * 60 * 60 * 1000;
498
+ _mscafStuckMs = 60 * 1000;
499
+ }
500
+ export function registerMscafProfileV2({
501
+ id,
502
+ owner,
503
+ transport,
504
+ metadata,
505
+ } = {}) {
506
+ if (!id) throw new Error("mscaf profile id required");
507
+ if (!owner) throw new Error("mscaf profile owner required");
508
+ if (_mscafPsV2.has(id))
509
+ throw new Error(`mscaf profile ${id} already registered`);
510
+ const now = Date.now();
511
+ const p = {
512
+ id,
513
+ owner,
514
+ transport: transport || "stdio",
515
+ status: MSCAF_PROFILE_MATURITY_V2.PENDING,
516
+ createdAt: now,
517
+ updatedAt: now,
518
+ activatedAt: null,
519
+ archivedAt: null,
520
+ lastTouchedAt: now,
521
+ metadata: { ...(metadata || {}) },
522
+ };
523
+ _mscafPsV2.set(id, p);
524
+ return { ...p, metadata: { ...p.metadata } };
525
+ }
526
+ function _mscafCountActive(owner) {
527
+ let n = 0;
528
+ for (const p of _mscafPsV2.values())
529
+ if (p.owner === owner && p.status === MSCAF_PROFILE_MATURITY_V2.ACTIVE) n++;
530
+ return n;
531
+ }
532
+ export function activateMscafProfileV2(id) {
533
+ const p = _mscafPsV2.get(id);
534
+ if (!p) throw new Error(`mscaf profile ${id} not found`);
535
+ _mscafCheckP(p.status, MSCAF_PROFILE_MATURITY_V2.ACTIVE);
536
+ const recovery = p.status === MSCAF_PROFILE_MATURITY_V2.STALE;
537
+ if (!recovery && _mscafCountActive(p.owner) >= _mscafMaxActive)
538
+ throw new Error(`max active mscaf profiles for owner ${p.owner} reached`);
539
+ const now = Date.now();
540
+ p.status = MSCAF_PROFILE_MATURITY_V2.ACTIVE;
541
+ p.updatedAt = now;
542
+ p.lastTouchedAt = now;
543
+ if (!p.activatedAt) p.activatedAt = now;
544
+ return { ...p, metadata: { ...p.metadata } };
545
+ }
546
+ export function staleMscafProfileV2(id) {
547
+ const p = _mscafPsV2.get(id);
548
+ if (!p) throw new Error(`mscaf profile ${id} not found`);
549
+ _mscafCheckP(p.status, MSCAF_PROFILE_MATURITY_V2.STALE);
550
+ p.status = MSCAF_PROFILE_MATURITY_V2.STALE;
551
+ p.updatedAt = Date.now();
552
+ return { ...p, metadata: { ...p.metadata } };
553
+ }
554
+ export function archiveMscafProfileV2(id) {
555
+ const p = _mscafPsV2.get(id);
556
+ if (!p) throw new Error(`mscaf profile ${id} not found`);
557
+ _mscafCheckP(p.status, MSCAF_PROFILE_MATURITY_V2.ARCHIVED);
558
+ const now = Date.now();
559
+ p.status = MSCAF_PROFILE_MATURITY_V2.ARCHIVED;
560
+ p.updatedAt = now;
561
+ if (!p.archivedAt) p.archivedAt = now;
562
+ return { ...p, metadata: { ...p.metadata } };
563
+ }
564
+ export function touchMscafProfileV2(id) {
565
+ const p = _mscafPsV2.get(id);
566
+ if (!p) throw new Error(`mscaf profile ${id} not found`);
567
+ if (_mscafPTerminal.has(p.status))
568
+ throw new Error(`cannot touch terminal mscaf profile ${id}`);
569
+ const now = Date.now();
570
+ p.lastTouchedAt = now;
571
+ p.updatedAt = now;
572
+ return { ...p, metadata: { ...p.metadata } };
573
+ }
574
+ export function getMscafProfileV2(id) {
575
+ const p = _mscafPsV2.get(id);
576
+ if (!p) return null;
577
+ return { ...p, metadata: { ...p.metadata } };
578
+ }
579
+ export function listMscafProfilesV2() {
580
+ return [..._mscafPsV2.values()].map((p) => ({
581
+ ...p,
582
+ metadata: { ...p.metadata },
583
+ }));
584
+ }
585
+ function _mscafCountPending(profileId) {
586
+ let n = 0;
587
+ for (const g of _mscafGsV2.values())
588
+ if (
589
+ g.profileId === profileId &&
590
+ (g.status === MSCAF_GENERATION_LIFECYCLE_V2.QUEUED ||
591
+ g.status === MSCAF_GENERATION_LIFECYCLE_V2.GENERATING)
592
+ )
593
+ n++;
594
+ return n;
595
+ }
596
+ export function createMscafGenerationV2({
597
+ id,
598
+ profileId,
599
+ target,
600
+ metadata,
601
+ } = {}) {
602
+ if (!id) throw new Error("mscaf generation id required");
603
+ if (!profileId) throw new Error("mscaf generation profileId required");
604
+ if (_mscafGsV2.has(id))
605
+ throw new Error(`mscaf generation ${id} already exists`);
606
+ if (!_mscafPsV2.has(profileId))
607
+ throw new Error(`mscaf profile ${profileId} not found`);
608
+ if (_mscafCountPending(profileId) >= _mscafMaxPending)
609
+ throw new Error(
610
+ `max pending mscaf generations for profile ${profileId} reached`,
611
+ );
612
+ const now = Date.now();
613
+ const g = {
614
+ id,
615
+ profileId,
616
+ target: target || "",
617
+ status: MSCAF_GENERATION_LIFECYCLE_V2.QUEUED,
618
+ createdAt: now,
619
+ updatedAt: now,
620
+ startedAt: null,
621
+ settledAt: null,
622
+ metadata: { ...(metadata || {}) },
623
+ };
624
+ _mscafGsV2.set(id, g);
625
+ return { ...g, metadata: { ...g.metadata } };
626
+ }
627
+ export function generatingMscafGenerationV2(id) {
628
+ const g = _mscafGsV2.get(id);
629
+ if (!g) throw new Error(`mscaf generation ${id} not found`);
630
+ _mscafCheckG(g.status, MSCAF_GENERATION_LIFECYCLE_V2.GENERATING);
631
+ const now = Date.now();
632
+ g.status = MSCAF_GENERATION_LIFECYCLE_V2.GENERATING;
633
+ g.updatedAt = now;
634
+ if (!g.startedAt) g.startedAt = now;
635
+ return { ...g, metadata: { ...g.metadata } };
636
+ }
637
+ export function generateMscafGenerationV2(id) {
638
+ const g = _mscafGsV2.get(id);
639
+ if (!g) throw new Error(`mscaf generation ${id} not found`);
640
+ _mscafCheckG(g.status, MSCAF_GENERATION_LIFECYCLE_V2.GENERATED);
641
+ const now = Date.now();
642
+ g.status = MSCAF_GENERATION_LIFECYCLE_V2.GENERATED;
643
+ g.updatedAt = now;
644
+ if (!g.settledAt) g.settledAt = now;
645
+ return { ...g, metadata: { ...g.metadata } };
646
+ }
647
+ export function failMscafGenerationV2(id, reason) {
648
+ const g = _mscafGsV2.get(id);
649
+ if (!g) throw new Error(`mscaf generation ${id} not found`);
650
+ _mscafCheckG(g.status, MSCAF_GENERATION_LIFECYCLE_V2.FAILED);
651
+ const now = Date.now();
652
+ g.status = MSCAF_GENERATION_LIFECYCLE_V2.FAILED;
653
+ g.updatedAt = now;
654
+ if (!g.settledAt) g.settledAt = now;
655
+ if (reason) g.metadata.failReason = String(reason);
656
+ return { ...g, metadata: { ...g.metadata } };
657
+ }
658
+ export function cancelMscafGenerationV2(id, reason) {
659
+ const g = _mscafGsV2.get(id);
660
+ if (!g) throw new Error(`mscaf generation ${id} not found`);
661
+ _mscafCheckG(g.status, MSCAF_GENERATION_LIFECYCLE_V2.CANCELLED);
662
+ const now = Date.now();
663
+ g.status = MSCAF_GENERATION_LIFECYCLE_V2.CANCELLED;
664
+ g.updatedAt = now;
665
+ if (!g.settledAt) g.settledAt = now;
666
+ if (reason) g.metadata.cancelReason = String(reason);
667
+ return { ...g, metadata: { ...g.metadata } };
668
+ }
669
+ export function getMscafGenerationV2(id) {
670
+ const g = _mscafGsV2.get(id);
671
+ if (!g) return null;
672
+ return { ...g, metadata: { ...g.metadata } };
673
+ }
674
+ export function listMscafGenerationsV2() {
675
+ return [..._mscafGsV2.values()].map((g) => ({
676
+ ...g,
677
+ metadata: { ...g.metadata },
678
+ }));
679
+ }
680
+ export function autoStaleIdleMscafProfilesV2({ now } = {}) {
681
+ const t = now ?? Date.now();
682
+ const flipped = [];
683
+ for (const p of _mscafPsV2.values())
684
+ if (
685
+ p.status === MSCAF_PROFILE_MATURITY_V2.ACTIVE &&
686
+ t - p.lastTouchedAt >= _mscafIdleMs
687
+ ) {
688
+ p.status = MSCAF_PROFILE_MATURITY_V2.STALE;
689
+ p.updatedAt = t;
690
+ flipped.push(p.id);
691
+ }
692
+ return { flipped, count: flipped.length };
693
+ }
694
+ export function autoFailStuckMscafGenerationsV2({ now } = {}) {
695
+ const t = now ?? Date.now();
696
+ const flipped = [];
697
+ for (const g of _mscafGsV2.values())
698
+ if (
699
+ g.status === MSCAF_GENERATION_LIFECYCLE_V2.GENERATING &&
700
+ g.startedAt != null &&
701
+ t - g.startedAt >= _mscafStuckMs
702
+ ) {
703
+ g.status = MSCAF_GENERATION_LIFECYCLE_V2.FAILED;
704
+ g.updatedAt = t;
705
+ if (!g.settledAt) g.settledAt = t;
706
+ g.metadata.failReason = "auto-fail-stuck";
707
+ flipped.push(g.id);
708
+ }
709
+ return { flipped, count: flipped.length };
710
+ }
711
+ export function getMcpScaffoldGovStatsV2() {
712
+ const profilesByStatus = {};
713
+ for (const v of Object.values(MSCAF_PROFILE_MATURITY_V2))
714
+ profilesByStatus[v] = 0;
715
+ for (const p of _mscafPsV2.values()) profilesByStatus[p.status]++;
716
+ const generationsByStatus = {};
717
+ for (const v of Object.values(MSCAF_GENERATION_LIFECYCLE_V2))
718
+ generationsByStatus[v] = 0;
719
+ for (const g of _mscafGsV2.values()) generationsByStatus[g.status]++;
720
+ return {
721
+ totalMscafProfilesV2: _mscafPsV2.size,
722
+ totalMscafGenerationsV2: _mscafGsV2.size,
723
+ maxActiveMscafProfilesPerOwner: _mscafMaxActive,
724
+ maxPendingMscafGenerationsPerProfile: _mscafMaxPending,
725
+ mscafProfileIdleMs: _mscafIdleMs,
726
+ mscafGenerationStuckMs: _mscafStuckMs,
727
+ profilesByStatus,
728
+ generationsByStatus,
729
+ };
730
+ }