chainlesschain 0.132.0 → 0.145.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +230 -0
  3. package/src/commands/activitypub.js +191 -0
  4. package/src/commands/agent.js +601 -0
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +186 -0
  7. package/src/commands/bm25.js +162 -0
  8. package/src/commands/browse.js +225 -0
  9. package/src/commands/ccron.js +178 -0
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +420 -0
  12. package/src/commands/compt.js +176 -0
  13. package/src/commands/consol.js +237 -0
  14. package/src/commands/cowork.js +588 -0
  15. package/src/commands/crosschain.js +216 -0
  16. package/src/commands/dao.js +216 -0
  17. package/src/commands/dlp.js +206 -0
  18. package/src/commands/economy.js +211 -0
  19. package/src/commands/evolution.js +209 -0
  20. package/src/commands/evomap.js +216 -0
  21. package/src/commands/fflag.js +230 -0
  22. package/src/commands/git.js +185 -0
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +210 -0
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +178 -0
  27. package/src/commands/itbudget.js +161 -0
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +201 -0
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +214 -0
  32. package/src/commands/mcpscaf.js +153 -0
  33. package/src/commands/meminj.js +153 -0
  34. package/src/commands/nostr.js +213 -0
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +156 -0
  37. package/src/commands/pdfp.js +160 -0
  38. package/src/commands/perf.js +176 -0
  39. package/src/commands/perm.js +156 -0
  40. package/src/commands/pipeline.js +211 -0
  41. package/src/commands/planmode.js +154 -0
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +166 -0
  44. package/src/commands/recommend.js +185 -0
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +153 -0
  48. package/src/commands/seshsearch.js +149 -0
  49. package/src/commands/seshtail.js +152 -0
  50. package/src/commands/seshu.js +160 -0
  51. package/src/commands/sganal.js +172 -0
  52. package/src/commands/siem.js +207 -0
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +154 -0
  55. package/src/commands/social.js +159 -0
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +157 -0
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +183 -0
  60. package/src/commands/topiccls.js +158 -0
  61. package/src/commands/uprof.js +154 -0
  62. package/src/commands/vcheck.js +172 -0
  63. package/src/commands/webfetch.js +150 -0
  64. package/src/commands/zkp.js +218 -0
  65. package/src/harness/prompt-compressor.js +331 -0
  66. package/src/index.js +101 -1
  67. package/src/lib/a2a-protocol.js +373 -0
  68. package/src/lib/activitypub-bridge.js +343 -0
  69. package/src/lib/agent-economy.js +358 -0
  70. package/src/lib/app-builder.js +338 -0
  71. package/src/lib/audit-logger.js +321 -0
  72. package/src/lib/autonomous-agent.js +341 -0
  73. package/src/lib/bi-engine.js +339 -0
  74. package/src/lib/bm25-search.js +333 -0
  75. package/src/lib/browser-automation.js +352 -0
  76. package/src/lib/chat-core.js +336 -0
  77. package/src/lib/claude-code-bridge.js +341 -0
  78. package/src/lib/compliance-framework-reporter.js +359 -0
  79. package/src/lib/compliance-manager.js +330 -0
  80. package/src/lib/compression-telemetry.js +333 -0
  81. package/src/lib/content-recommender.js +370 -0
  82. package/src/lib/cowork-cron.js +330 -0
  83. package/src/lib/cowork-learning.js +333 -0
  84. package/src/lib/cowork-task-runner.js +362 -0
  85. package/src/lib/cowork-workflow.js +327 -0
  86. package/src/lib/cross-chain.js +365 -0
  87. package/src/lib/dao-governance.js +339 -0
  88. package/src/lib/dlp-engine.js +343 -0
  89. package/src/lib/evolution-system.js +336 -0
  90. package/src/lib/evomap-manager.js +339 -0
  91. package/src/lib/execution-backend.js +351 -0
  92. package/src/lib/feature-flags.js +330 -0
  93. package/src/lib/git-integration.js +343 -0
  94. package/src/lib/hardening-manager.js +341 -0
  95. package/src/lib/hierarchical-memory.js +341 -0
  96. package/src/lib/inference-network.js +362 -0
  97. package/src/lib/iteration-budget.js +357 -0
  98. package/src/lib/knowledge-graph.js +333 -0
  99. package/src/lib/matrix-bridge.js +339 -0
  100. package/src/lib/mcp-scaffold.js +345 -0
  101. package/src/lib/memory-injection.js +320 -0
  102. package/src/lib/nostr-bridge.js +342 -0
  103. package/src/lib/orchestrator.js +350 -0
  104. package/src/lib/pdf-parser.js +330 -0
  105. package/src/lib/perf-tuning.js +364 -0
  106. package/src/lib/permission-engine.js +319 -0
  107. package/src/lib/pipeline-orchestrator.js +345 -0
  108. package/src/lib/plan-mode.js +328 -0
  109. package/src/lib/privacy-computing.js +335 -0
  110. package/src/lib/prompt-compressor.js +1 -10
  111. package/src/lib/reputation-optimizer.js +340 -0
  112. package/src/lib/sandbox-v2.js +327 -0
  113. package/src/lib/service-container.js +342 -0
  114. package/src/lib/session-consolidator.js +352 -0
  115. package/src/lib/session-hooks.js +340 -0
  116. package/src/lib/session-search.js +334 -0
  117. package/src/lib/session-tail.js +320 -0
  118. package/src/lib/session-usage.js +329 -0
  119. package/src/lib/siem-exporter.js +352 -0
  120. package/src/lib/skill-marketplace.js +345 -0
  121. package/src/lib/sla-manager.js +341 -0
  122. package/src/lib/slot-filler.js +333 -0
  123. package/src/lib/social-graph-analytics.js +327 -0
  124. package/src/lib/social-graph.js +304 -0
  125. package/src/lib/stress-tester.js +342 -0
  126. package/src/lib/sub-agent-registry.js +359 -0
  127. package/src/lib/task-model-selector.js +333 -0
  128. package/src/lib/terraform-manager.js +333 -0
  129. package/src/lib/todo-manager.js +339 -0
  130. package/src/lib/token-incentive.js +341 -0
  131. package/src/lib/topic-classifier.js +353 -0
  132. package/src/lib/user-profile.js +325 -0
  133. package/src/lib/version-checker.js +335 -0
  134. package/src/lib/web-fetch.js +322 -0
  135. package/src/lib/zkp-engine.js +342 -0
