chainlesschain 0.145.0 → 0.156.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/README.md +52 -3
  2. package/package.json +1 -1
  3. package/src/commands/a2a.js +201 -0
  4. package/src/commands/activitypub.js +207 -0
  5. package/src/commands/agent-network.js +217 -0
  6. package/src/commands/agent.js +1250 -0
  7. package/src/commands/automation.js +201 -0
  8. package/src/commands/bi.js +203 -0
  9. package/src/commands/browse.js +213 -0
  10. package/src/commands/chat.js +605 -0
  11. package/src/commands/cli-anything.js +426 -0
  12. package/src/commands/codegen.js +207 -0
  13. package/src/commands/collab.js +211 -0
  14. package/src/commands/compliance.js +822 -0
  15. package/src/commands/config.js +213 -0
  16. package/src/commands/cowork.js +1666 -0
  17. package/src/commands/crosschain.js +203 -0
  18. package/src/commands/dao.js +203 -0
  19. package/src/commands/dbevo.js +227 -0
  20. package/src/commands/dev.js +207 -0
  21. package/src/commands/did-v2.js +217 -0
  22. package/src/commands/did.js +221 -0
  23. package/src/commands/dlp.js +213 -0
  24. package/src/commands/economy.js +199 -0
  25. package/src/commands/encrypt.js +201 -0
  26. package/src/commands/evolution.js +199 -0
  27. package/src/commands/evomap.js +830 -0
  28. package/src/commands/export.js +213 -0
  29. package/src/commands/federation.js +209 -0
  30. package/src/commands/fusion.js +205 -0
  31. package/src/commands/governance.js +209 -0
  32. package/src/commands/hmemory.js +203 -0
  33. package/src/commands/hook.js +209 -0
  34. package/src/commands/import.js +209 -0
  35. package/src/commands/inference.js +207 -0
  36. package/src/commands/infra.js +203 -0
  37. package/src/commands/instinct.js +209 -0
  38. package/src/commands/ipfs.js +207 -0
  39. package/src/commands/kg.js +195 -0
  40. package/src/commands/llm.js +426 -0
  41. package/src/commands/matrix.js +207 -0
  42. package/src/commands/mcp.js +217 -0
  43. package/src/commands/memory.js +412 -0
  44. package/src/commands/multimodal.js +203 -0
  45. package/src/commands/nlprog.js +225 -0
  46. package/src/commands/nostr.js +209 -0
  47. package/src/commands/note.js +205 -0
  48. package/src/commands/ops.js +219 -0
  49. package/src/commands/orchestrate.js +406 -0
  50. package/src/commands/org.js +209 -0
  51. package/src/commands/p2p.js +209 -0
  52. package/src/commands/perception.js +209 -0
  53. package/src/commands/permmem.js +203 -0
  54. package/src/commands/pipeline.js +199 -0
  55. package/src/commands/planmode.js +426 -0
  56. package/src/commands/plugin-ecosystem.js +209 -0
  57. package/src/commands/plugin.js +209 -0
  58. package/src/commands/pqc.js +213 -0
  59. package/src/commands/quantization.js +207 -0
  60. package/src/commands/rcache.js +205 -0
  61. package/src/commands/recommend.js +233 -0
  62. package/src/commands/runtime.js +205 -0
  63. package/src/commands/scim.js +209 -0
  64. package/src/commands/services.js +207 -0
  65. package/src/commands/session.js +209 -0
  66. package/src/commands/setup.js +205 -0
  67. package/src/commands/skill.js +414 -0
  68. package/src/commands/social.js +201 -0
  69. package/src/commands/sso.js +209 -0
  70. package/src/commands/start.js +209 -0
  71. package/src/commands/stream.js +213 -0
  72. package/src/commands/sync.js +209 -0
  73. package/src/commands/tech.js +209 -0
  74. package/src/commands/tenant.js +217 -0
  75. package/src/commands/tokens.js +209 -0
  76. package/src/commands/trust.js +217 -0
  77. package/src/commands/ui.js +225 -0
  78. package/src/commands/wallet.js +209 -0
  79. package/src/commands/workflow.js +412 -0
  80. package/src/index.js +252 -0
  81. package/src/lib/a2a-protocol.js +332 -0
  82. package/src/lib/activitypub-bridge.js +334 -0
  83. package/src/lib/agent-coordinator.js +334 -0
  84. package/src/lib/agent-economy.js +334 -0
  85. package/src/lib/agent-network.js +341 -0
  86. package/src/lib/agent-router.js +333 -0
  87. package/src/lib/aiops.js +346 -0
  88. package/src/lib/automation-engine.js +335 -0
  89. package/src/lib/autonomous-agent.js +332 -0
  90. package/src/lib/autonomous-developer.js +332 -0
  91. package/src/lib/bi-engine.js +333 -0
  92. package/src/lib/browser-automation.js +334 -0
  93. package/src/lib/chat-core.js +335 -0
  94. package/src/lib/cli-anything-bridge.js +341 -0
  95. package/src/lib/cli-context-engineering.js +351 -0
  96. package/src/lib/code-agent.js +339 -0
  97. package/src/lib/collaboration-governance.js +334 -0
  98. package/src/lib/community-governance.js +346 -0
  99. package/src/lib/compliance-manager.js +334 -0
  100. package/src/lib/content-recommendation.js +351 -0
  101. package/src/lib/cowork-adapter.js +336 -0
  102. package/src/lib/cowork-evomap-adapter.js +341 -0
  103. package/src/lib/cowork-mcp-tools.js +341 -0
  104. package/src/lib/cowork-observe-html.js +341 -0
  105. package/src/lib/cowork-observe.js +341 -0
  106. package/src/lib/cowork-share.js +338 -0
  107. package/src/lib/cowork-task-templates.js +342 -1
  108. package/src/lib/cowork-template-marketplace.js +340 -0
  109. package/src/lib/cross-chain.js +339 -0
  110. package/src/lib/crypto-manager.js +334 -0
  111. package/src/lib/dao-governance.js +339 -0
  112. package/src/lib/dbevo.js +351 -0
  113. package/src/lib/decentral-infra.js +330 -0
  114. package/src/lib/did-manager.js +341 -0
  115. package/src/lib/did-v2-manager.js +341 -0
  116. package/src/lib/dlp-engine.js +339 -0
  117. package/src/lib/downloader.js +334 -0
  118. package/src/lib/evolution-system.js +334 -0
  119. package/src/lib/evomap-client.js +342 -0
  120. package/src/lib/evomap-federation.js +338 -0
  121. package/src/lib/evomap-governance.js +334 -0
  122. package/src/lib/evomap-manager.js +330 -0
  123. package/src/lib/execution-backend.js +330 -0
  124. package/src/lib/federation-hardening.js +340 -0
  125. package/src/lib/hashline.js +338 -0
  126. package/src/lib/hierarchical-memory.js +334 -0
  127. package/src/lib/hook-manager.js +341 -0
  128. package/src/lib/inference-network.js +341 -0
  129. package/src/lib/instinct-manager.js +346 -0
  130. package/src/lib/interaction-adapter.js +330 -0
  131. package/src/lib/interactive-planner.js +354 -0
  132. package/src/lib/ipfs-storage.js +334 -0
  133. package/src/lib/knowledge-exporter.js +341 -0
  134. package/src/lib/knowledge-graph.js +331 -0
  135. package/src/lib/knowledge-importer.js +341 -0
  136. package/src/lib/llm-providers.js +346 -0
  137. package/src/lib/matrix-bridge.js +339 -0
  138. package/src/lib/mcp-registry.js +346 -0
  139. package/src/lib/memory-manager.js +336 -0
  140. package/src/lib/multimodal.js +330 -0
  141. package/src/lib/nl-programming.js +341 -0
  142. package/src/lib/nostr-bridge.js +336 -0
  143. package/src/lib/note-versioning.js +339 -0
  144. package/src/lib/org-manager.js +336 -0
  145. package/src/lib/p2p-manager.js +341 -0
  146. package/src/lib/perception.js +346 -0
  147. package/src/lib/permanent-memory.js +327 -0
  148. package/src/lib/pipeline-orchestrator.js +332 -0
  149. package/src/lib/plan-mode.js +336 -0
  150. package/src/lib/plugin-autodiscovery.js +334 -0
  151. package/src/lib/plugin-ecosystem.js +346 -0
  152. package/src/lib/pqc-manager.js +346 -0
  153. package/src/lib/process-manager.js +336 -0
  154. package/src/lib/protocol-fusion.js +338 -0
  155. package/src/lib/provider-options.js +346 -0
  156. package/src/lib/provider-stream.js +348 -0
  157. package/src/lib/quantization.js +337 -0
  158. package/src/lib/response-cache.js +333 -0
  159. package/src/lib/scim-manager.js +346 -0
  160. package/src/lib/service-manager.js +337 -0
  161. package/src/lib/session-core-singletons.js +341 -0
  162. package/src/lib/session-manager.js +334 -0
  163. package/src/lib/skill-loader.js +334 -0
  164. package/src/lib/skill-mcp.js +336 -0
  165. package/src/lib/social-manager.js +330 -0
  166. package/src/lib/sso-manager.js +340 -0
  167. package/src/lib/stix-parser.js +346 -0
  168. package/src/lib/sub-agent-context.js +343 -0
  169. package/src/lib/sub-agent-profiles.js +335 -0
  170. package/src/lib/sub-agent-registry.js +336 -0
  171. package/src/lib/sync-manager.js +336 -0
  172. package/src/lib/tech-learning-engine.js +341 -0
  173. package/src/lib/tenant-saas.js +341 -0
  174. package/src/lib/threat-intel.js +330 -0
  175. package/src/lib/todo-manager.js +336 -0
  176. package/src/lib/token-tracker.js +336 -0
  177. package/src/lib/trust-security.js +343 -0
  178. package/src/lib/ueba.js +340 -0
  179. package/src/lib/universal-runtime.js +330 -0
  180. package/src/lib/wallet-manager.js +336 -0
  181. package/src/lib/web-ui-server.js +348 -0
  182. package/src/lib/workflow-engine.js +330 -0
  183. package/src/lib/workflow-expr.js +346 -0
  184. package/src/lib/ws-chat-handler.js +337 -0
