chainlesschain 0.145.0 → 0.156.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 (184) hide show
  1. package/README.md +52 -3
  2. package/package.json +1 -1
  3. package/src/commands/a2a.js +201 -0
  4. package/src/commands/activitypub.js +207 -0
  5. package/src/commands/agent-network.js +217 -0
  6. package/src/commands/agent.js +1250 -0
  7. package/src/commands/automation.js +201 -0
  8. package/src/commands/bi.js +203 -0
  9. package/src/commands/browse.js +213 -0
  10. package/src/commands/chat.js +605 -0
  11. package/src/commands/cli-anything.js +426 -0
  12. package/src/commands/codegen.js +207 -0
  13. package/src/commands/collab.js +211 -0
  14. package/src/commands/compliance.js +822 -0
  15. package/src/commands/config.js +213 -0
  16. package/src/commands/cowork.js +1666 -0
  17. package/src/commands/crosschain.js +203 -0
  18. package/src/commands/dao.js +203 -0
  19. package/src/commands/dbevo.js +227 -0
  20. package/src/commands/dev.js +207 -0
  21. package/src/commands/did-v2.js +217 -0
  22. package/src/commands/did.js +221 -0
  23. package/src/commands/dlp.js +213 -0
  24. package/src/commands/economy.js +199 -0
  25. package/src/commands/encrypt.js +201 -0
  26. package/src/commands/evolution.js +199 -0
  27. package/src/commands/evomap.js +830 -0
  28. package/src/commands/export.js +213 -0
  29. package/src/commands/federation.js +209 -0
  30. package/src/commands/fusion.js +205 -0
  31. package/src/commands/governance.js +209 -0
  32. package/src/commands/hmemory.js +203 -0
  33. package/src/commands/hook.js +209 -0
  34. package/src/commands/import.js +209 -0
  35. package/src/commands/inference.js +207 -0
  36. package/src/commands/infra.js +203 -0
  37. package/src/commands/instinct.js +209 -0
  38. package/src/commands/ipfs.js +207 -0
  39. package/src/commands/kg.js +195 -0
  40. package/src/commands/llm.js +426 -0
  41. package/src/commands/matrix.js +207 -0
  42. package/src/commands/mcp.js +217 -0
  43. package/src/commands/memory.js +412 -0
  44. package/src/commands/multimodal.js +203 -0
  45. package/src/commands/nlprog.js +225 -0
  46. package/src/commands/nostr.js +209 -0
  47. package/src/commands/note.js +205 -0
  48. package/src/commands/ops.js +219 -0
  49. package/src/commands/orchestrate.js +406 -0
  50. package/src/commands/org.js +209 -0
  51. package/src/commands/p2p.js +209 -0
  52. package/src/commands/perception.js +209 -0
  53. package/src/commands/permmem.js +203 -0
  54. package/src/commands/pipeline.js +199 -0
  55. package/src/commands/planmode.js +426 -0
  56. package/src/commands/plugin-ecosystem.js +209 -0
  57. package/src/commands/plugin.js +209 -0
  58. package/src/commands/pqc.js +213 -0
  59. package/src/commands/quantization.js +207 -0
  60. package/src/commands/rcache.js +205 -0
  61. package/src/commands/recommend.js +233 -0
  62. package/src/commands/runtime.js +205 -0
  63. package/src/commands/scim.js +209 -0
  64. package/src/commands/services.js +207 -0
  65. package/src/commands/session.js +209 -0
  66. package/src/commands/setup.js +205 -0
  67. package/src/commands/skill.js +414 -0
  68. package/src/commands/social.js +201 -0
  69. package/src/commands/sso.js +209 -0
  70. package/src/commands/start.js +209 -0
  71. package/src/commands/stream.js +213 -0
  72. package/src/commands/sync.js +209 -0
  73. package/src/commands/tech.js +209 -0
  74. package/src/commands/tenant.js +217 -0
  75. package/src/commands/tokens.js +209 -0
  76. package/src/commands/trust.js +217 -0
  77. package/src/commands/ui.js +225 -0
  78. package/src/commands/wallet.js +209 -0
  79. package/src/commands/workflow.js +412 -0
  80. package/src/index.js +252 -0
  81. package/src/lib/a2a-protocol.js +332 -0
  82. package/src/lib/activitypub-bridge.js +334 -0
  83. package/src/lib/agent-coordinator.js +334 -0
  84. package/src/lib/agent-economy.js +334 -0
  85. package/src/lib/agent-network.js +341 -0
  86. package/src/lib/agent-router.js +333 -0
  87. package/src/lib/aiops.js +346 -0
  88. package/src/lib/automation-engine.js +335 -0
  89. package/src/lib/autonomous-agent.js +332 -0
  90. package/src/lib/autonomous-developer.js +332 -0
  91. package/src/lib/bi-engine.js +333 -0
  92. package/src/lib/browser-automation.js +334 -0
  93. package/src/lib/chat-core.js +335 -0
  94. package/src/lib/cli-anything-bridge.js +341 -0
  95. package/src/lib/cli-context-engineering.js +351 -0
  96. package/src/lib/code-agent.js +339 -0
  97. package/src/lib/collaboration-governance.js +334 -0
  98. package/src/lib/community-governance.js +346 -0
  99. package/src/lib/compliance-manager.js +334 -0
  100. package/src/lib/content-recommendation.js +351 -0
  101. package/src/lib/cowork-adapter.js +336 -0
  102. package/src/lib/cowork-evomap-adapter.js +341 -0
  103. package/src/lib/cowork-mcp-tools.js +341 -0
  104. package/src/lib/cowork-observe-html.js +341 -0
  105. package/src/lib/cowork-observe.js +341 -0
  106. package/src/lib/cowork-share.js +338 -0
  107. package/src/lib/cowork-task-templates.js +342 -1
  108. package/src/lib/cowork-template-marketplace.js +340 -0
  109. package/src/lib/cross-chain.js +339 -0
  110. package/src/lib/crypto-manager.js +334 -0
  111. package/src/lib/dao-governance.js +339 -0
  112. package/src/lib/dbevo.js +351 -0
  113. package/src/lib/decentral-infra.js +330 -0
  114. package/src/lib/did-manager.js +341 -0
  115. package/src/lib/did-v2-manager.js +341 -0
  116. package/src/lib/dlp-engine.js +339 -0
  117. package/src/lib/downloader.js +334 -0
  118. package/src/lib/evolution-system.js +334 -0
  119. package/src/lib/evomap-client.js +342 -0
  120. package/src/lib/evomap-federation.js +338 -0
  121. package/src/lib/evomap-governance.js +334 -0
  122. package/src/lib/evomap-manager.js +330 -0
  123. package/src/lib/execution-backend.js +330 -0
  124. package/src/lib/federation-hardening.js +340 -0
  125. package/src/lib/hashline.js +338 -0
  126. package/src/lib/hierarchical-memory.js +334 -0
  127. package/src/lib/hook-manager.js +341 -0
  128. package/src/lib/inference-network.js +341 -0
  129. package/src/lib/instinct-manager.js +346 -0
  130. package/src/lib/interaction-adapter.js +330 -0
  131. package/src/lib/interactive-planner.js +354 -0
  132. package/src/lib/ipfs-storage.js +334 -0
  133. package/src/lib/knowledge-exporter.js +341 -0
  134. package/src/lib/knowledge-graph.js +331 -0
  135. package/src/lib/knowledge-importer.js +341 -0
  136. package/src/lib/llm-providers.js +346 -0
  137. package/src/lib/matrix-bridge.js +339 -0
  138. package/src/lib/mcp-registry.js +346 -0
  139. package/src/lib/memory-manager.js +336 -0
  140. package/src/lib/multimodal.js +330 -0
  141. package/src/lib/nl-programming.js +341 -0
  142. package/src/lib/nostr-bridge.js +336 -0
  143. package/src/lib/note-versioning.js +339 -0
  144. package/src/lib/org-manager.js +336 -0
  145. package/src/lib/p2p-manager.js +341 -0
  146. package/src/lib/perception.js +346 -0
  147. package/src/lib/permanent-memory.js +327 -0
  148. package/src/lib/pipeline-orchestrator.js +332 -0
  149. package/src/lib/plan-mode.js +336 -0
  150. package/src/lib/plugin-autodiscovery.js +334 -0
  151. package/src/lib/plugin-ecosystem.js +346 -0
  152. package/src/lib/pqc-manager.js +346 -0
  153. package/src/lib/process-manager.js +336 -0
  154. package/src/lib/protocol-fusion.js +338 -0
  155. package/src/lib/provider-options.js +346 -0
  156. package/src/lib/provider-stream.js +348 -0
  157. package/src/lib/quantization.js +337 -0
  158. package/src/lib/response-cache.js +333 -0
  159. package/src/lib/scim-manager.js +346 -0
  160. package/src/lib/service-manager.js +337 -0
  161. package/src/lib/session-core-singletons.js +341 -0
  162. package/src/lib/session-manager.js +334 -0
  163. package/src/lib/skill-loader.js +334 -0
  164. package/src/lib/skill-mcp.js +336 -0
  165. package/src/lib/social-manager.js +330 -0
  166. package/src/lib/sso-manager.js +340 -0
  167. package/src/lib/stix-parser.js +346 -0
  168. package/src/lib/sub-agent-context.js +343 -0
  169. package/src/lib/sub-agent-profiles.js +335 -0
  170. package/src/lib/sub-agent-registry.js +336 -0
  171. package/src/lib/sync-manager.js +336 -0
  172. package/src/lib/tech-learning-engine.js +341 -0
  173. package/src/lib/tenant-saas.js +341 -0
  174. package/src/lib/threat-intel.js +330 -0
  175. package/src/lib/todo-manager.js +336 -0
  176. package/src/lib/token-tracker.js +336 -0
  177. package/src/lib/trust-security.js +343 -0
  178. package/src/lib/ueba.js +340 -0
  179. package/src/lib/universal-runtime.js +330 -0
  180. package/src/lib/wallet-manager.js +336 -0
  181. package/src/lib/web-ui-server.js +348 -0
  182. package/src/lib/workflow-engine.js +330 -0
  183. package/src/lib/workflow-expr.js +346 -0
  184. package/src/lib/ws-chat-handler.js +337 -0
