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
@@ -1034,3 +1034,342 @@ export function getDlpEngineStatsV2() {
1034
1034
  scansByStatus,
1035
1035
  };
1036
1036
  }
1037
+
1038
+ // =====================================================================
1039
+ // dlp-engine V2 governance overlay (iter20)
1040
+ // =====================================================================
1041
+ export const DLPGOV_PROFILE_MATURITY_V2 = Object.freeze({
1042
+ PENDING: "pending",
1043
+ ACTIVE: "active",
1044
+ SUSPENDED: "suspended",
1045
+ ARCHIVED: "archived",
1046
+ });
1047
+ export const DLPGOV_SCAN_LIFECYCLE_V2 = Object.freeze({
1048
+ QUEUED: "queued",
1049
+ SCANNING: "scanning",
1050
+ SCANNED: "scanned",
1051
+ FAILED: "failed",
1052
+ CANCELLED: "cancelled",
1053
+ });
1054
+ const _dlpgovPTrans = new Map([
1055
+ [
1056
+ DLPGOV_PROFILE_MATURITY_V2.PENDING,
1057
+ new Set([
1058
+ DLPGOV_PROFILE_MATURITY_V2.ACTIVE,
1059
+ DLPGOV_PROFILE_MATURITY_V2.ARCHIVED,
1060
+ ]),
1061
+ ],
1062
+ [
1063
+ DLPGOV_PROFILE_MATURITY_V2.ACTIVE,
1064
+ new Set([
1065
+ DLPGOV_PROFILE_MATURITY_V2.SUSPENDED,
1066
+ DLPGOV_PROFILE_MATURITY_V2.ARCHIVED,
1067
+ ]),
1068
+ ],
1069
+ [
1070
+ DLPGOV_PROFILE_MATURITY_V2.SUSPENDED,
1071
+ new Set([
1072
+ DLPGOV_PROFILE_MATURITY_V2.ACTIVE,
1073
+ DLPGOV_PROFILE_MATURITY_V2.ARCHIVED,
1074
+ ]),
1075
+ ],
1076
+ [DLPGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1077
+ ]);
1078
+ const _dlpgovPTerminal = new Set([DLPGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1079
+ const _dlpgovJTrans = new Map([
1080
+ [
1081
+ DLPGOV_SCAN_LIFECYCLE_V2.QUEUED,
1082
+ new Set([
1083
+ DLPGOV_SCAN_LIFECYCLE_V2.SCANNING,
1084
+ DLPGOV_SCAN_LIFECYCLE_V2.CANCELLED,
1085
+ ]),
1086
+ ],
1087
+ [
1088
+ DLPGOV_SCAN_LIFECYCLE_V2.SCANNING,
1089
+ new Set([
1090
+ DLPGOV_SCAN_LIFECYCLE_V2.SCANNED,
1091
+ DLPGOV_SCAN_LIFECYCLE_V2.FAILED,
1092
+ DLPGOV_SCAN_LIFECYCLE_V2.CANCELLED,
1093
+ ]),
1094
+ ],
1095
+ [DLPGOV_SCAN_LIFECYCLE_V2.SCANNED, new Set()],
1096
+ [DLPGOV_SCAN_LIFECYCLE_V2.FAILED, new Set()],
1097
+ [DLPGOV_SCAN_LIFECYCLE_V2.CANCELLED, new Set()],
1098
+ ]);
1099
+ const _dlpgovPsV2 = new Map();
1100
+ const _dlpgovJsV2 = new Map();
1101
+ let _dlpgovMaxActive = 8,
1102
+ _dlpgovMaxPending = 20,
1103
+ _dlpgovIdleMs = 30 * 24 * 60 * 60 * 1000,
1104
+ _dlpgovStuckMs = 60 * 1000;
1105
+ function _dlpgovPos(n, label) {
1106
+ const v = Math.floor(Number(n));
1107
+ if (!Number.isFinite(v) || v <= 0)
1108
+ throw new Error(`${label} must be positive integer`);
1109
+ return v;
1110
+ }
1111
+ function _dlpgovCheckP(from, to) {
1112
+ const a = _dlpgovPTrans.get(from);
1113
+ if (!a || !a.has(to))
1114
+ throw new Error(`invalid dlpgov profile transition ${from} → ${to}`);
1115
+ }
1116
+ function _dlpgovCheckJ(from, to) {
1117
+ const a = _dlpgovJTrans.get(from);
1118
+ if (!a || !a.has(to))
1119
+ throw new Error(`invalid dlpgov scan transition ${from} → ${to}`);
1120
+ }
1121
+ function _dlpgovCountActive(owner) {
1122
+ let c = 0;
1123
+ for (const p of _dlpgovPsV2.values())
1124
+ if (p.owner === owner && p.status === DLPGOV_PROFILE_MATURITY_V2.ACTIVE)
1125
+ c++;
1126
+ return c;
1127
+ }
1128
+ function _dlpgovCountPending(profileId) {
1129
+ let c = 0;
1130
+ for (const j of _dlpgovJsV2.values())
1131
+ if (
1132
+ j.profileId === profileId &&
1133
+ (j.status === DLPGOV_SCAN_LIFECYCLE_V2.QUEUED ||
1134
+ j.status === DLPGOV_SCAN_LIFECYCLE_V2.SCANNING)
1135
+ )
1136
+ c++;
1137
+ return c;
1138
+ }
1139
+ export function setMaxActiveDlpgovProfilesPerOwnerV2(n) {
1140
+ _dlpgovMaxActive = _dlpgovPos(n, "maxActiveDlpgovProfilesPerOwner");
1141
+ }
1142
+ export function getMaxActiveDlpgovProfilesPerOwnerV2() {
1143
+ return _dlpgovMaxActive;
1144
+ }
1145
+ export function setMaxPendingDlpgovScansPerProfileV2(n) {
1146
+ _dlpgovMaxPending = _dlpgovPos(n, "maxPendingDlpgovScansPerProfile");
1147
+ }
1148
+ export function getMaxPendingDlpgovScansPerProfileV2() {
1149
+ return _dlpgovMaxPending;
1150
+ }
1151
+ export function setDlpgovProfileIdleMsV2(n) {
1152
+ _dlpgovIdleMs = _dlpgovPos(n, "dlpgovProfileIdleMs");
1153
+ }
1154
+ export function getDlpgovProfileIdleMsV2() {
1155
+ return _dlpgovIdleMs;
1156
+ }
1157
+ export function setDlpgovScanStuckMsV2(n) {
1158
+ _dlpgovStuckMs = _dlpgovPos(n, "dlpgovScanStuckMs");
1159
+ }
1160
+ export function getDlpgovScanStuckMsV2() {
1161
+ return _dlpgovStuckMs;
1162
+ }
1163
+ export function _resetStateDlpEngineGovV2() {
1164
+ _dlpgovPsV2.clear();
1165
+ _dlpgovJsV2.clear();
1166
+ _dlpgovMaxActive = 8;
1167
+ _dlpgovMaxPending = 20;
1168
+ _dlpgovIdleMs = 30 * 24 * 60 * 60 * 1000;
1169
+ _dlpgovStuckMs = 60 * 1000;
1170
+ }
1171
+ export function registerDlpgovProfileV2({
1172
+ id,
1173
+ owner,
1174
+ classification,
1175
+ metadata,
1176
+ } = {}) {
1177
+ if (!id || !owner) throw new Error("id and owner required");
1178
+ if (_dlpgovPsV2.has(id))
1179
+ throw new Error(`dlpgov profile ${id} already exists`);
1180
+ const now = Date.now();
1181
+ const p = {
1182
+ id,
1183
+ owner,
1184
+ classification: classification || "internal",
1185
+ status: DLPGOV_PROFILE_MATURITY_V2.PENDING,
1186
+ createdAt: now,
1187
+ updatedAt: now,
1188
+ lastTouchedAt: now,
1189
+ activatedAt: null,
1190
+ archivedAt: null,
1191
+ metadata: { ...(metadata || {}) },
1192
+ };
1193
+ _dlpgovPsV2.set(id, p);
1194
+ return { ...p, metadata: { ...p.metadata } };
1195
+ }
1196
+ export function activateDlpgovProfileV2(id) {
1197
+ const p = _dlpgovPsV2.get(id);
1198
+ if (!p) throw new Error(`dlpgov profile ${id} not found`);
1199
+ const isInitial = p.status === DLPGOV_PROFILE_MATURITY_V2.PENDING;
1200
+ _dlpgovCheckP(p.status, DLPGOV_PROFILE_MATURITY_V2.ACTIVE);
1201
+ if (isInitial && _dlpgovCountActive(p.owner) >= _dlpgovMaxActive)
1202
+ throw new Error(`max active dlpgov profiles for owner ${p.owner} reached`);
1203
+ const now = Date.now();
1204
+ p.status = DLPGOV_PROFILE_MATURITY_V2.ACTIVE;
1205
+ p.updatedAt = now;
1206
+ p.lastTouchedAt = now;
1207
+ if (!p.activatedAt) p.activatedAt = now;
1208
+ return { ...p, metadata: { ...p.metadata } };
1209
+ }
1210
+ export function suspendDlpgovProfileV2(id) {
1211
+ const p = _dlpgovPsV2.get(id);
1212
+ if (!p) throw new Error(`dlpgov profile ${id} not found`);
1213
+ _dlpgovCheckP(p.status, DLPGOV_PROFILE_MATURITY_V2.SUSPENDED);
1214
+ p.status = DLPGOV_PROFILE_MATURITY_V2.SUSPENDED;
1215
+ p.updatedAt = Date.now();
1216
+ return { ...p, metadata: { ...p.metadata } };
1217
+ }
1218
+ export function archiveDlpgovProfileV2(id) {
1219
+ const p = _dlpgovPsV2.get(id);
1220
+ if (!p) throw new Error(`dlpgov profile ${id} not found`);
1221
+ _dlpgovCheckP(p.status, DLPGOV_PROFILE_MATURITY_V2.ARCHIVED);
1222
+ const now = Date.now();
1223
+ p.status = DLPGOV_PROFILE_MATURITY_V2.ARCHIVED;
1224
+ p.updatedAt = now;
1225
+ if (!p.archivedAt) p.archivedAt = now;
1226
+ return { ...p, metadata: { ...p.metadata } };
1227
+ }
1228
+ export function touchDlpgovProfileV2(id) {
1229
+ const p = _dlpgovPsV2.get(id);
1230
+ if (!p) throw new Error(`dlpgov profile ${id} not found`);
1231
+ if (_dlpgovPTerminal.has(p.status))
1232
+ throw new Error(`cannot touch terminal dlpgov profile ${id}`);
1233
+ const now = Date.now();
1234
+ p.lastTouchedAt = now;
1235
+ p.updatedAt = now;
1236
+ return { ...p, metadata: { ...p.metadata } };
1237
+ }
1238
+ export function getDlpgovProfileV2(id) {
1239
+ const p = _dlpgovPsV2.get(id);
1240
+ if (!p) return null;
1241
+ return { ...p, metadata: { ...p.metadata } };
1242
+ }
1243
+ export function listDlpgovProfilesV2() {
1244
+ return [..._dlpgovPsV2.values()].map((p) => ({
1245
+ ...p,
1246
+ metadata: { ...p.metadata },
1247
+ }));
1248
+ }
1249
+ export function createDlpgovScanV2({ id, profileId, resource, metadata } = {}) {
1250
+ if (!id || !profileId) throw new Error("id and profileId required");
1251
+ if (_dlpgovJsV2.has(id)) throw new Error(`dlpgov scan ${id} already exists`);
1252
+ if (!_dlpgovPsV2.has(profileId))
1253
+ throw new Error(`dlpgov profile ${profileId} not found`);
1254
+ if (_dlpgovCountPending(profileId) >= _dlpgovMaxPending)
1255
+ throw new Error(
1256
+ `max pending dlpgov scans for profile ${profileId} reached`,
1257
+ );
1258
+ const now = Date.now();
1259
+ const j = {
1260
+ id,
1261
+ profileId,
1262
+ resource: resource || "",
1263
+ status: DLPGOV_SCAN_LIFECYCLE_V2.QUEUED,
1264
+ createdAt: now,
1265
+ updatedAt: now,
1266
+ startedAt: null,
1267
+ settledAt: null,
1268
+ metadata: { ...(metadata || {}) },
1269
+ };
1270
+ _dlpgovJsV2.set(id, j);
1271
+ return { ...j, metadata: { ...j.metadata } };
1272
+ }
1273
+ export function scanningDlpgovScanV2(id) {
1274
+ const j = _dlpgovJsV2.get(id);
1275
+ if (!j) throw new Error(`dlpgov scan ${id} not found`);
1276
+ _dlpgovCheckJ(j.status, DLPGOV_SCAN_LIFECYCLE_V2.SCANNING);
1277
+ const now = Date.now();
1278
+ j.status = DLPGOV_SCAN_LIFECYCLE_V2.SCANNING;
1279
+ j.updatedAt = now;
1280
+ if (!j.startedAt) j.startedAt = now;
1281
+ return { ...j, metadata: { ...j.metadata } };
1282
+ }
1283
+ export function completeScanDlpgovV2(id) {
1284
+ const j = _dlpgovJsV2.get(id);
1285
+ if (!j) throw new Error(`dlpgov scan ${id} not found`);
1286
+ _dlpgovCheckJ(j.status, DLPGOV_SCAN_LIFECYCLE_V2.SCANNED);
1287
+ const now = Date.now();
1288
+ j.status = DLPGOV_SCAN_LIFECYCLE_V2.SCANNED;
1289
+ j.updatedAt = now;
1290
+ if (!j.settledAt) j.settledAt = now;
1291
+ return { ...j, metadata: { ...j.metadata } };
1292
+ }
1293
+ export function failDlpgovScanV2(id, reason) {
1294
+ const j = _dlpgovJsV2.get(id);
1295
+ if (!j) throw new Error(`dlpgov scan ${id} not found`);
1296
+ _dlpgovCheckJ(j.status, DLPGOV_SCAN_LIFECYCLE_V2.FAILED);
1297
+ const now = Date.now();
1298
+ j.status = DLPGOV_SCAN_LIFECYCLE_V2.FAILED;
1299
+ j.updatedAt = now;
1300
+ if (!j.settledAt) j.settledAt = now;
1301
+ if (reason) j.metadata.failReason = String(reason);
1302
+ return { ...j, metadata: { ...j.metadata } };
1303
+ }
1304
+ export function cancelDlpgovScanV2(id, reason) {
1305
+ const j = _dlpgovJsV2.get(id);
1306
+ if (!j) throw new Error(`dlpgov scan ${id} not found`);
1307
+ _dlpgovCheckJ(j.status, DLPGOV_SCAN_LIFECYCLE_V2.CANCELLED);
1308
+ const now = Date.now();
1309
+ j.status = DLPGOV_SCAN_LIFECYCLE_V2.CANCELLED;
1310
+ j.updatedAt = now;
1311
+ if (!j.settledAt) j.settledAt = now;
1312
+ if (reason) j.metadata.cancelReason = String(reason);
1313
+ return { ...j, metadata: { ...j.metadata } };
1314
+ }
1315
+ export function getDlpgovScanV2(id) {
1316
+ const j = _dlpgovJsV2.get(id);
1317
+ if (!j) return null;
1318
+ return { ...j, metadata: { ...j.metadata } };
1319
+ }
1320
+ export function listDlpgovScansV2() {
1321
+ return [..._dlpgovJsV2.values()].map((j) => ({
1322
+ ...j,
1323
+ metadata: { ...j.metadata },
1324
+ }));
1325
+ }
1326
+ export function autoSuspendIdleDlpgovProfilesV2({ now } = {}) {
1327
+ const t = now ?? Date.now();
1328
+ const flipped = [];
1329
+ for (const p of _dlpgovPsV2.values())
1330
+ if (
1331
+ p.status === DLPGOV_PROFILE_MATURITY_V2.ACTIVE &&
1332
+ t - p.lastTouchedAt >= _dlpgovIdleMs
1333
+ ) {
1334
+ p.status = DLPGOV_PROFILE_MATURITY_V2.SUSPENDED;
1335
+ p.updatedAt = t;
1336
+ flipped.push(p.id);
1337
+ }
1338
+ return { flipped, count: flipped.length };
1339
+ }
1340
+ export function autoFailStuckDlpgovScansV2({ now } = {}) {
1341
+ const t = now ?? Date.now();
1342
+ const flipped = [];
1343
+ for (const j of _dlpgovJsV2.values())
1344
+ if (
1345
+ j.status === DLPGOV_SCAN_LIFECYCLE_V2.SCANNING &&
1346
+ j.startedAt != null &&
1347
+ t - j.startedAt >= _dlpgovStuckMs
1348
+ ) {
1349
+ j.status = DLPGOV_SCAN_LIFECYCLE_V2.FAILED;
1350
+ j.updatedAt = t;
1351
+ if (!j.settledAt) j.settledAt = t;
1352
+ j.metadata.failReason = "auto-fail-stuck";
1353
+ flipped.push(j.id);
1354
+ }
1355
+ return { flipped, count: flipped.length };
1356
+ }
1357
+ export function getDlpEngineGovStatsV2() {
1358
+ const profilesByStatus = {};
1359
+ for (const v of Object.values(DLPGOV_PROFILE_MATURITY_V2))
1360
+ profilesByStatus[v] = 0;
1361
+ for (const p of _dlpgovPsV2.values()) profilesByStatus[p.status]++;
1362
+ const scansByStatus = {};
1363
+ for (const v of Object.values(DLPGOV_SCAN_LIFECYCLE_V2)) scansByStatus[v] = 0;
1364
+ for (const j of _dlpgovJsV2.values()) scansByStatus[j.status]++;
1365
+ return {
1366
+ totalDlpgovProfilesV2: _dlpgovPsV2.size,
1367
+ totalDlpgovScansV2: _dlpgovJsV2.size,
1368
+ maxActiveDlpgovProfilesPerOwner: _dlpgovMaxActive,
1369
+ maxPendingDlpgovScansPerProfile: _dlpgovMaxPending,
1370
+ dlpgovProfileIdleMs: _dlpgovIdleMs,
1371
+ dlpgovScanStuckMs: _dlpgovStuckMs,
1372
+ profilesByStatus,
1373
+ scansByStatus,
1374
+ };
1375
+ }
@@ -524,3 +524,337 @@ export function getGovernanceStatsV2() {
524
524
  byType,
525
525
  };
526
526
  }
527
+
528
+ // =====================================================================
529
+ // evomap-governance V2 governance overlay (iter20)
530
+ // =====================================================================
531
+ export const EVGOV_PROFILE_MATURITY_V2 = Object.freeze({
532
+ PENDING: "pending",
533
+ ACTIVE: "active",
534
+ PAUSED: "paused",
535
+ ARCHIVED: "archived",
536
+ });
537
+ export const EVGOV_PROPOSAL_LIFECYCLE_V2 = Object.freeze({
538
+ QUEUED: "queued",
539
+ REVIEWING: "reviewing",
540
+ REVIEWED: "reviewed",
541
+ FAILED: "failed",
542
+ CANCELLED: "cancelled",
543
+ });
544
+ const _evgovPTrans = new Map([
545
+ [
546
+ EVGOV_PROFILE_MATURITY_V2.PENDING,
547
+ new Set([
548
+ EVGOV_PROFILE_MATURITY_V2.ACTIVE,
549
+ EVGOV_PROFILE_MATURITY_V2.ARCHIVED,
550
+ ]),
551
+ ],
552
+ [
553
+ EVGOV_PROFILE_MATURITY_V2.ACTIVE,
554
+ new Set([
555
+ EVGOV_PROFILE_MATURITY_V2.PAUSED,
556
+ EVGOV_PROFILE_MATURITY_V2.ARCHIVED,
557
+ ]),
558
+ ],
559
+ [
560
+ EVGOV_PROFILE_MATURITY_V2.PAUSED,
561
+ new Set([
562
+ EVGOV_PROFILE_MATURITY_V2.ACTIVE,
563
+ EVGOV_PROFILE_MATURITY_V2.ARCHIVED,
564
+ ]),
565
+ ],
566
+ [EVGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
567
+ ]);
568
+ const _evgovPTerminal = new Set([EVGOV_PROFILE_MATURITY_V2.ARCHIVED]);
569
+ const _evgovJTrans = new Map([
570
+ [
571
+ EVGOV_PROPOSAL_LIFECYCLE_V2.QUEUED,
572
+ new Set([
573
+ EVGOV_PROPOSAL_LIFECYCLE_V2.REVIEWING,
574
+ EVGOV_PROPOSAL_LIFECYCLE_V2.CANCELLED,
575
+ ]),
576
+ ],
577
+ [
578
+ EVGOV_PROPOSAL_LIFECYCLE_V2.REVIEWING,
579
+ new Set([
580
+ EVGOV_PROPOSAL_LIFECYCLE_V2.REVIEWED,
581
+ EVGOV_PROPOSAL_LIFECYCLE_V2.FAILED,
582
+ EVGOV_PROPOSAL_LIFECYCLE_V2.CANCELLED,
583
+ ]),
584
+ ],
585
+ [EVGOV_PROPOSAL_LIFECYCLE_V2.REVIEWED, new Set()],
586
+ [EVGOV_PROPOSAL_LIFECYCLE_V2.FAILED, new Set()],
587
+ [EVGOV_PROPOSAL_LIFECYCLE_V2.CANCELLED, new Set()],
588
+ ]);
589
+ const _evgovPsV2 = new Map();
590
+ const _evgovJsV2 = new Map();
591
+ let _evgovMaxActive = 6,
592
+ _evgovMaxPending = 15,
593
+ _evgovIdleMs = 30 * 24 * 60 * 60 * 1000,
594
+ _evgovStuckMs = 60 * 1000;
595
+ function _evgovPos(n, label) {
596
+ const v = Math.floor(Number(n));
597
+ if (!Number.isFinite(v) || v <= 0)
598
+ throw new Error(`${label} must be positive integer`);
599
+ return v;
600
+ }
601
+ function _evgovCheckP(from, to) {
602
+ const a = _evgovPTrans.get(from);
603
+ if (!a || !a.has(to))
604
+ throw new Error(`invalid evgov profile transition ${from} → ${to}`);
605
+ }
606
+ function _evgovCheckJ(from, to) {
607
+ const a = _evgovJTrans.get(from);
608
+ if (!a || !a.has(to))
609
+ throw new Error(`invalid evgov proposal transition ${from} → ${to}`);
610
+ }
611
+ function _evgovCountActive(owner) {
612
+ let c = 0;
613
+ for (const p of _evgovPsV2.values())
614
+ if (p.owner === owner && p.status === EVGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
615
+ return c;
616
+ }
617
+ function _evgovCountPending(profileId) {
618
+ let c = 0;
619
+ for (const j of _evgovJsV2.values())
620
+ if (
621
+ j.profileId === profileId &&
622
+ (j.status === EVGOV_PROPOSAL_LIFECYCLE_V2.QUEUED ||
623
+ j.status === EVGOV_PROPOSAL_LIFECYCLE_V2.REVIEWING)
624
+ )
625
+ c++;
626
+ return c;
627
+ }
628
+ export function setMaxActiveEvgovProfilesPerOwnerV2(n) {
629
+ _evgovMaxActive = _evgovPos(n, "maxActiveEvgovProfilesPerOwner");
630
+ }
631
+ export function getMaxActiveEvgovProfilesPerOwnerV2() {
632
+ return _evgovMaxActive;
633
+ }
634
+ export function setMaxPendingEvgovProposalsPerProfileV2(n) {
635
+ _evgovMaxPending = _evgovPos(n, "maxPendingEvgovProposalsPerProfile");
636
+ }
637
+ export function getMaxPendingEvgovProposalsPerProfileV2() {
638
+ return _evgovMaxPending;
639
+ }
640
+ export function setEvgovProfileIdleMsV2(n) {
641
+ _evgovIdleMs = _evgovPos(n, "evgovProfileIdleMs");
642
+ }
643
+ export function getEvgovProfileIdleMsV2() {
644
+ return _evgovIdleMs;
645
+ }
646
+ export function setEvgovProposalStuckMsV2(n) {
647
+ _evgovStuckMs = _evgovPos(n, "evgovProposalStuckMs");
648
+ }
649
+ export function getEvgovProposalStuckMsV2() {
650
+ return _evgovStuckMs;
651
+ }
652
+ export function _resetStateEvomapGovernanceGovV2() {
653
+ _evgovPsV2.clear();
654
+ _evgovJsV2.clear();
655
+ _evgovMaxActive = 6;
656
+ _evgovMaxPending = 15;
657
+ _evgovIdleMs = 30 * 24 * 60 * 60 * 1000;
658
+ _evgovStuckMs = 60 * 1000;
659
+ }
660
+ export function registerEvgovProfileV2({ id, owner, lane, metadata } = {}) {
661
+ if (!id || !owner) throw new Error("id and owner required");
662
+ if (_evgovPsV2.has(id)) throw new Error(`evgov profile ${id} already exists`);
663
+ const now = Date.now();
664
+ const p = {
665
+ id,
666
+ owner,
667
+ lane: lane || "core",
668
+ status: EVGOV_PROFILE_MATURITY_V2.PENDING,
669
+ createdAt: now,
670
+ updatedAt: now,
671
+ lastTouchedAt: now,
672
+ activatedAt: null,
673
+ archivedAt: null,
674
+ metadata: { ...(metadata || {}) },
675
+ };
676
+ _evgovPsV2.set(id, p);
677
+ return { ...p, metadata: { ...p.metadata } };
678
+ }
679
+ export function activateEvgovProfileV2(id) {
680
+ const p = _evgovPsV2.get(id);
681
+ if (!p) throw new Error(`evgov profile ${id} not found`);
682
+ const isInitial = p.status === EVGOV_PROFILE_MATURITY_V2.PENDING;
683
+ _evgovCheckP(p.status, EVGOV_PROFILE_MATURITY_V2.ACTIVE);
684
+ if (isInitial && _evgovCountActive(p.owner) >= _evgovMaxActive)
685
+ throw new Error(`max active evgov profiles for owner ${p.owner} reached`);
686
+ const now = Date.now();
687
+ p.status = EVGOV_PROFILE_MATURITY_V2.ACTIVE;
688
+ p.updatedAt = now;
689
+ p.lastTouchedAt = now;
690
+ if (!p.activatedAt) p.activatedAt = now;
691
+ return { ...p, metadata: { ...p.metadata } };
692
+ }
693
+ export function pauseEvgovProfileV2(id) {
694
+ const p = _evgovPsV2.get(id);
695
+ if (!p) throw new Error(`evgov profile ${id} not found`);
696
+ _evgovCheckP(p.status, EVGOV_PROFILE_MATURITY_V2.PAUSED);
697
+ p.status = EVGOV_PROFILE_MATURITY_V2.PAUSED;
698
+ p.updatedAt = Date.now();
699
+ return { ...p, metadata: { ...p.metadata } };
700
+ }
701
+ export function archiveEvgovProfileV2(id) {
702
+ const p = _evgovPsV2.get(id);
703
+ if (!p) throw new Error(`evgov profile ${id} not found`);
704
+ _evgovCheckP(p.status, EVGOV_PROFILE_MATURITY_V2.ARCHIVED);
705
+ const now = Date.now();
706
+ p.status = EVGOV_PROFILE_MATURITY_V2.ARCHIVED;
707
+ p.updatedAt = now;
708
+ if (!p.archivedAt) p.archivedAt = now;
709
+ return { ...p, metadata: { ...p.metadata } };
710
+ }
711
+ export function touchEvgovProfileV2(id) {
712
+ const p = _evgovPsV2.get(id);
713
+ if (!p) throw new Error(`evgov profile ${id} not found`);
714
+ if (_evgovPTerminal.has(p.status))
715
+ throw new Error(`cannot touch terminal evgov profile ${id}`);
716
+ const now = Date.now();
717
+ p.lastTouchedAt = now;
718
+ p.updatedAt = now;
719
+ return { ...p, metadata: { ...p.metadata } };
720
+ }
721
+ export function getEvgovProfileV2(id) {
722
+ const p = _evgovPsV2.get(id);
723
+ if (!p) return null;
724
+ return { ...p, metadata: { ...p.metadata } };
725
+ }
726
+ export function listEvgovProfilesV2() {
727
+ return [..._evgovPsV2.values()].map((p) => ({
728
+ ...p,
729
+ metadata: { ...p.metadata },
730
+ }));
731
+ }
732
+ export function createEvgovProposalV2({ id, profileId, topic, metadata } = {}) {
733
+ if (!id || !profileId) throw new Error("id and profileId required");
734
+ if (_evgovJsV2.has(id))
735
+ throw new Error(`evgov proposal ${id} already exists`);
736
+ if (!_evgovPsV2.has(profileId))
737
+ throw new Error(`evgov profile ${profileId} not found`);
738
+ if (_evgovCountPending(profileId) >= _evgovMaxPending)
739
+ throw new Error(
740
+ `max pending evgov proposals for profile ${profileId} reached`,
741
+ );
742
+ const now = Date.now();
743
+ const j = {
744
+ id,
745
+ profileId,
746
+ topic: topic || "",
747
+ status: EVGOV_PROPOSAL_LIFECYCLE_V2.QUEUED,
748
+ createdAt: now,
749
+ updatedAt: now,
750
+ startedAt: null,
751
+ settledAt: null,
752
+ metadata: { ...(metadata || {}) },
753
+ };
754
+ _evgovJsV2.set(id, j);
755
+ return { ...j, metadata: { ...j.metadata } };
756
+ }
757
+ export function reviewingEvgovProposalV2(id) {
758
+ const j = _evgovJsV2.get(id);
759
+ if (!j) throw new Error(`evgov proposal ${id} not found`);
760
+ _evgovCheckJ(j.status, EVGOV_PROPOSAL_LIFECYCLE_V2.REVIEWING);
761
+ const now = Date.now();
762
+ j.status = EVGOV_PROPOSAL_LIFECYCLE_V2.REVIEWING;
763
+ j.updatedAt = now;
764
+ if (!j.startedAt) j.startedAt = now;
765
+ return { ...j, metadata: { ...j.metadata } };
766
+ }
767
+ export function completeProposalEvgovV2(id) {
768
+ const j = _evgovJsV2.get(id);
769
+ if (!j) throw new Error(`evgov proposal ${id} not found`);
770
+ _evgovCheckJ(j.status, EVGOV_PROPOSAL_LIFECYCLE_V2.REVIEWED);
771
+ const now = Date.now();
772
+ j.status = EVGOV_PROPOSAL_LIFECYCLE_V2.REVIEWED;
773
+ j.updatedAt = now;
774
+ if (!j.settledAt) j.settledAt = now;
775
+ return { ...j, metadata: { ...j.metadata } };
776
+ }
777
+ export function failEvgovProposalV2(id, reason) {
778
+ const j = _evgovJsV2.get(id);
779
+ if (!j) throw new Error(`evgov proposal ${id} not found`);
780
+ _evgovCheckJ(j.status, EVGOV_PROPOSAL_LIFECYCLE_V2.FAILED);
781
+ const now = Date.now();
782
+ j.status = EVGOV_PROPOSAL_LIFECYCLE_V2.FAILED;
783
+ j.updatedAt = now;
784
+ if (!j.settledAt) j.settledAt = now;
785
+ if (reason) j.metadata.failReason = String(reason);
786
+ return { ...j, metadata: { ...j.metadata } };
787
+ }
788
+ export function cancelEvgovProposalV2(id, reason) {
789
+ const j = _evgovJsV2.get(id);
790
+ if (!j) throw new Error(`evgov proposal ${id} not found`);
791
+ _evgovCheckJ(j.status, EVGOV_PROPOSAL_LIFECYCLE_V2.CANCELLED);
792
+ const now = Date.now();
793
+ j.status = EVGOV_PROPOSAL_LIFECYCLE_V2.CANCELLED;
794
+ j.updatedAt = now;
795
+ if (!j.settledAt) j.settledAt = now;
796
+ if (reason) j.metadata.cancelReason = String(reason);
797
+ return { ...j, metadata: { ...j.metadata } };
798
+ }
799
+ export function getEvgovProposalV2(id) {
800
+ const j = _evgovJsV2.get(id);
801
+ if (!j) return null;
802
+ return { ...j, metadata: { ...j.metadata } };
803
+ }
804
+ export function listEvgovProposalsV2() {
805
+ return [..._evgovJsV2.values()].map((j) => ({
806
+ ...j,
807
+ metadata: { ...j.metadata },
808
+ }));
809
+ }
810
+ export function autoPauseIdleEvgovProfilesV2({ now } = {}) {
811
+ const t = now ?? Date.now();
812
+ const flipped = [];
813
+ for (const p of _evgovPsV2.values())
814
+ if (
815
+ p.status === EVGOV_PROFILE_MATURITY_V2.ACTIVE &&
816
+ t - p.lastTouchedAt >= _evgovIdleMs
817
+ ) {
818
+ p.status = EVGOV_PROFILE_MATURITY_V2.PAUSED;
819
+ p.updatedAt = t;
820
+ flipped.push(p.id);
821
+ }
822
+ return { flipped, count: flipped.length };
823
+ }
824
+ export function autoFailStuckEvgovProposalsV2({ now } = {}) {
825
+ const t = now ?? Date.now();
826
+ const flipped = [];
827
+ for (const j of _evgovJsV2.values())
828
+ if (
829
+ j.status === EVGOV_PROPOSAL_LIFECYCLE_V2.REVIEWING &&
830
+ j.startedAt != null &&
831
+ t - j.startedAt >= _evgovStuckMs
832
+ ) {
833
+ j.status = EVGOV_PROPOSAL_LIFECYCLE_V2.FAILED;
834
+ j.updatedAt = t;
835
+ if (!j.settledAt) j.settledAt = t;
836
+ j.metadata.failReason = "auto-fail-stuck";
837
+ flipped.push(j.id);
838
+ }
839
+ return { flipped, count: flipped.length };
840
+ }
841
+ export function getEvomapGovernanceGovStatsV2() {
842
+ const profilesByStatus = {};
843
+ for (const v of Object.values(EVGOV_PROFILE_MATURITY_V2))
844
+ profilesByStatus[v] = 0;
845
+ for (const p of _evgovPsV2.values()) profilesByStatus[p.status]++;
846
+ const proposalsByStatus = {};
847
+ for (const v of Object.values(EVGOV_PROPOSAL_LIFECYCLE_V2))
848
+ proposalsByStatus[v] = 0;
849
+ for (const j of _evgovJsV2.values()) proposalsByStatus[j.status]++;
850
+ return {
851
+ totalEvgovProfilesV2: _evgovPsV2.size,
852
+ totalEvgovProposalsV2: _evgovJsV2.size,
853
+ maxActiveEvgovProfilesPerOwner: _evgovMaxActive,
854
+ maxPendingEvgovProposalsPerProfile: _evgovMaxPending,
855
+ evgovProfileIdleMs: _evgovIdleMs,
856
+ evgovProposalStuckMs: _evgovStuckMs,
857
+ profilesByStatus,
858
+ proposalsByStatus,
859
+ };
860
+ }