chainlesschain 0.152.0 → 0.156.2

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 (80) 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/agent.js +1250 -0
  5. package/src/commands/chat.js +605 -0
  6. package/src/commands/cli-anything.js +426 -0
  7. package/src/commands/compliance.js +412 -0
  8. package/src/commands/config.js +213 -0
  9. package/src/commands/cowork.js +1463 -0
  10. package/src/commands/crosschain.js +203 -0
  11. package/src/commands/dao.js +203 -0
  12. package/src/commands/economy.js +199 -0
  13. package/src/commands/encrypt.js +201 -0
  14. package/src/commands/evolution.js +199 -0
  15. package/src/commands/evomap.js +625 -0
  16. package/src/commands/hmemory.js +203 -0
  17. package/src/commands/inference.js +207 -0
  18. package/src/commands/kg.js +195 -0
  19. package/src/commands/llm.js +209 -0
  20. package/src/commands/memory.js +203 -0
  21. package/src/commands/orchestrate.js +406 -0
  22. package/src/commands/pipeline.js +199 -0
  23. package/src/commands/planmode.js +426 -0
  24. package/src/commands/plugin.js +209 -0
  25. package/src/commands/services.js +207 -0
  26. package/src/commands/setup.js +205 -0
  27. package/src/commands/skill.js +207 -0
  28. package/src/commands/start.js +209 -0
  29. package/src/commands/stream.js +213 -0
  30. package/src/commands/ui.js +225 -0
  31. package/src/commands/workflow.js +209 -0
  32. package/src/index.js +112 -0
  33. package/src/lib/a2a-protocol.js +332 -0
  34. package/src/lib/agent-coordinator.js +334 -0
  35. package/src/lib/agent-economy.js +334 -0
  36. package/src/lib/agent-router.js +333 -0
  37. package/src/lib/autonomous-agent.js +332 -0
  38. package/src/lib/chat-core.js +335 -0
  39. package/src/lib/cli-anything-bridge.js +341 -0
  40. package/src/lib/cli-context-engineering.js +351 -0
  41. package/src/lib/compliance-manager.js +334 -0
  42. package/src/lib/cowork-adapter.js +336 -0
  43. package/src/lib/cowork-evomap-adapter.js +341 -0
  44. package/src/lib/cowork-mcp-tools.js +341 -0
  45. package/src/lib/cowork-observe-html.js +341 -0
  46. package/src/lib/cowork-observe.js +341 -0
  47. package/src/lib/cowork-task-templates.js +342 -1
  48. package/src/lib/cowork-template-marketplace.js +340 -0
  49. package/src/lib/cross-chain.js +339 -0
  50. package/src/lib/crypto-manager.js +334 -0
  51. package/src/lib/dao-governance.js +339 -0
  52. package/src/lib/downloader.js +334 -0
  53. package/src/lib/evolution-system.js +334 -0
  54. package/src/lib/evomap-client.js +342 -0
  55. package/src/lib/evomap-federation.js +338 -0
  56. package/src/lib/evomap-manager.js +330 -0
  57. package/src/lib/execution-backend.js +330 -0
  58. package/src/lib/hashline.js +338 -0
  59. package/src/lib/hierarchical-memory.js +334 -0
  60. package/src/lib/inference-network.js +341 -0
  61. package/src/lib/interaction-adapter.js +330 -0
  62. package/src/lib/interactive-planner.js +354 -0
  63. package/src/lib/knowledge-graph.js +331 -0
  64. package/src/lib/pipeline-orchestrator.js +332 -0
  65. package/src/lib/plan-mode.js +336 -0
  66. package/src/lib/plugin-autodiscovery.js +334 -0
  67. package/src/lib/process-manager.js +336 -0
  68. package/src/lib/provider-options.js +346 -0
  69. package/src/lib/provider-stream.js +348 -0
  70. package/src/lib/service-manager.js +337 -0
  71. package/src/lib/session-core-singletons.js +341 -0
  72. package/src/lib/skill-mcp.js +336 -0
  73. package/src/lib/stix-parser.js +346 -0
  74. package/src/lib/sub-agent-context.js +343 -0
  75. package/src/lib/sub-agent-profiles.js +335 -0
  76. package/src/lib/sub-agent-registry.js +336 -0
  77. package/src/lib/todo-manager.js +336 -0
  78. package/src/lib/web-ui-server.js +348 -0
  79. package/src/lib/workflow-expr.js +346 -0
  80. package/src/lib/ws-chat-handler.js +337 -0
@@ -594,3 +594,337 @@ export function _resetStateCryptoManagerV2() {
594
594
  _keyIdleMsV2 = CRYPTO_DEFAULT_KEY_IDLE_MS;
595
595
  _jobStuckMsV2 = CRYPTO_DEFAULT_JOB_STUCK_MS;
596
596
  }
