chainlesschain 0.145.0 → 0.152.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/package.json +1 -1
  2. package/src/commands/activitypub.js +207 -0
  3. package/src/commands/agent-network.js +217 -0
  4. package/src/commands/automation.js +201 -0
  5. package/src/commands/bi.js +203 -0
  6. package/src/commands/browse.js +213 -0
  7. package/src/commands/codegen.js +207 -0
  8. package/src/commands/collab.js +211 -0
  9. package/src/commands/compliance.js +410 -0
  10. package/src/commands/cowork.js +203 -0
  11. package/src/commands/dbevo.js +227 -0
  12. package/src/commands/dev.js +207 -0
  13. package/src/commands/did-v2.js +217 -0
  14. package/src/commands/did.js +221 -0
  15. package/src/commands/dlp.js +213 -0
  16. package/src/commands/evomap.js +205 -0
  17. package/src/commands/export.js +213 -0
  18. package/src/commands/federation.js +209 -0
  19. package/src/commands/fusion.js +205 -0
  20. package/src/commands/governance.js +209 -0
  21. package/src/commands/hook.js +209 -0
  22. package/src/commands/import.js +209 -0
  23. package/src/commands/infra.js +203 -0
  24. package/src/commands/instinct.js +209 -0
  25. package/src/commands/ipfs.js +207 -0
  26. package/src/commands/llm.js +217 -0
  27. package/src/commands/matrix.js +207 -0
  28. package/src/commands/mcp.js +217 -0
  29. package/src/commands/memory.js +209 -0
  30. package/src/commands/multimodal.js +203 -0
  31. package/src/commands/nlprog.js +225 -0
  32. package/src/commands/nostr.js +209 -0
  33. package/src/commands/note.js +205 -0
  34. package/src/commands/ops.js +219 -0
  35. package/src/commands/org.js +209 -0
  36. package/src/commands/p2p.js +209 -0
  37. package/src/commands/perception.js +209 -0
  38. package/src/commands/permmem.js +203 -0
  39. package/src/commands/plugin-ecosystem.js +209 -0
  40. package/src/commands/pqc.js +213 -0
  41. package/src/commands/quantization.js +207 -0
  42. package/src/commands/rcache.js +205 -0
  43. package/src/commands/recommend.js +233 -0
  44. package/src/commands/runtime.js +205 -0
  45. package/src/commands/scim.js +209 -0
  46. package/src/commands/session.js +209 -0
  47. package/src/commands/skill.js +207 -0
  48. package/src/commands/social.js +201 -0
  49. package/src/commands/sso.js +209 -0
  50. package/src/commands/sync.js +209 -0
  51. package/src/commands/tech.js +209 -0
  52. package/src/commands/tenant.js +217 -0
  53. package/src/commands/tokens.js +209 -0
  54. package/src/commands/trust.js +217 -0
  55. package/src/commands/wallet.js +209 -0
  56. package/src/commands/workflow.js +203 -0
  57. package/src/index.js +140 -0
  58. package/src/lib/activitypub-bridge.js +334 -0
  59. package/src/lib/agent-network.js +341 -0
  60. package/src/lib/aiops.js +346 -0
  61. package/src/lib/automation-engine.js +335 -0
  62. package/src/lib/autonomous-developer.js +332 -0
  63. package/src/lib/bi-engine.js +333 -0
  64. package/src/lib/browser-automation.js +334 -0
  65. package/src/lib/code-agent.js +339 -0
  66. package/src/lib/collaboration-governance.js +334 -0
  67. package/src/lib/community-governance.js +346 -0
  68. package/src/lib/content-recommendation.js +351 -0
  69. package/src/lib/cowork-share.js +338 -0
  70. package/src/lib/dbevo.js +351 -0
  71. package/src/lib/decentral-infra.js +330 -0
  72. package/src/lib/did-manager.js +341 -0
  73. package/src/lib/did-v2-manager.js +341 -0
  74. package/src/lib/dlp-engine.js +339 -0
  75. package/src/lib/evomap-governance.js +334 -0
  76. package/src/lib/federation-hardening.js +340 -0
  77. package/src/lib/hook-manager.js +341 -0
  78. package/src/lib/instinct-manager.js +346 -0
  79. package/src/lib/ipfs-storage.js +334 -0
  80. package/src/lib/knowledge-exporter.js +341 -0
  81. package/src/lib/knowledge-importer.js +341 -0
  82. package/src/lib/llm-providers.js +346 -0
  83. package/src/lib/matrix-bridge.js +339 -0
  84. package/src/lib/mcp-registry.js +346 -0
  85. package/src/lib/memory-manager.js +336 -0
  86. package/src/lib/multimodal.js +330 -0
  87. package/src/lib/nl-programming.js +341 -0
  88. package/src/lib/nostr-bridge.js +336 -0
  89. package/src/lib/note-versioning.js +339 -0
  90. package/src/lib/org-manager.js +336 -0
  91. package/src/lib/p2p-manager.js +341 -0
  92. package/src/lib/perception.js +346 -0
  93. package/src/lib/permanent-memory.js +327 -0
  94. package/src/lib/plugin-ecosystem.js +346 -0
  95. package/src/lib/pqc-manager.js +346 -0
  96. package/src/lib/protocol-fusion.js +338 -0
  97. package/src/lib/quantization.js +337 -0
  98. package/src/lib/response-cache.js +333 -0
  99. package/src/lib/scim-manager.js +346 -0
  100. package/src/lib/session-manager.js +334 -0
  101. package/src/lib/skill-loader.js +334 -0
  102. package/src/lib/social-manager.js +330 -0
  103. package/src/lib/sso-manager.js +340 -0
  104. package/src/lib/sync-manager.js +336 -0
  105. package/src/lib/tech-learning-engine.js +341 -0
  106. package/src/lib/tenant-saas.js +341 -0
  107. package/src/lib/threat-intel.js +330 -0
  108. package/src/lib/token-tracker.js +336 -0
  109. package/src/lib/trust-security.js +343 -0
  110. package/src/lib/ueba.js +340 -0
  111. package/src/lib/universal-runtime.js +330 -0
  112. package/src/lib/wallet-manager.js +336 -0
  113. package/src/lib/workflow-engine.js +330 -0