@@ -678,3 +678,349 @@ export function getMcpRegistryStatsV2() {
678
678
  invocationsByStatus,
679
679
  };
680
680
  }
681
+
682
+ // =====================================================================
683
+ // mcp-registry V2 governance overlay (iter24)
684
+ // =====================================================================
685
+ export const MCPGOV_PROFILE_MATURITY_V2 = Object.freeze({
686
+ PENDING: "pending",
687
+ ACTIVE: "active",
688
+ SUSPENDED: "suspended",
689
+ ARCHIVED: "archived",
690
+ });
691
+ export const MCPGOV_INVOCATION_LIFECYCLE_V2 = Object.freeze({
692
+ QUEUED: "queued",
693
+ INVOKING: "invoking",
694
+ INVOKED: "invoked",
695
+ FAILED: "failed",
696
+ CANCELLED: "cancelled",
697
+ });
698
+ const _mcpgovPTrans = new Map([
699
+ [
700
+ MCPGOV_PROFILE_MATURITY_V2.PENDING,
701
+ new Set([
702
+ MCPGOV_PROFILE_MATURITY_V2.ACTIVE,
703
+ MCPGOV_PROFILE_MATURITY_V2.ARCHIVED,
704
+ ]),
705
+ ],
706
+ [
707
+ MCPGOV_PROFILE_MATURITY_V2.ACTIVE,
708
+ new Set([
709
+ MCPGOV_PROFILE_MATURITY_V2.SUSPENDED,
710
+ MCPGOV_PROFILE_MATURITY_V2.ARCHIVED,
711
+ ]),
712
+ ],
713
+ [
714
+ MCPGOV_PROFILE_MATURITY_V2.SUSPENDED,
715
+ new Set([
716
+ MCPGOV_PROFILE_MATURITY_V2.ACTIVE,
717
+ MCPGOV_PROFILE_MATURITY_V2.ARCHIVED,
718
+ ]),
719
+ ],
720
+ [MCPGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
721
+ ]);
722
+ const _mcpgovPTerminal = new Set([MCPGOV_PROFILE_MATURITY_V2.ARCHIVED]);
723
+ const _mcpgovJTrans = new Map([
724
+ [
725
+ MCPGOV_INVOCATION_LIFECYCLE_V2.QUEUED,
726
+ new Set([
727
+ MCPGOV_INVOCATION_LIFECYCLE_V2.INVOKING,
728
+ MCPGOV_INVOCATION_LIFECYCLE_V2.CANCELLED,
729
+ ]),
730
+ ],
731
+ [
732
+ MCPGOV_INVOCATION_LIFECYCLE_V2.INVOKING,
733
+ new Set([
734
+ MCPGOV_INVOCATION_LIFECYCLE_V2.INVOKED,
735
+ MCPGOV_INVOCATION_LIFECYCLE_V2.FAILED,
736
+ MCPGOV_INVOCATION_LIFECYCLE_V2.CANCELLED,
737
+ ]),
738
+ ],
739
+ [MCPGOV_INVOCATION_LIFECYCLE_V2.INVOKED, new Set()],
740
+ [MCPGOV_INVOCATION_LIFECYCLE_V2.FAILED, new Set()],
741
+ [MCPGOV_INVOCATION_LIFECYCLE_V2.CANCELLED, new Set()],
742
+ ]);
743
+ const _mcpgovPsV2 = new Map();
744
+ const _mcpgovJsV2 = new Map();
745
+ let _mcpgovMaxActive = 10,
746
+ _mcpgovMaxPending = 25,
747
+ _mcpgovIdleMs = 30 * 24 * 60 * 60 * 1000,
748
+ _mcpgovStuckMs = 60 * 1000;
749
+ function _mcpgovPos(n, label) {
750
+ const v = Math.floor(Number(n));
751
+ if (!Number.isFinite(v) || v <= 0)
752
+ throw new Error(`${label} must be positive integer`);
753
+ return v;
754
+ }
755
+ function _mcpgovCheckP(from, to) {
756
+ const a = _mcpgovPTrans.get(from);
757
+ if (!a || !a.has(to))
758
+ throw new Error(`invalid mcpgov profile transition ${from} → ${to}`);
759
+ }
760
+ function _mcpgovCheckJ(from, to) {
761
+ const a = _mcpgovJTrans.get(from);
762
+ if (!a || !a.has(to))
763
+ throw new Error(`invalid mcpgov invocation transition ${from} → ${to}`);
764
+ }
765
+ function _mcpgovCountActive(owner) {
766
+ let c = 0;
767
+ for (const p of _mcpgovPsV2.values())
768
+ if (p.owner === owner && p.status === MCPGOV_PROFILE_MATURITY_V2.ACTIVE)
769
+ c++;
770
+ return c;
771
+ }
772
+ function _mcpgovCountPending(profileId) {
773
+ let c = 0;
774
+ for (const j of _mcpgovJsV2.values())
775
+ if (
776
+ j.profileId === profileId &&
777
+ (j.status === MCPGOV_INVOCATION_LIFECYCLE_V2.QUEUED ||
778
+ j.status === MCPGOV_INVOCATION_LIFECYCLE_V2.INVOKING)
779
+ )
780
+ c++;
781
+ return c;
782
+ }
783
+ export function setMaxActiveMcpgovProfilesPerOwnerV2(n) {
784
+ _mcpgovMaxActive = _mcpgovPos(n, "maxActiveMcpgovProfilesPerOwner");
785
+ }
786
+ export function getMaxActiveMcpgovProfilesPerOwnerV2() {
787
+ return _mcpgovMaxActive;
788
+ }
789
+ export function setMaxPendingMcpgovInvocationsPerProfileV2(n) {
790
+ _mcpgovMaxPending = _mcpgovPos(n, "maxPendingMcpgovInvocationsPerProfile");
791
+ }
792
+ export function getMaxPendingMcpgovInvocationsPerProfileV2() {
793
+ return _mcpgovMaxPending;
794
+ }
795
+ export function setMcpgovProfileIdleMsV2(n) {
796
+ _mcpgovIdleMs = _mcpgovPos(n, "mcpgovProfileIdleMs");
797
+ }
798
+ export function getMcpgovProfileIdleMsV2() {
799
+ return _mcpgovIdleMs;
800
+ }
801
+ export function setMcpgovInvocationStuckMsV2(n) {
802
+ _mcpgovStuckMs = _mcpgovPos(n, "mcpgovInvocationStuckMs");
803
+ }
804
+ export function getMcpgovInvocationStuckMsV2() {
805
+ return _mcpgovStuckMs;
806
+ }
807
+ export function _resetStateMcpRegistryGovV2() {
808
+ _mcpgovPsV2.clear();
809
+ _mcpgovJsV2.clear();
810
+ _mcpgovMaxActive = 10;
811
+ _mcpgovMaxPending = 25;
812
+ _mcpgovIdleMs = 30 * 24 * 60 * 60 * 1000;
813
+ _mcpgovStuckMs = 60 * 1000;
814
+ }
815
+ export function registerMcpgovProfileV2({
816
+ id,
817
+ owner,
818
+ transport,
819
+ metadata,
820
+ } = {}) {
821
+ if (!id || !owner) throw new Error("id and owner required");
822
+ if (_mcpgovPsV2.has(id))
823
+ throw new Error(`mcpgov profile ${id} already exists`);
824
+ const now = Date.now();
825
+ const p = {
826
+ id,
827
+ owner,
828
+ transport: transport || "stdio",
829
+ status: MCPGOV_PROFILE_MATURITY_V2.PENDING,
830
+ createdAt: now,
831
+ updatedAt: now,
832
+ lastTouchedAt: now,
833
+ activatedAt: null,
834
+ archivedAt: null,
835
+ metadata: { ...(metadata || {}) },
836
+ };
837
+ _mcpgovPsV2.set(id, p);
838
+ return { ...p, metadata: { ...p.metadata } };
839
+ }
840
+ export function activateMcpgovProfileV2(id) {
841
+ const p = _mcpgovPsV2.get(id);
842
+ if (!p) throw new Error(`mcpgov profile ${id} not found`);
843
+ const isInitial = p.status === MCPGOV_PROFILE_MATURITY_V2.PENDING;
844
+ _mcpgovCheckP(p.status, MCPGOV_PROFILE_MATURITY_V2.ACTIVE);
845
+ if (isInitial && _mcpgovCountActive(p.owner) >= _mcpgovMaxActive)
846
+ throw new Error(`max active mcpgov profiles for owner ${p.owner} reached`);
847
+ const now = Date.now();
848
+ p.status = MCPGOV_PROFILE_MATURITY_V2.ACTIVE;
849
+ p.updatedAt = now;
850
+ p.lastTouchedAt = now;
851
+ if (!p.activatedAt) p.activatedAt = now;
852
+ return { ...p, metadata: { ...p.metadata } };
853
+ }
854
+ export function suspendMcpgovProfileV2(id) {
855
+ const p = _mcpgovPsV2.get(id);
856
+ if (!p) throw new Error(`mcpgov profile ${id} not found`);
857
+ _mcpgovCheckP(p.status, MCPGOV_PROFILE_MATURITY_V2.SUSPENDED);
858
+ p.status = MCPGOV_PROFILE_MATURITY_V2.SUSPENDED;
859
+ p.updatedAt = Date.now();
860
+ return { ...p, metadata: { ...p.metadata } };
861
+ }
862
+ export function archiveMcpgovProfileV2(id) {
863
+ const p = _mcpgovPsV2.get(id);
864
+ if (!p) throw new Error(`mcpgov profile ${id} not found`);
865
+ _mcpgovCheckP(p.status, MCPGOV_PROFILE_MATURITY_V2.ARCHIVED);
866
+ const now = Date.now();
867
+ p.status = MCPGOV_PROFILE_MATURITY_V2.ARCHIVED;
868
+ p.updatedAt = now;
869
+ if (!p.archivedAt) p.archivedAt = now;
870
+ return { ...p, metadata: { ...p.metadata } };
871
+ }
872
+ export function touchMcpgovProfileV2(id) {
873
+ const p = _mcpgovPsV2.get(id);
874
+ if (!p) throw new Error(`mcpgov profile ${id} not found`);
875
+ if (_mcpgovPTerminal.has(p.status))
876
+ throw new Error(`cannot touch terminal mcpgov profile ${id}`);
877
+ const now = Date.now();
878
+ p.lastTouchedAt = now;
879
+ p.updatedAt = now;
880
+ return { ...p, metadata: { ...p.metadata } };
881
+ }
882
+ export function getMcpgovProfileV2(id) {
883
+ const p = _mcpgovPsV2.get(id);
884
+ if (!p) return null;
885
+ return { ...p, metadata: { ...p.metadata } };
886
+ }
887
+ export function listMcpgovProfilesV2() {
888
+ return [..._mcpgovPsV2.values()].map((p) => ({
889
+ ...p,
890
+ metadata: { ...p.metadata },
891
+ }));
892
+ }
893
+ export function createMcpgovInvocationV2({
894
+ id,
895
+ profileId,
896
+ tool,
897
+ metadata,
898
+ } = {}) {
899
+ if (!id || !profileId) throw new Error("id and profileId required");
900
+ if (_mcpgovJsV2.has(id))
901
+ throw new Error(`mcpgov invocation ${id} already exists`);
902
+ if (!_mcpgovPsV2.has(profileId))
903
+ throw new Error(`mcpgov profile ${profileId} not found`);
904
+ if (_mcpgovCountPending(profileId) >= _mcpgovMaxPending)
905
+ throw new Error(
906
+ `max pending mcpgov invocations for profile ${profileId} reached`,
907
+ );
908
+ const now = Date.now();
909
+ const j = {
910
+ id,
911
+ profileId,
912
+ tool: tool || "",
913
+ status: MCPGOV_INVOCATION_LIFECYCLE_V2.QUEUED,
914
+ createdAt: now,
915
+ updatedAt: now,
916
+ startedAt: null,
917
+ settledAt: null,
918
+ metadata: { ...(metadata || {}) },
919
+ };
920
+ _mcpgovJsV2.set(id, j);
921
+ return { ...j, metadata: { ...j.metadata } };
922
+ }
923
+ export function invokingMcpgovInvocationV2(id) {
924
+ const j = _mcpgovJsV2.get(id);
925
+ if (!j) throw new Error(`mcpgov invocation ${id} not found`);
926
+ _mcpgovCheckJ(j.status, MCPGOV_INVOCATION_LIFECYCLE_V2.INVOKING);
927
+ const now = Date.now();
928
+ j.status = MCPGOV_INVOCATION_LIFECYCLE_V2.INVOKING;
929
+ j.updatedAt = now;
930
+ if (!j.startedAt) j.startedAt = now;
931
+ return { ...j, metadata: { ...j.metadata } };
932
+ }
933
+ export function completeInvocationMcpgovV2(id) {
934
+ const j = _mcpgovJsV2.get(id);
935
+ if (!j) throw new Error(`mcpgov invocation ${id} not found`);
936
+ _mcpgovCheckJ(j.status, MCPGOV_INVOCATION_LIFECYCLE_V2.INVOKED);
937
+ const now = Date.now();
938
+ j.status = MCPGOV_INVOCATION_LIFECYCLE_V2.INVOKED;
939
+ j.updatedAt = now;
940
+ if (!j.settledAt) j.settledAt = now;
941
+ return { ...j, metadata: { ...j.metadata } };
942
+ }
943
+ export function failMcpgovInvocationV2(id, reason) {
944
+ const j = _mcpgovJsV2.get(id);
945
+ if (!j) throw new Error(`mcpgov invocation ${id} not found`);
946
+ _mcpgovCheckJ(j.status, MCPGOV_INVOCATION_LIFECYCLE_V2.FAILED);
947
+ const now = Date.now();
948
+ j.status = MCPGOV_INVOCATION_LIFECYCLE_V2.FAILED;
949
+ j.updatedAt = now;
950
+ if (!j.settledAt) j.settledAt = now;
951
+ if (reason) j.metadata.failReason = String(reason);
952
+ return { ...j, metadata: { ...j.metadata } };
953
+ }
954
+ export function cancelMcpgovInvocationV2(id, reason) {
955
+ const j = _mcpgovJsV2.get(id);
956
+ if (!j) throw new Error(`mcpgov invocation ${id} not found`);
957
+ _mcpgovCheckJ(j.status, MCPGOV_INVOCATION_LIFECYCLE_V2.CANCELLED);
958
+ const now = Date.now();
959
+ j.status = MCPGOV_INVOCATION_LIFECYCLE_V2.CANCELLED;
960
+ j.updatedAt = now;
961
+ if (!j.settledAt) j.settledAt = now;
962
+ if (reason) j.metadata.cancelReason = String(reason);
963
+ return { ...j, metadata: { ...j.metadata } };
964
+ }
965
+ export function getMcpgovInvocationV2(id) {
966
+ const j = _mcpgovJsV2.get(id);
967
+ if (!j) return null;
968
+ return { ...j, metadata: { ...j.metadata } };
969
+ }
970
+ export function listMcpgovInvocationsV2() {
971
+ return [..._mcpgovJsV2.values()].map((j) => ({
972
+ ...j,
973
+ metadata: { ...j.metadata },
974
+ }));
975
+ }
976
+ export function autoSuspendIdleMcpgovProfilesV2({ now } = {}) {
977
+ const t = now ?? Date.now();
978
+ const flipped = [];
979
+ for (const p of _mcpgovPsV2.values())
980
+ if (
981
+ p.status === MCPGOV_PROFILE_MATURITY_V2.ACTIVE &&
982
+ t - p.lastTouchedAt >= _mcpgovIdleMs
983
+ ) {
984
+ p.status = MCPGOV_PROFILE_MATURITY_V2.SUSPENDED;
985
+ p.updatedAt = t;
986
+ flipped.push(p.id);
987
+ }
988
+ return { flipped, count: flipped.length };
989
+ }
990
+ export function autoFailStuckMcpgovInvocationsV2({ now } = {}) {
991
+ const t = now ?? Date.now();
992
+ const flipped = [];
993
+ for (const j of _mcpgovJsV2.values())
994
+ if (
995
+ j.status === MCPGOV_INVOCATION_LIFECYCLE_V2.INVOKING &&
996
+ j.startedAt != null &&
997
+ t - j.startedAt >= _mcpgovStuckMs
998
+ ) {
999
+ j.status = MCPGOV_INVOCATION_LIFECYCLE_V2.FAILED;
1000
+ j.updatedAt = t;
1001
+ if (!j.settledAt) j.settledAt = t;
1002
+ j.metadata.failReason = "auto-fail-stuck";
1003
+ flipped.push(j.id);
1004
+ }
1005
+ return { flipped, count: flipped.length };
1006
+ }
1007
+ export function getMcpRegistryGovStatsV2() {
1008
+ const profilesByStatus = {};
1009
+ for (const v of Object.values(MCPGOV_PROFILE_MATURITY_V2))
1010
+ profilesByStatus[v] = 0;
1011
+ for (const p of _mcpgovPsV2.values()) profilesByStatus[p.status]++;
1012
+ const invocationsByStatus = {};
1013
+ for (const v of Object.values(MCPGOV_INVOCATION_LIFECYCLE_V2))
1014
+ invocationsByStatus[v] = 0;
1015
+ for (const j of _mcpgovJsV2.values()) invocationsByStatus[j.status]++;
1016
+ return {
1017
+ totalMcpgovProfilesV2: _mcpgovPsV2.size,
1018
+ totalMcpgovInvocationsV2: _mcpgovJsV2.size,
1019
+ maxActiveMcpgovProfilesPerOwner: _mcpgovMaxActive,
1020
+ maxPendingMcpgovInvocationsPerProfile: _mcpgovMaxPending,
1021
+ mcpgovProfileIdleMs: _mcpgovIdleMs,
1022
+ mcpgovInvocationStuckMs: _mcpgovStuckMs,
1023
+ profilesByStatus,
1024
+ invocationsByStatus,
1025
+ };
1026
+ }
@@ -539,3 +539,339 @@ export function _resetStateMemoryManagerV2() {
539
539
  _entryIdleMsV2 = MEMORY_DEFAULT_ENTRY_IDLE_MS;
540
540
  _jobStuckMsV2 = MEMORY_DEFAULT_JOB_STUCK_MS;
541
541
  }
542
+
543
+ // =====================================================================
544
+ // memory-manager V2 governance overlay (iter21)
545
+ // =====================================================================
546
+ export const MEMGOV_PROFILE_MATURITY_V2 = Object.freeze({
547
+ PENDING: "pending",
548
+ ACTIVE: "active",
549
+ STALE: "stale",
550
+ ARCHIVED: "archived",
551
+ });
552
+ export const MEMGOV_RECALL_LIFECYCLE_V2 = Object.freeze({
553
+ QUEUED: "queued",
554
+ RECALLING: "recalling",
555
+ RECALLED: "recalled",
556
+ FAILED: "failed",
557
+ CANCELLED: "cancelled",
558
+ });
559
+ const _memgovPTrans = new Map([
560
+ [
561
+ MEMGOV_PROFILE_MATURITY_V2.PENDING,
562
+ new Set([
563
+ MEMGOV_PROFILE_MATURITY_V2.ACTIVE,
564
+ MEMGOV_PROFILE_MATURITY_V2.ARCHIVED,
565
+ ]),
566
+ ],
567
+ [
568
+ MEMGOV_PROFILE_MATURITY_V2.ACTIVE,
569
+ new Set([
570
+ MEMGOV_PROFILE_MATURITY_V2.STALE,
571
+ MEMGOV_PROFILE_MATURITY_V2.ARCHIVED,
572
+ ]),
573
+ ],
574
+ [
575
+ MEMGOV_PROFILE_MATURITY_V2.STALE,
576
+ new Set([
577
+ MEMGOV_PROFILE_MATURITY_V2.ACTIVE,
578
+ MEMGOV_PROFILE_MATURITY_V2.ARCHIVED,
579
+ ]),
580
+ ],
581
+ [MEMGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
582
+ ]);
583
+ const _memgovPTerminal = new Set([MEMGOV_PROFILE_MATURITY_V2.ARCHIVED]);
584
+ const _memgovJTrans = new Map([
585
+ [
586
+ MEMGOV_RECALL_LIFECYCLE_V2.QUEUED,
587
+ new Set([
588
+ MEMGOV_RECALL_LIFECYCLE_V2.RECALLING,
589
+ MEMGOV_RECALL_LIFECYCLE_V2.CANCELLED,
590
+ ]),
591
+ ],
592
+ [
593
+ MEMGOV_RECALL_LIFECYCLE_V2.RECALLING,
594
+ new Set([
595
+ MEMGOV_RECALL_LIFECYCLE_V2.RECALLED,
596
+ MEMGOV_RECALL_LIFECYCLE_V2.FAILED,
597
+ MEMGOV_RECALL_LIFECYCLE_V2.CANCELLED,
598
+ ]),
599
+ ],
600
+ [MEMGOV_RECALL_LIFECYCLE_V2.RECALLED, new Set()],
601
+ [MEMGOV_RECALL_LIFECYCLE_V2.FAILED, new Set()],
602
+ [MEMGOV_RECALL_LIFECYCLE_V2.CANCELLED, new Set()],
603
+ ]);
604
+ const _memgovPsV2 = new Map();
605
+ const _memgovJsV2 = new Map();
606
+ let _memgovMaxActive = 10,
607
+ _memgovMaxPending = 30,
608
+ _memgovIdleMs = 30 * 24 * 60 * 60 * 1000,
609
+ _memgovStuckMs = 60 * 1000;
610
+ function _memgovPos(n, label) {
611
+ const v = Math.floor(Number(n));
612
+ if (!Number.isFinite(v) || v <= 0)
613
+ throw new Error(`${label} must be positive integer`);
614
+ return v;
615
+ }
616
+ function _memgovCheckP(from, to) {
617
+ const a = _memgovPTrans.get(from);
618
+ if (!a || !a.has(to))
619
+ throw new Error(`invalid memgov profile transition ${from} → ${to}`);
620
+ }
621
+ function _memgovCheckJ(from, to) {
622
+ const a = _memgovJTrans.get(from);
623
+ if (!a || !a.has(to))
624
+ throw new Error(`invalid memgov recall transition ${from} → ${to}`);
625
+ }
626
+ function _memgovCountActive(owner) {
627
+ let c = 0;
628
+ for (const p of _memgovPsV2.values())
629
+ if (p.owner === owner && p.status === MEMGOV_PROFILE_MATURITY_V2.ACTIVE)
630
+ c++;
631
+ return c;
632
+ }
633
+ function _memgovCountPending(profileId) {
634
+ let c = 0;
635
+ for (const j of _memgovJsV2.values())
636
+ if (
637
+ j.profileId === profileId &&
638
+ (j.status === MEMGOV_RECALL_LIFECYCLE_V2.QUEUED ||
639
+ j.status === MEMGOV_RECALL_LIFECYCLE_V2.RECALLING)
640
+ )
641
+ c++;
642
+ return c;
643
+ }
644
+ export function setMaxActiveMemgovProfilesPerOwnerV2(n) {
645
+ _memgovMaxActive = _memgovPos(n, "maxActiveMemgovProfilesPerOwner");
646
+ }
647
+ export function getMaxActiveMemgovProfilesPerOwnerV2() {
648
+ return _memgovMaxActive;
649
+ }
650
+ export function setMaxPendingMemgovRecallsPerProfileV2(n) {
651
+ _memgovMaxPending = _memgovPos(n, "maxPendingMemgovRecallsPerProfile");
652
+ }
653
+ export function getMaxPendingMemgovRecallsPerProfileV2() {
654
+ return _memgovMaxPending;
655
+ }
656
+ export function setMemgovProfileIdleMsV2(n) {
657
+ _memgovIdleMs = _memgovPos(n, "memgovProfileIdleMs");
658
+ }
659
+ export function getMemgovProfileIdleMsV2() {
660
+ return _memgovIdleMs;
661
+ }
662
+ export function setMemgovRecallStuckMsV2(n) {
663
+ _memgovStuckMs = _memgovPos(n, "memgovRecallStuckMs");
664
+ }
665
+ export function getMemgovRecallStuckMsV2() {
666
+ return _memgovStuckMs;
667
+ }
668
+ export function _resetStateMemoryManagerGovV2() {
669
+ _memgovPsV2.clear();
670
+ _memgovJsV2.clear();
671
+ _memgovMaxActive = 10;
672
+ _memgovMaxPending = 30;
673
+ _memgovIdleMs = 30 * 24 * 60 * 60 * 1000;
674
+ _memgovStuckMs = 60 * 1000;
675
+ }
676
+ export function registerMemgovProfileV2({ id, owner, scope, metadata } = {}) {
677
+ if (!id || !owner) throw new Error("id and owner required");
678
+ if (_memgovPsV2.has(id))
679
+ throw new Error(`memgov profile ${id} already exists`);
680
+ const now = Date.now();
681
+ const p = {
682
+ id,
683
+ owner,
684
+ scope: scope || "user",
685
+ status: MEMGOV_PROFILE_MATURITY_V2.PENDING,
686
+ createdAt: now,
687
+ updatedAt: now,
688
+ lastTouchedAt: now,
689
+ activatedAt: null,
690
+ archivedAt: null,
691
+ metadata: { ...(metadata || {}) },
692
+ };
693
+ _memgovPsV2.set(id, p);
694
+ return { ...p, metadata: { ...p.metadata } };
695
+ }
696
+ export function activateMemgovProfileV2(id) {
697
+ const p = _memgovPsV2.get(id);
698
+ if (!p) throw new Error(`memgov profile ${id} not found`);
699
+ const isInitial = p.status === MEMGOV_PROFILE_MATURITY_V2.PENDING;
700
+ _memgovCheckP(p.status, MEMGOV_PROFILE_MATURITY_V2.ACTIVE);
701
+ if (isInitial && _memgovCountActive(p.owner) >= _memgovMaxActive)
702
+ throw new Error(`max active memgov profiles for owner ${p.owner} reached`);
703
+ const now = Date.now();
704
+ p.status = MEMGOV_PROFILE_MATURITY_V2.ACTIVE;
705
+ p.updatedAt = now;
706
+ p.lastTouchedAt = now;
707
+ if (!p.activatedAt) p.activatedAt = now;
708
+ return { ...p, metadata: { ...p.metadata } };
709
+ }
710
+ export function staleMemgovProfileV2(id) {
711
+ const p = _memgovPsV2.get(id);
712
+ if (!p) throw new Error(`memgov profile ${id} not found`);
713
+ _memgovCheckP(p.status, MEMGOV_PROFILE_MATURITY_V2.STALE);
714
+ p.status = MEMGOV_PROFILE_MATURITY_V2.STALE;
715
+ p.updatedAt = Date.now();
716
+ return { ...p, metadata: { ...p.metadata } };
717
+ }
718
+ export function archiveMemgovProfileV2(id) {
719
+ const p = _memgovPsV2.get(id);
720
+ if (!p) throw new Error(`memgov profile ${id} not found`);
721
+ _memgovCheckP(p.status, MEMGOV_PROFILE_MATURITY_V2.ARCHIVED);
722
+ const now = Date.now();
723
+ p.status = MEMGOV_PROFILE_MATURITY_V2.ARCHIVED;
724
+ p.updatedAt = now;
725
+ if (!p.archivedAt) p.archivedAt = now;
726
+ return { ...p, metadata: { ...p.metadata } };
727
+ }
728
+ export function touchMemgovProfileV2(id) {
729
+ const p = _memgovPsV2.get(id);
730
+ if (!p) throw new Error(`memgov profile ${id} not found`);
731
+ if (_memgovPTerminal.has(p.status))
732
+ throw new Error(`cannot touch terminal memgov profile ${id}`);
733
+ const now = Date.now();
734
+ p.lastTouchedAt = now;
735
+ p.updatedAt = now;
736
+ return { ...p, metadata: { ...p.metadata } };
737
+ }
738
+ export function getMemgovProfileV2(id) {
739
+ const p = _memgovPsV2.get(id);
740
+ if (!p) return null;
741
+ return { ...p, metadata: { ...p.metadata } };
742
+ }
743
+ export function listMemgovProfilesV2() {
744
+ return [..._memgovPsV2.values()].map((p) => ({
745
+ ...p,
746
+ metadata: { ...p.metadata },
747
+ }));
748
+ }
749
+ export function createMemgovRecallV2({ id, profileId, key, metadata } = {}) {
750
+ if (!id || !profileId) throw new Error("id and profileId required");
751
+ if (_memgovJsV2.has(id))
752
+ throw new Error(`memgov recall ${id} already exists`);
753
+ if (!_memgovPsV2.has(profileId))
754
+ throw new Error(`memgov profile ${profileId} not found`);
755
+ if (_memgovCountPending(profileId) >= _memgovMaxPending)
756
+ throw new Error(
757
+ `max pending memgov recalls for profile ${profileId} reached`,
758
+ );
759
+ const now = Date.now();
760
+ const j = {
761
+ id,
762
+ profileId,
763
+ key: key || "",
764
+ status: MEMGOV_RECALL_LIFECYCLE_V2.QUEUED,
765
+ createdAt: now,
766
+ updatedAt: now,
767
+ startedAt: null,
768
+ settledAt: null,
769
+ metadata: { ...(metadata || {}) },
770
+ };
771
+ _memgovJsV2.set(id, j);
772
+ return { ...j, metadata: { ...j.metadata } };
773
+ }
774
+ export function recallingMemgovRecallV2(id) {
775
+ const j = _memgovJsV2.get(id);
776
+ if (!j) throw new Error(`memgov recall ${id} not found`);
777
+ _memgovCheckJ(j.status, MEMGOV_RECALL_LIFECYCLE_V2.RECALLING);
778
+ const now = Date.now();
779
+ j.status = MEMGOV_RECALL_LIFECYCLE_V2.RECALLING;
780
+ j.updatedAt = now;
781
+ if (!j.startedAt) j.startedAt = now;
782
+ return { ...j, metadata: { ...j.metadata } };
783
+ }
784
+ export function completeRecallMemgovV2(id) {
785
+ const j = _memgovJsV2.get(id);
786
+ if (!j) throw new Error(`memgov recall ${id} not found`);
787
+ _memgovCheckJ(j.status, MEMGOV_RECALL_LIFECYCLE_V2.RECALLED);
788
+ const now = Date.now();
789
+ j.status = MEMGOV_RECALL_LIFECYCLE_V2.RECALLED;
790
+ j.updatedAt = now;
791
+ if (!j.settledAt) j.settledAt = now;
792
+ return { ...j, metadata: { ...j.metadata } };
793
+ }
794
+ export function failMemgovRecallV2(id, reason) {
795
+ const j = _memgovJsV2.get(id);
796
+ if (!j) throw new Error(`memgov recall ${id} not found`);
797
+ _memgovCheckJ(j.status, MEMGOV_RECALL_LIFECYCLE_V2.FAILED);
798
+ const now = Date.now();
799
+ j.status = MEMGOV_RECALL_LIFECYCLE_V2.FAILED;
800
+ j.updatedAt = now;
801
+ if (!j.settledAt) j.settledAt = now;
802
+ if (reason) j.metadata.failReason = String(reason);
803
+ return { ...j, metadata: { ...j.metadata } };
804
+ }
805
+ export function cancelMemgovRecallV2(id, reason) {
806
+ const j = _memgovJsV2.get(id);
807
+ if (!j) throw new Error(`memgov recall ${id} not found`);
808
+ _memgovCheckJ(j.status, MEMGOV_RECALL_LIFECYCLE_V2.CANCELLED);
809
+ const now = Date.now();
810
+ j.status = MEMGOV_RECALL_LIFECYCLE_V2.CANCELLED;
811
+ j.updatedAt = now;
812
+ if (!j.settledAt) j.settledAt = now;
813
+ if (reason) j.metadata.cancelReason = String(reason);
814
+ return { ...j, metadata: { ...j.metadata } };
815
+ }
816
+ export function getMemgovRecallV2(id) {
817
+ const j = _memgovJsV2.get(id);
818
+ if (!j) return null;
819
+ return { ...j, metadata: { ...j.metadata } };
820
+ }
821
+ export function listMemgovRecallsV2() {
822
+ return [..._memgovJsV2.values()].map((j) => ({
823
+ ...j,
824
+ metadata: { ...j.metadata },
825
+ }));
826
+ }
827
+ export function autoStaleIdleMemgovProfilesV2({ now } = {}) {
828
+ const t = now ?? Date.now();
829
+ const flipped = [];
830
+ for (const p of _memgovPsV2.values())
831
+ if (
832
+ p.status === MEMGOV_PROFILE_MATURITY_V2.ACTIVE &&
833
+ t - p.lastTouchedAt >= _memgovIdleMs
834
+ ) {
835
+ p.status = MEMGOV_PROFILE_MATURITY_V2.STALE;
836
+ p.updatedAt = t;
837
+ flipped.push(p.id);
838
+ }
839
+ return { flipped, count: flipped.length };
840
+ }
841
+ export function autoFailStuckMemgovRecallsV2({ now } = {}) {
842
+ const t = now ?? Date.now();
843
+ const flipped = [];
844
+ for (const j of _memgovJsV2.values())
845
+ if (
846
+ j.status === MEMGOV_RECALL_LIFECYCLE_V2.RECALLING &&
847
+ j.startedAt != null &&
848
+ t - j.startedAt >= _memgovStuckMs
849
+ ) {
850
+ j.status = MEMGOV_RECALL_LIFECYCLE_V2.FAILED;
851
+ j.updatedAt = t;
852
+ if (!j.settledAt) j.settledAt = t;
853
+ j.metadata.failReason = "auto-fail-stuck";
854
+ flipped.push(j.id);
855
+ }
856
+ return { flipped, count: flipped.length };
857
+ }
858
+ export function getMemoryManagerGovStatsV2() {
859
+ const profilesByStatus = {};
860
+ for (const v of Object.values(MEMGOV_PROFILE_MATURITY_V2))
861
+ profilesByStatus[v] = 0;
862
+ for (const p of _memgovPsV2.values()) profilesByStatus[p.status]++;
863
+ const recallsByStatus = {};
864
+ for (const v of Object.values(MEMGOV_RECALL_LIFECYCLE_V2))
865
+ recallsByStatus[v] = 0;
866
+ for (const j of _memgovJsV2.values()) recallsByStatus[j.status]++;
867
+ return {
868
+ totalMemgovProfilesV2: _memgovPsV2.size,
869
+ totalMemgovRecallsV2: _memgovJsV2.size,
870
+ maxActiveMemgovProfilesPerOwner: _memgovMaxActive,
871
+ maxPendingMemgovRecallsPerProfile: _memgovMaxPending,
872
+ memgovProfileIdleMs: _memgovIdleMs,
873
+ memgovRecallStuckMs: _memgovStuckMs,
874
+ profilesByStatus,
875
+ recallsByStatus,
876
+ };
877
+ }