597
+
598
+ // === Iter28 V2 governance overlay: Crygov ===
599
+ export const CRYGOV_PROFILE_MATURITY_V2 = Object.freeze({
600
+ PENDING: "pending",
601
+ ACTIVE: "active",
602
+ STALE: "stale",
603
+ ARCHIVED: "archived",
604
+ });
605
+ export const CRYGOV_ENCRYPT_LIFECYCLE_V2 = Object.freeze({
606
+ QUEUED: "queued",
607
+ ENCRYPTING: "encrypting",
608
+ ENCRYPTED: "encrypted",
609
+ FAILED: "failed",
610
+ CANCELLED: "cancelled",
611
+ });
612
+ const _crygovPTrans = new Map([
613
+ [
614
+ CRYGOV_PROFILE_MATURITY_V2.PENDING,
615
+ new Set([
616
+ CRYGOV_PROFILE_MATURITY_V2.ACTIVE,
617
+ CRYGOV_PROFILE_MATURITY_V2.ARCHIVED,
618
+ ]),
619
+ ],
620
+ [
621
+ CRYGOV_PROFILE_MATURITY_V2.ACTIVE,
622
+ new Set([
623
+ CRYGOV_PROFILE_MATURITY_V2.STALE,
624
+ CRYGOV_PROFILE_MATURITY_V2.ARCHIVED,
625
+ ]),
626
+ ],
627
+ [
628
+ CRYGOV_PROFILE_MATURITY_V2.STALE,
629
+ new Set([
630
+ CRYGOV_PROFILE_MATURITY_V2.ACTIVE,
631
+ CRYGOV_PROFILE_MATURITY_V2.ARCHIVED,
632
+ ]),
633
+ ],
634
+ [CRYGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
635
+ ]);
636
+ const _crygovPTerminal = new Set([CRYGOV_PROFILE_MATURITY_V2.ARCHIVED]);
637
+ const _crygovJTrans = new Map([
638
+ [
639
+ CRYGOV_ENCRYPT_LIFECYCLE_V2.QUEUED,
640
+ new Set([
641
+ CRYGOV_ENCRYPT_LIFECYCLE_V2.ENCRYPTING,
642
+ CRYGOV_ENCRYPT_LIFECYCLE_V2.CANCELLED,
643
+ ]),
644
+ ],
645
+ [
646
+ CRYGOV_ENCRYPT_LIFECYCLE_V2.ENCRYPTING,
647
+ new Set([
648
+ CRYGOV_ENCRYPT_LIFECYCLE_V2.ENCRYPTED,
649
+ CRYGOV_ENCRYPT_LIFECYCLE_V2.FAILED,
650
+ CRYGOV_ENCRYPT_LIFECYCLE_V2.CANCELLED,
651
+ ]),
652
+ ],
653
+ [CRYGOV_ENCRYPT_LIFECYCLE_V2.ENCRYPTED, new Set()],
654
+ [CRYGOV_ENCRYPT_LIFECYCLE_V2.FAILED, new Set()],
655
+ [CRYGOV_ENCRYPT_LIFECYCLE_V2.CANCELLED, new Set()],
656
+ ]);
657
+ const _crygovPsV2 = new Map();
658
+ const _crygovJsV2 = new Map();
659
+ let _crygovMaxActive = 8,
660
+ _crygovMaxPending = 20,
661
+ _crygovIdleMs = 2592000000,
662
+ _crygovStuckMs = 60 * 1000;
663
+ function _crygovPos(n, label) {
664
+ const v = Math.floor(Number(n));
665
+ if (!Number.isFinite(v) || v <= 0)
666
+ throw new Error(`${label} must be positive integer`);
667
+ return v;
668
+ }
669
+ function _crygovCheckP(from, to) {
670
+ const a = _crygovPTrans.get(from);
671
+ if (!a || !a.has(to))
672
+ throw new Error(`invalid crygov profile transition ${from} → ${to}`);
673
+ }
674
+ function _crygovCheckJ(from, to) {
675
+ const a = _crygovJTrans.get(from);
676
+ if (!a || !a.has(to))
677
+ throw new Error(`invalid crygov encrypt transition ${from} → ${to}`);
678
+ }
679
+ function _crygovCountActive(owner) {
680
+ let c = 0;
681
+ for (const p of _crygovPsV2.values())
682
+ if (p.owner === owner && p.status === CRYGOV_PROFILE_MATURITY_V2.ACTIVE)
683
+ c++;
684
+ return c;
685
+ }
686
+ function _crygovCountPending(profileId) {
687
+ let c = 0;
688
+ for (const j of _crygovJsV2.values())
689
+ if (
690
+ j.profileId === profileId &&
691
+ (j.status === CRYGOV_ENCRYPT_LIFECYCLE_V2.QUEUED ||
692
+ j.status === CRYGOV_ENCRYPT_LIFECYCLE_V2.ENCRYPTING)
693
+ )
694
+ c++;
695
+ return c;
696
+ }
697
+ export function setMaxActiveCryProfilesPerOwnerV2(n) {
698
+ _crygovMaxActive = _crygovPos(n, "maxActiveCryProfilesPerOwner");
699
+ }
700
+ export function getMaxActiveCryProfilesPerOwnerV2() {
701
+ return _crygovMaxActive;
702
+ }
703
+ export function setMaxPendingCryEncryptsPerProfileV2(n) {
704
+ _crygovMaxPending = _crygovPos(n, "maxPendingCryEncryptsPerProfile");
705
+ }
706
+ export function getMaxPendingCryEncryptsPerProfileV2() {
707
+ return _crygovMaxPending;
708
+ }
709
+ export function setCryProfileIdleMsV2(n) {
710
+ _crygovIdleMs = _crygovPos(n, "crygovProfileIdleMs");
711
+ }
712
+ export function getCryProfileIdleMsV2() {
713
+ return _crygovIdleMs;
714
+ }
715
+ export function setCryEncryptStuckMsV2(n) {
716
+ _crygovStuckMs = _crygovPos(n, "crygovEncryptStuckMs");
717
+ }
718
+ export function getCryEncryptStuckMsV2() {
719
+ return _crygovStuckMs;
720
+ }
721
+ export function _resetStateCrygovV2() {
722
+ _crygovPsV2.clear();
723
+ _crygovJsV2.clear();
724
+ _crygovMaxActive = 8;
725
+ _crygovMaxPending = 20;
726
+ _crygovIdleMs = 2592000000;
727
+ _crygovStuckMs = 60 * 1000;
728
+ }
729
+ export function registerCryProfileV2({ id, owner, provider, metadata } = {}) {
730
+ if (!id || !owner) throw new Error("id and owner required");
731
+ if (_crygovPsV2.has(id))
732
+ throw new Error(`crygov profile ${id} already exists`);
733
+ const now = Date.now();
734
+ const p = {
735
+ id,
736
+ owner,
737
+ provider: provider || "default",
738
+ status: CRYGOV_PROFILE_MATURITY_V2.PENDING,
739
+ createdAt: now,
740
+ updatedAt: now,
741
+ lastTouchedAt: now,
742
+ activatedAt: null,
743
+ archivedAt: null,
744
+ metadata: { ...(metadata || {}) },
745
+ };
746
+ _crygovPsV2.set(id, p);
747
+ return { ...p, metadata: { ...p.metadata } };
748
+ }
749
+ export function activateCryProfileV2(id) {
750
+ const p = _crygovPsV2.get(id);
751
+ if (!p) throw new Error(`crygov profile ${id} not found`);
752
+ const isInitial = p.status === CRYGOV_PROFILE_MATURITY_V2.PENDING;
753
+ _crygovCheckP(p.status, CRYGOV_PROFILE_MATURITY_V2.ACTIVE);
754
+ if (isInitial && _crygovCountActive(p.owner) >= _crygovMaxActive)
755
+ throw new Error(`max active crygov profiles for owner ${p.owner} reached`);
756
+ const now = Date.now();
757
+ p.status = CRYGOV_PROFILE_MATURITY_V2.ACTIVE;
758
+ p.updatedAt = now;
759
+ p.lastTouchedAt = now;
760
+ if (!p.activatedAt) p.activatedAt = now;
761
+ return { ...p, metadata: { ...p.metadata } };
762
+ }
763
+ export function staleCryProfileV2(id) {
764
+ const p = _crygovPsV2.get(id);
765
+ if (!p) throw new Error(`crygov profile ${id} not found`);
766
+ _crygovCheckP(p.status, CRYGOV_PROFILE_MATURITY_V2.STALE);
767
+ p.status = CRYGOV_PROFILE_MATURITY_V2.STALE;
768
+ p.updatedAt = Date.now();
769
+ return { ...p, metadata: { ...p.metadata } };
770
+ }
771
+ export function archiveCryProfileV2(id) {
772
+ const p = _crygovPsV2.get(id);
773
+ if (!p) throw new Error(`crygov profile ${id} not found`);
774
+ _crygovCheckP(p.status, CRYGOV_PROFILE_MATURITY_V2.ARCHIVED);
775
+ const now = Date.now();
776
+ p.status = CRYGOV_PROFILE_MATURITY_V2.ARCHIVED;
777
+ p.updatedAt = now;
778
+ if (!p.archivedAt) p.archivedAt = now;
779
+ return { ...p, metadata: { ...p.metadata } };
780
+ }
781
+ export function touchCryProfileV2(id) {
782
+ const p = _crygovPsV2.get(id);
783
+ if (!p) throw new Error(`crygov profile ${id} not found`);
784
+ if (_crygovPTerminal.has(p.status))
785
+ throw new Error(`cannot touch terminal crygov profile ${id}`);
786
+ const now = Date.now();
787
+ p.lastTouchedAt = now;
788
+ p.updatedAt = now;
789
+ return { ...p, metadata: { ...p.metadata } };
790
+ }
791
+ export function getCryProfileV2(id) {
792
+ const p = _crygovPsV2.get(id);
793
+ if (!p) return null;
794
+ return { ...p, metadata: { ...p.metadata } };
795
+ }
796
+ export function listCryProfilesV2() {
797
+ return [..._crygovPsV2.values()].map((p) => ({
798
+ ...p,
799
+ metadata: { ...p.metadata },
800
+ }));
801
+ }
802
+ export function createCryEncryptV2({ id, profileId, keyId, metadata } = {}) {
803
+ if (!id || !profileId) throw new Error("id and profileId required");
804
+ if (_crygovJsV2.has(id))
805
+ throw new Error(`crygov encrypt ${id} already exists`);
806
+ if (!_crygovPsV2.has(profileId))
807
+ throw new Error(`crygov profile ${profileId} not found`);
808
+ if (_crygovCountPending(profileId) >= _crygovMaxPending)
809
+ throw new Error(
810
+ `max pending crygov encrypts for profile ${profileId} reached`,
811
+ );
812
+ const now = Date.now();
813
+ const j = {
814
+ id,
815
+ profileId,
816
+ keyId: keyId || "",
817
+ status: CRYGOV_ENCRYPT_LIFECYCLE_V2.QUEUED,
818
+ createdAt: now,
819
+ updatedAt: now,
820
+ startedAt: null,
821
+ settledAt: null,
822
+ metadata: { ...(metadata || {}) },
823
+ };
824
+ _crygovJsV2.set(id, j);
825
+ return { ...j, metadata: { ...j.metadata } };
826
+ }
827
+ export function encryptingCryEncryptV2(id) {
828
+ const j = _crygovJsV2.get(id);
829
+ if (!j) throw new Error(`crygov encrypt ${id} not found`);
830
+ _crygovCheckJ(j.status, CRYGOV_ENCRYPT_LIFECYCLE_V2.ENCRYPTING);
831
+ const now = Date.now();
832
+ j.status = CRYGOV_ENCRYPT_LIFECYCLE_V2.ENCRYPTING;
833
+ j.updatedAt = now;
834
+ if (!j.startedAt) j.startedAt = now;
835
+ return { ...j, metadata: { ...j.metadata } };
836
+ }
837
+ export function completeEncryptCryV2(id) {
838
+ const j = _crygovJsV2.get(id);
839
+ if (!j) throw new Error(`crygov encrypt ${id} not found`);
840
+ _crygovCheckJ(j.status, CRYGOV_ENCRYPT_LIFECYCLE_V2.ENCRYPTED);
841
+ const now = Date.now();
842
+ j.status = CRYGOV_ENCRYPT_LIFECYCLE_V2.ENCRYPTED;
843
+ j.updatedAt = now;
844
+ if (!j.settledAt) j.settledAt = now;
845
+ return { ...j, metadata: { ...j.metadata } };
846
+ }
847
+ export function failCryEncryptV2(id, reason) {
848
+ const j = _crygovJsV2.get(id);
849
+ if (!j) throw new Error(`crygov encrypt ${id} not found`);
850
+ _crygovCheckJ(j.status, CRYGOV_ENCRYPT_LIFECYCLE_V2.FAILED);
851
+ const now = Date.now();
852
+ j.status = CRYGOV_ENCRYPT_LIFECYCLE_V2.FAILED;
853
+ j.updatedAt = now;
854
+ if (!j.settledAt) j.settledAt = now;
855
+ if (reason) j.metadata.failReason = String(reason);
856
+ return { ...j, metadata: { ...j.metadata } };
857
+ }
858
+ export function cancelCryEncryptV2(id, reason) {
859
+ const j = _crygovJsV2.get(id);
860
+ if (!j) throw new Error(`crygov encrypt ${id} not found`);
861
+ _crygovCheckJ(j.status, CRYGOV_ENCRYPT_LIFECYCLE_V2.CANCELLED);
862
+ const now = Date.now();
863
+ j.status = CRYGOV_ENCRYPT_LIFECYCLE_V2.CANCELLED;
864
+ j.updatedAt = now;
865
+ if (!j.settledAt) j.settledAt = now;
866
+ if (reason) j.metadata.cancelReason = String(reason);
867
+ return { ...j, metadata: { ...j.metadata } };
868
+ }
869
+ export function getCryEncryptV2(id) {
870
+ const j = _crygovJsV2.get(id);
871
+ if (!j) return null;
872
+ return { ...j, metadata: { ...j.metadata } };
873
+ }
874
+ export function listCryEncryptsV2() {
875
+ return [..._crygovJsV2.values()].map((j) => ({
876
+ ...j,
877
+ metadata: { ...j.metadata },
878
+ }));
879
+ }
880
+ export function autoStaleIdleCryProfilesV2({ now } = {}) {
881
+ const t = now ?? Date.now();
882
+ const flipped = [];
883
+ for (const p of _crygovPsV2.values())
884
+ if (
885
+ p.status === CRYGOV_PROFILE_MATURITY_V2.ACTIVE &&
886
+ t - p.lastTouchedAt >= _crygovIdleMs
887
+ ) {
888
+ p.status = CRYGOV_PROFILE_MATURITY_V2.STALE;
889
+ p.updatedAt = t;
890
+ flipped.push(p.id);
891
+ }
892
+ return { flipped, count: flipped.length };
893
+ }
894
+ export function autoFailStuckCryEncryptsV2({ now } = {}) {
895
+ const t = now ?? Date.now();
896
+ const flipped = [];
897
+ for (const j of _crygovJsV2.values())
898
+ if (
899
+ j.status === CRYGOV_ENCRYPT_LIFECYCLE_V2.ENCRYPTING &&
900
+ j.startedAt != null &&
901
+ t - j.startedAt >= _crygovStuckMs
902
+ ) {
903
+ j.status = CRYGOV_ENCRYPT_LIFECYCLE_V2.FAILED;
904
+ j.updatedAt = t;
905
+ if (!j.settledAt) j.settledAt = t;
906
+ j.metadata.failReason = "auto-fail-stuck";
907
+ flipped.push(j.id);
908
+ }
909
+ return { flipped, count: flipped.length };
910
+ }
911
+ export function getCrygovStatsV2() {
912
+ const profilesByStatus = {};
913
+ for (const v of Object.values(CRYGOV_PROFILE_MATURITY_V2))
914
+ profilesByStatus[v] = 0;
915
+ for (const p of _crygovPsV2.values()) profilesByStatus[p.status]++;
916
+ const encryptsByStatus = {};
917
+ for (const v of Object.values(CRYGOV_ENCRYPT_LIFECYCLE_V2))
918
+ encryptsByStatus[v] = 0;
919
+ for (const j of _crygovJsV2.values()) encryptsByStatus[j.status]++;
920
+ return {
921
+ totalCryProfilesV2: _crygovPsV2.size,
922
+ totalCryEncryptsV2: _crygovJsV2.size,
923
+ maxActiveCryProfilesPerOwner: _crygovMaxActive,
924
+ maxPendingCryEncryptsPerProfile: _crygovMaxPending,
925
+ crygovProfileIdleMs: _crygovIdleMs,
926
+ crygovEncryptStuckMs: _crygovStuckMs,
927
+ profilesByStatus,
928
+ encryptsByStatus,
929
+ };
930
+ }
@@ -1202,3 +1202,342 @@ export function getDaoGovernanceGovStatsV2() {
1202
1202
  proposalsByStatus,
1203
1203
  };
1204
1204
  }