@@ -672,3 +672,339 @@ export function _resetStateWalletManagerV2() {
672
672
  _walletIdleMsV2 = WALLET_DEFAULT_WALLET_IDLE_MS;
673
673
  _txStuckMsV2 = WALLET_DEFAULT_TX_STUCK_MS;
674
674
  }
675
+
676
+ // =====================================================================
677
+ // wallet-manager V2 governance overlay (iter20)
678
+ // =====================================================================
679
+ export const WALGOV_PROFILE_MATURITY_V2 = Object.freeze({
680
+ PENDING: "pending",
681
+ ACTIVE: "active",
682
+ FROZEN: "frozen",
683
+ ARCHIVED: "archived",
684
+ });
685
+ export const WALGOV_TRANSFER_LIFECYCLE_V2 = Object.freeze({
686
+ QUEUED: "queued",
687
+ SIGNING: "signing",
688
+ SIGNED: "signed",
689
+ FAILED: "failed",
690
+ CANCELLED: "cancelled",
691
+ });
692
+ const _walgovPTrans = new Map([
693
+ [
694
+ WALGOV_PROFILE_MATURITY_V2.PENDING,
695
+ new Set([
696
+ WALGOV_PROFILE_MATURITY_V2.ACTIVE,
697
+ WALGOV_PROFILE_MATURITY_V2.ARCHIVED,
698
+ ]),
699
+ ],
700
+ [
701
+ WALGOV_PROFILE_MATURITY_V2.ACTIVE,
702
+ new Set([
703
+ WALGOV_PROFILE_MATURITY_V2.FROZEN,
704
+ WALGOV_PROFILE_MATURITY_V2.ARCHIVED,
705
+ ]),
706
+ ],
707
+ [
708
+ WALGOV_PROFILE_MATURITY_V2.FROZEN,
709
+ new Set([
710
+ WALGOV_PROFILE_MATURITY_V2.ACTIVE,
711
+ WALGOV_PROFILE_MATURITY_V2.ARCHIVED,
712
+ ]),
713
+ ],
714
+ [WALGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
715
+ ]);
716
+ const _walgovPTerminal = new Set([WALGOV_PROFILE_MATURITY_V2.ARCHIVED]);
717
+ const _walgovJTrans = new Map([
718
+ [
719
+ WALGOV_TRANSFER_LIFECYCLE_V2.QUEUED,
720
+ new Set([
721
+ WALGOV_TRANSFER_LIFECYCLE_V2.SIGNING,
722
+ WALGOV_TRANSFER_LIFECYCLE_V2.CANCELLED,
723
+ ]),
724
+ ],
725
+ [
726
+ WALGOV_TRANSFER_LIFECYCLE_V2.SIGNING,
727
+ new Set([
728
+ WALGOV_TRANSFER_LIFECYCLE_V2.SIGNED,
729
+ WALGOV_TRANSFER_LIFECYCLE_V2.FAILED,
730
+ WALGOV_TRANSFER_LIFECYCLE_V2.CANCELLED,
731
+ ]),
732
+ ],
733
+ [WALGOV_TRANSFER_LIFECYCLE_V2.SIGNED, new Set()],
734
+ [WALGOV_TRANSFER_LIFECYCLE_V2.FAILED, new Set()],
735
+ [WALGOV_TRANSFER_LIFECYCLE_V2.CANCELLED, new Set()],
736
+ ]);
737
+ const _walgovPsV2 = new Map();
738
+ const _walgovJsV2 = new Map();
739
+ let _walgovMaxActive = 6,
740
+ _walgovMaxPending = 15,
741
+ _walgovIdleMs = 30 * 24 * 60 * 60 * 1000,
742
+ _walgovStuckMs = 60 * 1000;
743
+ function _walgovPos(n, label) {
744
+ const v = Math.floor(Number(n));
745
+ if (!Number.isFinite(v) || v <= 0)
746
+ throw new Error(`${label} must be positive integer`);
747
+ return v;
748
+ }
749
+ function _walgovCheckP(from, to) {
750
+ const a = _walgovPTrans.get(from);
751
+ if (!a || !a.has(to))
752
+ throw new Error(`invalid walgov profile transition ${from} → ${to}`);
753
+ }
754
+ function _walgovCheckJ(from, to) {
755
+ const a = _walgovJTrans.get(from);
756
+ if (!a || !a.has(to))
757
+ throw new Error(`invalid walgov transfer transition ${from} → ${to}`);
758
+ }
759
+ function _walgovCountActive(owner) {
760
+ let c = 0;
761
+ for (const p of _walgovPsV2.values())
762
+ if (p.owner === owner && p.status === WALGOV_PROFILE_MATURITY_V2.ACTIVE)
763
+ c++;
764
+ return c;
765
+ }
766
+ function _walgovCountPending(profileId) {
767
+ let c = 0;
768
+ for (const j of _walgovJsV2.values())
769
+ if (
770
+ j.profileId === profileId &&
771
+ (j.status === WALGOV_TRANSFER_LIFECYCLE_V2.QUEUED ||
772
+ j.status === WALGOV_TRANSFER_LIFECYCLE_V2.SIGNING)
773
+ )
774
+ c++;
775
+ return c;
776
+ }
777
+ export function setMaxActiveWalgovProfilesPerOwnerV2(n) {
778
+ _walgovMaxActive = _walgovPos(n, "maxActiveWalgovProfilesPerOwner");
779
+ }
780
+ export function getMaxActiveWalgovProfilesPerOwnerV2() {
781
+ return _walgovMaxActive;
782
+ }
783
+ export function setMaxPendingWalgovTransfersPerProfileV2(n) {
784
+ _walgovMaxPending = _walgovPos(n, "maxPendingWalgovTransfersPerProfile");
785
+ }
786
+ export function getMaxPendingWalgovTransfersPerProfileV2() {
787
+ return _walgovMaxPending;
788
+ }
789
+ export function setWalgovProfileIdleMsV2(n) {
790
+ _walgovIdleMs = _walgovPos(n, "walgovProfileIdleMs");
791
+ }
792
+ export function getWalgovProfileIdleMsV2() {
793
+ return _walgovIdleMs;
794
+ }
795
+ export function setWalgovTransferStuckMsV2(n) {
796
+ _walgovStuckMs = _walgovPos(n, "walgovTransferStuckMs");
797
+ }
798
+ export function getWalgovTransferStuckMsV2() {
799
+ return _walgovStuckMs;
800
+ }
801
+ export function _resetStateWalletManagerGovV2() {
802
+ _walgovPsV2.clear();
803
+ _walgovJsV2.clear();
804
+ _walgovMaxActive = 6;
805
+ _walgovMaxPending = 15;
806
+ _walgovIdleMs = 30 * 24 * 60 * 60 * 1000;
807
+ _walgovStuckMs = 60 * 1000;
808
+ }
809
+ export function registerWalgovProfileV2({ id, owner, chain, metadata } = {}) {
810
+ if (!id || !owner) throw new Error("id and owner required");
811
+ if (_walgovPsV2.has(id))
812
+ throw new Error(`walgov profile ${id} already exists`);
813
+ const now = Date.now();
814
+ const p = {
815
+ id,
816
+ owner,
817
+ chain: chain || "mainnet",
818
+ status: WALGOV_PROFILE_MATURITY_V2.PENDING,
819
+ createdAt: now,
820
+ updatedAt: now,
821
+ lastTouchedAt: now,
822
+ activatedAt: null,
823
+ archivedAt: null,
824
+ metadata: { ...(metadata || {}) },
825
+ };
826
+ _walgovPsV2.set(id, p);
827
+ return { ...p, metadata: { ...p.metadata } };
828
+ }
829
+ export function activateWalgovProfileV2(id) {
830
+ const p = _walgovPsV2.get(id);
831
+ if (!p) throw new Error(`walgov profile ${id} not found`);
832
+ const isInitial = p.status === WALGOV_PROFILE_MATURITY_V2.PENDING;
833
+ _walgovCheckP(p.status, WALGOV_PROFILE_MATURITY_V2.ACTIVE);
834
+ if (isInitial && _walgovCountActive(p.owner) >= _walgovMaxActive)
835
+ throw new Error(`max active walgov profiles for owner ${p.owner} reached`);
836
+ const now = Date.now();
837
+ p.status = WALGOV_PROFILE_MATURITY_V2.ACTIVE;
838
+ p.updatedAt = now;
839
+ p.lastTouchedAt = now;
840
+ if (!p.activatedAt) p.activatedAt = now;
841
+ return { ...p, metadata: { ...p.metadata } };
842
+ }
843
+ export function freezeWalgovProfileV2(id) {
844
+ const p = _walgovPsV2.get(id);
845
+ if (!p) throw new Error(`walgov profile ${id} not found`);
846
+ _walgovCheckP(p.status, WALGOV_PROFILE_MATURITY_V2.FROZEN);
847
+ p.status = WALGOV_PROFILE_MATURITY_V2.FROZEN;
848
+ p.updatedAt = Date.now();
849
+ return { ...p, metadata: { ...p.metadata } };
850
+ }
851
+ export function archiveWalgovProfileV2(id) {
852
+ const p = _walgovPsV2.get(id);
853
+ if (!p) throw new Error(`walgov profile ${id} not found`);
854
+ _walgovCheckP(p.status, WALGOV_PROFILE_MATURITY_V2.ARCHIVED);
855
+ const now = Date.now();
856
+ p.status = WALGOV_PROFILE_MATURITY_V2.ARCHIVED;
857
+ p.updatedAt = now;
858
+ if (!p.archivedAt) p.archivedAt = now;
859
+ return { ...p, metadata: { ...p.metadata } };
860
+ }
861
+ export function touchWalgovProfileV2(id) {
862
+ const p = _walgovPsV2.get(id);
863
+ if (!p) throw new Error(`walgov profile ${id} not found`);
864
+ if (_walgovPTerminal.has(p.status))
865
+ throw new Error(`cannot touch terminal walgov profile ${id}`);
866
+ const now = Date.now();
867
+ p.lastTouchedAt = now;
868
+ p.updatedAt = now;
869
+ return { ...p, metadata: { ...p.metadata } };
870
+ }
871
+ export function getWalgovProfileV2(id) {
872
+ const p = _walgovPsV2.get(id);
873
+ if (!p) return null;
874
+ return { ...p, metadata: { ...p.metadata } };
875
+ }
876
+ export function listWalgovProfilesV2() {
877
+ return [..._walgovPsV2.values()].map((p) => ({
878
+ ...p,
879
+ metadata: { ...p.metadata },
880
+ }));
881
+ }
882
+ export function createWalgovTransferV2({ id, profileId, to, metadata } = {}) {
883
+ if (!id || !profileId) throw new Error("id and profileId required");
884
+ if (_walgovJsV2.has(id))
885
+ throw new Error(`walgov transfer ${id} already exists`);
886
+ if (!_walgovPsV2.has(profileId))
887
+ throw new Error(`walgov profile ${profileId} not found`);
888
+ if (_walgovCountPending(profileId) >= _walgovMaxPending)
889
+ throw new Error(
890
+ `max pending walgov transfers for profile ${profileId} reached`,
891
+ );
892
+ const now = Date.now();
893
+ const j = {
894
+ id,
895
+ profileId,
896
+ to: to || "",
897
+ status: WALGOV_TRANSFER_LIFECYCLE_V2.QUEUED,
898
+ createdAt: now,
899
+ updatedAt: now,
900
+ startedAt: null,
901
+ settledAt: null,
902
+ metadata: { ...(metadata || {}) },
903
+ };
904
+ _walgovJsV2.set(id, j);
905
+ return { ...j, metadata: { ...j.metadata } };
906
+ }
907
+ export function signingWalgovTransferV2(id) {
908
+ const j = _walgovJsV2.get(id);
909
+ if (!j) throw new Error(`walgov transfer ${id} not found`);
910
+ _walgovCheckJ(j.status, WALGOV_TRANSFER_LIFECYCLE_V2.SIGNING);
911
+ const now = Date.now();
912
+ j.status = WALGOV_TRANSFER_LIFECYCLE_V2.SIGNING;
913
+ j.updatedAt = now;
914
+ if (!j.startedAt) j.startedAt = now;
915
+ return { ...j, metadata: { ...j.metadata } };
916
+ }
917
+ export function completeTransferWalgovV2(id) {
918
+ const j = _walgovJsV2.get(id);
919
+ if (!j) throw new Error(`walgov transfer ${id} not found`);
920
+ _walgovCheckJ(j.status, WALGOV_TRANSFER_LIFECYCLE_V2.SIGNED);
921
+ const now = Date.now();
922
+ j.status = WALGOV_TRANSFER_LIFECYCLE_V2.SIGNED;
923
+ j.updatedAt = now;
924
+ if (!j.settledAt) j.settledAt = now;
925
+ return { ...j, metadata: { ...j.metadata } };
926
+ }
927
+ export function failWalgovTransferV2(id, reason) {
928
+ const j = _walgovJsV2.get(id);
929
+ if (!j) throw new Error(`walgov transfer ${id} not found`);
930
+ _walgovCheckJ(j.status, WALGOV_TRANSFER_LIFECYCLE_V2.FAILED);
931
+ const now = Date.now();
932
+ j.status = WALGOV_TRANSFER_LIFECYCLE_V2.FAILED;
933
+ j.updatedAt = now;
934
+ if (!j.settledAt) j.settledAt = now;
935
+ if (reason) j.metadata.failReason = String(reason);
936
+ return { ...j, metadata: { ...j.metadata } };
937
+ }
938
+ export function cancelWalgovTransferV2(id, reason) {
939
+ const j = _walgovJsV2.get(id);
940
+ if (!j) throw new Error(`walgov transfer ${id} not found`);
941
+ _walgovCheckJ(j.status, WALGOV_TRANSFER_LIFECYCLE_V2.CANCELLED);
942
+ const now = Date.now();
943
+ j.status = WALGOV_TRANSFER_LIFECYCLE_V2.CANCELLED;
944
+ j.updatedAt = now;
945
+ if (!j.settledAt) j.settledAt = now;
946
+ if (reason) j.metadata.cancelReason = String(reason);
947
+ return { ...j, metadata: { ...j.metadata } };
948
+ }
949
+ export function getWalgovTransferV2(id) {
950
+ const j = _walgovJsV2.get(id);
951
+ if (!j) return null;
952
+ return { ...j, metadata: { ...j.metadata } };
953
+ }
954
+ export function listWalgovTransfersV2() {
955
+ return [..._walgovJsV2.values()].map((j) => ({
956
+ ...j,
957
+ metadata: { ...j.metadata },
958
+ }));
959
+ }
960
+ export function autoFreezeIdleWalgovProfilesV2({ now } = {}) {
961
+ const t = now ?? Date.now();
962
+ const flipped = [];
963
+ for (const p of _walgovPsV2.values())
964
+ if (
965
+ p.status === WALGOV_PROFILE_MATURITY_V2.ACTIVE &&
966
+ t - p.lastTouchedAt >= _walgovIdleMs
967
+ ) {
968
+ p.status = WALGOV_PROFILE_MATURITY_V2.FROZEN;
969
+ p.updatedAt = t;
970
+ flipped.push(p.id);
971
+ }
972
+ return { flipped, count: flipped.length };
973
+ }
974
+ export function autoFailStuckWalgovTransfersV2({ now } = {}) {
975
+ const t = now ?? Date.now();
976
+ const flipped = [];
977
+ for (const j of _walgovJsV2.values())
978
+ if (
979
+ j.status === WALGOV_TRANSFER_LIFECYCLE_V2.SIGNING &&
980
+ j.startedAt != null &&
981
+ t - j.startedAt >= _walgovStuckMs
982
+ ) {
983
+ j.status = WALGOV_TRANSFER_LIFECYCLE_V2.FAILED;
984
+ j.updatedAt = t;
985
+ if (!j.settledAt) j.settledAt = t;
986
+ j.metadata.failReason = "auto-fail-stuck";
987
+ flipped.push(j.id);
988
+ }
989
+ return { flipped, count: flipped.length };
990
+ }
991
+ export function getWalletManagerGovStatsV2() {
992
+ const profilesByStatus = {};
993
+ for (const v of Object.values(WALGOV_PROFILE_MATURITY_V2))
994
+ profilesByStatus[v] = 0;
995
+ for (const p of _walgovPsV2.values()) profilesByStatus[p.status]++;
996
+ const transfersByStatus = {};
997
+ for (const v of Object.values(WALGOV_TRANSFER_LIFECYCLE_V2))
998
+ transfersByStatus[v] = 0;
999
+ for (const j of _walgovJsV2.values()) transfersByStatus[j.status]++;
1000
+ return {
1001
+ totalWalgovProfilesV2: _walgovPsV2.size,
1002
+ totalWalgovTransfersV2: _walgovJsV2.size,
1003
+ maxActiveWalgovProfilesPerOwner: _walgovMaxActive,
1004
+ maxPendingWalgovTransfersPerProfile: _walgovMaxPending,
1005
+ walgovProfileIdleMs: _walgovIdleMs,
1006
+ walgovTransferStuckMs: _walgovStuckMs,
1007
+ profilesByStatus,
1008
+ transfersByStatus,
1009
+ };
1010
+ }
@@ -1276,3 +1276,333 @@ export function getWorkflowEngineStatsV2() {
1276
1276
  runsByStatus,
1277
1277
  };
1278
1278
  }