@@ -691,3 +691,346 @@ export function getHighestUnresolvedSeverity() {
691
691
  }
692
692
 
693
693
  export { _v2PolicyMeta, _v2IncidentMeta, _builtinPolicyTemplates };
694
+
695
+ // ===== V2 Surface: DLP Engine governance overlay (CLI v0.135.0) =====
696
+ export const DLP_POLICY_MATURITY_V2 = Object.freeze({
697
+ PENDING: "pending",
698
+ ACTIVE: "active",
699
+ SUSPENDED: "suspended",
700
+ RETIRED: "retired",
701
+ });
702
+ export const DLP_SCAN_LIFECYCLE_V2 = Object.freeze({
703
+ QUEUED: "queued",
704
+ SCANNING: "scanning",
705
+ COMPLETED: "completed",
706
+ FAILED: "failed",
707
+ CANCELLED: "cancelled",
708
+ });
709
+
710
+ const _dlpPolTrans = new Map([
711
+ [
712
+ DLP_POLICY_MATURITY_V2.PENDING,
713
+ new Set([DLP_POLICY_MATURITY_V2.ACTIVE, DLP_POLICY_MATURITY_V2.RETIRED]),
714
+ ],
715
+ [
716
+ DLP_POLICY_MATURITY_V2.ACTIVE,
717
+ new Set([DLP_POLICY_MATURITY_V2.SUSPENDED, DLP_POLICY_MATURITY_V2.RETIRED]),
718
+ ],
719
+ [
720
+ DLP_POLICY_MATURITY_V2.SUSPENDED,
721
+ new Set([DLP_POLICY_MATURITY_V2.ACTIVE, DLP_POLICY_MATURITY_V2.RETIRED]),
722
+ ],
723
+ [DLP_POLICY_MATURITY_V2.RETIRED, new Set()],
724
+ ]);
725
+ const _dlpPolTerminal = new Set([DLP_POLICY_MATURITY_V2.RETIRED]);
726
+ const _dlpScanTrans = new Map([
727
+ [
728
+ DLP_SCAN_LIFECYCLE_V2.QUEUED,
729
+ new Set([DLP_SCAN_LIFECYCLE_V2.SCANNING, DLP_SCAN_LIFECYCLE_V2.CANCELLED]),
730
+ ],
731
+ [
732
+ DLP_SCAN_LIFECYCLE_V2.SCANNING,
733
+ new Set([
734
+ DLP_SCAN_LIFECYCLE_V2.COMPLETED,
735
+ DLP_SCAN_LIFECYCLE_V2.FAILED,
736
+ DLP_SCAN_LIFECYCLE_V2.CANCELLED,
737
+ ]),
738
+ ],
739
+ [DLP_SCAN_LIFECYCLE_V2.COMPLETED, new Set()],
740
+ [DLP_SCAN_LIFECYCLE_V2.FAILED, new Set()],
741
+ [DLP_SCAN_LIFECYCLE_V2.CANCELLED, new Set()],
742
+ ]);
743
+
744
+ const _dlpPols = new Map();
745
+ const _dlpScans = new Map();
746
+ let _dlpMaxActivePerOwner = 16;
747
+ let _dlpMaxPendingPerPol = 20;
748
+ let _dlpPolIdleMs = 12 * 60 * 60 * 1000;
749
+ let _dlpScanStuckMs = 5 * 60 * 1000;
750
+
751
+ function _dlpPos(n, lbl) {
752
+ const v = Math.floor(Number(n));
753
+ if (!Number.isFinite(v) || v <= 0)
754
+ throw new Error(`${lbl} must be positive integer`);
755
+ return v;
756
+ }
757
+
758
+ export function setMaxActiveDlpPoliciesPerOwnerV2(n) {
759
+ _dlpMaxActivePerOwner = _dlpPos(n, "maxActiveDlpPoliciesPerOwner");
760
+ }
761
+ export function getMaxActiveDlpPoliciesPerOwnerV2() {
762
+ return _dlpMaxActivePerOwner;
763
+ }
764
+ export function setMaxPendingDlpScansPerPolicyV2(n) {
765
+ _dlpMaxPendingPerPol = _dlpPos(n, "maxPendingDlpScansPerPolicy");
766
+ }
767
+ export function getMaxPendingDlpScansPerPolicyV2() {
768
+ return _dlpMaxPendingPerPol;
769
+ }
770
+ export function setDlpPolicyIdleMsV2(n) {
771
+ _dlpPolIdleMs = _dlpPos(n, "dlpPolicyIdleMs");
772
+ }
773
+ export function getDlpPolicyIdleMsV2() {
774
+ return _dlpPolIdleMs;
775
+ }
776
+ export function setDlpScanStuckMsV2(n) {
777
+ _dlpScanStuckMs = _dlpPos(n, "dlpScanStuckMs");
778
+ }
779
+ export function getDlpScanStuckMsV2() {
780
+ return _dlpScanStuckMs;
781
+ }
782
+
783
+ export function _resetStateDlpEngineV2() {
784
+ _dlpPols.clear();
785
+ _dlpScans.clear();
786
+ _dlpMaxActivePerOwner = 16;
787
+ _dlpMaxPendingPerPol = 20;
788
+ _dlpPolIdleMs = 12 * 60 * 60 * 1000;
789
+ _dlpScanStuckMs = 5 * 60 * 1000;
790
+ }
791
+
792
+ export function registerDlpPolicyV2({
793
+ id,
794
+ owner,
795
+ classification,
796
+ metadata,
797
+ } = {}) {
798
+ if (!id || typeof id !== "string") throw new Error("id is required");
799
+ if (!owner || typeof owner !== "string") throw new Error("owner is required");
800
+ if (_dlpPols.has(id)) throw new Error(`dlp policy ${id} already registered`);
801
+ const now = Date.now();
802
+ const p = {
803
+ id,
804
+ owner,
805
+ classification: classification || "internal",
806
+ status: DLP_POLICY_MATURITY_V2.PENDING,
807
+ createdAt: now,
808
+ updatedAt: now,
809
+ activatedAt: null,
810
+ retiredAt: null,
811
+ lastTouchedAt: now,
812
+ metadata: { ...(metadata || {}) },
813
+ };
814
+ _dlpPols.set(id, p);
815
+ return { ...p, metadata: { ...p.metadata } };
816
+ }
817
+ function _dlpCheckP(from, to) {
818
+ const a = _dlpPolTrans.get(from);
819
+ if (!a || !a.has(to))
820
+ throw new Error(`invalid dlp policy transition ${from} → ${to}`);
821
+ }
822
+ function _dlpCountActive(owner) {
823
+ let n = 0;
824
+ for (const p of _dlpPols.values())
825
+ if (p.owner === owner && p.status === DLP_POLICY_MATURITY_V2.ACTIVE) n++;
826
+ return n;
827
+ }
828
+
829
+ export function activateDlpPolicyV2(id) {
830
+ const p = _dlpPols.get(id);
831
+ if (!p) throw new Error(`dlp policy ${id} not found`);
832
+ _dlpCheckP(p.status, DLP_POLICY_MATURITY_V2.ACTIVE);
833
+ const recovery = p.status === DLP_POLICY_MATURITY_V2.SUSPENDED;
834
+ if (!recovery) {
835
+ const a = _dlpCountActive(p.owner);
836
+ if (a >= _dlpMaxActivePerOwner)
837
+ throw new Error(
838
+ `max active dlp policies per owner (${_dlpMaxActivePerOwner}) reached for ${p.owner}`,
839
+ );
840
+ }
841
+ const now = Date.now();
842
+ p.status = DLP_POLICY_MATURITY_V2.ACTIVE;
843
+ p.updatedAt = now;
844
+ p.lastTouchedAt = now;
845
+ if (!p.activatedAt) p.activatedAt = now;
846
+ return { ...p, metadata: { ...p.metadata } };
847
+ }
848
+ export function suspendDlpPolicyV2(id) {
849
+ const p = _dlpPols.get(id);
850
+ if (!p) throw new Error(`dlp policy ${id} not found`);
851
+ _dlpCheckP(p.status, DLP_POLICY_MATURITY_V2.SUSPENDED);
852
+ p.status = DLP_POLICY_MATURITY_V2.SUSPENDED;
853
+ p.updatedAt = Date.now();
854
+ return { ...p, metadata: { ...p.metadata } };
855
+ }
856
+ export function retireDlpPolicyV2(id) {
857
+ const p = _dlpPols.get(id);
858
+ if (!p) throw new Error(`dlp policy ${id} not found`);
859
+ _dlpCheckP(p.status, DLP_POLICY_MATURITY_V2.RETIRED);
860
+ const now = Date.now();
861
+ p.status = DLP_POLICY_MATURITY_V2.RETIRED;
862
+ p.updatedAt = now;
863
+ if (!p.retiredAt) p.retiredAt = now;
864
+ return { ...p, metadata: { ...p.metadata } };
865
+ }
866
+ export function touchDlpPolicyV2(id) {
867
+ const p = _dlpPols.get(id);
868
+ if (!p) throw new Error(`dlp policy ${id} not found`);
869
+ if (_dlpPolTerminal.has(p.status))
870
+ throw new Error(`cannot touch terminal dlp policy ${id}`);
871
+ const now = Date.now();
872
+ p.lastTouchedAt = now;
873
+ p.updatedAt = now;
874
+ return { ...p, metadata: { ...p.metadata } };
875
+ }
876
+ export function getDlpPolicyV2(id) {
877
+ const p = _dlpPols.get(id);
878
+ if (!p) return null;
879
+ return { ...p, metadata: { ...p.metadata } };
880
+ }
881
+ export function listDlpPoliciesV2() {
882
+ return [..._dlpPols.values()].map((p) => ({
883
+ ...p,
884
+ metadata: { ...p.metadata },
885
+ }));
886
+ }
887
+
888
+ function _dlpCountPending(pid) {
889
+ let n = 0;
890
+ for (const s of _dlpScans.values())
891
+ if (
892
+ s.policyId === pid &&
893
+ (s.status === DLP_SCAN_LIFECYCLE_V2.QUEUED ||
894
+ s.status === DLP_SCAN_LIFECYCLE_V2.SCANNING)
895
+ )
896
+ n++;
897
+ return n;
898
+ }
899
+
900
+ export function createDlpScanV2({ id, policyId, target, metadata } = {}) {
901
+ if (!id || typeof id !== "string") throw new Error("id is required");
902
+ if (!policyId || typeof policyId !== "string")
903
+ throw new Error("policyId is required");
904
+ if (_dlpScans.has(id)) throw new Error(`dlp scan ${id} already exists`);
905
+ if (!_dlpPols.has(policyId))
906
+ throw new Error(`dlp policy ${policyId} not found`);
907
+ const pending = _dlpCountPending(policyId);
908
+ if (pending >= _dlpMaxPendingPerPol)
909
+ throw new Error(
910
+ `max pending dlp scans per policy (${_dlpMaxPendingPerPol}) reached for ${policyId}`,
911
+ );
912
+ const now = Date.now();
913
+ const s = {
914
+ id,
915
+ policyId,
916
+ target: target || "",
917
+ status: DLP_SCAN_LIFECYCLE_V2.QUEUED,
918
+ createdAt: now,
919
+ updatedAt: now,
920
+ startedAt: null,
921
+ settledAt: null,
922
+ metadata: { ...(metadata || {}) },
923
+ };
924
+ _dlpScans.set(id, s);
925
+ return { ...s, metadata: { ...s.metadata } };
926
+ }
927
+ function _dlpCheckS(from, to) {
928
+ const a = _dlpScanTrans.get(from);
929
+ if (!a || !a.has(to))
930
+ throw new Error(`invalid dlp scan transition ${from} → ${to}`);
931
+ }
932
+ export function startDlpScanV2(id) {
933
+ const s = _dlpScans.get(id);
934
+ if (!s) throw new Error(`dlp scan ${id} not found`);
935
+ _dlpCheckS(s.status, DLP_SCAN_LIFECYCLE_V2.SCANNING);
936
+ const now = Date.now();
937
+ s.status = DLP_SCAN_LIFECYCLE_V2.SCANNING;
938
+ s.updatedAt = now;
939
+ if (!s.startedAt) s.startedAt = now;
940
+ return { ...s, metadata: { ...s.metadata } };
941
+ }
942
+ export function completeDlpScanV2(id) {
943
+ const s = _dlpScans.get(id);
944
+ if (!s) throw new Error(`dlp scan ${id} not found`);
945
+ _dlpCheckS(s.status, DLP_SCAN_LIFECYCLE_V2.COMPLETED);
946
+ const now = Date.now();
947
+ s.status = DLP_SCAN_LIFECYCLE_V2.COMPLETED;
948
+ s.updatedAt = now;
949
+ if (!s.settledAt) s.settledAt = now;
950
+ return { ...s, metadata: { ...s.metadata } };
951
+ }
952
+ export function failDlpScanV2(id, reason) {
953
+ const s = _dlpScans.get(id);
954
+ if (!s) throw new Error(`dlp scan ${id} not found`);
955
+ _dlpCheckS(s.status, DLP_SCAN_LIFECYCLE_V2.FAILED);
956
+ const now = Date.now();
957
+ s.status = DLP_SCAN_LIFECYCLE_V2.FAILED;
958
+ s.updatedAt = now;
959
+ if (!s.settledAt) s.settledAt = now;
960
+ if (reason) s.metadata.failReason = String(reason);
961
+ return { ...s, metadata: { ...s.metadata } };
962
+ }
963
+ export function cancelDlpScanV2(id, reason) {
964
+ const s = _dlpScans.get(id);
965
+ if (!s) throw new Error(`dlp scan ${id} not found`);
966
+ _dlpCheckS(s.status, DLP_SCAN_LIFECYCLE_V2.CANCELLED);
967
+ const now = Date.now();
968
+ s.status = DLP_SCAN_LIFECYCLE_V2.CANCELLED;
969
+ s.updatedAt = now;
970
+ if (!s.settledAt) s.settledAt = now;
971
+ if (reason) s.metadata.cancelReason = String(reason);
972
+ return { ...s, metadata: { ...s.metadata } };
973
+ }
974
+ export function getDlpScanV2(id) {
975
+ const s = _dlpScans.get(id);
976
+ if (!s) return null;
977
+ return { ...s, metadata: { ...s.metadata } };
978
+ }
979
+ export function listDlpScansV2() {
980
+ return [..._dlpScans.values()].map((s) => ({
981
+ ...s,
982
+ metadata: { ...s.metadata },
983
+ }));
984
+ }
985
+
986
+ export function autoSuspendIdleDlpPoliciesV2({ now } = {}) {
987
+ const t = now ?? Date.now();
988
+ const flipped = [];
989
+ for (const p of _dlpPols.values())
990
+ if (
991
+ p.status === DLP_POLICY_MATURITY_V2.ACTIVE &&
992
+ t - p.lastTouchedAt >= _dlpPolIdleMs
993
+ ) {
994
+ p.status = DLP_POLICY_MATURITY_V2.SUSPENDED;
995
+ p.updatedAt = t;
996
+ flipped.push(p.id);
997
+ }
998
+ return { flipped, count: flipped.length };
999
+ }
1000
+ export function autoFailStuckDlpScansV2({ now } = {}) {
1001
+ const t = now ?? Date.now();
1002
+ const flipped = [];
1003
+ for (const s of _dlpScans.values())
1004
+ if (
1005
+ s.status === DLP_SCAN_LIFECYCLE_V2.SCANNING &&
1006
+ s.startedAt != null &&
1007
+ t - s.startedAt >= _dlpScanStuckMs
1008
+ ) {
1009
+ s.status = DLP_SCAN_LIFECYCLE_V2.FAILED;
1010
+ s.updatedAt = t;
1011
+ if (!s.settledAt) s.settledAt = t;
1012
+ s.metadata.failReason = "auto-fail-stuck";
1013
+ flipped.push(s.id);
1014
+ }
1015
+ return { flipped, count: flipped.length };
1016
+ }
1017
+
1018
+ export function getDlpEngineStatsV2() {
1019
+ const policiesByStatus = {};
1020
+ for (const s of Object.values(DLP_POLICY_MATURITY_V2))
1021
+ policiesByStatus[s] = 0;
1022
+ for (const p of _dlpPols.values()) policiesByStatus[p.status]++;
1023
+ const scansByStatus = {};
1024
+ for (const s of Object.values(DLP_SCAN_LIFECYCLE_V2)) scansByStatus[s] = 0;
1025
+ for (const sc of _dlpScans.values()) scansByStatus[sc.status]++;
1026
+ return {
1027
+ totalPoliciesV2: _dlpPols.size,
1028
+ totalScansV2: _dlpScans.size,
1029
+ maxActiveDlpPoliciesPerOwner: _dlpMaxActivePerOwner,
1030
+ maxPendingDlpScansPerPolicy: _dlpMaxPendingPerPol,
1031
+ dlpPolicyIdleMs: _dlpPolIdleMs,
1032
+ dlpScanStuckMs: _dlpScanStuckMs,
1033
+ policiesByStatus,
1034
+ scansByStatus,
1035
+ };
1036
+ }
@@ -959,3 +959,339 @@ export function _resetV2State() {
959
959
  _v2Config.predictionHorizonMs = 86400000;
960
960
  _v2Config.growthLogRetentionDays = 365;
961
961
  }