1205
+
1206
+ // === Iter28 V2 governance overlay: Daomgov ===
1207
+ export const DAOMGOV_PROFILE_MATURITY_V2 = Object.freeze({
1208
+ PENDING: "pending",
1209
+ ACTIVE: "active",
1210
+ PAUSED: "paused",
1211
+ ARCHIVED: "archived",
1212
+ });
1213
+ export const DAOMGOV_PROPOSAL_LIFECYCLE_V2 = Object.freeze({
1214
+ QUEUED: "queued",
1215
+ VOTING: "voting",
1216
+ RESOLVED: "resolved",
1217
+ FAILED: "failed",
1218
+ CANCELLED: "cancelled",
1219
+ });
1220
+ const _daomgovPTrans = new Map([
1221
+ [
1222
+ DAOMGOV_PROFILE_MATURITY_V2.PENDING,
1223
+ new Set([
1224
+ DAOMGOV_PROFILE_MATURITY_V2.ACTIVE,
1225
+ DAOMGOV_PROFILE_MATURITY_V2.ARCHIVED,
1226
+ ]),
1227
+ ],
1228
+ [
1229
+ DAOMGOV_PROFILE_MATURITY_V2.ACTIVE,
1230
+ new Set([
1231
+ DAOMGOV_PROFILE_MATURITY_V2.PAUSED,
1232
+ DAOMGOV_PROFILE_MATURITY_V2.ARCHIVED,
1233
+ ]),
1234
+ ],
1235
+ [
1236
+ DAOMGOV_PROFILE_MATURITY_V2.PAUSED,
1237
+ new Set([
1238
+ DAOMGOV_PROFILE_MATURITY_V2.ACTIVE,
1239
+ DAOMGOV_PROFILE_MATURITY_V2.ARCHIVED,
1240
+ ]),
1241
+ ],
1242
+ [DAOMGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1243
+ ]);
1244
+ const _daomgovPTerminal = new Set([DAOMGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1245
+ const _daomgovJTrans = new Map([
1246
+ [
1247
+ DAOMGOV_PROPOSAL_LIFECYCLE_V2.QUEUED,
1248
+ new Set([
1249
+ DAOMGOV_PROPOSAL_LIFECYCLE_V2.VOTING,
1250
+ DAOMGOV_PROPOSAL_LIFECYCLE_V2.CANCELLED,
1251
+ ]),
1252
+ ],
1253
+ [
1254
+ DAOMGOV_PROPOSAL_LIFECYCLE_V2.VOTING,
1255
+ new Set([
1256
+ DAOMGOV_PROPOSAL_LIFECYCLE_V2.RESOLVED,
1257
+ DAOMGOV_PROPOSAL_LIFECYCLE_V2.FAILED,
1258
+ DAOMGOV_PROPOSAL_LIFECYCLE_V2.CANCELLED,
1259
+ ]),
1260
+ ],
1261
+ [DAOMGOV_PROPOSAL_LIFECYCLE_V2.RESOLVED, new Set()],
1262
+ [DAOMGOV_PROPOSAL_LIFECYCLE_V2.FAILED, new Set()],
1263
+ [DAOMGOV_PROPOSAL_LIFECYCLE_V2.CANCELLED, new Set()],
1264
+ ]);
1265
+ const _daomgovPsV2 = new Map();
1266
+ const _daomgovJsV2 = new Map();
1267
+ let _daomgovMaxActive = 6,
1268
+ _daomgovMaxPending = 15,
1269
+ _daomgovIdleMs = 2592000000,
1270
+ _daomgovStuckMs = 60 * 1000;
1271
+ function _daomgovPos(n, label) {
1272
+ const v = Math.floor(Number(n));
1273
+ if (!Number.isFinite(v) || v <= 0)
1274
+ throw new Error(`${label} must be positive integer`);
1275
+ return v;
1276
+ }
1277
+ function _daomgovCheckP(from, to) {
1278
+ const a = _daomgovPTrans.get(from);
1279
+ if (!a || !a.has(to))
1280
+ throw new Error(`invalid daomgov profile transition ${from} → ${to}`);
1281
+ }
1282
+ function _daomgovCheckJ(from, to) {
1283
+ const a = _daomgovJTrans.get(from);
1284
+ if (!a || !a.has(to))
1285
+ throw new Error(`invalid daomgov proposal transition ${from} → ${to}`);
1286
+ }
1287
+ function _daomgovCountActive(owner) {
1288
+ let c = 0;
1289
+ for (const p of _daomgovPsV2.values())
1290
+ if (p.owner === owner && p.status === DAOMGOV_PROFILE_MATURITY_V2.ACTIVE)
1291
+ c++;
1292
+ return c;
1293
+ }
1294
+ function _daomgovCountPending(profileId) {
1295
+ let c = 0;
1296
+ for (const j of _daomgovJsV2.values())
1297
+ if (
1298
+ j.profileId === profileId &&
1299
+ (j.status === DAOMGOV_PROPOSAL_LIFECYCLE_V2.QUEUED ||
1300
+ j.status === DAOMGOV_PROPOSAL_LIFECYCLE_V2.VOTING)
1301
+ )
1302
+ c++;
1303
+ return c;
1304
+ }
1305
+ export function setMaxActiveDaomProfilesPerOwnerV2(n) {
1306
+ _daomgovMaxActive = _daomgovPos(n, "maxActiveDaomProfilesPerOwner");
1307
+ }
1308
+ export function getMaxActiveDaomProfilesPerOwnerV2() {
1309
+ return _daomgovMaxActive;
1310
+ }
1311
+ export function setMaxPendingDaomProposalsPerProfileV2(n) {
1312
+ _daomgovMaxPending = _daomgovPos(n, "maxPendingDaomProposalsPerProfile");
1313
+ }
1314
+ export function getMaxPendingDaomProposalsPerProfileV2() {
1315
+ return _daomgovMaxPending;
1316
+ }
1317
+ export function setDaomProfileIdleMsV2(n) {
1318
+ _daomgovIdleMs = _daomgovPos(n, "daomgovProfileIdleMs");
1319
+ }
1320
+ export function getDaomProfileIdleMsV2() {
1321
+ return _daomgovIdleMs;
1322
+ }
1323
+ export function setDaomProposalStuckMsV2(n) {
1324
+ _daomgovStuckMs = _daomgovPos(n, "daomgovProposalStuckMs");
1325
+ }
1326
+ export function getDaomProposalStuckMsV2() {
1327
+ return _daomgovStuckMs;
1328
+ }
1329
+ export function _resetStateDaomgovV2() {
1330
+ _daomgovPsV2.clear();
1331
+ _daomgovJsV2.clear();
1332
+ _daomgovMaxActive = 6;
1333
+ _daomgovMaxPending = 15;
1334
+ _daomgovIdleMs = 2592000000;
1335
+ _daomgovStuckMs = 60 * 1000;
1336
+ }
1337
+ export function registerDaomProfileV2({ id, owner, realm, metadata } = {}) {
1338
+ if (!id || !owner) throw new Error("id and owner required");
1339
+ if (_daomgovPsV2.has(id))
1340
+ throw new Error(`daomgov profile ${id} already exists`);
1341
+ const now = Date.now();
1342
+ const p = {
1343
+ id,
1344
+ owner,
1345
+ realm: realm || "default",
1346
+ status: DAOMGOV_PROFILE_MATURITY_V2.PENDING,
1347
+ createdAt: now,
1348
+ updatedAt: now,
1349
+ lastTouchedAt: now,
1350
+ activatedAt: null,
1351
+ archivedAt: null,
1352
+ metadata: { ...(metadata || {}) },
1353
+ };
1354
+ _daomgovPsV2.set(id, p);
1355
+ return { ...p, metadata: { ...p.metadata } };
1356
+ }
1357
+ export function activateDaomProfileV2(id) {
1358
+ const p = _daomgovPsV2.get(id);
1359
+ if (!p) throw new Error(`daomgov profile ${id} not found`);
1360
+ const isInitial = p.status === DAOMGOV_PROFILE_MATURITY_V2.PENDING;
1361
+ _daomgovCheckP(p.status, DAOMGOV_PROFILE_MATURITY_V2.ACTIVE);
1362
+ if (isInitial && _daomgovCountActive(p.owner) >= _daomgovMaxActive)
1363
+ throw new Error(`max active daomgov profiles for owner ${p.owner} reached`);
1364
+ const now = Date.now();
1365
+ p.status = DAOMGOV_PROFILE_MATURITY_V2.ACTIVE;
1366
+ p.updatedAt = now;
1367
+ p.lastTouchedAt = now;
1368
+ if (!p.activatedAt) p.activatedAt = now;
1369
+ return { ...p, metadata: { ...p.metadata } };
1370
+ }
1371
+ export function pausedDaomProfileV2(id) {
1372
+ const p = _daomgovPsV2.get(id);
1373
+ if (!p) throw new Error(`daomgov profile ${id} not found`);
1374
+ _daomgovCheckP(p.status, DAOMGOV_PROFILE_MATURITY_V2.PAUSED);
1375
+ p.status = DAOMGOV_PROFILE_MATURITY_V2.PAUSED;
1376
+ p.updatedAt = Date.now();
1377
+ return { ...p, metadata: { ...p.metadata } };
1378
+ }
1379
+ export function archiveDaomProfileV2(id) {
1380
+ const p = _daomgovPsV2.get(id);
1381
+ if (!p) throw new Error(`daomgov profile ${id} not found`);
1382
+ _daomgovCheckP(p.status, DAOMGOV_PROFILE_MATURITY_V2.ARCHIVED);
1383
+ const now = Date.now();
1384
+ p.status = DAOMGOV_PROFILE_MATURITY_V2.ARCHIVED;
1385
+ p.updatedAt = now;
1386
+ if (!p.archivedAt) p.archivedAt = now;
1387
+ return { ...p, metadata: { ...p.metadata } };
1388
+ }
1389
+ export function touchDaomProfileV2(id) {
1390
+ const p = _daomgovPsV2.get(id);
1391
+ if (!p) throw new Error(`daomgov profile ${id} not found`);
1392
+ if (_daomgovPTerminal.has(p.status))
1393
+ throw new Error(`cannot touch terminal daomgov profile ${id}`);
1394
+ const now = Date.now();
1395
+ p.lastTouchedAt = now;
1396
+ p.updatedAt = now;
1397
+ return { ...p, metadata: { ...p.metadata } };
1398
+ }
1399
+ export function getDaomProfileV2(id) {
1400
+ const p = _daomgovPsV2.get(id);
1401
+ if (!p) return null;
1402
+ return { ...p, metadata: { ...p.metadata } };
1403
+ }
1404
+ export function listDaomProfilesV2() {
1405
+ return [..._daomgovPsV2.values()].map((p) => ({
1406
+ ...p,
1407
+ metadata: { ...p.metadata },
1408
+ }));
1409
+ }
1410
+ export function createDaomProposalV2({
1411
+ id,
1412
+ profileId,
1413
+ proposalId,
1414
+ metadata,
1415
+ } = {}) {
1416
+ if (!id || !profileId) throw new Error("id and profileId required");
1417
+ if (_daomgovJsV2.has(id))
1418
+ throw new Error(`daomgov proposal ${id} already exists`);
1419
+ if (!_daomgovPsV2.has(profileId))
1420
+ throw new Error(`daomgov profile ${profileId} not found`);
1421
+ if (_daomgovCountPending(profileId) >= _daomgovMaxPending)
1422
+ throw new Error(
1423
+ `max pending daomgov proposals for profile ${profileId} reached`,
1424
+ );
1425
+ const now = Date.now();
1426
+ const j = {
1427
+ id,
1428
+ profileId,
1429
+ proposalId: proposalId || "",
1430
+ status: DAOMGOV_PROPOSAL_LIFECYCLE_V2.QUEUED,
1431
+ createdAt: now,
1432
+ updatedAt: now,
1433
+ startedAt: null,
1434
+ settledAt: null,
1435
+ metadata: { ...(metadata || {}) },
1436
+ };
1437
+ _daomgovJsV2.set(id, j);
1438
+ return { ...j, metadata: { ...j.metadata } };
1439
+ }
1440
+ export function votingDaomProposalV2(id) {
1441
+ const j = _daomgovJsV2.get(id);
1442
+ if (!j) throw new Error(`daomgov proposal ${id} not found`);
1443
+ _daomgovCheckJ(j.status, DAOMGOV_PROPOSAL_LIFECYCLE_V2.VOTING);
1444
+ const now = Date.now();
1445
+ j.status = DAOMGOV_PROPOSAL_LIFECYCLE_V2.VOTING;
1446
+ j.updatedAt = now;
1447
+ if (!j.startedAt) j.startedAt = now;
1448
+ return { ...j, metadata: { ...j.metadata } };
1449
+ }
1450
+ export function completeProposalDaomV2(id) {
1451
+ const j = _daomgovJsV2.get(id);
1452
+ if (!j) throw new Error(`daomgov proposal ${id} not found`);
1453
+ _daomgovCheckJ(j.status, DAOMGOV_PROPOSAL_LIFECYCLE_V2.RESOLVED);
1454
+ const now = Date.now();
1455
+ j.status = DAOMGOV_PROPOSAL_LIFECYCLE_V2.RESOLVED;
1456
+ j.updatedAt = now;
1457
+ if (!j.settledAt) j.settledAt = now;
1458
+ return { ...j, metadata: { ...j.metadata } };
1459
+ }
1460
+ export function failDaomProposalV2(id, reason) {
1461
+ const j = _daomgovJsV2.get(id);
1462
+ if (!j) throw new Error(`daomgov proposal ${id} not found`);
1463
+ _daomgovCheckJ(j.status, DAOMGOV_PROPOSAL_LIFECYCLE_V2.FAILED);
1464
+ const now = Date.now();
1465
+ j.status = DAOMGOV_PROPOSAL_LIFECYCLE_V2.FAILED;
1466
+ j.updatedAt = now;
1467
+ if (!j.settledAt) j.settledAt = now;
1468
+ if (reason) j.metadata.failReason = String(reason);
1469
+ return { ...j, metadata: { ...j.metadata } };
1470
+ }
1471
+ export function cancelDaomProposalV2(id, reason) {
1472
+ const j = _daomgovJsV2.get(id);
1473
+ if (!j) throw new Error(`daomgov proposal ${id} not found`);
1474
+ _daomgovCheckJ(j.status, DAOMGOV_PROPOSAL_LIFECYCLE_V2.CANCELLED);
1475
+ const now = Date.now();
1476
+ j.status = DAOMGOV_PROPOSAL_LIFECYCLE_V2.CANCELLED;
1477
+ j.updatedAt = now;
1478
+ if (!j.settledAt) j.settledAt = now;
1479
+ if (reason) j.metadata.cancelReason = String(reason);
1480
+ return { ...j, metadata: { ...j.metadata } };
1481
+ }
1482
+ export function getDaomProposalV2(id) {
1483
+ const j = _daomgovJsV2.get(id);
1484
+ if (!j) return null;
1485
+ return { ...j, metadata: { ...j.metadata } };
1486
+ }
1487
+ export function listDaomProposalsV2() {
1488
+ return [..._daomgovJsV2.values()].map((j) => ({
1489
+ ...j,
1490
+ metadata: { ...j.metadata },
1491
+ }));
1492
+ }
1493
+ export function autoPausedIdleDaomProfilesV2({ now } = {}) {
1494
+ const t = now ?? Date.now();
1495
+ const flipped = [];
1496
+ for (const p of _daomgovPsV2.values())
1497
+ if (
1498
+ p.status === DAOMGOV_PROFILE_MATURITY_V2.ACTIVE &&
1499
+ t - p.lastTouchedAt >= _daomgovIdleMs
1500
+ ) {
1501
+ p.status = DAOMGOV_PROFILE_MATURITY_V2.PAUSED;
1502
+ p.updatedAt = t;
1503
+ flipped.push(p.id);
1504
+ }
1505
+ return { flipped, count: flipped.length };
1506
+ }
1507
+ export function autoFailStuckDaomProposalsV2({ now } = {}) {
1508
+ const t = now ?? Date.now();
1509
+ const flipped = [];
1510
+ for (const j of _daomgovJsV2.values())
1511
+ if (
1512
+ j.status === DAOMGOV_PROPOSAL_LIFECYCLE_V2.VOTING &&
1513
+ j.startedAt != null &&
1514
+ t - j.startedAt >= _daomgovStuckMs
1515
+ ) {
1516
+ j.status = DAOMGOV_PROPOSAL_LIFECYCLE_V2.FAILED;
1517
+ j.updatedAt = t;
1518
+ if (!j.settledAt) j.settledAt = t;
1519
+ j.metadata.failReason = "auto-fail-stuck";
1520
+ flipped.push(j.id);
1521
+ }
1522
+ return { flipped, count: flipped.length };
1523
+ }
1524
+ export function getDaomgovStatsV2() {
1525
+ const profilesByStatus = {};
1526
+ for (const v of Object.values(DAOMGOV_PROFILE_MATURITY_V2))
1527
+ profilesByStatus[v] = 0;
1528
+ for (const p of _daomgovPsV2.values()) profilesByStatus[p.status]++;
1529
+ const proposalsByStatus = {};
1530
+ for (const v of Object.values(DAOMGOV_PROPOSAL_LIFECYCLE_V2))
1531
+ proposalsByStatus[v] = 0;
1532
+ for (const j of _daomgovJsV2.values()) proposalsByStatus[j.status]++;
1533
+ return {
1534
+ totalDaomProfilesV2: _daomgovPsV2.size,
1535
+ totalDaomProposalsV2: _daomgovJsV2.size,
1536
+ maxActiveDaomProfilesPerOwner: _daomgovMaxActive,
1537
+ maxPendingDaomProposalsPerProfile: _daomgovMaxPending,
1538
+ daomgovProfileIdleMs: _daomgovIdleMs,
1539
+ daomgovProposalStuckMs: _daomgovStuckMs,
1540
+ profilesByStatus,
1541
+ proposalsByStatus,
1542
+ };
1543
+ }