@@ -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
+ }
@@ -192,3 +192,337 @@ function formatBytes(bytes) {
192
192
  }
193
193
 
194
194
  export { resolveAssetUrl, formatBytes };
195
+
196
+ // =====================================================================
197
+ // downloader V2 governance overlay (iter27)
198
+ // =====================================================================
199
+ export const DLGOV_PROFILE_MATURITY_V2 = Object.freeze({
200
+ PENDING: "pending",
201
+ ACTIVE: "active",
202
+ STALE: "stale",
203
+ ARCHIVED: "archived",
204
+ });
205
+ export const DLGOV_DOWNLOAD_LIFECYCLE_V2 = Object.freeze({
206
+ QUEUED: "queued",
207
+ FETCHING: "fetching",
208
+ FETCHED: "fetched",
209
+ FAILED: "failed",
210
+ CANCELLED: "cancelled",
211
+ });
212
+ const _dlgovPTrans = new Map([
213
+ [
214
+ DLGOV_PROFILE_MATURITY_V2.PENDING,
215
+ new Set([
216
+ DLGOV_PROFILE_MATURITY_V2.ACTIVE,
217
+ DLGOV_PROFILE_MATURITY_V2.ARCHIVED,
218
+ ]),
219
+ ],
220
+ [
221
+ DLGOV_PROFILE_MATURITY_V2.ACTIVE,
222
+ new Set([
223
+ DLGOV_PROFILE_MATURITY_V2.STALE,
224
+ DLGOV_PROFILE_MATURITY_V2.ARCHIVED,
225
+ ]),
226
+ ],
227
+ [
228
+ DLGOV_PROFILE_MATURITY_V2.STALE,
229
+ new Set([
230
+ DLGOV_PROFILE_MATURITY_V2.ACTIVE,
231
+ DLGOV_PROFILE_MATURITY_V2.ARCHIVED,
232
+ ]),
233
+ ],
234
+ [DLGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
235
+ ]);
236
+ const _dlgovPTerminal = new Set([DLGOV_PROFILE_MATURITY_V2.ARCHIVED]);
237
+ const _dlgovJTrans = new Map([
238
+ [
239
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.QUEUED,
240
+ new Set([
241
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING,
242
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.CANCELLED,
243
+ ]),
244
+ ],
245
+ [
246
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING,
247
+ new Set([
248
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHED,
249
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.FAILED,
250
+ DLGOV_DOWNLOAD_LIFECYCLE_V2.CANCELLED,
251
+ ]),
252
+ ],
253
+ [DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHED, new Set()],
254
+ [DLGOV_DOWNLOAD_LIFECYCLE_V2.FAILED, new Set()],
255
+ [DLGOV_DOWNLOAD_LIFECYCLE_V2.CANCELLED, new Set()],
256
+ ]);
257
+ const _dlgovPsV2 = new Map();
258
+ const _dlgovJsV2 = new Map();
259
+ let _dlgovMaxActive = 6,
260
+ _dlgovMaxPending = 15,
261
+ _dlgovIdleMs = 30 * 24 * 60 * 60 * 1000,
262
+ _dlgovStuckMs = 60 * 1000;
263
+ function _dlgovPos(n, label) {
264
+ const v = Math.floor(Number(n));
265
+ if (!Number.isFinite(v) || v <= 0)
266
+ throw new Error(`${label} must be positive integer`);
267
+ return v;
268
+ }
269
+ function _dlgovCheckP(from, to) {
270
+ const a = _dlgovPTrans.get(from);
271
+ if (!a || !a.has(to))
272
+ throw new Error(`invalid dlgov profile transition ${from} → ${to}`);
273
+ }
274
+ function _dlgovCheckJ(from, to) {
275
+ const a = _dlgovJTrans.get(from);
276
+ if (!a || !a.has(to))
277
+ throw new Error(`invalid dlgov download transition ${from} → ${to}`);
278
+ }
279
+ function _dlgovCountActive(owner) {
280
+ let c = 0;
281
+ for (const p of _dlgovPsV2.values())
282
+ if (p.owner === owner && p.status === DLGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
283
+ return c;
284
+ }
285
+ function _dlgovCountPending(profileId) {
286
+ let c = 0;
287
+ for (const j of _dlgovJsV2.values())
288
+ if (
289
+ j.profileId === profileId &&
290
+ (j.status === DLGOV_DOWNLOAD_LIFECYCLE_V2.QUEUED ||
291
+ j.status === DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING)
292
+ )
293
+ c++;
294
+ return c;
295
+ }
296
+ export function setMaxActiveDlgovProfilesPerOwnerV2(n) {
297
+ _dlgovMaxActive = _dlgovPos(n, "maxActiveDlgovProfilesPerOwner");
298
+ }
299
+ export function getMaxActiveDlgovProfilesPerOwnerV2() {
300
+ return _dlgovMaxActive;
301
+ }
302
+ export function setMaxPendingDlgovDownloadsPerProfileV2(n) {
303
+ _dlgovMaxPending = _dlgovPos(n, "maxPendingDlgovDownloadsPerProfile");
304
+ }
305
+ export function getMaxPendingDlgovDownloadsPerProfileV2() {
306
+ return _dlgovMaxPending;
307
+ }
308
+ export function setDlgovProfileIdleMsV2(n) {
309
+ _dlgovIdleMs = _dlgovPos(n, "dlgovProfileIdleMs");
310
+ }
311
+ export function getDlgovProfileIdleMsV2() {
312
+ return _dlgovIdleMs;
313
+ }
314
+ export function setDlgovDownloadStuckMsV2(n) {
315
+ _dlgovStuckMs = _dlgovPos(n, "dlgovDownloadStuckMs");
316
+ }
317
+ export function getDlgovDownloadStuckMsV2() {
318
+ return _dlgovStuckMs;
319
+ }
320
+ export function _resetStateDownloaderGovV2() {
321
+ _dlgovPsV2.clear();
322
+ _dlgovJsV2.clear();
323
+ _dlgovMaxActive = 6;
324
+ _dlgovMaxPending = 15;
325
+ _dlgovIdleMs = 30 * 24 * 60 * 60 * 1000;
326
+ _dlgovStuckMs = 60 * 1000;
327
+ }
328
+ export function registerDlgovProfileV2({ id, owner, mirror, metadata } = {}) {
329
+ if (!id || !owner) throw new Error("id and owner required");
330
+ if (_dlgovPsV2.has(id)) throw new Error(`dlgov profile ${id} already exists`);
331
+ const now = Date.now();
332
+ const p = {
333
+ id,
334
+ owner,
335
+ mirror: mirror || "default",
336
+ status: DLGOV_PROFILE_MATURITY_V2.PENDING,
337
+ createdAt: now,
338
+ updatedAt: now,
339
+ lastTouchedAt: now,
340
+ activatedAt: null,
341
+ archivedAt: null,
342
+ metadata: { ...(metadata || {}) },
343
+ };
344
+ _dlgovPsV2.set(id, p);
345
+ return { ...p, metadata: { ...p.metadata } };
346
+ }
347
+ export function activateDlgovProfileV2(id) {
348
+ const p = _dlgovPsV2.get(id);
349
+ if (!p) throw new Error(`dlgov profile ${id} not found`);
350
+ const isInitial = p.status === DLGOV_PROFILE_MATURITY_V2.PENDING;
351
+ _dlgovCheckP(p.status, DLGOV_PROFILE_MATURITY_V2.ACTIVE);
352
+ if (isInitial && _dlgovCountActive(p.owner) >= _dlgovMaxActive)
353
+ throw new Error(`max active dlgov profiles for owner ${p.owner} reached`);
354
+ const now = Date.now();
355
+ p.status = DLGOV_PROFILE_MATURITY_V2.ACTIVE;
356
+ p.updatedAt = now;
357
+ p.lastTouchedAt = now;
358
+ if (!p.activatedAt) p.activatedAt = now;
359
+ return { ...p, metadata: { ...p.metadata } };
360
+ }
361
+ export function staleDlgovProfileV2(id) {
362
+ const p = _dlgovPsV2.get(id);
363
+ if (!p) throw new Error(`dlgov profile ${id} not found`);
364
+ _dlgovCheckP(p.status, DLGOV_PROFILE_MATURITY_V2.STALE);
365
+ p.status = DLGOV_PROFILE_MATURITY_V2.STALE;
366
+ p.updatedAt = Date.now();
367
+ return { ...p, metadata: { ...p.metadata } };
368
+ }
369
+ export function archiveDlgovProfileV2(id) {
370
+ const p = _dlgovPsV2.get(id);
371
+ if (!p) throw new Error(`dlgov profile ${id} not found`);
372
+ _dlgovCheckP(p.status, DLGOV_PROFILE_MATURITY_V2.ARCHIVED);
373
+ const now = Date.now();
374
+ p.status = DLGOV_PROFILE_MATURITY_V2.ARCHIVED;
375
+ p.updatedAt = now;
376
+ if (!p.archivedAt) p.archivedAt = now;
377
+ return { ...p, metadata: { ...p.metadata } };
378
+ }
379
+ export function touchDlgovProfileV2(id) {
380
+ const p = _dlgovPsV2.get(id);
381
+ if (!p) throw new Error(`dlgov profile ${id} not found`);
382
+ if (_dlgovPTerminal.has(p.status))
383
+ throw new Error(`cannot touch terminal dlgov profile ${id}`);
384
+ const now = Date.now();
385
+ p.lastTouchedAt = now;
386
+ p.updatedAt = now;
387
+ return { ...p, metadata: { ...p.metadata } };
388
+ }
389
+ export function getDlgovProfileV2(id) {
390
+ const p = _dlgovPsV2.get(id);
391
+ if (!p) return null;
392
+ return { ...p, metadata: { ...p.metadata } };
393
+ }
394
+ export function listDlgovProfilesV2() {
395
+ return [..._dlgovPsV2.values()].map((p) => ({
396
+ ...p,
397
+ metadata: { ...p.metadata },
398
+ }));
399
+ }
400
+ export function createDlgovDownloadV2({ id, profileId, url, metadata } = {}) {
401
+ if (!id || !profileId) throw new Error("id and profileId required");
402
+ if (_dlgovJsV2.has(id))
403
+ throw new Error(`dlgov download ${id} already exists`);
404
+ if (!_dlgovPsV2.has(profileId))
405
+ throw new Error(`dlgov profile ${profileId} not found`);
406
+ if (_dlgovCountPending(profileId) >= _dlgovMaxPending)
407
+ throw new Error(
408
+ `max pending dlgov downloads for profile ${profileId} reached`,
409
+ );
410
+ const now = Date.now();
411
+ const j = {
412
+ id,
413
+ profileId,
414
+ url: url || "",
415
+ status: DLGOV_DOWNLOAD_LIFECYCLE_V2.QUEUED,
416
+ createdAt: now,
417
+ updatedAt: now,
418
+ startedAt: null,
419
+ settledAt: null,
420
+ metadata: { ...(metadata || {}) },
421
+ };
422
+ _dlgovJsV2.set(id, j);
423
+ return { ...j, metadata: { ...j.metadata } };
424
+ }
425
+ export function fetchingDlgovDownloadV2(id) {
426
+ const j = _dlgovJsV2.get(id);
427
+ if (!j) throw new Error(`dlgov download ${id} not found`);
428
+ _dlgovCheckJ(j.status, DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING);
429
+ const now = Date.now();
430
+ j.status = DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING;
431
+ j.updatedAt = now;
432
+ if (!j.startedAt) j.startedAt = now;
433
+ return { ...j, metadata: { ...j.metadata } };
434
+ }
435
+ export function completeDownloadDlgovV2(id) {
436
+ const j = _dlgovJsV2.get(id);
437
+ if (!j) throw new Error(`dlgov download ${id} not found`);
438
+ _dlgovCheckJ(j.status, DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHED);
439
+ const now = Date.now();
440
+ j.status = DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHED;
441
+ j.updatedAt = now;
442
+ if (!j.settledAt) j.settledAt = now;
443
+ return { ...j, metadata: { ...j.metadata } };
444
+ }
445
+ export function failDlgovDownloadV2(id, reason) {
446
+ const j = _dlgovJsV2.get(id);
447
+ if (!j) throw new Error(`dlgov download ${id} not found`);
448
+ _dlgovCheckJ(j.status, DLGOV_DOWNLOAD_LIFECYCLE_V2.FAILED);
449
+ const now = Date.now();
450
+ j.status = DLGOV_DOWNLOAD_LIFECYCLE_V2.FAILED;
451
+ j.updatedAt = now;
452
+ if (!j.settledAt) j.settledAt = now;
453
+ if (reason) j.metadata.failReason = String(reason);
454
+ return { ...j, metadata: { ...j.metadata } };
455
+ }
456
+ export function cancelDlgovDownloadV2(id, reason) {
457
+ const j = _dlgovJsV2.get(id);
458
+ if (!j) throw new Error(`dlgov download ${id} not found`);
459
+ _dlgovCheckJ(j.status, DLGOV_DOWNLOAD_LIFECYCLE_V2.CANCELLED);
460
+ const now = Date.now();
461
+ j.status = DLGOV_DOWNLOAD_LIFECYCLE_V2.CANCELLED;
462
+ j.updatedAt = now;
463
+ if (!j.settledAt) j.settledAt = now;
464
+ if (reason) j.metadata.cancelReason = String(reason);
465
+ return { ...j, metadata: { ...j.metadata } };
466
+ }
467
+ export function getDlgovDownloadV2(id) {
468
+ const j = _dlgovJsV2.get(id);
469
+ if (!j) return null;
470
+ return { ...j, metadata: { ...j.metadata } };
471
+ }
472
+ export function listDlgovDownloadsV2() {
473
+ return [..._dlgovJsV2.values()].map((j) => ({
474
+ ...j,
475
+ metadata: { ...j.metadata },
476
+ }));
477
+ }
478
+ export function autoStaleIdleDlgovProfilesV2({ now } = {}) {
479
+ const t = now ?? Date.now();
480
+ const flipped = [];
481
+ for (const p of _dlgovPsV2.values())
482
+ if (
483
+ p.status === DLGOV_PROFILE_MATURITY_V2.ACTIVE &&
484
+ t - p.lastTouchedAt >= _dlgovIdleMs
485
+ ) {
486
+ p.status = DLGOV_PROFILE_MATURITY_V2.STALE;
487
+ p.updatedAt = t;
488
+ flipped.push(p.id);
489
+ }
490
+ return { flipped, count: flipped.length };
491
+ }
492
+ export function autoFailStuckDlgovDownloadsV2({ now } = {}) {
493
+ const t = now ?? Date.now();
494
+ const flipped = [];
495
+ for (const j of _dlgovJsV2.values())
496
+ if (
497
+ j.status === DLGOV_DOWNLOAD_LIFECYCLE_V2.FETCHING &&
498
+ j.startedAt != null &&
499
+ t - j.startedAt >= _dlgovStuckMs
500
+ ) {
501
+ j.status = DLGOV_DOWNLOAD_LIFECYCLE_V2.FAILED;
502
+ j.updatedAt = t;
503
+ if (!j.settledAt) j.settledAt = t;
504
+ j.metadata.failReason = "auto-fail-stuck";
505
+ flipped.push(j.id);
506
+ }
507
+ return { flipped, count: flipped.length };
508
+ }
509
+ export function getDownloaderGovStatsV2() {
510
+ const profilesByStatus = {};
511
+ for (const v of Object.values(DLGOV_PROFILE_MATURITY_V2))
512
+ profilesByStatus[v] = 0;
513
+ for (const p of _dlgovPsV2.values()) profilesByStatus[p.status]++;
514
+ const downloadsByStatus = {};
515
+ for (const v of Object.values(DLGOV_DOWNLOAD_LIFECYCLE_V2))
516
+ downloadsByStatus[v] = 0;
517
+ for (const j of _dlgovJsV2.values()) downloadsByStatus[j.status]++;
518
+ return {
519
+ totalDlgovProfilesV2: _dlgovPsV2.size,
520
+ totalDlgovDownloadsV2: _dlgovJsV2.size,
521
+ maxActiveDlgovProfilesPerOwner: _dlgovMaxActive,
522
+ maxPendingDlgovDownloadsPerProfile: _dlgovMaxPending,
523
+ dlgovProfileIdleMs: _dlgovIdleMs,
524
+ dlgovDownloadStuckMs: _dlgovStuckMs,
525
+ profilesByStatus,
526
+ downloadsByStatus,
527
+ };
528
+ }