1279
+
1280
+ // =====================================================================
1281
+ // workflow-engine V2 governance overlay (iter21)
1282
+ // =====================================================================
1283
+ export const WFGOV_PROFILE_MATURITY_V2 = Object.freeze({
1284
+ PENDING: "pending",
1285
+ ACTIVE: "active",
1286
+ PAUSED: "paused",
1287
+ ARCHIVED: "archived",
1288
+ });
1289
+ export const WFGOV_STEP_LIFECYCLE_V2 = Object.freeze({
1290
+ QUEUED: "queued",
1291
+ EXECUTING: "executing",
1292
+ EXECUTED: "executed",
1293
+ FAILED: "failed",
1294
+ CANCELLED: "cancelled",
1295
+ });
1296
+ const _wfgovPTrans = new Map([
1297
+ [
1298
+ WFGOV_PROFILE_MATURITY_V2.PENDING,
1299
+ new Set([
1300
+ WFGOV_PROFILE_MATURITY_V2.ACTIVE,
1301
+ WFGOV_PROFILE_MATURITY_V2.ARCHIVED,
1302
+ ]),
1303
+ ],
1304
+ [
1305
+ WFGOV_PROFILE_MATURITY_V2.ACTIVE,
1306
+ new Set([
1307
+ WFGOV_PROFILE_MATURITY_V2.PAUSED,
1308
+ WFGOV_PROFILE_MATURITY_V2.ARCHIVED,
1309
+ ]),
1310
+ ],
1311
+ [
1312
+ WFGOV_PROFILE_MATURITY_V2.PAUSED,
1313
+ new Set([
1314
+ WFGOV_PROFILE_MATURITY_V2.ACTIVE,
1315
+ WFGOV_PROFILE_MATURITY_V2.ARCHIVED,
1316
+ ]),
1317
+ ],
1318
+ [WFGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1319
+ ]);
1320
+ const _wfgovPTerminal = new Set([WFGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1321
+ const _wfgovJTrans = new Map([
1322
+ [
1323
+ WFGOV_STEP_LIFECYCLE_V2.QUEUED,
1324
+ new Set([
1325
+ WFGOV_STEP_LIFECYCLE_V2.EXECUTING,
1326
+ WFGOV_STEP_LIFECYCLE_V2.CANCELLED,
1327
+ ]),
1328
+ ],
1329
+ [
1330
+ WFGOV_STEP_LIFECYCLE_V2.EXECUTING,
1331
+ new Set([
1332
+ WFGOV_STEP_LIFECYCLE_V2.EXECUTED,
1333
+ WFGOV_STEP_LIFECYCLE_V2.FAILED,
1334
+ WFGOV_STEP_LIFECYCLE_V2.CANCELLED,
1335
+ ]),
1336
+ ],
1337
+ [WFGOV_STEP_LIFECYCLE_V2.EXECUTED, new Set()],
1338
+ [WFGOV_STEP_LIFECYCLE_V2.FAILED, new Set()],
1339
+ [WFGOV_STEP_LIFECYCLE_V2.CANCELLED, new Set()],
1340
+ ]);
1341
+ const _wfgovPsV2 = new Map();
1342
+ const _wfgovJsV2 = new Map();
1343
+ let _wfgovMaxActive = 8,
1344
+ _wfgovMaxPending = 20,
1345
+ _wfgovIdleMs = 30 * 24 * 60 * 60 * 1000,
1346
+ _wfgovStuckMs = 60 * 1000;
1347
+ function _wfgovPos(n, label) {
1348
+ const v = Math.floor(Number(n));
1349
+ if (!Number.isFinite(v) || v <= 0)
1350
+ throw new Error(`${label} must be positive integer`);
1351
+ return v;
1352
+ }
1353
+ function _wfgovCheckP(from, to) {
1354
+ const a = _wfgovPTrans.get(from);
1355
+ if (!a || !a.has(to))
1356
+ throw new Error(`invalid wfgov profile transition ${from} → ${to}`);
1357
+ }
1358
+ function _wfgovCheckJ(from, to) {
1359
+ const a = _wfgovJTrans.get(from);
1360
+ if (!a || !a.has(to))
1361
+ throw new Error(`invalid wfgov step transition ${from} → ${to}`);
1362
+ }
1363
+ function _wfgovCountActive(owner) {
1364
+ let c = 0;
1365
+ for (const p of _wfgovPsV2.values())
1366
+ if (p.owner === owner && p.status === WFGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
1367
+ return c;
1368
+ }
1369
+ function _wfgovCountPending(profileId) {
1370
+ let c = 0;
1371
+ for (const j of _wfgovJsV2.values())
1372
+ if (
1373
+ j.profileId === profileId &&
1374
+ (j.status === WFGOV_STEP_LIFECYCLE_V2.QUEUED ||
1375
+ j.status === WFGOV_STEP_LIFECYCLE_V2.EXECUTING)
1376
+ )
1377
+ c++;
1378
+ return c;
1379
+ }
1380
+ export function setMaxActiveWfgovProfilesPerOwnerV2(n) {
1381
+ _wfgovMaxActive = _wfgovPos(n, "maxActiveWfgovProfilesPerOwner");
1382
+ }
1383
+ export function getMaxActiveWfgovProfilesPerOwnerV2() {
1384
+ return _wfgovMaxActive;
1385
+ }
1386
+ export function setMaxPendingWfgovStepsPerProfileV2(n) {
1387
+ _wfgovMaxPending = _wfgovPos(n, "maxPendingWfgovStepsPerProfile");
1388
+ }
1389
+ export function getMaxPendingWfgovStepsPerProfileV2() {
1390
+ return _wfgovMaxPending;
1391
+ }
1392
+ export function setWfgovProfileIdleMsV2(n) {
1393
+ _wfgovIdleMs = _wfgovPos(n, "wfgovProfileIdleMs");
1394
+ }
1395
+ export function getWfgovProfileIdleMsV2() {
1396
+ return _wfgovIdleMs;
1397
+ }
1398
+ export function setWfgovStepStuckMsV2(n) {
1399
+ _wfgovStuckMs = _wfgovPos(n, "wfgovStepStuckMs");
1400
+ }
1401
+ export function getWfgovStepStuckMsV2() {
1402
+ return _wfgovStuckMs;
1403
+ }
1404
+ export function _resetStateWorkflowEngineGovV2() {
1405
+ _wfgovPsV2.clear();
1406
+ _wfgovJsV2.clear();
1407
+ _wfgovMaxActive = 8;
1408
+ _wfgovMaxPending = 20;
1409
+ _wfgovIdleMs = 30 * 24 * 60 * 60 * 1000;
1410
+ _wfgovStuckMs = 60 * 1000;
1411
+ }
1412
+ export function registerWfgovProfileV2({ id, owner, kind, metadata } = {}) {
1413
+ if (!id || !owner) throw new Error("id and owner required");
1414
+ if (_wfgovPsV2.has(id)) throw new Error(`wfgov profile ${id} already exists`);
1415
+ const now = Date.now();
1416
+ const p = {
1417
+ id,
1418
+ owner,
1419
+ kind: kind || "sequential",
1420
+ status: WFGOV_PROFILE_MATURITY_V2.PENDING,
1421
+ createdAt: now,
1422
+ updatedAt: now,
1423
+ lastTouchedAt: now,
1424
+ activatedAt: null,
1425
+ archivedAt: null,
1426
+ metadata: { ...(metadata || {}) },
1427
+ };
1428
+ _wfgovPsV2.set(id, p);
1429
+ return { ...p, metadata: { ...p.metadata } };
1430
+ }
1431
+ export function activateWfgovProfileV2(id) {
1432
+ const p = _wfgovPsV2.get(id);
1433
+ if (!p) throw new Error(`wfgov profile ${id} not found`);
1434
+ const isInitial = p.status === WFGOV_PROFILE_MATURITY_V2.PENDING;
1435
+ _wfgovCheckP(p.status, WFGOV_PROFILE_MATURITY_V2.ACTIVE);
1436
+ if (isInitial && _wfgovCountActive(p.owner) >= _wfgovMaxActive)
1437
+ throw new Error(`max active wfgov profiles for owner ${p.owner} reached`);
1438
+ const now = Date.now();
1439
+ p.status = WFGOV_PROFILE_MATURITY_V2.ACTIVE;
1440
+ p.updatedAt = now;
1441
+ p.lastTouchedAt = now;
1442
+ if (!p.activatedAt) p.activatedAt = now;
1443
+ return { ...p, metadata: { ...p.metadata } };
1444
+ }
1445
+ export function pauseWfgovProfileV2(id) {
1446
+ const p = _wfgovPsV2.get(id);
1447
+ if (!p) throw new Error(`wfgov profile ${id} not found`);
1448
+ _wfgovCheckP(p.status, WFGOV_PROFILE_MATURITY_V2.PAUSED);
1449
+ p.status = WFGOV_PROFILE_MATURITY_V2.PAUSED;
1450
+ p.updatedAt = Date.now();
1451
+ return { ...p, metadata: { ...p.metadata } };
1452
+ }
1453
+ export function archiveWfgovProfileV2(id) {
1454
+ const p = _wfgovPsV2.get(id);
1455
+ if (!p) throw new Error(`wfgov profile ${id} not found`);
1456
+ _wfgovCheckP(p.status, WFGOV_PROFILE_MATURITY_V2.ARCHIVED);
1457
+ const now = Date.now();
1458
+ p.status = WFGOV_PROFILE_MATURITY_V2.ARCHIVED;
1459
+ p.updatedAt = now;
1460
+ if (!p.archivedAt) p.archivedAt = now;
1461
+ return { ...p, metadata: { ...p.metadata } };
1462
+ }
1463
+ export function touchWfgovProfileV2(id) {
1464
+ const p = _wfgovPsV2.get(id);
1465
+ if (!p) throw new Error(`wfgov profile ${id} not found`);
1466
+ if (_wfgovPTerminal.has(p.status))
1467
+ throw new Error(`cannot touch terminal wfgov profile ${id}`);
1468
+ const now = Date.now();
1469
+ p.lastTouchedAt = now;
1470
+ p.updatedAt = now;
1471
+ return { ...p, metadata: { ...p.metadata } };
1472
+ }
1473
+ export function getWfgovProfileV2(id) {
1474
+ const p = _wfgovPsV2.get(id);
1475
+ if (!p) return null;
1476
+ return { ...p, metadata: { ...p.metadata } };
1477
+ }
1478
+ export function listWfgovProfilesV2() {
1479
+ return [..._wfgovPsV2.values()].map((p) => ({
1480
+ ...p,
1481
+ metadata: { ...p.metadata },
1482
+ }));
1483
+ }
1484
+ export function createWfgovStepV2({ id, profileId, stepName, metadata } = {}) {
1485
+ if (!id || !profileId) throw new Error("id and profileId required");
1486
+ if (_wfgovJsV2.has(id)) throw new Error(`wfgov step ${id} already exists`);
1487
+ if (!_wfgovPsV2.has(profileId))
1488
+ throw new Error(`wfgov profile ${profileId} not found`);
1489
+ if (_wfgovCountPending(profileId) >= _wfgovMaxPending)
1490
+ throw new Error(`max pending wfgov steps for profile ${profileId} reached`);
1491
+ const now = Date.now();
1492
+ const j = {
1493
+ id,
1494
+ profileId,
1495
+ stepName: stepName || "",
1496
+ status: WFGOV_STEP_LIFECYCLE_V2.QUEUED,
1497
+ createdAt: now,
1498
+ updatedAt: now,
1499
+ startedAt: null,
1500
+ settledAt: null,
1501
+ metadata: { ...(metadata || {}) },
1502
+ };
1503
+ _wfgovJsV2.set(id, j);
1504
+ return { ...j, metadata: { ...j.metadata } };
1505
+ }
1506
+ export function executingWfgovStepV2(id) {
1507
+ const j = _wfgovJsV2.get(id);
1508
+ if (!j) throw new Error(`wfgov step ${id} not found`);
1509
+ _wfgovCheckJ(j.status, WFGOV_STEP_LIFECYCLE_V2.EXECUTING);
1510
+ const now = Date.now();
1511
+ j.status = WFGOV_STEP_LIFECYCLE_V2.EXECUTING;
1512
+ j.updatedAt = now;
1513
+ if (!j.startedAt) j.startedAt = now;
1514
+ return { ...j, metadata: { ...j.metadata } };
1515
+ }
1516
+ export function completeStepWfgovV2(id) {
1517
+ const j = _wfgovJsV2.get(id);
1518
+ if (!j) throw new Error(`wfgov step ${id} not found`);
1519
+ _wfgovCheckJ(j.status, WFGOV_STEP_LIFECYCLE_V2.EXECUTED);
1520
+ const now = Date.now();
1521
+ j.status = WFGOV_STEP_LIFECYCLE_V2.EXECUTED;
1522
+ j.updatedAt = now;
1523
+ if (!j.settledAt) j.settledAt = now;
1524
+ return { ...j, metadata: { ...j.metadata } };
1525
+ }
1526
+ export function failWfgovStepV2(id, reason) {
1527
+ const j = _wfgovJsV2.get(id);
1528
+ if (!j) throw new Error(`wfgov step ${id} not found`);
1529
+ _wfgovCheckJ(j.status, WFGOV_STEP_LIFECYCLE_V2.FAILED);
1530
+ const now = Date.now();
1531
+ j.status = WFGOV_STEP_LIFECYCLE_V2.FAILED;
1532
+ j.updatedAt = now;
1533
+ if (!j.settledAt) j.settledAt = now;
1534
+ if (reason) j.metadata.failReason = String(reason);
1535
+ return { ...j, metadata: { ...j.metadata } };
1536
+ }
1537
+ export function cancelWfgovStepV2(id, reason) {
1538
+ const j = _wfgovJsV2.get(id);
1539
+ if (!j) throw new Error(`wfgov step ${id} not found`);
1540
+ _wfgovCheckJ(j.status, WFGOV_STEP_LIFECYCLE_V2.CANCELLED);
1541
+ const now = Date.now();
1542
+ j.status = WFGOV_STEP_LIFECYCLE_V2.CANCELLED;
1543
+ j.updatedAt = now;
1544
+ if (!j.settledAt) j.settledAt = now;
1545
+ if (reason) j.metadata.cancelReason = String(reason);
1546
+ return { ...j, metadata: { ...j.metadata } };
1547
+ }
1548
+ export function getWfgovStepV2(id) {
1549
+ const j = _wfgovJsV2.get(id);
1550
+ if (!j) return null;
1551
+ return { ...j, metadata: { ...j.metadata } };
1552
+ }
1553
+ export function listWfgovStepsV2() {
1554
+ return [..._wfgovJsV2.values()].map((j) => ({
1555
+ ...j,
1556
+ metadata: { ...j.metadata },
1557
+ }));
1558
+ }
1559
+ export function autoPauseIdleWfgovProfilesV2({ now } = {}) {
1560
+ const t = now ?? Date.now();
1561
+ const flipped = [];
1562
+ for (const p of _wfgovPsV2.values())
1563
+ if (
1564
+ p.status === WFGOV_PROFILE_MATURITY_V2.ACTIVE &&
1565
+ t - p.lastTouchedAt >= _wfgovIdleMs
1566
+ ) {
1567
+ p.status = WFGOV_PROFILE_MATURITY_V2.PAUSED;
1568
+ p.updatedAt = t;
1569
+ flipped.push(p.id);
1570
+ }
1571
+ return { flipped, count: flipped.length };
1572
+ }
1573
+ export function autoFailStuckWfgovStepsV2({ now } = {}) {
1574
+ const t = now ?? Date.now();
1575
+ const flipped = [];
1576
+ for (const j of _wfgovJsV2.values())
1577
+ if (
1578
+ j.status === WFGOV_STEP_LIFECYCLE_V2.EXECUTING &&
1579
+ j.startedAt != null &&
1580
+ t - j.startedAt >= _wfgovStuckMs
1581
+ ) {
1582
+ j.status = WFGOV_STEP_LIFECYCLE_V2.FAILED;
1583
+ j.updatedAt = t;
1584
+ if (!j.settledAt) j.settledAt = t;
1585
+ j.metadata.failReason = "auto-fail-stuck";
1586
+ flipped.push(j.id);
1587
+ }
1588
+ return { flipped, count: flipped.length };
1589
+ }
1590
+ export function getWorkflowEngineGovStatsV2() {
1591
+ const profilesByStatus = {};
1592
+ for (const v of Object.values(WFGOV_PROFILE_MATURITY_V2))
1593
+ profilesByStatus[v] = 0;
1594
+ for (const p of _wfgovPsV2.values()) profilesByStatus[p.status]++;
1595
+ const stepsByStatus = {};
1596
+ for (const v of Object.values(WFGOV_STEP_LIFECYCLE_V2)) stepsByStatus[v] = 0;
1597
+ for (const j of _wfgovJsV2.values()) stepsByStatus[j.status]++;
1598
+ return {
1599
+ totalWfgovProfilesV2: _wfgovPsV2.size,
1600
+ totalWfgovStepsV2: _wfgovJsV2.size,
1601
+ maxActiveWfgovProfilesPerOwner: _wfgovMaxActive,
1602
+ maxPendingWfgovStepsPerProfile: _wfgovMaxPending,
1603
+ wfgovProfileIdleMs: _wfgovIdleMs,
1604
+ wfgovStepStuckMs: _wfgovStuckMs,
1605
+ profilesByStatus,
1606
+ stepsByStatus,
1607
+ };
1608
+ }