962
+
963
+ // ===== V2 Surface: Evolution System governance overlay (CLI v0.137.0) =====
964
+ export const EVO_GOAL_MATURITY_V2 = Object.freeze({
965
+ PENDING: "pending",
966
+ ACTIVE: "active",
967
+ PAUSED: "paused",
968
+ ARCHIVED: "archived",
969
+ });
970
+ export const EVO_CYCLE_LIFECYCLE_V2 = Object.freeze({
971
+ QUEUED: "queued",
972
+ RUNNING: "running",
973
+ COMPLETED: "completed",
974
+ FAILED: "failed",
975
+ CANCELLED: "cancelled",
976
+ });
977
+
978
+ const _evoGoalTrans = new Map([
979
+ [
980
+ EVO_GOAL_MATURITY_V2.PENDING,
981
+ new Set([EVO_GOAL_MATURITY_V2.ACTIVE, EVO_GOAL_MATURITY_V2.ARCHIVED]),
982
+ ],
983
+ [
984
+ EVO_GOAL_MATURITY_V2.ACTIVE,
985
+ new Set([EVO_GOAL_MATURITY_V2.PAUSED, EVO_GOAL_MATURITY_V2.ARCHIVED]),
986
+ ],
987
+ [
988
+ EVO_GOAL_MATURITY_V2.PAUSED,
989
+ new Set([EVO_GOAL_MATURITY_V2.ACTIVE, EVO_GOAL_MATURITY_V2.ARCHIVED]),
990
+ ],
991
+ [EVO_GOAL_MATURITY_V2.ARCHIVED, new Set()],
992
+ ]);
993
+ const _evoGoalTerminal = new Set([EVO_GOAL_MATURITY_V2.ARCHIVED]);
994
+ const _evoCycleTrans = new Map([
995
+ [
996
+ EVO_CYCLE_LIFECYCLE_V2.QUEUED,
997
+ new Set([EVO_CYCLE_LIFECYCLE_V2.RUNNING, EVO_CYCLE_LIFECYCLE_V2.CANCELLED]),
998
+ ],
999
+ [
1000
+ EVO_CYCLE_LIFECYCLE_V2.RUNNING,
1001
+ new Set([
1002
+ EVO_CYCLE_LIFECYCLE_V2.COMPLETED,
1003
+ EVO_CYCLE_LIFECYCLE_V2.FAILED,
1004
+ EVO_CYCLE_LIFECYCLE_V2.CANCELLED,
1005
+ ]),
1006
+ ],
1007
+ [EVO_CYCLE_LIFECYCLE_V2.COMPLETED, new Set()],
1008
+ [EVO_CYCLE_LIFECYCLE_V2.FAILED, new Set()],
1009
+ [EVO_CYCLE_LIFECYCLE_V2.CANCELLED, new Set()],
1010
+ ]);
1011
+
1012
+ const _evoGoals = new Map();
1013
+ const _evoCycles = new Map();
1014
+ let _evoMaxActivePerOwner = 6;
1015
+ let _evoMaxPendingPerGoal = 12;
1016
+ let _evoGoalIdleMs = 14 * 24 * 60 * 60 * 1000;
1017
+ let _evoCycleStuckMs = 10 * 60 * 1000;
1018
+
1019
+ function _evoPos(n, lbl) {
1020
+ const v = Math.floor(Number(n));
1021
+ if (!Number.isFinite(v) || v <= 0)
1022
+ throw new Error(`${lbl} must be positive integer`);
1023
+ return v;
1024
+ }
1025
+
1026
+ export function setMaxActiveEvoGoalsPerOwnerV2(n) {
1027
+ _evoMaxActivePerOwner = _evoPos(n, "maxActiveEvoGoalsPerOwner");
1028
+ }
1029
+ export function getMaxActiveEvoGoalsPerOwnerV2() {
1030
+ return _evoMaxActivePerOwner;
1031
+ }
1032
+ export function setMaxPendingEvoCyclesPerGoalV2(n) {
1033
+ _evoMaxPendingPerGoal = _evoPos(n, "maxPendingEvoCyclesPerGoal");
1034
+ }
1035
+ export function getMaxPendingEvoCyclesPerGoalV2() {
1036
+ return _evoMaxPendingPerGoal;
1037
+ }
1038
+ export function setEvoGoalIdleMsV2(n) {
1039
+ _evoGoalIdleMs = _evoPos(n, "evoGoalIdleMs");
1040
+ }
1041
+ export function getEvoGoalIdleMsV2() {
1042
+ return _evoGoalIdleMs;
1043
+ }
1044
+ export function setEvoCycleStuckMsV2(n) {
1045
+ _evoCycleStuckMs = _evoPos(n, "evoCycleStuckMs");
1046
+ }
1047
+ export function getEvoCycleStuckMsV2() {
1048
+ return _evoCycleStuckMs;
1049
+ }
1050
+
1051
+ export function _resetStateEvolutionSystemV2() {
1052
+ _evoGoals.clear();
1053
+ _evoCycles.clear();
1054
+ _evoMaxActivePerOwner = 6;
1055
+ _evoMaxPendingPerGoal = 12;
1056
+ _evoGoalIdleMs = 14 * 24 * 60 * 60 * 1000;
1057
+ _evoCycleStuckMs = 10 * 60 * 1000;
1058
+ }
1059
+
1060
+ export function registerEvoGoalV2({ id, owner, objective, metadata } = {}) {
1061
+ if (!id || typeof id !== "string") throw new Error("id is required");
1062
+ if (!owner || typeof owner !== "string") throw new Error("owner is required");
1063
+ if (_evoGoals.has(id)) throw new Error(`evo goal ${id} already registered`);
1064
+ const now = Date.now();
1065
+ const g = {
1066
+ id,
1067
+ owner,
1068
+ objective: objective || "",
1069
+ status: EVO_GOAL_MATURITY_V2.PENDING,
1070
+ createdAt: now,
1071
+ updatedAt: now,
1072
+ activatedAt: null,
1073
+ archivedAt: null,
1074
+ lastTouchedAt: now,
1075
+ metadata: { ...(metadata || {}) },
1076
+ };
1077
+ _evoGoals.set(id, g);
1078
+ return { ...g, metadata: { ...g.metadata } };
1079
+ }
1080
+ function _evoCheckG(from, to) {
1081
+ const a = _evoGoalTrans.get(from);
1082
+ if (!a || !a.has(to))
1083
+ throw new Error(`invalid evo goal transition ${from} → ${to}`);
1084
+ }
1085
+ function _evoCountActive(owner) {
1086
+ let n = 0;
1087
+ for (const g of _evoGoals.values())
1088
+ if (g.owner === owner && g.status === EVO_GOAL_MATURITY_V2.ACTIVE) n++;
1089
+ return n;
1090
+ }
1091
+
1092
+ export function activateEvoGoalV2(id) {
1093
+ const g = _evoGoals.get(id);
1094
+ if (!g) throw new Error(`evo goal ${id} not found`);
1095
+ _evoCheckG(g.status, EVO_GOAL_MATURITY_V2.ACTIVE);
1096
+ const recovery = g.status === EVO_GOAL_MATURITY_V2.PAUSED;
1097
+ if (!recovery) {
1098
+ const a = _evoCountActive(g.owner);
1099
+ if (a >= _evoMaxActivePerOwner)
1100
+ throw new Error(
1101
+ `max active evo goals per owner (${_evoMaxActivePerOwner}) reached for ${g.owner}`,
1102
+ );
1103
+ }
1104
+ const now = Date.now();
1105
+ g.status = EVO_GOAL_MATURITY_V2.ACTIVE;
1106
+ g.updatedAt = now;
1107
+ g.lastTouchedAt = now;
1108
+ if (!g.activatedAt) g.activatedAt = now;
1109
+ return { ...g, metadata: { ...g.metadata } };
1110
+ }
1111
+ export function pauseEvoGoalV2(id) {
1112
+ const g = _evoGoals.get(id);
1113
+ if (!g) throw new Error(`evo goal ${id} not found`);
1114
+ _evoCheckG(g.status, EVO_GOAL_MATURITY_V2.PAUSED);
1115
+ g.status = EVO_GOAL_MATURITY_V2.PAUSED;
1116
+ g.updatedAt = Date.now();
1117
+ return { ...g, metadata: { ...g.metadata } };
1118
+ }
1119
+ export function archiveEvoGoalV2(id) {
1120
+ const g = _evoGoals.get(id);
1121
+ if (!g) throw new Error(`evo goal ${id} not found`);
1122
+ _evoCheckG(g.status, EVO_GOAL_MATURITY_V2.ARCHIVED);
1123
+ const now = Date.now();
1124
+ g.status = EVO_GOAL_MATURITY_V2.ARCHIVED;
1125
+ g.updatedAt = now;
1126
+ if (!g.archivedAt) g.archivedAt = now;
1127
+ return { ...g, metadata: { ...g.metadata } };
1128
+ }
1129
+ export function touchEvoGoalV2(id) {
1130
+ const g = _evoGoals.get(id);
1131
+ if (!g) throw new Error(`evo goal ${id} not found`);
1132
+ if (_evoGoalTerminal.has(g.status))
1133
+ throw new Error(`cannot touch terminal evo goal ${id}`);
1134
+ const now = Date.now();
1135
+ g.lastTouchedAt = now;
1136
+ g.updatedAt = now;
1137
+ return { ...g, metadata: { ...g.metadata } };
1138
+ }
1139
+ export function getEvoGoalV2(id) {
1140
+ const g = _evoGoals.get(id);
1141
+ if (!g) return null;
1142
+ return { ...g, metadata: { ...g.metadata } };
1143
+ }
1144
+ export function listEvoGoalsV2() {
1145
+ return [..._evoGoals.values()].map((g) => ({
1146
+ ...g,
1147
+ metadata: { ...g.metadata },
1148
+ }));
1149
+ }
1150
+
1151
+ function _evoCountPending(gid) {
1152
+ let n = 0;
1153
+ for (const c of _evoCycles.values())
1154
+ if (
1155
+ c.goalId === gid &&
1156
+ (c.status === EVO_CYCLE_LIFECYCLE_V2.QUEUED ||
1157
+ c.status === EVO_CYCLE_LIFECYCLE_V2.RUNNING)
1158
+ )
1159
+ n++;
1160
+ return n;
1161
+ }
1162
+
1163
+ export function createEvoCycleV2({ id, goalId, generation, metadata } = {}) {
1164
+ if (!id || typeof id !== "string") throw new Error("id is required");
1165
+ if (!goalId || typeof goalId !== "string")
1166
+ throw new Error("goalId is required");
1167
+ if (_evoCycles.has(id)) throw new Error(`evo cycle ${id} already exists`);
1168
+ if (!_evoGoals.has(goalId)) throw new Error(`evo goal ${goalId} not found`);
1169
+ const pending = _evoCountPending(goalId);
1170
+ if (pending >= _evoMaxPendingPerGoal)
1171
+ throw new Error(
1172
+ `max pending evo cycles per goal (${_evoMaxPendingPerGoal}) reached for ${goalId}`,
1173
+ );
1174
+ const now = Date.now();
1175
+ const c = {
1176
+ id,
1177
+ goalId,
1178
+ generation: generation ?? 0,
1179
+ status: EVO_CYCLE_LIFECYCLE_V2.QUEUED,
1180
+ createdAt: now,
1181
+ updatedAt: now,
1182
+ startedAt: null,
1183
+ settledAt: null,
1184
+ metadata: { ...(metadata || {}) },
1185
+ };
1186
+ _evoCycles.set(id, c);
1187
+ return { ...c, metadata: { ...c.metadata } };
1188
+ }
1189
+ function _evoCheckC(from, to) {
1190
+ const a = _evoCycleTrans.get(from);
1191
+ if (!a || !a.has(to))
1192
+ throw new Error(`invalid evo cycle transition ${from} → ${to}`);
1193
+ }
1194
+ export function startEvoCycleV2(id) {
1195
+ const c = _evoCycles.get(id);
1196
+ if (!c) throw new Error(`evo cycle ${id} not found`);
1197
+ _evoCheckC(c.status, EVO_CYCLE_LIFECYCLE_V2.RUNNING);
1198
+ const now = Date.now();
1199
+ c.status = EVO_CYCLE_LIFECYCLE_V2.RUNNING;
1200
+ c.updatedAt = now;
1201
+ if (!c.startedAt) c.startedAt = now;
1202
+ return { ...c, metadata: { ...c.metadata } };
1203
+ }
1204
+ export function completeEvoCycleV2(id) {
1205
+ const c = _evoCycles.get(id);
1206
+ if (!c) throw new Error(`evo cycle ${id} not found`);
1207
+ _evoCheckC(c.status, EVO_CYCLE_LIFECYCLE_V2.COMPLETED);
1208
+ const now = Date.now();
1209
+ c.status = EVO_CYCLE_LIFECYCLE_V2.COMPLETED;
1210
+ c.updatedAt = now;
1211
+ if (!c.settledAt) c.settledAt = now;
1212
+ return { ...c, metadata: { ...c.metadata } };
1213
+ }
1214
+ export function failEvoCycleV2(id, reason) {
1215
+ const c = _evoCycles.get(id);
1216
+ if (!c) throw new Error(`evo cycle ${id} not found`);
1217
+ _evoCheckC(c.status, EVO_CYCLE_LIFECYCLE_V2.FAILED);
1218
+ const now = Date.now();
1219
+ c.status = EVO_CYCLE_LIFECYCLE_V2.FAILED;
1220
+ c.updatedAt = now;
1221
+ if (!c.settledAt) c.settledAt = now;
1222
+ if (reason) c.metadata.failReason = String(reason);
1223
+ return { ...c, metadata: { ...c.metadata } };
1224
+ }
1225
+ export function cancelEvoCycleV2(id, reason) {
1226
+ const c = _evoCycles.get(id);
1227
+ if (!c) throw new Error(`evo cycle ${id} not found`);
1228
+ _evoCheckC(c.status, EVO_CYCLE_LIFECYCLE_V2.CANCELLED);
1229
+ const now = Date.now();
1230
+ c.status = EVO_CYCLE_LIFECYCLE_V2.CANCELLED;
1231
+ c.updatedAt = now;
1232
+ if (!c.settledAt) c.settledAt = now;
1233
+ if (reason) c.metadata.cancelReason = String(reason);
1234
+ return { ...c, metadata: { ...c.metadata } };
1235
+ }
1236
+ export function getEvoCycleV2(id) {
1237
+ const c = _evoCycles.get(id);
1238
+ if (!c) return null;
1239
+ return { ...c, metadata: { ...c.metadata } };
1240
+ }
1241
+ export function listEvoCyclesV2() {
1242
+ return [..._evoCycles.values()].map((c) => ({
1243
+ ...c,
1244
+ metadata: { ...c.metadata },
1245
+ }));
1246
+ }
1247
+
1248
+ export function autoPauseIdleEvoGoalsV2({ now } = {}) {
1249
+ const t = now ?? Date.now();
1250
+ const flipped = [];
1251
+ for (const g of _evoGoals.values())
1252
+ if (
1253
+ g.status === EVO_GOAL_MATURITY_V2.ACTIVE &&
1254
+ t - g.lastTouchedAt >= _evoGoalIdleMs
1255
+ ) {
1256
+ g.status = EVO_GOAL_MATURITY_V2.PAUSED;
1257
+ g.updatedAt = t;
1258
+ flipped.push(g.id);
1259
+ }
1260
+ return { flipped, count: flipped.length };
1261
+ }
1262
+ export function autoFailStuckEvoCyclesV2({ now } = {}) {
1263
+ const t = now ?? Date.now();
1264
+ const flipped = [];
1265
+ for (const c of _evoCycles.values())
1266
+ if (
1267
+ c.status === EVO_CYCLE_LIFECYCLE_V2.RUNNING &&
1268
+ c.startedAt != null &&
1269
+ t - c.startedAt >= _evoCycleStuckMs
1270
+ ) {
1271
+ c.status = EVO_CYCLE_LIFECYCLE_V2.FAILED;
1272
+ c.updatedAt = t;
1273
+ if (!c.settledAt) c.settledAt = t;
1274
+ c.metadata.failReason = "auto-fail-stuck";
1275
+ flipped.push(c.id);
1276
+ }
1277
+ return { flipped, count: flipped.length };
1278
+ }
1279
+
1280
+ export function getEvolutionSystemGovStatsV2() {
1281
+ const goalsByStatus = {};
1282
+ for (const s of Object.values(EVO_GOAL_MATURITY_V2)) goalsByStatus[s] = 0;
1283
+ for (const g of _evoGoals.values()) goalsByStatus[g.status]++;
1284
+ const cyclesByStatus = {};
1285
+ for (const s of Object.values(EVO_CYCLE_LIFECYCLE_V2)) cyclesByStatus[s] = 0;
1286
+ for (const c of _evoCycles.values()) cyclesByStatus[c.status]++;
1287
+ return {
1288
+ totalGoalsV2: _evoGoals.size,
1289
+ totalCyclesV2: _evoCycles.size,
1290
+ maxActiveEvoGoalsPerOwner: _evoMaxActivePerOwner,
1291
+ maxPendingEvoCyclesPerGoal: _evoMaxPendingPerGoal,
1292
+ evoGoalIdleMs: _evoGoalIdleMs,
1293
+ evoCycleStuckMs: _evoCycleStuckMs,
1294
+ goalsByStatus,
1295
+ cyclesByStatus,
1296
+ };
1297
+ }