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
@@ -1069,3 +1069,333 @@ export function _resetStateV2() {
1069
1069
  _sessionsV2.clear();
1070
1070
  _artifactsV2.clear();
1071
1071
  }
1072
+
1073
+ // =====================================================================
1074
+ // multimodal V2 governance overlay (iter18)
1075
+ // =====================================================================
1076
+ export const MMGOV_PROFILE_MATURITY_V2 = Object.freeze({
1077
+ PENDING: "pending",
1078
+ ACTIVE: "active",
1079
+ STALE: "stale",
1080
+ ARCHIVED: "archived",
1081
+ });
1082
+ export const MMGOV_JOB_LIFECYCLE_V2 = Object.freeze({
1083
+ QUEUED: "queued",
1084
+ PROCESSING: "processing",
1085
+ PROCESSED: "processed",
1086
+ FAILED: "failed",
1087
+ CANCELLED: "cancelled",
1088
+ });
1089
+ const _mmgovPTrans = new Map([
1090
+ [
1091
+ MMGOV_PROFILE_MATURITY_V2.PENDING,
1092
+ new Set([
1093
+ MMGOV_PROFILE_MATURITY_V2.ACTIVE,
1094
+ MMGOV_PROFILE_MATURITY_V2.ARCHIVED,
1095
+ ]),
1096
+ ],
1097
+ [
1098
+ MMGOV_PROFILE_MATURITY_V2.ACTIVE,
1099
+ new Set([
1100
+ MMGOV_PROFILE_MATURITY_V2.STALE,
1101
+ MMGOV_PROFILE_MATURITY_V2.ARCHIVED,
1102
+ ]),
1103
+ ],
1104
+ [
1105
+ MMGOV_PROFILE_MATURITY_V2.STALE,
1106
+ new Set([
1107
+ MMGOV_PROFILE_MATURITY_V2.ACTIVE,
1108
+ MMGOV_PROFILE_MATURITY_V2.ARCHIVED,
1109
+ ]),
1110
+ ],
1111
+ [MMGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1112
+ ]);
1113
+ const _mmgovPTerminal = new Set([MMGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1114
+ const _mmgovJTrans = new Map([
1115
+ [
1116
+ MMGOV_JOB_LIFECYCLE_V2.QUEUED,
1117
+ new Set([
1118
+ MMGOV_JOB_LIFECYCLE_V2.PROCESSING,
1119
+ MMGOV_JOB_LIFECYCLE_V2.CANCELLED,
1120
+ ]),
1121
+ ],
1122
+ [
1123
+ MMGOV_JOB_LIFECYCLE_V2.PROCESSING,
1124
+ new Set([
1125
+ MMGOV_JOB_LIFECYCLE_V2.PROCESSED,
1126
+ MMGOV_JOB_LIFECYCLE_V2.FAILED,
1127
+ MMGOV_JOB_LIFECYCLE_V2.CANCELLED,
1128
+ ]),
1129
+ ],
1130
+ [MMGOV_JOB_LIFECYCLE_V2.PROCESSED, new Set()],
1131
+ [MMGOV_JOB_LIFECYCLE_V2.FAILED, new Set()],
1132
+ [MMGOV_JOB_LIFECYCLE_V2.CANCELLED, new Set()],
1133
+ ]);
1134
+ const _mmgovPsV2 = new Map();
1135
+ const _mmgovJsV2 = new Map();
1136
+ let _mmgovMaxActive = 6,
1137
+ _mmgovMaxPending = 15,
1138
+ _mmgovIdleMs = 30 * 24 * 60 * 60 * 1000,
1139
+ _mmgovStuckMs = 60 * 1000;
1140
+ function _mmgovPos(n, label) {
1141
+ const v = Math.floor(Number(n));
1142
+ if (!Number.isFinite(v) || v <= 0)
1143
+ throw new Error(`${label} must be positive integer`);
1144
+ return v;
1145
+ }
1146
+ function _mmgovCheckP(from, to) {
1147
+ const a = _mmgovPTrans.get(from);
1148
+ if (!a || !a.has(to))
1149
+ throw new Error(`invalid mmgov profile transition ${from} → ${to}`);
1150
+ }
1151
+ function _mmgovCheckJ(from, to) {
1152
+ const a = _mmgovJTrans.get(from);
1153
+ if (!a || !a.has(to))
1154
+ throw new Error(`invalid mmgov job transition ${from} → ${to}`);
1155
+ }
1156
+ function _mmgovCountActive(owner) {
1157
+ let c = 0;
1158
+ for (const p of _mmgovPsV2.values())
1159
+ if (p.owner === owner && p.status === MMGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
1160
+ return c;
1161
+ }
1162
+ function _mmgovCountPending(profileId) {
1163
+ let c = 0;
1164
+ for (const j of _mmgovJsV2.values())
1165
+ if (
1166
+ j.profileId === profileId &&
1167
+ (j.status === MMGOV_JOB_LIFECYCLE_V2.QUEUED ||
1168
+ j.status === MMGOV_JOB_LIFECYCLE_V2.PROCESSING)
1169
+ )
1170
+ c++;
1171
+ return c;
1172
+ }
1173
+ export function setMaxActiveMmgovProfilesPerOwnerV2(n) {
1174
+ _mmgovMaxActive = _mmgovPos(n, "maxActiveMmgovProfilesPerOwner");
1175
+ }
1176
+ export function getMaxActiveMmgovProfilesPerOwnerV2() {
1177
+ return _mmgovMaxActive;
1178
+ }
1179
+ export function setMaxPendingMmgovJobsPerProfileV2(n) {
1180
+ _mmgovMaxPending = _mmgovPos(n, "maxPendingMmgovJobsPerProfile");
1181
+ }
1182
+ export function getMaxPendingMmgovJobsPerProfileV2() {
1183
+ return _mmgovMaxPending;
1184
+ }
1185
+ export function setMmgovProfileIdleMsV2(n) {
1186
+ _mmgovIdleMs = _mmgovPos(n, "mmgovProfileIdleMs");
1187
+ }
1188
+ export function getMmgovProfileIdleMsV2() {
1189
+ return _mmgovIdleMs;
1190
+ }
1191
+ export function setMmgovJobStuckMsV2(n) {
1192
+ _mmgovStuckMs = _mmgovPos(n, "mmgovJobStuckMs");
1193
+ }
1194
+ export function getMmgovJobStuckMsV2() {
1195
+ return _mmgovStuckMs;
1196
+ }
1197
+ export function _resetStateMultimodalGovV2() {
1198
+ _mmgovPsV2.clear();
1199
+ _mmgovJsV2.clear();
1200
+ _mmgovMaxActive = 6;
1201
+ _mmgovMaxPending = 15;
1202
+ _mmgovIdleMs = 30 * 24 * 60 * 60 * 1000;
1203
+ _mmgovStuckMs = 60 * 1000;
1204
+ }
1205
+ export function registerMmgovProfileV2({ id, owner, kind, metadata } = {}) {
1206
+ if (!id || !owner) throw new Error("id and owner required");
1207
+ if (_mmgovPsV2.has(id)) throw new Error(`mmgov profile ${id} already exists`);
1208
+ const now = Date.now();
1209
+ const p = {
1210
+ id,
1211
+ owner,
1212
+ kind: kind || "text",
1213
+ status: MMGOV_PROFILE_MATURITY_V2.PENDING,
1214
+ createdAt: now,
1215
+ updatedAt: now,
1216
+ lastTouchedAt: now,
1217
+ activatedAt: null,
1218
+ archivedAt: null,
1219
+ metadata: { ...(metadata || {}) },
1220
+ };
1221
+ _mmgovPsV2.set(id, p);
1222
+ return { ...p, metadata: { ...p.metadata } };
1223
+ }
1224
+ export function activateMmgovProfileV2(id) {
1225
+ const p = _mmgovPsV2.get(id);
1226
+ if (!p) throw new Error(`mmgov profile ${id} not found`);
1227
+ const isInitial = p.status === MMGOV_PROFILE_MATURITY_V2.PENDING;
1228
+ _mmgovCheckP(p.status, MMGOV_PROFILE_MATURITY_V2.ACTIVE);
1229
+ if (isInitial && _mmgovCountActive(p.owner) >= _mmgovMaxActive)
1230
+ throw new Error(`max active mmgov profiles for owner ${p.owner} reached`);
1231
+ const now = Date.now();
1232
+ p.status = MMGOV_PROFILE_MATURITY_V2.ACTIVE;
1233
+ p.updatedAt = now;
1234
+ p.lastTouchedAt = now;
1235
+ if (!p.activatedAt) p.activatedAt = now;
1236
+ return { ...p, metadata: { ...p.metadata } };
1237
+ }
1238
+ export function staleMmgovProfileV2(id) {
1239
+ const p = _mmgovPsV2.get(id);
1240
+ if (!p) throw new Error(`mmgov profile ${id} not found`);
1241
+ _mmgovCheckP(p.status, MMGOV_PROFILE_MATURITY_V2.STALE);
1242
+ p.status = MMGOV_PROFILE_MATURITY_V2.STALE;
1243
+ p.updatedAt = Date.now();
1244
+ return { ...p, metadata: { ...p.metadata } };
1245
+ }
1246
+ export function archiveMmgovProfileV2(id) {
1247
+ const p = _mmgovPsV2.get(id);
1248
+ if (!p) throw new Error(`mmgov profile ${id} not found`);
1249
+ _mmgovCheckP(p.status, MMGOV_PROFILE_MATURITY_V2.ARCHIVED);
1250
+ const now = Date.now();
1251
+ p.status = MMGOV_PROFILE_MATURITY_V2.ARCHIVED;
1252
+ p.updatedAt = now;
1253
+ if (!p.archivedAt) p.archivedAt = now;
1254
+ return { ...p, metadata: { ...p.metadata } };
1255
+ }
1256
+ export function touchMmgovProfileV2(id) {
1257
+ const p = _mmgovPsV2.get(id);
1258
+ if (!p) throw new Error(`mmgov profile ${id} not found`);
1259
+ if (_mmgovPTerminal.has(p.status))
1260
+ throw new Error(`cannot touch terminal mmgov profile ${id}`);
1261
+ const now = Date.now();
1262
+ p.lastTouchedAt = now;
1263
+ p.updatedAt = now;
1264
+ return { ...p, metadata: { ...p.metadata } };
1265
+ }
1266
+ export function getMmgovProfileV2(id) {
1267
+ const p = _mmgovPsV2.get(id);
1268
+ if (!p) return null;
1269
+ return { ...p, metadata: { ...p.metadata } };
1270
+ }
1271
+ export function listMmgovProfilesV2() {
1272
+ return [..._mmgovPsV2.values()].map((p) => ({
1273
+ ...p,
1274
+ metadata: { ...p.metadata },
1275
+ }));
1276
+ }
1277
+ export function createMmgovJobV2({ id, profileId, input, metadata } = {}) {
1278
+ if (!id || !profileId) throw new Error("id and profileId required");
1279
+ if (_mmgovJsV2.has(id)) throw new Error(`mmgov job ${id} already exists`);
1280
+ if (!_mmgovPsV2.has(profileId))
1281
+ throw new Error(`mmgov profile ${profileId} not found`);
1282
+ if (_mmgovCountPending(profileId) >= _mmgovMaxPending)
1283
+ throw new Error(`max pending mmgov jobs for profile ${profileId} reached`);
1284
+ const now = Date.now();
1285
+ const j = {
1286
+ id,
1287
+ profileId,
1288
+ input: input || "",
1289
+ status: MMGOV_JOB_LIFECYCLE_V2.QUEUED,
1290
+ createdAt: now,
1291
+ updatedAt: now,
1292
+ startedAt: null,
1293
+ settledAt: null,
1294
+ metadata: { ...(metadata || {}) },
1295
+ };
1296
+ _mmgovJsV2.set(id, j);
1297
+ return { ...j, metadata: { ...j.metadata } };
1298
+ }
1299
+ export function processingMmgovJobV2(id) {
1300
+ const j = _mmgovJsV2.get(id);
1301
+ if (!j) throw new Error(`mmgov job ${id} not found`);
1302
+ _mmgovCheckJ(j.status, MMGOV_JOB_LIFECYCLE_V2.PROCESSING);
1303
+ const now = Date.now();
1304
+ j.status = MMGOV_JOB_LIFECYCLE_V2.PROCESSING;
1305
+ j.updatedAt = now;
1306
+ if (!j.startedAt) j.startedAt = now;
1307
+ return { ...j, metadata: { ...j.metadata } };
1308
+ }
1309
+ export function completeJobMmgovV2(id) {
1310
+ const j = _mmgovJsV2.get(id);
1311
+ if (!j) throw new Error(`mmgov job ${id} not found`);
1312
+ _mmgovCheckJ(j.status, MMGOV_JOB_LIFECYCLE_V2.PROCESSED);
1313
+ const now = Date.now();
1314
+ j.status = MMGOV_JOB_LIFECYCLE_V2.PROCESSED;
1315
+ j.updatedAt = now;
1316
+ if (!j.settledAt) j.settledAt = now;
1317
+ return { ...j, metadata: { ...j.metadata } };
1318
+ }
1319
+ export function failMmgovJobV2(id, reason) {
1320
+ const j = _mmgovJsV2.get(id);
1321
+ if (!j) throw new Error(`mmgov job ${id} not found`);
1322
+ _mmgovCheckJ(j.status, MMGOV_JOB_LIFECYCLE_V2.FAILED);
1323
+ const now = Date.now();
1324
+ j.status = MMGOV_JOB_LIFECYCLE_V2.FAILED;
1325
+ j.updatedAt = now;
1326
+ if (!j.settledAt) j.settledAt = now;
1327
+ if (reason) j.metadata.failReason = String(reason);
1328
+ return { ...j, metadata: { ...j.metadata } };
1329
+ }
1330
+ export function cancelMmgovJobV2(id, reason) {
1331
+ const j = _mmgovJsV2.get(id);
1332
+ if (!j) throw new Error(`mmgov job ${id} not found`);
1333
+ _mmgovCheckJ(j.status, MMGOV_JOB_LIFECYCLE_V2.CANCELLED);
1334
+ const now = Date.now();
1335
+ j.status = MMGOV_JOB_LIFECYCLE_V2.CANCELLED;
1336
+ j.updatedAt = now;
1337
+ if (!j.settledAt) j.settledAt = now;
1338
+ if (reason) j.metadata.cancelReason = String(reason);
1339
+ return { ...j, metadata: { ...j.metadata } };
1340
+ }
1341
+ export function getMmgovJobV2(id) {
1342
+ const j = _mmgovJsV2.get(id);
1343
+ if (!j) return null;
1344
+ return { ...j, metadata: { ...j.metadata } };
1345
+ }
1346
+ export function listMmgovJobsV2() {
1347
+ return [..._mmgovJsV2.values()].map((j) => ({
1348
+ ...j,
1349
+ metadata: { ...j.metadata },
1350
+ }));
1351
+ }
1352
+ export function autoStaleIdleMmgovProfilesV2({ now } = {}) {
1353
+ const t = now ?? Date.now();
1354
+ const flipped = [];
1355
+ for (const p of _mmgovPsV2.values())
1356
+ if (
1357
+ p.status === MMGOV_PROFILE_MATURITY_V2.ACTIVE &&
1358
+ t - p.lastTouchedAt >= _mmgovIdleMs
1359
+ ) {
1360
+ p.status = MMGOV_PROFILE_MATURITY_V2.STALE;
1361
+ p.updatedAt = t;
1362
+ flipped.push(p.id);
1363
+ }
1364
+ return { flipped, count: flipped.length };
1365
+ }
1366
+ export function autoFailStuckMmgovJobsV2({ now } = {}) {
1367
+ const t = now ?? Date.now();
1368
+ const flipped = [];
1369
+ for (const j of _mmgovJsV2.values())
1370
+ if (
1371
+ j.status === MMGOV_JOB_LIFECYCLE_V2.PROCESSING &&
1372
+ j.startedAt != null &&
1373
+ t - j.startedAt >= _mmgovStuckMs
1374
+ ) {
1375
+ j.status = MMGOV_JOB_LIFECYCLE_V2.FAILED;
1376
+ j.updatedAt = t;
1377
+ if (!j.settledAt) j.settledAt = t;
1378
+ j.metadata.failReason = "auto-fail-stuck";
1379
+ flipped.push(j.id);
1380
+ }
1381
+ return { flipped, count: flipped.length };
1382
+ }
1383
+ export function getMultimodalGovStatsV2() {
1384
+ const profilesByStatus = {};
1385
+ for (const v of Object.values(MMGOV_PROFILE_MATURITY_V2))
1386
+ profilesByStatus[v] = 0;
1387
+ for (const p of _mmgovPsV2.values()) profilesByStatus[p.status]++;
1388
+ const jobsByStatus = {};
1389
+ for (const v of Object.values(MMGOV_JOB_LIFECYCLE_V2)) jobsByStatus[v] = 0;
1390
+ for (const j of _mmgovJsV2.values()) jobsByStatus[j.status]++;
1391
+ return {
1392
+ totalMmgovProfilesV2: _mmgovPsV2.size,
1393
+ totalMmgovJobsV2: _mmgovJsV2.size,
1394
+ maxActiveMmgovProfilesPerOwner: _mmgovMaxActive,
1395
+ maxPendingMmgovJobsPerProfile: _mmgovMaxPending,
1396
+ mmgovProfileIdleMs: _mmgovIdleMs,
1397
+ mmgovJobStuckMs: _mmgovStuckMs,
1398
+ profilesByStatus,
1399
+ jobsByStatus,
1400
+ };
1401
+ }
@@ -936,3 +936,344 @@ export function _resetStateV2() {
936
936
  _specsV2.clear();
937
937
  _turnsV2.clear();
938
938
  }
939
+
940
+ // =====================================================================
941
+ // nl-programming V2 governance overlay (iter18)
942
+ // =====================================================================
943
+ export const NLPGOV_PROFILE_MATURITY_V2 = Object.freeze({
944
+ PENDING: "pending",
945
+ ACTIVE: "active",
946
+ STALE: "stale",
947
+ ARCHIVED: "archived",
948
+ });
949
+ export const NLPGOV_TRANSLATION_LIFECYCLE_V2 = Object.freeze({
950
+ QUEUED: "queued",
951
+ TRANSLATING: "translating",
952
+ TRANSLATED: "translated",
953
+ FAILED: "failed",
954
+ CANCELLED: "cancelled",
955
+ });
956
+ const _nlpgovPTrans = new Map([
957
+ [
958
+ NLPGOV_PROFILE_MATURITY_V2.PENDING,
959
+ new Set([
960
+ NLPGOV_PROFILE_MATURITY_V2.ACTIVE,
961
+ NLPGOV_PROFILE_MATURITY_V2.ARCHIVED,
962
+ ]),
963
+ ],
964
+ [
965
+ NLPGOV_PROFILE_MATURITY_V2.ACTIVE,
966
+ new Set([
967
+ NLPGOV_PROFILE_MATURITY_V2.STALE,
968
+ NLPGOV_PROFILE_MATURITY_V2.ARCHIVED,
969
+ ]),
970
+ ],
971
+ [
972
+ NLPGOV_PROFILE_MATURITY_V2.STALE,
973
+ new Set([
974
+ NLPGOV_PROFILE_MATURITY_V2.ACTIVE,
975
+ NLPGOV_PROFILE_MATURITY_V2.ARCHIVED,
976
+ ]),
977
+ ],
978
+ [NLPGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
979
+ ]);
980
+ const _nlpgovPTerminal = new Set([NLPGOV_PROFILE_MATURITY_V2.ARCHIVED]);
981
+ const _nlpgovJTrans = new Map([
982
+ [
983
+ NLPGOV_TRANSLATION_LIFECYCLE_V2.QUEUED,
984
+ new Set([
985
+ NLPGOV_TRANSLATION_LIFECYCLE_V2.TRANSLATING,
986
+ NLPGOV_TRANSLATION_LIFECYCLE_V2.CANCELLED,
987
+ ]),
988
+ ],
989
+ [
990
+ NLPGOV_TRANSLATION_LIFECYCLE_V2.TRANSLATING,
991
+ new Set([
992
+ NLPGOV_TRANSLATION_LIFECYCLE_V2.TRANSLATED,
993
+ NLPGOV_TRANSLATION_LIFECYCLE_V2.FAILED,
994
+ NLPGOV_TRANSLATION_LIFECYCLE_V2.CANCELLED,
995
+ ]),
996
+ ],
997
+ [NLPGOV_TRANSLATION_LIFECYCLE_V2.TRANSLATED, new Set()],
998
+ [NLPGOV_TRANSLATION_LIFECYCLE_V2.FAILED, new Set()],
999
+ [NLPGOV_TRANSLATION_LIFECYCLE_V2.CANCELLED, new Set()],
1000
+ ]);
1001
+ const _nlpgovPsV2 = new Map();
1002
+ const _nlpgovJsV2 = new Map();
1003
+ let _nlpgovMaxActive = 8,
1004
+ _nlpgovMaxPending = 20,
1005
+ _nlpgovIdleMs = 30 * 24 * 60 * 60 * 1000,
1006
+ _nlpgovStuckMs = 60 * 1000;
1007
+ function _nlpgovPos(n, label) {
1008
+ const v = Math.floor(Number(n));
1009
+ if (!Number.isFinite(v) || v <= 0)
1010
+ throw new Error(`${label} must be positive integer`);
1011
+ return v;
1012
+ }
1013
+ function _nlpgovCheckP(from, to) {
1014
+ const a = _nlpgovPTrans.get(from);
1015
+ if (!a || !a.has(to))
1016
+ throw new Error(`invalid nlpgov profile transition ${from} → ${to}`);
1017
+ }
1018
+ function _nlpgovCheckJ(from, to) {
1019
+ const a = _nlpgovJTrans.get(from);
1020
+ if (!a || !a.has(to))
1021
+ throw new Error(`invalid nlpgov translation transition ${from} → ${to}`);
1022
+ }
1023
+ function _nlpgovCountActive(owner) {
1024
+ let c = 0;
1025
+ for (const p of _nlpgovPsV2.values())
1026
+ if (p.owner === owner && p.status === NLPGOV_PROFILE_MATURITY_V2.ACTIVE)
1027
+ c++;
1028
+ return c;
1029
+ }
1030
+ function _nlpgovCountPending(profileId) {
1031
+ let c = 0;
1032
+ for (const j of _nlpgovJsV2.values())
1033
+ if (
1034
+ j.profileId === profileId &&
1035
+ (j.status === NLPGOV_TRANSLATION_LIFECYCLE_V2.QUEUED ||
1036
+ j.status === NLPGOV_TRANSLATION_LIFECYCLE_V2.TRANSLATING)
1037
+ )
1038
+ c++;
1039
+ return c;
1040
+ }
1041
+ export function setMaxActiveNlpgovProfilesPerOwnerV2(n) {
1042
+ _nlpgovMaxActive = _nlpgovPos(n, "maxActiveNlpgovProfilesPerOwner");
1043
+ }
1044
+ export function getMaxActiveNlpgovProfilesPerOwnerV2() {
1045
+ return _nlpgovMaxActive;
1046
+ }
1047
+ export function setMaxPendingNlpgovTranslationsPerProfileV2(n) {
1048
+ _nlpgovMaxPending = _nlpgovPos(n, "maxPendingNlpgovTranslationsPerProfile");
1049
+ }
1050
+ export function getMaxPendingNlpgovTranslationsPerProfileV2() {
1051
+ return _nlpgovMaxPending;
1052
+ }
1053
+ export function setNlpgovProfileIdleMsV2(n) {
1054
+ _nlpgovIdleMs = _nlpgovPos(n, "nlpgovProfileIdleMs");
1055
+ }
1056
+ export function getNlpgovProfileIdleMsV2() {
1057
+ return _nlpgovIdleMs;
1058
+ }
1059
+ export function setNlpgovTranslationStuckMsV2(n) {
1060
+ _nlpgovStuckMs = _nlpgovPos(n, "nlpgovTranslationStuckMs");
1061
+ }
1062
+ export function getNlpgovTranslationStuckMsV2() {
1063
+ return _nlpgovStuckMs;
1064
+ }
1065
+ export function _resetStateNlProgrammingGovV2() {
1066
+ _nlpgovPsV2.clear();
1067
+ _nlpgovJsV2.clear();
1068
+ _nlpgovMaxActive = 8;
1069
+ _nlpgovMaxPending = 20;
1070
+ _nlpgovIdleMs = 30 * 24 * 60 * 60 * 1000;
1071
+ _nlpgovStuckMs = 60 * 1000;
1072
+ }
1073
+ export function registerNlpgovProfileV2({ id, owner, style, metadata } = {}) {
1074
+ if (!id || !owner) throw new Error("id and owner required");
1075
+ if (_nlpgovPsV2.has(id))
1076
+ throw new Error(`nlpgov profile ${id} already exists`);
1077
+ const now = Date.now();
1078
+ const p = {
1079
+ id,
1080
+ owner,
1081
+ style: style || "natural",
1082
+ status: NLPGOV_PROFILE_MATURITY_V2.PENDING,
1083
+ createdAt: now,
1084
+ updatedAt: now,
1085
+ lastTouchedAt: now,
1086
+ activatedAt: null,
1087
+ archivedAt: null,
1088
+ metadata: { ...(metadata || {}) },
1089
+ };
1090
+ _nlpgovPsV2.set(id, p);
1091
+ return { ...p, metadata: { ...p.metadata } };
1092
+ }
1093
+ export function activateNlpgovProfileV2(id) {
1094
+ const p = _nlpgovPsV2.get(id);
1095
+ if (!p) throw new Error(`nlpgov profile ${id} not found`);
1096
+ const isInitial = p.status === NLPGOV_PROFILE_MATURITY_V2.PENDING;
1097
+ _nlpgovCheckP(p.status, NLPGOV_PROFILE_MATURITY_V2.ACTIVE);
1098
+ if (isInitial && _nlpgovCountActive(p.owner) >= _nlpgovMaxActive)
1099
+ throw new Error(`max active nlpgov profiles for owner ${p.owner} reached`);
1100
+ const now = Date.now();
1101
+ p.status = NLPGOV_PROFILE_MATURITY_V2.ACTIVE;
1102
+ p.updatedAt = now;
1103
+ p.lastTouchedAt = now;
1104
+ if (!p.activatedAt) p.activatedAt = now;
1105
+ return { ...p, metadata: { ...p.metadata } };
1106
+ }
1107
+ export function staleNlpgovProfileV2(id) {
1108
+ const p = _nlpgovPsV2.get(id);
1109
+ if (!p) throw new Error(`nlpgov profile ${id} not found`);
1110
+ _nlpgovCheckP(p.status, NLPGOV_PROFILE_MATURITY_V2.STALE);
1111
+ p.status = NLPGOV_PROFILE_MATURITY_V2.STALE;
1112
+ p.updatedAt = Date.now();
1113
+ return { ...p, metadata: { ...p.metadata } };
1114
+ }
1115
+ export function archiveNlpgovProfileV2(id) {
1116
+ const p = _nlpgovPsV2.get(id);
1117
+ if (!p) throw new Error(`nlpgov profile ${id} not found`);
1118
+ _nlpgovCheckP(p.status, NLPGOV_PROFILE_MATURITY_V2.ARCHIVED);
1119
+ const now = Date.now();
1120
+ p.status = NLPGOV_PROFILE_MATURITY_V2.ARCHIVED;
1121
+ p.updatedAt = now;
1122
+ if (!p.archivedAt) p.archivedAt = now;
1123
+ return { ...p, metadata: { ...p.metadata } };
1124
+ }
1125
+ export function touchNlpgovProfileV2(id) {
1126
+ const p = _nlpgovPsV2.get(id);
1127
+ if (!p) throw new Error(`nlpgov profile ${id} not found`);
1128
+ if (_nlpgovPTerminal.has(p.status))
1129
+ throw new Error(`cannot touch terminal nlpgov profile ${id}`);
1130
+ const now = Date.now();
1131
+ p.lastTouchedAt = now;
1132
+ p.updatedAt = now;
1133
+ return { ...p, metadata: { ...p.metadata } };
1134
+ }
1135
+ export function getNlpgovProfileV2(id) {
1136
+ const p = _nlpgovPsV2.get(id);
1137
+ if (!p) return null;
1138
+ return { ...p, metadata: { ...p.metadata } };
1139
+ }
1140
+ export function listNlpgovProfilesV2() {
1141
+ return [..._nlpgovPsV2.values()].map((p) => ({
1142
+ ...p,
1143
+ metadata: { ...p.metadata },
1144
+ }));
1145
+ }
1146
+ export function createNlpgovTranslationV2({
1147
+ id,
1148
+ profileId,
1149
+ intent,
1150
+ metadata,
1151
+ } = {}) {
1152
+ if (!id || !profileId) throw new Error("id and profileId required");
1153
+ if (_nlpgovJsV2.has(id))
1154
+ throw new Error(`nlpgov translation ${id} already exists`);
1155
+ if (!_nlpgovPsV2.has(profileId))
1156
+ throw new Error(`nlpgov profile ${profileId} not found`);
1157
+ if (_nlpgovCountPending(profileId) >= _nlpgovMaxPending)
1158
+ throw new Error(
1159
+ `max pending nlpgov translations for profile ${profileId} reached`,
1160
+ );
1161
+ const now = Date.now();
1162
+ const j = {
1163
+ id,
1164
+ profileId,
1165
+ intent: intent || "",
1166
+ status: NLPGOV_TRANSLATION_LIFECYCLE_V2.QUEUED,
1167
+ createdAt: now,
1168
+ updatedAt: now,
1169
+ startedAt: null,
1170
+ settledAt: null,
1171
+ metadata: { ...(metadata || {}) },
1172
+ };
1173
+ _nlpgovJsV2.set(id, j);
1174
+ return { ...j, metadata: { ...j.metadata } };
1175
+ }
1176
+ export function translatingNlpgovTranslationV2(id) {
1177
+ const j = _nlpgovJsV2.get(id);
1178
+ if (!j) throw new Error(`nlpgov translation ${id} not found`);
1179
+ _nlpgovCheckJ(j.status, NLPGOV_TRANSLATION_LIFECYCLE_V2.TRANSLATING);
1180
+ const now = Date.now();
1181
+ j.status = NLPGOV_TRANSLATION_LIFECYCLE_V2.TRANSLATING;
1182
+ j.updatedAt = now;
1183
+ if (!j.startedAt) j.startedAt = now;
1184
+ return { ...j, metadata: { ...j.metadata } };
1185
+ }
1186
+ export function completeTranslationNlpgovV2(id) {
1187
+ const j = _nlpgovJsV2.get(id);
1188
+ if (!j) throw new Error(`nlpgov translation ${id} not found`);
1189
+ _nlpgovCheckJ(j.status, NLPGOV_TRANSLATION_LIFECYCLE_V2.TRANSLATED);
1190
+ const now = Date.now();
1191
+ j.status = NLPGOV_TRANSLATION_LIFECYCLE_V2.TRANSLATED;
1192
+ j.updatedAt = now;
1193
+ if (!j.settledAt) j.settledAt = now;
1194
+ return { ...j, metadata: { ...j.metadata } };
1195
+ }
1196
+ export function failNlpgovTranslationV2(id, reason) {
1197
+ const j = _nlpgovJsV2.get(id);
1198
+ if (!j) throw new Error(`nlpgov translation ${id} not found`);
1199
+ _nlpgovCheckJ(j.status, NLPGOV_TRANSLATION_LIFECYCLE_V2.FAILED);
1200
+ const now = Date.now();
1201
+ j.status = NLPGOV_TRANSLATION_LIFECYCLE_V2.FAILED;
1202
+ j.updatedAt = now;
1203
+ if (!j.settledAt) j.settledAt = now;
1204
+ if (reason) j.metadata.failReason = String(reason);
1205
+ return { ...j, metadata: { ...j.metadata } };
1206
+ }
1207
+ export function cancelNlpgovTranslationV2(id, reason) {
1208
+ const j = _nlpgovJsV2.get(id);
1209
+ if (!j) throw new Error(`nlpgov translation ${id} not found`);
1210
+ _nlpgovCheckJ(j.status, NLPGOV_TRANSLATION_LIFECYCLE_V2.CANCELLED);
1211
+ const now = Date.now();
1212
+ j.status = NLPGOV_TRANSLATION_LIFECYCLE_V2.CANCELLED;
1213
+ j.updatedAt = now;
1214
+ if (!j.settledAt) j.settledAt = now;
1215
+ if (reason) j.metadata.cancelReason = String(reason);
1216
+ return { ...j, metadata: { ...j.metadata } };
1217
+ }
1218
+ export function getNlpgovTranslationV2(id) {
1219
+ const j = _nlpgovJsV2.get(id);
1220
+ if (!j) return null;
1221
+ return { ...j, metadata: { ...j.metadata } };
1222
+ }
1223
+ export function listNlpgovTranslationsV2() {
1224
+ return [..._nlpgovJsV2.values()].map((j) => ({
1225
+ ...j,
1226
+ metadata: { ...j.metadata },
1227
+ }));
1228
+ }
1229
+ export function autoStaleIdleNlpgovProfilesV2({ now } = {}) {
1230
+ const t = now ?? Date.now();
1231
+ const flipped = [];
1232
+ for (const p of _nlpgovPsV2.values())
1233
+ if (
1234
+ p.status === NLPGOV_PROFILE_MATURITY_V2.ACTIVE &&
1235
+ t - p.lastTouchedAt >= _nlpgovIdleMs
1236
+ ) {
1237
+ p.status = NLPGOV_PROFILE_MATURITY_V2.STALE;
1238
+ p.updatedAt = t;
1239
+ flipped.push(p.id);
1240
+ }
1241
+ return { flipped, count: flipped.length };
1242
+ }
1243
+ export function autoFailStuckNlpgovTranslationsV2({ now } = {}) {
1244
+ const t = now ?? Date.now();
1245
+ const flipped = [];
1246
+ for (const j of _nlpgovJsV2.values())
1247
+ if (
1248
+ j.status === NLPGOV_TRANSLATION_LIFECYCLE_V2.TRANSLATING &&
1249
+ j.startedAt != null &&
1250
+ t - j.startedAt >= _nlpgovStuckMs
1251
+ ) {
1252
+ j.status = NLPGOV_TRANSLATION_LIFECYCLE_V2.FAILED;
1253
+ j.updatedAt = t;
1254
+ if (!j.settledAt) j.settledAt = t;
1255
+ j.metadata.failReason = "auto-fail-stuck";
1256
+ flipped.push(j.id);
1257
+ }
1258
+ return { flipped, count: flipped.length };
1259
+ }
1260
+ export function getNlProgrammingGovStatsV2() {
1261
+ const profilesByStatus = {};
1262
+ for (const v of Object.values(NLPGOV_PROFILE_MATURITY_V2))
1263
+ profilesByStatus[v] = 0;
1264
+ for (const p of _nlpgovPsV2.values()) profilesByStatus[p.status]++;
1265
+ const translationsByStatus = {};
1266
+ for (const v of Object.values(NLPGOV_TRANSLATION_LIFECYCLE_V2))
1267
+ translationsByStatus[v] = 0;
1268
+ for (const j of _nlpgovJsV2.values()) translationsByStatus[j.status]++;
1269
+ return {
1270
+ totalNlpgovProfilesV2: _nlpgovPsV2.size,
1271
+ totalNlpgovTranslationsV2: _nlpgovJsV2.size,
1272
+ maxActiveNlpgovProfilesPerOwner: _nlpgovMaxActive,
1273
+ maxPendingNlpgovTranslationsPerProfile: _nlpgovMaxPending,
1274
+ nlpgovProfileIdleMs: _nlpgovIdleMs,
1275
+ nlpgovTranslationStuckMs: _nlpgovStuckMs,
1276
+ profilesByStatus,
1277
+ translationsByStatus,
1278
+ };
1279
+ }